blob: ba5d3fe753b694d58b93bedf2fc050f001995fef [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 Carlsonfcb389d2008-11-03 16:55:44 -0800940 phydev = tp->mdio_bus->phy_map[PHY_ADDR];
941 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
942 case TG3_PHY_ID_BCM50610:
943 val = MAC_PHYCFG2_50610_LED_MODES;
944 break;
945 case TG3_PHY_ID_BCMAC131:
946 val = MAC_PHYCFG2_AC131_LED_MODES;
947 break;
948 case TG3_PHY_ID_RTL8211C:
949 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
950 break;
951 case TG3_PHY_ID_RTL8201E:
952 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
953 break;
954 default:
Matt Carlsona9daf362008-05-25 23:49:44 -0700955 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800956 }
957
958 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
959 tw32(MAC_PHYCFG2, val);
960
961 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +0000962 val &= ~(MAC_PHYCFG1_RGMII_INT |
963 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
964 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800965 tw32(MAC_PHYCFG1, val);
966
967 return;
968 }
969
970 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_STD_IBND_DISABLE))
971 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
972 MAC_PHYCFG2_FMODE_MASK_MASK |
973 MAC_PHYCFG2_GMODE_MASK_MASK |
974 MAC_PHYCFG2_ACT_MASK_MASK |
975 MAC_PHYCFG2_QUAL_MASK_MASK |
976 MAC_PHYCFG2_INBAND_ENABLE;
977
978 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -0700979
Matt Carlsonbb85fbb2009-08-25 10:09:07 +0000980 val = tr32(MAC_PHYCFG1);
981 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
982 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
983 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_STD_IBND_DISABLE)) {
Matt Carlsona9daf362008-05-25 23:49:44 -0700984 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
985 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
986 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
987 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
988 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +0000989 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
990 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
991 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -0700992
Matt Carlsona9daf362008-05-25 23:49:44 -0700993 val = tr32(MAC_EXT_RGMII_MODE);
994 val &= ~(MAC_RGMII_MODE_RX_INT_B |
995 MAC_RGMII_MODE_RX_QUALITY |
996 MAC_RGMII_MODE_RX_ACTIVITY |
997 MAC_RGMII_MODE_RX_ENG_DET |
998 MAC_RGMII_MODE_TX_ENABLE |
999 MAC_RGMII_MODE_TX_LOWPWR |
1000 MAC_RGMII_MODE_TX_RESET);
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001001 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_STD_IBND_DISABLE)) {
Matt Carlsona9daf362008-05-25 23:49:44 -07001002 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
1003 val |= MAC_RGMII_MODE_RX_INT_B |
1004 MAC_RGMII_MODE_RX_QUALITY |
1005 MAC_RGMII_MODE_RX_ACTIVITY |
1006 MAC_RGMII_MODE_RX_ENG_DET;
1007 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
1008 val |= MAC_RGMII_MODE_TX_ENABLE |
1009 MAC_RGMII_MODE_TX_LOWPWR |
1010 MAC_RGMII_MODE_TX_RESET;
1011 }
1012 tw32(MAC_EXT_RGMII_MODE, val);
1013}
1014
Matt Carlson158d7ab2008-05-29 01:37:54 -07001015static void tg3_mdio_start(struct tg3 *tp)
1016{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001017 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1018 tw32_f(MAC_MI_MODE, tp->mi_mode);
1019 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001020
Matt Carlson882e9792009-09-01 13:21:36 +00001021 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
1022 u32 funcnum, is_serdes;
1023
1024 funcnum = tr32(TG3_CPMU_STATUS) & TG3_CPMU_STATUS_PCIE_FUNC;
1025 if (funcnum)
1026 tp->phy_addr = 2;
1027 else
1028 tp->phy_addr = 1;
1029
1030 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1031 if (is_serdes)
1032 tp->phy_addr += 7;
1033 } else
1034 tp->phy_addr = PHY_ADDR;
1035
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001036 if ((tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED) &&
1037 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1038 tg3_mdio_config_5785(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001039}
1040
Matt Carlson158d7ab2008-05-29 01:37:54 -07001041static int tg3_mdio_init(struct tg3 *tp)
1042{
1043 int i;
1044 u32 reg;
Matt Carlsona9daf362008-05-25 23:49:44 -07001045 struct phy_device *phydev;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001046
1047 tg3_mdio_start(tp);
1048
1049 if (!(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) ||
1050 (tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED))
1051 return 0;
1052
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001053 tp->mdio_bus = mdiobus_alloc();
1054 if (tp->mdio_bus == NULL)
1055 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001056
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001057 tp->mdio_bus->name = "tg3 mdio bus";
1058 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001059 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001060 tp->mdio_bus->priv = tp;
1061 tp->mdio_bus->parent = &tp->pdev->dev;
1062 tp->mdio_bus->read = &tg3_mdio_read;
1063 tp->mdio_bus->write = &tg3_mdio_write;
1064 tp->mdio_bus->reset = &tg3_mdio_reset;
1065 tp->mdio_bus->phy_mask = ~(1 << PHY_ADDR);
1066 tp->mdio_bus->irq = &tp->mdio_irq[0];
Matt Carlson158d7ab2008-05-29 01:37:54 -07001067
1068 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001069 tp->mdio_bus->irq[i] = PHY_POLL;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001070
1071 /* The bus registration will look for all the PHYs on the mdio bus.
1072 * Unfortunately, it does not ensure the PHY is powered up before
1073 * accessing the PHY ID registers. A chip reset is the
1074 * quickest way to bring the device back to an operational state..
1075 */
1076 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1077 tg3_bmcr_reset(tp);
1078
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001079 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001080 if (i) {
Matt Carlson158d7ab2008-05-29 01:37:54 -07001081 printk(KERN_WARNING "%s: mdiobus_reg failed (0x%x)\n",
1082 tp->dev->name, i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001083 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001084 return i;
1085 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001086
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001087 phydev = tp->mdio_bus->phy_map[PHY_ADDR];
Matt Carlsona9daf362008-05-25 23:49:44 -07001088
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001089 if (!phydev || !phydev->drv) {
1090 printk(KERN_WARNING "%s: No PHY devices\n", tp->dev->name);
1091 mdiobus_unregister(tp->mdio_bus);
1092 mdiobus_free(tp->mdio_bus);
1093 return -ENODEV;
1094 }
1095
1096 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson321d32a2008-11-21 17:22:19 -08001097 case TG3_PHY_ID_BCM57780:
1098 phydev->interface = PHY_INTERFACE_MODE_GMII;
1099 break;
Matt Carlsona9daf362008-05-25 23:49:44 -07001100 case TG3_PHY_ID_BCM50610:
Matt Carlsona9daf362008-05-25 23:49:44 -07001101 if (tp->tg3_flags3 & TG3_FLG3_RGMII_STD_IBND_DISABLE)
1102 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
1103 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
1104 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
1105 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
1106 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001107 /* fallthru */
1108 case TG3_PHY_ID_RTL8211C:
1109 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001110 break;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001111 case TG3_PHY_ID_RTL8201E:
Matt Carlsona9daf362008-05-25 23:49:44 -07001112 case TG3_PHY_ID_BCMAC131:
1113 phydev->interface = PHY_INTERFACE_MODE_MII;
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001114 tp->tg3_flags3 |= TG3_FLG3_PHY_IS_FET;
Matt Carlsona9daf362008-05-25 23:49:44 -07001115 break;
1116 }
1117
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001118 tp->tg3_flags3 |= TG3_FLG3_MDIOBUS_INITED;
1119
1120 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1121 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001122
1123 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001124}
1125
1126static void tg3_mdio_fini(struct tg3 *tp)
1127{
1128 if (tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED) {
1129 tp->tg3_flags3 &= ~TG3_FLG3_MDIOBUS_INITED;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001130 mdiobus_unregister(tp->mdio_bus);
1131 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001132 }
1133}
1134
Matt Carlson95e28692008-05-25 23:44:14 -07001135/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001136static inline void tg3_generate_fw_event(struct tg3 *tp)
1137{
1138 u32 val;
1139
1140 val = tr32(GRC_RX_CPU_EVENT);
1141 val |= GRC_RX_CPU_DRIVER_EVENT;
1142 tw32_f(GRC_RX_CPU_EVENT, val);
1143
1144 tp->last_event_jiffies = jiffies;
1145}
1146
1147#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1148
1149/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001150static void tg3_wait_for_event_ack(struct tg3 *tp)
1151{
1152 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001153 unsigned int delay_cnt;
1154 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001155
Matt Carlson4ba526c2008-08-15 14:10:04 -07001156 /* If enough time has passed, no wait is necessary. */
1157 time_remain = (long)(tp->last_event_jiffies + 1 +
1158 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1159 (long)jiffies;
1160 if (time_remain < 0)
1161 return;
1162
1163 /* Check if we can shorten the wait time. */
1164 delay_cnt = jiffies_to_usecs(time_remain);
1165 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1166 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1167 delay_cnt = (delay_cnt >> 3) + 1;
1168
1169 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001170 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1171 break;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001172 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001173 }
1174}
1175
1176/* tp->lock is held. */
1177static void tg3_ump_link_report(struct tg3 *tp)
1178{
1179 u32 reg;
1180 u32 val;
1181
1182 if (!(tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
1183 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
1184 return;
1185
1186 tg3_wait_for_event_ack(tp);
1187
1188 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1189
1190 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1191
1192 val = 0;
1193 if (!tg3_readphy(tp, MII_BMCR, &reg))
1194 val = reg << 16;
1195 if (!tg3_readphy(tp, MII_BMSR, &reg))
1196 val |= (reg & 0xffff);
1197 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX, val);
1198
1199 val = 0;
1200 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1201 val = reg << 16;
1202 if (!tg3_readphy(tp, MII_LPA, &reg))
1203 val |= (reg & 0xffff);
1204 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 4, val);
1205
1206 val = 0;
1207 if (!(tp->tg3_flags2 & TG3_FLG2_MII_SERDES)) {
1208 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1209 val = reg << 16;
1210 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1211 val |= (reg & 0xffff);
1212 }
1213 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 8, val);
1214
1215 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1216 val = reg << 16;
1217 else
1218 val = 0;
1219 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 12, val);
1220
Matt Carlson4ba526c2008-08-15 14:10:04 -07001221 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001222}
1223
1224static void tg3_link_report(struct tg3 *tp)
1225{
1226 if (!netif_carrier_ok(tp->dev)) {
1227 if (netif_msg_link(tp))
1228 printk(KERN_INFO PFX "%s: Link is down.\n",
1229 tp->dev->name);
1230 tg3_ump_link_report(tp);
1231 } else if (netif_msg_link(tp)) {
1232 printk(KERN_INFO PFX "%s: Link is up at %d Mbps, %s duplex.\n",
1233 tp->dev->name,
1234 (tp->link_config.active_speed == SPEED_1000 ?
1235 1000 :
1236 (tp->link_config.active_speed == SPEED_100 ?
1237 100 : 10)),
1238 (tp->link_config.active_duplex == DUPLEX_FULL ?
1239 "full" : "half"));
1240
1241 printk(KERN_INFO PFX
1242 "%s: Flow control is %s for TX and %s for RX.\n",
1243 tp->dev->name,
Steve Glendinninge18ce342008-12-16 02:00:00 -08001244 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
Matt Carlson95e28692008-05-25 23:44:14 -07001245 "on" : "off",
Steve Glendinninge18ce342008-12-16 02:00:00 -08001246 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
Matt Carlson95e28692008-05-25 23:44:14 -07001247 "on" : "off");
1248 tg3_ump_link_report(tp);
1249 }
1250}
1251
1252static u16 tg3_advert_flowctrl_1000T(u8 flow_ctrl)
1253{
1254 u16 miireg;
1255
Steve Glendinninge18ce342008-12-16 02:00:00 -08001256 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001257 miireg = ADVERTISE_PAUSE_CAP;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001258 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001259 miireg = ADVERTISE_PAUSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001260 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001261 miireg = ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1262 else
1263 miireg = 0;
1264
1265 return miireg;
1266}
1267
1268static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1269{
1270 u16 miireg;
1271
Steve Glendinninge18ce342008-12-16 02:00:00 -08001272 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001273 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001274 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001275 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001276 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001277 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1278 else
1279 miireg = 0;
1280
1281 return miireg;
1282}
1283
Matt Carlson95e28692008-05-25 23:44:14 -07001284static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1285{
1286 u8 cap = 0;
1287
1288 if (lcladv & ADVERTISE_1000XPAUSE) {
1289 if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1290 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001291 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001292 else if (rmtadv & LPA_1000XPAUSE_ASYM)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001293 cap = FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001294 } else {
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 }
1298 } else if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1299 if ((rmtadv & LPA_1000XPAUSE) && (rmtadv & LPA_1000XPAUSE_ASYM))
Steve Glendinninge18ce342008-12-16 02:00:00 -08001300 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001301 }
1302
1303 return cap;
1304}
1305
Matt Carlsonf51f3562008-05-25 23:45:08 -07001306static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001307{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001308 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001309 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001310 u32 old_rx_mode = tp->rx_mode;
1311 u32 old_tx_mode = tp->tx_mode;
1312
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001313 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001314 autoneg = tp->mdio_bus->phy_map[PHY_ADDR]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001315 else
1316 autoneg = tp->link_config.autoneg;
1317
1318 if (autoneg == AUTONEG_ENABLE &&
Matt Carlson95e28692008-05-25 23:44:14 -07001319 (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG)) {
1320 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001321 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001322 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001323 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001324 } else
1325 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001326
Matt Carlsonf51f3562008-05-25 23:45:08 -07001327 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001328
Steve Glendinninge18ce342008-12-16 02:00:00 -08001329 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001330 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1331 else
1332 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1333
Matt Carlsonf51f3562008-05-25 23:45:08 -07001334 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001335 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001336
Steve Glendinninge18ce342008-12-16 02:00:00 -08001337 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001338 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1339 else
1340 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1341
Matt Carlsonf51f3562008-05-25 23:45:08 -07001342 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001343 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001344}
1345
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001346static void tg3_adjust_link(struct net_device *dev)
1347{
1348 u8 oldflowctrl, linkmesg = 0;
1349 u32 mac_mode, lcl_adv, rmt_adv;
1350 struct tg3 *tp = netdev_priv(dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001351 struct phy_device *phydev = tp->mdio_bus->phy_map[PHY_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001352
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001353 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001354
1355 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
1356 MAC_MODE_HALF_DUPLEX);
1357
1358 oldflowctrl = tp->link_config.active_flowctrl;
1359
1360 if (phydev->link) {
1361 lcl_adv = 0;
1362 rmt_adv = 0;
1363
1364 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
1365 mac_mode |= MAC_MODE_PORT_MODE_MII;
1366 else
1367 mac_mode |= MAC_MODE_PORT_MODE_GMII;
1368
1369 if (phydev->duplex == DUPLEX_HALF)
1370 mac_mode |= MAC_MODE_HALF_DUPLEX;
1371 else {
1372 lcl_adv = tg3_advert_flowctrl_1000T(
1373 tp->link_config.flowctrl);
1374
1375 if (phydev->pause)
1376 rmt_adv = LPA_PAUSE_CAP;
1377 if (phydev->asym_pause)
1378 rmt_adv |= LPA_PAUSE_ASYM;
1379 }
1380
1381 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
1382 } else
1383 mac_mode |= MAC_MODE_PORT_MODE_GMII;
1384
1385 if (mac_mode != tp->mac_mode) {
1386 tp->mac_mode = mac_mode;
1387 tw32_f(MAC_MODE, tp->mac_mode);
1388 udelay(40);
1389 }
1390
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001391 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
1392 if (phydev->speed == SPEED_10)
1393 tw32(MAC_MI_STAT,
1394 MAC_MI_STAT_10MBPS_MODE |
1395 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1396 else
1397 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1398 }
1399
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001400 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
1401 tw32(MAC_TX_LENGTHS,
1402 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1403 (6 << TX_LENGTHS_IPG_SHIFT) |
1404 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
1405 else
1406 tw32(MAC_TX_LENGTHS,
1407 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1408 (6 << TX_LENGTHS_IPG_SHIFT) |
1409 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
1410
1411 if ((phydev->link && tp->link_config.active_speed == SPEED_INVALID) ||
1412 (!phydev->link && tp->link_config.active_speed != SPEED_INVALID) ||
1413 phydev->speed != tp->link_config.active_speed ||
1414 phydev->duplex != tp->link_config.active_duplex ||
1415 oldflowctrl != tp->link_config.active_flowctrl)
1416 linkmesg = 1;
1417
1418 tp->link_config.active_speed = phydev->speed;
1419 tp->link_config.active_duplex = phydev->duplex;
1420
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001421 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001422
1423 if (linkmesg)
1424 tg3_link_report(tp);
1425}
1426
1427static int tg3_phy_init(struct tg3 *tp)
1428{
1429 struct phy_device *phydev;
1430
1431 if (tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED)
1432 return 0;
1433
1434 /* Bring the PHY back to a known state. */
1435 tg3_bmcr_reset(tp);
1436
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001437 phydev = tp->mdio_bus->phy_map[PHY_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001438
1439 /* Attach the MAC to the PHY. */
Kay Sieversfb28ad32008-11-10 13:55:14 -08001440 phydev = phy_connect(tp->dev, dev_name(&phydev->dev), tg3_adjust_link,
Matt Carlsona9daf362008-05-25 23:49:44 -07001441 phydev->dev_flags, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001442 if (IS_ERR(phydev)) {
1443 printk(KERN_ERR "%s: Could not attach to PHY\n", tp->dev->name);
1444 return PTR_ERR(phydev);
1445 }
1446
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001447 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001448 switch (phydev->interface) {
1449 case PHY_INTERFACE_MODE_GMII:
1450 case PHY_INTERFACE_MODE_RGMII:
Matt Carlson321d32a2008-11-21 17:22:19 -08001451 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY)) {
1452 phydev->supported &= (PHY_GBIT_FEATURES |
1453 SUPPORTED_Pause |
1454 SUPPORTED_Asym_Pause);
1455 break;
1456 }
1457 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001458 case PHY_INTERFACE_MODE_MII:
1459 phydev->supported &= (PHY_BASIC_FEATURES |
1460 SUPPORTED_Pause |
1461 SUPPORTED_Asym_Pause);
1462 break;
1463 default:
1464 phy_disconnect(tp->mdio_bus->phy_map[PHY_ADDR]);
1465 return -EINVAL;
1466 }
1467
1468 tp->tg3_flags3 |= TG3_FLG3_PHY_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001469
1470 phydev->advertising = phydev->supported;
1471
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001472 return 0;
1473}
1474
1475static void tg3_phy_start(struct tg3 *tp)
1476{
1477 struct phy_device *phydev;
1478
1479 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
1480 return;
1481
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001482 phydev = tp->mdio_bus->phy_map[PHY_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001483
1484 if (tp->link_config.phy_is_low_power) {
1485 tp->link_config.phy_is_low_power = 0;
1486 phydev->speed = tp->link_config.orig_speed;
1487 phydev->duplex = tp->link_config.orig_duplex;
1488 phydev->autoneg = tp->link_config.orig_autoneg;
1489 phydev->advertising = tp->link_config.orig_advertising;
1490 }
1491
1492 phy_start(phydev);
1493
1494 phy_start_aneg(phydev);
1495}
1496
1497static void tg3_phy_stop(struct tg3 *tp)
1498{
1499 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
1500 return;
1501
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001502 phy_stop(tp->mdio_bus->phy_map[PHY_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001503}
1504
1505static void tg3_phy_fini(struct tg3 *tp)
1506{
1507 if (tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED) {
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001508 phy_disconnect(tp->mdio_bus->phy_map[PHY_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001509 tp->tg3_flags3 &= ~TG3_FLG3_PHY_CONNECTED;
1510 }
1511}
1512
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001513static void tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
1514{
1515 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1516 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
1517}
1518
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001519static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
1520{
1521 u32 phytest;
1522
1523 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
1524 u32 phy;
1525
1526 tg3_writephy(tp, MII_TG3_FET_TEST,
1527 phytest | MII_TG3_FET_SHADOW_EN);
1528 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
1529 if (enable)
1530 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
1531 else
1532 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
1533 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
1534 }
1535 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
1536 }
1537}
1538
Matt Carlson6833c042008-11-21 17:18:59 -08001539static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
1540{
1541 u32 reg;
1542
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001543 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Matt Carlson6833c042008-11-21 17:18:59 -08001544 return;
1545
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001546 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
1547 tg3_phy_fet_toggle_apd(tp, enable);
1548 return;
1549 }
1550
Matt Carlson6833c042008-11-21 17:18:59 -08001551 reg = MII_TG3_MISC_SHDW_WREN |
1552 MII_TG3_MISC_SHDW_SCR5_SEL |
1553 MII_TG3_MISC_SHDW_SCR5_LPED |
1554 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
1555 MII_TG3_MISC_SHDW_SCR5_SDTL |
1556 MII_TG3_MISC_SHDW_SCR5_C125OE;
1557 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 || !enable)
1558 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
1559
1560 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1561
1562
1563 reg = MII_TG3_MISC_SHDW_WREN |
1564 MII_TG3_MISC_SHDW_APD_SEL |
1565 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
1566 if (enable)
1567 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
1568
1569 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1570}
1571
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001572static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
1573{
1574 u32 phy;
1575
1576 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
1577 (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES))
1578 return;
1579
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001580 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001581 u32 ephy;
1582
Matt Carlson535ef6e2009-08-25 10:09:36 +00001583 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
1584 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
1585
1586 tg3_writephy(tp, MII_TG3_FET_TEST,
1587 ephy | MII_TG3_FET_SHADOW_EN);
1588 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001589 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00001590 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001591 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00001592 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
1593 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001594 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00001595 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001596 }
1597 } else {
1598 phy = MII_TG3_AUXCTL_MISC_RDSEL_MISC |
1599 MII_TG3_AUXCTL_SHDWSEL_MISC;
1600 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, phy) &&
1601 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &phy)) {
1602 if (enable)
1603 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1604 else
1605 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1606 phy |= MII_TG3_AUXCTL_MISC_WREN;
1607 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1608 }
1609 }
1610}
1611
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612static void tg3_phy_set_wirespeed(struct tg3 *tp)
1613{
1614 u32 val;
1615
1616 if (tp->tg3_flags2 & TG3_FLG2_NO_ETH_WIRE_SPEED)
1617 return;
1618
1619 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x7007) &&
1620 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &val))
1621 tg3_writephy(tp, MII_TG3_AUX_CTRL,
1622 (val | (1 << 15) | (1 << 4)));
1623}
1624
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001625static void tg3_phy_apply_otp(struct tg3 *tp)
1626{
1627 u32 otp, phy;
1628
1629 if (!tp->phy_otp)
1630 return;
1631
1632 otp = tp->phy_otp;
1633
1634 /* Enable SM_DSP clock and tx 6dB coding. */
1635 phy = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1636 MII_TG3_AUXCTL_ACTL_SMDSP_ENA |
1637 MII_TG3_AUXCTL_ACTL_TX_6DB;
1638 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1639
1640 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
1641 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
1642 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
1643
1644 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
1645 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
1646 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
1647
1648 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
1649 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
1650 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
1651
1652 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
1653 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
1654
1655 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
1656 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
1657
1658 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
1659 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
1660 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
1661
1662 /* Turn off SM_DSP clock. */
1663 phy = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1664 MII_TG3_AUXCTL_ACTL_TX_6DB;
1665 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1666}
1667
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668static int tg3_wait_macro_done(struct tg3 *tp)
1669{
1670 int limit = 100;
1671
1672 while (limit--) {
1673 u32 tmp32;
1674
1675 if (!tg3_readphy(tp, 0x16, &tmp32)) {
1676 if ((tmp32 & 0x1000) == 0)
1677 break;
1678 }
1679 }
Roel Kluind4675b52009-02-12 16:33:27 -08001680 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 return -EBUSY;
1682
1683 return 0;
1684}
1685
1686static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
1687{
1688 static const u32 test_pat[4][6] = {
1689 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
1690 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
1691 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
1692 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
1693 };
1694 int chan;
1695
1696 for (chan = 0; chan < 4; chan++) {
1697 int i;
1698
1699 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1700 (chan * 0x2000) | 0x0200);
1701 tg3_writephy(tp, 0x16, 0x0002);
1702
1703 for (i = 0; i < 6; i++)
1704 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
1705 test_pat[chan][i]);
1706
1707 tg3_writephy(tp, 0x16, 0x0202);
1708 if (tg3_wait_macro_done(tp)) {
1709 *resetp = 1;
1710 return -EBUSY;
1711 }
1712
1713 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1714 (chan * 0x2000) | 0x0200);
1715 tg3_writephy(tp, 0x16, 0x0082);
1716 if (tg3_wait_macro_done(tp)) {
1717 *resetp = 1;
1718 return -EBUSY;
1719 }
1720
1721 tg3_writephy(tp, 0x16, 0x0802);
1722 if (tg3_wait_macro_done(tp)) {
1723 *resetp = 1;
1724 return -EBUSY;
1725 }
1726
1727 for (i = 0; i < 6; i += 2) {
1728 u32 low, high;
1729
1730 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
1731 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
1732 tg3_wait_macro_done(tp)) {
1733 *resetp = 1;
1734 return -EBUSY;
1735 }
1736 low &= 0x7fff;
1737 high &= 0x000f;
1738 if (low != test_pat[chan][i] ||
1739 high != test_pat[chan][i+1]) {
1740 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
1741 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
1742 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
1743
1744 return -EBUSY;
1745 }
1746 }
1747 }
1748
1749 return 0;
1750}
1751
1752static int tg3_phy_reset_chanpat(struct tg3 *tp)
1753{
1754 int chan;
1755
1756 for (chan = 0; chan < 4; chan++) {
1757 int i;
1758
1759 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1760 (chan * 0x2000) | 0x0200);
1761 tg3_writephy(tp, 0x16, 0x0002);
1762 for (i = 0; i < 6; i++)
1763 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
1764 tg3_writephy(tp, 0x16, 0x0202);
1765 if (tg3_wait_macro_done(tp))
1766 return -EBUSY;
1767 }
1768
1769 return 0;
1770}
1771
1772static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
1773{
1774 u32 reg32, phy9_orig;
1775 int retries, do_phy_reset, err;
1776
1777 retries = 10;
1778 do_phy_reset = 1;
1779 do {
1780 if (do_phy_reset) {
1781 err = tg3_bmcr_reset(tp);
1782 if (err)
1783 return err;
1784 do_phy_reset = 0;
1785 }
1786
1787 /* Disable transmitter and interrupt. */
1788 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
1789 continue;
1790
1791 reg32 |= 0x3000;
1792 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
1793
1794 /* Set full-duplex, 1000 mbps. */
1795 tg3_writephy(tp, MII_BMCR,
1796 BMCR_FULLDPLX | TG3_BMCR_SPEED1000);
1797
1798 /* Set to master mode. */
1799 if (tg3_readphy(tp, MII_TG3_CTRL, &phy9_orig))
1800 continue;
1801
1802 tg3_writephy(tp, MII_TG3_CTRL,
1803 (MII_TG3_CTRL_AS_MASTER |
1804 MII_TG3_CTRL_ENABLE_AS_MASTER));
1805
1806 /* Enable SM_DSP_CLOCK and 6dB. */
1807 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
1808
1809 /* Block the PHY control access. */
1810 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8005);
1811 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0800);
1812
1813 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
1814 if (!err)
1815 break;
1816 } while (--retries);
1817
1818 err = tg3_phy_reset_chanpat(tp);
1819 if (err)
1820 return err;
1821
1822 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8005);
1823 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0000);
1824
1825 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
1826 tg3_writephy(tp, 0x16, 0x0000);
1827
1828 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
1829 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
1830 /* Set Extended packet length bit for jumbo frames */
1831 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4400);
1832 }
1833 else {
1834 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
1835 }
1836
1837 tg3_writephy(tp, MII_TG3_CTRL, phy9_orig);
1838
1839 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
1840 reg32 &= ~0x3000;
1841 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
1842 } else if (!err)
1843 err = -EBUSY;
1844
1845 return err;
1846}
1847
1848/* This will reset the tigon3 PHY if there is no valid
1849 * link unless the FORCE argument is non-zero.
1850 */
1851static int tg3_phy_reset(struct tg3 *tp)
1852{
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001853 u32 cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 u32 phy_status;
1855 int err;
1856
Michael Chan60189dd2006-12-17 17:08:07 -08001857 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
1858 u32 val;
1859
1860 val = tr32(GRC_MISC_CFG);
1861 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
1862 udelay(40);
1863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 err = tg3_readphy(tp, MII_BMSR, &phy_status);
1865 err |= tg3_readphy(tp, MII_BMSR, &phy_status);
1866 if (err != 0)
1867 return -EBUSY;
1868
Michael Chanc8e1e822006-04-29 18:55:17 -07001869 if (netif_running(tp->dev) && netif_carrier_ok(tp->dev)) {
1870 netif_carrier_off(tp->dev);
1871 tg3_link_report(tp);
1872 }
1873
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
1875 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
1876 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
1877 err = tg3_phy_reset_5703_4_5(tp);
1878 if (err)
1879 return err;
1880 goto out;
1881 }
1882
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001883 cpmuctrl = 0;
1884 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
1885 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
1886 cpmuctrl = tr32(TG3_CPMU_CTRL);
1887 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
1888 tw32(TG3_CPMU_CTRL,
1889 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
1890 }
1891
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 err = tg3_bmcr_reset(tp);
1893 if (err)
1894 return err;
1895
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001896 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
1897 u32 phy;
1898
1899 phy = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
1900 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, phy);
1901
1902 tw32(TG3_CPMU_CTRL, cpmuctrl);
1903 }
1904
Matt Carlsonbcb37f62008-11-03 16:52:09 -08001905 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
1906 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08001907 u32 val;
1908
1909 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
1910 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
1911 CPMU_LSPD_1000MB_MACCLK_12_5) {
1912 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
1913 udelay(40);
1914 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
1915 }
1916 }
1917
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001918 tg3_phy_apply_otp(tp);
1919
Matt Carlson6833c042008-11-21 17:18:59 -08001920 if (tp->tg3_flags3 & TG3_FLG3_PHY_ENABLE_APD)
1921 tg3_phy_toggle_apd(tp, true);
1922 else
1923 tg3_phy_toggle_apd(tp, false);
1924
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925out:
1926 if (tp->tg3_flags2 & TG3_FLG2_PHY_ADC_BUG) {
1927 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
1928 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x201f);
1929 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x2aaa);
1930 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
1931 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0323);
1932 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
1933 }
1934 if (tp->tg3_flags2 & TG3_FLG2_PHY_5704_A0_BUG) {
1935 tg3_writephy(tp, 0x1c, 0x8d68);
1936 tg3_writephy(tp, 0x1c, 0x8d68);
1937 }
1938 if (tp->tg3_flags2 & TG3_FLG2_PHY_BER_BUG) {
1939 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
1940 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
1941 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x310b);
1942 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x201f);
1943 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x9506);
1944 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x401f);
1945 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x14e2);
1946 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
1947 }
Michael Chanc424cb22006-04-29 18:56:34 -07001948 else if (tp->tg3_flags2 & TG3_FLG2_PHY_JITTER_BUG) {
1949 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
1950 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
Michael Chanc1d2a192007-01-08 19:57:20 -08001951 if (tp->tg3_flags2 & TG3_FLG2_PHY_ADJUST_TRIM) {
1952 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
1953 tg3_writephy(tp, MII_TG3_TEST1,
1954 MII_TG3_TEST1_TRIM_EN | 0x4);
1955 } else
1956 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
Michael Chanc424cb22006-04-29 18:56:34 -07001957 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
1958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 /* Set Extended packet length bit (bit 14) on all chips that */
1960 /* support jumbo frames */
1961 if ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5401) {
1962 /* Cannot do read-modify-write on 5401 */
1963 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4c20);
Matt Carlson8f666b02009-08-28 13:58:24 +00001964 } else if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 u32 phy_reg;
1966
1967 /* Set bit 14 with read-modify-write to preserve other bits */
1968 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0007) &&
1969 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &phy_reg))
1970 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy_reg | 0x4000);
1971 }
1972
1973 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
1974 * jumbo frames transmission.
1975 */
Matt Carlson8f666b02009-08-28 13:58:24 +00001976 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 u32 phy_reg;
1978
1979 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &phy_reg))
1980 tg3_writephy(tp, MII_TG3_EXT_CTRL,
1981 phy_reg | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
1982 }
1983
Michael Chan715116a2006-09-27 16:09:25 -07001984 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07001985 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00001986 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07001987 }
1988
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001989 tg3_phy_toggle_automdix(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 tg3_phy_set_wirespeed(tp);
1991 return 0;
1992}
1993
1994static void tg3_frob_aux_power(struct tg3 *tp)
1995{
1996 struct tg3 *tp_peer = tp;
1997
Michael Chan9d26e212006-12-07 00:21:14 -08001998 if ((tp->tg3_flags2 & TG3_FLG2_IS_NIC) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 return;
2000
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00002001 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2002 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
2003 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002004 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002006 dev_peer = pci_get_drvdata(tp->pdev_peer);
Michael Chanbc1c7562006-03-20 17:48:03 -08002007 /* remove_one() may have been run on the peer. */
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002008 if (!dev_peer)
Michael Chanbc1c7562006-03-20 17:48:03 -08002009 tp_peer = tp;
2010 else
2011 tp_peer = netdev_priv(dev_peer);
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002012 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
2014 if ((tp->tg3_flags & TG3_FLAG_WOL_ENABLE) != 0 ||
Michael Chan6921d202005-12-13 21:15:53 -08002015 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0 ||
2016 (tp_peer->tg3_flags & TG3_FLAG_WOL_ENABLE) != 0 ||
2017 (tp_peer->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2019 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Michael Chanb401e9e2005-12-19 16:27:04 -08002020 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2021 (GRC_LCLCTRL_GPIO_OE0 |
2022 GRC_LCLCTRL_GPIO_OE1 |
2023 GRC_LCLCTRL_GPIO_OE2 |
2024 GRC_LCLCTRL_GPIO_OUTPUT0 |
2025 GRC_LCLCTRL_GPIO_OUTPUT1),
2026 100);
Matt Carlson8d519ab2009-04-20 06:58:01 +00002027 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2028 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -07002029 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2030 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2031 GRC_LCLCTRL_GPIO_OE1 |
2032 GRC_LCLCTRL_GPIO_OE2 |
2033 GRC_LCLCTRL_GPIO_OUTPUT0 |
2034 GRC_LCLCTRL_GPIO_OUTPUT1 |
2035 tp->grc_local_ctrl;
2036 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2037
2038 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2039 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2040
2041 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2042 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 } else {
2044 u32 no_gpio2;
Michael Chandc56b7d2005-12-19 16:26:28 -08002045 u32 grc_local_ctrl = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
2047 if (tp_peer != tp &&
2048 (tp_peer->tg3_flags & TG3_FLAG_INIT_COMPLETE) != 0)
2049 return;
2050
Michael Chandc56b7d2005-12-19 16:26:28 -08002051 /* Workaround to prevent overdrawing Amps. */
2052 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2053 ASIC_REV_5714) {
2054 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chanb401e9e2005-12-19 16:27:04 -08002055 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2056 grc_local_ctrl, 100);
Michael Chandc56b7d2005-12-19 16:26:28 -08002057 }
2058
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 /* On 5753 and variants, GPIO2 cannot be used. */
2060 no_gpio2 = tp->nic_sram_data_cfg &
2061 NIC_SRAM_DATA_CFG_NO_GPIO2;
2062
Michael Chandc56b7d2005-12-19 16:26:28 -08002063 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 GRC_LCLCTRL_GPIO_OE1 |
2065 GRC_LCLCTRL_GPIO_OE2 |
2066 GRC_LCLCTRL_GPIO_OUTPUT1 |
2067 GRC_LCLCTRL_GPIO_OUTPUT2;
2068 if (no_gpio2) {
2069 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2070 GRC_LCLCTRL_GPIO_OUTPUT2);
2071 }
Michael Chanb401e9e2005-12-19 16:27:04 -08002072 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2073 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
2075 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2076
Michael Chanb401e9e2005-12-19 16:27:04 -08002077 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2078 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
2080 if (!no_gpio2) {
2081 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chanb401e9e2005-12-19 16:27:04 -08002082 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2083 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 }
2085 }
2086 } else {
2087 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
2088 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
2089 if (tp_peer != tp &&
2090 (tp_peer->tg3_flags & TG3_FLAG_INIT_COMPLETE) != 0)
2091 return;
2092
Michael Chanb401e9e2005-12-19 16:27:04 -08002093 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2094 (GRC_LCLCTRL_GPIO_OE1 |
2095 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
Michael Chanb401e9e2005-12-19 16:27:04 -08002097 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2098 GRC_LCLCTRL_GPIO_OE1, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
Michael Chanb401e9e2005-12-19 16:27:04 -08002100 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2101 (GRC_LCLCTRL_GPIO_OE1 |
2102 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 }
2104 }
2105}
2106
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002107static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2108{
2109 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2110 return 1;
2111 else if ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5411) {
2112 if (speed != SPEED_10)
2113 return 1;
2114 } else if (speed == SPEED_10)
2115 return 1;
2116
2117 return 0;
2118}
2119
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120static int tg3_setup_phy(struct tg3 *, int);
2121
2122#define RESET_KIND_SHUTDOWN 0
2123#define RESET_KIND_INIT 1
2124#define RESET_KIND_SUSPEND 2
2125
2126static void tg3_write_sig_post_reset(struct tg3 *, int);
2127static int tg3_halt_cpu(struct tg3 *, u32);
2128
Matt Carlson0a459aa2008-11-03 16:54:15 -08002129static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002130{
Matt Carlsonce057f02007-11-12 21:08:03 -08002131 u32 val;
2132
Michael Chan51297242007-02-13 12:17:57 -08002133 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
2134 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2135 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2136 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2137
2138 sg_dig_ctrl |=
2139 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2140 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2141 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2142 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002143 return;
Michael Chan51297242007-02-13 12:17:57 -08002144 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002145
Michael Chan60189dd2006-12-17 17:08:07 -08002146 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002147 tg3_bmcr_reset(tp);
2148 val = tr32(GRC_MISC_CFG);
2149 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2150 udelay(40);
2151 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002152 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07002153 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2154 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002155
2156 tg3_writephy(tp, MII_TG3_AUX_CTRL,
2157 MII_TG3_AUXCTL_SHDWSEL_PWRCTL |
2158 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2159 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2160 MII_TG3_AUXCTL_PCTL_VREG_11V);
Michael Chan715116a2006-09-27 16:09:25 -07002161 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002162
Michael Chan15c3b692006-03-22 01:06:52 -08002163 /* The PHY should not be powered down on some chips because
2164 * of bugs.
2165 */
2166 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2167 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2168 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
2169 (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)))
2170 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08002171
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002172 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2173 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002174 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2175 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2176 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2177 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2178 }
2179
Michael Chan15c3b692006-03-22 01:06:52 -08002180 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2181}
2182
Matt Carlson3f007892008-11-03 16:51:36 -08002183/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002184static int tg3_nvram_lock(struct tg3 *tp)
2185{
2186 if (tp->tg3_flags & TG3_FLAG_NVRAM) {
2187 int i;
2188
2189 if (tp->nvram_lock_cnt == 0) {
2190 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2191 for (i = 0; i < 8000; i++) {
2192 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2193 break;
2194 udelay(20);
2195 }
2196 if (i == 8000) {
2197 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2198 return -ENODEV;
2199 }
2200 }
2201 tp->nvram_lock_cnt++;
2202 }
2203 return 0;
2204}
2205
2206/* tp->lock is held. */
2207static void tg3_nvram_unlock(struct tg3 *tp)
2208{
2209 if (tp->tg3_flags & TG3_FLAG_NVRAM) {
2210 if (tp->nvram_lock_cnt > 0)
2211 tp->nvram_lock_cnt--;
2212 if (tp->nvram_lock_cnt == 0)
2213 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
2214 }
2215}
2216
2217/* tp->lock is held. */
2218static void tg3_enable_nvram_access(struct tg3 *tp)
2219{
2220 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
2221 !(tp->tg3_flags2 & TG3_FLG2_PROTECTED_NVRAM)) {
2222 u32 nvaccess = tr32(NVRAM_ACCESS);
2223
2224 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
2225 }
2226}
2227
2228/* tp->lock is held. */
2229static void tg3_disable_nvram_access(struct tg3 *tp)
2230{
2231 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
2232 !(tp->tg3_flags2 & TG3_FLG2_PROTECTED_NVRAM)) {
2233 u32 nvaccess = tr32(NVRAM_ACCESS);
2234
2235 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
2236 }
2237}
2238
2239static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
2240 u32 offset, u32 *val)
2241{
2242 u32 tmp;
2243 int i;
2244
2245 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
2246 return -EINVAL;
2247
2248 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
2249 EEPROM_ADDR_DEVID_MASK |
2250 EEPROM_ADDR_READ);
2251 tw32(GRC_EEPROM_ADDR,
2252 tmp |
2253 (0 << EEPROM_ADDR_DEVID_SHIFT) |
2254 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
2255 EEPROM_ADDR_ADDR_MASK) |
2256 EEPROM_ADDR_READ | EEPROM_ADDR_START);
2257
2258 for (i = 0; i < 1000; i++) {
2259 tmp = tr32(GRC_EEPROM_ADDR);
2260
2261 if (tmp & EEPROM_ADDR_COMPLETE)
2262 break;
2263 msleep(1);
2264 }
2265 if (!(tmp & EEPROM_ADDR_COMPLETE))
2266 return -EBUSY;
2267
Matt Carlson62cedd12009-04-20 14:52:29 -07002268 tmp = tr32(GRC_EEPROM_DATA);
2269
2270 /*
2271 * The data will always be opposite the native endian
2272 * format. Perform a blind byteswap to compensate.
2273 */
2274 *val = swab32(tmp);
2275
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002276 return 0;
2277}
2278
2279#define NVRAM_CMD_TIMEOUT 10000
2280
2281static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
2282{
2283 int i;
2284
2285 tw32(NVRAM_CMD, nvram_cmd);
2286 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
2287 udelay(10);
2288 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
2289 udelay(10);
2290 break;
2291 }
2292 }
2293
2294 if (i == NVRAM_CMD_TIMEOUT)
2295 return -EBUSY;
2296
2297 return 0;
2298}
2299
2300static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
2301{
2302 if ((tp->tg3_flags & TG3_FLAG_NVRAM) &&
2303 (tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) &&
2304 (tp->tg3_flags2 & TG3_FLG2_FLASH) &&
2305 !(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM_ADDR_TRANS) &&
2306 (tp->nvram_jedecnum == JEDEC_ATMEL))
2307
2308 addr = ((addr / tp->nvram_pagesize) <<
2309 ATMEL_AT45DB0X1B_PAGE_POS) +
2310 (addr % tp->nvram_pagesize);
2311
2312 return addr;
2313}
2314
2315static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
2316{
2317 if ((tp->tg3_flags & TG3_FLAG_NVRAM) &&
2318 (tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) &&
2319 (tp->tg3_flags2 & TG3_FLG2_FLASH) &&
2320 !(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM_ADDR_TRANS) &&
2321 (tp->nvram_jedecnum == JEDEC_ATMEL))
2322
2323 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
2324 tp->nvram_pagesize) +
2325 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
2326
2327 return addr;
2328}
2329
Matt Carlsone4f34112009-02-25 14:25:00 +00002330/* NOTE: Data read in from NVRAM is byteswapped according to
2331 * the byteswapping settings for all other register accesses.
2332 * tg3 devices are BE devices, so on a BE machine, the data
2333 * returned will be exactly as it is seen in NVRAM. On a LE
2334 * machine, the 32-bit value will be byteswapped.
2335 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002336static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
2337{
2338 int ret;
2339
2340 if (!(tp->tg3_flags & TG3_FLAG_NVRAM))
2341 return tg3_nvram_read_using_eeprom(tp, offset, val);
2342
2343 offset = tg3_nvram_phys_addr(tp, offset);
2344
2345 if (offset > NVRAM_ADDR_MSK)
2346 return -EINVAL;
2347
2348 ret = tg3_nvram_lock(tp);
2349 if (ret)
2350 return ret;
2351
2352 tg3_enable_nvram_access(tp);
2353
2354 tw32(NVRAM_ADDR, offset);
2355 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
2356 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
2357
2358 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00002359 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002360
2361 tg3_disable_nvram_access(tp);
2362
2363 tg3_nvram_unlock(tp);
2364
2365 return ret;
2366}
2367
Matt Carlsona9dc5292009-02-25 14:25:30 +00002368/* Ensures NVRAM data is in bytestream format. */
2369static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002370{
2371 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00002372 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002373 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00002374 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002375 return res;
2376}
2377
2378/* tp->lock is held. */
Matt Carlson3f007892008-11-03 16:51:36 -08002379static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
2380{
2381 u32 addr_high, addr_low;
2382 int i;
2383
2384 addr_high = ((tp->dev->dev_addr[0] << 8) |
2385 tp->dev->dev_addr[1]);
2386 addr_low = ((tp->dev->dev_addr[2] << 24) |
2387 (tp->dev->dev_addr[3] << 16) |
2388 (tp->dev->dev_addr[4] << 8) |
2389 (tp->dev->dev_addr[5] << 0));
2390 for (i = 0; i < 4; i++) {
2391 if (i == 1 && skip_mac_1)
2392 continue;
2393 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
2394 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
2395 }
2396
2397 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2398 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2399 for (i = 0; i < 12; i++) {
2400 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
2401 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
2402 }
2403 }
2404
2405 addr_high = (tp->dev->dev_addr[0] +
2406 tp->dev->dev_addr[1] +
2407 tp->dev->dev_addr[2] +
2408 tp->dev->dev_addr[3] +
2409 tp->dev->dev_addr[4] +
2410 tp->dev->dev_addr[5]) &
2411 TX_BACKOFF_SEED_MASK;
2412 tw32(MAC_TX_BACKOFF_SEED, addr_high);
2413}
2414
Michael Chanbc1c7562006-03-20 17:48:03 -08002415static int tg3_set_power_state(struct tg3 *tp, pci_power_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416{
2417 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002418 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419
2420 /* Make sure register accesses (indirect or otherwise)
2421 * will function correctly.
2422 */
2423 pci_write_config_dword(tp->pdev,
2424 TG3PCI_MISC_HOST_CTRL,
2425 tp->misc_host_ctrl);
2426
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 switch (state) {
Michael Chanbc1c7562006-03-20 17:48:03 -08002428 case PCI_D0:
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07002429 pci_enable_wake(tp->pdev, state, false);
2430 pci_set_power_state(tp->pdev, PCI_D0);
Michael Chan8c6bda12005-04-21 17:09:08 -07002431
Michael Chan9d26e212006-12-07 00:21:14 -08002432 /* Switch out of Vaux if it is a NIC */
2433 if (tp->tg3_flags2 & TG3_FLG2_IS_NIC)
Michael Chanb401e9e2005-12-19 16:27:04 -08002434 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
2436 return 0;
2437
Michael Chanbc1c7562006-03-20 17:48:03 -08002438 case PCI_D1:
Michael Chanbc1c7562006-03-20 17:48:03 -08002439 case PCI_D2:
Michael Chanbc1c7562006-03-20 17:48:03 -08002440 case PCI_D3hot:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 break;
2442
2443 default:
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07002444 printk(KERN_ERR PFX "%s: Invalid power state (D%d) requested\n",
2445 tp->dev->name, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002447 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002448
2449 /* Restore the CLKREQ setting. */
2450 if (tp->tg3_flags3 & TG3_FLG3_CLKREQ_BUG) {
2451 u16 lnkctl;
2452
2453 pci_read_config_word(tp->pdev,
2454 tp->pcie_cap + PCI_EXP_LNKCTL,
2455 &lnkctl);
2456 lnkctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
2457 pci_write_config_word(tp->pdev,
2458 tp->pcie_cap + PCI_EXP_LNKCTL,
2459 lnkctl);
2460 }
2461
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
2463 tw32(TG3PCI_MISC_HOST_CTRL,
2464 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
2465
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002466 device_should_wake = pci_pme_capable(tp->pdev, state) &&
2467 device_may_wakeup(&tp->pdev->dev) &&
2468 (tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
2469
Matt Carlsondd477002008-05-25 23:45:58 -07002470 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08002471 do_low_power = false;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002472 if ((tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED) &&
2473 !tp->link_config.phy_is_low_power) {
2474 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002475 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002476
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07002477 phydev = tp->mdio_bus->phy_map[PHY_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002478
2479 tp->link_config.phy_is_low_power = 1;
2480
2481 tp->link_config.orig_speed = phydev->speed;
2482 tp->link_config.orig_duplex = phydev->duplex;
2483 tp->link_config.orig_autoneg = phydev->autoneg;
2484 tp->link_config.orig_advertising = phydev->advertising;
2485
2486 advertising = ADVERTISED_TP |
2487 ADVERTISED_Pause |
2488 ADVERTISED_Autoneg |
2489 ADVERTISED_10baseT_Half;
2490
2491 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002492 device_should_wake) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002493 if (tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB)
2494 advertising |=
2495 ADVERTISED_100baseT_Half |
2496 ADVERTISED_100baseT_Full |
2497 ADVERTISED_10baseT_Full;
2498 else
2499 advertising |= ADVERTISED_10baseT_Full;
2500 }
2501
2502 phydev->advertising = advertising;
2503
2504 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002505
2506 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
2507 if (phyid != TG3_PHY_ID_BCMAC131) {
2508 phyid &= TG3_PHY_OUI_MASK;
Roel Kluinf72b5342009-02-18 17:42:42 -08002509 if (phyid == TG3_PHY_OUI_1 ||
2510 phyid == TG3_PHY_OUI_2 ||
Matt Carlson0a459aa2008-11-03 16:54:15 -08002511 phyid == TG3_PHY_OUI_3)
2512 do_low_power = true;
2513 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002514 }
Matt Carlsondd477002008-05-25 23:45:58 -07002515 } else {
Matt Carlson20232762008-12-21 20:18:56 -08002516 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002517
Matt Carlsondd477002008-05-25 23:45:58 -07002518 if (tp->link_config.phy_is_low_power == 0) {
2519 tp->link_config.phy_is_low_power = 1;
2520 tp->link_config.orig_speed = tp->link_config.speed;
2521 tp->link_config.orig_duplex = tp->link_config.duplex;
2522 tp->link_config.orig_autoneg = tp->link_config.autoneg;
2523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524
Matt Carlsondd477002008-05-25 23:45:58 -07002525 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)) {
2526 tp->link_config.speed = SPEED_10;
2527 tp->link_config.duplex = DUPLEX_HALF;
2528 tp->link_config.autoneg = AUTONEG_ENABLE;
2529 tg3_setup_phy(tp, 0);
2530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 }
2532
Michael Chanb5d37722006-09-27 16:06:21 -07002533 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
2534 u32 val;
2535
2536 val = tr32(GRC_VCPU_EXT_CTRL);
2537 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
2538 } else if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08002539 int i;
2540 u32 val;
2541
2542 for (i = 0; i < 200; i++) {
2543 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
2544 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
2545 break;
2546 msleep(1);
2547 }
2548 }
Gary Zambranoa85feb82007-05-05 11:52:19 -07002549 if (tp->tg3_flags & TG3_FLAG_WOL_CAP)
2550 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
2551 WOL_DRV_STATE_SHUTDOWN |
2552 WOL_DRV_WOL |
2553 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08002554
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002555 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 u32 mac_mode;
2557
2558 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08002559 if (do_low_power) {
Matt Carlsondd477002008-05-25 23:45:58 -07002560 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x5a);
2561 udelay(40);
2562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563
Michael Chan3f7045c2006-09-27 16:02:29 -07002564 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)
2565 mac_mode = MAC_MODE_PORT_MODE_GMII;
2566 else
2567 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002569 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
2570 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2571 ASIC_REV_5700) {
2572 u32 speed = (tp->tg3_flags &
2573 TG3_FLAG_WOL_SPEED_100MB) ?
2574 SPEED_100 : SPEED_10;
2575 if (tg3_5700_link_polarity(tp, speed))
2576 mac_mode |= MAC_MODE_LINK_POLARITY;
2577 else
2578 mac_mode &= ~MAC_MODE_LINK_POLARITY;
2579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 } else {
2581 mac_mode = MAC_MODE_PORT_MODE_TBI;
2582 }
2583
John W. Linvillecbf46852005-04-21 17:01:29 -07002584 if (!(tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 tw32(MAC_LED_CTRL, tp->led_ctrl);
2586
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002587 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
2588 if (((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
2589 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) &&
2590 ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
2591 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)))
2592 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593
Matt Carlson3bda1252008-08-15 14:08:22 -07002594 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
2595 mac_mode |= tp->mac_mode &
2596 (MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN);
2597 if (mac_mode & MAC_MODE_APE_TX_EN)
2598 mac_mode |= MAC_MODE_TDE_ENABLE;
2599 }
2600
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 tw32_f(MAC_MODE, mac_mode);
2602 udelay(100);
2603
2604 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
2605 udelay(10);
2606 }
2607
2608 if (!(tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB) &&
2609 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2610 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
2611 u32 base_val;
2612
2613 base_val = tp->pci_clock_ctrl;
2614 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
2615 CLOCK_CTRL_TXCLK_DISABLE);
2616
Michael Chanb401e9e2005-12-19 16:27:04 -08002617 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
2618 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Michael Chand7b0a852007-02-13 12:17:38 -08002619 } else if ((tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
Matt Carlson795d01c2007-10-07 23:28:17 -07002620 (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
Michael Chand7b0a852007-02-13 12:17:38 -08002621 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)) {
Michael Chan4cf78e42005-07-25 12:29:19 -07002622 /* do nothing */
Michael Chan85e94ce2005-04-21 17:05:28 -07002623 } else if (!((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF))) {
2625 u32 newbits1, newbits2;
2626
2627 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2628 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2629 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
2630 CLOCK_CTRL_TXCLK_DISABLE |
2631 CLOCK_CTRL_ALTCLK);
2632 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2633 } else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
2634 newbits1 = CLOCK_CTRL_625_CORE;
2635 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
2636 } else {
2637 newbits1 = CLOCK_CTRL_ALTCLK;
2638 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2639 }
2640
Michael Chanb401e9e2005-12-19 16:27:04 -08002641 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
2642 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643
Michael Chanb401e9e2005-12-19 16:27:04 -08002644 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
2645 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646
2647 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
2648 u32 newbits3;
2649
2650 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2651 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2652 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
2653 CLOCK_CTRL_TXCLK_DISABLE |
2654 CLOCK_CTRL_44MHZ_CORE);
2655 } else {
2656 newbits3 = CLOCK_CTRL_44MHZ_CORE;
2657 }
2658
Michael Chanb401e9e2005-12-19 16:27:04 -08002659 tw32_wait_f(TG3PCI_CLOCK_CTRL,
2660 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 }
2662 }
2663
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002664 if (!(device_should_wake) &&
Matt Carlson22435842008-11-21 17:21:13 -08002665 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08002666 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08002667
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 tg3_frob_aux_power(tp);
2669
2670 /* Workaround for unstable PLL clock */
2671 if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
2672 (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
2673 u32 val = tr32(0x7d00);
2674
2675 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
2676 tw32(0x7d00, val);
Michael Chan6921d202005-12-13 21:15:53 -08002677 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08002678 int err;
2679
2680 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08002682 if (!err)
2683 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08002684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 }
2686
Michael Chanbbadf502006-04-06 21:46:34 -07002687 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
2688
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002689 if (device_should_wake)
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07002690 pci_enable_wake(tp->pdev, state, true);
2691
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 /* Finally, set the new power state. */
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07002693 pci_set_power_state(tp->pdev, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 return 0;
2696}
2697
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
2699{
2700 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
2701 case MII_TG3_AUX_STAT_10HALF:
2702 *speed = SPEED_10;
2703 *duplex = DUPLEX_HALF;
2704 break;
2705
2706 case MII_TG3_AUX_STAT_10FULL:
2707 *speed = SPEED_10;
2708 *duplex = DUPLEX_FULL;
2709 break;
2710
2711 case MII_TG3_AUX_STAT_100HALF:
2712 *speed = SPEED_100;
2713 *duplex = DUPLEX_HALF;
2714 break;
2715
2716 case MII_TG3_AUX_STAT_100FULL:
2717 *speed = SPEED_100;
2718 *duplex = DUPLEX_FULL;
2719 break;
2720
2721 case MII_TG3_AUX_STAT_1000HALF:
2722 *speed = SPEED_1000;
2723 *duplex = DUPLEX_HALF;
2724 break;
2725
2726 case MII_TG3_AUX_STAT_1000FULL:
2727 *speed = SPEED_1000;
2728 *duplex = DUPLEX_FULL;
2729 break;
2730
2731 default:
Matt Carlson7f97a4b2009-08-25 10:10:03 +00002732 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07002733 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
2734 SPEED_10;
2735 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
2736 DUPLEX_HALF;
2737 break;
2738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 *speed = SPEED_INVALID;
2740 *duplex = DUPLEX_INVALID;
2741 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743}
2744
2745static void tg3_phy_copper_begin(struct tg3 *tp)
2746{
2747 u32 new_adv;
2748 int i;
2749
2750 if (tp->link_config.phy_is_low_power) {
2751 /* Entering low power mode. Disable gigabit and
2752 * 100baseT advertisements.
2753 */
2754 tg3_writephy(tp, MII_TG3_CTRL, 0);
2755
2756 new_adv = (ADVERTISE_10HALF | ADVERTISE_10FULL |
2757 ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
2758 if (tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB)
2759 new_adv |= (ADVERTISE_100HALF | ADVERTISE_100FULL);
2760
2761 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2762 } else if (tp->link_config.speed == SPEED_INVALID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 if (tp->tg3_flags & TG3_FLAG_10_100_ONLY)
2764 tp->link_config.advertising &=
2765 ~(ADVERTISED_1000baseT_Half |
2766 ADVERTISED_1000baseT_Full);
2767
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002768 new_adv = ADVERTISE_CSMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 if (tp->link_config.advertising & ADVERTISED_10baseT_Half)
2770 new_adv |= ADVERTISE_10HALF;
2771 if (tp->link_config.advertising & ADVERTISED_10baseT_Full)
2772 new_adv |= ADVERTISE_10FULL;
2773 if (tp->link_config.advertising & ADVERTISED_100baseT_Half)
2774 new_adv |= ADVERTISE_100HALF;
2775 if (tp->link_config.advertising & ADVERTISED_100baseT_Full)
2776 new_adv |= ADVERTISE_100FULL;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002777
2778 new_adv |= tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2779
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2781
2782 if (tp->link_config.advertising &
2783 (ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full)) {
2784 new_adv = 0;
2785 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
2786 new_adv |= MII_TG3_CTRL_ADV_1000_HALF;
2787 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
2788 new_adv |= MII_TG3_CTRL_ADV_1000_FULL;
2789 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY) &&
2790 (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2791 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0))
2792 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2793 MII_TG3_CTRL_ENABLE_AS_MASTER);
2794 tg3_writephy(tp, MII_TG3_CTRL, new_adv);
2795 } else {
2796 tg3_writephy(tp, MII_TG3_CTRL, 0);
2797 }
2798 } else {
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002799 new_adv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2800 new_adv |= ADVERTISE_CSMA;
2801
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 /* Asking for a specific link mode. */
2803 if (tp->link_config.speed == SPEED_1000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2805
2806 if (tp->link_config.duplex == DUPLEX_FULL)
2807 new_adv = MII_TG3_CTRL_ADV_1000_FULL;
2808 else
2809 new_adv = MII_TG3_CTRL_ADV_1000_HALF;
2810 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2811 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
2812 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2813 MII_TG3_CTRL_ENABLE_AS_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 if (tp->link_config.speed == SPEED_100) {
2816 if (tp->link_config.duplex == DUPLEX_FULL)
2817 new_adv |= ADVERTISE_100FULL;
2818 else
2819 new_adv |= ADVERTISE_100HALF;
2820 } else {
2821 if (tp->link_config.duplex == DUPLEX_FULL)
2822 new_adv |= ADVERTISE_10FULL;
2823 else
2824 new_adv |= ADVERTISE_10HALF;
2825 }
2826 tg3_writephy(tp, MII_ADVERTISE, new_adv);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002827
2828 new_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002830
2831 tg3_writephy(tp, MII_TG3_CTRL, new_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 }
2833
2834 if (tp->link_config.autoneg == AUTONEG_DISABLE &&
2835 tp->link_config.speed != SPEED_INVALID) {
2836 u32 bmcr, orig_bmcr;
2837
2838 tp->link_config.active_speed = tp->link_config.speed;
2839 tp->link_config.active_duplex = tp->link_config.duplex;
2840
2841 bmcr = 0;
2842 switch (tp->link_config.speed) {
2843 default:
2844 case SPEED_10:
2845 break;
2846
2847 case SPEED_100:
2848 bmcr |= BMCR_SPEED100;
2849 break;
2850
2851 case SPEED_1000:
2852 bmcr |= TG3_BMCR_SPEED1000;
2853 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855
2856 if (tp->link_config.duplex == DUPLEX_FULL)
2857 bmcr |= BMCR_FULLDPLX;
2858
2859 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
2860 (bmcr != orig_bmcr)) {
2861 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
2862 for (i = 0; i < 1500; i++) {
2863 u32 tmp;
2864
2865 udelay(10);
2866 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
2867 tg3_readphy(tp, MII_BMSR, &tmp))
2868 continue;
2869 if (!(tmp & BMSR_LSTATUS)) {
2870 udelay(40);
2871 break;
2872 }
2873 }
2874 tg3_writephy(tp, MII_BMCR, bmcr);
2875 udelay(40);
2876 }
2877 } else {
2878 tg3_writephy(tp, MII_BMCR,
2879 BMCR_ANENABLE | BMCR_ANRESTART);
2880 }
2881}
2882
2883static int tg3_init_5401phy_dsp(struct tg3 *tp)
2884{
2885 int err;
2886
2887 /* Turn off tap power management. */
2888 /* Set Extended packet length bit */
2889 err = tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4c20);
2890
2891 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x0012);
2892 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x1804);
2893
2894 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x0013);
2895 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x1204);
2896
2897 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8006);
2898 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0132);
2899
2900 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8006);
2901 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0232);
2902
2903 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x201f);
2904 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0a20);
2905
2906 udelay(40);
2907
2908 return err;
2909}
2910
Michael Chan3600d912006-12-07 00:21:48 -08002911static int tg3_copper_is_advertising_all(struct tg3 *tp, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912{
Michael Chan3600d912006-12-07 00:21:48 -08002913 u32 adv_reg, all_mask = 0;
2914
2915 if (mask & ADVERTISED_10baseT_Half)
2916 all_mask |= ADVERTISE_10HALF;
2917 if (mask & ADVERTISED_10baseT_Full)
2918 all_mask |= ADVERTISE_10FULL;
2919 if (mask & ADVERTISED_100baseT_Half)
2920 all_mask |= ADVERTISE_100HALF;
2921 if (mask & ADVERTISED_100baseT_Full)
2922 all_mask |= ADVERTISE_100FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923
2924 if (tg3_readphy(tp, MII_ADVERTISE, &adv_reg))
2925 return 0;
2926
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 if ((adv_reg & all_mask) != all_mask)
2928 return 0;
2929 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY)) {
2930 u32 tg3_ctrl;
2931
Michael Chan3600d912006-12-07 00:21:48 -08002932 all_mask = 0;
2933 if (mask & ADVERTISED_1000baseT_Half)
2934 all_mask |= ADVERTISE_1000HALF;
2935 if (mask & ADVERTISED_1000baseT_Full)
2936 all_mask |= ADVERTISE_1000FULL;
2937
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 if (tg3_readphy(tp, MII_TG3_CTRL, &tg3_ctrl))
2939 return 0;
2940
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 if ((tg3_ctrl & all_mask) != all_mask)
2942 return 0;
2943 }
2944 return 1;
2945}
2946
Matt Carlsonef167e22007-12-20 20:10:01 -08002947static int tg3_adv_1000T_flowctrl_ok(struct tg3 *tp, u32 *lcladv, u32 *rmtadv)
2948{
2949 u32 curadv, reqadv;
2950
2951 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
2952 return 1;
2953
2954 curadv = *lcladv & (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
2955 reqadv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2956
2957 if (tp->link_config.active_duplex == DUPLEX_FULL) {
2958 if (curadv != reqadv)
2959 return 0;
2960
2961 if (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG)
2962 tg3_readphy(tp, MII_LPA, rmtadv);
2963 } else {
2964 /* Reprogram the advertisement register, even if it
2965 * does not affect the current link. If the link
2966 * gets renegotiated in the future, we can save an
2967 * additional renegotiation cycle by advertising
2968 * it correctly in the first place.
2969 */
2970 if (curadv != reqadv) {
2971 *lcladv &= ~(ADVERTISE_PAUSE_CAP |
2972 ADVERTISE_PAUSE_ASYM);
2973 tg3_writephy(tp, MII_ADVERTISE, *lcladv | reqadv);
2974 }
2975 }
2976
2977 return 1;
2978}
2979
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
2981{
2982 int current_link_up;
2983 u32 bmsr, dummy;
Matt Carlsonef167e22007-12-20 20:10:01 -08002984 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 u16 current_speed;
2986 u8 current_duplex;
2987 int i, err;
2988
2989 tw32(MAC_EVENT, 0);
2990
2991 tw32_f(MAC_STATUS,
2992 (MAC_STATUS_SYNC_CHANGED |
2993 MAC_STATUS_CFG_CHANGED |
2994 MAC_STATUS_MI_COMPLETION |
2995 MAC_STATUS_LNKSTATE_CHANGED));
2996 udelay(40);
2997
Matt Carlson8ef21422008-05-02 16:47:53 -07002998 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
2999 tw32_f(MAC_MI_MODE,
3000 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
3001 udelay(80);
3002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003
3004 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x02);
3005
3006 /* Some third-party PHYs need to be reset on link going
3007 * down.
3008 */
3009 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3010 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
3011 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
3012 netif_carrier_ok(tp->dev)) {
3013 tg3_readphy(tp, MII_BMSR, &bmsr);
3014 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3015 !(bmsr & BMSR_LSTATUS))
3016 force_reset = 1;
3017 }
3018 if (force_reset)
3019 tg3_phy_reset(tp);
3020
3021 if ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5401) {
3022 tg3_readphy(tp, MII_BMSR, &bmsr);
3023 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
3024 !(tp->tg3_flags & TG3_FLAG_INIT_COMPLETE))
3025 bmsr = 0;
3026
3027 if (!(bmsr & BMSR_LSTATUS)) {
3028 err = tg3_init_5401phy_dsp(tp);
3029 if (err)
3030 return err;
3031
3032 tg3_readphy(tp, MII_BMSR, &bmsr);
3033 for (i = 0; i < 1000; i++) {
3034 udelay(10);
3035 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3036 (bmsr & BMSR_LSTATUS)) {
3037 udelay(40);
3038 break;
3039 }
3040 }
3041
3042 if ((tp->phy_id & PHY_ID_REV_MASK) == PHY_REV_BCM5401_B0 &&
3043 !(bmsr & BMSR_LSTATUS) &&
3044 tp->link_config.active_speed == SPEED_1000) {
3045 err = tg3_phy_reset(tp);
3046 if (!err)
3047 err = tg3_init_5401phy_dsp(tp);
3048 if (err)
3049 return err;
3050 }
3051 }
3052 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3053 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
3054 /* 5701 {A0,B0} CRC bug workaround */
3055 tg3_writephy(tp, 0x15, 0x0a75);
3056 tg3_writephy(tp, 0x1c, 0x8c68);
3057 tg3_writephy(tp, 0x1c, 0x8d68);
3058 tg3_writephy(tp, 0x1c, 0x8c68);
3059 }
3060
3061 /* Clear pending interrupts... */
3062 tg3_readphy(tp, MII_TG3_ISTAT, &dummy);
3063 tg3_readphy(tp, MII_TG3_ISTAT, &dummy);
3064
3065 if (tp->tg3_flags & TG3_FLAG_USE_MI_INTERRUPT)
3066 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlson7f97a4b2009-08-25 10:10:03 +00003067 else if (!(tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 tg3_writephy(tp, MII_TG3_IMASK, ~0);
3069
3070 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3071 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3072 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
3073 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3074 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
3075 else
3076 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
3077 }
3078
3079 current_link_up = 0;
3080 current_speed = SPEED_INVALID;
3081 current_duplex = DUPLEX_INVALID;
3082
3083 if (tp->tg3_flags2 & TG3_FLG2_CAPACITIVE_COUPLING) {
3084 u32 val;
3085
3086 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4007);
3087 tg3_readphy(tp, MII_TG3_AUX_CTRL, &val);
3088 if (!(val & (1 << 10))) {
3089 val |= (1 << 10);
3090 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
3091 goto relink;
3092 }
3093 }
3094
3095 bmsr = 0;
3096 for (i = 0; i < 100; i++) {
3097 tg3_readphy(tp, MII_BMSR, &bmsr);
3098 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3099 (bmsr & BMSR_LSTATUS))
3100 break;
3101 udelay(40);
3102 }
3103
3104 if (bmsr & BMSR_LSTATUS) {
3105 u32 aux_stat, bmcr;
3106
3107 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
3108 for (i = 0; i < 2000; i++) {
3109 udelay(10);
3110 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
3111 aux_stat)
3112 break;
3113 }
3114
3115 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
3116 &current_speed,
3117 &current_duplex);
3118
3119 bmcr = 0;
3120 for (i = 0; i < 200; i++) {
3121 tg3_readphy(tp, MII_BMCR, &bmcr);
3122 if (tg3_readphy(tp, MII_BMCR, &bmcr))
3123 continue;
3124 if (bmcr && bmcr != 0x7fff)
3125 break;
3126 udelay(10);
3127 }
3128
Matt Carlsonef167e22007-12-20 20:10:01 -08003129 lcl_adv = 0;
3130 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131
Matt Carlsonef167e22007-12-20 20:10:01 -08003132 tp->link_config.active_speed = current_speed;
3133 tp->link_config.active_duplex = current_duplex;
3134
3135 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3136 if ((bmcr & BMCR_ANENABLE) &&
3137 tg3_copper_is_advertising_all(tp,
3138 tp->link_config.advertising)) {
3139 if (tg3_adv_1000T_flowctrl_ok(tp, &lcl_adv,
3140 &rmt_adv))
3141 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 }
3143 } else {
3144 if (!(bmcr & BMCR_ANENABLE) &&
3145 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08003146 tp->link_config.duplex == current_duplex &&
3147 tp->link_config.flowctrl ==
3148 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 }
3151 }
3152
Matt Carlsonef167e22007-12-20 20:10:01 -08003153 if (current_link_up == 1 &&
3154 tp->link_config.active_duplex == DUPLEX_FULL)
3155 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 }
3157
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158relink:
Michael Chan6921d202005-12-13 21:15:53 -08003159 if (current_link_up == 0 || tp->link_config.phy_is_low_power) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 u32 tmp;
3161
3162 tg3_phy_copper_begin(tp);
3163
3164 tg3_readphy(tp, MII_BMSR, &tmp);
3165 if (!tg3_readphy(tp, MII_BMSR, &tmp) &&
3166 (tmp & BMSR_LSTATUS))
3167 current_link_up = 1;
3168 }
3169
3170 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
3171 if (current_link_up == 1) {
3172 if (tp->link_config.active_speed == SPEED_100 ||
3173 tp->link_config.active_speed == SPEED_10)
3174 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3175 else
3176 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlson7f97a4b2009-08-25 10:10:03 +00003177 } else if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET)
3178 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3179 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
3181
3182 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
3183 if (tp->link_config.active_duplex == DUPLEX_HALF)
3184 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
3185
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003187 if (current_link_up == 1 &&
3188 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003190 else
3191 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 }
3193
3194 /* ??? Without this setting Netgear GA302T PHY does not
3195 * ??? send/receive packets...
3196 */
3197 if ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5411 &&
3198 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
3199 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
3200 tw32_f(MAC_MI_MODE, tp->mi_mode);
3201 udelay(80);
3202 }
3203
3204 tw32_f(MAC_MODE, tp->mac_mode);
3205 udelay(40);
3206
3207 if (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) {
3208 /* Polled via timer. */
3209 tw32_f(MAC_EVENT, 0);
3210 } else {
3211 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
3212 }
3213 udelay(40);
3214
3215 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
3216 current_link_up == 1 &&
3217 tp->link_config.active_speed == SPEED_1000 &&
3218 ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) ||
3219 (tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED))) {
3220 udelay(120);
3221 tw32_f(MAC_STATUS,
3222 (MAC_STATUS_SYNC_CHANGED |
3223 MAC_STATUS_CFG_CHANGED));
3224 udelay(40);
3225 tg3_write_mem(tp,
3226 NIC_SRAM_FIRMWARE_MBOX,
3227 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
3228 }
3229
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003230 /* Prevent send BD corruption. */
3231 if (tp->tg3_flags3 & TG3_FLG3_CLKREQ_BUG) {
3232 u16 oldlnkctl, newlnkctl;
3233
3234 pci_read_config_word(tp->pdev,
3235 tp->pcie_cap + PCI_EXP_LNKCTL,
3236 &oldlnkctl);
3237 if (tp->link_config.active_speed == SPEED_100 ||
3238 tp->link_config.active_speed == SPEED_10)
3239 newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
3240 else
3241 newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
3242 if (newlnkctl != oldlnkctl)
3243 pci_write_config_word(tp->pdev,
3244 tp->pcie_cap + PCI_EXP_LNKCTL,
3245 newlnkctl);
Matt Carlson255ca312009-08-25 10:07:27 +00003246 } else if (tp->tg3_flags3 & TG3_FLG3_TOGGLE_10_100_L1PLLPD) {
3247 u32 newreg, oldreg = tr32(TG3_PCIE_LNKCTL);
3248 if (tp->link_config.active_speed == SPEED_100 ||
3249 tp->link_config.active_speed == SPEED_10)
3250 newreg = oldreg & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
3251 else
3252 newreg = oldreg | TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
3253 if (newreg != oldreg)
3254 tw32(TG3_PCIE_LNKCTL, newreg);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003255 }
3256
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 if (current_link_up != netif_carrier_ok(tp->dev)) {
3258 if (current_link_up)
3259 netif_carrier_on(tp->dev);
3260 else
3261 netif_carrier_off(tp->dev);
3262 tg3_link_report(tp);
3263 }
3264
3265 return 0;
3266}
3267
3268struct tg3_fiber_aneginfo {
3269 int state;
3270#define ANEG_STATE_UNKNOWN 0
3271#define ANEG_STATE_AN_ENABLE 1
3272#define ANEG_STATE_RESTART_INIT 2
3273#define ANEG_STATE_RESTART 3
3274#define ANEG_STATE_DISABLE_LINK_OK 4
3275#define ANEG_STATE_ABILITY_DETECT_INIT 5
3276#define ANEG_STATE_ABILITY_DETECT 6
3277#define ANEG_STATE_ACK_DETECT_INIT 7
3278#define ANEG_STATE_ACK_DETECT 8
3279#define ANEG_STATE_COMPLETE_ACK_INIT 9
3280#define ANEG_STATE_COMPLETE_ACK 10
3281#define ANEG_STATE_IDLE_DETECT_INIT 11
3282#define ANEG_STATE_IDLE_DETECT 12
3283#define ANEG_STATE_LINK_OK 13
3284#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
3285#define ANEG_STATE_NEXT_PAGE_WAIT 15
3286
3287 u32 flags;
3288#define MR_AN_ENABLE 0x00000001
3289#define MR_RESTART_AN 0x00000002
3290#define MR_AN_COMPLETE 0x00000004
3291#define MR_PAGE_RX 0x00000008
3292#define MR_NP_LOADED 0x00000010
3293#define MR_TOGGLE_TX 0x00000020
3294#define MR_LP_ADV_FULL_DUPLEX 0x00000040
3295#define MR_LP_ADV_HALF_DUPLEX 0x00000080
3296#define MR_LP_ADV_SYM_PAUSE 0x00000100
3297#define MR_LP_ADV_ASYM_PAUSE 0x00000200
3298#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
3299#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
3300#define MR_LP_ADV_NEXT_PAGE 0x00001000
3301#define MR_TOGGLE_RX 0x00002000
3302#define MR_NP_RX 0x00004000
3303
3304#define MR_LINK_OK 0x80000000
3305
3306 unsigned long link_time, cur_time;
3307
3308 u32 ability_match_cfg;
3309 int ability_match_count;
3310
3311 char ability_match, idle_match, ack_match;
3312
3313 u32 txconfig, rxconfig;
3314#define ANEG_CFG_NP 0x00000080
3315#define ANEG_CFG_ACK 0x00000040
3316#define ANEG_CFG_RF2 0x00000020
3317#define ANEG_CFG_RF1 0x00000010
3318#define ANEG_CFG_PS2 0x00000001
3319#define ANEG_CFG_PS1 0x00008000
3320#define ANEG_CFG_HD 0x00004000
3321#define ANEG_CFG_FD 0x00002000
3322#define ANEG_CFG_INVAL 0x00001f06
3323
3324};
3325#define ANEG_OK 0
3326#define ANEG_DONE 1
3327#define ANEG_TIMER_ENAB 2
3328#define ANEG_FAILED -1
3329
3330#define ANEG_STATE_SETTLE_TIME 10000
3331
3332static int tg3_fiber_aneg_smachine(struct tg3 *tp,
3333 struct tg3_fiber_aneginfo *ap)
3334{
Matt Carlson5be73b42007-12-20 20:09:29 -08003335 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 unsigned long delta;
3337 u32 rx_cfg_reg;
3338 int ret;
3339
3340 if (ap->state == ANEG_STATE_UNKNOWN) {
3341 ap->rxconfig = 0;
3342 ap->link_time = 0;
3343 ap->cur_time = 0;
3344 ap->ability_match_cfg = 0;
3345 ap->ability_match_count = 0;
3346 ap->ability_match = 0;
3347 ap->idle_match = 0;
3348 ap->ack_match = 0;
3349 }
3350 ap->cur_time++;
3351
3352 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
3353 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
3354
3355 if (rx_cfg_reg != ap->ability_match_cfg) {
3356 ap->ability_match_cfg = rx_cfg_reg;
3357 ap->ability_match = 0;
3358 ap->ability_match_count = 0;
3359 } else {
3360 if (++ap->ability_match_count > 1) {
3361 ap->ability_match = 1;
3362 ap->ability_match_cfg = rx_cfg_reg;
3363 }
3364 }
3365 if (rx_cfg_reg & ANEG_CFG_ACK)
3366 ap->ack_match = 1;
3367 else
3368 ap->ack_match = 0;
3369
3370 ap->idle_match = 0;
3371 } else {
3372 ap->idle_match = 1;
3373 ap->ability_match_cfg = 0;
3374 ap->ability_match_count = 0;
3375 ap->ability_match = 0;
3376 ap->ack_match = 0;
3377
3378 rx_cfg_reg = 0;
3379 }
3380
3381 ap->rxconfig = rx_cfg_reg;
3382 ret = ANEG_OK;
3383
3384 switch(ap->state) {
3385 case ANEG_STATE_UNKNOWN:
3386 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
3387 ap->state = ANEG_STATE_AN_ENABLE;
3388
3389 /* fallthru */
3390 case ANEG_STATE_AN_ENABLE:
3391 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
3392 if (ap->flags & MR_AN_ENABLE) {
3393 ap->link_time = 0;
3394 ap->cur_time = 0;
3395 ap->ability_match_cfg = 0;
3396 ap->ability_match_count = 0;
3397 ap->ability_match = 0;
3398 ap->idle_match = 0;
3399 ap->ack_match = 0;
3400
3401 ap->state = ANEG_STATE_RESTART_INIT;
3402 } else {
3403 ap->state = ANEG_STATE_DISABLE_LINK_OK;
3404 }
3405 break;
3406
3407 case ANEG_STATE_RESTART_INIT:
3408 ap->link_time = ap->cur_time;
3409 ap->flags &= ~(MR_NP_LOADED);
3410 ap->txconfig = 0;
3411 tw32(MAC_TX_AUTO_NEG, 0);
3412 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3413 tw32_f(MAC_MODE, tp->mac_mode);
3414 udelay(40);
3415
3416 ret = ANEG_TIMER_ENAB;
3417 ap->state = ANEG_STATE_RESTART;
3418
3419 /* fallthru */
3420 case ANEG_STATE_RESTART:
3421 delta = ap->cur_time - ap->link_time;
3422 if (delta > ANEG_STATE_SETTLE_TIME) {
3423 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
3424 } else {
3425 ret = ANEG_TIMER_ENAB;
3426 }
3427 break;
3428
3429 case ANEG_STATE_DISABLE_LINK_OK:
3430 ret = ANEG_DONE;
3431 break;
3432
3433 case ANEG_STATE_ABILITY_DETECT_INIT:
3434 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08003435 ap->txconfig = ANEG_CFG_FD;
3436 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3437 if (flowctrl & ADVERTISE_1000XPAUSE)
3438 ap->txconfig |= ANEG_CFG_PS1;
3439 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3440 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3442 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3443 tw32_f(MAC_MODE, tp->mac_mode);
3444 udelay(40);
3445
3446 ap->state = ANEG_STATE_ABILITY_DETECT;
3447 break;
3448
3449 case ANEG_STATE_ABILITY_DETECT:
3450 if (ap->ability_match != 0 && ap->rxconfig != 0) {
3451 ap->state = ANEG_STATE_ACK_DETECT_INIT;
3452 }
3453 break;
3454
3455 case ANEG_STATE_ACK_DETECT_INIT:
3456 ap->txconfig |= ANEG_CFG_ACK;
3457 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3458 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3459 tw32_f(MAC_MODE, tp->mac_mode);
3460 udelay(40);
3461
3462 ap->state = ANEG_STATE_ACK_DETECT;
3463
3464 /* fallthru */
3465 case ANEG_STATE_ACK_DETECT:
3466 if (ap->ack_match != 0) {
3467 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
3468 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
3469 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
3470 } else {
3471 ap->state = ANEG_STATE_AN_ENABLE;
3472 }
3473 } else if (ap->ability_match != 0 &&
3474 ap->rxconfig == 0) {
3475 ap->state = ANEG_STATE_AN_ENABLE;
3476 }
3477 break;
3478
3479 case ANEG_STATE_COMPLETE_ACK_INIT:
3480 if (ap->rxconfig & ANEG_CFG_INVAL) {
3481 ret = ANEG_FAILED;
3482 break;
3483 }
3484 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
3485 MR_LP_ADV_HALF_DUPLEX |
3486 MR_LP_ADV_SYM_PAUSE |
3487 MR_LP_ADV_ASYM_PAUSE |
3488 MR_LP_ADV_REMOTE_FAULT1 |
3489 MR_LP_ADV_REMOTE_FAULT2 |
3490 MR_LP_ADV_NEXT_PAGE |
3491 MR_TOGGLE_RX |
3492 MR_NP_RX);
3493 if (ap->rxconfig & ANEG_CFG_FD)
3494 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
3495 if (ap->rxconfig & ANEG_CFG_HD)
3496 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
3497 if (ap->rxconfig & ANEG_CFG_PS1)
3498 ap->flags |= MR_LP_ADV_SYM_PAUSE;
3499 if (ap->rxconfig & ANEG_CFG_PS2)
3500 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
3501 if (ap->rxconfig & ANEG_CFG_RF1)
3502 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
3503 if (ap->rxconfig & ANEG_CFG_RF2)
3504 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
3505 if (ap->rxconfig & ANEG_CFG_NP)
3506 ap->flags |= MR_LP_ADV_NEXT_PAGE;
3507
3508 ap->link_time = ap->cur_time;
3509
3510 ap->flags ^= (MR_TOGGLE_TX);
3511 if (ap->rxconfig & 0x0008)
3512 ap->flags |= MR_TOGGLE_RX;
3513 if (ap->rxconfig & ANEG_CFG_NP)
3514 ap->flags |= MR_NP_RX;
3515 ap->flags |= MR_PAGE_RX;
3516
3517 ap->state = ANEG_STATE_COMPLETE_ACK;
3518 ret = ANEG_TIMER_ENAB;
3519 break;
3520
3521 case ANEG_STATE_COMPLETE_ACK:
3522 if (ap->ability_match != 0 &&
3523 ap->rxconfig == 0) {
3524 ap->state = ANEG_STATE_AN_ENABLE;
3525 break;
3526 }
3527 delta = ap->cur_time - ap->link_time;
3528 if (delta > ANEG_STATE_SETTLE_TIME) {
3529 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
3530 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3531 } else {
3532 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
3533 !(ap->flags & MR_NP_RX)) {
3534 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3535 } else {
3536 ret = ANEG_FAILED;
3537 }
3538 }
3539 }
3540 break;
3541
3542 case ANEG_STATE_IDLE_DETECT_INIT:
3543 ap->link_time = ap->cur_time;
3544 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3545 tw32_f(MAC_MODE, tp->mac_mode);
3546 udelay(40);
3547
3548 ap->state = ANEG_STATE_IDLE_DETECT;
3549 ret = ANEG_TIMER_ENAB;
3550 break;
3551
3552 case ANEG_STATE_IDLE_DETECT:
3553 if (ap->ability_match != 0 &&
3554 ap->rxconfig == 0) {
3555 ap->state = ANEG_STATE_AN_ENABLE;
3556 break;
3557 }
3558 delta = ap->cur_time - ap->link_time;
3559 if (delta > ANEG_STATE_SETTLE_TIME) {
3560 /* XXX another gem from the Broadcom driver :( */
3561 ap->state = ANEG_STATE_LINK_OK;
3562 }
3563 break;
3564
3565 case ANEG_STATE_LINK_OK:
3566 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
3567 ret = ANEG_DONE;
3568 break;
3569
3570 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
3571 /* ??? unimplemented */
3572 break;
3573
3574 case ANEG_STATE_NEXT_PAGE_WAIT:
3575 /* ??? unimplemented */
3576 break;
3577
3578 default:
3579 ret = ANEG_FAILED;
3580 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582
3583 return ret;
3584}
3585
Matt Carlson5be73b42007-12-20 20:09:29 -08003586static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587{
3588 int res = 0;
3589 struct tg3_fiber_aneginfo aninfo;
3590 int status = ANEG_FAILED;
3591 unsigned int tick;
3592 u32 tmp;
3593
3594 tw32_f(MAC_TX_AUTO_NEG, 0);
3595
3596 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
3597 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
3598 udelay(40);
3599
3600 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
3601 udelay(40);
3602
3603 memset(&aninfo, 0, sizeof(aninfo));
3604 aninfo.flags |= MR_AN_ENABLE;
3605 aninfo.state = ANEG_STATE_UNKNOWN;
3606 aninfo.cur_time = 0;
3607 tick = 0;
3608 while (++tick < 195000) {
3609 status = tg3_fiber_aneg_smachine(tp, &aninfo);
3610 if (status == ANEG_DONE || status == ANEG_FAILED)
3611 break;
3612
3613 udelay(1);
3614 }
3615
3616 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3617 tw32_f(MAC_MODE, tp->mac_mode);
3618 udelay(40);
3619
Matt Carlson5be73b42007-12-20 20:09:29 -08003620 *txflags = aninfo.txconfig;
3621 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622
3623 if (status == ANEG_DONE &&
3624 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
3625 MR_LP_ADV_FULL_DUPLEX)))
3626 res = 1;
3627
3628 return res;
3629}
3630
3631static void tg3_init_bcm8002(struct tg3 *tp)
3632{
3633 u32 mac_status = tr32(MAC_STATUS);
3634 int i;
3635
3636 /* Reset when initting first time or we have a link. */
3637 if ((tp->tg3_flags & TG3_FLAG_INIT_COMPLETE) &&
3638 !(mac_status & MAC_STATUS_PCS_SYNCED))
3639 return;
3640
3641 /* Set PLL lock range. */
3642 tg3_writephy(tp, 0x16, 0x8007);
3643
3644 /* SW reset */
3645 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
3646
3647 /* Wait for reset to complete. */
3648 /* XXX schedule_timeout() ... */
3649 for (i = 0; i < 500; i++)
3650 udelay(10);
3651
3652 /* Config mode; select PMA/Ch 1 regs. */
3653 tg3_writephy(tp, 0x10, 0x8411);
3654
3655 /* Enable auto-lock and comdet, select txclk for tx. */
3656 tg3_writephy(tp, 0x11, 0x0a10);
3657
3658 tg3_writephy(tp, 0x18, 0x00a0);
3659 tg3_writephy(tp, 0x16, 0x41ff);
3660
3661 /* Assert and deassert POR. */
3662 tg3_writephy(tp, 0x13, 0x0400);
3663 udelay(40);
3664 tg3_writephy(tp, 0x13, 0x0000);
3665
3666 tg3_writephy(tp, 0x11, 0x0a50);
3667 udelay(40);
3668 tg3_writephy(tp, 0x11, 0x0a10);
3669
3670 /* Wait for signal to stabilize */
3671 /* XXX schedule_timeout() ... */
3672 for (i = 0; i < 15000; i++)
3673 udelay(10);
3674
3675 /* Deselect the channel register so we can read the PHYID
3676 * later.
3677 */
3678 tg3_writephy(tp, 0x10, 0x8011);
3679}
3680
3681static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
3682{
Matt Carlson82cd3d12007-12-20 20:09:00 -08003683 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 u32 sg_dig_ctrl, sg_dig_status;
3685 u32 serdes_cfg, expected_sg_dig_ctrl;
3686 int workaround, port_a;
3687 int current_link_up;
3688
3689 serdes_cfg = 0;
3690 expected_sg_dig_ctrl = 0;
3691 workaround = 0;
3692 port_a = 1;
3693 current_link_up = 0;
3694
3695 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
3696 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
3697 workaround = 1;
3698 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
3699 port_a = 0;
3700
3701 /* preserve bits 0-11,13,14 for signal pre-emphasis */
3702 /* preserve bits 20-23 for voltage regulator */
3703 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
3704 }
3705
3706 sg_dig_ctrl = tr32(SG_DIG_CTRL);
3707
3708 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003709 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 if (workaround) {
3711 u32 val = serdes_cfg;
3712
3713 if (port_a)
3714 val |= 0xc010000;
3715 else
3716 val |= 0x4010000;
3717 tw32_f(MAC_SERDES_CFG, val);
3718 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003719
3720 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 }
3722 if (mac_status & MAC_STATUS_PCS_SYNCED) {
3723 tg3_setup_flow_control(tp, 0, 0);
3724 current_link_up = 1;
3725 }
3726 goto out;
3727 }
3728
3729 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003730 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731
Matt Carlson82cd3d12007-12-20 20:09:00 -08003732 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3733 if (flowctrl & ADVERTISE_1000XPAUSE)
3734 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
3735 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3736 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737
3738 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003739 if ((tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT) &&
3740 tp->serdes_counter &&
3741 ((mac_status & (MAC_STATUS_PCS_SYNCED |
3742 MAC_STATUS_RCVD_CFG)) ==
3743 MAC_STATUS_PCS_SYNCED)) {
3744 tp->serdes_counter--;
3745 current_link_up = 1;
3746 goto out;
3747 }
3748restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 if (workaround)
3750 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003751 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 udelay(5);
3753 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
3754
Michael Chan3d3ebe72006-09-27 15:59:15 -07003755 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
3756 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
3758 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003759 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 mac_status = tr32(MAC_STATUS);
3761
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003762 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08003764 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765
Matt Carlson82cd3d12007-12-20 20:09:00 -08003766 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
3767 local_adv |= ADVERTISE_1000XPAUSE;
3768 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
3769 local_adv |= ADVERTISE_1000XPSE_ASYM;
3770
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003771 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003772 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003773 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003774 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775
3776 tg3_setup_flow_control(tp, local_adv, remote_adv);
3777 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003778 tp->serdes_counter = 0;
3779 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003780 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003781 if (tp->serdes_counter)
3782 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783 else {
3784 if (workaround) {
3785 u32 val = serdes_cfg;
3786
3787 if (port_a)
3788 val |= 0xc010000;
3789 else
3790 val |= 0x4010000;
3791
3792 tw32_f(MAC_SERDES_CFG, val);
3793 }
3794
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003795 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 udelay(40);
3797
3798 /* Link parallel detection - link is up */
3799 /* only if we have PCS_SYNC and not */
3800 /* receiving config code words */
3801 mac_status = tr32(MAC_STATUS);
3802 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
3803 !(mac_status & MAC_STATUS_RCVD_CFG)) {
3804 tg3_setup_flow_control(tp, 0, 0);
3805 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003806 tp->tg3_flags2 |=
3807 TG3_FLG2_PARALLEL_DETECT;
3808 tp->serdes_counter =
3809 SERDES_PARALLEL_DET_TIMEOUT;
3810 } else
3811 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812 }
3813 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07003814 } else {
3815 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
3816 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817 }
3818
3819out:
3820 return current_link_up;
3821}
3822
3823static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
3824{
3825 int current_link_up = 0;
3826
Michael Chan5cf64b8a2007-05-05 12:11:21 -07003827 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829
3830 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08003831 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003833
Matt Carlson5be73b42007-12-20 20:09:29 -08003834 if (fiber_autoneg(tp, &txflags, &rxflags)) {
3835 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836
Matt Carlson5be73b42007-12-20 20:09:29 -08003837 if (txflags & ANEG_CFG_PS1)
3838 local_adv |= ADVERTISE_1000XPAUSE;
3839 if (txflags & ANEG_CFG_PS2)
3840 local_adv |= ADVERTISE_1000XPSE_ASYM;
3841
3842 if (rxflags & MR_LP_ADV_SYM_PAUSE)
3843 remote_adv |= LPA_1000XPAUSE;
3844 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
3845 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846
3847 tg3_setup_flow_control(tp, local_adv, remote_adv);
3848
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 current_link_up = 1;
3850 }
3851 for (i = 0; i < 30; i++) {
3852 udelay(20);
3853 tw32_f(MAC_STATUS,
3854 (MAC_STATUS_SYNC_CHANGED |
3855 MAC_STATUS_CFG_CHANGED));
3856 udelay(40);
3857 if ((tr32(MAC_STATUS) &
3858 (MAC_STATUS_SYNC_CHANGED |
3859 MAC_STATUS_CFG_CHANGED)) == 0)
3860 break;
3861 }
3862
3863 mac_status = tr32(MAC_STATUS);
3864 if (current_link_up == 0 &&
3865 (mac_status & MAC_STATUS_PCS_SYNCED) &&
3866 !(mac_status & MAC_STATUS_RCVD_CFG))
3867 current_link_up = 1;
3868 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08003869 tg3_setup_flow_control(tp, 0, 0);
3870
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 /* Forcing 1000FD link up. */
3872 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873
3874 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
3875 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003876
3877 tw32_f(MAC_MODE, tp->mac_mode);
3878 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879 }
3880
3881out:
3882 return current_link_up;
3883}
3884
3885static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
3886{
3887 u32 orig_pause_cfg;
3888 u16 orig_active_speed;
3889 u8 orig_active_duplex;
3890 u32 mac_status;
3891 int current_link_up;
3892 int i;
3893
Matt Carlson8d018622007-12-20 20:05:44 -08003894 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 orig_active_speed = tp->link_config.active_speed;
3896 orig_active_duplex = tp->link_config.active_duplex;
3897
3898 if (!(tp->tg3_flags2 & TG3_FLG2_HW_AUTONEG) &&
3899 netif_carrier_ok(tp->dev) &&
3900 (tp->tg3_flags & TG3_FLAG_INIT_COMPLETE)) {
3901 mac_status = tr32(MAC_STATUS);
3902 mac_status &= (MAC_STATUS_PCS_SYNCED |
3903 MAC_STATUS_SIGNAL_DET |
3904 MAC_STATUS_CFG_CHANGED |
3905 MAC_STATUS_RCVD_CFG);
3906 if (mac_status == (MAC_STATUS_PCS_SYNCED |
3907 MAC_STATUS_SIGNAL_DET)) {
3908 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
3909 MAC_STATUS_CFG_CHANGED));
3910 return 0;
3911 }
3912 }
3913
3914 tw32_f(MAC_TX_AUTO_NEG, 0);
3915
3916 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
3917 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
3918 tw32_f(MAC_MODE, tp->mac_mode);
3919 udelay(40);
3920
3921 if (tp->phy_id == PHY_ID_BCM8002)
3922 tg3_init_bcm8002(tp);
3923
3924 /* Enable link change event even when serdes polling. */
3925 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
3926 udelay(40);
3927
3928 current_link_up = 0;
3929 mac_status = tr32(MAC_STATUS);
3930
3931 if (tp->tg3_flags2 & TG3_FLG2_HW_AUTONEG)
3932 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
3933 else
3934 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
3935
Matt Carlson898a56f2009-08-28 14:02:40 +00003936 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00003938 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939
3940 for (i = 0; i < 100; i++) {
3941 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
3942 MAC_STATUS_CFG_CHANGED));
3943 udelay(5);
3944 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07003945 MAC_STATUS_CFG_CHANGED |
3946 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 break;
3948 }
3949
3950 mac_status = tr32(MAC_STATUS);
3951 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
3952 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003953 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
3954 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 tw32_f(MAC_MODE, (tp->mac_mode |
3956 MAC_MODE_SEND_CONFIGS));
3957 udelay(1);
3958 tw32_f(MAC_MODE, tp->mac_mode);
3959 }
3960 }
3961
3962 if (current_link_up == 1) {
3963 tp->link_config.active_speed = SPEED_1000;
3964 tp->link_config.active_duplex = DUPLEX_FULL;
3965 tw32(MAC_LED_CTRL, (tp->led_ctrl |
3966 LED_CTRL_LNKLED_OVERRIDE |
3967 LED_CTRL_1000MBPS_ON));
3968 } else {
3969 tp->link_config.active_speed = SPEED_INVALID;
3970 tp->link_config.active_duplex = DUPLEX_INVALID;
3971 tw32(MAC_LED_CTRL, (tp->led_ctrl |
3972 LED_CTRL_LNKLED_OVERRIDE |
3973 LED_CTRL_TRAFFIC_OVERRIDE));
3974 }
3975
3976 if (current_link_up != netif_carrier_ok(tp->dev)) {
3977 if (current_link_up)
3978 netif_carrier_on(tp->dev);
3979 else
3980 netif_carrier_off(tp->dev);
3981 tg3_link_report(tp);
3982 } else {
Matt Carlson8d018622007-12-20 20:05:44 -08003983 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 if (orig_pause_cfg != now_pause_cfg ||
3985 orig_active_speed != tp->link_config.active_speed ||
3986 orig_active_duplex != tp->link_config.active_duplex)
3987 tg3_link_report(tp);
3988 }
3989
3990 return 0;
3991}
3992
Michael Chan747e8f82005-07-25 12:33:22 -07003993static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
3994{
3995 int current_link_up, err = 0;
3996 u32 bmsr, bmcr;
3997 u16 current_speed;
3998 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08003999 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07004000
4001 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4002 tw32_f(MAC_MODE, tp->mac_mode);
4003 udelay(40);
4004
4005 tw32(MAC_EVENT, 0);
4006
4007 tw32_f(MAC_STATUS,
4008 (MAC_STATUS_SYNC_CHANGED |
4009 MAC_STATUS_CFG_CHANGED |
4010 MAC_STATUS_MI_COMPLETION |
4011 MAC_STATUS_LNKSTATE_CHANGED));
4012 udelay(40);
4013
4014 if (force_reset)
4015 tg3_phy_reset(tp);
4016
4017 current_link_up = 0;
4018 current_speed = SPEED_INVALID;
4019 current_duplex = DUPLEX_INVALID;
4020
4021 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4022 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004023 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
4024 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4025 bmsr |= BMSR_LSTATUS;
4026 else
4027 bmsr &= ~BMSR_LSTATUS;
4028 }
Michael Chan747e8f82005-07-25 12:33:22 -07004029
4030 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
4031
4032 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlson2bd3ed02008-06-09 15:39:55 -07004033 (tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004034 /* do nothing, just check for link up at the end */
4035 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4036 u32 adv, new_adv;
4037
4038 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4039 new_adv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
4040 ADVERTISE_1000XPAUSE |
4041 ADVERTISE_1000XPSE_ASYM |
4042 ADVERTISE_SLCT);
4043
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004044 new_adv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Michael Chan747e8f82005-07-25 12:33:22 -07004045
4046 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
4047 new_adv |= ADVERTISE_1000XHALF;
4048 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
4049 new_adv |= ADVERTISE_1000XFULL;
4050
4051 if ((new_adv != adv) || !(bmcr & BMCR_ANENABLE)) {
4052 tg3_writephy(tp, MII_ADVERTISE, new_adv);
4053 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
4054 tg3_writephy(tp, MII_BMCR, bmcr);
4055
4056 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07004057 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Michael Chan747e8f82005-07-25 12:33:22 -07004058 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
4059
4060 return err;
4061 }
4062 } else {
4063 u32 new_bmcr;
4064
4065 bmcr &= ~BMCR_SPEED1000;
4066 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
4067
4068 if (tp->link_config.duplex == DUPLEX_FULL)
4069 new_bmcr |= BMCR_FULLDPLX;
4070
4071 if (new_bmcr != bmcr) {
4072 /* BMCR_SPEED1000 is a reserved bit that needs
4073 * to be set on write.
4074 */
4075 new_bmcr |= BMCR_SPEED1000;
4076
4077 /* Force a linkdown */
4078 if (netif_carrier_ok(tp->dev)) {
4079 u32 adv;
4080
4081 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4082 adv &= ~(ADVERTISE_1000XFULL |
4083 ADVERTISE_1000XHALF |
4084 ADVERTISE_SLCT);
4085 tg3_writephy(tp, MII_ADVERTISE, adv);
4086 tg3_writephy(tp, MII_BMCR, bmcr |
4087 BMCR_ANRESTART |
4088 BMCR_ANENABLE);
4089 udelay(10);
4090 netif_carrier_off(tp->dev);
4091 }
4092 tg3_writephy(tp, MII_BMCR, new_bmcr);
4093 bmcr = new_bmcr;
4094 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4095 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004096 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
4097 ASIC_REV_5714) {
4098 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4099 bmsr |= BMSR_LSTATUS;
4100 else
4101 bmsr &= ~BMSR_LSTATUS;
4102 }
Michael Chan747e8f82005-07-25 12:33:22 -07004103 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
4104 }
4105 }
4106
4107 if (bmsr & BMSR_LSTATUS) {
4108 current_speed = SPEED_1000;
4109 current_link_up = 1;
4110 if (bmcr & BMCR_FULLDPLX)
4111 current_duplex = DUPLEX_FULL;
4112 else
4113 current_duplex = DUPLEX_HALF;
4114
Matt Carlsonef167e22007-12-20 20:10:01 -08004115 local_adv = 0;
4116 remote_adv = 0;
4117
Michael Chan747e8f82005-07-25 12:33:22 -07004118 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08004119 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07004120
4121 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
4122 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
4123 common = local_adv & remote_adv;
4124 if (common & (ADVERTISE_1000XHALF |
4125 ADVERTISE_1000XFULL)) {
4126 if (common & ADVERTISE_1000XFULL)
4127 current_duplex = DUPLEX_FULL;
4128 else
4129 current_duplex = DUPLEX_HALF;
Michael Chan747e8f82005-07-25 12:33:22 -07004130 }
4131 else
4132 current_link_up = 0;
4133 }
4134 }
4135
Matt Carlsonef167e22007-12-20 20:10:01 -08004136 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
4137 tg3_setup_flow_control(tp, local_adv, remote_adv);
4138
Michael Chan747e8f82005-07-25 12:33:22 -07004139 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4140 if (tp->link_config.active_duplex == DUPLEX_HALF)
4141 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4142
4143 tw32_f(MAC_MODE, tp->mac_mode);
4144 udelay(40);
4145
4146 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4147
4148 tp->link_config.active_speed = current_speed;
4149 tp->link_config.active_duplex = current_duplex;
4150
4151 if (current_link_up != netif_carrier_ok(tp->dev)) {
4152 if (current_link_up)
4153 netif_carrier_on(tp->dev);
4154 else {
4155 netif_carrier_off(tp->dev);
4156 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
4157 }
4158 tg3_link_report(tp);
4159 }
4160 return err;
4161}
4162
4163static void tg3_serdes_parallel_detect(struct tg3 *tp)
4164{
Michael Chan3d3ebe72006-09-27 15:59:15 -07004165 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07004166 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07004167 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07004168 return;
4169 }
4170 if (!netif_carrier_ok(tp->dev) &&
4171 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
4172 u32 bmcr;
4173
4174 tg3_readphy(tp, MII_BMCR, &bmcr);
4175 if (bmcr & BMCR_ANENABLE) {
4176 u32 phy1, phy2;
4177
4178 /* Select shadow register 0x1f */
4179 tg3_writephy(tp, 0x1c, 0x7c00);
4180 tg3_readphy(tp, 0x1c, &phy1);
4181
4182 /* Select expansion interrupt status register */
4183 tg3_writephy(tp, 0x17, 0x0f01);
4184 tg3_readphy(tp, 0x15, &phy2);
4185 tg3_readphy(tp, 0x15, &phy2);
4186
4187 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
4188 /* We have signal detect and not receiving
4189 * config code words, link is up by parallel
4190 * detection.
4191 */
4192
4193 bmcr &= ~BMCR_ANENABLE;
4194 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
4195 tg3_writephy(tp, MII_BMCR, bmcr);
4196 tp->tg3_flags2 |= TG3_FLG2_PARALLEL_DETECT;
4197 }
4198 }
4199 }
4200 else if (netif_carrier_ok(tp->dev) &&
4201 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
4202 (tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT)) {
4203 u32 phy2;
4204
4205 /* Select expansion interrupt status register */
4206 tg3_writephy(tp, 0x17, 0x0f01);
4207 tg3_readphy(tp, 0x15, &phy2);
4208 if (phy2 & 0x20) {
4209 u32 bmcr;
4210
4211 /* Config code words received, turn on autoneg. */
4212 tg3_readphy(tp, MII_BMCR, &bmcr);
4213 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
4214
4215 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
4216
4217 }
4218 }
4219}
4220
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221static int tg3_setup_phy(struct tg3 *tp, int force_reset)
4222{
4223 int err;
4224
4225 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
4226 err = tg3_setup_fiber_phy(tp, force_reset);
Michael Chan747e8f82005-07-25 12:33:22 -07004227 } else if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES) {
4228 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 } else {
4230 err = tg3_setup_copper_phy(tp, force_reset);
4231 }
4232
Matt Carlsonbcb37f62008-11-03 16:52:09 -08004233 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08004234 u32 val, scale;
4235
4236 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
4237 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
4238 scale = 65;
4239 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
4240 scale = 6;
4241 else
4242 scale = 12;
4243
4244 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
4245 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
4246 tw32(GRC_MISC_CFG, val);
4247 }
4248
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249 if (tp->link_config.active_speed == SPEED_1000 &&
4250 tp->link_config.active_duplex == DUPLEX_HALF)
4251 tw32(MAC_TX_LENGTHS,
4252 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4253 (6 << TX_LENGTHS_IPG_SHIFT) |
4254 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
4255 else
4256 tw32(MAC_TX_LENGTHS,
4257 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4258 (6 << TX_LENGTHS_IPG_SHIFT) |
4259 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
4260
4261 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
4262 if (netif_carrier_ok(tp->dev)) {
4263 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07004264 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 } else {
4266 tw32(HOSTCC_STAT_COAL_TICKS, 0);
4267 }
4268 }
4269
Matt Carlson8ed5d972007-05-07 00:25:49 -07004270 if (tp->tg3_flags & TG3_FLAG_ASPM_WORKAROUND) {
4271 u32 val = tr32(PCIE_PWR_MGMT_THRESH);
4272 if (!netif_carrier_ok(tp->dev))
4273 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
4274 tp->pwrmgmt_thresh;
4275 else
4276 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
4277 tw32(PCIE_PWR_MGMT_THRESH, val);
4278 }
4279
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 return err;
4281}
4282
Michael Chandf3e6542006-05-26 17:48:07 -07004283/* This is called whenever we suspect that the system chipset is re-
4284 * ordering the sequence of MMIO to the tx send mailbox. The symptom
4285 * is bogus tx completions. We try to recover by setting the
4286 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
4287 * in the workqueue.
4288 */
4289static void tg3_tx_recover(struct tg3 *tp)
4290{
4291 BUG_ON((tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER) ||
4292 tp->write32_tx_mbox == tg3_write_indirect_mbox);
4293
4294 printk(KERN_WARNING PFX "%s: The system may be re-ordering memory-"
4295 "mapped I/O cycles to the network device, attempting to "
4296 "recover. Please report the problem to the driver maintainer "
4297 "and include system chipset information.\n", tp->dev->name);
4298
4299 spin_lock(&tp->lock);
Michael Chandf3e6542006-05-26 17:48:07 -07004300 tp->tg3_flags |= TG3_FLAG_TX_RECOVERY_PENDING;
Michael Chandf3e6542006-05-26 17:48:07 -07004301 spin_unlock(&tp->lock);
4302}
4303
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004304static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07004305{
4306 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004307 return tnapi->tx_pending -
4308 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07004309}
4310
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311/* Tigon3 never reports partial packet sends. So we do not
4312 * need special logic to handle SKBs that have not had all
4313 * of their frags sent yet, like SunGEM does.
4314 */
Matt Carlson17375d22009-08-28 14:02:18 +00004315static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316{
Matt Carlson17375d22009-08-28 14:02:18 +00004317 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00004318 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004319 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004320 struct netdev_queue *txq;
4321 int index = tnapi - tp->napi;
4322
4323 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
4324 index--;
4325
4326 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327
4328 while (sw_idx != hw_idx) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004329 struct tx_ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07004331 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332
Michael Chandf3e6542006-05-26 17:48:07 -07004333 if (unlikely(skb == NULL)) {
4334 tg3_tx_recover(tp);
4335 return;
4336 }
4337
David S. Miller90079ce2008-09-11 04:52:51 -07004338 skb_dma_unmap(&tp->pdev->dev, skb, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339
4340 ri->skb = NULL;
4341
4342 sw_idx = NEXT_TX(sw_idx);
4343
4344 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004345 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07004346 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
4347 tx_bug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348 sw_idx = NEXT_TX(sw_idx);
4349 }
4350
David S. Millerf47c11e2005-06-24 20:18:35 -07004351 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07004352
4353 if (unlikely(tx_bug)) {
4354 tg3_tx_recover(tp);
4355 return;
4356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 }
4358
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004359 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360
Michael Chan1b2a7202006-08-07 21:46:02 -07004361 /* Need to make the tx_cons update visible to tg3_start_xmit()
4362 * before checking for netif_queue_stopped(). Without the
4363 * memory barrier, there is a small possibility that tg3_start_xmit()
4364 * will miss it and cause the queue to be stopped forever.
4365 */
4366 smp_mb();
4367
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004368 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004369 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004370 __netif_tx_lock(txq, smp_processor_id());
4371 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004372 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004373 netif_tx_wake_queue(txq);
4374 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07004375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376}
4377
4378/* Returns size of skb allocated or < 0 on error.
4379 *
4380 * We only need to fill in the address because the other members
4381 * of the RX descriptor are invariant, see tg3_init_rings.
4382 *
4383 * Note the purposeful assymetry of cpu vs. chip accesses. For
4384 * posting buffers we only dirty the first cache line of the RX
4385 * descriptor (containing the address). Whereas for the RX status
4386 * buffers the cpu only reads the last cacheline of the RX descriptor
4387 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
4388 */
Matt Carlson17375d22009-08-28 14:02:18 +00004389static int tg3_alloc_rx_skb(struct tg3_napi *tnapi, u32 opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 int src_idx, u32 dest_idx_unmasked)
4391{
Matt Carlson17375d22009-08-28 14:02:18 +00004392 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 struct tg3_rx_buffer_desc *desc;
4394 struct ring_info *map, *src_map;
4395 struct sk_buff *skb;
4396 dma_addr_t mapping;
4397 int skb_size, dest_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00004398 struct tg3_rx_prodring_set *tpr = &tp->prodring[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399
4400 src_map = NULL;
4401 switch (opaque_key) {
4402 case RXD_OPAQUE_RING_STD:
4403 dest_idx = dest_idx_unmasked % TG3_RX_RING_SIZE;
Matt Carlson21f581a2009-08-28 14:00:25 +00004404 desc = &tpr->rx_std[dest_idx];
4405 map = &tpr->rx_std_buffers[dest_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 if (src_idx >= 0)
Matt Carlson21f581a2009-08-28 14:00:25 +00004407 src_map = &tpr->rx_std_buffers[src_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004408 skb_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 break;
4410
4411 case RXD_OPAQUE_RING_JUMBO:
4412 dest_idx = dest_idx_unmasked % TG3_RX_JUMBO_RING_SIZE;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00004413 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00004414 map = &tpr->rx_jmb_buffers[dest_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415 if (src_idx >= 0)
Matt Carlson21f581a2009-08-28 14:00:25 +00004416 src_map = &tpr->rx_jmb_buffers[src_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004417 skb_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 break;
4419
4420 default:
4421 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423
4424 /* Do not overwrite any of the map or rp information
4425 * until we are sure we can commit to a new buffer.
4426 *
4427 * Callers depend upon this behavior and assume that
4428 * we leave everything unchanged if we fail.
4429 */
Matt Carlson287be122009-08-28 13:58:46 +00004430 skb = netdev_alloc_skb(tp->dev, skb_size + tp->rx_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 if (skb == NULL)
4432 return -ENOMEM;
4433
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 skb_reserve(skb, tp->rx_offset);
4435
Matt Carlson287be122009-08-28 13:58:46 +00004436 mapping = pci_map_single(tp->pdev, skb->data, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 PCI_DMA_FROMDEVICE);
4438
4439 map->skb = skb;
4440 pci_unmap_addr_set(map, mapping, mapping);
4441
4442 if (src_map != NULL)
4443 src_map->skb = NULL;
4444
4445 desc->addr_hi = ((u64)mapping >> 32);
4446 desc->addr_lo = ((u64)mapping & 0xffffffff);
4447
4448 return skb_size;
4449}
4450
4451/* We only need to move over in the address because the other
4452 * members of the RX descriptor are invariant. See notes above
4453 * tg3_alloc_rx_skb for full details.
4454 */
Matt Carlson17375d22009-08-28 14:02:18 +00004455static void tg3_recycle_rx(struct tg3_napi *tnapi, u32 opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456 int src_idx, u32 dest_idx_unmasked)
4457{
Matt Carlson17375d22009-08-28 14:02:18 +00004458 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
4460 struct ring_info *src_map, *dest_map;
4461 int dest_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00004462 struct tg3_rx_prodring_set *tpr = &tp->prodring[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463
4464 switch (opaque_key) {
4465 case RXD_OPAQUE_RING_STD:
4466 dest_idx = dest_idx_unmasked % TG3_RX_RING_SIZE;
Matt Carlson21f581a2009-08-28 14:00:25 +00004467 dest_desc = &tpr->rx_std[dest_idx];
4468 dest_map = &tpr->rx_std_buffers[dest_idx];
4469 src_desc = &tpr->rx_std[src_idx];
4470 src_map = &tpr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 break;
4472
4473 case RXD_OPAQUE_RING_JUMBO:
4474 dest_idx = dest_idx_unmasked % TG3_RX_JUMBO_RING_SIZE;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00004475 dest_desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00004476 dest_map = &tpr->rx_jmb_buffers[dest_idx];
Matt Carlson79ed5ac2009-08-28 14:00:55 +00004477 src_desc = &tpr->rx_jmb[src_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00004478 src_map = &tpr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 break;
4480
4481 default:
4482 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484
4485 dest_map->skb = src_map->skb;
4486 pci_unmap_addr_set(dest_map, mapping,
4487 pci_unmap_addr(src_map, mapping));
4488 dest_desc->addr_hi = src_desc->addr_hi;
4489 dest_desc->addr_lo = src_desc->addr_lo;
4490
4491 src_map->skb = NULL;
4492}
4493
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494/* The RX ring scheme is composed of multiple rings which post fresh
4495 * buffers to the chip, and one special ring the chip uses to report
4496 * status back to the host.
4497 *
4498 * The special ring reports the status of received packets to the
4499 * host. The chip does not write into the original descriptor the
4500 * RX buffer was obtained from. The chip simply takes the original
4501 * descriptor as provided by the host, updates the status and length
4502 * field, then writes this into the next status ring entry.
4503 *
4504 * Each ring the host uses to post buffers to the chip is described
4505 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
4506 * it is first placed into the on-chip ram. When the packet's length
4507 * is known, it walks down the TG3_BDINFO entries to select the ring.
4508 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
4509 * which is within the range of the new packet's length is chosen.
4510 *
4511 * The "separate ring for rx status" scheme may sound queer, but it makes
4512 * sense from a cache coherency perspective. If only the host writes
4513 * to the buffer post rings, and only the chip writes to the rx status
4514 * rings, then cache lines never move beyond shared-modified state.
4515 * If both the host and chip were to write into the same ring, cache line
4516 * eviction could occur since both entities want it in an exclusive state.
4517 */
Matt Carlson17375d22009-08-28 14:02:18 +00004518static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519{
Matt Carlson17375d22009-08-28 14:02:18 +00004520 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07004521 u32 work_mask, rx_std_posted = 0;
Matt Carlson72334482009-08-28 14:03:01 +00004522 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07004523 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 int received;
Matt Carlson21f581a2009-08-28 14:00:25 +00004525 struct tg3_rx_prodring_set *tpr = &tp->prodring[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004527 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 /*
4529 * We need to order the read of hw_idx and the read of
4530 * the opaque cookie.
4531 */
4532 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 work_mask = 0;
4534 received = 0;
4535 while (sw_idx != hw_idx && budget > 0) {
Matt Carlson72334482009-08-28 14:03:01 +00004536 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 unsigned int len;
4538 struct sk_buff *skb;
4539 dma_addr_t dma_addr;
4540 u32 opaque_key, desc_idx, *post_ptr;
4541
4542 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
4543 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
4544 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson21f581a2009-08-28 14:00:25 +00004545 struct ring_info *ri = &tpr->rx_std_buffers[desc_idx];
4546 dma_addr = pci_unmap_addr(ri, mapping);
4547 skb = ri->skb;
4548 post_ptr = &tpr->rx_std_ptr;
Michael Chanf92905d2006-06-29 20:14:29 -07004549 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson21f581a2009-08-28 14:00:25 +00004551 struct ring_info *ri = &tpr->rx_jmb_buffers[desc_idx];
4552 dma_addr = pci_unmap_addr(ri, mapping);
4553 skb = ri->skb;
4554 post_ptr = &tpr->rx_jmb_ptr;
4555 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557
4558 work_mask |= opaque_key;
4559
4560 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
4561 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
4562 drop_it:
Matt Carlson17375d22009-08-28 14:02:18 +00004563 tg3_recycle_rx(tnapi, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 desc_idx, *post_ptr);
4565 drop_it_no_recycle:
4566 /* Other statistics kept track of by card. */
4567 tp->net_stats.rx_dropped++;
4568 goto next_pkt;
4569 }
4570
Matt Carlsonad829262008-11-21 17:16:16 -08004571 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
4572 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004574 if (len > RX_COPY_THRESHOLD
Matt Carlsonad829262008-11-21 17:16:16 -08004575 && tp->rx_offset == NET_IP_ALIGN
4576 /* rx_offset will likely not equal NET_IP_ALIGN
4577 * if this is a 5701 card running in PCI-X mode
4578 * [see tg3_get_invariants()]
4579 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580 ) {
4581 int skb_size;
4582
Matt Carlson17375d22009-08-28 14:02:18 +00004583 skb_size = tg3_alloc_rx_skb(tnapi, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 desc_idx, *post_ptr);
4585 if (skb_size < 0)
4586 goto drop_it;
4587
Matt Carlson287be122009-08-28 13:58:46 +00004588 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589 PCI_DMA_FROMDEVICE);
4590
4591 skb_put(skb, len);
4592 } else {
4593 struct sk_buff *copy_skb;
4594
Matt Carlson17375d22009-08-28 14:02:18 +00004595 tg3_recycle_rx(tnapi, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 desc_idx, *post_ptr);
4597
Matt Carlsonad829262008-11-21 17:16:16 -08004598 copy_skb = netdev_alloc_skb(tp->dev,
4599 len + TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600 if (copy_skb == NULL)
4601 goto drop_it_no_recycle;
4602
Matt Carlsonad829262008-11-21 17:16:16 -08004603 skb_reserve(copy_skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604 skb_put(copy_skb, len);
4605 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03004606 skb_copy_from_linear_data(skb, copy_skb->data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
4608
4609 /* We'll reuse the original ring buffer. */
4610 skb = copy_skb;
4611 }
4612
4613 if ((tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) &&
4614 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
4615 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
4616 >> RXD_TCPCSUM_SHIFT) == 0xffff))
4617 skb->ip_summed = CHECKSUM_UNNECESSARY;
4618 else
4619 skb->ip_summed = CHECKSUM_NONE;
4620
4621 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00004622
4623 if (len > (tp->dev->mtu + ETH_HLEN) &&
4624 skb->protocol != htons(ETH_P_8021Q)) {
4625 dev_kfree_skb(skb);
4626 goto next_pkt;
4627 }
4628
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629#if TG3_VLAN_TAG_USED
4630 if (tp->vlgrp != NULL &&
4631 desc->type_flags & RXD_FLAG_VLAN) {
Matt Carlson17375d22009-08-28 14:02:18 +00004632 vlan_gro_receive(&tnapi->napi, tp->vlgrp,
Matt Carlson8ef04422009-08-28 14:01:37 +00004633 desc->err_vlan & RXD_VLAN_MASK, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 } else
4635#endif
Matt Carlson17375d22009-08-28 14:02:18 +00004636 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638 received++;
4639 budget--;
4640
4641next_pkt:
4642 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07004643
4644 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
4645 u32 idx = *post_ptr % TG3_RX_RING_SIZE;
4646
4647 tw32_rx_mbox(MAILBOX_RCV_STD_PROD_IDX +
4648 TG3_64BIT_REG_LOW, idx);
4649 work_mask &= ~RXD_OPAQUE_RING_STD;
4650 rx_std_posted = 0;
4651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07004653 sw_idx++;
Eric Dumazet6b31a512007-02-06 13:29:21 -08004654 sw_idx &= (TG3_RX_RCB_RING_SIZE(tp) - 1);
Michael Chan52f6d692005-04-25 15:14:32 -07004655
4656 /* Refresh hw_idx to see if there is new work */
4657 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004658 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07004659 rmb();
4660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 }
4662
4663 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00004664 tnapi->rx_rcb_ptr = sw_idx;
4665 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666
4667 /* Refill RX ring(s). */
4668 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson21f581a2009-08-28 14:00:25 +00004669 sw_idx = tpr->rx_std_ptr % TG3_RX_RING_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670 tw32_rx_mbox(MAILBOX_RCV_STD_PROD_IDX + TG3_64BIT_REG_LOW,
4671 sw_idx);
4672 }
4673 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson21f581a2009-08-28 14:00:25 +00004674 sw_idx = tpr->rx_jmb_ptr % TG3_RX_JUMBO_RING_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675 tw32_rx_mbox(MAILBOX_RCV_JUMBO_PROD_IDX + TG3_64BIT_REG_LOW,
4676 sw_idx);
4677 }
4678 mmiowb();
4679
4680 return received;
4681}
4682
Matt Carlson17375d22009-08-28 14:02:18 +00004683static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684{
Matt Carlson17375d22009-08-28 14:02:18 +00004685 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00004686 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 /* handle link change and other phy events */
4689 if (!(tp->tg3_flags &
4690 (TG3_FLAG_USE_LINKCHG_REG |
4691 TG3_FLAG_POLL_SERDES))) {
4692 if (sblk->status & SD_STATUS_LINK_CHG) {
4693 sblk->status = SD_STATUS_UPDATED |
4694 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07004695 spin_lock(&tp->lock);
Matt Carlsondd477002008-05-25 23:45:58 -07004696 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
4697 tw32_f(MAC_STATUS,
4698 (MAC_STATUS_SYNC_CHANGED |
4699 MAC_STATUS_CFG_CHANGED |
4700 MAC_STATUS_MI_COMPLETION |
4701 MAC_STATUS_LNKSTATE_CHANGED));
4702 udelay(40);
4703 } else
4704 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07004705 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 }
4707 }
4708
4709 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004710 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00004711 tg3_tx(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07004712 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
Michael Chan4fd7ab52007-10-12 01:39:50 -07004713 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714 }
4715
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716 /* run RX thread, within the bounds set by NAPI.
4717 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004718 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004720 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00004721 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722
David S. Miller6f535762007-10-11 18:08:29 -07004723 return work_done;
4724}
David S. Millerf7383c22005-05-18 22:50:53 -07004725
David S. Miller6f535762007-10-11 18:08:29 -07004726static int tg3_poll(struct napi_struct *napi, int budget)
4727{
Matt Carlson8ef04422009-08-28 14:01:37 +00004728 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
4729 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07004730 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00004731 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07004732
4733 while (1) {
Matt Carlson17375d22009-08-28 14:02:18 +00004734 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07004735
4736 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
4737 goto tx_recovery;
4738
4739 if (unlikely(work_done >= budget))
4740 break;
4741
Michael Chan4fd7ab52007-10-12 01:39:50 -07004742 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) {
Matt Carlson17375d22009-08-28 14:02:18 +00004743 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07004744 * to tell the hw how much work has been processed,
4745 * so we must read it before checking for more work.
4746 */
Matt Carlson898a56f2009-08-28 14:02:40 +00004747 tnapi->last_tag = sblk->status_tag;
4748 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07004749 rmb();
4750 } else
4751 sblk->status &= ~SD_STATUS_UPDATED;
4752
Matt Carlson17375d22009-08-28 14:02:18 +00004753 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08004754 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00004755 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07004756 break;
4757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758 }
4759
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004760 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07004761
4762tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07004763 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08004764 napi_complete(napi);
David S. Miller6f535762007-10-11 18:08:29 -07004765 schedule_work(&tp->reset_task);
Michael Chan4fd7ab52007-10-12 01:39:50 -07004766 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767}
4768
David S. Millerf47c11e2005-06-24 20:18:35 -07004769static void tg3_irq_quiesce(struct tg3 *tp)
4770{
Matt Carlson4f125f42009-09-01 12:55:02 +00004771 int i;
4772
David S. Millerf47c11e2005-06-24 20:18:35 -07004773 BUG_ON(tp->irq_sync);
4774
4775 tp->irq_sync = 1;
4776 smp_mb();
4777
Matt Carlson4f125f42009-09-01 12:55:02 +00004778 for (i = 0; i < tp->irq_cnt; i++)
4779 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07004780}
4781
4782static inline int tg3_irq_sync(struct tg3 *tp)
4783{
4784 return tp->irq_sync;
4785}
4786
4787/* Fully shutdown all tg3 driver activity elsewhere in the system.
4788 * If irq_sync is non-zero, then the IRQ handler must be synchronized
4789 * with as well. Most of the time, this is not necessary except when
4790 * shutting down the device.
4791 */
4792static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
4793{
Michael Chan46966542007-07-11 19:47:19 -07004794 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07004795 if (irq_sync)
4796 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07004797}
4798
4799static inline void tg3_full_unlock(struct tg3 *tp)
4800{
David S. Millerf47c11e2005-06-24 20:18:35 -07004801 spin_unlock_bh(&tp->lock);
4802}
4803
Michael Chanfcfa0a32006-03-20 22:28:41 -08004804/* One-shot MSI handler - Chip automatically disables interrupt
4805 * after sending MSI so driver doesn't have to do it.
4806 */
David Howells7d12e782006-10-05 14:55:46 +01004807static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08004808{
Matt Carlson09943a12009-08-28 14:01:57 +00004809 struct tg3_napi *tnapi = dev_id;
4810 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08004811
Matt Carlson898a56f2009-08-28 14:02:40 +00004812 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00004813 if (tnapi->rx_rcb)
4814 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08004815
4816 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00004817 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08004818
4819 return IRQ_HANDLED;
4820}
4821
Michael Chan88b06bc22005-04-21 17:13:25 -07004822/* MSI ISR - No need to check for interrupt sharing and no need to
4823 * flush status block and interrupt mailbox. PCI ordering rules
4824 * guarantee that MSI will arrive after the status block.
4825 */
David Howells7d12e782006-10-05 14:55:46 +01004826static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07004827{
Matt Carlson09943a12009-08-28 14:01:57 +00004828 struct tg3_napi *tnapi = dev_id;
4829 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07004830
Matt Carlson898a56f2009-08-28 14:02:40 +00004831 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00004832 if (tnapi->rx_rcb)
4833 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07004834 /*
David S. Millerfac9b832005-05-18 22:46:34 -07004835 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07004836 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07004837 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07004838 * NIC to stop sending us irqs, engaging "in-intr-handler"
4839 * event coalescing.
4840 */
4841 tw32_mailbox(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07004842 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00004843 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07004844
Michael Chan88b06bc22005-04-21 17:13:25 -07004845 return IRQ_RETVAL(1);
4846}
4847
David Howells7d12e782006-10-05 14:55:46 +01004848static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849{
Matt Carlson09943a12009-08-28 14:01:57 +00004850 struct tg3_napi *tnapi = dev_id;
4851 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00004852 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853 unsigned int handled = 1;
4854
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855 /* In INTx mode, it is possible for the interrupt to arrive at
4856 * the CPU before the status block posted prior to the interrupt.
4857 * Reading the PCI State register will confirm whether the
4858 * interrupt is ours and will flush the status block.
4859 */
Michael Chand18edcb2007-03-24 20:57:11 -07004860 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
4861 if ((tp->tg3_flags & TG3_FLAG_CHIP_RESETTING) ||
4862 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
4863 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07004864 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07004865 }
Michael Chand18edcb2007-03-24 20:57:11 -07004866 }
4867
4868 /*
4869 * Writing any value to intr-mbox-0 clears PCI INTA# and
4870 * chip-internal interrupt pending events.
4871 * Writing non-zero to intr-mbox-0 additional tells the
4872 * NIC to stop sending us irqs, engaging "in-intr-handler"
4873 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07004874 *
4875 * Flush the mailbox to de-assert the IRQ immediately to prevent
4876 * spurious interrupts. The flush impacts performance but
4877 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07004878 */
Michael Chanc04cb342007-05-07 00:26:15 -07004879 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07004880 if (tg3_irq_sync(tp))
4881 goto out;
4882 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00004883 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00004884 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00004885 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07004886 } else {
4887 /* No work, shared interrupt perhaps? re-enable
4888 * interrupts, and flush that PCI write
4889 */
4890 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
4891 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07004892 }
David S. Millerf47c11e2005-06-24 20:18:35 -07004893out:
David S. Millerfac9b832005-05-18 22:46:34 -07004894 return IRQ_RETVAL(handled);
4895}
4896
David Howells7d12e782006-10-05 14:55:46 +01004897static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07004898{
Matt Carlson09943a12009-08-28 14:01:57 +00004899 struct tg3_napi *tnapi = dev_id;
4900 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00004901 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07004902 unsigned int handled = 1;
4903
David S. Millerfac9b832005-05-18 22:46:34 -07004904 /* In INTx mode, it is possible for the interrupt to arrive at
4905 * the CPU before the status block posted prior to the interrupt.
4906 * Reading the PCI State register will confirm whether the
4907 * interrupt is ours and will flush the status block.
4908 */
Matt Carlson898a56f2009-08-28 14:02:40 +00004909 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Michael Chand18edcb2007-03-24 20:57:11 -07004910 if ((tp->tg3_flags & TG3_FLAG_CHIP_RESETTING) ||
4911 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
4912 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07004913 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 }
Michael Chand18edcb2007-03-24 20:57:11 -07004915 }
4916
4917 /*
4918 * writing any value to intr-mbox-0 clears PCI INTA# and
4919 * chip-internal interrupt pending events.
4920 * writing non-zero to intr-mbox-0 additional tells the
4921 * NIC to stop sending us irqs, engaging "in-intr-handler"
4922 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07004923 *
4924 * Flush the mailbox to de-assert the IRQ immediately to prevent
4925 * spurious interrupts. The flush impacts performance but
4926 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07004927 */
Michael Chanc04cb342007-05-07 00:26:15 -07004928 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00004929
4930 /*
4931 * In a shared interrupt configuration, sometimes other devices'
4932 * interrupts will scream. We record the current status tag here
4933 * so that the above check can report that the screaming interrupts
4934 * are unhandled. Eventually they will be silenced.
4935 */
Matt Carlson898a56f2009-08-28 14:02:40 +00004936 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00004937
Michael Chand18edcb2007-03-24 20:57:11 -07004938 if (tg3_irq_sync(tp))
4939 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00004940
Matt Carlson72334482009-08-28 14:03:01 +00004941 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00004942
Matt Carlson09943a12009-08-28 14:01:57 +00004943 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00004944
David S. Millerf47c11e2005-06-24 20:18:35 -07004945out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 return IRQ_RETVAL(handled);
4947}
4948
Michael Chan79381092005-04-21 17:13:59 -07004949/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01004950static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07004951{
Matt Carlson09943a12009-08-28 14:01:57 +00004952 struct tg3_napi *tnapi = dev_id;
4953 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00004954 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07004955
Michael Chanf9804dd2005-09-27 12:13:10 -07004956 if ((sblk->status & SD_STATUS_UPDATED) ||
4957 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07004958 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07004959 return IRQ_RETVAL(1);
4960 }
4961 return IRQ_RETVAL(0);
4962}
4963
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07004964static int tg3_init_hw(struct tg3 *, int);
Michael Chan944d9802005-05-29 14:57:48 -07004965static int tg3_halt(struct tg3 *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966
Michael Chanb9ec6c12006-07-25 16:37:27 -07004967/* Restart hardware after configuration changes, self-test, etc.
4968 * Invoked with tp->lock held.
4969 */
4970static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
Eric Dumazet78c61462008-04-24 23:33:06 -07004971 __releases(tp->lock)
4972 __acquires(tp->lock)
Michael Chanb9ec6c12006-07-25 16:37:27 -07004973{
4974 int err;
4975
4976 err = tg3_init_hw(tp, reset_phy);
4977 if (err) {
4978 printk(KERN_ERR PFX "%s: Failed to re-initialize device, "
4979 "aborting.\n", tp->dev->name);
4980 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
4981 tg3_full_unlock(tp);
4982 del_timer_sync(&tp->timer);
4983 tp->irq_sync = 0;
Matt Carlsonfed97812009-09-01 13:10:19 +00004984 tg3_napi_enable(tp);
Michael Chanb9ec6c12006-07-25 16:37:27 -07004985 dev_close(tp->dev);
4986 tg3_full_lock(tp, 0);
4987 }
4988 return err;
4989}
4990
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991#ifdef CONFIG_NET_POLL_CONTROLLER
4992static void tg3_poll_controller(struct net_device *dev)
4993{
Matt Carlson4f125f42009-09-01 12:55:02 +00004994 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07004995 struct tg3 *tp = netdev_priv(dev);
4996
Matt Carlson4f125f42009-09-01 12:55:02 +00004997 for (i = 0; i < tp->irq_cnt; i++)
4998 tg3_interrupt(tp->napi[i].irq_vec, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999}
5000#endif
5001
David Howellsc4028952006-11-22 14:57:56 +00005002static void tg3_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003{
David Howellsc4028952006-11-22 14:57:56 +00005004 struct tg3 *tp = container_of(work, struct tg3, reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005005 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006 unsigned int restart_timer;
5007
Michael Chan7faa0062006-02-02 17:29:28 -08005008 tg3_full_lock(tp, 0);
Michael Chan7faa0062006-02-02 17:29:28 -08005009
5010 if (!netif_running(tp->dev)) {
Michael Chan7faa0062006-02-02 17:29:28 -08005011 tg3_full_unlock(tp);
5012 return;
5013 }
5014
5015 tg3_full_unlock(tp);
5016
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005017 tg3_phy_stop(tp);
5018
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019 tg3_netif_stop(tp);
5020
David S. Millerf47c11e2005-06-24 20:18:35 -07005021 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022
5023 restart_timer = tp->tg3_flags2 & TG3_FLG2_RESTART_TIMER;
5024 tp->tg3_flags2 &= ~TG3_FLG2_RESTART_TIMER;
5025
Michael Chandf3e6542006-05-26 17:48:07 -07005026 if (tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING) {
5027 tp->write32_tx_mbox = tg3_write32_tx_mbox;
5028 tp->write32_rx_mbox = tg3_write_flush_reg32;
5029 tp->tg3_flags |= TG3_FLAG_MBOX_WRITE_REORDER;
5030 tp->tg3_flags &= ~TG3_FLAG_TX_RECOVERY_PENDING;
5031 }
5032
Michael Chan944d9802005-05-29 14:57:48 -07005033 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005034 err = tg3_init_hw(tp, 1);
5035 if (err)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005036 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037
5038 tg3_netif_start(tp);
5039
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040 if (restart_timer)
5041 mod_timer(&tp->timer, jiffies + 1);
Michael Chan7faa0062006-02-02 17:29:28 -08005042
Michael Chanb9ec6c12006-07-25 16:37:27 -07005043out:
Michael Chan7faa0062006-02-02 17:29:28 -08005044 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005045
5046 if (!err)
5047 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048}
5049
Michael Chanb0408752007-02-13 12:18:30 -08005050static void tg3_dump_short_state(struct tg3 *tp)
5051{
5052 printk(KERN_ERR PFX "DEBUG: MAC_TX_STATUS[%08x] MAC_RX_STATUS[%08x]\n",
5053 tr32(MAC_TX_STATUS), tr32(MAC_RX_STATUS));
5054 printk(KERN_ERR PFX "DEBUG: RDMAC_STATUS[%08x] WDMAC_STATUS[%08x]\n",
5055 tr32(RDMAC_STATUS), tr32(WDMAC_STATUS));
5056}
5057
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058static void tg3_tx_timeout(struct net_device *dev)
5059{
5060 struct tg3 *tp = netdev_priv(dev);
5061
Michael Chanb0408752007-02-13 12:18:30 -08005062 if (netif_msg_tx_err(tp)) {
Michael Chan9f88f292006-12-07 00:22:54 -08005063 printk(KERN_ERR PFX "%s: transmit timed out, resetting\n",
5064 dev->name);
Michael Chanb0408752007-02-13 12:18:30 -08005065 tg3_dump_short_state(tp);
5066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067
5068 schedule_work(&tp->reset_task);
5069}
5070
Michael Chanc58ec932005-09-17 00:46:27 -07005071/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
5072static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
5073{
5074 u32 base = (u32) mapping & 0xffffffff;
5075
5076 return ((base > 0xffffdcc0) &&
5077 (base + len + 8 < base));
5078}
5079
Michael Chan72f2afb2006-03-06 19:28:35 -08005080/* Test for DMA addresses > 40-bit */
5081static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
5082 int len)
5083{
5084#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Michael Chan6728a8e2006-03-27 23:16:49 -08005085 if (tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG)
Yang Hongyang50cf1562009-04-06 19:01:14 -07005086 return (((u64) mapping + len) > DMA_BIT_MASK(40));
Michael Chan72f2afb2006-03-06 19:28:35 -08005087 return 0;
5088#else
5089 return 0;
5090#endif
5091}
5092
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005093static void tg3_set_txd(struct tg3_napi *, int, dma_addr_t, int, u32, u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005094
Michael Chan72f2afb2006-03-06 19:28:35 -08005095/* Workaround 4GB and 40-bit hardware DMA bugs. */
5096static int tigon3_dma_hwbug_workaround(struct tg3 *tp, struct sk_buff *skb,
Michael Chanc58ec932005-09-17 00:46:27 -07005097 u32 last_plus_one, u32 *start,
5098 u32 base_flags, u32 mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099{
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005100 struct tg3_napi *tnapi = &tp->napi[0];
Matt Carlson41588ba2008-04-19 18:12:33 -07005101 struct sk_buff *new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07005102 dma_addr_t new_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103 u32 entry = *start;
Michael Chanc58ec932005-09-17 00:46:27 -07005104 int i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105
Matt Carlson41588ba2008-04-19 18:12:33 -07005106 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
5107 new_skb = skb_copy(skb, GFP_ATOMIC);
5108 else {
5109 int more_headroom = 4 - ((unsigned long)skb->data & 3);
5110
5111 new_skb = skb_copy_expand(skb,
5112 skb_headroom(skb) + more_headroom,
5113 skb_tailroom(skb), GFP_ATOMIC);
5114 }
5115
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07005117 ret = -1;
5118 } else {
5119 /* New SKB is guaranteed to be linear. */
5120 entry = *start;
David S. Miller90079ce2008-09-11 04:52:51 -07005121 ret = skb_dma_map(&tp->pdev->dev, new_skb, DMA_TO_DEVICE);
Eric Dumazet042a53a2009-06-05 04:04:16 +00005122 new_addr = skb_shinfo(new_skb)->dma_head;
David S. Miller90079ce2008-09-11 04:52:51 -07005123
Michael Chanc58ec932005-09-17 00:46:27 -07005124 /* Make sure new skb does not cross any 4G boundaries.
5125 * Drop the packet if it does.
5126 */
David S. Miller90079ce2008-09-11 04:52:51 -07005127 if (ret || tg3_4g_overflow_test(new_addr, new_skb->len)) {
David S. Miller638266f2008-09-11 15:45:19 -07005128 if (!ret)
5129 skb_dma_unmap(&tp->pdev->dev, new_skb,
5130 DMA_TO_DEVICE);
Michael Chanc58ec932005-09-17 00:46:27 -07005131 ret = -1;
5132 dev_kfree_skb(new_skb);
5133 new_skb = NULL;
5134 } else {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005135 tg3_set_txd(tnapi, entry, new_addr, new_skb->len,
Michael Chanc58ec932005-09-17 00:46:27 -07005136 base_flags, 1 | (mss << 1));
5137 *start = NEXT_TX(entry);
5138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139 }
5140
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141 /* Now clean up the sw ring entries. */
5142 i = 0;
5143 while (entry != last_plus_one) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005144 if (i == 0)
5145 tnapi->tx_buffers[entry].skb = new_skb;
5146 else
5147 tnapi->tx_buffers[entry].skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148 entry = NEXT_TX(entry);
5149 i++;
5150 }
5151
David S. Miller90079ce2008-09-11 04:52:51 -07005152 skb_dma_unmap(&tp->pdev->dev, skb, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153 dev_kfree_skb(skb);
5154
Michael Chanc58ec932005-09-17 00:46:27 -07005155 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156}
5157
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005158static void tg3_set_txd(struct tg3_napi *tnapi, int entry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159 dma_addr_t mapping, int len, u32 flags,
5160 u32 mss_and_is_end)
5161{
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005162 struct tg3_tx_buffer_desc *txd = &tnapi->tx_ring[entry];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163 int is_end = (mss_and_is_end & 0x1);
5164 u32 mss = (mss_and_is_end >> 1);
5165 u32 vlan_tag = 0;
5166
5167 if (is_end)
5168 flags |= TXD_FLAG_END;
5169 if (flags & TXD_FLAG_VLAN) {
5170 vlan_tag = flags >> 16;
5171 flags &= 0xffff;
5172 }
5173 vlan_tag |= (mss << TXD_MSS_SHIFT);
5174
5175 txd->addr_hi = ((u64) mapping >> 32);
5176 txd->addr_lo = ((u64) mapping & 0xffffffff);
5177 txd->len_flags = (len << TXD_LEN_SHIFT) | flags;
5178 txd->vlan_tag = vlan_tag << TXD_VLAN_TAG_SHIFT;
5179}
5180
Michael Chan5a6f3072006-03-20 22:28:05 -08005181/* hard_start_xmit for devices that don't have any bugs and
5182 * support TG3_FLG2_HW_TSO_2 only.
5183 */
Stephen Hemminger613573252009-08-31 19:50:58 +00005184static netdev_tx_t tg3_start_xmit(struct sk_buff *skb,
5185 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186{
5187 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188 u32 len, entry, base_flags, mss;
David S. Miller90079ce2008-09-11 04:52:51 -07005189 struct skb_shared_info *sp;
5190 dma_addr_t mapping;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005191 struct tg3_napi *tnapi;
5192 struct netdev_queue *txq;
Michael Chan5a6f3072006-03-20 22:28:05 -08005193
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005194 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
5195 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
5196 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
5197 tnapi++;
Michael Chan5a6f3072006-03-20 22:28:05 -08005198
Michael Chan00b70502006-06-17 21:58:45 -07005199 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005200 * and TX reclaim runs via tp->napi.poll inside of a software
Michael Chan5a6f3072006-03-20 22:28:05 -08005201 * interrupt. Furthermore, IRQ processing runs lockless so we have
5202 * no IRQ context deadlocks to worry about either. Rejoice!
5203 */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005204 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005205 if (!netif_tx_queue_stopped(txq)) {
5206 netif_tx_stop_queue(txq);
Michael Chan5a6f3072006-03-20 22:28:05 -08005207
5208 /* This is a hard error, log it. */
5209 printk(KERN_ERR PFX "%s: BUG! Tx Ring full when "
5210 "queue awake!\n", dev->name);
5211 }
Michael Chan5a6f3072006-03-20 22:28:05 -08005212 return NETDEV_TX_BUSY;
5213 }
5214
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005215 entry = tnapi->tx_prod;
Michael Chan5a6f3072006-03-20 22:28:05 -08005216 base_flags = 0;
Michael Chan5a6f3072006-03-20 22:28:05 -08005217 mss = 0;
Matt Carlsonc13e3712007-05-05 11:50:04 -07005218 if ((mss = skb_shinfo(skb)->gso_size) != 0) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005219 int tcp_opt_len, ip_tcp_len;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005220 u32 hdrlen;
Michael Chan5a6f3072006-03-20 22:28:05 -08005221
5222 if (skb_header_cloned(skb) &&
5223 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
5224 dev_kfree_skb(skb);
5225 goto out_unlock;
5226 }
5227
Michael Chanb0026622006-07-03 19:42:14 -07005228 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005229 hdrlen = skb_headlen(skb) - ETH_HLEN;
Michael Chanb0026622006-07-03 19:42:14 -07005230 else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005231 struct iphdr *iph = ip_hdr(skb);
5232
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07005233 tcp_opt_len = tcp_optlen(skb);
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -03005234 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
Michael Chanb0026622006-07-03 19:42:14 -07005235
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005236 iph->check = 0;
5237 iph->tot_len = htons(mss + ip_tcp_len + tcp_opt_len);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005238 hdrlen = ip_tcp_len + tcp_opt_len;
Michael Chanb0026622006-07-03 19:42:14 -07005239 }
Michael Chan5a6f3072006-03-20 22:28:05 -08005240
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005241 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
5242 mss |= (hdrlen & 0xc) << 12;
5243 if (hdrlen & 0x10)
5244 base_flags |= 0x00000010;
5245 base_flags |= (hdrlen & 0x3e0) << 5;
5246 } else
5247 mss |= hdrlen << 9;
5248
Michael Chan5a6f3072006-03-20 22:28:05 -08005249 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
5250 TXD_FLAG_CPU_POST_DMA);
5251
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005252 tcp_hdr(skb)->check = 0;
Michael Chan5a6f3072006-03-20 22:28:05 -08005253
Michael Chan5a6f3072006-03-20 22:28:05 -08005254 }
Patrick McHardy84fa7932006-08-29 16:44:56 -07005255 else if (skb->ip_summed == CHECKSUM_PARTIAL)
Michael Chan5a6f3072006-03-20 22:28:05 -08005256 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Michael Chan5a6f3072006-03-20 22:28:05 -08005257#if TG3_VLAN_TAG_USED
5258 if (tp->vlgrp != NULL && vlan_tx_tag_present(skb))
5259 base_flags |= (TXD_FLAG_VLAN |
5260 (vlan_tx_tag_get(skb) << 16));
5261#endif
5262
David S. Miller90079ce2008-09-11 04:52:51 -07005263 if (skb_dma_map(&tp->pdev->dev, skb, DMA_TO_DEVICE)) {
5264 dev_kfree_skb(skb);
5265 goto out_unlock;
5266 }
5267
5268 sp = skb_shinfo(skb);
5269
Eric Dumazet042a53a2009-06-05 04:04:16 +00005270 mapping = sp->dma_head;
Michael Chan5a6f3072006-03-20 22:28:05 -08005271
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005272 tnapi->tx_buffers[entry].skb = skb;
Michael Chan5a6f3072006-03-20 22:28:05 -08005273
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005274 len = skb_headlen(skb);
5275
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005276 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 &&
5277 !mss && skb->len > ETH_DATA_LEN)
5278 base_flags |= TXD_FLAG_JMB_PKT;
5279
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005280 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
Michael Chan5a6f3072006-03-20 22:28:05 -08005281 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
5282
5283 entry = NEXT_TX(entry);
5284
5285 /* Now loop through additional data fragments, and queue them. */
5286 if (skb_shinfo(skb)->nr_frags > 0) {
5287 unsigned int i, last;
5288
5289 last = skb_shinfo(skb)->nr_frags - 1;
5290 for (i = 0; i <= last; i++) {
5291 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5292
5293 len = frag->size;
Eric Dumazet042a53a2009-06-05 04:04:16 +00005294 mapping = sp->dma_maps[i];
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005295 tnapi->tx_buffers[entry].skb = NULL;
Michael Chan5a6f3072006-03-20 22:28:05 -08005296
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005297 tg3_set_txd(tnapi, entry, mapping, len,
Michael Chan5a6f3072006-03-20 22:28:05 -08005298 base_flags, (i == last) | (mss << 1));
5299
5300 entry = NEXT_TX(entry);
5301 }
5302 }
5303
5304 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005305 tw32_tx_mbox(tnapi->prodmbox, entry);
Michael Chan5a6f3072006-03-20 22:28:05 -08005306
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005307 tnapi->tx_prod = entry;
5308 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005309 netif_tx_stop_queue(txq);
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005310 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005311 netif_tx_wake_queue(txq);
Michael Chan5a6f3072006-03-20 22:28:05 -08005312 }
5313
5314out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00005315 mmiowb();
Michael Chan5a6f3072006-03-20 22:28:05 -08005316
5317 return NETDEV_TX_OK;
5318}
5319
Stephen Hemminger613573252009-08-31 19:50:58 +00005320static netdev_tx_t tg3_start_xmit_dma_bug(struct sk_buff *,
5321 struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07005322
5323/* Use GSO to workaround a rare TSO bug that may be triggered when the
5324 * TSO header is greater than 80 bytes.
5325 */
5326static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
5327{
5328 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005329 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07005330
5331 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005332 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07005333 netif_stop_queue(tp->dev);
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005334 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08005335 return NETDEV_TX_BUSY;
5336
5337 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07005338 }
5339
5340 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07005341 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07005342 goto tg3_tso_bug_end;
5343
5344 do {
5345 nskb = segs;
5346 segs = segs->next;
5347 nskb->next = NULL;
5348 tg3_start_xmit_dma_bug(nskb, tp->dev);
5349 } while (segs);
5350
5351tg3_tso_bug_end:
5352 dev_kfree_skb(skb);
5353
5354 return NETDEV_TX_OK;
5355}
Michael Chan52c0fd82006-06-29 20:15:54 -07005356
Michael Chan5a6f3072006-03-20 22:28:05 -08005357/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
5358 * support TG3_FLG2_HW_TSO_1 or firmware TSO only.
5359 */
Stephen Hemminger613573252009-08-31 19:50:58 +00005360static netdev_tx_t tg3_start_xmit_dma_bug(struct sk_buff *skb,
5361 struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08005362{
5363 struct tg3 *tp = netdev_priv(dev);
Michael Chan5a6f3072006-03-20 22:28:05 -08005364 u32 len, entry, base_flags, mss;
David S. Miller90079ce2008-09-11 04:52:51 -07005365 struct skb_shared_info *sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366 int would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07005367 dma_addr_t mapping;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005368 struct tg3_napi *tnapi = &tp->napi[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369
5370 len = skb_headlen(skb);
5371
Michael Chan00b70502006-06-17 21:58:45 -07005372 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005373 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07005374 * interrupt. Furthermore, IRQ processing runs lockless so we have
5375 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376 */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005377 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
Stephen Hemminger1f064a82005-12-06 17:36:44 -08005378 if (!netif_queue_stopped(dev)) {
5379 netif_stop_queue(dev);
5380
5381 /* This is a hard error, log it. */
5382 printk(KERN_ERR PFX "%s: BUG! Tx Ring full when "
5383 "queue awake!\n", dev->name);
5384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385 return NETDEV_TX_BUSY;
5386 }
5387
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005388 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07005390 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392 mss = 0;
Matt Carlsonc13e3712007-05-05 11:50:04 -07005393 if ((mss = skb_shinfo(skb)->gso_size) != 0) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005394 struct iphdr *iph;
Michael Chan52c0fd82006-06-29 20:15:54 -07005395 int tcp_opt_len, ip_tcp_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396
5397 if (skb_header_cloned(skb) &&
5398 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
5399 dev_kfree_skb(skb);
5400 goto out_unlock;
5401 }
5402
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07005403 tcp_opt_len = tcp_optlen(skb);
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -03005404 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405
Michael Chan52c0fd82006-06-29 20:15:54 -07005406 hdr_len = ip_tcp_len + tcp_opt_len;
5407 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Michael Chan7f62ad52007-02-20 23:25:40 -08005408 (tp->tg3_flags2 & TG3_FLG2_TSO_BUG))
Michael Chan52c0fd82006-06-29 20:15:54 -07005409 return (tg3_tso_bug(tp, skb));
5410
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
5412 TXD_FLAG_CPU_POST_DMA);
5413
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005414 iph = ip_hdr(skb);
5415 iph->check = 0;
5416 iph->tot_len = htons(mss + hdr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005418 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005420 } else
5421 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
5422 iph->daddr, 0,
5423 IPPROTO_TCP,
5424 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425
5426 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO) ||
5427 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005428 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429 int tsflags;
5430
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005431 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432 mss |= (tsflags << 11);
5433 }
5434 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005435 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 int tsflags;
5437
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005438 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439 base_flags |= tsflags << 12;
5440 }
5441 }
5442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443#if TG3_VLAN_TAG_USED
5444 if (tp->vlgrp != NULL && vlan_tx_tag_present(skb))
5445 base_flags |= (TXD_FLAG_VLAN |
5446 (vlan_tx_tag_get(skb) << 16));
5447#endif
5448
David S. Miller90079ce2008-09-11 04:52:51 -07005449 if (skb_dma_map(&tp->pdev->dev, skb, DMA_TO_DEVICE)) {
5450 dev_kfree_skb(skb);
5451 goto out_unlock;
5452 }
5453
5454 sp = skb_shinfo(skb);
5455
Eric Dumazet042a53a2009-06-05 04:04:16 +00005456 mapping = sp->dma_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005458 tnapi->tx_buffers[entry].skb = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459
5460 would_hit_hwbug = 0;
5461
Matt Carlson41588ba2008-04-19 18:12:33 -07005462 if (tp->tg3_flags3 & TG3_FLG3_5701_DMA_BUG)
5463 would_hit_hwbug = 1;
5464 else if (tg3_4g_overflow_test(mapping, len))
Michael Chanc58ec932005-09-17 00:46:27 -07005465 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005467 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
5469
5470 entry = NEXT_TX(entry);
5471
5472 /* Now loop through additional data fragments, and queue them. */
5473 if (skb_shinfo(skb)->nr_frags > 0) {
5474 unsigned int i, last;
5475
5476 last = skb_shinfo(skb)->nr_frags - 1;
5477 for (i = 0; i <= last; i++) {
5478 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5479
5480 len = frag->size;
Eric Dumazet042a53a2009-06-05 04:04:16 +00005481 mapping = sp->dma_maps[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005483 tnapi->tx_buffers[entry].skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484
Michael Chanc58ec932005-09-17 00:46:27 -07005485 if (tg3_4g_overflow_test(mapping, len))
5486 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487
Michael Chan72f2afb2006-03-06 19:28:35 -08005488 if (tg3_40bit_overflow_test(tp, mapping, len))
5489 would_hit_hwbug = 1;
5490
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005492 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493 base_flags, (i == last)|(mss << 1));
5494 else
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005495 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496 base_flags, (i == last));
5497
5498 entry = NEXT_TX(entry);
5499 }
5500 }
5501
5502 if (would_hit_hwbug) {
5503 u32 last_plus_one = entry;
5504 u32 start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505
Michael Chanc58ec932005-09-17 00:46:27 -07005506 start = entry - 1 - skb_shinfo(skb)->nr_frags;
5507 start &= (TG3_TX_RING_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508
5509 /* If the workaround fails due to memory/mapping
5510 * failure, silently drop this packet.
5511 */
Michael Chan72f2afb2006-03-06 19:28:35 -08005512 if (tigon3_dma_hwbug_workaround(tp, skb, last_plus_one,
Michael Chanc58ec932005-09-17 00:46:27 -07005513 &start, base_flags, mss))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 goto out_unlock;
5515
5516 entry = start;
5517 }
5518
5519 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005520 tw32_tx_mbox(MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005522 tnapi->tx_prod = entry;
5523 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524 netif_stop_queue(dev);
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005525 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Michael Chan51b91462005-09-01 17:41:28 -07005526 netif_wake_queue(tp->dev);
5527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528
5529out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00005530 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531
5532 return NETDEV_TX_OK;
5533}
5534
5535static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
5536 int new_mtu)
5537{
5538 dev->mtu = new_mtu;
5539
Michael Chanef7f5ec2005-07-25 12:32:25 -07005540 if (new_mtu > ETH_DATA_LEN) {
Michael Chana4e2b342005-10-26 15:46:52 -07005541 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
Michael Chanef7f5ec2005-07-25 12:32:25 -07005542 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
5543 ethtool_op_set_tso(dev, 0);
5544 }
5545 else
5546 tp->tg3_flags |= TG3_FLAG_JUMBO_RING_ENABLE;
5547 } else {
Michael Chana4e2b342005-10-26 15:46:52 -07005548 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
Michael Chanef7f5ec2005-07-25 12:32:25 -07005549 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
Michael Chan0f893dc2005-07-25 12:30:38 -07005550 tp->tg3_flags &= ~TG3_FLAG_JUMBO_RING_ENABLE;
Michael Chanef7f5ec2005-07-25 12:32:25 -07005551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552}
5553
5554static int tg3_change_mtu(struct net_device *dev, int new_mtu)
5555{
5556 struct tg3 *tp = netdev_priv(dev);
Michael Chanb9ec6c12006-07-25 16:37:27 -07005557 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558
5559 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
5560 return -EINVAL;
5561
5562 if (!netif_running(dev)) {
5563 /* We'll just catch it later when the
5564 * device is up'd.
5565 */
5566 tg3_set_mtu(dev, tp, new_mtu);
5567 return 0;
5568 }
5569
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005570 tg3_phy_stop(tp);
5571
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572 tg3_netif_stop(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07005573
5574 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575
Michael Chan944d9802005-05-29 14:57:48 -07005576 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577
5578 tg3_set_mtu(dev, tp, new_mtu);
5579
Michael Chanb9ec6c12006-07-25 16:37:27 -07005580 err = tg3_restart_hw(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581
Michael Chanb9ec6c12006-07-25 16:37:27 -07005582 if (!err)
5583 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584
David S. Millerf47c11e2005-06-24 20:18:35 -07005585 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005587 if (!err)
5588 tg3_phy_start(tp);
5589
Michael Chanb9ec6c12006-07-25 16:37:27 -07005590 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591}
5592
Matt Carlson21f581a2009-08-28 14:00:25 +00005593static void tg3_rx_prodring_free(struct tg3 *tp,
5594 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005596 int i;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005597 struct ring_info *rxp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598
5599 for (i = 0; i < TG3_RX_RING_SIZE; i++) {
Matt Carlson21f581a2009-08-28 14:00:25 +00005600 rxp = &tpr->rx_std_buffers[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601
5602 if (rxp->skb == NULL)
5603 continue;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005604
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605 pci_unmap_single(tp->pdev,
5606 pci_unmap_addr(rxp, mapping),
Matt Carlson287be122009-08-28 13:58:46 +00005607 tp->rx_pkt_map_sz,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608 PCI_DMA_FROMDEVICE);
5609 dev_kfree_skb_any(rxp->skb);
5610 rxp->skb = NULL;
5611 }
5612
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005613 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
5614 for (i = 0; i < TG3_RX_JUMBO_RING_SIZE; i++) {
Matt Carlson21f581a2009-08-28 14:00:25 +00005615 rxp = &tpr->rx_jmb_buffers[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005617 if (rxp->skb == NULL)
5618 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005620 pci_unmap_single(tp->pdev,
5621 pci_unmap_addr(rxp, mapping),
5622 TG3_RX_JMB_MAP_SZ,
5623 PCI_DMA_FROMDEVICE);
5624 dev_kfree_skb_any(rxp->skb);
5625 rxp->skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627 }
5628}
5629
5630/* Initialize tx/rx rings for packet processing.
5631 *
5632 * The chip has been shut down and the driver detached from
5633 * the networking, so no interrupts or new tx packets will
5634 * end up in the driver. tp->{tx,}lock are held and thus
5635 * we may not sleep.
5636 */
Matt Carlson21f581a2009-08-28 14:00:25 +00005637static int tg3_rx_prodring_alloc(struct tg3 *tp,
5638 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639{
Matt Carlson287be122009-08-28 13:58:46 +00005640 u32 i, rx_pkt_dma_sz;
Matt Carlson17375d22009-08-28 14:02:18 +00005641 struct tg3_napi *tnapi = &tp->napi[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643 /* Zero out all descriptors. */
Matt Carlson21f581a2009-08-28 14:00:25 +00005644 memset(tpr->rx_std, 0, TG3_RX_RING_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645
Matt Carlson287be122009-08-28 13:58:46 +00005646 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Michael Chana4e2b342005-10-26 15:46:52 -07005647 if ((tp->tg3_flags2 & TG3_FLG2_5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00005648 tp->dev->mtu > ETH_DATA_LEN)
5649 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
5650 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07005651
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652 /* Initialize invariants of the rings, we only set this
5653 * stuff once. This works because the card does not
5654 * write into the rx buffer posting rings.
5655 */
5656 for (i = 0; i < TG3_RX_RING_SIZE; i++) {
5657 struct tg3_rx_buffer_desc *rxd;
5658
Matt Carlson21f581a2009-08-28 14:00:25 +00005659 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00005660 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
5662 rxd->opaque = (RXD_OPAQUE_RING_STD |
5663 (i << RXD_OPAQUE_INDEX_SHIFT));
5664 }
5665
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005666 /* Now allocate fresh SKBs for each rx ring. */
5667 for (i = 0; i < tp->rx_pending; i++) {
Matt Carlson17375d22009-08-28 14:02:18 +00005668 if (tg3_alloc_rx_skb(tnapi, RXD_OPAQUE_RING_STD, -1, i) < 0) {
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005669 printk(KERN_WARNING PFX
5670 "%s: Using a smaller RX standard ring, "
5671 "only %d out of %d buffers were allocated "
5672 "successfully.\n",
5673 tp->dev->name, i, tp->rx_pending);
5674 if (i == 0)
5675 goto initfail;
5676 tp->rx_pending = i;
5677 break;
5678 }
5679 }
5680
5681 if (!(tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE))
5682 goto done;
5683
Matt Carlson21f581a2009-08-28 14:00:25 +00005684 memset(tpr->rx_jmb, 0, TG3_RX_JUMBO_RING_BYTES);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005685
Michael Chan0f893dc2005-07-25 12:30:38 -07005686 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687 for (i = 0; i < TG3_RX_JUMBO_RING_SIZE; i++) {
5688 struct tg3_rx_buffer_desc *rxd;
5689
Matt Carlson79ed5ac2009-08-28 14:00:55 +00005690 rxd = &tpr->rx_jmb[i].std;
Matt Carlson287be122009-08-28 13:58:46 +00005691 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
5693 RXD_FLAG_JUMBO;
5694 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
5695 (i << RXD_OPAQUE_INDEX_SHIFT));
5696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697
Linus Torvalds1da177e2005-04-16 15:20:36 -07005698 for (i = 0; i < tp->rx_jumbo_pending; i++) {
Matt Carlson17375d22009-08-28 14:02:18 +00005699 if (tg3_alloc_rx_skb(tnapi, RXD_OPAQUE_RING_JUMBO,
Michael Chan32d8c572006-07-25 16:38:29 -07005700 -1, i) < 0) {
5701 printk(KERN_WARNING PFX
5702 "%s: Using a smaller RX jumbo ring, "
5703 "only %d out of %d buffers were "
5704 "allocated successfully.\n",
5705 tp->dev->name, i, tp->rx_jumbo_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005706 if (i == 0)
5707 goto initfail;
Michael Chan32d8c572006-07-25 16:38:29 -07005708 tp->rx_jumbo_pending = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709 break;
Michael Chan32d8c572006-07-25 16:38:29 -07005710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711 }
5712 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005713
5714done:
Michael Chan32d8c572006-07-25 16:38:29 -07005715 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005716
5717initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00005718 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005719 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720}
5721
Matt Carlson21f581a2009-08-28 14:00:25 +00005722static void tg3_rx_prodring_fini(struct tg3 *tp,
5723 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724{
Matt Carlson21f581a2009-08-28 14:00:25 +00005725 kfree(tpr->rx_std_buffers);
5726 tpr->rx_std_buffers = NULL;
5727 kfree(tpr->rx_jmb_buffers);
5728 tpr->rx_jmb_buffers = NULL;
5729 if (tpr->rx_std) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730 pci_free_consistent(tp->pdev, TG3_RX_RING_BYTES,
Matt Carlson21f581a2009-08-28 14:00:25 +00005731 tpr->rx_std, tpr->rx_std_mapping);
5732 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005733 }
Matt Carlson21f581a2009-08-28 14:00:25 +00005734 if (tpr->rx_jmb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735 pci_free_consistent(tp->pdev, TG3_RX_JUMBO_RING_BYTES,
Matt Carlson21f581a2009-08-28 14:00:25 +00005736 tpr->rx_jmb, tpr->rx_jmb_mapping);
5737 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005739}
5740
Matt Carlson21f581a2009-08-28 14:00:25 +00005741static int tg3_rx_prodring_init(struct tg3 *tp,
5742 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005743{
Matt Carlson21f581a2009-08-28 14:00:25 +00005744 tpr->rx_std_buffers = kzalloc(sizeof(struct ring_info) *
5745 TG3_RX_RING_SIZE, GFP_KERNEL);
5746 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005747 return -ENOMEM;
5748
Matt Carlson21f581a2009-08-28 14:00:25 +00005749 tpr->rx_std = pci_alloc_consistent(tp->pdev, TG3_RX_RING_BYTES,
5750 &tpr->rx_std_mapping);
5751 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005752 goto err_out;
5753
5754 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Matt Carlson21f581a2009-08-28 14:00:25 +00005755 tpr->rx_jmb_buffers = kzalloc(sizeof(struct ring_info) *
5756 TG3_RX_JUMBO_RING_SIZE,
5757 GFP_KERNEL);
5758 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005759 goto err_out;
5760
Matt Carlson21f581a2009-08-28 14:00:25 +00005761 tpr->rx_jmb = pci_alloc_consistent(tp->pdev,
5762 TG3_RX_JUMBO_RING_BYTES,
5763 &tpr->rx_jmb_mapping);
5764 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005765 goto err_out;
5766 }
5767
5768 return 0;
5769
5770err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00005771 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005772 return -ENOMEM;
5773}
5774
5775/* Free up pending packets in all rx/tx rings.
5776 *
5777 * The chip has been shut down and the driver detached from
5778 * the networking, so no interrupts or new tx packets will
5779 * end up in the driver. tp->{tx,}lock is not held and we are not
5780 * in an interrupt context and thus may sleep.
5781 */
5782static void tg3_free_rings(struct tg3 *tp)
5783{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005784 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005785
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005786 for (j = 0; j < tp->irq_cnt; j++) {
5787 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005788
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005789 if (!tnapi->tx_buffers)
5790 continue;
5791
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005792 for (i = 0; i < TG3_TX_RING_SIZE; ) {
5793 struct tx_ring_info *txp;
5794 struct sk_buff *skb;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005795
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005796 txp = &tnapi->tx_buffers[i];
5797 skb = txp->skb;
5798
5799 if (skb == NULL) {
5800 i++;
5801 continue;
5802 }
5803
5804 skb_dma_unmap(&tp->pdev->dev, skb, DMA_TO_DEVICE);
5805
5806 txp->skb = NULL;
5807
5808 i += skb_shinfo(skb)->nr_frags + 1;
5809
5810 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005811 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005812 }
5813
Matt Carlson21f581a2009-08-28 14:00:25 +00005814 tg3_rx_prodring_free(tp, &tp->prodring[0]);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005815}
5816
5817/* Initialize tx/rx rings for packet processing.
5818 *
5819 * The chip has been shut down and the driver detached from
5820 * the networking, so no interrupts or new tx packets will
5821 * end up in the driver. tp->{tx,}lock are held and thus
5822 * we may not sleep.
5823 */
5824static int tg3_init_rings(struct tg3 *tp)
5825{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005826 int i;
Matt Carlson72334482009-08-28 14:03:01 +00005827
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005828 /* Free up all the SKBs. */
5829 tg3_free_rings(tp);
5830
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005831 for (i = 0; i < tp->irq_cnt; i++) {
5832 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005833
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005834 tnapi->last_tag = 0;
5835 tnapi->last_irq_tag = 0;
5836 tnapi->hw_status->status = 0;
5837 tnapi->hw_status->status_tag = 0;
5838 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
5839
5840 tnapi->tx_prod = 0;
5841 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005842 if (tnapi->tx_ring)
5843 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005844
5845 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005846 if (tnapi->rx_rcb)
5847 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005848 }
Matt Carlson72334482009-08-28 14:03:01 +00005849
Matt Carlson21f581a2009-08-28 14:00:25 +00005850 return tg3_rx_prodring_alloc(tp, &tp->prodring[0]);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005851}
5852
5853/*
5854 * Must not be invoked with interrupt sources disabled and
5855 * the hardware shutdown down.
5856 */
5857static void tg3_free_consistent(struct tg3 *tp)
5858{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005859 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00005860
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005861 for (i = 0; i < tp->irq_cnt; i++) {
5862 struct tg3_napi *tnapi = &tp->napi[i];
5863
5864 if (tnapi->tx_ring) {
5865 pci_free_consistent(tp->pdev, TG3_TX_RING_BYTES,
5866 tnapi->tx_ring, tnapi->tx_desc_mapping);
5867 tnapi->tx_ring = NULL;
5868 }
5869
5870 kfree(tnapi->tx_buffers);
5871 tnapi->tx_buffers = NULL;
5872
5873 if (tnapi->rx_rcb) {
5874 pci_free_consistent(tp->pdev, TG3_RX_RCB_RING_BYTES(tp),
5875 tnapi->rx_rcb,
5876 tnapi->rx_rcb_mapping);
5877 tnapi->rx_rcb = NULL;
5878 }
5879
5880 if (tnapi->hw_status) {
5881 pci_free_consistent(tp->pdev, TG3_HW_STATUS_SIZE,
5882 tnapi->hw_status,
5883 tnapi->status_mapping);
5884 tnapi->hw_status = NULL;
5885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005886 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005887
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888 if (tp->hw_stats) {
5889 pci_free_consistent(tp->pdev, sizeof(struct tg3_hw_stats),
5890 tp->hw_stats, tp->stats_mapping);
5891 tp->hw_stats = NULL;
5892 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005893
Matt Carlson21f581a2009-08-28 14:00:25 +00005894 tg3_rx_prodring_fini(tp, &tp->prodring[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005895}
5896
5897/*
5898 * Must not be invoked with interrupt sources disabled and
5899 * the hardware shutdown down. Can sleep.
5900 */
5901static int tg3_alloc_consistent(struct tg3 *tp)
5902{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005903 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00005904
Matt Carlson21f581a2009-08-28 14:00:25 +00005905 if (tg3_rx_prodring_init(tp, &tp->prodring[0]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005906 return -ENOMEM;
5907
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908 tp->hw_stats = pci_alloc_consistent(tp->pdev,
5909 sizeof(struct tg3_hw_stats),
5910 &tp->stats_mapping);
5911 if (!tp->hw_stats)
5912 goto err_out;
5913
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
5915
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005916 for (i = 0; i < tp->irq_cnt; i++) {
5917 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005918 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005919
5920 tnapi->hw_status = pci_alloc_consistent(tp->pdev,
5921 TG3_HW_STATUS_SIZE,
5922 &tnapi->status_mapping);
5923 if (!tnapi->hw_status)
5924 goto err_out;
5925
5926 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005927 sblk = tnapi->hw_status;
5928
5929 /*
5930 * When RSS is enabled, the status block format changes
5931 * slightly. The "rx_jumbo_consumer", "reserved",
5932 * and "rx_mini_consumer" members get mapped to the
5933 * other three rx return ring producer indexes.
5934 */
5935 switch (i) {
5936 default:
5937 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
5938 break;
5939 case 2:
5940 tnapi->rx_rcb_prod_idx = &sblk->rx_jumbo_consumer;
5941 break;
5942 case 3:
5943 tnapi->rx_rcb_prod_idx = &sblk->reserved;
5944 break;
5945 case 4:
5946 tnapi->rx_rcb_prod_idx = &sblk->rx_mini_consumer;
5947 break;
5948 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005949
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005950 /*
5951 * If multivector RSS is enabled, vector 0 does not handle
5952 * rx or tx interrupts. Don't allocate any resources for it.
5953 */
5954 if (!i && (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS))
5955 continue;
5956
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005957 tnapi->rx_rcb = pci_alloc_consistent(tp->pdev,
5958 TG3_RX_RCB_RING_BYTES(tp),
5959 &tnapi->rx_rcb_mapping);
5960 if (!tnapi->rx_rcb)
5961 goto err_out;
5962
5963 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
5964
5965 tnapi->tx_buffers = kzalloc(sizeof(struct tx_ring_info) *
5966 TG3_TX_RING_SIZE, GFP_KERNEL);
5967 if (!tnapi->tx_buffers)
5968 goto err_out;
5969
5970 tnapi->tx_ring = pci_alloc_consistent(tp->pdev,
5971 TG3_TX_RING_BYTES,
5972 &tnapi->tx_desc_mapping);
5973 if (!tnapi->tx_ring)
5974 goto err_out;
5975 }
5976
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977 return 0;
5978
5979err_out:
5980 tg3_free_consistent(tp);
5981 return -ENOMEM;
5982}
5983
5984#define MAX_WAIT_CNT 1000
5985
5986/* To stop a block, clear the enable bit and poll till it
5987 * clears. tp->lock is held.
5988 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07005989static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005990{
5991 unsigned int i;
5992 u32 val;
5993
5994 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
5995 switch (ofs) {
5996 case RCVLSC_MODE:
5997 case DMAC_MODE:
5998 case MBFREE_MODE:
5999 case BUFMGR_MODE:
6000 case MEMARB_MODE:
6001 /* We can't enable/disable these bits of the
6002 * 5705/5750, just say success.
6003 */
6004 return 0;
6005
6006 default:
6007 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006009 }
6010
6011 val = tr32(ofs);
6012 val &= ~enable_bit;
6013 tw32_f(ofs, val);
6014
6015 for (i = 0; i < MAX_WAIT_CNT; i++) {
6016 udelay(100);
6017 val = tr32(ofs);
6018 if ((val & enable_bit) == 0)
6019 break;
6020 }
6021
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006022 if (i == MAX_WAIT_CNT && !silent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006023 printk(KERN_ERR PFX "tg3_stop_block timed out, "
6024 "ofs=%lx enable_bit=%x\n",
6025 ofs, enable_bit);
6026 return -ENODEV;
6027 }
6028
6029 return 0;
6030}
6031
6032/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006033static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034{
6035 int i, err;
6036
6037 tg3_disable_ints(tp);
6038
6039 tp->rx_mode &= ~RX_MODE_ENABLE;
6040 tw32_f(MAC_RX_MODE, tp->rx_mode);
6041 udelay(10);
6042
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006043 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
6044 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
6045 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
6046 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
6047 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
6048 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006050 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
6051 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
6052 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
6053 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
6054 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
6055 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
6056 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006057
6058 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
6059 tw32_f(MAC_MODE, tp->mac_mode);
6060 udelay(40);
6061
6062 tp->tx_mode &= ~TX_MODE_ENABLE;
6063 tw32_f(MAC_TX_MODE, tp->tx_mode);
6064
6065 for (i = 0; i < MAX_WAIT_CNT; i++) {
6066 udelay(100);
6067 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
6068 break;
6069 }
6070 if (i >= MAX_WAIT_CNT) {
6071 printk(KERN_ERR PFX "tg3_abort_hw timed out for %s, "
6072 "TX_MODE_ENABLE will not clear MAC_TX_MODE=%08x\n",
6073 tp->dev->name, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07006074 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075 }
6076
Michael Chane6de8ad2005-05-05 14:42:41 -07006077 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006078 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
6079 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080
6081 tw32(FTQ_RESET, 0xffffffff);
6082 tw32(FTQ_RESET, 0x00000000);
6083
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006084 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
6085 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006087 for (i = 0; i < tp->irq_cnt; i++) {
6088 struct tg3_napi *tnapi = &tp->napi[i];
6089 if (tnapi->hw_status)
6090 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006092 if (tp->hw_stats)
6093 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6094
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095 return err;
6096}
6097
Matt Carlson0d3031d2007-10-10 18:02:43 -07006098static void tg3_ape_send_event(struct tg3 *tp, u32 event)
6099{
6100 int i;
6101 u32 apedata;
6102
6103 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
6104 if (apedata != APE_SEG_SIG_MAGIC)
6105 return;
6106
6107 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
Matt Carlson731fd792008-08-15 14:07:51 -07006108 if (!(apedata & APE_FW_STATUS_READY))
Matt Carlson0d3031d2007-10-10 18:02:43 -07006109 return;
6110
6111 /* Wait for up to 1 millisecond for APE to service previous event. */
6112 for (i = 0; i < 10; i++) {
6113 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
6114 return;
6115
6116 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
6117
6118 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6119 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
6120 event | APE_EVENT_STATUS_EVENT_PENDING);
6121
6122 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
6123
6124 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6125 break;
6126
6127 udelay(100);
6128 }
6129
6130 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6131 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
6132}
6133
6134static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
6135{
6136 u32 event;
6137 u32 apedata;
6138
6139 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
6140 return;
6141
6142 switch (kind) {
6143 case RESET_KIND_INIT:
6144 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
6145 APE_HOST_SEG_SIG_MAGIC);
6146 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
6147 APE_HOST_SEG_LEN_MAGIC);
6148 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
6149 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
6150 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
6151 APE_HOST_DRIVER_ID_MAGIC);
6152 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
6153 APE_HOST_BEHAV_NO_PHYLOCK);
6154
6155 event = APE_EVENT_STATUS_STATE_START;
6156 break;
6157 case RESET_KIND_SHUTDOWN:
Matt Carlsonb2aee152008-11-03 16:51:11 -08006158 /* With the interface we are currently using,
6159 * APE does not track driver state. Wiping
6160 * out the HOST SEGMENT SIGNATURE forces
6161 * the APE to assume OS absent status.
6162 */
6163 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
6164
Matt Carlson0d3031d2007-10-10 18:02:43 -07006165 event = APE_EVENT_STATUS_STATE_UNLOAD;
6166 break;
6167 case RESET_KIND_SUSPEND:
6168 event = APE_EVENT_STATUS_STATE_SUSPEND;
6169 break;
6170 default:
6171 return;
6172 }
6173
6174 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
6175
6176 tg3_ape_send_event(tp, event);
6177}
6178
Michael Chane6af3012005-04-21 17:12:05 -07006179/* tp->lock is held. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006180static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
6181{
David S. Millerf49639e2006-06-09 11:58:36 -07006182 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
6183 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006184
6185 if (tp->tg3_flags2 & TG3_FLG2_ASF_NEW_HANDSHAKE) {
6186 switch (kind) {
6187 case RESET_KIND_INIT:
6188 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6189 DRV_STATE_START);
6190 break;
6191
6192 case RESET_KIND_SHUTDOWN:
6193 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6194 DRV_STATE_UNLOAD);
6195 break;
6196
6197 case RESET_KIND_SUSPEND:
6198 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6199 DRV_STATE_SUSPEND);
6200 break;
6201
6202 default:
6203 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006205 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07006206
6207 if (kind == RESET_KIND_INIT ||
6208 kind == RESET_KIND_SUSPEND)
6209 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006210}
6211
6212/* tp->lock is held. */
6213static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
6214{
6215 if (tp->tg3_flags2 & TG3_FLG2_ASF_NEW_HANDSHAKE) {
6216 switch (kind) {
6217 case RESET_KIND_INIT:
6218 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6219 DRV_STATE_START_DONE);
6220 break;
6221
6222 case RESET_KIND_SHUTDOWN:
6223 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6224 DRV_STATE_UNLOAD_DONE);
6225 break;
6226
6227 default:
6228 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006230 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07006231
6232 if (kind == RESET_KIND_SHUTDOWN)
6233 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006234}
6235
6236/* tp->lock is held. */
6237static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
6238{
6239 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) {
6240 switch (kind) {
6241 case RESET_KIND_INIT:
6242 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6243 DRV_STATE_START);
6244 break;
6245
6246 case RESET_KIND_SHUTDOWN:
6247 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6248 DRV_STATE_UNLOAD);
6249 break;
6250
6251 case RESET_KIND_SUSPEND:
6252 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6253 DRV_STATE_SUSPEND);
6254 break;
6255
6256 default:
6257 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006259 }
6260}
6261
Michael Chan7a6f4362006-09-27 16:03:31 -07006262static int tg3_poll_fw(struct tg3 *tp)
6263{
6264 int i;
6265 u32 val;
6266
Michael Chanb5d37722006-09-27 16:06:21 -07006267 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Gary Zambrano0ccead12006-11-14 16:34:00 -08006268 /* Wait up to 20ms for init done. */
6269 for (i = 0; i < 200; i++) {
Michael Chanb5d37722006-09-27 16:06:21 -07006270 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
6271 return 0;
Gary Zambrano0ccead12006-11-14 16:34:00 -08006272 udelay(100);
Michael Chanb5d37722006-09-27 16:06:21 -07006273 }
6274 return -ENODEV;
6275 }
6276
Michael Chan7a6f4362006-09-27 16:03:31 -07006277 /* Wait for firmware initialization to complete. */
6278 for (i = 0; i < 100000; i++) {
6279 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
6280 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
6281 break;
6282 udelay(10);
6283 }
6284
6285 /* Chip might not be fitted with firmware. Some Sun onboard
6286 * parts are configured like that. So don't signal the timeout
6287 * of the above loop as an error, but do report the lack of
6288 * running firmware once.
6289 */
6290 if (i >= 100000 &&
6291 !(tp->tg3_flags2 & TG3_FLG2_NO_FWARE_REPORTED)) {
6292 tp->tg3_flags2 |= TG3_FLG2_NO_FWARE_REPORTED;
6293
6294 printk(KERN_INFO PFX "%s: No firmware running.\n",
6295 tp->dev->name);
6296 }
6297
6298 return 0;
6299}
6300
Michael Chanee6a99b2007-07-18 21:49:10 -07006301/* Save PCI command register before chip reset */
6302static void tg3_save_pci_state(struct tg3 *tp)
6303{
Matt Carlson8a6eac92007-10-21 16:17:55 -07006304 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07006305}
6306
6307/* Restore PCI state after chip reset */
6308static void tg3_restore_pci_state(struct tg3 *tp)
6309{
6310 u32 val;
6311
6312 /* Re-enable indirect register accesses. */
6313 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
6314 tp->misc_host_ctrl);
6315
6316 /* Set MAX PCI retry to zero. */
6317 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
6318 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
6319 (tp->tg3_flags & TG3_FLAG_PCIX_MODE))
6320 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07006321 /* Allow reads and writes to the APE register and memory space. */
6322 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
6323 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
6324 PCISTATE_ALLOW_APE_SHMEM_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07006325 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
6326
Matt Carlson8a6eac92007-10-21 16:17:55 -07006327 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07006328
Matt Carlsonfcb389d2008-11-03 16:55:44 -08006329 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785) {
6330 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)
6331 pcie_set_readrq(tp->pdev, 4096);
6332 else {
6333 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
6334 tp->pci_cacheline_sz);
6335 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
6336 tp->pci_lat_timer);
6337 }
Michael Chan114342f2007-10-15 02:12:26 -07006338 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08006339
Michael Chanee6a99b2007-07-18 21:49:10 -07006340 /* Make sure PCI-X relaxed ordering bit is clear. */
Matt Carlson52f44902008-11-21 17:17:04 -08006341 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
Matt Carlson9974a352007-10-07 23:27:28 -07006342 u16 pcix_cmd;
6343
6344 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
6345 &pcix_cmd);
6346 pcix_cmd &= ~PCI_X_CMD_ERO;
6347 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
6348 pcix_cmd);
6349 }
Michael Chanee6a99b2007-07-18 21:49:10 -07006350
6351 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
Michael Chanee6a99b2007-07-18 21:49:10 -07006352
6353 /* Chip reset on 5780 will reset MSI enable bit,
6354 * so need to restore it.
6355 */
6356 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
6357 u16 ctrl;
6358
6359 pci_read_config_word(tp->pdev,
6360 tp->msi_cap + PCI_MSI_FLAGS,
6361 &ctrl);
6362 pci_write_config_word(tp->pdev,
6363 tp->msi_cap + PCI_MSI_FLAGS,
6364 ctrl | PCI_MSI_FLAGS_ENABLE);
6365 val = tr32(MSGINT_MODE);
6366 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
6367 }
6368 }
6369}
6370
Linus Torvalds1da177e2005-04-16 15:20:36 -07006371static void tg3_stop_fw(struct tg3 *);
6372
6373/* tp->lock is held. */
6374static int tg3_chip_reset(struct tg3 *tp)
6375{
6376 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07006377 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00006378 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006379
David S. Millerf49639e2006-06-09 11:58:36 -07006380 tg3_nvram_lock(tp);
6381
Matt Carlson77b483f2008-08-15 14:07:24 -07006382 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
6383
David S. Millerf49639e2006-06-09 11:58:36 -07006384 /* No matching tg3_nvram_unlock() after this because
6385 * chip reset below will undo the nvram lock.
6386 */
6387 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006388
Michael Chanee6a99b2007-07-18 21:49:10 -07006389 /* GRC_MISC_CFG core clock reset will clear the memory
6390 * enable bit in PCI register 4 and the MSI enable bit
6391 * on some chips, so we save relevant registers here.
6392 */
6393 tg3_save_pci_state(tp);
6394
Michael Chand9ab5ad12006-03-20 22:27:35 -08006395 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08006396 (tp->tg3_flags3 & TG3_FLG3_5755_PLUS))
Michael Chand9ab5ad12006-03-20 22:27:35 -08006397 tw32(GRC_FASTBOOT_PC, 0);
6398
Linus Torvalds1da177e2005-04-16 15:20:36 -07006399 /*
6400 * We must avoid the readl() that normally takes place.
6401 * It locks machines, causes machine checks, and other
6402 * fun things. So, temporarily disable the 5701
6403 * hardware workaround, while we do the reset.
6404 */
Michael Chan1ee582d2005-08-09 20:16:46 -07006405 write_op = tp->write32;
6406 if (write_op == tg3_write_flush_reg32)
6407 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006408
Michael Chand18edcb2007-03-24 20:57:11 -07006409 /* Prevent the irq handler from reading or writing PCI registers
6410 * during chip reset when the memory enable bit in the PCI command
6411 * register may be cleared. The chip does not generate interrupt
6412 * at this time, but the irq handler may still be called due to irq
6413 * sharing or irqpoll.
6414 */
6415 tp->tg3_flags |= TG3_FLAG_CHIP_RESETTING;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006416 for (i = 0; i < tp->irq_cnt; i++) {
6417 struct tg3_napi *tnapi = &tp->napi[i];
6418 if (tnapi->hw_status) {
6419 tnapi->hw_status->status = 0;
6420 tnapi->hw_status->status_tag = 0;
6421 }
6422 tnapi->last_tag = 0;
6423 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07006424 }
Michael Chand18edcb2007-03-24 20:57:11 -07006425 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00006426
6427 for (i = 0; i < tp->irq_cnt; i++)
6428 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07006429
Matt Carlson255ca312009-08-25 10:07:27 +00006430 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
6431 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
6432 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
6433 }
6434
Linus Torvalds1da177e2005-04-16 15:20:36 -07006435 /* do the reset */
6436 val = GRC_MISC_CFG_CORECLK_RESET;
6437
6438 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
6439 if (tr32(0x7e2c) == 0x60) {
6440 tw32(0x7e2c, 0x20);
6441 }
6442 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
6443 tw32(GRC_MISC_CFG, (1 << 29));
6444 val |= (1 << 29);
6445 }
6446 }
6447
Michael Chanb5d37722006-09-27 16:06:21 -07006448 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
6449 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
6450 tw32(GRC_VCPU_EXT_CTRL,
6451 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
6452 }
6453
Linus Torvalds1da177e2005-04-16 15:20:36 -07006454 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
6455 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
6456 tw32(GRC_MISC_CFG, val);
6457
Michael Chan1ee582d2005-08-09 20:16:46 -07006458 /* restore 5701 hardware bug workaround write method */
6459 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006460
6461 /* Unfortunately, we have to delay before the PCI read back.
6462 * Some 575X chips even will not respond to a PCI cfg access
6463 * when the reset command is given to the chip.
6464 *
6465 * How do these hardware designers expect things to work
6466 * properly if the PCI write is posted for a long period
6467 * of time? It is always necessary to have some method by
6468 * which a register read back can occur to push the write
6469 * out which does the reset.
6470 *
6471 * For most tg3 variants the trick below was working.
6472 * Ho hum...
6473 */
6474 udelay(120);
6475
6476 /* Flush PCI posted writes. The normal MMIO registers
6477 * are inaccessible at this time so this is the only
6478 * way to make this reliably (actually, this is no longer
6479 * the case, see above). I tried to use indirect
6480 * register read/write but this upset some 5701 variants.
6481 */
6482 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
6483
6484 udelay(120);
6485
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006486 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) && tp->pcie_cap) {
Matt Carlsone7126992009-08-25 10:08:16 +00006487 u16 val16;
6488
Linus Torvalds1da177e2005-04-16 15:20:36 -07006489 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
6490 int i;
6491 u32 cfg_val;
6492
6493 /* Wait for link training to complete. */
6494 for (i = 0; i < 5000; i++)
6495 udelay(100);
6496
6497 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
6498 pci_write_config_dword(tp->pdev, 0xc4,
6499 cfg_val | (1 << 15));
6500 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006501
Matt Carlsone7126992009-08-25 10:08:16 +00006502 /* Clear the "no snoop" and "relaxed ordering" bits. */
6503 pci_read_config_word(tp->pdev,
6504 tp->pcie_cap + PCI_EXP_DEVCTL,
6505 &val16);
6506 val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
6507 PCI_EXP_DEVCTL_NOSNOOP_EN);
6508 /*
6509 * Older PCIe devices only support the 128 byte
6510 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006511 */
Matt Carlsone7126992009-08-25 10:08:16 +00006512 if (!(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
6513 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784))
6514 val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006515 pci_write_config_word(tp->pdev,
6516 tp->pcie_cap + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00006517 val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006518
6519 pcie_set_readrq(tp->pdev, 4096);
6520
6521 /* Clear error status */
6522 pci_write_config_word(tp->pdev,
6523 tp->pcie_cap + PCI_EXP_DEVSTA,
6524 PCI_EXP_DEVSTA_CED |
6525 PCI_EXP_DEVSTA_NFED |
6526 PCI_EXP_DEVSTA_FED |
6527 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006528 }
6529
Michael Chanee6a99b2007-07-18 21:49:10 -07006530 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006531
Michael Chand18edcb2007-03-24 20:57:11 -07006532 tp->tg3_flags &= ~TG3_FLAG_CHIP_RESETTING;
6533
Michael Chanee6a99b2007-07-18 21:49:10 -07006534 val = 0;
6535 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
Michael Chan4cf78e42005-07-25 12:29:19 -07006536 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07006537 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006538
6539 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
6540 tg3_stop_fw(tp);
6541 tw32(0x5000, 0x400);
6542 }
6543
6544 tw32(GRC_MODE, tp->grc_mode);
6545
6546 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01006547 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006548
6549 tw32(0xc4, val | (1 << 15));
6550 }
6551
6552 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
6553 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
6554 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
6555 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
6556 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
6557 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
6558 }
6559
6560 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
6561 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
6562 tw32_f(MAC_MODE, tp->mac_mode);
Michael Chan747e8f82005-07-25 12:33:22 -07006563 } else if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES) {
6564 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
6565 tw32_f(MAC_MODE, tp->mac_mode);
Matt Carlson3bda1252008-08-15 14:08:22 -07006566 } else if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
6567 tp->mac_mode &= (MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN);
6568 if (tp->mac_mode & MAC_MODE_APE_TX_EN)
6569 tp->mac_mode |= MAC_MODE_TDE_ENABLE;
6570 tw32_f(MAC_MODE, tp->mac_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006571 } else
6572 tw32_f(MAC_MODE, 0);
6573 udelay(40);
6574
Matt Carlson77b483f2008-08-15 14:07:24 -07006575 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
6576
Michael Chan7a6f4362006-09-27 16:03:31 -07006577 err = tg3_poll_fw(tp);
6578 if (err)
6579 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006580
Matt Carlson0a9140c2009-08-28 12:27:50 +00006581 tg3_mdio_start(tp);
6582
Linus Torvalds1da177e2005-04-16 15:20:36 -07006583 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00006584 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
6585 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
6586 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01006587 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006588
6589 tw32(0x7c00, val | (1 << 25));
6590 }
6591
6592 /* Reprobe ASF enable state. */
6593 tp->tg3_flags &= ~TG3_FLAG_ENABLE_ASF;
6594 tp->tg3_flags2 &= ~TG3_FLG2_ASF_NEW_HANDSHAKE;
6595 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
6596 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
6597 u32 nic_cfg;
6598
6599 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
6600 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
6601 tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
Matt Carlson4ba526c2008-08-15 14:10:04 -07006602 tp->last_event_jiffies = jiffies;
John W. Linvillecbf46852005-04-21 17:01:29 -07006603 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006604 tp->tg3_flags2 |= TG3_FLG2_ASF_NEW_HANDSHAKE;
6605 }
6606 }
6607
6608 return 0;
6609}
6610
6611/* tp->lock is held. */
6612static void tg3_stop_fw(struct tg3 *tp)
6613{
Matt Carlson0d3031d2007-10-10 18:02:43 -07006614 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
6615 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07006616 /* Wait for RX cpu to ACK the previous event. */
6617 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006618
6619 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
Matt Carlson4ba526c2008-08-15 14:10:04 -07006620
6621 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006622
Matt Carlson7c5026a2008-05-02 16:49:29 -07006623 /* Wait for RX cpu to ACK this event. */
6624 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006625 }
6626}
6627
6628/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07006629static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006630{
6631 int err;
6632
6633 tg3_stop_fw(tp);
6634
Michael Chan944d9802005-05-29 14:57:48 -07006635 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006636
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006637 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006638 err = tg3_chip_reset(tp);
6639
Matt Carlsondaba2a62009-04-20 06:58:52 +00006640 __tg3_set_mac_addr(tp, 0);
6641
Michael Chan944d9802005-05-29 14:57:48 -07006642 tg3_write_sig_legacy(tp, kind);
6643 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006644
6645 if (err)
6646 return err;
6647
6648 return 0;
6649}
6650
Linus Torvalds1da177e2005-04-16 15:20:36 -07006651#define RX_CPU_SCRATCH_BASE 0x30000
6652#define RX_CPU_SCRATCH_SIZE 0x04000
6653#define TX_CPU_SCRATCH_BASE 0x34000
6654#define TX_CPU_SCRATCH_SIZE 0x04000
6655
6656/* tp->lock is held. */
6657static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
6658{
6659 int i;
6660
Eric Sesterhenn5d9428d2006-04-02 13:52:48 +02006661 BUG_ON(offset == TX_CPU_BASE &&
6662 (tp->tg3_flags2 & TG3_FLG2_5705_PLUS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006663
Michael Chanb5d37722006-09-27 16:06:21 -07006664 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
6665 u32 val = tr32(GRC_VCPU_EXT_CTRL);
6666
6667 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
6668 return 0;
6669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006670 if (offset == RX_CPU_BASE) {
6671 for (i = 0; i < 10000; i++) {
6672 tw32(offset + CPU_STATE, 0xffffffff);
6673 tw32(offset + CPU_MODE, CPU_MODE_HALT);
6674 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
6675 break;
6676 }
6677
6678 tw32(offset + CPU_STATE, 0xffffffff);
6679 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
6680 udelay(10);
6681 } else {
6682 for (i = 0; i < 10000; i++) {
6683 tw32(offset + CPU_STATE, 0xffffffff);
6684 tw32(offset + CPU_MODE, CPU_MODE_HALT);
6685 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
6686 break;
6687 }
6688 }
6689
6690 if (i >= 10000) {
6691 printk(KERN_ERR PFX "tg3_reset_cpu timed out for %s, "
6692 "and %s CPU\n",
6693 tp->dev->name,
6694 (offset == RX_CPU_BASE ? "RX" : "TX"));
6695 return -ENODEV;
6696 }
Michael Chanec41c7d2006-01-17 02:40:55 -08006697
6698 /* Clear firmware's nvram arbitration. */
6699 if (tp->tg3_flags & TG3_FLAG_NVRAM)
6700 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006701 return 0;
6702}
6703
6704struct fw_info {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006705 unsigned int fw_base;
6706 unsigned int fw_len;
6707 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006708};
6709
6710/* tp->lock is held. */
6711static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base, u32 cpu_scratch_base,
6712 int cpu_scratch_size, struct fw_info *info)
6713{
Michael Chanec41c7d2006-01-17 02:40:55 -08006714 int err, lock_err, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006715 void (*write_op)(struct tg3 *, u32, u32);
6716
6717 if (cpu_base == TX_CPU_BASE &&
6718 (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
6719 printk(KERN_ERR PFX "tg3_load_firmware_cpu: Trying to load "
6720 "TX cpu firmware on %s which is 5705.\n",
6721 tp->dev->name);
6722 return -EINVAL;
6723 }
6724
6725 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
6726 write_op = tg3_write_mem;
6727 else
6728 write_op = tg3_write_indirect_reg32;
6729
Michael Chan1b628152005-05-29 14:59:49 -07006730 /* It is possible that bootcode is still loading at this point.
6731 * Get the nvram lock first before halting the cpu.
6732 */
Michael Chanec41c7d2006-01-17 02:40:55 -08006733 lock_err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006734 err = tg3_halt_cpu(tp, cpu_base);
Michael Chanec41c7d2006-01-17 02:40:55 -08006735 if (!lock_err)
6736 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006737 if (err)
6738 goto out;
6739
6740 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
6741 write_op(tp, cpu_scratch_base + i, 0);
6742 tw32(cpu_base + CPU_STATE, 0xffffffff);
6743 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006744 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006745 write_op(tp, (cpu_scratch_base +
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006746 (info->fw_base & 0xffff) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07006747 (i * sizeof(u32))),
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006748 be32_to_cpu(info->fw_data[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006749
6750 err = 0;
6751
6752out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006753 return err;
6754}
6755
6756/* tp->lock is held. */
6757static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
6758{
6759 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006760 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006761 int err, i;
6762
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006763 fw_data = (void *)tp->fw->data;
6764
6765 /* Firmware blob starts with version numbers, followed by
6766 start address and length. We are setting complete length.
6767 length = end_address_of_bss - start_address_of_text.
6768 Remainder is the blob to be loaded contiguously
6769 from start address. */
6770
6771 info.fw_base = be32_to_cpu(fw_data[1]);
6772 info.fw_len = tp->fw->size - 12;
6773 info.fw_data = &fw_data[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006774
6775 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
6776 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
6777 &info);
6778 if (err)
6779 return err;
6780
6781 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
6782 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
6783 &info);
6784 if (err)
6785 return err;
6786
6787 /* Now startup only the RX cpu. */
6788 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006789 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006790
6791 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006792 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006793 break;
6794 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
6795 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006796 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006797 udelay(1000);
6798 }
6799 if (i >= 5) {
6800 printk(KERN_ERR PFX "tg3_load_firmware fails for %s "
6801 "to set RX CPU PC, is %08x should be %08x\n",
6802 tp->dev->name, tr32(RX_CPU_BASE + CPU_PC),
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006803 info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006804 return -ENODEV;
6805 }
6806 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
6807 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
6808
6809 return 0;
6810}
6811
Linus Torvalds1da177e2005-04-16 15:20:36 -07006812/* 5705 needs a special version of the TSO firmware. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006813
6814/* tp->lock is held. */
6815static int tg3_load_tso_firmware(struct tg3 *tp)
6816{
6817 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006818 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006819 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
6820 int err, i;
6821
6822 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
6823 return 0;
6824
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006825 fw_data = (void *)tp->fw->data;
6826
6827 /* Firmware blob starts with version numbers, followed by
6828 start address and length. We are setting complete length.
6829 length = end_address_of_bss - start_address_of_text.
6830 Remainder is the blob to be loaded contiguously
6831 from start address. */
6832
6833 info.fw_base = be32_to_cpu(fw_data[1]);
6834 cpu_scratch_size = tp->fw_len;
6835 info.fw_len = tp->fw->size - 12;
6836 info.fw_data = &fw_data[3];
6837
Linus Torvalds1da177e2005-04-16 15:20:36 -07006838 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006839 cpu_base = RX_CPU_BASE;
6840 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006841 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006842 cpu_base = TX_CPU_BASE;
6843 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
6844 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
6845 }
6846
6847 err = tg3_load_firmware_cpu(tp, cpu_base,
6848 cpu_scratch_base, cpu_scratch_size,
6849 &info);
6850 if (err)
6851 return err;
6852
6853 /* Now startup the cpu. */
6854 tw32(cpu_base + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006855 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006856
6857 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006858 if (tr32(cpu_base + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006859 break;
6860 tw32(cpu_base + CPU_STATE, 0xffffffff);
6861 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006862 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006863 udelay(1000);
6864 }
6865 if (i >= 5) {
6866 printk(KERN_ERR PFX "tg3_load_tso_firmware fails for %s "
6867 "to set CPU PC, is %08x should be %08x\n",
6868 tp->dev->name, tr32(cpu_base + CPU_PC),
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006869 info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006870 return -ENODEV;
6871 }
6872 tw32(cpu_base + CPU_STATE, 0xffffffff);
6873 tw32_f(cpu_base + CPU_MODE, 0x00000000);
6874 return 0;
6875}
6876
Linus Torvalds1da177e2005-04-16 15:20:36 -07006877
Linus Torvalds1da177e2005-04-16 15:20:36 -07006878static int tg3_set_mac_addr(struct net_device *dev, void *p)
6879{
6880 struct tg3 *tp = netdev_priv(dev);
6881 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07006882 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006883
Michael Chanf9804dd2005-09-27 12:13:10 -07006884 if (!is_valid_ether_addr(addr->sa_data))
6885 return -EINVAL;
6886
Linus Torvalds1da177e2005-04-16 15:20:36 -07006887 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
6888
Michael Chane75f7c92006-03-20 21:33:26 -08006889 if (!netif_running(dev))
6890 return 0;
6891
Michael Chan58712ef2006-04-29 18:58:01 -07006892 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) {
Michael Chan986e0ae2007-05-05 12:10:20 -07006893 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07006894
Michael Chan986e0ae2007-05-05 12:10:20 -07006895 addr0_high = tr32(MAC_ADDR_0_HIGH);
6896 addr0_low = tr32(MAC_ADDR_0_LOW);
6897 addr1_high = tr32(MAC_ADDR_1_HIGH);
6898 addr1_low = tr32(MAC_ADDR_1_LOW);
6899
6900 /* Skip MAC addr 1 if ASF is using it. */
6901 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
6902 !(addr1_high == 0 && addr1_low == 0))
6903 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07006904 }
Michael Chan986e0ae2007-05-05 12:10:20 -07006905 spin_lock_bh(&tp->lock);
6906 __tg3_set_mac_addr(tp, skip_mac_1);
6907 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006908
Michael Chanb9ec6c12006-07-25 16:37:27 -07006909 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006910}
6911
6912/* tp->lock is held. */
6913static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
6914 dma_addr_t mapping, u32 maxlen_flags,
6915 u32 nic_addr)
6916{
6917 tg3_write_mem(tp,
6918 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
6919 ((u64) mapping >> 32));
6920 tg3_write_mem(tp,
6921 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
6922 ((u64) mapping & 0xffffffff));
6923 tg3_write_mem(tp,
6924 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
6925 maxlen_flags);
6926
6927 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
6928 tg3_write_mem(tp,
6929 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
6930 nic_addr);
6931}
6932
6933static void __tg3_set_rx_mode(struct net_device *);
Michael Chand244c892005-07-05 14:42:33 -07006934static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07006935{
Matt Carlsonb6080e12009-09-01 13:12:00 +00006936 int i;
6937
6938 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSIX)) {
6939 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
6940 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
6941 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
6942
6943 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
6944 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
6945 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
6946 } else {
6947 tw32(HOSTCC_TXCOL_TICKS, 0);
6948 tw32(HOSTCC_TXMAX_FRAMES, 0);
6949 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
6950
6951 tw32(HOSTCC_RXCOL_TICKS, 0);
6952 tw32(HOSTCC_RXMAX_FRAMES, 0);
6953 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07006954 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00006955
David S. Miller15f98502005-05-18 22:49:26 -07006956 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
6957 u32 val = ec->stats_block_coalesce_usecs;
6958
Matt Carlsonb6080e12009-09-01 13:12:00 +00006959 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
6960 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
6961
David S. Miller15f98502005-05-18 22:49:26 -07006962 if (!netif_carrier_ok(tp->dev))
6963 val = 0;
6964
6965 tw32(HOSTCC_STAT_COAL_TICKS, val);
6966 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00006967
6968 for (i = 0; i < tp->irq_cnt - 1; i++) {
6969 u32 reg;
6970
6971 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
6972 tw32(reg, ec->rx_coalesce_usecs);
6973 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
6974 tw32(reg, ec->tx_coalesce_usecs);
6975 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
6976 tw32(reg, ec->rx_max_coalesced_frames);
6977 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
6978 tw32(reg, ec->tx_max_coalesced_frames);
6979 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
6980 tw32(reg, ec->rx_max_coalesced_frames_irq);
6981 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
6982 tw32(reg, ec->tx_max_coalesced_frames_irq);
6983 }
6984
6985 for (; i < tp->irq_max - 1; i++) {
6986 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
6987 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
6988 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
6989 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
6990 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
6991 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
6992 }
David S. Miller15f98502005-05-18 22:49:26 -07006993}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006994
6995/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00006996static void tg3_rings_reset(struct tg3 *tp)
6997{
6998 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006999 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007000 struct tg3_napi *tnapi = &tp->napi[0];
7001
7002 /* Disable all transmit rings but the first. */
7003 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7004 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
7005 else
7006 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7007
7008 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7009 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
7010 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
7011 BDINFO_FLAGS_DISABLED);
7012
7013
7014 /* Disable all receive return rings but the first. */
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007015 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
7016 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
7017 else if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00007018 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
7019 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
7020 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
7021 else
7022 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7023
7024 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7025 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
7026 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
7027 BDINFO_FLAGS_DISABLED);
7028
7029 /* Disable interrupts */
7030 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
7031
7032 /* Zero mailbox registers. */
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007033 if (tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX) {
7034 for (i = 1; i < TG3_IRQ_MAX_VECS; i++) {
7035 tp->napi[i].tx_prod = 0;
7036 tp->napi[i].tx_cons = 0;
7037 tw32_mailbox(tp->napi[i].prodmbox, 0);
7038 tw32_rx_mbox(tp->napi[i].consmbox, 0);
7039 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
7040 }
7041 } else {
7042 tp->napi[0].tx_prod = 0;
7043 tp->napi[0].tx_cons = 0;
7044 tw32_mailbox(tp->napi[0].prodmbox, 0);
7045 tw32_rx_mbox(tp->napi[0].consmbox, 0);
7046 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007047
7048 /* Make sure the NIC-based send BD rings are disabled. */
7049 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7050 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
7051 for (i = 0; i < 16; i++)
7052 tw32_tx_mbox(mbox + i * 8, 0);
7053 }
7054
7055 txrcb = NIC_SRAM_SEND_RCB;
7056 rxrcb = NIC_SRAM_RCV_RET_RCB;
7057
7058 /* Clear status block in ram. */
7059 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7060
7061 /* Set status block DMA address */
7062 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
7063 ((u64) tnapi->status_mapping >> 32));
7064 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
7065 ((u64) tnapi->status_mapping & 0xffffffff));
7066
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007067 if (tnapi->tx_ring) {
7068 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7069 (TG3_TX_RING_SIZE <<
7070 BDINFO_FLAGS_MAXLEN_SHIFT),
7071 NIC_SRAM_TX_BUFFER_DESC);
7072 txrcb += TG3_BDINFO_SIZE;
7073 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007074
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007075 if (tnapi->rx_rcb) {
7076 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
7077 (TG3_RX_RCB_RING_SIZE(tp) <<
7078 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
7079 rxrcb += TG3_BDINFO_SIZE;
7080 }
7081
7082 stblk = HOSTCC_STATBLCK_RING1;
7083
7084 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
7085 u64 mapping = (u64)tnapi->status_mapping;
7086 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
7087 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
7088
7089 /* Clear status block in ram. */
7090 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7091
7092 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7093 (TG3_TX_RING_SIZE <<
7094 BDINFO_FLAGS_MAXLEN_SHIFT),
7095 NIC_SRAM_TX_BUFFER_DESC);
7096
7097 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
7098 (TG3_RX_RCB_RING_SIZE(tp) <<
7099 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
7100
7101 stblk += 8;
7102 txrcb += TG3_BDINFO_SIZE;
7103 rxrcb += TG3_BDINFO_SIZE;
7104 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007105}
7106
7107/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07007108static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007109{
7110 u32 val, rdmac_mode;
7111 int i, err, limit;
Matt Carlson21f581a2009-08-28 14:00:25 +00007112 struct tg3_rx_prodring_set *tpr = &tp->prodring[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007113
7114 tg3_disable_ints(tp);
7115
7116 tg3_stop_fw(tp);
7117
7118 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
7119
7120 if (tp->tg3_flags & TG3_FLAG_INIT_COMPLETE) {
Michael Chane6de8ad2005-05-05 14:42:41 -07007121 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007122 }
7123
Matt Carlsondd477002008-05-25 23:45:58 -07007124 if (reset_phy &&
7125 !(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB))
Michael Chand4d2c552006-03-20 17:47:20 -08007126 tg3_phy_reset(tp);
7127
Linus Torvalds1da177e2005-04-16 15:20:36 -07007128 err = tg3_chip_reset(tp);
7129 if (err)
7130 return err;
7131
7132 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
7133
Matt Carlsonbcb37f62008-11-03 16:52:09 -08007134 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07007135 val = tr32(TG3_CPMU_CTRL);
7136 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
7137 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08007138
7139 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
7140 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
7141 val |= CPMU_LSPD_10MB_MACCLK_6_25;
7142 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
7143
7144 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
7145 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
7146 val |= CPMU_LNK_AWARE_MACCLK_6_25;
7147 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
7148
7149 val = tr32(TG3_CPMU_HST_ACC);
7150 val &= ~CPMU_HST_ACC_MACCLK_MASK;
7151 val |= CPMU_HST_ACC_MACCLK_6_25;
7152 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07007153 }
7154
Matt Carlson33466d92009-04-20 06:57:41 +00007155 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7156 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
7157 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
7158 PCIE_PWR_MGMT_L1_THRESH_4MS;
7159 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00007160
7161 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
7162 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
7163
7164 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00007165 }
7166
Matt Carlson255ca312009-08-25 10:07:27 +00007167 if (tp->tg3_flags3 & TG3_FLG3_TOGGLE_10_100_L1PLLPD) {
7168 val = tr32(TG3_PCIE_LNKCTL);
7169 if (tp->tg3_flags3 & TG3_FLG3_CLKREQ_BUG)
7170 val |= TG3_PCIE_LNKCTL_L1_PLL_PD_DIS;
7171 else
7172 val &= ~TG3_PCIE_LNKCTL_L1_PLL_PD_DIS;
7173 tw32(TG3_PCIE_LNKCTL, val);
7174 }
7175
Linus Torvalds1da177e2005-04-16 15:20:36 -07007176 /* This works around an issue with Athlon chipsets on
7177 * B3 tigon3 silicon. This bit has no effect on any
7178 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07007179 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007180 */
Matt Carlson795d01c2007-10-07 23:28:17 -07007181 if (!(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT)) {
7182 if (!(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
7183 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
7184 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007186
7187 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
7188 (tp->tg3_flags & TG3_FLAG_PCIX_MODE)) {
7189 val = tr32(TG3PCI_PCISTATE);
7190 val |= PCISTATE_RETRY_SAME_DMA;
7191 tw32(TG3PCI_PCISTATE, val);
7192 }
7193
Matt Carlson0d3031d2007-10-10 18:02:43 -07007194 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
7195 /* Allow reads and writes to the
7196 * APE register and memory space.
7197 */
7198 val = tr32(TG3PCI_PCISTATE);
7199 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
7200 PCISTATE_ALLOW_APE_SHMEM_WR;
7201 tw32(TG3PCI_PCISTATE, val);
7202 }
7203
Linus Torvalds1da177e2005-04-16 15:20:36 -07007204 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
7205 /* Enable some hw fixes. */
7206 val = tr32(TG3PCI_MSI_DATA);
7207 val |= (1 << 26) | (1 << 28) | (1 << 29);
7208 tw32(TG3PCI_MSI_DATA, val);
7209 }
7210
7211 /* Descriptor ring init may make accesses to the
7212 * NIC SRAM area to setup the TX descriptors, so we
7213 * can only do this after the hardware has been
7214 * successfully reset.
7215 */
Michael Chan32d8c572006-07-25 16:38:29 -07007216 err = tg3_init_rings(tp);
7217 if (err)
7218 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007219
Matt Carlson9936bcf2007-10-10 18:03:07 -07007220 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007221 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761 &&
7222 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07007223 /* This value is determined during the probe time DMA
7224 * engine test, tg3_test_dma.
7225 */
7226 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
7227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007228
7229 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
7230 GRC_MODE_4X_NIC_SEND_RINGS |
7231 GRC_MODE_NO_TX_PHDR_CSUM |
7232 GRC_MODE_NO_RX_PHDR_CSUM);
7233 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07007234
7235 /* Pseudo-header checksum is done by hardware logic and not
7236 * the offload processers, so make the chip do the pseudo-
7237 * header checksums on receive. For transmit it is more
7238 * convenient to do the pseudo-header checksum in software
7239 * as Linux does that on transmit for us in all cases.
7240 */
7241 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007242
7243 tw32(GRC_MODE,
7244 tp->grc_mode |
7245 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
7246
7247 /* Setup the timer prescalar register. Clock is always 66Mhz. */
7248 val = tr32(GRC_MISC_CFG);
7249 val &= ~0xff;
7250 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
7251 tw32(GRC_MISC_CFG, val);
7252
7253 /* Initialize MBUF/DESC pool. */
John W. Linvillecbf46852005-04-21 17:01:29 -07007254 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007255 /* Do nothing. */
7256 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
7257 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
7258 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
7259 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
7260 else
7261 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
7262 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
7263 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
7264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007265 else if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) {
7266 int fw_len;
7267
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007268 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007269 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
7270 tw32(BUFMGR_MB_POOL_ADDR,
7271 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
7272 tw32(BUFMGR_MB_POOL_SIZE,
7273 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
7274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007275
Michael Chan0f893dc2005-07-25 12:30:38 -07007276 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007277 tw32(BUFMGR_MB_RDMA_LOW_WATER,
7278 tp->bufmgr_config.mbuf_read_dma_low_water);
7279 tw32(BUFMGR_MB_MACRX_LOW_WATER,
7280 tp->bufmgr_config.mbuf_mac_rx_low_water);
7281 tw32(BUFMGR_MB_HIGH_WATER,
7282 tp->bufmgr_config.mbuf_high_water);
7283 } else {
7284 tw32(BUFMGR_MB_RDMA_LOW_WATER,
7285 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
7286 tw32(BUFMGR_MB_MACRX_LOW_WATER,
7287 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
7288 tw32(BUFMGR_MB_HIGH_WATER,
7289 tp->bufmgr_config.mbuf_high_water_jumbo);
7290 }
7291 tw32(BUFMGR_DMA_LOW_WATER,
7292 tp->bufmgr_config.dma_low_water);
7293 tw32(BUFMGR_DMA_HIGH_WATER,
7294 tp->bufmgr_config.dma_high_water);
7295
7296 tw32(BUFMGR_MODE, BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE);
7297 for (i = 0; i < 2000; i++) {
7298 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
7299 break;
7300 udelay(10);
7301 }
7302 if (i >= 2000) {
7303 printk(KERN_ERR PFX "tg3_reset_hw cannot enable BUFMGR for %s.\n",
7304 tp->dev->name);
7305 return -ENODEV;
7306 }
7307
7308 /* Setup replenish threshold. */
Michael Chanf92905d2006-06-29 20:14:29 -07007309 val = tp->rx_pending / 8;
7310 if (val == 0)
7311 val = 1;
7312 else if (val > tp->rx_std_max_post)
7313 val = tp->rx_std_max_post;
Michael Chanb5d37722006-09-27 16:06:21 -07007314 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7315 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
7316 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
7317
7318 if (val > (TG3_RX_INTERNAL_RING_SZ_5906 / 2))
7319 val = TG3_RX_INTERNAL_RING_SZ_5906 / 2;
7320 }
Michael Chanf92905d2006-06-29 20:14:29 -07007321
7322 tw32(RCVBDI_STD_THRESH, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007323
7324 /* Initialize TG3_BDINFO's at:
7325 * RCVDBDI_STD_BD: standard eth size rx ring
7326 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
7327 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
7328 *
7329 * like so:
7330 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
7331 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
7332 * ring attribute flags
7333 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
7334 *
7335 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
7336 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
7337 *
7338 * The size of each ring is fixed in the firmware, but the location is
7339 * configurable.
7340 */
7341 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00007342 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007343 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00007344 ((u64) tpr->rx_std_mapping & 0xffffffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007345 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
7346 NIC_SRAM_RX_BUFFER_DESC);
7347
Matt Carlsonfdb72b32009-08-28 13:57:12 +00007348 /* Disable the mini ring */
7349 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007350 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
7351 BDINFO_FLAGS_DISABLED);
7352
Matt Carlsonfdb72b32009-08-28 13:57:12 +00007353 /* Program the jumbo buffer descriptor ring control
7354 * blocks on those devices that have them.
7355 */
Matt Carlson8f666b02009-08-28 13:58:24 +00007356 if ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) &&
Matt Carlsonfdb72b32009-08-28 13:57:12 +00007357 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007358 /* Setup replenish threshold. */
7359 tw32(RCVBDI_JUMBO_THRESH, tp->rx_jumbo_pending / 8);
7360
Michael Chan0f893dc2005-07-25 12:30:38 -07007361 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007362 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00007363 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007364 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00007365 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007366 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlson79ed5ac2009-08-28 14:00:55 +00007367 (RX_JUMBO_MAX_SIZE << BDINFO_FLAGS_MAXLEN_SHIFT) |
7368 BDINFO_FLAGS_USE_EXT_RECV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007369 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
7370 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
7371 } else {
7372 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
7373 BDINFO_FLAGS_DISABLED);
7374 }
7375
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007376 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
7377 val = (RX_STD_MAX_SIZE_5705 << BDINFO_FLAGS_MAXLEN_SHIFT) |
7378 (RX_STD_MAX_SIZE << 2);
7379 else
7380 val = RX_STD_MAX_SIZE << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00007381 } else
7382 val = RX_STD_MAX_SIZE_5705 << BDINFO_FLAGS_MAXLEN_SHIFT;
7383
7384 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007385
Matt Carlson21f581a2009-08-28 14:00:25 +00007386 tpr->rx_std_ptr = tp->rx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007387 tw32_rx_mbox(MAILBOX_RCV_STD_PROD_IDX + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00007388 tpr->rx_std_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007389
Matt Carlson21f581a2009-08-28 14:00:25 +00007390 tpr->rx_jmb_ptr = (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) ?
7391 tp->rx_jumbo_pending : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007392 tw32_rx_mbox(MAILBOX_RCV_JUMBO_PROD_IDX + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00007393 tpr->rx_jmb_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007394
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007395 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
7396 tw32(STD_REPLENISH_LWM, 32);
7397 tw32(JMB_REPLENISH_LWM, 16);
7398 }
7399
Matt Carlson2d31eca2009-09-01 12:53:31 +00007400 tg3_rings_reset(tp);
7401
Linus Torvalds1da177e2005-04-16 15:20:36 -07007402 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07007403 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007404
7405 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00007406 tw32(MAC_RX_MTU_SIZE,
7407 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007408
7409 /* The slot time is changed by tg3_setup_phy if we
7410 * run at gigabit with half duplex.
7411 */
7412 tw32(MAC_TX_LENGTHS,
7413 (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
7414 (6 << TX_LENGTHS_IPG_SHIFT) |
7415 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
7416
7417 /* Receive rules. */
7418 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
7419 tw32(RCVLPC_CONFIG, 0x0181);
7420
7421 /* Calculate RDMAC_MODE setting early, we need it to determine
7422 * the RCVLPC_STATE_ENABLE mask.
7423 */
7424 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
7425 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
7426 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
7427 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
7428 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07007429
Matt Carlson57e69832008-05-25 23:48:31 -07007430 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08007431 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
7432 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07007433 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
7434 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
7435 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
7436
Michael Chan85e94ce2005-04-21 17:05:28 -07007437 /* If statement applies to 5705 and 5750 PCI devices only */
7438 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
7439 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) ||
7440 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007441 if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07007442 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007443 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
7444 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
7445 !(tp->tg3_flags2 & TG3_FLG2_IS_5788)) {
7446 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
7447 }
7448 }
7449
Michael Chan85e94ce2005-04-21 17:05:28 -07007450 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)
7451 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
7452
Linus Torvalds1da177e2005-04-16 15:20:36 -07007453 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
Matt Carlson027455a2008-12-21 20:19:30 -08007454 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
7455
7456 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
7457 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
7458 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007459
7460 /* Receive/send statistics. */
Michael Chan16613942006-06-29 20:15:13 -07007461 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
7462 val = tr32(RCVLPC_STATS_ENABLE);
7463 val &= ~RCVLPC_STATSENAB_DACK_FIX;
7464 tw32(RCVLPC_STATS_ENABLE, val);
7465 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
7466 (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007467 val = tr32(RCVLPC_STATS_ENABLE);
7468 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
7469 tw32(RCVLPC_STATS_ENABLE, val);
7470 } else {
7471 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
7472 }
7473 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
7474 tw32(SNDDATAI_STATSENAB, 0xffffff);
7475 tw32(SNDDATAI_STATSCTRL,
7476 (SNDDATAI_SCTRL_ENABLE |
7477 SNDDATAI_SCTRL_FASTUPD));
7478
7479 /* Setup host coalescing engine. */
7480 tw32(HOSTCC_MODE, 0);
7481 for (i = 0; i < 2000; i++) {
7482 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
7483 break;
7484 udelay(10);
7485 }
7486
Michael Chand244c892005-07-05 14:42:33 -07007487 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007488
Linus Torvalds1da177e2005-04-16 15:20:36 -07007489 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7490 /* Status/statistics block address. See tg3_timer,
7491 * the tg3_periodic_fetch_stats call there, and
7492 * tg3_get_stats to see how this works for 5705/5750 chips.
7493 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007494 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
7495 ((u64) tp->stats_mapping >> 32));
7496 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
7497 ((u64) tp->stats_mapping & 0xffffffff));
7498 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00007499
Linus Torvalds1da177e2005-04-16 15:20:36 -07007500 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00007501
7502 /* Clear statistics and status block memory areas */
7503 for (i = NIC_SRAM_STATS_BLK;
7504 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
7505 i += sizeof(u32)) {
7506 tg3_write_mem(tp, i, 0);
7507 udelay(40);
7508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007509 }
7510
7511 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
7512
7513 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
7514 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
7515 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7516 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
7517
Michael Chanc94e3942005-09-27 12:12:42 -07007518 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES) {
7519 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
7520 /* reset to prevent losing 1st rx packet intermittently */
7521 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
7522 udelay(10);
7523 }
7524
Matt Carlson3bda1252008-08-15 14:08:22 -07007525 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
7526 tp->mac_mode &= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
7527 else
7528 tp->mac_mode = 0;
7529 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Linus Torvalds1da177e2005-04-16 15:20:36 -07007530 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE | MAC_MODE_FHDE_ENABLE;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07007531 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
7532 !(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
7533 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
7534 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007535 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
7536 udelay(40);
7537
Michael Chan314fba32005-04-21 17:07:04 -07007538 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Michael Chan9d26e212006-12-07 00:21:14 -08007539 * If TG3_FLG2_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07007540 * register to preserve the GPIO settings for LOMs. The GPIOs,
7541 * whether used as inputs or outputs, are set by boot code after
7542 * reset.
7543 */
Michael Chan9d26e212006-12-07 00:21:14 -08007544 if (!(tp->tg3_flags2 & TG3_FLG2_IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07007545 u32 gpio_mask;
7546
Michael Chan9d26e212006-12-07 00:21:14 -08007547 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
7548 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
7549 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07007550
7551 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
7552 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
7553 GRC_LCLCTRL_GPIO_OUTPUT3;
7554
Michael Chanaf36e6b2006-03-23 01:28:06 -08007555 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
7556 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
7557
Gary Zambranoaaf84462007-05-05 11:51:45 -07007558 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07007559 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
7560
7561 /* GPIO1 must be driven high for eeprom write protect */
Michael Chan9d26e212006-12-07 00:21:14 -08007562 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT)
7563 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
7564 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07007565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007566 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
7567 udelay(100);
7568
Matt Carlsonbaf8a942009-09-01 13:13:00 +00007569 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX) {
7570 val = tr32(MSGINT_MODE);
7571 val |= MSGINT_MODE_MULTIVEC_EN | MSGINT_MODE_ENABLE;
7572 tw32(MSGINT_MODE, val);
7573 }
7574
Linus Torvalds1da177e2005-04-16 15:20:36 -07007575 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7576 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
7577 udelay(40);
7578 }
7579
7580 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
7581 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
7582 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
7583 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
7584 WDMAC_MODE_LNGREAD_ENAB);
7585
Michael Chan85e94ce2005-04-21 17:05:28 -07007586 /* If statement applies to 5705 and 5750 PCI devices only */
7587 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
7588 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) ||
7589 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750) {
Matt Carlson29ea0952009-08-25 10:07:54 +00007590 if ((tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07007591 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
7592 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
7593 /* nothing */
7594 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
7595 !(tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
7596 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)) {
7597 val |= WDMAC_MODE_RX_ACCEL;
7598 }
7599 }
7600
Michael Chand9ab5ad12006-03-20 22:27:35 -08007601 /* Enable host coalescing bug fix */
Matt Carlson321d32a2008-11-21 17:22:19 -08007602 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Matt Carlsonf51f3562008-05-25 23:45:08 -07007603 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad12006-03-20 22:27:35 -08007604
Linus Torvalds1da177e2005-04-16 15:20:36 -07007605 tw32_f(WDMAC_MODE, val);
7606 udelay(40);
7607
Matt Carlson9974a352007-10-07 23:27:28 -07007608 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
7609 u16 pcix_cmd;
7610
7611 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7612 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007613 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07007614 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
7615 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007616 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07007617 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
7618 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007619 }
Matt Carlson9974a352007-10-07 23:27:28 -07007620 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7621 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007622 }
7623
7624 tw32_f(RDMAC_MODE, rdmac_mode);
7625 udelay(40);
7626
7627 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
7628 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7629 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07007630
7631 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
7632 tw32(SNDDATAC_MODE,
7633 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
7634 else
7635 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
7636
Linus Torvalds1da177e2005-04-16 15:20:36 -07007637 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
7638 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
7639 tw32(RCVDBDI_MODE, RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ);
7640 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007641 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
7642 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00007643 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
7644 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
7645 val |= SNDBDI_MODE_MULTI_TXQ_EN;
7646 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007647 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
7648
7649 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
7650 err = tg3_load_5701_a0_firmware_fix(tp);
7651 if (err)
7652 return err;
7653 }
7654
Linus Torvalds1da177e2005-04-16 15:20:36 -07007655 if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) {
7656 err = tg3_load_tso_firmware(tp);
7657 if (err)
7658 return err;
7659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007660
7661 tp->tx_mode = TX_MODE_ENABLE;
7662 tw32_f(MAC_TX_MODE, tp->tx_mode);
7663 udelay(100);
7664
Matt Carlsonbaf8a942009-09-01 13:13:00 +00007665 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS) {
7666 u32 reg = MAC_RSS_INDIR_TBL_0;
7667 u8 *ent = (u8 *)&val;
7668
7669 /* Setup the indirection table */
7670 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
7671 int idx = i % sizeof(val);
7672
7673 ent[idx] = i % (tp->irq_cnt - 1);
7674 if (idx == sizeof(val) - 1) {
7675 tw32(reg, val);
7676 reg += 4;
7677 }
7678 }
7679
7680 /* Setup the "secret" hash key. */
7681 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
7682 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
7683 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
7684 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
7685 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
7686 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
7687 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
7688 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
7689 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
7690 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
7691 }
7692
Linus Torvalds1da177e2005-04-16 15:20:36 -07007693 tp->rx_mode = RX_MODE_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08007694 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Michael Chanaf36e6b2006-03-23 01:28:06 -08007695 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
7696
Matt Carlsonbaf8a942009-09-01 13:13:00 +00007697 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)
7698 tp->rx_mode |= RX_MODE_RSS_ENABLE |
7699 RX_MODE_RSS_ITBL_HASH_BITS_7 |
7700 RX_MODE_RSS_IPV6_HASH_EN |
7701 RX_MODE_RSS_TCP_IPV6_HASH_EN |
7702 RX_MODE_RSS_IPV4_HASH_EN |
7703 RX_MODE_RSS_TCP_IPV4_HASH_EN;
7704
Linus Torvalds1da177e2005-04-16 15:20:36 -07007705 tw32_f(MAC_RX_MODE, tp->rx_mode);
7706 udelay(10);
7707
Linus Torvalds1da177e2005-04-16 15:20:36 -07007708 tw32(MAC_LED_CTRL, tp->led_ctrl);
7709
7710 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Michael Chanc94e3942005-09-27 12:12:42 -07007711 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007712 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
7713 udelay(10);
7714 }
7715 tw32_f(MAC_RX_MODE, tp->rx_mode);
7716 udelay(10);
7717
7718 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
7719 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
7720 !(tp->tg3_flags2 & TG3_FLG2_SERDES_PREEMPHASIS)) {
7721 /* Set drive transmission level to 1.2V */
7722 /* only if the signal pre-emphasis bit is not set */
7723 val = tr32(MAC_SERDES_CFG);
7724 val &= 0xfffff000;
7725 val |= 0x880;
7726 tw32(MAC_SERDES_CFG, val);
7727 }
7728 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
7729 tw32(MAC_SERDES_CFG, 0x616000);
7730 }
7731
7732 /* Prevent chip from dropping frames when flow control
7733 * is enabled.
7734 */
7735 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, 2);
7736
7737 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
7738 (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)) {
7739 /* Use hardware link auto-negotiation */
7740 tp->tg3_flags2 |= TG3_FLG2_HW_AUTONEG;
7741 }
7742
Michael Chand4d2c552006-03-20 17:47:20 -08007743 if ((tp->tg3_flags2 & TG3_FLG2_MII_SERDES) &&
7744 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)) {
7745 u32 tmp;
7746
7747 tmp = tr32(SERDES_RX_CTRL);
7748 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
7749 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
7750 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
7751 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
7752 }
7753
Matt Carlsondd477002008-05-25 23:45:58 -07007754 if (!(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)) {
7755 if (tp->link_config.phy_is_low_power) {
7756 tp->link_config.phy_is_low_power = 0;
7757 tp->link_config.speed = tp->link_config.orig_speed;
7758 tp->link_config.duplex = tp->link_config.orig_duplex;
7759 tp->link_config.autoneg = tp->link_config.orig_autoneg;
7760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007761
Matt Carlsondd477002008-05-25 23:45:58 -07007762 err = tg3_setup_phy(tp, 0);
7763 if (err)
7764 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007765
Matt Carlsondd477002008-05-25 23:45:58 -07007766 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
Matt Carlson7f97a4b2009-08-25 10:10:03 +00007767 !(tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07007768 u32 tmp;
7769
7770 /* Clear CRC stats. */
7771 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
7772 tg3_writephy(tp, MII_TG3_TEST1,
7773 tmp | MII_TG3_TEST1_CRC_EN);
7774 tg3_readphy(tp, 0x14, &tmp);
7775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007776 }
7777 }
7778
7779 __tg3_set_rx_mode(tp->dev);
7780
7781 /* Initialize receive rules. */
7782 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
7783 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
7784 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
7785 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
7786
Michael Chan4cf78e42005-07-25 12:29:19 -07007787 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Michael Chana4e2b342005-10-26 15:46:52 -07007788 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007789 limit = 8;
7790 else
7791 limit = 16;
7792 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF)
7793 limit -= 4;
7794 switch (limit) {
7795 case 16:
7796 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
7797 case 15:
7798 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
7799 case 14:
7800 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
7801 case 13:
7802 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
7803 case 12:
7804 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
7805 case 11:
7806 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
7807 case 10:
7808 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
7809 case 9:
7810 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
7811 case 8:
7812 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
7813 case 7:
7814 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
7815 case 6:
7816 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
7817 case 5:
7818 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
7819 case 4:
7820 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
7821 case 3:
7822 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
7823 case 2:
7824 case 1:
7825
7826 default:
7827 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007829
Matt Carlson9ce768e2007-10-11 19:49:11 -07007830 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
7831 /* Write our heartbeat update interval to APE. */
7832 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
7833 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07007834
Linus Torvalds1da177e2005-04-16 15:20:36 -07007835 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
7836
Linus Torvalds1da177e2005-04-16 15:20:36 -07007837 return 0;
7838}
7839
7840/* Called at device open time to get the chip ready for
7841 * packet processing. Invoked with tp->lock held.
7842 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07007843static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007844{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007845 tg3_switch_clocks(tp);
7846
7847 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
7848
Matt Carlson2f751b62008-08-04 23:17:34 -07007849 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007850}
7851
7852#define TG3_STAT_ADD32(PSTAT, REG) \
7853do { u32 __val = tr32(REG); \
7854 (PSTAT)->low += __val; \
7855 if ((PSTAT)->low < __val) \
7856 (PSTAT)->high += 1; \
7857} while (0)
7858
7859static void tg3_periodic_fetch_stats(struct tg3 *tp)
7860{
7861 struct tg3_hw_stats *sp = tp->hw_stats;
7862
7863 if (!netif_carrier_ok(tp->dev))
7864 return;
7865
7866 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
7867 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
7868 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
7869 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
7870 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
7871 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
7872 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
7873 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
7874 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
7875 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
7876 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
7877 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
7878 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
7879
7880 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
7881 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
7882 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
7883 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
7884 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
7885 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
7886 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
7887 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
7888 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
7889 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
7890 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
7891 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
7892 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
7893 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07007894
7895 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
7896 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
7897 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007898}
7899
7900static void tg3_timer(unsigned long __opaque)
7901{
7902 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007903
Michael Chanf475f162006-03-27 23:20:14 -08007904 if (tp->irq_sync)
7905 goto restart_timer;
7906
David S. Millerf47c11e2005-06-24 20:18:35 -07007907 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007908
David S. Millerfac9b832005-05-18 22:46:34 -07007909 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)) {
7910 /* All of this garbage is because when using non-tagged
7911 * IRQ status the mailbox/status_block protocol the chip
7912 * uses with the cpu is race prone.
7913 */
Matt Carlson898a56f2009-08-28 14:02:40 +00007914 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -07007915 tw32(GRC_LOCAL_CTRL,
7916 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
7917 } else {
7918 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00007919 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -07007920 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007921
David S. Millerfac9b832005-05-18 22:46:34 -07007922 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
7923 tp->tg3_flags2 |= TG3_FLG2_RESTART_TIMER;
David S. Millerf47c11e2005-06-24 20:18:35 -07007924 spin_unlock(&tp->lock);
David S. Millerfac9b832005-05-18 22:46:34 -07007925 schedule_work(&tp->reset_task);
7926 return;
7927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007928 }
7929
Linus Torvalds1da177e2005-04-16 15:20:36 -07007930 /* This part only runs once per second. */
7931 if (!--tp->timer_counter) {
David S. Millerfac9b832005-05-18 22:46:34 -07007932 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
7933 tg3_periodic_fetch_stats(tp);
7934
Linus Torvalds1da177e2005-04-16 15:20:36 -07007935 if (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) {
7936 u32 mac_stat;
7937 int phy_event;
7938
7939 mac_stat = tr32(MAC_STATUS);
7940
7941 phy_event = 0;
7942 if (tp->tg3_flags & TG3_FLAG_USE_MI_INTERRUPT) {
7943 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
7944 phy_event = 1;
7945 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
7946 phy_event = 1;
7947
7948 if (phy_event)
7949 tg3_setup_phy(tp, 0);
7950 } else if (tp->tg3_flags & TG3_FLAG_POLL_SERDES) {
7951 u32 mac_stat = tr32(MAC_STATUS);
7952 int need_setup = 0;
7953
7954 if (netif_carrier_ok(tp->dev) &&
7955 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
7956 need_setup = 1;
7957 }
7958 if (! netif_carrier_ok(tp->dev) &&
7959 (mac_stat & (MAC_STATUS_PCS_SYNCED |
7960 MAC_STATUS_SIGNAL_DET))) {
7961 need_setup = 1;
7962 }
7963 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07007964 if (!tp->serdes_counter) {
7965 tw32_f(MAC_MODE,
7966 (tp->mac_mode &
7967 ~MAC_MODE_PORT_MODE_MASK));
7968 udelay(40);
7969 tw32_f(MAC_MODE, tp->mac_mode);
7970 udelay(40);
7971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007972 tg3_setup_phy(tp, 0);
7973 }
Michael Chan747e8f82005-07-25 12:33:22 -07007974 } else if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)
7975 tg3_serdes_parallel_detect(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007976
7977 tp->timer_counter = tp->timer_multiplier;
7978 }
7979
Michael Chan130b8e42006-09-27 16:00:40 -07007980 /* Heartbeat is only sent once every 2 seconds.
7981 *
7982 * The heartbeat is to tell the ASF firmware that the host
7983 * driver is still alive. In the event that the OS crashes,
7984 * ASF needs to reset the hardware to free up the FIFO space
7985 * that may be filled with rx packets destined for the host.
7986 * If the FIFO is full, ASF will no longer function properly.
7987 *
7988 * Unintended resets have been reported on real time kernels
7989 * where the timer doesn't run on time. Netpoll will also have
7990 * same problem.
7991 *
7992 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
7993 * to check the ring condition when the heartbeat is expiring
7994 * before doing the reset. This will prevent most unintended
7995 * resets.
7996 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007997 if (!--tp->asf_counter) {
Matt Carlsonbc7959b2008-08-15 14:08:55 -07007998 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
7999 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07008000 tg3_wait_for_event_ack(tp);
8001
Michael Chanbbadf502006-04-06 21:46:34 -07008002 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07008003 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07008004 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Michael Chan28fbef72005-10-26 15:48:35 -07008005 /* 5 seconds timeout */
Michael Chanbbadf502006-04-06 21:46:34 -07008006 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX, 5);
Matt Carlson4ba526c2008-08-15 14:10:04 -07008007
8008 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008009 }
8010 tp->asf_counter = tp->asf_multiplier;
8011 }
8012
David S. Millerf47c11e2005-06-24 20:18:35 -07008013 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008014
Michael Chanf475f162006-03-27 23:20:14 -08008015restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07008016 tp->timer.expires = jiffies + tp->timer_offset;
8017 add_timer(&tp->timer);
8018}
8019
Matt Carlson4f125f42009-09-01 12:55:02 +00008020static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -08008021{
David Howells7d12e782006-10-05 14:55:46 +01008022 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008023 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +00008024 char *name;
8025 struct tg3_napi *tnapi = &tp->napi[irq_num];
8026
8027 if (tp->irq_cnt == 1)
8028 name = tp->dev->name;
8029 else {
8030 name = &tnapi->irq_lbl[0];
8031 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
8032 name[IFNAMSIZ-1] = 0;
8033 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08008034
Matt Carlson679563f2009-09-01 12:55:46 +00008035 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI_OR_MSIX) {
Michael Chanfcfa0a32006-03-20 22:28:41 -08008036 fn = tg3_msi;
8037 if (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)
8038 fn = tg3_msi_1shot;
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07008039 flags = IRQF_SAMPLE_RANDOM;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008040 } else {
8041 fn = tg3_interrupt;
8042 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)
8043 fn = tg3_interrupt_tagged;
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07008044 flags = IRQF_SHARED | IRQF_SAMPLE_RANDOM;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008045 }
Matt Carlson4f125f42009-09-01 12:55:02 +00008046
8047 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08008048}
8049
Michael Chan79381092005-04-21 17:13:59 -07008050static int tg3_test_interrupt(struct tg3 *tp)
8051{
Matt Carlson09943a12009-08-28 14:01:57 +00008052 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -07008053 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -07008054 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008055 u32 val;
Michael Chan79381092005-04-21 17:13:59 -07008056
Michael Chand4bc3922005-05-29 14:59:20 -07008057 if (!netif_running(dev))
8058 return -ENODEV;
8059
Michael Chan79381092005-04-21 17:13:59 -07008060 tg3_disable_ints(tp);
8061
Matt Carlson4f125f42009-09-01 12:55:02 +00008062 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07008063
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008064 /*
8065 * Turn off MSI one shot mode. Otherwise this test has no
8066 * observable way to know whether the interrupt was delivered.
8067 */
8068 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 &&
8069 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
8070 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
8071 tw32(MSGINT_MODE, val);
8072 }
8073
Matt Carlson4f125f42009-09-01 12:55:02 +00008074 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Matt Carlson09943a12009-08-28 14:01:57 +00008075 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07008076 if (err)
8077 return err;
8078
Matt Carlson898a56f2009-08-28 14:02:40 +00008079 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -07008080 tg3_enable_ints(tp);
8081
8082 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00008083 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -07008084
8085 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -07008086 u32 int_mbox, misc_host_ctrl;
8087
Matt Carlson898a56f2009-08-28 14:02:40 +00008088 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -07008089 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
8090
8091 if ((int_mbox != 0) ||
8092 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
8093 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -07008094 break;
Michael Chanb16250e2006-09-27 16:10:14 -07008095 }
8096
Michael Chan79381092005-04-21 17:13:59 -07008097 msleep(10);
8098 }
8099
8100 tg3_disable_ints(tp);
8101
Matt Carlson4f125f42009-09-01 12:55:02 +00008102 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008103
Matt Carlson4f125f42009-09-01 12:55:02 +00008104 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07008105
8106 if (err)
8107 return err;
8108
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008109 if (intr_ok) {
8110 /* Reenable MSI one shot mode. */
8111 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 &&
8112 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
8113 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
8114 tw32(MSGINT_MODE, val);
8115 }
Michael Chan79381092005-04-21 17:13:59 -07008116 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008117 }
Michael Chan79381092005-04-21 17:13:59 -07008118
8119 return -EIO;
8120}
8121
8122/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
8123 * successfully restored
8124 */
8125static int tg3_test_msi(struct tg3 *tp)
8126{
Michael Chan79381092005-04-21 17:13:59 -07008127 int err;
8128 u16 pci_cmd;
8129
8130 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSI))
8131 return 0;
8132
8133 /* Turn off SERR reporting in case MSI terminates with Master
8134 * Abort.
8135 */
8136 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
8137 pci_write_config_word(tp->pdev, PCI_COMMAND,
8138 pci_cmd & ~PCI_COMMAND_SERR);
8139
8140 err = tg3_test_interrupt(tp);
8141
8142 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
8143
8144 if (!err)
8145 return 0;
8146
8147 /* other failures */
8148 if (err != -EIO)
8149 return err;
8150
8151 /* MSI test failed, go back to INTx mode */
8152 printk(KERN_WARNING PFX "%s: No interrupt was generated using MSI, "
8153 "switching to INTx mode. Please report this failure to "
8154 "the PCI maintainer and include system chipset information.\n",
8155 tp->dev->name);
8156
Matt Carlson4f125f42009-09-01 12:55:02 +00008157 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +00008158
Michael Chan79381092005-04-21 17:13:59 -07008159 pci_disable_msi(tp->pdev);
8160
8161 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI;
8162
Matt Carlson4f125f42009-09-01 12:55:02 +00008163 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07008164 if (err)
8165 return err;
8166
8167 /* Need to reset the chip because the MSI cycle may have terminated
8168 * with Master Abort.
8169 */
David S. Millerf47c11e2005-06-24 20:18:35 -07008170 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07008171
Michael Chan944d9802005-05-29 14:57:48 -07008172 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008173 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07008174
David S. Millerf47c11e2005-06-24 20:18:35 -07008175 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07008176
8177 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +00008178 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -07008179
8180 return err;
8181}
8182
Matt Carlson9e9fd122009-01-19 16:57:45 -08008183static int tg3_request_firmware(struct tg3 *tp)
8184{
8185 const __be32 *fw_data;
8186
8187 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
8188 printk(KERN_ERR "%s: Failed to load firmware \"%s\"\n",
8189 tp->dev->name, tp->fw_needed);
8190 return -ENOENT;
8191 }
8192
8193 fw_data = (void *)tp->fw->data;
8194
8195 /* Firmware blob starts with version numbers, followed by
8196 * start address and _full_ length including BSS sections
8197 * (which must be longer than the actual data, of course
8198 */
8199
8200 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
8201 if (tp->fw_len < (tp->fw->size - 12)) {
8202 printk(KERN_ERR "%s: bogus length %d in \"%s\"\n",
8203 tp->dev->name, tp->fw_len, tp->fw_needed);
8204 release_firmware(tp->fw);
8205 tp->fw = NULL;
8206 return -EINVAL;
8207 }
8208
8209 /* We no longer need firmware; we have it. */
8210 tp->fw_needed = NULL;
8211 return 0;
8212}
8213
Matt Carlson679563f2009-09-01 12:55:46 +00008214static bool tg3_enable_msix(struct tg3 *tp)
8215{
8216 int i, rc, cpus = num_online_cpus();
8217 struct msix_entry msix_ent[tp->irq_max];
8218
8219 if (cpus == 1)
8220 /* Just fallback to the simpler MSI mode. */
8221 return false;
8222
8223 /*
8224 * We want as many rx rings enabled as there are cpus.
8225 * The first MSIX vector only deals with link interrupts, etc,
8226 * so we add one to the number of vectors we are requesting.
8227 */
8228 tp->irq_cnt = min_t(unsigned, cpus + 1, tp->irq_max);
8229
8230 for (i = 0; i < tp->irq_max; i++) {
8231 msix_ent[i].entry = i;
8232 msix_ent[i].vector = 0;
8233 }
8234
8235 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
8236 if (rc != 0) {
8237 if (rc < TG3_RSS_MIN_NUM_MSIX_VECS)
8238 return false;
8239 if (pci_enable_msix(tp->pdev, msix_ent, rc))
8240 return false;
8241 printk(KERN_NOTICE
8242 "%s: Requested %d MSI-X vectors, received %d\n",
8243 tp->dev->name, tp->irq_cnt, rc);
8244 tp->irq_cnt = rc;
8245 }
8246
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008247 tp->tg3_flags3 |= TG3_FLG3_ENABLE_RSS;
8248
Matt Carlson679563f2009-09-01 12:55:46 +00008249 for (i = 0; i < tp->irq_max; i++)
8250 tp->napi[i].irq_vec = msix_ent[i].vector;
8251
Matt Carlsonfe5f5782009-09-01 13:09:39 +00008252 tp->dev->real_num_tx_queues = tp->irq_cnt - 1;
8253
Matt Carlson679563f2009-09-01 12:55:46 +00008254 return true;
8255}
8256
Matt Carlson07b01732009-08-28 14:01:15 +00008257static void tg3_ints_init(struct tg3 *tp)
8258{
Matt Carlson679563f2009-09-01 12:55:46 +00008259 if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSI_OR_MSIX) &&
8260 !(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +00008261 /* All MSI supporting chips should support tagged
8262 * status. Assert that this is the case.
8263 */
Matt Carlson679563f2009-09-01 12:55:46 +00008264 printk(KERN_WARNING PFX "%s: MSI without TAGGED? "
8265 "Not using MSI.\n", tp->dev->name);
8266 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +00008267 }
Matt Carlson4f125f42009-09-01 12:55:02 +00008268
Matt Carlson679563f2009-09-01 12:55:46 +00008269 if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX) && tg3_enable_msix(tp))
8270 tp->tg3_flags2 |= TG3_FLG2_USING_MSIX;
8271 else if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSI) &&
8272 pci_enable_msi(tp->pdev) == 0)
8273 tp->tg3_flags2 |= TG3_FLG2_USING_MSI;
8274
8275 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI_OR_MSIX) {
8276 u32 msi_mode = tr32(MSGINT_MODE);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008277 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
8278 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson679563f2009-09-01 12:55:46 +00008279 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
8280 }
8281defcfg:
8282 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSIX)) {
8283 tp->irq_cnt = 1;
8284 tp->napi[0].irq_vec = tp->pdev->irq;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00008285 tp->dev->real_num_tx_queues = 1;
Matt Carlson679563f2009-09-01 12:55:46 +00008286 }
Matt Carlson07b01732009-08-28 14:01:15 +00008287}
8288
8289static void tg3_ints_fini(struct tg3 *tp)
8290{
Matt Carlson679563f2009-09-01 12:55:46 +00008291 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
8292 pci_disable_msix(tp->pdev);
8293 else if (tp->tg3_flags2 & TG3_FLG2_USING_MSI)
8294 pci_disable_msi(tp->pdev);
8295 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI_OR_MSIX;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008296 tp->tg3_flags3 &= ~TG3_FLG3_ENABLE_RSS;
Matt Carlson07b01732009-08-28 14:01:15 +00008297}
8298
Linus Torvalds1da177e2005-04-16 15:20:36 -07008299static int tg3_open(struct net_device *dev)
8300{
8301 struct tg3 *tp = netdev_priv(dev);
Matt Carlson4f125f42009-09-01 12:55:02 +00008302 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008303
Matt Carlson9e9fd122009-01-19 16:57:45 -08008304 if (tp->fw_needed) {
8305 err = tg3_request_firmware(tp);
8306 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
8307 if (err)
8308 return err;
8309 } else if (err) {
8310 printk(KERN_WARNING "%s: TSO capability disabled.\n",
8311 tp->dev->name);
8312 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
8313 } else if (!(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
8314 printk(KERN_NOTICE "%s: TSO capability restored.\n",
8315 tp->dev->name);
8316 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
8317 }
8318 }
8319
Michael Chanc49a1562006-12-17 17:07:29 -08008320 netif_carrier_off(tp->dev);
8321
Michael Chanbc1c7562006-03-20 17:48:03 -08008322 err = tg3_set_power_state(tp, PCI_D0);
Matt Carlson2f751b62008-08-04 23:17:34 -07008323 if (err)
Michael Chanbc1c7562006-03-20 17:48:03 -08008324 return err;
Matt Carlson2f751b62008-08-04 23:17:34 -07008325
8326 tg3_full_lock(tp, 0);
Michael Chanbc1c7562006-03-20 17:48:03 -08008327
Linus Torvalds1da177e2005-04-16 15:20:36 -07008328 tg3_disable_ints(tp);
8329 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
8330
David S. Millerf47c11e2005-06-24 20:18:35 -07008331 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008332
Matt Carlson679563f2009-09-01 12:55:46 +00008333 /*
8334 * Setup interrupts first so we know how
8335 * many NAPI resources to allocate
8336 */
8337 tg3_ints_init(tp);
8338
Linus Torvalds1da177e2005-04-16 15:20:36 -07008339 /* The placement of this call is tied
8340 * to the setup and use of Host TX descriptors.
8341 */
8342 err = tg3_alloc_consistent(tp);
8343 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00008344 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008345
Matt Carlsonfed97812009-09-01 13:10:19 +00008346 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07008347
Matt Carlson4f125f42009-09-01 12:55:02 +00008348 for (i = 0; i < tp->irq_cnt; i++) {
8349 struct tg3_napi *tnapi = &tp->napi[i];
8350 err = tg3_request_irq(tp, i);
8351 if (err) {
8352 for (i--; i >= 0; i--)
8353 free_irq(tnapi->irq_vec, tnapi);
8354 break;
8355 }
8356 }
Matt Carlson07b01732009-08-28 14:01:15 +00008357
8358 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00008359 goto err_out2;
Matt Carlson07b01732009-08-28 14:01:15 +00008360
David S. Millerf47c11e2005-06-24 20:18:35 -07008361 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008362
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008363 err = tg3_init_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008364 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -07008365 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008366 tg3_free_rings(tp);
8367 } else {
David S. Millerfac9b832005-05-18 22:46:34 -07008368 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)
8369 tp->timer_offset = HZ;
8370 else
8371 tp->timer_offset = HZ / 10;
8372
8373 BUG_ON(tp->timer_offset > HZ);
8374 tp->timer_counter = tp->timer_multiplier =
8375 (HZ / tp->timer_offset);
8376 tp->asf_counter = tp->asf_multiplier =
Michael Chan28fbef72005-10-26 15:48:35 -07008377 ((HZ / tp->timer_offset) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008378
8379 init_timer(&tp->timer);
8380 tp->timer.expires = jiffies + tp->timer_offset;
8381 tp->timer.data = (unsigned long) tp;
8382 tp->timer.function = tg3_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008383 }
8384
David S. Millerf47c11e2005-06-24 20:18:35 -07008385 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008386
Matt Carlson07b01732009-08-28 14:01:15 +00008387 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00008388 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008389
Michael Chan79381092005-04-21 17:13:59 -07008390 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
8391 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -07008392
Michael Chan79381092005-04-21 17:13:59 -07008393 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -07008394 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -07008395 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -07008396 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07008397 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07008398
Matt Carlson679563f2009-09-01 12:55:46 +00008399 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -07008400 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08008401
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008402 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
8403 (tp->tg3_flags2 & TG3_FLG2_USING_MSI) &&
8404 (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)) {
8405 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -08008406
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008407 tw32(PCIE_TRANSACTION_CFG,
8408 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -08008409 }
Michael Chan79381092005-04-21 17:13:59 -07008410 }
8411
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07008412 tg3_phy_start(tp);
8413
David S. Millerf47c11e2005-06-24 20:18:35 -07008414 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008415
Michael Chan79381092005-04-21 17:13:59 -07008416 add_timer(&tp->timer);
8417 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008418 tg3_enable_ints(tp);
8419
David S. Millerf47c11e2005-06-24 20:18:35 -07008420 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008421
Matt Carlsonfe5f5782009-09-01 13:09:39 +00008422 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008423
8424 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +00008425
Matt Carlson679563f2009-09-01 12:55:46 +00008426err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +00008427 for (i = tp->irq_cnt - 1; i >= 0; i--) {
8428 struct tg3_napi *tnapi = &tp->napi[i];
8429 free_irq(tnapi->irq_vec, tnapi);
8430 }
Matt Carlson07b01732009-08-28 14:01:15 +00008431
Matt Carlson679563f2009-09-01 12:55:46 +00008432err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +00008433 tg3_napi_disable(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00008434 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +00008435
8436err_out1:
8437 tg3_ints_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00008438 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008439}
8440
8441#if 0
8442/*static*/ void tg3_dump_state(struct tg3 *tp)
8443{
8444 u32 val32, val32_2, val32_3, val32_4, val32_5;
8445 u16 val16;
8446 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00008447 struct tg3_hw_status *sblk = tp->napi[0]->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008448
8449 pci_read_config_word(tp->pdev, PCI_STATUS, &val16);
8450 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE, &val32);
8451 printk("DEBUG: PCI status [%04x] TG3PCI state[%08x]\n",
8452 val16, val32);
8453
8454 /* MAC block */
8455 printk("DEBUG: MAC_MODE[%08x] MAC_STATUS[%08x]\n",
8456 tr32(MAC_MODE), tr32(MAC_STATUS));
8457 printk(" MAC_EVENT[%08x] MAC_LED_CTRL[%08x]\n",
8458 tr32(MAC_EVENT), tr32(MAC_LED_CTRL));
8459 printk("DEBUG: MAC_TX_MODE[%08x] MAC_TX_STATUS[%08x]\n",
8460 tr32(MAC_TX_MODE), tr32(MAC_TX_STATUS));
8461 printk(" MAC_RX_MODE[%08x] MAC_RX_STATUS[%08x]\n",
8462 tr32(MAC_RX_MODE), tr32(MAC_RX_STATUS));
8463
8464 /* Send data initiator control block */
8465 printk("DEBUG: SNDDATAI_MODE[%08x] SNDDATAI_STATUS[%08x]\n",
8466 tr32(SNDDATAI_MODE), tr32(SNDDATAI_STATUS));
8467 printk(" SNDDATAI_STATSCTRL[%08x]\n",
8468 tr32(SNDDATAI_STATSCTRL));
8469
8470 /* Send data completion control block */
8471 printk("DEBUG: SNDDATAC_MODE[%08x]\n", tr32(SNDDATAC_MODE));
8472
8473 /* Send BD ring selector block */
8474 printk("DEBUG: SNDBDS_MODE[%08x] SNDBDS_STATUS[%08x]\n",
8475 tr32(SNDBDS_MODE), tr32(SNDBDS_STATUS));
8476
8477 /* Send BD initiator control block */
8478 printk("DEBUG: SNDBDI_MODE[%08x] SNDBDI_STATUS[%08x]\n",
8479 tr32(SNDBDI_MODE), tr32(SNDBDI_STATUS));
8480
8481 /* Send BD completion control block */
8482 printk("DEBUG: SNDBDC_MODE[%08x]\n", tr32(SNDBDC_MODE));
8483
8484 /* Receive list placement control block */
8485 printk("DEBUG: RCVLPC_MODE[%08x] RCVLPC_STATUS[%08x]\n",
8486 tr32(RCVLPC_MODE), tr32(RCVLPC_STATUS));
8487 printk(" RCVLPC_STATSCTRL[%08x]\n",
8488 tr32(RCVLPC_STATSCTRL));
8489
8490 /* Receive data and receive BD initiator control block */
8491 printk("DEBUG: RCVDBDI_MODE[%08x] RCVDBDI_STATUS[%08x]\n",
8492 tr32(RCVDBDI_MODE), tr32(RCVDBDI_STATUS));
8493
8494 /* Receive data completion control block */
8495 printk("DEBUG: RCVDCC_MODE[%08x]\n",
8496 tr32(RCVDCC_MODE));
8497
8498 /* Receive BD initiator control block */
8499 printk("DEBUG: RCVBDI_MODE[%08x] RCVBDI_STATUS[%08x]\n",
8500 tr32(RCVBDI_MODE), tr32(RCVBDI_STATUS));
8501
8502 /* Receive BD completion control block */
8503 printk("DEBUG: RCVCC_MODE[%08x] RCVCC_STATUS[%08x]\n",
8504 tr32(RCVCC_MODE), tr32(RCVCC_STATUS));
8505
8506 /* Receive list selector control block */
8507 printk("DEBUG: RCVLSC_MODE[%08x] RCVLSC_STATUS[%08x]\n",
8508 tr32(RCVLSC_MODE), tr32(RCVLSC_STATUS));
8509
8510 /* Mbuf cluster free block */
8511 printk("DEBUG: MBFREE_MODE[%08x] MBFREE_STATUS[%08x]\n",
8512 tr32(MBFREE_MODE), tr32(MBFREE_STATUS));
8513
8514 /* Host coalescing control block */
8515 printk("DEBUG: HOSTCC_MODE[%08x] HOSTCC_STATUS[%08x]\n",
8516 tr32(HOSTCC_MODE), tr32(HOSTCC_STATUS));
8517 printk("DEBUG: HOSTCC_STATS_BLK_HOST_ADDR[%08x%08x]\n",
8518 tr32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH),
8519 tr32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW));
8520 printk("DEBUG: HOSTCC_STATUS_BLK_HOST_ADDR[%08x%08x]\n",
8521 tr32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH),
8522 tr32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW));
8523 printk("DEBUG: HOSTCC_STATS_BLK_NIC_ADDR[%08x]\n",
8524 tr32(HOSTCC_STATS_BLK_NIC_ADDR));
8525 printk("DEBUG: HOSTCC_STATUS_BLK_NIC_ADDR[%08x]\n",
8526 tr32(HOSTCC_STATUS_BLK_NIC_ADDR));
8527
8528 /* Memory arbiter control block */
8529 printk("DEBUG: MEMARB_MODE[%08x] MEMARB_STATUS[%08x]\n",
8530 tr32(MEMARB_MODE), tr32(MEMARB_STATUS));
8531
8532 /* Buffer manager control block */
8533 printk("DEBUG: BUFMGR_MODE[%08x] BUFMGR_STATUS[%08x]\n",
8534 tr32(BUFMGR_MODE), tr32(BUFMGR_STATUS));
8535 printk("DEBUG: BUFMGR_MB_POOL_ADDR[%08x] BUFMGR_MB_POOL_SIZE[%08x]\n",
8536 tr32(BUFMGR_MB_POOL_ADDR), tr32(BUFMGR_MB_POOL_SIZE));
8537 printk("DEBUG: BUFMGR_DMA_DESC_POOL_ADDR[%08x] "
8538 "BUFMGR_DMA_DESC_POOL_SIZE[%08x]\n",
8539 tr32(BUFMGR_DMA_DESC_POOL_ADDR),
8540 tr32(BUFMGR_DMA_DESC_POOL_SIZE));
8541
8542 /* Read DMA control block */
8543 printk("DEBUG: RDMAC_MODE[%08x] RDMAC_STATUS[%08x]\n",
8544 tr32(RDMAC_MODE), tr32(RDMAC_STATUS));
8545
8546 /* Write DMA control block */
8547 printk("DEBUG: WDMAC_MODE[%08x] WDMAC_STATUS[%08x]\n",
8548 tr32(WDMAC_MODE), tr32(WDMAC_STATUS));
8549
8550 /* DMA completion block */
8551 printk("DEBUG: DMAC_MODE[%08x]\n",
8552 tr32(DMAC_MODE));
8553
8554 /* GRC block */
8555 printk("DEBUG: GRC_MODE[%08x] GRC_MISC_CFG[%08x]\n",
8556 tr32(GRC_MODE), tr32(GRC_MISC_CFG));
8557 printk("DEBUG: GRC_LOCAL_CTRL[%08x]\n",
8558 tr32(GRC_LOCAL_CTRL));
8559
8560 /* TG3_BDINFOs */
8561 printk("DEBUG: RCVDBDI_JUMBO_BD[%08x%08x:%08x:%08x]\n",
8562 tr32(RCVDBDI_JUMBO_BD + 0x0),
8563 tr32(RCVDBDI_JUMBO_BD + 0x4),
8564 tr32(RCVDBDI_JUMBO_BD + 0x8),
8565 tr32(RCVDBDI_JUMBO_BD + 0xc));
8566 printk("DEBUG: RCVDBDI_STD_BD[%08x%08x:%08x:%08x]\n",
8567 tr32(RCVDBDI_STD_BD + 0x0),
8568 tr32(RCVDBDI_STD_BD + 0x4),
8569 tr32(RCVDBDI_STD_BD + 0x8),
8570 tr32(RCVDBDI_STD_BD + 0xc));
8571 printk("DEBUG: RCVDBDI_MINI_BD[%08x%08x:%08x:%08x]\n",
8572 tr32(RCVDBDI_MINI_BD + 0x0),
8573 tr32(RCVDBDI_MINI_BD + 0x4),
8574 tr32(RCVDBDI_MINI_BD + 0x8),
8575 tr32(RCVDBDI_MINI_BD + 0xc));
8576
8577 tg3_read_mem(tp, NIC_SRAM_SEND_RCB + 0x0, &val32);
8578 tg3_read_mem(tp, NIC_SRAM_SEND_RCB + 0x4, &val32_2);
8579 tg3_read_mem(tp, NIC_SRAM_SEND_RCB + 0x8, &val32_3);
8580 tg3_read_mem(tp, NIC_SRAM_SEND_RCB + 0xc, &val32_4);
8581 printk("DEBUG: SRAM_SEND_RCB_0[%08x%08x:%08x:%08x]\n",
8582 val32, val32_2, val32_3, val32_4);
8583
8584 tg3_read_mem(tp, NIC_SRAM_RCV_RET_RCB + 0x0, &val32);
8585 tg3_read_mem(tp, NIC_SRAM_RCV_RET_RCB + 0x4, &val32_2);
8586 tg3_read_mem(tp, NIC_SRAM_RCV_RET_RCB + 0x8, &val32_3);
8587 tg3_read_mem(tp, NIC_SRAM_RCV_RET_RCB + 0xc, &val32_4);
8588 printk("DEBUG: SRAM_RCV_RET_RCB_0[%08x%08x:%08x:%08x]\n",
8589 val32, val32_2, val32_3, val32_4);
8590
8591 tg3_read_mem(tp, NIC_SRAM_STATUS_BLK + 0x0, &val32);
8592 tg3_read_mem(tp, NIC_SRAM_STATUS_BLK + 0x4, &val32_2);
8593 tg3_read_mem(tp, NIC_SRAM_STATUS_BLK + 0x8, &val32_3);
8594 tg3_read_mem(tp, NIC_SRAM_STATUS_BLK + 0xc, &val32_4);
8595 tg3_read_mem(tp, NIC_SRAM_STATUS_BLK + 0x10, &val32_5);
8596 printk("DEBUG: SRAM_STATUS_BLK[%08x:%08x:%08x:%08x:%08x]\n",
8597 val32, val32_2, val32_3, val32_4, val32_5);
8598
8599 /* SW status block */
Matt Carlson898a56f2009-08-28 14:02:40 +00008600 printk(KERN_DEBUG
8601 "Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
8602 sblk->status,
8603 sblk->status_tag,
8604 sblk->rx_jumbo_consumer,
8605 sblk->rx_consumer,
8606 sblk->rx_mini_consumer,
8607 sblk->idx[0].rx_producer,
8608 sblk->idx[0].tx_consumer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008609
8610 /* SW statistics block */
8611 printk("DEBUG: Host statistics block [%08x:%08x:%08x:%08x]\n",
8612 ((u32 *)tp->hw_stats)[0],
8613 ((u32 *)tp->hw_stats)[1],
8614 ((u32 *)tp->hw_stats)[2],
8615 ((u32 *)tp->hw_stats)[3]);
8616
8617 /* Mailboxes */
8618 printk("DEBUG: SNDHOST_PROD[%08x%08x] SNDNIC_PROD[%08x%08x]\n",
Michael Chan09ee9292005-08-09 20:17:00 -07008619 tr32_mailbox(MAILBOX_SNDHOST_PROD_IDX_0 + 0x0),
8620 tr32_mailbox(MAILBOX_SNDHOST_PROD_IDX_0 + 0x4),
8621 tr32_mailbox(MAILBOX_SNDNIC_PROD_IDX_0 + 0x0),
8622 tr32_mailbox(MAILBOX_SNDNIC_PROD_IDX_0 + 0x4));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008623
8624 /* NIC side send descriptors. */
8625 for (i = 0; i < 6; i++) {
8626 unsigned long txd;
8627
8628 txd = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_TX_BUFFER_DESC
8629 + (i * sizeof(struct tg3_tx_buffer_desc));
8630 printk("DEBUG: NIC TXD(%d)[%08x:%08x:%08x:%08x]\n",
8631 i,
8632 readl(txd + 0x0), readl(txd + 0x4),
8633 readl(txd + 0x8), readl(txd + 0xc));
8634 }
8635
8636 /* NIC side RX descriptors. */
8637 for (i = 0; i < 6; i++) {
8638 unsigned long rxd;
8639
8640 rxd = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_RX_BUFFER_DESC
8641 + (i * sizeof(struct tg3_rx_buffer_desc));
8642 printk("DEBUG: NIC RXD_STD(%d)[0][%08x:%08x:%08x:%08x]\n",
8643 i,
8644 readl(rxd + 0x0), readl(rxd + 0x4),
8645 readl(rxd + 0x8), readl(rxd + 0xc));
8646 rxd += (4 * sizeof(u32));
8647 printk("DEBUG: NIC RXD_STD(%d)[1][%08x:%08x:%08x:%08x]\n",
8648 i,
8649 readl(rxd + 0x0), readl(rxd + 0x4),
8650 readl(rxd + 0x8), readl(rxd + 0xc));
8651 }
8652
8653 for (i = 0; i < 6; i++) {
8654 unsigned long rxd;
8655
8656 rxd = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_RX_JUMBO_BUFFER_DESC
8657 + (i * sizeof(struct tg3_rx_buffer_desc));
8658 printk("DEBUG: NIC RXD_JUMBO(%d)[0][%08x:%08x:%08x:%08x]\n",
8659 i,
8660 readl(rxd + 0x0), readl(rxd + 0x4),
8661 readl(rxd + 0x8), readl(rxd + 0xc));
8662 rxd += (4 * sizeof(u32));
8663 printk("DEBUG: NIC RXD_JUMBO(%d)[1][%08x:%08x:%08x:%08x]\n",
8664 i,
8665 readl(rxd + 0x0), readl(rxd + 0x4),
8666 readl(rxd + 0x8), readl(rxd + 0xc));
8667 }
8668}
8669#endif
8670
8671static struct net_device_stats *tg3_get_stats(struct net_device *);
8672static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *);
8673
8674static int tg3_close(struct net_device *dev)
8675{
Matt Carlson4f125f42009-09-01 12:55:02 +00008676 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008677 struct tg3 *tp = netdev_priv(dev);
8678
Matt Carlsonfed97812009-09-01 13:10:19 +00008679 tg3_napi_disable(tp);
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07008680 cancel_work_sync(&tp->reset_task);
Michael Chan7faa0062006-02-02 17:29:28 -08008681
Matt Carlsonfe5f5782009-09-01 13:09:39 +00008682 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008683
8684 del_timer_sync(&tp->timer);
8685
Matt Carlson24bb4fb2009-10-05 17:55:29 +00008686 tg3_phy_stop(tp);
8687
David S. Millerf47c11e2005-06-24 20:18:35 -07008688 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008689#if 0
8690 tg3_dump_state(tp);
8691#endif
8692
8693 tg3_disable_ints(tp);
8694
Michael Chan944d9802005-05-29 14:57:48 -07008695 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008696 tg3_free_rings(tp);
Michael Chan5cf64b8a2007-05-05 12:11:21 -07008697 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008698
David S. Millerf47c11e2005-06-24 20:18:35 -07008699 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008700
Matt Carlson4f125f42009-09-01 12:55:02 +00008701 for (i = tp->irq_cnt - 1; i >= 0; i--) {
8702 struct tg3_napi *tnapi = &tp->napi[i];
8703 free_irq(tnapi->irq_vec, tnapi);
8704 }
Matt Carlson07b01732009-08-28 14:01:15 +00008705
8706 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008707
8708 memcpy(&tp->net_stats_prev, tg3_get_stats(tp->dev),
8709 sizeof(tp->net_stats_prev));
8710 memcpy(&tp->estats_prev, tg3_get_estats(tp),
8711 sizeof(tp->estats_prev));
8712
8713 tg3_free_consistent(tp);
8714
Michael Chanbc1c7562006-03-20 17:48:03 -08008715 tg3_set_power_state(tp, PCI_D3hot);
8716
8717 netif_carrier_off(tp->dev);
8718
Linus Torvalds1da177e2005-04-16 15:20:36 -07008719 return 0;
8720}
8721
8722static inline unsigned long get_stat64(tg3_stat64_t *val)
8723{
8724 unsigned long ret;
8725
8726#if (BITS_PER_LONG == 32)
8727 ret = val->low;
8728#else
8729 ret = ((u64)val->high << 32) | ((u64)val->low);
8730#endif
8731 return ret;
8732}
8733
Stefan Buehler816f8b82008-08-15 14:10:54 -07008734static inline u64 get_estat64(tg3_stat64_t *val)
8735{
8736 return ((u64)val->high << 32) | ((u64)val->low);
8737}
8738
Linus Torvalds1da177e2005-04-16 15:20:36 -07008739static unsigned long calc_crc_errors(struct tg3 *tp)
8740{
8741 struct tg3_hw_stats *hw_stats = tp->hw_stats;
8742
8743 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
8744 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
8745 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008746 u32 val;
8747
David S. Millerf47c11e2005-06-24 20:18:35 -07008748 spin_lock_bh(&tp->lock);
Michael Chan569a5df2007-02-13 12:18:15 -08008749 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
8750 tg3_writephy(tp, MII_TG3_TEST1,
8751 val | MII_TG3_TEST1_CRC_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008752 tg3_readphy(tp, 0x14, &val);
8753 } else
8754 val = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07008755 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008756
8757 tp->phy_crc_errors += val;
8758
8759 return tp->phy_crc_errors;
8760 }
8761
8762 return get_stat64(&hw_stats->rx_fcs_errors);
8763}
8764
8765#define ESTAT_ADD(member) \
8766 estats->member = old_estats->member + \
Stefan Buehler816f8b82008-08-15 14:10:54 -07008767 get_estat64(&hw_stats->member)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008768
8769static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *tp)
8770{
8771 struct tg3_ethtool_stats *estats = &tp->estats;
8772 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
8773 struct tg3_hw_stats *hw_stats = tp->hw_stats;
8774
8775 if (!hw_stats)
8776 return old_estats;
8777
8778 ESTAT_ADD(rx_octets);
8779 ESTAT_ADD(rx_fragments);
8780 ESTAT_ADD(rx_ucast_packets);
8781 ESTAT_ADD(rx_mcast_packets);
8782 ESTAT_ADD(rx_bcast_packets);
8783 ESTAT_ADD(rx_fcs_errors);
8784 ESTAT_ADD(rx_align_errors);
8785 ESTAT_ADD(rx_xon_pause_rcvd);
8786 ESTAT_ADD(rx_xoff_pause_rcvd);
8787 ESTAT_ADD(rx_mac_ctrl_rcvd);
8788 ESTAT_ADD(rx_xoff_entered);
8789 ESTAT_ADD(rx_frame_too_long_errors);
8790 ESTAT_ADD(rx_jabbers);
8791 ESTAT_ADD(rx_undersize_packets);
8792 ESTAT_ADD(rx_in_length_errors);
8793 ESTAT_ADD(rx_out_length_errors);
8794 ESTAT_ADD(rx_64_or_less_octet_packets);
8795 ESTAT_ADD(rx_65_to_127_octet_packets);
8796 ESTAT_ADD(rx_128_to_255_octet_packets);
8797 ESTAT_ADD(rx_256_to_511_octet_packets);
8798 ESTAT_ADD(rx_512_to_1023_octet_packets);
8799 ESTAT_ADD(rx_1024_to_1522_octet_packets);
8800 ESTAT_ADD(rx_1523_to_2047_octet_packets);
8801 ESTAT_ADD(rx_2048_to_4095_octet_packets);
8802 ESTAT_ADD(rx_4096_to_8191_octet_packets);
8803 ESTAT_ADD(rx_8192_to_9022_octet_packets);
8804
8805 ESTAT_ADD(tx_octets);
8806 ESTAT_ADD(tx_collisions);
8807 ESTAT_ADD(tx_xon_sent);
8808 ESTAT_ADD(tx_xoff_sent);
8809 ESTAT_ADD(tx_flow_control);
8810 ESTAT_ADD(tx_mac_errors);
8811 ESTAT_ADD(tx_single_collisions);
8812 ESTAT_ADD(tx_mult_collisions);
8813 ESTAT_ADD(tx_deferred);
8814 ESTAT_ADD(tx_excessive_collisions);
8815 ESTAT_ADD(tx_late_collisions);
8816 ESTAT_ADD(tx_collide_2times);
8817 ESTAT_ADD(tx_collide_3times);
8818 ESTAT_ADD(tx_collide_4times);
8819 ESTAT_ADD(tx_collide_5times);
8820 ESTAT_ADD(tx_collide_6times);
8821 ESTAT_ADD(tx_collide_7times);
8822 ESTAT_ADD(tx_collide_8times);
8823 ESTAT_ADD(tx_collide_9times);
8824 ESTAT_ADD(tx_collide_10times);
8825 ESTAT_ADD(tx_collide_11times);
8826 ESTAT_ADD(tx_collide_12times);
8827 ESTAT_ADD(tx_collide_13times);
8828 ESTAT_ADD(tx_collide_14times);
8829 ESTAT_ADD(tx_collide_15times);
8830 ESTAT_ADD(tx_ucast_packets);
8831 ESTAT_ADD(tx_mcast_packets);
8832 ESTAT_ADD(tx_bcast_packets);
8833 ESTAT_ADD(tx_carrier_sense_errors);
8834 ESTAT_ADD(tx_discards);
8835 ESTAT_ADD(tx_errors);
8836
8837 ESTAT_ADD(dma_writeq_full);
8838 ESTAT_ADD(dma_write_prioq_full);
8839 ESTAT_ADD(rxbds_empty);
8840 ESTAT_ADD(rx_discards);
8841 ESTAT_ADD(rx_errors);
8842 ESTAT_ADD(rx_threshold_hit);
8843
8844 ESTAT_ADD(dma_readq_full);
8845 ESTAT_ADD(dma_read_prioq_full);
8846 ESTAT_ADD(tx_comp_queue_full);
8847
8848 ESTAT_ADD(ring_set_send_prod_index);
8849 ESTAT_ADD(ring_status_update);
8850 ESTAT_ADD(nic_irqs);
8851 ESTAT_ADD(nic_avoided_irqs);
8852 ESTAT_ADD(nic_tx_threshold_hit);
8853
8854 return estats;
8855}
8856
8857static struct net_device_stats *tg3_get_stats(struct net_device *dev)
8858{
8859 struct tg3 *tp = netdev_priv(dev);
8860 struct net_device_stats *stats = &tp->net_stats;
8861 struct net_device_stats *old_stats = &tp->net_stats_prev;
8862 struct tg3_hw_stats *hw_stats = tp->hw_stats;
8863
8864 if (!hw_stats)
8865 return old_stats;
8866
8867 stats->rx_packets = old_stats->rx_packets +
8868 get_stat64(&hw_stats->rx_ucast_packets) +
8869 get_stat64(&hw_stats->rx_mcast_packets) +
8870 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008871
Linus Torvalds1da177e2005-04-16 15:20:36 -07008872 stats->tx_packets = old_stats->tx_packets +
8873 get_stat64(&hw_stats->tx_ucast_packets) +
8874 get_stat64(&hw_stats->tx_mcast_packets) +
8875 get_stat64(&hw_stats->tx_bcast_packets);
8876
8877 stats->rx_bytes = old_stats->rx_bytes +
8878 get_stat64(&hw_stats->rx_octets);
8879 stats->tx_bytes = old_stats->tx_bytes +
8880 get_stat64(&hw_stats->tx_octets);
8881
8882 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -07008883 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008884 stats->tx_errors = old_stats->tx_errors +
8885 get_stat64(&hw_stats->tx_errors) +
8886 get_stat64(&hw_stats->tx_mac_errors) +
8887 get_stat64(&hw_stats->tx_carrier_sense_errors) +
8888 get_stat64(&hw_stats->tx_discards);
8889
8890 stats->multicast = old_stats->multicast +
8891 get_stat64(&hw_stats->rx_mcast_packets);
8892 stats->collisions = old_stats->collisions +
8893 get_stat64(&hw_stats->tx_collisions);
8894
8895 stats->rx_length_errors = old_stats->rx_length_errors +
8896 get_stat64(&hw_stats->rx_frame_too_long_errors) +
8897 get_stat64(&hw_stats->rx_undersize_packets);
8898
8899 stats->rx_over_errors = old_stats->rx_over_errors +
8900 get_stat64(&hw_stats->rxbds_empty);
8901 stats->rx_frame_errors = old_stats->rx_frame_errors +
8902 get_stat64(&hw_stats->rx_align_errors);
8903 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
8904 get_stat64(&hw_stats->tx_discards);
8905 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
8906 get_stat64(&hw_stats->tx_carrier_sense_errors);
8907
8908 stats->rx_crc_errors = old_stats->rx_crc_errors +
8909 calc_crc_errors(tp);
8910
John W. Linville4f63b872005-09-12 14:43:18 -07008911 stats->rx_missed_errors = old_stats->rx_missed_errors +
8912 get_stat64(&hw_stats->rx_discards);
8913
Linus Torvalds1da177e2005-04-16 15:20:36 -07008914 return stats;
8915}
8916
8917static inline u32 calc_crc(unsigned char *buf, int len)
8918{
8919 u32 reg;
8920 u32 tmp;
8921 int j, k;
8922
8923 reg = 0xffffffff;
8924
8925 for (j = 0; j < len; j++) {
8926 reg ^= buf[j];
8927
8928 for (k = 0; k < 8; k++) {
8929 tmp = reg & 0x01;
8930
8931 reg >>= 1;
8932
8933 if (tmp) {
8934 reg ^= 0xedb88320;
8935 }
8936 }
8937 }
8938
8939 return ~reg;
8940}
8941
8942static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
8943{
8944 /* accept or reject all multicast frames */
8945 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
8946 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
8947 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
8948 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
8949}
8950
8951static void __tg3_set_rx_mode(struct net_device *dev)
8952{
8953 struct tg3 *tp = netdev_priv(dev);
8954 u32 rx_mode;
8955
8956 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
8957 RX_MODE_KEEP_VLAN_TAG);
8958
8959 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
8960 * flag clear.
8961 */
8962#if TG3_VLAN_TAG_USED
8963 if (!tp->vlgrp &&
8964 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
8965 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
8966#else
8967 /* By definition, VLAN is disabled always in this
8968 * case.
8969 */
8970 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
8971 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
8972#endif
8973
8974 if (dev->flags & IFF_PROMISC) {
8975 /* Promiscuous mode. */
8976 rx_mode |= RX_MODE_PROMISC;
8977 } else if (dev->flags & IFF_ALLMULTI) {
8978 /* Accept all multicast. */
8979 tg3_set_multi (tp, 1);
8980 } else if (dev->mc_count < 1) {
8981 /* Reject all multicast. */
8982 tg3_set_multi (tp, 0);
8983 } else {
8984 /* Accept one or more multicast(s). */
8985 struct dev_mc_list *mclist;
8986 unsigned int i;
8987 u32 mc_filter[4] = { 0, };
8988 u32 regidx;
8989 u32 bit;
8990 u32 crc;
8991
8992 for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
8993 i++, mclist = mclist->next) {
8994
8995 crc = calc_crc (mclist->dmi_addr, ETH_ALEN);
8996 bit = ~crc & 0x7f;
8997 regidx = (bit & 0x60) >> 5;
8998 bit &= 0x1f;
8999 mc_filter[regidx] |= (1 << bit);
9000 }
9001
9002 tw32(MAC_HASH_REG_0, mc_filter[0]);
9003 tw32(MAC_HASH_REG_1, mc_filter[1]);
9004 tw32(MAC_HASH_REG_2, mc_filter[2]);
9005 tw32(MAC_HASH_REG_3, mc_filter[3]);
9006 }
9007
9008 if (rx_mode != tp->rx_mode) {
9009 tp->rx_mode = rx_mode;
9010 tw32_f(MAC_RX_MODE, rx_mode);
9011 udelay(10);
9012 }
9013}
9014
9015static void tg3_set_rx_mode(struct net_device *dev)
9016{
9017 struct tg3 *tp = netdev_priv(dev);
9018
Michael Chane75f7c92006-03-20 21:33:26 -08009019 if (!netif_running(dev))
9020 return;
9021
David S. Millerf47c11e2005-06-24 20:18:35 -07009022 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009023 __tg3_set_rx_mode(dev);
David S. Millerf47c11e2005-06-24 20:18:35 -07009024 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009025}
9026
9027#define TG3_REGDUMP_LEN (32 * 1024)
9028
9029static int tg3_get_regs_len(struct net_device *dev)
9030{
9031 return TG3_REGDUMP_LEN;
9032}
9033
9034static void tg3_get_regs(struct net_device *dev,
9035 struct ethtool_regs *regs, void *_p)
9036{
9037 u32 *p = _p;
9038 struct tg3 *tp = netdev_priv(dev);
9039 u8 *orig_p = _p;
9040 int i;
9041
9042 regs->version = 0;
9043
9044 memset(p, 0, TG3_REGDUMP_LEN);
9045
Michael Chanbc1c7562006-03-20 17:48:03 -08009046 if (tp->link_config.phy_is_low_power)
9047 return;
9048
David S. Millerf47c11e2005-06-24 20:18:35 -07009049 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009050
9051#define __GET_REG32(reg) (*(p)++ = tr32(reg))
9052#define GET_REG32_LOOP(base,len) \
9053do { p = (u32 *)(orig_p + (base)); \
9054 for (i = 0; i < len; i += 4) \
9055 __GET_REG32((base) + i); \
9056} while (0)
9057#define GET_REG32_1(reg) \
9058do { p = (u32 *)(orig_p + (reg)); \
9059 __GET_REG32((reg)); \
9060} while (0)
9061
9062 GET_REG32_LOOP(TG3PCI_VENDOR, 0xb0);
9063 GET_REG32_LOOP(MAILBOX_INTERRUPT_0, 0x200);
9064 GET_REG32_LOOP(MAC_MODE, 0x4f0);
9065 GET_REG32_LOOP(SNDDATAI_MODE, 0xe0);
9066 GET_REG32_1(SNDDATAC_MODE);
9067 GET_REG32_LOOP(SNDBDS_MODE, 0x80);
9068 GET_REG32_LOOP(SNDBDI_MODE, 0x48);
9069 GET_REG32_1(SNDBDC_MODE);
9070 GET_REG32_LOOP(RCVLPC_MODE, 0x20);
9071 GET_REG32_LOOP(RCVLPC_SELLST_BASE, 0x15c);
9072 GET_REG32_LOOP(RCVDBDI_MODE, 0x0c);
9073 GET_REG32_LOOP(RCVDBDI_JUMBO_BD, 0x3c);
9074 GET_REG32_LOOP(RCVDBDI_BD_PROD_IDX_0, 0x44);
9075 GET_REG32_1(RCVDCC_MODE);
9076 GET_REG32_LOOP(RCVBDI_MODE, 0x20);
9077 GET_REG32_LOOP(RCVCC_MODE, 0x14);
9078 GET_REG32_LOOP(RCVLSC_MODE, 0x08);
9079 GET_REG32_1(MBFREE_MODE);
9080 GET_REG32_LOOP(HOSTCC_MODE, 0x100);
9081 GET_REG32_LOOP(MEMARB_MODE, 0x10);
9082 GET_REG32_LOOP(BUFMGR_MODE, 0x58);
9083 GET_REG32_LOOP(RDMAC_MODE, 0x08);
9084 GET_REG32_LOOP(WDMAC_MODE, 0x08);
Chris Elmquist091465d2005-12-20 13:25:19 -08009085 GET_REG32_1(RX_CPU_MODE);
9086 GET_REG32_1(RX_CPU_STATE);
9087 GET_REG32_1(RX_CPU_PGMCTR);
9088 GET_REG32_1(RX_CPU_HWBKPT);
9089 GET_REG32_1(TX_CPU_MODE);
9090 GET_REG32_1(TX_CPU_STATE);
9091 GET_REG32_1(TX_CPU_PGMCTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009092 GET_REG32_LOOP(GRCMBOX_INTERRUPT_0, 0x110);
9093 GET_REG32_LOOP(FTQ_RESET, 0x120);
9094 GET_REG32_LOOP(MSGINT_MODE, 0x0c);
9095 GET_REG32_1(DMAC_MODE);
9096 GET_REG32_LOOP(GRC_MODE, 0x4c);
9097 if (tp->tg3_flags & TG3_FLAG_NVRAM)
9098 GET_REG32_LOOP(NVRAM_CMD, 0x24);
9099
9100#undef __GET_REG32
9101#undef GET_REG32_LOOP
9102#undef GET_REG32_1
9103
David S. Millerf47c11e2005-06-24 20:18:35 -07009104 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009105}
9106
9107static int tg3_get_eeprom_len(struct net_device *dev)
9108{
9109 struct tg3 *tp = netdev_priv(dev);
9110
9111 return tp->nvram_size;
9112}
9113
Linus Torvalds1da177e2005-04-16 15:20:36 -07009114static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9115{
9116 struct tg3 *tp = netdev_priv(dev);
9117 int ret;
9118 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -08009119 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009120 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009121
Matt Carlsondf259d82009-04-20 06:57:14 +00009122 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM)
9123 return -EINVAL;
9124
Michael Chanbc1c7562006-03-20 17:48:03 -08009125 if (tp->link_config.phy_is_low_power)
9126 return -EAGAIN;
9127
Linus Torvalds1da177e2005-04-16 15:20:36 -07009128 offset = eeprom->offset;
9129 len = eeprom->len;
9130 eeprom->len = 0;
9131
9132 eeprom->magic = TG3_EEPROM_MAGIC;
9133
9134 if (offset & 3) {
9135 /* adjustments to start on required 4 byte boundary */
9136 b_offset = offset & 3;
9137 b_count = 4 - b_offset;
9138 if (b_count > len) {
9139 /* i.e. offset=1 len=2 */
9140 b_count = len;
9141 }
Matt Carlsona9dc5292009-02-25 14:25:30 +00009142 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009143 if (ret)
9144 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009145 memcpy(data, ((char*)&val) + b_offset, b_count);
9146 len -= b_count;
9147 offset += b_count;
9148 eeprom->len += b_count;
9149 }
9150
9151 /* read bytes upto the last 4 byte boundary */
9152 pd = &data[eeprom->len];
9153 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +00009154 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009155 if (ret) {
9156 eeprom->len += i;
9157 return ret;
9158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009159 memcpy(pd + i, &val, 4);
9160 }
9161 eeprom->len += i;
9162
9163 if (len & 3) {
9164 /* read last bytes not ending on 4 byte boundary */
9165 pd = &data[eeprom->len];
9166 b_count = len & 3;
9167 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009168 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009169 if (ret)
9170 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009171 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009172 eeprom->len += b_count;
9173 }
9174 return 0;
9175}
9176
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009177static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009178
9179static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9180{
9181 struct tg3 *tp = netdev_priv(dev);
9182 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009183 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009184 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009185 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009186
Michael Chanbc1c7562006-03-20 17:48:03 -08009187 if (tp->link_config.phy_is_low_power)
9188 return -EAGAIN;
9189
Matt Carlsondf259d82009-04-20 06:57:14 +00009190 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
9191 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009192 return -EINVAL;
9193
9194 offset = eeprom->offset;
9195 len = eeprom->len;
9196
9197 if ((b_offset = (offset & 3))) {
9198 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +00009199 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009200 if (ret)
9201 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009202 len += b_offset;
9203 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -07009204 if (len < 4)
9205 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009206 }
9207
9208 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -07009209 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009210 /* adjustments to end on required 4 byte boundary */
9211 odd_len = 1;
9212 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009213 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009214 if (ret)
9215 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009216 }
9217
9218 buf = data;
9219 if (b_offset || odd_len) {
9220 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009221 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009222 return -ENOMEM;
9223 if (b_offset)
9224 memcpy(buf, &start, 4);
9225 if (odd_len)
9226 memcpy(buf+len-4, &end, 4);
9227 memcpy(buf + b_offset, data, eeprom->len);
9228 }
9229
9230 ret = tg3_nvram_write_block(tp, offset, len, buf);
9231
9232 if (buf != data)
9233 kfree(buf);
9234
9235 return ret;
9236}
9237
9238static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9239{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009240 struct tg3 *tp = netdev_priv(dev);
9241
9242 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
9243 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
9244 return -EAGAIN;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07009245 return phy_ethtool_gset(tp->mdio_bus->phy_map[PHY_ADDR], cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009246 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009247
Linus Torvalds1da177e2005-04-16 15:20:36 -07009248 cmd->supported = (SUPPORTED_Autoneg);
9249
9250 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY))
9251 cmd->supported |= (SUPPORTED_1000baseT_Half |
9252 SUPPORTED_1000baseT_Full);
9253
Karsten Keilef348142006-05-12 12:49:08 -07009254 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009255 cmd->supported |= (SUPPORTED_100baseT_Half |
9256 SUPPORTED_100baseT_Full |
9257 SUPPORTED_10baseT_Half |
9258 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -08009259 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -07009260 cmd->port = PORT_TP;
9261 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009262 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -07009263 cmd->port = PORT_FIBRE;
9264 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009265
Linus Torvalds1da177e2005-04-16 15:20:36 -07009266 cmd->advertising = tp->link_config.advertising;
9267 if (netif_running(dev)) {
9268 cmd->speed = tp->link_config.active_speed;
9269 cmd->duplex = tp->link_config.active_duplex;
9270 }
Matt Carlson882e9792009-09-01 13:21:36 +00009271 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +00009272 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009273 cmd->autoneg = tp->link_config.autoneg;
9274 cmd->maxtxpkt = 0;
9275 cmd->maxrxpkt = 0;
9276 return 0;
9277}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009278
Linus Torvalds1da177e2005-04-16 15:20:36 -07009279static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9280{
9281 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009282
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009283 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
9284 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
9285 return -EAGAIN;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07009286 return phy_ethtool_sset(tp->mdio_bus->phy_map[PHY_ADDR], cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009287 }
9288
Matt Carlson7e5856b2009-02-25 14:23:01 +00009289 if (cmd->autoneg != AUTONEG_ENABLE &&
9290 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -07009291 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +00009292
9293 if (cmd->autoneg == AUTONEG_DISABLE &&
9294 cmd->duplex != DUPLEX_FULL &&
9295 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -07009296 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009297
Matt Carlson7e5856b2009-02-25 14:23:01 +00009298 if (cmd->autoneg == AUTONEG_ENABLE) {
9299 u32 mask = ADVERTISED_Autoneg |
9300 ADVERTISED_Pause |
9301 ADVERTISED_Asym_Pause;
9302
9303 if (!(tp->tg3_flags2 & TG3_FLAG_10_100_ONLY))
9304 mask |= ADVERTISED_1000baseT_Half |
9305 ADVERTISED_1000baseT_Full;
9306
9307 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES))
9308 mask |= ADVERTISED_100baseT_Half |
9309 ADVERTISED_100baseT_Full |
9310 ADVERTISED_10baseT_Half |
9311 ADVERTISED_10baseT_Full |
9312 ADVERTISED_TP;
9313 else
9314 mask |= ADVERTISED_FIBRE;
9315
9316 if (cmd->advertising & ~mask)
9317 return -EINVAL;
9318
9319 mask &= (ADVERTISED_1000baseT_Half |
9320 ADVERTISED_1000baseT_Full |
9321 ADVERTISED_100baseT_Half |
9322 ADVERTISED_100baseT_Full |
9323 ADVERTISED_10baseT_Half |
9324 ADVERTISED_10baseT_Full);
9325
9326 cmd->advertising &= mask;
9327 } else {
9328 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES) {
9329 if (cmd->speed != SPEED_1000)
9330 return -EINVAL;
9331
9332 if (cmd->duplex != DUPLEX_FULL)
9333 return -EINVAL;
9334 } else {
9335 if (cmd->speed != SPEED_100 &&
9336 cmd->speed != SPEED_10)
9337 return -EINVAL;
9338 }
9339 }
9340
David S. Millerf47c11e2005-06-24 20:18:35 -07009341 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009342
9343 tp->link_config.autoneg = cmd->autoneg;
9344 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -07009345 tp->link_config.advertising = (cmd->advertising |
9346 ADVERTISED_Autoneg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009347 tp->link_config.speed = SPEED_INVALID;
9348 tp->link_config.duplex = DUPLEX_INVALID;
9349 } else {
9350 tp->link_config.advertising = 0;
9351 tp->link_config.speed = cmd->speed;
9352 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009353 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009354
Michael Chan24fcad62006-12-17 17:06:46 -08009355 tp->link_config.orig_speed = tp->link_config.speed;
9356 tp->link_config.orig_duplex = tp->link_config.duplex;
9357 tp->link_config.orig_autoneg = tp->link_config.autoneg;
9358
Linus Torvalds1da177e2005-04-16 15:20:36 -07009359 if (netif_running(dev))
9360 tg3_setup_phy(tp, 1);
9361
David S. Millerf47c11e2005-06-24 20:18:35 -07009362 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009363
Linus Torvalds1da177e2005-04-16 15:20:36 -07009364 return 0;
9365}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009366
Linus Torvalds1da177e2005-04-16 15:20:36 -07009367static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
9368{
9369 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009370
Linus Torvalds1da177e2005-04-16 15:20:36 -07009371 strcpy(info->driver, DRV_MODULE_NAME);
9372 strcpy(info->version, DRV_MODULE_VERSION);
Michael Chanc4e65752006-03-20 22:29:32 -08009373 strcpy(info->fw_version, tp->fw_ver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009374 strcpy(info->bus_info, pci_name(tp->pdev));
9375}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009376
Linus Torvalds1da177e2005-04-16 15:20:36 -07009377static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
9378{
9379 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009380
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009381 if ((tp->tg3_flags & TG3_FLAG_WOL_CAP) &&
9382 device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -07009383 wol->supported = WAKE_MAGIC;
9384 else
9385 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009386 wol->wolopts = 0;
Matt Carlson05ac4cb2008-11-03 16:53:46 -08009387 if ((tp->tg3_flags & TG3_FLAG_WOL_ENABLE) &&
9388 device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009389 wol->wolopts = WAKE_MAGIC;
9390 memset(&wol->sopass, 0, sizeof(wol->sopass));
9391}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009392
Linus Torvalds1da177e2005-04-16 15:20:36 -07009393static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
9394{
9395 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009396 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009397
Linus Torvalds1da177e2005-04-16 15:20:36 -07009398 if (wol->wolopts & ~WAKE_MAGIC)
9399 return -EINVAL;
9400 if ((wol->wolopts & WAKE_MAGIC) &&
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009401 !((tp->tg3_flags & TG3_FLAG_WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009402 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009403
David S. Millerf47c11e2005-06-24 20:18:35 -07009404 spin_lock_bh(&tp->lock);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009405 if (wol->wolopts & WAKE_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009406 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009407 device_set_wakeup_enable(dp, true);
9408 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009409 tp->tg3_flags &= ~TG3_FLAG_WOL_ENABLE;
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009410 device_set_wakeup_enable(dp, false);
9411 }
David S. Millerf47c11e2005-06-24 20:18:35 -07009412 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009413
Linus Torvalds1da177e2005-04-16 15:20:36 -07009414 return 0;
9415}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009416
Linus Torvalds1da177e2005-04-16 15:20:36 -07009417static u32 tg3_get_msglevel(struct net_device *dev)
9418{
9419 struct tg3 *tp = netdev_priv(dev);
9420 return tp->msg_enable;
9421}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009422
Linus Torvalds1da177e2005-04-16 15:20:36 -07009423static void tg3_set_msglevel(struct net_device *dev, u32 value)
9424{
9425 struct tg3 *tp = netdev_priv(dev);
9426 tp->msg_enable = value;
9427}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009428
Linus Torvalds1da177e2005-04-16 15:20:36 -07009429static int tg3_set_tso(struct net_device *dev, u32 value)
9430{
9431 struct tg3 *tp = netdev_priv(dev);
9432
9433 if (!(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
9434 if (value)
9435 return -EINVAL;
9436 return 0;
9437 }
Matt Carlson027455a2008-12-21 20:19:30 -08009438 if ((dev->features & NETIF_F_IPV6_CSUM) &&
9439 (tp->tg3_flags2 & TG3_FLG2_HW_TSO_2)) {
Matt Carlson9936bcf2007-10-10 18:03:07 -07009440 if (value) {
Michael Chanb0026622006-07-03 19:42:14 -07009441 dev->features |= NETIF_F_TSO6;
Matt Carlson57e69832008-05-25 23:48:31 -07009442 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
9443 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
9444 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Matt Carlson321d32a2008-11-21 17:22:19 -08009445 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009446 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
9447 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson9936bcf2007-10-10 18:03:07 -07009448 dev->features |= NETIF_F_TSO_ECN;
9449 } else
9450 dev->features &= ~(NETIF_F_TSO6 | NETIF_F_TSO_ECN);
Michael Chanb0026622006-07-03 19:42:14 -07009451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009452 return ethtool_op_set_tso(dev, value);
9453}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009454
Linus Torvalds1da177e2005-04-16 15:20:36 -07009455static int tg3_nway_reset(struct net_device *dev)
9456{
9457 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009458 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009459
Linus Torvalds1da177e2005-04-16 15:20:36 -07009460 if (!netif_running(dev))
9461 return -EAGAIN;
9462
Michael Chanc94e3942005-09-27 12:12:42 -07009463 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
9464 return -EINVAL;
9465
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009466 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
9467 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
9468 return -EAGAIN;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07009469 r = phy_start_aneg(tp->mdio_bus->phy_map[PHY_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009470 } else {
9471 u32 bmcr;
9472
9473 spin_lock_bh(&tp->lock);
9474 r = -EINVAL;
9475 tg3_readphy(tp, MII_BMCR, &bmcr);
9476 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
9477 ((bmcr & BMCR_ANENABLE) ||
9478 (tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT))) {
9479 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
9480 BMCR_ANENABLE);
9481 r = 0;
9482 }
9483 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009484 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009485
Linus Torvalds1da177e2005-04-16 15:20:36 -07009486 return r;
9487}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009488
Linus Torvalds1da177e2005-04-16 15:20:36 -07009489static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
9490{
9491 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009492
Linus Torvalds1da177e2005-04-16 15:20:36 -07009493 ering->rx_max_pending = TG3_RX_RING_SIZE - 1;
9494 ering->rx_mini_max_pending = 0;
Michael Chan4f81c322006-03-20 21:33:42 -08009495 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE)
9496 ering->rx_jumbo_max_pending = TG3_RX_JUMBO_RING_SIZE - 1;
9497 else
9498 ering->rx_jumbo_max_pending = 0;
9499
9500 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009501
9502 ering->rx_pending = tp->rx_pending;
9503 ering->rx_mini_pending = 0;
Michael Chan4f81c322006-03-20 21:33:42 -08009504 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE)
9505 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
9506 else
9507 ering->rx_jumbo_pending = 0;
9508
Matt Carlsonf3f3f272009-08-28 14:03:21 +00009509 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009510}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009511
Linus Torvalds1da177e2005-04-16 15:20:36 -07009512static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
9513{
9514 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +00009515 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009516
Linus Torvalds1da177e2005-04-16 15:20:36 -07009517 if ((ering->rx_pending > TG3_RX_RING_SIZE - 1) ||
9518 (ering->rx_jumbo_pending > TG3_RX_JUMBO_RING_SIZE - 1) ||
Michael Chanbc3a9252006-10-18 20:55:18 -07009519 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
9520 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Michael Chan7f62ad52007-02-20 23:25:40 -08009521 ((tp->tg3_flags2 & TG3_FLG2_TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -07009522 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009523 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009524
Michael Chanbbe832c2005-06-24 20:20:04 -07009525 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009526 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009527 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -07009528 irq_sync = 1;
9529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009530
Michael Chanbbe832c2005-06-24 20:20:04 -07009531 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009532
Linus Torvalds1da177e2005-04-16 15:20:36 -07009533 tp->rx_pending = ering->rx_pending;
9534
9535 if ((tp->tg3_flags2 & TG3_FLG2_MAX_RXPEND_64) &&
9536 tp->rx_pending > 63)
9537 tp->rx_pending = 63;
9538 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +00009539
9540 for (i = 0; i < TG3_IRQ_MAX_VECS; i++)
9541 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009542
9543 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -07009544 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -07009545 err = tg3_restart_hw(tp, 1);
9546 if (!err)
9547 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009548 }
9549
David S. Millerf47c11e2005-06-24 20:18:35 -07009550 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009551
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009552 if (irq_sync && !err)
9553 tg3_phy_start(tp);
9554
Michael Chanb9ec6c12006-07-25 16:37:27 -07009555 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009556}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009557
Linus Torvalds1da177e2005-04-16 15:20:36 -07009558static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
9559{
9560 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009561
Linus Torvalds1da177e2005-04-16 15:20:36 -07009562 epause->autoneg = (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG) != 0;
Matt Carlson8d018622007-12-20 20:05:44 -08009563
Steve Glendinninge18ce342008-12-16 02:00:00 -08009564 if (tp->link_config.active_flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -08009565 epause->rx_pause = 1;
9566 else
9567 epause->rx_pause = 0;
9568
Steve Glendinninge18ce342008-12-16 02:00:00 -08009569 if (tp->link_config.active_flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -08009570 epause->tx_pause = 1;
9571 else
9572 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009573}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009574
Linus Torvalds1da177e2005-04-16 15:20:36 -07009575static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
9576{
9577 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009578 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009579
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009580 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
9581 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
9582 return -EAGAIN;
9583
9584 if (epause->autoneg) {
9585 u32 newadv;
9586 struct phy_device *phydev;
9587
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07009588 phydev = tp->mdio_bus->phy_map[PHY_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009589
9590 if (epause->rx_pause) {
9591 if (epause->tx_pause)
9592 newadv = ADVERTISED_Pause;
9593 else
9594 newadv = ADVERTISED_Pause |
9595 ADVERTISED_Asym_Pause;
9596 } else if (epause->tx_pause) {
9597 newadv = ADVERTISED_Asym_Pause;
9598 } else
9599 newadv = 0;
9600
9601 if (tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED) {
9602 u32 oldadv = phydev->advertising &
9603 (ADVERTISED_Pause |
9604 ADVERTISED_Asym_Pause);
9605 if (oldadv != newadv) {
9606 phydev->advertising &=
9607 ~(ADVERTISED_Pause |
9608 ADVERTISED_Asym_Pause);
9609 phydev->advertising |= newadv;
9610 err = phy_start_aneg(phydev);
9611 }
9612 } else {
9613 tp->link_config.advertising &=
9614 ~(ADVERTISED_Pause |
9615 ADVERTISED_Asym_Pause);
9616 tp->link_config.advertising |= newadv;
9617 }
9618 } else {
9619 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -08009620 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009621 else
Steve Glendinninge18ce342008-12-16 02:00:00 -08009622 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009623
9624 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -08009625 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009626 else
Steve Glendinninge18ce342008-12-16 02:00:00 -08009627 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009628
9629 if (netif_running(dev))
9630 tg3_setup_flow_control(tp, 0, 0);
9631 }
9632 } else {
9633 int irq_sync = 0;
9634
9635 if (netif_running(dev)) {
9636 tg3_netif_stop(tp);
9637 irq_sync = 1;
9638 }
9639
9640 tg3_full_lock(tp, irq_sync);
9641
9642 if (epause->autoneg)
9643 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
9644 else
9645 tp->tg3_flags &= ~TG3_FLAG_PAUSE_AUTONEG;
9646 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -08009647 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009648 else
Steve Glendinninge18ce342008-12-16 02:00:00 -08009649 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009650 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -08009651 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009652 else
Steve Glendinninge18ce342008-12-16 02:00:00 -08009653 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009654
9655 if (netif_running(dev)) {
9656 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
9657 err = tg3_restart_hw(tp, 1);
9658 if (!err)
9659 tg3_netif_start(tp);
9660 }
9661
9662 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -07009663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009664
Michael Chanb9ec6c12006-07-25 16:37:27 -07009665 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009666}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009667
Linus Torvalds1da177e2005-04-16 15:20:36 -07009668static u32 tg3_get_rx_csum(struct net_device *dev)
9669{
9670 struct tg3 *tp = netdev_priv(dev);
9671 return (tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) != 0;
9672}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009673
Linus Torvalds1da177e2005-04-16 15:20:36 -07009674static int tg3_set_rx_csum(struct net_device *dev, u32 data)
9675{
9676 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009677
Linus Torvalds1da177e2005-04-16 15:20:36 -07009678 if (tp->tg3_flags & TG3_FLAG_BROKEN_CHECKSUMS) {
9679 if (data != 0)
9680 return -EINVAL;
9681 return 0;
9682 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009683
David S. Millerf47c11e2005-06-24 20:18:35 -07009684 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009685 if (data)
9686 tp->tg3_flags |= TG3_FLAG_RX_CHECKSUMS;
9687 else
9688 tp->tg3_flags &= ~TG3_FLAG_RX_CHECKSUMS;
David S. Millerf47c11e2005-06-24 20:18:35 -07009689 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009690
Linus Torvalds1da177e2005-04-16 15:20:36 -07009691 return 0;
9692}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009693
Linus Torvalds1da177e2005-04-16 15:20:36 -07009694static int tg3_set_tx_csum(struct net_device *dev, u32 data)
9695{
9696 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009697
Linus Torvalds1da177e2005-04-16 15:20:36 -07009698 if (tp->tg3_flags & TG3_FLAG_BROKEN_CHECKSUMS) {
9699 if (data != 0)
9700 return -EINVAL;
9701 return 0;
9702 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009703
Matt Carlson321d32a2008-11-21 17:22:19 -08009704 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Michael Chan6460d942007-07-14 19:07:52 -07009705 ethtool_op_set_tx_ipv6_csum(dev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009706 else
Michael Chan9c27dbd2006-03-20 22:28:27 -08009707 ethtool_op_set_tx_csum(dev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009708
9709 return 0;
9710}
9711
Jeff Garzikb9f2c042007-10-03 18:07:32 -07009712static int tg3_get_sset_count (struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009713{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07009714 switch (sset) {
9715 case ETH_SS_TEST:
9716 return TG3_NUM_TEST;
9717 case ETH_SS_STATS:
9718 return TG3_NUM_STATS;
9719 default:
9720 return -EOPNOTSUPP;
9721 }
Michael Chan4cafd3f2005-05-29 14:56:34 -07009722}
9723
Linus Torvalds1da177e2005-04-16 15:20:36 -07009724static void tg3_get_strings (struct net_device *dev, u32 stringset, u8 *buf)
9725{
9726 switch (stringset) {
9727 case ETH_SS_STATS:
9728 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
9729 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -07009730 case ETH_SS_TEST:
9731 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
9732 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009733 default:
9734 WARN_ON(1); /* we need a WARN() */
9735 break;
9736 }
9737}
9738
Michael Chan4009a932005-09-05 17:52:54 -07009739static int tg3_phys_id(struct net_device *dev, u32 data)
9740{
9741 struct tg3 *tp = netdev_priv(dev);
9742 int i;
9743
9744 if (!netif_running(tp->dev))
9745 return -EAGAIN;
9746
9747 if (data == 0)
Stephen Hemminger759afc32008-02-23 19:51:59 -08009748 data = UINT_MAX / 2;
Michael Chan4009a932005-09-05 17:52:54 -07009749
9750 for (i = 0; i < (data * 2); i++) {
9751 if ((i % 2) == 0)
9752 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
9753 LED_CTRL_1000MBPS_ON |
9754 LED_CTRL_100MBPS_ON |
9755 LED_CTRL_10MBPS_ON |
9756 LED_CTRL_TRAFFIC_OVERRIDE |
9757 LED_CTRL_TRAFFIC_BLINK |
9758 LED_CTRL_TRAFFIC_LED);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009759
Michael Chan4009a932005-09-05 17:52:54 -07009760 else
9761 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
9762 LED_CTRL_TRAFFIC_OVERRIDE);
9763
9764 if (msleep_interruptible(500))
9765 break;
9766 }
9767 tw32(MAC_LED_CTRL, tp->led_ctrl);
9768 return 0;
9769}
9770
Linus Torvalds1da177e2005-04-16 15:20:36 -07009771static void tg3_get_ethtool_stats (struct net_device *dev,
9772 struct ethtool_stats *estats, u64 *tmp_stats)
9773{
9774 struct tg3 *tp = netdev_priv(dev);
9775 memcpy(tmp_stats, tg3_get_estats(tp), sizeof(tp->estats));
9776}
9777
Michael Chan566f86a2005-05-29 14:56:58 -07009778#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -08009779#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
9780#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
9781#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Michael Chanb16250e2006-09-27 16:10:14 -07009782#define NVRAM_SELFBOOT_HW_SIZE 0x20
9783#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -07009784
9785static int tg3_test_nvram(struct tg3 *tp)
9786{
Al Virob9fc7dc2007-12-17 22:59:57 -08009787 u32 csum, magic;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009788 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009789 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -07009790
Matt Carlsondf259d82009-04-20 06:57:14 +00009791 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM)
9792 return 0;
9793
Matt Carlsone4f34112009-02-25 14:25:00 +00009794 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -08009795 return -EIO;
9796
Michael Chan1b277772006-03-20 22:27:48 -08009797 if (magic == TG3_EEPROM_MAGIC)
9798 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -07009799 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -08009800 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
9801 TG3_EEPROM_SB_FORMAT_1) {
9802 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
9803 case TG3_EEPROM_SB_REVISION_0:
9804 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
9805 break;
9806 case TG3_EEPROM_SB_REVISION_2:
9807 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
9808 break;
9809 case TG3_EEPROM_SB_REVISION_3:
9810 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
9811 break;
9812 default:
9813 return 0;
9814 }
9815 } else
Michael Chan1b277772006-03-20 22:27:48 -08009816 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -07009817 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
9818 size = NVRAM_SELFBOOT_HW_SIZE;
9819 else
Michael Chan1b277772006-03-20 22:27:48 -08009820 return -EIO;
9821
9822 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -07009823 if (buf == NULL)
9824 return -ENOMEM;
9825
Michael Chan1b277772006-03-20 22:27:48 -08009826 err = -EIO;
9827 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +00009828 err = tg3_nvram_read_be32(tp, i, &buf[j]);
9829 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -07009830 break;
Michael Chan566f86a2005-05-29 14:56:58 -07009831 }
Michael Chan1b277772006-03-20 22:27:48 -08009832 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -07009833 goto out;
9834
Michael Chan1b277772006-03-20 22:27:48 -08009835 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +00009836 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -08009837 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -07009838 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -08009839 u8 *buf8 = (u8 *) buf, csum8 = 0;
9840
Al Virob9fc7dc2007-12-17 22:59:57 -08009841 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -08009842 TG3_EEPROM_SB_REVISION_2) {
9843 /* For rev 2, the csum doesn't include the MBA. */
9844 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
9845 csum8 += buf8[i];
9846 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
9847 csum8 += buf8[i];
9848 } else {
9849 for (i = 0; i < size; i++)
9850 csum8 += buf8[i];
9851 }
Michael Chan1b277772006-03-20 22:27:48 -08009852
Adrian Bunkad96b482006-04-05 22:21:04 -07009853 if (csum8 == 0) {
9854 err = 0;
9855 goto out;
9856 }
9857
9858 err = -EIO;
9859 goto out;
Michael Chan1b277772006-03-20 22:27:48 -08009860 }
Michael Chan566f86a2005-05-29 14:56:58 -07009861
Al Virob9fc7dc2007-12-17 22:59:57 -08009862 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -07009863 TG3_EEPROM_MAGIC_HW) {
9864 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +00009865 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -07009866 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -07009867
9868 /* Separate the parity bits and the data bytes. */
9869 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
9870 if ((i == 0) || (i == 8)) {
9871 int l;
9872 u8 msk;
9873
9874 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
9875 parity[k++] = buf8[i] & msk;
9876 i++;
9877 }
9878 else if (i == 16) {
9879 int l;
9880 u8 msk;
9881
9882 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
9883 parity[k++] = buf8[i] & msk;
9884 i++;
9885
9886 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
9887 parity[k++] = buf8[i] & msk;
9888 i++;
9889 }
9890 data[j++] = buf8[i];
9891 }
9892
9893 err = -EIO;
9894 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
9895 u8 hw8 = hweight8(data[i]);
9896
9897 if ((hw8 & 0x1) && parity[i])
9898 goto out;
9899 else if (!(hw8 & 0x1) && !parity[i])
9900 goto out;
9901 }
9902 err = 0;
9903 goto out;
9904 }
9905
Michael Chan566f86a2005-05-29 14:56:58 -07009906 /* Bootstrap checksum at offset 0x10 */
9907 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlsona9dc5292009-02-25 14:25:30 +00009908 if (csum != be32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -07009909 goto out;
9910
9911 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
9912 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlsona9dc5292009-02-25 14:25:30 +00009913 if (csum != be32_to_cpu(buf[0xfc/4]))
9914 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -07009915
9916 err = 0;
9917
9918out:
9919 kfree(buf);
9920 return err;
9921}
9922
Michael Chanca430072005-05-29 14:57:23 -07009923#define TG3_SERDES_TIMEOUT_SEC 2
9924#define TG3_COPPER_TIMEOUT_SEC 6
9925
9926static int tg3_test_link(struct tg3 *tp)
9927{
9928 int i, max;
9929
9930 if (!netif_running(tp->dev))
9931 return -ENODEV;
9932
Michael Chan4c987482005-09-05 17:52:38 -07009933 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -07009934 max = TG3_SERDES_TIMEOUT_SEC;
9935 else
9936 max = TG3_COPPER_TIMEOUT_SEC;
9937
9938 for (i = 0; i < max; i++) {
9939 if (netif_carrier_ok(tp->dev))
9940 return 0;
9941
9942 if (msleep_interruptible(1000))
9943 break;
9944 }
9945
9946 return -EIO;
9947}
9948
Michael Chana71116d2005-05-29 14:58:11 -07009949/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -08009950static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -07009951{
Michael Chanb16250e2006-09-27 16:10:14 -07009952 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -07009953 u32 offset, read_mask, write_mask, val, save_val, read_val;
9954 static struct {
9955 u16 offset;
9956 u16 flags;
9957#define TG3_FL_5705 0x1
9958#define TG3_FL_NOT_5705 0x2
9959#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -07009960#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -07009961 u32 read_mask;
9962 u32 write_mask;
9963 } reg_tbl[] = {
9964 /* MAC Control Registers */
9965 { MAC_MODE, TG3_FL_NOT_5705,
9966 0x00000000, 0x00ef6f8c },
9967 { MAC_MODE, TG3_FL_5705,
9968 0x00000000, 0x01ef6b8c },
9969 { MAC_STATUS, TG3_FL_NOT_5705,
9970 0x03800107, 0x00000000 },
9971 { MAC_STATUS, TG3_FL_5705,
9972 0x03800100, 0x00000000 },
9973 { MAC_ADDR_0_HIGH, 0x0000,
9974 0x00000000, 0x0000ffff },
9975 { MAC_ADDR_0_LOW, 0x0000,
9976 0x00000000, 0xffffffff },
9977 { MAC_RX_MTU_SIZE, 0x0000,
9978 0x00000000, 0x0000ffff },
9979 { MAC_TX_MODE, 0x0000,
9980 0x00000000, 0x00000070 },
9981 { MAC_TX_LENGTHS, 0x0000,
9982 0x00000000, 0x00003fff },
9983 { MAC_RX_MODE, TG3_FL_NOT_5705,
9984 0x00000000, 0x000007fc },
9985 { MAC_RX_MODE, TG3_FL_5705,
9986 0x00000000, 0x000007dc },
9987 { MAC_HASH_REG_0, 0x0000,
9988 0x00000000, 0xffffffff },
9989 { MAC_HASH_REG_1, 0x0000,
9990 0x00000000, 0xffffffff },
9991 { MAC_HASH_REG_2, 0x0000,
9992 0x00000000, 0xffffffff },
9993 { MAC_HASH_REG_3, 0x0000,
9994 0x00000000, 0xffffffff },
9995
9996 /* Receive Data and Receive BD Initiator Control Registers. */
9997 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
9998 0x00000000, 0xffffffff },
9999 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
10000 0x00000000, 0xffffffff },
10001 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
10002 0x00000000, 0x00000003 },
10003 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
10004 0x00000000, 0xffffffff },
10005 { RCVDBDI_STD_BD+0, 0x0000,
10006 0x00000000, 0xffffffff },
10007 { RCVDBDI_STD_BD+4, 0x0000,
10008 0x00000000, 0xffffffff },
10009 { RCVDBDI_STD_BD+8, 0x0000,
10010 0x00000000, 0xffff0002 },
10011 { RCVDBDI_STD_BD+0xc, 0x0000,
10012 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010013
Michael Chana71116d2005-05-29 14:58:11 -070010014 /* Receive BD Initiator Control Registers. */
10015 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
10016 0x00000000, 0xffffffff },
10017 { RCVBDI_STD_THRESH, TG3_FL_5705,
10018 0x00000000, 0x000003ff },
10019 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
10020 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010021
Michael Chana71116d2005-05-29 14:58:11 -070010022 /* Host Coalescing Control Registers. */
10023 { HOSTCC_MODE, TG3_FL_NOT_5705,
10024 0x00000000, 0x00000004 },
10025 { HOSTCC_MODE, TG3_FL_5705,
10026 0x00000000, 0x000000f6 },
10027 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
10028 0x00000000, 0xffffffff },
10029 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
10030 0x00000000, 0x000003ff },
10031 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
10032 0x00000000, 0xffffffff },
10033 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
10034 0x00000000, 0x000003ff },
10035 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
10036 0x00000000, 0xffffffff },
10037 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10038 0x00000000, 0x000000ff },
10039 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
10040 0x00000000, 0xffffffff },
10041 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10042 0x00000000, 0x000000ff },
10043 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
10044 0x00000000, 0xffffffff },
10045 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
10046 0x00000000, 0xffffffff },
10047 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10048 0x00000000, 0xffffffff },
10049 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10050 0x00000000, 0x000000ff },
10051 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10052 0x00000000, 0xffffffff },
10053 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10054 0x00000000, 0x000000ff },
10055 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
10056 0x00000000, 0xffffffff },
10057 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
10058 0x00000000, 0xffffffff },
10059 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
10060 0x00000000, 0xffffffff },
10061 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
10062 0x00000000, 0xffffffff },
10063 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
10064 0x00000000, 0xffffffff },
10065 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
10066 0xffffffff, 0x00000000 },
10067 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
10068 0xffffffff, 0x00000000 },
10069
10070 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070010071 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010072 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070010073 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010074 0x00000000, 0x007fffff },
10075 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
10076 0x00000000, 0x0000003f },
10077 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
10078 0x00000000, 0x000001ff },
10079 { BUFMGR_MB_HIGH_WATER, 0x0000,
10080 0x00000000, 0x000001ff },
10081 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
10082 0xffffffff, 0x00000000 },
10083 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
10084 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010085
Michael Chana71116d2005-05-29 14:58:11 -070010086 /* Mailbox Registers */
10087 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
10088 0x00000000, 0x000001ff },
10089 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
10090 0x00000000, 0x000001ff },
10091 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
10092 0x00000000, 0x000007ff },
10093 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
10094 0x00000000, 0x000001ff },
10095
10096 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
10097 };
10098
Michael Chanb16250e2006-09-27 16:10:14 -070010099 is_5705 = is_5750 = 0;
10100 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
Michael Chana71116d2005-05-29 14:58:11 -070010101 is_5705 = 1;
Michael Chanb16250e2006-09-27 16:10:14 -070010102 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
10103 is_5750 = 1;
10104 }
Michael Chana71116d2005-05-29 14:58:11 -070010105
10106 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
10107 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
10108 continue;
10109
10110 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
10111 continue;
10112
10113 if ((tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
10114 (reg_tbl[i].flags & TG3_FL_NOT_5788))
10115 continue;
10116
Michael Chanb16250e2006-09-27 16:10:14 -070010117 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
10118 continue;
10119
Michael Chana71116d2005-05-29 14:58:11 -070010120 offset = (u32) reg_tbl[i].offset;
10121 read_mask = reg_tbl[i].read_mask;
10122 write_mask = reg_tbl[i].write_mask;
10123
10124 /* Save the original register content */
10125 save_val = tr32(offset);
10126
10127 /* Determine the read-only value. */
10128 read_val = save_val & read_mask;
10129
10130 /* Write zero to the register, then make sure the read-only bits
10131 * are not changed and the read/write bits are all zeros.
10132 */
10133 tw32(offset, 0);
10134
10135 val = tr32(offset);
10136
10137 /* Test the read-only and read/write bits. */
10138 if (((val & read_mask) != read_val) || (val & write_mask))
10139 goto out;
10140
10141 /* Write ones to all the bits defined by RdMask and WrMask, then
10142 * make sure the read-only bits are not changed and the
10143 * read/write bits are all ones.
10144 */
10145 tw32(offset, read_mask | write_mask);
10146
10147 val = tr32(offset);
10148
10149 /* Test the read-only bits. */
10150 if ((val & read_mask) != read_val)
10151 goto out;
10152
10153 /* Test the read/write bits. */
10154 if ((val & write_mask) != write_mask)
10155 goto out;
10156
10157 tw32(offset, save_val);
10158 }
10159
10160 return 0;
10161
10162out:
Michael Chan9f88f292006-12-07 00:22:54 -080010163 if (netif_msg_hw(tp))
10164 printk(KERN_ERR PFX "Register test failed at offset %x\n",
10165 offset);
Michael Chana71116d2005-05-29 14:58:11 -070010166 tw32(offset, save_val);
10167 return -EIO;
10168}
10169
Michael Chan7942e1d2005-05-29 14:58:36 -070010170static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
10171{
Arjan van de Venf71e1302006-03-03 21:33:57 -050010172 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070010173 int i;
10174 u32 j;
10175
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020010176 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070010177 for (j = 0; j < len; j += 4) {
10178 u32 val;
10179
10180 tg3_write_mem(tp, offset + j, test_pattern[i]);
10181 tg3_read_mem(tp, offset + j, &val);
10182 if (val != test_pattern[i])
10183 return -EIO;
10184 }
10185 }
10186 return 0;
10187}
10188
10189static int tg3_test_memory(struct tg3 *tp)
10190{
10191 static struct mem_entry {
10192 u32 offset;
10193 u32 len;
10194 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080010195 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070010196 { 0x00002000, 0x1c000},
10197 { 0xffffffff, 0x00000}
10198 }, mem_tbl_5705[] = {
10199 { 0x00000100, 0x0000c},
10200 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070010201 { 0x00004000, 0x00800},
10202 { 0x00006000, 0x01000},
10203 { 0x00008000, 0x02000},
10204 { 0x00010000, 0x0e000},
10205 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080010206 }, mem_tbl_5755[] = {
10207 { 0x00000200, 0x00008},
10208 { 0x00004000, 0x00800},
10209 { 0x00006000, 0x00800},
10210 { 0x00008000, 0x02000},
10211 { 0x00010000, 0x0c000},
10212 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070010213 }, mem_tbl_5906[] = {
10214 { 0x00000200, 0x00008},
10215 { 0x00004000, 0x00400},
10216 { 0x00006000, 0x00400},
10217 { 0x00008000, 0x01000},
10218 { 0x00010000, 0x01000},
10219 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070010220 };
10221 struct mem_entry *mem_tbl;
10222 int err = 0;
10223 int i;
10224
Matt Carlson321d32a2008-11-21 17:22:19 -080010225 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
10226 mem_tbl = mem_tbl_5755;
10227 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
10228 mem_tbl = mem_tbl_5906;
10229 else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
10230 mem_tbl = mem_tbl_5705;
10231 else
Michael Chan7942e1d2005-05-29 14:58:36 -070010232 mem_tbl = mem_tbl_570x;
10233
10234 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
10235 if ((err = tg3_do_mem_test(tp, mem_tbl[i].offset,
10236 mem_tbl[i].len)) != 0)
10237 break;
10238 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010239
Michael Chan7942e1d2005-05-29 14:58:36 -070010240 return err;
10241}
10242
Michael Chan9f40dea2005-09-05 17:53:06 -070010243#define TG3_MAC_LOOPBACK 0
10244#define TG3_PHY_LOOPBACK 1
10245
10246static int tg3_run_loopback(struct tg3 *tp, int loopback_mode)
Michael Chanc76949a2005-05-29 14:58:59 -070010247{
Michael Chan9f40dea2005-09-05 17:53:06 -070010248 u32 mac_mode, rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010249 u32 desc_idx, coal_now;
Michael Chanc76949a2005-05-29 14:58:59 -070010250 struct sk_buff *skb, *rx_skb;
10251 u8 *tx_data;
10252 dma_addr_t map;
10253 int num_pkts, tx_len, rx_len, i, err;
10254 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000010255 struct tg3_napi *tnapi, *rnapi;
Matt Carlson21f581a2009-08-28 14:00:25 +000010256 struct tg3_rx_prodring_set *tpr = &tp->prodring[0];
Michael Chanc76949a2005-05-29 14:58:59 -070010257
Matt Carlson0c1d0e22009-09-01 13:16:33 +000010258 if (tp->irq_cnt > 1) {
10259 tnapi = &tp->napi[1];
10260 rnapi = &tp->napi[1];
10261 } else {
10262 tnapi = &tp->napi[0];
10263 rnapi = &tp->napi[0];
10264 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010265 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000010266
Michael Chan9f40dea2005-09-05 17:53:06 -070010267 if (loopback_mode == TG3_MAC_LOOPBACK) {
Michael Chanc94e3942005-09-27 12:12:42 -070010268 /* HW errata - mac loopback fails in some cases on 5780.
10269 * Normal traffic and PHY loopback are not affected by
10270 * errata.
10271 */
10272 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780)
10273 return 0;
10274
Michael Chan9f40dea2005-09-05 17:53:06 -070010275 mac_mode = (tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK) |
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010276 MAC_MODE_PORT_INT_LPBACK;
10277 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
10278 mac_mode |= MAC_MODE_LINK_POLARITY;
Michael Chan3f7045c2006-09-27 16:02:29 -070010279 if (tp->tg3_flags & TG3_FLAG_10_100_ONLY)
10280 mac_mode |= MAC_MODE_PORT_MODE_MII;
10281 else
10282 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chan9f40dea2005-09-05 17:53:06 -070010283 tw32(MAC_MODE, mac_mode);
10284 } else if (loopback_mode == TG3_PHY_LOOPBACK) {
Michael Chan3f7045c2006-09-27 16:02:29 -070010285 u32 val;
10286
Matt Carlson7f97a4b2009-08-25 10:10:03 +000010287 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
10288 tg3_phy_fet_toggle_apd(tp, false);
Michael Chan5d64ad32006-12-07 00:19:40 -080010289 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED100;
10290 } else
10291 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED1000;
Michael Chan3f7045c2006-09-27 16:02:29 -070010292
Matt Carlson9ef8ca92007-07-11 19:48:29 -070010293 tg3_phy_toggle_automdix(tp, 0);
10294
Michael Chan3f7045c2006-09-27 16:02:29 -070010295 tg3_writephy(tp, MII_BMCR, val);
Michael Chanc94e3942005-09-27 12:12:42 -070010296 udelay(40);
Michael Chan5d64ad32006-12-07 00:19:40 -080010297
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010298 mac_mode = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000010299 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
10300 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
10301 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x1800);
Michael Chan5d64ad32006-12-07 00:19:40 -080010302 mac_mode |= MAC_MODE_PORT_MODE_MII;
10303 } else
10304 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chanb16250e2006-09-27 16:10:14 -070010305
Michael Chanc94e3942005-09-27 12:12:42 -070010306 /* reset to prevent losing 1st rx packet intermittently */
10307 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES) {
10308 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
10309 udelay(10);
10310 tw32_f(MAC_RX_MODE, tp->rx_mode);
10311 }
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010312 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
10313 if ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5401)
10314 mac_mode &= ~MAC_MODE_LINK_POLARITY;
10315 else if ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5411)
10316 mac_mode |= MAC_MODE_LINK_POLARITY;
Michael Chanff18ff02006-03-27 23:17:27 -080010317 tg3_writephy(tp, MII_TG3_EXT_CTRL,
10318 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
10319 }
Michael Chan9f40dea2005-09-05 17:53:06 -070010320 tw32(MAC_MODE, mac_mode);
Michael Chan9f40dea2005-09-05 17:53:06 -070010321 }
10322 else
10323 return -EINVAL;
Michael Chanc76949a2005-05-29 14:58:59 -070010324
10325 err = -EIO;
10326
Michael Chanc76949a2005-05-29 14:58:59 -070010327 tx_len = 1514;
David S. Millera20e9c62006-07-31 22:38:16 -070010328 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070010329 if (!skb)
10330 return -ENOMEM;
10331
Michael Chanc76949a2005-05-29 14:58:59 -070010332 tx_data = skb_put(skb, tx_len);
10333 memcpy(tx_data, tp->dev->dev_addr, 6);
10334 memset(tx_data + 6, 0x0, 8);
10335
10336 tw32(MAC_RX_MTU_SIZE, tx_len + 4);
10337
10338 for (i = 14; i < tx_len; i++)
10339 tx_data[i] = (u8) (i & 0xff);
10340
10341 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
10342
10343 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010344 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070010345
10346 udelay(10);
10347
Matt Carlson898a56f2009-08-28 14:02:40 +000010348 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070010349
Michael Chanc76949a2005-05-29 14:58:59 -070010350 num_pkts = 0;
10351
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010352 tg3_set_txd(tnapi, tnapi->tx_prod, map, tx_len, 0, 1);
Michael Chanc76949a2005-05-29 14:58:59 -070010353
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010354 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070010355 num_pkts++;
10356
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010357 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
10358 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070010359
10360 udelay(10);
10361
Michael Chan3f7045c2006-09-27 16:02:29 -070010362 /* 250 usec to allow enough time on some 10/100 Mbps devices. */
10363 for (i = 0; i < 25; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070010364 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010365 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070010366
10367 udelay(10);
10368
Matt Carlson898a56f2009-08-28 14:02:40 +000010369 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
10370 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010371 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070010372 (rx_idx == (rx_start_idx + num_pkts)))
10373 break;
10374 }
10375
10376 pci_unmap_single(tp->pdev, map, tx_len, PCI_DMA_TODEVICE);
10377 dev_kfree_skb(skb);
10378
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010379 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070010380 goto out;
10381
10382 if (rx_idx != rx_start_idx + num_pkts)
10383 goto out;
10384
Matt Carlson72334482009-08-28 14:03:01 +000010385 desc = &rnapi->rx_rcb[rx_start_idx];
Michael Chanc76949a2005-05-29 14:58:59 -070010386 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
10387 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
10388 if (opaque_key != RXD_OPAQUE_RING_STD)
10389 goto out;
10390
10391 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
10392 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
10393 goto out;
10394
10395 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) - 4;
10396 if (rx_len != tx_len)
10397 goto out;
10398
Matt Carlson21f581a2009-08-28 14:00:25 +000010399 rx_skb = tpr->rx_std_buffers[desc_idx].skb;
Michael Chanc76949a2005-05-29 14:58:59 -070010400
Matt Carlson21f581a2009-08-28 14:00:25 +000010401 map = pci_unmap_addr(&tpr->rx_std_buffers[desc_idx], mapping);
Michael Chanc76949a2005-05-29 14:58:59 -070010402 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len, PCI_DMA_FROMDEVICE);
10403
10404 for (i = 14; i < tx_len; i++) {
10405 if (*(rx_skb->data + i) != (u8) (i & 0xff))
10406 goto out;
10407 }
10408 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010409
Michael Chanc76949a2005-05-29 14:58:59 -070010410 /* tg3_free_rings will unmap and free the rx_skb */
10411out:
10412 return err;
10413}
10414
Michael Chan9f40dea2005-09-05 17:53:06 -070010415#define TG3_MAC_LOOPBACK_FAILED 1
10416#define TG3_PHY_LOOPBACK_FAILED 2
10417#define TG3_LOOPBACK_FAILED (TG3_MAC_LOOPBACK_FAILED | \
10418 TG3_PHY_LOOPBACK_FAILED)
10419
10420static int tg3_test_loopback(struct tg3 *tp)
10421{
10422 int err = 0;
Matt Carlson9936bcf2007-10-10 18:03:07 -070010423 u32 cpmuctrl = 0;
Michael Chan9f40dea2005-09-05 17:53:06 -070010424
10425 if (!netif_running(tp->dev))
10426 return TG3_LOOPBACK_FAILED;
10427
Michael Chanb9ec6c12006-07-25 16:37:27 -070010428 err = tg3_reset_hw(tp, 1);
10429 if (err)
10430 return TG3_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070010431
Matt Carlson6833c042008-11-21 17:18:59 -080010432 /* Turn off gphy autopowerdown. */
10433 if (tp->tg3_flags3 & TG3_FLG3_PHY_ENABLE_APD)
10434 tg3_phy_toggle_apd(tp, false);
10435
Matt Carlson321d32a2008-11-21 17:22:19 -080010436 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070010437 int i;
10438 u32 status;
10439
10440 tw32(TG3_CPMU_MUTEX_REQ, CPMU_MUTEX_REQ_DRIVER);
10441
10442 /* Wait for up to 40 microseconds to acquire lock. */
10443 for (i = 0; i < 4; i++) {
10444 status = tr32(TG3_CPMU_MUTEX_GNT);
10445 if (status == CPMU_MUTEX_GNT_DRIVER)
10446 break;
10447 udelay(10);
10448 }
10449
10450 if (status != CPMU_MUTEX_GNT_DRIVER)
10451 return TG3_LOOPBACK_FAILED;
10452
Matt Carlsonb2a5c192008-04-03 21:44:44 -070010453 /* Turn off link-based power management. */
Matt Carlsone8750932007-11-12 21:11:51 -080010454 cpmuctrl = tr32(TG3_CPMU_CTRL);
Matt Carlson109115e2008-05-02 16:48:59 -070010455 tw32(TG3_CPMU_CTRL,
10456 cpmuctrl & ~(CPMU_CTRL_LINK_SPEED_MODE |
10457 CPMU_CTRL_LINK_AWARE_MODE));
Matt Carlson9936bcf2007-10-10 18:03:07 -070010458 }
10459
Michael Chan9f40dea2005-09-05 17:53:06 -070010460 if (tg3_run_loopback(tp, TG3_MAC_LOOPBACK))
10461 err |= TG3_MAC_LOOPBACK_FAILED;
Matt Carlson9936bcf2007-10-10 18:03:07 -070010462
Matt Carlson321d32a2008-11-21 17:22:19 -080010463 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070010464 tw32(TG3_CPMU_CTRL, cpmuctrl);
10465
10466 /* Release the mutex */
10467 tw32(TG3_CPMU_MUTEX_GNT, CPMU_MUTEX_GNT_DRIVER);
10468 }
10469
Matt Carlsondd477002008-05-25 23:45:58 -070010470 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
10471 !(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)) {
Michael Chan9f40dea2005-09-05 17:53:06 -070010472 if (tg3_run_loopback(tp, TG3_PHY_LOOPBACK))
10473 err |= TG3_PHY_LOOPBACK_FAILED;
10474 }
10475
Matt Carlson6833c042008-11-21 17:18:59 -080010476 /* Re-enable gphy autopowerdown. */
10477 if (tp->tg3_flags3 & TG3_FLG3_PHY_ENABLE_APD)
10478 tg3_phy_toggle_apd(tp, true);
10479
Michael Chan9f40dea2005-09-05 17:53:06 -070010480 return err;
10481}
10482
Michael Chan4cafd3f2005-05-29 14:56:34 -070010483static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
10484 u64 *data)
10485{
Michael Chan566f86a2005-05-29 14:56:58 -070010486 struct tg3 *tp = netdev_priv(dev);
10487
Michael Chanbc1c7562006-03-20 17:48:03 -080010488 if (tp->link_config.phy_is_low_power)
10489 tg3_set_power_state(tp, PCI_D0);
10490
Michael Chan566f86a2005-05-29 14:56:58 -070010491 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
10492
10493 if (tg3_test_nvram(tp) != 0) {
10494 etest->flags |= ETH_TEST_FL_FAILED;
10495 data[0] = 1;
10496 }
Michael Chanca430072005-05-29 14:57:23 -070010497 if (tg3_test_link(tp) != 0) {
10498 etest->flags |= ETH_TEST_FL_FAILED;
10499 data[1] = 1;
10500 }
Michael Chana71116d2005-05-29 14:58:11 -070010501 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010502 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070010503
Michael Chanbbe832c2005-06-24 20:20:04 -070010504 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010505 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010506 tg3_netif_stop(tp);
10507 irq_sync = 1;
10508 }
10509
10510 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070010511
10512 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080010513 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070010514 tg3_halt_cpu(tp, RX_CPU_BASE);
10515 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
10516 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080010517 if (!err)
10518 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070010519
Michael Chand9ab5ad12006-03-20 22:27:35 -080010520 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)
10521 tg3_phy_reset(tp);
10522
Michael Chana71116d2005-05-29 14:58:11 -070010523 if (tg3_test_registers(tp) != 0) {
10524 etest->flags |= ETH_TEST_FL_FAILED;
10525 data[2] = 1;
10526 }
Michael Chan7942e1d2005-05-29 14:58:36 -070010527 if (tg3_test_memory(tp) != 0) {
10528 etest->flags |= ETH_TEST_FL_FAILED;
10529 data[3] = 1;
10530 }
Michael Chan9f40dea2005-09-05 17:53:06 -070010531 if ((data[4] = tg3_test_loopback(tp)) != 0)
Michael Chanc76949a2005-05-29 14:58:59 -070010532 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070010533
David S. Millerf47c11e2005-06-24 20:18:35 -070010534 tg3_full_unlock(tp);
10535
Michael Chand4bc3922005-05-29 14:59:20 -070010536 if (tg3_test_interrupt(tp) != 0) {
10537 etest->flags |= ETH_TEST_FL_FAILED;
10538 data[5] = 1;
10539 }
David S. Millerf47c11e2005-06-24 20:18:35 -070010540
10541 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070010542
Michael Chana71116d2005-05-29 14:58:11 -070010543 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10544 if (netif_running(dev)) {
10545 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010546 err2 = tg3_restart_hw(tp, 1);
10547 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070010548 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070010549 }
David S. Millerf47c11e2005-06-24 20:18:35 -070010550
10551 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010552
10553 if (irq_sync && !err2)
10554 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070010555 }
Michael Chanbc1c7562006-03-20 17:48:03 -080010556 if (tp->link_config.phy_is_low_power)
10557 tg3_set_power_state(tp, PCI_D3hot);
10558
Michael Chan4cafd3f2005-05-29 14:56:34 -070010559}
10560
Linus Torvalds1da177e2005-04-16 15:20:36 -070010561static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
10562{
10563 struct mii_ioctl_data *data = if_mii(ifr);
10564 struct tg3 *tp = netdev_priv(dev);
10565 int err;
10566
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010567 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
10568 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
10569 return -EAGAIN;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -070010570 return phy_mii_ioctl(tp->mdio_bus->phy_map[PHY_ADDR], data, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010571 }
10572
Linus Torvalds1da177e2005-04-16 15:20:36 -070010573 switch(cmd) {
10574 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000010575 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010576
10577 /* fallthru */
10578 case SIOCGMIIREG: {
10579 u32 mii_regval;
10580
10581 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
10582 break; /* We have no PHY */
10583
Michael Chanbc1c7562006-03-20 17:48:03 -080010584 if (tp->link_config.phy_is_low_power)
10585 return -EAGAIN;
10586
David S. Millerf47c11e2005-06-24 20:18:35 -070010587 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010588 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070010589 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010590
10591 data->val_out = mii_regval;
10592
10593 return err;
10594 }
10595
10596 case SIOCSMIIREG:
10597 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
10598 break; /* We have no PHY */
10599
Michael Chanbc1c7562006-03-20 17:48:03 -080010600 if (tp->link_config.phy_is_low_power)
10601 return -EAGAIN;
10602
David S. Millerf47c11e2005-06-24 20:18:35 -070010603 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010604 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070010605 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010606
10607 return err;
10608
10609 default:
10610 /* do nothing */
10611 break;
10612 }
10613 return -EOPNOTSUPP;
10614}
10615
10616#if TG3_VLAN_TAG_USED
10617static void tg3_vlan_rx_register(struct net_device *dev, struct vlan_group *grp)
10618{
10619 struct tg3 *tp = netdev_priv(dev);
10620
Matt Carlson844b3ee2009-02-25 14:23:56 +000010621 if (!netif_running(dev)) {
10622 tp->vlgrp = grp;
10623 return;
10624 }
10625
10626 tg3_netif_stop(tp);
Michael Chan29315e82006-06-29 20:12:30 -070010627
David S. Millerf47c11e2005-06-24 20:18:35 -070010628 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010629
10630 tp->vlgrp = grp;
10631
10632 /* Update RX_MODE_KEEP_VLAN_TAG bit in RX_MODE register. */
10633 __tg3_set_rx_mode(dev);
10634
Matt Carlson844b3ee2009-02-25 14:23:56 +000010635 tg3_netif_start(tp);
Michael Chan46966542007-07-11 19:47:19 -070010636
10637 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010638}
Linus Torvalds1da177e2005-04-16 15:20:36 -070010639#endif
10640
David S. Miller15f98502005-05-18 22:49:26 -070010641static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
10642{
10643 struct tg3 *tp = netdev_priv(dev);
10644
10645 memcpy(ec, &tp->coal, sizeof(*ec));
10646 return 0;
10647}
10648
Michael Chand244c892005-07-05 14:42:33 -070010649static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
10650{
10651 struct tg3 *tp = netdev_priv(dev);
10652 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
10653 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
10654
10655 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
10656 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
10657 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
10658 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
10659 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
10660 }
10661
10662 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
10663 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
10664 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
10665 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
10666 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
10667 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
10668 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
10669 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
10670 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
10671 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
10672 return -EINVAL;
10673
10674 /* No rx interrupts will be generated if both are zero */
10675 if ((ec->rx_coalesce_usecs == 0) &&
10676 (ec->rx_max_coalesced_frames == 0))
10677 return -EINVAL;
10678
10679 /* No tx interrupts will be generated if both are zero */
10680 if ((ec->tx_coalesce_usecs == 0) &&
10681 (ec->tx_max_coalesced_frames == 0))
10682 return -EINVAL;
10683
10684 /* Only copy relevant parameters, ignore all others. */
10685 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
10686 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
10687 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
10688 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
10689 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
10690 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
10691 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
10692 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
10693 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
10694
10695 if (netif_running(dev)) {
10696 tg3_full_lock(tp, 0);
10697 __tg3_set_coalesce(tp, &tp->coal);
10698 tg3_full_unlock(tp);
10699 }
10700 return 0;
10701}
10702
Jeff Garzik7282d492006-09-13 14:30:00 -040010703static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010704 .get_settings = tg3_get_settings,
10705 .set_settings = tg3_set_settings,
10706 .get_drvinfo = tg3_get_drvinfo,
10707 .get_regs_len = tg3_get_regs_len,
10708 .get_regs = tg3_get_regs,
10709 .get_wol = tg3_get_wol,
10710 .set_wol = tg3_set_wol,
10711 .get_msglevel = tg3_get_msglevel,
10712 .set_msglevel = tg3_set_msglevel,
10713 .nway_reset = tg3_nway_reset,
10714 .get_link = ethtool_op_get_link,
10715 .get_eeprom_len = tg3_get_eeprom_len,
10716 .get_eeprom = tg3_get_eeprom,
10717 .set_eeprom = tg3_set_eeprom,
10718 .get_ringparam = tg3_get_ringparam,
10719 .set_ringparam = tg3_set_ringparam,
10720 .get_pauseparam = tg3_get_pauseparam,
10721 .set_pauseparam = tg3_set_pauseparam,
10722 .get_rx_csum = tg3_get_rx_csum,
10723 .set_rx_csum = tg3_set_rx_csum,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010724 .set_tx_csum = tg3_set_tx_csum,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010725 .set_sg = ethtool_op_set_sg,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010726 .set_tso = tg3_set_tso,
Michael Chan4cafd3f2005-05-29 14:56:34 -070010727 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010728 .get_strings = tg3_get_strings,
Michael Chan4009a932005-09-05 17:52:54 -070010729 .phys_id = tg3_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010730 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070010731 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070010732 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010733 .get_sset_count = tg3_get_sset_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010734};
10735
10736static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
10737{
Michael Chan1b277772006-03-20 22:27:48 -080010738 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010739
10740 tp->nvram_size = EEPROM_CHIP_SIZE;
10741
Matt Carlsone4f34112009-02-25 14:25:00 +000010742 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010743 return;
10744
Michael Chanb16250e2006-09-27 16:10:14 -070010745 if ((magic != TG3_EEPROM_MAGIC) &&
10746 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
10747 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010748 return;
10749
10750 /*
10751 * Size the chip by reading offsets at increasing powers of two.
10752 * When we encounter our validation signature, we know the addressing
10753 * has wrapped around, and thus have our chip size.
10754 */
Michael Chan1b277772006-03-20 22:27:48 -080010755 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010756
10757 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000010758 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010759 return;
10760
Michael Chan18201802006-03-20 22:29:15 -080010761 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010762 break;
10763
10764 cursize <<= 1;
10765 }
10766
10767 tp->nvram_size = cursize;
10768}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010769
Linus Torvalds1da177e2005-04-16 15:20:36 -070010770static void __devinit tg3_get_nvram_size(struct tg3 *tp)
10771{
10772 u32 val;
10773
Matt Carlsondf259d82009-04-20 06:57:14 +000010774 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
10775 tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080010776 return;
10777
10778 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080010779 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080010780 tg3_get_eeprom_size(tp);
10781 return;
10782 }
10783
Matt Carlson6d348f22009-02-25 14:25:52 +000010784 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010785 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000010786 /* This is confusing. We want to operate on the
10787 * 16-bit value at offset 0xf2. The tg3_nvram_read()
10788 * call will read from NVRAM and byteswap the data
10789 * according to the byteswapping settings for all
10790 * other register accesses. This ensures the data we
10791 * want will always reside in the lower 16-bits.
10792 * However, the data in NVRAM is in LE format, which
10793 * means the data from the NVRAM read will always be
10794 * opposite the endianness of the CPU. The 16-bit
10795 * byteswap then brings the data to CPU endianness.
10796 */
10797 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010798 return;
10799 }
10800 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070010801 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010802}
10803
10804static void __devinit tg3_get_nvram_info(struct tg3 *tp)
10805{
10806 u32 nvcfg1;
10807
10808 nvcfg1 = tr32(NVRAM_CFG1);
10809 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
10810 tp->tg3_flags2 |= TG3_FLG2_FLASH;
Matt Carlson8590a602009-08-28 12:29:16 +000010811 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010812 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
10813 tw32(NVRAM_CFG1, nvcfg1);
10814 }
10815
Michael Chan4c987482005-09-05 17:52:38 -070010816 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750) ||
Michael Chana4e2b342005-10-26 15:46:52 -070010817 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010818 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000010819 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
10820 tp->nvram_jedecnum = JEDEC_ATMEL;
10821 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
10822 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10823 break;
10824 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
10825 tp->nvram_jedecnum = JEDEC_ATMEL;
10826 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
10827 break;
10828 case FLASH_VENDOR_ATMEL_EEPROM:
10829 tp->nvram_jedecnum = JEDEC_ATMEL;
10830 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
10831 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10832 break;
10833 case FLASH_VENDOR_ST:
10834 tp->nvram_jedecnum = JEDEC_ST;
10835 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
10836 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10837 break;
10838 case FLASH_VENDOR_SAIFUN:
10839 tp->nvram_jedecnum = JEDEC_SAIFUN;
10840 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
10841 break;
10842 case FLASH_VENDOR_SST_SMALL:
10843 case FLASH_VENDOR_SST_LARGE:
10844 tp->nvram_jedecnum = JEDEC_SST;
10845 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
10846 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010847 }
Matt Carlson8590a602009-08-28 12:29:16 +000010848 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010849 tp->nvram_jedecnum = JEDEC_ATMEL;
10850 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
10851 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10852 }
10853}
10854
Matt Carlsona1b950d2009-09-01 13:20:17 +000010855static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
10856{
10857 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
10858 case FLASH_5752PAGE_SIZE_256:
10859 tp->nvram_pagesize = 256;
10860 break;
10861 case FLASH_5752PAGE_SIZE_512:
10862 tp->nvram_pagesize = 512;
10863 break;
10864 case FLASH_5752PAGE_SIZE_1K:
10865 tp->nvram_pagesize = 1024;
10866 break;
10867 case FLASH_5752PAGE_SIZE_2K:
10868 tp->nvram_pagesize = 2048;
10869 break;
10870 case FLASH_5752PAGE_SIZE_4K:
10871 tp->nvram_pagesize = 4096;
10872 break;
10873 case FLASH_5752PAGE_SIZE_264:
10874 tp->nvram_pagesize = 264;
10875 break;
10876 case FLASH_5752PAGE_SIZE_528:
10877 tp->nvram_pagesize = 528;
10878 break;
10879 }
10880}
10881
Michael Chan361b4ac2005-04-21 17:11:21 -070010882static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
10883{
10884 u32 nvcfg1;
10885
10886 nvcfg1 = tr32(NVRAM_CFG1);
10887
Michael Chane6af3012005-04-21 17:12:05 -070010888 /* NVRAM protection for TPM */
10889 if (nvcfg1 & (1 << 27))
10890 tp->tg3_flags2 |= TG3_FLG2_PROTECTED_NVRAM;
10891
Michael Chan361b4ac2005-04-21 17:11:21 -070010892 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000010893 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
10894 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
10895 tp->nvram_jedecnum = JEDEC_ATMEL;
10896 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10897 break;
10898 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
10899 tp->nvram_jedecnum = JEDEC_ATMEL;
10900 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10901 tp->tg3_flags2 |= TG3_FLG2_FLASH;
10902 break;
10903 case FLASH_5752VENDOR_ST_M45PE10:
10904 case FLASH_5752VENDOR_ST_M45PE20:
10905 case FLASH_5752VENDOR_ST_M45PE40:
10906 tp->nvram_jedecnum = JEDEC_ST;
10907 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10908 tp->tg3_flags2 |= TG3_FLG2_FLASH;
10909 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070010910 }
10911
10912 if (tp->tg3_flags2 & TG3_FLG2_FLASH) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000010913 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000010914 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070010915 /* For eeprom, set pagesize to maximum eeprom size */
10916 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
10917
10918 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
10919 tw32(NVRAM_CFG1, nvcfg1);
10920 }
10921}
10922
Michael Chand3c7b882006-03-23 01:28:25 -080010923static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
10924{
Matt Carlson989a9d22007-05-05 11:51:05 -070010925 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080010926
10927 nvcfg1 = tr32(NVRAM_CFG1);
10928
10929 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070010930 if (nvcfg1 & (1 << 27)) {
Michael Chand3c7b882006-03-23 01:28:25 -080010931 tp->tg3_flags2 |= TG3_FLG2_PROTECTED_NVRAM;
Matt Carlson989a9d22007-05-05 11:51:05 -070010932 protect = 1;
10933 }
Michael Chand3c7b882006-03-23 01:28:25 -080010934
Matt Carlson989a9d22007-05-05 11:51:05 -070010935 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
10936 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000010937 case FLASH_5755VENDOR_ATMEL_FLASH_1:
10938 case FLASH_5755VENDOR_ATMEL_FLASH_2:
10939 case FLASH_5755VENDOR_ATMEL_FLASH_3:
10940 case FLASH_5755VENDOR_ATMEL_FLASH_5:
10941 tp->nvram_jedecnum = JEDEC_ATMEL;
10942 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10943 tp->tg3_flags2 |= TG3_FLG2_FLASH;
10944 tp->nvram_pagesize = 264;
10945 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
10946 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
10947 tp->nvram_size = (protect ? 0x3e200 :
10948 TG3_NVRAM_SIZE_512KB);
10949 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
10950 tp->nvram_size = (protect ? 0x1f200 :
10951 TG3_NVRAM_SIZE_256KB);
10952 else
10953 tp->nvram_size = (protect ? 0x1f200 :
10954 TG3_NVRAM_SIZE_128KB);
10955 break;
10956 case FLASH_5752VENDOR_ST_M45PE10:
10957 case FLASH_5752VENDOR_ST_M45PE20:
10958 case FLASH_5752VENDOR_ST_M45PE40:
10959 tp->nvram_jedecnum = JEDEC_ST;
10960 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10961 tp->tg3_flags2 |= TG3_FLG2_FLASH;
10962 tp->nvram_pagesize = 256;
10963 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
10964 tp->nvram_size = (protect ?
10965 TG3_NVRAM_SIZE_64KB :
10966 TG3_NVRAM_SIZE_128KB);
10967 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
10968 tp->nvram_size = (protect ?
10969 TG3_NVRAM_SIZE_64KB :
10970 TG3_NVRAM_SIZE_256KB);
10971 else
10972 tp->nvram_size = (protect ?
10973 TG3_NVRAM_SIZE_128KB :
10974 TG3_NVRAM_SIZE_512KB);
10975 break;
Michael Chand3c7b882006-03-23 01:28:25 -080010976 }
10977}
10978
Michael Chan1b277772006-03-20 22:27:48 -080010979static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
10980{
10981 u32 nvcfg1;
10982
10983 nvcfg1 = tr32(NVRAM_CFG1);
10984
10985 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000010986 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
10987 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
10988 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
10989 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
10990 tp->nvram_jedecnum = JEDEC_ATMEL;
10991 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10992 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080010993
Matt Carlson8590a602009-08-28 12:29:16 +000010994 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
10995 tw32(NVRAM_CFG1, nvcfg1);
10996 break;
10997 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
10998 case FLASH_5755VENDOR_ATMEL_FLASH_1:
10999 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11000 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11001 tp->nvram_jedecnum = JEDEC_ATMEL;
11002 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11003 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11004 tp->nvram_pagesize = 264;
11005 break;
11006 case FLASH_5752VENDOR_ST_M45PE10:
11007 case FLASH_5752VENDOR_ST_M45PE20:
11008 case FLASH_5752VENDOR_ST_M45PE40:
11009 tp->nvram_jedecnum = JEDEC_ST;
11010 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11011 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11012 tp->nvram_pagesize = 256;
11013 break;
Michael Chan1b277772006-03-20 22:27:48 -080011014 }
11015}
11016
Matt Carlson6b91fa02007-10-10 18:01:09 -070011017static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
11018{
11019 u32 nvcfg1, protect = 0;
11020
11021 nvcfg1 = tr32(NVRAM_CFG1);
11022
11023 /* NVRAM protection for TPM */
11024 if (nvcfg1 & (1 << 27)) {
11025 tp->tg3_flags2 |= TG3_FLG2_PROTECTED_NVRAM;
11026 protect = 1;
11027 }
11028
11029 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11030 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011031 case FLASH_5761VENDOR_ATMEL_ADB021D:
11032 case FLASH_5761VENDOR_ATMEL_ADB041D:
11033 case FLASH_5761VENDOR_ATMEL_ADB081D:
11034 case FLASH_5761VENDOR_ATMEL_ADB161D:
11035 case FLASH_5761VENDOR_ATMEL_MDB021D:
11036 case FLASH_5761VENDOR_ATMEL_MDB041D:
11037 case FLASH_5761VENDOR_ATMEL_MDB081D:
11038 case FLASH_5761VENDOR_ATMEL_MDB161D:
11039 tp->nvram_jedecnum = JEDEC_ATMEL;
11040 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11041 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11042 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
11043 tp->nvram_pagesize = 256;
11044 break;
11045 case FLASH_5761VENDOR_ST_A_M45PE20:
11046 case FLASH_5761VENDOR_ST_A_M45PE40:
11047 case FLASH_5761VENDOR_ST_A_M45PE80:
11048 case FLASH_5761VENDOR_ST_A_M45PE16:
11049 case FLASH_5761VENDOR_ST_M_M45PE20:
11050 case FLASH_5761VENDOR_ST_M_M45PE40:
11051 case FLASH_5761VENDOR_ST_M_M45PE80:
11052 case FLASH_5761VENDOR_ST_M_M45PE16:
11053 tp->nvram_jedecnum = JEDEC_ST;
11054 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11055 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11056 tp->nvram_pagesize = 256;
11057 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011058 }
11059
11060 if (protect) {
11061 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
11062 } else {
11063 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011064 case FLASH_5761VENDOR_ATMEL_ADB161D:
11065 case FLASH_5761VENDOR_ATMEL_MDB161D:
11066 case FLASH_5761VENDOR_ST_A_M45PE16:
11067 case FLASH_5761VENDOR_ST_M_M45PE16:
11068 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
11069 break;
11070 case FLASH_5761VENDOR_ATMEL_ADB081D:
11071 case FLASH_5761VENDOR_ATMEL_MDB081D:
11072 case FLASH_5761VENDOR_ST_A_M45PE80:
11073 case FLASH_5761VENDOR_ST_M_M45PE80:
11074 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
11075 break;
11076 case FLASH_5761VENDOR_ATMEL_ADB041D:
11077 case FLASH_5761VENDOR_ATMEL_MDB041D:
11078 case FLASH_5761VENDOR_ST_A_M45PE40:
11079 case FLASH_5761VENDOR_ST_M_M45PE40:
11080 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11081 break;
11082 case FLASH_5761VENDOR_ATMEL_ADB021D:
11083 case FLASH_5761VENDOR_ATMEL_MDB021D:
11084 case FLASH_5761VENDOR_ST_A_M45PE20:
11085 case FLASH_5761VENDOR_ST_M_M45PE20:
11086 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11087 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011088 }
11089 }
11090}
11091
Michael Chanb5d37722006-09-27 16:06:21 -070011092static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
11093{
11094 tp->nvram_jedecnum = JEDEC_ATMEL;
11095 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11096 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11097}
11098
Matt Carlson321d32a2008-11-21 17:22:19 -080011099static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
11100{
11101 u32 nvcfg1;
11102
11103 nvcfg1 = tr32(NVRAM_CFG1);
11104
11105 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11106 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11107 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11108 tp->nvram_jedecnum = JEDEC_ATMEL;
11109 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11110 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11111
11112 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11113 tw32(NVRAM_CFG1, nvcfg1);
11114 return;
11115 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11116 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11117 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11118 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11119 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11120 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11121 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
11122 tp->nvram_jedecnum = JEDEC_ATMEL;
11123 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11124 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11125
11126 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11127 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11128 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11129 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11130 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11131 break;
11132 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11133 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11134 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11135 break;
11136 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11137 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
11138 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11139 break;
11140 }
11141 break;
11142 case FLASH_5752VENDOR_ST_M45PE10:
11143 case FLASH_5752VENDOR_ST_M45PE20:
11144 case FLASH_5752VENDOR_ST_M45PE40:
11145 tp->nvram_jedecnum = JEDEC_ST;
11146 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11147 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11148
11149 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11150 case FLASH_5752VENDOR_ST_M45PE10:
11151 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11152 break;
11153 case FLASH_5752VENDOR_ST_M45PE20:
11154 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11155 break;
11156 case FLASH_5752VENDOR_ST_M45PE40:
11157 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11158 break;
11159 }
11160 break;
11161 default:
Matt Carlsondf259d82009-04-20 06:57:14 +000011162 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
Matt Carlson321d32a2008-11-21 17:22:19 -080011163 return;
11164 }
11165
Matt Carlsona1b950d2009-09-01 13:20:17 +000011166 tg3_nvram_get_pagesize(tp, nvcfg1);
11167 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Matt Carlson321d32a2008-11-21 17:22:19 -080011168 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011169}
11170
11171
11172static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
11173{
11174 u32 nvcfg1;
11175
11176 nvcfg1 = tr32(NVRAM_CFG1);
11177
11178 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11179 case FLASH_5717VENDOR_ATMEL_EEPROM:
11180 case FLASH_5717VENDOR_MICRO_EEPROM:
11181 tp->nvram_jedecnum = JEDEC_ATMEL;
11182 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11183 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11184
11185 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11186 tw32(NVRAM_CFG1, nvcfg1);
11187 return;
11188 case FLASH_5717VENDOR_ATMEL_MDB011D:
11189 case FLASH_5717VENDOR_ATMEL_ADB011B:
11190 case FLASH_5717VENDOR_ATMEL_ADB011D:
11191 case FLASH_5717VENDOR_ATMEL_MDB021D:
11192 case FLASH_5717VENDOR_ATMEL_ADB021B:
11193 case FLASH_5717VENDOR_ATMEL_ADB021D:
11194 case FLASH_5717VENDOR_ATMEL_45USPT:
11195 tp->nvram_jedecnum = JEDEC_ATMEL;
11196 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11197 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11198
11199 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11200 case FLASH_5717VENDOR_ATMEL_MDB021D:
11201 case FLASH_5717VENDOR_ATMEL_ADB021B:
11202 case FLASH_5717VENDOR_ATMEL_ADB021D:
11203 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11204 break;
11205 default:
11206 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11207 break;
11208 }
Matt Carlson321d32a2008-11-21 17:22:19 -080011209 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011210 case FLASH_5717VENDOR_ST_M_M25PE10:
11211 case FLASH_5717VENDOR_ST_A_M25PE10:
11212 case FLASH_5717VENDOR_ST_M_M45PE10:
11213 case FLASH_5717VENDOR_ST_A_M45PE10:
11214 case FLASH_5717VENDOR_ST_M_M25PE20:
11215 case FLASH_5717VENDOR_ST_A_M25PE20:
11216 case FLASH_5717VENDOR_ST_M_M45PE20:
11217 case FLASH_5717VENDOR_ST_A_M45PE20:
11218 case FLASH_5717VENDOR_ST_25USPT:
11219 case FLASH_5717VENDOR_ST_45USPT:
11220 tp->nvram_jedecnum = JEDEC_ST;
11221 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11222 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11223
11224 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11225 case FLASH_5717VENDOR_ST_M_M25PE20:
11226 case FLASH_5717VENDOR_ST_A_M25PE20:
11227 case FLASH_5717VENDOR_ST_M_M45PE20:
11228 case FLASH_5717VENDOR_ST_A_M45PE20:
11229 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11230 break;
11231 default:
11232 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11233 break;
11234 }
Matt Carlson321d32a2008-11-21 17:22:19 -080011235 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011236 default:
11237 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
11238 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080011239 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000011240
11241 tg3_nvram_get_pagesize(tp, nvcfg1);
11242 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
11243 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
Matt Carlson321d32a2008-11-21 17:22:19 -080011244}
11245
Linus Torvalds1da177e2005-04-16 15:20:36 -070011246/* Chips other than 5700/5701 use the NVRAM for fetching info. */
11247static void __devinit tg3_nvram_init(struct tg3 *tp)
11248{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011249 tw32_f(GRC_EEPROM_ADDR,
11250 (EEPROM_ADDR_FSM_RESET |
11251 (EEPROM_DEFAULT_CLOCK_PERIOD <<
11252 EEPROM_ADDR_CLKPERD_SHIFT)));
11253
Michael Chan9d57f012006-12-07 00:23:25 -080011254 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011255
11256 /* Enable seeprom accesses. */
11257 tw32_f(GRC_LOCAL_CTRL,
11258 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
11259 udelay(100);
11260
11261 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
11262 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
11263 tp->tg3_flags |= TG3_FLAG_NVRAM;
11264
Michael Chanec41c7d2006-01-17 02:40:55 -080011265 if (tg3_nvram_lock(tp)) {
11266 printk(KERN_WARNING PFX "%s: Cannot get nvarm lock, "
11267 "tg3_nvram_init failed.\n", tp->dev->name);
11268 return;
11269 }
Michael Chane6af3012005-04-21 17:12:05 -070011270 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011271
Matt Carlson989a9d22007-05-05 11:51:05 -070011272 tp->nvram_size = 0;
11273
Michael Chan361b4ac2005-04-21 17:11:21 -070011274 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
11275 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080011276 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
11277 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070011278 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070011279 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
11280 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080011281 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070011282 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
11283 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070011284 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
11285 tg3_get_5906_nvram_info(tp);
Matt Carlson321d32a2008-11-21 17:22:19 -080011286 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
11287 tg3_get_57780_nvram_info(tp);
Matt Carlsona1b950d2009-09-01 13:20:17 +000011288 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
11289 tg3_get_5717_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070011290 else
11291 tg3_get_nvram_info(tp);
11292
Matt Carlson989a9d22007-05-05 11:51:05 -070011293 if (tp->nvram_size == 0)
11294 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011295
Michael Chane6af3012005-04-21 17:12:05 -070011296 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080011297 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011298
11299 } else {
11300 tp->tg3_flags &= ~(TG3_FLAG_NVRAM | TG3_FLAG_NVRAM_BUFFERED);
11301
11302 tg3_get_eeprom_size(tp);
11303 }
11304}
11305
Linus Torvalds1da177e2005-04-16 15:20:36 -070011306static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
11307 u32 offset, u32 len, u8 *buf)
11308{
11309 int i, j, rc = 0;
11310 u32 val;
11311
11312 for (i = 0; i < len; i += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080011313 u32 addr;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011314 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011315
11316 addr = offset + i;
11317
11318 memcpy(&data, buf + i, 4);
11319
Matt Carlson62cedd12009-04-20 14:52:29 -070011320 /*
11321 * The SEEPROM interface expects the data to always be opposite
11322 * the native endian format. We accomplish this by reversing
11323 * all the operations that would have been performed on the
11324 * data from a call to tg3_nvram_read_be32().
11325 */
11326 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011327
11328 val = tr32(GRC_EEPROM_ADDR);
11329 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
11330
11331 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
11332 EEPROM_ADDR_READ);
11333 tw32(GRC_EEPROM_ADDR, val |
11334 (0 << EEPROM_ADDR_DEVID_SHIFT) |
11335 (addr & EEPROM_ADDR_ADDR_MASK) |
11336 EEPROM_ADDR_START |
11337 EEPROM_ADDR_WRITE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011338
Michael Chan9d57f012006-12-07 00:23:25 -080011339 for (j = 0; j < 1000; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011340 val = tr32(GRC_EEPROM_ADDR);
11341
11342 if (val & EEPROM_ADDR_COMPLETE)
11343 break;
Michael Chan9d57f012006-12-07 00:23:25 -080011344 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011345 }
11346 if (!(val & EEPROM_ADDR_COMPLETE)) {
11347 rc = -EBUSY;
11348 break;
11349 }
11350 }
11351
11352 return rc;
11353}
11354
11355/* offset and length are dword aligned */
11356static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
11357 u8 *buf)
11358{
11359 int ret = 0;
11360 u32 pagesize = tp->nvram_pagesize;
11361 u32 pagemask = pagesize - 1;
11362 u32 nvram_cmd;
11363 u8 *tmp;
11364
11365 tmp = kmalloc(pagesize, GFP_KERNEL);
11366 if (tmp == NULL)
11367 return -ENOMEM;
11368
11369 while (len) {
11370 int j;
Michael Chane6af3012005-04-21 17:12:05 -070011371 u32 phy_addr, page_off, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011372
11373 phy_addr = offset & ~pagemask;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011374
Linus Torvalds1da177e2005-04-16 15:20:36 -070011375 for (j = 0; j < pagesize; j += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000011376 ret = tg3_nvram_read_be32(tp, phy_addr + j,
11377 (__be32 *) (tmp + j));
11378 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011379 break;
11380 }
11381 if (ret)
11382 break;
11383
11384 page_off = offset & pagemask;
11385 size = pagesize;
11386 if (len < size)
11387 size = len;
11388
11389 len -= size;
11390
11391 memcpy(tmp + page_off, buf, size);
11392
11393 offset = offset + (pagesize - page_off);
11394
Michael Chane6af3012005-04-21 17:12:05 -070011395 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011396
11397 /*
11398 * Before we can erase the flash page, we need
11399 * to issue a special "write enable" command.
11400 */
11401 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
11402
11403 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
11404 break;
11405
11406 /* Erase the target page */
11407 tw32(NVRAM_ADDR, phy_addr);
11408
11409 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
11410 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
11411
11412 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
11413 break;
11414
11415 /* Issue another write enable to start the write. */
11416 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
11417
11418 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
11419 break;
11420
11421 for (j = 0; j < pagesize; j += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080011422 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011423
Al Virob9fc7dc2007-12-17 22:59:57 -080011424 data = *((__be32 *) (tmp + j));
Matt Carlsona9dc5292009-02-25 14:25:30 +000011425
Al Virob9fc7dc2007-12-17 22:59:57 -080011426 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011427
11428 tw32(NVRAM_ADDR, phy_addr + j);
11429
11430 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
11431 NVRAM_CMD_WR;
11432
11433 if (j == 0)
11434 nvram_cmd |= NVRAM_CMD_FIRST;
11435 else if (j == (pagesize - 4))
11436 nvram_cmd |= NVRAM_CMD_LAST;
11437
11438 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
11439 break;
11440 }
11441 if (ret)
11442 break;
11443 }
11444
11445 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
11446 tg3_nvram_exec_cmd(tp, nvram_cmd);
11447
11448 kfree(tmp);
11449
11450 return ret;
11451}
11452
11453/* offset and length are dword aligned */
11454static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
11455 u8 *buf)
11456{
11457 int i, ret = 0;
11458
11459 for (i = 0; i < len; i += 4, offset += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080011460 u32 page_off, phy_addr, nvram_cmd;
11461 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011462
11463 memcpy(&data, buf + i, 4);
Al Virob9fc7dc2007-12-17 22:59:57 -080011464 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011465
11466 page_off = offset % tp->nvram_pagesize;
11467
Michael Chan18201802006-03-20 22:29:15 -080011468 phy_addr = tg3_nvram_phys_addr(tp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011469
11470 tw32(NVRAM_ADDR, phy_addr);
11471
11472 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
11473
11474 if ((page_off == 0) || (i == 0))
11475 nvram_cmd |= NVRAM_CMD_FIRST;
Michael Chanf6d9a252006-04-29 19:00:24 -070011476 if (page_off == (tp->nvram_pagesize - 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011477 nvram_cmd |= NVRAM_CMD_LAST;
11478
11479 if (i == (len - 4))
11480 nvram_cmd |= NVRAM_CMD_LAST;
11481
Matt Carlson321d32a2008-11-21 17:22:19 -080011482 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
11483 !(tp->tg3_flags3 & TG3_FLG3_5755_PLUS) &&
Michael Chan4c987482005-09-05 17:52:38 -070011484 (tp->nvram_jedecnum == JEDEC_ST) &&
11485 (nvram_cmd & NVRAM_CMD_FIRST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011486
11487 if ((ret = tg3_nvram_exec_cmd(tp,
11488 NVRAM_CMD_WREN | NVRAM_CMD_GO |
11489 NVRAM_CMD_DONE)))
11490
11491 break;
11492 }
11493 if (!(tp->tg3_flags2 & TG3_FLG2_FLASH)) {
11494 /* We always do complete word writes to eeprom. */
11495 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
11496 }
11497
11498 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
11499 break;
11500 }
11501 return ret;
11502}
11503
11504/* offset and length are dword aligned */
11505static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
11506{
11507 int ret;
11508
Linus Torvalds1da177e2005-04-16 15:20:36 -070011509 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT) {
Michael Chan314fba32005-04-21 17:07:04 -070011510 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
11511 ~GRC_LCLCTRL_GPIO_OUTPUT1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011512 udelay(40);
11513 }
11514
11515 if (!(tp->tg3_flags & TG3_FLAG_NVRAM)) {
11516 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
11517 }
11518 else {
11519 u32 grc_mode;
11520
Michael Chanec41c7d2006-01-17 02:40:55 -080011521 ret = tg3_nvram_lock(tp);
11522 if (ret)
11523 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011524
Michael Chane6af3012005-04-21 17:12:05 -070011525 tg3_enable_nvram_access(tp);
11526 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
11527 !(tp->tg3_flags2 & TG3_FLG2_PROTECTED_NVRAM))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011528 tw32(NVRAM_WRITE1, 0x406);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011529
11530 grc_mode = tr32(GRC_MODE);
11531 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
11532
11533 if ((tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) ||
11534 !(tp->tg3_flags2 & TG3_FLG2_FLASH)) {
11535
11536 ret = tg3_nvram_write_block_buffered(tp, offset, len,
11537 buf);
11538 }
11539 else {
11540 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
11541 buf);
11542 }
11543
11544 grc_mode = tr32(GRC_MODE);
11545 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
11546
Michael Chane6af3012005-04-21 17:12:05 -070011547 tg3_disable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011548 tg3_nvram_unlock(tp);
11549 }
11550
11551 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT) {
Michael Chan314fba32005-04-21 17:07:04 -070011552 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011553 udelay(40);
11554 }
11555
11556 return ret;
11557}
11558
11559struct subsys_tbl_ent {
11560 u16 subsys_vendor, subsys_devid;
11561 u32 phy_id;
11562};
11563
11564static struct subsys_tbl_ent subsys_id_to_phy_id[] = {
11565 /* Broadcom boards. */
11566 { PCI_VENDOR_ID_BROADCOM, 0x1644, PHY_ID_BCM5401 }, /* BCM95700A6 */
11567 { PCI_VENDOR_ID_BROADCOM, 0x0001, PHY_ID_BCM5701 }, /* BCM95701A5 */
11568 { PCI_VENDOR_ID_BROADCOM, 0x0002, PHY_ID_BCM8002 }, /* BCM95700T6 */
11569 { PCI_VENDOR_ID_BROADCOM, 0x0003, 0 }, /* BCM95700A9 */
11570 { PCI_VENDOR_ID_BROADCOM, 0x0005, PHY_ID_BCM5701 }, /* BCM95701T1 */
11571 { PCI_VENDOR_ID_BROADCOM, 0x0006, PHY_ID_BCM5701 }, /* BCM95701T8 */
11572 { PCI_VENDOR_ID_BROADCOM, 0x0007, 0 }, /* BCM95701A7 */
11573 { PCI_VENDOR_ID_BROADCOM, 0x0008, PHY_ID_BCM5701 }, /* BCM95701A10 */
11574 { PCI_VENDOR_ID_BROADCOM, 0x8008, PHY_ID_BCM5701 }, /* BCM95701A12 */
11575 { PCI_VENDOR_ID_BROADCOM, 0x0009, PHY_ID_BCM5703 }, /* BCM95703Ax1 */
11576 { PCI_VENDOR_ID_BROADCOM, 0x8009, PHY_ID_BCM5703 }, /* BCM95703Ax2 */
11577
11578 /* 3com boards. */
11579 { PCI_VENDOR_ID_3COM, 0x1000, PHY_ID_BCM5401 }, /* 3C996T */
11580 { PCI_VENDOR_ID_3COM, 0x1006, PHY_ID_BCM5701 }, /* 3C996BT */
11581 { PCI_VENDOR_ID_3COM, 0x1004, 0 }, /* 3C996SX */
11582 { PCI_VENDOR_ID_3COM, 0x1007, PHY_ID_BCM5701 }, /* 3C1000T */
11583 { PCI_VENDOR_ID_3COM, 0x1008, PHY_ID_BCM5701 }, /* 3C940BR01 */
11584
11585 /* DELL boards. */
11586 { PCI_VENDOR_ID_DELL, 0x00d1, PHY_ID_BCM5401 }, /* VIPER */
11587 { PCI_VENDOR_ID_DELL, 0x0106, PHY_ID_BCM5401 }, /* JAGUAR */
11588 { PCI_VENDOR_ID_DELL, 0x0109, PHY_ID_BCM5411 }, /* MERLOT */
11589 { PCI_VENDOR_ID_DELL, 0x010a, PHY_ID_BCM5411 }, /* SLIM_MERLOT */
11590
11591 /* Compaq boards. */
11592 { PCI_VENDOR_ID_COMPAQ, 0x007c, PHY_ID_BCM5701 }, /* BANSHEE */
11593 { PCI_VENDOR_ID_COMPAQ, 0x009a, PHY_ID_BCM5701 }, /* BANSHEE_2 */
11594 { PCI_VENDOR_ID_COMPAQ, 0x007d, 0 }, /* CHANGELING */
11595 { PCI_VENDOR_ID_COMPAQ, 0x0085, PHY_ID_BCM5701 }, /* NC7780 */
11596 { PCI_VENDOR_ID_COMPAQ, 0x0099, PHY_ID_BCM5701 }, /* NC7780_2 */
11597
11598 /* IBM boards. */
11599 { PCI_VENDOR_ID_IBM, 0x0281, 0 } /* IBM??? */
11600};
11601
11602static inline struct subsys_tbl_ent *lookup_by_subsys(struct tg3 *tp)
11603{
11604 int i;
11605
11606 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
11607 if ((subsys_id_to_phy_id[i].subsys_vendor ==
11608 tp->pdev->subsystem_vendor) &&
11609 (subsys_id_to_phy_id[i].subsys_devid ==
11610 tp->pdev->subsystem_device))
11611 return &subsys_id_to_phy_id[i];
11612 }
11613 return NULL;
11614}
11615
Michael Chan7d0c41e2005-04-21 17:06:20 -070011616static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011617{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011618 u32 val;
Michael Chancaf636c72006-03-22 01:05:31 -080011619 u16 pmcsr;
11620
11621 /* On some early chips the SRAM cannot be accessed in D3hot state,
11622 * so need make sure we're in D0.
11623 */
11624 pci_read_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, &pmcsr);
11625 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
11626 pci_write_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, pmcsr);
11627 msleep(1);
Michael Chan7d0c41e2005-04-21 17:06:20 -070011628
11629 /* Make sure register accesses (indirect or otherwise)
11630 * will function correctly.
11631 */
11632 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
11633 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011634
David S. Millerf49639e2006-06-09 11:58:36 -070011635 /* The memory arbiter has to be enabled in order for SRAM accesses
11636 * to succeed. Normally on powerup the tg3 chip firmware will make
11637 * sure it is enabled, but other entities such as system netboot
11638 * code might disable it.
11639 */
11640 val = tr32(MEMARB_MODE);
11641 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
11642
Linus Torvalds1da177e2005-04-16 15:20:36 -070011643 tp->phy_id = PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070011644 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
11645
Gary Zambranoa85feb82007-05-05 11:52:19 -070011646 /* Assume an onboard device and WOL capable by default. */
11647 tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT | TG3_FLAG_WOL_CAP;
David S. Miller72b845e2006-03-14 14:11:48 -080011648
Michael Chanb5d37722006-09-27 16:06:21 -070011649 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080011650 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Michael Chanb5d37722006-09-27 16:06:21 -070011651 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080011652 tp->tg3_flags2 |= TG3_FLG2_IS_NIC;
11653 }
Matt Carlson0527ba32007-10-10 18:03:30 -070011654 val = tr32(VCPU_CFGSHDW);
11655 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Matt Carlson8ed5d972007-05-07 00:25:49 -070011656 tp->tg3_flags |= TG3_FLAG_ASPM_WORKAROUND;
Matt Carlson0527ba32007-10-10 18:03:30 -070011657 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Matt Carlson20232762008-12-21 20:18:56 -080011658 (val & VCPU_CFGSHDW_WOL_MAGPKT))
Matt Carlson0527ba32007-10-10 18:03:30 -070011659 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
Matt Carlson05ac4cb2008-11-03 16:53:46 -080011660 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070011661 }
11662
Linus Torvalds1da177e2005-04-16 15:20:36 -070011663 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
11664 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
11665 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070011666 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070011667 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011668
11669 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
11670 tp->nic_sram_data_cfg = nic_cfg;
11671
11672 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
11673 ver >>= NIC_SRAM_DATA_VER_SHIFT;
11674 if ((GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700) &&
11675 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) &&
11676 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703) &&
11677 (ver > 0) && (ver < 0x100))
11678 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
11679
Matt Carlsona9daf362008-05-25 23:49:44 -070011680 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
11681 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
11682
Linus Torvalds1da177e2005-04-16 15:20:36 -070011683 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
11684 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
11685 eeprom_phy_serdes = 1;
11686
11687 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
11688 if (nic_phy_id != 0) {
11689 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
11690 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
11691
11692 eeprom_phy_id = (id1 >> 16) << 10;
11693 eeprom_phy_id |= (id2 & 0xfc00) << 16;
11694 eeprom_phy_id |= (id2 & 0x03ff) << 0;
11695 } else
11696 eeprom_phy_id = 0;
11697
Michael Chan7d0c41e2005-04-21 17:06:20 -070011698 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070011699 if (eeprom_phy_serdes) {
Michael Chana4e2b342005-10-26 15:46:52 -070011700 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
Michael Chan747e8f82005-07-25 12:33:22 -070011701 tp->tg3_flags2 |= TG3_FLG2_MII_SERDES;
11702 else
11703 tp->tg3_flags2 |= TG3_FLG2_PHY_SERDES;
11704 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070011705
John W. Linvillecbf46852005-04-21 17:01:29 -070011706 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011707 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
11708 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070011709 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070011710 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
11711
11712 switch (led_cfg) {
11713 default:
11714 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
11715 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
11716 break;
11717
11718 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
11719 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
11720 break;
11721
11722 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
11723 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070011724
11725 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
11726 * read on some older 5700/5701 bootcode.
11727 */
11728 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
11729 ASIC_REV_5700 ||
11730 GET_ASIC_REV(tp->pci_chip_rev_id) ==
11731 ASIC_REV_5701)
11732 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
11733
Linus Torvalds1da177e2005-04-16 15:20:36 -070011734 break;
11735
11736 case SHASTA_EXT_LED_SHARED:
11737 tp->led_ctrl = LED_CTRL_MODE_SHARED;
11738 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
11739 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
11740 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
11741 LED_CTRL_MODE_PHY_2);
11742 break;
11743
11744 case SHASTA_EXT_LED_MAC:
11745 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
11746 break;
11747
11748 case SHASTA_EXT_LED_COMBO:
11749 tp->led_ctrl = LED_CTRL_MODE_COMBO;
11750 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
11751 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
11752 LED_CTRL_MODE_PHY_2);
11753 break;
11754
Stephen Hemminger855e1112008-04-16 16:37:28 -070011755 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011756
11757 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
11758 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
11759 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
11760 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
11761
Matt Carlsonb2a5c192008-04-03 21:44:44 -070011762 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
11763 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080011764
Michael Chan9d26e212006-12-07 00:21:14 -080011765 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011766 tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080011767 if ((tp->pdev->subsystem_vendor ==
11768 PCI_VENDOR_ID_ARIMA) &&
11769 (tp->pdev->subsystem_device == 0x205a ||
11770 tp->pdev->subsystem_device == 0x2063))
11771 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
11772 } else {
David S. Millerf49639e2006-06-09 11:58:36 -070011773 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080011774 tp->tg3_flags2 |= TG3_FLG2_IS_NIC;
11775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011776
11777 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
11778 tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
John W. Linvillecbf46852005-04-21 17:01:29 -070011779 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011780 tp->tg3_flags2 |= TG3_FLG2_ASF_NEW_HANDSHAKE;
11781 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080011782
11783 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
11784 (tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
Matt Carlson0d3031d2007-10-10 18:02:43 -070011785 tp->tg3_flags3 |= TG3_FLG3_ENABLE_APE;
Matt Carlsonb2b98d42008-11-03 16:52:32 -080011786
Gary Zambranoa85feb82007-05-05 11:52:19 -070011787 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES &&
11788 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
11789 tp->tg3_flags &= ~TG3_FLAG_WOL_CAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011790
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070011791 if ((tp->tg3_flags & TG3_FLAG_WOL_CAP) &&
Matt Carlson05ac4cb2008-11-03 16:53:46 -080011792 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE))
Matt Carlson0527ba32007-10-10 18:03:30 -070011793 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
11794
Linus Torvalds1da177e2005-04-16 15:20:36 -070011795 if (cfg2 & (1 << 17))
11796 tp->tg3_flags2 |= TG3_FLG2_CAPACITIVE_COUPLING;
11797
11798 /* serdes signal pre-emphasis in register 0x590 set by */
11799 /* bootcode if bit 18 is set */
11800 if (cfg2 & (1 << 18))
11801 tp->tg3_flags2 |= TG3_FLG2_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070011802
Matt Carlson321d32a2008-11-21 17:22:19 -080011803 if (((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
11804 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080011805 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
11806 tp->tg3_flags3 |= TG3_FLG3_PHY_ENABLE_APD;
11807
Matt Carlson8ed5d972007-05-07 00:25:49 -070011808 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
11809 u32 cfg3;
11810
11811 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
11812 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
11813 tp->tg3_flags |= TG3_FLAG_ASPM_WORKAROUND;
11814 }
Matt Carlsona9daf362008-05-25 23:49:44 -070011815
11816 if (cfg4 & NIC_SRAM_RGMII_STD_IBND_DISABLE)
11817 tp->tg3_flags3 |= TG3_FLG3_RGMII_STD_IBND_DISABLE;
11818 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
11819 tp->tg3_flags3 |= TG3_FLG3_RGMII_EXT_IBND_RX_EN;
11820 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
11821 tp->tg3_flags3 |= TG3_FLG3_RGMII_EXT_IBND_TX_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011822 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080011823done:
11824 device_init_wakeup(&tp->pdev->dev, tp->tg3_flags & TG3_FLAG_WOL_CAP);
11825 device_set_wakeup_enable(&tp->pdev->dev,
11826 tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
Michael Chan7d0c41e2005-04-21 17:06:20 -070011827}
11828
Matt Carlsonb2a5c192008-04-03 21:44:44 -070011829static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
11830{
11831 int i;
11832 u32 val;
11833
11834 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
11835 tw32(OTP_CTRL, cmd);
11836
11837 /* Wait for up to 1 ms for command to execute. */
11838 for (i = 0; i < 100; i++) {
11839 val = tr32(OTP_STATUS);
11840 if (val & OTP_STATUS_CMD_DONE)
11841 break;
11842 udelay(10);
11843 }
11844
11845 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
11846}
11847
11848/* Read the gphy configuration from the OTP region of the chip. The gphy
11849 * configuration is a 32-bit value that straddles the alignment boundary.
11850 * We do two 32-bit reads and then shift and merge the results.
11851 */
11852static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
11853{
11854 u32 bhalf_otp, thalf_otp;
11855
11856 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
11857
11858 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
11859 return 0;
11860
11861 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
11862
11863 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
11864 return 0;
11865
11866 thalf_otp = tr32(OTP_READ_DATA);
11867
11868 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
11869
11870 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
11871 return 0;
11872
11873 bhalf_otp = tr32(OTP_READ_DATA);
11874
11875 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
11876}
11877
Michael Chan7d0c41e2005-04-21 17:06:20 -070011878static int __devinit tg3_phy_probe(struct tg3 *tp)
11879{
11880 u32 hw_phy_id_1, hw_phy_id_2;
11881 u32 hw_phy_id, hw_phy_id_masked;
11882 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011883
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011884 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)
11885 return tg3_phy_init(tp);
11886
Linus Torvalds1da177e2005-04-16 15:20:36 -070011887 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010011888 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011889 */
11890 err = 0;
Matt Carlson0d3031d2007-10-10 18:02:43 -070011891 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
11892 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011893 hw_phy_id = hw_phy_id_masked = PHY_ID_INVALID;
11894 } else {
11895 /* Now read the physical PHY_ID from the chip and verify
11896 * that it is sane. If it doesn't look good, we fall back
11897 * to either the hard-coded table based PHY_ID and failing
11898 * that the value found in the eeprom area.
11899 */
11900 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
11901 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
11902
11903 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
11904 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
11905 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
11906
11907 hw_phy_id_masked = hw_phy_id & PHY_ID_MASK;
11908 }
11909
11910 if (!err && KNOWN_PHY_ID(hw_phy_id_masked)) {
11911 tp->phy_id = hw_phy_id;
11912 if (hw_phy_id_masked == PHY_ID_BCM8002)
11913 tp->tg3_flags2 |= TG3_FLG2_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070011914 else
11915 tp->tg3_flags2 &= ~TG3_FLG2_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011916 } else {
Michael Chan7d0c41e2005-04-21 17:06:20 -070011917 if (tp->phy_id != PHY_ID_INVALID) {
11918 /* Do nothing, phy ID already set up in
11919 * tg3_get_eeprom_hw_cfg().
11920 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011921 } else {
11922 struct subsys_tbl_ent *p;
11923
11924 /* No eeprom signature? Try the hardcoded
11925 * subsys device table.
11926 */
11927 p = lookup_by_subsys(tp);
11928 if (!p)
11929 return -ENODEV;
11930
11931 tp->phy_id = p->phy_id;
11932 if (!tp->phy_id ||
11933 tp->phy_id == PHY_ID_BCM8002)
11934 tp->tg3_flags2 |= TG3_FLG2_PHY_SERDES;
11935 }
11936 }
11937
Michael Chan747e8f82005-07-25 12:33:22 -070011938 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES) &&
Matt Carlson0d3031d2007-10-10 18:02:43 -070011939 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070011940 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chan3600d912006-12-07 00:21:48 -080011941 u32 bmsr, adv_reg, tg3_ctrl, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011942
11943 tg3_readphy(tp, MII_BMSR, &bmsr);
11944 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
11945 (bmsr & BMSR_LSTATUS))
11946 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011947
Linus Torvalds1da177e2005-04-16 15:20:36 -070011948 err = tg3_phy_reset(tp);
11949 if (err)
11950 return err;
11951
11952 adv_reg = (ADVERTISE_10HALF | ADVERTISE_10FULL |
11953 ADVERTISE_100HALF | ADVERTISE_100FULL |
11954 ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
11955 tg3_ctrl = 0;
11956 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY)) {
11957 tg3_ctrl = (MII_TG3_CTRL_ADV_1000_HALF |
11958 MII_TG3_CTRL_ADV_1000_FULL);
11959 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
11960 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
11961 tg3_ctrl |= (MII_TG3_CTRL_AS_MASTER |
11962 MII_TG3_CTRL_ENABLE_AS_MASTER);
11963 }
11964
Michael Chan3600d912006-12-07 00:21:48 -080011965 mask = (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
11966 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
11967 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full);
11968 if (!tg3_copper_is_advertising_all(tp, mask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011969 tg3_writephy(tp, MII_ADVERTISE, adv_reg);
11970
11971 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY))
11972 tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
11973
11974 tg3_writephy(tp, MII_BMCR,
11975 BMCR_ANENABLE | BMCR_ANRESTART);
11976 }
11977 tg3_phy_set_wirespeed(tp);
11978
11979 tg3_writephy(tp, MII_ADVERTISE, adv_reg);
11980 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY))
11981 tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
11982 }
11983
11984skip_phy_reset:
11985 if ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5401) {
11986 err = tg3_init_5401phy_dsp(tp);
11987 if (err)
11988 return err;
11989 }
11990
11991 if (!err && ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5401)) {
11992 err = tg3_init_5401phy_dsp(tp);
11993 }
11994
Michael Chan747e8f82005-07-25 12:33:22 -070011995 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011996 tp->link_config.advertising =
11997 (ADVERTISED_1000baseT_Half |
11998 ADVERTISED_1000baseT_Full |
11999 ADVERTISED_Autoneg |
12000 ADVERTISED_FIBRE);
12001 if (tp->tg3_flags & TG3_FLAG_10_100_ONLY)
12002 tp->link_config.advertising &=
12003 ~(ADVERTISED_1000baseT_Half |
12004 ADVERTISED_1000baseT_Full);
12005
12006 return err;
12007}
12008
12009static void __devinit tg3_read_partno(struct tg3 *tp)
12010{
Matt Carlson6d348f22009-02-25 14:25:52 +000012011 unsigned char vpd_data[256]; /* in little-endian format */
Michael Chanaf2c6a42006-11-07 14:57:51 -080012012 unsigned int i;
Michael Chan1b277772006-03-20 22:27:48 -080012013 u32 magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012014
Matt Carlsondf259d82009-04-20 06:57:14 +000012015 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
12016 tg3_nvram_read(tp, 0x0, &magic))
David S. Millerf49639e2006-06-09 11:58:36 -070012017 goto out_not_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012018
Michael Chan18201802006-03-20 22:29:15 -080012019 if (magic == TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080012020 for (i = 0; i < 256; i += 4) {
12021 u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012022
Matt Carlson6d348f22009-02-25 14:25:52 +000012023 /* The data is in little-endian format in NVRAM.
12024 * Use the big-endian read routines to preserve
12025 * the byte order as it exists in NVRAM.
12026 */
12027 if (tg3_nvram_read_be32(tp, 0x100 + i, &tmp))
Michael Chan1b277772006-03-20 22:27:48 -080012028 goto out_not_found;
12029
Matt Carlson6d348f22009-02-25 14:25:52 +000012030 memcpy(&vpd_data[i], &tmp, sizeof(tmp));
Michael Chan1b277772006-03-20 22:27:48 -080012031 }
12032 } else {
12033 int vpd_cap;
12034
12035 vpd_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_VPD);
12036 for (i = 0; i < 256; i += 4) {
12037 u32 tmp, j = 0;
Al Virob9fc7dc2007-12-17 22:59:57 -080012038 __le32 v;
Michael Chan1b277772006-03-20 22:27:48 -080012039 u16 tmp16;
12040
12041 pci_write_config_word(tp->pdev, vpd_cap + PCI_VPD_ADDR,
12042 i);
12043 while (j++ < 100) {
12044 pci_read_config_word(tp->pdev, vpd_cap +
12045 PCI_VPD_ADDR, &tmp16);
12046 if (tmp16 & 0x8000)
12047 break;
12048 msleep(1);
12049 }
David S. Millerf49639e2006-06-09 11:58:36 -070012050 if (!(tmp16 & 0x8000))
12051 goto out_not_found;
12052
Michael Chan1b277772006-03-20 22:27:48 -080012053 pci_read_config_dword(tp->pdev, vpd_cap + PCI_VPD_DATA,
12054 &tmp);
Al Virob9fc7dc2007-12-17 22:59:57 -080012055 v = cpu_to_le32(tmp);
Matt Carlson6d348f22009-02-25 14:25:52 +000012056 memcpy(&vpd_data[i], &v, sizeof(v));
Michael Chan1b277772006-03-20 22:27:48 -080012057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012058 }
12059
12060 /* Now parse and find the part number. */
Michael Chanaf2c6a42006-11-07 14:57:51 -080012061 for (i = 0; i < 254; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012062 unsigned char val = vpd_data[i];
Michael Chanaf2c6a42006-11-07 14:57:51 -080012063 unsigned int block_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012064
12065 if (val == 0x82 || val == 0x91) {
12066 i = (i + 3 +
12067 (vpd_data[i + 1] +
12068 (vpd_data[i + 2] << 8)));
12069 continue;
12070 }
12071
12072 if (val != 0x90)
12073 goto out_not_found;
12074
12075 block_end = (i + 3 +
12076 (vpd_data[i + 1] +
12077 (vpd_data[i + 2] << 8)));
12078 i += 3;
Michael Chanaf2c6a42006-11-07 14:57:51 -080012079
12080 if (block_end > 256)
12081 goto out_not_found;
12082
12083 while (i < (block_end - 2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012084 if (vpd_data[i + 0] == 'P' &&
12085 vpd_data[i + 1] == 'N') {
12086 int partno_len = vpd_data[i + 2];
12087
Michael Chanaf2c6a42006-11-07 14:57:51 -080012088 i += 3;
12089 if (partno_len > 24 || (partno_len + i) > 256)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012090 goto out_not_found;
12091
12092 memcpy(tp->board_part_number,
Michael Chanaf2c6a42006-11-07 14:57:51 -080012093 &vpd_data[i], partno_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012094
12095 /* Success. */
12096 return;
12097 }
Michael Chanaf2c6a42006-11-07 14:57:51 -080012098 i += 3 + vpd_data[i + 2];
Linus Torvalds1da177e2005-04-16 15:20:36 -070012099 }
12100
12101 /* Part number not found. */
12102 goto out_not_found;
12103 }
12104
12105out_not_found:
Michael Chanb5d37722006-09-27 16:06:21 -070012106 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12107 strcpy(tp->board_part_number, "BCM95906");
Matt Carlsondf259d82009-04-20 06:57:14 +000012108 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12109 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
12110 strcpy(tp->board_part_number, "BCM57780");
12111 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12112 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
12113 strcpy(tp->board_part_number, "BCM57760");
12114 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12115 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
12116 strcpy(tp->board_part_number, "BCM57790");
Matt Carlson5e7ccf22009-08-25 10:08:42 +000012117 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12118 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
12119 strcpy(tp->board_part_number, "BCM57788");
Michael Chanb5d37722006-09-27 16:06:21 -070012120 else
12121 strcpy(tp->board_part_number, "none");
Linus Torvalds1da177e2005-04-16 15:20:36 -070012122}
12123
Matt Carlson9c8a6202007-10-21 16:16:08 -070012124static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
12125{
12126 u32 val;
12127
Matt Carlsone4f34112009-02-25 14:25:00 +000012128 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070012129 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000012130 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070012131 val != 0)
12132 return 0;
12133
12134 return 1;
12135}
12136
Matt Carlsonacd9c112009-02-25 14:26:33 +000012137static void __devinit tg3_read_bc_ver(struct tg3 *tp)
12138{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012139 u32 val, offset, start, ver_offset;
Matt Carlsonacd9c112009-02-25 14:26:33 +000012140 int i;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012141 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000012142
12143 if (tg3_nvram_read(tp, 0xc, &offset) ||
12144 tg3_nvram_read(tp, 0x4, &start))
12145 return;
12146
12147 offset = tg3_nvram_logical_addr(tp, offset);
12148
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012149 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000012150 return;
12151
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012152 if ((val & 0xfc000000) == 0x0c000000) {
12153 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000012154 return;
12155
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012156 if (val == 0)
12157 newver = true;
12158 }
12159
12160 if (newver) {
12161 if (tg3_nvram_read(tp, offset + 8, &ver_offset))
12162 return;
12163
12164 offset = offset + ver_offset - start;
12165 for (i = 0; i < 16; i += 4) {
12166 __be32 v;
12167 if (tg3_nvram_read_be32(tp, offset + i, &v))
12168 return;
12169
12170 memcpy(tp->fw_ver + i, &v, sizeof(v));
12171 }
12172 } else {
12173 u32 major, minor;
12174
12175 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
12176 return;
12177
12178 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
12179 TG3_NVM_BCVER_MAJSFT;
12180 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
12181 snprintf(&tp->fw_ver[0], 32, "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000012182 }
12183}
12184
Matt Carlsona6f6cb12009-02-25 14:27:43 +000012185static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
12186{
12187 u32 val, major, minor;
12188
12189 /* Use native endian representation */
12190 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
12191 return;
12192
12193 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
12194 TG3_NVM_HWSB_CFG1_MAJSFT;
12195 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
12196 TG3_NVM_HWSB_CFG1_MINSFT;
12197
12198 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
12199}
12200
Matt Carlsondfe00d72008-11-21 17:19:41 -080012201static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
12202{
12203 u32 offset, major, minor, build;
12204
12205 tp->fw_ver[0] = 's';
12206 tp->fw_ver[1] = 'b';
12207 tp->fw_ver[2] = '\0';
12208
12209 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
12210 return;
12211
12212 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
12213 case TG3_EEPROM_SB_REVISION_0:
12214 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
12215 break;
12216 case TG3_EEPROM_SB_REVISION_2:
12217 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
12218 break;
12219 case TG3_EEPROM_SB_REVISION_3:
12220 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
12221 break;
12222 default:
12223 return;
12224 }
12225
Matt Carlsone4f34112009-02-25 14:25:00 +000012226 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080012227 return;
12228
12229 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
12230 TG3_EEPROM_SB_EDH_BLD_SHFT;
12231 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
12232 TG3_EEPROM_SB_EDH_MAJ_SHFT;
12233 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
12234
12235 if (minor > 99 || build > 26)
12236 return;
12237
12238 snprintf(&tp->fw_ver[2], 30, " v%d.%02d", major, minor);
12239
12240 if (build > 0) {
12241 tp->fw_ver[8] = 'a' + build - 1;
12242 tp->fw_ver[9] = '\0';
12243 }
12244}
12245
Matt Carlsonacd9c112009-02-25 14:26:33 +000012246static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080012247{
12248 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000012249 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070012250
12251 for (offset = TG3_NVM_DIR_START;
12252 offset < TG3_NVM_DIR_END;
12253 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000012254 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012255 return;
12256
12257 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
12258 break;
12259 }
12260
12261 if (offset == TG3_NVM_DIR_END)
12262 return;
12263
12264 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
12265 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000012266 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012267 return;
12268
Matt Carlsone4f34112009-02-25 14:25:00 +000012269 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070012270 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000012271 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012272 return;
12273
12274 offset += val - start;
12275
Matt Carlsonacd9c112009-02-25 14:26:33 +000012276 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012277
Matt Carlsonacd9c112009-02-25 14:26:33 +000012278 tp->fw_ver[vlen++] = ',';
12279 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070012280
12281 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012282 __be32 v;
12283 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012284 return;
12285
Al Virob9fc7dc2007-12-17 22:59:57 -080012286 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012287
Matt Carlsonacd9c112009-02-25 14:26:33 +000012288 if (vlen > TG3_VER_SIZE - sizeof(v)) {
12289 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012290 break;
12291 }
12292
Matt Carlsonacd9c112009-02-25 14:26:33 +000012293 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
12294 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012295 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000012296}
12297
Matt Carlson7fd76442009-02-25 14:27:20 +000012298static void __devinit tg3_read_dash_ver(struct tg3 *tp)
12299{
12300 int vlen;
12301 u32 apedata;
12302
12303 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) ||
12304 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
12305 return;
12306
12307 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
12308 if (apedata != APE_SEG_SIG_MAGIC)
12309 return;
12310
12311 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
12312 if (!(apedata & APE_FW_STATUS_READY))
12313 return;
12314
12315 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
12316
12317 vlen = strlen(tp->fw_ver);
12318
12319 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " DASH v%d.%d.%d.%d",
12320 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
12321 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
12322 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
12323 (apedata & APE_FW_VERSION_BLDMSK));
12324}
12325
Matt Carlsonacd9c112009-02-25 14:26:33 +000012326static void __devinit tg3_read_fw_ver(struct tg3 *tp)
12327{
12328 u32 val;
12329
Matt Carlsondf259d82009-04-20 06:57:14 +000012330 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) {
12331 tp->fw_ver[0] = 's';
12332 tp->fw_ver[1] = 'b';
12333 tp->fw_ver[2] = '\0';
12334
12335 return;
12336 }
12337
Matt Carlsonacd9c112009-02-25 14:26:33 +000012338 if (tg3_nvram_read(tp, 0, &val))
12339 return;
12340
12341 if (val == TG3_EEPROM_MAGIC)
12342 tg3_read_bc_ver(tp);
12343 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
12344 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000012345 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
12346 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000012347 else
12348 return;
12349
12350 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
12351 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
12352 return;
12353
12354 tg3_read_mgmtfw_ver(tp);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012355
12356 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080012357}
12358
Michael Chan7544b092007-05-05 13:08:32 -070012359static struct pci_dev * __devinit tg3_find_peer(struct tg3 *);
12360
Linus Torvalds1da177e2005-04-16 15:20:36 -070012361static int __devinit tg3_get_invariants(struct tg3 *tp)
12362{
12363 static struct pci_device_id write_reorder_chipsets[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012364 { PCI_DEVICE(PCI_VENDOR_ID_AMD,
12365 PCI_DEVICE_ID_AMD_FE_GATE_700C) },
John W. Linvillec165b002006-07-08 13:28:53 -070012366 { PCI_DEVICE(PCI_VENDOR_ID_AMD,
12367 PCI_DEVICE_ID_AMD_8131_BRIDGE) },
Michael Chan399de502005-10-03 14:02:39 -070012368 { PCI_DEVICE(PCI_VENDOR_ID_VIA,
12369 PCI_DEVICE_ID_VIA_8385_0) },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012370 { },
12371 };
12372 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012373 u32 pci_state_reg, grc_misc_cfg;
12374 u32 val;
12375 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080012376 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012377
Linus Torvalds1da177e2005-04-16 15:20:36 -070012378 /* Force memory write invalidate off. If we leave it on,
12379 * then on 5700_BX chips we have to enable a workaround.
12380 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
12381 * to match the cacheline size. The Broadcom driver have this
12382 * workaround but turns MWI off all the times so never uses
12383 * it. This seems to suggest that the workaround is insufficient.
12384 */
12385 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
12386 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
12387 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
12388
12389 /* It is absolutely critical that TG3PCI_MISC_HOST_CTRL
12390 * has the register indirect write enable bit set before
12391 * we try to access any of the MMIO registers. It is also
12392 * critical that the PCI-X hw workaround situation is decided
12393 * before that as well.
12394 */
12395 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
12396 &misc_ctrl_reg);
12397
12398 tp->pci_chip_rev_id = (misc_ctrl_reg >>
12399 MISC_HOST_CTRL_CHIPREV_SHIFT);
Matt Carlson795d01c2007-10-07 23:28:17 -070012400 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
12401 u32 prod_id_asic_rev;
12402
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012403 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717C ||
12404 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717S ||
12405 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718C ||
12406 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718S)
12407 pci_read_config_dword(tp->pdev,
12408 TG3PCI_GEN2_PRODID_ASICREV,
12409 &prod_id_asic_rev);
12410 else
12411 pci_read_config_dword(tp->pdev, TG3PCI_PRODID_ASICREV,
12412 &prod_id_asic_rev);
12413
Matt Carlson321d32a2008-11-21 17:22:19 -080012414 tp->pci_chip_rev_id = prod_id_asic_rev;
Matt Carlson795d01c2007-10-07 23:28:17 -070012415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012416
Michael Chanff645be2005-04-21 17:09:53 -070012417 /* Wrong chip ID in 5752 A0. This code can be removed later
12418 * as A0 is not in production.
12419 */
12420 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
12421 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
12422
Michael Chan68929142005-08-09 20:17:14 -070012423 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
12424 * we need to disable memory and use config. cycles
12425 * only to access all registers. The 5702/03 chips
12426 * can mistakenly decode the special cycles from the
12427 * ICH chipsets as memory write cycles, causing corruption
12428 * of register and memory space. Only certain ICH bridges
12429 * will drive special cycles with non-zero data during the
12430 * address phase which can fall within the 5703's address
12431 * range. This is not an ICH bug as the PCI spec allows
12432 * non-zero address during special cycles. However, only
12433 * these ICH bridges are known to drive non-zero addresses
12434 * during special cycles.
12435 *
12436 * Since special cycles do not cross PCI bridges, we only
12437 * enable this workaround if the 5703 is on the secondary
12438 * bus of these ICH bridges.
12439 */
12440 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
12441 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
12442 static struct tg3_dev_id {
12443 u32 vendor;
12444 u32 device;
12445 u32 rev;
12446 } ich_chipsets[] = {
12447 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
12448 PCI_ANY_ID },
12449 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
12450 PCI_ANY_ID },
12451 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
12452 0xa },
12453 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
12454 PCI_ANY_ID },
12455 { },
12456 };
12457 struct tg3_dev_id *pci_id = &ich_chipsets[0];
12458 struct pci_dev *bridge = NULL;
12459
12460 while (pci_id->vendor != 0) {
12461 bridge = pci_get_device(pci_id->vendor, pci_id->device,
12462 bridge);
12463 if (!bridge) {
12464 pci_id++;
12465 continue;
12466 }
12467 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070012468 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070012469 continue;
12470 }
12471 if (bridge->subordinate &&
12472 (bridge->subordinate->number ==
12473 tp->pdev->bus->number)) {
12474
12475 tp->tg3_flags2 |= TG3_FLG2_ICH_WORKAROUND;
12476 pci_dev_put(bridge);
12477 break;
12478 }
12479 }
12480 }
12481
Matt Carlson41588ba2008-04-19 18:12:33 -070012482 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
12483 static struct tg3_dev_id {
12484 u32 vendor;
12485 u32 device;
12486 } bridge_chipsets[] = {
12487 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
12488 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
12489 { },
12490 };
12491 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
12492 struct pci_dev *bridge = NULL;
12493
12494 while (pci_id->vendor != 0) {
12495 bridge = pci_get_device(pci_id->vendor,
12496 pci_id->device,
12497 bridge);
12498 if (!bridge) {
12499 pci_id++;
12500 continue;
12501 }
12502 if (bridge->subordinate &&
12503 (bridge->subordinate->number <=
12504 tp->pdev->bus->number) &&
12505 (bridge->subordinate->subordinate >=
12506 tp->pdev->bus->number)) {
12507 tp->tg3_flags3 |= TG3_FLG3_5701_DMA_BUG;
12508 pci_dev_put(bridge);
12509 break;
12510 }
12511 }
12512 }
12513
Michael Chan4a29cc22006-03-19 13:21:12 -080012514 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
12515 * DMA addresses > 40-bit. This bridge may have other additional
12516 * 57xx devices behind it in some 4-port NIC designs for example.
12517 * Any tg3 device found behind the bridge will also need the 40-bit
12518 * DMA workaround.
12519 */
Michael Chana4e2b342005-10-26 15:46:52 -070012520 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
12521 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
12522 tp->tg3_flags2 |= TG3_FLG2_5780_CLASS;
Michael Chan4a29cc22006-03-19 13:21:12 -080012523 tp->tg3_flags |= TG3_FLAG_40BIT_DMA_BUG;
Michael Chan4cf78e42005-07-25 12:29:19 -070012524 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Michael Chana4e2b342005-10-26 15:46:52 -070012525 }
Michael Chan4a29cc22006-03-19 13:21:12 -080012526 else {
12527 struct pci_dev *bridge = NULL;
12528
12529 do {
12530 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
12531 PCI_DEVICE_ID_SERVERWORKS_EPB,
12532 bridge);
12533 if (bridge && bridge->subordinate &&
12534 (bridge->subordinate->number <=
12535 tp->pdev->bus->number) &&
12536 (bridge->subordinate->subordinate >=
12537 tp->pdev->bus->number)) {
12538 tp->tg3_flags |= TG3_FLAG_40BIT_DMA_BUG;
12539 pci_dev_put(bridge);
12540 break;
12541 }
12542 } while (bridge);
12543 }
Michael Chan4cf78e42005-07-25 12:29:19 -070012544
Linus Torvalds1da177e2005-04-16 15:20:36 -070012545 /* Initialize misc host control in PCI block. */
12546 tp->misc_host_ctrl |= (misc_ctrl_reg &
12547 MISC_HOST_CTRL_CHIPREV);
12548 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
12549 tp->misc_host_ctrl);
12550
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012551 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
12552 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
12553 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Michael Chan7544b092007-05-05 13:08:32 -070012554 tp->pdev_peer = tg3_find_peer(tp);
12555
Matt Carlson321d32a2008-11-21 17:22:19 -080012556 /* Intentionally exclude ASIC_REV_5906 */
12557 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Michael Chand9ab5ad12006-03-20 22:27:35 -080012558 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070012559 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070012560 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070012561 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012562 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
12563 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson321d32a2008-11-21 17:22:19 -080012564 tp->tg3_flags3 |= TG3_FLG3_5755_PLUS;
12565
12566 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
12567 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Michael Chanb5d37722006-09-27 16:06:21 -070012568 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080012569 (tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
Michael Chana4e2b342005-10-26 15:46:52 -070012570 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
John W. Linville6708e5c2005-04-21 17:00:52 -070012571 tp->tg3_flags2 |= TG3_FLG2_5750_PLUS;
12572
John W. Linville1b440c562005-04-21 17:03:18 -070012573 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) ||
12574 (tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
12575 tp->tg3_flags2 |= TG3_FLG2_5705_PLUS;
12576
Matt Carlson027455a2008-12-21 20:19:30 -080012577 /* 5700 B0 chips do not support checksumming correctly due
12578 * to hardware bugs.
12579 */
12580 if (tp->pci_chip_rev_id == CHIPREV_ID_5700_B0)
12581 tp->tg3_flags |= TG3_FLAG_BROKEN_CHECKSUMS;
12582 else {
12583 tp->tg3_flags |= TG3_FLAG_RX_CHECKSUMS;
12584 tp->dev->features |= NETIF_F_IP_CSUM | NETIF_F_SG;
12585 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
12586 tp->dev->features |= NETIF_F_IPV6_CSUM;
12587 }
12588
Michael Chan5a6f3072006-03-20 22:28:05 -080012589 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
Michael Chan7544b092007-05-05 13:08:32 -070012590 tp->tg3_flags |= TG3_FLAG_SUPPORT_MSI;
12591 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
12592 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
12593 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
12594 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
12595 tp->pdev_peer == tp->pdev))
12596 tp->tg3_flags &= ~TG3_FLAG_SUPPORT_MSI;
12597
Matt Carlson321d32a2008-11-21 17:22:19 -080012598 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070012599 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan5a6f3072006-03-20 22:28:05 -080012600 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_2;
Michael Chanfcfa0a32006-03-20 22:28:41 -080012601 tp->tg3_flags2 |= TG3_FLG2_1SHOT_MSI;
Michael Chan52c0fd82006-06-29 20:15:54 -070012602 } else {
Michael Chan7f62ad52007-02-20 23:25:40 -080012603 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_1 | TG3_FLG2_TSO_BUG;
Michael Chan52c0fd82006-06-29 20:15:54 -070012604 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
12605 ASIC_REV_5750 &&
12606 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
Michael Chan7f62ad52007-02-20 23:25:40 -080012607 tp->tg3_flags2 &= ~TG3_FLG2_TSO_BUG;
Michael Chan52c0fd82006-06-29 20:15:54 -070012608 }
Michael Chan5a6f3072006-03-20 22:28:05 -080012609 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012610
Matt Carlson4f125f42009-09-01 12:55:02 +000012611 tp->irq_max = 1;
12612
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012613#ifdef TG3_NAPI
12614 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
12615 tp->tg3_flags |= TG3_FLAG_SUPPORT_MSIX;
12616 tp->irq_max = TG3_IRQ_MAX_VECS;
12617 }
12618#endif
12619
Matt Carlsonf51f3562008-05-25 23:45:08 -070012620 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012621 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
12622 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson8f666b02009-08-28 13:58:24 +000012623 tp->tg3_flags |= TG3_FLAG_JUMBO_CAPABLE;
Michael Chan0f893dc2005-07-25 12:30:38 -070012624
Matt Carlson52f44902008-11-21 17:17:04 -080012625 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
12626 &pci_state_reg);
12627
Matt Carlson5e7dfd02008-11-21 17:18:16 -080012628 tp->pcie_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_EXP);
12629 if (tp->pcie_cap != 0) {
12630 u16 lnkctl;
12631
Linus Torvalds1da177e2005-04-16 15:20:36 -070012632 tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS;
Matt Carlson5f5c51e2007-11-12 21:19:37 -080012633
12634 pcie_set_readrq(tp->pdev, 4096);
12635
Matt Carlson5e7dfd02008-11-21 17:18:16 -080012636 pci_read_config_word(tp->pdev,
12637 tp->pcie_cap + PCI_EXP_LNKCTL,
12638 &lnkctl);
12639 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
12640 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanc7835a72006-11-15 21:14:42 -080012641 tp->tg3_flags2 &= ~TG3_FLG2_HW_TSO_2;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080012642 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080012643 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000012644 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
12645 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Matt Carlson5e7dfd02008-11-21 17:18:16 -080012646 tp->tg3_flags3 |= TG3_FLG3_CLKREQ_BUG;
Michael Chanc7835a72006-11-15 21:14:42 -080012647 }
Matt Carlson52f44902008-11-21 17:17:04 -080012648 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Matt Carlsonfcb389d2008-11-03 16:55:44 -080012649 tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS;
Matt Carlson52f44902008-11-21 17:17:04 -080012650 } else if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
12651 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
12652 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
12653 if (!tp->pcix_cap) {
12654 printk(KERN_ERR PFX "Cannot find PCI-X "
12655 "capability, aborting.\n");
12656 return -EIO;
12657 }
12658
12659 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
12660 tp->tg3_flags |= TG3_FLAG_PCIX_MODE;
12661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012662
Michael Chan399de502005-10-03 14:02:39 -070012663 /* If we have an AMD 762 or VIA K8T800 chipset, write
12664 * reordering to the mailbox registers done by the host
12665 * controller can cause major troubles. We read back from
12666 * every mailbox register write to force the writes to be
12667 * posted to the chip in order.
12668 */
12669 if (pci_dev_present(write_reorder_chipsets) &&
12670 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
12671 tp->tg3_flags |= TG3_FLAG_MBOX_WRITE_REORDER;
12672
Matt Carlson69fc4052008-12-21 20:19:57 -080012673 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
12674 &tp->pci_cacheline_sz);
12675 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
12676 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012677 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
12678 tp->pci_lat_timer < 64) {
12679 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080012680 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
12681 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012682 }
12683
Matt Carlson52f44902008-11-21 17:17:04 -080012684 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
12685 /* 5700 BX chips need to have their TX producer index
12686 * mailboxes written twice to workaround a bug.
12687 */
12688 tp->tg3_flags |= TG3_FLAG_TXD_MBOX_HWBUG;
Matt Carlson9974a352007-10-07 23:27:28 -070012689
Matt Carlson52f44902008-11-21 17:17:04 -080012690 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012691 *
12692 * The workaround is to use indirect register accesses
12693 * for all chip writes not to mailbox registers.
12694 */
Matt Carlson52f44902008-11-21 17:17:04 -080012695 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012696 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012697
12698 tp->tg3_flags |= TG3_FLAG_PCIX_TARGET_HWBUG;
12699
12700 /* The chip can have it's power management PCI config
12701 * space registers clobbered due to this bug.
12702 * So explicitly force the chip into D0 here.
12703 */
Matt Carlson9974a352007-10-07 23:27:28 -070012704 pci_read_config_dword(tp->pdev,
12705 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012706 &pm_reg);
12707 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
12708 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070012709 pci_write_config_dword(tp->pdev,
12710 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012711 pm_reg);
12712
12713 /* Also, force SERR#/PERR# in PCI command. */
12714 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
12715 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
12716 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
12717 }
12718 }
12719
Linus Torvalds1da177e2005-04-16 15:20:36 -070012720 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
12721 tp->tg3_flags |= TG3_FLAG_PCI_HIGH_SPEED;
12722 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
12723 tp->tg3_flags |= TG3_FLAG_PCI_32BIT;
12724
12725 /* Chip-specific fixup from Broadcom driver */
12726 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
12727 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
12728 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
12729 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
12730 }
12731
Michael Chan1ee582d2005-08-09 20:16:46 -070012732 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070012733 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070012734 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070012735 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070012736 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070012737 tp->write32_tx_mbox = tg3_write32;
12738 tp->write32_rx_mbox = tg3_write32;
12739
12740 /* Various workaround register access methods */
12741 if (tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG)
12742 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070012743 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
12744 ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
12745 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
12746 /*
12747 * Back to back register writes can cause problems on these
12748 * chips, the workaround is to read back all reg writes
12749 * except those to mailbox regs.
12750 *
12751 * See tg3_write_indirect_reg32().
12752 */
Michael Chan1ee582d2005-08-09 20:16:46 -070012753 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070012754 }
12755
Michael Chan1ee582d2005-08-09 20:16:46 -070012756 if ((tp->tg3_flags & TG3_FLAG_TXD_MBOX_HWBUG) ||
12757 (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)) {
12758 tp->write32_tx_mbox = tg3_write32_tx_mbox;
12759 if (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)
12760 tp->write32_rx_mbox = tg3_write_flush_reg32;
12761 }
Michael Chan20094932005-08-09 20:16:32 -070012762
Michael Chan68929142005-08-09 20:17:14 -070012763 if (tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND) {
12764 tp->read32 = tg3_read_indirect_reg32;
12765 tp->write32 = tg3_write_indirect_reg32;
12766 tp->read32_mbox = tg3_read_indirect_mbox;
12767 tp->write32_mbox = tg3_write_indirect_mbox;
12768 tp->write32_tx_mbox = tg3_write_indirect_mbox;
12769 tp->write32_rx_mbox = tg3_write_indirect_mbox;
12770
12771 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070012772 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070012773
12774 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
12775 pci_cmd &= ~PCI_COMMAND_MEMORY;
12776 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
12777 }
Michael Chanb5d37722006-09-27 16:06:21 -070012778 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
12779 tp->read32_mbox = tg3_read32_mbox_5906;
12780 tp->write32_mbox = tg3_write32_mbox_5906;
12781 tp->write32_tx_mbox = tg3_write32_mbox_5906;
12782 tp->write32_rx_mbox = tg3_write32_mbox_5906;
12783 }
Michael Chan68929142005-08-09 20:17:14 -070012784
Michael Chanbbadf502006-04-06 21:46:34 -070012785 if (tp->write32 == tg3_write_indirect_reg32 ||
12786 ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) &&
12787 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070012788 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Michael Chanbbadf502006-04-06 21:46:34 -070012789 tp->tg3_flags |= TG3_FLAG_SRAM_USE_CONFIG;
12790
Michael Chan7d0c41e2005-04-21 17:06:20 -070012791 /* Get eeprom hw config before calling tg3_set_power_state().
Michael Chan9d26e212006-12-07 00:21:14 -080012792 * In particular, the TG3_FLG2_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070012793 * determined before calling tg3_set_power_state() so that
12794 * we know whether or not to switch out of Vaux power.
12795 * When the flag is set, it means that GPIO1 is used for eeprom
12796 * write protect and also implies that it is a LOM where GPIOs
12797 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012798 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070012799 tg3_get_eeprom_hw_cfg(tp);
12800
Matt Carlson0d3031d2007-10-10 18:02:43 -070012801 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
12802 /* Allow reads and writes to the
12803 * APE register and memory space.
12804 */
12805 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
12806 PCISTATE_ALLOW_APE_SHMEM_WR;
12807 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
12808 pci_state_reg);
12809 }
12810
Matt Carlson9936bcf2007-10-10 18:03:07 -070012811 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson57e69832008-05-25 23:48:31 -070012812 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080012813 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012814 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
12815 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlsond30cdd22007-10-07 23:28:35 -070012816 tp->tg3_flags |= TG3_FLAG_CPMU_PRESENT;
12817
Michael Chan314fba32005-04-21 17:07:04 -070012818 /* Set up tp->grc_local_ctrl before calling tg3_set_power_state().
12819 * GPIO1 driven high will bring 5700's external PHY out of reset.
12820 * It is also used as eeprom write protect on LOMs.
12821 */
12822 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
12823 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
12824 (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT))
12825 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
12826 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070012827 /* Unused GPIO3 must be driven as output on 5752 because there
12828 * are no pull-up resistors on unused GPIO pins.
12829 */
12830 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
12831 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070012832
Matt Carlson321d32a2008-11-21 17:22:19 -080012833 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
12834 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Michael Chanaf36e6b2006-03-23 01:28:06 -080012835 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
12836
Matt Carlson8d519ab2009-04-20 06:58:01 +000012837 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
12838 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070012839 /* Turn off the debug UART. */
12840 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
12841 if (tp->tg3_flags2 & TG3_FLG2_IS_NIC)
12842 /* Keep VMain power. */
12843 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
12844 GRC_LCLCTRL_GPIO_OUTPUT0;
12845 }
12846
Linus Torvalds1da177e2005-04-16 15:20:36 -070012847 /* Force the chip into D0. */
Michael Chanbc1c7562006-03-20 17:48:03 -080012848 err = tg3_set_power_state(tp, PCI_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012849 if (err) {
12850 printk(KERN_ERR PFX "(%s) transition to D0 failed\n",
12851 pci_name(tp->pdev));
12852 return err;
12853 }
12854
Linus Torvalds1da177e2005-04-16 15:20:36 -070012855 /* Derive initial jumbo mode from MTU assigned in
12856 * ether_setup() via the alloc_etherdev() call
12857 */
Michael Chan0f893dc2005-07-25 12:30:38 -070012858 if (tp->dev->mtu > ETH_DATA_LEN &&
Michael Chana4e2b342005-10-26 15:46:52 -070012859 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Michael Chan0f893dc2005-07-25 12:30:38 -070012860 tp->tg3_flags |= TG3_FLAG_JUMBO_RING_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012861
12862 /* Determine WakeOnLan speed to use. */
12863 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
12864 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
12865 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
12866 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
12867 tp->tg3_flags &= ~(TG3_FLAG_WOL_SPEED_100MB);
12868 } else {
12869 tp->tg3_flags |= TG3_FLAG_WOL_SPEED_100MB;
12870 }
12871
Matt Carlson7f97a4b2009-08-25 10:10:03 +000012872 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12873 tp->tg3_flags3 |= TG3_FLG3_PHY_IS_FET;
12874
Linus Torvalds1da177e2005-04-16 15:20:36 -070012875 /* A few boards don't want Ethernet@WireSpeed phy feature */
12876 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
12877 ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
12878 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070012879 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlson7f97a4b2009-08-25 10:10:03 +000012880 (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) ||
Michael Chan747e8f82005-07-25 12:33:22 -070012881 (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012882 tp->tg3_flags2 |= TG3_FLG2_NO_ETH_WIRE_SPEED;
12883
12884 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
12885 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
12886 tp->tg3_flags2 |= TG3_FLG2_PHY_ADC_BUG;
12887 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
12888 tp->tg3_flags2 |= TG3_FLG2_PHY_5704_A0_BUG;
12889
Matt Carlson321d32a2008-11-21 17:22:19 -080012890 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Matt Carlson7f97a4b2009-08-25 10:10:03 +000012891 !(tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) &&
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) {
Michael Chanc424cb22006-04-29 18:56:34 -070012895 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070012896 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070012897 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
12898 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080012899 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
12900 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
12901 tp->tg3_flags2 |= TG3_FLG2_PHY_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080012902 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
12903 tp->tg3_flags2 |= TG3_FLG2_PHY_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080012904 } else
Michael Chanc424cb22006-04-29 18:56:34 -070012905 tp->tg3_flags2 |= TG3_FLG2_PHY_BER_BUG;
12906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012907
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012908 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
12909 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
12910 tp->phy_otp = tg3_read_otp_phycfg(tp);
12911 if (tp->phy_otp == 0)
12912 tp->phy_otp = TG3_OTP_DEFAULT;
12913 }
12914
Matt Carlsonf51f3562008-05-25 23:45:08 -070012915 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT)
Matt Carlson8ef21422008-05-02 16:47:53 -070012916 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
12917 else
12918 tp->mi_mode = MAC_MI_MODE_BASE;
12919
Linus Torvalds1da177e2005-04-16 15:20:36 -070012920 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012921 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
12922 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
12923 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
12924
Matt Carlson321d32a2008-11-21 17:22:19 -080012925 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
12926 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson57e69832008-05-25 23:48:31 -070012927 tp->tg3_flags3 |= TG3_FLG3_USE_PHYLIB;
12928
Matt Carlson255ca312009-08-25 10:07:27 +000012929 if ((tp->pci_chip_rev_id == CHIPREV_ID_57780_A1 &&
12930 tr32(RCVLPC_STATS_ENABLE) & RCVLPC_STATSENAB_ASF_FIX) ||
12931 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0)
12932 tp->tg3_flags3 |= TG3_FLG3_TOGGLE_10_100_L1PLLPD;
12933
Matt Carlson158d7ab2008-05-29 01:37:54 -070012934 err = tg3_mdio_init(tp);
12935 if (err)
12936 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012937
12938 /* Initialize data/descriptor byte/word swapping. */
12939 val = tr32(GRC_MODE);
12940 val &= GRC_MODE_HOST_STACKUP;
12941 tw32(GRC_MODE, val | tp->grc_mode);
12942
12943 tg3_switch_clocks(tp);
12944
12945 /* Clear this out for sanity. */
12946 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
12947
12948 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
12949 &pci_state_reg);
12950 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
12951 (tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG) == 0) {
12952 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
12953
12954 if (chiprevid == CHIPREV_ID_5701_A0 ||
12955 chiprevid == CHIPREV_ID_5701_B0 ||
12956 chiprevid == CHIPREV_ID_5701_B2 ||
12957 chiprevid == CHIPREV_ID_5701_B5) {
12958 void __iomem *sram_base;
12959
12960 /* Write some dummy words into the SRAM status block
12961 * area, see if it reads back correctly. If the return
12962 * value is bad, force enable the PCIX workaround.
12963 */
12964 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
12965
12966 writel(0x00000000, sram_base);
12967 writel(0x00000000, sram_base + 4);
12968 writel(0xffffffff, sram_base + 4);
12969 if (readl(sram_base) != 0x00000000)
12970 tp->tg3_flags |= TG3_FLAG_PCIX_TARGET_HWBUG;
12971 }
12972 }
12973
12974 udelay(50);
12975 tg3_nvram_init(tp);
12976
12977 grc_misc_cfg = tr32(GRC_MISC_CFG);
12978 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
12979
Linus Torvalds1da177e2005-04-16 15:20:36 -070012980 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
12981 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
12982 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
12983 tp->tg3_flags2 |= TG3_FLG2_IS_5788;
12984
David S. Millerfac9b832005-05-18 22:46:34 -070012985 if (!(tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
12986 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700))
12987 tp->tg3_flags |= TG3_FLAG_TAGGED_STATUS;
12988 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) {
12989 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
12990 HOSTCC_MODE_CLRTICK_TXBD);
12991
12992 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
12993 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
12994 tp->misc_host_ctrl);
12995 }
12996
Matt Carlson3bda1252008-08-15 14:08:22 -070012997 /* Preserve the APE MAC_MODE bits */
12998 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
12999 tp->mac_mode = tr32(MAC_MODE) |
13000 MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
13001 else
13002 tp->mac_mode = TG3_DEF_MAC_MODE;
13003
Linus Torvalds1da177e2005-04-16 15:20:36 -070013004 /* these are limited to 10/100 only */
13005 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
13006 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
13007 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
13008 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
13009 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
13010 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
13011 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
13012 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
13013 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
Michael Chan676917d2006-12-07 00:20:22 -080013014 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
13015 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013016 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013017 (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013018 tp->tg3_flags |= TG3_FLAG_10_100_ONLY;
13019
13020 err = tg3_phy_probe(tp);
13021 if (err) {
13022 printk(KERN_ERR PFX "(%s) phy probe failed, err %d\n",
13023 pci_name(tp->pdev), err);
13024 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013025 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013026 }
13027
13028 tg3_read_partno(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080013029 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013030
13031 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
13032 tp->tg3_flags &= ~TG3_FLAG_USE_MI_INTERRUPT;
13033 } else {
13034 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
13035 tp->tg3_flags |= TG3_FLAG_USE_MI_INTERRUPT;
13036 else
13037 tp->tg3_flags &= ~TG3_FLAG_USE_MI_INTERRUPT;
13038 }
13039
13040 /* 5700 {AX,BX} chips have a broken status block link
13041 * change bit implementation, so we must use the
13042 * status register in those cases.
13043 */
13044 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
13045 tp->tg3_flags |= TG3_FLAG_USE_LINKCHG_REG;
13046 else
13047 tp->tg3_flags &= ~TG3_FLAG_USE_LINKCHG_REG;
13048
13049 /* The led_ctrl is set during tg3_phy_probe, here we might
13050 * have to force the link status polling mechanism based
13051 * upon subsystem IDs.
13052 */
13053 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070013054 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070013055 !(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)) {
13056 tp->tg3_flags |= (TG3_FLAG_USE_MI_INTERRUPT |
13057 TG3_FLAG_USE_LINKCHG_REG);
13058 }
13059
13060 /* For all SERDES we poll the MAC status register. */
13061 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
13062 tp->tg3_flags |= TG3_FLAG_POLL_SERDES;
13063 else
13064 tp->tg3_flags &= ~TG3_FLAG_POLL_SERDES;
13065
Matt Carlsonad829262008-11-21 17:16:16 -080013066 tp->rx_offset = NET_IP_ALIGN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013067 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
13068 (tp->tg3_flags & TG3_FLAG_PCIX_MODE) != 0)
13069 tp->rx_offset = 0;
13070
Michael Chanf92905d2006-06-29 20:14:29 -070013071 tp->rx_std_max_post = TG3_RX_RING_SIZE;
13072
13073 /* Increment the rx prod index on the rx std ring by at most
13074 * 8 for these chips to workaround hw errata.
13075 */
13076 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13077 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
13078 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
13079 tp->rx_std_max_post = 8;
13080
Matt Carlson8ed5d972007-05-07 00:25:49 -070013081 if (tp->tg3_flags & TG3_FLAG_ASPM_WORKAROUND)
13082 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
13083 PCIE_PWR_MGMT_L1_THRESH_MSK;
13084
Linus Torvalds1da177e2005-04-16 15:20:36 -070013085 return err;
13086}
13087
David S. Miller49b6e95f2007-03-29 01:38:42 -070013088#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070013089static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
13090{
13091 struct net_device *dev = tp->dev;
13092 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070013093 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070013094 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070013095 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013096
David S. Miller49b6e95f2007-03-29 01:38:42 -070013097 addr = of_get_property(dp, "local-mac-address", &len);
13098 if (addr && len == 6) {
13099 memcpy(dev->dev_addr, addr, 6);
13100 memcpy(dev->perm_addr, dev->dev_addr, 6);
13101 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013102 }
13103 return -ENODEV;
13104}
13105
13106static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
13107{
13108 struct net_device *dev = tp->dev;
13109
13110 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070013111 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013112 return 0;
13113}
13114#endif
13115
13116static int __devinit tg3_get_device_address(struct tg3 *tp)
13117{
13118 struct net_device *dev = tp->dev;
13119 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080013120 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013121
David S. Miller49b6e95f2007-03-29 01:38:42 -070013122#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070013123 if (!tg3_get_macaddr_sparc(tp))
13124 return 0;
13125#endif
13126
13127 mac_offset = 0x7c;
David S. Millerf49639e2006-06-09 11:58:36 -070013128 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) ||
Michael Chana4e2b342005-10-26 15:46:52 -070013129 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013130 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
13131 mac_offset = 0xcc;
13132 if (tg3_nvram_lock(tp))
13133 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
13134 else
13135 tg3_nvram_unlock(tp);
Matt Carlsona1b950d2009-09-01 13:20:17 +000013136 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
13137 if (tr32(TG3_CPMU_STATUS) & TG3_CPMU_STATUS_PCIE_FUNC)
13138 mac_offset = 0xcc;
13139 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070013140 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013141
13142 /* First try to get it from MAC address mailbox. */
13143 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
13144 if ((hi >> 16) == 0x484b) {
13145 dev->dev_addr[0] = (hi >> 8) & 0xff;
13146 dev->dev_addr[1] = (hi >> 0) & 0xff;
13147
13148 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
13149 dev->dev_addr[2] = (lo >> 24) & 0xff;
13150 dev->dev_addr[3] = (lo >> 16) & 0xff;
13151 dev->dev_addr[4] = (lo >> 8) & 0xff;
13152 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013153
Michael Chan008652b2006-03-27 23:14:53 -080013154 /* Some old bootcode may report a 0 MAC address in SRAM */
13155 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
13156 }
13157 if (!addr_ok) {
13158 /* Next, try NVRAM. */
Matt Carlsondf259d82009-04-20 06:57:14 +000013159 if (!(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) &&
13160 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000013161 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070013162 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
13163 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080013164 }
13165 /* Finally just fetch it out of the MAC control regs. */
13166 else {
13167 hi = tr32(MAC_ADDR_0_HIGH);
13168 lo = tr32(MAC_ADDR_0_LOW);
13169
13170 dev->dev_addr[5] = lo & 0xff;
13171 dev->dev_addr[4] = (lo >> 8) & 0xff;
13172 dev->dev_addr[3] = (lo >> 16) & 0xff;
13173 dev->dev_addr[2] = (lo >> 24) & 0xff;
13174 dev->dev_addr[1] = hi & 0xff;
13175 dev->dev_addr[0] = (hi >> 8) & 0xff;
13176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013177 }
13178
13179 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070013180#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070013181 if (!tg3_get_default_macaddr_sparc(tp))
13182 return 0;
13183#endif
13184 return -EINVAL;
13185 }
John W. Linville2ff43692005-09-12 14:44:20 -070013186 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013187 return 0;
13188}
13189
David S. Miller59e6b432005-05-18 22:50:10 -070013190#define BOUNDARY_SINGLE_CACHELINE 1
13191#define BOUNDARY_MULTI_CACHELINE 2
13192
13193static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
13194{
13195 int cacheline_size;
13196 u8 byte;
13197 int goal;
13198
13199 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
13200 if (byte == 0)
13201 cacheline_size = 1024;
13202 else
13203 cacheline_size = (int) byte * 4;
13204
13205 /* On 5703 and later chips, the boundary bits have no
13206 * effect.
13207 */
13208 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13209 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13210 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
13211 goto out;
13212
13213#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
13214 goal = BOUNDARY_MULTI_CACHELINE;
13215#else
13216#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
13217 goal = BOUNDARY_SINGLE_CACHELINE;
13218#else
13219 goal = 0;
13220#endif
13221#endif
13222
13223 if (!goal)
13224 goto out;
13225
13226 /* PCI controllers on most RISC systems tend to disconnect
13227 * when a device tries to burst across a cache-line boundary.
13228 * Therefore, letting tg3 do so just wastes PCI bandwidth.
13229 *
13230 * Unfortunately, for PCI-E there are only limited
13231 * write-side controls for this, and thus for reads
13232 * we will still get the disconnects. We'll also waste
13233 * these PCI cycles for both read and write for chips
13234 * other than 5700 and 5701 which do not implement the
13235 * boundary bits.
13236 */
13237 if ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) &&
13238 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)) {
13239 switch (cacheline_size) {
13240 case 16:
13241 case 32:
13242 case 64:
13243 case 128:
13244 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13245 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
13246 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
13247 } else {
13248 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
13249 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
13250 }
13251 break;
13252
13253 case 256:
13254 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
13255 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
13256 break;
13257
13258 default:
13259 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
13260 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
13261 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070013262 }
David S. Miller59e6b432005-05-18 22:50:10 -070013263 } else if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
13264 switch (cacheline_size) {
13265 case 16:
13266 case 32:
13267 case 64:
13268 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13269 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
13270 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
13271 break;
13272 }
13273 /* fallthrough */
13274 case 128:
13275 default:
13276 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
13277 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
13278 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070013279 }
David S. Miller59e6b432005-05-18 22:50:10 -070013280 } else {
13281 switch (cacheline_size) {
13282 case 16:
13283 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13284 val |= (DMA_RWCTRL_READ_BNDRY_16 |
13285 DMA_RWCTRL_WRITE_BNDRY_16);
13286 break;
13287 }
13288 /* fallthrough */
13289 case 32:
13290 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13291 val |= (DMA_RWCTRL_READ_BNDRY_32 |
13292 DMA_RWCTRL_WRITE_BNDRY_32);
13293 break;
13294 }
13295 /* fallthrough */
13296 case 64:
13297 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13298 val |= (DMA_RWCTRL_READ_BNDRY_64 |
13299 DMA_RWCTRL_WRITE_BNDRY_64);
13300 break;
13301 }
13302 /* fallthrough */
13303 case 128:
13304 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13305 val |= (DMA_RWCTRL_READ_BNDRY_128 |
13306 DMA_RWCTRL_WRITE_BNDRY_128);
13307 break;
13308 }
13309 /* fallthrough */
13310 case 256:
13311 val |= (DMA_RWCTRL_READ_BNDRY_256 |
13312 DMA_RWCTRL_WRITE_BNDRY_256);
13313 break;
13314 case 512:
13315 val |= (DMA_RWCTRL_READ_BNDRY_512 |
13316 DMA_RWCTRL_WRITE_BNDRY_512);
13317 break;
13318 case 1024:
13319 default:
13320 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
13321 DMA_RWCTRL_WRITE_BNDRY_1024);
13322 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070013323 }
David S. Miller59e6b432005-05-18 22:50:10 -070013324 }
13325
13326out:
13327 return val;
13328}
13329
Linus Torvalds1da177e2005-04-16 15:20:36 -070013330static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
13331{
13332 struct tg3_internal_buffer_desc test_desc;
13333 u32 sram_dma_descs;
13334 int i, ret;
13335
13336 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
13337
13338 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
13339 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
13340 tw32(RDMAC_STATUS, 0);
13341 tw32(WDMAC_STATUS, 0);
13342
13343 tw32(BUFMGR_MODE, 0);
13344 tw32(FTQ_RESET, 0);
13345
13346 test_desc.addr_hi = ((u64) buf_dma) >> 32;
13347 test_desc.addr_lo = buf_dma & 0xffffffff;
13348 test_desc.nic_mbuf = 0x00002100;
13349 test_desc.len = size;
13350
13351 /*
13352 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
13353 * the *second* time the tg3 driver was getting loaded after an
13354 * initial scan.
13355 *
13356 * Broadcom tells me:
13357 * ...the DMA engine is connected to the GRC block and a DMA
13358 * reset may affect the GRC block in some unpredictable way...
13359 * The behavior of resets to individual blocks has not been tested.
13360 *
13361 * Broadcom noted the GRC reset will also reset all sub-components.
13362 */
13363 if (to_device) {
13364 test_desc.cqid_sqid = (13 << 8) | 2;
13365
13366 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
13367 udelay(40);
13368 } else {
13369 test_desc.cqid_sqid = (16 << 8) | 7;
13370
13371 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
13372 udelay(40);
13373 }
13374 test_desc.flags = 0x00000005;
13375
13376 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
13377 u32 val;
13378
13379 val = *(((u32 *)&test_desc) + i);
13380 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
13381 sram_dma_descs + (i * sizeof(u32)));
13382 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
13383 }
13384 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
13385
13386 if (to_device) {
13387 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
13388 } else {
13389 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
13390 }
13391
13392 ret = -ENODEV;
13393 for (i = 0; i < 40; i++) {
13394 u32 val;
13395
13396 if (to_device)
13397 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
13398 else
13399 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
13400 if ((val & 0xffff) == sram_dma_descs) {
13401 ret = 0;
13402 break;
13403 }
13404
13405 udelay(100);
13406 }
13407
13408 return ret;
13409}
13410
David S. Millerded73402005-05-23 13:59:47 -070013411#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070013412
13413static int __devinit tg3_test_dma(struct tg3 *tp)
13414{
13415 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070013416 u32 *buf, saved_dma_rwctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013417 int ret;
13418
13419 buf = pci_alloc_consistent(tp->pdev, TEST_BUFFER_SIZE, &buf_dma);
13420 if (!buf) {
13421 ret = -ENOMEM;
13422 goto out_nofree;
13423 }
13424
13425 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
13426 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
13427
David S. Miller59e6b432005-05-18 22:50:10 -070013428 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013429
13430 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
13431 /* DMA read watermark not used on PCIE */
13432 tp->dma_rwctrl |= 0x00180000;
13433 } else if (!(tp->tg3_flags & TG3_FLAG_PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070013434 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
13435 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013436 tp->dma_rwctrl |= 0x003f0000;
13437 else
13438 tp->dma_rwctrl |= 0x003f000f;
13439 } else {
13440 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
13441 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
13442 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080013443 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013444
Michael Chan4a29cc22006-03-19 13:21:12 -080013445 /* If the 5704 is behind the EPB bridge, we can
13446 * do the less restrictive ONE_DMA workaround for
13447 * better performance.
13448 */
13449 if ((tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG) &&
13450 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
13451 tp->dma_rwctrl |= 0x8000;
13452 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013453 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
13454
Michael Chan49afdeb2007-02-13 12:17:03 -080013455 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
13456 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070013457 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080013458 tp->dma_rwctrl |=
13459 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
13460 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
13461 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070013462 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
13463 /* 5780 always in PCIX mode */
13464 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070013465 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
13466 /* 5714 always in PCIX mode */
13467 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013468 } else {
13469 tp->dma_rwctrl |= 0x001b000f;
13470 }
13471 }
13472
13473 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
13474 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
13475 tp->dma_rwctrl &= 0xfffffff0;
13476
13477 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13478 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
13479 /* Remove this if it causes problems for some boards. */
13480 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
13481
13482 /* On 5700/5701 chips, we need to set this bit.
13483 * Otherwise the chip will issue cacheline transactions
13484 * to streamable DMA memory with not all the byte
13485 * enables turned on. This is an error on several
13486 * RISC PCI controllers, in particular sparc64.
13487 *
13488 * On 5703/5704 chips, this bit has been reassigned
13489 * a different meaning. In particular, it is used
13490 * on those chips to enable a PCI-X workaround.
13491 */
13492 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
13493 }
13494
13495 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
13496
13497#if 0
13498 /* Unneeded, already done by tg3_get_invariants. */
13499 tg3_switch_clocks(tp);
13500#endif
13501
13502 ret = 0;
13503 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13504 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
13505 goto out;
13506
David S. Miller59e6b432005-05-18 22:50:10 -070013507 /* It is best to perform DMA test with maximum write burst size
13508 * to expose the 5700/5701 write DMA bug.
13509 */
13510 saved_dma_rwctrl = tp->dma_rwctrl;
13511 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
13512 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
13513
Linus Torvalds1da177e2005-04-16 15:20:36 -070013514 while (1) {
13515 u32 *p = buf, i;
13516
13517 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
13518 p[i] = i;
13519
13520 /* Send the buffer to the chip. */
13521 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
13522 if (ret) {
13523 printk(KERN_ERR "tg3_test_dma() Write the buffer failed %d\n", ret);
13524 break;
13525 }
13526
13527#if 0
13528 /* validate data reached card RAM correctly. */
13529 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
13530 u32 val;
13531 tg3_read_mem(tp, 0x2100 + (i*4), &val);
13532 if (le32_to_cpu(val) != p[i]) {
13533 printk(KERN_ERR " tg3_test_dma() Card buffer corrupted on write! (%d != %d)\n", val, i);
13534 /* ret = -ENODEV here? */
13535 }
13536 p[i] = 0;
13537 }
13538#endif
13539 /* Now read it back. */
13540 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
13541 if (ret) {
13542 printk(KERN_ERR "tg3_test_dma() Read the buffer failed %d\n", ret);
13543
13544 break;
13545 }
13546
13547 /* Verify it. */
13548 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
13549 if (p[i] == i)
13550 continue;
13551
David S. Miller59e6b432005-05-18 22:50:10 -070013552 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
13553 DMA_RWCTRL_WRITE_BNDRY_16) {
13554 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013555 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
13556 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
13557 break;
13558 } else {
13559 printk(KERN_ERR "tg3_test_dma() buffer corrupted on read back! (%d != %d)\n", p[i], i);
13560 ret = -ENODEV;
13561 goto out;
13562 }
13563 }
13564
13565 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
13566 /* Success. */
13567 ret = 0;
13568 break;
13569 }
13570 }
David S. Miller59e6b432005-05-18 22:50:10 -070013571 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
13572 DMA_RWCTRL_WRITE_BNDRY_16) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070013573 static struct pci_device_id dma_wait_state_chipsets[] = {
13574 { PCI_DEVICE(PCI_VENDOR_ID_APPLE,
13575 PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
13576 { },
13577 };
13578
David S. Miller59e6b432005-05-18 22:50:10 -070013579 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070013580 * now look for chipsets that are known to expose the
13581 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070013582 */
Michael Chan6d1cfba2005-06-08 14:13:14 -070013583 if (pci_dev_present(dma_wait_state_chipsets)) {
13584 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
13585 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
13586 }
13587 else
13588 /* Safe to use the calculated DMA boundary. */
13589 tp->dma_rwctrl = saved_dma_rwctrl;
13590
David S. Miller59e6b432005-05-18 22:50:10 -070013591 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
13592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013593
13594out:
13595 pci_free_consistent(tp->pdev, TEST_BUFFER_SIZE, buf, buf_dma);
13596out_nofree:
13597 return ret;
13598}
13599
13600static void __devinit tg3_init_link_config(struct tg3 *tp)
13601{
13602 tp->link_config.advertising =
13603 (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
13604 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
13605 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full |
13606 ADVERTISED_Autoneg | ADVERTISED_MII);
13607 tp->link_config.speed = SPEED_INVALID;
13608 tp->link_config.duplex = DUPLEX_INVALID;
13609 tp->link_config.autoneg = AUTONEG_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013610 tp->link_config.active_speed = SPEED_INVALID;
13611 tp->link_config.active_duplex = DUPLEX_INVALID;
13612 tp->link_config.phy_is_low_power = 0;
13613 tp->link_config.orig_speed = SPEED_INVALID;
13614 tp->link_config.orig_duplex = DUPLEX_INVALID;
13615 tp->link_config.orig_autoneg = AUTONEG_INVALID;
13616}
13617
13618static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
13619{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013620 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS &&
13621 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717) {
Michael Chanfdfec1722005-07-25 12:31:48 -070013622 tp->bufmgr_config.mbuf_read_dma_low_water =
13623 DEFAULT_MB_RDMA_LOW_WATER_5705;
13624 tp->bufmgr_config.mbuf_mac_rx_low_water =
13625 DEFAULT_MB_MACRX_LOW_WATER_5705;
13626 tp->bufmgr_config.mbuf_high_water =
13627 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070013628 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
13629 tp->bufmgr_config.mbuf_mac_rx_low_water =
13630 DEFAULT_MB_MACRX_LOW_WATER_5906;
13631 tp->bufmgr_config.mbuf_high_water =
13632 DEFAULT_MB_HIGH_WATER_5906;
13633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013634
Michael Chanfdfec1722005-07-25 12:31:48 -070013635 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
13636 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
13637 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
13638 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
13639 tp->bufmgr_config.mbuf_high_water_jumbo =
13640 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
13641 } else {
13642 tp->bufmgr_config.mbuf_read_dma_low_water =
13643 DEFAULT_MB_RDMA_LOW_WATER;
13644 tp->bufmgr_config.mbuf_mac_rx_low_water =
13645 DEFAULT_MB_MACRX_LOW_WATER;
13646 tp->bufmgr_config.mbuf_high_water =
13647 DEFAULT_MB_HIGH_WATER;
13648
13649 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
13650 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
13651 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
13652 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
13653 tp->bufmgr_config.mbuf_high_water_jumbo =
13654 DEFAULT_MB_HIGH_WATER_JUMBO;
13655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013656
13657 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
13658 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
13659}
13660
13661static char * __devinit tg3_phy_string(struct tg3 *tp)
13662{
13663 switch (tp->phy_id & PHY_ID_MASK) {
13664 case PHY_ID_BCM5400: return "5400";
13665 case PHY_ID_BCM5401: return "5401";
13666 case PHY_ID_BCM5411: return "5411";
13667 case PHY_ID_BCM5701: return "5701";
13668 case PHY_ID_BCM5703: return "5703";
13669 case PHY_ID_BCM5704: return "5704";
13670 case PHY_ID_BCM5705: return "5705";
13671 case PHY_ID_BCM5750: return "5750";
Michael Chan85e94ce2005-04-21 17:05:28 -070013672 case PHY_ID_BCM5752: return "5752";
Michael Chana4e2b342005-10-26 15:46:52 -070013673 case PHY_ID_BCM5714: return "5714";
Michael Chan4cf78e42005-07-25 12:29:19 -070013674 case PHY_ID_BCM5780: return "5780";
Michael Chanaf36e6b2006-03-23 01:28:06 -080013675 case PHY_ID_BCM5755: return "5755";
Michael Chand9ab5ad12006-03-20 22:27:35 -080013676 case PHY_ID_BCM5787: return "5787";
Matt Carlsond30cdd22007-10-07 23:28:35 -070013677 case PHY_ID_BCM5784: return "5784";
Michael Chan126a3362006-09-27 16:03:07 -070013678 case PHY_ID_BCM5756: return "5722/5756";
Michael Chanb5d37722006-09-27 16:06:21 -070013679 case PHY_ID_BCM5906: return "5906";
Matt Carlson9936bcf2007-10-10 18:03:07 -070013680 case PHY_ID_BCM5761: return "5761";
Linus Torvalds1da177e2005-04-16 15:20:36 -070013681 case PHY_ID_BCM8002: return "8002/serdes";
13682 case 0: return "serdes";
13683 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070013684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013685}
13686
Michael Chanf9804dd2005-09-27 12:13:10 -070013687static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
13688{
13689 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
13690 strcpy(str, "PCI Express");
13691 return str;
13692 } else if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
13693 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
13694
13695 strcpy(str, "PCIX:");
13696
13697 if ((clock_ctrl == 7) ||
13698 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
13699 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
13700 strcat(str, "133MHz");
13701 else if (clock_ctrl == 0)
13702 strcat(str, "33MHz");
13703 else if (clock_ctrl == 2)
13704 strcat(str, "50MHz");
13705 else if (clock_ctrl == 4)
13706 strcat(str, "66MHz");
13707 else if (clock_ctrl == 6)
13708 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070013709 } else {
13710 strcpy(str, "PCI:");
13711 if (tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED)
13712 strcat(str, "66MHz");
13713 else
13714 strcat(str, "33MHz");
13715 }
13716 if (tp->tg3_flags & TG3_FLAG_PCI_32BIT)
13717 strcat(str, ":32-bit");
13718 else
13719 strcat(str, ":64-bit");
13720 return str;
13721}
13722
Michael Chan8c2dc7e2005-12-19 16:26:02 -080013723static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013724{
13725 struct pci_dev *peer;
13726 unsigned int func, devnr = tp->pdev->devfn & ~7;
13727
13728 for (func = 0; func < 8; func++) {
13729 peer = pci_get_slot(tp->pdev->bus, devnr | func);
13730 if (peer && peer != tp->pdev)
13731 break;
13732 pci_dev_put(peer);
13733 }
Michael Chan16fe9d72005-12-13 21:09:54 -080013734 /* 5704 can be configured in single-port mode, set peer to
13735 * tp->pdev in that case.
13736 */
13737 if (!peer) {
13738 peer = tp->pdev;
13739 return peer;
13740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013741
13742 /*
13743 * We don't need to keep the refcount elevated; there's no way
13744 * to remove one half of this device without removing the other
13745 */
13746 pci_dev_put(peer);
13747
13748 return peer;
13749}
13750
David S. Miller15f98502005-05-18 22:49:26 -070013751static void __devinit tg3_init_coal(struct tg3 *tp)
13752{
13753 struct ethtool_coalesce *ec = &tp->coal;
13754
13755 memset(ec, 0, sizeof(*ec));
13756 ec->cmd = ETHTOOL_GCOALESCE;
13757 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
13758 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
13759 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
13760 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
13761 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
13762 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
13763 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
13764 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
13765 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
13766
13767 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
13768 HOSTCC_MODE_CLRTICK_TXBD)) {
13769 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
13770 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
13771 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
13772 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
13773 }
Michael Chand244c892005-07-05 14:42:33 -070013774
13775 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
13776 ec->rx_coalesce_usecs_irq = 0;
13777 ec->tx_coalesce_usecs_irq = 0;
13778 ec->stats_block_coalesce_usecs = 0;
13779 }
David S. Miller15f98502005-05-18 22:49:26 -070013780}
13781
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080013782static const struct net_device_ops tg3_netdev_ops = {
13783 .ndo_open = tg3_open,
13784 .ndo_stop = tg3_close,
Stephen Hemminger00829822008-11-20 20:14:53 -080013785 .ndo_start_xmit = tg3_start_xmit,
13786 .ndo_get_stats = tg3_get_stats,
13787 .ndo_validate_addr = eth_validate_addr,
13788 .ndo_set_multicast_list = tg3_set_rx_mode,
13789 .ndo_set_mac_address = tg3_set_mac_addr,
13790 .ndo_do_ioctl = tg3_ioctl,
13791 .ndo_tx_timeout = tg3_tx_timeout,
13792 .ndo_change_mtu = tg3_change_mtu,
13793#if TG3_VLAN_TAG_USED
13794 .ndo_vlan_rx_register = tg3_vlan_rx_register,
13795#endif
13796#ifdef CONFIG_NET_POLL_CONTROLLER
13797 .ndo_poll_controller = tg3_poll_controller,
13798#endif
13799};
13800
13801static const struct net_device_ops tg3_netdev_ops_dma_bug = {
13802 .ndo_open = tg3_open,
13803 .ndo_stop = tg3_close,
13804 .ndo_start_xmit = tg3_start_xmit_dma_bug,
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080013805 .ndo_get_stats = tg3_get_stats,
13806 .ndo_validate_addr = eth_validate_addr,
13807 .ndo_set_multicast_list = tg3_set_rx_mode,
13808 .ndo_set_mac_address = tg3_set_mac_addr,
13809 .ndo_do_ioctl = tg3_ioctl,
13810 .ndo_tx_timeout = tg3_tx_timeout,
13811 .ndo_change_mtu = tg3_change_mtu,
13812#if TG3_VLAN_TAG_USED
13813 .ndo_vlan_rx_register = tg3_vlan_rx_register,
13814#endif
13815#ifdef CONFIG_NET_POLL_CONTROLLER
13816 .ndo_poll_controller = tg3_poll_controller,
13817#endif
13818};
13819
Linus Torvalds1da177e2005-04-16 15:20:36 -070013820static int __devinit tg3_init_one(struct pci_dev *pdev,
13821 const struct pci_device_id *ent)
13822{
13823 static int tg3_version_printed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013824 struct net_device *dev;
13825 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000013826 int i, err, pm_cap;
13827 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070013828 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080013829 u64 dma_mask, persist_dma_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013830
13831 if (tg3_version_printed++ == 0)
13832 printk(KERN_INFO "%s", version);
13833
13834 err = pci_enable_device(pdev);
13835 if (err) {
13836 printk(KERN_ERR PFX "Cannot enable PCI device, "
13837 "aborting.\n");
13838 return err;
13839 }
13840
Linus Torvalds1da177e2005-04-16 15:20:36 -070013841 err = pci_request_regions(pdev, DRV_MODULE_NAME);
13842 if (err) {
13843 printk(KERN_ERR PFX "Cannot obtain PCI resources, "
13844 "aborting.\n");
13845 goto err_out_disable_pdev;
13846 }
13847
13848 pci_set_master(pdev);
13849
13850 /* Find power-management capability. */
13851 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
13852 if (pm_cap == 0) {
13853 printk(KERN_ERR PFX "Cannot find PowerManagement capability, "
13854 "aborting.\n");
13855 err = -EIO;
13856 goto err_out_free_res;
13857 }
13858
Matt Carlsonfe5f5782009-09-01 13:09:39 +000013859 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013860 if (!dev) {
13861 printk(KERN_ERR PFX "Etherdev alloc failed, aborting.\n");
13862 err = -ENOMEM;
13863 goto err_out_free_res;
13864 }
13865
Linus Torvalds1da177e2005-04-16 15:20:36 -070013866 SET_NETDEV_DEV(dev, &pdev->dev);
13867
Linus Torvalds1da177e2005-04-16 15:20:36 -070013868#if TG3_VLAN_TAG_USED
13869 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013870#endif
13871
13872 tp = netdev_priv(dev);
13873 tp->pdev = pdev;
13874 tp->dev = dev;
13875 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013876 tp->rx_mode = TG3_DEF_RX_MODE;
13877 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070013878
Linus Torvalds1da177e2005-04-16 15:20:36 -070013879 if (tg3_debug > 0)
13880 tp->msg_enable = tg3_debug;
13881 else
13882 tp->msg_enable = TG3_DEF_MSG_ENABLE;
13883
13884 /* The word/byte swap controls here control register access byte
13885 * swapping. DMA data byte swapping is controlled in the GRC_MODE
13886 * setting below.
13887 */
13888 tp->misc_host_ctrl =
13889 MISC_HOST_CTRL_MASK_PCI_INT |
13890 MISC_HOST_CTRL_WORD_SWAP |
13891 MISC_HOST_CTRL_INDIR_ACCESS |
13892 MISC_HOST_CTRL_PCISTATE_RW;
13893
13894 /* The NONFRM (non-frame) byte/word swap controls take effect
13895 * on descriptor entries, anything which isn't packet data.
13896 *
13897 * The StrongARM chips on the board (one for tx, one for rx)
13898 * are running in big-endian mode.
13899 */
13900 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
13901 GRC_MODE_WSWAP_NONFRM_DATA);
13902#ifdef __BIG_ENDIAN
13903 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
13904#endif
13905 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013906 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000013907 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013908
Matt Carlsond5fe4882008-11-21 17:20:32 -080013909 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010013910 if (!tp->regs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013911 printk(KERN_ERR PFX "Cannot map device registers, "
13912 "aborting.\n");
13913 err = -ENOMEM;
13914 goto err_out_free_dev;
13915 }
13916
13917 tg3_init_link_config(tp);
13918
Linus Torvalds1da177e2005-04-16 15:20:36 -070013919 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
13920 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013921
Matt Carlson646c9ed2009-09-01 12:58:41 +000013922 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
13923 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
13924 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
13925 for (i = 0; i < TG3_IRQ_MAX_VECS; i++) {
13926 struct tg3_napi *tnapi = &tp->napi[i];
13927
13928 tnapi->tp = tp;
13929 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
13930
13931 tnapi->int_mbox = intmbx;
13932 if (i < 4)
13933 intmbx += 0x8;
13934 else
13935 intmbx += 0x4;
13936
13937 tnapi->consmbox = rcvmbx;
13938 tnapi->prodmbox = sndmbx;
13939
13940 if (i)
13941 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
13942 else
13943 tnapi->coal_now = HOSTCC_MODE_NOW;
13944
13945 if (!(tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX))
13946 break;
13947
13948 /*
13949 * If we support MSIX, we'll be using RSS. If we're using
13950 * RSS, the first vector only handles link interrupts and the
13951 * remaining vectors handle rx and tx interrupts. Reuse the
13952 * mailbox values for the next iteration. The values we setup
13953 * above are still useful for the single vectored mode.
13954 */
13955 if (!i)
13956 continue;
13957
13958 rcvmbx += 0x8;
13959
13960 if (sndmbx & 0x4)
13961 sndmbx -= 0x4;
13962 else
13963 sndmbx += 0xc;
13964 }
13965
Matt Carlson8ef04422009-08-28 14:01:37 +000013966 netif_napi_add(dev, &tp->napi[0].napi, tg3_poll, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013967 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013968 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013969 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013970
13971 err = tg3_get_invariants(tp);
13972 if (err) {
13973 printk(KERN_ERR PFX "Problem fetching invariants of chip, "
13974 "aborting.\n");
13975 goto err_out_iounmap;
13976 }
13977
Matt Carlson321d32a2008-11-21 17:22:19 -080013978 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
Stephen Hemminger00829822008-11-20 20:14:53 -080013979 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
13980 dev->netdev_ops = &tg3_netdev_ops;
13981 else
13982 dev->netdev_ops = &tg3_netdev_ops_dma_bug;
13983
13984
Michael Chan4a29cc22006-03-19 13:21:12 -080013985 /* The EPB bridge inside 5714, 5715, and 5780 and any
13986 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080013987 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
13988 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
13989 * do DMA address check in tg3_start_xmit().
13990 */
Michael Chan4a29cc22006-03-19 13:21:12 -080013991 if (tp->tg3_flags2 & TG3_FLG2_IS_5788)
Yang Hongyang284901a2009-04-06 19:01:15 -070013992 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Michael Chan4a29cc22006-03-19 13:21:12 -080013993 else if (tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070013994 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080013995#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070013996 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080013997#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080013998 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070013999 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080014000
14001 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070014002 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080014003 err = pci_set_dma_mask(pdev, dma_mask);
14004 if (!err) {
14005 dev->features |= NETIF_F_HIGHDMA;
14006 err = pci_set_consistent_dma_mask(pdev,
14007 persist_dma_mask);
14008 if (err < 0) {
14009 printk(KERN_ERR PFX "Unable to obtain 64 bit "
14010 "DMA for consistent allocations\n");
14011 goto err_out_iounmap;
14012 }
14013 }
14014 }
Yang Hongyang284901a2009-04-06 19:01:15 -070014015 if (err || dma_mask == DMA_BIT_MASK(32)) {
14016 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080014017 if (err) {
14018 printk(KERN_ERR PFX "No usable DMA configuration, "
14019 "aborting.\n");
14020 goto err_out_iounmap;
14021 }
14022 }
14023
Michael Chanfdfec1722005-07-25 12:31:48 -070014024 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014025
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014026 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
Matt Carlson9e9fd122009-01-19 16:57:45 -080014027 tp->fw_needed = FIRMWARE_TG3;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014028
Linus Torvalds1da177e2005-04-16 15:20:36 -070014029 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO) {
14030 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
14031 }
14032 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14033 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
14034 tp->pci_chip_rev_id == CHIPREV_ID_5705_A0 ||
Michael Chanc7835a72006-11-15 21:14:42 -080014035 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -070014036 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0) {
14037 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
14038 } else {
Michael Chan7f62ad52007-02-20 23:25:40 -080014039 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE | TG3_FLG2_TSO_BUG;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014040 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
Matt Carlson9e9fd122009-01-19 16:57:45 -080014041 tp->fw_needed = FIRMWARE_TG3TSO5;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014042 else
Matt Carlson9e9fd122009-01-19 16:57:45 -080014043 tp->fw_needed = FIRMWARE_TG3TSO;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014044 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014045
Michael Chan4e3a7aa2006-03-20 17:47:44 -080014046 /* TSO is on by default on chips that support hardware TSO.
14047 * Firmware TSO on older chips gives lower performance, so it
14048 * is off by default, but can be enabled using ethtool.
14049 */
Michael Chanb0026622006-07-03 19:42:14 -070014050 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO) {
Matt Carlson027455a2008-12-21 20:19:30 -080014051 if (dev->features & NETIF_F_IP_CSUM)
14052 dev->features |= NETIF_F_TSO;
14053 if ((dev->features & NETIF_F_IPV6_CSUM) &&
14054 (tp->tg3_flags2 & TG3_FLG2_HW_TSO_2))
Michael Chanb0026622006-07-03 19:42:14 -070014055 dev->features |= NETIF_F_TSO6;
Matt Carlson57e69832008-05-25 23:48:31 -070014056 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
14057 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
14058 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014059 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014060 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
14061 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson9936bcf2007-10-10 18:03:07 -070014062 dev->features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070014063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014064
Linus Torvalds1da177e2005-04-16 15:20:36 -070014065
14066 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
14067 !(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) &&
14068 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
14069 tp->tg3_flags2 |= TG3_FLG2_MAX_RXPEND_64;
14070 tp->rx_pending = 63;
14071 }
14072
Linus Torvalds1da177e2005-04-16 15:20:36 -070014073 err = tg3_get_device_address(tp);
14074 if (err) {
14075 printk(KERN_ERR PFX "Could not obtain valid ethernet address, "
14076 "aborting.\n");
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014077 goto err_out_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014078 }
14079
Matt Carlson0d3031d2007-10-10 18:02:43 -070014080 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
Matt Carlson63532392008-11-03 16:49:57 -080014081 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
Al Viro79ea13c2008-01-24 02:06:46 -080014082 if (!tp->aperegs) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070014083 printk(KERN_ERR PFX "Cannot map APE registers, "
14084 "aborting.\n");
14085 err = -ENOMEM;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014086 goto err_out_fw;
Matt Carlson0d3031d2007-10-10 18:02:43 -070014087 }
14088
14089 tg3_ape_lock_init(tp);
Matt Carlson7fd76442009-02-25 14:27:20 +000014090
14091 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF)
14092 tg3_read_dash_ver(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070014093 }
14094
Matt Carlsonc88864d2007-11-12 21:07:01 -080014095 /*
14096 * Reset chip in case UNDI or EFI driver did not shutdown
14097 * DMA self test will enable WDMAC and we'll see (spurious)
14098 * pending DMA on the PCI bus at that point.
14099 */
14100 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
14101 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
14102 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
14103 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
14104 }
14105
14106 err = tg3_test_dma(tp);
14107 if (err) {
14108 printk(KERN_ERR PFX "DMA engine test failed, aborting.\n");
14109 goto err_out_apeunmap;
14110 }
14111
Matt Carlsonc88864d2007-11-12 21:07:01 -080014112 /* flow control autonegotiation is default behavior */
14113 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
Steve Glendinninge18ce342008-12-16 02:00:00 -080014114 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlsonc88864d2007-11-12 21:07:01 -080014115
14116 tg3_init_coal(tp);
14117
Michael Chanc49a1562006-12-17 17:07:29 -080014118 pci_set_drvdata(pdev, dev);
14119
Linus Torvalds1da177e2005-04-16 15:20:36 -070014120 err = register_netdev(dev);
14121 if (err) {
14122 printk(KERN_ERR PFX "Cannot register net device, "
14123 "aborting.\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070014124 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014125 }
14126
Matt Carlsondf59c942008-11-03 16:52:56 -080014127 printk(KERN_INFO "%s: Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070014128 dev->name,
14129 tp->board_part_number,
14130 tp->pci_chip_rev_id,
Michael Chanf9804dd2005-09-27 12:13:10 -070014131 tg3_bus_string(tp, str),
Johannes Berge1749612008-10-27 15:59:26 -070014132 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014133
Matt Carlsondf59c942008-11-03 16:52:56 -080014134 if (tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED)
14135 printk(KERN_INFO
14136 "%s: attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
14137 tp->dev->name,
14138 tp->mdio_bus->phy_map[PHY_ADDR]->drv->name,
Kay Sieversfb28ad32008-11-10 13:55:14 -080014139 dev_name(&tp->mdio_bus->phy_map[PHY_ADDR]->dev));
Matt Carlsondf59c942008-11-03 16:52:56 -080014140 else
14141 printk(KERN_INFO
14142 "%s: attached PHY is %s (%s Ethernet) (WireSpeed[%d])\n",
14143 tp->dev->name, tg3_phy_string(tp),
14144 ((tp->tg3_flags & TG3_FLAG_10_100_ONLY) ? "10/100Base-TX" :
14145 ((tp->tg3_flags2 & TG3_FLG2_ANY_SERDES) ? "1000Base-SX" :
14146 "10/100/1000Base-T")),
14147 (tp->tg3_flags2 & TG3_FLG2_NO_ETH_WIRE_SPEED) == 0);
14148
14149 printk(KERN_INFO "%s: RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070014150 dev->name,
14151 (tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) != 0,
14152 (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) != 0,
14153 (tp->tg3_flags & TG3_FLAG_USE_MI_INTERRUPT) != 0,
14154 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014155 (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) != 0);
Michael Chan4a29cc22006-03-19 13:21:12 -080014156 printk(KERN_INFO "%s: dma_rwctrl[%08x] dma_mask[%d-bit]\n",
14157 dev->name, tp->dma_rwctrl,
Yang Hongyang284901a2009-04-06 19:01:15 -070014158 (pdev->dma_mask == DMA_BIT_MASK(32)) ? 32 :
Yang Hongyang50cf1562009-04-06 19:01:14 -070014159 (((u64) pdev->dma_mask == DMA_BIT_MASK(40)) ? 40 : 64));
Linus Torvalds1da177e2005-04-16 15:20:36 -070014160
14161 return 0;
14162
Matt Carlson0d3031d2007-10-10 18:02:43 -070014163err_out_apeunmap:
14164 if (tp->aperegs) {
14165 iounmap(tp->aperegs);
14166 tp->aperegs = NULL;
14167 }
14168
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014169err_out_fw:
14170 if (tp->fw)
14171 release_firmware(tp->fw);
14172
Linus Torvalds1da177e2005-04-16 15:20:36 -070014173err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070014174 if (tp->regs) {
14175 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070014176 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070014177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014178
14179err_out_free_dev:
14180 free_netdev(dev);
14181
14182err_out_free_res:
14183 pci_release_regions(pdev);
14184
14185err_out_disable_pdev:
14186 pci_disable_device(pdev);
14187 pci_set_drvdata(pdev, NULL);
14188 return err;
14189}
14190
14191static void __devexit tg3_remove_one(struct pci_dev *pdev)
14192{
14193 struct net_device *dev = pci_get_drvdata(pdev);
14194
14195 if (dev) {
14196 struct tg3 *tp = netdev_priv(dev);
14197
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014198 if (tp->fw)
14199 release_firmware(tp->fw);
14200
Michael Chan7faa0062006-02-02 17:29:28 -080014201 flush_scheduled_work();
Matt Carlson158d7ab2008-05-29 01:37:54 -070014202
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014203 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
14204 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070014205 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014206 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070014207
Linus Torvalds1da177e2005-04-16 15:20:36 -070014208 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070014209 if (tp->aperegs) {
14210 iounmap(tp->aperegs);
14211 tp->aperegs = NULL;
14212 }
Michael Chan68929142005-08-09 20:17:14 -070014213 if (tp->regs) {
14214 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070014215 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070014216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014217 free_netdev(dev);
14218 pci_release_regions(pdev);
14219 pci_disable_device(pdev);
14220 pci_set_drvdata(pdev, NULL);
14221 }
14222}
14223
14224static int tg3_suspend(struct pci_dev *pdev, pm_message_t state)
14225{
14226 struct net_device *dev = pci_get_drvdata(pdev);
14227 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070014228 pci_power_t target_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014229 int err;
14230
Michael Chan3e0c95f2007-08-03 20:56:54 -070014231 /* PCI register 4 needs to be saved whether netif_running() or not.
14232 * MSI address and data need to be saved if using MSI and
14233 * netif_running().
14234 */
14235 pci_save_state(pdev);
14236
Linus Torvalds1da177e2005-04-16 15:20:36 -070014237 if (!netif_running(dev))
14238 return 0;
14239
Michael Chan7faa0062006-02-02 17:29:28 -080014240 flush_scheduled_work();
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014241 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014242 tg3_netif_stop(tp);
14243
14244 del_timer_sync(&tp->timer);
14245
David S. Millerf47c11e2005-06-24 20:18:35 -070014246 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014247 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070014248 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014249
14250 netif_device_detach(dev);
14251
David S. Millerf47c11e2005-06-24 20:18:35 -070014252 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070014253 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan6a9eba12005-12-13 21:08:58 -080014254 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
David S. Millerf47c11e2005-06-24 20:18:35 -070014255 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014256
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070014257 target_state = pdev->pm_cap ? pci_target_state(pdev) : PCI_D3hot;
14258
14259 err = tg3_set_power_state(tp, target_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014260 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014261 int err2;
14262
David S. Millerf47c11e2005-06-24 20:18:35 -070014263 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014264
Michael Chan6a9eba12005-12-13 21:08:58 -080014265 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014266 err2 = tg3_restart_hw(tp, 1);
14267 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070014268 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014269
14270 tp->timer.expires = jiffies + tp->timer_offset;
14271 add_timer(&tp->timer);
14272
14273 netif_device_attach(dev);
14274 tg3_netif_start(tp);
14275
Michael Chanb9ec6c12006-07-25 16:37:27 -070014276out:
David S. Millerf47c11e2005-06-24 20:18:35 -070014277 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014278
14279 if (!err2)
14280 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014281 }
14282
14283 return err;
14284}
14285
14286static int tg3_resume(struct pci_dev *pdev)
14287{
14288 struct net_device *dev = pci_get_drvdata(pdev);
14289 struct tg3 *tp = netdev_priv(dev);
14290 int err;
14291
Michael Chan3e0c95f2007-08-03 20:56:54 -070014292 pci_restore_state(tp->pdev);
14293
Linus Torvalds1da177e2005-04-16 15:20:36 -070014294 if (!netif_running(dev))
14295 return 0;
14296
Michael Chanbc1c7562006-03-20 17:48:03 -080014297 err = tg3_set_power_state(tp, PCI_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014298 if (err)
14299 return err;
14300
14301 netif_device_attach(dev);
14302
David S. Millerf47c11e2005-06-24 20:18:35 -070014303 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014304
Michael Chan6a9eba12005-12-13 21:08:58 -080014305 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Michael Chanb9ec6c12006-07-25 16:37:27 -070014306 err = tg3_restart_hw(tp, 1);
14307 if (err)
14308 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014309
14310 tp->timer.expires = jiffies + tp->timer_offset;
14311 add_timer(&tp->timer);
14312
Linus Torvalds1da177e2005-04-16 15:20:36 -070014313 tg3_netif_start(tp);
14314
Michael Chanb9ec6c12006-07-25 16:37:27 -070014315out:
David S. Millerf47c11e2005-06-24 20:18:35 -070014316 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014317
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014318 if (!err)
14319 tg3_phy_start(tp);
14320
Michael Chanb9ec6c12006-07-25 16:37:27 -070014321 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014322}
14323
14324static struct pci_driver tg3_driver = {
14325 .name = DRV_MODULE_NAME,
14326 .id_table = tg3_pci_tbl,
14327 .probe = tg3_init_one,
14328 .remove = __devexit_p(tg3_remove_one),
14329 .suspend = tg3_suspend,
14330 .resume = tg3_resume
14331};
14332
14333static int __init tg3_init(void)
14334{
Jeff Garzik29917622006-08-19 17:48:59 -040014335 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014336}
14337
14338static void __exit tg3_cleanup(void)
14339{
14340 pci_unregister_driver(&tg3_driver);
14341}
14342
14343module_init(tg3_init);
14344module_exit(tg3_cleanup);