blob: 54dbe988f5f1dcd585bbb5351047a2c6280a98e8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * tg3.c: Broadcom Tigon3 ethernet driver.
3 *
4 * Copyright (C) 2001, 2002, 2003, 2004 David S. Miller (davem@redhat.com)
5 * Copyright (C) 2001, 2002, 2003 Jeff Garzik (jgarzik@pobox.com)
6 * Copyright (C) 2004 Sun Microsystems Inc.
Matt Carlson0d2a5062009-02-25 14:40:42 +00007 * Copyright (C) 2005-2009 Broadcom Corporation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * Firmware is:
Michael Chan49cabf42005-06-06 15:15:17 -070010 * Derived from proprietary unpublished source code,
11 * Copyright (C) 2000-2003 Broadcom Corporation.
12 *
13 * Permission is hereby granted for the distribution of this firmware
14 * data in hexadecimal or equivalent format, provided this copyright
15 * notice is accompanying it.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 */
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#include <linux/module.h>
20#include <linux/moduleparam.h>
21#include <linux/kernel.h>
22#include <linux/types.h>
23#include <linux/compiler.h>
24#include <linux/slab.h>
25#include <linux/delay.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020026#include <linux/in.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/init.h>
28#include <linux/ioport.h>
29#include <linux/pci.h>
30#include <linux/netdevice.h>
31#include <linux/etherdevice.h>
32#include <linux/skbuff.h>
33#include <linux/ethtool.h>
34#include <linux/mii.h>
Matt Carlson158d7ab2008-05-29 01:37:54 -070035#include <linux/phy.h>
Matt Carlsona9daf362008-05-25 23:49:44 -070036#include <linux/brcmphy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/if_vlan.h>
38#include <linux/ip.h>
39#include <linux/tcp.h>
40#include <linux/workqueue.h>
Michael Chan61487482005-09-05 17:53:19 -070041#include <linux/prefetch.h>
Tobias Klauserf9a5f7d2005-10-29 15:09:26 +020042#include <linux/dma-mapping.h>
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080043#include <linux/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45#include <net/checksum.h>
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030046#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#include <asm/system.h>
49#include <asm/io.h>
50#include <asm/byteorder.h>
51#include <asm/uaccess.h>
52
David S. Miller49b6e95f2007-03-29 01:38:42 -070053#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <asm/idprom.h>
David S. Miller49b6e95f2007-03-29 01:38:42 -070055#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#endif
57
Matt Carlson63532392008-11-03 16:49:57 -080058#define BAR_0 0
59#define BAR_2 2
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
62#define TG3_VLAN_TAG_USED 1
63#else
64#define TG3_VLAN_TAG_USED 0
65#endif
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include "tg3.h"
68
69#define DRV_MODULE_NAME "tg3"
70#define PFX DRV_MODULE_NAME ": "
Matt Carlsondaf09de2009-09-01 13:22:42 +000071#define DRV_MODULE_VERSION "3.102"
72#define DRV_MODULE_RELDATE "September 1, 2009"
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74#define TG3_DEF_MAC_MODE 0
75#define TG3_DEF_RX_MODE 0
76#define TG3_DEF_TX_MODE 0
77#define TG3_DEF_MSG_ENABLE \
78 (NETIF_MSG_DRV | \
79 NETIF_MSG_PROBE | \
80 NETIF_MSG_LINK | \
81 NETIF_MSG_TIMER | \
82 NETIF_MSG_IFDOWN | \
83 NETIF_MSG_IFUP | \
84 NETIF_MSG_RX_ERR | \
85 NETIF_MSG_TX_ERR)
86
87/* length of time before we decide the hardware is borked,
88 * and dev->tx_timeout() should be called to fix the problem
89 */
90#define TG3_TX_TIMEOUT (5 * HZ)
91
92/* hardware minimum and maximum for a single frame's data payload */
93#define TG3_MIN_MTU 60
94#define TG3_MAX_MTU(tp) \
Matt Carlson8f666b02009-08-28 13:58:24 +000095 ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) ? 9000 : 1500)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97/* These numbers seem to be hard coded in the NIC firmware somehow.
98 * You can't change the ring sizes, but you can change where you place
99 * them in the NIC onboard memory.
100 */
101#define TG3_RX_RING_SIZE 512
102#define TG3_DEF_RX_RING_PENDING 200
103#define TG3_RX_JUMBO_RING_SIZE 256
104#define TG3_DEF_RX_JUMBO_RING_PENDING 100
Matt Carlsonbaf8a942009-09-01 13:13:00 +0000105#define TG3_RSS_INDIR_TBL_SIZE 128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107/* Do not place this n-ring entries value into the tp struct itself,
108 * we really want to expose these constants to GCC so that modulo et
109 * al. operations are done with shifts and masks instead of with
110 * hw multiply/modulo instructions. Another solution would be to
111 * replace things like '% foo' with '& (foo - 1)'.
112 */
113#define TG3_RX_RCB_RING_SIZE(tp) \
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000114 (((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) && \
Matt Carlson5ea1c502009-09-11 16:50:16 -0700115 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) ? 1024 : 512)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117#define TG3_TX_RING_SIZE 512
118#define TG3_DEF_TX_RING_PENDING (TG3_TX_RING_SIZE - 1)
119
120#define TG3_RX_RING_BYTES (sizeof(struct tg3_rx_buffer_desc) * \
121 TG3_RX_RING_SIZE)
Matt Carlson79ed5ac2009-08-28 14:00:55 +0000122#define TG3_RX_JUMBO_RING_BYTES (sizeof(struct tg3_ext_rx_buffer_desc) * \
123 TG3_RX_JUMBO_RING_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124#define TG3_RX_RCB_RING_BYTES(tp) (sizeof(struct tg3_rx_buffer_desc) * \
Matt Carlson79ed5ac2009-08-28 14:00:55 +0000125 TG3_RX_RCB_RING_SIZE(tp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126#define TG3_TX_RING_BYTES (sizeof(struct tg3_tx_buffer_desc) * \
127 TG3_TX_RING_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128#define NEXT_TX(N) (((N) + 1) & (TG3_TX_RING_SIZE - 1))
129
Matt Carlson287be122009-08-28 13:58:46 +0000130#define TG3_DMA_BYTE_ENAB 64
131
132#define TG3_RX_STD_DMA_SZ 1536
133#define TG3_RX_JMB_DMA_SZ 9046
134
135#define TG3_RX_DMA_TO_MAP_SZ(x) ((x) + TG3_DMA_BYTE_ENAB)
136
137#define TG3_RX_STD_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_STD_DMA_SZ)
138#define TG3_RX_JMB_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_JMB_DMA_SZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140/* minimum number of free TX descriptors required to wake up TX process */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000141#define TG3_TX_WAKEUP_THRESH(tnapi) ((tnapi)->tx_pending / 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Matt Carlsonad829262008-11-21 17:16:16 -0800143#define TG3_RAW_IP_ALIGN 2
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145/* number of ETHTOOL_GSTATS u64's */
146#define TG3_NUM_STATS (sizeof(struct tg3_ethtool_stats)/sizeof(u64))
147
Michael Chan4cafd3f2005-05-29 14:56:34 -0700148#define TG3_NUM_TEST 6
149
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800150#define FIRMWARE_TG3 "tigon/tg3.bin"
151#define FIRMWARE_TG3TSO "tigon/tg3_tso.bin"
152#define FIRMWARE_TG3TSO5 "tigon/tg3_tso5.bin"
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154static char version[] __devinitdata =
155 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
156
157MODULE_AUTHOR("David S. Miller (davem@redhat.com) and Jeff Garzik (jgarzik@pobox.com)");
158MODULE_DESCRIPTION("Broadcom Tigon3 ethernet driver");
159MODULE_LICENSE("GPL");
160MODULE_VERSION(DRV_MODULE_VERSION);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800161MODULE_FIRMWARE(FIRMWARE_TG3);
162MODULE_FIRMWARE(FIRMWARE_TG3TSO);
163MODULE_FIRMWARE(FIRMWARE_TG3TSO5);
164
Matt Carlson679563f2009-09-01 12:55:46 +0000165#define TG3_RSS_MIN_NUM_MSIX_VECS 2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167static int tg3_debug = -1; /* -1 == use TG3_DEF_MSG_ENABLE as value */
168module_param(tg3_debug, int, 0);
169MODULE_PARM_DESC(tg3_debug, "Tigon3 bitmapped debugging message enable value");
170
171static struct pci_device_id tg3_pci_tbl[] = {
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700172 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5700)},
173 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5701)},
174 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702)},
175 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703)},
176 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704)},
177 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702FE)},
178 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705)},
179 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705_2)},
180 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M)},
181 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M_2)},
182 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702X)},
183 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703X)},
184 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S)},
185 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702A3)},
186 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703A3)},
187 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5782)},
188 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5788)},
189 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5789)},
190 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901)},
191 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901_2)},
192 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S_2)},
193 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705F)},
194 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5720)},
195 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5721)},
Michael Chan126a3362006-09-27 16:03:07 -0700196 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5722)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700197 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5750)},
198 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751)},
199 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5750M)},
200 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751M)},
201 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751F)},
202 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752)},
203 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752M)},
204 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753)},
205 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753M)},
206 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753F)},
207 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754)},
208 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754M)},
209 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755)},
210 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755M)},
Michael Chan126a3362006-09-27 16:03:07 -0700211 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5756)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700212 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5786)},
213 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787)},
214 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787M)},
Michael Chan676917d2006-12-07 00:20:22 -0800215 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787F)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700216 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714)},
217 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714S)},
218 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715)},
219 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715S)},
220 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780)},
221 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780S)},
222 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5781)},
Michael Chanb5d37722006-09-27 16:06:21 -0700223 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906)},
224 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906M)},
Matt Carlsond30cdd22007-10-07 23:28:35 -0700225 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5784)},
226 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5764)},
Matt Carlson6c7af272007-10-21 16:12:02 -0700227 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5723)},
Matt Carlson9936bcf2007-10-10 18:03:07 -0700228 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761)},
229 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761E)},
Matt Carlsonc88e6682008-11-03 16:49:18 -0800230 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761S)},
231 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761SE)},
Matt Carlson2befdce2009-08-28 12:28:45 +0000232 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_G)},
233 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_F)},
Matt Carlson321d32a2008-11-21 17:22:19 -0800234 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780)},
235 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57760)},
236 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57790)},
Matt Carlson5e7ccf22009-08-25 10:08:42 +0000237 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57788)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700238 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9DXX)},
239 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9MXX)},
240 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1000)},
241 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1001)},
242 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1003)},
243 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC9100)},
244 {PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_TIGON3)},
245 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246};
247
248MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
249
Andreas Mohr50da8592006-08-14 23:54:30 -0700250static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 const char string[ETH_GSTRING_LEN];
252} ethtool_stats_keys[TG3_NUM_STATS] = {
253 { "rx_octets" },
254 { "rx_fragments" },
255 { "rx_ucast_packets" },
256 { "rx_mcast_packets" },
257 { "rx_bcast_packets" },
258 { "rx_fcs_errors" },
259 { "rx_align_errors" },
260 { "rx_xon_pause_rcvd" },
261 { "rx_xoff_pause_rcvd" },
262 { "rx_mac_ctrl_rcvd" },
263 { "rx_xoff_entered" },
264 { "rx_frame_too_long_errors" },
265 { "rx_jabbers" },
266 { "rx_undersize_packets" },
267 { "rx_in_length_errors" },
268 { "rx_out_length_errors" },
269 { "rx_64_or_less_octet_packets" },
270 { "rx_65_to_127_octet_packets" },
271 { "rx_128_to_255_octet_packets" },
272 { "rx_256_to_511_octet_packets" },
273 { "rx_512_to_1023_octet_packets" },
274 { "rx_1024_to_1522_octet_packets" },
275 { "rx_1523_to_2047_octet_packets" },
276 { "rx_2048_to_4095_octet_packets" },
277 { "rx_4096_to_8191_octet_packets" },
278 { "rx_8192_to_9022_octet_packets" },
279
280 { "tx_octets" },
281 { "tx_collisions" },
282
283 { "tx_xon_sent" },
284 { "tx_xoff_sent" },
285 { "tx_flow_control" },
286 { "tx_mac_errors" },
287 { "tx_single_collisions" },
288 { "tx_mult_collisions" },
289 { "tx_deferred" },
290 { "tx_excessive_collisions" },
291 { "tx_late_collisions" },
292 { "tx_collide_2times" },
293 { "tx_collide_3times" },
294 { "tx_collide_4times" },
295 { "tx_collide_5times" },
296 { "tx_collide_6times" },
297 { "tx_collide_7times" },
298 { "tx_collide_8times" },
299 { "tx_collide_9times" },
300 { "tx_collide_10times" },
301 { "tx_collide_11times" },
302 { "tx_collide_12times" },
303 { "tx_collide_13times" },
304 { "tx_collide_14times" },
305 { "tx_collide_15times" },
306 { "tx_ucast_packets" },
307 { "tx_mcast_packets" },
308 { "tx_bcast_packets" },
309 { "tx_carrier_sense_errors" },
310 { "tx_discards" },
311 { "tx_errors" },
312
313 { "dma_writeq_full" },
314 { "dma_write_prioq_full" },
315 { "rxbds_empty" },
316 { "rx_discards" },
317 { "rx_errors" },
318 { "rx_threshold_hit" },
319
320 { "dma_readq_full" },
321 { "dma_read_prioq_full" },
322 { "tx_comp_queue_full" },
323
324 { "ring_set_send_prod_index" },
325 { "ring_status_update" },
326 { "nic_irqs" },
327 { "nic_avoided_irqs" },
328 { "nic_tx_threshold_hit" }
329};
330
Andreas Mohr50da8592006-08-14 23:54:30 -0700331static const struct {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700332 const char string[ETH_GSTRING_LEN];
333} ethtool_test_keys[TG3_NUM_TEST] = {
334 { "nvram test (online) " },
335 { "link test (online) " },
336 { "register test (offline)" },
337 { "memory test (offline)" },
338 { "loopback test (offline)" },
339 { "interrupt test (offline)" },
340};
341
Michael Chanb401e9e2005-12-19 16:27:04 -0800342static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
343{
344 writel(val, tp->regs + off);
345}
346
347static u32 tg3_read32(struct tg3 *tp, u32 off)
348{
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400349 return (readl(tp->regs + off));
Michael Chanb401e9e2005-12-19 16:27:04 -0800350}
351
Matt Carlson0d3031d2007-10-10 18:02:43 -0700352static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
353{
354 writel(val, tp->aperegs + off);
355}
356
357static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
358{
359 return (readl(tp->aperegs + off));
360}
361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
363{
Michael Chan68929142005-08-09 20:17:14 -0700364 unsigned long flags;
365
366 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700367 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
368 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
Michael Chan68929142005-08-09 20:17:14 -0700369 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700370}
371
372static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
373{
374 writel(val, tp->regs + off);
375 readl(tp->regs + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376}
377
Michael Chan68929142005-08-09 20:17:14 -0700378static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
379{
380 unsigned long flags;
381 u32 val;
382
383 spin_lock_irqsave(&tp->indirect_lock, flags);
384 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
385 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
386 spin_unlock_irqrestore(&tp->indirect_lock, flags);
387 return val;
388}
389
390static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
391{
392 unsigned long flags;
393
394 if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
395 pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
396 TG3_64BIT_REG_LOW, val);
397 return;
398 }
399 if (off == (MAILBOX_RCV_STD_PROD_IDX + TG3_64BIT_REG_LOW)) {
400 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
401 TG3_64BIT_REG_LOW, val);
402 return;
403 }
404
405 spin_lock_irqsave(&tp->indirect_lock, flags);
406 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
407 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
408 spin_unlock_irqrestore(&tp->indirect_lock, flags);
409
410 /* In indirect mode when disabling interrupts, we also need
411 * to clear the interrupt bit in the GRC local ctrl register.
412 */
413 if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
414 (val == 0x1)) {
415 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
416 tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
417 }
418}
419
420static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
421{
422 unsigned long flags;
423 u32 val;
424
425 spin_lock_irqsave(&tp->indirect_lock, flags);
426 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
427 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
428 spin_unlock_irqrestore(&tp->indirect_lock, flags);
429 return val;
430}
431
Michael Chanb401e9e2005-12-19 16:27:04 -0800432/* usec_wait specifies the wait time in usec when writing to certain registers
433 * where it is unsafe to read back the register without some delay.
434 * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
435 * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
436 */
437static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Michael Chanb401e9e2005-12-19 16:27:04 -0800439 if ((tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG) ||
440 (tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND))
441 /* Non-posted methods */
442 tp->write32(tp, off, val);
443 else {
444 /* Posted method */
445 tg3_write32(tp, off, val);
446 if (usec_wait)
447 udelay(usec_wait);
448 tp->read32(tp, off);
449 }
450 /* Wait again after the read for the posted method to guarantee that
451 * the wait time is met.
452 */
453 if (usec_wait)
454 udelay(usec_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
Michael Chan09ee9292005-08-09 20:17:00 -0700457static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
458{
459 tp->write32_mbox(tp, off, val);
Michael Chan68929142005-08-09 20:17:14 -0700460 if (!(tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER) &&
461 !(tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND))
462 tp->read32_mbox(tp, off);
Michael Chan09ee9292005-08-09 20:17:00 -0700463}
464
Michael Chan20094932005-08-09 20:16:32 -0700465static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
467 void __iomem *mbox = tp->regs + off;
468 writel(val, mbox);
469 if (tp->tg3_flags & TG3_FLAG_TXD_MBOX_HWBUG)
470 writel(val, mbox);
471 if (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)
472 readl(mbox);
473}
474
Michael Chanb5d37722006-09-27 16:06:21 -0700475static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
476{
477 return (readl(tp->regs + off + GRCMBOX_BASE));
478}
479
480static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
481{
482 writel(val, tp->regs + off + GRCMBOX_BASE);
483}
484
Michael Chan20094932005-08-09 20:16:32 -0700485#define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700486#define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
Michael Chan20094932005-08-09 20:16:32 -0700487#define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
488#define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700489#define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
Michael Chan20094932005-08-09 20:16:32 -0700490
491#define tw32(reg,val) tp->write32(tp, reg, val)
Michael Chanb401e9e2005-12-19 16:27:04 -0800492#define tw32_f(reg,val) _tw32_flush(tp,(reg),(val), 0)
493#define tw32_wait_f(reg,val,us) _tw32_flush(tp,(reg),(val), (us))
Michael Chan20094932005-08-09 20:16:32 -0700494#define tr32(reg) tp->read32(tp, reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
497{
Michael Chan68929142005-08-09 20:17:14 -0700498 unsigned long flags;
499
Michael Chanb5d37722006-09-27 16:06:21 -0700500 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) &&
501 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
502 return;
503
Michael Chan68929142005-08-09 20:17:14 -0700504 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chanbbadf502006-04-06 21:46:34 -0700505 if (tp->tg3_flags & TG3_FLAG_SRAM_USE_CONFIG) {
506 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
507 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Michael Chanbbadf502006-04-06 21:46:34 -0700509 /* Always leave this as zero. */
510 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
511 } else {
512 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
513 tw32_f(TG3PCI_MEM_WIN_DATA, val);
514
515 /* Always leave this as zero. */
516 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
517 }
Michael Chan68929142005-08-09 20:17:14 -0700518 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519}
520
521static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
522{
Michael Chan68929142005-08-09 20:17:14 -0700523 unsigned long flags;
524
Michael Chanb5d37722006-09-27 16:06:21 -0700525 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) &&
526 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
527 *val = 0;
528 return;
529 }
530
Michael Chan68929142005-08-09 20:17:14 -0700531 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chanbbadf502006-04-06 21:46:34 -0700532 if (tp->tg3_flags & TG3_FLAG_SRAM_USE_CONFIG) {
533 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
534 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Michael Chanbbadf502006-04-06 21:46:34 -0700536 /* Always leave this as zero. */
537 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
538 } else {
539 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
540 *val = tr32(TG3PCI_MEM_WIN_DATA);
541
542 /* Always leave this as zero. */
543 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
544 }
Michael Chan68929142005-08-09 20:17:14 -0700545 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546}
547
Matt Carlson0d3031d2007-10-10 18:02:43 -0700548static void tg3_ape_lock_init(struct tg3 *tp)
549{
550 int i;
551
552 /* Make sure the driver hasn't any stale locks. */
553 for (i = 0; i < 8; i++)
554 tg3_ape_write32(tp, TG3_APE_LOCK_GRANT + 4 * i,
555 APE_LOCK_GRANT_DRIVER);
556}
557
558static int tg3_ape_lock(struct tg3 *tp, int locknum)
559{
560 int i, off;
561 int ret = 0;
562 u32 status;
563
564 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
565 return 0;
566
567 switch (locknum) {
Matt Carlson77b483f2008-08-15 14:07:24 -0700568 case TG3_APE_LOCK_GRC:
Matt Carlson0d3031d2007-10-10 18:02:43 -0700569 case TG3_APE_LOCK_MEM:
570 break;
571 default:
572 return -EINVAL;
573 }
574
575 off = 4 * locknum;
576
577 tg3_ape_write32(tp, TG3_APE_LOCK_REQ + off, APE_LOCK_REQ_DRIVER);
578
579 /* Wait for up to 1 millisecond to acquire lock. */
580 for (i = 0; i < 100; i++) {
581 status = tg3_ape_read32(tp, TG3_APE_LOCK_GRANT + off);
582 if (status == APE_LOCK_GRANT_DRIVER)
583 break;
584 udelay(10);
585 }
586
587 if (status != APE_LOCK_GRANT_DRIVER) {
588 /* Revoke the lock request. */
589 tg3_ape_write32(tp, TG3_APE_LOCK_GRANT + off,
590 APE_LOCK_GRANT_DRIVER);
591
592 ret = -EBUSY;
593 }
594
595 return ret;
596}
597
598static void tg3_ape_unlock(struct tg3 *tp, int locknum)
599{
600 int off;
601
602 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
603 return;
604
605 switch (locknum) {
Matt Carlson77b483f2008-08-15 14:07:24 -0700606 case TG3_APE_LOCK_GRC:
Matt Carlson0d3031d2007-10-10 18:02:43 -0700607 case TG3_APE_LOCK_MEM:
608 break;
609 default:
610 return;
611 }
612
613 off = 4 * locknum;
614 tg3_ape_write32(tp, TG3_APE_LOCK_GRANT + off, APE_LOCK_GRANT_DRIVER);
615}
616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617static void tg3_disable_ints(struct tg3 *tp)
618{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000619 int i;
620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 tw32(TG3PCI_MISC_HOST_CTRL,
622 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000623 for (i = 0; i < tp->irq_max; i++)
624 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625}
626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627static void tg3_enable_ints(struct tg3 *tp)
628{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000629 int i;
630 u32 coal_now = 0;
631
Michael Chanbbe832c2005-06-24 20:20:04 -0700632 tp->irq_sync = 0;
633 wmb();
634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 tw32(TG3PCI_MISC_HOST_CTRL,
636 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000637
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000638 for (i = 0; i < tp->irq_cnt; i++) {
639 struct tg3_napi *tnapi = &tp->napi[i];
640 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
641 if (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)
642 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
643
644 coal_now |= tnapi->coal_now;
645 }
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000646
647 /* Force an initial interrupt */
648 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) &&
649 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
650 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
651 else
652 tw32(HOSTCC_MODE, tp->coalesce_mode |
653 HOSTCC_MODE_ENABLE | coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
Matt Carlson17375d22009-08-28 14:02:18 +0000656static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
Michael Chan04237dd2005-04-25 15:17:17 -0700657{
Matt Carlson17375d22009-08-28 14:02:18 +0000658 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +0000659 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan04237dd2005-04-25 15:17:17 -0700660 unsigned int work_exists = 0;
661
662 /* check for phy events */
663 if (!(tp->tg3_flags &
664 (TG3_FLAG_USE_LINKCHG_REG |
665 TG3_FLAG_POLL_SERDES))) {
666 if (sblk->status & SD_STATUS_LINK_CHG)
667 work_exists = 1;
668 }
669 /* check for RX/TX work to do */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000670 if (sblk->idx[0].tx_consumer != tnapi->tx_cons ||
Matt Carlson8d9d7cf2009-09-01 13:19:05 +0000671 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -0700672 work_exists = 1;
673
674 return work_exists;
675}
676
Matt Carlson17375d22009-08-28 14:02:18 +0000677/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -0700678 * similar to tg3_enable_ints, but it accurately determines whether there
679 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400680 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 */
Matt Carlson17375d22009-08-28 14:02:18 +0000682static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
Matt Carlson17375d22009-08-28 14:02:18 +0000684 struct tg3 *tp = tnapi->tp;
685
Matt Carlson898a56f2009-08-28 14:02:40 +0000686 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 mmiowb();
688
David S. Millerfac9b832005-05-18 22:46:34 -0700689 /* When doing tagged status, this work check is unnecessary.
690 * The last_tag we write above tells the chip which piece of
691 * work we've completed.
692 */
693 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) &&
Matt Carlson17375d22009-08-28 14:02:18 +0000694 tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -0700695 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +0000696 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697}
698
Matt Carlsonfed97812009-09-01 13:10:19 +0000699static void tg3_napi_disable(struct tg3 *tp)
700{
701 int i;
702
703 for (i = tp->irq_cnt - 1; i >= 0; i--)
704 napi_disable(&tp->napi[i].napi);
705}
706
707static void tg3_napi_enable(struct tg3 *tp)
708{
709 int i;
710
711 for (i = 0; i < tp->irq_cnt; i++)
712 napi_enable(&tp->napi[i].napi);
713}
714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715static inline void tg3_netif_stop(struct tg3 *tp)
716{
Michael Chanbbe832c2005-06-24 20:20:04 -0700717 tp->dev->trans_start = jiffies; /* prevent tx timeout */
Matt Carlsonfed97812009-09-01 13:10:19 +0000718 tg3_napi_disable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 netif_tx_disable(tp->dev);
720}
721
722static inline void tg3_netif_start(struct tg3 *tp)
723{
Matt Carlsonfe5f5782009-09-01 13:09:39 +0000724 /* NOTE: unconditional netif_tx_wake_all_queues is only
725 * appropriate so long as all callers are assured to
726 * have free tx slots (such as after tg3_init_hw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 */
Matt Carlsonfe5f5782009-09-01 13:09:39 +0000728 netif_tx_wake_all_queues(tp->dev);
729
Matt Carlsonfed97812009-09-01 13:10:19 +0000730 tg3_napi_enable(tp);
731 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
David S. Millerf47c11e2005-06-24 20:18:35 -0700732 tg3_enable_ints(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733}
734
735static void tg3_switch_clocks(struct tg3 *tp)
736{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000737 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 u32 orig_clock_ctrl;
739
Matt Carlson795d01c2007-10-07 23:28:17 -0700740 if ((tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
741 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -0700742 return;
743
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000744 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 orig_clock_ctrl = clock_ctrl;
747 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
748 CLOCK_CTRL_CLKRUN_OENABLE |
749 0x1f);
750 tp->pci_clock_ctrl = clock_ctrl;
751
752 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
753 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800754 tw32_wait_f(TG3PCI_CLOCK_CTRL,
755 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 }
757 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800758 tw32_wait_f(TG3PCI_CLOCK_CTRL,
759 clock_ctrl |
760 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
761 40);
762 tw32_wait_f(TG3PCI_CLOCK_CTRL,
763 clock_ctrl | (CLOCK_CTRL_ALTCLK),
764 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 }
Michael Chanb401e9e2005-12-19 16:27:04 -0800766 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767}
768
769#define PHY_BUSY_LOOPS 5000
770
771static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
772{
773 u32 frame_val;
774 unsigned int loops;
775 int ret;
776
777 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
778 tw32_f(MAC_MI_MODE,
779 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
780 udelay(80);
781 }
782
783 *val = 0x0;
784
Matt Carlson882e9792009-09-01 13:21:36 +0000785 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 MI_COM_PHY_ADDR_MASK);
787 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
788 MI_COM_REG_ADDR_MASK);
789 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 tw32_f(MAC_MI_COM, frame_val);
792
793 loops = PHY_BUSY_LOOPS;
794 while (loops != 0) {
795 udelay(10);
796 frame_val = tr32(MAC_MI_COM);
797
798 if ((frame_val & MI_COM_BUSY) == 0) {
799 udelay(5);
800 frame_val = tr32(MAC_MI_COM);
801 break;
802 }
803 loops -= 1;
804 }
805
806 ret = -EBUSY;
807 if (loops != 0) {
808 *val = frame_val & MI_COM_DATA_MASK;
809 ret = 0;
810 }
811
812 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
813 tw32_f(MAC_MI_MODE, tp->mi_mode);
814 udelay(80);
815 }
816
817 return ret;
818}
819
820static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
821{
822 u32 frame_val;
823 unsigned int loops;
824 int ret;
825
Matt Carlson7f97a4b2009-08-25 10:10:03 +0000826 if ((tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) &&
Michael Chanb5d37722006-09-27 16:06:21 -0700827 (reg == MII_TG3_CTRL || reg == MII_TG3_AUX_CTRL))
828 return 0;
829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
831 tw32_f(MAC_MI_MODE,
832 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
833 udelay(80);
834 }
835
Matt Carlson882e9792009-09-01 13:21:36 +0000836 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 MI_COM_PHY_ADDR_MASK);
838 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
839 MI_COM_REG_ADDR_MASK);
840 frame_val |= (val & MI_COM_DATA_MASK);
841 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 tw32_f(MAC_MI_COM, frame_val);
844
845 loops = PHY_BUSY_LOOPS;
846 while (loops != 0) {
847 udelay(10);
848 frame_val = tr32(MAC_MI_COM);
849 if ((frame_val & MI_COM_BUSY) == 0) {
850 udelay(5);
851 frame_val = tr32(MAC_MI_COM);
852 break;
853 }
854 loops -= 1;
855 }
856
857 ret = -EBUSY;
858 if (loops != 0)
859 ret = 0;
860
861 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
862 tw32_f(MAC_MI_MODE, tp->mi_mode);
863 udelay(80);
864 }
865
866 return ret;
867}
868
Matt Carlson95e28692008-05-25 23:44:14 -0700869static int tg3_bmcr_reset(struct tg3 *tp)
870{
871 u32 phy_control;
872 int limit, err;
873
874 /* OK, reset it, and poll the BMCR_RESET bit until it
875 * clears or we time out.
876 */
877 phy_control = BMCR_RESET;
878 err = tg3_writephy(tp, MII_BMCR, phy_control);
879 if (err != 0)
880 return -EBUSY;
881
882 limit = 5000;
883 while (limit--) {
884 err = tg3_readphy(tp, MII_BMCR, &phy_control);
885 if (err != 0)
886 return -EBUSY;
887
888 if ((phy_control & BMCR_RESET) == 0) {
889 udelay(40);
890 break;
891 }
892 udelay(10);
893 }
Roel Kluind4675b52009-02-12 16:33:27 -0800894 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -0700895 return -EBUSY;
896
897 return 0;
898}
899
Matt Carlson158d7ab2008-05-29 01:37:54 -0700900static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
901{
Francois Romieu3d165432009-01-19 16:56:50 -0800902 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700903 u32 val;
904
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000905 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -0700906
907 if (tg3_readphy(tp, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000908 val = -EIO;
909
910 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -0700911
912 return val;
913}
914
915static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
916{
Francois Romieu3d165432009-01-19 16:56:50 -0800917 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000918 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700919
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000920 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -0700921
922 if (tg3_writephy(tp, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000923 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700924
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000925 spin_unlock_bh(&tp->lock);
926
927 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700928}
929
930static int tg3_mdio_reset(struct mii_bus *bp)
931{
932 return 0;
933}
934
Matt Carlson9c61d6b2008-11-03 16:54:56 -0800935static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -0700936{
937 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800938 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -0700939
Matt Carlson3f0e3ad2009-11-02 14:24:36 +0000940 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800941 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
942 case TG3_PHY_ID_BCM50610:
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
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001034 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001035
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 Buytenhek298cf9be2008-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 Buytenhek298cf9be2008-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 Buytenhek298cf9be2008-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;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001065 tp->mdio_bus->phy_mask = ~(1 << TG3_PHY_MII_ADDR);
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001066 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 Buytenhek298cf9be2008-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 Buytenhek298cf9be2008-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
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001087 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_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 Buytenhek298cf9be2008-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)
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001314 autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_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);
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001351 struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_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
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001437 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_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:
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001464 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001465 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
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001482 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_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
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001502 phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_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) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001508 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_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
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002477 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_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);
3246 }
3247
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 if (current_link_up != netif_carrier_ok(tp->dev)) {
3249 if (current_link_up)
3250 netif_carrier_on(tp->dev);
3251 else
3252 netif_carrier_off(tp->dev);
3253 tg3_link_report(tp);
3254 }
3255
3256 return 0;
3257}
3258
3259struct tg3_fiber_aneginfo {
3260 int state;
3261#define ANEG_STATE_UNKNOWN 0
3262#define ANEG_STATE_AN_ENABLE 1
3263#define ANEG_STATE_RESTART_INIT 2
3264#define ANEG_STATE_RESTART 3
3265#define ANEG_STATE_DISABLE_LINK_OK 4
3266#define ANEG_STATE_ABILITY_DETECT_INIT 5
3267#define ANEG_STATE_ABILITY_DETECT 6
3268#define ANEG_STATE_ACK_DETECT_INIT 7
3269#define ANEG_STATE_ACK_DETECT 8
3270#define ANEG_STATE_COMPLETE_ACK_INIT 9
3271#define ANEG_STATE_COMPLETE_ACK 10
3272#define ANEG_STATE_IDLE_DETECT_INIT 11
3273#define ANEG_STATE_IDLE_DETECT 12
3274#define ANEG_STATE_LINK_OK 13
3275#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
3276#define ANEG_STATE_NEXT_PAGE_WAIT 15
3277
3278 u32 flags;
3279#define MR_AN_ENABLE 0x00000001
3280#define MR_RESTART_AN 0x00000002
3281#define MR_AN_COMPLETE 0x00000004
3282#define MR_PAGE_RX 0x00000008
3283#define MR_NP_LOADED 0x00000010
3284#define MR_TOGGLE_TX 0x00000020
3285#define MR_LP_ADV_FULL_DUPLEX 0x00000040
3286#define MR_LP_ADV_HALF_DUPLEX 0x00000080
3287#define MR_LP_ADV_SYM_PAUSE 0x00000100
3288#define MR_LP_ADV_ASYM_PAUSE 0x00000200
3289#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
3290#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
3291#define MR_LP_ADV_NEXT_PAGE 0x00001000
3292#define MR_TOGGLE_RX 0x00002000
3293#define MR_NP_RX 0x00004000
3294
3295#define MR_LINK_OK 0x80000000
3296
3297 unsigned long link_time, cur_time;
3298
3299 u32 ability_match_cfg;
3300 int ability_match_count;
3301
3302 char ability_match, idle_match, ack_match;
3303
3304 u32 txconfig, rxconfig;
3305#define ANEG_CFG_NP 0x00000080
3306#define ANEG_CFG_ACK 0x00000040
3307#define ANEG_CFG_RF2 0x00000020
3308#define ANEG_CFG_RF1 0x00000010
3309#define ANEG_CFG_PS2 0x00000001
3310#define ANEG_CFG_PS1 0x00008000
3311#define ANEG_CFG_HD 0x00004000
3312#define ANEG_CFG_FD 0x00002000
3313#define ANEG_CFG_INVAL 0x00001f06
3314
3315};
3316#define ANEG_OK 0
3317#define ANEG_DONE 1
3318#define ANEG_TIMER_ENAB 2
3319#define ANEG_FAILED -1
3320
3321#define ANEG_STATE_SETTLE_TIME 10000
3322
3323static int tg3_fiber_aneg_smachine(struct tg3 *tp,
3324 struct tg3_fiber_aneginfo *ap)
3325{
Matt Carlson5be73b42007-12-20 20:09:29 -08003326 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 unsigned long delta;
3328 u32 rx_cfg_reg;
3329 int ret;
3330
3331 if (ap->state == ANEG_STATE_UNKNOWN) {
3332 ap->rxconfig = 0;
3333 ap->link_time = 0;
3334 ap->cur_time = 0;
3335 ap->ability_match_cfg = 0;
3336 ap->ability_match_count = 0;
3337 ap->ability_match = 0;
3338 ap->idle_match = 0;
3339 ap->ack_match = 0;
3340 }
3341 ap->cur_time++;
3342
3343 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
3344 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
3345
3346 if (rx_cfg_reg != ap->ability_match_cfg) {
3347 ap->ability_match_cfg = rx_cfg_reg;
3348 ap->ability_match = 0;
3349 ap->ability_match_count = 0;
3350 } else {
3351 if (++ap->ability_match_count > 1) {
3352 ap->ability_match = 1;
3353 ap->ability_match_cfg = rx_cfg_reg;
3354 }
3355 }
3356 if (rx_cfg_reg & ANEG_CFG_ACK)
3357 ap->ack_match = 1;
3358 else
3359 ap->ack_match = 0;
3360
3361 ap->idle_match = 0;
3362 } else {
3363 ap->idle_match = 1;
3364 ap->ability_match_cfg = 0;
3365 ap->ability_match_count = 0;
3366 ap->ability_match = 0;
3367 ap->ack_match = 0;
3368
3369 rx_cfg_reg = 0;
3370 }
3371
3372 ap->rxconfig = rx_cfg_reg;
3373 ret = ANEG_OK;
3374
3375 switch(ap->state) {
3376 case ANEG_STATE_UNKNOWN:
3377 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
3378 ap->state = ANEG_STATE_AN_ENABLE;
3379
3380 /* fallthru */
3381 case ANEG_STATE_AN_ENABLE:
3382 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
3383 if (ap->flags & MR_AN_ENABLE) {
3384 ap->link_time = 0;
3385 ap->cur_time = 0;
3386 ap->ability_match_cfg = 0;
3387 ap->ability_match_count = 0;
3388 ap->ability_match = 0;
3389 ap->idle_match = 0;
3390 ap->ack_match = 0;
3391
3392 ap->state = ANEG_STATE_RESTART_INIT;
3393 } else {
3394 ap->state = ANEG_STATE_DISABLE_LINK_OK;
3395 }
3396 break;
3397
3398 case ANEG_STATE_RESTART_INIT:
3399 ap->link_time = ap->cur_time;
3400 ap->flags &= ~(MR_NP_LOADED);
3401 ap->txconfig = 0;
3402 tw32(MAC_TX_AUTO_NEG, 0);
3403 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3404 tw32_f(MAC_MODE, tp->mac_mode);
3405 udelay(40);
3406
3407 ret = ANEG_TIMER_ENAB;
3408 ap->state = ANEG_STATE_RESTART;
3409
3410 /* fallthru */
3411 case ANEG_STATE_RESTART:
3412 delta = ap->cur_time - ap->link_time;
3413 if (delta > ANEG_STATE_SETTLE_TIME) {
3414 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
3415 } else {
3416 ret = ANEG_TIMER_ENAB;
3417 }
3418 break;
3419
3420 case ANEG_STATE_DISABLE_LINK_OK:
3421 ret = ANEG_DONE;
3422 break;
3423
3424 case ANEG_STATE_ABILITY_DETECT_INIT:
3425 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08003426 ap->txconfig = ANEG_CFG_FD;
3427 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3428 if (flowctrl & ADVERTISE_1000XPAUSE)
3429 ap->txconfig |= ANEG_CFG_PS1;
3430 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3431 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3433 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3434 tw32_f(MAC_MODE, tp->mac_mode);
3435 udelay(40);
3436
3437 ap->state = ANEG_STATE_ABILITY_DETECT;
3438 break;
3439
3440 case ANEG_STATE_ABILITY_DETECT:
3441 if (ap->ability_match != 0 && ap->rxconfig != 0) {
3442 ap->state = ANEG_STATE_ACK_DETECT_INIT;
3443 }
3444 break;
3445
3446 case ANEG_STATE_ACK_DETECT_INIT:
3447 ap->txconfig |= ANEG_CFG_ACK;
3448 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3449 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3450 tw32_f(MAC_MODE, tp->mac_mode);
3451 udelay(40);
3452
3453 ap->state = ANEG_STATE_ACK_DETECT;
3454
3455 /* fallthru */
3456 case ANEG_STATE_ACK_DETECT:
3457 if (ap->ack_match != 0) {
3458 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
3459 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
3460 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
3461 } else {
3462 ap->state = ANEG_STATE_AN_ENABLE;
3463 }
3464 } else if (ap->ability_match != 0 &&
3465 ap->rxconfig == 0) {
3466 ap->state = ANEG_STATE_AN_ENABLE;
3467 }
3468 break;
3469
3470 case ANEG_STATE_COMPLETE_ACK_INIT:
3471 if (ap->rxconfig & ANEG_CFG_INVAL) {
3472 ret = ANEG_FAILED;
3473 break;
3474 }
3475 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
3476 MR_LP_ADV_HALF_DUPLEX |
3477 MR_LP_ADV_SYM_PAUSE |
3478 MR_LP_ADV_ASYM_PAUSE |
3479 MR_LP_ADV_REMOTE_FAULT1 |
3480 MR_LP_ADV_REMOTE_FAULT2 |
3481 MR_LP_ADV_NEXT_PAGE |
3482 MR_TOGGLE_RX |
3483 MR_NP_RX);
3484 if (ap->rxconfig & ANEG_CFG_FD)
3485 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
3486 if (ap->rxconfig & ANEG_CFG_HD)
3487 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
3488 if (ap->rxconfig & ANEG_CFG_PS1)
3489 ap->flags |= MR_LP_ADV_SYM_PAUSE;
3490 if (ap->rxconfig & ANEG_CFG_PS2)
3491 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
3492 if (ap->rxconfig & ANEG_CFG_RF1)
3493 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
3494 if (ap->rxconfig & ANEG_CFG_RF2)
3495 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
3496 if (ap->rxconfig & ANEG_CFG_NP)
3497 ap->flags |= MR_LP_ADV_NEXT_PAGE;
3498
3499 ap->link_time = ap->cur_time;
3500
3501 ap->flags ^= (MR_TOGGLE_TX);
3502 if (ap->rxconfig & 0x0008)
3503 ap->flags |= MR_TOGGLE_RX;
3504 if (ap->rxconfig & ANEG_CFG_NP)
3505 ap->flags |= MR_NP_RX;
3506 ap->flags |= MR_PAGE_RX;
3507
3508 ap->state = ANEG_STATE_COMPLETE_ACK;
3509 ret = ANEG_TIMER_ENAB;
3510 break;
3511
3512 case ANEG_STATE_COMPLETE_ACK:
3513 if (ap->ability_match != 0 &&
3514 ap->rxconfig == 0) {
3515 ap->state = ANEG_STATE_AN_ENABLE;
3516 break;
3517 }
3518 delta = ap->cur_time - ap->link_time;
3519 if (delta > ANEG_STATE_SETTLE_TIME) {
3520 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
3521 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3522 } else {
3523 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
3524 !(ap->flags & MR_NP_RX)) {
3525 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3526 } else {
3527 ret = ANEG_FAILED;
3528 }
3529 }
3530 }
3531 break;
3532
3533 case ANEG_STATE_IDLE_DETECT_INIT:
3534 ap->link_time = ap->cur_time;
3535 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3536 tw32_f(MAC_MODE, tp->mac_mode);
3537 udelay(40);
3538
3539 ap->state = ANEG_STATE_IDLE_DETECT;
3540 ret = ANEG_TIMER_ENAB;
3541 break;
3542
3543 case ANEG_STATE_IDLE_DETECT:
3544 if (ap->ability_match != 0 &&
3545 ap->rxconfig == 0) {
3546 ap->state = ANEG_STATE_AN_ENABLE;
3547 break;
3548 }
3549 delta = ap->cur_time - ap->link_time;
3550 if (delta > ANEG_STATE_SETTLE_TIME) {
3551 /* XXX another gem from the Broadcom driver :( */
3552 ap->state = ANEG_STATE_LINK_OK;
3553 }
3554 break;
3555
3556 case ANEG_STATE_LINK_OK:
3557 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
3558 ret = ANEG_DONE;
3559 break;
3560
3561 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
3562 /* ??? unimplemented */
3563 break;
3564
3565 case ANEG_STATE_NEXT_PAGE_WAIT:
3566 /* ??? unimplemented */
3567 break;
3568
3569 default:
3570 ret = ANEG_FAILED;
3571 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573
3574 return ret;
3575}
3576
Matt Carlson5be73b42007-12-20 20:09:29 -08003577static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578{
3579 int res = 0;
3580 struct tg3_fiber_aneginfo aninfo;
3581 int status = ANEG_FAILED;
3582 unsigned int tick;
3583 u32 tmp;
3584
3585 tw32_f(MAC_TX_AUTO_NEG, 0);
3586
3587 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
3588 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
3589 udelay(40);
3590
3591 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
3592 udelay(40);
3593
3594 memset(&aninfo, 0, sizeof(aninfo));
3595 aninfo.flags |= MR_AN_ENABLE;
3596 aninfo.state = ANEG_STATE_UNKNOWN;
3597 aninfo.cur_time = 0;
3598 tick = 0;
3599 while (++tick < 195000) {
3600 status = tg3_fiber_aneg_smachine(tp, &aninfo);
3601 if (status == ANEG_DONE || status == ANEG_FAILED)
3602 break;
3603
3604 udelay(1);
3605 }
3606
3607 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3608 tw32_f(MAC_MODE, tp->mac_mode);
3609 udelay(40);
3610
Matt Carlson5be73b42007-12-20 20:09:29 -08003611 *txflags = aninfo.txconfig;
3612 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613
3614 if (status == ANEG_DONE &&
3615 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
3616 MR_LP_ADV_FULL_DUPLEX)))
3617 res = 1;
3618
3619 return res;
3620}
3621
3622static void tg3_init_bcm8002(struct tg3 *tp)
3623{
3624 u32 mac_status = tr32(MAC_STATUS);
3625 int i;
3626
3627 /* Reset when initting first time or we have a link. */
3628 if ((tp->tg3_flags & TG3_FLAG_INIT_COMPLETE) &&
3629 !(mac_status & MAC_STATUS_PCS_SYNCED))
3630 return;
3631
3632 /* Set PLL lock range. */
3633 tg3_writephy(tp, 0x16, 0x8007);
3634
3635 /* SW reset */
3636 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
3637
3638 /* Wait for reset to complete. */
3639 /* XXX schedule_timeout() ... */
3640 for (i = 0; i < 500; i++)
3641 udelay(10);
3642
3643 /* Config mode; select PMA/Ch 1 regs. */
3644 tg3_writephy(tp, 0x10, 0x8411);
3645
3646 /* Enable auto-lock and comdet, select txclk for tx. */
3647 tg3_writephy(tp, 0x11, 0x0a10);
3648
3649 tg3_writephy(tp, 0x18, 0x00a0);
3650 tg3_writephy(tp, 0x16, 0x41ff);
3651
3652 /* Assert and deassert POR. */
3653 tg3_writephy(tp, 0x13, 0x0400);
3654 udelay(40);
3655 tg3_writephy(tp, 0x13, 0x0000);
3656
3657 tg3_writephy(tp, 0x11, 0x0a50);
3658 udelay(40);
3659 tg3_writephy(tp, 0x11, 0x0a10);
3660
3661 /* Wait for signal to stabilize */
3662 /* XXX schedule_timeout() ... */
3663 for (i = 0; i < 15000; i++)
3664 udelay(10);
3665
3666 /* Deselect the channel register so we can read the PHYID
3667 * later.
3668 */
3669 tg3_writephy(tp, 0x10, 0x8011);
3670}
3671
3672static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
3673{
Matt Carlson82cd3d12007-12-20 20:09:00 -08003674 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 u32 sg_dig_ctrl, sg_dig_status;
3676 u32 serdes_cfg, expected_sg_dig_ctrl;
3677 int workaround, port_a;
3678 int current_link_up;
3679
3680 serdes_cfg = 0;
3681 expected_sg_dig_ctrl = 0;
3682 workaround = 0;
3683 port_a = 1;
3684 current_link_up = 0;
3685
3686 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
3687 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
3688 workaround = 1;
3689 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
3690 port_a = 0;
3691
3692 /* preserve bits 0-11,13,14 for signal pre-emphasis */
3693 /* preserve bits 20-23 for voltage regulator */
3694 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
3695 }
3696
3697 sg_dig_ctrl = tr32(SG_DIG_CTRL);
3698
3699 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003700 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 if (workaround) {
3702 u32 val = serdes_cfg;
3703
3704 if (port_a)
3705 val |= 0xc010000;
3706 else
3707 val |= 0x4010000;
3708 tw32_f(MAC_SERDES_CFG, val);
3709 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003710
3711 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 }
3713 if (mac_status & MAC_STATUS_PCS_SYNCED) {
3714 tg3_setup_flow_control(tp, 0, 0);
3715 current_link_up = 1;
3716 }
3717 goto out;
3718 }
3719
3720 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003721 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722
Matt Carlson82cd3d12007-12-20 20:09:00 -08003723 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3724 if (flowctrl & ADVERTISE_1000XPAUSE)
3725 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
3726 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3727 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728
3729 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003730 if ((tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT) &&
3731 tp->serdes_counter &&
3732 ((mac_status & (MAC_STATUS_PCS_SYNCED |
3733 MAC_STATUS_RCVD_CFG)) ==
3734 MAC_STATUS_PCS_SYNCED)) {
3735 tp->serdes_counter--;
3736 current_link_up = 1;
3737 goto out;
3738 }
3739restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 if (workaround)
3741 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003742 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 udelay(5);
3744 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
3745
Michael Chan3d3ebe72006-09-27 15:59:15 -07003746 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
3747 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
3749 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003750 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 mac_status = tr32(MAC_STATUS);
3752
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003753 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08003755 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756
Matt Carlson82cd3d12007-12-20 20:09:00 -08003757 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
3758 local_adv |= ADVERTISE_1000XPAUSE;
3759 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
3760 local_adv |= ADVERTISE_1000XPSE_ASYM;
3761
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003762 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003763 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003764 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003765 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766
3767 tg3_setup_flow_control(tp, local_adv, remote_adv);
3768 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003769 tp->serdes_counter = 0;
3770 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003771 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003772 if (tp->serdes_counter)
3773 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 else {
3775 if (workaround) {
3776 u32 val = serdes_cfg;
3777
3778 if (port_a)
3779 val |= 0xc010000;
3780 else
3781 val |= 0x4010000;
3782
3783 tw32_f(MAC_SERDES_CFG, val);
3784 }
3785
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003786 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 udelay(40);
3788
3789 /* Link parallel detection - link is up */
3790 /* only if we have PCS_SYNC and not */
3791 /* receiving config code words */
3792 mac_status = tr32(MAC_STATUS);
3793 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
3794 !(mac_status & MAC_STATUS_RCVD_CFG)) {
3795 tg3_setup_flow_control(tp, 0, 0);
3796 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003797 tp->tg3_flags2 |=
3798 TG3_FLG2_PARALLEL_DETECT;
3799 tp->serdes_counter =
3800 SERDES_PARALLEL_DET_TIMEOUT;
3801 } else
3802 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 }
3804 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07003805 } else {
3806 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
3807 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 }
3809
3810out:
3811 return current_link_up;
3812}
3813
3814static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
3815{
3816 int current_link_up = 0;
3817
Michael Chan5cf64b8a2007-05-05 12:11:21 -07003818 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820
3821 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08003822 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003824
Matt Carlson5be73b42007-12-20 20:09:29 -08003825 if (fiber_autoneg(tp, &txflags, &rxflags)) {
3826 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827
Matt Carlson5be73b42007-12-20 20:09:29 -08003828 if (txflags & ANEG_CFG_PS1)
3829 local_adv |= ADVERTISE_1000XPAUSE;
3830 if (txflags & ANEG_CFG_PS2)
3831 local_adv |= ADVERTISE_1000XPSE_ASYM;
3832
3833 if (rxflags & MR_LP_ADV_SYM_PAUSE)
3834 remote_adv |= LPA_1000XPAUSE;
3835 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
3836 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837
3838 tg3_setup_flow_control(tp, local_adv, remote_adv);
3839
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840 current_link_up = 1;
3841 }
3842 for (i = 0; i < 30; i++) {
3843 udelay(20);
3844 tw32_f(MAC_STATUS,
3845 (MAC_STATUS_SYNC_CHANGED |
3846 MAC_STATUS_CFG_CHANGED));
3847 udelay(40);
3848 if ((tr32(MAC_STATUS) &
3849 (MAC_STATUS_SYNC_CHANGED |
3850 MAC_STATUS_CFG_CHANGED)) == 0)
3851 break;
3852 }
3853
3854 mac_status = tr32(MAC_STATUS);
3855 if (current_link_up == 0 &&
3856 (mac_status & MAC_STATUS_PCS_SYNCED) &&
3857 !(mac_status & MAC_STATUS_RCVD_CFG))
3858 current_link_up = 1;
3859 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08003860 tg3_setup_flow_control(tp, 0, 0);
3861
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862 /* Forcing 1000FD link up. */
3863 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864
3865 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
3866 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003867
3868 tw32_f(MAC_MODE, tp->mac_mode);
3869 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 }
3871
3872out:
3873 return current_link_up;
3874}
3875
3876static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
3877{
3878 u32 orig_pause_cfg;
3879 u16 orig_active_speed;
3880 u8 orig_active_duplex;
3881 u32 mac_status;
3882 int current_link_up;
3883 int i;
3884
Matt Carlson8d018622007-12-20 20:05:44 -08003885 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 orig_active_speed = tp->link_config.active_speed;
3887 orig_active_duplex = tp->link_config.active_duplex;
3888
3889 if (!(tp->tg3_flags2 & TG3_FLG2_HW_AUTONEG) &&
3890 netif_carrier_ok(tp->dev) &&
3891 (tp->tg3_flags & TG3_FLAG_INIT_COMPLETE)) {
3892 mac_status = tr32(MAC_STATUS);
3893 mac_status &= (MAC_STATUS_PCS_SYNCED |
3894 MAC_STATUS_SIGNAL_DET |
3895 MAC_STATUS_CFG_CHANGED |
3896 MAC_STATUS_RCVD_CFG);
3897 if (mac_status == (MAC_STATUS_PCS_SYNCED |
3898 MAC_STATUS_SIGNAL_DET)) {
3899 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
3900 MAC_STATUS_CFG_CHANGED));
3901 return 0;
3902 }
3903 }
3904
3905 tw32_f(MAC_TX_AUTO_NEG, 0);
3906
3907 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
3908 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
3909 tw32_f(MAC_MODE, tp->mac_mode);
3910 udelay(40);
3911
3912 if (tp->phy_id == PHY_ID_BCM8002)
3913 tg3_init_bcm8002(tp);
3914
3915 /* Enable link change event even when serdes polling. */
3916 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
3917 udelay(40);
3918
3919 current_link_up = 0;
3920 mac_status = tr32(MAC_STATUS);
3921
3922 if (tp->tg3_flags2 & TG3_FLG2_HW_AUTONEG)
3923 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
3924 else
3925 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
3926
Matt Carlson898a56f2009-08-28 14:02:40 +00003927 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00003929 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930
3931 for (i = 0; i < 100; i++) {
3932 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
3933 MAC_STATUS_CFG_CHANGED));
3934 udelay(5);
3935 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07003936 MAC_STATUS_CFG_CHANGED |
3937 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 break;
3939 }
3940
3941 mac_status = tr32(MAC_STATUS);
3942 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
3943 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003944 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
3945 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 tw32_f(MAC_MODE, (tp->mac_mode |
3947 MAC_MODE_SEND_CONFIGS));
3948 udelay(1);
3949 tw32_f(MAC_MODE, tp->mac_mode);
3950 }
3951 }
3952
3953 if (current_link_up == 1) {
3954 tp->link_config.active_speed = SPEED_1000;
3955 tp->link_config.active_duplex = DUPLEX_FULL;
3956 tw32(MAC_LED_CTRL, (tp->led_ctrl |
3957 LED_CTRL_LNKLED_OVERRIDE |
3958 LED_CTRL_1000MBPS_ON));
3959 } else {
3960 tp->link_config.active_speed = SPEED_INVALID;
3961 tp->link_config.active_duplex = DUPLEX_INVALID;
3962 tw32(MAC_LED_CTRL, (tp->led_ctrl |
3963 LED_CTRL_LNKLED_OVERRIDE |
3964 LED_CTRL_TRAFFIC_OVERRIDE));
3965 }
3966
3967 if (current_link_up != netif_carrier_ok(tp->dev)) {
3968 if (current_link_up)
3969 netif_carrier_on(tp->dev);
3970 else
3971 netif_carrier_off(tp->dev);
3972 tg3_link_report(tp);
3973 } else {
Matt Carlson8d018622007-12-20 20:05:44 -08003974 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 if (orig_pause_cfg != now_pause_cfg ||
3976 orig_active_speed != tp->link_config.active_speed ||
3977 orig_active_duplex != tp->link_config.active_duplex)
3978 tg3_link_report(tp);
3979 }
3980
3981 return 0;
3982}
3983
Michael Chan747e8f82005-07-25 12:33:22 -07003984static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
3985{
3986 int current_link_up, err = 0;
3987 u32 bmsr, bmcr;
3988 u16 current_speed;
3989 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08003990 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07003991
3992 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
3993 tw32_f(MAC_MODE, tp->mac_mode);
3994 udelay(40);
3995
3996 tw32(MAC_EVENT, 0);
3997
3998 tw32_f(MAC_STATUS,
3999 (MAC_STATUS_SYNC_CHANGED |
4000 MAC_STATUS_CFG_CHANGED |
4001 MAC_STATUS_MI_COMPLETION |
4002 MAC_STATUS_LNKSTATE_CHANGED));
4003 udelay(40);
4004
4005 if (force_reset)
4006 tg3_phy_reset(tp);
4007
4008 current_link_up = 0;
4009 current_speed = SPEED_INVALID;
4010 current_duplex = DUPLEX_INVALID;
4011
4012 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4013 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004014 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
4015 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4016 bmsr |= BMSR_LSTATUS;
4017 else
4018 bmsr &= ~BMSR_LSTATUS;
4019 }
Michael Chan747e8f82005-07-25 12:33:22 -07004020
4021 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
4022
4023 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlson2bd3ed02008-06-09 15:39:55 -07004024 (tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004025 /* do nothing, just check for link up at the end */
4026 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4027 u32 adv, new_adv;
4028
4029 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4030 new_adv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
4031 ADVERTISE_1000XPAUSE |
4032 ADVERTISE_1000XPSE_ASYM |
4033 ADVERTISE_SLCT);
4034
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004035 new_adv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Michael Chan747e8f82005-07-25 12:33:22 -07004036
4037 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
4038 new_adv |= ADVERTISE_1000XHALF;
4039 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
4040 new_adv |= ADVERTISE_1000XFULL;
4041
4042 if ((new_adv != adv) || !(bmcr & BMCR_ANENABLE)) {
4043 tg3_writephy(tp, MII_ADVERTISE, new_adv);
4044 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
4045 tg3_writephy(tp, MII_BMCR, bmcr);
4046
4047 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07004048 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Michael Chan747e8f82005-07-25 12:33:22 -07004049 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
4050
4051 return err;
4052 }
4053 } else {
4054 u32 new_bmcr;
4055
4056 bmcr &= ~BMCR_SPEED1000;
4057 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
4058
4059 if (tp->link_config.duplex == DUPLEX_FULL)
4060 new_bmcr |= BMCR_FULLDPLX;
4061
4062 if (new_bmcr != bmcr) {
4063 /* BMCR_SPEED1000 is a reserved bit that needs
4064 * to be set on write.
4065 */
4066 new_bmcr |= BMCR_SPEED1000;
4067
4068 /* Force a linkdown */
4069 if (netif_carrier_ok(tp->dev)) {
4070 u32 adv;
4071
4072 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4073 adv &= ~(ADVERTISE_1000XFULL |
4074 ADVERTISE_1000XHALF |
4075 ADVERTISE_SLCT);
4076 tg3_writephy(tp, MII_ADVERTISE, adv);
4077 tg3_writephy(tp, MII_BMCR, bmcr |
4078 BMCR_ANRESTART |
4079 BMCR_ANENABLE);
4080 udelay(10);
4081 netif_carrier_off(tp->dev);
4082 }
4083 tg3_writephy(tp, MII_BMCR, new_bmcr);
4084 bmcr = new_bmcr;
4085 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4086 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004087 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
4088 ASIC_REV_5714) {
4089 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4090 bmsr |= BMSR_LSTATUS;
4091 else
4092 bmsr &= ~BMSR_LSTATUS;
4093 }
Michael Chan747e8f82005-07-25 12:33:22 -07004094 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
4095 }
4096 }
4097
4098 if (bmsr & BMSR_LSTATUS) {
4099 current_speed = SPEED_1000;
4100 current_link_up = 1;
4101 if (bmcr & BMCR_FULLDPLX)
4102 current_duplex = DUPLEX_FULL;
4103 else
4104 current_duplex = DUPLEX_HALF;
4105
Matt Carlsonef167e22007-12-20 20:10:01 -08004106 local_adv = 0;
4107 remote_adv = 0;
4108
Michael Chan747e8f82005-07-25 12:33:22 -07004109 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08004110 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07004111
4112 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
4113 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
4114 common = local_adv & remote_adv;
4115 if (common & (ADVERTISE_1000XHALF |
4116 ADVERTISE_1000XFULL)) {
4117 if (common & ADVERTISE_1000XFULL)
4118 current_duplex = DUPLEX_FULL;
4119 else
4120 current_duplex = DUPLEX_HALF;
Michael Chan747e8f82005-07-25 12:33:22 -07004121 }
4122 else
4123 current_link_up = 0;
4124 }
4125 }
4126
Matt Carlsonef167e22007-12-20 20:10:01 -08004127 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
4128 tg3_setup_flow_control(tp, local_adv, remote_adv);
4129
Michael Chan747e8f82005-07-25 12:33:22 -07004130 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4131 if (tp->link_config.active_duplex == DUPLEX_HALF)
4132 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4133
4134 tw32_f(MAC_MODE, tp->mac_mode);
4135 udelay(40);
4136
4137 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4138
4139 tp->link_config.active_speed = current_speed;
4140 tp->link_config.active_duplex = current_duplex;
4141
4142 if (current_link_up != netif_carrier_ok(tp->dev)) {
4143 if (current_link_up)
4144 netif_carrier_on(tp->dev);
4145 else {
4146 netif_carrier_off(tp->dev);
4147 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
4148 }
4149 tg3_link_report(tp);
4150 }
4151 return err;
4152}
4153
4154static void tg3_serdes_parallel_detect(struct tg3 *tp)
4155{
Michael Chan3d3ebe72006-09-27 15:59:15 -07004156 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07004157 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07004158 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07004159 return;
4160 }
4161 if (!netif_carrier_ok(tp->dev) &&
4162 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
4163 u32 bmcr;
4164
4165 tg3_readphy(tp, MII_BMCR, &bmcr);
4166 if (bmcr & BMCR_ANENABLE) {
4167 u32 phy1, phy2;
4168
4169 /* Select shadow register 0x1f */
4170 tg3_writephy(tp, 0x1c, 0x7c00);
4171 tg3_readphy(tp, 0x1c, &phy1);
4172
4173 /* Select expansion interrupt status register */
4174 tg3_writephy(tp, 0x17, 0x0f01);
4175 tg3_readphy(tp, 0x15, &phy2);
4176 tg3_readphy(tp, 0x15, &phy2);
4177
4178 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
4179 /* We have signal detect and not receiving
4180 * config code words, link is up by parallel
4181 * detection.
4182 */
4183
4184 bmcr &= ~BMCR_ANENABLE;
4185 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
4186 tg3_writephy(tp, MII_BMCR, bmcr);
4187 tp->tg3_flags2 |= TG3_FLG2_PARALLEL_DETECT;
4188 }
4189 }
4190 }
4191 else if (netif_carrier_ok(tp->dev) &&
4192 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
4193 (tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT)) {
4194 u32 phy2;
4195
4196 /* Select expansion interrupt status register */
4197 tg3_writephy(tp, 0x17, 0x0f01);
4198 tg3_readphy(tp, 0x15, &phy2);
4199 if (phy2 & 0x20) {
4200 u32 bmcr;
4201
4202 /* Config code words received, turn on autoneg. */
4203 tg3_readphy(tp, MII_BMCR, &bmcr);
4204 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
4205
4206 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
4207
4208 }
4209 }
4210}
4211
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212static int tg3_setup_phy(struct tg3 *tp, int force_reset)
4213{
4214 int err;
4215
4216 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
4217 err = tg3_setup_fiber_phy(tp, force_reset);
Michael Chan747e8f82005-07-25 12:33:22 -07004218 } else if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES) {
4219 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 } else {
4221 err = tg3_setup_copper_phy(tp, force_reset);
4222 }
4223
Matt Carlsonbcb37f62008-11-03 16:52:09 -08004224 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08004225 u32 val, scale;
4226
4227 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
4228 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
4229 scale = 65;
4230 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
4231 scale = 6;
4232 else
4233 scale = 12;
4234
4235 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
4236 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
4237 tw32(GRC_MISC_CFG, val);
4238 }
4239
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 if (tp->link_config.active_speed == SPEED_1000 &&
4241 tp->link_config.active_duplex == DUPLEX_HALF)
4242 tw32(MAC_TX_LENGTHS,
4243 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4244 (6 << TX_LENGTHS_IPG_SHIFT) |
4245 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
4246 else
4247 tw32(MAC_TX_LENGTHS,
4248 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4249 (6 << TX_LENGTHS_IPG_SHIFT) |
4250 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
4251
4252 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
4253 if (netif_carrier_ok(tp->dev)) {
4254 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07004255 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 } else {
4257 tw32(HOSTCC_STAT_COAL_TICKS, 0);
4258 }
4259 }
4260
Matt Carlson8ed5d972007-05-07 00:25:49 -07004261 if (tp->tg3_flags & TG3_FLAG_ASPM_WORKAROUND) {
4262 u32 val = tr32(PCIE_PWR_MGMT_THRESH);
4263 if (!netif_carrier_ok(tp->dev))
4264 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
4265 tp->pwrmgmt_thresh;
4266 else
4267 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
4268 tw32(PCIE_PWR_MGMT_THRESH, val);
4269 }
4270
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271 return err;
4272}
4273
Michael Chandf3e6542006-05-26 17:48:07 -07004274/* This is called whenever we suspect that the system chipset is re-
4275 * ordering the sequence of MMIO to the tx send mailbox. The symptom
4276 * is bogus tx completions. We try to recover by setting the
4277 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
4278 * in the workqueue.
4279 */
4280static void tg3_tx_recover(struct tg3 *tp)
4281{
4282 BUG_ON((tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER) ||
4283 tp->write32_tx_mbox == tg3_write_indirect_mbox);
4284
4285 printk(KERN_WARNING PFX "%s: The system may be re-ordering memory-"
4286 "mapped I/O cycles to the network device, attempting to "
4287 "recover. Please report the problem to the driver maintainer "
4288 "and include system chipset information.\n", tp->dev->name);
4289
4290 spin_lock(&tp->lock);
Michael Chandf3e6542006-05-26 17:48:07 -07004291 tp->tg3_flags |= TG3_FLAG_TX_RECOVERY_PENDING;
Michael Chandf3e6542006-05-26 17:48:07 -07004292 spin_unlock(&tp->lock);
4293}
4294
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004295static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07004296{
4297 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004298 return tnapi->tx_pending -
4299 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07004300}
4301
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302/* Tigon3 never reports partial packet sends. So we do not
4303 * need special logic to handle SKBs that have not had all
4304 * of their frags sent yet, like SunGEM does.
4305 */
Matt Carlson17375d22009-08-28 14:02:18 +00004306static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307{
Matt Carlson17375d22009-08-28 14:02:18 +00004308 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00004309 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004310 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004311 struct netdev_queue *txq;
4312 int index = tnapi - tp->napi;
4313
4314 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
4315 index--;
4316
4317 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318
4319 while (sw_idx != hw_idx) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004320 struct tx_ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07004322 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323
Michael Chandf3e6542006-05-26 17:48:07 -07004324 if (unlikely(skb == NULL)) {
4325 tg3_tx_recover(tp);
4326 return;
4327 }
4328
David S. Miller90079ce2008-09-11 04:52:51 -07004329 skb_dma_unmap(&tp->pdev->dev, skb, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330
4331 ri->skb = NULL;
4332
4333 sw_idx = NEXT_TX(sw_idx);
4334
4335 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004336 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07004337 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
4338 tx_bug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 sw_idx = NEXT_TX(sw_idx);
4340 }
4341
David S. Millerf47c11e2005-06-24 20:18:35 -07004342 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07004343
4344 if (unlikely(tx_bug)) {
4345 tg3_tx_recover(tp);
4346 return;
4347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348 }
4349
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004350 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351
Michael Chan1b2a7202006-08-07 21:46:02 -07004352 /* Need to make the tx_cons update visible to tg3_start_xmit()
4353 * before checking for netif_queue_stopped(). Without the
4354 * memory barrier, there is a small possibility that tg3_start_xmit()
4355 * will miss it and cause the queue to be stopped forever.
4356 */
4357 smp_mb();
4358
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004359 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004360 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004361 __netif_tx_lock(txq, smp_processor_id());
4362 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004363 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004364 netif_tx_wake_queue(txq);
4365 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07004366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367}
4368
4369/* Returns size of skb allocated or < 0 on error.
4370 *
4371 * We only need to fill in the address because the other members
4372 * of the RX descriptor are invariant, see tg3_init_rings.
4373 *
4374 * Note the purposeful assymetry of cpu vs. chip accesses. For
4375 * posting buffers we only dirty the first cache line of the RX
4376 * descriptor (containing the address). Whereas for the RX status
4377 * buffers the cpu only reads the last cacheline of the RX descriptor
4378 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
4379 */
Matt Carlson17375d22009-08-28 14:02:18 +00004380static int tg3_alloc_rx_skb(struct tg3_napi *tnapi, u32 opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381 int src_idx, u32 dest_idx_unmasked)
4382{
Matt Carlson17375d22009-08-28 14:02:18 +00004383 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384 struct tg3_rx_buffer_desc *desc;
4385 struct ring_info *map, *src_map;
4386 struct sk_buff *skb;
4387 dma_addr_t mapping;
4388 int skb_size, dest_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00004389 struct tg3_rx_prodring_set *tpr = &tp->prodring[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390
4391 src_map = NULL;
4392 switch (opaque_key) {
4393 case RXD_OPAQUE_RING_STD:
4394 dest_idx = dest_idx_unmasked % TG3_RX_RING_SIZE;
Matt Carlson21f581a2009-08-28 14:00:25 +00004395 desc = &tpr->rx_std[dest_idx];
4396 map = &tpr->rx_std_buffers[dest_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 if (src_idx >= 0)
Matt Carlson21f581a2009-08-28 14:00:25 +00004398 src_map = &tpr->rx_std_buffers[src_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004399 skb_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400 break;
4401
4402 case RXD_OPAQUE_RING_JUMBO:
4403 dest_idx = dest_idx_unmasked % TG3_RX_JUMBO_RING_SIZE;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00004404 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00004405 map = &tpr->rx_jmb_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_jmb_buffers[src_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004408 skb_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 break;
4410
4411 default:
4412 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414
4415 /* Do not overwrite any of the map or rp information
4416 * until we are sure we can commit to a new buffer.
4417 *
4418 * Callers depend upon this behavior and assume that
4419 * we leave everything unchanged if we fail.
4420 */
Matt Carlson287be122009-08-28 13:58:46 +00004421 skb = netdev_alloc_skb(tp->dev, skb_size + tp->rx_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422 if (skb == NULL)
4423 return -ENOMEM;
4424
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 skb_reserve(skb, tp->rx_offset);
4426
Matt Carlson287be122009-08-28 13:58:46 +00004427 mapping = pci_map_single(tp->pdev, skb->data, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428 PCI_DMA_FROMDEVICE);
Matt Carlsona21771d2009-11-02 14:25:31 +00004429 if (pci_dma_mapping_error(tp->pdev, mapping)) {
4430 dev_kfree_skb(skb);
4431 return -EIO;
4432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433
4434 map->skb = skb;
4435 pci_unmap_addr_set(map, mapping, mapping);
4436
4437 if (src_map != NULL)
4438 src_map->skb = NULL;
4439
4440 desc->addr_hi = ((u64)mapping >> 32);
4441 desc->addr_lo = ((u64)mapping & 0xffffffff);
4442
4443 return skb_size;
4444}
4445
4446/* We only need to move over in the address because the other
4447 * members of the RX descriptor are invariant. See notes above
4448 * tg3_alloc_rx_skb for full details.
4449 */
Matt Carlson17375d22009-08-28 14:02:18 +00004450static void tg3_recycle_rx(struct tg3_napi *tnapi, u32 opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 int src_idx, u32 dest_idx_unmasked)
4452{
Matt Carlson17375d22009-08-28 14:02:18 +00004453 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
4455 struct ring_info *src_map, *dest_map;
4456 int dest_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00004457 struct tg3_rx_prodring_set *tpr = &tp->prodring[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458
4459 switch (opaque_key) {
4460 case RXD_OPAQUE_RING_STD:
4461 dest_idx = dest_idx_unmasked % TG3_RX_RING_SIZE;
Matt Carlson21f581a2009-08-28 14:00:25 +00004462 dest_desc = &tpr->rx_std[dest_idx];
4463 dest_map = &tpr->rx_std_buffers[dest_idx];
4464 src_desc = &tpr->rx_std[src_idx];
4465 src_map = &tpr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 break;
4467
4468 case RXD_OPAQUE_RING_JUMBO:
4469 dest_idx = dest_idx_unmasked % TG3_RX_JUMBO_RING_SIZE;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00004470 dest_desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00004471 dest_map = &tpr->rx_jmb_buffers[dest_idx];
Matt Carlson79ed5ac2009-08-28 14:00:55 +00004472 src_desc = &tpr->rx_jmb[src_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00004473 src_map = &tpr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 break;
4475
4476 default:
4477 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479
4480 dest_map->skb = src_map->skb;
4481 pci_unmap_addr_set(dest_map, mapping,
4482 pci_unmap_addr(src_map, mapping));
4483 dest_desc->addr_hi = src_desc->addr_hi;
4484 dest_desc->addr_lo = src_desc->addr_lo;
4485
4486 src_map->skb = NULL;
4487}
4488
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489/* The RX ring scheme is composed of multiple rings which post fresh
4490 * buffers to the chip, and one special ring the chip uses to report
4491 * status back to the host.
4492 *
4493 * The special ring reports the status of received packets to the
4494 * host. The chip does not write into the original descriptor the
4495 * RX buffer was obtained from. The chip simply takes the original
4496 * descriptor as provided by the host, updates the status and length
4497 * field, then writes this into the next status ring entry.
4498 *
4499 * Each ring the host uses to post buffers to the chip is described
4500 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
4501 * it is first placed into the on-chip ram. When the packet's length
4502 * is known, it walks down the TG3_BDINFO entries to select the ring.
4503 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
4504 * which is within the range of the new packet's length is chosen.
4505 *
4506 * The "separate ring for rx status" scheme may sound queer, but it makes
4507 * sense from a cache coherency perspective. If only the host writes
4508 * to the buffer post rings, and only the chip writes to the rx status
4509 * rings, then cache lines never move beyond shared-modified state.
4510 * If both the host and chip were to write into the same ring, cache line
4511 * eviction could occur since both entities want it in an exclusive state.
4512 */
Matt Carlson17375d22009-08-28 14:02:18 +00004513static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514{
Matt Carlson17375d22009-08-28 14:02:18 +00004515 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07004516 u32 work_mask, rx_std_posted = 0;
Matt Carlson72334482009-08-28 14:03:01 +00004517 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07004518 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 int received;
Matt Carlson21f581a2009-08-28 14:00:25 +00004520 struct tg3_rx_prodring_set *tpr = &tp->prodring[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004522 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523 /*
4524 * We need to order the read of hw_idx and the read of
4525 * the opaque cookie.
4526 */
4527 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 work_mask = 0;
4529 received = 0;
4530 while (sw_idx != hw_idx && budget > 0) {
Matt Carlson72334482009-08-28 14:03:01 +00004531 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 unsigned int len;
4533 struct sk_buff *skb;
4534 dma_addr_t dma_addr;
4535 u32 opaque_key, desc_idx, *post_ptr;
4536
4537 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
4538 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
4539 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson21f581a2009-08-28 14:00:25 +00004540 struct ring_info *ri = &tpr->rx_std_buffers[desc_idx];
4541 dma_addr = pci_unmap_addr(ri, mapping);
4542 skb = ri->skb;
4543 post_ptr = &tpr->rx_std_ptr;
Michael Chanf92905d2006-06-29 20:14:29 -07004544 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson21f581a2009-08-28 14:00:25 +00004546 struct ring_info *ri = &tpr->rx_jmb_buffers[desc_idx];
4547 dma_addr = pci_unmap_addr(ri, mapping);
4548 skb = ri->skb;
4549 post_ptr = &tpr->rx_jmb_ptr;
4550 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552
4553 work_mask |= opaque_key;
4554
4555 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
4556 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
4557 drop_it:
Matt Carlson17375d22009-08-28 14:02:18 +00004558 tg3_recycle_rx(tnapi, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 desc_idx, *post_ptr);
4560 drop_it_no_recycle:
4561 /* Other statistics kept track of by card. */
4562 tp->net_stats.rx_dropped++;
4563 goto next_pkt;
4564 }
4565
Matt Carlsonad829262008-11-21 17:16:16 -08004566 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
4567 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004569 if (len > RX_COPY_THRESHOLD
Matt Carlsonad829262008-11-21 17:16:16 -08004570 && tp->rx_offset == NET_IP_ALIGN
4571 /* rx_offset will likely not equal NET_IP_ALIGN
4572 * if this is a 5701 card running in PCI-X mode
4573 * [see tg3_get_invariants()]
4574 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575 ) {
4576 int skb_size;
4577
Matt Carlson17375d22009-08-28 14:02:18 +00004578 skb_size = tg3_alloc_rx_skb(tnapi, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 desc_idx, *post_ptr);
4580 if (skb_size < 0)
4581 goto drop_it;
4582
Matt Carlson287be122009-08-28 13:58:46 +00004583 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 PCI_DMA_FROMDEVICE);
4585
4586 skb_put(skb, len);
4587 } else {
4588 struct sk_buff *copy_skb;
4589
Matt Carlson17375d22009-08-28 14:02:18 +00004590 tg3_recycle_rx(tnapi, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591 desc_idx, *post_ptr);
4592
Matt Carlsonad829262008-11-21 17:16:16 -08004593 copy_skb = netdev_alloc_skb(tp->dev,
4594 len + TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595 if (copy_skb == NULL)
4596 goto drop_it_no_recycle;
4597
Matt Carlsonad829262008-11-21 17:16:16 -08004598 skb_reserve(copy_skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599 skb_put(copy_skb, len);
4600 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03004601 skb_copy_from_linear_data(skb, copy_skb->data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
4603
4604 /* We'll reuse the original ring buffer. */
4605 skb = copy_skb;
4606 }
4607
4608 if ((tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) &&
4609 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
4610 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
4611 >> RXD_TCPCSUM_SHIFT) == 0xffff))
4612 skb->ip_summed = CHECKSUM_UNNECESSARY;
4613 else
4614 skb->ip_summed = CHECKSUM_NONE;
4615
4616 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00004617
4618 if (len > (tp->dev->mtu + ETH_HLEN) &&
4619 skb->protocol != htons(ETH_P_8021Q)) {
4620 dev_kfree_skb(skb);
4621 goto next_pkt;
4622 }
4623
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624#if TG3_VLAN_TAG_USED
4625 if (tp->vlgrp != NULL &&
4626 desc->type_flags & RXD_FLAG_VLAN) {
Matt Carlson17375d22009-08-28 14:02:18 +00004627 vlan_gro_receive(&tnapi->napi, tp->vlgrp,
Matt Carlson8ef04422009-08-28 14:01:37 +00004628 desc->err_vlan & RXD_VLAN_MASK, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629 } else
4630#endif
Matt Carlson17375d22009-08-28 14:02:18 +00004631 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633 received++;
4634 budget--;
4635
4636next_pkt:
4637 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07004638
4639 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
4640 u32 idx = *post_ptr % TG3_RX_RING_SIZE;
4641
4642 tw32_rx_mbox(MAILBOX_RCV_STD_PROD_IDX +
4643 TG3_64BIT_REG_LOW, idx);
4644 work_mask &= ~RXD_OPAQUE_RING_STD;
4645 rx_std_posted = 0;
4646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07004648 sw_idx++;
Eric Dumazet6b31a512007-02-06 13:29:21 -08004649 sw_idx &= (TG3_RX_RCB_RING_SIZE(tp) - 1);
Michael Chan52f6d692005-04-25 15:14:32 -07004650
4651 /* Refresh hw_idx to see if there is new work */
4652 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004653 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07004654 rmb();
4655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 }
4657
4658 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00004659 tnapi->rx_rcb_ptr = sw_idx;
4660 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661
4662 /* Refill RX ring(s). */
4663 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson21f581a2009-08-28 14:00:25 +00004664 sw_idx = tpr->rx_std_ptr % TG3_RX_RING_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 tw32_rx_mbox(MAILBOX_RCV_STD_PROD_IDX + TG3_64BIT_REG_LOW,
4666 sw_idx);
4667 }
4668 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson21f581a2009-08-28 14:00:25 +00004669 sw_idx = tpr->rx_jmb_ptr % TG3_RX_JUMBO_RING_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670 tw32_rx_mbox(MAILBOX_RCV_JUMBO_PROD_IDX + TG3_64BIT_REG_LOW,
4671 sw_idx);
4672 }
4673 mmiowb();
4674
4675 return received;
4676}
4677
Matt Carlson17375d22009-08-28 14:02:18 +00004678static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679{
Matt Carlson17375d22009-08-28 14:02:18 +00004680 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00004681 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683 /* handle link change and other phy events */
4684 if (!(tp->tg3_flags &
4685 (TG3_FLAG_USE_LINKCHG_REG |
4686 TG3_FLAG_POLL_SERDES))) {
4687 if (sblk->status & SD_STATUS_LINK_CHG) {
4688 sblk->status = SD_STATUS_UPDATED |
4689 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07004690 spin_lock(&tp->lock);
Matt Carlsondd477002008-05-25 23:45:58 -07004691 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
4692 tw32_f(MAC_STATUS,
4693 (MAC_STATUS_SYNC_CHANGED |
4694 MAC_STATUS_CFG_CHANGED |
4695 MAC_STATUS_MI_COMPLETION |
4696 MAC_STATUS_LNKSTATE_CHANGED));
4697 udelay(40);
4698 } else
4699 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07004700 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701 }
4702 }
4703
4704 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004705 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00004706 tg3_tx(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07004707 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
Michael Chan4fd7ab52007-10-12 01:39:50 -07004708 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709 }
4710
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711 /* run RX thread, within the bounds set by NAPI.
4712 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004713 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004715 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00004716 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717
David S. Miller6f535762007-10-11 18:08:29 -07004718 return work_done;
4719}
David S. Millerf7383c22005-05-18 22:50:53 -07004720
David S. Miller6f535762007-10-11 18:08:29 -07004721static int tg3_poll(struct napi_struct *napi, int budget)
4722{
Matt Carlson8ef04422009-08-28 14:01:37 +00004723 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
4724 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07004725 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00004726 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07004727
4728 while (1) {
Matt Carlson17375d22009-08-28 14:02:18 +00004729 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07004730
4731 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
4732 goto tx_recovery;
4733
4734 if (unlikely(work_done >= budget))
4735 break;
4736
Michael Chan4fd7ab52007-10-12 01:39:50 -07004737 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) {
Matt Carlson17375d22009-08-28 14:02:18 +00004738 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07004739 * to tell the hw how much work has been processed,
4740 * so we must read it before checking for more work.
4741 */
Matt Carlson898a56f2009-08-28 14:02:40 +00004742 tnapi->last_tag = sblk->status_tag;
4743 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07004744 rmb();
4745 } else
4746 sblk->status &= ~SD_STATUS_UPDATED;
4747
Matt Carlson17375d22009-08-28 14:02:18 +00004748 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08004749 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00004750 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07004751 break;
4752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753 }
4754
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004755 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07004756
4757tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07004758 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08004759 napi_complete(napi);
David S. Miller6f535762007-10-11 18:08:29 -07004760 schedule_work(&tp->reset_task);
Michael Chan4fd7ab52007-10-12 01:39:50 -07004761 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762}
4763
David S. Millerf47c11e2005-06-24 20:18:35 -07004764static void tg3_irq_quiesce(struct tg3 *tp)
4765{
Matt Carlson4f125f42009-09-01 12:55:02 +00004766 int i;
4767
David S. Millerf47c11e2005-06-24 20:18:35 -07004768 BUG_ON(tp->irq_sync);
4769
4770 tp->irq_sync = 1;
4771 smp_mb();
4772
Matt Carlson4f125f42009-09-01 12:55:02 +00004773 for (i = 0; i < tp->irq_cnt; i++)
4774 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07004775}
4776
4777static inline int tg3_irq_sync(struct tg3 *tp)
4778{
4779 return tp->irq_sync;
4780}
4781
4782/* Fully shutdown all tg3 driver activity elsewhere in the system.
4783 * If irq_sync is non-zero, then the IRQ handler must be synchronized
4784 * with as well. Most of the time, this is not necessary except when
4785 * shutting down the device.
4786 */
4787static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
4788{
Michael Chan46966542007-07-11 19:47:19 -07004789 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07004790 if (irq_sync)
4791 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07004792}
4793
4794static inline void tg3_full_unlock(struct tg3 *tp)
4795{
David S. Millerf47c11e2005-06-24 20:18:35 -07004796 spin_unlock_bh(&tp->lock);
4797}
4798
Michael Chanfcfa0a32006-03-20 22:28:41 -08004799/* One-shot MSI handler - Chip automatically disables interrupt
4800 * after sending MSI so driver doesn't have to do it.
4801 */
David Howells7d12e782006-10-05 14:55:46 +01004802static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08004803{
Matt Carlson09943a12009-08-28 14:01:57 +00004804 struct tg3_napi *tnapi = dev_id;
4805 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08004806
Matt Carlson898a56f2009-08-28 14:02:40 +00004807 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00004808 if (tnapi->rx_rcb)
4809 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08004810
4811 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00004812 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08004813
4814 return IRQ_HANDLED;
4815}
4816
Michael Chan88b06bc22005-04-21 17:13:25 -07004817/* MSI ISR - No need to check for interrupt sharing and no need to
4818 * flush status block and interrupt mailbox. PCI ordering rules
4819 * guarantee that MSI will arrive after the status block.
4820 */
David Howells7d12e782006-10-05 14:55:46 +01004821static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07004822{
Matt Carlson09943a12009-08-28 14:01:57 +00004823 struct tg3_napi *tnapi = dev_id;
4824 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07004825
Matt Carlson898a56f2009-08-28 14:02:40 +00004826 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00004827 if (tnapi->rx_rcb)
4828 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07004829 /*
David S. Millerfac9b832005-05-18 22:46:34 -07004830 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07004831 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07004832 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07004833 * NIC to stop sending us irqs, engaging "in-intr-handler"
4834 * event coalescing.
4835 */
4836 tw32_mailbox(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07004837 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00004838 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07004839
Michael Chan88b06bc22005-04-21 17:13:25 -07004840 return IRQ_RETVAL(1);
4841}
4842
David Howells7d12e782006-10-05 14:55:46 +01004843static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844{
Matt Carlson09943a12009-08-28 14:01:57 +00004845 struct tg3_napi *tnapi = dev_id;
4846 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00004847 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848 unsigned int handled = 1;
4849
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850 /* In INTx mode, it is possible for the interrupt to arrive at
4851 * the CPU before the status block posted prior to the interrupt.
4852 * Reading the PCI State register will confirm whether the
4853 * interrupt is ours and will flush the status block.
4854 */
Michael Chand18edcb2007-03-24 20:57:11 -07004855 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
4856 if ((tp->tg3_flags & TG3_FLAG_CHIP_RESETTING) ||
4857 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
4858 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07004859 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07004860 }
Michael Chand18edcb2007-03-24 20:57:11 -07004861 }
4862
4863 /*
4864 * Writing any value to intr-mbox-0 clears PCI INTA# and
4865 * chip-internal interrupt pending events.
4866 * Writing non-zero to intr-mbox-0 additional tells the
4867 * NIC to stop sending us irqs, engaging "in-intr-handler"
4868 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07004869 *
4870 * Flush the mailbox to de-assert the IRQ immediately to prevent
4871 * spurious interrupts. The flush impacts performance but
4872 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07004873 */
Michael Chanc04cb342007-05-07 00:26:15 -07004874 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07004875 if (tg3_irq_sync(tp))
4876 goto out;
4877 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00004878 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00004879 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00004880 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07004881 } else {
4882 /* No work, shared interrupt perhaps? re-enable
4883 * interrupts, and flush that PCI write
4884 */
4885 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
4886 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07004887 }
David S. Millerf47c11e2005-06-24 20:18:35 -07004888out:
David S. Millerfac9b832005-05-18 22:46:34 -07004889 return IRQ_RETVAL(handled);
4890}
4891
David Howells7d12e782006-10-05 14:55:46 +01004892static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07004893{
Matt Carlson09943a12009-08-28 14:01:57 +00004894 struct tg3_napi *tnapi = dev_id;
4895 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00004896 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07004897 unsigned int handled = 1;
4898
David S. Millerfac9b832005-05-18 22:46:34 -07004899 /* In INTx mode, it is possible for the interrupt to arrive at
4900 * the CPU before the status block posted prior to the interrupt.
4901 * Reading the PCI State register will confirm whether the
4902 * interrupt is ours and will flush the status block.
4903 */
Matt Carlson898a56f2009-08-28 14:02:40 +00004904 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Michael Chand18edcb2007-03-24 20:57:11 -07004905 if ((tp->tg3_flags & TG3_FLAG_CHIP_RESETTING) ||
4906 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
4907 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07004908 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909 }
Michael Chand18edcb2007-03-24 20:57:11 -07004910 }
4911
4912 /*
4913 * writing any value to intr-mbox-0 clears PCI INTA# and
4914 * chip-internal interrupt pending events.
4915 * writing non-zero to intr-mbox-0 additional tells the
4916 * NIC to stop sending us irqs, engaging "in-intr-handler"
4917 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07004918 *
4919 * Flush the mailbox to de-assert the IRQ immediately to prevent
4920 * spurious interrupts. The flush impacts performance but
4921 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07004922 */
Michael Chanc04cb342007-05-07 00:26:15 -07004923 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00004924
4925 /*
4926 * In a shared interrupt configuration, sometimes other devices'
4927 * interrupts will scream. We record the current status tag here
4928 * so that the above check can report that the screaming interrupts
4929 * are unhandled. Eventually they will be silenced.
4930 */
Matt Carlson898a56f2009-08-28 14:02:40 +00004931 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00004932
Michael Chand18edcb2007-03-24 20:57:11 -07004933 if (tg3_irq_sync(tp))
4934 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00004935
Matt Carlson72334482009-08-28 14:03:01 +00004936 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00004937
Matt Carlson09943a12009-08-28 14:01:57 +00004938 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00004939
David S. Millerf47c11e2005-06-24 20:18:35 -07004940out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941 return IRQ_RETVAL(handled);
4942}
4943
Michael Chan79381092005-04-21 17:13:59 -07004944/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01004945static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07004946{
Matt Carlson09943a12009-08-28 14:01:57 +00004947 struct tg3_napi *tnapi = dev_id;
4948 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00004949 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07004950
Michael Chanf9804dd2005-09-27 12:13:10 -07004951 if ((sblk->status & SD_STATUS_UPDATED) ||
4952 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07004953 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07004954 return IRQ_RETVAL(1);
4955 }
4956 return IRQ_RETVAL(0);
4957}
4958
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07004959static int tg3_init_hw(struct tg3 *, int);
Michael Chan944d9802005-05-29 14:57:48 -07004960static int tg3_halt(struct tg3 *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961
Michael Chanb9ec6c12006-07-25 16:37:27 -07004962/* Restart hardware after configuration changes, self-test, etc.
4963 * Invoked with tp->lock held.
4964 */
4965static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
Eric Dumazet78c61462008-04-24 23:33:06 -07004966 __releases(tp->lock)
4967 __acquires(tp->lock)
Michael Chanb9ec6c12006-07-25 16:37:27 -07004968{
4969 int err;
4970
4971 err = tg3_init_hw(tp, reset_phy);
4972 if (err) {
4973 printk(KERN_ERR PFX "%s: Failed to re-initialize device, "
4974 "aborting.\n", tp->dev->name);
4975 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
4976 tg3_full_unlock(tp);
4977 del_timer_sync(&tp->timer);
4978 tp->irq_sync = 0;
Matt Carlsonfed97812009-09-01 13:10:19 +00004979 tg3_napi_enable(tp);
Michael Chanb9ec6c12006-07-25 16:37:27 -07004980 dev_close(tp->dev);
4981 tg3_full_lock(tp, 0);
4982 }
4983 return err;
4984}
4985
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986#ifdef CONFIG_NET_POLL_CONTROLLER
4987static void tg3_poll_controller(struct net_device *dev)
4988{
Matt Carlson4f125f42009-09-01 12:55:02 +00004989 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07004990 struct tg3 *tp = netdev_priv(dev);
4991
Matt Carlson4f125f42009-09-01 12:55:02 +00004992 for (i = 0; i < tp->irq_cnt; i++)
4993 tg3_interrupt(tp->napi[i].irq_vec, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994}
4995#endif
4996
David Howellsc4028952006-11-22 14:57:56 +00004997static void tg3_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998{
David Howellsc4028952006-11-22 14:57:56 +00004999 struct tg3 *tp = container_of(work, struct tg3, reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005000 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001 unsigned int restart_timer;
5002
Michael Chan7faa0062006-02-02 17:29:28 -08005003 tg3_full_lock(tp, 0);
Michael Chan7faa0062006-02-02 17:29:28 -08005004
5005 if (!netif_running(tp->dev)) {
Michael Chan7faa0062006-02-02 17:29:28 -08005006 tg3_full_unlock(tp);
5007 return;
5008 }
5009
5010 tg3_full_unlock(tp);
5011
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005012 tg3_phy_stop(tp);
5013
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014 tg3_netif_stop(tp);
5015
David S. Millerf47c11e2005-06-24 20:18:35 -07005016 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017
5018 restart_timer = tp->tg3_flags2 & TG3_FLG2_RESTART_TIMER;
5019 tp->tg3_flags2 &= ~TG3_FLG2_RESTART_TIMER;
5020
Michael Chandf3e6542006-05-26 17:48:07 -07005021 if (tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING) {
5022 tp->write32_tx_mbox = tg3_write32_tx_mbox;
5023 tp->write32_rx_mbox = tg3_write_flush_reg32;
5024 tp->tg3_flags |= TG3_FLAG_MBOX_WRITE_REORDER;
5025 tp->tg3_flags &= ~TG3_FLAG_TX_RECOVERY_PENDING;
5026 }
5027
Michael Chan944d9802005-05-29 14:57:48 -07005028 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005029 err = tg3_init_hw(tp, 1);
5030 if (err)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005031 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032
5033 tg3_netif_start(tp);
5034
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035 if (restart_timer)
5036 mod_timer(&tp->timer, jiffies + 1);
Michael Chan7faa0062006-02-02 17:29:28 -08005037
Michael Chanb9ec6c12006-07-25 16:37:27 -07005038out:
Michael Chan7faa0062006-02-02 17:29:28 -08005039 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005040
5041 if (!err)
5042 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043}
5044
Michael Chanb0408752007-02-13 12:18:30 -08005045static void tg3_dump_short_state(struct tg3 *tp)
5046{
5047 printk(KERN_ERR PFX "DEBUG: MAC_TX_STATUS[%08x] MAC_RX_STATUS[%08x]\n",
5048 tr32(MAC_TX_STATUS), tr32(MAC_RX_STATUS));
5049 printk(KERN_ERR PFX "DEBUG: RDMAC_STATUS[%08x] WDMAC_STATUS[%08x]\n",
5050 tr32(RDMAC_STATUS), tr32(WDMAC_STATUS));
5051}
5052
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053static void tg3_tx_timeout(struct net_device *dev)
5054{
5055 struct tg3 *tp = netdev_priv(dev);
5056
Michael Chanb0408752007-02-13 12:18:30 -08005057 if (netif_msg_tx_err(tp)) {
Michael Chan9f88f292006-12-07 00:22:54 -08005058 printk(KERN_ERR PFX "%s: transmit timed out, resetting\n",
5059 dev->name);
Michael Chanb0408752007-02-13 12:18:30 -08005060 tg3_dump_short_state(tp);
5061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062
5063 schedule_work(&tp->reset_task);
5064}
5065
Michael Chanc58ec932005-09-17 00:46:27 -07005066/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
5067static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
5068{
5069 u32 base = (u32) mapping & 0xffffffff;
5070
5071 return ((base > 0xffffdcc0) &&
5072 (base + len + 8 < base));
5073}
5074
Michael Chan72f2afb2006-03-06 19:28:35 -08005075/* Test for DMA addresses > 40-bit */
5076static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
5077 int len)
5078{
5079#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Michael Chan6728a8e2006-03-27 23:16:49 -08005080 if (tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG)
Yang Hongyang50cf1562009-04-06 19:01:14 -07005081 return (((u64) mapping + len) > DMA_BIT_MASK(40));
Michael Chan72f2afb2006-03-06 19:28:35 -08005082 return 0;
5083#else
5084 return 0;
5085#endif
5086}
5087
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005088static void tg3_set_txd(struct tg3_napi *, int, dma_addr_t, int, u32, u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089
Michael Chan72f2afb2006-03-06 19:28:35 -08005090/* Workaround 4GB and 40-bit hardware DMA bugs. */
5091static int tigon3_dma_hwbug_workaround(struct tg3 *tp, struct sk_buff *skb,
Michael Chanc58ec932005-09-17 00:46:27 -07005092 u32 last_plus_one, u32 *start,
5093 u32 base_flags, u32 mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005094{
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005095 struct tg3_napi *tnapi = &tp->napi[0];
Matt Carlson41588ba2008-04-19 18:12:33 -07005096 struct sk_buff *new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07005097 dma_addr_t new_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098 u32 entry = *start;
Michael Chanc58ec932005-09-17 00:46:27 -07005099 int i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100
Matt Carlson41588ba2008-04-19 18:12:33 -07005101 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
5102 new_skb = skb_copy(skb, GFP_ATOMIC);
5103 else {
5104 int more_headroom = 4 - ((unsigned long)skb->data & 3);
5105
5106 new_skb = skb_copy_expand(skb,
5107 skb_headroom(skb) + more_headroom,
5108 skb_tailroom(skb), GFP_ATOMIC);
5109 }
5110
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07005112 ret = -1;
5113 } else {
5114 /* New SKB is guaranteed to be linear. */
5115 entry = *start;
David S. Miller90079ce2008-09-11 04:52:51 -07005116 ret = skb_dma_map(&tp->pdev->dev, new_skb, DMA_TO_DEVICE);
Eric Dumazet042a53a2009-06-05 04:04:16 +00005117 new_addr = skb_shinfo(new_skb)->dma_head;
David S. Miller90079ce2008-09-11 04:52:51 -07005118
Michael Chanc58ec932005-09-17 00:46:27 -07005119 /* Make sure new skb does not cross any 4G boundaries.
5120 * Drop the packet if it does.
5121 */
Matt Carlson0e1406d2009-11-02 12:33:33 +00005122 if (ret || ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
5123 tg3_4g_overflow_test(new_addr, new_skb->len))) {
David S. Miller638266f2008-09-11 15:45:19 -07005124 if (!ret)
5125 skb_dma_unmap(&tp->pdev->dev, new_skb,
5126 DMA_TO_DEVICE);
Michael Chanc58ec932005-09-17 00:46:27 -07005127 ret = -1;
5128 dev_kfree_skb(new_skb);
5129 new_skb = NULL;
5130 } else {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005131 tg3_set_txd(tnapi, entry, new_addr, new_skb->len,
Michael Chanc58ec932005-09-17 00:46:27 -07005132 base_flags, 1 | (mss << 1));
5133 *start = NEXT_TX(entry);
5134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135 }
5136
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137 /* Now clean up the sw ring entries. */
5138 i = 0;
5139 while (entry != last_plus_one) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005140 if (i == 0)
5141 tnapi->tx_buffers[entry].skb = new_skb;
5142 else
5143 tnapi->tx_buffers[entry].skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144 entry = NEXT_TX(entry);
5145 i++;
5146 }
5147
David S. Miller90079ce2008-09-11 04:52:51 -07005148 skb_dma_unmap(&tp->pdev->dev, skb, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149 dev_kfree_skb(skb);
5150
Michael Chanc58ec932005-09-17 00:46:27 -07005151 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152}
5153
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005154static void tg3_set_txd(struct tg3_napi *tnapi, int entry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155 dma_addr_t mapping, int len, u32 flags,
5156 u32 mss_and_is_end)
5157{
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005158 struct tg3_tx_buffer_desc *txd = &tnapi->tx_ring[entry];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159 int is_end = (mss_and_is_end & 0x1);
5160 u32 mss = (mss_and_is_end >> 1);
5161 u32 vlan_tag = 0;
5162
5163 if (is_end)
5164 flags |= TXD_FLAG_END;
5165 if (flags & TXD_FLAG_VLAN) {
5166 vlan_tag = flags >> 16;
5167 flags &= 0xffff;
5168 }
5169 vlan_tag |= (mss << TXD_MSS_SHIFT);
5170
5171 txd->addr_hi = ((u64) mapping >> 32);
5172 txd->addr_lo = ((u64) mapping & 0xffffffff);
5173 txd->len_flags = (len << TXD_LEN_SHIFT) | flags;
5174 txd->vlan_tag = vlan_tag << TXD_VLAN_TAG_SHIFT;
5175}
5176
Michael Chan5a6f3072006-03-20 22:28:05 -08005177/* hard_start_xmit for devices that don't have any bugs and
5178 * support TG3_FLG2_HW_TSO_2 only.
5179 */
Stephen Hemminger613573252009-08-31 19:50:58 +00005180static netdev_tx_t tg3_start_xmit(struct sk_buff *skb,
5181 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182{
5183 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184 u32 len, entry, base_flags, mss;
David S. Miller90079ce2008-09-11 04:52:51 -07005185 struct skb_shared_info *sp;
5186 dma_addr_t mapping;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005187 struct tg3_napi *tnapi;
5188 struct netdev_queue *txq;
Michael Chan5a6f3072006-03-20 22:28:05 -08005189
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005190 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
5191 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
5192 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
5193 tnapi++;
Michael Chan5a6f3072006-03-20 22:28:05 -08005194
Michael Chan00b70502006-06-17 21:58:45 -07005195 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005196 * and TX reclaim runs via tp->napi.poll inside of a software
Michael Chan5a6f3072006-03-20 22:28:05 -08005197 * interrupt. Furthermore, IRQ processing runs lockless so we have
5198 * no IRQ context deadlocks to worry about either. Rejoice!
5199 */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005200 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005201 if (!netif_tx_queue_stopped(txq)) {
5202 netif_tx_stop_queue(txq);
Michael Chan5a6f3072006-03-20 22:28:05 -08005203
5204 /* This is a hard error, log it. */
5205 printk(KERN_ERR PFX "%s: BUG! Tx Ring full when "
5206 "queue awake!\n", dev->name);
5207 }
Michael Chan5a6f3072006-03-20 22:28:05 -08005208 return NETDEV_TX_BUSY;
5209 }
5210
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005211 entry = tnapi->tx_prod;
Michael Chan5a6f3072006-03-20 22:28:05 -08005212 base_flags = 0;
Michael Chan5a6f3072006-03-20 22:28:05 -08005213 mss = 0;
Matt Carlsonc13e3712007-05-05 11:50:04 -07005214 if ((mss = skb_shinfo(skb)->gso_size) != 0) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005215 int tcp_opt_len, ip_tcp_len;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005216 u32 hdrlen;
Michael Chan5a6f3072006-03-20 22:28:05 -08005217
5218 if (skb_header_cloned(skb) &&
5219 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
5220 dev_kfree_skb(skb);
5221 goto out_unlock;
5222 }
5223
Michael Chanb0026622006-07-03 19:42:14 -07005224 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005225 hdrlen = skb_headlen(skb) - ETH_HLEN;
Michael Chanb0026622006-07-03 19:42:14 -07005226 else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005227 struct iphdr *iph = ip_hdr(skb);
5228
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07005229 tcp_opt_len = tcp_optlen(skb);
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -03005230 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
Michael Chanb0026622006-07-03 19:42:14 -07005231
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005232 iph->check = 0;
5233 iph->tot_len = htons(mss + ip_tcp_len + tcp_opt_len);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005234 hdrlen = ip_tcp_len + tcp_opt_len;
Michael Chanb0026622006-07-03 19:42:14 -07005235 }
Michael Chan5a6f3072006-03-20 22:28:05 -08005236
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005237 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
5238 mss |= (hdrlen & 0xc) << 12;
5239 if (hdrlen & 0x10)
5240 base_flags |= 0x00000010;
5241 base_flags |= (hdrlen & 0x3e0) << 5;
5242 } else
5243 mss |= hdrlen << 9;
5244
Michael Chan5a6f3072006-03-20 22:28:05 -08005245 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
5246 TXD_FLAG_CPU_POST_DMA);
5247
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005248 tcp_hdr(skb)->check = 0;
Michael Chan5a6f3072006-03-20 22:28:05 -08005249
Michael Chan5a6f3072006-03-20 22:28:05 -08005250 }
Patrick McHardy84fa7932006-08-29 16:44:56 -07005251 else if (skb->ip_summed == CHECKSUM_PARTIAL)
Michael Chan5a6f3072006-03-20 22:28:05 -08005252 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Michael Chan5a6f3072006-03-20 22:28:05 -08005253#if TG3_VLAN_TAG_USED
5254 if (tp->vlgrp != NULL && vlan_tx_tag_present(skb))
5255 base_flags |= (TXD_FLAG_VLAN |
5256 (vlan_tx_tag_get(skb) << 16));
5257#endif
5258
David S. Miller90079ce2008-09-11 04:52:51 -07005259 if (skb_dma_map(&tp->pdev->dev, skb, DMA_TO_DEVICE)) {
5260 dev_kfree_skb(skb);
5261 goto out_unlock;
5262 }
5263
5264 sp = skb_shinfo(skb);
5265
Eric Dumazet042a53a2009-06-05 04:04:16 +00005266 mapping = sp->dma_head;
Michael Chan5a6f3072006-03-20 22:28:05 -08005267
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005268 tnapi->tx_buffers[entry].skb = skb;
Michael Chan5a6f3072006-03-20 22:28:05 -08005269
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005270 len = skb_headlen(skb);
5271
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005272 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 &&
5273 !mss && skb->len > ETH_DATA_LEN)
5274 base_flags |= TXD_FLAG_JMB_PKT;
5275
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005276 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
Michael Chan5a6f3072006-03-20 22:28:05 -08005277 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
5278
5279 entry = NEXT_TX(entry);
5280
5281 /* Now loop through additional data fragments, and queue them. */
5282 if (skb_shinfo(skb)->nr_frags > 0) {
5283 unsigned int i, last;
5284
5285 last = skb_shinfo(skb)->nr_frags - 1;
5286 for (i = 0; i <= last; i++) {
5287 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5288
5289 len = frag->size;
Eric Dumazet042a53a2009-06-05 04:04:16 +00005290 mapping = sp->dma_maps[i];
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005291 tnapi->tx_buffers[entry].skb = NULL;
Michael Chan5a6f3072006-03-20 22:28:05 -08005292
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005293 tg3_set_txd(tnapi, entry, mapping, len,
Michael Chan5a6f3072006-03-20 22:28:05 -08005294 base_flags, (i == last) | (mss << 1));
5295
5296 entry = NEXT_TX(entry);
5297 }
5298 }
5299
5300 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005301 tw32_tx_mbox(tnapi->prodmbox, entry);
Michael Chan5a6f3072006-03-20 22:28:05 -08005302
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005303 tnapi->tx_prod = entry;
5304 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005305 netif_tx_stop_queue(txq);
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005306 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005307 netif_tx_wake_queue(txq);
Michael Chan5a6f3072006-03-20 22:28:05 -08005308 }
5309
5310out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00005311 mmiowb();
Michael Chan5a6f3072006-03-20 22:28:05 -08005312
5313 return NETDEV_TX_OK;
5314}
5315
Stephen Hemminger613573252009-08-31 19:50:58 +00005316static netdev_tx_t tg3_start_xmit_dma_bug(struct sk_buff *,
5317 struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07005318
5319/* Use GSO to workaround a rare TSO bug that may be triggered when the
5320 * TSO header is greater than 80 bytes.
5321 */
5322static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
5323{
5324 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005325 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07005326
5327 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005328 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07005329 netif_stop_queue(tp->dev);
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005330 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08005331 return NETDEV_TX_BUSY;
5332
5333 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07005334 }
5335
5336 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07005337 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07005338 goto tg3_tso_bug_end;
5339
5340 do {
5341 nskb = segs;
5342 segs = segs->next;
5343 nskb->next = NULL;
5344 tg3_start_xmit_dma_bug(nskb, tp->dev);
5345 } while (segs);
5346
5347tg3_tso_bug_end:
5348 dev_kfree_skb(skb);
5349
5350 return NETDEV_TX_OK;
5351}
Michael Chan52c0fd82006-06-29 20:15:54 -07005352
Michael Chan5a6f3072006-03-20 22:28:05 -08005353/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
5354 * support TG3_FLG2_HW_TSO_1 or firmware TSO only.
5355 */
Stephen Hemminger613573252009-08-31 19:50:58 +00005356static netdev_tx_t tg3_start_xmit_dma_bug(struct sk_buff *skb,
5357 struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08005358{
5359 struct tg3 *tp = netdev_priv(dev);
Michael Chan5a6f3072006-03-20 22:28:05 -08005360 u32 len, entry, base_flags, mss;
David S. Miller90079ce2008-09-11 04:52:51 -07005361 struct skb_shared_info *sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362 int would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07005363 dma_addr_t mapping;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005364 struct tg3_napi *tnapi = &tp->napi[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365
5366 len = skb_headlen(skb);
5367
Michael Chan00b70502006-06-17 21:58:45 -07005368 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005369 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07005370 * interrupt. Furthermore, IRQ processing runs lockless so we have
5371 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372 */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005373 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
Stephen Hemminger1f064a82005-12-06 17:36:44 -08005374 if (!netif_queue_stopped(dev)) {
5375 netif_stop_queue(dev);
5376
5377 /* This is a hard error, log it. */
5378 printk(KERN_ERR PFX "%s: BUG! Tx Ring full when "
5379 "queue awake!\n", dev->name);
5380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381 return NETDEV_TX_BUSY;
5382 }
5383
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005384 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07005386 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388 mss = 0;
Matt Carlsonc13e3712007-05-05 11:50:04 -07005389 if ((mss = skb_shinfo(skb)->gso_size) != 0) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005390 struct iphdr *iph;
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005391 u32 tcp_opt_len, ip_tcp_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392
5393 if (skb_header_cloned(skb) &&
5394 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
5395 dev_kfree_skb(skb);
5396 goto out_unlock;
5397 }
5398
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07005399 tcp_opt_len = tcp_optlen(skb);
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -03005400 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401
Michael Chan52c0fd82006-06-29 20:15:54 -07005402 hdr_len = ip_tcp_len + tcp_opt_len;
5403 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Michael Chan7f62ad52007-02-20 23:25:40 -08005404 (tp->tg3_flags2 & TG3_FLG2_TSO_BUG))
Michael Chan52c0fd82006-06-29 20:15:54 -07005405 return (tg3_tso_bug(tp, skb));
5406
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
5408 TXD_FLAG_CPU_POST_DMA);
5409
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005410 iph = ip_hdr(skb);
5411 iph->check = 0;
5412 iph->tot_len = htons(mss + hdr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005414 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005416 } else
5417 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
5418 iph->daddr, 0,
5419 IPPROTO_TCP,
5420 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005422 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_2)
5423 mss |= hdr_len << 9;
5424 else if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_1) ||
5425 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005426 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427 int tsflags;
5428
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005429 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430 mss |= (tsflags << 11);
5431 }
5432 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005433 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434 int tsflags;
5435
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005436 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437 base_flags |= tsflags << 12;
5438 }
5439 }
5440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441#if TG3_VLAN_TAG_USED
5442 if (tp->vlgrp != NULL && vlan_tx_tag_present(skb))
5443 base_flags |= (TXD_FLAG_VLAN |
5444 (vlan_tx_tag_get(skb) << 16));
5445#endif
5446
David S. Miller90079ce2008-09-11 04:52:51 -07005447 if (skb_dma_map(&tp->pdev->dev, skb, DMA_TO_DEVICE)) {
5448 dev_kfree_skb(skb);
5449 goto out_unlock;
5450 }
5451
5452 sp = skb_shinfo(skb);
5453
Eric Dumazet042a53a2009-06-05 04:04:16 +00005454 mapping = sp->dma_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005456 tnapi->tx_buffers[entry].skb = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457
5458 would_hit_hwbug = 0;
5459
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005460 if ((tp->tg3_flags3 & TG3_FLG3_SHORT_DMA_BUG) && len <= 8)
5461 would_hit_hwbug = 1;
5462
Matt Carlson0e1406d2009-11-02 12:33:33 +00005463 if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
5464 tg3_4g_overflow_test(mapping, len))
Matt Carlson41588ba2008-04-19 18:12:33 -07005465 would_hit_hwbug = 1;
Matt Carlson0e1406d2009-11-02 12:33:33 +00005466
5467 if ((tp->tg3_flags3 & TG3_FLG3_40BIT_DMA_LIMIT_BUG) &&
5468 tg3_40bit_overflow_test(tp, mapping, len))
5469 would_hit_hwbug = 1;
5470
5471 if (tp->tg3_flags3 & TG3_FLG3_5701_DMA_BUG)
Michael Chanc58ec932005-09-17 00:46:27 -07005472 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005474 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
5476
5477 entry = NEXT_TX(entry);
5478
5479 /* Now loop through additional data fragments, and queue them. */
5480 if (skb_shinfo(skb)->nr_frags > 0) {
5481 unsigned int i, last;
5482
5483 last = skb_shinfo(skb)->nr_frags - 1;
5484 for (i = 0; i <= last; i++) {
5485 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5486
5487 len = frag->size;
Eric Dumazet042a53a2009-06-05 04:04:16 +00005488 mapping = sp->dma_maps[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005490 tnapi->tx_buffers[entry].skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005492 if ((tp->tg3_flags3 & TG3_FLG3_SHORT_DMA_BUG) &&
5493 len <= 8)
5494 would_hit_hwbug = 1;
5495
Matt Carlson0e1406d2009-11-02 12:33:33 +00005496 if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
5497 tg3_4g_overflow_test(mapping, len))
Michael Chanc58ec932005-09-17 00:46:27 -07005498 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499
Matt Carlson0e1406d2009-11-02 12:33:33 +00005500 if ((tp->tg3_flags3 & TG3_FLG3_40BIT_DMA_LIMIT_BUG) &&
5501 tg3_40bit_overflow_test(tp, mapping, len))
Michael Chan72f2afb2006-03-06 19:28:35 -08005502 would_hit_hwbug = 1;
5503
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005505 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506 base_flags, (i == last)|(mss << 1));
5507 else
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005508 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509 base_flags, (i == last));
5510
5511 entry = NEXT_TX(entry);
5512 }
5513 }
5514
5515 if (would_hit_hwbug) {
5516 u32 last_plus_one = entry;
5517 u32 start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518
Michael Chanc58ec932005-09-17 00:46:27 -07005519 start = entry - 1 - skb_shinfo(skb)->nr_frags;
5520 start &= (TG3_TX_RING_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521
5522 /* If the workaround fails due to memory/mapping
5523 * failure, silently drop this packet.
5524 */
Michael Chan72f2afb2006-03-06 19:28:35 -08005525 if (tigon3_dma_hwbug_workaround(tp, skb, last_plus_one,
Michael Chanc58ec932005-09-17 00:46:27 -07005526 &start, base_flags, mss))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527 goto out_unlock;
5528
5529 entry = start;
5530 }
5531
5532 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005533 tw32_tx_mbox(MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005535 tnapi->tx_prod = entry;
5536 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537 netif_stop_queue(dev);
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005538 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Michael Chan51b91462005-09-01 17:41:28 -07005539 netif_wake_queue(tp->dev);
5540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541
5542out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00005543 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544
5545 return NETDEV_TX_OK;
5546}
5547
5548static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
5549 int new_mtu)
5550{
5551 dev->mtu = new_mtu;
5552
Michael Chanef7f5ec2005-07-25 12:32:25 -07005553 if (new_mtu > ETH_DATA_LEN) {
Michael Chana4e2b342005-10-26 15:46:52 -07005554 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
Michael Chanef7f5ec2005-07-25 12:32:25 -07005555 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
5556 ethtool_op_set_tso(dev, 0);
5557 }
5558 else
5559 tp->tg3_flags |= TG3_FLAG_JUMBO_RING_ENABLE;
5560 } else {
Michael Chana4e2b342005-10-26 15:46:52 -07005561 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
Michael Chanef7f5ec2005-07-25 12:32:25 -07005562 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
Michael Chan0f893dc2005-07-25 12:30:38 -07005563 tp->tg3_flags &= ~TG3_FLAG_JUMBO_RING_ENABLE;
Michael Chanef7f5ec2005-07-25 12:32:25 -07005564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565}
5566
5567static int tg3_change_mtu(struct net_device *dev, int new_mtu)
5568{
5569 struct tg3 *tp = netdev_priv(dev);
Michael Chanb9ec6c12006-07-25 16:37:27 -07005570 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571
5572 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
5573 return -EINVAL;
5574
5575 if (!netif_running(dev)) {
5576 /* We'll just catch it later when the
5577 * device is up'd.
5578 */
5579 tg3_set_mtu(dev, tp, new_mtu);
5580 return 0;
5581 }
5582
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005583 tg3_phy_stop(tp);
5584
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585 tg3_netif_stop(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07005586
5587 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588
Michael Chan944d9802005-05-29 14:57:48 -07005589 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590
5591 tg3_set_mtu(dev, tp, new_mtu);
5592
Michael Chanb9ec6c12006-07-25 16:37:27 -07005593 err = tg3_restart_hw(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594
Michael Chanb9ec6c12006-07-25 16:37:27 -07005595 if (!err)
5596 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597
David S. Millerf47c11e2005-06-24 20:18:35 -07005598 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005600 if (!err)
5601 tg3_phy_start(tp);
5602
Michael Chanb9ec6c12006-07-25 16:37:27 -07005603 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604}
5605
Matt Carlson21f581a2009-08-28 14:00:25 +00005606static void tg3_rx_prodring_free(struct tg3 *tp,
5607 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609 int i;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005610 struct ring_info *rxp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611
5612 for (i = 0; i < TG3_RX_RING_SIZE; i++) {
Matt Carlson21f581a2009-08-28 14:00:25 +00005613 rxp = &tpr->rx_std_buffers[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614
5615 if (rxp->skb == NULL)
5616 continue;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005617
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618 pci_unmap_single(tp->pdev,
5619 pci_unmap_addr(rxp, mapping),
Matt Carlson287be122009-08-28 13:58:46 +00005620 tp->rx_pkt_map_sz,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621 PCI_DMA_FROMDEVICE);
5622 dev_kfree_skb_any(rxp->skb);
5623 rxp->skb = NULL;
5624 }
5625
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005626 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
5627 for (i = 0; i < TG3_RX_JUMBO_RING_SIZE; i++) {
Matt Carlson21f581a2009-08-28 14:00:25 +00005628 rxp = &tpr->rx_jmb_buffers[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005630 if (rxp->skb == NULL)
5631 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005633 pci_unmap_single(tp->pdev,
5634 pci_unmap_addr(rxp, mapping),
5635 TG3_RX_JMB_MAP_SZ,
5636 PCI_DMA_FROMDEVICE);
5637 dev_kfree_skb_any(rxp->skb);
5638 rxp->skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640 }
5641}
5642
5643/* Initialize tx/rx rings for packet processing.
5644 *
5645 * The chip has been shut down and the driver detached from
5646 * the networking, so no interrupts or new tx packets will
5647 * end up in the driver. tp->{tx,}lock are held and thus
5648 * we may not sleep.
5649 */
Matt Carlson21f581a2009-08-28 14:00:25 +00005650static int tg3_rx_prodring_alloc(struct tg3 *tp,
5651 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652{
Matt Carlson287be122009-08-28 13:58:46 +00005653 u32 i, rx_pkt_dma_sz;
Matt Carlson17375d22009-08-28 14:02:18 +00005654 struct tg3_napi *tnapi = &tp->napi[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656 /* Zero out all descriptors. */
Matt Carlson21f581a2009-08-28 14:00:25 +00005657 memset(tpr->rx_std, 0, TG3_RX_RING_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658
Matt Carlson287be122009-08-28 13:58:46 +00005659 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Michael Chana4e2b342005-10-26 15:46:52 -07005660 if ((tp->tg3_flags2 & TG3_FLG2_5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00005661 tp->dev->mtu > ETH_DATA_LEN)
5662 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
5663 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07005664
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665 /* Initialize invariants of the rings, we only set this
5666 * stuff once. This works because the card does not
5667 * write into the rx buffer posting rings.
5668 */
5669 for (i = 0; i < TG3_RX_RING_SIZE; i++) {
5670 struct tg3_rx_buffer_desc *rxd;
5671
Matt Carlson21f581a2009-08-28 14:00:25 +00005672 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00005673 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
5675 rxd->opaque = (RXD_OPAQUE_RING_STD |
5676 (i << RXD_OPAQUE_INDEX_SHIFT));
5677 }
5678
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005679 /* Now allocate fresh SKBs for each rx ring. */
5680 for (i = 0; i < tp->rx_pending; i++) {
Matt Carlson17375d22009-08-28 14:02:18 +00005681 if (tg3_alloc_rx_skb(tnapi, RXD_OPAQUE_RING_STD, -1, i) < 0) {
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005682 printk(KERN_WARNING PFX
5683 "%s: Using a smaller RX standard ring, "
5684 "only %d out of %d buffers were allocated "
5685 "successfully.\n",
5686 tp->dev->name, i, tp->rx_pending);
5687 if (i == 0)
5688 goto initfail;
5689 tp->rx_pending = i;
5690 break;
5691 }
5692 }
5693
5694 if (!(tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE))
5695 goto done;
5696
Matt Carlson21f581a2009-08-28 14:00:25 +00005697 memset(tpr->rx_jmb, 0, TG3_RX_JUMBO_RING_BYTES);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005698
Michael Chan0f893dc2005-07-25 12:30:38 -07005699 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700 for (i = 0; i < TG3_RX_JUMBO_RING_SIZE; i++) {
5701 struct tg3_rx_buffer_desc *rxd;
5702
Matt Carlson79ed5ac2009-08-28 14:00:55 +00005703 rxd = &tpr->rx_jmb[i].std;
Matt Carlson287be122009-08-28 13:58:46 +00005704 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
5706 RXD_FLAG_JUMBO;
5707 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
5708 (i << RXD_OPAQUE_INDEX_SHIFT));
5709 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711 for (i = 0; i < tp->rx_jumbo_pending; i++) {
Matt Carlson17375d22009-08-28 14:02:18 +00005712 if (tg3_alloc_rx_skb(tnapi, RXD_OPAQUE_RING_JUMBO,
Michael Chan32d8c572006-07-25 16:38:29 -07005713 -1, i) < 0) {
5714 printk(KERN_WARNING PFX
5715 "%s: Using a smaller RX jumbo ring, "
5716 "only %d out of %d buffers were "
5717 "allocated successfully.\n",
5718 tp->dev->name, i, tp->rx_jumbo_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005719 if (i == 0)
5720 goto initfail;
Michael Chan32d8c572006-07-25 16:38:29 -07005721 tp->rx_jumbo_pending = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722 break;
Michael Chan32d8c572006-07-25 16:38:29 -07005723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724 }
5725 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005726
5727done:
Michael Chan32d8c572006-07-25 16:38:29 -07005728 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005729
5730initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00005731 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005732 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005733}
5734
Matt Carlson21f581a2009-08-28 14:00:25 +00005735static void tg3_rx_prodring_fini(struct tg3 *tp,
5736 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737{
Matt Carlson21f581a2009-08-28 14:00:25 +00005738 kfree(tpr->rx_std_buffers);
5739 tpr->rx_std_buffers = NULL;
5740 kfree(tpr->rx_jmb_buffers);
5741 tpr->rx_jmb_buffers = NULL;
5742 if (tpr->rx_std) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743 pci_free_consistent(tp->pdev, TG3_RX_RING_BYTES,
Matt Carlson21f581a2009-08-28 14:00:25 +00005744 tpr->rx_std, tpr->rx_std_mapping);
5745 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746 }
Matt Carlson21f581a2009-08-28 14:00:25 +00005747 if (tpr->rx_jmb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748 pci_free_consistent(tp->pdev, TG3_RX_JUMBO_RING_BYTES,
Matt Carlson21f581a2009-08-28 14:00:25 +00005749 tpr->rx_jmb, tpr->rx_jmb_mapping);
5750 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005752}
5753
Matt Carlson21f581a2009-08-28 14:00:25 +00005754static int tg3_rx_prodring_init(struct tg3 *tp,
5755 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005756{
Matt Carlson21f581a2009-08-28 14:00:25 +00005757 tpr->rx_std_buffers = kzalloc(sizeof(struct ring_info) *
5758 TG3_RX_RING_SIZE, GFP_KERNEL);
5759 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005760 return -ENOMEM;
5761
Matt Carlson21f581a2009-08-28 14:00:25 +00005762 tpr->rx_std = pci_alloc_consistent(tp->pdev, TG3_RX_RING_BYTES,
5763 &tpr->rx_std_mapping);
5764 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005765 goto err_out;
5766
5767 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Matt Carlson21f581a2009-08-28 14:00:25 +00005768 tpr->rx_jmb_buffers = kzalloc(sizeof(struct ring_info) *
5769 TG3_RX_JUMBO_RING_SIZE,
5770 GFP_KERNEL);
5771 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005772 goto err_out;
5773
Matt Carlson21f581a2009-08-28 14:00:25 +00005774 tpr->rx_jmb = pci_alloc_consistent(tp->pdev,
5775 TG3_RX_JUMBO_RING_BYTES,
5776 &tpr->rx_jmb_mapping);
5777 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005778 goto err_out;
5779 }
5780
5781 return 0;
5782
5783err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00005784 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005785 return -ENOMEM;
5786}
5787
5788/* Free up pending packets in all rx/tx rings.
5789 *
5790 * The chip has been shut down and the driver detached from
5791 * the networking, so no interrupts or new tx packets will
5792 * end up in the driver. tp->{tx,}lock is not held and we are not
5793 * in an interrupt context and thus may sleep.
5794 */
5795static void tg3_free_rings(struct tg3 *tp)
5796{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005797 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005798
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005799 for (j = 0; j < tp->irq_cnt; j++) {
5800 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005801
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005802 if (!tnapi->tx_buffers)
5803 continue;
5804
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005805 for (i = 0; i < TG3_TX_RING_SIZE; ) {
5806 struct tx_ring_info *txp;
5807 struct sk_buff *skb;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005808
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005809 txp = &tnapi->tx_buffers[i];
5810 skb = txp->skb;
5811
5812 if (skb == NULL) {
5813 i++;
5814 continue;
5815 }
5816
5817 skb_dma_unmap(&tp->pdev->dev, skb, DMA_TO_DEVICE);
5818
5819 txp->skb = NULL;
5820
5821 i += skb_shinfo(skb)->nr_frags + 1;
5822
5823 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005824 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005825 }
5826
Matt Carlson21f581a2009-08-28 14:00:25 +00005827 tg3_rx_prodring_free(tp, &tp->prodring[0]);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005828}
5829
5830/* Initialize tx/rx rings for packet processing.
5831 *
5832 * The chip has been shut down and the driver detached from
5833 * the networking, so no interrupts or new tx packets will
5834 * end up in the driver. tp->{tx,}lock are held and thus
5835 * we may not sleep.
5836 */
5837static int tg3_init_rings(struct tg3 *tp)
5838{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005839 int i;
Matt Carlson72334482009-08-28 14:03:01 +00005840
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005841 /* Free up all the SKBs. */
5842 tg3_free_rings(tp);
5843
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005844 for (i = 0; i < tp->irq_cnt; i++) {
5845 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005846
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005847 tnapi->last_tag = 0;
5848 tnapi->last_irq_tag = 0;
5849 tnapi->hw_status->status = 0;
5850 tnapi->hw_status->status_tag = 0;
5851 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
5852
5853 tnapi->tx_prod = 0;
5854 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005855 if (tnapi->tx_ring)
5856 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005857
5858 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005859 if (tnapi->rx_rcb)
5860 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005861 }
Matt Carlson72334482009-08-28 14:03:01 +00005862
Matt Carlson21f581a2009-08-28 14:00:25 +00005863 return tg3_rx_prodring_alloc(tp, &tp->prodring[0]);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005864}
5865
5866/*
5867 * Must not be invoked with interrupt sources disabled and
5868 * the hardware shutdown down.
5869 */
5870static void tg3_free_consistent(struct tg3 *tp)
5871{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005872 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00005873
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005874 for (i = 0; i < tp->irq_cnt; i++) {
5875 struct tg3_napi *tnapi = &tp->napi[i];
5876
5877 if (tnapi->tx_ring) {
5878 pci_free_consistent(tp->pdev, TG3_TX_RING_BYTES,
5879 tnapi->tx_ring, tnapi->tx_desc_mapping);
5880 tnapi->tx_ring = NULL;
5881 }
5882
5883 kfree(tnapi->tx_buffers);
5884 tnapi->tx_buffers = NULL;
5885
5886 if (tnapi->rx_rcb) {
5887 pci_free_consistent(tp->pdev, TG3_RX_RCB_RING_BYTES(tp),
5888 tnapi->rx_rcb,
5889 tnapi->rx_rcb_mapping);
5890 tnapi->rx_rcb = NULL;
5891 }
5892
5893 if (tnapi->hw_status) {
5894 pci_free_consistent(tp->pdev, TG3_HW_STATUS_SIZE,
5895 tnapi->hw_status,
5896 tnapi->status_mapping);
5897 tnapi->hw_status = NULL;
5898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005900
Linus Torvalds1da177e2005-04-16 15:20:36 -07005901 if (tp->hw_stats) {
5902 pci_free_consistent(tp->pdev, sizeof(struct tg3_hw_stats),
5903 tp->hw_stats, tp->stats_mapping);
5904 tp->hw_stats = NULL;
5905 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005906
Matt Carlson21f581a2009-08-28 14:00:25 +00005907 tg3_rx_prodring_fini(tp, &tp->prodring[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908}
5909
5910/*
5911 * Must not be invoked with interrupt sources disabled and
5912 * the hardware shutdown down. Can sleep.
5913 */
5914static int tg3_alloc_consistent(struct tg3 *tp)
5915{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005916 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00005917
Matt Carlson21f581a2009-08-28 14:00:25 +00005918 if (tg3_rx_prodring_init(tp, &tp->prodring[0]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005919 return -ENOMEM;
5920
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921 tp->hw_stats = pci_alloc_consistent(tp->pdev,
5922 sizeof(struct tg3_hw_stats),
5923 &tp->stats_mapping);
5924 if (!tp->hw_stats)
5925 goto err_out;
5926
Linus Torvalds1da177e2005-04-16 15:20:36 -07005927 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
5928
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005929 for (i = 0; i < tp->irq_cnt; i++) {
5930 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005931 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005932
5933 tnapi->hw_status = pci_alloc_consistent(tp->pdev,
5934 TG3_HW_STATUS_SIZE,
5935 &tnapi->status_mapping);
5936 if (!tnapi->hw_status)
5937 goto err_out;
5938
5939 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005940 sblk = tnapi->hw_status;
5941
5942 /*
5943 * When RSS is enabled, the status block format changes
5944 * slightly. The "rx_jumbo_consumer", "reserved",
5945 * and "rx_mini_consumer" members get mapped to the
5946 * other three rx return ring producer indexes.
5947 */
5948 switch (i) {
5949 default:
5950 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
5951 break;
5952 case 2:
5953 tnapi->rx_rcb_prod_idx = &sblk->rx_jumbo_consumer;
5954 break;
5955 case 3:
5956 tnapi->rx_rcb_prod_idx = &sblk->reserved;
5957 break;
5958 case 4:
5959 tnapi->rx_rcb_prod_idx = &sblk->rx_mini_consumer;
5960 break;
5961 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005962
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005963 /*
5964 * If multivector RSS is enabled, vector 0 does not handle
5965 * rx or tx interrupts. Don't allocate any resources for it.
5966 */
5967 if (!i && (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS))
5968 continue;
5969
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005970 tnapi->rx_rcb = pci_alloc_consistent(tp->pdev,
5971 TG3_RX_RCB_RING_BYTES(tp),
5972 &tnapi->rx_rcb_mapping);
5973 if (!tnapi->rx_rcb)
5974 goto err_out;
5975
5976 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
5977
5978 tnapi->tx_buffers = kzalloc(sizeof(struct tx_ring_info) *
5979 TG3_TX_RING_SIZE, GFP_KERNEL);
5980 if (!tnapi->tx_buffers)
5981 goto err_out;
5982
5983 tnapi->tx_ring = pci_alloc_consistent(tp->pdev,
5984 TG3_TX_RING_BYTES,
5985 &tnapi->tx_desc_mapping);
5986 if (!tnapi->tx_ring)
5987 goto err_out;
5988 }
5989
Linus Torvalds1da177e2005-04-16 15:20:36 -07005990 return 0;
5991
5992err_out:
5993 tg3_free_consistent(tp);
5994 return -ENOMEM;
5995}
5996
5997#define MAX_WAIT_CNT 1000
5998
5999/* To stop a block, clear the enable bit and poll till it
6000 * clears. tp->lock is held.
6001 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006002static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006003{
6004 unsigned int i;
6005 u32 val;
6006
6007 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
6008 switch (ofs) {
6009 case RCVLSC_MODE:
6010 case DMAC_MODE:
6011 case MBFREE_MODE:
6012 case BUFMGR_MODE:
6013 case MEMARB_MODE:
6014 /* We can't enable/disable these bits of the
6015 * 5705/5750, just say success.
6016 */
6017 return 0;
6018
6019 default:
6020 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006021 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022 }
6023
6024 val = tr32(ofs);
6025 val &= ~enable_bit;
6026 tw32_f(ofs, val);
6027
6028 for (i = 0; i < MAX_WAIT_CNT; i++) {
6029 udelay(100);
6030 val = tr32(ofs);
6031 if ((val & enable_bit) == 0)
6032 break;
6033 }
6034
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006035 if (i == MAX_WAIT_CNT && !silent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036 printk(KERN_ERR PFX "tg3_stop_block timed out, "
6037 "ofs=%lx enable_bit=%x\n",
6038 ofs, enable_bit);
6039 return -ENODEV;
6040 }
6041
6042 return 0;
6043}
6044
6045/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006046static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047{
6048 int i, err;
6049
6050 tg3_disable_ints(tp);
6051
6052 tp->rx_mode &= ~RX_MODE_ENABLE;
6053 tw32_f(MAC_RX_MODE, tp->rx_mode);
6054 udelay(10);
6055
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006056 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
6057 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
6058 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
6059 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
6060 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
6061 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006063 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
6064 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
6065 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
6066 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
6067 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
6068 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
6069 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070
6071 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
6072 tw32_f(MAC_MODE, tp->mac_mode);
6073 udelay(40);
6074
6075 tp->tx_mode &= ~TX_MODE_ENABLE;
6076 tw32_f(MAC_TX_MODE, tp->tx_mode);
6077
6078 for (i = 0; i < MAX_WAIT_CNT; i++) {
6079 udelay(100);
6080 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
6081 break;
6082 }
6083 if (i >= MAX_WAIT_CNT) {
6084 printk(KERN_ERR PFX "tg3_abort_hw timed out for %s, "
6085 "TX_MODE_ENABLE will not clear MAC_TX_MODE=%08x\n",
6086 tp->dev->name, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07006087 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006088 }
6089
Michael Chane6de8ad2005-05-05 14:42:41 -07006090 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006091 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
6092 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006093
6094 tw32(FTQ_RESET, 0xffffffff);
6095 tw32(FTQ_RESET, 0x00000000);
6096
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006097 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
6098 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006099
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006100 for (i = 0; i < tp->irq_cnt; i++) {
6101 struct tg3_napi *tnapi = &tp->napi[i];
6102 if (tnapi->hw_status)
6103 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006105 if (tp->hw_stats)
6106 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6107
Linus Torvalds1da177e2005-04-16 15:20:36 -07006108 return err;
6109}
6110
Matt Carlson0d3031d2007-10-10 18:02:43 -07006111static void tg3_ape_send_event(struct tg3 *tp, u32 event)
6112{
6113 int i;
6114 u32 apedata;
6115
6116 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
6117 if (apedata != APE_SEG_SIG_MAGIC)
6118 return;
6119
6120 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
Matt Carlson731fd792008-08-15 14:07:51 -07006121 if (!(apedata & APE_FW_STATUS_READY))
Matt Carlson0d3031d2007-10-10 18:02:43 -07006122 return;
6123
6124 /* Wait for up to 1 millisecond for APE to service previous event. */
6125 for (i = 0; i < 10; i++) {
6126 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
6127 return;
6128
6129 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
6130
6131 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6132 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
6133 event | APE_EVENT_STATUS_EVENT_PENDING);
6134
6135 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
6136
6137 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6138 break;
6139
6140 udelay(100);
6141 }
6142
6143 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6144 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
6145}
6146
6147static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
6148{
6149 u32 event;
6150 u32 apedata;
6151
6152 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
6153 return;
6154
6155 switch (kind) {
6156 case RESET_KIND_INIT:
6157 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
6158 APE_HOST_SEG_SIG_MAGIC);
6159 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
6160 APE_HOST_SEG_LEN_MAGIC);
6161 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
6162 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
6163 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
6164 APE_HOST_DRIVER_ID_MAGIC);
6165 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
6166 APE_HOST_BEHAV_NO_PHYLOCK);
6167
6168 event = APE_EVENT_STATUS_STATE_START;
6169 break;
6170 case RESET_KIND_SHUTDOWN:
Matt Carlsonb2aee152008-11-03 16:51:11 -08006171 /* With the interface we are currently using,
6172 * APE does not track driver state. Wiping
6173 * out the HOST SEGMENT SIGNATURE forces
6174 * the APE to assume OS absent status.
6175 */
6176 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
6177
Matt Carlson0d3031d2007-10-10 18:02:43 -07006178 event = APE_EVENT_STATUS_STATE_UNLOAD;
6179 break;
6180 case RESET_KIND_SUSPEND:
6181 event = APE_EVENT_STATUS_STATE_SUSPEND;
6182 break;
6183 default:
6184 return;
6185 }
6186
6187 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
6188
6189 tg3_ape_send_event(tp, event);
6190}
6191
Michael Chane6af3012005-04-21 17:12:05 -07006192/* tp->lock is held. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006193static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
6194{
David S. Millerf49639e2006-06-09 11:58:36 -07006195 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
6196 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006197
6198 if (tp->tg3_flags2 & TG3_FLG2_ASF_NEW_HANDSHAKE) {
6199 switch (kind) {
6200 case RESET_KIND_INIT:
6201 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6202 DRV_STATE_START);
6203 break;
6204
6205 case RESET_KIND_SHUTDOWN:
6206 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6207 DRV_STATE_UNLOAD);
6208 break;
6209
6210 case RESET_KIND_SUSPEND:
6211 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6212 DRV_STATE_SUSPEND);
6213 break;
6214
6215 default:
6216 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07006219
6220 if (kind == RESET_KIND_INIT ||
6221 kind == RESET_KIND_SUSPEND)
6222 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006223}
6224
6225/* tp->lock is held. */
6226static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
6227{
6228 if (tp->tg3_flags2 & TG3_FLG2_ASF_NEW_HANDSHAKE) {
6229 switch (kind) {
6230 case RESET_KIND_INIT:
6231 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6232 DRV_STATE_START_DONE);
6233 break;
6234
6235 case RESET_KIND_SHUTDOWN:
6236 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6237 DRV_STATE_UNLOAD_DONE);
6238 break;
6239
6240 default:
6241 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006243 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07006244
6245 if (kind == RESET_KIND_SHUTDOWN)
6246 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006247}
6248
6249/* tp->lock is held. */
6250static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
6251{
6252 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) {
6253 switch (kind) {
6254 case RESET_KIND_INIT:
6255 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6256 DRV_STATE_START);
6257 break;
6258
6259 case RESET_KIND_SHUTDOWN:
6260 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6261 DRV_STATE_UNLOAD);
6262 break;
6263
6264 case RESET_KIND_SUSPEND:
6265 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6266 DRV_STATE_SUSPEND);
6267 break;
6268
6269 default:
6270 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006272 }
6273}
6274
Michael Chan7a6f4362006-09-27 16:03:31 -07006275static int tg3_poll_fw(struct tg3 *tp)
6276{
6277 int i;
6278 u32 val;
6279
Michael Chanb5d37722006-09-27 16:06:21 -07006280 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Gary Zambrano0ccead12006-11-14 16:34:00 -08006281 /* Wait up to 20ms for init done. */
6282 for (i = 0; i < 200; i++) {
Michael Chanb5d37722006-09-27 16:06:21 -07006283 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
6284 return 0;
Gary Zambrano0ccead12006-11-14 16:34:00 -08006285 udelay(100);
Michael Chanb5d37722006-09-27 16:06:21 -07006286 }
6287 return -ENODEV;
6288 }
6289
Michael Chan7a6f4362006-09-27 16:03:31 -07006290 /* Wait for firmware initialization to complete. */
6291 for (i = 0; i < 100000; i++) {
6292 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
6293 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
6294 break;
6295 udelay(10);
6296 }
6297
6298 /* Chip might not be fitted with firmware. Some Sun onboard
6299 * parts are configured like that. So don't signal the timeout
6300 * of the above loop as an error, but do report the lack of
6301 * running firmware once.
6302 */
6303 if (i >= 100000 &&
6304 !(tp->tg3_flags2 & TG3_FLG2_NO_FWARE_REPORTED)) {
6305 tp->tg3_flags2 |= TG3_FLG2_NO_FWARE_REPORTED;
6306
6307 printk(KERN_INFO PFX "%s: No firmware running.\n",
6308 tp->dev->name);
6309 }
6310
6311 return 0;
6312}
6313
Michael Chanee6a99b2007-07-18 21:49:10 -07006314/* Save PCI command register before chip reset */
6315static void tg3_save_pci_state(struct tg3 *tp)
6316{
Matt Carlson8a6eac92007-10-21 16:17:55 -07006317 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07006318}
6319
6320/* Restore PCI state after chip reset */
6321static void tg3_restore_pci_state(struct tg3 *tp)
6322{
6323 u32 val;
6324
6325 /* Re-enable indirect register accesses. */
6326 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
6327 tp->misc_host_ctrl);
6328
6329 /* Set MAX PCI retry to zero. */
6330 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
6331 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
6332 (tp->tg3_flags & TG3_FLAG_PCIX_MODE))
6333 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07006334 /* Allow reads and writes to the APE register and memory space. */
6335 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
6336 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
6337 PCISTATE_ALLOW_APE_SHMEM_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07006338 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
6339
Matt Carlson8a6eac92007-10-21 16:17:55 -07006340 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07006341
Matt Carlsonfcb389d2008-11-03 16:55:44 -08006342 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785) {
6343 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)
6344 pcie_set_readrq(tp->pdev, 4096);
6345 else {
6346 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
6347 tp->pci_cacheline_sz);
6348 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
6349 tp->pci_lat_timer);
6350 }
Michael Chan114342f2007-10-15 02:12:26 -07006351 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08006352
Michael Chanee6a99b2007-07-18 21:49:10 -07006353 /* Make sure PCI-X relaxed ordering bit is clear. */
Matt Carlson52f44902008-11-21 17:17:04 -08006354 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
Matt Carlson9974a352007-10-07 23:27:28 -07006355 u16 pcix_cmd;
6356
6357 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
6358 &pcix_cmd);
6359 pcix_cmd &= ~PCI_X_CMD_ERO;
6360 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
6361 pcix_cmd);
6362 }
Michael Chanee6a99b2007-07-18 21:49:10 -07006363
6364 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
Michael Chanee6a99b2007-07-18 21:49:10 -07006365
6366 /* Chip reset on 5780 will reset MSI enable bit,
6367 * so need to restore it.
6368 */
6369 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
6370 u16 ctrl;
6371
6372 pci_read_config_word(tp->pdev,
6373 tp->msi_cap + PCI_MSI_FLAGS,
6374 &ctrl);
6375 pci_write_config_word(tp->pdev,
6376 tp->msi_cap + PCI_MSI_FLAGS,
6377 ctrl | PCI_MSI_FLAGS_ENABLE);
6378 val = tr32(MSGINT_MODE);
6379 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
6380 }
6381 }
6382}
6383
Linus Torvalds1da177e2005-04-16 15:20:36 -07006384static void tg3_stop_fw(struct tg3 *);
6385
6386/* tp->lock is held. */
6387static int tg3_chip_reset(struct tg3 *tp)
6388{
6389 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07006390 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00006391 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006392
David S. Millerf49639e2006-06-09 11:58:36 -07006393 tg3_nvram_lock(tp);
6394
Matt Carlson77b483f2008-08-15 14:07:24 -07006395 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
6396
David S. Millerf49639e2006-06-09 11:58:36 -07006397 /* No matching tg3_nvram_unlock() after this because
6398 * chip reset below will undo the nvram lock.
6399 */
6400 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006401
Michael Chanee6a99b2007-07-18 21:49:10 -07006402 /* GRC_MISC_CFG core clock reset will clear the memory
6403 * enable bit in PCI register 4 and the MSI enable bit
6404 * on some chips, so we save relevant registers here.
6405 */
6406 tg3_save_pci_state(tp);
6407
Michael Chand9ab5ad2006-03-20 22:27:35 -08006408 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08006409 (tp->tg3_flags3 & TG3_FLG3_5755_PLUS))
Michael Chand9ab5ad2006-03-20 22:27:35 -08006410 tw32(GRC_FASTBOOT_PC, 0);
6411
Linus Torvalds1da177e2005-04-16 15:20:36 -07006412 /*
6413 * We must avoid the readl() that normally takes place.
6414 * It locks machines, causes machine checks, and other
6415 * fun things. So, temporarily disable the 5701
6416 * hardware workaround, while we do the reset.
6417 */
Michael Chan1ee582d2005-08-09 20:16:46 -07006418 write_op = tp->write32;
6419 if (write_op == tg3_write_flush_reg32)
6420 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006421
Michael Chand18edcb2007-03-24 20:57:11 -07006422 /* Prevent the irq handler from reading or writing PCI registers
6423 * during chip reset when the memory enable bit in the PCI command
6424 * register may be cleared. The chip does not generate interrupt
6425 * at this time, but the irq handler may still be called due to irq
6426 * sharing or irqpoll.
6427 */
6428 tp->tg3_flags |= TG3_FLAG_CHIP_RESETTING;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006429 for (i = 0; i < tp->irq_cnt; i++) {
6430 struct tg3_napi *tnapi = &tp->napi[i];
6431 if (tnapi->hw_status) {
6432 tnapi->hw_status->status = 0;
6433 tnapi->hw_status->status_tag = 0;
6434 }
6435 tnapi->last_tag = 0;
6436 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07006437 }
Michael Chand18edcb2007-03-24 20:57:11 -07006438 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00006439
6440 for (i = 0; i < tp->irq_cnt; i++)
6441 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07006442
Matt Carlson255ca312009-08-25 10:07:27 +00006443 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
6444 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
6445 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
6446 }
6447
Linus Torvalds1da177e2005-04-16 15:20:36 -07006448 /* do the reset */
6449 val = GRC_MISC_CFG_CORECLK_RESET;
6450
6451 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
6452 if (tr32(0x7e2c) == 0x60) {
6453 tw32(0x7e2c, 0x20);
6454 }
6455 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
6456 tw32(GRC_MISC_CFG, (1 << 29));
6457 val |= (1 << 29);
6458 }
6459 }
6460
Michael Chanb5d37722006-09-27 16:06:21 -07006461 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
6462 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
6463 tw32(GRC_VCPU_EXT_CTRL,
6464 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
6465 }
6466
Linus Torvalds1da177e2005-04-16 15:20:36 -07006467 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
6468 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
6469 tw32(GRC_MISC_CFG, val);
6470
Michael Chan1ee582d2005-08-09 20:16:46 -07006471 /* restore 5701 hardware bug workaround write method */
6472 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006473
6474 /* Unfortunately, we have to delay before the PCI read back.
6475 * Some 575X chips even will not respond to a PCI cfg access
6476 * when the reset command is given to the chip.
6477 *
6478 * How do these hardware designers expect things to work
6479 * properly if the PCI write is posted for a long period
6480 * of time? It is always necessary to have some method by
6481 * which a register read back can occur to push the write
6482 * out which does the reset.
6483 *
6484 * For most tg3 variants the trick below was working.
6485 * Ho hum...
6486 */
6487 udelay(120);
6488
6489 /* Flush PCI posted writes. The normal MMIO registers
6490 * are inaccessible at this time so this is the only
6491 * way to make this reliably (actually, this is no longer
6492 * the case, see above). I tried to use indirect
6493 * register read/write but this upset some 5701 variants.
6494 */
6495 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
6496
6497 udelay(120);
6498
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006499 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) && tp->pcie_cap) {
Matt Carlsone7126992009-08-25 10:08:16 +00006500 u16 val16;
6501
Linus Torvalds1da177e2005-04-16 15:20:36 -07006502 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
6503 int i;
6504 u32 cfg_val;
6505
6506 /* Wait for link training to complete. */
6507 for (i = 0; i < 5000; i++)
6508 udelay(100);
6509
6510 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
6511 pci_write_config_dword(tp->pdev, 0xc4,
6512 cfg_val | (1 << 15));
6513 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006514
Matt Carlsone7126992009-08-25 10:08:16 +00006515 /* Clear the "no snoop" and "relaxed ordering" bits. */
6516 pci_read_config_word(tp->pdev,
6517 tp->pcie_cap + PCI_EXP_DEVCTL,
6518 &val16);
6519 val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
6520 PCI_EXP_DEVCTL_NOSNOOP_EN);
6521 /*
6522 * Older PCIe devices only support the 128 byte
6523 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006524 */
Matt Carlsone7126992009-08-25 10:08:16 +00006525 if (!(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
6526 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784))
6527 val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006528 pci_write_config_word(tp->pdev,
6529 tp->pcie_cap + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00006530 val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006531
6532 pcie_set_readrq(tp->pdev, 4096);
6533
6534 /* Clear error status */
6535 pci_write_config_word(tp->pdev,
6536 tp->pcie_cap + PCI_EXP_DEVSTA,
6537 PCI_EXP_DEVSTA_CED |
6538 PCI_EXP_DEVSTA_NFED |
6539 PCI_EXP_DEVSTA_FED |
6540 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006541 }
6542
Michael Chanee6a99b2007-07-18 21:49:10 -07006543 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006544
Michael Chand18edcb2007-03-24 20:57:11 -07006545 tp->tg3_flags &= ~TG3_FLAG_CHIP_RESETTING;
6546
Michael Chanee6a99b2007-07-18 21:49:10 -07006547 val = 0;
6548 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
Michael Chan4cf78e42005-07-25 12:29:19 -07006549 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07006550 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006551
6552 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
6553 tg3_stop_fw(tp);
6554 tw32(0x5000, 0x400);
6555 }
6556
6557 tw32(GRC_MODE, tp->grc_mode);
6558
6559 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01006560 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006561
6562 tw32(0xc4, val | (1 << 15));
6563 }
6564
6565 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
6566 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
6567 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
6568 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
6569 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
6570 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
6571 }
6572
6573 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
6574 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
6575 tw32_f(MAC_MODE, tp->mac_mode);
Michael Chan747e8f82005-07-25 12:33:22 -07006576 } else if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES) {
6577 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
6578 tw32_f(MAC_MODE, tp->mac_mode);
Matt Carlson3bda1252008-08-15 14:08:22 -07006579 } else if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
6580 tp->mac_mode &= (MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN);
6581 if (tp->mac_mode & MAC_MODE_APE_TX_EN)
6582 tp->mac_mode |= MAC_MODE_TDE_ENABLE;
6583 tw32_f(MAC_MODE, tp->mac_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006584 } else
6585 tw32_f(MAC_MODE, 0);
6586 udelay(40);
6587
Matt Carlson77b483f2008-08-15 14:07:24 -07006588 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
6589
Michael Chan7a6f4362006-09-27 16:03:31 -07006590 err = tg3_poll_fw(tp);
6591 if (err)
6592 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006593
Matt Carlson0a9140c2009-08-28 12:27:50 +00006594 tg3_mdio_start(tp);
6595
Matt Carlson52cdf852009-11-02 14:25:06 +00006596 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
6597 u8 phy_addr;
6598
6599 phy_addr = tp->phy_addr;
6600 tp->phy_addr = TG3_PHY_PCIE_ADDR;
6601
6602 tg3_writephy(tp, TG3_PCIEPHY_BLOCK_ADDR,
6603 TG3_PCIEPHY_TXB_BLK << TG3_PCIEPHY_BLOCK_SHIFT);
6604 val = TG3_PCIEPHY_TX0CTRL1_TXOCM | TG3_PCIEPHY_TX0CTRL1_RDCTL |
6605 TG3_PCIEPHY_TX0CTRL1_TXCMV | TG3_PCIEPHY_TX0CTRL1_TKSEL |
6606 TG3_PCIEPHY_TX0CTRL1_NB_EN;
6607 tg3_writephy(tp, TG3_PCIEPHY_TX0CTRL1, val);
6608 udelay(10);
6609
6610 tg3_writephy(tp, TG3_PCIEPHY_BLOCK_ADDR,
6611 TG3_PCIEPHY_XGXS_BLK1 << TG3_PCIEPHY_BLOCK_SHIFT);
6612 val = TG3_PCIEPHY_PWRMGMT4_LOWPWR_EN |
6613 TG3_PCIEPHY_PWRMGMT4_L1PLLPD_EN;
6614 tg3_writephy(tp, TG3_PCIEPHY_PWRMGMT4, val);
6615 udelay(10);
6616
6617 tp->phy_addr = phy_addr;
6618 }
6619
Linus Torvalds1da177e2005-04-16 15:20:36 -07006620 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00006621 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
6622 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
6623 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01006624 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006625
6626 tw32(0x7c00, val | (1 << 25));
6627 }
6628
6629 /* Reprobe ASF enable state. */
6630 tp->tg3_flags &= ~TG3_FLAG_ENABLE_ASF;
6631 tp->tg3_flags2 &= ~TG3_FLG2_ASF_NEW_HANDSHAKE;
6632 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
6633 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
6634 u32 nic_cfg;
6635
6636 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
6637 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
6638 tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
Matt Carlson4ba526c2008-08-15 14:10:04 -07006639 tp->last_event_jiffies = jiffies;
John W. Linvillecbf46852005-04-21 17:01:29 -07006640 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006641 tp->tg3_flags2 |= TG3_FLG2_ASF_NEW_HANDSHAKE;
6642 }
6643 }
6644
6645 return 0;
6646}
6647
6648/* tp->lock is held. */
6649static void tg3_stop_fw(struct tg3 *tp)
6650{
Matt Carlson0d3031d2007-10-10 18:02:43 -07006651 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
6652 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07006653 /* Wait for RX cpu to ACK the previous event. */
6654 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006655
6656 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
Matt Carlson4ba526c2008-08-15 14:10:04 -07006657
6658 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006659
Matt Carlson7c5026a2008-05-02 16:49:29 -07006660 /* Wait for RX cpu to ACK this event. */
6661 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006662 }
6663}
6664
6665/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07006666static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006667{
6668 int err;
6669
6670 tg3_stop_fw(tp);
6671
Michael Chan944d9802005-05-29 14:57:48 -07006672 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006673
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006674 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006675 err = tg3_chip_reset(tp);
6676
Matt Carlsondaba2a62009-04-20 06:58:52 +00006677 __tg3_set_mac_addr(tp, 0);
6678
Michael Chan944d9802005-05-29 14:57:48 -07006679 tg3_write_sig_legacy(tp, kind);
6680 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006681
6682 if (err)
6683 return err;
6684
6685 return 0;
6686}
6687
Linus Torvalds1da177e2005-04-16 15:20:36 -07006688#define RX_CPU_SCRATCH_BASE 0x30000
6689#define RX_CPU_SCRATCH_SIZE 0x04000
6690#define TX_CPU_SCRATCH_BASE 0x34000
6691#define TX_CPU_SCRATCH_SIZE 0x04000
6692
6693/* tp->lock is held. */
6694static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
6695{
6696 int i;
6697
Eric Sesterhenn5d9428d2006-04-02 13:52:48 +02006698 BUG_ON(offset == TX_CPU_BASE &&
6699 (tp->tg3_flags2 & TG3_FLG2_5705_PLUS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006700
Michael Chanb5d37722006-09-27 16:06:21 -07006701 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
6702 u32 val = tr32(GRC_VCPU_EXT_CTRL);
6703
6704 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
6705 return 0;
6706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006707 if (offset == RX_CPU_BASE) {
6708 for (i = 0; i < 10000; i++) {
6709 tw32(offset + CPU_STATE, 0xffffffff);
6710 tw32(offset + CPU_MODE, CPU_MODE_HALT);
6711 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
6712 break;
6713 }
6714
6715 tw32(offset + CPU_STATE, 0xffffffff);
6716 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
6717 udelay(10);
6718 } else {
6719 for (i = 0; i < 10000; i++) {
6720 tw32(offset + CPU_STATE, 0xffffffff);
6721 tw32(offset + CPU_MODE, CPU_MODE_HALT);
6722 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
6723 break;
6724 }
6725 }
6726
6727 if (i >= 10000) {
6728 printk(KERN_ERR PFX "tg3_reset_cpu timed out for %s, "
6729 "and %s CPU\n",
6730 tp->dev->name,
6731 (offset == RX_CPU_BASE ? "RX" : "TX"));
6732 return -ENODEV;
6733 }
Michael Chanec41c7d2006-01-17 02:40:55 -08006734
6735 /* Clear firmware's nvram arbitration. */
6736 if (tp->tg3_flags & TG3_FLAG_NVRAM)
6737 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006738 return 0;
6739}
6740
6741struct fw_info {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006742 unsigned int fw_base;
6743 unsigned int fw_len;
6744 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006745};
6746
6747/* tp->lock is held. */
6748static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base, u32 cpu_scratch_base,
6749 int cpu_scratch_size, struct fw_info *info)
6750{
Michael Chanec41c7d2006-01-17 02:40:55 -08006751 int err, lock_err, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006752 void (*write_op)(struct tg3 *, u32, u32);
6753
6754 if (cpu_base == TX_CPU_BASE &&
6755 (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
6756 printk(KERN_ERR PFX "tg3_load_firmware_cpu: Trying to load "
6757 "TX cpu firmware on %s which is 5705.\n",
6758 tp->dev->name);
6759 return -EINVAL;
6760 }
6761
6762 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
6763 write_op = tg3_write_mem;
6764 else
6765 write_op = tg3_write_indirect_reg32;
6766
Michael Chan1b628152005-05-29 14:59:49 -07006767 /* It is possible that bootcode is still loading at this point.
6768 * Get the nvram lock first before halting the cpu.
6769 */
Michael Chanec41c7d2006-01-17 02:40:55 -08006770 lock_err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006771 err = tg3_halt_cpu(tp, cpu_base);
Michael Chanec41c7d2006-01-17 02:40:55 -08006772 if (!lock_err)
6773 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006774 if (err)
6775 goto out;
6776
6777 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
6778 write_op(tp, cpu_scratch_base + i, 0);
6779 tw32(cpu_base + CPU_STATE, 0xffffffff);
6780 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006781 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006782 write_op(tp, (cpu_scratch_base +
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006783 (info->fw_base & 0xffff) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07006784 (i * sizeof(u32))),
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006785 be32_to_cpu(info->fw_data[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006786
6787 err = 0;
6788
6789out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006790 return err;
6791}
6792
6793/* tp->lock is held. */
6794static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
6795{
6796 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006797 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006798 int err, i;
6799
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006800 fw_data = (void *)tp->fw->data;
6801
6802 /* Firmware blob starts with version numbers, followed by
6803 start address and length. We are setting complete length.
6804 length = end_address_of_bss - start_address_of_text.
6805 Remainder is the blob to be loaded contiguously
6806 from start address. */
6807
6808 info.fw_base = be32_to_cpu(fw_data[1]);
6809 info.fw_len = tp->fw->size - 12;
6810 info.fw_data = &fw_data[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006811
6812 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
6813 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
6814 &info);
6815 if (err)
6816 return err;
6817
6818 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
6819 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
6820 &info);
6821 if (err)
6822 return err;
6823
6824 /* Now startup only the RX cpu. */
6825 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006826 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006827
6828 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006829 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006830 break;
6831 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
6832 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006833 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006834 udelay(1000);
6835 }
6836 if (i >= 5) {
6837 printk(KERN_ERR PFX "tg3_load_firmware fails for %s "
6838 "to set RX CPU PC, is %08x should be %08x\n",
6839 tp->dev->name, tr32(RX_CPU_BASE + CPU_PC),
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006840 info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006841 return -ENODEV;
6842 }
6843 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
6844 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
6845
6846 return 0;
6847}
6848
Linus Torvalds1da177e2005-04-16 15:20:36 -07006849/* 5705 needs a special version of the TSO firmware. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006850
6851/* tp->lock is held. */
6852static int tg3_load_tso_firmware(struct tg3 *tp)
6853{
6854 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006855 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006856 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
6857 int err, i;
6858
6859 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
6860 return 0;
6861
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006862 fw_data = (void *)tp->fw->data;
6863
6864 /* Firmware blob starts with version numbers, followed by
6865 start address and length. We are setting complete length.
6866 length = end_address_of_bss - start_address_of_text.
6867 Remainder is the blob to be loaded contiguously
6868 from start address. */
6869
6870 info.fw_base = be32_to_cpu(fw_data[1]);
6871 cpu_scratch_size = tp->fw_len;
6872 info.fw_len = tp->fw->size - 12;
6873 info.fw_data = &fw_data[3];
6874
Linus Torvalds1da177e2005-04-16 15:20:36 -07006875 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006876 cpu_base = RX_CPU_BASE;
6877 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006878 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006879 cpu_base = TX_CPU_BASE;
6880 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
6881 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
6882 }
6883
6884 err = tg3_load_firmware_cpu(tp, cpu_base,
6885 cpu_scratch_base, cpu_scratch_size,
6886 &info);
6887 if (err)
6888 return err;
6889
6890 /* Now startup the cpu. */
6891 tw32(cpu_base + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006892 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006893
6894 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006895 if (tr32(cpu_base + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006896 break;
6897 tw32(cpu_base + CPU_STATE, 0xffffffff);
6898 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006899 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006900 udelay(1000);
6901 }
6902 if (i >= 5) {
6903 printk(KERN_ERR PFX "tg3_load_tso_firmware fails for %s "
6904 "to set CPU PC, is %08x should be %08x\n",
6905 tp->dev->name, tr32(cpu_base + CPU_PC),
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006906 info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006907 return -ENODEV;
6908 }
6909 tw32(cpu_base + CPU_STATE, 0xffffffff);
6910 tw32_f(cpu_base + CPU_MODE, 0x00000000);
6911 return 0;
6912}
6913
Linus Torvalds1da177e2005-04-16 15:20:36 -07006914
Linus Torvalds1da177e2005-04-16 15:20:36 -07006915static int tg3_set_mac_addr(struct net_device *dev, void *p)
6916{
6917 struct tg3 *tp = netdev_priv(dev);
6918 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07006919 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006920
Michael Chanf9804dd2005-09-27 12:13:10 -07006921 if (!is_valid_ether_addr(addr->sa_data))
6922 return -EINVAL;
6923
Linus Torvalds1da177e2005-04-16 15:20:36 -07006924 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
6925
Michael Chane75f7c92006-03-20 21:33:26 -08006926 if (!netif_running(dev))
6927 return 0;
6928
Michael Chan58712ef2006-04-29 18:58:01 -07006929 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) {
Michael Chan986e0ae2007-05-05 12:10:20 -07006930 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07006931
Michael Chan986e0ae2007-05-05 12:10:20 -07006932 addr0_high = tr32(MAC_ADDR_0_HIGH);
6933 addr0_low = tr32(MAC_ADDR_0_LOW);
6934 addr1_high = tr32(MAC_ADDR_1_HIGH);
6935 addr1_low = tr32(MAC_ADDR_1_LOW);
6936
6937 /* Skip MAC addr 1 if ASF is using it. */
6938 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
6939 !(addr1_high == 0 && addr1_low == 0))
6940 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07006941 }
Michael Chan986e0ae2007-05-05 12:10:20 -07006942 spin_lock_bh(&tp->lock);
6943 __tg3_set_mac_addr(tp, skip_mac_1);
6944 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006945
Michael Chanb9ec6c12006-07-25 16:37:27 -07006946 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006947}
6948
6949/* tp->lock is held. */
6950static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
6951 dma_addr_t mapping, u32 maxlen_flags,
6952 u32 nic_addr)
6953{
6954 tg3_write_mem(tp,
6955 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
6956 ((u64) mapping >> 32));
6957 tg3_write_mem(tp,
6958 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
6959 ((u64) mapping & 0xffffffff));
6960 tg3_write_mem(tp,
6961 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
6962 maxlen_flags);
6963
6964 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
6965 tg3_write_mem(tp,
6966 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
6967 nic_addr);
6968}
6969
6970static void __tg3_set_rx_mode(struct net_device *);
Michael Chand244c892005-07-05 14:42:33 -07006971static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07006972{
Matt Carlsonb6080e12009-09-01 13:12:00 +00006973 int i;
6974
6975 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSIX)) {
6976 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
6977 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
6978 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
6979
6980 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
6981 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
6982 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
6983 } else {
6984 tw32(HOSTCC_TXCOL_TICKS, 0);
6985 tw32(HOSTCC_TXMAX_FRAMES, 0);
6986 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
6987
6988 tw32(HOSTCC_RXCOL_TICKS, 0);
6989 tw32(HOSTCC_RXMAX_FRAMES, 0);
6990 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07006991 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00006992
David S. Miller15f98502005-05-18 22:49:26 -07006993 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
6994 u32 val = ec->stats_block_coalesce_usecs;
6995
Matt Carlsonb6080e12009-09-01 13:12:00 +00006996 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
6997 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
6998
David S. Miller15f98502005-05-18 22:49:26 -07006999 if (!netif_carrier_ok(tp->dev))
7000 val = 0;
7001
7002 tw32(HOSTCC_STAT_COAL_TICKS, val);
7003 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007004
7005 for (i = 0; i < tp->irq_cnt - 1; i++) {
7006 u32 reg;
7007
7008 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
7009 tw32(reg, ec->rx_coalesce_usecs);
7010 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
7011 tw32(reg, ec->tx_coalesce_usecs);
7012 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
7013 tw32(reg, ec->rx_max_coalesced_frames);
7014 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
7015 tw32(reg, ec->tx_max_coalesced_frames);
7016 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
7017 tw32(reg, ec->rx_max_coalesced_frames_irq);
7018 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
7019 tw32(reg, ec->tx_max_coalesced_frames_irq);
7020 }
7021
7022 for (; i < tp->irq_max - 1; i++) {
7023 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
7024 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
7025 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
7026 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
7027 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
7028 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
7029 }
David S. Miller15f98502005-05-18 22:49:26 -07007030}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007031
7032/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00007033static void tg3_rings_reset(struct tg3 *tp)
7034{
7035 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007036 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007037 struct tg3_napi *tnapi = &tp->napi[0];
7038
7039 /* Disable all transmit rings but the first. */
7040 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7041 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
7042 else
7043 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7044
7045 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7046 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
7047 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
7048 BDINFO_FLAGS_DISABLED);
7049
7050
7051 /* Disable all receive return rings but the first. */
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007052 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
7053 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
7054 else if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00007055 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
7056 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
7057 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
7058 else
7059 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7060
7061 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7062 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
7063 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
7064 BDINFO_FLAGS_DISABLED);
7065
7066 /* Disable interrupts */
7067 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
7068
7069 /* Zero mailbox registers. */
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007070 if (tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX) {
7071 for (i = 1; i < TG3_IRQ_MAX_VECS; i++) {
7072 tp->napi[i].tx_prod = 0;
7073 tp->napi[i].tx_cons = 0;
7074 tw32_mailbox(tp->napi[i].prodmbox, 0);
7075 tw32_rx_mbox(tp->napi[i].consmbox, 0);
7076 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
7077 }
7078 } else {
7079 tp->napi[0].tx_prod = 0;
7080 tp->napi[0].tx_cons = 0;
7081 tw32_mailbox(tp->napi[0].prodmbox, 0);
7082 tw32_rx_mbox(tp->napi[0].consmbox, 0);
7083 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007084
7085 /* Make sure the NIC-based send BD rings are disabled. */
7086 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7087 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
7088 for (i = 0; i < 16; i++)
7089 tw32_tx_mbox(mbox + i * 8, 0);
7090 }
7091
7092 txrcb = NIC_SRAM_SEND_RCB;
7093 rxrcb = NIC_SRAM_RCV_RET_RCB;
7094
7095 /* Clear status block in ram. */
7096 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7097
7098 /* Set status block DMA address */
7099 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
7100 ((u64) tnapi->status_mapping >> 32));
7101 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
7102 ((u64) tnapi->status_mapping & 0xffffffff));
7103
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007104 if (tnapi->tx_ring) {
7105 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7106 (TG3_TX_RING_SIZE <<
7107 BDINFO_FLAGS_MAXLEN_SHIFT),
7108 NIC_SRAM_TX_BUFFER_DESC);
7109 txrcb += TG3_BDINFO_SIZE;
7110 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007111
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007112 if (tnapi->rx_rcb) {
7113 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
7114 (TG3_RX_RCB_RING_SIZE(tp) <<
7115 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
7116 rxrcb += TG3_BDINFO_SIZE;
7117 }
7118
7119 stblk = HOSTCC_STATBLCK_RING1;
7120
7121 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
7122 u64 mapping = (u64)tnapi->status_mapping;
7123 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
7124 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
7125
7126 /* Clear status block in ram. */
7127 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7128
7129 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7130 (TG3_TX_RING_SIZE <<
7131 BDINFO_FLAGS_MAXLEN_SHIFT),
7132 NIC_SRAM_TX_BUFFER_DESC);
7133
7134 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
7135 (TG3_RX_RCB_RING_SIZE(tp) <<
7136 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
7137
7138 stblk += 8;
7139 txrcb += TG3_BDINFO_SIZE;
7140 rxrcb += TG3_BDINFO_SIZE;
7141 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007142}
7143
7144/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07007145static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007146{
7147 u32 val, rdmac_mode;
7148 int i, err, limit;
Matt Carlson21f581a2009-08-28 14:00:25 +00007149 struct tg3_rx_prodring_set *tpr = &tp->prodring[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007150
7151 tg3_disable_ints(tp);
7152
7153 tg3_stop_fw(tp);
7154
7155 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
7156
7157 if (tp->tg3_flags & TG3_FLAG_INIT_COMPLETE) {
Michael Chane6de8ad2005-05-05 14:42:41 -07007158 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007159 }
7160
Matt Carlsondd477002008-05-25 23:45:58 -07007161 if (reset_phy &&
7162 !(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB))
Michael Chand4d2c552006-03-20 17:47:20 -08007163 tg3_phy_reset(tp);
7164
Linus Torvalds1da177e2005-04-16 15:20:36 -07007165 err = tg3_chip_reset(tp);
7166 if (err)
7167 return err;
7168
7169 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
7170
Matt Carlsonbcb37f62008-11-03 16:52:09 -08007171 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07007172 val = tr32(TG3_CPMU_CTRL);
7173 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
7174 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08007175
7176 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
7177 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
7178 val |= CPMU_LSPD_10MB_MACCLK_6_25;
7179 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
7180
7181 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
7182 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
7183 val |= CPMU_LNK_AWARE_MACCLK_6_25;
7184 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
7185
7186 val = tr32(TG3_CPMU_HST_ACC);
7187 val &= ~CPMU_HST_ACC_MACCLK_MASK;
7188 val |= CPMU_HST_ACC_MACCLK_6_25;
7189 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07007190 }
7191
Matt Carlson33466d92009-04-20 06:57:41 +00007192 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7193 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
7194 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
7195 PCIE_PWR_MGMT_L1_THRESH_4MS;
7196 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00007197
7198 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
7199 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
7200
7201 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00007202
Matt Carlsonf40386c2009-11-02 14:24:02 +00007203 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7204 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00007205 }
7206
Linus Torvalds1da177e2005-04-16 15:20:36 -07007207 /* This works around an issue with Athlon chipsets on
7208 * B3 tigon3 silicon. This bit has no effect on any
7209 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07007210 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007211 */
Matt Carlson795d01c2007-10-07 23:28:17 -07007212 if (!(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT)) {
7213 if (!(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
7214 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
7215 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007217
7218 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
7219 (tp->tg3_flags & TG3_FLAG_PCIX_MODE)) {
7220 val = tr32(TG3PCI_PCISTATE);
7221 val |= PCISTATE_RETRY_SAME_DMA;
7222 tw32(TG3PCI_PCISTATE, val);
7223 }
7224
Matt Carlson0d3031d2007-10-10 18:02:43 -07007225 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
7226 /* Allow reads and writes to the
7227 * APE register and memory space.
7228 */
7229 val = tr32(TG3PCI_PCISTATE);
7230 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
7231 PCISTATE_ALLOW_APE_SHMEM_WR;
7232 tw32(TG3PCI_PCISTATE, val);
7233 }
7234
Linus Torvalds1da177e2005-04-16 15:20:36 -07007235 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
7236 /* Enable some hw fixes. */
7237 val = tr32(TG3PCI_MSI_DATA);
7238 val |= (1 << 26) | (1 << 28) | (1 << 29);
7239 tw32(TG3PCI_MSI_DATA, val);
7240 }
7241
7242 /* Descriptor ring init may make accesses to the
7243 * NIC SRAM area to setup the TX descriptors, so we
7244 * can only do this after the hardware has been
7245 * successfully reset.
7246 */
Michael Chan32d8c572006-07-25 16:38:29 -07007247 err = tg3_init_rings(tp);
7248 if (err)
7249 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007250
Matt Carlson9936bcf2007-10-10 18:03:07 -07007251 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007252 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761 &&
7253 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07007254 /* This value is determined during the probe time DMA
7255 * engine test, tg3_test_dma.
7256 */
7257 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
7258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007259
7260 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
7261 GRC_MODE_4X_NIC_SEND_RINGS |
7262 GRC_MODE_NO_TX_PHDR_CSUM |
7263 GRC_MODE_NO_RX_PHDR_CSUM);
7264 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07007265
7266 /* Pseudo-header checksum is done by hardware logic and not
7267 * the offload processers, so make the chip do the pseudo-
7268 * header checksums on receive. For transmit it is more
7269 * convenient to do the pseudo-header checksum in software
7270 * as Linux does that on transmit for us in all cases.
7271 */
7272 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007273
7274 tw32(GRC_MODE,
7275 tp->grc_mode |
7276 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
7277
7278 /* Setup the timer prescalar register. Clock is always 66Mhz. */
7279 val = tr32(GRC_MISC_CFG);
7280 val &= ~0xff;
7281 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
7282 tw32(GRC_MISC_CFG, val);
7283
7284 /* Initialize MBUF/DESC pool. */
John W. Linvillecbf46852005-04-21 17:01:29 -07007285 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007286 /* Do nothing. */
7287 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
7288 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
7289 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
7290 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
7291 else
7292 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
7293 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
7294 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
7295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007296 else if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) {
7297 int fw_len;
7298
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007299 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007300 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
7301 tw32(BUFMGR_MB_POOL_ADDR,
7302 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
7303 tw32(BUFMGR_MB_POOL_SIZE,
7304 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
7305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007306
Michael Chan0f893dc2005-07-25 12:30:38 -07007307 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007308 tw32(BUFMGR_MB_RDMA_LOW_WATER,
7309 tp->bufmgr_config.mbuf_read_dma_low_water);
7310 tw32(BUFMGR_MB_MACRX_LOW_WATER,
7311 tp->bufmgr_config.mbuf_mac_rx_low_water);
7312 tw32(BUFMGR_MB_HIGH_WATER,
7313 tp->bufmgr_config.mbuf_high_water);
7314 } else {
7315 tw32(BUFMGR_MB_RDMA_LOW_WATER,
7316 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
7317 tw32(BUFMGR_MB_MACRX_LOW_WATER,
7318 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
7319 tw32(BUFMGR_MB_HIGH_WATER,
7320 tp->bufmgr_config.mbuf_high_water_jumbo);
7321 }
7322 tw32(BUFMGR_DMA_LOW_WATER,
7323 tp->bufmgr_config.dma_low_water);
7324 tw32(BUFMGR_DMA_HIGH_WATER,
7325 tp->bufmgr_config.dma_high_water);
7326
7327 tw32(BUFMGR_MODE, BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE);
7328 for (i = 0; i < 2000; i++) {
7329 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
7330 break;
7331 udelay(10);
7332 }
7333 if (i >= 2000) {
7334 printk(KERN_ERR PFX "tg3_reset_hw cannot enable BUFMGR for %s.\n",
7335 tp->dev->name);
7336 return -ENODEV;
7337 }
7338
7339 /* Setup replenish threshold. */
Michael Chanf92905d2006-06-29 20:14:29 -07007340 val = tp->rx_pending / 8;
7341 if (val == 0)
7342 val = 1;
7343 else if (val > tp->rx_std_max_post)
7344 val = tp->rx_std_max_post;
Michael Chanb5d37722006-09-27 16:06:21 -07007345 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7346 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
7347 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
7348
7349 if (val > (TG3_RX_INTERNAL_RING_SZ_5906 / 2))
7350 val = TG3_RX_INTERNAL_RING_SZ_5906 / 2;
7351 }
Michael Chanf92905d2006-06-29 20:14:29 -07007352
7353 tw32(RCVBDI_STD_THRESH, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007354
7355 /* Initialize TG3_BDINFO's at:
7356 * RCVDBDI_STD_BD: standard eth size rx ring
7357 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
7358 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
7359 *
7360 * like so:
7361 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
7362 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
7363 * ring attribute flags
7364 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
7365 *
7366 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
7367 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
7368 *
7369 * The size of each ring is fixed in the firmware, but the location is
7370 * configurable.
7371 */
7372 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00007373 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007374 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00007375 ((u64) tpr->rx_std_mapping & 0xffffffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007376 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
7377 NIC_SRAM_RX_BUFFER_DESC);
7378
Matt Carlsonfdb72b32009-08-28 13:57:12 +00007379 /* Disable the mini ring */
7380 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007381 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
7382 BDINFO_FLAGS_DISABLED);
7383
Matt Carlsonfdb72b32009-08-28 13:57:12 +00007384 /* Program the jumbo buffer descriptor ring control
7385 * blocks on those devices that have them.
7386 */
Matt Carlson8f666b02009-08-28 13:58:24 +00007387 if ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) &&
Matt Carlsonfdb72b32009-08-28 13:57:12 +00007388 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007389 /* Setup replenish threshold. */
7390 tw32(RCVBDI_JUMBO_THRESH, tp->rx_jumbo_pending / 8);
7391
Michael Chan0f893dc2005-07-25 12:30:38 -07007392 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007393 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00007394 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007395 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00007396 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007397 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlson79ed5ac2009-08-28 14:00:55 +00007398 (RX_JUMBO_MAX_SIZE << BDINFO_FLAGS_MAXLEN_SHIFT) |
7399 BDINFO_FLAGS_USE_EXT_RECV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007400 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
7401 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
7402 } else {
7403 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
7404 BDINFO_FLAGS_DISABLED);
7405 }
7406
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007407 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
7408 val = (RX_STD_MAX_SIZE_5705 << BDINFO_FLAGS_MAXLEN_SHIFT) |
7409 (RX_STD_MAX_SIZE << 2);
7410 else
7411 val = RX_STD_MAX_SIZE << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00007412 } else
7413 val = RX_STD_MAX_SIZE_5705 << BDINFO_FLAGS_MAXLEN_SHIFT;
7414
7415 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007416
Matt Carlson21f581a2009-08-28 14:00:25 +00007417 tpr->rx_std_ptr = tp->rx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007418 tw32_rx_mbox(MAILBOX_RCV_STD_PROD_IDX + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00007419 tpr->rx_std_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007420
Matt Carlson21f581a2009-08-28 14:00:25 +00007421 tpr->rx_jmb_ptr = (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) ?
7422 tp->rx_jumbo_pending : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007423 tw32_rx_mbox(MAILBOX_RCV_JUMBO_PROD_IDX + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00007424 tpr->rx_jmb_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007425
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007426 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
7427 tw32(STD_REPLENISH_LWM, 32);
7428 tw32(JMB_REPLENISH_LWM, 16);
7429 }
7430
Matt Carlson2d31eca2009-09-01 12:53:31 +00007431 tg3_rings_reset(tp);
7432
Linus Torvalds1da177e2005-04-16 15:20:36 -07007433 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07007434 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007435
7436 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00007437 tw32(MAC_RX_MTU_SIZE,
7438 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007439
7440 /* The slot time is changed by tg3_setup_phy if we
7441 * run at gigabit with half duplex.
7442 */
7443 tw32(MAC_TX_LENGTHS,
7444 (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
7445 (6 << TX_LENGTHS_IPG_SHIFT) |
7446 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
7447
7448 /* Receive rules. */
7449 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
7450 tw32(RCVLPC_CONFIG, 0x0181);
7451
7452 /* Calculate RDMAC_MODE setting early, we need it to determine
7453 * the RCVLPC_STATE_ENABLE mask.
7454 */
7455 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
7456 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
7457 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
7458 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
7459 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07007460
Matt Carlson57e69832008-05-25 23:48:31 -07007461 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08007462 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
7463 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07007464 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
7465 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
7466 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
7467
Michael Chan85e94ce2005-04-21 17:05:28 -07007468 /* If statement applies to 5705 and 5750 PCI devices only */
7469 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
7470 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) ||
7471 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007472 if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07007473 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007474 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
7475 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
7476 !(tp->tg3_flags2 & TG3_FLG2_IS_5788)) {
7477 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
7478 }
7479 }
7480
Michael Chan85e94ce2005-04-21 17:05:28 -07007481 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)
7482 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
7483
Linus Torvalds1da177e2005-04-16 15:20:36 -07007484 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
Matt Carlson027455a2008-12-21 20:19:30 -08007485 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
7486
7487 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
7488 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
7489 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007490
7491 /* Receive/send statistics. */
Michael Chan16613942006-06-29 20:15:13 -07007492 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
7493 val = tr32(RCVLPC_STATS_ENABLE);
7494 val &= ~RCVLPC_STATSENAB_DACK_FIX;
7495 tw32(RCVLPC_STATS_ENABLE, val);
7496 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
7497 (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007498 val = tr32(RCVLPC_STATS_ENABLE);
7499 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
7500 tw32(RCVLPC_STATS_ENABLE, val);
7501 } else {
7502 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
7503 }
7504 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
7505 tw32(SNDDATAI_STATSENAB, 0xffffff);
7506 tw32(SNDDATAI_STATSCTRL,
7507 (SNDDATAI_SCTRL_ENABLE |
7508 SNDDATAI_SCTRL_FASTUPD));
7509
7510 /* Setup host coalescing engine. */
7511 tw32(HOSTCC_MODE, 0);
7512 for (i = 0; i < 2000; i++) {
7513 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
7514 break;
7515 udelay(10);
7516 }
7517
Michael Chand244c892005-07-05 14:42:33 -07007518 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007519
Linus Torvalds1da177e2005-04-16 15:20:36 -07007520 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7521 /* Status/statistics block address. See tg3_timer,
7522 * the tg3_periodic_fetch_stats call there, and
7523 * tg3_get_stats to see how this works for 5705/5750 chips.
7524 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007525 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
7526 ((u64) tp->stats_mapping >> 32));
7527 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
7528 ((u64) tp->stats_mapping & 0xffffffff));
7529 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00007530
Linus Torvalds1da177e2005-04-16 15:20:36 -07007531 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00007532
7533 /* Clear statistics and status block memory areas */
7534 for (i = NIC_SRAM_STATS_BLK;
7535 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
7536 i += sizeof(u32)) {
7537 tg3_write_mem(tp, i, 0);
7538 udelay(40);
7539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007540 }
7541
7542 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
7543
7544 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
7545 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
7546 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7547 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
7548
Michael Chanc94e3942005-09-27 12:12:42 -07007549 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES) {
7550 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
7551 /* reset to prevent losing 1st rx packet intermittently */
7552 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
7553 udelay(10);
7554 }
7555
Matt Carlson3bda1252008-08-15 14:08:22 -07007556 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
7557 tp->mac_mode &= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
7558 else
7559 tp->mac_mode = 0;
7560 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Linus Torvalds1da177e2005-04-16 15:20:36 -07007561 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE | MAC_MODE_FHDE_ENABLE;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07007562 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
7563 !(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
7564 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
7565 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007566 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
7567 udelay(40);
7568
Michael Chan314fba32005-04-21 17:07:04 -07007569 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Michael Chan9d26e212006-12-07 00:21:14 -08007570 * If TG3_FLG2_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07007571 * register to preserve the GPIO settings for LOMs. The GPIOs,
7572 * whether used as inputs or outputs, are set by boot code after
7573 * reset.
7574 */
Michael Chan9d26e212006-12-07 00:21:14 -08007575 if (!(tp->tg3_flags2 & TG3_FLG2_IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07007576 u32 gpio_mask;
7577
Michael Chan9d26e212006-12-07 00:21:14 -08007578 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
7579 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
7580 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07007581
7582 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
7583 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
7584 GRC_LCLCTRL_GPIO_OUTPUT3;
7585
Michael Chanaf36e6b2006-03-23 01:28:06 -08007586 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
7587 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
7588
Gary Zambranoaaf84462007-05-05 11:51:45 -07007589 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07007590 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
7591
7592 /* GPIO1 must be driven high for eeprom write protect */
Michael Chan9d26e212006-12-07 00:21:14 -08007593 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT)
7594 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
7595 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07007596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007597 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
7598 udelay(100);
7599
Matt Carlsonbaf8a942009-09-01 13:13:00 +00007600 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX) {
7601 val = tr32(MSGINT_MODE);
7602 val |= MSGINT_MODE_MULTIVEC_EN | MSGINT_MODE_ENABLE;
7603 tw32(MSGINT_MODE, val);
7604 }
7605
Linus Torvalds1da177e2005-04-16 15:20:36 -07007606 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7607 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
7608 udelay(40);
7609 }
7610
7611 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
7612 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
7613 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
7614 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
7615 WDMAC_MODE_LNGREAD_ENAB);
7616
Michael Chan85e94ce2005-04-21 17:05:28 -07007617 /* If statement applies to 5705 and 5750 PCI devices only */
7618 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
7619 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) ||
7620 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750) {
Matt Carlson29ea0952009-08-25 10:07:54 +00007621 if ((tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07007622 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
7623 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
7624 /* nothing */
7625 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
7626 !(tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
7627 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)) {
7628 val |= WDMAC_MODE_RX_ACCEL;
7629 }
7630 }
7631
Michael Chand9ab5ad2006-03-20 22:27:35 -08007632 /* Enable host coalescing bug fix */
Matt Carlson321d32a2008-11-21 17:22:19 -08007633 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Matt Carlsonf51f3562008-05-25 23:45:08 -07007634 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad2006-03-20 22:27:35 -08007635
Linus Torvalds1da177e2005-04-16 15:20:36 -07007636 tw32_f(WDMAC_MODE, val);
7637 udelay(40);
7638
Matt Carlson9974a352007-10-07 23:27:28 -07007639 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
7640 u16 pcix_cmd;
7641
7642 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7643 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007644 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07007645 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
7646 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007647 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07007648 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
7649 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007650 }
Matt Carlson9974a352007-10-07 23:27:28 -07007651 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7652 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007653 }
7654
7655 tw32_f(RDMAC_MODE, rdmac_mode);
7656 udelay(40);
7657
7658 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
7659 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7660 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07007661
7662 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
7663 tw32(SNDDATAC_MODE,
7664 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
7665 else
7666 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
7667
Linus Torvalds1da177e2005-04-16 15:20:36 -07007668 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
7669 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
7670 tw32(RCVDBDI_MODE, RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ);
7671 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007672 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
7673 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00007674 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
7675 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
7676 val |= SNDBDI_MODE_MULTI_TXQ_EN;
7677 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007678 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
7679
7680 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
7681 err = tg3_load_5701_a0_firmware_fix(tp);
7682 if (err)
7683 return err;
7684 }
7685
Linus Torvalds1da177e2005-04-16 15:20:36 -07007686 if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) {
7687 err = tg3_load_tso_firmware(tp);
7688 if (err)
7689 return err;
7690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007691
7692 tp->tx_mode = TX_MODE_ENABLE;
7693 tw32_f(MAC_TX_MODE, tp->tx_mode);
7694 udelay(100);
7695
Matt Carlsonbaf8a942009-09-01 13:13:00 +00007696 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS) {
7697 u32 reg = MAC_RSS_INDIR_TBL_0;
7698 u8 *ent = (u8 *)&val;
7699
7700 /* Setup the indirection table */
7701 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
7702 int idx = i % sizeof(val);
7703
7704 ent[idx] = i % (tp->irq_cnt - 1);
7705 if (idx == sizeof(val) - 1) {
7706 tw32(reg, val);
7707 reg += 4;
7708 }
7709 }
7710
7711 /* Setup the "secret" hash key. */
7712 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
7713 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
7714 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
7715 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
7716 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
7717 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
7718 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
7719 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
7720 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
7721 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
7722 }
7723
Linus Torvalds1da177e2005-04-16 15:20:36 -07007724 tp->rx_mode = RX_MODE_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08007725 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Michael Chanaf36e6b2006-03-23 01:28:06 -08007726 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
7727
Matt Carlsonbaf8a942009-09-01 13:13:00 +00007728 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)
7729 tp->rx_mode |= RX_MODE_RSS_ENABLE |
7730 RX_MODE_RSS_ITBL_HASH_BITS_7 |
7731 RX_MODE_RSS_IPV6_HASH_EN |
7732 RX_MODE_RSS_TCP_IPV6_HASH_EN |
7733 RX_MODE_RSS_IPV4_HASH_EN |
7734 RX_MODE_RSS_TCP_IPV4_HASH_EN;
7735
Linus Torvalds1da177e2005-04-16 15:20:36 -07007736 tw32_f(MAC_RX_MODE, tp->rx_mode);
7737 udelay(10);
7738
Linus Torvalds1da177e2005-04-16 15:20:36 -07007739 tw32(MAC_LED_CTRL, tp->led_ctrl);
7740
7741 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Michael Chanc94e3942005-09-27 12:12:42 -07007742 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007743 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
7744 udelay(10);
7745 }
7746 tw32_f(MAC_RX_MODE, tp->rx_mode);
7747 udelay(10);
7748
7749 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
7750 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
7751 !(tp->tg3_flags2 & TG3_FLG2_SERDES_PREEMPHASIS)) {
7752 /* Set drive transmission level to 1.2V */
7753 /* only if the signal pre-emphasis bit is not set */
7754 val = tr32(MAC_SERDES_CFG);
7755 val &= 0xfffff000;
7756 val |= 0x880;
7757 tw32(MAC_SERDES_CFG, val);
7758 }
7759 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
7760 tw32(MAC_SERDES_CFG, 0x616000);
7761 }
7762
7763 /* Prevent chip from dropping frames when flow control
7764 * is enabled.
7765 */
7766 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, 2);
7767
7768 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
7769 (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)) {
7770 /* Use hardware link auto-negotiation */
7771 tp->tg3_flags2 |= TG3_FLG2_HW_AUTONEG;
7772 }
7773
Michael Chand4d2c552006-03-20 17:47:20 -08007774 if ((tp->tg3_flags2 & TG3_FLG2_MII_SERDES) &&
7775 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)) {
7776 u32 tmp;
7777
7778 tmp = tr32(SERDES_RX_CTRL);
7779 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
7780 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
7781 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
7782 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
7783 }
7784
Matt Carlsondd477002008-05-25 23:45:58 -07007785 if (!(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)) {
7786 if (tp->link_config.phy_is_low_power) {
7787 tp->link_config.phy_is_low_power = 0;
7788 tp->link_config.speed = tp->link_config.orig_speed;
7789 tp->link_config.duplex = tp->link_config.orig_duplex;
7790 tp->link_config.autoneg = tp->link_config.orig_autoneg;
7791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007792
Matt Carlsondd477002008-05-25 23:45:58 -07007793 err = tg3_setup_phy(tp, 0);
7794 if (err)
7795 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007796
Matt Carlsondd477002008-05-25 23:45:58 -07007797 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
Matt Carlson7f97a4b2009-08-25 10:10:03 +00007798 !(tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07007799 u32 tmp;
7800
7801 /* Clear CRC stats. */
7802 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
7803 tg3_writephy(tp, MII_TG3_TEST1,
7804 tmp | MII_TG3_TEST1_CRC_EN);
7805 tg3_readphy(tp, 0x14, &tmp);
7806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007807 }
7808 }
7809
7810 __tg3_set_rx_mode(tp->dev);
7811
7812 /* Initialize receive rules. */
7813 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
7814 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
7815 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
7816 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
7817
Michael Chan4cf78e42005-07-25 12:29:19 -07007818 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Michael Chana4e2b342005-10-26 15:46:52 -07007819 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007820 limit = 8;
7821 else
7822 limit = 16;
7823 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF)
7824 limit -= 4;
7825 switch (limit) {
7826 case 16:
7827 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
7828 case 15:
7829 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
7830 case 14:
7831 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
7832 case 13:
7833 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
7834 case 12:
7835 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
7836 case 11:
7837 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
7838 case 10:
7839 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
7840 case 9:
7841 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
7842 case 8:
7843 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
7844 case 7:
7845 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
7846 case 6:
7847 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
7848 case 5:
7849 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
7850 case 4:
7851 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
7852 case 3:
7853 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
7854 case 2:
7855 case 1:
7856
7857 default:
7858 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007859 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007860
Matt Carlson9ce768e2007-10-11 19:49:11 -07007861 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
7862 /* Write our heartbeat update interval to APE. */
7863 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
7864 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07007865
Linus Torvalds1da177e2005-04-16 15:20:36 -07007866 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
7867
Linus Torvalds1da177e2005-04-16 15:20:36 -07007868 return 0;
7869}
7870
7871/* Called at device open time to get the chip ready for
7872 * packet processing. Invoked with tp->lock held.
7873 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07007874static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007875{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007876 tg3_switch_clocks(tp);
7877
7878 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
7879
Matt Carlson2f751b62008-08-04 23:17:34 -07007880 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007881}
7882
7883#define TG3_STAT_ADD32(PSTAT, REG) \
7884do { u32 __val = tr32(REG); \
7885 (PSTAT)->low += __val; \
7886 if ((PSTAT)->low < __val) \
7887 (PSTAT)->high += 1; \
7888} while (0)
7889
7890static void tg3_periodic_fetch_stats(struct tg3 *tp)
7891{
7892 struct tg3_hw_stats *sp = tp->hw_stats;
7893
7894 if (!netif_carrier_ok(tp->dev))
7895 return;
7896
7897 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
7898 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
7899 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
7900 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
7901 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
7902 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
7903 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
7904 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
7905 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
7906 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
7907 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
7908 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
7909 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
7910
7911 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
7912 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
7913 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
7914 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
7915 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
7916 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
7917 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
7918 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
7919 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
7920 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
7921 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
7922 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
7923 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
7924 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07007925
7926 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
7927 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
7928 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007929}
7930
7931static void tg3_timer(unsigned long __opaque)
7932{
7933 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007934
Michael Chanf475f162006-03-27 23:20:14 -08007935 if (tp->irq_sync)
7936 goto restart_timer;
7937
David S. Millerf47c11e2005-06-24 20:18:35 -07007938 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007939
David S. Millerfac9b832005-05-18 22:46:34 -07007940 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)) {
7941 /* All of this garbage is because when using non-tagged
7942 * IRQ status the mailbox/status_block protocol the chip
7943 * uses with the cpu is race prone.
7944 */
Matt Carlson898a56f2009-08-28 14:02:40 +00007945 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -07007946 tw32(GRC_LOCAL_CTRL,
7947 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
7948 } else {
7949 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00007950 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -07007951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007952
David S. Millerfac9b832005-05-18 22:46:34 -07007953 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
7954 tp->tg3_flags2 |= TG3_FLG2_RESTART_TIMER;
David S. Millerf47c11e2005-06-24 20:18:35 -07007955 spin_unlock(&tp->lock);
David S. Millerfac9b832005-05-18 22:46:34 -07007956 schedule_work(&tp->reset_task);
7957 return;
7958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007959 }
7960
Linus Torvalds1da177e2005-04-16 15:20:36 -07007961 /* This part only runs once per second. */
7962 if (!--tp->timer_counter) {
David S. Millerfac9b832005-05-18 22:46:34 -07007963 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
7964 tg3_periodic_fetch_stats(tp);
7965
Linus Torvalds1da177e2005-04-16 15:20:36 -07007966 if (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) {
7967 u32 mac_stat;
7968 int phy_event;
7969
7970 mac_stat = tr32(MAC_STATUS);
7971
7972 phy_event = 0;
7973 if (tp->tg3_flags & TG3_FLAG_USE_MI_INTERRUPT) {
7974 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
7975 phy_event = 1;
7976 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
7977 phy_event = 1;
7978
7979 if (phy_event)
7980 tg3_setup_phy(tp, 0);
7981 } else if (tp->tg3_flags & TG3_FLAG_POLL_SERDES) {
7982 u32 mac_stat = tr32(MAC_STATUS);
7983 int need_setup = 0;
7984
7985 if (netif_carrier_ok(tp->dev) &&
7986 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
7987 need_setup = 1;
7988 }
7989 if (! netif_carrier_ok(tp->dev) &&
7990 (mac_stat & (MAC_STATUS_PCS_SYNCED |
7991 MAC_STATUS_SIGNAL_DET))) {
7992 need_setup = 1;
7993 }
7994 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07007995 if (!tp->serdes_counter) {
7996 tw32_f(MAC_MODE,
7997 (tp->mac_mode &
7998 ~MAC_MODE_PORT_MODE_MASK));
7999 udelay(40);
8000 tw32_f(MAC_MODE, tp->mac_mode);
8001 udelay(40);
8002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008003 tg3_setup_phy(tp, 0);
8004 }
Michael Chan747e8f82005-07-25 12:33:22 -07008005 } else if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)
8006 tg3_serdes_parallel_detect(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008007
8008 tp->timer_counter = tp->timer_multiplier;
8009 }
8010
Michael Chan130b8e42006-09-27 16:00:40 -07008011 /* Heartbeat is only sent once every 2 seconds.
8012 *
8013 * The heartbeat is to tell the ASF firmware that the host
8014 * driver is still alive. In the event that the OS crashes,
8015 * ASF needs to reset the hardware to free up the FIFO space
8016 * that may be filled with rx packets destined for the host.
8017 * If the FIFO is full, ASF will no longer function properly.
8018 *
8019 * Unintended resets have been reported on real time kernels
8020 * where the timer doesn't run on time. Netpoll will also have
8021 * same problem.
8022 *
8023 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
8024 * to check the ring condition when the heartbeat is expiring
8025 * before doing the reset. This will prevent most unintended
8026 * resets.
8027 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008028 if (!--tp->asf_counter) {
Matt Carlsonbc7959b2008-08-15 14:08:55 -07008029 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
8030 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07008031 tg3_wait_for_event_ack(tp);
8032
Michael Chanbbadf502006-04-06 21:46:34 -07008033 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07008034 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07008035 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Michael Chan28fbef72005-10-26 15:48:35 -07008036 /* 5 seconds timeout */
Michael Chanbbadf502006-04-06 21:46:34 -07008037 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX, 5);
Matt Carlson4ba526c2008-08-15 14:10:04 -07008038
8039 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008040 }
8041 tp->asf_counter = tp->asf_multiplier;
8042 }
8043
David S. Millerf47c11e2005-06-24 20:18:35 -07008044 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008045
Michael Chanf475f162006-03-27 23:20:14 -08008046restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07008047 tp->timer.expires = jiffies + tp->timer_offset;
8048 add_timer(&tp->timer);
8049}
8050
Matt Carlson4f125f42009-09-01 12:55:02 +00008051static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -08008052{
David Howells7d12e782006-10-05 14:55:46 +01008053 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008054 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +00008055 char *name;
8056 struct tg3_napi *tnapi = &tp->napi[irq_num];
8057
8058 if (tp->irq_cnt == 1)
8059 name = tp->dev->name;
8060 else {
8061 name = &tnapi->irq_lbl[0];
8062 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
8063 name[IFNAMSIZ-1] = 0;
8064 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08008065
Matt Carlson679563f2009-09-01 12:55:46 +00008066 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI_OR_MSIX) {
Michael Chanfcfa0a32006-03-20 22:28:41 -08008067 fn = tg3_msi;
8068 if (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)
8069 fn = tg3_msi_1shot;
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07008070 flags = IRQF_SAMPLE_RANDOM;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008071 } else {
8072 fn = tg3_interrupt;
8073 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)
8074 fn = tg3_interrupt_tagged;
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07008075 flags = IRQF_SHARED | IRQF_SAMPLE_RANDOM;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008076 }
Matt Carlson4f125f42009-09-01 12:55:02 +00008077
8078 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08008079}
8080
Michael Chan79381092005-04-21 17:13:59 -07008081static int tg3_test_interrupt(struct tg3 *tp)
8082{
Matt Carlson09943a12009-08-28 14:01:57 +00008083 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -07008084 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -07008085 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008086 u32 val;
Michael Chan79381092005-04-21 17:13:59 -07008087
Michael Chand4bc3922005-05-29 14:59:20 -07008088 if (!netif_running(dev))
8089 return -ENODEV;
8090
Michael Chan79381092005-04-21 17:13:59 -07008091 tg3_disable_ints(tp);
8092
Matt Carlson4f125f42009-09-01 12:55:02 +00008093 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07008094
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008095 /*
8096 * Turn off MSI one shot mode. Otherwise this test has no
8097 * observable way to know whether the interrupt was delivered.
8098 */
8099 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 &&
8100 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
8101 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
8102 tw32(MSGINT_MODE, val);
8103 }
8104
Matt Carlson4f125f42009-09-01 12:55:02 +00008105 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Matt Carlson09943a12009-08-28 14:01:57 +00008106 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07008107 if (err)
8108 return err;
8109
Matt Carlson898a56f2009-08-28 14:02:40 +00008110 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -07008111 tg3_enable_ints(tp);
8112
8113 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00008114 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -07008115
8116 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -07008117 u32 int_mbox, misc_host_ctrl;
8118
Matt Carlson898a56f2009-08-28 14:02:40 +00008119 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -07008120 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
8121
8122 if ((int_mbox != 0) ||
8123 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
8124 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -07008125 break;
Michael Chanb16250e2006-09-27 16:10:14 -07008126 }
8127
Michael Chan79381092005-04-21 17:13:59 -07008128 msleep(10);
8129 }
8130
8131 tg3_disable_ints(tp);
8132
Matt Carlson4f125f42009-09-01 12:55:02 +00008133 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008134
Matt Carlson4f125f42009-09-01 12:55:02 +00008135 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07008136
8137 if (err)
8138 return err;
8139
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008140 if (intr_ok) {
8141 /* Reenable MSI one shot mode. */
8142 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 &&
8143 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
8144 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
8145 tw32(MSGINT_MODE, val);
8146 }
Michael Chan79381092005-04-21 17:13:59 -07008147 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008148 }
Michael Chan79381092005-04-21 17:13:59 -07008149
8150 return -EIO;
8151}
8152
8153/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
8154 * successfully restored
8155 */
8156static int tg3_test_msi(struct tg3 *tp)
8157{
Michael Chan79381092005-04-21 17:13:59 -07008158 int err;
8159 u16 pci_cmd;
8160
8161 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSI))
8162 return 0;
8163
8164 /* Turn off SERR reporting in case MSI terminates with Master
8165 * Abort.
8166 */
8167 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
8168 pci_write_config_word(tp->pdev, PCI_COMMAND,
8169 pci_cmd & ~PCI_COMMAND_SERR);
8170
8171 err = tg3_test_interrupt(tp);
8172
8173 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
8174
8175 if (!err)
8176 return 0;
8177
8178 /* other failures */
8179 if (err != -EIO)
8180 return err;
8181
8182 /* MSI test failed, go back to INTx mode */
8183 printk(KERN_WARNING PFX "%s: No interrupt was generated using MSI, "
8184 "switching to INTx mode. Please report this failure to "
8185 "the PCI maintainer and include system chipset information.\n",
8186 tp->dev->name);
8187
Matt Carlson4f125f42009-09-01 12:55:02 +00008188 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +00008189
Michael Chan79381092005-04-21 17:13:59 -07008190 pci_disable_msi(tp->pdev);
8191
8192 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI;
8193
Matt Carlson4f125f42009-09-01 12:55:02 +00008194 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07008195 if (err)
8196 return err;
8197
8198 /* Need to reset the chip because the MSI cycle may have terminated
8199 * with Master Abort.
8200 */
David S. Millerf47c11e2005-06-24 20:18:35 -07008201 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07008202
Michael Chan944d9802005-05-29 14:57:48 -07008203 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008204 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07008205
David S. Millerf47c11e2005-06-24 20:18:35 -07008206 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07008207
8208 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +00008209 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -07008210
8211 return err;
8212}
8213
Matt Carlson9e9fd122009-01-19 16:57:45 -08008214static int tg3_request_firmware(struct tg3 *tp)
8215{
8216 const __be32 *fw_data;
8217
8218 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
8219 printk(KERN_ERR "%s: Failed to load firmware \"%s\"\n",
8220 tp->dev->name, tp->fw_needed);
8221 return -ENOENT;
8222 }
8223
8224 fw_data = (void *)tp->fw->data;
8225
8226 /* Firmware blob starts with version numbers, followed by
8227 * start address and _full_ length including BSS sections
8228 * (which must be longer than the actual data, of course
8229 */
8230
8231 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
8232 if (tp->fw_len < (tp->fw->size - 12)) {
8233 printk(KERN_ERR "%s: bogus length %d in \"%s\"\n",
8234 tp->dev->name, tp->fw_len, tp->fw_needed);
8235 release_firmware(tp->fw);
8236 tp->fw = NULL;
8237 return -EINVAL;
8238 }
8239
8240 /* We no longer need firmware; we have it. */
8241 tp->fw_needed = NULL;
8242 return 0;
8243}
8244
Matt Carlson679563f2009-09-01 12:55:46 +00008245static bool tg3_enable_msix(struct tg3 *tp)
8246{
8247 int i, rc, cpus = num_online_cpus();
8248 struct msix_entry msix_ent[tp->irq_max];
8249
8250 if (cpus == 1)
8251 /* Just fallback to the simpler MSI mode. */
8252 return false;
8253
8254 /*
8255 * We want as many rx rings enabled as there are cpus.
8256 * The first MSIX vector only deals with link interrupts, etc,
8257 * so we add one to the number of vectors we are requesting.
8258 */
8259 tp->irq_cnt = min_t(unsigned, cpus + 1, tp->irq_max);
8260
8261 for (i = 0; i < tp->irq_max; i++) {
8262 msix_ent[i].entry = i;
8263 msix_ent[i].vector = 0;
8264 }
8265
8266 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
8267 if (rc != 0) {
8268 if (rc < TG3_RSS_MIN_NUM_MSIX_VECS)
8269 return false;
8270 if (pci_enable_msix(tp->pdev, msix_ent, rc))
8271 return false;
8272 printk(KERN_NOTICE
8273 "%s: Requested %d MSI-X vectors, received %d\n",
8274 tp->dev->name, tp->irq_cnt, rc);
8275 tp->irq_cnt = rc;
8276 }
8277
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008278 tp->tg3_flags3 |= TG3_FLG3_ENABLE_RSS;
8279
Matt Carlson679563f2009-09-01 12:55:46 +00008280 for (i = 0; i < tp->irq_max; i++)
8281 tp->napi[i].irq_vec = msix_ent[i].vector;
8282
Matt Carlsonfe5f5782009-09-01 13:09:39 +00008283 tp->dev->real_num_tx_queues = tp->irq_cnt - 1;
8284
Matt Carlson679563f2009-09-01 12:55:46 +00008285 return true;
8286}
8287
Matt Carlson07b01732009-08-28 14:01:15 +00008288static void tg3_ints_init(struct tg3 *tp)
8289{
Matt Carlson679563f2009-09-01 12:55:46 +00008290 if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSI_OR_MSIX) &&
8291 !(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +00008292 /* All MSI supporting chips should support tagged
8293 * status. Assert that this is the case.
8294 */
Matt Carlson679563f2009-09-01 12:55:46 +00008295 printk(KERN_WARNING PFX "%s: MSI without TAGGED? "
8296 "Not using MSI.\n", tp->dev->name);
8297 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +00008298 }
Matt Carlson4f125f42009-09-01 12:55:02 +00008299
Matt Carlson679563f2009-09-01 12:55:46 +00008300 if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX) && tg3_enable_msix(tp))
8301 tp->tg3_flags2 |= TG3_FLG2_USING_MSIX;
8302 else if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSI) &&
8303 pci_enable_msi(tp->pdev) == 0)
8304 tp->tg3_flags2 |= TG3_FLG2_USING_MSI;
8305
8306 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI_OR_MSIX) {
8307 u32 msi_mode = tr32(MSGINT_MODE);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008308 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
8309 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson679563f2009-09-01 12:55:46 +00008310 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
8311 }
8312defcfg:
8313 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSIX)) {
8314 tp->irq_cnt = 1;
8315 tp->napi[0].irq_vec = tp->pdev->irq;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00008316 tp->dev->real_num_tx_queues = 1;
Matt Carlson679563f2009-09-01 12:55:46 +00008317 }
Matt Carlson07b01732009-08-28 14:01:15 +00008318}
8319
8320static void tg3_ints_fini(struct tg3 *tp)
8321{
Matt Carlson679563f2009-09-01 12:55:46 +00008322 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
8323 pci_disable_msix(tp->pdev);
8324 else if (tp->tg3_flags2 & TG3_FLG2_USING_MSI)
8325 pci_disable_msi(tp->pdev);
8326 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI_OR_MSIX;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008327 tp->tg3_flags3 &= ~TG3_FLG3_ENABLE_RSS;
Matt Carlson07b01732009-08-28 14:01:15 +00008328}
8329
Linus Torvalds1da177e2005-04-16 15:20:36 -07008330static int tg3_open(struct net_device *dev)
8331{
8332 struct tg3 *tp = netdev_priv(dev);
Matt Carlson4f125f42009-09-01 12:55:02 +00008333 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008334
Matt Carlson9e9fd122009-01-19 16:57:45 -08008335 if (tp->fw_needed) {
8336 err = tg3_request_firmware(tp);
8337 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
8338 if (err)
8339 return err;
8340 } else if (err) {
8341 printk(KERN_WARNING "%s: TSO capability disabled.\n",
8342 tp->dev->name);
8343 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
8344 } else if (!(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
8345 printk(KERN_NOTICE "%s: TSO capability restored.\n",
8346 tp->dev->name);
8347 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
8348 }
8349 }
8350
Michael Chanc49a1562006-12-17 17:07:29 -08008351 netif_carrier_off(tp->dev);
8352
Michael Chanbc1c7562006-03-20 17:48:03 -08008353 err = tg3_set_power_state(tp, PCI_D0);
Matt Carlson2f751b62008-08-04 23:17:34 -07008354 if (err)
Michael Chanbc1c7562006-03-20 17:48:03 -08008355 return err;
Matt Carlson2f751b62008-08-04 23:17:34 -07008356
8357 tg3_full_lock(tp, 0);
Michael Chanbc1c7562006-03-20 17:48:03 -08008358
Linus Torvalds1da177e2005-04-16 15:20:36 -07008359 tg3_disable_ints(tp);
8360 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
8361
David S. Millerf47c11e2005-06-24 20:18:35 -07008362 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008363
Matt Carlson679563f2009-09-01 12:55:46 +00008364 /*
8365 * Setup interrupts first so we know how
8366 * many NAPI resources to allocate
8367 */
8368 tg3_ints_init(tp);
8369
Linus Torvalds1da177e2005-04-16 15:20:36 -07008370 /* The placement of this call is tied
8371 * to the setup and use of Host TX descriptors.
8372 */
8373 err = tg3_alloc_consistent(tp);
8374 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00008375 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008376
Matt Carlsonfed97812009-09-01 13:10:19 +00008377 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07008378
Matt Carlson4f125f42009-09-01 12:55:02 +00008379 for (i = 0; i < tp->irq_cnt; i++) {
8380 struct tg3_napi *tnapi = &tp->napi[i];
8381 err = tg3_request_irq(tp, i);
8382 if (err) {
8383 for (i--; i >= 0; i--)
8384 free_irq(tnapi->irq_vec, tnapi);
8385 break;
8386 }
8387 }
Matt Carlson07b01732009-08-28 14:01:15 +00008388
8389 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00008390 goto err_out2;
Matt Carlson07b01732009-08-28 14:01:15 +00008391
David S. Millerf47c11e2005-06-24 20:18:35 -07008392 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008393
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008394 err = tg3_init_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008395 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -07008396 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008397 tg3_free_rings(tp);
8398 } else {
David S. Millerfac9b832005-05-18 22:46:34 -07008399 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)
8400 tp->timer_offset = HZ;
8401 else
8402 tp->timer_offset = HZ / 10;
8403
8404 BUG_ON(tp->timer_offset > HZ);
8405 tp->timer_counter = tp->timer_multiplier =
8406 (HZ / tp->timer_offset);
8407 tp->asf_counter = tp->asf_multiplier =
Michael Chan28fbef72005-10-26 15:48:35 -07008408 ((HZ / tp->timer_offset) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008409
8410 init_timer(&tp->timer);
8411 tp->timer.expires = jiffies + tp->timer_offset;
8412 tp->timer.data = (unsigned long) tp;
8413 tp->timer.function = tg3_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008414 }
8415
David S. Millerf47c11e2005-06-24 20:18:35 -07008416 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008417
Matt Carlson07b01732009-08-28 14:01:15 +00008418 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00008419 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008420
Michael Chan79381092005-04-21 17:13:59 -07008421 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
8422 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -07008423
Michael Chan79381092005-04-21 17:13:59 -07008424 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -07008425 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -07008426 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -07008427 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07008428 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07008429
Matt Carlson679563f2009-09-01 12:55:46 +00008430 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -07008431 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08008432
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008433 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
8434 (tp->tg3_flags2 & TG3_FLG2_USING_MSI) &&
8435 (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)) {
8436 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -08008437
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008438 tw32(PCIE_TRANSACTION_CFG,
8439 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -08008440 }
Michael Chan79381092005-04-21 17:13:59 -07008441 }
8442
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07008443 tg3_phy_start(tp);
8444
David S. Millerf47c11e2005-06-24 20:18:35 -07008445 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008446
Michael Chan79381092005-04-21 17:13:59 -07008447 add_timer(&tp->timer);
8448 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008449 tg3_enable_ints(tp);
8450
David S. Millerf47c11e2005-06-24 20:18:35 -07008451 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008452
Matt Carlsonfe5f5782009-09-01 13:09:39 +00008453 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008454
8455 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +00008456
Matt Carlson679563f2009-09-01 12:55:46 +00008457err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +00008458 for (i = tp->irq_cnt - 1; i >= 0; i--) {
8459 struct tg3_napi *tnapi = &tp->napi[i];
8460 free_irq(tnapi->irq_vec, tnapi);
8461 }
Matt Carlson07b01732009-08-28 14:01:15 +00008462
Matt Carlson679563f2009-09-01 12:55:46 +00008463err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +00008464 tg3_napi_disable(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00008465 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +00008466
8467err_out1:
8468 tg3_ints_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00008469 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008470}
8471
8472#if 0
8473/*static*/ void tg3_dump_state(struct tg3 *tp)
8474{
8475 u32 val32, val32_2, val32_3, val32_4, val32_5;
8476 u16 val16;
8477 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00008478 struct tg3_hw_status *sblk = tp->napi[0]->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008479
8480 pci_read_config_word(tp->pdev, PCI_STATUS, &val16);
8481 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE, &val32);
8482 printk("DEBUG: PCI status [%04x] TG3PCI state[%08x]\n",
8483 val16, val32);
8484
8485 /* MAC block */
8486 printk("DEBUG: MAC_MODE[%08x] MAC_STATUS[%08x]\n",
8487 tr32(MAC_MODE), tr32(MAC_STATUS));
8488 printk(" MAC_EVENT[%08x] MAC_LED_CTRL[%08x]\n",
8489 tr32(MAC_EVENT), tr32(MAC_LED_CTRL));
8490 printk("DEBUG: MAC_TX_MODE[%08x] MAC_TX_STATUS[%08x]\n",
8491 tr32(MAC_TX_MODE), tr32(MAC_TX_STATUS));
8492 printk(" MAC_RX_MODE[%08x] MAC_RX_STATUS[%08x]\n",
8493 tr32(MAC_RX_MODE), tr32(MAC_RX_STATUS));
8494
8495 /* Send data initiator control block */
8496 printk("DEBUG: SNDDATAI_MODE[%08x] SNDDATAI_STATUS[%08x]\n",
8497 tr32(SNDDATAI_MODE), tr32(SNDDATAI_STATUS));
8498 printk(" SNDDATAI_STATSCTRL[%08x]\n",
8499 tr32(SNDDATAI_STATSCTRL));
8500
8501 /* Send data completion control block */
8502 printk("DEBUG: SNDDATAC_MODE[%08x]\n", tr32(SNDDATAC_MODE));
8503
8504 /* Send BD ring selector block */
8505 printk("DEBUG: SNDBDS_MODE[%08x] SNDBDS_STATUS[%08x]\n",
8506 tr32(SNDBDS_MODE), tr32(SNDBDS_STATUS));
8507
8508 /* Send BD initiator control block */
8509 printk("DEBUG: SNDBDI_MODE[%08x] SNDBDI_STATUS[%08x]\n",
8510 tr32(SNDBDI_MODE), tr32(SNDBDI_STATUS));
8511
8512 /* Send BD completion control block */
8513 printk("DEBUG: SNDBDC_MODE[%08x]\n", tr32(SNDBDC_MODE));
8514
8515 /* Receive list placement control block */
8516 printk("DEBUG: RCVLPC_MODE[%08x] RCVLPC_STATUS[%08x]\n",
8517 tr32(RCVLPC_MODE), tr32(RCVLPC_STATUS));
8518 printk(" RCVLPC_STATSCTRL[%08x]\n",
8519 tr32(RCVLPC_STATSCTRL));
8520
8521 /* Receive data and receive BD initiator control block */
8522 printk("DEBUG: RCVDBDI_MODE[%08x] RCVDBDI_STATUS[%08x]\n",
8523 tr32(RCVDBDI_MODE), tr32(RCVDBDI_STATUS));
8524
8525 /* Receive data completion control block */
8526 printk("DEBUG: RCVDCC_MODE[%08x]\n",
8527 tr32(RCVDCC_MODE));
8528
8529 /* Receive BD initiator control block */
8530 printk("DEBUG: RCVBDI_MODE[%08x] RCVBDI_STATUS[%08x]\n",
8531 tr32(RCVBDI_MODE), tr32(RCVBDI_STATUS));
8532
8533 /* Receive BD completion control block */
8534 printk("DEBUG: RCVCC_MODE[%08x] RCVCC_STATUS[%08x]\n",
8535 tr32(RCVCC_MODE), tr32(RCVCC_STATUS));
8536
8537 /* Receive list selector control block */
8538 printk("DEBUG: RCVLSC_MODE[%08x] RCVLSC_STATUS[%08x]\n",
8539 tr32(RCVLSC_MODE), tr32(RCVLSC_STATUS));
8540
8541 /* Mbuf cluster free block */
8542 printk("DEBUG: MBFREE_MODE[%08x] MBFREE_STATUS[%08x]\n",
8543 tr32(MBFREE_MODE), tr32(MBFREE_STATUS));
8544
8545 /* Host coalescing control block */
8546 printk("DEBUG: HOSTCC_MODE[%08x] HOSTCC_STATUS[%08x]\n",
8547 tr32(HOSTCC_MODE), tr32(HOSTCC_STATUS));
8548 printk("DEBUG: HOSTCC_STATS_BLK_HOST_ADDR[%08x%08x]\n",
8549 tr32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH),
8550 tr32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW));
8551 printk("DEBUG: HOSTCC_STATUS_BLK_HOST_ADDR[%08x%08x]\n",
8552 tr32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH),
8553 tr32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW));
8554 printk("DEBUG: HOSTCC_STATS_BLK_NIC_ADDR[%08x]\n",
8555 tr32(HOSTCC_STATS_BLK_NIC_ADDR));
8556 printk("DEBUG: HOSTCC_STATUS_BLK_NIC_ADDR[%08x]\n",
8557 tr32(HOSTCC_STATUS_BLK_NIC_ADDR));
8558
8559 /* Memory arbiter control block */
8560 printk("DEBUG: MEMARB_MODE[%08x] MEMARB_STATUS[%08x]\n",
8561 tr32(MEMARB_MODE), tr32(MEMARB_STATUS));
8562
8563 /* Buffer manager control block */
8564 printk("DEBUG: BUFMGR_MODE[%08x] BUFMGR_STATUS[%08x]\n",
8565 tr32(BUFMGR_MODE), tr32(BUFMGR_STATUS));
8566 printk("DEBUG: BUFMGR_MB_POOL_ADDR[%08x] BUFMGR_MB_POOL_SIZE[%08x]\n",
8567 tr32(BUFMGR_MB_POOL_ADDR), tr32(BUFMGR_MB_POOL_SIZE));
8568 printk("DEBUG: BUFMGR_DMA_DESC_POOL_ADDR[%08x] "
8569 "BUFMGR_DMA_DESC_POOL_SIZE[%08x]\n",
8570 tr32(BUFMGR_DMA_DESC_POOL_ADDR),
8571 tr32(BUFMGR_DMA_DESC_POOL_SIZE));
8572
8573 /* Read DMA control block */
8574 printk("DEBUG: RDMAC_MODE[%08x] RDMAC_STATUS[%08x]\n",
8575 tr32(RDMAC_MODE), tr32(RDMAC_STATUS));
8576
8577 /* Write DMA control block */
8578 printk("DEBUG: WDMAC_MODE[%08x] WDMAC_STATUS[%08x]\n",
8579 tr32(WDMAC_MODE), tr32(WDMAC_STATUS));
8580
8581 /* DMA completion block */
8582 printk("DEBUG: DMAC_MODE[%08x]\n",
8583 tr32(DMAC_MODE));
8584
8585 /* GRC block */
8586 printk("DEBUG: GRC_MODE[%08x] GRC_MISC_CFG[%08x]\n",
8587 tr32(GRC_MODE), tr32(GRC_MISC_CFG));
8588 printk("DEBUG: GRC_LOCAL_CTRL[%08x]\n",
8589 tr32(GRC_LOCAL_CTRL));
8590
8591 /* TG3_BDINFOs */
8592 printk("DEBUG: RCVDBDI_JUMBO_BD[%08x%08x:%08x:%08x]\n",
8593 tr32(RCVDBDI_JUMBO_BD + 0x0),
8594 tr32(RCVDBDI_JUMBO_BD + 0x4),
8595 tr32(RCVDBDI_JUMBO_BD + 0x8),
8596 tr32(RCVDBDI_JUMBO_BD + 0xc));
8597 printk("DEBUG: RCVDBDI_STD_BD[%08x%08x:%08x:%08x]\n",
8598 tr32(RCVDBDI_STD_BD + 0x0),
8599 tr32(RCVDBDI_STD_BD + 0x4),
8600 tr32(RCVDBDI_STD_BD + 0x8),
8601 tr32(RCVDBDI_STD_BD + 0xc));
8602 printk("DEBUG: RCVDBDI_MINI_BD[%08x%08x:%08x:%08x]\n",
8603 tr32(RCVDBDI_MINI_BD + 0x0),
8604 tr32(RCVDBDI_MINI_BD + 0x4),
8605 tr32(RCVDBDI_MINI_BD + 0x8),
8606 tr32(RCVDBDI_MINI_BD + 0xc));
8607
8608 tg3_read_mem(tp, NIC_SRAM_SEND_RCB + 0x0, &val32);
8609 tg3_read_mem(tp, NIC_SRAM_SEND_RCB + 0x4, &val32_2);
8610 tg3_read_mem(tp, NIC_SRAM_SEND_RCB + 0x8, &val32_3);
8611 tg3_read_mem(tp, NIC_SRAM_SEND_RCB + 0xc, &val32_4);
8612 printk("DEBUG: SRAM_SEND_RCB_0[%08x%08x:%08x:%08x]\n",
8613 val32, val32_2, val32_3, val32_4);
8614
8615 tg3_read_mem(tp, NIC_SRAM_RCV_RET_RCB + 0x0, &val32);
8616 tg3_read_mem(tp, NIC_SRAM_RCV_RET_RCB + 0x4, &val32_2);
8617 tg3_read_mem(tp, NIC_SRAM_RCV_RET_RCB + 0x8, &val32_3);
8618 tg3_read_mem(tp, NIC_SRAM_RCV_RET_RCB + 0xc, &val32_4);
8619 printk("DEBUG: SRAM_RCV_RET_RCB_0[%08x%08x:%08x:%08x]\n",
8620 val32, val32_2, val32_3, val32_4);
8621
8622 tg3_read_mem(tp, NIC_SRAM_STATUS_BLK + 0x0, &val32);
8623 tg3_read_mem(tp, NIC_SRAM_STATUS_BLK + 0x4, &val32_2);
8624 tg3_read_mem(tp, NIC_SRAM_STATUS_BLK + 0x8, &val32_3);
8625 tg3_read_mem(tp, NIC_SRAM_STATUS_BLK + 0xc, &val32_4);
8626 tg3_read_mem(tp, NIC_SRAM_STATUS_BLK + 0x10, &val32_5);
8627 printk("DEBUG: SRAM_STATUS_BLK[%08x:%08x:%08x:%08x:%08x]\n",
8628 val32, val32_2, val32_3, val32_4, val32_5);
8629
8630 /* SW status block */
Matt Carlson898a56f2009-08-28 14:02:40 +00008631 printk(KERN_DEBUG
8632 "Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
8633 sblk->status,
8634 sblk->status_tag,
8635 sblk->rx_jumbo_consumer,
8636 sblk->rx_consumer,
8637 sblk->rx_mini_consumer,
8638 sblk->idx[0].rx_producer,
8639 sblk->idx[0].tx_consumer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008640
8641 /* SW statistics block */
8642 printk("DEBUG: Host statistics block [%08x:%08x:%08x:%08x]\n",
8643 ((u32 *)tp->hw_stats)[0],
8644 ((u32 *)tp->hw_stats)[1],
8645 ((u32 *)tp->hw_stats)[2],
8646 ((u32 *)tp->hw_stats)[3]);
8647
8648 /* Mailboxes */
8649 printk("DEBUG: SNDHOST_PROD[%08x%08x] SNDNIC_PROD[%08x%08x]\n",
Michael Chan09ee9292005-08-09 20:17:00 -07008650 tr32_mailbox(MAILBOX_SNDHOST_PROD_IDX_0 + 0x0),
8651 tr32_mailbox(MAILBOX_SNDHOST_PROD_IDX_0 + 0x4),
8652 tr32_mailbox(MAILBOX_SNDNIC_PROD_IDX_0 + 0x0),
8653 tr32_mailbox(MAILBOX_SNDNIC_PROD_IDX_0 + 0x4));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008654
8655 /* NIC side send descriptors. */
8656 for (i = 0; i < 6; i++) {
8657 unsigned long txd;
8658
8659 txd = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_TX_BUFFER_DESC
8660 + (i * sizeof(struct tg3_tx_buffer_desc));
8661 printk("DEBUG: NIC TXD(%d)[%08x:%08x:%08x:%08x]\n",
8662 i,
8663 readl(txd + 0x0), readl(txd + 0x4),
8664 readl(txd + 0x8), readl(txd + 0xc));
8665 }
8666
8667 /* NIC side RX descriptors. */
8668 for (i = 0; i < 6; i++) {
8669 unsigned long rxd;
8670
8671 rxd = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_RX_BUFFER_DESC
8672 + (i * sizeof(struct tg3_rx_buffer_desc));
8673 printk("DEBUG: NIC RXD_STD(%d)[0][%08x:%08x:%08x:%08x]\n",
8674 i,
8675 readl(rxd + 0x0), readl(rxd + 0x4),
8676 readl(rxd + 0x8), readl(rxd + 0xc));
8677 rxd += (4 * sizeof(u32));
8678 printk("DEBUG: NIC RXD_STD(%d)[1][%08x:%08x:%08x:%08x]\n",
8679 i,
8680 readl(rxd + 0x0), readl(rxd + 0x4),
8681 readl(rxd + 0x8), readl(rxd + 0xc));
8682 }
8683
8684 for (i = 0; i < 6; i++) {
8685 unsigned long rxd;
8686
8687 rxd = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_RX_JUMBO_BUFFER_DESC
8688 + (i * sizeof(struct tg3_rx_buffer_desc));
8689 printk("DEBUG: NIC RXD_JUMBO(%d)[0][%08x:%08x:%08x:%08x]\n",
8690 i,
8691 readl(rxd + 0x0), readl(rxd + 0x4),
8692 readl(rxd + 0x8), readl(rxd + 0xc));
8693 rxd += (4 * sizeof(u32));
8694 printk("DEBUG: NIC RXD_JUMBO(%d)[1][%08x:%08x:%08x:%08x]\n",
8695 i,
8696 readl(rxd + 0x0), readl(rxd + 0x4),
8697 readl(rxd + 0x8), readl(rxd + 0xc));
8698 }
8699}
8700#endif
8701
8702static struct net_device_stats *tg3_get_stats(struct net_device *);
8703static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *);
8704
8705static int tg3_close(struct net_device *dev)
8706{
Matt Carlson4f125f42009-09-01 12:55:02 +00008707 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008708 struct tg3 *tp = netdev_priv(dev);
8709
Matt Carlsonfed97812009-09-01 13:10:19 +00008710 tg3_napi_disable(tp);
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07008711 cancel_work_sync(&tp->reset_task);
Michael Chan7faa0062006-02-02 17:29:28 -08008712
Matt Carlsonfe5f5782009-09-01 13:09:39 +00008713 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008714
8715 del_timer_sync(&tp->timer);
8716
Matt Carlson24bb4fb2009-10-05 17:55:29 +00008717 tg3_phy_stop(tp);
8718
David S. Millerf47c11e2005-06-24 20:18:35 -07008719 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008720#if 0
8721 tg3_dump_state(tp);
8722#endif
8723
8724 tg3_disable_ints(tp);
8725
Michael Chan944d9802005-05-29 14:57:48 -07008726 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008727 tg3_free_rings(tp);
Michael Chan5cf64b8a2007-05-05 12:11:21 -07008728 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008729
David S. Millerf47c11e2005-06-24 20:18:35 -07008730 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008731
Matt Carlson4f125f42009-09-01 12:55:02 +00008732 for (i = tp->irq_cnt - 1; i >= 0; i--) {
8733 struct tg3_napi *tnapi = &tp->napi[i];
8734 free_irq(tnapi->irq_vec, tnapi);
8735 }
Matt Carlson07b01732009-08-28 14:01:15 +00008736
8737 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008738
8739 memcpy(&tp->net_stats_prev, tg3_get_stats(tp->dev),
8740 sizeof(tp->net_stats_prev));
8741 memcpy(&tp->estats_prev, tg3_get_estats(tp),
8742 sizeof(tp->estats_prev));
8743
8744 tg3_free_consistent(tp);
8745
Michael Chanbc1c7562006-03-20 17:48:03 -08008746 tg3_set_power_state(tp, PCI_D3hot);
8747
8748 netif_carrier_off(tp->dev);
8749
Linus Torvalds1da177e2005-04-16 15:20:36 -07008750 return 0;
8751}
8752
8753static inline unsigned long get_stat64(tg3_stat64_t *val)
8754{
8755 unsigned long ret;
8756
8757#if (BITS_PER_LONG == 32)
8758 ret = val->low;
8759#else
8760 ret = ((u64)val->high << 32) | ((u64)val->low);
8761#endif
8762 return ret;
8763}
8764
Stefan Buehler816f8b82008-08-15 14:10:54 -07008765static inline u64 get_estat64(tg3_stat64_t *val)
8766{
8767 return ((u64)val->high << 32) | ((u64)val->low);
8768}
8769
Linus Torvalds1da177e2005-04-16 15:20:36 -07008770static unsigned long calc_crc_errors(struct tg3 *tp)
8771{
8772 struct tg3_hw_stats *hw_stats = tp->hw_stats;
8773
8774 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
8775 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
8776 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008777 u32 val;
8778
David S. Millerf47c11e2005-06-24 20:18:35 -07008779 spin_lock_bh(&tp->lock);
Michael Chan569a5df2007-02-13 12:18:15 -08008780 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
8781 tg3_writephy(tp, MII_TG3_TEST1,
8782 val | MII_TG3_TEST1_CRC_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008783 tg3_readphy(tp, 0x14, &val);
8784 } else
8785 val = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07008786 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008787
8788 tp->phy_crc_errors += val;
8789
8790 return tp->phy_crc_errors;
8791 }
8792
8793 return get_stat64(&hw_stats->rx_fcs_errors);
8794}
8795
8796#define ESTAT_ADD(member) \
8797 estats->member = old_estats->member + \
Stefan Buehler816f8b82008-08-15 14:10:54 -07008798 get_estat64(&hw_stats->member)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008799
8800static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *tp)
8801{
8802 struct tg3_ethtool_stats *estats = &tp->estats;
8803 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
8804 struct tg3_hw_stats *hw_stats = tp->hw_stats;
8805
8806 if (!hw_stats)
8807 return old_estats;
8808
8809 ESTAT_ADD(rx_octets);
8810 ESTAT_ADD(rx_fragments);
8811 ESTAT_ADD(rx_ucast_packets);
8812 ESTAT_ADD(rx_mcast_packets);
8813 ESTAT_ADD(rx_bcast_packets);
8814 ESTAT_ADD(rx_fcs_errors);
8815 ESTAT_ADD(rx_align_errors);
8816 ESTAT_ADD(rx_xon_pause_rcvd);
8817 ESTAT_ADD(rx_xoff_pause_rcvd);
8818 ESTAT_ADD(rx_mac_ctrl_rcvd);
8819 ESTAT_ADD(rx_xoff_entered);
8820 ESTAT_ADD(rx_frame_too_long_errors);
8821 ESTAT_ADD(rx_jabbers);
8822 ESTAT_ADD(rx_undersize_packets);
8823 ESTAT_ADD(rx_in_length_errors);
8824 ESTAT_ADD(rx_out_length_errors);
8825 ESTAT_ADD(rx_64_or_less_octet_packets);
8826 ESTAT_ADD(rx_65_to_127_octet_packets);
8827 ESTAT_ADD(rx_128_to_255_octet_packets);
8828 ESTAT_ADD(rx_256_to_511_octet_packets);
8829 ESTAT_ADD(rx_512_to_1023_octet_packets);
8830 ESTAT_ADD(rx_1024_to_1522_octet_packets);
8831 ESTAT_ADD(rx_1523_to_2047_octet_packets);
8832 ESTAT_ADD(rx_2048_to_4095_octet_packets);
8833 ESTAT_ADD(rx_4096_to_8191_octet_packets);
8834 ESTAT_ADD(rx_8192_to_9022_octet_packets);
8835
8836 ESTAT_ADD(tx_octets);
8837 ESTAT_ADD(tx_collisions);
8838 ESTAT_ADD(tx_xon_sent);
8839 ESTAT_ADD(tx_xoff_sent);
8840 ESTAT_ADD(tx_flow_control);
8841 ESTAT_ADD(tx_mac_errors);
8842 ESTAT_ADD(tx_single_collisions);
8843 ESTAT_ADD(tx_mult_collisions);
8844 ESTAT_ADD(tx_deferred);
8845 ESTAT_ADD(tx_excessive_collisions);
8846 ESTAT_ADD(tx_late_collisions);
8847 ESTAT_ADD(tx_collide_2times);
8848 ESTAT_ADD(tx_collide_3times);
8849 ESTAT_ADD(tx_collide_4times);
8850 ESTAT_ADD(tx_collide_5times);
8851 ESTAT_ADD(tx_collide_6times);
8852 ESTAT_ADD(tx_collide_7times);
8853 ESTAT_ADD(tx_collide_8times);
8854 ESTAT_ADD(tx_collide_9times);
8855 ESTAT_ADD(tx_collide_10times);
8856 ESTAT_ADD(tx_collide_11times);
8857 ESTAT_ADD(tx_collide_12times);
8858 ESTAT_ADD(tx_collide_13times);
8859 ESTAT_ADD(tx_collide_14times);
8860 ESTAT_ADD(tx_collide_15times);
8861 ESTAT_ADD(tx_ucast_packets);
8862 ESTAT_ADD(tx_mcast_packets);
8863 ESTAT_ADD(tx_bcast_packets);
8864 ESTAT_ADD(tx_carrier_sense_errors);
8865 ESTAT_ADD(tx_discards);
8866 ESTAT_ADD(tx_errors);
8867
8868 ESTAT_ADD(dma_writeq_full);
8869 ESTAT_ADD(dma_write_prioq_full);
8870 ESTAT_ADD(rxbds_empty);
8871 ESTAT_ADD(rx_discards);
8872 ESTAT_ADD(rx_errors);
8873 ESTAT_ADD(rx_threshold_hit);
8874
8875 ESTAT_ADD(dma_readq_full);
8876 ESTAT_ADD(dma_read_prioq_full);
8877 ESTAT_ADD(tx_comp_queue_full);
8878
8879 ESTAT_ADD(ring_set_send_prod_index);
8880 ESTAT_ADD(ring_status_update);
8881 ESTAT_ADD(nic_irqs);
8882 ESTAT_ADD(nic_avoided_irqs);
8883 ESTAT_ADD(nic_tx_threshold_hit);
8884
8885 return estats;
8886}
8887
8888static struct net_device_stats *tg3_get_stats(struct net_device *dev)
8889{
8890 struct tg3 *tp = netdev_priv(dev);
8891 struct net_device_stats *stats = &tp->net_stats;
8892 struct net_device_stats *old_stats = &tp->net_stats_prev;
8893 struct tg3_hw_stats *hw_stats = tp->hw_stats;
8894
8895 if (!hw_stats)
8896 return old_stats;
8897
8898 stats->rx_packets = old_stats->rx_packets +
8899 get_stat64(&hw_stats->rx_ucast_packets) +
8900 get_stat64(&hw_stats->rx_mcast_packets) +
8901 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008902
Linus Torvalds1da177e2005-04-16 15:20:36 -07008903 stats->tx_packets = old_stats->tx_packets +
8904 get_stat64(&hw_stats->tx_ucast_packets) +
8905 get_stat64(&hw_stats->tx_mcast_packets) +
8906 get_stat64(&hw_stats->tx_bcast_packets);
8907
8908 stats->rx_bytes = old_stats->rx_bytes +
8909 get_stat64(&hw_stats->rx_octets);
8910 stats->tx_bytes = old_stats->tx_bytes +
8911 get_stat64(&hw_stats->tx_octets);
8912
8913 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -07008914 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008915 stats->tx_errors = old_stats->tx_errors +
8916 get_stat64(&hw_stats->tx_errors) +
8917 get_stat64(&hw_stats->tx_mac_errors) +
8918 get_stat64(&hw_stats->tx_carrier_sense_errors) +
8919 get_stat64(&hw_stats->tx_discards);
8920
8921 stats->multicast = old_stats->multicast +
8922 get_stat64(&hw_stats->rx_mcast_packets);
8923 stats->collisions = old_stats->collisions +
8924 get_stat64(&hw_stats->tx_collisions);
8925
8926 stats->rx_length_errors = old_stats->rx_length_errors +
8927 get_stat64(&hw_stats->rx_frame_too_long_errors) +
8928 get_stat64(&hw_stats->rx_undersize_packets);
8929
8930 stats->rx_over_errors = old_stats->rx_over_errors +
8931 get_stat64(&hw_stats->rxbds_empty);
8932 stats->rx_frame_errors = old_stats->rx_frame_errors +
8933 get_stat64(&hw_stats->rx_align_errors);
8934 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
8935 get_stat64(&hw_stats->tx_discards);
8936 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
8937 get_stat64(&hw_stats->tx_carrier_sense_errors);
8938
8939 stats->rx_crc_errors = old_stats->rx_crc_errors +
8940 calc_crc_errors(tp);
8941
John W. Linville4f63b872005-09-12 14:43:18 -07008942 stats->rx_missed_errors = old_stats->rx_missed_errors +
8943 get_stat64(&hw_stats->rx_discards);
8944
Linus Torvalds1da177e2005-04-16 15:20:36 -07008945 return stats;
8946}
8947
8948static inline u32 calc_crc(unsigned char *buf, int len)
8949{
8950 u32 reg;
8951 u32 tmp;
8952 int j, k;
8953
8954 reg = 0xffffffff;
8955
8956 for (j = 0; j < len; j++) {
8957 reg ^= buf[j];
8958
8959 for (k = 0; k < 8; k++) {
8960 tmp = reg & 0x01;
8961
8962 reg >>= 1;
8963
8964 if (tmp) {
8965 reg ^= 0xedb88320;
8966 }
8967 }
8968 }
8969
8970 return ~reg;
8971}
8972
8973static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
8974{
8975 /* accept or reject all multicast frames */
8976 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
8977 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
8978 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
8979 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
8980}
8981
8982static void __tg3_set_rx_mode(struct net_device *dev)
8983{
8984 struct tg3 *tp = netdev_priv(dev);
8985 u32 rx_mode;
8986
8987 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
8988 RX_MODE_KEEP_VLAN_TAG);
8989
8990 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
8991 * flag clear.
8992 */
8993#if TG3_VLAN_TAG_USED
8994 if (!tp->vlgrp &&
8995 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
8996 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
8997#else
8998 /* By definition, VLAN is disabled always in this
8999 * case.
9000 */
9001 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
9002 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9003#endif
9004
9005 if (dev->flags & IFF_PROMISC) {
9006 /* Promiscuous mode. */
9007 rx_mode |= RX_MODE_PROMISC;
9008 } else if (dev->flags & IFF_ALLMULTI) {
9009 /* Accept all multicast. */
9010 tg3_set_multi (tp, 1);
9011 } else if (dev->mc_count < 1) {
9012 /* Reject all multicast. */
9013 tg3_set_multi (tp, 0);
9014 } else {
9015 /* Accept one or more multicast(s). */
9016 struct dev_mc_list *mclist;
9017 unsigned int i;
9018 u32 mc_filter[4] = { 0, };
9019 u32 regidx;
9020 u32 bit;
9021 u32 crc;
9022
9023 for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
9024 i++, mclist = mclist->next) {
9025
9026 crc = calc_crc (mclist->dmi_addr, ETH_ALEN);
9027 bit = ~crc & 0x7f;
9028 regidx = (bit & 0x60) >> 5;
9029 bit &= 0x1f;
9030 mc_filter[regidx] |= (1 << bit);
9031 }
9032
9033 tw32(MAC_HASH_REG_0, mc_filter[0]);
9034 tw32(MAC_HASH_REG_1, mc_filter[1]);
9035 tw32(MAC_HASH_REG_2, mc_filter[2]);
9036 tw32(MAC_HASH_REG_3, mc_filter[3]);
9037 }
9038
9039 if (rx_mode != tp->rx_mode) {
9040 tp->rx_mode = rx_mode;
9041 tw32_f(MAC_RX_MODE, rx_mode);
9042 udelay(10);
9043 }
9044}
9045
9046static void tg3_set_rx_mode(struct net_device *dev)
9047{
9048 struct tg3 *tp = netdev_priv(dev);
9049
Michael Chane75f7c92006-03-20 21:33:26 -08009050 if (!netif_running(dev))
9051 return;
9052
David S. Millerf47c11e2005-06-24 20:18:35 -07009053 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009054 __tg3_set_rx_mode(dev);
David S. Millerf47c11e2005-06-24 20:18:35 -07009055 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009056}
9057
9058#define TG3_REGDUMP_LEN (32 * 1024)
9059
9060static int tg3_get_regs_len(struct net_device *dev)
9061{
9062 return TG3_REGDUMP_LEN;
9063}
9064
9065static void tg3_get_regs(struct net_device *dev,
9066 struct ethtool_regs *regs, void *_p)
9067{
9068 u32 *p = _p;
9069 struct tg3 *tp = netdev_priv(dev);
9070 u8 *orig_p = _p;
9071 int i;
9072
9073 regs->version = 0;
9074
9075 memset(p, 0, TG3_REGDUMP_LEN);
9076
Michael Chanbc1c7562006-03-20 17:48:03 -08009077 if (tp->link_config.phy_is_low_power)
9078 return;
9079
David S. Millerf47c11e2005-06-24 20:18:35 -07009080 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009081
9082#define __GET_REG32(reg) (*(p)++ = tr32(reg))
9083#define GET_REG32_LOOP(base,len) \
9084do { p = (u32 *)(orig_p + (base)); \
9085 for (i = 0; i < len; i += 4) \
9086 __GET_REG32((base) + i); \
9087} while (0)
9088#define GET_REG32_1(reg) \
9089do { p = (u32 *)(orig_p + (reg)); \
9090 __GET_REG32((reg)); \
9091} while (0)
9092
9093 GET_REG32_LOOP(TG3PCI_VENDOR, 0xb0);
9094 GET_REG32_LOOP(MAILBOX_INTERRUPT_0, 0x200);
9095 GET_REG32_LOOP(MAC_MODE, 0x4f0);
9096 GET_REG32_LOOP(SNDDATAI_MODE, 0xe0);
9097 GET_REG32_1(SNDDATAC_MODE);
9098 GET_REG32_LOOP(SNDBDS_MODE, 0x80);
9099 GET_REG32_LOOP(SNDBDI_MODE, 0x48);
9100 GET_REG32_1(SNDBDC_MODE);
9101 GET_REG32_LOOP(RCVLPC_MODE, 0x20);
9102 GET_REG32_LOOP(RCVLPC_SELLST_BASE, 0x15c);
9103 GET_REG32_LOOP(RCVDBDI_MODE, 0x0c);
9104 GET_REG32_LOOP(RCVDBDI_JUMBO_BD, 0x3c);
9105 GET_REG32_LOOP(RCVDBDI_BD_PROD_IDX_0, 0x44);
9106 GET_REG32_1(RCVDCC_MODE);
9107 GET_REG32_LOOP(RCVBDI_MODE, 0x20);
9108 GET_REG32_LOOP(RCVCC_MODE, 0x14);
9109 GET_REG32_LOOP(RCVLSC_MODE, 0x08);
9110 GET_REG32_1(MBFREE_MODE);
9111 GET_REG32_LOOP(HOSTCC_MODE, 0x100);
9112 GET_REG32_LOOP(MEMARB_MODE, 0x10);
9113 GET_REG32_LOOP(BUFMGR_MODE, 0x58);
9114 GET_REG32_LOOP(RDMAC_MODE, 0x08);
9115 GET_REG32_LOOP(WDMAC_MODE, 0x08);
Chris Elmquist091465d2005-12-20 13:25:19 -08009116 GET_REG32_1(RX_CPU_MODE);
9117 GET_REG32_1(RX_CPU_STATE);
9118 GET_REG32_1(RX_CPU_PGMCTR);
9119 GET_REG32_1(RX_CPU_HWBKPT);
9120 GET_REG32_1(TX_CPU_MODE);
9121 GET_REG32_1(TX_CPU_STATE);
9122 GET_REG32_1(TX_CPU_PGMCTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009123 GET_REG32_LOOP(GRCMBOX_INTERRUPT_0, 0x110);
9124 GET_REG32_LOOP(FTQ_RESET, 0x120);
9125 GET_REG32_LOOP(MSGINT_MODE, 0x0c);
9126 GET_REG32_1(DMAC_MODE);
9127 GET_REG32_LOOP(GRC_MODE, 0x4c);
9128 if (tp->tg3_flags & TG3_FLAG_NVRAM)
9129 GET_REG32_LOOP(NVRAM_CMD, 0x24);
9130
9131#undef __GET_REG32
9132#undef GET_REG32_LOOP
9133#undef GET_REG32_1
9134
David S. Millerf47c11e2005-06-24 20:18:35 -07009135 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009136}
9137
9138static int tg3_get_eeprom_len(struct net_device *dev)
9139{
9140 struct tg3 *tp = netdev_priv(dev);
9141
9142 return tp->nvram_size;
9143}
9144
Linus Torvalds1da177e2005-04-16 15:20:36 -07009145static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9146{
9147 struct tg3 *tp = netdev_priv(dev);
9148 int ret;
9149 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -08009150 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009151 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009152
Matt Carlsondf259d82009-04-20 06:57:14 +00009153 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM)
9154 return -EINVAL;
9155
Michael Chanbc1c7562006-03-20 17:48:03 -08009156 if (tp->link_config.phy_is_low_power)
9157 return -EAGAIN;
9158
Linus Torvalds1da177e2005-04-16 15:20:36 -07009159 offset = eeprom->offset;
9160 len = eeprom->len;
9161 eeprom->len = 0;
9162
9163 eeprom->magic = TG3_EEPROM_MAGIC;
9164
9165 if (offset & 3) {
9166 /* adjustments to start on required 4 byte boundary */
9167 b_offset = offset & 3;
9168 b_count = 4 - b_offset;
9169 if (b_count > len) {
9170 /* i.e. offset=1 len=2 */
9171 b_count = len;
9172 }
Matt Carlsona9dc5292009-02-25 14:25:30 +00009173 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009174 if (ret)
9175 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009176 memcpy(data, ((char*)&val) + b_offset, b_count);
9177 len -= b_count;
9178 offset += b_count;
9179 eeprom->len += b_count;
9180 }
9181
9182 /* read bytes upto the last 4 byte boundary */
9183 pd = &data[eeprom->len];
9184 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +00009185 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009186 if (ret) {
9187 eeprom->len += i;
9188 return ret;
9189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009190 memcpy(pd + i, &val, 4);
9191 }
9192 eeprom->len += i;
9193
9194 if (len & 3) {
9195 /* read last bytes not ending on 4 byte boundary */
9196 pd = &data[eeprom->len];
9197 b_count = len & 3;
9198 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009199 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009200 if (ret)
9201 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009202 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009203 eeprom->len += b_count;
9204 }
9205 return 0;
9206}
9207
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009208static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009209
9210static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9211{
9212 struct tg3 *tp = netdev_priv(dev);
9213 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009214 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009215 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009216 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009217
Michael Chanbc1c7562006-03-20 17:48:03 -08009218 if (tp->link_config.phy_is_low_power)
9219 return -EAGAIN;
9220
Matt Carlsondf259d82009-04-20 06:57:14 +00009221 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
9222 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009223 return -EINVAL;
9224
9225 offset = eeprom->offset;
9226 len = eeprom->len;
9227
9228 if ((b_offset = (offset & 3))) {
9229 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +00009230 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009231 if (ret)
9232 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009233 len += b_offset;
9234 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -07009235 if (len < 4)
9236 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009237 }
9238
9239 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -07009240 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009241 /* adjustments to end on required 4 byte boundary */
9242 odd_len = 1;
9243 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009244 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009245 if (ret)
9246 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009247 }
9248
9249 buf = data;
9250 if (b_offset || odd_len) {
9251 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009252 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009253 return -ENOMEM;
9254 if (b_offset)
9255 memcpy(buf, &start, 4);
9256 if (odd_len)
9257 memcpy(buf+len-4, &end, 4);
9258 memcpy(buf + b_offset, data, eeprom->len);
9259 }
9260
9261 ret = tg3_nvram_write_block(tp, offset, len, buf);
9262
9263 if (buf != data)
9264 kfree(buf);
9265
9266 return ret;
9267}
9268
9269static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9270{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009271 struct tg3 *tp = netdev_priv(dev);
9272
9273 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009274 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009275 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
9276 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009277 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
9278 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009279 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009280
Linus Torvalds1da177e2005-04-16 15:20:36 -07009281 cmd->supported = (SUPPORTED_Autoneg);
9282
9283 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY))
9284 cmd->supported |= (SUPPORTED_1000baseT_Half |
9285 SUPPORTED_1000baseT_Full);
9286
Karsten Keilef348142006-05-12 12:49:08 -07009287 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009288 cmd->supported |= (SUPPORTED_100baseT_Half |
9289 SUPPORTED_100baseT_Full |
9290 SUPPORTED_10baseT_Half |
9291 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -08009292 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -07009293 cmd->port = PORT_TP;
9294 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009295 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -07009296 cmd->port = PORT_FIBRE;
9297 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009298
Linus Torvalds1da177e2005-04-16 15:20:36 -07009299 cmd->advertising = tp->link_config.advertising;
9300 if (netif_running(dev)) {
9301 cmd->speed = tp->link_config.active_speed;
9302 cmd->duplex = tp->link_config.active_duplex;
9303 }
Matt Carlson882e9792009-09-01 13:21:36 +00009304 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +00009305 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009306 cmd->autoneg = tp->link_config.autoneg;
9307 cmd->maxtxpkt = 0;
9308 cmd->maxrxpkt = 0;
9309 return 0;
9310}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009311
Linus Torvalds1da177e2005-04-16 15:20:36 -07009312static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9313{
9314 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009315
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009316 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009317 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009318 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
9319 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009320 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
9321 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009322 }
9323
Matt Carlson7e5856b2009-02-25 14:23:01 +00009324 if (cmd->autoneg != AUTONEG_ENABLE &&
9325 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -07009326 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +00009327
9328 if (cmd->autoneg == AUTONEG_DISABLE &&
9329 cmd->duplex != DUPLEX_FULL &&
9330 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -07009331 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009332
Matt Carlson7e5856b2009-02-25 14:23:01 +00009333 if (cmd->autoneg == AUTONEG_ENABLE) {
9334 u32 mask = ADVERTISED_Autoneg |
9335 ADVERTISED_Pause |
9336 ADVERTISED_Asym_Pause;
9337
9338 if (!(tp->tg3_flags2 & TG3_FLAG_10_100_ONLY))
9339 mask |= ADVERTISED_1000baseT_Half |
9340 ADVERTISED_1000baseT_Full;
9341
9342 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES))
9343 mask |= ADVERTISED_100baseT_Half |
9344 ADVERTISED_100baseT_Full |
9345 ADVERTISED_10baseT_Half |
9346 ADVERTISED_10baseT_Full |
9347 ADVERTISED_TP;
9348 else
9349 mask |= ADVERTISED_FIBRE;
9350
9351 if (cmd->advertising & ~mask)
9352 return -EINVAL;
9353
9354 mask &= (ADVERTISED_1000baseT_Half |
9355 ADVERTISED_1000baseT_Full |
9356 ADVERTISED_100baseT_Half |
9357 ADVERTISED_100baseT_Full |
9358 ADVERTISED_10baseT_Half |
9359 ADVERTISED_10baseT_Full);
9360
9361 cmd->advertising &= mask;
9362 } else {
9363 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES) {
9364 if (cmd->speed != SPEED_1000)
9365 return -EINVAL;
9366
9367 if (cmd->duplex != DUPLEX_FULL)
9368 return -EINVAL;
9369 } else {
9370 if (cmd->speed != SPEED_100 &&
9371 cmd->speed != SPEED_10)
9372 return -EINVAL;
9373 }
9374 }
9375
David S. Millerf47c11e2005-06-24 20:18:35 -07009376 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009377
9378 tp->link_config.autoneg = cmd->autoneg;
9379 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -07009380 tp->link_config.advertising = (cmd->advertising |
9381 ADVERTISED_Autoneg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009382 tp->link_config.speed = SPEED_INVALID;
9383 tp->link_config.duplex = DUPLEX_INVALID;
9384 } else {
9385 tp->link_config.advertising = 0;
9386 tp->link_config.speed = cmd->speed;
9387 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009388 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009389
Michael Chan24fcad62006-12-17 17:06:46 -08009390 tp->link_config.orig_speed = tp->link_config.speed;
9391 tp->link_config.orig_duplex = tp->link_config.duplex;
9392 tp->link_config.orig_autoneg = tp->link_config.autoneg;
9393
Linus Torvalds1da177e2005-04-16 15:20:36 -07009394 if (netif_running(dev))
9395 tg3_setup_phy(tp, 1);
9396
David S. Millerf47c11e2005-06-24 20:18:35 -07009397 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009398
Linus Torvalds1da177e2005-04-16 15:20:36 -07009399 return 0;
9400}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009401
Linus Torvalds1da177e2005-04-16 15:20:36 -07009402static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
9403{
9404 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009405
Linus Torvalds1da177e2005-04-16 15:20:36 -07009406 strcpy(info->driver, DRV_MODULE_NAME);
9407 strcpy(info->version, DRV_MODULE_VERSION);
Michael Chanc4e65752006-03-20 22:29:32 -08009408 strcpy(info->fw_version, tp->fw_ver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009409 strcpy(info->bus_info, pci_name(tp->pdev));
9410}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009411
Linus Torvalds1da177e2005-04-16 15:20:36 -07009412static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
9413{
9414 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009415
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009416 if ((tp->tg3_flags & TG3_FLAG_WOL_CAP) &&
9417 device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -07009418 wol->supported = WAKE_MAGIC;
9419 else
9420 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009421 wol->wolopts = 0;
Matt Carlson05ac4cb2008-11-03 16:53:46 -08009422 if ((tp->tg3_flags & TG3_FLAG_WOL_ENABLE) &&
9423 device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009424 wol->wolopts = WAKE_MAGIC;
9425 memset(&wol->sopass, 0, sizeof(wol->sopass));
9426}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009427
Linus Torvalds1da177e2005-04-16 15:20:36 -07009428static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
9429{
9430 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009431 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009432
Linus Torvalds1da177e2005-04-16 15:20:36 -07009433 if (wol->wolopts & ~WAKE_MAGIC)
9434 return -EINVAL;
9435 if ((wol->wolopts & WAKE_MAGIC) &&
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009436 !((tp->tg3_flags & TG3_FLAG_WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009437 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009438
David S. Millerf47c11e2005-06-24 20:18:35 -07009439 spin_lock_bh(&tp->lock);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009440 if (wol->wolopts & WAKE_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009441 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009442 device_set_wakeup_enable(dp, true);
9443 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009444 tp->tg3_flags &= ~TG3_FLAG_WOL_ENABLE;
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009445 device_set_wakeup_enable(dp, false);
9446 }
David S. Millerf47c11e2005-06-24 20:18:35 -07009447 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009448
Linus Torvalds1da177e2005-04-16 15:20:36 -07009449 return 0;
9450}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009451
Linus Torvalds1da177e2005-04-16 15:20:36 -07009452static u32 tg3_get_msglevel(struct net_device *dev)
9453{
9454 struct tg3 *tp = netdev_priv(dev);
9455 return tp->msg_enable;
9456}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009457
Linus Torvalds1da177e2005-04-16 15:20:36 -07009458static void tg3_set_msglevel(struct net_device *dev, u32 value)
9459{
9460 struct tg3 *tp = netdev_priv(dev);
9461 tp->msg_enable = value;
9462}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009463
Linus Torvalds1da177e2005-04-16 15:20:36 -07009464static int tg3_set_tso(struct net_device *dev, u32 value)
9465{
9466 struct tg3 *tp = netdev_priv(dev);
9467
9468 if (!(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
9469 if (value)
9470 return -EINVAL;
9471 return 0;
9472 }
Matt Carlson027455a2008-12-21 20:19:30 -08009473 if ((dev->features & NETIF_F_IPV6_CSUM) &&
9474 (tp->tg3_flags2 & TG3_FLG2_HW_TSO_2)) {
Matt Carlson9936bcf2007-10-10 18:03:07 -07009475 if (value) {
Michael Chanb0026622006-07-03 19:42:14 -07009476 dev->features |= NETIF_F_TSO6;
Matt Carlson57e69832008-05-25 23:48:31 -07009477 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
9478 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
9479 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Matt Carlson321d32a2008-11-21 17:22:19 -08009480 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009481 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
9482 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson9936bcf2007-10-10 18:03:07 -07009483 dev->features |= NETIF_F_TSO_ECN;
9484 } else
9485 dev->features &= ~(NETIF_F_TSO6 | NETIF_F_TSO_ECN);
Michael Chanb0026622006-07-03 19:42:14 -07009486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009487 return ethtool_op_set_tso(dev, value);
9488}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009489
Linus Torvalds1da177e2005-04-16 15:20:36 -07009490static int tg3_nway_reset(struct net_device *dev)
9491{
9492 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009493 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009494
Linus Torvalds1da177e2005-04-16 15:20:36 -07009495 if (!netif_running(dev))
9496 return -EAGAIN;
9497
Michael Chanc94e3942005-09-27 12:12:42 -07009498 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
9499 return -EINVAL;
9500
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009501 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
9502 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
9503 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009504 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009505 } else {
9506 u32 bmcr;
9507
9508 spin_lock_bh(&tp->lock);
9509 r = -EINVAL;
9510 tg3_readphy(tp, MII_BMCR, &bmcr);
9511 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
9512 ((bmcr & BMCR_ANENABLE) ||
9513 (tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT))) {
9514 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
9515 BMCR_ANENABLE);
9516 r = 0;
9517 }
9518 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009519 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009520
Linus Torvalds1da177e2005-04-16 15:20:36 -07009521 return r;
9522}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009523
Linus Torvalds1da177e2005-04-16 15:20:36 -07009524static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
9525{
9526 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009527
Linus Torvalds1da177e2005-04-16 15:20:36 -07009528 ering->rx_max_pending = TG3_RX_RING_SIZE - 1;
9529 ering->rx_mini_max_pending = 0;
Michael Chan4f81c322006-03-20 21:33:42 -08009530 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE)
9531 ering->rx_jumbo_max_pending = TG3_RX_JUMBO_RING_SIZE - 1;
9532 else
9533 ering->rx_jumbo_max_pending = 0;
9534
9535 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009536
9537 ering->rx_pending = tp->rx_pending;
9538 ering->rx_mini_pending = 0;
Michael Chan4f81c322006-03-20 21:33:42 -08009539 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE)
9540 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
9541 else
9542 ering->rx_jumbo_pending = 0;
9543
Matt Carlsonf3f3f272009-08-28 14:03:21 +00009544 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009545}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009546
Linus Torvalds1da177e2005-04-16 15:20:36 -07009547static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
9548{
9549 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +00009550 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009551
Linus Torvalds1da177e2005-04-16 15:20:36 -07009552 if ((ering->rx_pending > TG3_RX_RING_SIZE - 1) ||
9553 (ering->rx_jumbo_pending > TG3_RX_JUMBO_RING_SIZE - 1) ||
Michael Chanbc3a9252006-10-18 20:55:18 -07009554 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
9555 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Michael Chan7f62ad52007-02-20 23:25:40 -08009556 ((tp->tg3_flags2 & TG3_FLG2_TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -07009557 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009558 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009559
Michael Chanbbe832c2005-06-24 20:20:04 -07009560 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009561 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009562 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -07009563 irq_sync = 1;
9564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009565
Michael Chanbbe832c2005-06-24 20:20:04 -07009566 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009567
Linus Torvalds1da177e2005-04-16 15:20:36 -07009568 tp->rx_pending = ering->rx_pending;
9569
9570 if ((tp->tg3_flags2 & TG3_FLG2_MAX_RXPEND_64) &&
9571 tp->rx_pending > 63)
9572 tp->rx_pending = 63;
9573 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +00009574
9575 for (i = 0; i < TG3_IRQ_MAX_VECS; i++)
9576 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009577
9578 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -07009579 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -07009580 err = tg3_restart_hw(tp, 1);
9581 if (!err)
9582 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009583 }
9584
David S. Millerf47c11e2005-06-24 20:18:35 -07009585 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009586
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009587 if (irq_sync && !err)
9588 tg3_phy_start(tp);
9589
Michael Chanb9ec6c12006-07-25 16:37:27 -07009590 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009591}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009592
Linus Torvalds1da177e2005-04-16 15:20:36 -07009593static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
9594{
9595 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009596
Linus Torvalds1da177e2005-04-16 15:20:36 -07009597 epause->autoneg = (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG) != 0;
Matt Carlson8d018622007-12-20 20:05:44 -08009598
Steve Glendinninge18ce342008-12-16 02:00:00 -08009599 if (tp->link_config.active_flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -08009600 epause->rx_pause = 1;
9601 else
9602 epause->rx_pause = 0;
9603
Steve Glendinninge18ce342008-12-16 02:00:00 -08009604 if (tp->link_config.active_flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -08009605 epause->tx_pause = 1;
9606 else
9607 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009608}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009609
Linus Torvalds1da177e2005-04-16 15:20:36 -07009610static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
9611{
9612 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009613 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009614
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009615 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
9616 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
9617 return -EAGAIN;
9618
9619 if (epause->autoneg) {
9620 u32 newadv;
9621 struct phy_device *phydev;
9622
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009623 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009624
9625 if (epause->rx_pause) {
9626 if (epause->tx_pause)
9627 newadv = ADVERTISED_Pause;
9628 else
9629 newadv = ADVERTISED_Pause |
9630 ADVERTISED_Asym_Pause;
9631 } else if (epause->tx_pause) {
9632 newadv = ADVERTISED_Asym_Pause;
9633 } else
9634 newadv = 0;
9635
9636 if (tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED) {
9637 u32 oldadv = phydev->advertising &
9638 (ADVERTISED_Pause |
9639 ADVERTISED_Asym_Pause);
9640 if (oldadv != newadv) {
9641 phydev->advertising &=
9642 ~(ADVERTISED_Pause |
9643 ADVERTISED_Asym_Pause);
9644 phydev->advertising |= newadv;
9645 err = phy_start_aneg(phydev);
9646 }
9647 } else {
9648 tp->link_config.advertising &=
9649 ~(ADVERTISED_Pause |
9650 ADVERTISED_Asym_Pause);
9651 tp->link_config.advertising |= newadv;
9652 }
9653 } else {
9654 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -08009655 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009656 else
Steve Glendinninge18ce342008-12-16 02:00:00 -08009657 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009658
9659 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -08009660 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009661 else
Steve Glendinninge18ce342008-12-16 02:00:00 -08009662 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009663
9664 if (netif_running(dev))
9665 tg3_setup_flow_control(tp, 0, 0);
9666 }
9667 } else {
9668 int irq_sync = 0;
9669
9670 if (netif_running(dev)) {
9671 tg3_netif_stop(tp);
9672 irq_sync = 1;
9673 }
9674
9675 tg3_full_lock(tp, irq_sync);
9676
9677 if (epause->autoneg)
9678 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
9679 else
9680 tp->tg3_flags &= ~TG3_FLAG_PAUSE_AUTONEG;
9681 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -08009682 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009683 else
Steve Glendinninge18ce342008-12-16 02:00:00 -08009684 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009685 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -08009686 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009687 else
Steve Glendinninge18ce342008-12-16 02:00:00 -08009688 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009689
9690 if (netif_running(dev)) {
9691 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
9692 err = tg3_restart_hw(tp, 1);
9693 if (!err)
9694 tg3_netif_start(tp);
9695 }
9696
9697 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -07009698 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009699
Michael Chanb9ec6c12006-07-25 16:37:27 -07009700 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009701}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009702
Linus Torvalds1da177e2005-04-16 15:20:36 -07009703static u32 tg3_get_rx_csum(struct net_device *dev)
9704{
9705 struct tg3 *tp = netdev_priv(dev);
9706 return (tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) != 0;
9707}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009708
Linus Torvalds1da177e2005-04-16 15:20:36 -07009709static int tg3_set_rx_csum(struct net_device *dev, u32 data)
9710{
9711 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009712
Linus Torvalds1da177e2005-04-16 15:20:36 -07009713 if (tp->tg3_flags & TG3_FLAG_BROKEN_CHECKSUMS) {
9714 if (data != 0)
9715 return -EINVAL;
9716 return 0;
9717 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009718
David S. Millerf47c11e2005-06-24 20:18:35 -07009719 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009720 if (data)
9721 tp->tg3_flags |= TG3_FLAG_RX_CHECKSUMS;
9722 else
9723 tp->tg3_flags &= ~TG3_FLAG_RX_CHECKSUMS;
David S. Millerf47c11e2005-06-24 20:18:35 -07009724 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009725
Linus Torvalds1da177e2005-04-16 15:20:36 -07009726 return 0;
9727}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009728
Linus Torvalds1da177e2005-04-16 15:20:36 -07009729static int tg3_set_tx_csum(struct net_device *dev, u32 data)
9730{
9731 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009732
Linus Torvalds1da177e2005-04-16 15:20:36 -07009733 if (tp->tg3_flags & TG3_FLAG_BROKEN_CHECKSUMS) {
9734 if (data != 0)
9735 return -EINVAL;
9736 return 0;
9737 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009738
Matt Carlson321d32a2008-11-21 17:22:19 -08009739 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Michael Chan6460d942007-07-14 19:07:52 -07009740 ethtool_op_set_tx_ipv6_csum(dev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009741 else
Michael Chan9c27dbd2006-03-20 22:28:27 -08009742 ethtool_op_set_tx_csum(dev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009743
9744 return 0;
9745}
9746
Jeff Garzikb9f2c042007-10-03 18:07:32 -07009747static int tg3_get_sset_count (struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009748{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07009749 switch (sset) {
9750 case ETH_SS_TEST:
9751 return TG3_NUM_TEST;
9752 case ETH_SS_STATS:
9753 return TG3_NUM_STATS;
9754 default:
9755 return -EOPNOTSUPP;
9756 }
Michael Chan4cafd3f2005-05-29 14:56:34 -07009757}
9758
Linus Torvalds1da177e2005-04-16 15:20:36 -07009759static void tg3_get_strings (struct net_device *dev, u32 stringset, u8 *buf)
9760{
9761 switch (stringset) {
9762 case ETH_SS_STATS:
9763 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
9764 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -07009765 case ETH_SS_TEST:
9766 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
9767 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009768 default:
9769 WARN_ON(1); /* we need a WARN() */
9770 break;
9771 }
9772}
9773
Michael Chan4009a932005-09-05 17:52:54 -07009774static int tg3_phys_id(struct net_device *dev, u32 data)
9775{
9776 struct tg3 *tp = netdev_priv(dev);
9777 int i;
9778
9779 if (!netif_running(tp->dev))
9780 return -EAGAIN;
9781
9782 if (data == 0)
Stephen Hemminger759afc32008-02-23 19:51:59 -08009783 data = UINT_MAX / 2;
Michael Chan4009a932005-09-05 17:52:54 -07009784
9785 for (i = 0; i < (data * 2); i++) {
9786 if ((i % 2) == 0)
9787 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
9788 LED_CTRL_1000MBPS_ON |
9789 LED_CTRL_100MBPS_ON |
9790 LED_CTRL_10MBPS_ON |
9791 LED_CTRL_TRAFFIC_OVERRIDE |
9792 LED_CTRL_TRAFFIC_BLINK |
9793 LED_CTRL_TRAFFIC_LED);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009794
Michael Chan4009a932005-09-05 17:52:54 -07009795 else
9796 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
9797 LED_CTRL_TRAFFIC_OVERRIDE);
9798
9799 if (msleep_interruptible(500))
9800 break;
9801 }
9802 tw32(MAC_LED_CTRL, tp->led_ctrl);
9803 return 0;
9804}
9805
Linus Torvalds1da177e2005-04-16 15:20:36 -07009806static void tg3_get_ethtool_stats (struct net_device *dev,
9807 struct ethtool_stats *estats, u64 *tmp_stats)
9808{
9809 struct tg3 *tp = netdev_priv(dev);
9810 memcpy(tmp_stats, tg3_get_estats(tp), sizeof(tp->estats));
9811}
9812
Michael Chan566f86a2005-05-29 14:56:58 -07009813#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -08009814#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
9815#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
9816#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Michael Chanb16250e2006-09-27 16:10:14 -07009817#define NVRAM_SELFBOOT_HW_SIZE 0x20
9818#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -07009819
9820static int tg3_test_nvram(struct tg3 *tp)
9821{
Al Virob9fc7dc2007-12-17 22:59:57 -08009822 u32 csum, magic;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009823 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009824 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -07009825
Matt Carlsondf259d82009-04-20 06:57:14 +00009826 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM)
9827 return 0;
9828
Matt Carlsone4f34112009-02-25 14:25:00 +00009829 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -08009830 return -EIO;
9831
Michael Chan1b277772006-03-20 22:27:48 -08009832 if (magic == TG3_EEPROM_MAGIC)
9833 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -07009834 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -08009835 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
9836 TG3_EEPROM_SB_FORMAT_1) {
9837 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
9838 case TG3_EEPROM_SB_REVISION_0:
9839 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
9840 break;
9841 case TG3_EEPROM_SB_REVISION_2:
9842 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
9843 break;
9844 case TG3_EEPROM_SB_REVISION_3:
9845 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
9846 break;
9847 default:
9848 return 0;
9849 }
9850 } else
Michael Chan1b277772006-03-20 22:27:48 -08009851 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -07009852 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
9853 size = NVRAM_SELFBOOT_HW_SIZE;
9854 else
Michael Chan1b277772006-03-20 22:27:48 -08009855 return -EIO;
9856
9857 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -07009858 if (buf == NULL)
9859 return -ENOMEM;
9860
Michael Chan1b277772006-03-20 22:27:48 -08009861 err = -EIO;
9862 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +00009863 err = tg3_nvram_read_be32(tp, i, &buf[j]);
9864 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -07009865 break;
Michael Chan566f86a2005-05-29 14:56:58 -07009866 }
Michael Chan1b277772006-03-20 22:27:48 -08009867 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -07009868 goto out;
9869
Michael Chan1b277772006-03-20 22:27:48 -08009870 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +00009871 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -08009872 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -07009873 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -08009874 u8 *buf8 = (u8 *) buf, csum8 = 0;
9875
Al Virob9fc7dc2007-12-17 22:59:57 -08009876 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -08009877 TG3_EEPROM_SB_REVISION_2) {
9878 /* For rev 2, the csum doesn't include the MBA. */
9879 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
9880 csum8 += buf8[i];
9881 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
9882 csum8 += buf8[i];
9883 } else {
9884 for (i = 0; i < size; i++)
9885 csum8 += buf8[i];
9886 }
Michael Chan1b277772006-03-20 22:27:48 -08009887
Adrian Bunkad96b482006-04-05 22:21:04 -07009888 if (csum8 == 0) {
9889 err = 0;
9890 goto out;
9891 }
9892
9893 err = -EIO;
9894 goto out;
Michael Chan1b277772006-03-20 22:27:48 -08009895 }
Michael Chan566f86a2005-05-29 14:56:58 -07009896
Al Virob9fc7dc2007-12-17 22:59:57 -08009897 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -07009898 TG3_EEPROM_MAGIC_HW) {
9899 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +00009900 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -07009901 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -07009902
9903 /* Separate the parity bits and the data bytes. */
9904 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
9905 if ((i == 0) || (i == 8)) {
9906 int l;
9907 u8 msk;
9908
9909 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
9910 parity[k++] = buf8[i] & msk;
9911 i++;
9912 }
9913 else if (i == 16) {
9914 int l;
9915 u8 msk;
9916
9917 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
9918 parity[k++] = buf8[i] & msk;
9919 i++;
9920
9921 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
9922 parity[k++] = buf8[i] & msk;
9923 i++;
9924 }
9925 data[j++] = buf8[i];
9926 }
9927
9928 err = -EIO;
9929 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
9930 u8 hw8 = hweight8(data[i]);
9931
9932 if ((hw8 & 0x1) && parity[i])
9933 goto out;
9934 else if (!(hw8 & 0x1) && !parity[i])
9935 goto out;
9936 }
9937 err = 0;
9938 goto out;
9939 }
9940
Michael Chan566f86a2005-05-29 14:56:58 -07009941 /* Bootstrap checksum at offset 0x10 */
9942 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlsona9dc5292009-02-25 14:25:30 +00009943 if (csum != be32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -07009944 goto out;
9945
9946 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
9947 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlsona9dc5292009-02-25 14:25:30 +00009948 if (csum != be32_to_cpu(buf[0xfc/4]))
9949 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -07009950
9951 err = 0;
9952
9953out:
9954 kfree(buf);
9955 return err;
9956}
9957
Michael Chanca430072005-05-29 14:57:23 -07009958#define TG3_SERDES_TIMEOUT_SEC 2
9959#define TG3_COPPER_TIMEOUT_SEC 6
9960
9961static int tg3_test_link(struct tg3 *tp)
9962{
9963 int i, max;
9964
9965 if (!netif_running(tp->dev))
9966 return -ENODEV;
9967
Michael Chan4c987482005-09-05 17:52:38 -07009968 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -07009969 max = TG3_SERDES_TIMEOUT_SEC;
9970 else
9971 max = TG3_COPPER_TIMEOUT_SEC;
9972
9973 for (i = 0; i < max; i++) {
9974 if (netif_carrier_ok(tp->dev))
9975 return 0;
9976
9977 if (msleep_interruptible(1000))
9978 break;
9979 }
9980
9981 return -EIO;
9982}
9983
Michael Chana71116d2005-05-29 14:58:11 -07009984/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -08009985static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -07009986{
Michael Chanb16250e2006-09-27 16:10:14 -07009987 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -07009988 u32 offset, read_mask, write_mask, val, save_val, read_val;
9989 static struct {
9990 u16 offset;
9991 u16 flags;
9992#define TG3_FL_5705 0x1
9993#define TG3_FL_NOT_5705 0x2
9994#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -07009995#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -07009996 u32 read_mask;
9997 u32 write_mask;
9998 } reg_tbl[] = {
9999 /* MAC Control Registers */
10000 { MAC_MODE, TG3_FL_NOT_5705,
10001 0x00000000, 0x00ef6f8c },
10002 { MAC_MODE, TG3_FL_5705,
10003 0x00000000, 0x01ef6b8c },
10004 { MAC_STATUS, TG3_FL_NOT_5705,
10005 0x03800107, 0x00000000 },
10006 { MAC_STATUS, TG3_FL_5705,
10007 0x03800100, 0x00000000 },
10008 { MAC_ADDR_0_HIGH, 0x0000,
10009 0x00000000, 0x0000ffff },
10010 { MAC_ADDR_0_LOW, 0x0000,
10011 0x00000000, 0xffffffff },
10012 { MAC_RX_MTU_SIZE, 0x0000,
10013 0x00000000, 0x0000ffff },
10014 { MAC_TX_MODE, 0x0000,
10015 0x00000000, 0x00000070 },
10016 { MAC_TX_LENGTHS, 0x0000,
10017 0x00000000, 0x00003fff },
10018 { MAC_RX_MODE, TG3_FL_NOT_5705,
10019 0x00000000, 0x000007fc },
10020 { MAC_RX_MODE, TG3_FL_5705,
10021 0x00000000, 0x000007dc },
10022 { MAC_HASH_REG_0, 0x0000,
10023 0x00000000, 0xffffffff },
10024 { MAC_HASH_REG_1, 0x0000,
10025 0x00000000, 0xffffffff },
10026 { MAC_HASH_REG_2, 0x0000,
10027 0x00000000, 0xffffffff },
10028 { MAC_HASH_REG_3, 0x0000,
10029 0x00000000, 0xffffffff },
10030
10031 /* Receive Data and Receive BD Initiator Control Registers. */
10032 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
10033 0x00000000, 0xffffffff },
10034 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
10035 0x00000000, 0xffffffff },
10036 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
10037 0x00000000, 0x00000003 },
10038 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
10039 0x00000000, 0xffffffff },
10040 { RCVDBDI_STD_BD+0, 0x0000,
10041 0x00000000, 0xffffffff },
10042 { RCVDBDI_STD_BD+4, 0x0000,
10043 0x00000000, 0xffffffff },
10044 { RCVDBDI_STD_BD+8, 0x0000,
10045 0x00000000, 0xffff0002 },
10046 { RCVDBDI_STD_BD+0xc, 0x0000,
10047 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010048
Michael Chana71116d2005-05-29 14:58:11 -070010049 /* Receive BD Initiator Control Registers. */
10050 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
10051 0x00000000, 0xffffffff },
10052 { RCVBDI_STD_THRESH, TG3_FL_5705,
10053 0x00000000, 0x000003ff },
10054 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
10055 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010056
Michael Chana71116d2005-05-29 14:58:11 -070010057 /* Host Coalescing Control Registers. */
10058 { HOSTCC_MODE, TG3_FL_NOT_5705,
10059 0x00000000, 0x00000004 },
10060 { HOSTCC_MODE, TG3_FL_5705,
10061 0x00000000, 0x000000f6 },
10062 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
10063 0x00000000, 0xffffffff },
10064 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
10065 0x00000000, 0x000003ff },
10066 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
10067 0x00000000, 0xffffffff },
10068 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
10069 0x00000000, 0x000003ff },
10070 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
10071 0x00000000, 0xffffffff },
10072 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10073 0x00000000, 0x000000ff },
10074 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
10075 0x00000000, 0xffffffff },
10076 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10077 0x00000000, 0x000000ff },
10078 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
10079 0x00000000, 0xffffffff },
10080 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
10081 0x00000000, 0xffffffff },
10082 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10083 0x00000000, 0xffffffff },
10084 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10085 0x00000000, 0x000000ff },
10086 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10087 0x00000000, 0xffffffff },
10088 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10089 0x00000000, 0x000000ff },
10090 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
10091 0x00000000, 0xffffffff },
10092 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
10093 0x00000000, 0xffffffff },
10094 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
10095 0x00000000, 0xffffffff },
10096 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
10097 0x00000000, 0xffffffff },
10098 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
10099 0x00000000, 0xffffffff },
10100 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
10101 0xffffffff, 0x00000000 },
10102 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
10103 0xffffffff, 0x00000000 },
10104
10105 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070010106 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010107 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070010108 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010109 0x00000000, 0x007fffff },
10110 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
10111 0x00000000, 0x0000003f },
10112 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
10113 0x00000000, 0x000001ff },
10114 { BUFMGR_MB_HIGH_WATER, 0x0000,
10115 0x00000000, 0x000001ff },
10116 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
10117 0xffffffff, 0x00000000 },
10118 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
10119 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010120
Michael Chana71116d2005-05-29 14:58:11 -070010121 /* Mailbox Registers */
10122 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
10123 0x00000000, 0x000001ff },
10124 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
10125 0x00000000, 0x000001ff },
10126 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
10127 0x00000000, 0x000007ff },
10128 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
10129 0x00000000, 0x000001ff },
10130
10131 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
10132 };
10133
Michael Chanb16250e2006-09-27 16:10:14 -070010134 is_5705 = is_5750 = 0;
10135 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
Michael Chana71116d2005-05-29 14:58:11 -070010136 is_5705 = 1;
Michael Chanb16250e2006-09-27 16:10:14 -070010137 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
10138 is_5750 = 1;
10139 }
Michael Chana71116d2005-05-29 14:58:11 -070010140
10141 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
10142 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
10143 continue;
10144
10145 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
10146 continue;
10147
10148 if ((tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
10149 (reg_tbl[i].flags & TG3_FL_NOT_5788))
10150 continue;
10151
Michael Chanb16250e2006-09-27 16:10:14 -070010152 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
10153 continue;
10154
Michael Chana71116d2005-05-29 14:58:11 -070010155 offset = (u32) reg_tbl[i].offset;
10156 read_mask = reg_tbl[i].read_mask;
10157 write_mask = reg_tbl[i].write_mask;
10158
10159 /* Save the original register content */
10160 save_val = tr32(offset);
10161
10162 /* Determine the read-only value. */
10163 read_val = save_val & read_mask;
10164
10165 /* Write zero to the register, then make sure the read-only bits
10166 * are not changed and the read/write bits are all zeros.
10167 */
10168 tw32(offset, 0);
10169
10170 val = tr32(offset);
10171
10172 /* Test the read-only and read/write bits. */
10173 if (((val & read_mask) != read_val) || (val & write_mask))
10174 goto out;
10175
10176 /* Write ones to all the bits defined by RdMask and WrMask, then
10177 * make sure the read-only bits are not changed and the
10178 * read/write bits are all ones.
10179 */
10180 tw32(offset, read_mask | write_mask);
10181
10182 val = tr32(offset);
10183
10184 /* Test the read-only bits. */
10185 if ((val & read_mask) != read_val)
10186 goto out;
10187
10188 /* Test the read/write bits. */
10189 if ((val & write_mask) != write_mask)
10190 goto out;
10191
10192 tw32(offset, save_val);
10193 }
10194
10195 return 0;
10196
10197out:
Michael Chan9f88f292006-12-07 00:22:54 -080010198 if (netif_msg_hw(tp))
10199 printk(KERN_ERR PFX "Register test failed at offset %x\n",
10200 offset);
Michael Chana71116d2005-05-29 14:58:11 -070010201 tw32(offset, save_val);
10202 return -EIO;
10203}
10204
Michael Chan7942e1d2005-05-29 14:58:36 -070010205static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
10206{
Arjan van de Venf71e1302006-03-03 21:33:57 -050010207 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070010208 int i;
10209 u32 j;
10210
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020010211 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070010212 for (j = 0; j < len; j += 4) {
10213 u32 val;
10214
10215 tg3_write_mem(tp, offset + j, test_pattern[i]);
10216 tg3_read_mem(tp, offset + j, &val);
10217 if (val != test_pattern[i])
10218 return -EIO;
10219 }
10220 }
10221 return 0;
10222}
10223
10224static int tg3_test_memory(struct tg3 *tp)
10225{
10226 static struct mem_entry {
10227 u32 offset;
10228 u32 len;
10229 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080010230 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070010231 { 0x00002000, 0x1c000},
10232 { 0xffffffff, 0x00000}
10233 }, mem_tbl_5705[] = {
10234 { 0x00000100, 0x0000c},
10235 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070010236 { 0x00004000, 0x00800},
10237 { 0x00006000, 0x01000},
10238 { 0x00008000, 0x02000},
10239 { 0x00010000, 0x0e000},
10240 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080010241 }, mem_tbl_5755[] = {
10242 { 0x00000200, 0x00008},
10243 { 0x00004000, 0x00800},
10244 { 0x00006000, 0x00800},
10245 { 0x00008000, 0x02000},
10246 { 0x00010000, 0x0c000},
10247 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070010248 }, mem_tbl_5906[] = {
10249 { 0x00000200, 0x00008},
10250 { 0x00004000, 0x00400},
10251 { 0x00006000, 0x00400},
10252 { 0x00008000, 0x01000},
10253 { 0x00010000, 0x01000},
10254 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070010255 };
10256 struct mem_entry *mem_tbl;
10257 int err = 0;
10258 int i;
10259
Matt Carlson321d32a2008-11-21 17:22:19 -080010260 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
10261 mem_tbl = mem_tbl_5755;
10262 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
10263 mem_tbl = mem_tbl_5906;
10264 else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
10265 mem_tbl = mem_tbl_5705;
10266 else
Michael Chan7942e1d2005-05-29 14:58:36 -070010267 mem_tbl = mem_tbl_570x;
10268
10269 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
10270 if ((err = tg3_do_mem_test(tp, mem_tbl[i].offset,
10271 mem_tbl[i].len)) != 0)
10272 break;
10273 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010274
Michael Chan7942e1d2005-05-29 14:58:36 -070010275 return err;
10276}
10277
Michael Chan9f40dea2005-09-05 17:53:06 -070010278#define TG3_MAC_LOOPBACK 0
10279#define TG3_PHY_LOOPBACK 1
10280
10281static int tg3_run_loopback(struct tg3 *tp, int loopback_mode)
Michael Chanc76949a2005-05-29 14:58:59 -070010282{
Michael Chan9f40dea2005-09-05 17:53:06 -070010283 u32 mac_mode, rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010284 u32 desc_idx, coal_now;
Michael Chanc76949a2005-05-29 14:58:59 -070010285 struct sk_buff *skb, *rx_skb;
10286 u8 *tx_data;
10287 dma_addr_t map;
10288 int num_pkts, tx_len, rx_len, i, err;
10289 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000010290 struct tg3_napi *tnapi, *rnapi;
Matt Carlson21f581a2009-08-28 14:00:25 +000010291 struct tg3_rx_prodring_set *tpr = &tp->prodring[0];
Michael Chanc76949a2005-05-29 14:58:59 -070010292
Matt Carlson0c1d0e22009-09-01 13:16:33 +000010293 if (tp->irq_cnt > 1) {
10294 tnapi = &tp->napi[1];
10295 rnapi = &tp->napi[1];
10296 } else {
10297 tnapi = &tp->napi[0];
10298 rnapi = &tp->napi[0];
10299 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010300 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000010301
Michael Chan9f40dea2005-09-05 17:53:06 -070010302 if (loopback_mode == TG3_MAC_LOOPBACK) {
Michael Chanc94e3942005-09-27 12:12:42 -070010303 /* HW errata - mac loopback fails in some cases on 5780.
10304 * Normal traffic and PHY loopback are not affected by
10305 * errata.
10306 */
10307 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780)
10308 return 0;
10309
Michael Chan9f40dea2005-09-05 17:53:06 -070010310 mac_mode = (tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK) |
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010311 MAC_MODE_PORT_INT_LPBACK;
10312 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
10313 mac_mode |= MAC_MODE_LINK_POLARITY;
Michael Chan3f7045c2006-09-27 16:02:29 -070010314 if (tp->tg3_flags & TG3_FLAG_10_100_ONLY)
10315 mac_mode |= MAC_MODE_PORT_MODE_MII;
10316 else
10317 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chan9f40dea2005-09-05 17:53:06 -070010318 tw32(MAC_MODE, mac_mode);
10319 } else if (loopback_mode == TG3_PHY_LOOPBACK) {
Michael Chan3f7045c2006-09-27 16:02:29 -070010320 u32 val;
10321
Matt Carlson7f97a4b2009-08-25 10:10:03 +000010322 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
10323 tg3_phy_fet_toggle_apd(tp, false);
Michael Chan5d64ad32006-12-07 00:19:40 -080010324 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED100;
10325 } else
10326 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED1000;
Michael Chan3f7045c2006-09-27 16:02:29 -070010327
Matt Carlson9ef8ca92007-07-11 19:48:29 -070010328 tg3_phy_toggle_automdix(tp, 0);
10329
Michael Chan3f7045c2006-09-27 16:02:29 -070010330 tg3_writephy(tp, MII_BMCR, val);
Michael Chanc94e3942005-09-27 12:12:42 -070010331 udelay(40);
Michael Chan5d64ad32006-12-07 00:19:40 -080010332
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010333 mac_mode = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000010334 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
10335 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
10336 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x1800);
Michael Chan5d64ad32006-12-07 00:19:40 -080010337 mac_mode |= MAC_MODE_PORT_MODE_MII;
10338 } else
10339 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chanb16250e2006-09-27 16:10:14 -070010340
Michael Chanc94e3942005-09-27 12:12:42 -070010341 /* reset to prevent losing 1st rx packet intermittently */
10342 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES) {
10343 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
10344 udelay(10);
10345 tw32_f(MAC_RX_MODE, tp->rx_mode);
10346 }
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010347 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
10348 if ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5401)
10349 mac_mode &= ~MAC_MODE_LINK_POLARITY;
10350 else if ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5411)
10351 mac_mode |= MAC_MODE_LINK_POLARITY;
Michael Chanff18ff02006-03-27 23:17:27 -080010352 tg3_writephy(tp, MII_TG3_EXT_CTRL,
10353 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
10354 }
Michael Chan9f40dea2005-09-05 17:53:06 -070010355 tw32(MAC_MODE, mac_mode);
Michael Chan9f40dea2005-09-05 17:53:06 -070010356 }
10357 else
10358 return -EINVAL;
Michael Chanc76949a2005-05-29 14:58:59 -070010359
10360 err = -EIO;
10361
Michael Chanc76949a2005-05-29 14:58:59 -070010362 tx_len = 1514;
David S. Millera20e9c62006-07-31 22:38:16 -070010363 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070010364 if (!skb)
10365 return -ENOMEM;
10366
Michael Chanc76949a2005-05-29 14:58:59 -070010367 tx_data = skb_put(skb, tx_len);
10368 memcpy(tx_data, tp->dev->dev_addr, 6);
10369 memset(tx_data + 6, 0x0, 8);
10370
10371 tw32(MAC_RX_MTU_SIZE, tx_len + 4);
10372
10373 for (i = 14; i < tx_len; i++)
10374 tx_data[i] = (u8) (i & 0xff);
10375
Matt Carlsona21771d2009-11-02 14:25:31 +000010376 if (skb_dma_map(&tp->pdev->dev, skb, DMA_TO_DEVICE)) {
10377 dev_kfree_skb(skb);
10378 return -EIO;
10379 }
Michael Chanc76949a2005-05-29 14:58:59 -070010380
10381 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010382 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070010383
10384 udelay(10);
10385
Matt Carlson898a56f2009-08-28 14:02:40 +000010386 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070010387
Michael Chanc76949a2005-05-29 14:58:59 -070010388 num_pkts = 0;
10389
Matt Carlsona21771d2009-11-02 14:25:31 +000010390 tg3_set_txd(tnapi, tnapi->tx_prod,
10391 skb_shinfo(skb)->dma_head, tx_len, 0, 1);
Michael Chanc76949a2005-05-29 14:58:59 -070010392
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010393 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070010394 num_pkts++;
10395
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010396 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
10397 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070010398
10399 udelay(10);
10400
Michael Chan3f7045c2006-09-27 16:02:29 -070010401 /* 250 usec to allow enough time on some 10/100 Mbps devices. */
10402 for (i = 0; i < 25; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070010403 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010404 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070010405
10406 udelay(10);
10407
Matt Carlson898a56f2009-08-28 14:02:40 +000010408 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
10409 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010410 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070010411 (rx_idx == (rx_start_idx + num_pkts)))
10412 break;
10413 }
10414
Matt Carlsona21771d2009-11-02 14:25:31 +000010415 skb_dma_unmap(&tp->pdev->dev, skb, DMA_TO_DEVICE);
Michael Chanc76949a2005-05-29 14:58:59 -070010416 dev_kfree_skb(skb);
10417
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010418 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070010419 goto out;
10420
10421 if (rx_idx != rx_start_idx + num_pkts)
10422 goto out;
10423
Matt Carlson72334482009-08-28 14:03:01 +000010424 desc = &rnapi->rx_rcb[rx_start_idx];
Michael Chanc76949a2005-05-29 14:58:59 -070010425 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
10426 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
10427 if (opaque_key != RXD_OPAQUE_RING_STD)
10428 goto out;
10429
10430 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
10431 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
10432 goto out;
10433
10434 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) - 4;
10435 if (rx_len != tx_len)
10436 goto out;
10437
Matt Carlson21f581a2009-08-28 14:00:25 +000010438 rx_skb = tpr->rx_std_buffers[desc_idx].skb;
Michael Chanc76949a2005-05-29 14:58:59 -070010439
Matt Carlson21f581a2009-08-28 14:00:25 +000010440 map = pci_unmap_addr(&tpr->rx_std_buffers[desc_idx], mapping);
Michael Chanc76949a2005-05-29 14:58:59 -070010441 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len, PCI_DMA_FROMDEVICE);
10442
10443 for (i = 14; i < tx_len; i++) {
10444 if (*(rx_skb->data + i) != (u8) (i & 0xff))
10445 goto out;
10446 }
10447 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010448
Michael Chanc76949a2005-05-29 14:58:59 -070010449 /* tg3_free_rings will unmap and free the rx_skb */
10450out:
10451 return err;
10452}
10453
Michael Chan9f40dea2005-09-05 17:53:06 -070010454#define TG3_MAC_LOOPBACK_FAILED 1
10455#define TG3_PHY_LOOPBACK_FAILED 2
10456#define TG3_LOOPBACK_FAILED (TG3_MAC_LOOPBACK_FAILED | \
10457 TG3_PHY_LOOPBACK_FAILED)
10458
10459static int tg3_test_loopback(struct tg3 *tp)
10460{
10461 int err = 0;
Matt Carlson9936bcf2007-10-10 18:03:07 -070010462 u32 cpmuctrl = 0;
Michael Chan9f40dea2005-09-05 17:53:06 -070010463
10464 if (!netif_running(tp->dev))
10465 return TG3_LOOPBACK_FAILED;
10466
Michael Chanb9ec6c12006-07-25 16:37:27 -070010467 err = tg3_reset_hw(tp, 1);
10468 if (err)
10469 return TG3_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070010470
Matt Carlson6833c042008-11-21 17:18:59 -080010471 /* Turn off gphy autopowerdown. */
10472 if (tp->tg3_flags3 & TG3_FLG3_PHY_ENABLE_APD)
10473 tg3_phy_toggle_apd(tp, false);
10474
Matt Carlson321d32a2008-11-21 17:22:19 -080010475 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070010476 int i;
10477 u32 status;
10478
10479 tw32(TG3_CPMU_MUTEX_REQ, CPMU_MUTEX_REQ_DRIVER);
10480
10481 /* Wait for up to 40 microseconds to acquire lock. */
10482 for (i = 0; i < 4; i++) {
10483 status = tr32(TG3_CPMU_MUTEX_GNT);
10484 if (status == CPMU_MUTEX_GNT_DRIVER)
10485 break;
10486 udelay(10);
10487 }
10488
10489 if (status != CPMU_MUTEX_GNT_DRIVER)
10490 return TG3_LOOPBACK_FAILED;
10491
Matt Carlsonb2a5c192008-04-03 21:44:44 -070010492 /* Turn off link-based power management. */
Matt Carlsone8750932007-11-12 21:11:51 -080010493 cpmuctrl = tr32(TG3_CPMU_CTRL);
Matt Carlson109115e2008-05-02 16:48:59 -070010494 tw32(TG3_CPMU_CTRL,
10495 cpmuctrl & ~(CPMU_CTRL_LINK_SPEED_MODE |
10496 CPMU_CTRL_LINK_AWARE_MODE));
Matt Carlson9936bcf2007-10-10 18:03:07 -070010497 }
10498
Michael Chan9f40dea2005-09-05 17:53:06 -070010499 if (tg3_run_loopback(tp, TG3_MAC_LOOPBACK))
10500 err |= TG3_MAC_LOOPBACK_FAILED;
Matt Carlson9936bcf2007-10-10 18:03:07 -070010501
Matt Carlson321d32a2008-11-21 17:22:19 -080010502 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070010503 tw32(TG3_CPMU_CTRL, cpmuctrl);
10504
10505 /* Release the mutex */
10506 tw32(TG3_CPMU_MUTEX_GNT, CPMU_MUTEX_GNT_DRIVER);
10507 }
10508
Matt Carlsondd477002008-05-25 23:45:58 -070010509 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
10510 !(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)) {
Michael Chan9f40dea2005-09-05 17:53:06 -070010511 if (tg3_run_loopback(tp, TG3_PHY_LOOPBACK))
10512 err |= TG3_PHY_LOOPBACK_FAILED;
10513 }
10514
Matt Carlson6833c042008-11-21 17:18:59 -080010515 /* Re-enable gphy autopowerdown. */
10516 if (tp->tg3_flags3 & TG3_FLG3_PHY_ENABLE_APD)
10517 tg3_phy_toggle_apd(tp, true);
10518
Michael Chan9f40dea2005-09-05 17:53:06 -070010519 return err;
10520}
10521
Michael Chan4cafd3f2005-05-29 14:56:34 -070010522static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
10523 u64 *data)
10524{
Michael Chan566f86a2005-05-29 14:56:58 -070010525 struct tg3 *tp = netdev_priv(dev);
10526
Michael Chanbc1c7562006-03-20 17:48:03 -080010527 if (tp->link_config.phy_is_low_power)
10528 tg3_set_power_state(tp, PCI_D0);
10529
Michael Chan566f86a2005-05-29 14:56:58 -070010530 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
10531
10532 if (tg3_test_nvram(tp) != 0) {
10533 etest->flags |= ETH_TEST_FL_FAILED;
10534 data[0] = 1;
10535 }
Michael Chanca430072005-05-29 14:57:23 -070010536 if (tg3_test_link(tp) != 0) {
10537 etest->flags |= ETH_TEST_FL_FAILED;
10538 data[1] = 1;
10539 }
Michael Chana71116d2005-05-29 14:58:11 -070010540 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010541 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070010542
Michael Chanbbe832c2005-06-24 20:20:04 -070010543 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010544 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010545 tg3_netif_stop(tp);
10546 irq_sync = 1;
10547 }
10548
10549 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070010550
10551 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080010552 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070010553 tg3_halt_cpu(tp, RX_CPU_BASE);
10554 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
10555 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080010556 if (!err)
10557 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070010558
Michael Chand9ab5ad2006-03-20 22:27:35 -080010559 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)
10560 tg3_phy_reset(tp);
10561
Michael Chana71116d2005-05-29 14:58:11 -070010562 if (tg3_test_registers(tp) != 0) {
10563 etest->flags |= ETH_TEST_FL_FAILED;
10564 data[2] = 1;
10565 }
Michael Chan7942e1d2005-05-29 14:58:36 -070010566 if (tg3_test_memory(tp) != 0) {
10567 etest->flags |= ETH_TEST_FL_FAILED;
10568 data[3] = 1;
10569 }
Michael Chan9f40dea2005-09-05 17:53:06 -070010570 if ((data[4] = tg3_test_loopback(tp)) != 0)
Michael Chanc76949a2005-05-29 14:58:59 -070010571 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070010572
David S. Millerf47c11e2005-06-24 20:18:35 -070010573 tg3_full_unlock(tp);
10574
Michael Chand4bc3922005-05-29 14:59:20 -070010575 if (tg3_test_interrupt(tp) != 0) {
10576 etest->flags |= ETH_TEST_FL_FAILED;
10577 data[5] = 1;
10578 }
David S. Millerf47c11e2005-06-24 20:18:35 -070010579
10580 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070010581
Michael Chana71116d2005-05-29 14:58:11 -070010582 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10583 if (netif_running(dev)) {
10584 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010585 err2 = tg3_restart_hw(tp, 1);
10586 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070010587 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070010588 }
David S. Millerf47c11e2005-06-24 20:18:35 -070010589
10590 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010591
10592 if (irq_sync && !err2)
10593 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070010594 }
Michael Chanbc1c7562006-03-20 17:48:03 -080010595 if (tp->link_config.phy_is_low_power)
10596 tg3_set_power_state(tp, PCI_D3hot);
10597
Michael Chan4cafd3f2005-05-29 14:56:34 -070010598}
10599
Linus Torvalds1da177e2005-04-16 15:20:36 -070010600static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
10601{
10602 struct mii_ioctl_data *data = if_mii(ifr);
10603 struct tg3 *tp = netdev_priv(dev);
10604 int err;
10605
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010606 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010607 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010608 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
10609 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010610 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10611 return phy_mii_ioctl(phydev, data, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010612 }
10613
Linus Torvalds1da177e2005-04-16 15:20:36 -070010614 switch(cmd) {
10615 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000010616 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010617
10618 /* fallthru */
10619 case SIOCGMIIREG: {
10620 u32 mii_regval;
10621
10622 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
10623 break; /* We have no PHY */
10624
Michael Chanbc1c7562006-03-20 17:48:03 -080010625 if (tp->link_config.phy_is_low_power)
10626 return -EAGAIN;
10627
David S. Millerf47c11e2005-06-24 20:18:35 -070010628 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010629 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070010630 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010631
10632 data->val_out = mii_regval;
10633
10634 return err;
10635 }
10636
10637 case SIOCSMIIREG:
10638 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
10639 break; /* We have no PHY */
10640
Michael Chanbc1c7562006-03-20 17:48:03 -080010641 if (tp->link_config.phy_is_low_power)
10642 return -EAGAIN;
10643
David S. Millerf47c11e2005-06-24 20:18:35 -070010644 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010645 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070010646 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010647
10648 return err;
10649
10650 default:
10651 /* do nothing */
10652 break;
10653 }
10654 return -EOPNOTSUPP;
10655}
10656
10657#if TG3_VLAN_TAG_USED
10658static void tg3_vlan_rx_register(struct net_device *dev, struct vlan_group *grp)
10659{
10660 struct tg3 *tp = netdev_priv(dev);
10661
Matt Carlson844b3ee2009-02-25 14:23:56 +000010662 if (!netif_running(dev)) {
10663 tp->vlgrp = grp;
10664 return;
10665 }
10666
10667 tg3_netif_stop(tp);
Michael Chan29315e82006-06-29 20:12:30 -070010668
David S. Millerf47c11e2005-06-24 20:18:35 -070010669 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010670
10671 tp->vlgrp = grp;
10672
10673 /* Update RX_MODE_KEEP_VLAN_TAG bit in RX_MODE register. */
10674 __tg3_set_rx_mode(dev);
10675
Matt Carlson844b3ee2009-02-25 14:23:56 +000010676 tg3_netif_start(tp);
Michael Chan46966542007-07-11 19:47:19 -070010677
10678 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010679}
Linus Torvalds1da177e2005-04-16 15:20:36 -070010680#endif
10681
David S. Miller15f98502005-05-18 22:49:26 -070010682static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
10683{
10684 struct tg3 *tp = netdev_priv(dev);
10685
10686 memcpy(ec, &tp->coal, sizeof(*ec));
10687 return 0;
10688}
10689
Michael Chand244c892005-07-05 14:42:33 -070010690static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
10691{
10692 struct tg3 *tp = netdev_priv(dev);
10693 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
10694 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
10695
10696 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
10697 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
10698 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
10699 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
10700 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
10701 }
10702
10703 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
10704 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
10705 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
10706 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
10707 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
10708 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
10709 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
10710 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
10711 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
10712 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
10713 return -EINVAL;
10714
10715 /* No rx interrupts will be generated if both are zero */
10716 if ((ec->rx_coalesce_usecs == 0) &&
10717 (ec->rx_max_coalesced_frames == 0))
10718 return -EINVAL;
10719
10720 /* No tx interrupts will be generated if both are zero */
10721 if ((ec->tx_coalesce_usecs == 0) &&
10722 (ec->tx_max_coalesced_frames == 0))
10723 return -EINVAL;
10724
10725 /* Only copy relevant parameters, ignore all others. */
10726 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
10727 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
10728 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
10729 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
10730 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
10731 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
10732 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
10733 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
10734 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
10735
10736 if (netif_running(dev)) {
10737 tg3_full_lock(tp, 0);
10738 __tg3_set_coalesce(tp, &tp->coal);
10739 tg3_full_unlock(tp);
10740 }
10741 return 0;
10742}
10743
Jeff Garzik7282d492006-09-13 14:30:00 -040010744static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010745 .get_settings = tg3_get_settings,
10746 .set_settings = tg3_set_settings,
10747 .get_drvinfo = tg3_get_drvinfo,
10748 .get_regs_len = tg3_get_regs_len,
10749 .get_regs = tg3_get_regs,
10750 .get_wol = tg3_get_wol,
10751 .set_wol = tg3_set_wol,
10752 .get_msglevel = tg3_get_msglevel,
10753 .set_msglevel = tg3_set_msglevel,
10754 .nway_reset = tg3_nway_reset,
10755 .get_link = ethtool_op_get_link,
10756 .get_eeprom_len = tg3_get_eeprom_len,
10757 .get_eeprom = tg3_get_eeprom,
10758 .set_eeprom = tg3_set_eeprom,
10759 .get_ringparam = tg3_get_ringparam,
10760 .set_ringparam = tg3_set_ringparam,
10761 .get_pauseparam = tg3_get_pauseparam,
10762 .set_pauseparam = tg3_set_pauseparam,
10763 .get_rx_csum = tg3_get_rx_csum,
10764 .set_rx_csum = tg3_set_rx_csum,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010765 .set_tx_csum = tg3_set_tx_csum,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010766 .set_sg = ethtool_op_set_sg,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010767 .set_tso = tg3_set_tso,
Michael Chan4cafd3f2005-05-29 14:56:34 -070010768 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010769 .get_strings = tg3_get_strings,
Michael Chan4009a932005-09-05 17:52:54 -070010770 .phys_id = tg3_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010771 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070010772 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070010773 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010774 .get_sset_count = tg3_get_sset_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010775};
10776
10777static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
10778{
Michael Chan1b277772006-03-20 22:27:48 -080010779 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010780
10781 tp->nvram_size = EEPROM_CHIP_SIZE;
10782
Matt Carlsone4f34112009-02-25 14:25:00 +000010783 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010784 return;
10785
Michael Chanb16250e2006-09-27 16:10:14 -070010786 if ((magic != TG3_EEPROM_MAGIC) &&
10787 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
10788 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010789 return;
10790
10791 /*
10792 * Size the chip by reading offsets at increasing powers of two.
10793 * When we encounter our validation signature, we know the addressing
10794 * has wrapped around, and thus have our chip size.
10795 */
Michael Chan1b277772006-03-20 22:27:48 -080010796 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010797
10798 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000010799 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010800 return;
10801
Michael Chan18201802006-03-20 22:29:15 -080010802 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010803 break;
10804
10805 cursize <<= 1;
10806 }
10807
10808 tp->nvram_size = cursize;
10809}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010810
Linus Torvalds1da177e2005-04-16 15:20:36 -070010811static void __devinit tg3_get_nvram_size(struct tg3 *tp)
10812{
10813 u32 val;
10814
Matt Carlsondf259d82009-04-20 06:57:14 +000010815 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
10816 tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080010817 return;
10818
10819 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080010820 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080010821 tg3_get_eeprom_size(tp);
10822 return;
10823 }
10824
Matt Carlson6d348f22009-02-25 14:25:52 +000010825 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010826 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000010827 /* This is confusing. We want to operate on the
10828 * 16-bit value at offset 0xf2. The tg3_nvram_read()
10829 * call will read from NVRAM and byteswap the data
10830 * according to the byteswapping settings for all
10831 * other register accesses. This ensures the data we
10832 * want will always reside in the lower 16-bits.
10833 * However, the data in NVRAM is in LE format, which
10834 * means the data from the NVRAM read will always be
10835 * opposite the endianness of the CPU. The 16-bit
10836 * byteswap then brings the data to CPU endianness.
10837 */
10838 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010839 return;
10840 }
10841 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070010842 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010843}
10844
10845static void __devinit tg3_get_nvram_info(struct tg3 *tp)
10846{
10847 u32 nvcfg1;
10848
10849 nvcfg1 = tr32(NVRAM_CFG1);
10850 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
10851 tp->tg3_flags2 |= TG3_FLG2_FLASH;
Matt Carlson8590a602009-08-28 12:29:16 +000010852 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010853 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
10854 tw32(NVRAM_CFG1, nvcfg1);
10855 }
10856
Michael Chan4c987482005-09-05 17:52:38 -070010857 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750) ||
Michael Chana4e2b342005-10-26 15:46:52 -070010858 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010859 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000010860 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
10861 tp->nvram_jedecnum = JEDEC_ATMEL;
10862 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
10863 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10864 break;
10865 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
10866 tp->nvram_jedecnum = JEDEC_ATMEL;
10867 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
10868 break;
10869 case FLASH_VENDOR_ATMEL_EEPROM:
10870 tp->nvram_jedecnum = JEDEC_ATMEL;
10871 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
10872 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10873 break;
10874 case FLASH_VENDOR_ST:
10875 tp->nvram_jedecnum = JEDEC_ST;
10876 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
10877 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10878 break;
10879 case FLASH_VENDOR_SAIFUN:
10880 tp->nvram_jedecnum = JEDEC_SAIFUN;
10881 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
10882 break;
10883 case FLASH_VENDOR_SST_SMALL:
10884 case FLASH_VENDOR_SST_LARGE:
10885 tp->nvram_jedecnum = JEDEC_SST;
10886 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
10887 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010888 }
Matt Carlson8590a602009-08-28 12:29:16 +000010889 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010890 tp->nvram_jedecnum = JEDEC_ATMEL;
10891 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
10892 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10893 }
10894}
10895
Matt Carlsona1b950d2009-09-01 13:20:17 +000010896static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
10897{
10898 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
10899 case FLASH_5752PAGE_SIZE_256:
10900 tp->nvram_pagesize = 256;
10901 break;
10902 case FLASH_5752PAGE_SIZE_512:
10903 tp->nvram_pagesize = 512;
10904 break;
10905 case FLASH_5752PAGE_SIZE_1K:
10906 tp->nvram_pagesize = 1024;
10907 break;
10908 case FLASH_5752PAGE_SIZE_2K:
10909 tp->nvram_pagesize = 2048;
10910 break;
10911 case FLASH_5752PAGE_SIZE_4K:
10912 tp->nvram_pagesize = 4096;
10913 break;
10914 case FLASH_5752PAGE_SIZE_264:
10915 tp->nvram_pagesize = 264;
10916 break;
10917 case FLASH_5752PAGE_SIZE_528:
10918 tp->nvram_pagesize = 528;
10919 break;
10920 }
10921}
10922
Michael Chan361b4ac2005-04-21 17:11:21 -070010923static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
10924{
10925 u32 nvcfg1;
10926
10927 nvcfg1 = tr32(NVRAM_CFG1);
10928
Michael Chane6af3012005-04-21 17:12:05 -070010929 /* NVRAM protection for TPM */
10930 if (nvcfg1 & (1 << 27))
10931 tp->tg3_flags2 |= TG3_FLG2_PROTECTED_NVRAM;
10932
Michael Chan361b4ac2005-04-21 17:11:21 -070010933 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000010934 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
10935 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
10936 tp->nvram_jedecnum = JEDEC_ATMEL;
10937 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10938 break;
10939 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
10940 tp->nvram_jedecnum = JEDEC_ATMEL;
10941 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10942 tp->tg3_flags2 |= TG3_FLG2_FLASH;
10943 break;
10944 case FLASH_5752VENDOR_ST_M45PE10:
10945 case FLASH_5752VENDOR_ST_M45PE20:
10946 case FLASH_5752VENDOR_ST_M45PE40:
10947 tp->nvram_jedecnum = JEDEC_ST;
10948 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10949 tp->tg3_flags2 |= TG3_FLG2_FLASH;
10950 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070010951 }
10952
10953 if (tp->tg3_flags2 & TG3_FLG2_FLASH) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000010954 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000010955 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070010956 /* For eeprom, set pagesize to maximum eeprom size */
10957 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
10958
10959 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
10960 tw32(NVRAM_CFG1, nvcfg1);
10961 }
10962}
10963
Michael Chand3c7b882006-03-23 01:28:25 -080010964static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
10965{
Matt Carlson989a9d22007-05-05 11:51:05 -070010966 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080010967
10968 nvcfg1 = tr32(NVRAM_CFG1);
10969
10970 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070010971 if (nvcfg1 & (1 << 27)) {
Michael Chand3c7b882006-03-23 01:28:25 -080010972 tp->tg3_flags2 |= TG3_FLG2_PROTECTED_NVRAM;
Matt Carlson989a9d22007-05-05 11:51:05 -070010973 protect = 1;
10974 }
Michael Chand3c7b882006-03-23 01:28:25 -080010975
Matt Carlson989a9d22007-05-05 11:51:05 -070010976 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
10977 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000010978 case FLASH_5755VENDOR_ATMEL_FLASH_1:
10979 case FLASH_5755VENDOR_ATMEL_FLASH_2:
10980 case FLASH_5755VENDOR_ATMEL_FLASH_3:
10981 case FLASH_5755VENDOR_ATMEL_FLASH_5:
10982 tp->nvram_jedecnum = JEDEC_ATMEL;
10983 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10984 tp->tg3_flags2 |= TG3_FLG2_FLASH;
10985 tp->nvram_pagesize = 264;
10986 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
10987 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
10988 tp->nvram_size = (protect ? 0x3e200 :
10989 TG3_NVRAM_SIZE_512KB);
10990 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
10991 tp->nvram_size = (protect ? 0x1f200 :
10992 TG3_NVRAM_SIZE_256KB);
10993 else
10994 tp->nvram_size = (protect ? 0x1f200 :
10995 TG3_NVRAM_SIZE_128KB);
10996 break;
10997 case FLASH_5752VENDOR_ST_M45PE10:
10998 case FLASH_5752VENDOR_ST_M45PE20:
10999 case FLASH_5752VENDOR_ST_M45PE40:
11000 tp->nvram_jedecnum = JEDEC_ST;
11001 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11002 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11003 tp->nvram_pagesize = 256;
11004 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
11005 tp->nvram_size = (protect ?
11006 TG3_NVRAM_SIZE_64KB :
11007 TG3_NVRAM_SIZE_128KB);
11008 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
11009 tp->nvram_size = (protect ?
11010 TG3_NVRAM_SIZE_64KB :
11011 TG3_NVRAM_SIZE_256KB);
11012 else
11013 tp->nvram_size = (protect ?
11014 TG3_NVRAM_SIZE_128KB :
11015 TG3_NVRAM_SIZE_512KB);
11016 break;
Michael Chand3c7b882006-03-23 01:28:25 -080011017 }
11018}
11019
Michael Chan1b277772006-03-20 22:27:48 -080011020static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
11021{
11022 u32 nvcfg1;
11023
11024 nvcfg1 = tr32(NVRAM_CFG1);
11025
11026 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011027 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
11028 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11029 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
11030 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11031 tp->nvram_jedecnum = JEDEC_ATMEL;
11032 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11033 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080011034
Matt Carlson8590a602009-08-28 12:29:16 +000011035 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11036 tw32(NVRAM_CFG1, nvcfg1);
11037 break;
11038 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11039 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11040 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11041 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11042 tp->nvram_jedecnum = JEDEC_ATMEL;
11043 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11044 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11045 tp->nvram_pagesize = 264;
11046 break;
11047 case FLASH_5752VENDOR_ST_M45PE10:
11048 case FLASH_5752VENDOR_ST_M45PE20:
11049 case FLASH_5752VENDOR_ST_M45PE40:
11050 tp->nvram_jedecnum = JEDEC_ST;
11051 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11052 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11053 tp->nvram_pagesize = 256;
11054 break;
Michael Chan1b277772006-03-20 22:27:48 -080011055 }
11056}
11057
Matt Carlson6b91fa02007-10-10 18:01:09 -070011058static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
11059{
11060 u32 nvcfg1, protect = 0;
11061
11062 nvcfg1 = tr32(NVRAM_CFG1);
11063
11064 /* NVRAM protection for TPM */
11065 if (nvcfg1 & (1 << 27)) {
11066 tp->tg3_flags2 |= TG3_FLG2_PROTECTED_NVRAM;
11067 protect = 1;
11068 }
11069
11070 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11071 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011072 case FLASH_5761VENDOR_ATMEL_ADB021D:
11073 case FLASH_5761VENDOR_ATMEL_ADB041D:
11074 case FLASH_5761VENDOR_ATMEL_ADB081D:
11075 case FLASH_5761VENDOR_ATMEL_ADB161D:
11076 case FLASH_5761VENDOR_ATMEL_MDB021D:
11077 case FLASH_5761VENDOR_ATMEL_MDB041D:
11078 case FLASH_5761VENDOR_ATMEL_MDB081D:
11079 case FLASH_5761VENDOR_ATMEL_MDB161D:
11080 tp->nvram_jedecnum = JEDEC_ATMEL;
11081 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11082 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11083 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
11084 tp->nvram_pagesize = 256;
11085 break;
11086 case FLASH_5761VENDOR_ST_A_M45PE20:
11087 case FLASH_5761VENDOR_ST_A_M45PE40:
11088 case FLASH_5761VENDOR_ST_A_M45PE80:
11089 case FLASH_5761VENDOR_ST_A_M45PE16:
11090 case FLASH_5761VENDOR_ST_M_M45PE20:
11091 case FLASH_5761VENDOR_ST_M_M45PE40:
11092 case FLASH_5761VENDOR_ST_M_M45PE80:
11093 case FLASH_5761VENDOR_ST_M_M45PE16:
11094 tp->nvram_jedecnum = JEDEC_ST;
11095 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11096 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11097 tp->nvram_pagesize = 256;
11098 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011099 }
11100
11101 if (protect) {
11102 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
11103 } else {
11104 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011105 case FLASH_5761VENDOR_ATMEL_ADB161D:
11106 case FLASH_5761VENDOR_ATMEL_MDB161D:
11107 case FLASH_5761VENDOR_ST_A_M45PE16:
11108 case FLASH_5761VENDOR_ST_M_M45PE16:
11109 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
11110 break;
11111 case FLASH_5761VENDOR_ATMEL_ADB081D:
11112 case FLASH_5761VENDOR_ATMEL_MDB081D:
11113 case FLASH_5761VENDOR_ST_A_M45PE80:
11114 case FLASH_5761VENDOR_ST_M_M45PE80:
11115 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
11116 break;
11117 case FLASH_5761VENDOR_ATMEL_ADB041D:
11118 case FLASH_5761VENDOR_ATMEL_MDB041D:
11119 case FLASH_5761VENDOR_ST_A_M45PE40:
11120 case FLASH_5761VENDOR_ST_M_M45PE40:
11121 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11122 break;
11123 case FLASH_5761VENDOR_ATMEL_ADB021D:
11124 case FLASH_5761VENDOR_ATMEL_MDB021D:
11125 case FLASH_5761VENDOR_ST_A_M45PE20:
11126 case FLASH_5761VENDOR_ST_M_M45PE20:
11127 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11128 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011129 }
11130 }
11131}
11132
Michael Chanb5d37722006-09-27 16:06:21 -070011133static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
11134{
11135 tp->nvram_jedecnum = JEDEC_ATMEL;
11136 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11137 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11138}
11139
Matt Carlson321d32a2008-11-21 17:22:19 -080011140static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
11141{
11142 u32 nvcfg1;
11143
11144 nvcfg1 = tr32(NVRAM_CFG1);
11145
11146 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11147 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11148 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11149 tp->nvram_jedecnum = JEDEC_ATMEL;
11150 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11151 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11152
11153 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11154 tw32(NVRAM_CFG1, nvcfg1);
11155 return;
11156 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11157 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11158 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11159 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11160 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11161 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11162 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
11163 tp->nvram_jedecnum = JEDEC_ATMEL;
11164 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11165 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11166
11167 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11168 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11169 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11170 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11171 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11172 break;
11173 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11174 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11175 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11176 break;
11177 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11178 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
11179 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11180 break;
11181 }
11182 break;
11183 case FLASH_5752VENDOR_ST_M45PE10:
11184 case FLASH_5752VENDOR_ST_M45PE20:
11185 case FLASH_5752VENDOR_ST_M45PE40:
11186 tp->nvram_jedecnum = JEDEC_ST;
11187 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11188 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11189
11190 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11191 case FLASH_5752VENDOR_ST_M45PE10:
11192 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11193 break;
11194 case FLASH_5752VENDOR_ST_M45PE20:
11195 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11196 break;
11197 case FLASH_5752VENDOR_ST_M45PE40:
11198 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11199 break;
11200 }
11201 break;
11202 default:
Matt Carlsondf259d82009-04-20 06:57:14 +000011203 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
Matt Carlson321d32a2008-11-21 17:22:19 -080011204 return;
11205 }
11206
Matt Carlsona1b950d2009-09-01 13:20:17 +000011207 tg3_nvram_get_pagesize(tp, nvcfg1);
11208 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Matt Carlson321d32a2008-11-21 17:22:19 -080011209 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011210}
11211
11212
11213static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
11214{
11215 u32 nvcfg1;
11216
11217 nvcfg1 = tr32(NVRAM_CFG1);
11218
11219 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11220 case FLASH_5717VENDOR_ATMEL_EEPROM:
11221 case FLASH_5717VENDOR_MICRO_EEPROM:
11222 tp->nvram_jedecnum = JEDEC_ATMEL;
11223 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11224 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11225
11226 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11227 tw32(NVRAM_CFG1, nvcfg1);
11228 return;
11229 case FLASH_5717VENDOR_ATMEL_MDB011D:
11230 case FLASH_5717VENDOR_ATMEL_ADB011B:
11231 case FLASH_5717VENDOR_ATMEL_ADB011D:
11232 case FLASH_5717VENDOR_ATMEL_MDB021D:
11233 case FLASH_5717VENDOR_ATMEL_ADB021B:
11234 case FLASH_5717VENDOR_ATMEL_ADB021D:
11235 case FLASH_5717VENDOR_ATMEL_45USPT:
11236 tp->nvram_jedecnum = JEDEC_ATMEL;
11237 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11238 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11239
11240 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11241 case FLASH_5717VENDOR_ATMEL_MDB021D:
11242 case FLASH_5717VENDOR_ATMEL_ADB021B:
11243 case FLASH_5717VENDOR_ATMEL_ADB021D:
11244 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11245 break;
11246 default:
11247 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11248 break;
11249 }
Matt Carlson321d32a2008-11-21 17:22:19 -080011250 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011251 case FLASH_5717VENDOR_ST_M_M25PE10:
11252 case FLASH_5717VENDOR_ST_A_M25PE10:
11253 case FLASH_5717VENDOR_ST_M_M45PE10:
11254 case FLASH_5717VENDOR_ST_A_M45PE10:
11255 case FLASH_5717VENDOR_ST_M_M25PE20:
11256 case FLASH_5717VENDOR_ST_A_M25PE20:
11257 case FLASH_5717VENDOR_ST_M_M45PE20:
11258 case FLASH_5717VENDOR_ST_A_M45PE20:
11259 case FLASH_5717VENDOR_ST_25USPT:
11260 case FLASH_5717VENDOR_ST_45USPT:
11261 tp->nvram_jedecnum = JEDEC_ST;
11262 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11263 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11264
11265 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11266 case FLASH_5717VENDOR_ST_M_M25PE20:
11267 case FLASH_5717VENDOR_ST_A_M25PE20:
11268 case FLASH_5717VENDOR_ST_M_M45PE20:
11269 case FLASH_5717VENDOR_ST_A_M45PE20:
11270 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11271 break;
11272 default:
11273 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11274 break;
11275 }
Matt Carlson321d32a2008-11-21 17:22:19 -080011276 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011277 default:
11278 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
11279 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080011280 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000011281
11282 tg3_nvram_get_pagesize(tp, nvcfg1);
11283 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
11284 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
Matt Carlson321d32a2008-11-21 17:22:19 -080011285}
11286
Linus Torvalds1da177e2005-04-16 15:20:36 -070011287/* Chips other than 5700/5701 use the NVRAM for fetching info. */
11288static void __devinit tg3_nvram_init(struct tg3 *tp)
11289{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011290 tw32_f(GRC_EEPROM_ADDR,
11291 (EEPROM_ADDR_FSM_RESET |
11292 (EEPROM_DEFAULT_CLOCK_PERIOD <<
11293 EEPROM_ADDR_CLKPERD_SHIFT)));
11294
Michael Chan9d57f012006-12-07 00:23:25 -080011295 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011296
11297 /* Enable seeprom accesses. */
11298 tw32_f(GRC_LOCAL_CTRL,
11299 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
11300 udelay(100);
11301
11302 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
11303 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
11304 tp->tg3_flags |= TG3_FLAG_NVRAM;
11305
Michael Chanec41c7d2006-01-17 02:40:55 -080011306 if (tg3_nvram_lock(tp)) {
11307 printk(KERN_WARNING PFX "%s: Cannot get nvarm lock, "
11308 "tg3_nvram_init failed.\n", tp->dev->name);
11309 return;
11310 }
Michael Chane6af3012005-04-21 17:12:05 -070011311 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011312
Matt Carlson989a9d22007-05-05 11:51:05 -070011313 tp->nvram_size = 0;
11314
Michael Chan361b4ac2005-04-21 17:11:21 -070011315 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
11316 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080011317 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
11318 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070011319 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070011320 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
11321 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080011322 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070011323 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
11324 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070011325 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
11326 tg3_get_5906_nvram_info(tp);
Matt Carlson321d32a2008-11-21 17:22:19 -080011327 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
11328 tg3_get_57780_nvram_info(tp);
Matt Carlsona1b950d2009-09-01 13:20:17 +000011329 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
11330 tg3_get_5717_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070011331 else
11332 tg3_get_nvram_info(tp);
11333
Matt Carlson989a9d22007-05-05 11:51:05 -070011334 if (tp->nvram_size == 0)
11335 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011336
Michael Chane6af3012005-04-21 17:12:05 -070011337 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080011338 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011339
11340 } else {
11341 tp->tg3_flags &= ~(TG3_FLAG_NVRAM | TG3_FLAG_NVRAM_BUFFERED);
11342
11343 tg3_get_eeprom_size(tp);
11344 }
11345}
11346
Linus Torvalds1da177e2005-04-16 15:20:36 -070011347static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
11348 u32 offset, u32 len, u8 *buf)
11349{
11350 int i, j, rc = 0;
11351 u32 val;
11352
11353 for (i = 0; i < len; i += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080011354 u32 addr;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011355 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011356
11357 addr = offset + i;
11358
11359 memcpy(&data, buf + i, 4);
11360
Matt Carlson62cedd12009-04-20 14:52:29 -070011361 /*
11362 * The SEEPROM interface expects the data to always be opposite
11363 * the native endian format. We accomplish this by reversing
11364 * all the operations that would have been performed on the
11365 * data from a call to tg3_nvram_read_be32().
11366 */
11367 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011368
11369 val = tr32(GRC_EEPROM_ADDR);
11370 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
11371
11372 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
11373 EEPROM_ADDR_READ);
11374 tw32(GRC_EEPROM_ADDR, val |
11375 (0 << EEPROM_ADDR_DEVID_SHIFT) |
11376 (addr & EEPROM_ADDR_ADDR_MASK) |
11377 EEPROM_ADDR_START |
11378 EEPROM_ADDR_WRITE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011379
Michael Chan9d57f012006-12-07 00:23:25 -080011380 for (j = 0; j < 1000; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011381 val = tr32(GRC_EEPROM_ADDR);
11382
11383 if (val & EEPROM_ADDR_COMPLETE)
11384 break;
Michael Chan9d57f012006-12-07 00:23:25 -080011385 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011386 }
11387 if (!(val & EEPROM_ADDR_COMPLETE)) {
11388 rc = -EBUSY;
11389 break;
11390 }
11391 }
11392
11393 return rc;
11394}
11395
11396/* offset and length are dword aligned */
11397static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
11398 u8 *buf)
11399{
11400 int ret = 0;
11401 u32 pagesize = tp->nvram_pagesize;
11402 u32 pagemask = pagesize - 1;
11403 u32 nvram_cmd;
11404 u8 *tmp;
11405
11406 tmp = kmalloc(pagesize, GFP_KERNEL);
11407 if (tmp == NULL)
11408 return -ENOMEM;
11409
11410 while (len) {
11411 int j;
Michael Chane6af3012005-04-21 17:12:05 -070011412 u32 phy_addr, page_off, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011413
11414 phy_addr = offset & ~pagemask;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011415
Linus Torvalds1da177e2005-04-16 15:20:36 -070011416 for (j = 0; j < pagesize; j += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000011417 ret = tg3_nvram_read_be32(tp, phy_addr + j,
11418 (__be32 *) (tmp + j));
11419 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011420 break;
11421 }
11422 if (ret)
11423 break;
11424
11425 page_off = offset & pagemask;
11426 size = pagesize;
11427 if (len < size)
11428 size = len;
11429
11430 len -= size;
11431
11432 memcpy(tmp + page_off, buf, size);
11433
11434 offset = offset + (pagesize - page_off);
11435
Michael Chane6af3012005-04-21 17:12:05 -070011436 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011437
11438 /*
11439 * Before we can erase the flash page, we need
11440 * to issue a special "write enable" command.
11441 */
11442 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
11443
11444 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
11445 break;
11446
11447 /* Erase the target page */
11448 tw32(NVRAM_ADDR, phy_addr);
11449
11450 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
11451 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
11452
11453 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
11454 break;
11455
11456 /* Issue another write enable to start the write. */
11457 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
11458
11459 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
11460 break;
11461
11462 for (j = 0; j < pagesize; j += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080011463 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011464
Al Virob9fc7dc2007-12-17 22:59:57 -080011465 data = *((__be32 *) (tmp + j));
Matt Carlsona9dc5292009-02-25 14:25:30 +000011466
Al Virob9fc7dc2007-12-17 22:59:57 -080011467 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011468
11469 tw32(NVRAM_ADDR, phy_addr + j);
11470
11471 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
11472 NVRAM_CMD_WR;
11473
11474 if (j == 0)
11475 nvram_cmd |= NVRAM_CMD_FIRST;
11476 else if (j == (pagesize - 4))
11477 nvram_cmd |= NVRAM_CMD_LAST;
11478
11479 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
11480 break;
11481 }
11482 if (ret)
11483 break;
11484 }
11485
11486 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
11487 tg3_nvram_exec_cmd(tp, nvram_cmd);
11488
11489 kfree(tmp);
11490
11491 return ret;
11492}
11493
11494/* offset and length are dword aligned */
11495static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
11496 u8 *buf)
11497{
11498 int i, ret = 0;
11499
11500 for (i = 0; i < len; i += 4, offset += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080011501 u32 page_off, phy_addr, nvram_cmd;
11502 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011503
11504 memcpy(&data, buf + i, 4);
Al Virob9fc7dc2007-12-17 22:59:57 -080011505 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011506
11507 page_off = offset % tp->nvram_pagesize;
11508
Michael Chan18201802006-03-20 22:29:15 -080011509 phy_addr = tg3_nvram_phys_addr(tp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011510
11511 tw32(NVRAM_ADDR, phy_addr);
11512
11513 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
11514
11515 if ((page_off == 0) || (i == 0))
11516 nvram_cmd |= NVRAM_CMD_FIRST;
Michael Chanf6d9a252006-04-29 19:00:24 -070011517 if (page_off == (tp->nvram_pagesize - 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011518 nvram_cmd |= NVRAM_CMD_LAST;
11519
11520 if (i == (len - 4))
11521 nvram_cmd |= NVRAM_CMD_LAST;
11522
Matt Carlson321d32a2008-11-21 17:22:19 -080011523 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
11524 !(tp->tg3_flags3 & TG3_FLG3_5755_PLUS) &&
Michael Chan4c987482005-09-05 17:52:38 -070011525 (tp->nvram_jedecnum == JEDEC_ST) &&
11526 (nvram_cmd & NVRAM_CMD_FIRST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011527
11528 if ((ret = tg3_nvram_exec_cmd(tp,
11529 NVRAM_CMD_WREN | NVRAM_CMD_GO |
11530 NVRAM_CMD_DONE)))
11531
11532 break;
11533 }
11534 if (!(tp->tg3_flags2 & TG3_FLG2_FLASH)) {
11535 /* We always do complete word writes to eeprom. */
11536 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
11537 }
11538
11539 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
11540 break;
11541 }
11542 return ret;
11543}
11544
11545/* offset and length are dword aligned */
11546static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
11547{
11548 int ret;
11549
Linus Torvalds1da177e2005-04-16 15:20:36 -070011550 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT) {
Michael Chan314fba32005-04-21 17:07:04 -070011551 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
11552 ~GRC_LCLCTRL_GPIO_OUTPUT1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011553 udelay(40);
11554 }
11555
11556 if (!(tp->tg3_flags & TG3_FLAG_NVRAM)) {
11557 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
11558 }
11559 else {
11560 u32 grc_mode;
11561
Michael Chanec41c7d2006-01-17 02:40:55 -080011562 ret = tg3_nvram_lock(tp);
11563 if (ret)
11564 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011565
Michael Chane6af3012005-04-21 17:12:05 -070011566 tg3_enable_nvram_access(tp);
11567 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
11568 !(tp->tg3_flags2 & TG3_FLG2_PROTECTED_NVRAM))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011569 tw32(NVRAM_WRITE1, 0x406);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011570
11571 grc_mode = tr32(GRC_MODE);
11572 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
11573
11574 if ((tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) ||
11575 !(tp->tg3_flags2 & TG3_FLG2_FLASH)) {
11576
11577 ret = tg3_nvram_write_block_buffered(tp, offset, len,
11578 buf);
11579 }
11580 else {
11581 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
11582 buf);
11583 }
11584
11585 grc_mode = tr32(GRC_MODE);
11586 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
11587
Michael Chane6af3012005-04-21 17:12:05 -070011588 tg3_disable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011589 tg3_nvram_unlock(tp);
11590 }
11591
11592 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT) {
Michael Chan314fba32005-04-21 17:07:04 -070011593 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011594 udelay(40);
11595 }
11596
11597 return ret;
11598}
11599
11600struct subsys_tbl_ent {
11601 u16 subsys_vendor, subsys_devid;
11602 u32 phy_id;
11603};
11604
11605static struct subsys_tbl_ent subsys_id_to_phy_id[] = {
11606 /* Broadcom boards. */
11607 { PCI_VENDOR_ID_BROADCOM, 0x1644, PHY_ID_BCM5401 }, /* BCM95700A6 */
11608 { PCI_VENDOR_ID_BROADCOM, 0x0001, PHY_ID_BCM5701 }, /* BCM95701A5 */
11609 { PCI_VENDOR_ID_BROADCOM, 0x0002, PHY_ID_BCM8002 }, /* BCM95700T6 */
11610 { PCI_VENDOR_ID_BROADCOM, 0x0003, 0 }, /* BCM95700A9 */
11611 { PCI_VENDOR_ID_BROADCOM, 0x0005, PHY_ID_BCM5701 }, /* BCM95701T1 */
11612 { PCI_VENDOR_ID_BROADCOM, 0x0006, PHY_ID_BCM5701 }, /* BCM95701T8 */
11613 { PCI_VENDOR_ID_BROADCOM, 0x0007, 0 }, /* BCM95701A7 */
11614 { PCI_VENDOR_ID_BROADCOM, 0x0008, PHY_ID_BCM5701 }, /* BCM95701A10 */
11615 { PCI_VENDOR_ID_BROADCOM, 0x8008, PHY_ID_BCM5701 }, /* BCM95701A12 */
11616 { PCI_VENDOR_ID_BROADCOM, 0x0009, PHY_ID_BCM5703 }, /* BCM95703Ax1 */
11617 { PCI_VENDOR_ID_BROADCOM, 0x8009, PHY_ID_BCM5703 }, /* BCM95703Ax2 */
11618
11619 /* 3com boards. */
11620 { PCI_VENDOR_ID_3COM, 0x1000, PHY_ID_BCM5401 }, /* 3C996T */
11621 { PCI_VENDOR_ID_3COM, 0x1006, PHY_ID_BCM5701 }, /* 3C996BT */
11622 { PCI_VENDOR_ID_3COM, 0x1004, 0 }, /* 3C996SX */
11623 { PCI_VENDOR_ID_3COM, 0x1007, PHY_ID_BCM5701 }, /* 3C1000T */
11624 { PCI_VENDOR_ID_3COM, 0x1008, PHY_ID_BCM5701 }, /* 3C940BR01 */
11625
11626 /* DELL boards. */
11627 { PCI_VENDOR_ID_DELL, 0x00d1, PHY_ID_BCM5401 }, /* VIPER */
11628 { PCI_VENDOR_ID_DELL, 0x0106, PHY_ID_BCM5401 }, /* JAGUAR */
11629 { PCI_VENDOR_ID_DELL, 0x0109, PHY_ID_BCM5411 }, /* MERLOT */
11630 { PCI_VENDOR_ID_DELL, 0x010a, PHY_ID_BCM5411 }, /* SLIM_MERLOT */
11631
11632 /* Compaq boards. */
11633 { PCI_VENDOR_ID_COMPAQ, 0x007c, PHY_ID_BCM5701 }, /* BANSHEE */
11634 { PCI_VENDOR_ID_COMPAQ, 0x009a, PHY_ID_BCM5701 }, /* BANSHEE_2 */
11635 { PCI_VENDOR_ID_COMPAQ, 0x007d, 0 }, /* CHANGELING */
11636 { PCI_VENDOR_ID_COMPAQ, 0x0085, PHY_ID_BCM5701 }, /* NC7780 */
11637 { PCI_VENDOR_ID_COMPAQ, 0x0099, PHY_ID_BCM5701 }, /* NC7780_2 */
11638
11639 /* IBM boards. */
11640 { PCI_VENDOR_ID_IBM, 0x0281, 0 } /* IBM??? */
11641};
11642
11643static inline struct subsys_tbl_ent *lookup_by_subsys(struct tg3 *tp)
11644{
11645 int i;
11646
11647 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
11648 if ((subsys_id_to_phy_id[i].subsys_vendor ==
11649 tp->pdev->subsystem_vendor) &&
11650 (subsys_id_to_phy_id[i].subsys_devid ==
11651 tp->pdev->subsystem_device))
11652 return &subsys_id_to_phy_id[i];
11653 }
11654 return NULL;
11655}
11656
Michael Chan7d0c41e2005-04-21 17:06:20 -070011657static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011658{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011659 u32 val;
Michael Chancaf636c72006-03-22 01:05:31 -080011660 u16 pmcsr;
11661
11662 /* On some early chips the SRAM cannot be accessed in D3hot state,
11663 * so need make sure we're in D0.
11664 */
11665 pci_read_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, &pmcsr);
11666 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
11667 pci_write_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, pmcsr);
11668 msleep(1);
Michael Chan7d0c41e2005-04-21 17:06:20 -070011669
11670 /* Make sure register accesses (indirect or otherwise)
11671 * will function correctly.
11672 */
11673 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
11674 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011675
David S. Millerf49639e2006-06-09 11:58:36 -070011676 /* The memory arbiter has to be enabled in order for SRAM accesses
11677 * to succeed. Normally on powerup the tg3 chip firmware will make
11678 * sure it is enabled, but other entities such as system netboot
11679 * code might disable it.
11680 */
11681 val = tr32(MEMARB_MODE);
11682 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
11683
Linus Torvalds1da177e2005-04-16 15:20:36 -070011684 tp->phy_id = PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070011685 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
11686
Gary Zambranoa85feb82007-05-05 11:52:19 -070011687 /* Assume an onboard device and WOL capable by default. */
11688 tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT | TG3_FLAG_WOL_CAP;
David S. Miller72b845e2006-03-14 14:11:48 -080011689
Michael Chanb5d37722006-09-27 16:06:21 -070011690 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080011691 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Michael Chanb5d37722006-09-27 16:06:21 -070011692 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080011693 tp->tg3_flags2 |= TG3_FLG2_IS_NIC;
11694 }
Matt Carlson0527ba32007-10-10 18:03:30 -070011695 val = tr32(VCPU_CFGSHDW);
11696 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Matt Carlson8ed5d972007-05-07 00:25:49 -070011697 tp->tg3_flags |= TG3_FLAG_ASPM_WORKAROUND;
Matt Carlson0527ba32007-10-10 18:03:30 -070011698 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Matt Carlson20232762008-12-21 20:18:56 -080011699 (val & VCPU_CFGSHDW_WOL_MAGPKT))
Matt Carlson0527ba32007-10-10 18:03:30 -070011700 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
Matt Carlson05ac4cb2008-11-03 16:53:46 -080011701 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070011702 }
11703
Linus Torvalds1da177e2005-04-16 15:20:36 -070011704 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
11705 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
11706 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070011707 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070011708 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011709
11710 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
11711 tp->nic_sram_data_cfg = nic_cfg;
11712
11713 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
11714 ver >>= NIC_SRAM_DATA_VER_SHIFT;
11715 if ((GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700) &&
11716 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) &&
11717 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703) &&
11718 (ver > 0) && (ver < 0x100))
11719 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
11720
Matt Carlsona9daf362008-05-25 23:49:44 -070011721 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
11722 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
11723
Linus Torvalds1da177e2005-04-16 15:20:36 -070011724 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
11725 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
11726 eeprom_phy_serdes = 1;
11727
11728 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
11729 if (nic_phy_id != 0) {
11730 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
11731 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
11732
11733 eeprom_phy_id = (id1 >> 16) << 10;
11734 eeprom_phy_id |= (id2 & 0xfc00) << 16;
11735 eeprom_phy_id |= (id2 & 0x03ff) << 0;
11736 } else
11737 eeprom_phy_id = 0;
11738
Michael Chan7d0c41e2005-04-21 17:06:20 -070011739 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070011740 if (eeprom_phy_serdes) {
Michael Chana4e2b342005-10-26 15:46:52 -070011741 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
Michael Chan747e8f82005-07-25 12:33:22 -070011742 tp->tg3_flags2 |= TG3_FLG2_MII_SERDES;
11743 else
11744 tp->tg3_flags2 |= TG3_FLG2_PHY_SERDES;
11745 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070011746
John W. Linvillecbf46852005-04-21 17:01:29 -070011747 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011748 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
11749 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070011750 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070011751 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
11752
11753 switch (led_cfg) {
11754 default:
11755 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
11756 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
11757 break;
11758
11759 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
11760 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
11761 break;
11762
11763 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
11764 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070011765
11766 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
11767 * read on some older 5700/5701 bootcode.
11768 */
11769 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
11770 ASIC_REV_5700 ||
11771 GET_ASIC_REV(tp->pci_chip_rev_id) ==
11772 ASIC_REV_5701)
11773 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
11774
Linus Torvalds1da177e2005-04-16 15:20:36 -070011775 break;
11776
11777 case SHASTA_EXT_LED_SHARED:
11778 tp->led_ctrl = LED_CTRL_MODE_SHARED;
11779 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
11780 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
11781 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
11782 LED_CTRL_MODE_PHY_2);
11783 break;
11784
11785 case SHASTA_EXT_LED_MAC:
11786 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
11787 break;
11788
11789 case SHASTA_EXT_LED_COMBO:
11790 tp->led_ctrl = LED_CTRL_MODE_COMBO;
11791 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
11792 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
11793 LED_CTRL_MODE_PHY_2);
11794 break;
11795
Stephen Hemminger855e1112008-04-16 16:37:28 -070011796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011797
11798 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
11799 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
11800 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
11801 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
11802
Matt Carlsonb2a5c192008-04-03 21:44:44 -070011803 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
11804 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080011805
Michael Chan9d26e212006-12-07 00:21:14 -080011806 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011807 tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080011808 if ((tp->pdev->subsystem_vendor ==
11809 PCI_VENDOR_ID_ARIMA) &&
11810 (tp->pdev->subsystem_device == 0x205a ||
11811 tp->pdev->subsystem_device == 0x2063))
11812 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
11813 } else {
David S. Millerf49639e2006-06-09 11:58:36 -070011814 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080011815 tp->tg3_flags2 |= TG3_FLG2_IS_NIC;
11816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011817
11818 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
11819 tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
John W. Linvillecbf46852005-04-21 17:01:29 -070011820 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011821 tp->tg3_flags2 |= TG3_FLG2_ASF_NEW_HANDSHAKE;
11822 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080011823
11824 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
11825 (tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
Matt Carlson0d3031d2007-10-10 18:02:43 -070011826 tp->tg3_flags3 |= TG3_FLG3_ENABLE_APE;
Matt Carlsonb2b98d42008-11-03 16:52:32 -080011827
Gary Zambranoa85feb82007-05-05 11:52:19 -070011828 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES &&
11829 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
11830 tp->tg3_flags &= ~TG3_FLAG_WOL_CAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011831
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070011832 if ((tp->tg3_flags & TG3_FLAG_WOL_CAP) &&
Matt Carlson05ac4cb2008-11-03 16:53:46 -080011833 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE))
Matt Carlson0527ba32007-10-10 18:03:30 -070011834 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
11835
Linus Torvalds1da177e2005-04-16 15:20:36 -070011836 if (cfg2 & (1 << 17))
11837 tp->tg3_flags2 |= TG3_FLG2_CAPACITIVE_COUPLING;
11838
11839 /* serdes signal pre-emphasis in register 0x590 set by */
11840 /* bootcode if bit 18 is set */
11841 if (cfg2 & (1 << 18))
11842 tp->tg3_flags2 |= TG3_FLG2_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070011843
Matt Carlson321d32a2008-11-21 17:22:19 -080011844 if (((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
11845 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080011846 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
11847 tp->tg3_flags3 |= TG3_FLG3_PHY_ENABLE_APD;
11848
Matt Carlson8ed5d972007-05-07 00:25:49 -070011849 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
11850 u32 cfg3;
11851
11852 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
11853 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
11854 tp->tg3_flags |= TG3_FLAG_ASPM_WORKAROUND;
11855 }
Matt Carlsona9daf362008-05-25 23:49:44 -070011856
11857 if (cfg4 & NIC_SRAM_RGMII_STD_IBND_DISABLE)
11858 tp->tg3_flags3 |= TG3_FLG3_RGMII_STD_IBND_DISABLE;
11859 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
11860 tp->tg3_flags3 |= TG3_FLG3_RGMII_EXT_IBND_RX_EN;
11861 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
11862 tp->tg3_flags3 |= TG3_FLG3_RGMII_EXT_IBND_TX_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011863 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080011864done:
11865 device_init_wakeup(&tp->pdev->dev, tp->tg3_flags & TG3_FLAG_WOL_CAP);
11866 device_set_wakeup_enable(&tp->pdev->dev,
11867 tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
Michael Chan7d0c41e2005-04-21 17:06:20 -070011868}
11869
Matt Carlsonb2a5c192008-04-03 21:44:44 -070011870static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
11871{
11872 int i;
11873 u32 val;
11874
11875 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
11876 tw32(OTP_CTRL, cmd);
11877
11878 /* Wait for up to 1 ms for command to execute. */
11879 for (i = 0; i < 100; i++) {
11880 val = tr32(OTP_STATUS);
11881 if (val & OTP_STATUS_CMD_DONE)
11882 break;
11883 udelay(10);
11884 }
11885
11886 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
11887}
11888
11889/* Read the gphy configuration from the OTP region of the chip. The gphy
11890 * configuration is a 32-bit value that straddles the alignment boundary.
11891 * We do two 32-bit reads and then shift and merge the results.
11892 */
11893static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
11894{
11895 u32 bhalf_otp, thalf_otp;
11896
11897 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
11898
11899 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
11900 return 0;
11901
11902 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
11903
11904 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
11905 return 0;
11906
11907 thalf_otp = tr32(OTP_READ_DATA);
11908
11909 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
11910
11911 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
11912 return 0;
11913
11914 bhalf_otp = tr32(OTP_READ_DATA);
11915
11916 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
11917}
11918
Michael Chan7d0c41e2005-04-21 17:06:20 -070011919static int __devinit tg3_phy_probe(struct tg3 *tp)
11920{
11921 u32 hw_phy_id_1, hw_phy_id_2;
11922 u32 hw_phy_id, hw_phy_id_masked;
11923 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011924
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011925 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)
11926 return tg3_phy_init(tp);
11927
Linus Torvalds1da177e2005-04-16 15:20:36 -070011928 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010011929 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011930 */
11931 err = 0;
Matt Carlson0d3031d2007-10-10 18:02:43 -070011932 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
11933 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011934 hw_phy_id = hw_phy_id_masked = PHY_ID_INVALID;
11935 } else {
11936 /* Now read the physical PHY_ID from the chip and verify
11937 * that it is sane. If it doesn't look good, we fall back
11938 * to either the hard-coded table based PHY_ID and failing
11939 * that the value found in the eeprom area.
11940 */
11941 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
11942 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
11943
11944 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
11945 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
11946 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
11947
11948 hw_phy_id_masked = hw_phy_id & PHY_ID_MASK;
11949 }
11950
11951 if (!err && KNOWN_PHY_ID(hw_phy_id_masked)) {
11952 tp->phy_id = hw_phy_id;
11953 if (hw_phy_id_masked == PHY_ID_BCM8002)
11954 tp->tg3_flags2 |= TG3_FLG2_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070011955 else
11956 tp->tg3_flags2 &= ~TG3_FLG2_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011957 } else {
Michael Chan7d0c41e2005-04-21 17:06:20 -070011958 if (tp->phy_id != PHY_ID_INVALID) {
11959 /* Do nothing, phy ID already set up in
11960 * tg3_get_eeprom_hw_cfg().
11961 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011962 } else {
11963 struct subsys_tbl_ent *p;
11964
11965 /* No eeprom signature? Try the hardcoded
11966 * subsys device table.
11967 */
11968 p = lookup_by_subsys(tp);
11969 if (!p)
11970 return -ENODEV;
11971
11972 tp->phy_id = p->phy_id;
11973 if (!tp->phy_id ||
11974 tp->phy_id == PHY_ID_BCM8002)
11975 tp->tg3_flags2 |= TG3_FLG2_PHY_SERDES;
11976 }
11977 }
11978
Michael Chan747e8f82005-07-25 12:33:22 -070011979 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES) &&
Matt Carlson0d3031d2007-10-10 18:02:43 -070011980 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070011981 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chan3600d912006-12-07 00:21:48 -080011982 u32 bmsr, adv_reg, tg3_ctrl, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011983
11984 tg3_readphy(tp, MII_BMSR, &bmsr);
11985 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
11986 (bmsr & BMSR_LSTATUS))
11987 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011988
Linus Torvalds1da177e2005-04-16 15:20:36 -070011989 err = tg3_phy_reset(tp);
11990 if (err)
11991 return err;
11992
11993 adv_reg = (ADVERTISE_10HALF | ADVERTISE_10FULL |
11994 ADVERTISE_100HALF | ADVERTISE_100FULL |
11995 ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
11996 tg3_ctrl = 0;
11997 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY)) {
11998 tg3_ctrl = (MII_TG3_CTRL_ADV_1000_HALF |
11999 MII_TG3_CTRL_ADV_1000_FULL);
12000 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
12001 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
12002 tg3_ctrl |= (MII_TG3_CTRL_AS_MASTER |
12003 MII_TG3_CTRL_ENABLE_AS_MASTER);
12004 }
12005
Michael Chan3600d912006-12-07 00:21:48 -080012006 mask = (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
12007 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
12008 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full);
12009 if (!tg3_copper_is_advertising_all(tp, mask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012010 tg3_writephy(tp, MII_ADVERTISE, adv_reg);
12011
12012 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY))
12013 tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
12014
12015 tg3_writephy(tp, MII_BMCR,
12016 BMCR_ANENABLE | BMCR_ANRESTART);
12017 }
12018 tg3_phy_set_wirespeed(tp);
12019
12020 tg3_writephy(tp, MII_ADVERTISE, adv_reg);
12021 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY))
12022 tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
12023 }
12024
12025skip_phy_reset:
12026 if ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5401) {
12027 err = tg3_init_5401phy_dsp(tp);
12028 if (err)
12029 return err;
12030 }
12031
12032 if (!err && ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5401)) {
12033 err = tg3_init_5401phy_dsp(tp);
12034 }
12035
Michael Chan747e8f82005-07-25 12:33:22 -070012036 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012037 tp->link_config.advertising =
12038 (ADVERTISED_1000baseT_Half |
12039 ADVERTISED_1000baseT_Full |
12040 ADVERTISED_Autoneg |
12041 ADVERTISED_FIBRE);
12042 if (tp->tg3_flags & TG3_FLAG_10_100_ONLY)
12043 tp->link_config.advertising &=
12044 ~(ADVERTISED_1000baseT_Half |
12045 ADVERTISED_1000baseT_Full);
12046
12047 return err;
12048}
12049
12050static void __devinit tg3_read_partno(struct tg3 *tp)
12051{
Matt Carlson6d348f22009-02-25 14:25:52 +000012052 unsigned char vpd_data[256]; /* in little-endian format */
Michael Chanaf2c6a42006-11-07 14:57:51 -080012053 unsigned int i;
Michael Chan1b277772006-03-20 22:27:48 -080012054 u32 magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012055
Matt Carlsondf259d82009-04-20 06:57:14 +000012056 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
12057 tg3_nvram_read(tp, 0x0, &magic))
David S. Millerf49639e2006-06-09 11:58:36 -070012058 goto out_not_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012059
Michael Chan18201802006-03-20 22:29:15 -080012060 if (magic == TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080012061 for (i = 0; i < 256; i += 4) {
12062 u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012063
Matt Carlson6d348f22009-02-25 14:25:52 +000012064 /* The data is in little-endian format in NVRAM.
12065 * Use the big-endian read routines to preserve
12066 * the byte order as it exists in NVRAM.
12067 */
12068 if (tg3_nvram_read_be32(tp, 0x100 + i, &tmp))
Michael Chan1b277772006-03-20 22:27:48 -080012069 goto out_not_found;
12070
Matt Carlson6d348f22009-02-25 14:25:52 +000012071 memcpy(&vpd_data[i], &tmp, sizeof(tmp));
Michael Chan1b277772006-03-20 22:27:48 -080012072 }
12073 } else {
12074 int vpd_cap;
12075
12076 vpd_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_VPD);
12077 for (i = 0; i < 256; i += 4) {
12078 u32 tmp, j = 0;
Al Virob9fc7dc2007-12-17 22:59:57 -080012079 __le32 v;
Michael Chan1b277772006-03-20 22:27:48 -080012080 u16 tmp16;
12081
12082 pci_write_config_word(tp->pdev, vpd_cap + PCI_VPD_ADDR,
12083 i);
12084 while (j++ < 100) {
12085 pci_read_config_word(tp->pdev, vpd_cap +
12086 PCI_VPD_ADDR, &tmp16);
12087 if (tmp16 & 0x8000)
12088 break;
12089 msleep(1);
12090 }
David S. Millerf49639e2006-06-09 11:58:36 -070012091 if (!(tmp16 & 0x8000))
12092 goto out_not_found;
12093
Michael Chan1b277772006-03-20 22:27:48 -080012094 pci_read_config_dword(tp->pdev, vpd_cap + PCI_VPD_DATA,
12095 &tmp);
Al Virob9fc7dc2007-12-17 22:59:57 -080012096 v = cpu_to_le32(tmp);
Matt Carlson6d348f22009-02-25 14:25:52 +000012097 memcpy(&vpd_data[i], &v, sizeof(v));
Michael Chan1b277772006-03-20 22:27:48 -080012098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012099 }
12100
12101 /* Now parse and find the part number. */
Michael Chanaf2c6a42006-11-07 14:57:51 -080012102 for (i = 0; i < 254; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012103 unsigned char val = vpd_data[i];
Michael Chanaf2c6a42006-11-07 14:57:51 -080012104 unsigned int block_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012105
12106 if (val == 0x82 || val == 0x91) {
12107 i = (i + 3 +
12108 (vpd_data[i + 1] +
12109 (vpd_data[i + 2] << 8)));
12110 continue;
12111 }
12112
12113 if (val != 0x90)
12114 goto out_not_found;
12115
12116 block_end = (i + 3 +
12117 (vpd_data[i + 1] +
12118 (vpd_data[i + 2] << 8)));
12119 i += 3;
Michael Chanaf2c6a42006-11-07 14:57:51 -080012120
12121 if (block_end > 256)
12122 goto out_not_found;
12123
12124 while (i < (block_end - 2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012125 if (vpd_data[i + 0] == 'P' &&
12126 vpd_data[i + 1] == 'N') {
12127 int partno_len = vpd_data[i + 2];
12128
Michael Chanaf2c6a42006-11-07 14:57:51 -080012129 i += 3;
12130 if (partno_len > 24 || (partno_len + i) > 256)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012131 goto out_not_found;
12132
12133 memcpy(tp->board_part_number,
Michael Chanaf2c6a42006-11-07 14:57:51 -080012134 &vpd_data[i], partno_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012135
12136 /* Success. */
12137 return;
12138 }
Michael Chanaf2c6a42006-11-07 14:57:51 -080012139 i += 3 + vpd_data[i + 2];
Linus Torvalds1da177e2005-04-16 15:20:36 -070012140 }
12141
12142 /* Part number not found. */
12143 goto out_not_found;
12144 }
12145
12146out_not_found:
Michael Chanb5d37722006-09-27 16:06:21 -070012147 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12148 strcpy(tp->board_part_number, "BCM95906");
Matt Carlsondf259d82009-04-20 06:57:14 +000012149 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12150 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
12151 strcpy(tp->board_part_number, "BCM57780");
12152 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12153 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
12154 strcpy(tp->board_part_number, "BCM57760");
12155 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12156 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
12157 strcpy(tp->board_part_number, "BCM57790");
Matt Carlson5e7ccf22009-08-25 10:08:42 +000012158 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12159 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
12160 strcpy(tp->board_part_number, "BCM57788");
Michael Chanb5d37722006-09-27 16:06:21 -070012161 else
12162 strcpy(tp->board_part_number, "none");
Linus Torvalds1da177e2005-04-16 15:20:36 -070012163}
12164
Matt Carlson9c8a6202007-10-21 16:16:08 -070012165static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
12166{
12167 u32 val;
12168
Matt Carlsone4f34112009-02-25 14:25:00 +000012169 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070012170 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000012171 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070012172 val != 0)
12173 return 0;
12174
12175 return 1;
12176}
12177
Matt Carlsonacd9c112009-02-25 14:26:33 +000012178static void __devinit tg3_read_bc_ver(struct tg3 *tp)
12179{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012180 u32 val, offset, start, ver_offset;
Matt Carlsonacd9c112009-02-25 14:26:33 +000012181 int i;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012182 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000012183
12184 if (tg3_nvram_read(tp, 0xc, &offset) ||
12185 tg3_nvram_read(tp, 0x4, &start))
12186 return;
12187
12188 offset = tg3_nvram_logical_addr(tp, offset);
12189
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012190 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000012191 return;
12192
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012193 if ((val & 0xfc000000) == 0x0c000000) {
12194 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000012195 return;
12196
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012197 if (val == 0)
12198 newver = true;
12199 }
12200
12201 if (newver) {
12202 if (tg3_nvram_read(tp, offset + 8, &ver_offset))
12203 return;
12204
12205 offset = offset + ver_offset - start;
12206 for (i = 0; i < 16; i += 4) {
12207 __be32 v;
12208 if (tg3_nvram_read_be32(tp, offset + i, &v))
12209 return;
12210
12211 memcpy(tp->fw_ver + i, &v, sizeof(v));
12212 }
12213 } else {
12214 u32 major, minor;
12215
12216 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
12217 return;
12218
12219 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
12220 TG3_NVM_BCVER_MAJSFT;
12221 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
12222 snprintf(&tp->fw_ver[0], 32, "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000012223 }
12224}
12225
Matt Carlsona6f6cb12009-02-25 14:27:43 +000012226static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
12227{
12228 u32 val, major, minor;
12229
12230 /* Use native endian representation */
12231 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
12232 return;
12233
12234 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
12235 TG3_NVM_HWSB_CFG1_MAJSFT;
12236 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
12237 TG3_NVM_HWSB_CFG1_MINSFT;
12238
12239 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
12240}
12241
Matt Carlsondfe00d72008-11-21 17:19:41 -080012242static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
12243{
12244 u32 offset, major, minor, build;
12245
12246 tp->fw_ver[0] = 's';
12247 tp->fw_ver[1] = 'b';
12248 tp->fw_ver[2] = '\0';
12249
12250 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
12251 return;
12252
12253 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
12254 case TG3_EEPROM_SB_REVISION_0:
12255 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
12256 break;
12257 case TG3_EEPROM_SB_REVISION_2:
12258 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
12259 break;
12260 case TG3_EEPROM_SB_REVISION_3:
12261 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
12262 break;
12263 default:
12264 return;
12265 }
12266
Matt Carlsone4f34112009-02-25 14:25:00 +000012267 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080012268 return;
12269
12270 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
12271 TG3_EEPROM_SB_EDH_BLD_SHFT;
12272 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
12273 TG3_EEPROM_SB_EDH_MAJ_SHFT;
12274 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
12275
12276 if (minor > 99 || build > 26)
12277 return;
12278
12279 snprintf(&tp->fw_ver[2], 30, " v%d.%02d", major, minor);
12280
12281 if (build > 0) {
12282 tp->fw_ver[8] = 'a' + build - 1;
12283 tp->fw_ver[9] = '\0';
12284 }
12285}
12286
Matt Carlsonacd9c112009-02-25 14:26:33 +000012287static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080012288{
12289 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000012290 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070012291
12292 for (offset = TG3_NVM_DIR_START;
12293 offset < TG3_NVM_DIR_END;
12294 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000012295 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012296 return;
12297
12298 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
12299 break;
12300 }
12301
12302 if (offset == TG3_NVM_DIR_END)
12303 return;
12304
12305 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
12306 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000012307 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012308 return;
12309
Matt Carlsone4f34112009-02-25 14:25:00 +000012310 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070012311 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000012312 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012313 return;
12314
12315 offset += val - start;
12316
Matt Carlsonacd9c112009-02-25 14:26:33 +000012317 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012318
Matt Carlsonacd9c112009-02-25 14:26:33 +000012319 tp->fw_ver[vlen++] = ',';
12320 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070012321
12322 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012323 __be32 v;
12324 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012325 return;
12326
Al Virob9fc7dc2007-12-17 22:59:57 -080012327 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012328
Matt Carlsonacd9c112009-02-25 14:26:33 +000012329 if (vlen > TG3_VER_SIZE - sizeof(v)) {
12330 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012331 break;
12332 }
12333
Matt Carlsonacd9c112009-02-25 14:26:33 +000012334 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
12335 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012336 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000012337}
12338
Matt Carlson7fd76442009-02-25 14:27:20 +000012339static void __devinit tg3_read_dash_ver(struct tg3 *tp)
12340{
12341 int vlen;
12342 u32 apedata;
12343
12344 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) ||
12345 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
12346 return;
12347
12348 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
12349 if (apedata != APE_SEG_SIG_MAGIC)
12350 return;
12351
12352 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
12353 if (!(apedata & APE_FW_STATUS_READY))
12354 return;
12355
12356 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
12357
12358 vlen = strlen(tp->fw_ver);
12359
12360 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " DASH v%d.%d.%d.%d",
12361 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
12362 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
12363 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
12364 (apedata & APE_FW_VERSION_BLDMSK));
12365}
12366
Matt Carlsonacd9c112009-02-25 14:26:33 +000012367static void __devinit tg3_read_fw_ver(struct tg3 *tp)
12368{
12369 u32 val;
12370
Matt Carlsondf259d82009-04-20 06:57:14 +000012371 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) {
12372 tp->fw_ver[0] = 's';
12373 tp->fw_ver[1] = 'b';
12374 tp->fw_ver[2] = '\0';
12375
12376 return;
12377 }
12378
Matt Carlsonacd9c112009-02-25 14:26:33 +000012379 if (tg3_nvram_read(tp, 0, &val))
12380 return;
12381
12382 if (val == TG3_EEPROM_MAGIC)
12383 tg3_read_bc_ver(tp);
12384 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
12385 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000012386 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
12387 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000012388 else
12389 return;
12390
12391 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
12392 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
12393 return;
12394
12395 tg3_read_mgmtfw_ver(tp);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012396
12397 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080012398}
12399
Michael Chan7544b092007-05-05 13:08:32 -070012400static struct pci_dev * __devinit tg3_find_peer(struct tg3 *);
12401
Linus Torvalds1da177e2005-04-16 15:20:36 -070012402static int __devinit tg3_get_invariants(struct tg3 *tp)
12403{
12404 static struct pci_device_id write_reorder_chipsets[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012405 { PCI_DEVICE(PCI_VENDOR_ID_AMD,
12406 PCI_DEVICE_ID_AMD_FE_GATE_700C) },
John W. Linvillec165b002006-07-08 13:28:53 -070012407 { PCI_DEVICE(PCI_VENDOR_ID_AMD,
12408 PCI_DEVICE_ID_AMD_8131_BRIDGE) },
Michael Chan399de502005-10-03 14:02:39 -070012409 { PCI_DEVICE(PCI_VENDOR_ID_VIA,
12410 PCI_DEVICE_ID_VIA_8385_0) },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012411 { },
12412 };
12413 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012414 u32 pci_state_reg, grc_misc_cfg;
12415 u32 val;
12416 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080012417 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012418
Linus Torvalds1da177e2005-04-16 15:20:36 -070012419 /* Force memory write invalidate off. If we leave it on,
12420 * then on 5700_BX chips we have to enable a workaround.
12421 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
12422 * to match the cacheline size. The Broadcom driver have this
12423 * workaround but turns MWI off all the times so never uses
12424 * it. This seems to suggest that the workaround is insufficient.
12425 */
12426 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
12427 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
12428 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
12429
12430 /* It is absolutely critical that TG3PCI_MISC_HOST_CTRL
12431 * has the register indirect write enable bit set before
12432 * we try to access any of the MMIO registers. It is also
12433 * critical that the PCI-X hw workaround situation is decided
12434 * before that as well.
12435 */
12436 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
12437 &misc_ctrl_reg);
12438
12439 tp->pci_chip_rev_id = (misc_ctrl_reg >>
12440 MISC_HOST_CTRL_CHIPREV_SHIFT);
Matt Carlson795d01c2007-10-07 23:28:17 -070012441 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
12442 u32 prod_id_asic_rev;
12443
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012444 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717C ||
12445 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717S ||
12446 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718C ||
12447 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718S)
12448 pci_read_config_dword(tp->pdev,
12449 TG3PCI_GEN2_PRODID_ASICREV,
12450 &prod_id_asic_rev);
12451 else
12452 pci_read_config_dword(tp->pdev, TG3PCI_PRODID_ASICREV,
12453 &prod_id_asic_rev);
12454
Matt Carlson321d32a2008-11-21 17:22:19 -080012455 tp->pci_chip_rev_id = prod_id_asic_rev;
Matt Carlson795d01c2007-10-07 23:28:17 -070012456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012457
Michael Chanff645be2005-04-21 17:09:53 -070012458 /* Wrong chip ID in 5752 A0. This code can be removed later
12459 * as A0 is not in production.
12460 */
12461 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
12462 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
12463
Michael Chan68929142005-08-09 20:17:14 -070012464 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
12465 * we need to disable memory and use config. cycles
12466 * only to access all registers. The 5702/03 chips
12467 * can mistakenly decode the special cycles from the
12468 * ICH chipsets as memory write cycles, causing corruption
12469 * of register and memory space. Only certain ICH bridges
12470 * will drive special cycles with non-zero data during the
12471 * address phase which can fall within the 5703's address
12472 * range. This is not an ICH bug as the PCI spec allows
12473 * non-zero address during special cycles. However, only
12474 * these ICH bridges are known to drive non-zero addresses
12475 * during special cycles.
12476 *
12477 * Since special cycles do not cross PCI bridges, we only
12478 * enable this workaround if the 5703 is on the secondary
12479 * bus of these ICH bridges.
12480 */
12481 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
12482 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
12483 static struct tg3_dev_id {
12484 u32 vendor;
12485 u32 device;
12486 u32 rev;
12487 } ich_chipsets[] = {
12488 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
12489 PCI_ANY_ID },
12490 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
12491 PCI_ANY_ID },
12492 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
12493 0xa },
12494 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
12495 PCI_ANY_ID },
12496 { },
12497 };
12498 struct tg3_dev_id *pci_id = &ich_chipsets[0];
12499 struct pci_dev *bridge = NULL;
12500
12501 while (pci_id->vendor != 0) {
12502 bridge = pci_get_device(pci_id->vendor, pci_id->device,
12503 bridge);
12504 if (!bridge) {
12505 pci_id++;
12506 continue;
12507 }
12508 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070012509 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070012510 continue;
12511 }
12512 if (bridge->subordinate &&
12513 (bridge->subordinate->number ==
12514 tp->pdev->bus->number)) {
12515
12516 tp->tg3_flags2 |= TG3_FLG2_ICH_WORKAROUND;
12517 pci_dev_put(bridge);
12518 break;
12519 }
12520 }
12521 }
12522
Matt Carlson41588ba2008-04-19 18:12:33 -070012523 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
12524 static struct tg3_dev_id {
12525 u32 vendor;
12526 u32 device;
12527 } bridge_chipsets[] = {
12528 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
12529 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
12530 { },
12531 };
12532 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
12533 struct pci_dev *bridge = NULL;
12534
12535 while (pci_id->vendor != 0) {
12536 bridge = pci_get_device(pci_id->vendor,
12537 pci_id->device,
12538 bridge);
12539 if (!bridge) {
12540 pci_id++;
12541 continue;
12542 }
12543 if (bridge->subordinate &&
12544 (bridge->subordinate->number <=
12545 tp->pdev->bus->number) &&
12546 (bridge->subordinate->subordinate >=
12547 tp->pdev->bus->number)) {
12548 tp->tg3_flags3 |= TG3_FLG3_5701_DMA_BUG;
12549 pci_dev_put(bridge);
12550 break;
12551 }
12552 }
12553 }
12554
Michael Chan4a29cc22006-03-19 13:21:12 -080012555 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
12556 * DMA addresses > 40-bit. This bridge may have other additional
12557 * 57xx devices behind it in some 4-port NIC designs for example.
12558 * Any tg3 device found behind the bridge will also need the 40-bit
12559 * DMA workaround.
12560 */
Michael Chana4e2b342005-10-26 15:46:52 -070012561 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
12562 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
12563 tp->tg3_flags2 |= TG3_FLG2_5780_CLASS;
Michael Chan4a29cc22006-03-19 13:21:12 -080012564 tp->tg3_flags |= TG3_FLAG_40BIT_DMA_BUG;
Michael Chan4cf78e42005-07-25 12:29:19 -070012565 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Michael Chana4e2b342005-10-26 15:46:52 -070012566 }
Michael Chan4a29cc22006-03-19 13:21:12 -080012567 else {
12568 struct pci_dev *bridge = NULL;
12569
12570 do {
12571 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
12572 PCI_DEVICE_ID_SERVERWORKS_EPB,
12573 bridge);
12574 if (bridge && bridge->subordinate &&
12575 (bridge->subordinate->number <=
12576 tp->pdev->bus->number) &&
12577 (bridge->subordinate->subordinate >=
12578 tp->pdev->bus->number)) {
12579 tp->tg3_flags |= TG3_FLAG_40BIT_DMA_BUG;
12580 pci_dev_put(bridge);
12581 break;
12582 }
12583 } while (bridge);
12584 }
Michael Chan4cf78e42005-07-25 12:29:19 -070012585
Linus Torvalds1da177e2005-04-16 15:20:36 -070012586 /* Initialize misc host control in PCI block. */
12587 tp->misc_host_ctrl |= (misc_ctrl_reg &
12588 MISC_HOST_CTRL_CHIPREV);
12589 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
12590 tp->misc_host_ctrl);
12591
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012592 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
12593 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
12594 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Michael Chan7544b092007-05-05 13:08:32 -070012595 tp->pdev_peer = tg3_find_peer(tp);
12596
Matt Carlson321d32a2008-11-21 17:22:19 -080012597 /* Intentionally exclude ASIC_REV_5906 */
12598 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Michael Chand9ab5ad2006-03-20 22:27:35 -080012599 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070012600 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070012601 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070012602 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012603 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
12604 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson321d32a2008-11-21 17:22:19 -080012605 tp->tg3_flags3 |= TG3_FLG3_5755_PLUS;
12606
12607 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
12608 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Michael Chanb5d37722006-09-27 16:06:21 -070012609 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080012610 (tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
Michael Chana4e2b342005-10-26 15:46:52 -070012611 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
John W. Linville6708e5c2005-04-21 17:00:52 -070012612 tp->tg3_flags2 |= TG3_FLG2_5750_PLUS;
12613
John W. Linville1b440c562005-04-21 17:03:18 -070012614 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) ||
12615 (tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
12616 tp->tg3_flags2 |= TG3_FLG2_5705_PLUS;
12617
Matt Carlson027455a2008-12-21 20:19:30 -080012618 /* 5700 B0 chips do not support checksumming correctly due
12619 * to hardware bugs.
12620 */
12621 if (tp->pci_chip_rev_id == CHIPREV_ID_5700_B0)
12622 tp->tg3_flags |= TG3_FLAG_BROKEN_CHECKSUMS;
12623 else {
12624 tp->tg3_flags |= TG3_FLAG_RX_CHECKSUMS;
12625 tp->dev->features |= NETIF_F_IP_CSUM | NETIF_F_SG;
12626 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
12627 tp->dev->features |= NETIF_F_IPV6_CSUM;
12628 }
12629
Michael Chan5a6f3072006-03-20 22:28:05 -080012630 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
Michael Chan7544b092007-05-05 13:08:32 -070012631 tp->tg3_flags |= TG3_FLAG_SUPPORT_MSI;
12632 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
12633 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
12634 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
12635 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
12636 tp->pdev_peer == tp->pdev))
12637 tp->tg3_flags &= ~TG3_FLAG_SUPPORT_MSI;
12638
Matt Carlson321d32a2008-11-21 17:22:19 -080012639 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070012640 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan5a6f3072006-03-20 22:28:05 -080012641 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_2;
Michael Chanfcfa0a32006-03-20 22:28:41 -080012642 tp->tg3_flags2 |= TG3_FLG2_1SHOT_MSI;
Michael Chan52c0fd82006-06-29 20:15:54 -070012643 } else {
Michael Chan7f62ad52007-02-20 23:25:40 -080012644 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_1 | TG3_FLG2_TSO_BUG;
Michael Chan52c0fd82006-06-29 20:15:54 -070012645 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
12646 ASIC_REV_5750 &&
12647 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
Michael Chan7f62ad52007-02-20 23:25:40 -080012648 tp->tg3_flags2 &= ~TG3_FLG2_TSO_BUG;
Michael Chan52c0fd82006-06-29 20:15:54 -070012649 }
Michael Chan5a6f3072006-03-20 22:28:05 -080012650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012651
Matt Carlson4f125f42009-09-01 12:55:02 +000012652 tp->irq_max = 1;
12653
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012654 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
12655 tp->tg3_flags |= TG3_FLAG_SUPPORT_MSIX;
12656 tp->irq_max = TG3_IRQ_MAX_VECS;
12657 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000012658
12659 if (!(tp->tg3_flags3 & TG3_FLG3_5755_PLUS)) {
Matt Carlson92c6b8d2009-11-02 14:23:27 +000012660 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12661 tp->tg3_flags3 |= TG3_FLG3_SHORT_DMA_BUG;
12662 else {
12663 tp->tg3_flags3 |= TG3_FLG3_4G_DMA_BNDRY_BUG;
12664 tp->tg3_flags3 |= TG3_FLG3_40BIT_DMA_LIMIT_BUG;
12665 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000012666 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012667
Matt Carlsonf51f3562008-05-25 23:45:08 -070012668 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012669 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
12670 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson8f666b02009-08-28 13:58:24 +000012671 tp->tg3_flags |= TG3_FLAG_JUMBO_CAPABLE;
Michael Chan0f893dc2005-07-25 12:30:38 -070012672
Matt Carlson52f44902008-11-21 17:17:04 -080012673 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
12674 &pci_state_reg);
12675
Matt Carlson5e7dfd02008-11-21 17:18:16 -080012676 tp->pcie_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_EXP);
12677 if (tp->pcie_cap != 0) {
12678 u16 lnkctl;
12679
Linus Torvalds1da177e2005-04-16 15:20:36 -070012680 tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS;
Matt Carlson5f5c51e2007-11-12 21:19:37 -080012681
12682 pcie_set_readrq(tp->pdev, 4096);
12683
Matt Carlson5e7dfd02008-11-21 17:18:16 -080012684 pci_read_config_word(tp->pdev,
12685 tp->pcie_cap + PCI_EXP_LNKCTL,
12686 &lnkctl);
12687 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
12688 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanc7835a72006-11-15 21:14:42 -080012689 tp->tg3_flags2 &= ~TG3_FLG2_HW_TSO_2;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080012690 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080012691 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000012692 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
12693 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Matt Carlson5e7dfd02008-11-21 17:18:16 -080012694 tp->tg3_flags3 |= TG3_FLG3_CLKREQ_BUG;
Michael Chanc7835a72006-11-15 21:14:42 -080012695 }
Matt Carlson52f44902008-11-21 17:17:04 -080012696 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Matt Carlsonfcb389d2008-11-03 16:55:44 -080012697 tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS;
Matt Carlson52f44902008-11-21 17:17:04 -080012698 } else if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
12699 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
12700 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
12701 if (!tp->pcix_cap) {
12702 printk(KERN_ERR PFX "Cannot find PCI-X "
12703 "capability, aborting.\n");
12704 return -EIO;
12705 }
12706
12707 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
12708 tp->tg3_flags |= TG3_FLAG_PCIX_MODE;
12709 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012710
Michael Chan399de502005-10-03 14:02:39 -070012711 /* If we have an AMD 762 or VIA K8T800 chipset, write
12712 * reordering to the mailbox registers done by the host
12713 * controller can cause major troubles. We read back from
12714 * every mailbox register write to force the writes to be
12715 * posted to the chip in order.
12716 */
12717 if (pci_dev_present(write_reorder_chipsets) &&
12718 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
12719 tp->tg3_flags |= TG3_FLAG_MBOX_WRITE_REORDER;
12720
Matt Carlson69fc4052008-12-21 20:19:57 -080012721 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
12722 &tp->pci_cacheline_sz);
12723 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
12724 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012725 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
12726 tp->pci_lat_timer < 64) {
12727 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080012728 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
12729 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012730 }
12731
Matt Carlson52f44902008-11-21 17:17:04 -080012732 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
12733 /* 5700 BX chips need to have their TX producer index
12734 * mailboxes written twice to workaround a bug.
12735 */
12736 tp->tg3_flags |= TG3_FLAG_TXD_MBOX_HWBUG;
Matt Carlson9974a352007-10-07 23:27:28 -070012737
Matt Carlson52f44902008-11-21 17:17:04 -080012738 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012739 *
12740 * The workaround is to use indirect register accesses
12741 * for all chip writes not to mailbox registers.
12742 */
Matt Carlson52f44902008-11-21 17:17:04 -080012743 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012744 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012745
12746 tp->tg3_flags |= TG3_FLAG_PCIX_TARGET_HWBUG;
12747
12748 /* The chip can have it's power management PCI config
12749 * space registers clobbered due to this bug.
12750 * So explicitly force the chip into D0 here.
12751 */
Matt Carlson9974a352007-10-07 23:27:28 -070012752 pci_read_config_dword(tp->pdev,
12753 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012754 &pm_reg);
12755 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
12756 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070012757 pci_write_config_dword(tp->pdev,
12758 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012759 pm_reg);
12760
12761 /* Also, force SERR#/PERR# in PCI command. */
12762 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
12763 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
12764 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
12765 }
12766 }
12767
Linus Torvalds1da177e2005-04-16 15:20:36 -070012768 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
12769 tp->tg3_flags |= TG3_FLAG_PCI_HIGH_SPEED;
12770 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
12771 tp->tg3_flags |= TG3_FLAG_PCI_32BIT;
12772
12773 /* Chip-specific fixup from Broadcom driver */
12774 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
12775 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
12776 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
12777 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
12778 }
12779
Michael Chan1ee582d2005-08-09 20:16:46 -070012780 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070012781 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070012782 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070012783 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070012784 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070012785 tp->write32_tx_mbox = tg3_write32;
12786 tp->write32_rx_mbox = tg3_write32;
12787
12788 /* Various workaround register access methods */
12789 if (tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG)
12790 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070012791 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
12792 ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
12793 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
12794 /*
12795 * Back to back register writes can cause problems on these
12796 * chips, the workaround is to read back all reg writes
12797 * except those to mailbox regs.
12798 *
12799 * See tg3_write_indirect_reg32().
12800 */
Michael Chan1ee582d2005-08-09 20:16:46 -070012801 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070012802 }
12803
Michael Chan1ee582d2005-08-09 20:16:46 -070012804 if ((tp->tg3_flags & TG3_FLAG_TXD_MBOX_HWBUG) ||
12805 (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)) {
12806 tp->write32_tx_mbox = tg3_write32_tx_mbox;
12807 if (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)
12808 tp->write32_rx_mbox = tg3_write_flush_reg32;
12809 }
Michael Chan20094932005-08-09 20:16:32 -070012810
Michael Chan68929142005-08-09 20:17:14 -070012811 if (tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND) {
12812 tp->read32 = tg3_read_indirect_reg32;
12813 tp->write32 = tg3_write_indirect_reg32;
12814 tp->read32_mbox = tg3_read_indirect_mbox;
12815 tp->write32_mbox = tg3_write_indirect_mbox;
12816 tp->write32_tx_mbox = tg3_write_indirect_mbox;
12817 tp->write32_rx_mbox = tg3_write_indirect_mbox;
12818
12819 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070012820 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070012821
12822 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
12823 pci_cmd &= ~PCI_COMMAND_MEMORY;
12824 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
12825 }
Michael Chanb5d37722006-09-27 16:06:21 -070012826 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
12827 tp->read32_mbox = tg3_read32_mbox_5906;
12828 tp->write32_mbox = tg3_write32_mbox_5906;
12829 tp->write32_tx_mbox = tg3_write32_mbox_5906;
12830 tp->write32_rx_mbox = tg3_write32_mbox_5906;
12831 }
Michael Chan68929142005-08-09 20:17:14 -070012832
Michael Chanbbadf502006-04-06 21:46:34 -070012833 if (tp->write32 == tg3_write_indirect_reg32 ||
12834 ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) &&
12835 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070012836 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Michael Chanbbadf502006-04-06 21:46:34 -070012837 tp->tg3_flags |= TG3_FLAG_SRAM_USE_CONFIG;
12838
Michael Chan7d0c41e2005-04-21 17:06:20 -070012839 /* Get eeprom hw config before calling tg3_set_power_state().
Michael Chan9d26e212006-12-07 00:21:14 -080012840 * In particular, the TG3_FLG2_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070012841 * determined before calling tg3_set_power_state() so that
12842 * we know whether or not to switch out of Vaux power.
12843 * When the flag is set, it means that GPIO1 is used for eeprom
12844 * write protect and also implies that it is a LOM where GPIOs
12845 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012846 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070012847 tg3_get_eeprom_hw_cfg(tp);
12848
Matt Carlson0d3031d2007-10-10 18:02:43 -070012849 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
12850 /* Allow reads and writes to the
12851 * APE register and memory space.
12852 */
12853 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
12854 PCISTATE_ALLOW_APE_SHMEM_WR;
12855 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
12856 pci_state_reg);
12857 }
12858
Matt Carlson9936bcf2007-10-10 18:03:07 -070012859 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson57e69832008-05-25 23:48:31 -070012860 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080012861 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012862 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
12863 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlsond30cdd22007-10-07 23:28:35 -070012864 tp->tg3_flags |= TG3_FLAG_CPMU_PRESENT;
12865
Michael Chan314fba32005-04-21 17:07:04 -070012866 /* Set up tp->grc_local_ctrl before calling tg3_set_power_state().
12867 * GPIO1 driven high will bring 5700's external PHY out of reset.
12868 * It is also used as eeprom write protect on LOMs.
12869 */
12870 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
12871 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
12872 (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT))
12873 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
12874 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070012875 /* Unused GPIO3 must be driven as output on 5752 because there
12876 * are no pull-up resistors on unused GPIO pins.
12877 */
12878 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
12879 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070012880
Matt Carlson321d32a2008-11-21 17:22:19 -080012881 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
12882 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Michael Chanaf36e6b2006-03-23 01:28:06 -080012883 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
12884
Matt Carlson8d519ab2009-04-20 06:58:01 +000012885 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
12886 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070012887 /* Turn off the debug UART. */
12888 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
12889 if (tp->tg3_flags2 & TG3_FLG2_IS_NIC)
12890 /* Keep VMain power. */
12891 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
12892 GRC_LCLCTRL_GPIO_OUTPUT0;
12893 }
12894
Linus Torvalds1da177e2005-04-16 15:20:36 -070012895 /* Force the chip into D0. */
Michael Chanbc1c7562006-03-20 17:48:03 -080012896 err = tg3_set_power_state(tp, PCI_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012897 if (err) {
12898 printk(KERN_ERR PFX "(%s) transition to D0 failed\n",
12899 pci_name(tp->pdev));
12900 return err;
12901 }
12902
Linus Torvalds1da177e2005-04-16 15:20:36 -070012903 /* Derive initial jumbo mode from MTU assigned in
12904 * ether_setup() via the alloc_etherdev() call
12905 */
Michael Chan0f893dc2005-07-25 12:30:38 -070012906 if (tp->dev->mtu > ETH_DATA_LEN &&
Michael Chana4e2b342005-10-26 15:46:52 -070012907 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Michael Chan0f893dc2005-07-25 12:30:38 -070012908 tp->tg3_flags |= TG3_FLAG_JUMBO_RING_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012909
12910 /* Determine WakeOnLan speed to use. */
12911 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
12912 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
12913 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
12914 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
12915 tp->tg3_flags &= ~(TG3_FLAG_WOL_SPEED_100MB);
12916 } else {
12917 tp->tg3_flags |= TG3_FLAG_WOL_SPEED_100MB;
12918 }
12919
Matt Carlson7f97a4b2009-08-25 10:10:03 +000012920 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12921 tp->tg3_flags3 |= TG3_FLG3_PHY_IS_FET;
12922
Linus Torvalds1da177e2005-04-16 15:20:36 -070012923 /* A few boards don't want Ethernet@WireSpeed phy feature */
12924 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
12925 ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
12926 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070012927 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlson7f97a4b2009-08-25 10:10:03 +000012928 (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) ||
Michael Chan747e8f82005-07-25 12:33:22 -070012929 (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012930 tp->tg3_flags2 |= TG3_FLG2_NO_ETH_WIRE_SPEED;
12931
12932 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
12933 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
12934 tp->tg3_flags2 |= TG3_FLG2_PHY_ADC_BUG;
12935 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
12936 tp->tg3_flags2 |= TG3_FLG2_PHY_5704_A0_BUG;
12937
Matt Carlson321d32a2008-11-21 17:22:19 -080012938 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Matt Carlson7f97a4b2009-08-25 10:10:03 +000012939 !(tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080012940 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012941 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
12942 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717) {
Michael Chanc424cb22006-04-29 18:56:34 -070012943 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070012944 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070012945 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
12946 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080012947 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
12948 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
12949 tp->tg3_flags2 |= TG3_FLG2_PHY_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080012950 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
12951 tp->tg3_flags2 |= TG3_FLG2_PHY_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080012952 } else
Michael Chanc424cb22006-04-29 18:56:34 -070012953 tp->tg3_flags2 |= TG3_FLG2_PHY_BER_BUG;
12954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012955
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012956 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
12957 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
12958 tp->phy_otp = tg3_read_otp_phycfg(tp);
12959 if (tp->phy_otp == 0)
12960 tp->phy_otp = TG3_OTP_DEFAULT;
12961 }
12962
Matt Carlsonf51f3562008-05-25 23:45:08 -070012963 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT)
Matt Carlson8ef21422008-05-02 16:47:53 -070012964 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
12965 else
12966 tp->mi_mode = MAC_MI_MODE_BASE;
12967
Linus Torvalds1da177e2005-04-16 15:20:36 -070012968 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012969 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
12970 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
12971 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
12972
Matt Carlson321d32a2008-11-21 17:22:19 -080012973 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
12974 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson57e69832008-05-25 23:48:31 -070012975 tp->tg3_flags3 |= TG3_FLG3_USE_PHYLIB;
12976
Matt Carlson158d7ab2008-05-29 01:37:54 -070012977 err = tg3_mdio_init(tp);
12978 if (err)
12979 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012980
12981 /* Initialize data/descriptor byte/word swapping. */
12982 val = tr32(GRC_MODE);
12983 val &= GRC_MODE_HOST_STACKUP;
12984 tw32(GRC_MODE, val | tp->grc_mode);
12985
12986 tg3_switch_clocks(tp);
12987
12988 /* Clear this out for sanity. */
12989 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
12990
12991 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
12992 &pci_state_reg);
12993 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
12994 (tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG) == 0) {
12995 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
12996
12997 if (chiprevid == CHIPREV_ID_5701_A0 ||
12998 chiprevid == CHIPREV_ID_5701_B0 ||
12999 chiprevid == CHIPREV_ID_5701_B2 ||
13000 chiprevid == CHIPREV_ID_5701_B5) {
13001 void __iomem *sram_base;
13002
13003 /* Write some dummy words into the SRAM status block
13004 * area, see if it reads back correctly. If the return
13005 * value is bad, force enable the PCIX workaround.
13006 */
13007 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
13008
13009 writel(0x00000000, sram_base);
13010 writel(0x00000000, sram_base + 4);
13011 writel(0xffffffff, sram_base + 4);
13012 if (readl(sram_base) != 0x00000000)
13013 tp->tg3_flags |= TG3_FLAG_PCIX_TARGET_HWBUG;
13014 }
13015 }
13016
13017 udelay(50);
13018 tg3_nvram_init(tp);
13019
13020 grc_misc_cfg = tr32(GRC_MISC_CFG);
13021 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
13022
Linus Torvalds1da177e2005-04-16 15:20:36 -070013023 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
13024 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
13025 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
13026 tp->tg3_flags2 |= TG3_FLG2_IS_5788;
13027
David S. Millerfac9b832005-05-18 22:46:34 -070013028 if (!(tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
13029 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700))
13030 tp->tg3_flags |= TG3_FLAG_TAGGED_STATUS;
13031 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) {
13032 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
13033 HOSTCC_MODE_CLRTICK_TXBD);
13034
13035 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
13036 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13037 tp->misc_host_ctrl);
13038 }
13039
Matt Carlson3bda1252008-08-15 14:08:22 -070013040 /* Preserve the APE MAC_MODE bits */
13041 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
13042 tp->mac_mode = tr32(MAC_MODE) |
13043 MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
13044 else
13045 tp->mac_mode = TG3_DEF_MAC_MODE;
13046
Linus Torvalds1da177e2005-04-16 15:20:36 -070013047 /* these are limited to 10/100 only */
13048 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
13049 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
13050 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
13051 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
13052 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
13053 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
13054 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
13055 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
13056 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
Michael Chan676917d2006-12-07 00:20:22 -080013057 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
13058 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013059 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013060 (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013061 tp->tg3_flags |= TG3_FLAG_10_100_ONLY;
13062
13063 err = tg3_phy_probe(tp);
13064 if (err) {
13065 printk(KERN_ERR PFX "(%s) phy probe failed, err %d\n",
13066 pci_name(tp->pdev), err);
13067 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013068 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013069 }
13070
13071 tg3_read_partno(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080013072 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013073
13074 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
13075 tp->tg3_flags &= ~TG3_FLAG_USE_MI_INTERRUPT;
13076 } else {
13077 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
13078 tp->tg3_flags |= TG3_FLAG_USE_MI_INTERRUPT;
13079 else
13080 tp->tg3_flags &= ~TG3_FLAG_USE_MI_INTERRUPT;
13081 }
13082
13083 /* 5700 {AX,BX} chips have a broken status block link
13084 * change bit implementation, so we must use the
13085 * status register in those cases.
13086 */
13087 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
13088 tp->tg3_flags |= TG3_FLAG_USE_LINKCHG_REG;
13089 else
13090 tp->tg3_flags &= ~TG3_FLAG_USE_LINKCHG_REG;
13091
13092 /* The led_ctrl is set during tg3_phy_probe, here we might
13093 * have to force the link status polling mechanism based
13094 * upon subsystem IDs.
13095 */
13096 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070013097 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070013098 !(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)) {
13099 tp->tg3_flags |= (TG3_FLAG_USE_MI_INTERRUPT |
13100 TG3_FLAG_USE_LINKCHG_REG);
13101 }
13102
13103 /* For all SERDES we poll the MAC status register. */
13104 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
13105 tp->tg3_flags |= TG3_FLAG_POLL_SERDES;
13106 else
13107 tp->tg3_flags &= ~TG3_FLAG_POLL_SERDES;
13108
Matt Carlsonad829262008-11-21 17:16:16 -080013109 tp->rx_offset = NET_IP_ALIGN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013110 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
13111 (tp->tg3_flags & TG3_FLAG_PCIX_MODE) != 0)
13112 tp->rx_offset = 0;
13113
Michael Chanf92905d2006-06-29 20:14:29 -070013114 tp->rx_std_max_post = TG3_RX_RING_SIZE;
13115
13116 /* Increment the rx prod index on the rx std ring by at most
13117 * 8 for these chips to workaround hw errata.
13118 */
13119 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13120 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
13121 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
13122 tp->rx_std_max_post = 8;
13123
Matt Carlson8ed5d972007-05-07 00:25:49 -070013124 if (tp->tg3_flags & TG3_FLAG_ASPM_WORKAROUND)
13125 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
13126 PCIE_PWR_MGMT_L1_THRESH_MSK;
13127
Linus Torvalds1da177e2005-04-16 15:20:36 -070013128 return err;
13129}
13130
David S. Miller49b6e95f2007-03-29 01:38:42 -070013131#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070013132static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
13133{
13134 struct net_device *dev = tp->dev;
13135 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070013136 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070013137 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070013138 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013139
David S. Miller49b6e95f2007-03-29 01:38:42 -070013140 addr = of_get_property(dp, "local-mac-address", &len);
13141 if (addr && len == 6) {
13142 memcpy(dev->dev_addr, addr, 6);
13143 memcpy(dev->perm_addr, dev->dev_addr, 6);
13144 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013145 }
13146 return -ENODEV;
13147}
13148
13149static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
13150{
13151 struct net_device *dev = tp->dev;
13152
13153 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070013154 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013155 return 0;
13156}
13157#endif
13158
13159static int __devinit tg3_get_device_address(struct tg3 *tp)
13160{
13161 struct net_device *dev = tp->dev;
13162 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080013163 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013164
David S. Miller49b6e95f2007-03-29 01:38:42 -070013165#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070013166 if (!tg3_get_macaddr_sparc(tp))
13167 return 0;
13168#endif
13169
13170 mac_offset = 0x7c;
David S. Millerf49639e2006-06-09 11:58:36 -070013171 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) ||
Michael Chana4e2b342005-10-26 15:46:52 -070013172 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013173 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
13174 mac_offset = 0xcc;
13175 if (tg3_nvram_lock(tp))
13176 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
13177 else
13178 tg3_nvram_unlock(tp);
Matt Carlsona1b950d2009-09-01 13:20:17 +000013179 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
13180 if (tr32(TG3_CPMU_STATUS) & TG3_CPMU_STATUS_PCIE_FUNC)
13181 mac_offset = 0xcc;
13182 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070013183 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013184
13185 /* First try to get it from MAC address mailbox. */
13186 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
13187 if ((hi >> 16) == 0x484b) {
13188 dev->dev_addr[0] = (hi >> 8) & 0xff;
13189 dev->dev_addr[1] = (hi >> 0) & 0xff;
13190
13191 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
13192 dev->dev_addr[2] = (lo >> 24) & 0xff;
13193 dev->dev_addr[3] = (lo >> 16) & 0xff;
13194 dev->dev_addr[4] = (lo >> 8) & 0xff;
13195 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013196
Michael Chan008652b2006-03-27 23:14:53 -080013197 /* Some old bootcode may report a 0 MAC address in SRAM */
13198 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
13199 }
13200 if (!addr_ok) {
13201 /* Next, try NVRAM. */
Matt Carlsondf259d82009-04-20 06:57:14 +000013202 if (!(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) &&
13203 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000013204 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070013205 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
13206 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080013207 }
13208 /* Finally just fetch it out of the MAC control regs. */
13209 else {
13210 hi = tr32(MAC_ADDR_0_HIGH);
13211 lo = tr32(MAC_ADDR_0_LOW);
13212
13213 dev->dev_addr[5] = lo & 0xff;
13214 dev->dev_addr[4] = (lo >> 8) & 0xff;
13215 dev->dev_addr[3] = (lo >> 16) & 0xff;
13216 dev->dev_addr[2] = (lo >> 24) & 0xff;
13217 dev->dev_addr[1] = hi & 0xff;
13218 dev->dev_addr[0] = (hi >> 8) & 0xff;
13219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013220 }
13221
13222 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070013223#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070013224 if (!tg3_get_default_macaddr_sparc(tp))
13225 return 0;
13226#endif
13227 return -EINVAL;
13228 }
John W. Linville2ff43692005-09-12 14:44:20 -070013229 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013230 return 0;
13231}
13232
David S. Miller59e6b432005-05-18 22:50:10 -070013233#define BOUNDARY_SINGLE_CACHELINE 1
13234#define BOUNDARY_MULTI_CACHELINE 2
13235
13236static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
13237{
13238 int cacheline_size;
13239 u8 byte;
13240 int goal;
13241
13242 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
13243 if (byte == 0)
13244 cacheline_size = 1024;
13245 else
13246 cacheline_size = (int) byte * 4;
13247
13248 /* On 5703 and later chips, the boundary bits have no
13249 * effect.
13250 */
13251 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13252 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13253 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
13254 goto out;
13255
13256#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
13257 goal = BOUNDARY_MULTI_CACHELINE;
13258#else
13259#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
13260 goal = BOUNDARY_SINGLE_CACHELINE;
13261#else
13262 goal = 0;
13263#endif
13264#endif
13265
13266 if (!goal)
13267 goto out;
13268
13269 /* PCI controllers on most RISC systems tend to disconnect
13270 * when a device tries to burst across a cache-line boundary.
13271 * Therefore, letting tg3 do so just wastes PCI bandwidth.
13272 *
13273 * Unfortunately, for PCI-E there are only limited
13274 * write-side controls for this, and thus for reads
13275 * we will still get the disconnects. We'll also waste
13276 * these PCI cycles for both read and write for chips
13277 * other than 5700 and 5701 which do not implement the
13278 * boundary bits.
13279 */
13280 if ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) &&
13281 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)) {
13282 switch (cacheline_size) {
13283 case 16:
13284 case 32:
13285 case 64:
13286 case 128:
13287 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13288 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
13289 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
13290 } else {
13291 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
13292 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
13293 }
13294 break;
13295
13296 case 256:
13297 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
13298 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
13299 break;
13300
13301 default:
13302 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
13303 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
13304 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070013305 }
David S. Miller59e6b432005-05-18 22:50:10 -070013306 } else if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
13307 switch (cacheline_size) {
13308 case 16:
13309 case 32:
13310 case 64:
13311 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13312 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
13313 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
13314 break;
13315 }
13316 /* fallthrough */
13317 case 128:
13318 default:
13319 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
13320 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
13321 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070013322 }
David S. Miller59e6b432005-05-18 22:50:10 -070013323 } else {
13324 switch (cacheline_size) {
13325 case 16:
13326 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13327 val |= (DMA_RWCTRL_READ_BNDRY_16 |
13328 DMA_RWCTRL_WRITE_BNDRY_16);
13329 break;
13330 }
13331 /* fallthrough */
13332 case 32:
13333 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13334 val |= (DMA_RWCTRL_READ_BNDRY_32 |
13335 DMA_RWCTRL_WRITE_BNDRY_32);
13336 break;
13337 }
13338 /* fallthrough */
13339 case 64:
13340 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13341 val |= (DMA_RWCTRL_READ_BNDRY_64 |
13342 DMA_RWCTRL_WRITE_BNDRY_64);
13343 break;
13344 }
13345 /* fallthrough */
13346 case 128:
13347 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13348 val |= (DMA_RWCTRL_READ_BNDRY_128 |
13349 DMA_RWCTRL_WRITE_BNDRY_128);
13350 break;
13351 }
13352 /* fallthrough */
13353 case 256:
13354 val |= (DMA_RWCTRL_READ_BNDRY_256 |
13355 DMA_RWCTRL_WRITE_BNDRY_256);
13356 break;
13357 case 512:
13358 val |= (DMA_RWCTRL_READ_BNDRY_512 |
13359 DMA_RWCTRL_WRITE_BNDRY_512);
13360 break;
13361 case 1024:
13362 default:
13363 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
13364 DMA_RWCTRL_WRITE_BNDRY_1024);
13365 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070013366 }
David S. Miller59e6b432005-05-18 22:50:10 -070013367 }
13368
13369out:
13370 return val;
13371}
13372
Linus Torvalds1da177e2005-04-16 15:20:36 -070013373static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
13374{
13375 struct tg3_internal_buffer_desc test_desc;
13376 u32 sram_dma_descs;
13377 int i, ret;
13378
13379 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
13380
13381 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
13382 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
13383 tw32(RDMAC_STATUS, 0);
13384 tw32(WDMAC_STATUS, 0);
13385
13386 tw32(BUFMGR_MODE, 0);
13387 tw32(FTQ_RESET, 0);
13388
13389 test_desc.addr_hi = ((u64) buf_dma) >> 32;
13390 test_desc.addr_lo = buf_dma & 0xffffffff;
13391 test_desc.nic_mbuf = 0x00002100;
13392 test_desc.len = size;
13393
13394 /*
13395 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
13396 * the *second* time the tg3 driver was getting loaded after an
13397 * initial scan.
13398 *
13399 * Broadcom tells me:
13400 * ...the DMA engine is connected to the GRC block and a DMA
13401 * reset may affect the GRC block in some unpredictable way...
13402 * The behavior of resets to individual blocks has not been tested.
13403 *
13404 * Broadcom noted the GRC reset will also reset all sub-components.
13405 */
13406 if (to_device) {
13407 test_desc.cqid_sqid = (13 << 8) | 2;
13408
13409 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
13410 udelay(40);
13411 } else {
13412 test_desc.cqid_sqid = (16 << 8) | 7;
13413
13414 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
13415 udelay(40);
13416 }
13417 test_desc.flags = 0x00000005;
13418
13419 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
13420 u32 val;
13421
13422 val = *(((u32 *)&test_desc) + i);
13423 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
13424 sram_dma_descs + (i * sizeof(u32)));
13425 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
13426 }
13427 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
13428
13429 if (to_device) {
13430 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
13431 } else {
13432 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
13433 }
13434
13435 ret = -ENODEV;
13436 for (i = 0; i < 40; i++) {
13437 u32 val;
13438
13439 if (to_device)
13440 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
13441 else
13442 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
13443 if ((val & 0xffff) == sram_dma_descs) {
13444 ret = 0;
13445 break;
13446 }
13447
13448 udelay(100);
13449 }
13450
13451 return ret;
13452}
13453
David S. Millerded73402005-05-23 13:59:47 -070013454#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070013455
13456static int __devinit tg3_test_dma(struct tg3 *tp)
13457{
13458 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070013459 u32 *buf, saved_dma_rwctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013460 int ret;
13461
13462 buf = pci_alloc_consistent(tp->pdev, TEST_BUFFER_SIZE, &buf_dma);
13463 if (!buf) {
13464 ret = -ENOMEM;
13465 goto out_nofree;
13466 }
13467
13468 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
13469 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
13470
David S. Miller59e6b432005-05-18 22:50:10 -070013471 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013472
13473 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
13474 /* DMA read watermark not used on PCIE */
13475 tp->dma_rwctrl |= 0x00180000;
13476 } else if (!(tp->tg3_flags & TG3_FLAG_PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070013477 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
13478 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013479 tp->dma_rwctrl |= 0x003f0000;
13480 else
13481 tp->dma_rwctrl |= 0x003f000f;
13482 } else {
13483 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
13484 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
13485 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080013486 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013487
Michael Chan4a29cc22006-03-19 13:21:12 -080013488 /* If the 5704 is behind the EPB bridge, we can
13489 * do the less restrictive ONE_DMA workaround for
13490 * better performance.
13491 */
13492 if ((tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG) &&
13493 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
13494 tp->dma_rwctrl |= 0x8000;
13495 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013496 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
13497
Michael Chan49afdeb2007-02-13 12:17:03 -080013498 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
13499 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070013500 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080013501 tp->dma_rwctrl |=
13502 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
13503 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
13504 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070013505 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
13506 /* 5780 always in PCIX mode */
13507 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070013508 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
13509 /* 5714 always in PCIX mode */
13510 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013511 } else {
13512 tp->dma_rwctrl |= 0x001b000f;
13513 }
13514 }
13515
13516 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
13517 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
13518 tp->dma_rwctrl &= 0xfffffff0;
13519
13520 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13521 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
13522 /* Remove this if it causes problems for some boards. */
13523 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
13524
13525 /* On 5700/5701 chips, we need to set this bit.
13526 * Otherwise the chip will issue cacheline transactions
13527 * to streamable DMA memory with not all the byte
13528 * enables turned on. This is an error on several
13529 * RISC PCI controllers, in particular sparc64.
13530 *
13531 * On 5703/5704 chips, this bit has been reassigned
13532 * a different meaning. In particular, it is used
13533 * on those chips to enable a PCI-X workaround.
13534 */
13535 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
13536 }
13537
13538 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
13539
13540#if 0
13541 /* Unneeded, already done by tg3_get_invariants. */
13542 tg3_switch_clocks(tp);
13543#endif
13544
13545 ret = 0;
13546 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13547 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
13548 goto out;
13549
David S. Miller59e6b432005-05-18 22:50:10 -070013550 /* It is best to perform DMA test with maximum write burst size
13551 * to expose the 5700/5701 write DMA bug.
13552 */
13553 saved_dma_rwctrl = tp->dma_rwctrl;
13554 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
13555 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
13556
Linus Torvalds1da177e2005-04-16 15:20:36 -070013557 while (1) {
13558 u32 *p = buf, i;
13559
13560 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
13561 p[i] = i;
13562
13563 /* Send the buffer to the chip. */
13564 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
13565 if (ret) {
13566 printk(KERN_ERR "tg3_test_dma() Write the buffer failed %d\n", ret);
13567 break;
13568 }
13569
13570#if 0
13571 /* validate data reached card RAM correctly. */
13572 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
13573 u32 val;
13574 tg3_read_mem(tp, 0x2100 + (i*4), &val);
13575 if (le32_to_cpu(val) != p[i]) {
13576 printk(KERN_ERR " tg3_test_dma() Card buffer corrupted on write! (%d != %d)\n", val, i);
13577 /* ret = -ENODEV here? */
13578 }
13579 p[i] = 0;
13580 }
13581#endif
13582 /* Now read it back. */
13583 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
13584 if (ret) {
13585 printk(KERN_ERR "tg3_test_dma() Read the buffer failed %d\n", ret);
13586
13587 break;
13588 }
13589
13590 /* Verify it. */
13591 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
13592 if (p[i] == i)
13593 continue;
13594
David S. Miller59e6b432005-05-18 22:50:10 -070013595 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
13596 DMA_RWCTRL_WRITE_BNDRY_16) {
13597 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013598 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
13599 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
13600 break;
13601 } else {
13602 printk(KERN_ERR "tg3_test_dma() buffer corrupted on read back! (%d != %d)\n", p[i], i);
13603 ret = -ENODEV;
13604 goto out;
13605 }
13606 }
13607
13608 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
13609 /* Success. */
13610 ret = 0;
13611 break;
13612 }
13613 }
David S. Miller59e6b432005-05-18 22:50:10 -070013614 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
13615 DMA_RWCTRL_WRITE_BNDRY_16) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070013616 static struct pci_device_id dma_wait_state_chipsets[] = {
13617 { PCI_DEVICE(PCI_VENDOR_ID_APPLE,
13618 PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
13619 { },
13620 };
13621
David S. Miller59e6b432005-05-18 22:50:10 -070013622 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070013623 * now look for chipsets that are known to expose the
13624 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070013625 */
Michael Chan6d1cfba2005-06-08 14:13:14 -070013626 if (pci_dev_present(dma_wait_state_chipsets)) {
13627 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
13628 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
13629 }
13630 else
13631 /* Safe to use the calculated DMA boundary. */
13632 tp->dma_rwctrl = saved_dma_rwctrl;
13633
David S. Miller59e6b432005-05-18 22:50:10 -070013634 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
13635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013636
13637out:
13638 pci_free_consistent(tp->pdev, TEST_BUFFER_SIZE, buf, buf_dma);
13639out_nofree:
13640 return ret;
13641}
13642
13643static void __devinit tg3_init_link_config(struct tg3 *tp)
13644{
13645 tp->link_config.advertising =
13646 (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
13647 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
13648 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full |
13649 ADVERTISED_Autoneg | ADVERTISED_MII);
13650 tp->link_config.speed = SPEED_INVALID;
13651 tp->link_config.duplex = DUPLEX_INVALID;
13652 tp->link_config.autoneg = AUTONEG_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013653 tp->link_config.active_speed = SPEED_INVALID;
13654 tp->link_config.active_duplex = DUPLEX_INVALID;
13655 tp->link_config.phy_is_low_power = 0;
13656 tp->link_config.orig_speed = SPEED_INVALID;
13657 tp->link_config.orig_duplex = DUPLEX_INVALID;
13658 tp->link_config.orig_autoneg = AUTONEG_INVALID;
13659}
13660
13661static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
13662{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013663 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS &&
13664 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717) {
Michael Chanfdfec1722005-07-25 12:31:48 -070013665 tp->bufmgr_config.mbuf_read_dma_low_water =
13666 DEFAULT_MB_RDMA_LOW_WATER_5705;
13667 tp->bufmgr_config.mbuf_mac_rx_low_water =
13668 DEFAULT_MB_MACRX_LOW_WATER_5705;
13669 tp->bufmgr_config.mbuf_high_water =
13670 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070013671 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
13672 tp->bufmgr_config.mbuf_mac_rx_low_water =
13673 DEFAULT_MB_MACRX_LOW_WATER_5906;
13674 tp->bufmgr_config.mbuf_high_water =
13675 DEFAULT_MB_HIGH_WATER_5906;
13676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013677
Michael Chanfdfec1722005-07-25 12:31:48 -070013678 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
13679 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
13680 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
13681 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
13682 tp->bufmgr_config.mbuf_high_water_jumbo =
13683 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
13684 } else {
13685 tp->bufmgr_config.mbuf_read_dma_low_water =
13686 DEFAULT_MB_RDMA_LOW_WATER;
13687 tp->bufmgr_config.mbuf_mac_rx_low_water =
13688 DEFAULT_MB_MACRX_LOW_WATER;
13689 tp->bufmgr_config.mbuf_high_water =
13690 DEFAULT_MB_HIGH_WATER;
13691
13692 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
13693 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
13694 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
13695 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
13696 tp->bufmgr_config.mbuf_high_water_jumbo =
13697 DEFAULT_MB_HIGH_WATER_JUMBO;
13698 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013699
13700 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
13701 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
13702}
13703
13704static char * __devinit tg3_phy_string(struct tg3 *tp)
13705{
13706 switch (tp->phy_id & PHY_ID_MASK) {
13707 case PHY_ID_BCM5400: return "5400";
13708 case PHY_ID_BCM5401: return "5401";
13709 case PHY_ID_BCM5411: return "5411";
13710 case PHY_ID_BCM5701: return "5701";
13711 case PHY_ID_BCM5703: return "5703";
13712 case PHY_ID_BCM5704: return "5704";
13713 case PHY_ID_BCM5705: return "5705";
13714 case PHY_ID_BCM5750: return "5750";
Michael Chan85e94ce2005-04-21 17:05:28 -070013715 case PHY_ID_BCM5752: return "5752";
Michael Chana4e2b342005-10-26 15:46:52 -070013716 case PHY_ID_BCM5714: return "5714";
Michael Chan4cf78e42005-07-25 12:29:19 -070013717 case PHY_ID_BCM5780: return "5780";
Michael Chanaf36e6b2006-03-23 01:28:06 -080013718 case PHY_ID_BCM5755: return "5755";
Michael Chand9ab5ad2006-03-20 22:27:35 -080013719 case PHY_ID_BCM5787: return "5787";
Matt Carlsond30cdd22007-10-07 23:28:35 -070013720 case PHY_ID_BCM5784: return "5784";
Michael Chan126a3362006-09-27 16:03:07 -070013721 case PHY_ID_BCM5756: return "5722/5756";
Michael Chanb5d37722006-09-27 16:06:21 -070013722 case PHY_ID_BCM5906: return "5906";
Matt Carlson9936bcf2007-10-10 18:03:07 -070013723 case PHY_ID_BCM5761: return "5761";
Linus Torvalds1da177e2005-04-16 15:20:36 -070013724 case PHY_ID_BCM8002: return "8002/serdes";
13725 case 0: return "serdes";
13726 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070013727 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013728}
13729
Michael Chanf9804dd2005-09-27 12:13:10 -070013730static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
13731{
13732 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
13733 strcpy(str, "PCI Express");
13734 return str;
13735 } else if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
13736 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
13737
13738 strcpy(str, "PCIX:");
13739
13740 if ((clock_ctrl == 7) ||
13741 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
13742 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
13743 strcat(str, "133MHz");
13744 else if (clock_ctrl == 0)
13745 strcat(str, "33MHz");
13746 else if (clock_ctrl == 2)
13747 strcat(str, "50MHz");
13748 else if (clock_ctrl == 4)
13749 strcat(str, "66MHz");
13750 else if (clock_ctrl == 6)
13751 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070013752 } else {
13753 strcpy(str, "PCI:");
13754 if (tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED)
13755 strcat(str, "66MHz");
13756 else
13757 strcat(str, "33MHz");
13758 }
13759 if (tp->tg3_flags & TG3_FLAG_PCI_32BIT)
13760 strcat(str, ":32-bit");
13761 else
13762 strcat(str, ":64-bit");
13763 return str;
13764}
13765
Michael Chan8c2dc7e2005-12-19 16:26:02 -080013766static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013767{
13768 struct pci_dev *peer;
13769 unsigned int func, devnr = tp->pdev->devfn & ~7;
13770
13771 for (func = 0; func < 8; func++) {
13772 peer = pci_get_slot(tp->pdev->bus, devnr | func);
13773 if (peer && peer != tp->pdev)
13774 break;
13775 pci_dev_put(peer);
13776 }
Michael Chan16fe9d72005-12-13 21:09:54 -080013777 /* 5704 can be configured in single-port mode, set peer to
13778 * tp->pdev in that case.
13779 */
13780 if (!peer) {
13781 peer = tp->pdev;
13782 return peer;
13783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013784
13785 /*
13786 * We don't need to keep the refcount elevated; there's no way
13787 * to remove one half of this device without removing the other
13788 */
13789 pci_dev_put(peer);
13790
13791 return peer;
13792}
13793
David S. Miller15f98502005-05-18 22:49:26 -070013794static void __devinit tg3_init_coal(struct tg3 *tp)
13795{
13796 struct ethtool_coalesce *ec = &tp->coal;
13797
13798 memset(ec, 0, sizeof(*ec));
13799 ec->cmd = ETHTOOL_GCOALESCE;
13800 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
13801 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
13802 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
13803 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
13804 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
13805 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
13806 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
13807 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
13808 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
13809
13810 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
13811 HOSTCC_MODE_CLRTICK_TXBD)) {
13812 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
13813 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
13814 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
13815 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
13816 }
Michael Chand244c892005-07-05 14:42:33 -070013817
13818 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
13819 ec->rx_coalesce_usecs_irq = 0;
13820 ec->tx_coalesce_usecs_irq = 0;
13821 ec->stats_block_coalesce_usecs = 0;
13822 }
David S. Miller15f98502005-05-18 22:49:26 -070013823}
13824
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080013825static const struct net_device_ops tg3_netdev_ops = {
13826 .ndo_open = tg3_open,
13827 .ndo_stop = tg3_close,
Stephen Hemminger00829822008-11-20 20:14:53 -080013828 .ndo_start_xmit = tg3_start_xmit,
13829 .ndo_get_stats = tg3_get_stats,
13830 .ndo_validate_addr = eth_validate_addr,
13831 .ndo_set_multicast_list = tg3_set_rx_mode,
13832 .ndo_set_mac_address = tg3_set_mac_addr,
13833 .ndo_do_ioctl = tg3_ioctl,
13834 .ndo_tx_timeout = tg3_tx_timeout,
13835 .ndo_change_mtu = tg3_change_mtu,
13836#if TG3_VLAN_TAG_USED
13837 .ndo_vlan_rx_register = tg3_vlan_rx_register,
13838#endif
13839#ifdef CONFIG_NET_POLL_CONTROLLER
13840 .ndo_poll_controller = tg3_poll_controller,
13841#endif
13842};
13843
13844static const struct net_device_ops tg3_netdev_ops_dma_bug = {
13845 .ndo_open = tg3_open,
13846 .ndo_stop = tg3_close,
13847 .ndo_start_xmit = tg3_start_xmit_dma_bug,
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080013848 .ndo_get_stats = tg3_get_stats,
13849 .ndo_validate_addr = eth_validate_addr,
13850 .ndo_set_multicast_list = tg3_set_rx_mode,
13851 .ndo_set_mac_address = tg3_set_mac_addr,
13852 .ndo_do_ioctl = tg3_ioctl,
13853 .ndo_tx_timeout = tg3_tx_timeout,
13854 .ndo_change_mtu = tg3_change_mtu,
13855#if TG3_VLAN_TAG_USED
13856 .ndo_vlan_rx_register = tg3_vlan_rx_register,
13857#endif
13858#ifdef CONFIG_NET_POLL_CONTROLLER
13859 .ndo_poll_controller = tg3_poll_controller,
13860#endif
13861};
13862
Linus Torvalds1da177e2005-04-16 15:20:36 -070013863static int __devinit tg3_init_one(struct pci_dev *pdev,
13864 const struct pci_device_id *ent)
13865{
13866 static int tg3_version_printed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013867 struct net_device *dev;
13868 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000013869 int i, err, pm_cap;
13870 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070013871 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080013872 u64 dma_mask, persist_dma_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013873
13874 if (tg3_version_printed++ == 0)
13875 printk(KERN_INFO "%s", version);
13876
13877 err = pci_enable_device(pdev);
13878 if (err) {
13879 printk(KERN_ERR PFX "Cannot enable PCI device, "
13880 "aborting.\n");
13881 return err;
13882 }
13883
Linus Torvalds1da177e2005-04-16 15:20:36 -070013884 err = pci_request_regions(pdev, DRV_MODULE_NAME);
13885 if (err) {
13886 printk(KERN_ERR PFX "Cannot obtain PCI resources, "
13887 "aborting.\n");
13888 goto err_out_disable_pdev;
13889 }
13890
13891 pci_set_master(pdev);
13892
13893 /* Find power-management capability. */
13894 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
13895 if (pm_cap == 0) {
13896 printk(KERN_ERR PFX "Cannot find PowerManagement capability, "
13897 "aborting.\n");
13898 err = -EIO;
13899 goto err_out_free_res;
13900 }
13901
Matt Carlsonfe5f5782009-09-01 13:09:39 +000013902 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013903 if (!dev) {
13904 printk(KERN_ERR PFX "Etherdev alloc failed, aborting.\n");
13905 err = -ENOMEM;
13906 goto err_out_free_res;
13907 }
13908
Linus Torvalds1da177e2005-04-16 15:20:36 -070013909 SET_NETDEV_DEV(dev, &pdev->dev);
13910
Linus Torvalds1da177e2005-04-16 15:20:36 -070013911#if TG3_VLAN_TAG_USED
13912 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013913#endif
13914
13915 tp = netdev_priv(dev);
13916 tp->pdev = pdev;
13917 tp->dev = dev;
13918 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013919 tp->rx_mode = TG3_DEF_RX_MODE;
13920 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070013921
Linus Torvalds1da177e2005-04-16 15:20:36 -070013922 if (tg3_debug > 0)
13923 tp->msg_enable = tg3_debug;
13924 else
13925 tp->msg_enable = TG3_DEF_MSG_ENABLE;
13926
13927 /* The word/byte swap controls here control register access byte
13928 * swapping. DMA data byte swapping is controlled in the GRC_MODE
13929 * setting below.
13930 */
13931 tp->misc_host_ctrl =
13932 MISC_HOST_CTRL_MASK_PCI_INT |
13933 MISC_HOST_CTRL_WORD_SWAP |
13934 MISC_HOST_CTRL_INDIR_ACCESS |
13935 MISC_HOST_CTRL_PCISTATE_RW;
13936
13937 /* The NONFRM (non-frame) byte/word swap controls take effect
13938 * on descriptor entries, anything which isn't packet data.
13939 *
13940 * The StrongARM chips on the board (one for tx, one for rx)
13941 * are running in big-endian mode.
13942 */
13943 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
13944 GRC_MODE_WSWAP_NONFRM_DATA);
13945#ifdef __BIG_ENDIAN
13946 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
13947#endif
13948 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013949 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000013950 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013951
Matt Carlsond5fe4882008-11-21 17:20:32 -080013952 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010013953 if (!tp->regs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013954 printk(KERN_ERR PFX "Cannot map device registers, "
13955 "aborting.\n");
13956 err = -ENOMEM;
13957 goto err_out_free_dev;
13958 }
13959
13960 tg3_init_link_config(tp);
13961
Linus Torvalds1da177e2005-04-16 15:20:36 -070013962 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
13963 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013964
Matt Carlson646c9ed2009-09-01 12:58:41 +000013965 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
13966 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
13967 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
13968 for (i = 0; i < TG3_IRQ_MAX_VECS; i++) {
13969 struct tg3_napi *tnapi = &tp->napi[i];
13970
13971 tnapi->tp = tp;
13972 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
13973
13974 tnapi->int_mbox = intmbx;
13975 if (i < 4)
13976 intmbx += 0x8;
13977 else
13978 intmbx += 0x4;
13979
13980 tnapi->consmbox = rcvmbx;
13981 tnapi->prodmbox = sndmbx;
13982
13983 if (i)
13984 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
13985 else
13986 tnapi->coal_now = HOSTCC_MODE_NOW;
13987
13988 if (!(tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX))
13989 break;
13990
13991 /*
13992 * If we support MSIX, we'll be using RSS. If we're using
13993 * RSS, the first vector only handles link interrupts and the
13994 * remaining vectors handle rx and tx interrupts. Reuse the
13995 * mailbox values for the next iteration. The values we setup
13996 * above are still useful for the single vectored mode.
13997 */
13998 if (!i)
13999 continue;
14000
14001 rcvmbx += 0x8;
14002
14003 if (sndmbx & 0x4)
14004 sndmbx -= 0x4;
14005 else
14006 sndmbx += 0xc;
14007 }
14008
Matt Carlson8ef04422009-08-28 14:01:37 +000014009 netif_napi_add(dev, &tp->napi[0].napi, tg3_poll, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014010 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014011 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014012 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014013
14014 err = tg3_get_invariants(tp);
14015 if (err) {
14016 printk(KERN_ERR PFX "Problem fetching invariants of chip, "
14017 "aborting.\n");
14018 goto err_out_iounmap;
14019 }
14020
Matt Carlson92c6b8d2009-11-02 14:23:27 +000014021 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Stephen Hemminger00829822008-11-20 20:14:53 -080014022 dev->netdev_ops = &tg3_netdev_ops;
14023 else
14024 dev->netdev_ops = &tg3_netdev_ops_dma_bug;
14025
14026
Michael Chan4a29cc22006-03-19 13:21:12 -080014027 /* The EPB bridge inside 5714, 5715, and 5780 and any
14028 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080014029 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
14030 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
14031 * do DMA address check in tg3_start_xmit().
14032 */
Michael Chan4a29cc22006-03-19 13:21:12 -080014033 if (tp->tg3_flags2 & TG3_FLG2_IS_5788)
Yang Hongyang284901a2009-04-06 19:01:15 -070014034 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Michael Chan4a29cc22006-03-19 13:21:12 -080014035 else if (tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070014036 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080014037#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070014038 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080014039#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080014040 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070014041 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080014042
14043 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070014044 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080014045 err = pci_set_dma_mask(pdev, dma_mask);
14046 if (!err) {
14047 dev->features |= NETIF_F_HIGHDMA;
14048 err = pci_set_consistent_dma_mask(pdev,
14049 persist_dma_mask);
14050 if (err < 0) {
14051 printk(KERN_ERR PFX "Unable to obtain 64 bit "
14052 "DMA for consistent allocations\n");
14053 goto err_out_iounmap;
14054 }
14055 }
14056 }
Yang Hongyang284901a2009-04-06 19:01:15 -070014057 if (err || dma_mask == DMA_BIT_MASK(32)) {
14058 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080014059 if (err) {
14060 printk(KERN_ERR PFX "No usable DMA configuration, "
14061 "aborting.\n");
14062 goto err_out_iounmap;
14063 }
14064 }
14065
Michael Chanfdfec1722005-07-25 12:31:48 -070014066 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014067
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014068 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
Matt Carlson9e9fd122009-01-19 16:57:45 -080014069 tp->fw_needed = FIRMWARE_TG3;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014070
Linus Torvalds1da177e2005-04-16 15:20:36 -070014071 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO) {
14072 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
14073 }
14074 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14075 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
14076 tp->pci_chip_rev_id == CHIPREV_ID_5705_A0 ||
Michael Chanc7835a72006-11-15 21:14:42 -080014077 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -070014078 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0) {
14079 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
14080 } else {
Michael Chan7f62ad52007-02-20 23:25:40 -080014081 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE | TG3_FLG2_TSO_BUG;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014082 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
Matt Carlson9e9fd122009-01-19 16:57:45 -080014083 tp->fw_needed = FIRMWARE_TG3TSO5;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014084 else
Matt Carlson9e9fd122009-01-19 16:57:45 -080014085 tp->fw_needed = FIRMWARE_TG3TSO;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014087
Michael Chan4e3a7aa2006-03-20 17:47:44 -080014088 /* TSO is on by default on chips that support hardware TSO.
14089 * Firmware TSO on older chips gives lower performance, so it
14090 * is off by default, but can be enabled using ethtool.
14091 */
Michael Chanb0026622006-07-03 19:42:14 -070014092 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO) {
Matt Carlson027455a2008-12-21 20:19:30 -080014093 if (dev->features & NETIF_F_IP_CSUM)
14094 dev->features |= NETIF_F_TSO;
14095 if ((dev->features & NETIF_F_IPV6_CSUM) &&
14096 (tp->tg3_flags2 & TG3_FLG2_HW_TSO_2))
Michael Chanb0026622006-07-03 19:42:14 -070014097 dev->features |= NETIF_F_TSO6;
Matt Carlson57e69832008-05-25 23:48:31 -070014098 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
14099 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
14100 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014101 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014102 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
14103 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson9936bcf2007-10-10 18:03:07 -070014104 dev->features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070014105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014106
Linus Torvalds1da177e2005-04-16 15:20:36 -070014107
14108 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
14109 !(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) &&
14110 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
14111 tp->tg3_flags2 |= TG3_FLG2_MAX_RXPEND_64;
14112 tp->rx_pending = 63;
14113 }
14114
Linus Torvalds1da177e2005-04-16 15:20:36 -070014115 err = tg3_get_device_address(tp);
14116 if (err) {
14117 printk(KERN_ERR PFX "Could not obtain valid ethernet address, "
14118 "aborting.\n");
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014119 goto err_out_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014120 }
14121
Matt Carlson0d3031d2007-10-10 18:02:43 -070014122 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
Matt Carlson63532392008-11-03 16:49:57 -080014123 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
Al Viro79ea13c2008-01-24 02:06:46 -080014124 if (!tp->aperegs) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070014125 printk(KERN_ERR PFX "Cannot map APE registers, "
14126 "aborting.\n");
14127 err = -ENOMEM;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014128 goto err_out_fw;
Matt Carlson0d3031d2007-10-10 18:02:43 -070014129 }
14130
14131 tg3_ape_lock_init(tp);
Matt Carlson7fd76442009-02-25 14:27:20 +000014132
14133 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF)
14134 tg3_read_dash_ver(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070014135 }
14136
Matt Carlsonc88864d2007-11-12 21:07:01 -080014137 /*
14138 * Reset chip in case UNDI or EFI driver did not shutdown
14139 * DMA self test will enable WDMAC and we'll see (spurious)
14140 * pending DMA on the PCI bus at that point.
14141 */
14142 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
14143 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
14144 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
14145 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
14146 }
14147
14148 err = tg3_test_dma(tp);
14149 if (err) {
14150 printk(KERN_ERR PFX "DMA engine test failed, aborting.\n");
14151 goto err_out_apeunmap;
14152 }
14153
Matt Carlsonc88864d2007-11-12 21:07:01 -080014154 /* flow control autonegotiation is default behavior */
14155 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
Steve Glendinninge18ce342008-12-16 02:00:00 -080014156 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlsonc88864d2007-11-12 21:07:01 -080014157
14158 tg3_init_coal(tp);
14159
Michael Chanc49a1562006-12-17 17:07:29 -080014160 pci_set_drvdata(pdev, dev);
14161
Linus Torvalds1da177e2005-04-16 15:20:36 -070014162 err = register_netdev(dev);
14163 if (err) {
14164 printk(KERN_ERR PFX "Cannot register net device, "
14165 "aborting.\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070014166 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014167 }
14168
Matt Carlsondf59c942008-11-03 16:52:56 -080014169 printk(KERN_INFO "%s: Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070014170 dev->name,
14171 tp->board_part_number,
14172 tp->pci_chip_rev_id,
Michael Chanf9804dd2005-09-27 12:13:10 -070014173 tg3_bus_string(tp, str),
Johannes Berge1749612008-10-27 15:59:26 -070014174 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014175
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000014176 if (tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED) {
14177 struct phy_device *phydev;
14178 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsondf59c942008-11-03 16:52:56 -080014179 printk(KERN_INFO
14180 "%s: attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000014181 tp->dev->name, phydev->drv->name,
14182 dev_name(&phydev->dev));
14183 } else
Matt Carlsondf59c942008-11-03 16:52:56 -080014184 printk(KERN_INFO
14185 "%s: attached PHY is %s (%s Ethernet) (WireSpeed[%d])\n",
14186 tp->dev->name, tg3_phy_string(tp),
14187 ((tp->tg3_flags & TG3_FLAG_10_100_ONLY) ? "10/100Base-TX" :
14188 ((tp->tg3_flags2 & TG3_FLG2_ANY_SERDES) ? "1000Base-SX" :
14189 "10/100/1000Base-T")),
14190 (tp->tg3_flags2 & TG3_FLG2_NO_ETH_WIRE_SPEED) == 0);
14191
14192 printk(KERN_INFO "%s: RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070014193 dev->name,
14194 (tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) != 0,
14195 (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) != 0,
14196 (tp->tg3_flags & TG3_FLAG_USE_MI_INTERRUPT) != 0,
14197 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014198 (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) != 0);
Michael Chan4a29cc22006-03-19 13:21:12 -080014199 printk(KERN_INFO "%s: dma_rwctrl[%08x] dma_mask[%d-bit]\n",
14200 dev->name, tp->dma_rwctrl,
Yang Hongyang284901a2009-04-06 19:01:15 -070014201 (pdev->dma_mask == DMA_BIT_MASK(32)) ? 32 :
Yang Hongyang50cf1562009-04-06 19:01:14 -070014202 (((u64) pdev->dma_mask == DMA_BIT_MASK(40)) ? 40 : 64));
Linus Torvalds1da177e2005-04-16 15:20:36 -070014203
14204 return 0;
14205
Matt Carlson0d3031d2007-10-10 18:02:43 -070014206err_out_apeunmap:
14207 if (tp->aperegs) {
14208 iounmap(tp->aperegs);
14209 tp->aperegs = NULL;
14210 }
14211
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014212err_out_fw:
14213 if (tp->fw)
14214 release_firmware(tp->fw);
14215
Linus Torvalds1da177e2005-04-16 15:20:36 -070014216err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070014217 if (tp->regs) {
14218 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070014219 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070014220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014221
14222err_out_free_dev:
14223 free_netdev(dev);
14224
14225err_out_free_res:
14226 pci_release_regions(pdev);
14227
14228err_out_disable_pdev:
14229 pci_disable_device(pdev);
14230 pci_set_drvdata(pdev, NULL);
14231 return err;
14232}
14233
14234static void __devexit tg3_remove_one(struct pci_dev *pdev)
14235{
14236 struct net_device *dev = pci_get_drvdata(pdev);
14237
14238 if (dev) {
14239 struct tg3 *tp = netdev_priv(dev);
14240
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014241 if (tp->fw)
14242 release_firmware(tp->fw);
14243
Michael Chan7faa0062006-02-02 17:29:28 -080014244 flush_scheduled_work();
Matt Carlson158d7ab2008-05-29 01:37:54 -070014245
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014246 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
14247 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070014248 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014249 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070014250
Linus Torvalds1da177e2005-04-16 15:20:36 -070014251 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070014252 if (tp->aperegs) {
14253 iounmap(tp->aperegs);
14254 tp->aperegs = NULL;
14255 }
Michael Chan68929142005-08-09 20:17:14 -070014256 if (tp->regs) {
14257 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070014258 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070014259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014260 free_netdev(dev);
14261 pci_release_regions(pdev);
14262 pci_disable_device(pdev);
14263 pci_set_drvdata(pdev, NULL);
14264 }
14265}
14266
14267static int tg3_suspend(struct pci_dev *pdev, pm_message_t state)
14268{
14269 struct net_device *dev = pci_get_drvdata(pdev);
14270 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070014271 pci_power_t target_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014272 int err;
14273
Michael Chan3e0c95f2007-08-03 20:56:54 -070014274 /* PCI register 4 needs to be saved whether netif_running() or not.
14275 * MSI address and data need to be saved if using MSI and
14276 * netif_running().
14277 */
14278 pci_save_state(pdev);
14279
Linus Torvalds1da177e2005-04-16 15:20:36 -070014280 if (!netif_running(dev))
14281 return 0;
14282
Michael Chan7faa0062006-02-02 17:29:28 -080014283 flush_scheduled_work();
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014284 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014285 tg3_netif_stop(tp);
14286
14287 del_timer_sync(&tp->timer);
14288
David S. Millerf47c11e2005-06-24 20:18:35 -070014289 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014290 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070014291 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014292
14293 netif_device_detach(dev);
14294
David S. Millerf47c11e2005-06-24 20:18:35 -070014295 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070014296 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan6a9eba12005-12-13 21:08:58 -080014297 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
David S. Millerf47c11e2005-06-24 20:18:35 -070014298 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014299
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070014300 target_state = pdev->pm_cap ? pci_target_state(pdev) : PCI_D3hot;
14301
14302 err = tg3_set_power_state(tp, target_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014303 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014304 int err2;
14305
David S. Millerf47c11e2005-06-24 20:18:35 -070014306 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014307
Michael Chan6a9eba12005-12-13 21:08:58 -080014308 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014309 err2 = tg3_restart_hw(tp, 1);
14310 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070014311 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014312
14313 tp->timer.expires = jiffies + tp->timer_offset;
14314 add_timer(&tp->timer);
14315
14316 netif_device_attach(dev);
14317 tg3_netif_start(tp);
14318
Michael Chanb9ec6c12006-07-25 16:37:27 -070014319out:
David S. Millerf47c11e2005-06-24 20:18:35 -070014320 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014321
14322 if (!err2)
14323 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014324 }
14325
14326 return err;
14327}
14328
14329static int tg3_resume(struct pci_dev *pdev)
14330{
14331 struct net_device *dev = pci_get_drvdata(pdev);
14332 struct tg3 *tp = netdev_priv(dev);
14333 int err;
14334
Michael Chan3e0c95f2007-08-03 20:56:54 -070014335 pci_restore_state(tp->pdev);
14336
Linus Torvalds1da177e2005-04-16 15:20:36 -070014337 if (!netif_running(dev))
14338 return 0;
14339
Michael Chanbc1c7562006-03-20 17:48:03 -080014340 err = tg3_set_power_state(tp, PCI_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014341 if (err)
14342 return err;
14343
14344 netif_device_attach(dev);
14345
David S. Millerf47c11e2005-06-24 20:18:35 -070014346 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014347
Michael Chan6a9eba12005-12-13 21:08:58 -080014348 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Michael Chanb9ec6c12006-07-25 16:37:27 -070014349 err = tg3_restart_hw(tp, 1);
14350 if (err)
14351 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014352
14353 tp->timer.expires = jiffies + tp->timer_offset;
14354 add_timer(&tp->timer);
14355
Linus Torvalds1da177e2005-04-16 15:20:36 -070014356 tg3_netif_start(tp);
14357
Michael Chanb9ec6c12006-07-25 16:37:27 -070014358out:
David S. Millerf47c11e2005-06-24 20:18:35 -070014359 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014360
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014361 if (!err)
14362 tg3_phy_start(tp);
14363
Michael Chanb9ec6c12006-07-25 16:37:27 -070014364 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014365}
14366
14367static struct pci_driver tg3_driver = {
14368 .name = DRV_MODULE_NAME,
14369 .id_table = tg3_pci_tbl,
14370 .probe = tg3_init_one,
14371 .remove = __devexit_p(tg3_remove_one),
14372 .suspend = tg3_suspend,
14373 .resume = tg3_resume
14374};
14375
14376static int __init tg3_init(void)
14377{
Jeff Garzik29917622006-08-19 17:48:59 -040014378 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014379}
14380
14381static void __exit tg3_cleanup(void)
14382{
14383 pci_unregister_driver(&tg3_driver);
14384}
14385
14386module_init(tg3_init);
14387module_exit(tg3_cleanup);