blob: b01ea2e9ab5f38489012437ef2018f099fc8ea99 [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 Carlson123b43e2009-11-02 14:33:03 +000071#define DRV_MODULE_VERSION "3.103"
72#define DRV_MODULE_RELDATE "November 2, 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
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000140#define TG3_RX_STD_BUFF_RING_SIZE \
141 (sizeof(struct ring_info) * TG3_RX_RING_SIZE)
142
143#define TG3_RX_JMB_BUFF_RING_SIZE \
144 (sizeof(struct ring_info) * TG3_RX_JUMBO_RING_SIZE)
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146/* minimum number of free TX descriptors required to wake up TX process */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000147#define TG3_TX_WAKEUP_THRESH(tnapi) ((tnapi)->tx_pending / 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Matt Carlsonad829262008-11-21 17:16:16 -0800149#define TG3_RAW_IP_ALIGN 2
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151/* number of ETHTOOL_GSTATS u64's */
152#define TG3_NUM_STATS (sizeof(struct tg3_ethtool_stats)/sizeof(u64))
153
Michael Chan4cafd3f2005-05-29 14:56:34 -0700154#define TG3_NUM_TEST 6
155
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800156#define FIRMWARE_TG3 "tigon/tg3.bin"
157#define FIRMWARE_TG3TSO "tigon/tg3_tso.bin"
158#define FIRMWARE_TG3TSO5 "tigon/tg3_tso5.bin"
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160static char version[] __devinitdata =
161 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
162
163MODULE_AUTHOR("David S. Miller (davem@redhat.com) and Jeff Garzik (jgarzik@pobox.com)");
164MODULE_DESCRIPTION("Broadcom Tigon3 ethernet driver");
165MODULE_LICENSE("GPL");
166MODULE_VERSION(DRV_MODULE_VERSION);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800167MODULE_FIRMWARE(FIRMWARE_TG3);
168MODULE_FIRMWARE(FIRMWARE_TG3TSO);
169MODULE_FIRMWARE(FIRMWARE_TG3TSO5);
170
Matt Carlson679563f2009-09-01 12:55:46 +0000171#define TG3_RSS_MIN_NUM_MSIX_VECS 2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173static int tg3_debug = -1; /* -1 == use TG3_DEF_MSG_ENABLE as value */
174module_param(tg3_debug, int, 0);
175MODULE_PARM_DESC(tg3_debug, "Tigon3 bitmapped debugging message enable value");
176
177static struct pci_device_id tg3_pci_tbl[] = {
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700178 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5700)},
179 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5701)},
180 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702)},
181 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703)},
182 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704)},
183 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702FE)},
184 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705)},
185 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705_2)},
186 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M)},
187 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M_2)},
188 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702X)},
189 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703X)},
190 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S)},
191 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702A3)},
192 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703A3)},
193 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5782)},
194 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5788)},
195 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5789)},
196 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901)},
197 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901_2)},
198 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S_2)},
199 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705F)},
200 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5720)},
201 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5721)},
Michael Chan126a3362006-09-27 16:03:07 -0700202 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5722)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700203 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5750)},
204 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751)},
205 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5750M)},
206 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751M)},
207 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751F)},
208 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752)},
209 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752M)},
210 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753)},
211 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753M)},
212 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753F)},
213 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754)},
214 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754M)},
215 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755)},
216 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755M)},
Michael Chan126a3362006-09-27 16:03:07 -0700217 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5756)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700218 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5786)},
219 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787)},
220 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787M)},
Michael Chan676917d2006-12-07 00:20:22 -0800221 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787F)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700222 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714)},
223 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714S)},
224 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715)},
225 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715S)},
226 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780)},
227 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780S)},
228 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5781)},
Michael Chanb5d37722006-09-27 16:06:21 -0700229 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906)},
230 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906M)},
Matt Carlsond30cdd22007-10-07 23:28:35 -0700231 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5784)},
232 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5764)},
Matt Carlson6c7af272007-10-21 16:12:02 -0700233 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5723)},
Matt Carlson9936bcf2007-10-10 18:03:07 -0700234 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761)},
235 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761E)},
Matt Carlsonc88e6682008-11-03 16:49:18 -0800236 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761S)},
237 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761SE)},
Matt Carlson2befdce2009-08-28 12:28:45 +0000238 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_G)},
239 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_F)},
Matt Carlson321d32a2008-11-21 17:22:19 -0800240 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780)},
241 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57760)},
242 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57790)},
Matt Carlson5e7ccf22009-08-25 10:08:42 +0000243 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57788)},
Matt Carlson5001e2f2009-11-13 13:03:51 +0000244 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717)},
245 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5718)},
246 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5724)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700247 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9DXX)},
248 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9MXX)},
249 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1000)},
250 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1001)},
251 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1003)},
252 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC9100)},
253 {PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_TIGON3)},
254 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255};
256
257MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
258
Andreas Mohr50da8592006-08-14 23:54:30 -0700259static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 const char string[ETH_GSTRING_LEN];
261} ethtool_stats_keys[TG3_NUM_STATS] = {
262 { "rx_octets" },
263 { "rx_fragments" },
264 { "rx_ucast_packets" },
265 { "rx_mcast_packets" },
266 { "rx_bcast_packets" },
267 { "rx_fcs_errors" },
268 { "rx_align_errors" },
269 { "rx_xon_pause_rcvd" },
270 { "rx_xoff_pause_rcvd" },
271 { "rx_mac_ctrl_rcvd" },
272 { "rx_xoff_entered" },
273 { "rx_frame_too_long_errors" },
274 { "rx_jabbers" },
275 { "rx_undersize_packets" },
276 { "rx_in_length_errors" },
277 { "rx_out_length_errors" },
278 { "rx_64_or_less_octet_packets" },
279 { "rx_65_to_127_octet_packets" },
280 { "rx_128_to_255_octet_packets" },
281 { "rx_256_to_511_octet_packets" },
282 { "rx_512_to_1023_octet_packets" },
283 { "rx_1024_to_1522_octet_packets" },
284 { "rx_1523_to_2047_octet_packets" },
285 { "rx_2048_to_4095_octet_packets" },
286 { "rx_4096_to_8191_octet_packets" },
287 { "rx_8192_to_9022_octet_packets" },
288
289 { "tx_octets" },
290 { "tx_collisions" },
291
292 { "tx_xon_sent" },
293 { "tx_xoff_sent" },
294 { "tx_flow_control" },
295 { "tx_mac_errors" },
296 { "tx_single_collisions" },
297 { "tx_mult_collisions" },
298 { "tx_deferred" },
299 { "tx_excessive_collisions" },
300 { "tx_late_collisions" },
301 { "tx_collide_2times" },
302 { "tx_collide_3times" },
303 { "tx_collide_4times" },
304 { "tx_collide_5times" },
305 { "tx_collide_6times" },
306 { "tx_collide_7times" },
307 { "tx_collide_8times" },
308 { "tx_collide_9times" },
309 { "tx_collide_10times" },
310 { "tx_collide_11times" },
311 { "tx_collide_12times" },
312 { "tx_collide_13times" },
313 { "tx_collide_14times" },
314 { "tx_collide_15times" },
315 { "tx_ucast_packets" },
316 { "tx_mcast_packets" },
317 { "tx_bcast_packets" },
318 { "tx_carrier_sense_errors" },
319 { "tx_discards" },
320 { "tx_errors" },
321
322 { "dma_writeq_full" },
323 { "dma_write_prioq_full" },
324 { "rxbds_empty" },
325 { "rx_discards" },
326 { "rx_errors" },
327 { "rx_threshold_hit" },
328
329 { "dma_readq_full" },
330 { "dma_read_prioq_full" },
331 { "tx_comp_queue_full" },
332
333 { "ring_set_send_prod_index" },
334 { "ring_status_update" },
335 { "nic_irqs" },
336 { "nic_avoided_irqs" },
337 { "nic_tx_threshold_hit" }
338};
339
Andreas Mohr50da8592006-08-14 23:54:30 -0700340static const struct {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700341 const char string[ETH_GSTRING_LEN];
342} ethtool_test_keys[TG3_NUM_TEST] = {
343 { "nvram test (online) " },
344 { "link test (online) " },
345 { "register test (offline)" },
346 { "memory test (offline)" },
347 { "loopback test (offline)" },
348 { "interrupt test (offline)" },
349};
350
Michael Chanb401e9e2005-12-19 16:27:04 -0800351static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
352{
353 writel(val, tp->regs + off);
354}
355
356static u32 tg3_read32(struct tg3 *tp, u32 off)
357{
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400358 return (readl(tp->regs + off));
Michael Chanb401e9e2005-12-19 16:27:04 -0800359}
360
Matt Carlson0d3031d2007-10-10 18:02:43 -0700361static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
362{
363 writel(val, tp->aperegs + off);
364}
365
366static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
367{
368 return (readl(tp->aperegs + off));
369}
370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
372{
Michael Chan68929142005-08-09 20:17:14 -0700373 unsigned long flags;
374
375 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700376 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
377 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
Michael Chan68929142005-08-09 20:17:14 -0700378 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700379}
380
381static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
382{
383 writel(val, tp->regs + off);
384 readl(tp->regs + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385}
386
Michael Chan68929142005-08-09 20:17:14 -0700387static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
388{
389 unsigned long flags;
390 u32 val;
391
392 spin_lock_irqsave(&tp->indirect_lock, flags);
393 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
394 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
395 spin_unlock_irqrestore(&tp->indirect_lock, flags);
396 return val;
397}
398
399static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
400{
401 unsigned long flags;
402
403 if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
404 pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
405 TG3_64BIT_REG_LOW, val);
406 return;
407 }
Matt Carlson66711e62009-11-13 13:03:49 +0000408 if (off == TG3_RX_STD_PROD_IDX_REG) {
Michael Chan68929142005-08-09 20:17:14 -0700409 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
410 TG3_64BIT_REG_LOW, val);
411 return;
412 }
413
414 spin_lock_irqsave(&tp->indirect_lock, flags);
415 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
416 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
417 spin_unlock_irqrestore(&tp->indirect_lock, flags);
418
419 /* In indirect mode when disabling interrupts, we also need
420 * to clear the interrupt bit in the GRC local ctrl register.
421 */
422 if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
423 (val == 0x1)) {
424 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
425 tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
426 }
427}
428
429static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
430{
431 unsigned long flags;
432 u32 val;
433
434 spin_lock_irqsave(&tp->indirect_lock, flags);
435 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
436 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
437 spin_unlock_irqrestore(&tp->indirect_lock, flags);
438 return val;
439}
440
Michael Chanb401e9e2005-12-19 16:27:04 -0800441/* usec_wait specifies the wait time in usec when writing to certain registers
442 * where it is unsafe to read back the register without some delay.
443 * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
444 * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
445 */
446static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
Michael Chanb401e9e2005-12-19 16:27:04 -0800448 if ((tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG) ||
449 (tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND))
450 /* Non-posted methods */
451 tp->write32(tp, off, val);
452 else {
453 /* Posted method */
454 tg3_write32(tp, off, val);
455 if (usec_wait)
456 udelay(usec_wait);
457 tp->read32(tp, off);
458 }
459 /* Wait again after the read for the posted method to guarantee that
460 * the wait time is met.
461 */
462 if (usec_wait)
463 udelay(usec_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
Michael Chan09ee9292005-08-09 20:17:00 -0700466static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
467{
468 tp->write32_mbox(tp, off, val);
Michael Chan68929142005-08-09 20:17:14 -0700469 if (!(tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER) &&
470 !(tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND))
471 tp->read32_mbox(tp, off);
Michael Chan09ee9292005-08-09 20:17:00 -0700472}
473
Michael Chan20094932005-08-09 20:16:32 -0700474static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
476 void __iomem *mbox = tp->regs + off;
477 writel(val, mbox);
478 if (tp->tg3_flags & TG3_FLAG_TXD_MBOX_HWBUG)
479 writel(val, mbox);
480 if (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)
481 readl(mbox);
482}
483
Michael Chanb5d37722006-09-27 16:06:21 -0700484static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
485{
486 return (readl(tp->regs + off + GRCMBOX_BASE));
487}
488
489static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
490{
491 writel(val, tp->regs + off + GRCMBOX_BASE);
492}
493
Michael Chan20094932005-08-09 20:16:32 -0700494#define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700495#define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
Michael Chan20094932005-08-09 20:16:32 -0700496#define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
497#define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700498#define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
Michael Chan20094932005-08-09 20:16:32 -0700499
500#define tw32(reg,val) tp->write32(tp, reg, val)
Michael Chanb401e9e2005-12-19 16:27:04 -0800501#define tw32_f(reg,val) _tw32_flush(tp,(reg),(val), 0)
502#define tw32_wait_f(reg,val,us) _tw32_flush(tp,(reg),(val), (us))
Michael Chan20094932005-08-09 20:16:32 -0700503#define tr32(reg) tp->read32(tp, reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
506{
Michael Chan68929142005-08-09 20:17:14 -0700507 unsigned long flags;
508
Michael Chanb5d37722006-09-27 16:06:21 -0700509 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) &&
510 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
511 return;
512
Michael Chan68929142005-08-09 20:17:14 -0700513 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chanbbadf502006-04-06 21:46:34 -0700514 if (tp->tg3_flags & TG3_FLAG_SRAM_USE_CONFIG) {
515 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
516 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Michael Chanbbadf502006-04-06 21:46:34 -0700518 /* Always leave this as zero. */
519 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
520 } else {
521 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
522 tw32_f(TG3PCI_MEM_WIN_DATA, val);
523
524 /* Always leave this as zero. */
525 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
526 }
Michael Chan68929142005-08-09 20:17:14 -0700527 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
530static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
531{
Michael Chan68929142005-08-09 20:17:14 -0700532 unsigned long flags;
533
Michael Chanb5d37722006-09-27 16:06:21 -0700534 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) &&
535 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
536 *val = 0;
537 return;
538 }
539
Michael Chan68929142005-08-09 20:17:14 -0700540 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chanbbadf502006-04-06 21:46:34 -0700541 if (tp->tg3_flags & TG3_FLAG_SRAM_USE_CONFIG) {
542 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
543 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Michael Chanbbadf502006-04-06 21:46:34 -0700545 /* Always leave this as zero. */
546 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
547 } else {
548 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
549 *val = tr32(TG3PCI_MEM_WIN_DATA);
550
551 /* Always leave this as zero. */
552 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
553 }
Michael Chan68929142005-08-09 20:17:14 -0700554 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555}
556
Matt Carlson0d3031d2007-10-10 18:02:43 -0700557static void tg3_ape_lock_init(struct tg3 *tp)
558{
559 int i;
560
561 /* Make sure the driver hasn't any stale locks. */
562 for (i = 0; i < 8; i++)
563 tg3_ape_write32(tp, TG3_APE_LOCK_GRANT + 4 * i,
564 APE_LOCK_GRANT_DRIVER);
565}
566
567static int tg3_ape_lock(struct tg3 *tp, int locknum)
568{
569 int i, off;
570 int ret = 0;
571 u32 status;
572
573 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
574 return 0;
575
576 switch (locknum) {
Matt Carlson77b483f2008-08-15 14:07:24 -0700577 case TG3_APE_LOCK_GRC:
Matt Carlson0d3031d2007-10-10 18:02:43 -0700578 case TG3_APE_LOCK_MEM:
579 break;
580 default:
581 return -EINVAL;
582 }
583
584 off = 4 * locknum;
585
586 tg3_ape_write32(tp, TG3_APE_LOCK_REQ + off, APE_LOCK_REQ_DRIVER);
587
588 /* Wait for up to 1 millisecond to acquire lock. */
589 for (i = 0; i < 100; i++) {
590 status = tg3_ape_read32(tp, TG3_APE_LOCK_GRANT + off);
591 if (status == APE_LOCK_GRANT_DRIVER)
592 break;
593 udelay(10);
594 }
595
596 if (status != APE_LOCK_GRANT_DRIVER) {
597 /* Revoke the lock request. */
598 tg3_ape_write32(tp, TG3_APE_LOCK_GRANT + off,
599 APE_LOCK_GRANT_DRIVER);
600
601 ret = -EBUSY;
602 }
603
604 return ret;
605}
606
607static void tg3_ape_unlock(struct tg3 *tp, int locknum)
608{
609 int off;
610
611 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
612 return;
613
614 switch (locknum) {
Matt Carlson77b483f2008-08-15 14:07:24 -0700615 case TG3_APE_LOCK_GRC:
Matt Carlson0d3031d2007-10-10 18:02:43 -0700616 case TG3_APE_LOCK_MEM:
617 break;
618 default:
619 return;
620 }
621
622 off = 4 * locknum;
623 tg3_ape_write32(tp, TG3_APE_LOCK_GRANT + off, APE_LOCK_GRANT_DRIVER);
624}
625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626static void tg3_disable_ints(struct tg3 *tp)
627{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000628 int i;
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 tw32(TG3PCI_MISC_HOST_CTRL,
631 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000632 for (i = 0; i < tp->irq_max; i++)
633 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634}
635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636static void tg3_enable_ints(struct tg3 *tp)
637{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000638 int i;
639 u32 coal_now = 0;
640
Michael Chanbbe832c2005-06-24 20:20:04 -0700641 tp->irq_sync = 0;
642 wmb();
643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 tw32(TG3PCI_MISC_HOST_CTRL,
645 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000646
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000647 for (i = 0; i < tp->irq_cnt; i++) {
648 struct tg3_napi *tnapi = &tp->napi[i];
649 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
650 if (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)
651 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
652
653 coal_now |= tnapi->coal_now;
654 }
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000655
656 /* Force an initial interrupt */
657 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) &&
658 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
659 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
660 else
661 tw32(HOSTCC_MODE, tp->coalesce_mode |
662 HOSTCC_MODE_ENABLE | coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663}
664
Matt Carlson17375d22009-08-28 14:02:18 +0000665static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
Michael Chan04237dd2005-04-25 15:17:17 -0700666{
Matt Carlson17375d22009-08-28 14:02:18 +0000667 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +0000668 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan04237dd2005-04-25 15:17:17 -0700669 unsigned int work_exists = 0;
670
671 /* check for phy events */
672 if (!(tp->tg3_flags &
673 (TG3_FLAG_USE_LINKCHG_REG |
674 TG3_FLAG_POLL_SERDES))) {
675 if (sblk->status & SD_STATUS_LINK_CHG)
676 work_exists = 1;
677 }
678 /* check for RX/TX work to do */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000679 if (sblk->idx[0].tx_consumer != tnapi->tx_cons ||
Matt Carlson8d9d7cf2009-09-01 13:19:05 +0000680 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -0700681 work_exists = 1;
682
683 return work_exists;
684}
685
Matt Carlson17375d22009-08-28 14:02:18 +0000686/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -0700687 * similar to tg3_enable_ints, but it accurately determines whether there
688 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400689 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 */
Matt Carlson17375d22009-08-28 14:02:18 +0000691static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
Matt Carlson17375d22009-08-28 14:02:18 +0000693 struct tg3 *tp = tnapi->tp;
694
Matt Carlson898a56f2009-08-28 14:02:40 +0000695 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 mmiowb();
697
David S. Millerfac9b832005-05-18 22:46:34 -0700698 /* When doing tagged status, this work check is unnecessary.
699 * The last_tag we write above tells the chip which piece of
700 * work we've completed.
701 */
702 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) &&
Matt Carlson17375d22009-08-28 14:02:18 +0000703 tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -0700704 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +0000705 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}
707
Matt Carlsonfed97812009-09-01 13:10:19 +0000708static void tg3_napi_disable(struct tg3 *tp)
709{
710 int i;
711
712 for (i = tp->irq_cnt - 1; i >= 0; i--)
713 napi_disable(&tp->napi[i].napi);
714}
715
716static void tg3_napi_enable(struct tg3 *tp)
717{
718 int i;
719
720 for (i = 0; i < tp->irq_cnt; i++)
721 napi_enable(&tp->napi[i].napi);
722}
723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724static inline void tg3_netif_stop(struct tg3 *tp)
725{
Michael Chanbbe832c2005-06-24 20:20:04 -0700726 tp->dev->trans_start = jiffies; /* prevent tx timeout */
Matt Carlsonfed97812009-09-01 13:10:19 +0000727 tg3_napi_disable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 netif_tx_disable(tp->dev);
729}
730
731static inline void tg3_netif_start(struct tg3 *tp)
732{
Matt Carlsonfe5f5782009-09-01 13:09:39 +0000733 /* NOTE: unconditional netif_tx_wake_all_queues is only
734 * appropriate so long as all callers are assured to
735 * have free tx slots (such as after tg3_init_hw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 */
Matt Carlsonfe5f5782009-09-01 13:09:39 +0000737 netif_tx_wake_all_queues(tp->dev);
738
Matt Carlsonfed97812009-09-01 13:10:19 +0000739 tg3_napi_enable(tp);
740 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
David S. Millerf47c11e2005-06-24 20:18:35 -0700741 tg3_enable_ints(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742}
743
744static void tg3_switch_clocks(struct tg3 *tp)
745{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000746 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 u32 orig_clock_ctrl;
748
Matt Carlson795d01c2007-10-07 23:28:17 -0700749 if ((tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
750 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -0700751 return;
752
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000753 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 orig_clock_ctrl = clock_ctrl;
756 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
757 CLOCK_CTRL_CLKRUN_OENABLE |
758 0x1f);
759 tp->pci_clock_ctrl = clock_ctrl;
760
761 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
762 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800763 tw32_wait_f(TG3PCI_CLOCK_CTRL,
764 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 }
766 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800767 tw32_wait_f(TG3PCI_CLOCK_CTRL,
768 clock_ctrl |
769 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
770 40);
771 tw32_wait_f(TG3PCI_CLOCK_CTRL,
772 clock_ctrl | (CLOCK_CTRL_ALTCLK),
773 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 }
Michael Chanb401e9e2005-12-19 16:27:04 -0800775 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776}
777
778#define PHY_BUSY_LOOPS 5000
779
780static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
781{
782 u32 frame_val;
783 unsigned int loops;
784 int ret;
785
786 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
787 tw32_f(MAC_MI_MODE,
788 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
789 udelay(80);
790 }
791
792 *val = 0x0;
793
Matt Carlson882e9792009-09-01 13:21:36 +0000794 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 MI_COM_PHY_ADDR_MASK);
796 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
797 MI_COM_REG_ADDR_MASK);
798 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 tw32_f(MAC_MI_COM, frame_val);
801
802 loops = PHY_BUSY_LOOPS;
803 while (loops != 0) {
804 udelay(10);
805 frame_val = tr32(MAC_MI_COM);
806
807 if ((frame_val & MI_COM_BUSY) == 0) {
808 udelay(5);
809 frame_val = tr32(MAC_MI_COM);
810 break;
811 }
812 loops -= 1;
813 }
814
815 ret = -EBUSY;
816 if (loops != 0) {
817 *val = frame_val & MI_COM_DATA_MASK;
818 ret = 0;
819 }
820
821 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
822 tw32_f(MAC_MI_MODE, tp->mi_mode);
823 udelay(80);
824 }
825
826 return ret;
827}
828
829static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
830{
831 u32 frame_val;
832 unsigned int loops;
833 int ret;
834
Matt Carlson7f97a4b2009-08-25 10:10:03 +0000835 if ((tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) &&
Michael Chanb5d37722006-09-27 16:06:21 -0700836 (reg == MII_TG3_CTRL || reg == MII_TG3_AUX_CTRL))
837 return 0;
838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
840 tw32_f(MAC_MI_MODE,
841 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
842 udelay(80);
843 }
844
Matt Carlson882e9792009-09-01 13:21:36 +0000845 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 MI_COM_PHY_ADDR_MASK);
847 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
848 MI_COM_REG_ADDR_MASK);
849 frame_val |= (val & MI_COM_DATA_MASK);
850 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 tw32_f(MAC_MI_COM, frame_val);
853
854 loops = PHY_BUSY_LOOPS;
855 while (loops != 0) {
856 udelay(10);
857 frame_val = tr32(MAC_MI_COM);
858 if ((frame_val & MI_COM_BUSY) == 0) {
859 udelay(5);
860 frame_val = tr32(MAC_MI_COM);
861 break;
862 }
863 loops -= 1;
864 }
865
866 ret = -EBUSY;
867 if (loops != 0)
868 ret = 0;
869
870 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
871 tw32_f(MAC_MI_MODE, tp->mi_mode);
872 udelay(80);
873 }
874
875 return ret;
876}
877
Matt Carlson95e28692008-05-25 23:44:14 -0700878static int tg3_bmcr_reset(struct tg3 *tp)
879{
880 u32 phy_control;
881 int limit, err;
882
883 /* OK, reset it, and poll the BMCR_RESET bit until it
884 * clears or we time out.
885 */
886 phy_control = BMCR_RESET;
887 err = tg3_writephy(tp, MII_BMCR, phy_control);
888 if (err != 0)
889 return -EBUSY;
890
891 limit = 5000;
892 while (limit--) {
893 err = tg3_readphy(tp, MII_BMCR, &phy_control);
894 if (err != 0)
895 return -EBUSY;
896
897 if ((phy_control & BMCR_RESET) == 0) {
898 udelay(40);
899 break;
900 }
901 udelay(10);
902 }
Roel Kluind4675b52009-02-12 16:33:27 -0800903 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -0700904 return -EBUSY;
905
906 return 0;
907}
908
Matt Carlson158d7ab2008-05-29 01:37:54 -0700909static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
910{
Francois Romieu3d165432009-01-19 16:56:50 -0800911 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700912 u32 val;
913
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000914 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -0700915
916 if (tg3_readphy(tp, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000917 val = -EIO;
918
919 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -0700920
921 return val;
922}
923
924static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
925{
Francois Romieu3d165432009-01-19 16:56:50 -0800926 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000927 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700928
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000929 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -0700930
931 if (tg3_writephy(tp, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000932 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700933
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000934 spin_unlock_bh(&tp->lock);
935
936 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700937}
938
939static int tg3_mdio_reset(struct mii_bus *bp)
940{
941 return 0;
942}
943
Matt Carlson9c61d6b2008-11-03 16:54:56 -0800944static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -0700945{
946 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800947 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -0700948
Matt Carlson3f0e3ad2009-11-02 14:24:36 +0000949 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800950 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
951 case TG3_PHY_ID_BCM50610:
Matt Carlsonc73430d2009-11-02 14:29:34 +0000952 case TG3_PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800953 val = MAC_PHYCFG2_50610_LED_MODES;
954 break;
955 case TG3_PHY_ID_BCMAC131:
956 val = MAC_PHYCFG2_AC131_LED_MODES;
957 break;
958 case TG3_PHY_ID_RTL8211C:
959 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
960 break;
961 case TG3_PHY_ID_RTL8201E:
962 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
963 break;
964 default:
Matt Carlsona9daf362008-05-25 23:49:44 -0700965 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800966 }
967
968 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
969 tw32(MAC_PHYCFG2, val);
970
971 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +0000972 val &= ~(MAC_PHYCFG1_RGMII_INT |
973 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
974 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800975 tw32(MAC_PHYCFG1, val);
976
977 return;
978 }
979
980 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_STD_IBND_DISABLE))
981 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
982 MAC_PHYCFG2_FMODE_MASK_MASK |
983 MAC_PHYCFG2_GMODE_MASK_MASK |
984 MAC_PHYCFG2_ACT_MASK_MASK |
985 MAC_PHYCFG2_QUAL_MASK_MASK |
986 MAC_PHYCFG2_INBAND_ENABLE;
987
988 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -0700989
Matt Carlsonbb85fbb2009-08-25 10:09:07 +0000990 val = tr32(MAC_PHYCFG1);
991 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
992 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
993 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_STD_IBND_DISABLE)) {
Matt Carlsona9daf362008-05-25 23:49:44 -0700994 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
995 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
996 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
997 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
998 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +0000999 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1000 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1001 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001002
Matt Carlsona9daf362008-05-25 23:49:44 -07001003 val = tr32(MAC_EXT_RGMII_MODE);
1004 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1005 MAC_RGMII_MODE_RX_QUALITY |
1006 MAC_RGMII_MODE_RX_ACTIVITY |
1007 MAC_RGMII_MODE_RX_ENG_DET |
1008 MAC_RGMII_MODE_TX_ENABLE |
1009 MAC_RGMII_MODE_TX_LOWPWR |
1010 MAC_RGMII_MODE_TX_RESET);
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001011 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_STD_IBND_DISABLE)) {
Matt Carlsona9daf362008-05-25 23:49:44 -07001012 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
1013 val |= MAC_RGMII_MODE_RX_INT_B |
1014 MAC_RGMII_MODE_RX_QUALITY |
1015 MAC_RGMII_MODE_RX_ACTIVITY |
1016 MAC_RGMII_MODE_RX_ENG_DET;
1017 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
1018 val |= MAC_RGMII_MODE_TX_ENABLE |
1019 MAC_RGMII_MODE_TX_LOWPWR |
1020 MAC_RGMII_MODE_TX_RESET;
1021 }
1022 tw32(MAC_EXT_RGMII_MODE, val);
1023}
1024
Matt Carlson158d7ab2008-05-29 01:37:54 -07001025static void tg3_mdio_start(struct tg3 *tp)
1026{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001027 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1028 tw32_f(MAC_MI_MODE, tp->mi_mode);
1029 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001030
Matt Carlson882e9792009-09-01 13:21:36 +00001031 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
1032 u32 funcnum, is_serdes;
1033
1034 funcnum = tr32(TG3_CPMU_STATUS) & TG3_CPMU_STATUS_PCIE_FUNC;
1035 if (funcnum)
1036 tp->phy_addr = 2;
1037 else
1038 tp->phy_addr = 1;
1039
1040 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1041 if (is_serdes)
1042 tp->phy_addr += 7;
1043 } else
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001044 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001045
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001046 if ((tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED) &&
1047 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1048 tg3_mdio_config_5785(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001049}
1050
Matt Carlson158d7ab2008-05-29 01:37:54 -07001051static int tg3_mdio_init(struct tg3 *tp)
1052{
1053 int i;
1054 u32 reg;
Matt Carlsona9daf362008-05-25 23:49:44 -07001055 struct phy_device *phydev;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001056
1057 tg3_mdio_start(tp);
1058
1059 if (!(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) ||
1060 (tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED))
1061 return 0;
1062
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001063 tp->mdio_bus = mdiobus_alloc();
1064 if (tp->mdio_bus == NULL)
1065 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001066
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001067 tp->mdio_bus->name = "tg3 mdio bus";
1068 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001069 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001070 tp->mdio_bus->priv = tp;
1071 tp->mdio_bus->parent = &tp->pdev->dev;
1072 tp->mdio_bus->read = &tg3_mdio_read;
1073 tp->mdio_bus->write = &tg3_mdio_write;
1074 tp->mdio_bus->reset = &tg3_mdio_reset;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001075 tp->mdio_bus->phy_mask = ~(1 << TG3_PHY_MII_ADDR);
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001076 tp->mdio_bus->irq = &tp->mdio_irq[0];
Matt Carlson158d7ab2008-05-29 01:37:54 -07001077
1078 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001079 tp->mdio_bus->irq[i] = PHY_POLL;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001080
1081 /* The bus registration will look for all the PHYs on the mdio bus.
1082 * Unfortunately, it does not ensure the PHY is powered up before
1083 * accessing the PHY ID registers. A chip reset is the
1084 * quickest way to bring the device back to an operational state..
1085 */
1086 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1087 tg3_bmcr_reset(tp);
1088
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001089 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001090 if (i) {
Matt Carlson158d7ab2008-05-29 01:37:54 -07001091 printk(KERN_WARNING "%s: mdiobus_reg failed (0x%x)\n",
1092 tp->dev->name, i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001093 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001094 return i;
1095 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001096
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001097 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsona9daf362008-05-25 23:49:44 -07001098
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001099 if (!phydev || !phydev->drv) {
1100 printk(KERN_WARNING "%s: No PHY devices\n", tp->dev->name);
1101 mdiobus_unregister(tp->mdio_bus);
1102 mdiobus_free(tp->mdio_bus);
1103 return -ENODEV;
1104 }
1105
1106 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson321d32a2008-11-21 17:22:19 -08001107 case TG3_PHY_ID_BCM57780:
1108 phydev->interface = PHY_INTERFACE_MODE_GMII;
Matt Carlsonc704dc22009-11-02 14:32:12 +00001109 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08001110 break;
Matt Carlsona9daf362008-05-25 23:49:44 -07001111 case TG3_PHY_ID_BCM50610:
Matt Carlsonc73430d2009-11-02 14:29:34 +00001112 case TG3_PHY_ID_BCM50610M:
Matt Carlson32e5a8d2009-11-02 14:31:39 +00001113 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001114 PHY_BRCM_RX_REFCLK_UNUSED |
Matt Carlson52fae082009-11-02 14:32:38 +00001115 PHY_BRCM_DIS_TXCRXC_NOENRGY |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001116 PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlsona9daf362008-05-25 23:49:44 -07001117 if (tp->tg3_flags3 & TG3_FLG3_RGMII_STD_IBND_DISABLE)
1118 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
1119 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
1120 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
1121 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
1122 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001123 /* fallthru */
1124 case TG3_PHY_ID_RTL8211C:
1125 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001126 break;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001127 case TG3_PHY_ID_RTL8201E:
Matt Carlsona9daf362008-05-25 23:49:44 -07001128 case TG3_PHY_ID_BCMAC131:
1129 phydev->interface = PHY_INTERFACE_MODE_MII;
Matt Carlsoncdd4e09d2009-11-02 14:31:11 +00001130 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001131 tp->tg3_flags3 |= TG3_FLG3_PHY_IS_FET;
Matt Carlsona9daf362008-05-25 23:49:44 -07001132 break;
1133 }
1134
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001135 tp->tg3_flags3 |= TG3_FLG3_MDIOBUS_INITED;
1136
1137 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1138 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001139
1140 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001141}
1142
1143static void tg3_mdio_fini(struct tg3 *tp)
1144{
1145 if (tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED) {
1146 tp->tg3_flags3 &= ~TG3_FLG3_MDIOBUS_INITED;
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001147 mdiobus_unregister(tp->mdio_bus);
1148 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001149 }
1150}
1151
Matt Carlson95e28692008-05-25 23:44:14 -07001152/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001153static inline void tg3_generate_fw_event(struct tg3 *tp)
1154{
1155 u32 val;
1156
1157 val = tr32(GRC_RX_CPU_EVENT);
1158 val |= GRC_RX_CPU_DRIVER_EVENT;
1159 tw32_f(GRC_RX_CPU_EVENT, val);
1160
1161 tp->last_event_jiffies = jiffies;
1162}
1163
1164#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1165
1166/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001167static void tg3_wait_for_event_ack(struct tg3 *tp)
1168{
1169 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001170 unsigned int delay_cnt;
1171 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001172
Matt Carlson4ba526c2008-08-15 14:10:04 -07001173 /* If enough time has passed, no wait is necessary. */
1174 time_remain = (long)(tp->last_event_jiffies + 1 +
1175 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1176 (long)jiffies;
1177 if (time_remain < 0)
1178 return;
1179
1180 /* Check if we can shorten the wait time. */
1181 delay_cnt = jiffies_to_usecs(time_remain);
1182 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1183 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1184 delay_cnt = (delay_cnt >> 3) + 1;
1185
1186 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001187 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1188 break;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001189 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001190 }
1191}
1192
1193/* tp->lock is held. */
1194static void tg3_ump_link_report(struct tg3 *tp)
1195{
1196 u32 reg;
1197 u32 val;
1198
1199 if (!(tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
1200 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
1201 return;
1202
1203 tg3_wait_for_event_ack(tp);
1204
1205 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1206
1207 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1208
1209 val = 0;
1210 if (!tg3_readphy(tp, MII_BMCR, &reg))
1211 val = reg << 16;
1212 if (!tg3_readphy(tp, MII_BMSR, &reg))
1213 val |= (reg & 0xffff);
1214 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX, val);
1215
1216 val = 0;
1217 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1218 val = reg << 16;
1219 if (!tg3_readphy(tp, MII_LPA, &reg))
1220 val |= (reg & 0xffff);
1221 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 4, val);
1222
1223 val = 0;
1224 if (!(tp->tg3_flags2 & TG3_FLG2_MII_SERDES)) {
1225 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1226 val = reg << 16;
1227 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1228 val |= (reg & 0xffff);
1229 }
1230 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 8, val);
1231
1232 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1233 val = reg << 16;
1234 else
1235 val = 0;
1236 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 12, val);
1237
Matt Carlson4ba526c2008-08-15 14:10:04 -07001238 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001239}
1240
1241static void tg3_link_report(struct tg3 *tp)
1242{
1243 if (!netif_carrier_ok(tp->dev)) {
1244 if (netif_msg_link(tp))
1245 printk(KERN_INFO PFX "%s: Link is down.\n",
1246 tp->dev->name);
1247 tg3_ump_link_report(tp);
1248 } else if (netif_msg_link(tp)) {
1249 printk(KERN_INFO PFX "%s: Link is up at %d Mbps, %s duplex.\n",
1250 tp->dev->name,
1251 (tp->link_config.active_speed == SPEED_1000 ?
1252 1000 :
1253 (tp->link_config.active_speed == SPEED_100 ?
1254 100 : 10)),
1255 (tp->link_config.active_duplex == DUPLEX_FULL ?
1256 "full" : "half"));
1257
1258 printk(KERN_INFO PFX
1259 "%s: Flow control is %s for TX and %s for RX.\n",
1260 tp->dev->name,
Steve Glendinninge18ce342008-12-16 02:00:00 -08001261 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
Matt Carlson95e28692008-05-25 23:44:14 -07001262 "on" : "off",
Steve Glendinninge18ce342008-12-16 02:00:00 -08001263 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
Matt Carlson95e28692008-05-25 23:44:14 -07001264 "on" : "off");
1265 tg3_ump_link_report(tp);
1266 }
1267}
1268
1269static u16 tg3_advert_flowctrl_1000T(u8 flow_ctrl)
1270{
1271 u16 miireg;
1272
Steve Glendinninge18ce342008-12-16 02:00:00 -08001273 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001274 miireg = ADVERTISE_PAUSE_CAP;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001275 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001276 miireg = ADVERTISE_PAUSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001277 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001278 miireg = ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1279 else
1280 miireg = 0;
1281
1282 return miireg;
1283}
1284
1285static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1286{
1287 u16 miireg;
1288
Steve Glendinninge18ce342008-12-16 02:00:00 -08001289 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001290 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001291 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001292 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001293 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001294 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1295 else
1296 miireg = 0;
1297
1298 return miireg;
1299}
1300
Matt Carlson95e28692008-05-25 23:44:14 -07001301static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1302{
1303 u8 cap = 0;
1304
1305 if (lcladv & ADVERTISE_1000XPAUSE) {
1306 if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1307 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001308 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001309 else if (rmtadv & LPA_1000XPAUSE_ASYM)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001310 cap = FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001311 } else {
1312 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001313 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001314 }
1315 } else if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1316 if ((rmtadv & LPA_1000XPAUSE) && (rmtadv & LPA_1000XPAUSE_ASYM))
Steve Glendinninge18ce342008-12-16 02:00:00 -08001317 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001318 }
1319
1320 return cap;
1321}
1322
Matt Carlsonf51f3562008-05-25 23:45:08 -07001323static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001324{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001325 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001326 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001327 u32 old_rx_mode = tp->rx_mode;
1328 u32 old_tx_mode = tp->tx_mode;
1329
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001330 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001331 autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001332 else
1333 autoneg = tp->link_config.autoneg;
1334
1335 if (autoneg == AUTONEG_ENABLE &&
Matt Carlson95e28692008-05-25 23:44:14 -07001336 (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG)) {
1337 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001338 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001339 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001340 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001341 } else
1342 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001343
Matt Carlsonf51f3562008-05-25 23:45:08 -07001344 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001345
Steve Glendinninge18ce342008-12-16 02:00:00 -08001346 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001347 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1348 else
1349 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1350
Matt Carlsonf51f3562008-05-25 23:45:08 -07001351 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001352 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001353
Steve Glendinninge18ce342008-12-16 02:00:00 -08001354 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001355 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1356 else
1357 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1358
Matt Carlsonf51f3562008-05-25 23:45:08 -07001359 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001360 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001361}
1362
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001363static void tg3_adjust_link(struct net_device *dev)
1364{
1365 u8 oldflowctrl, linkmesg = 0;
1366 u32 mac_mode, lcl_adv, rmt_adv;
1367 struct tg3 *tp = netdev_priv(dev);
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001368 struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001369
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001370 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001371
1372 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
1373 MAC_MODE_HALF_DUPLEX);
1374
1375 oldflowctrl = tp->link_config.active_flowctrl;
1376
1377 if (phydev->link) {
1378 lcl_adv = 0;
1379 rmt_adv = 0;
1380
1381 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
1382 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001383 else if (phydev->speed == SPEED_1000 ||
1384 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001385 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001386 else
1387 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001388
1389 if (phydev->duplex == DUPLEX_HALF)
1390 mac_mode |= MAC_MODE_HALF_DUPLEX;
1391 else {
1392 lcl_adv = tg3_advert_flowctrl_1000T(
1393 tp->link_config.flowctrl);
1394
1395 if (phydev->pause)
1396 rmt_adv = LPA_PAUSE_CAP;
1397 if (phydev->asym_pause)
1398 rmt_adv |= LPA_PAUSE_ASYM;
1399 }
1400
1401 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
1402 } else
1403 mac_mode |= MAC_MODE_PORT_MODE_GMII;
1404
1405 if (mac_mode != tp->mac_mode) {
1406 tp->mac_mode = mac_mode;
1407 tw32_f(MAC_MODE, tp->mac_mode);
1408 udelay(40);
1409 }
1410
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001411 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
1412 if (phydev->speed == SPEED_10)
1413 tw32(MAC_MI_STAT,
1414 MAC_MI_STAT_10MBPS_MODE |
1415 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1416 else
1417 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1418 }
1419
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001420 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
1421 tw32(MAC_TX_LENGTHS,
1422 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1423 (6 << TX_LENGTHS_IPG_SHIFT) |
1424 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
1425 else
1426 tw32(MAC_TX_LENGTHS,
1427 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1428 (6 << TX_LENGTHS_IPG_SHIFT) |
1429 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
1430
1431 if ((phydev->link && tp->link_config.active_speed == SPEED_INVALID) ||
1432 (!phydev->link && tp->link_config.active_speed != SPEED_INVALID) ||
1433 phydev->speed != tp->link_config.active_speed ||
1434 phydev->duplex != tp->link_config.active_duplex ||
1435 oldflowctrl != tp->link_config.active_flowctrl)
1436 linkmesg = 1;
1437
1438 tp->link_config.active_speed = phydev->speed;
1439 tp->link_config.active_duplex = phydev->duplex;
1440
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001441 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001442
1443 if (linkmesg)
1444 tg3_link_report(tp);
1445}
1446
1447static int tg3_phy_init(struct tg3 *tp)
1448{
1449 struct phy_device *phydev;
1450
1451 if (tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED)
1452 return 0;
1453
1454 /* Bring the PHY back to a known state. */
1455 tg3_bmcr_reset(tp);
1456
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001457 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001458
1459 /* Attach the MAC to the PHY. */
Kay Sieversfb28ad32008-11-10 13:55:14 -08001460 phydev = phy_connect(tp->dev, dev_name(&phydev->dev), tg3_adjust_link,
Matt Carlsona9daf362008-05-25 23:49:44 -07001461 phydev->dev_flags, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001462 if (IS_ERR(phydev)) {
1463 printk(KERN_ERR "%s: Could not attach to PHY\n", tp->dev->name);
1464 return PTR_ERR(phydev);
1465 }
1466
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001467 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001468 switch (phydev->interface) {
1469 case PHY_INTERFACE_MODE_GMII:
1470 case PHY_INTERFACE_MODE_RGMII:
Matt Carlson321d32a2008-11-21 17:22:19 -08001471 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY)) {
1472 phydev->supported &= (PHY_GBIT_FEATURES |
1473 SUPPORTED_Pause |
1474 SUPPORTED_Asym_Pause);
1475 break;
1476 }
1477 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001478 case PHY_INTERFACE_MODE_MII:
1479 phydev->supported &= (PHY_BASIC_FEATURES |
1480 SUPPORTED_Pause |
1481 SUPPORTED_Asym_Pause);
1482 break;
1483 default:
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001484 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001485 return -EINVAL;
1486 }
1487
1488 tp->tg3_flags3 |= TG3_FLG3_PHY_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001489
1490 phydev->advertising = phydev->supported;
1491
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001492 return 0;
1493}
1494
1495static void tg3_phy_start(struct tg3 *tp)
1496{
1497 struct phy_device *phydev;
1498
1499 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
1500 return;
1501
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001502 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001503
1504 if (tp->link_config.phy_is_low_power) {
1505 tp->link_config.phy_is_low_power = 0;
1506 phydev->speed = tp->link_config.orig_speed;
1507 phydev->duplex = tp->link_config.orig_duplex;
1508 phydev->autoneg = tp->link_config.orig_autoneg;
1509 phydev->advertising = tp->link_config.orig_advertising;
1510 }
1511
1512 phy_start(phydev);
1513
1514 phy_start_aneg(phydev);
1515}
1516
1517static void tg3_phy_stop(struct tg3 *tp)
1518{
1519 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
1520 return;
1521
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001522 phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001523}
1524
1525static void tg3_phy_fini(struct tg3 *tp)
1526{
1527 if (tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001528 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001529 tp->tg3_flags3 &= ~TG3_FLG3_PHY_CONNECTED;
1530 }
1531}
1532
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001533static void tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
1534{
1535 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1536 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
1537}
1538
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001539static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
1540{
1541 u32 phytest;
1542
1543 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
1544 u32 phy;
1545
1546 tg3_writephy(tp, MII_TG3_FET_TEST,
1547 phytest | MII_TG3_FET_SHADOW_EN);
1548 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
1549 if (enable)
1550 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
1551 else
1552 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
1553 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
1554 }
1555 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
1556 }
1557}
1558
Matt Carlson6833c042008-11-21 17:18:59 -08001559static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
1560{
1561 u32 reg;
1562
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001563 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Matt Carlson6833c042008-11-21 17:18:59 -08001564 return;
1565
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001566 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
1567 tg3_phy_fet_toggle_apd(tp, enable);
1568 return;
1569 }
1570
Matt Carlson6833c042008-11-21 17:18:59 -08001571 reg = MII_TG3_MISC_SHDW_WREN |
1572 MII_TG3_MISC_SHDW_SCR5_SEL |
1573 MII_TG3_MISC_SHDW_SCR5_LPED |
1574 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
1575 MII_TG3_MISC_SHDW_SCR5_SDTL |
1576 MII_TG3_MISC_SHDW_SCR5_C125OE;
1577 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 || !enable)
1578 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
1579
1580 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1581
1582
1583 reg = MII_TG3_MISC_SHDW_WREN |
1584 MII_TG3_MISC_SHDW_APD_SEL |
1585 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
1586 if (enable)
1587 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
1588
1589 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1590}
1591
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001592static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
1593{
1594 u32 phy;
1595
1596 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
1597 (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES))
1598 return;
1599
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001600 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001601 u32 ephy;
1602
Matt Carlson535ef6e2009-08-25 10:09:36 +00001603 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
1604 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
1605
1606 tg3_writephy(tp, MII_TG3_FET_TEST,
1607 ephy | MII_TG3_FET_SHADOW_EN);
1608 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001609 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00001610 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001611 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00001612 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
1613 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001614 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00001615 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001616 }
1617 } else {
1618 phy = MII_TG3_AUXCTL_MISC_RDSEL_MISC |
1619 MII_TG3_AUXCTL_SHDWSEL_MISC;
1620 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, phy) &&
1621 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &phy)) {
1622 if (enable)
1623 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1624 else
1625 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1626 phy |= MII_TG3_AUXCTL_MISC_WREN;
1627 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1628 }
1629 }
1630}
1631
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632static void tg3_phy_set_wirespeed(struct tg3 *tp)
1633{
1634 u32 val;
1635
1636 if (tp->tg3_flags2 & TG3_FLG2_NO_ETH_WIRE_SPEED)
1637 return;
1638
1639 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x7007) &&
1640 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &val))
1641 tg3_writephy(tp, MII_TG3_AUX_CTRL,
1642 (val | (1 << 15) | (1 << 4)));
1643}
1644
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001645static void tg3_phy_apply_otp(struct tg3 *tp)
1646{
1647 u32 otp, phy;
1648
1649 if (!tp->phy_otp)
1650 return;
1651
1652 otp = tp->phy_otp;
1653
1654 /* Enable SM_DSP clock and tx 6dB coding. */
1655 phy = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1656 MII_TG3_AUXCTL_ACTL_SMDSP_ENA |
1657 MII_TG3_AUXCTL_ACTL_TX_6DB;
1658 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1659
1660 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
1661 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
1662 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
1663
1664 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
1665 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
1666 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
1667
1668 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
1669 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
1670 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
1671
1672 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
1673 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
1674
1675 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
1676 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
1677
1678 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
1679 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
1680 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
1681
1682 /* Turn off SM_DSP clock. */
1683 phy = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1684 MII_TG3_AUXCTL_ACTL_TX_6DB;
1685 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1686}
1687
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688static int tg3_wait_macro_done(struct tg3 *tp)
1689{
1690 int limit = 100;
1691
1692 while (limit--) {
1693 u32 tmp32;
1694
1695 if (!tg3_readphy(tp, 0x16, &tmp32)) {
1696 if ((tmp32 & 0x1000) == 0)
1697 break;
1698 }
1699 }
Roel Kluind4675b52009-02-12 16:33:27 -08001700 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 return -EBUSY;
1702
1703 return 0;
1704}
1705
1706static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
1707{
1708 static const u32 test_pat[4][6] = {
1709 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
1710 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
1711 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
1712 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
1713 };
1714 int chan;
1715
1716 for (chan = 0; chan < 4; chan++) {
1717 int i;
1718
1719 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1720 (chan * 0x2000) | 0x0200);
1721 tg3_writephy(tp, 0x16, 0x0002);
1722
1723 for (i = 0; i < 6; i++)
1724 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
1725 test_pat[chan][i]);
1726
1727 tg3_writephy(tp, 0x16, 0x0202);
1728 if (tg3_wait_macro_done(tp)) {
1729 *resetp = 1;
1730 return -EBUSY;
1731 }
1732
1733 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1734 (chan * 0x2000) | 0x0200);
1735 tg3_writephy(tp, 0x16, 0x0082);
1736 if (tg3_wait_macro_done(tp)) {
1737 *resetp = 1;
1738 return -EBUSY;
1739 }
1740
1741 tg3_writephy(tp, 0x16, 0x0802);
1742 if (tg3_wait_macro_done(tp)) {
1743 *resetp = 1;
1744 return -EBUSY;
1745 }
1746
1747 for (i = 0; i < 6; i += 2) {
1748 u32 low, high;
1749
1750 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
1751 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
1752 tg3_wait_macro_done(tp)) {
1753 *resetp = 1;
1754 return -EBUSY;
1755 }
1756 low &= 0x7fff;
1757 high &= 0x000f;
1758 if (low != test_pat[chan][i] ||
1759 high != test_pat[chan][i+1]) {
1760 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
1761 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
1762 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
1763
1764 return -EBUSY;
1765 }
1766 }
1767 }
1768
1769 return 0;
1770}
1771
1772static int tg3_phy_reset_chanpat(struct tg3 *tp)
1773{
1774 int chan;
1775
1776 for (chan = 0; chan < 4; chan++) {
1777 int i;
1778
1779 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1780 (chan * 0x2000) | 0x0200);
1781 tg3_writephy(tp, 0x16, 0x0002);
1782 for (i = 0; i < 6; i++)
1783 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
1784 tg3_writephy(tp, 0x16, 0x0202);
1785 if (tg3_wait_macro_done(tp))
1786 return -EBUSY;
1787 }
1788
1789 return 0;
1790}
1791
1792static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
1793{
1794 u32 reg32, phy9_orig;
1795 int retries, do_phy_reset, err;
1796
1797 retries = 10;
1798 do_phy_reset = 1;
1799 do {
1800 if (do_phy_reset) {
1801 err = tg3_bmcr_reset(tp);
1802 if (err)
1803 return err;
1804 do_phy_reset = 0;
1805 }
1806
1807 /* Disable transmitter and interrupt. */
1808 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
1809 continue;
1810
1811 reg32 |= 0x3000;
1812 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
1813
1814 /* Set full-duplex, 1000 mbps. */
1815 tg3_writephy(tp, MII_BMCR,
1816 BMCR_FULLDPLX | TG3_BMCR_SPEED1000);
1817
1818 /* Set to master mode. */
1819 if (tg3_readphy(tp, MII_TG3_CTRL, &phy9_orig))
1820 continue;
1821
1822 tg3_writephy(tp, MII_TG3_CTRL,
1823 (MII_TG3_CTRL_AS_MASTER |
1824 MII_TG3_CTRL_ENABLE_AS_MASTER));
1825
1826 /* Enable SM_DSP_CLOCK and 6dB. */
1827 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
1828
1829 /* Block the PHY control access. */
1830 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8005);
1831 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0800);
1832
1833 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
1834 if (!err)
1835 break;
1836 } while (--retries);
1837
1838 err = tg3_phy_reset_chanpat(tp);
1839 if (err)
1840 return err;
1841
1842 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8005);
1843 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0000);
1844
1845 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
1846 tg3_writephy(tp, 0x16, 0x0000);
1847
1848 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
1849 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
1850 /* Set Extended packet length bit for jumbo frames */
1851 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4400);
1852 }
1853 else {
1854 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
1855 }
1856
1857 tg3_writephy(tp, MII_TG3_CTRL, phy9_orig);
1858
1859 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
1860 reg32 &= ~0x3000;
1861 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
1862 } else if (!err)
1863 err = -EBUSY;
1864
1865 return err;
1866}
1867
1868/* This will reset the tigon3 PHY if there is no valid
1869 * link unless the FORCE argument is non-zero.
1870 */
1871static int tg3_phy_reset(struct tg3 *tp)
1872{
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001873 u32 cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 u32 phy_status;
1875 int err;
1876
Michael Chan60189dd2006-12-17 17:08:07 -08001877 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
1878 u32 val;
1879
1880 val = tr32(GRC_MISC_CFG);
1881 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
1882 udelay(40);
1883 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 err = tg3_readphy(tp, MII_BMSR, &phy_status);
1885 err |= tg3_readphy(tp, MII_BMSR, &phy_status);
1886 if (err != 0)
1887 return -EBUSY;
1888
Michael Chanc8e1e822006-04-29 18:55:17 -07001889 if (netif_running(tp->dev) && netif_carrier_ok(tp->dev)) {
1890 netif_carrier_off(tp->dev);
1891 tg3_link_report(tp);
1892 }
1893
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
1895 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
1896 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
1897 err = tg3_phy_reset_5703_4_5(tp);
1898 if (err)
1899 return err;
1900 goto out;
1901 }
1902
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001903 cpmuctrl = 0;
1904 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
1905 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
1906 cpmuctrl = tr32(TG3_CPMU_CTRL);
1907 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
1908 tw32(TG3_CPMU_CTRL,
1909 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
1910 }
1911
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 err = tg3_bmcr_reset(tp);
1913 if (err)
1914 return err;
1915
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001916 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
1917 u32 phy;
1918
1919 phy = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
1920 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, phy);
1921
1922 tw32(TG3_CPMU_CTRL, cpmuctrl);
1923 }
1924
Matt Carlsonbcb37f62008-11-03 16:52:09 -08001925 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
1926 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08001927 u32 val;
1928
1929 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
1930 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
1931 CPMU_LSPD_1000MB_MACCLK_12_5) {
1932 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
1933 udelay(40);
1934 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
1935 }
1936 }
1937
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001938 tg3_phy_apply_otp(tp);
1939
Matt Carlson6833c042008-11-21 17:18:59 -08001940 if (tp->tg3_flags3 & TG3_FLG3_PHY_ENABLE_APD)
1941 tg3_phy_toggle_apd(tp, true);
1942 else
1943 tg3_phy_toggle_apd(tp, false);
1944
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945out:
1946 if (tp->tg3_flags2 & TG3_FLG2_PHY_ADC_BUG) {
1947 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
1948 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x201f);
1949 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x2aaa);
1950 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
1951 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0323);
1952 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
1953 }
1954 if (tp->tg3_flags2 & TG3_FLG2_PHY_5704_A0_BUG) {
1955 tg3_writephy(tp, 0x1c, 0x8d68);
1956 tg3_writephy(tp, 0x1c, 0x8d68);
1957 }
1958 if (tp->tg3_flags2 & TG3_FLG2_PHY_BER_BUG) {
1959 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
1960 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
1961 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x310b);
1962 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x201f);
1963 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x9506);
1964 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x401f);
1965 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x14e2);
1966 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
1967 }
Michael Chanc424cb22006-04-29 18:56:34 -07001968 else if (tp->tg3_flags2 & TG3_FLG2_PHY_JITTER_BUG) {
1969 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
1970 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
Michael Chanc1d2a192007-01-08 19:57:20 -08001971 if (tp->tg3_flags2 & TG3_FLG2_PHY_ADJUST_TRIM) {
1972 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
1973 tg3_writephy(tp, MII_TG3_TEST1,
1974 MII_TG3_TEST1_TRIM_EN | 0x4);
1975 } else
1976 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
Michael Chanc424cb22006-04-29 18:56:34 -07001977 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
1978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 /* Set Extended packet length bit (bit 14) on all chips that */
1980 /* support jumbo frames */
1981 if ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5401) {
1982 /* Cannot do read-modify-write on 5401 */
1983 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4c20);
Matt Carlson8f666b02009-08-28 13:58:24 +00001984 } else if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 u32 phy_reg;
1986
1987 /* Set bit 14 with read-modify-write to preserve other bits */
1988 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0007) &&
1989 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &phy_reg))
1990 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy_reg | 0x4000);
1991 }
1992
1993 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
1994 * jumbo frames transmission.
1995 */
Matt Carlson8f666b02009-08-28 13:58:24 +00001996 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 u32 phy_reg;
1998
1999 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &phy_reg))
2000 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2001 phy_reg | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
2002 }
2003
Michael Chan715116a2006-09-27 16:09:25 -07002004 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002005 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002006 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002007 }
2008
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002009 tg3_phy_toggle_automdix(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 tg3_phy_set_wirespeed(tp);
2011 return 0;
2012}
2013
2014static void tg3_frob_aux_power(struct tg3 *tp)
2015{
2016 struct tg3 *tp_peer = tp;
2017
Michael Chan9d26e212006-12-07 00:21:14 -08002018 if ((tp->tg3_flags2 & TG3_FLG2_IS_NIC) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 return;
2020
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00002021 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2022 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
2023 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002024 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002026 dev_peer = pci_get_drvdata(tp->pdev_peer);
Michael Chanbc1c7562006-03-20 17:48:03 -08002027 /* remove_one() may have been run on the peer. */
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002028 if (!dev_peer)
Michael Chanbc1c7562006-03-20 17:48:03 -08002029 tp_peer = tp;
2030 else
2031 tp_peer = netdev_priv(dev_peer);
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
2034 if ((tp->tg3_flags & TG3_FLAG_WOL_ENABLE) != 0 ||
Michael Chan6921d202005-12-13 21:15:53 -08002035 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0 ||
2036 (tp_peer->tg3_flags & TG3_FLAG_WOL_ENABLE) != 0 ||
2037 (tp_peer->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2039 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Michael Chanb401e9e2005-12-19 16:27:04 -08002040 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2041 (GRC_LCLCTRL_GPIO_OE0 |
2042 GRC_LCLCTRL_GPIO_OE1 |
2043 GRC_LCLCTRL_GPIO_OE2 |
2044 GRC_LCLCTRL_GPIO_OUTPUT0 |
2045 GRC_LCLCTRL_GPIO_OUTPUT1),
2046 100);
Matt Carlson8d519ab2009-04-20 06:58:01 +00002047 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2048 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -07002049 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2050 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2051 GRC_LCLCTRL_GPIO_OE1 |
2052 GRC_LCLCTRL_GPIO_OE2 |
2053 GRC_LCLCTRL_GPIO_OUTPUT0 |
2054 GRC_LCLCTRL_GPIO_OUTPUT1 |
2055 tp->grc_local_ctrl;
2056 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2057
2058 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2059 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2060
2061 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2062 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 } else {
2064 u32 no_gpio2;
Michael Chandc56b7d2005-12-19 16:26:28 -08002065 u32 grc_local_ctrl = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
2067 if (tp_peer != tp &&
2068 (tp_peer->tg3_flags & TG3_FLAG_INIT_COMPLETE) != 0)
2069 return;
2070
Michael Chandc56b7d2005-12-19 16:26:28 -08002071 /* Workaround to prevent overdrawing Amps. */
2072 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2073 ASIC_REV_5714) {
2074 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chanb401e9e2005-12-19 16:27:04 -08002075 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2076 grc_local_ctrl, 100);
Michael Chandc56b7d2005-12-19 16:26:28 -08002077 }
2078
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 /* On 5753 and variants, GPIO2 cannot be used. */
2080 no_gpio2 = tp->nic_sram_data_cfg &
2081 NIC_SRAM_DATA_CFG_NO_GPIO2;
2082
Michael Chandc56b7d2005-12-19 16:26:28 -08002083 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 GRC_LCLCTRL_GPIO_OE1 |
2085 GRC_LCLCTRL_GPIO_OE2 |
2086 GRC_LCLCTRL_GPIO_OUTPUT1 |
2087 GRC_LCLCTRL_GPIO_OUTPUT2;
2088 if (no_gpio2) {
2089 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2090 GRC_LCLCTRL_GPIO_OUTPUT2);
2091 }
Michael Chanb401e9e2005-12-19 16:27:04 -08002092 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2093 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
2095 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2096
Michael Chanb401e9e2005-12-19 16:27:04 -08002097 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2098 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
2100 if (!no_gpio2) {
2101 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chanb401e9e2005-12-19 16:27:04 -08002102 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2103 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 }
2105 }
2106 } else {
2107 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
2108 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
2109 if (tp_peer != tp &&
2110 (tp_peer->tg3_flags & TG3_FLAG_INIT_COMPLETE) != 0)
2111 return;
2112
Michael Chanb401e9e2005-12-19 16:27:04 -08002113 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2114 (GRC_LCLCTRL_GPIO_OE1 |
2115 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
Michael Chanb401e9e2005-12-19 16:27:04 -08002117 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2118 GRC_LCLCTRL_GPIO_OE1, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
Michael Chanb401e9e2005-12-19 16:27:04 -08002120 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2121 (GRC_LCLCTRL_GPIO_OE1 |
2122 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 }
2124 }
2125}
2126
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002127static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2128{
2129 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2130 return 1;
2131 else if ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5411) {
2132 if (speed != SPEED_10)
2133 return 1;
2134 } else if (speed == SPEED_10)
2135 return 1;
2136
2137 return 0;
2138}
2139
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140static int tg3_setup_phy(struct tg3 *, int);
2141
2142#define RESET_KIND_SHUTDOWN 0
2143#define RESET_KIND_INIT 1
2144#define RESET_KIND_SUSPEND 2
2145
2146static void tg3_write_sig_post_reset(struct tg3 *, int);
2147static int tg3_halt_cpu(struct tg3 *, u32);
2148
Matt Carlson0a459aa2008-11-03 16:54:15 -08002149static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002150{
Matt Carlsonce057f02007-11-12 21:08:03 -08002151 u32 val;
2152
Michael Chan51297242007-02-13 12:17:57 -08002153 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
2154 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2155 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2156 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2157
2158 sg_dig_ctrl |=
2159 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2160 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2161 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2162 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002163 return;
Michael Chan51297242007-02-13 12:17:57 -08002164 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002165
Michael Chan60189dd2006-12-17 17:08:07 -08002166 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002167 tg3_bmcr_reset(tp);
2168 val = tr32(GRC_MISC_CFG);
2169 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2170 udelay(40);
2171 return;
Matt Carlson0e5f7842009-11-02 14:26:38 +00002172 } else if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
2173 u32 phytest;
2174 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2175 u32 phy;
2176
2177 tg3_writephy(tp, MII_ADVERTISE, 0);
2178 tg3_writephy(tp, MII_BMCR,
2179 BMCR_ANENABLE | BMCR_ANRESTART);
2180
2181 tg3_writephy(tp, MII_TG3_FET_TEST,
2182 phytest | MII_TG3_FET_SHADOW_EN);
2183 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
2184 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
2185 tg3_writephy(tp,
2186 MII_TG3_FET_SHDW_AUXMODE4,
2187 phy);
2188 }
2189 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2190 }
2191 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002192 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07002193 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2194 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002195
2196 tg3_writephy(tp, MII_TG3_AUX_CTRL,
2197 MII_TG3_AUXCTL_SHDWSEL_PWRCTL |
2198 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2199 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2200 MII_TG3_AUXCTL_PCTL_VREG_11V);
Michael Chan715116a2006-09-27 16:09:25 -07002201 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002202
Michael Chan15c3b692006-03-22 01:06:52 -08002203 /* The PHY should not be powered down on some chips because
2204 * of bugs.
2205 */
2206 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2207 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2208 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
2209 (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)))
2210 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08002211
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002212 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2213 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002214 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2215 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2216 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2217 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2218 }
2219
Michael Chan15c3b692006-03-22 01:06:52 -08002220 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2221}
2222
Matt Carlson3f007892008-11-03 16:51:36 -08002223/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002224static int tg3_nvram_lock(struct tg3 *tp)
2225{
2226 if (tp->tg3_flags & TG3_FLAG_NVRAM) {
2227 int i;
2228
2229 if (tp->nvram_lock_cnt == 0) {
2230 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2231 for (i = 0; i < 8000; i++) {
2232 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2233 break;
2234 udelay(20);
2235 }
2236 if (i == 8000) {
2237 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2238 return -ENODEV;
2239 }
2240 }
2241 tp->nvram_lock_cnt++;
2242 }
2243 return 0;
2244}
2245
2246/* tp->lock is held. */
2247static void tg3_nvram_unlock(struct tg3 *tp)
2248{
2249 if (tp->tg3_flags & TG3_FLAG_NVRAM) {
2250 if (tp->nvram_lock_cnt > 0)
2251 tp->nvram_lock_cnt--;
2252 if (tp->nvram_lock_cnt == 0)
2253 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
2254 }
2255}
2256
2257/* tp->lock is held. */
2258static void tg3_enable_nvram_access(struct tg3 *tp)
2259{
2260 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Matt Carlsonf66a29b2009-11-13 13:03:36 +00002261 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002262 u32 nvaccess = tr32(NVRAM_ACCESS);
2263
2264 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
2265 }
2266}
2267
2268/* tp->lock is held. */
2269static void tg3_disable_nvram_access(struct tg3 *tp)
2270{
2271 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Matt Carlsonf66a29b2009-11-13 13:03:36 +00002272 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002273 u32 nvaccess = tr32(NVRAM_ACCESS);
2274
2275 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
2276 }
2277}
2278
2279static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
2280 u32 offset, u32 *val)
2281{
2282 u32 tmp;
2283 int i;
2284
2285 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
2286 return -EINVAL;
2287
2288 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
2289 EEPROM_ADDR_DEVID_MASK |
2290 EEPROM_ADDR_READ);
2291 tw32(GRC_EEPROM_ADDR,
2292 tmp |
2293 (0 << EEPROM_ADDR_DEVID_SHIFT) |
2294 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
2295 EEPROM_ADDR_ADDR_MASK) |
2296 EEPROM_ADDR_READ | EEPROM_ADDR_START);
2297
2298 for (i = 0; i < 1000; i++) {
2299 tmp = tr32(GRC_EEPROM_ADDR);
2300
2301 if (tmp & EEPROM_ADDR_COMPLETE)
2302 break;
2303 msleep(1);
2304 }
2305 if (!(tmp & EEPROM_ADDR_COMPLETE))
2306 return -EBUSY;
2307
Matt Carlson62cedd12009-04-20 14:52:29 -07002308 tmp = tr32(GRC_EEPROM_DATA);
2309
2310 /*
2311 * The data will always be opposite the native endian
2312 * format. Perform a blind byteswap to compensate.
2313 */
2314 *val = swab32(tmp);
2315
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002316 return 0;
2317}
2318
2319#define NVRAM_CMD_TIMEOUT 10000
2320
2321static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
2322{
2323 int i;
2324
2325 tw32(NVRAM_CMD, nvram_cmd);
2326 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
2327 udelay(10);
2328 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
2329 udelay(10);
2330 break;
2331 }
2332 }
2333
2334 if (i == NVRAM_CMD_TIMEOUT)
2335 return -EBUSY;
2336
2337 return 0;
2338}
2339
2340static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
2341{
2342 if ((tp->tg3_flags & TG3_FLAG_NVRAM) &&
2343 (tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) &&
2344 (tp->tg3_flags2 & TG3_FLG2_FLASH) &&
2345 !(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM_ADDR_TRANS) &&
2346 (tp->nvram_jedecnum == JEDEC_ATMEL))
2347
2348 addr = ((addr / tp->nvram_pagesize) <<
2349 ATMEL_AT45DB0X1B_PAGE_POS) +
2350 (addr % tp->nvram_pagesize);
2351
2352 return addr;
2353}
2354
2355static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
2356{
2357 if ((tp->tg3_flags & TG3_FLAG_NVRAM) &&
2358 (tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) &&
2359 (tp->tg3_flags2 & TG3_FLG2_FLASH) &&
2360 !(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM_ADDR_TRANS) &&
2361 (tp->nvram_jedecnum == JEDEC_ATMEL))
2362
2363 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
2364 tp->nvram_pagesize) +
2365 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
2366
2367 return addr;
2368}
2369
Matt Carlsone4f34112009-02-25 14:25:00 +00002370/* NOTE: Data read in from NVRAM is byteswapped according to
2371 * the byteswapping settings for all other register accesses.
2372 * tg3 devices are BE devices, so on a BE machine, the data
2373 * returned will be exactly as it is seen in NVRAM. On a LE
2374 * machine, the 32-bit value will be byteswapped.
2375 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002376static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
2377{
2378 int ret;
2379
2380 if (!(tp->tg3_flags & TG3_FLAG_NVRAM))
2381 return tg3_nvram_read_using_eeprom(tp, offset, val);
2382
2383 offset = tg3_nvram_phys_addr(tp, offset);
2384
2385 if (offset > NVRAM_ADDR_MSK)
2386 return -EINVAL;
2387
2388 ret = tg3_nvram_lock(tp);
2389 if (ret)
2390 return ret;
2391
2392 tg3_enable_nvram_access(tp);
2393
2394 tw32(NVRAM_ADDR, offset);
2395 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
2396 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
2397
2398 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00002399 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002400
2401 tg3_disable_nvram_access(tp);
2402
2403 tg3_nvram_unlock(tp);
2404
2405 return ret;
2406}
2407
Matt Carlsona9dc5292009-02-25 14:25:30 +00002408/* Ensures NVRAM data is in bytestream format. */
2409static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002410{
2411 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00002412 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002413 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00002414 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002415 return res;
2416}
2417
2418/* tp->lock is held. */
Matt Carlson3f007892008-11-03 16:51:36 -08002419static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
2420{
2421 u32 addr_high, addr_low;
2422 int i;
2423
2424 addr_high = ((tp->dev->dev_addr[0] << 8) |
2425 tp->dev->dev_addr[1]);
2426 addr_low = ((tp->dev->dev_addr[2] << 24) |
2427 (tp->dev->dev_addr[3] << 16) |
2428 (tp->dev->dev_addr[4] << 8) |
2429 (tp->dev->dev_addr[5] << 0));
2430 for (i = 0; i < 4; i++) {
2431 if (i == 1 && skip_mac_1)
2432 continue;
2433 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
2434 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
2435 }
2436
2437 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2438 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2439 for (i = 0; i < 12; i++) {
2440 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
2441 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
2442 }
2443 }
2444
2445 addr_high = (tp->dev->dev_addr[0] +
2446 tp->dev->dev_addr[1] +
2447 tp->dev->dev_addr[2] +
2448 tp->dev->dev_addr[3] +
2449 tp->dev->dev_addr[4] +
2450 tp->dev->dev_addr[5]) &
2451 TX_BACKOFF_SEED_MASK;
2452 tw32(MAC_TX_BACKOFF_SEED, addr_high);
2453}
2454
Michael Chanbc1c7562006-03-20 17:48:03 -08002455static int tg3_set_power_state(struct tg3 *tp, pci_power_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456{
2457 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002458 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
2460 /* Make sure register accesses (indirect or otherwise)
2461 * will function correctly.
2462 */
2463 pci_write_config_dword(tp->pdev,
2464 TG3PCI_MISC_HOST_CTRL,
2465 tp->misc_host_ctrl);
2466
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 switch (state) {
Michael Chanbc1c7562006-03-20 17:48:03 -08002468 case PCI_D0:
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07002469 pci_enable_wake(tp->pdev, state, false);
2470 pci_set_power_state(tp->pdev, PCI_D0);
Michael Chan8c6bda12005-04-21 17:09:08 -07002471
Michael Chan9d26e212006-12-07 00:21:14 -08002472 /* Switch out of Vaux if it is a NIC */
2473 if (tp->tg3_flags2 & TG3_FLG2_IS_NIC)
Michael Chanb401e9e2005-12-19 16:27:04 -08002474 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
2476 return 0;
2477
Michael Chanbc1c7562006-03-20 17:48:03 -08002478 case PCI_D1:
Michael Chanbc1c7562006-03-20 17:48:03 -08002479 case PCI_D2:
Michael Chanbc1c7562006-03-20 17:48:03 -08002480 case PCI_D3hot:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 break;
2482
2483 default:
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07002484 printk(KERN_ERR PFX "%s: Invalid power state (D%d) requested\n",
2485 tp->dev->name, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002487 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002488
2489 /* Restore the CLKREQ setting. */
2490 if (tp->tg3_flags3 & TG3_FLG3_CLKREQ_BUG) {
2491 u16 lnkctl;
2492
2493 pci_read_config_word(tp->pdev,
2494 tp->pcie_cap + PCI_EXP_LNKCTL,
2495 &lnkctl);
2496 lnkctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
2497 pci_write_config_word(tp->pdev,
2498 tp->pcie_cap + PCI_EXP_LNKCTL,
2499 lnkctl);
2500 }
2501
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
2503 tw32(TG3PCI_MISC_HOST_CTRL,
2504 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
2505
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002506 device_should_wake = pci_pme_capable(tp->pdev, state) &&
2507 device_may_wakeup(&tp->pdev->dev) &&
2508 (tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
2509
Matt Carlsondd477002008-05-25 23:45:58 -07002510 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08002511 do_low_power = false;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002512 if ((tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED) &&
2513 !tp->link_config.phy_is_low_power) {
2514 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002515 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002516
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002517 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002518
2519 tp->link_config.phy_is_low_power = 1;
2520
2521 tp->link_config.orig_speed = phydev->speed;
2522 tp->link_config.orig_duplex = phydev->duplex;
2523 tp->link_config.orig_autoneg = phydev->autoneg;
2524 tp->link_config.orig_advertising = phydev->advertising;
2525
2526 advertising = ADVERTISED_TP |
2527 ADVERTISED_Pause |
2528 ADVERTISED_Autoneg |
2529 ADVERTISED_10baseT_Half;
2530
2531 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002532 device_should_wake) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002533 if (tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB)
2534 advertising |=
2535 ADVERTISED_100baseT_Half |
2536 ADVERTISED_100baseT_Full |
2537 ADVERTISED_10baseT_Full;
2538 else
2539 advertising |= ADVERTISED_10baseT_Full;
2540 }
2541
2542 phydev->advertising = advertising;
2543
2544 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002545
2546 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
2547 if (phyid != TG3_PHY_ID_BCMAC131) {
2548 phyid &= TG3_PHY_OUI_MASK;
Roel Kluinf72b5342009-02-18 17:42:42 -08002549 if (phyid == TG3_PHY_OUI_1 ||
2550 phyid == TG3_PHY_OUI_2 ||
Matt Carlson0a459aa2008-11-03 16:54:15 -08002551 phyid == TG3_PHY_OUI_3)
2552 do_low_power = true;
2553 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002554 }
Matt Carlsondd477002008-05-25 23:45:58 -07002555 } else {
Matt Carlson20232762008-12-21 20:18:56 -08002556 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002557
Matt Carlsondd477002008-05-25 23:45:58 -07002558 if (tp->link_config.phy_is_low_power == 0) {
2559 tp->link_config.phy_is_low_power = 1;
2560 tp->link_config.orig_speed = tp->link_config.speed;
2561 tp->link_config.orig_duplex = tp->link_config.duplex;
2562 tp->link_config.orig_autoneg = tp->link_config.autoneg;
2563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564
Matt Carlsondd477002008-05-25 23:45:58 -07002565 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)) {
2566 tp->link_config.speed = SPEED_10;
2567 tp->link_config.duplex = DUPLEX_HALF;
2568 tp->link_config.autoneg = AUTONEG_ENABLE;
2569 tg3_setup_phy(tp, 0);
2570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 }
2572
Michael Chanb5d37722006-09-27 16:06:21 -07002573 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
2574 u32 val;
2575
2576 val = tr32(GRC_VCPU_EXT_CTRL);
2577 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
2578 } else if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08002579 int i;
2580 u32 val;
2581
2582 for (i = 0; i < 200; i++) {
2583 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
2584 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
2585 break;
2586 msleep(1);
2587 }
2588 }
Gary Zambranoa85feb82007-05-05 11:52:19 -07002589 if (tp->tg3_flags & TG3_FLAG_WOL_CAP)
2590 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
2591 WOL_DRV_STATE_SHUTDOWN |
2592 WOL_DRV_WOL |
2593 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08002594
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002595 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 u32 mac_mode;
2597
2598 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08002599 if (do_low_power) {
Matt Carlsondd477002008-05-25 23:45:58 -07002600 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x5a);
2601 udelay(40);
2602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603
Michael Chan3f7045c2006-09-27 16:02:29 -07002604 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)
2605 mac_mode = MAC_MODE_PORT_MODE_GMII;
2606 else
2607 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002609 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
2610 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2611 ASIC_REV_5700) {
2612 u32 speed = (tp->tg3_flags &
2613 TG3_FLAG_WOL_SPEED_100MB) ?
2614 SPEED_100 : SPEED_10;
2615 if (tg3_5700_link_polarity(tp, speed))
2616 mac_mode |= MAC_MODE_LINK_POLARITY;
2617 else
2618 mac_mode &= ~MAC_MODE_LINK_POLARITY;
2619 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 } else {
2621 mac_mode = MAC_MODE_PORT_MODE_TBI;
2622 }
2623
John W. Linvillecbf46852005-04-21 17:01:29 -07002624 if (!(tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 tw32(MAC_LED_CTRL, tp->led_ctrl);
2626
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002627 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
2628 if (((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
2629 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) &&
2630 ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
2631 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)))
2632 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633
Matt Carlson3bda1252008-08-15 14:08:22 -07002634 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
2635 mac_mode |= tp->mac_mode &
2636 (MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN);
2637 if (mac_mode & MAC_MODE_APE_TX_EN)
2638 mac_mode |= MAC_MODE_TDE_ENABLE;
2639 }
2640
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 tw32_f(MAC_MODE, mac_mode);
2642 udelay(100);
2643
2644 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
2645 udelay(10);
2646 }
2647
2648 if (!(tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB) &&
2649 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2650 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
2651 u32 base_val;
2652
2653 base_val = tp->pci_clock_ctrl;
2654 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
2655 CLOCK_CTRL_TXCLK_DISABLE);
2656
Michael Chanb401e9e2005-12-19 16:27:04 -08002657 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
2658 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Michael Chand7b0a852007-02-13 12:17:38 -08002659 } else if ((tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
Matt Carlson795d01c2007-10-07 23:28:17 -07002660 (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
Michael Chand7b0a852007-02-13 12:17:38 -08002661 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)) {
Michael Chan4cf78e42005-07-25 12:29:19 -07002662 /* do nothing */
Michael Chan85e94ce2005-04-21 17:05:28 -07002663 } else if (!((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF))) {
2665 u32 newbits1, newbits2;
2666
2667 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2668 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2669 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
2670 CLOCK_CTRL_TXCLK_DISABLE |
2671 CLOCK_CTRL_ALTCLK);
2672 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2673 } else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
2674 newbits1 = CLOCK_CTRL_625_CORE;
2675 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
2676 } else {
2677 newbits1 = CLOCK_CTRL_ALTCLK;
2678 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2679 }
2680
Michael Chanb401e9e2005-12-19 16:27:04 -08002681 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
2682 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683
Michael Chanb401e9e2005-12-19 16:27:04 -08002684 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
2685 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686
2687 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
2688 u32 newbits3;
2689
2690 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2691 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2692 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
2693 CLOCK_CTRL_TXCLK_DISABLE |
2694 CLOCK_CTRL_44MHZ_CORE);
2695 } else {
2696 newbits3 = CLOCK_CTRL_44MHZ_CORE;
2697 }
2698
Michael Chanb401e9e2005-12-19 16:27:04 -08002699 tw32_wait_f(TG3PCI_CLOCK_CTRL,
2700 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 }
2702 }
2703
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002704 if (!(device_should_wake) &&
Matt Carlson22435842008-11-21 17:21:13 -08002705 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08002706 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08002707
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 tg3_frob_aux_power(tp);
2709
2710 /* Workaround for unstable PLL clock */
2711 if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
2712 (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
2713 u32 val = tr32(0x7d00);
2714
2715 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
2716 tw32(0x7d00, val);
Michael Chan6921d202005-12-13 21:15:53 -08002717 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08002718 int err;
2719
2720 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08002722 if (!err)
2723 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08002724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 }
2726
Michael Chanbbadf502006-04-06 21:46:34 -07002727 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
2728
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002729 if (device_should_wake)
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07002730 pci_enable_wake(tp->pdev, state, true);
2731
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 /* Finally, set the new power state. */
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07002733 pci_set_power_state(tp->pdev, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 return 0;
2736}
2737
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
2739{
2740 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
2741 case MII_TG3_AUX_STAT_10HALF:
2742 *speed = SPEED_10;
2743 *duplex = DUPLEX_HALF;
2744 break;
2745
2746 case MII_TG3_AUX_STAT_10FULL:
2747 *speed = SPEED_10;
2748 *duplex = DUPLEX_FULL;
2749 break;
2750
2751 case MII_TG3_AUX_STAT_100HALF:
2752 *speed = SPEED_100;
2753 *duplex = DUPLEX_HALF;
2754 break;
2755
2756 case MII_TG3_AUX_STAT_100FULL:
2757 *speed = SPEED_100;
2758 *duplex = DUPLEX_FULL;
2759 break;
2760
2761 case MII_TG3_AUX_STAT_1000HALF:
2762 *speed = SPEED_1000;
2763 *duplex = DUPLEX_HALF;
2764 break;
2765
2766 case MII_TG3_AUX_STAT_1000FULL:
2767 *speed = SPEED_1000;
2768 *duplex = DUPLEX_FULL;
2769 break;
2770
2771 default:
Matt Carlson7f97a4b2009-08-25 10:10:03 +00002772 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07002773 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
2774 SPEED_10;
2775 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
2776 DUPLEX_HALF;
2777 break;
2778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 *speed = SPEED_INVALID;
2780 *duplex = DUPLEX_INVALID;
2781 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783}
2784
2785static void tg3_phy_copper_begin(struct tg3 *tp)
2786{
2787 u32 new_adv;
2788 int i;
2789
2790 if (tp->link_config.phy_is_low_power) {
2791 /* Entering low power mode. Disable gigabit and
2792 * 100baseT advertisements.
2793 */
2794 tg3_writephy(tp, MII_TG3_CTRL, 0);
2795
2796 new_adv = (ADVERTISE_10HALF | ADVERTISE_10FULL |
2797 ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
2798 if (tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB)
2799 new_adv |= (ADVERTISE_100HALF | ADVERTISE_100FULL);
2800
2801 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2802 } else if (tp->link_config.speed == SPEED_INVALID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 if (tp->tg3_flags & TG3_FLAG_10_100_ONLY)
2804 tp->link_config.advertising &=
2805 ~(ADVERTISED_1000baseT_Half |
2806 ADVERTISED_1000baseT_Full);
2807
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002808 new_adv = ADVERTISE_CSMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 if (tp->link_config.advertising & ADVERTISED_10baseT_Half)
2810 new_adv |= ADVERTISE_10HALF;
2811 if (tp->link_config.advertising & ADVERTISED_10baseT_Full)
2812 new_adv |= ADVERTISE_10FULL;
2813 if (tp->link_config.advertising & ADVERTISED_100baseT_Half)
2814 new_adv |= ADVERTISE_100HALF;
2815 if (tp->link_config.advertising & ADVERTISED_100baseT_Full)
2816 new_adv |= ADVERTISE_100FULL;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002817
2818 new_adv |= tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2819
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2821
2822 if (tp->link_config.advertising &
2823 (ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full)) {
2824 new_adv = 0;
2825 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
2826 new_adv |= MII_TG3_CTRL_ADV_1000_HALF;
2827 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
2828 new_adv |= MII_TG3_CTRL_ADV_1000_FULL;
2829 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY) &&
2830 (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2831 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0))
2832 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2833 MII_TG3_CTRL_ENABLE_AS_MASTER);
2834 tg3_writephy(tp, MII_TG3_CTRL, new_adv);
2835 } else {
2836 tg3_writephy(tp, MII_TG3_CTRL, 0);
2837 }
2838 } else {
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002839 new_adv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2840 new_adv |= ADVERTISE_CSMA;
2841
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 /* Asking for a specific link mode. */
2843 if (tp->link_config.speed == SPEED_1000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2845
2846 if (tp->link_config.duplex == DUPLEX_FULL)
2847 new_adv = MII_TG3_CTRL_ADV_1000_FULL;
2848 else
2849 new_adv = MII_TG3_CTRL_ADV_1000_HALF;
2850 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2851 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
2852 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2853 MII_TG3_CTRL_ENABLE_AS_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 if (tp->link_config.speed == SPEED_100) {
2856 if (tp->link_config.duplex == DUPLEX_FULL)
2857 new_adv |= ADVERTISE_100FULL;
2858 else
2859 new_adv |= ADVERTISE_100HALF;
2860 } else {
2861 if (tp->link_config.duplex == DUPLEX_FULL)
2862 new_adv |= ADVERTISE_10FULL;
2863 else
2864 new_adv |= ADVERTISE_10HALF;
2865 }
2866 tg3_writephy(tp, MII_ADVERTISE, new_adv);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002867
2868 new_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002870
2871 tg3_writephy(tp, MII_TG3_CTRL, new_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 }
2873
2874 if (tp->link_config.autoneg == AUTONEG_DISABLE &&
2875 tp->link_config.speed != SPEED_INVALID) {
2876 u32 bmcr, orig_bmcr;
2877
2878 tp->link_config.active_speed = tp->link_config.speed;
2879 tp->link_config.active_duplex = tp->link_config.duplex;
2880
2881 bmcr = 0;
2882 switch (tp->link_config.speed) {
2883 default:
2884 case SPEED_10:
2885 break;
2886
2887 case SPEED_100:
2888 bmcr |= BMCR_SPEED100;
2889 break;
2890
2891 case SPEED_1000:
2892 bmcr |= TG3_BMCR_SPEED1000;
2893 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895
2896 if (tp->link_config.duplex == DUPLEX_FULL)
2897 bmcr |= BMCR_FULLDPLX;
2898
2899 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
2900 (bmcr != orig_bmcr)) {
2901 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
2902 for (i = 0; i < 1500; i++) {
2903 u32 tmp;
2904
2905 udelay(10);
2906 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
2907 tg3_readphy(tp, MII_BMSR, &tmp))
2908 continue;
2909 if (!(tmp & BMSR_LSTATUS)) {
2910 udelay(40);
2911 break;
2912 }
2913 }
2914 tg3_writephy(tp, MII_BMCR, bmcr);
2915 udelay(40);
2916 }
2917 } else {
2918 tg3_writephy(tp, MII_BMCR,
2919 BMCR_ANENABLE | BMCR_ANRESTART);
2920 }
2921}
2922
2923static int tg3_init_5401phy_dsp(struct tg3 *tp)
2924{
2925 int err;
2926
2927 /* Turn off tap power management. */
2928 /* Set Extended packet length bit */
2929 err = tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4c20);
2930
2931 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x0012);
2932 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x1804);
2933
2934 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x0013);
2935 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x1204);
2936
2937 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8006);
2938 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0132);
2939
2940 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8006);
2941 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0232);
2942
2943 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x201f);
2944 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0a20);
2945
2946 udelay(40);
2947
2948 return err;
2949}
2950
Michael Chan3600d912006-12-07 00:21:48 -08002951static int tg3_copper_is_advertising_all(struct tg3 *tp, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952{
Michael Chan3600d912006-12-07 00:21:48 -08002953 u32 adv_reg, all_mask = 0;
2954
2955 if (mask & ADVERTISED_10baseT_Half)
2956 all_mask |= ADVERTISE_10HALF;
2957 if (mask & ADVERTISED_10baseT_Full)
2958 all_mask |= ADVERTISE_10FULL;
2959 if (mask & ADVERTISED_100baseT_Half)
2960 all_mask |= ADVERTISE_100HALF;
2961 if (mask & ADVERTISED_100baseT_Full)
2962 all_mask |= ADVERTISE_100FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963
2964 if (tg3_readphy(tp, MII_ADVERTISE, &adv_reg))
2965 return 0;
2966
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 if ((adv_reg & all_mask) != all_mask)
2968 return 0;
2969 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY)) {
2970 u32 tg3_ctrl;
2971
Michael Chan3600d912006-12-07 00:21:48 -08002972 all_mask = 0;
2973 if (mask & ADVERTISED_1000baseT_Half)
2974 all_mask |= ADVERTISE_1000HALF;
2975 if (mask & ADVERTISED_1000baseT_Full)
2976 all_mask |= ADVERTISE_1000FULL;
2977
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 if (tg3_readphy(tp, MII_TG3_CTRL, &tg3_ctrl))
2979 return 0;
2980
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 if ((tg3_ctrl & all_mask) != all_mask)
2982 return 0;
2983 }
2984 return 1;
2985}
2986
Matt Carlsonef167e22007-12-20 20:10:01 -08002987static int tg3_adv_1000T_flowctrl_ok(struct tg3 *tp, u32 *lcladv, u32 *rmtadv)
2988{
2989 u32 curadv, reqadv;
2990
2991 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
2992 return 1;
2993
2994 curadv = *lcladv & (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
2995 reqadv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2996
2997 if (tp->link_config.active_duplex == DUPLEX_FULL) {
2998 if (curadv != reqadv)
2999 return 0;
3000
3001 if (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG)
3002 tg3_readphy(tp, MII_LPA, rmtadv);
3003 } else {
3004 /* Reprogram the advertisement register, even if it
3005 * does not affect the current link. If the link
3006 * gets renegotiated in the future, we can save an
3007 * additional renegotiation cycle by advertising
3008 * it correctly in the first place.
3009 */
3010 if (curadv != reqadv) {
3011 *lcladv &= ~(ADVERTISE_PAUSE_CAP |
3012 ADVERTISE_PAUSE_ASYM);
3013 tg3_writephy(tp, MII_ADVERTISE, *lcladv | reqadv);
3014 }
3015 }
3016
3017 return 1;
3018}
3019
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
3021{
3022 int current_link_up;
3023 u32 bmsr, dummy;
Matt Carlsonef167e22007-12-20 20:10:01 -08003024 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 u16 current_speed;
3026 u8 current_duplex;
3027 int i, err;
3028
3029 tw32(MAC_EVENT, 0);
3030
3031 tw32_f(MAC_STATUS,
3032 (MAC_STATUS_SYNC_CHANGED |
3033 MAC_STATUS_CFG_CHANGED |
3034 MAC_STATUS_MI_COMPLETION |
3035 MAC_STATUS_LNKSTATE_CHANGED));
3036 udelay(40);
3037
Matt Carlson8ef21422008-05-02 16:47:53 -07003038 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
3039 tw32_f(MAC_MI_MODE,
3040 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
3041 udelay(80);
3042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043
3044 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x02);
3045
3046 /* Some third-party PHYs need to be reset on link going
3047 * down.
3048 */
3049 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3050 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
3051 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
3052 netif_carrier_ok(tp->dev)) {
3053 tg3_readphy(tp, MII_BMSR, &bmsr);
3054 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3055 !(bmsr & BMSR_LSTATUS))
3056 force_reset = 1;
3057 }
3058 if (force_reset)
3059 tg3_phy_reset(tp);
3060
3061 if ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5401) {
3062 tg3_readphy(tp, MII_BMSR, &bmsr);
3063 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
3064 !(tp->tg3_flags & TG3_FLAG_INIT_COMPLETE))
3065 bmsr = 0;
3066
3067 if (!(bmsr & BMSR_LSTATUS)) {
3068 err = tg3_init_5401phy_dsp(tp);
3069 if (err)
3070 return err;
3071
3072 tg3_readphy(tp, MII_BMSR, &bmsr);
3073 for (i = 0; i < 1000; i++) {
3074 udelay(10);
3075 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3076 (bmsr & BMSR_LSTATUS)) {
3077 udelay(40);
3078 break;
3079 }
3080 }
3081
3082 if ((tp->phy_id & PHY_ID_REV_MASK) == PHY_REV_BCM5401_B0 &&
3083 !(bmsr & BMSR_LSTATUS) &&
3084 tp->link_config.active_speed == SPEED_1000) {
3085 err = tg3_phy_reset(tp);
3086 if (!err)
3087 err = tg3_init_5401phy_dsp(tp);
3088 if (err)
3089 return err;
3090 }
3091 }
3092 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3093 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
3094 /* 5701 {A0,B0} CRC bug workaround */
3095 tg3_writephy(tp, 0x15, 0x0a75);
3096 tg3_writephy(tp, 0x1c, 0x8c68);
3097 tg3_writephy(tp, 0x1c, 0x8d68);
3098 tg3_writephy(tp, 0x1c, 0x8c68);
3099 }
3100
3101 /* Clear pending interrupts... */
3102 tg3_readphy(tp, MII_TG3_ISTAT, &dummy);
3103 tg3_readphy(tp, MII_TG3_ISTAT, &dummy);
3104
3105 if (tp->tg3_flags & TG3_FLAG_USE_MI_INTERRUPT)
3106 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlson7f97a4b2009-08-25 10:10:03 +00003107 else if (!(tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 tg3_writephy(tp, MII_TG3_IMASK, ~0);
3109
3110 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3111 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3112 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
3113 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3114 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
3115 else
3116 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
3117 }
3118
3119 current_link_up = 0;
3120 current_speed = SPEED_INVALID;
3121 current_duplex = DUPLEX_INVALID;
3122
3123 if (tp->tg3_flags2 & TG3_FLG2_CAPACITIVE_COUPLING) {
3124 u32 val;
3125
3126 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4007);
3127 tg3_readphy(tp, MII_TG3_AUX_CTRL, &val);
3128 if (!(val & (1 << 10))) {
3129 val |= (1 << 10);
3130 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
3131 goto relink;
3132 }
3133 }
3134
3135 bmsr = 0;
3136 for (i = 0; i < 100; i++) {
3137 tg3_readphy(tp, MII_BMSR, &bmsr);
3138 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3139 (bmsr & BMSR_LSTATUS))
3140 break;
3141 udelay(40);
3142 }
3143
3144 if (bmsr & BMSR_LSTATUS) {
3145 u32 aux_stat, bmcr;
3146
3147 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
3148 for (i = 0; i < 2000; i++) {
3149 udelay(10);
3150 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
3151 aux_stat)
3152 break;
3153 }
3154
3155 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
3156 &current_speed,
3157 &current_duplex);
3158
3159 bmcr = 0;
3160 for (i = 0; i < 200; i++) {
3161 tg3_readphy(tp, MII_BMCR, &bmcr);
3162 if (tg3_readphy(tp, MII_BMCR, &bmcr))
3163 continue;
3164 if (bmcr && bmcr != 0x7fff)
3165 break;
3166 udelay(10);
3167 }
3168
Matt Carlsonef167e22007-12-20 20:10:01 -08003169 lcl_adv = 0;
3170 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171
Matt Carlsonef167e22007-12-20 20:10:01 -08003172 tp->link_config.active_speed = current_speed;
3173 tp->link_config.active_duplex = current_duplex;
3174
3175 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3176 if ((bmcr & BMCR_ANENABLE) &&
3177 tg3_copper_is_advertising_all(tp,
3178 tp->link_config.advertising)) {
3179 if (tg3_adv_1000T_flowctrl_ok(tp, &lcl_adv,
3180 &rmt_adv))
3181 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 }
3183 } else {
3184 if (!(bmcr & BMCR_ANENABLE) &&
3185 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08003186 tp->link_config.duplex == current_duplex &&
3187 tp->link_config.flowctrl ==
3188 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 }
3191 }
3192
Matt Carlsonef167e22007-12-20 20:10:01 -08003193 if (current_link_up == 1 &&
3194 tp->link_config.active_duplex == DUPLEX_FULL)
3195 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 }
3197
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198relink:
Michael Chan6921d202005-12-13 21:15:53 -08003199 if (current_link_up == 0 || tp->link_config.phy_is_low_power) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 u32 tmp;
3201
3202 tg3_phy_copper_begin(tp);
3203
3204 tg3_readphy(tp, MII_BMSR, &tmp);
3205 if (!tg3_readphy(tp, MII_BMSR, &tmp) &&
3206 (tmp & BMSR_LSTATUS))
3207 current_link_up = 1;
3208 }
3209
3210 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
3211 if (current_link_up == 1) {
3212 if (tp->link_config.active_speed == SPEED_100 ||
3213 tp->link_config.active_speed == SPEED_10)
3214 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3215 else
3216 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlson7f97a4b2009-08-25 10:10:03 +00003217 } else if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET)
3218 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3219 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
3221
3222 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
3223 if (tp->link_config.active_duplex == DUPLEX_HALF)
3224 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
3225
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003227 if (current_link_up == 1 &&
3228 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003230 else
3231 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 }
3233
3234 /* ??? Without this setting Netgear GA302T PHY does not
3235 * ??? send/receive packets...
3236 */
3237 if ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5411 &&
3238 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
3239 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
3240 tw32_f(MAC_MI_MODE, tp->mi_mode);
3241 udelay(80);
3242 }
3243
3244 tw32_f(MAC_MODE, tp->mac_mode);
3245 udelay(40);
3246
3247 if (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) {
3248 /* Polled via timer. */
3249 tw32_f(MAC_EVENT, 0);
3250 } else {
3251 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
3252 }
3253 udelay(40);
3254
3255 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
3256 current_link_up == 1 &&
3257 tp->link_config.active_speed == SPEED_1000 &&
3258 ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) ||
3259 (tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED))) {
3260 udelay(120);
3261 tw32_f(MAC_STATUS,
3262 (MAC_STATUS_SYNC_CHANGED |
3263 MAC_STATUS_CFG_CHANGED));
3264 udelay(40);
3265 tg3_write_mem(tp,
3266 NIC_SRAM_FIRMWARE_MBOX,
3267 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
3268 }
3269
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003270 /* Prevent send BD corruption. */
3271 if (tp->tg3_flags3 & TG3_FLG3_CLKREQ_BUG) {
3272 u16 oldlnkctl, newlnkctl;
3273
3274 pci_read_config_word(tp->pdev,
3275 tp->pcie_cap + PCI_EXP_LNKCTL,
3276 &oldlnkctl);
3277 if (tp->link_config.active_speed == SPEED_100 ||
3278 tp->link_config.active_speed == SPEED_10)
3279 newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
3280 else
3281 newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
3282 if (newlnkctl != oldlnkctl)
3283 pci_write_config_word(tp->pdev,
3284 tp->pcie_cap + PCI_EXP_LNKCTL,
3285 newlnkctl);
3286 }
3287
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 if (current_link_up != netif_carrier_ok(tp->dev)) {
3289 if (current_link_up)
3290 netif_carrier_on(tp->dev);
3291 else
3292 netif_carrier_off(tp->dev);
3293 tg3_link_report(tp);
3294 }
3295
3296 return 0;
3297}
3298
3299struct tg3_fiber_aneginfo {
3300 int state;
3301#define ANEG_STATE_UNKNOWN 0
3302#define ANEG_STATE_AN_ENABLE 1
3303#define ANEG_STATE_RESTART_INIT 2
3304#define ANEG_STATE_RESTART 3
3305#define ANEG_STATE_DISABLE_LINK_OK 4
3306#define ANEG_STATE_ABILITY_DETECT_INIT 5
3307#define ANEG_STATE_ABILITY_DETECT 6
3308#define ANEG_STATE_ACK_DETECT_INIT 7
3309#define ANEG_STATE_ACK_DETECT 8
3310#define ANEG_STATE_COMPLETE_ACK_INIT 9
3311#define ANEG_STATE_COMPLETE_ACK 10
3312#define ANEG_STATE_IDLE_DETECT_INIT 11
3313#define ANEG_STATE_IDLE_DETECT 12
3314#define ANEG_STATE_LINK_OK 13
3315#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
3316#define ANEG_STATE_NEXT_PAGE_WAIT 15
3317
3318 u32 flags;
3319#define MR_AN_ENABLE 0x00000001
3320#define MR_RESTART_AN 0x00000002
3321#define MR_AN_COMPLETE 0x00000004
3322#define MR_PAGE_RX 0x00000008
3323#define MR_NP_LOADED 0x00000010
3324#define MR_TOGGLE_TX 0x00000020
3325#define MR_LP_ADV_FULL_DUPLEX 0x00000040
3326#define MR_LP_ADV_HALF_DUPLEX 0x00000080
3327#define MR_LP_ADV_SYM_PAUSE 0x00000100
3328#define MR_LP_ADV_ASYM_PAUSE 0x00000200
3329#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
3330#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
3331#define MR_LP_ADV_NEXT_PAGE 0x00001000
3332#define MR_TOGGLE_RX 0x00002000
3333#define MR_NP_RX 0x00004000
3334
3335#define MR_LINK_OK 0x80000000
3336
3337 unsigned long link_time, cur_time;
3338
3339 u32 ability_match_cfg;
3340 int ability_match_count;
3341
3342 char ability_match, idle_match, ack_match;
3343
3344 u32 txconfig, rxconfig;
3345#define ANEG_CFG_NP 0x00000080
3346#define ANEG_CFG_ACK 0x00000040
3347#define ANEG_CFG_RF2 0x00000020
3348#define ANEG_CFG_RF1 0x00000010
3349#define ANEG_CFG_PS2 0x00000001
3350#define ANEG_CFG_PS1 0x00008000
3351#define ANEG_CFG_HD 0x00004000
3352#define ANEG_CFG_FD 0x00002000
3353#define ANEG_CFG_INVAL 0x00001f06
3354
3355};
3356#define ANEG_OK 0
3357#define ANEG_DONE 1
3358#define ANEG_TIMER_ENAB 2
3359#define ANEG_FAILED -1
3360
3361#define ANEG_STATE_SETTLE_TIME 10000
3362
3363static int tg3_fiber_aneg_smachine(struct tg3 *tp,
3364 struct tg3_fiber_aneginfo *ap)
3365{
Matt Carlson5be73b42007-12-20 20:09:29 -08003366 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 unsigned long delta;
3368 u32 rx_cfg_reg;
3369 int ret;
3370
3371 if (ap->state == ANEG_STATE_UNKNOWN) {
3372 ap->rxconfig = 0;
3373 ap->link_time = 0;
3374 ap->cur_time = 0;
3375 ap->ability_match_cfg = 0;
3376 ap->ability_match_count = 0;
3377 ap->ability_match = 0;
3378 ap->idle_match = 0;
3379 ap->ack_match = 0;
3380 }
3381 ap->cur_time++;
3382
3383 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
3384 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
3385
3386 if (rx_cfg_reg != ap->ability_match_cfg) {
3387 ap->ability_match_cfg = rx_cfg_reg;
3388 ap->ability_match = 0;
3389 ap->ability_match_count = 0;
3390 } else {
3391 if (++ap->ability_match_count > 1) {
3392 ap->ability_match = 1;
3393 ap->ability_match_cfg = rx_cfg_reg;
3394 }
3395 }
3396 if (rx_cfg_reg & ANEG_CFG_ACK)
3397 ap->ack_match = 1;
3398 else
3399 ap->ack_match = 0;
3400
3401 ap->idle_match = 0;
3402 } else {
3403 ap->idle_match = 1;
3404 ap->ability_match_cfg = 0;
3405 ap->ability_match_count = 0;
3406 ap->ability_match = 0;
3407 ap->ack_match = 0;
3408
3409 rx_cfg_reg = 0;
3410 }
3411
3412 ap->rxconfig = rx_cfg_reg;
3413 ret = ANEG_OK;
3414
3415 switch(ap->state) {
3416 case ANEG_STATE_UNKNOWN:
3417 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
3418 ap->state = ANEG_STATE_AN_ENABLE;
3419
3420 /* fallthru */
3421 case ANEG_STATE_AN_ENABLE:
3422 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
3423 if (ap->flags & MR_AN_ENABLE) {
3424 ap->link_time = 0;
3425 ap->cur_time = 0;
3426 ap->ability_match_cfg = 0;
3427 ap->ability_match_count = 0;
3428 ap->ability_match = 0;
3429 ap->idle_match = 0;
3430 ap->ack_match = 0;
3431
3432 ap->state = ANEG_STATE_RESTART_INIT;
3433 } else {
3434 ap->state = ANEG_STATE_DISABLE_LINK_OK;
3435 }
3436 break;
3437
3438 case ANEG_STATE_RESTART_INIT:
3439 ap->link_time = ap->cur_time;
3440 ap->flags &= ~(MR_NP_LOADED);
3441 ap->txconfig = 0;
3442 tw32(MAC_TX_AUTO_NEG, 0);
3443 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3444 tw32_f(MAC_MODE, tp->mac_mode);
3445 udelay(40);
3446
3447 ret = ANEG_TIMER_ENAB;
3448 ap->state = ANEG_STATE_RESTART;
3449
3450 /* fallthru */
3451 case ANEG_STATE_RESTART:
3452 delta = ap->cur_time - ap->link_time;
3453 if (delta > ANEG_STATE_SETTLE_TIME) {
3454 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
3455 } else {
3456 ret = ANEG_TIMER_ENAB;
3457 }
3458 break;
3459
3460 case ANEG_STATE_DISABLE_LINK_OK:
3461 ret = ANEG_DONE;
3462 break;
3463
3464 case ANEG_STATE_ABILITY_DETECT_INIT:
3465 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08003466 ap->txconfig = ANEG_CFG_FD;
3467 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3468 if (flowctrl & ADVERTISE_1000XPAUSE)
3469 ap->txconfig |= ANEG_CFG_PS1;
3470 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3471 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3473 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3474 tw32_f(MAC_MODE, tp->mac_mode);
3475 udelay(40);
3476
3477 ap->state = ANEG_STATE_ABILITY_DETECT;
3478 break;
3479
3480 case ANEG_STATE_ABILITY_DETECT:
3481 if (ap->ability_match != 0 && ap->rxconfig != 0) {
3482 ap->state = ANEG_STATE_ACK_DETECT_INIT;
3483 }
3484 break;
3485
3486 case ANEG_STATE_ACK_DETECT_INIT:
3487 ap->txconfig |= ANEG_CFG_ACK;
3488 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3489 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3490 tw32_f(MAC_MODE, tp->mac_mode);
3491 udelay(40);
3492
3493 ap->state = ANEG_STATE_ACK_DETECT;
3494
3495 /* fallthru */
3496 case ANEG_STATE_ACK_DETECT:
3497 if (ap->ack_match != 0) {
3498 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
3499 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
3500 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
3501 } else {
3502 ap->state = ANEG_STATE_AN_ENABLE;
3503 }
3504 } else if (ap->ability_match != 0 &&
3505 ap->rxconfig == 0) {
3506 ap->state = ANEG_STATE_AN_ENABLE;
3507 }
3508 break;
3509
3510 case ANEG_STATE_COMPLETE_ACK_INIT:
3511 if (ap->rxconfig & ANEG_CFG_INVAL) {
3512 ret = ANEG_FAILED;
3513 break;
3514 }
3515 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
3516 MR_LP_ADV_HALF_DUPLEX |
3517 MR_LP_ADV_SYM_PAUSE |
3518 MR_LP_ADV_ASYM_PAUSE |
3519 MR_LP_ADV_REMOTE_FAULT1 |
3520 MR_LP_ADV_REMOTE_FAULT2 |
3521 MR_LP_ADV_NEXT_PAGE |
3522 MR_TOGGLE_RX |
3523 MR_NP_RX);
3524 if (ap->rxconfig & ANEG_CFG_FD)
3525 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
3526 if (ap->rxconfig & ANEG_CFG_HD)
3527 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
3528 if (ap->rxconfig & ANEG_CFG_PS1)
3529 ap->flags |= MR_LP_ADV_SYM_PAUSE;
3530 if (ap->rxconfig & ANEG_CFG_PS2)
3531 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
3532 if (ap->rxconfig & ANEG_CFG_RF1)
3533 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
3534 if (ap->rxconfig & ANEG_CFG_RF2)
3535 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
3536 if (ap->rxconfig & ANEG_CFG_NP)
3537 ap->flags |= MR_LP_ADV_NEXT_PAGE;
3538
3539 ap->link_time = ap->cur_time;
3540
3541 ap->flags ^= (MR_TOGGLE_TX);
3542 if (ap->rxconfig & 0x0008)
3543 ap->flags |= MR_TOGGLE_RX;
3544 if (ap->rxconfig & ANEG_CFG_NP)
3545 ap->flags |= MR_NP_RX;
3546 ap->flags |= MR_PAGE_RX;
3547
3548 ap->state = ANEG_STATE_COMPLETE_ACK;
3549 ret = ANEG_TIMER_ENAB;
3550 break;
3551
3552 case ANEG_STATE_COMPLETE_ACK:
3553 if (ap->ability_match != 0 &&
3554 ap->rxconfig == 0) {
3555 ap->state = ANEG_STATE_AN_ENABLE;
3556 break;
3557 }
3558 delta = ap->cur_time - ap->link_time;
3559 if (delta > ANEG_STATE_SETTLE_TIME) {
3560 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
3561 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3562 } else {
3563 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
3564 !(ap->flags & MR_NP_RX)) {
3565 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3566 } else {
3567 ret = ANEG_FAILED;
3568 }
3569 }
3570 }
3571 break;
3572
3573 case ANEG_STATE_IDLE_DETECT_INIT:
3574 ap->link_time = ap->cur_time;
3575 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3576 tw32_f(MAC_MODE, tp->mac_mode);
3577 udelay(40);
3578
3579 ap->state = ANEG_STATE_IDLE_DETECT;
3580 ret = ANEG_TIMER_ENAB;
3581 break;
3582
3583 case ANEG_STATE_IDLE_DETECT:
3584 if (ap->ability_match != 0 &&
3585 ap->rxconfig == 0) {
3586 ap->state = ANEG_STATE_AN_ENABLE;
3587 break;
3588 }
3589 delta = ap->cur_time - ap->link_time;
3590 if (delta > ANEG_STATE_SETTLE_TIME) {
3591 /* XXX another gem from the Broadcom driver :( */
3592 ap->state = ANEG_STATE_LINK_OK;
3593 }
3594 break;
3595
3596 case ANEG_STATE_LINK_OK:
3597 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
3598 ret = ANEG_DONE;
3599 break;
3600
3601 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
3602 /* ??? unimplemented */
3603 break;
3604
3605 case ANEG_STATE_NEXT_PAGE_WAIT:
3606 /* ??? unimplemented */
3607 break;
3608
3609 default:
3610 ret = ANEG_FAILED;
3611 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613
3614 return ret;
3615}
3616
Matt Carlson5be73b42007-12-20 20:09:29 -08003617static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618{
3619 int res = 0;
3620 struct tg3_fiber_aneginfo aninfo;
3621 int status = ANEG_FAILED;
3622 unsigned int tick;
3623 u32 tmp;
3624
3625 tw32_f(MAC_TX_AUTO_NEG, 0);
3626
3627 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
3628 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
3629 udelay(40);
3630
3631 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
3632 udelay(40);
3633
3634 memset(&aninfo, 0, sizeof(aninfo));
3635 aninfo.flags |= MR_AN_ENABLE;
3636 aninfo.state = ANEG_STATE_UNKNOWN;
3637 aninfo.cur_time = 0;
3638 tick = 0;
3639 while (++tick < 195000) {
3640 status = tg3_fiber_aneg_smachine(tp, &aninfo);
3641 if (status == ANEG_DONE || status == ANEG_FAILED)
3642 break;
3643
3644 udelay(1);
3645 }
3646
3647 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3648 tw32_f(MAC_MODE, tp->mac_mode);
3649 udelay(40);
3650
Matt Carlson5be73b42007-12-20 20:09:29 -08003651 *txflags = aninfo.txconfig;
3652 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653
3654 if (status == ANEG_DONE &&
3655 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
3656 MR_LP_ADV_FULL_DUPLEX)))
3657 res = 1;
3658
3659 return res;
3660}
3661
3662static void tg3_init_bcm8002(struct tg3 *tp)
3663{
3664 u32 mac_status = tr32(MAC_STATUS);
3665 int i;
3666
3667 /* Reset when initting first time or we have a link. */
3668 if ((tp->tg3_flags & TG3_FLAG_INIT_COMPLETE) &&
3669 !(mac_status & MAC_STATUS_PCS_SYNCED))
3670 return;
3671
3672 /* Set PLL lock range. */
3673 tg3_writephy(tp, 0x16, 0x8007);
3674
3675 /* SW reset */
3676 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
3677
3678 /* Wait for reset to complete. */
3679 /* XXX schedule_timeout() ... */
3680 for (i = 0; i < 500; i++)
3681 udelay(10);
3682
3683 /* Config mode; select PMA/Ch 1 regs. */
3684 tg3_writephy(tp, 0x10, 0x8411);
3685
3686 /* Enable auto-lock and comdet, select txclk for tx. */
3687 tg3_writephy(tp, 0x11, 0x0a10);
3688
3689 tg3_writephy(tp, 0x18, 0x00a0);
3690 tg3_writephy(tp, 0x16, 0x41ff);
3691
3692 /* Assert and deassert POR. */
3693 tg3_writephy(tp, 0x13, 0x0400);
3694 udelay(40);
3695 tg3_writephy(tp, 0x13, 0x0000);
3696
3697 tg3_writephy(tp, 0x11, 0x0a50);
3698 udelay(40);
3699 tg3_writephy(tp, 0x11, 0x0a10);
3700
3701 /* Wait for signal to stabilize */
3702 /* XXX schedule_timeout() ... */
3703 for (i = 0; i < 15000; i++)
3704 udelay(10);
3705
3706 /* Deselect the channel register so we can read the PHYID
3707 * later.
3708 */
3709 tg3_writephy(tp, 0x10, 0x8011);
3710}
3711
3712static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
3713{
Matt Carlson82cd3d12007-12-20 20:09:00 -08003714 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 u32 sg_dig_ctrl, sg_dig_status;
3716 u32 serdes_cfg, expected_sg_dig_ctrl;
3717 int workaround, port_a;
3718 int current_link_up;
3719
3720 serdes_cfg = 0;
3721 expected_sg_dig_ctrl = 0;
3722 workaround = 0;
3723 port_a = 1;
3724 current_link_up = 0;
3725
3726 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
3727 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
3728 workaround = 1;
3729 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
3730 port_a = 0;
3731
3732 /* preserve bits 0-11,13,14 for signal pre-emphasis */
3733 /* preserve bits 20-23 for voltage regulator */
3734 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
3735 }
3736
3737 sg_dig_ctrl = tr32(SG_DIG_CTRL);
3738
3739 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003740 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741 if (workaround) {
3742 u32 val = serdes_cfg;
3743
3744 if (port_a)
3745 val |= 0xc010000;
3746 else
3747 val |= 0x4010000;
3748 tw32_f(MAC_SERDES_CFG, val);
3749 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003750
3751 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 }
3753 if (mac_status & MAC_STATUS_PCS_SYNCED) {
3754 tg3_setup_flow_control(tp, 0, 0);
3755 current_link_up = 1;
3756 }
3757 goto out;
3758 }
3759
3760 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003761 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762
Matt Carlson82cd3d12007-12-20 20:09:00 -08003763 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3764 if (flowctrl & ADVERTISE_1000XPAUSE)
3765 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
3766 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3767 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768
3769 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003770 if ((tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT) &&
3771 tp->serdes_counter &&
3772 ((mac_status & (MAC_STATUS_PCS_SYNCED |
3773 MAC_STATUS_RCVD_CFG)) ==
3774 MAC_STATUS_PCS_SYNCED)) {
3775 tp->serdes_counter--;
3776 current_link_up = 1;
3777 goto out;
3778 }
3779restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 if (workaround)
3781 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003782 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783 udelay(5);
3784 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
3785
Michael Chan3d3ebe72006-09-27 15:59:15 -07003786 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
3787 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
3789 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003790 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 mac_status = tr32(MAC_STATUS);
3792
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003793 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08003795 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796
Matt Carlson82cd3d12007-12-20 20:09:00 -08003797 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
3798 local_adv |= ADVERTISE_1000XPAUSE;
3799 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
3800 local_adv |= ADVERTISE_1000XPSE_ASYM;
3801
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003802 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003803 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003804 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003805 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806
3807 tg3_setup_flow_control(tp, local_adv, remote_adv);
3808 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003809 tp->serdes_counter = 0;
3810 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003811 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003812 if (tp->serdes_counter)
3813 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 else {
3815 if (workaround) {
3816 u32 val = serdes_cfg;
3817
3818 if (port_a)
3819 val |= 0xc010000;
3820 else
3821 val |= 0x4010000;
3822
3823 tw32_f(MAC_SERDES_CFG, val);
3824 }
3825
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003826 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 udelay(40);
3828
3829 /* Link parallel detection - link is up */
3830 /* only if we have PCS_SYNC and not */
3831 /* receiving config code words */
3832 mac_status = tr32(MAC_STATUS);
3833 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
3834 !(mac_status & MAC_STATUS_RCVD_CFG)) {
3835 tg3_setup_flow_control(tp, 0, 0);
3836 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003837 tp->tg3_flags2 |=
3838 TG3_FLG2_PARALLEL_DETECT;
3839 tp->serdes_counter =
3840 SERDES_PARALLEL_DET_TIMEOUT;
3841 } else
3842 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 }
3844 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07003845 } else {
3846 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
3847 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 }
3849
3850out:
3851 return current_link_up;
3852}
3853
3854static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
3855{
3856 int current_link_up = 0;
3857
Michael Chan5cf64b8a2007-05-05 12:11:21 -07003858 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860
3861 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08003862 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003864
Matt Carlson5be73b42007-12-20 20:09:29 -08003865 if (fiber_autoneg(tp, &txflags, &rxflags)) {
3866 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867
Matt Carlson5be73b42007-12-20 20:09:29 -08003868 if (txflags & ANEG_CFG_PS1)
3869 local_adv |= ADVERTISE_1000XPAUSE;
3870 if (txflags & ANEG_CFG_PS2)
3871 local_adv |= ADVERTISE_1000XPSE_ASYM;
3872
3873 if (rxflags & MR_LP_ADV_SYM_PAUSE)
3874 remote_adv |= LPA_1000XPAUSE;
3875 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
3876 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877
3878 tg3_setup_flow_control(tp, local_adv, remote_adv);
3879
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 current_link_up = 1;
3881 }
3882 for (i = 0; i < 30; i++) {
3883 udelay(20);
3884 tw32_f(MAC_STATUS,
3885 (MAC_STATUS_SYNC_CHANGED |
3886 MAC_STATUS_CFG_CHANGED));
3887 udelay(40);
3888 if ((tr32(MAC_STATUS) &
3889 (MAC_STATUS_SYNC_CHANGED |
3890 MAC_STATUS_CFG_CHANGED)) == 0)
3891 break;
3892 }
3893
3894 mac_status = tr32(MAC_STATUS);
3895 if (current_link_up == 0 &&
3896 (mac_status & MAC_STATUS_PCS_SYNCED) &&
3897 !(mac_status & MAC_STATUS_RCVD_CFG))
3898 current_link_up = 1;
3899 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08003900 tg3_setup_flow_control(tp, 0, 0);
3901
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 /* Forcing 1000FD link up. */
3903 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904
3905 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
3906 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003907
3908 tw32_f(MAC_MODE, tp->mac_mode);
3909 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 }
3911
3912out:
3913 return current_link_up;
3914}
3915
3916static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
3917{
3918 u32 orig_pause_cfg;
3919 u16 orig_active_speed;
3920 u8 orig_active_duplex;
3921 u32 mac_status;
3922 int current_link_up;
3923 int i;
3924
Matt Carlson8d018622007-12-20 20:05:44 -08003925 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 orig_active_speed = tp->link_config.active_speed;
3927 orig_active_duplex = tp->link_config.active_duplex;
3928
3929 if (!(tp->tg3_flags2 & TG3_FLG2_HW_AUTONEG) &&
3930 netif_carrier_ok(tp->dev) &&
3931 (tp->tg3_flags & TG3_FLAG_INIT_COMPLETE)) {
3932 mac_status = tr32(MAC_STATUS);
3933 mac_status &= (MAC_STATUS_PCS_SYNCED |
3934 MAC_STATUS_SIGNAL_DET |
3935 MAC_STATUS_CFG_CHANGED |
3936 MAC_STATUS_RCVD_CFG);
3937 if (mac_status == (MAC_STATUS_PCS_SYNCED |
3938 MAC_STATUS_SIGNAL_DET)) {
3939 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
3940 MAC_STATUS_CFG_CHANGED));
3941 return 0;
3942 }
3943 }
3944
3945 tw32_f(MAC_TX_AUTO_NEG, 0);
3946
3947 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
3948 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
3949 tw32_f(MAC_MODE, tp->mac_mode);
3950 udelay(40);
3951
3952 if (tp->phy_id == PHY_ID_BCM8002)
3953 tg3_init_bcm8002(tp);
3954
3955 /* Enable link change event even when serdes polling. */
3956 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
3957 udelay(40);
3958
3959 current_link_up = 0;
3960 mac_status = tr32(MAC_STATUS);
3961
3962 if (tp->tg3_flags2 & TG3_FLG2_HW_AUTONEG)
3963 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
3964 else
3965 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
3966
Matt Carlson898a56f2009-08-28 14:02:40 +00003967 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00003969 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970
3971 for (i = 0; i < 100; i++) {
3972 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
3973 MAC_STATUS_CFG_CHANGED));
3974 udelay(5);
3975 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07003976 MAC_STATUS_CFG_CHANGED |
3977 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 break;
3979 }
3980
3981 mac_status = tr32(MAC_STATUS);
3982 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
3983 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003984 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
3985 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 tw32_f(MAC_MODE, (tp->mac_mode |
3987 MAC_MODE_SEND_CONFIGS));
3988 udelay(1);
3989 tw32_f(MAC_MODE, tp->mac_mode);
3990 }
3991 }
3992
3993 if (current_link_up == 1) {
3994 tp->link_config.active_speed = SPEED_1000;
3995 tp->link_config.active_duplex = DUPLEX_FULL;
3996 tw32(MAC_LED_CTRL, (tp->led_ctrl |
3997 LED_CTRL_LNKLED_OVERRIDE |
3998 LED_CTRL_1000MBPS_ON));
3999 } else {
4000 tp->link_config.active_speed = SPEED_INVALID;
4001 tp->link_config.active_duplex = DUPLEX_INVALID;
4002 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4003 LED_CTRL_LNKLED_OVERRIDE |
4004 LED_CTRL_TRAFFIC_OVERRIDE));
4005 }
4006
4007 if (current_link_up != netif_carrier_ok(tp->dev)) {
4008 if (current_link_up)
4009 netif_carrier_on(tp->dev);
4010 else
4011 netif_carrier_off(tp->dev);
4012 tg3_link_report(tp);
4013 } else {
Matt Carlson8d018622007-12-20 20:05:44 -08004014 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 if (orig_pause_cfg != now_pause_cfg ||
4016 orig_active_speed != tp->link_config.active_speed ||
4017 orig_active_duplex != tp->link_config.active_duplex)
4018 tg3_link_report(tp);
4019 }
4020
4021 return 0;
4022}
4023
Michael Chan747e8f82005-07-25 12:33:22 -07004024static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
4025{
4026 int current_link_up, err = 0;
4027 u32 bmsr, bmcr;
4028 u16 current_speed;
4029 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08004030 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07004031
4032 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4033 tw32_f(MAC_MODE, tp->mac_mode);
4034 udelay(40);
4035
4036 tw32(MAC_EVENT, 0);
4037
4038 tw32_f(MAC_STATUS,
4039 (MAC_STATUS_SYNC_CHANGED |
4040 MAC_STATUS_CFG_CHANGED |
4041 MAC_STATUS_MI_COMPLETION |
4042 MAC_STATUS_LNKSTATE_CHANGED));
4043 udelay(40);
4044
4045 if (force_reset)
4046 tg3_phy_reset(tp);
4047
4048 current_link_up = 0;
4049 current_speed = SPEED_INVALID;
4050 current_duplex = DUPLEX_INVALID;
4051
4052 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4053 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004054 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
4055 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4056 bmsr |= BMSR_LSTATUS;
4057 else
4058 bmsr &= ~BMSR_LSTATUS;
4059 }
Michael Chan747e8f82005-07-25 12:33:22 -07004060
4061 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
4062
4063 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlson2bd3ed02008-06-09 15:39:55 -07004064 (tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004065 /* do nothing, just check for link up at the end */
4066 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4067 u32 adv, new_adv;
4068
4069 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4070 new_adv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
4071 ADVERTISE_1000XPAUSE |
4072 ADVERTISE_1000XPSE_ASYM |
4073 ADVERTISE_SLCT);
4074
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004075 new_adv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Michael Chan747e8f82005-07-25 12:33:22 -07004076
4077 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
4078 new_adv |= ADVERTISE_1000XHALF;
4079 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
4080 new_adv |= ADVERTISE_1000XFULL;
4081
4082 if ((new_adv != adv) || !(bmcr & BMCR_ANENABLE)) {
4083 tg3_writephy(tp, MII_ADVERTISE, new_adv);
4084 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
4085 tg3_writephy(tp, MII_BMCR, bmcr);
4086
4087 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07004088 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Michael Chan747e8f82005-07-25 12:33:22 -07004089 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
4090
4091 return err;
4092 }
4093 } else {
4094 u32 new_bmcr;
4095
4096 bmcr &= ~BMCR_SPEED1000;
4097 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
4098
4099 if (tp->link_config.duplex == DUPLEX_FULL)
4100 new_bmcr |= BMCR_FULLDPLX;
4101
4102 if (new_bmcr != bmcr) {
4103 /* BMCR_SPEED1000 is a reserved bit that needs
4104 * to be set on write.
4105 */
4106 new_bmcr |= BMCR_SPEED1000;
4107
4108 /* Force a linkdown */
4109 if (netif_carrier_ok(tp->dev)) {
4110 u32 adv;
4111
4112 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4113 adv &= ~(ADVERTISE_1000XFULL |
4114 ADVERTISE_1000XHALF |
4115 ADVERTISE_SLCT);
4116 tg3_writephy(tp, MII_ADVERTISE, adv);
4117 tg3_writephy(tp, MII_BMCR, bmcr |
4118 BMCR_ANRESTART |
4119 BMCR_ANENABLE);
4120 udelay(10);
4121 netif_carrier_off(tp->dev);
4122 }
4123 tg3_writephy(tp, MII_BMCR, new_bmcr);
4124 bmcr = new_bmcr;
4125 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4126 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004127 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
4128 ASIC_REV_5714) {
4129 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4130 bmsr |= BMSR_LSTATUS;
4131 else
4132 bmsr &= ~BMSR_LSTATUS;
4133 }
Michael Chan747e8f82005-07-25 12:33:22 -07004134 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
4135 }
4136 }
4137
4138 if (bmsr & BMSR_LSTATUS) {
4139 current_speed = SPEED_1000;
4140 current_link_up = 1;
4141 if (bmcr & BMCR_FULLDPLX)
4142 current_duplex = DUPLEX_FULL;
4143 else
4144 current_duplex = DUPLEX_HALF;
4145
Matt Carlsonef167e22007-12-20 20:10:01 -08004146 local_adv = 0;
4147 remote_adv = 0;
4148
Michael Chan747e8f82005-07-25 12:33:22 -07004149 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08004150 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07004151
4152 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
4153 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
4154 common = local_adv & remote_adv;
4155 if (common & (ADVERTISE_1000XHALF |
4156 ADVERTISE_1000XFULL)) {
4157 if (common & ADVERTISE_1000XFULL)
4158 current_duplex = DUPLEX_FULL;
4159 else
4160 current_duplex = DUPLEX_HALF;
Michael Chan747e8f82005-07-25 12:33:22 -07004161 }
4162 else
4163 current_link_up = 0;
4164 }
4165 }
4166
Matt Carlsonef167e22007-12-20 20:10:01 -08004167 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
4168 tg3_setup_flow_control(tp, local_adv, remote_adv);
4169
Michael Chan747e8f82005-07-25 12:33:22 -07004170 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4171 if (tp->link_config.active_duplex == DUPLEX_HALF)
4172 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4173
4174 tw32_f(MAC_MODE, tp->mac_mode);
4175 udelay(40);
4176
4177 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4178
4179 tp->link_config.active_speed = current_speed;
4180 tp->link_config.active_duplex = current_duplex;
4181
4182 if (current_link_up != netif_carrier_ok(tp->dev)) {
4183 if (current_link_up)
4184 netif_carrier_on(tp->dev);
4185 else {
4186 netif_carrier_off(tp->dev);
4187 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
4188 }
4189 tg3_link_report(tp);
4190 }
4191 return err;
4192}
4193
4194static void tg3_serdes_parallel_detect(struct tg3 *tp)
4195{
Michael Chan3d3ebe72006-09-27 15:59:15 -07004196 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07004197 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07004198 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07004199 return;
4200 }
4201 if (!netif_carrier_ok(tp->dev) &&
4202 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
4203 u32 bmcr;
4204
4205 tg3_readphy(tp, MII_BMCR, &bmcr);
4206 if (bmcr & BMCR_ANENABLE) {
4207 u32 phy1, phy2;
4208
4209 /* Select shadow register 0x1f */
4210 tg3_writephy(tp, 0x1c, 0x7c00);
4211 tg3_readphy(tp, 0x1c, &phy1);
4212
4213 /* Select expansion interrupt status register */
4214 tg3_writephy(tp, 0x17, 0x0f01);
4215 tg3_readphy(tp, 0x15, &phy2);
4216 tg3_readphy(tp, 0x15, &phy2);
4217
4218 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
4219 /* We have signal detect and not receiving
4220 * config code words, link is up by parallel
4221 * detection.
4222 */
4223
4224 bmcr &= ~BMCR_ANENABLE;
4225 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
4226 tg3_writephy(tp, MII_BMCR, bmcr);
4227 tp->tg3_flags2 |= TG3_FLG2_PARALLEL_DETECT;
4228 }
4229 }
4230 }
4231 else if (netif_carrier_ok(tp->dev) &&
4232 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
4233 (tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT)) {
4234 u32 phy2;
4235
4236 /* Select expansion interrupt status register */
4237 tg3_writephy(tp, 0x17, 0x0f01);
4238 tg3_readphy(tp, 0x15, &phy2);
4239 if (phy2 & 0x20) {
4240 u32 bmcr;
4241
4242 /* Config code words received, turn on autoneg. */
4243 tg3_readphy(tp, MII_BMCR, &bmcr);
4244 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
4245
4246 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
4247
4248 }
4249 }
4250}
4251
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252static int tg3_setup_phy(struct tg3 *tp, int force_reset)
4253{
4254 int err;
4255
4256 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
4257 err = tg3_setup_fiber_phy(tp, force_reset);
Michael Chan747e8f82005-07-25 12:33:22 -07004258 } else if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES) {
4259 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 } else {
4261 err = tg3_setup_copper_phy(tp, force_reset);
4262 }
4263
Matt Carlsonbcb37f62008-11-03 16:52:09 -08004264 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08004265 u32 val, scale;
4266
4267 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
4268 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
4269 scale = 65;
4270 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
4271 scale = 6;
4272 else
4273 scale = 12;
4274
4275 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
4276 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
4277 tw32(GRC_MISC_CFG, val);
4278 }
4279
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 if (tp->link_config.active_speed == SPEED_1000 &&
4281 tp->link_config.active_duplex == DUPLEX_HALF)
4282 tw32(MAC_TX_LENGTHS,
4283 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4284 (6 << TX_LENGTHS_IPG_SHIFT) |
4285 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
4286 else
4287 tw32(MAC_TX_LENGTHS,
4288 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4289 (6 << TX_LENGTHS_IPG_SHIFT) |
4290 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
4291
4292 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
4293 if (netif_carrier_ok(tp->dev)) {
4294 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07004295 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 } else {
4297 tw32(HOSTCC_STAT_COAL_TICKS, 0);
4298 }
4299 }
4300
Matt Carlson8ed5d972007-05-07 00:25:49 -07004301 if (tp->tg3_flags & TG3_FLAG_ASPM_WORKAROUND) {
4302 u32 val = tr32(PCIE_PWR_MGMT_THRESH);
4303 if (!netif_carrier_ok(tp->dev))
4304 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
4305 tp->pwrmgmt_thresh;
4306 else
4307 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
4308 tw32(PCIE_PWR_MGMT_THRESH, val);
4309 }
4310
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 return err;
4312}
4313
Michael Chandf3e6542006-05-26 17:48:07 -07004314/* This is called whenever we suspect that the system chipset is re-
4315 * ordering the sequence of MMIO to the tx send mailbox. The symptom
4316 * is bogus tx completions. We try to recover by setting the
4317 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
4318 * in the workqueue.
4319 */
4320static void tg3_tx_recover(struct tg3 *tp)
4321{
4322 BUG_ON((tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER) ||
4323 tp->write32_tx_mbox == tg3_write_indirect_mbox);
4324
4325 printk(KERN_WARNING PFX "%s: The system may be re-ordering memory-"
4326 "mapped I/O cycles to the network device, attempting to "
4327 "recover. Please report the problem to the driver maintainer "
4328 "and include system chipset information.\n", tp->dev->name);
4329
4330 spin_lock(&tp->lock);
Michael Chandf3e6542006-05-26 17:48:07 -07004331 tp->tg3_flags |= TG3_FLAG_TX_RECOVERY_PENDING;
Michael Chandf3e6542006-05-26 17:48:07 -07004332 spin_unlock(&tp->lock);
4333}
4334
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004335static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07004336{
4337 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004338 return tnapi->tx_pending -
4339 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07004340}
4341
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342/* Tigon3 never reports partial packet sends. So we do not
4343 * need special logic to handle SKBs that have not had all
4344 * of their frags sent yet, like SunGEM does.
4345 */
Matt Carlson17375d22009-08-28 14:02:18 +00004346static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347{
Matt Carlson17375d22009-08-28 14:02:18 +00004348 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00004349 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004350 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004351 struct netdev_queue *txq;
4352 int index = tnapi - tp->napi;
4353
4354 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
4355 index--;
4356
4357 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358
4359 while (sw_idx != hw_idx) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004360 struct tx_ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07004362 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363
Michael Chandf3e6542006-05-26 17:48:07 -07004364 if (unlikely(skb == NULL)) {
4365 tg3_tx_recover(tp);
4366 return;
4367 }
4368
David S. Miller90079ce2008-09-11 04:52:51 -07004369 skb_dma_unmap(&tp->pdev->dev, skb, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370
4371 ri->skb = NULL;
4372
4373 sw_idx = NEXT_TX(sw_idx);
4374
4375 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004376 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07004377 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
4378 tx_bug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 sw_idx = NEXT_TX(sw_idx);
4380 }
4381
David S. Millerf47c11e2005-06-24 20:18:35 -07004382 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07004383
4384 if (unlikely(tx_bug)) {
4385 tg3_tx_recover(tp);
4386 return;
4387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388 }
4389
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004390 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391
Michael Chan1b2a7202006-08-07 21:46:02 -07004392 /* Need to make the tx_cons update visible to tg3_start_xmit()
4393 * before checking for netif_queue_stopped(). Without the
4394 * memory barrier, there is a small possibility that tg3_start_xmit()
4395 * will miss it and cause the queue to be stopped forever.
4396 */
4397 smp_mb();
4398
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004399 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004400 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004401 __netif_tx_lock(txq, smp_processor_id());
4402 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004403 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004404 netif_tx_wake_queue(txq);
4405 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07004406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407}
4408
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004409static void tg3_rx_skb_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
4410{
4411 if (!ri->skb)
4412 return;
4413
4414 pci_unmap_single(tp->pdev, pci_unmap_addr(ri, mapping),
4415 map_sz, PCI_DMA_FROMDEVICE);
4416 dev_kfree_skb_any(ri->skb);
4417 ri->skb = NULL;
4418}
4419
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420/* Returns size of skb allocated or < 0 on error.
4421 *
4422 * We only need to fill in the address because the other members
4423 * of the RX descriptor are invariant, see tg3_init_rings.
4424 *
4425 * Note the purposeful assymetry of cpu vs. chip accesses. For
4426 * posting buffers we only dirty the first cache line of the RX
4427 * descriptor (containing the address). Whereas for the RX status
4428 * buffers the cpu only reads the last cacheline of the RX descriptor
4429 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
4430 */
Matt Carlson86b21e52009-11-13 13:03:45 +00004431static int tg3_alloc_rx_skb(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Matt Carlsona3896162009-11-13 13:03:44 +00004432 u32 opaque_key, u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433{
4434 struct tg3_rx_buffer_desc *desc;
4435 struct ring_info *map, *src_map;
4436 struct sk_buff *skb;
4437 dma_addr_t mapping;
4438 int skb_size, dest_idx;
4439
4440 src_map = NULL;
4441 switch (opaque_key) {
4442 case RXD_OPAQUE_RING_STD:
4443 dest_idx = dest_idx_unmasked % TG3_RX_RING_SIZE;
Matt Carlson21f581a2009-08-28 14:00:25 +00004444 desc = &tpr->rx_std[dest_idx];
4445 map = &tpr->rx_std_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004446 skb_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 break;
4448
4449 case RXD_OPAQUE_RING_JUMBO:
4450 dest_idx = dest_idx_unmasked % TG3_RX_JUMBO_RING_SIZE;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00004451 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00004452 map = &tpr->rx_jmb_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004453 skb_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 break;
4455
4456 default:
4457 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459
4460 /* Do not overwrite any of the map or rp information
4461 * until we are sure we can commit to a new buffer.
4462 *
4463 * Callers depend upon this behavior and assume that
4464 * we leave everything unchanged if we fail.
4465 */
Matt Carlson287be122009-08-28 13:58:46 +00004466 skb = netdev_alloc_skb(tp->dev, skb_size + tp->rx_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467 if (skb == NULL)
4468 return -ENOMEM;
4469
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 skb_reserve(skb, tp->rx_offset);
4471
Matt Carlson287be122009-08-28 13:58:46 +00004472 mapping = pci_map_single(tp->pdev, skb->data, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 PCI_DMA_FROMDEVICE);
Matt Carlsona21771d2009-11-02 14:25:31 +00004474 if (pci_dma_mapping_error(tp->pdev, mapping)) {
4475 dev_kfree_skb(skb);
4476 return -EIO;
4477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478
4479 map->skb = skb;
4480 pci_unmap_addr_set(map, mapping, mapping);
4481
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 desc->addr_hi = ((u64)mapping >> 32);
4483 desc->addr_lo = ((u64)mapping & 0xffffffff);
4484
4485 return skb_size;
4486}
4487
4488/* We only need to move over in the address because the other
4489 * members of the RX descriptor are invariant. See notes above
4490 * tg3_alloc_rx_skb for full details.
4491 */
Matt Carlsona3896162009-11-13 13:03:44 +00004492static void tg3_recycle_rx(struct tg3_napi *tnapi,
4493 struct tg3_rx_prodring_set *dpr,
4494 u32 opaque_key, int src_idx,
4495 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496{
Matt Carlson17375d22009-08-28 14:02:18 +00004497 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
4499 struct ring_info *src_map, *dest_map;
4500 int dest_idx;
Matt Carlsona3896162009-11-13 13:03:44 +00004501 struct tg3_rx_prodring_set *spr = &tp->prodring[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502
4503 switch (opaque_key) {
4504 case RXD_OPAQUE_RING_STD:
4505 dest_idx = dest_idx_unmasked % TG3_RX_RING_SIZE;
Matt Carlsona3896162009-11-13 13:03:44 +00004506 dest_desc = &dpr->rx_std[dest_idx];
4507 dest_map = &dpr->rx_std_buffers[dest_idx];
4508 src_desc = &spr->rx_std[src_idx];
4509 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 break;
4511
4512 case RXD_OPAQUE_RING_JUMBO:
4513 dest_idx = dest_idx_unmasked % TG3_RX_JUMBO_RING_SIZE;
Matt Carlsona3896162009-11-13 13:03:44 +00004514 dest_desc = &dpr->rx_jmb[dest_idx].std;
4515 dest_map = &dpr->rx_jmb_buffers[dest_idx];
4516 src_desc = &spr->rx_jmb[src_idx].std;
4517 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 break;
4519
4520 default:
4521 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523
4524 dest_map->skb = src_map->skb;
4525 pci_unmap_addr_set(dest_map, mapping,
4526 pci_unmap_addr(src_map, mapping));
4527 dest_desc->addr_hi = src_desc->addr_hi;
4528 dest_desc->addr_lo = src_desc->addr_lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 src_map->skb = NULL;
4530}
4531
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532/* The RX ring scheme is composed of multiple rings which post fresh
4533 * buffers to the chip, and one special ring the chip uses to report
4534 * status back to the host.
4535 *
4536 * The special ring reports the status of received packets to the
4537 * host. The chip does not write into the original descriptor the
4538 * RX buffer was obtained from. The chip simply takes the original
4539 * descriptor as provided by the host, updates the status and length
4540 * field, then writes this into the next status ring entry.
4541 *
4542 * Each ring the host uses to post buffers to the chip is described
4543 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
4544 * it is first placed into the on-chip ram. When the packet's length
4545 * is known, it walks down the TG3_BDINFO entries to select the ring.
4546 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
4547 * which is within the range of the new packet's length is chosen.
4548 *
4549 * The "separate ring for rx status" scheme may sound queer, but it makes
4550 * sense from a cache coherency perspective. If only the host writes
4551 * to the buffer post rings, and only the chip writes to the rx status
4552 * rings, then cache lines never move beyond shared-modified state.
4553 * If both the host and chip were to write into the same ring, cache line
4554 * eviction could occur since both entities want it in an exclusive state.
4555 */
Matt Carlson17375d22009-08-28 14:02:18 +00004556static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557{
Matt Carlson17375d22009-08-28 14:02:18 +00004558 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07004559 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00004560 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00004561 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07004562 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 int received;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004564 struct tg3_rx_prodring_set *tpr = tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004566 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567 /*
4568 * We need to order the read of hw_idx and the read of
4569 * the opaque cookie.
4570 */
4571 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 work_mask = 0;
4573 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00004574 std_prod_idx = tpr->rx_std_prod_idx;
4575 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00004577 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00004578 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 unsigned int len;
4580 struct sk_buff *skb;
4581 dma_addr_t dma_addr;
4582 u32 opaque_key, desc_idx, *post_ptr;
4583
4584 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
4585 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
4586 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004587 ri = &tp->prodring[0].rx_std_buffers[desc_idx];
Matt Carlson21f581a2009-08-28 14:00:25 +00004588 dma_addr = pci_unmap_addr(ri, mapping);
4589 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00004590 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07004591 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004593 ri = &tp->prodring[0].rx_jmb_buffers[desc_idx];
Matt Carlson21f581a2009-08-28 14:00:25 +00004594 dma_addr = pci_unmap_addr(ri, mapping);
4595 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00004596 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00004597 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599
4600 work_mask |= opaque_key;
4601
4602 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
4603 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
4604 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00004605 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 desc_idx, *post_ptr);
4607 drop_it_no_recycle:
4608 /* Other statistics kept track of by card. */
4609 tp->net_stats.rx_dropped++;
4610 goto next_pkt;
4611 }
4612
Matt Carlsonad829262008-11-21 17:16:16 -08004613 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
4614 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004616 if (len > RX_COPY_THRESHOLD
Matt Carlsonad829262008-11-21 17:16:16 -08004617 && tp->rx_offset == NET_IP_ALIGN
4618 /* rx_offset will likely not equal NET_IP_ALIGN
4619 * if this is a 5701 card running in PCI-X mode
4620 * [see tg3_get_invariants()]
4621 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 ) {
4623 int skb_size;
4624
Matt Carlson86b21e52009-11-13 13:03:45 +00004625 skb_size = tg3_alloc_rx_skb(tp, tpr, opaque_key,
Matt Carlsonafc081f2009-11-13 13:03:43 +00004626 *post_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627 if (skb_size < 0)
4628 goto drop_it;
4629
Matt Carlsonafc081f2009-11-13 13:03:43 +00004630 ri->skb = NULL;
4631
Matt Carlson287be122009-08-28 13:58:46 +00004632 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633 PCI_DMA_FROMDEVICE);
4634
4635 skb_put(skb, len);
4636 } else {
4637 struct sk_buff *copy_skb;
4638
Matt Carlsona3896162009-11-13 13:03:44 +00004639 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 desc_idx, *post_ptr);
4641
Matt Carlsonad829262008-11-21 17:16:16 -08004642 copy_skb = netdev_alloc_skb(tp->dev,
4643 len + TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644 if (copy_skb == NULL)
4645 goto drop_it_no_recycle;
4646
Matt Carlsonad829262008-11-21 17:16:16 -08004647 skb_reserve(copy_skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 skb_put(copy_skb, len);
4649 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03004650 skb_copy_from_linear_data(skb, copy_skb->data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
4652
4653 /* We'll reuse the original ring buffer. */
4654 skb = copy_skb;
4655 }
4656
4657 if ((tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) &&
4658 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
4659 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
4660 >> RXD_TCPCSUM_SHIFT) == 0xffff))
4661 skb->ip_summed = CHECKSUM_UNNECESSARY;
4662 else
4663 skb->ip_summed = CHECKSUM_NONE;
4664
4665 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00004666
4667 if (len > (tp->dev->mtu + ETH_HLEN) &&
4668 skb->protocol != htons(ETH_P_8021Q)) {
4669 dev_kfree_skb(skb);
4670 goto next_pkt;
4671 }
4672
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673#if TG3_VLAN_TAG_USED
4674 if (tp->vlgrp != NULL &&
4675 desc->type_flags & RXD_FLAG_VLAN) {
Matt Carlson17375d22009-08-28 14:02:18 +00004676 vlan_gro_receive(&tnapi->napi, tp->vlgrp,
Matt Carlson8ef04422009-08-28 14:01:37 +00004677 desc->err_vlan & RXD_VLAN_MASK, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 } else
4679#endif
Matt Carlson17375d22009-08-28 14:02:18 +00004680 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 received++;
4683 budget--;
4684
4685next_pkt:
4686 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07004687
4688 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
4689 u32 idx = *post_ptr % TG3_RX_RING_SIZE;
Matt Carlson66711e62009-11-13 13:03:49 +00004690 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, idx);
Michael Chanf92905d2006-06-29 20:14:29 -07004691 work_mask &= ~RXD_OPAQUE_RING_STD;
4692 rx_std_posted = 0;
4693 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07004695 sw_idx++;
Eric Dumazet6b31a512007-02-06 13:29:21 -08004696 sw_idx &= (TG3_RX_RCB_RING_SIZE(tp) - 1);
Michael Chan52f6d692005-04-25 15:14:32 -07004697
4698 /* Refresh hw_idx to see if there is new work */
4699 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004700 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07004701 rmb();
4702 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 }
4704
4705 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00004706 tnapi->rx_rcb_ptr = sw_idx;
4707 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708
4709 /* Refill RX ring(s). */
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004710 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS) || tnapi == &tp->napi[1]) {
4711 if (work_mask & RXD_OPAQUE_RING_STD) {
4712 tpr->rx_std_prod_idx = std_prod_idx % TG3_RX_RING_SIZE;
4713 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
4714 tpr->rx_std_prod_idx);
4715 }
4716 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
4717 tpr->rx_jmb_prod_idx = jmb_prod_idx %
4718 TG3_RX_JUMBO_RING_SIZE;
4719 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
4720 tpr->rx_jmb_prod_idx);
4721 }
4722 mmiowb();
4723 } else if (work_mask) {
4724 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
4725 * updated before the producer indices can be updated.
4726 */
4727 smp_wmb();
4728
Matt Carlson43619352009-11-13 13:03:47 +00004729 tpr->rx_std_prod_idx = std_prod_idx % TG3_RX_RING_SIZE;
Matt Carlson43619352009-11-13 13:03:47 +00004730 tpr->rx_jmb_prod_idx = jmb_prod_idx % TG3_RX_JUMBO_RING_SIZE;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004731
4732 napi_schedule(&tp->napi[1].napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734
4735 return received;
4736}
4737
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004738static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 /* handle link change and other phy events */
4741 if (!(tp->tg3_flags &
4742 (TG3_FLAG_USE_LINKCHG_REG |
4743 TG3_FLAG_POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004744 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
4745
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746 if (sblk->status & SD_STATUS_LINK_CHG) {
4747 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004748 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07004749 spin_lock(&tp->lock);
Matt Carlsondd477002008-05-25 23:45:58 -07004750 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
4751 tw32_f(MAC_STATUS,
4752 (MAC_STATUS_SYNC_CHANGED |
4753 MAC_STATUS_CFG_CHANGED |
4754 MAC_STATUS_MI_COMPLETION |
4755 MAC_STATUS_LNKSTATE_CHANGED));
4756 udelay(40);
4757 } else
4758 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07004759 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 }
4761 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004762}
4763
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004764static void tg3_rx_prodring_xfer(struct tg3 *tp,
4765 struct tg3_rx_prodring_set *dpr,
4766 struct tg3_rx_prodring_set *spr)
4767{
4768 u32 si, di, cpycnt, src_prod_idx;
4769 int i;
4770
4771 while (1) {
4772 src_prod_idx = spr->rx_std_prod_idx;
4773
4774 /* Make sure updates to the rx_std_buffers[] entries and the
4775 * standard producer index are seen in the correct order.
4776 */
4777 smp_rmb();
4778
4779 if (spr->rx_std_cons_idx == src_prod_idx)
4780 break;
4781
4782 if (spr->rx_std_cons_idx < src_prod_idx)
4783 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
4784 else
4785 cpycnt = TG3_RX_RING_SIZE - spr->rx_std_cons_idx;
4786
4787 cpycnt = min(cpycnt, TG3_RX_RING_SIZE - dpr->rx_std_prod_idx);
4788
4789 si = spr->rx_std_cons_idx;
4790 di = dpr->rx_std_prod_idx;
4791
4792 memcpy(&dpr->rx_std_buffers[di],
4793 &spr->rx_std_buffers[si],
4794 cpycnt * sizeof(struct ring_info));
4795
4796 for (i = 0; i < cpycnt; i++, di++, si++) {
4797 struct tg3_rx_buffer_desc *sbd, *dbd;
4798 sbd = &spr->rx_std[si];
4799 dbd = &dpr->rx_std[di];
4800 dbd->addr_hi = sbd->addr_hi;
4801 dbd->addr_lo = sbd->addr_lo;
4802 }
4803
4804 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) %
4805 TG3_RX_RING_SIZE;
4806 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) %
4807 TG3_RX_RING_SIZE;
4808 }
4809
4810 while (1) {
4811 src_prod_idx = spr->rx_jmb_prod_idx;
4812
4813 /* Make sure updates to the rx_jmb_buffers[] entries and
4814 * the jumbo producer index are seen in the correct order.
4815 */
4816 smp_rmb();
4817
4818 if (spr->rx_jmb_cons_idx == src_prod_idx)
4819 break;
4820
4821 if (spr->rx_jmb_cons_idx < src_prod_idx)
4822 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
4823 else
4824 cpycnt = TG3_RX_JUMBO_RING_SIZE - spr->rx_jmb_cons_idx;
4825
4826 cpycnt = min(cpycnt,
4827 TG3_RX_JUMBO_RING_SIZE - dpr->rx_jmb_prod_idx);
4828
4829 si = spr->rx_jmb_cons_idx;
4830 di = dpr->rx_jmb_prod_idx;
4831
4832 memcpy(&dpr->rx_jmb_buffers[di],
4833 &spr->rx_jmb_buffers[si],
4834 cpycnt * sizeof(struct ring_info));
4835
4836 for (i = 0; i < cpycnt; i++, di++, si++) {
4837 struct tg3_rx_buffer_desc *sbd, *dbd;
4838 sbd = &spr->rx_jmb[si].std;
4839 dbd = &dpr->rx_jmb[di].std;
4840 dbd->addr_hi = sbd->addr_hi;
4841 dbd->addr_lo = sbd->addr_lo;
4842 }
4843
4844 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) %
4845 TG3_RX_JUMBO_RING_SIZE;
4846 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) %
4847 TG3_RX_JUMBO_RING_SIZE;
4848 }
4849}
4850
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004851static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
4852{
4853 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854
4855 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004856 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00004857 tg3_tx(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07004858 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
Michael Chan4fd7ab52007-10-12 01:39:50 -07004859 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860 }
4861
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862 /* run RX thread, within the bounds set by NAPI.
4863 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004864 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004866 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00004867 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004869 if ((tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS) && tnapi == &tp->napi[1]) {
4870 int i;
4871 u32 std_prod_idx = tp->prodring[0].rx_std_prod_idx;
4872 u32 jmb_prod_idx = tp->prodring[0].rx_jmb_prod_idx;
4873
4874 for (i = 2; i < tp->irq_cnt; i++)
4875 tg3_rx_prodring_xfer(tp, tnapi->prodring,
4876 tp->napi[i].prodring);
4877
4878 wmb();
4879
4880 if (std_prod_idx != tp->prodring[0].rx_std_prod_idx) {
4881 u32 mbox = TG3_RX_STD_PROD_IDX_REG;
4882 tw32_rx_mbox(mbox, tp->prodring[0].rx_std_prod_idx);
4883 }
4884
4885 if (jmb_prod_idx != tp->prodring[0].rx_jmb_prod_idx) {
4886 u32 mbox = TG3_RX_JMB_PROD_IDX_REG;
4887 tw32_rx_mbox(mbox, tp->prodring[0].rx_jmb_prod_idx);
4888 }
4889
4890 mmiowb();
4891 }
4892
David S. Miller6f535762007-10-11 18:08:29 -07004893 return work_done;
4894}
David S. Millerf7383c22005-05-18 22:50:53 -07004895
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004896static int tg3_poll_msix(struct napi_struct *napi, int budget)
4897{
4898 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
4899 struct tg3 *tp = tnapi->tp;
4900 int work_done = 0;
4901 struct tg3_hw_status *sblk = tnapi->hw_status;
4902
4903 while (1) {
4904 work_done = tg3_poll_work(tnapi, work_done, budget);
4905
4906 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
4907 goto tx_recovery;
4908
4909 if (unlikely(work_done >= budget))
4910 break;
4911
4912 /* tp->last_tag is used in tg3_restart_ints() below
4913 * to tell the hw how much work has been processed,
4914 * so we must read it before checking for more work.
4915 */
4916 tnapi->last_tag = sblk->status_tag;
4917 tnapi->last_irq_tag = tnapi->last_tag;
4918 rmb();
4919
4920 /* check for RX/TX work to do */
4921 if (sblk->idx[0].tx_consumer == tnapi->tx_cons &&
4922 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr) {
4923 napi_complete(napi);
4924 /* Reenable interrupts. */
4925 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
4926 mmiowb();
4927 break;
4928 }
4929 }
4930
4931 return work_done;
4932
4933tx_recovery:
4934 /* work_done is guaranteed to be less than budget. */
4935 napi_complete(napi);
4936 schedule_work(&tp->reset_task);
4937 return work_done;
4938}
4939
David S. Miller6f535762007-10-11 18:08:29 -07004940static int tg3_poll(struct napi_struct *napi, int budget)
4941{
Matt Carlson8ef04422009-08-28 14:01:37 +00004942 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
4943 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07004944 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00004945 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07004946
4947 while (1) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004948 tg3_poll_link(tp);
4949
Matt Carlson17375d22009-08-28 14:02:18 +00004950 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07004951
4952 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
4953 goto tx_recovery;
4954
4955 if (unlikely(work_done >= budget))
4956 break;
4957
Michael Chan4fd7ab52007-10-12 01:39:50 -07004958 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) {
Matt Carlson17375d22009-08-28 14:02:18 +00004959 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07004960 * to tell the hw how much work has been processed,
4961 * so we must read it before checking for more work.
4962 */
Matt Carlson898a56f2009-08-28 14:02:40 +00004963 tnapi->last_tag = sblk->status_tag;
4964 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07004965 rmb();
4966 } else
4967 sblk->status &= ~SD_STATUS_UPDATED;
4968
Matt Carlson17375d22009-08-28 14:02:18 +00004969 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08004970 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00004971 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07004972 break;
4973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 }
4975
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004976 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07004977
4978tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07004979 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08004980 napi_complete(napi);
David S. Miller6f535762007-10-11 18:08:29 -07004981 schedule_work(&tp->reset_task);
Michael Chan4fd7ab52007-10-12 01:39:50 -07004982 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983}
4984
David S. Millerf47c11e2005-06-24 20:18:35 -07004985static void tg3_irq_quiesce(struct tg3 *tp)
4986{
Matt Carlson4f125f42009-09-01 12:55:02 +00004987 int i;
4988
David S. Millerf47c11e2005-06-24 20:18:35 -07004989 BUG_ON(tp->irq_sync);
4990
4991 tp->irq_sync = 1;
4992 smp_mb();
4993
Matt Carlson4f125f42009-09-01 12:55:02 +00004994 for (i = 0; i < tp->irq_cnt; i++)
4995 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07004996}
4997
4998static inline int tg3_irq_sync(struct tg3 *tp)
4999{
5000 return tp->irq_sync;
5001}
5002
5003/* Fully shutdown all tg3 driver activity elsewhere in the system.
5004 * If irq_sync is non-zero, then the IRQ handler must be synchronized
5005 * with as well. Most of the time, this is not necessary except when
5006 * shutting down the device.
5007 */
5008static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
5009{
Michael Chan46966542007-07-11 19:47:19 -07005010 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07005011 if (irq_sync)
5012 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07005013}
5014
5015static inline void tg3_full_unlock(struct tg3 *tp)
5016{
David S. Millerf47c11e2005-06-24 20:18:35 -07005017 spin_unlock_bh(&tp->lock);
5018}
5019
Michael Chanfcfa0a32006-03-20 22:28:41 -08005020/* One-shot MSI handler - Chip automatically disables interrupt
5021 * after sending MSI so driver doesn't have to do it.
5022 */
David Howells7d12e782006-10-05 14:55:46 +01005023static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08005024{
Matt Carlson09943a12009-08-28 14:01:57 +00005025 struct tg3_napi *tnapi = dev_id;
5026 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08005027
Matt Carlson898a56f2009-08-28 14:02:40 +00005028 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005029 if (tnapi->rx_rcb)
5030 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005031
5032 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005033 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005034
5035 return IRQ_HANDLED;
5036}
5037
Michael Chan88b06bc22005-04-21 17:13:25 -07005038/* MSI ISR - No need to check for interrupt sharing and no need to
5039 * flush status block and interrupt mailbox. PCI ordering rules
5040 * guarantee that MSI will arrive after the status block.
5041 */
David Howells7d12e782006-10-05 14:55:46 +01005042static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07005043{
Matt Carlson09943a12009-08-28 14:01:57 +00005044 struct tg3_napi *tnapi = dev_id;
5045 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07005046
Matt Carlson898a56f2009-08-28 14:02:40 +00005047 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005048 if (tnapi->rx_rcb)
5049 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07005050 /*
David S. Millerfac9b832005-05-18 22:46:34 -07005051 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07005052 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07005053 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07005054 * NIC to stop sending us irqs, engaging "in-intr-handler"
5055 * event coalescing.
5056 */
5057 tw32_mailbox(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07005058 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005059 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07005060
Michael Chan88b06bc22005-04-21 17:13:25 -07005061 return IRQ_RETVAL(1);
5062}
5063
David Howells7d12e782006-10-05 14:55:46 +01005064static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065{
Matt Carlson09943a12009-08-28 14:01:57 +00005066 struct tg3_napi *tnapi = dev_id;
5067 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005068 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 unsigned int handled = 1;
5070
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071 /* In INTx mode, it is possible for the interrupt to arrive at
5072 * the CPU before the status block posted prior to the interrupt.
5073 * Reading the PCI State register will confirm whether the
5074 * interrupt is ours and will flush the status block.
5075 */
Michael Chand18edcb2007-03-24 20:57:11 -07005076 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
5077 if ((tp->tg3_flags & TG3_FLAG_CHIP_RESETTING) ||
5078 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5079 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005080 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07005081 }
Michael Chand18edcb2007-03-24 20:57:11 -07005082 }
5083
5084 /*
5085 * Writing any value to intr-mbox-0 clears PCI INTA# and
5086 * chip-internal interrupt pending events.
5087 * Writing non-zero to intr-mbox-0 additional tells the
5088 * NIC to stop sending us irqs, engaging "in-intr-handler"
5089 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005090 *
5091 * Flush the mailbox to de-assert the IRQ immediately to prevent
5092 * spurious interrupts. The flush impacts performance but
5093 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005094 */
Michael Chanc04cb342007-05-07 00:26:15 -07005095 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07005096 if (tg3_irq_sync(tp))
5097 goto out;
5098 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00005099 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00005100 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00005101 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07005102 } else {
5103 /* No work, shared interrupt perhaps? re-enable
5104 * interrupts, and flush that PCI write
5105 */
5106 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
5107 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07005108 }
David S. Millerf47c11e2005-06-24 20:18:35 -07005109out:
David S. Millerfac9b832005-05-18 22:46:34 -07005110 return IRQ_RETVAL(handled);
5111}
5112
David Howells7d12e782006-10-05 14:55:46 +01005113static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07005114{
Matt Carlson09943a12009-08-28 14:01:57 +00005115 struct tg3_napi *tnapi = dev_id;
5116 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005117 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07005118 unsigned int handled = 1;
5119
David S. Millerfac9b832005-05-18 22:46:34 -07005120 /* In INTx mode, it is possible for the interrupt to arrive at
5121 * the CPU before the status block posted prior to the interrupt.
5122 * Reading the PCI State register will confirm whether the
5123 * interrupt is ours and will flush the status block.
5124 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005125 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Michael Chand18edcb2007-03-24 20:57:11 -07005126 if ((tp->tg3_flags & TG3_FLAG_CHIP_RESETTING) ||
5127 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5128 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005129 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130 }
Michael Chand18edcb2007-03-24 20:57:11 -07005131 }
5132
5133 /*
5134 * writing any value to intr-mbox-0 clears PCI INTA# and
5135 * chip-internal interrupt pending events.
5136 * writing non-zero to intr-mbox-0 additional tells the
5137 * NIC to stop sending us irqs, engaging "in-intr-handler"
5138 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005139 *
5140 * Flush the mailbox to de-assert the IRQ immediately to prevent
5141 * spurious interrupts. The flush impacts performance but
5142 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005143 */
Michael Chanc04cb342007-05-07 00:26:15 -07005144 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00005145
5146 /*
5147 * In a shared interrupt configuration, sometimes other devices'
5148 * interrupts will scream. We record the current status tag here
5149 * so that the above check can report that the screaming interrupts
5150 * are unhandled. Eventually they will be silenced.
5151 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005152 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00005153
Michael Chand18edcb2007-03-24 20:57:11 -07005154 if (tg3_irq_sync(tp))
5155 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00005156
Matt Carlson72334482009-08-28 14:03:01 +00005157 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00005158
Matt Carlson09943a12009-08-28 14:01:57 +00005159 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00005160
David S. Millerf47c11e2005-06-24 20:18:35 -07005161out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162 return IRQ_RETVAL(handled);
5163}
5164
Michael Chan79381092005-04-21 17:13:59 -07005165/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01005166static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07005167{
Matt Carlson09943a12009-08-28 14:01:57 +00005168 struct tg3_napi *tnapi = dev_id;
5169 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005170 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07005171
Michael Chanf9804dd2005-09-27 12:13:10 -07005172 if ((sblk->status & SD_STATUS_UPDATED) ||
5173 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07005174 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07005175 return IRQ_RETVAL(1);
5176 }
5177 return IRQ_RETVAL(0);
5178}
5179
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07005180static int tg3_init_hw(struct tg3 *, int);
Michael Chan944d9802005-05-29 14:57:48 -07005181static int tg3_halt(struct tg3 *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182
Michael Chanb9ec6c12006-07-25 16:37:27 -07005183/* Restart hardware after configuration changes, self-test, etc.
5184 * Invoked with tp->lock held.
5185 */
5186static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
Eric Dumazet78c61462008-04-24 23:33:06 -07005187 __releases(tp->lock)
5188 __acquires(tp->lock)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005189{
5190 int err;
5191
5192 err = tg3_init_hw(tp, reset_phy);
5193 if (err) {
5194 printk(KERN_ERR PFX "%s: Failed to re-initialize device, "
5195 "aborting.\n", tp->dev->name);
5196 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
5197 tg3_full_unlock(tp);
5198 del_timer_sync(&tp->timer);
5199 tp->irq_sync = 0;
Matt Carlsonfed97812009-09-01 13:10:19 +00005200 tg3_napi_enable(tp);
Michael Chanb9ec6c12006-07-25 16:37:27 -07005201 dev_close(tp->dev);
5202 tg3_full_lock(tp, 0);
5203 }
5204 return err;
5205}
5206
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207#ifdef CONFIG_NET_POLL_CONTROLLER
5208static void tg3_poll_controller(struct net_device *dev)
5209{
Matt Carlson4f125f42009-09-01 12:55:02 +00005210 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07005211 struct tg3 *tp = netdev_priv(dev);
5212
Matt Carlson4f125f42009-09-01 12:55:02 +00005213 for (i = 0; i < tp->irq_cnt; i++)
5214 tg3_interrupt(tp->napi[i].irq_vec, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215}
5216#endif
5217
David Howellsc4028952006-11-22 14:57:56 +00005218static void tg3_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219{
David Howellsc4028952006-11-22 14:57:56 +00005220 struct tg3 *tp = container_of(work, struct tg3, reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005221 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222 unsigned int restart_timer;
5223
Michael Chan7faa0062006-02-02 17:29:28 -08005224 tg3_full_lock(tp, 0);
Michael Chan7faa0062006-02-02 17:29:28 -08005225
5226 if (!netif_running(tp->dev)) {
Michael Chan7faa0062006-02-02 17:29:28 -08005227 tg3_full_unlock(tp);
5228 return;
5229 }
5230
5231 tg3_full_unlock(tp);
5232
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005233 tg3_phy_stop(tp);
5234
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235 tg3_netif_stop(tp);
5236
David S. Millerf47c11e2005-06-24 20:18:35 -07005237 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238
5239 restart_timer = tp->tg3_flags2 & TG3_FLG2_RESTART_TIMER;
5240 tp->tg3_flags2 &= ~TG3_FLG2_RESTART_TIMER;
5241
Michael Chandf3e6542006-05-26 17:48:07 -07005242 if (tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING) {
5243 tp->write32_tx_mbox = tg3_write32_tx_mbox;
5244 tp->write32_rx_mbox = tg3_write_flush_reg32;
5245 tp->tg3_flags |= TG3_FLAG_MBOX_WRITE_REORDER;
5246 tp->tg3_flags &= ~TG3_FLAG_TX_RECOVERY_PENDING;
5247 }
5248
Michael Chan944d9802005-05-29 14:57:48 -07005249 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005250 err = tg3_init_hw(tp, 1);
5251 if (err)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005252 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253
5254 tg3_netif_start(tp);
5255
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256 if (restart_timer)
5257 mod_timer(&tp->timer, jiffies + 1);
Michael Chan7faa0062006-02-02 17:29:28 -08005258
Michael Chanb9ec6c12006-07-25 16:37:27 -07005259out:
Michael Chan7faa0062006-02-02 17:29:28 -08005260 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005261
5262 if (!err)
5263 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005264}
5265
Michael Chanb0408752007-02-13 12:18:30 -08005266static void tg3_dump_short_state(struct tg3 *tp)
5267{
5268 printk(KERN_ERR PFX "DEBUG: MAC_TX_STATUS[%08x] MAC_RX_STATUS[%08x]\n",
5269 tr32(MAC_TX_STATUS), tr32(MAC_RX_STATUS));
5270 printk(KERN_ERR PFX "DEBUG: RDMAC_STATUS[%08x] WDMAC_STATUS[%08x]\n",
5271 tr32(RDMAC_STATUS), tr32(WDMAC_STATUS));
5272}
5273
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274static void tg3_tx_timeout(struct net_device *dev)
5275{
5276 struct tg3 *tp = netdev_priv(dev);
5277
Michael Chanb0408752007-02-13 12:18:30 -08005278 if (netif_msg_tx_err(tp)) {
Michael Chan9f88f292006-12-07 00:22:54 -08005279 printk(KERN_ERR PFX "%s: transmit timed out, resetting\n",
5280 dev->name);
Michael Chanb0408752007-02-13 12:18:30 -08005281 tg3_dump_short_state(tp);
5282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283
5284 schedule_work(&tp->reset_task);
5285}
5286
Michael Chanc58ec932005-09-17 00:46:27 -07005287/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
5288static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
5289{
5290 u32 base = (u32) mapping & 0xffffffff;
5291
5292 return ((base > 0xffffdcc0) &&
5293 (base + len + 8 < base));
5294}
5295
Michael Chan72f2afb2006-03-06 19:28:35 -08005296/* Test for DMA addresses > 40-bit */
5297static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
5298 int len)
5299{
5300#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Michael Chan6728a8e2006-03-27 23:16:49 -08005301 if (tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG)
Yang Hongyang50cf1562009-04-06 19:01:14 -07005302 return (((u64) mapping + len) > DMA_BIT_MASK(40));
Michael Chan72f2afb2006-03-06 19:28:35 -08005303 return 0;
5304#else
5305 return 0;
5306#endif
5307}
5308
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005309static void tg3_set_txd(struct tg3_napi *, int, dma_addr_t, int, u32, u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005310
Michael Chan72f2afb2006-03-06 19:28:35 -08005311/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00005312static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
5313 struct sk_buff *skb, u32 last_plus_one,
5314 u32 *start, u32 base_flags, u32 mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005315{
Matt Carlson24f4efd2009-11-13 13:03:35 +00005316 struct tg3 *tp = tnapi->tp;
Matt Carlson41588ba2008-04-19 18:12:33 -07005317 struct sk_buff *new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07005318 dma_addr_t new_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319 u32 entry = *start;
Michael Chanc58ec932005-09-17 00:46:27 -07005320 int i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321
Matt Carlson41588ba2008-04-19 18:12:33 -07005322 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
5323 new_skb = skb_copy(skb, GFP_ATOMIC);
5324 else {
5325 int more_headroom = 4 - ((unsigned long)skb->data & 3);
5326
5327 new_skb = skb_copy_expand(skb,
5328 skb_headroom(skb) + more_headroom,
5329 skb_tailroom(skb), GFP_ATOMIC);
5330 }
5331
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07005333 ret = -1;
5334 } else {
5335 /* New SKB is guaranteed to be linear. */
5336 entry = *start;
David S. Miller90079ce2008-09-11 04:52:51 -07005337 ret = skb_dma_map(&tp->pdev->dev, new_skb, DMA_TO_DEVICE);
Eric Dumazet042a53a2009-06-05 04:04:16 +00005338 new_addr = skb_shinfo(new_skb)->dma_head;
David S. Miller90079ce2008-09-11 04:52:51 -07005339
Michael Chanc58ec932005-09-17 00:46:27 -07005340 /* Make sure new skb does not cross any 4G boundaries.
5341 * Drop the packet if it does.
5342 */
Matt Carlson0e1406d2009-11-02 12:33:33 +00005343 if (ret || ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
5344 tg3_4g_overflow_test(new_addr, new_skb->len))) {
David S. Miller638266f2008-09-11 15:45:19 -07005345 if (!ret)
5346 skb_dma_unmap(&tp->pdev->dev, new_skb,
5347 DMA_TO_DEVICE);
Michael Chanc58ec932005-09-17 00:46:27 -07005348 ret = -1;
5349 dev_kfree_skb(new_skb);
5350 new_skb = NULL;
5351 } else {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005352 tg3_set_txd(tnapi, entry, new_addr, new_skb->len,
Michael Chanc58ec932005-09-17 00:46:27 -07005353 base_flags, 1 | (mss << 1));
5354 *start = NEXT_TX(entry);
5355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356 }
5357
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358 /* Now clean up the sw ring entries. */
5359 i = 0;
5360 while (entry != last_plus_one) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005361 if (i == 0)
5362 tnapi->tx_buffers[entry].skb = new_skb;
5363 else
5364 tnapi->tx_buffers[entry].skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365 entry = NEXT_TX(entry);
5366 i++;
5367 }
5368
David S. Miller90079ce2008-09-11 04:52:51 -07005369 skb_dma_unmap(&tp->pdev->dev, skb, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370 dev_kfree_skb(skb);
5371
Michael Chanc58ec932005-09-17 00:46:27 -07005372 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373}
5374
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005375static void tg3_set_txd(struct tg3_napi *tnapi, int entry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376 dma_addr_t mapping, int len, u32 flags,
5377 u32 mss_and_is_end)
5378{
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005379 struct tg3_tx_buffer_desc *txd = &tnapi->tx_ring[entry];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380 int is_end = (mss_and_is_end & 0x1);
5381 u32 mss = (mss_and_is_end >> 1);
5382 u32 vlan_tag = 0;
5383
5384 if (is_end)
5385 flags |= TXD_FLAG_END;
5386 if (flags & TXD_FLAG_VLAN) {
5387 vlan_tag = flags >> 16;
5388 flags &= 0xffff;
5389 }
5390 vlan_tag |= (mss << TXD_MSS_SHIFT);
5391
5392 txd->addr_hi = ((u64) mapping >> 32);
5393 txd->addr_lo = ((u64) mapping & 0xffffffff);
5394 txd->len_flags = (len << TXD_LEN_SHIFT) | flags;
5395 txd->vlan_tag = vlan_tag << TXD_VLAN_TAG_SHIFT;
5396}
5397
Michael Chan5a6f3072006-03-20 22:28:05 -08005398/* hard_start_xmit for devices that don't have any bugs and
Matt Carlsone849cdc2009-11-13 13:03:38 +00005399 * support TG3_FLG2_HW_TSO_2 and TG3_FLG2_HW_TSO_3 only.
Michael Chan5a6f3072006-03-20 22:28:05 -08005400 */
Stephen Hemminger613573252009-08-31 19:50:58 +00005401static netdev_tx_t tg3_start_xmit(struct sk_buff *skb,
5402 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403{
5404 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405 u32 len, entry, base_flags, mss;
David S. Miller90079ce2008-09-11 04:52:51 -07005406 struct skb_shared_info *sp;
5407 dma_addr_t mapping;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005408 struct tg3_napi *tnapi;
5409 struct netdev_queue *txq;
Michael Chan5a6f3072006-03-20 22:28:05 -08005410
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005411 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
5412 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
5413 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
5414 tnapi++;
Michael Chan5a6f3072006-03-20 22:28:05 -08005415
Michael Chan00b70502006-06-17 21:58:45 -07005416 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005417 * and TX reclaim runs via tp->napi.poll inside of a software
Michael Chan5a6f3072006-03-20 22:28:05 -08005418 * interrupt. Furthermore, IRQ processing runs lockless so we have
5419 * no IRQ context deadlocks to worry about either. Rejoice!
5420 */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005421 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005422 if (!netif_tx_queue_stopped(txq)) {
5423 netif_tx_stop_queue(txq);
Michael Chan5a6f3072006-03-20 22:28:05 -08005424
5425 /* This is a hard error, log it. */
5426 printk(KERN_ERR PFX "%s: BUG! Tx Ring full when "
5427 "queue awake!\n", dev->name);
5428 }
Michael Chan5a6f3072006-03-20 22:28:05 -08005429 return NETDEV_TX_BUSY;
5430 }
5431
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005432 entry = tnapi->tx_prod;
Michael Chan5a6f3072006-03-20 22:28:05 -08005433 base_flags = 0;
Michael Chan5a6f3072006-03-20 22:28:05 -08005434 mss = 0;
Matt Carlsonc13e3712007-05-05 11:50:04 -07005435 if ((mss = skb_shinfo(skb)->gso_size) != 0) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005436 int tcp_opt_len, ip_tcp_len;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005437 u32 hdrlen;
Michael Chan5a6f3072006-03-20 22:28:05 -08005438
5439 if (skb_header_cloned(skb) &&
5440 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
5441 dev_kfree_skb(skb);
5442 goto out_unlock;
5443 }
5444
Michael Chanb0026622006-07-03 19:42:14 -07005445 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005446 hdrlen = skb_headlen(skb) - ETH_HLEN;
Michael Chanb0026622006-07-03 19:42:14 -07005447 else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005448 struct iphdr *iph = ip_hdr(skb);
5449
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07005450 tcp_opt_len = tcp_optlen(skb);
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -03005451 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
Michael Chanb0026622006-07-03 19:42:14 -07005452
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005453 iph->check = 0;
5454 iph->tot_len = htons(mss + ip_tcp_len + tcp_opt_len);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005455 hdrlen = ip_tcp_len + tcp_opt_len;
Michael Chanb0026622006-07-03 19:42:14 -07005456 }
Michael Chan5a6f3072006-03-20 22:28:05 -08005457
Matt Carlsone849cdc2009-11-13 13:03:38 +00005458 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005459 mss |= (hdrlen & 0xc) << 12;
5460 if (hdrlen & 0x10)
5461 base_flags |= 0x00000010;
5462 base_flags |= (hdrlen & 0x3e0) << 5;
5463 } else
5464 mss |= hdrlen << 9;
5465
Michael Chan5a6f3072006-03-20 22:28:05 -08005466 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
5467 TXD_FLAG_CPU_POST_DMA);
5468
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005469 tcp_hdr(skb)->check = 0;
Michael Chan5a6f3072006-03-20 22:28:05 -08005470
Michael Chan5a6f3072006-03-20 22:28:05 -08005471 }
Patrick McHardy84fa7932006-08-29 16:44:56 -07005472 else if (skb->ip_summed == CHECKSUM_PARTIAL)
Michael Chan5a6f3072006-03-20 22:28:05 -08005473 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Michael Chan5a6f3072006-03-20 22:28:05 -08005474#if TG3_VLAN_TAG_USED
5475 if (tp->vlgrp != NULL && vlan_tx_tag_present(skb))
5476 base_flags |= (TXD_FLAG_VLAN |
5477 (vlan_tx_tag_get(skb) << 16));
5478#endif
5479
David S. Miller90079ce2008-09-11 04:52:51 -07005480 if (skb_dma_map(&tp->pdev->dev, skb, DMA_TO_DEVICE)) {
5481 dev_kfree_skb(skb);
5482 goto out_unlock;
5483 }
5484
5485 sp = skb_shinfo(skb);
5486
Eric Dumazet042a53a2009-06-05 04:04:16 +00005487 mapping = sp->dma_head;
Michael Chan5a6f3072006-03-20 22:28:05 -08005488
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005489 tnapi->tx_buffers[entry].skb = skb;
Michael Chan5a6f3072006-03-20 22:28:05 -08005490
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005491 len = skb_headlen(skb);
5492
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005493 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 &&
5494 !mss && skb->len > ETH_DATA_LEN)
5495 base_flags |= TXD_FLAG_JMB_PKT;
5496
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005497 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
Michael Chan5a6f3072006-03-20 22:28:05 -08005498 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
5499
5500 entry = NEXT_TX(entry);
5501
5502 /* Now loop through additional data fragments, and queue them. */
5503 if (skb_shinfo(skb)->nr_frags > 0) {
5504 unsigned int i, last;
5505
5506 last = skb_shinfo(skb)->nr_frags - 1;
5507 for (i = 0; i <= last; i++) {
5508 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5509
5510 len = frag->size;
Eric Dumazet042a53a2009-06-05 04:04:16 +00005511 mapping = sp->dma_maps[i];
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005512 tnapi->tx_buffers[entry].skb = NULL;
Michael Chan5a6f3072006-03-20 22:28:05 -08005513
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005514 tg3_set_txd(tnapi, entry, mapping, len,
Michael Chan5a6f3072006-03-20 22:28:05 -08005515 base_flags, (i == last) | (mss << 1));
5516
5517 entry = NEXT_TX(entry);
5518 }
5519 }
5520
5521 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005522 tw32_tx_mbox(tnapi->prodmbox, entry);
Michael Chan5a6f3072006-03-20 22:28:05 -08005523
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005524 tnapi->tx_prod = entry;
5525 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005526 netif_tx_stop_queue(txq);
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005527 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005528 netif_tx_wake_queue(txq);
Michael Chan5a6f3072006-03-20 22:28:05 -08005529 }
5530
5531out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00005532 mmiowb();
Michael Chan5a6f3072006-03-20 22:28:05 -08005533
5534 return NETDEV_TX_OK;
5535}
5536
Stephen Hemminger613573252009-08-31 19:50:58 +00005537static netdev_tx_t tg3_start_xmit_dma_bug(struct sk_buff *,
5538 struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07005539
5540/* Use GSO to workaround a rare TSO bug that may be triggered when the
5541 * TSO header is greater than 80 bytes.
5542 */
5543static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
5544{
5545 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005546 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07005547
5548 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005549 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07005550 netif_stop_queue(tp->dev);
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005551 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08005552 return NETDEV_TX_BUSY;
5553
5554 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07005555 }
5556
5557 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07005558 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07005559 goto tg3_tso_bug_end;
5560
5561 do {
5562 nskb = segs;
5563 segs = segs->next;
5564 nskb->next = NULL;
5565 tg3_start_xmit_dma_bug(nskb, tp->dev);
5566 } while (segs);
5567
5568tg3_tso_bug_end:
5569 dev_kfree_skb(skb);
5570
5571 return NETDEV_TX_OK;
5572}
Michael Chan52c0fd82006-06-29 20:15:54 -07005573
Michael Chan5a6f3072006-03-20 22:28:05 -08005574/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
5575 * support TG3_FLG2_HW_TSO_1 or firmware TSO only.
5576 */
Stephen Hemminger613573252009-08-31 19:50:58 +00005577static netdev_tx_t tg3_start_xmit_dma_bug(struct sk_buff *skb,
5578 struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08005579{
5580 struct tg3 *tp = netdev_priv(dev);
Michael Chan5a6f3072006-03-20 22:28:05 -08005581 u32 len, entry, base_flags, mss;
David S. Miller90079ce2008-09-11 04:52:51 -07005582 struct skb_shared_info *sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583 int would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07005584 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00005585 struct tg3_napi *tnapi;
5586 struct netdev_queue *txq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587
Matt Carlson24f4efd2009-11-13 13:03:35 +00005588 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
5589 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
5590 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
5591 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592
Michael Chan00b70502006-06-17 21:58:45 -07005593 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005594 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07005595 * interrupt. Furthermore, IRQ processing runs lockless so we have
5596 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597 */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005598 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00005599 if (!netif_tx_queue_stopped(txq)) {
5600 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08005601
5602 /* This is a hard error, log it. */
5603 printk(KERN_ERR PFX "%s: BUG! Tx Ring full when "
5604 "queue awake!\n", dev->name);
5605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606 return NETDEV_TX_BUSY;
5607 }
5608
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005609 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07005611 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00005613
Matt Carlsonc13e3712007-05-05 11:50:04 -07005614 if ((mss = skb_shinfo(skb)->gso_size) != 0) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005615 struct iphdr *iph;
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005616 u32 tcp_opt_len, ip_tcp_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617
5618 if (skb_header_cloned(skb) &&
5619 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
5620 dev_kfree_skb(skb);
5621 goto out_unlock;
5622 }
5623
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07005624 tcp_opt_len = tcp_optlen(skb);
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -03005625 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626
Michael Chan52c0fd82006-06-29 20:15:54 -07005627 hdr_len = ip_tcp_len + tcp_opt_len;
5628 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Michael Chan7f62ad52007-02-20 23:25:40 -08005629 (tp->tg3_flags2 & TG3_FLG2_TSO_BUG))
Michael Chan52c0fd82006-06-29 20:15:54 -07005630 return (tg3_tso_bug(tp, skb));
5631
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
5633 TXD_FLAG_CPU_POST_DMA);
5634
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005635 iph = ip_hdr(skb);
5636 iph->check = 0;
5637 iph->tot_len = htons(mss + hdr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005639 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005641 } else
5642 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
5643 iph->daddr, 0,
5644 IPPROTO_TCP,
5645 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646
Matt Carlson615774f2009-11-13 13:03:39 +00005647 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) {
5648 mss |= (hdr_len & 0xc) << 12;
5649 if (hdr_len & 0x10)
5650 base_flags |= 0x00000010;
5651 base_flags |= (hdr_len & 0x3e0) << 5;
5652 } else if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_2)
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005653 mss |= hdr_len << 9;
5654 else if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_1) ||
5655 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005656 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657 int tsflags;
5658
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005659 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660 mss |= (tsflags << 11);
5661 }
5662 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005663 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664 int tsflags;
5665
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005666 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667 base_flags |= tsflags << 12;
5668 }
5669 }
5670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671#if TG3_VLAN_TAG_USED
5672 if (tp->vlgrp != NULL && vlan_tx_tag_present(skb))
5673 base_flags |= (TXD_FLAG_VLAN |
5674 (vlan_tx_tag_get(skb) << 16));
5675#endif
5676
Matt Carlson615774f2009-11-13 13:03:39 +00005677 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 &&
5678 !mss && skb->len > ETH_DATA_LEN)
5679 base_flags |= TXD_FLAG_JMB_PKT;
5680
David S. Miller90079ce2008-09-11 04:52:51 -07005681 if (skb_dma_map(&tp->pdev->dev, skb, DMA_TO_DEVICE)) {
5682 dev_kfree_skb(skb);
5683 goto out_unlock;
5684 }
5685
5686 sp = skb_shinfo(skb);
5687
Eric Dumazet042a53a2009-06-05 04:04:16 +00005688 mapping = sp->dma_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005690 tnapi->tx_buffers[entry].skb = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691
5692 would_hit_hwbug = 0;
5693
Matt Carlson24f4efd2009-11-13 13:03:35 +00005694 len = skb_headlen(skb);
5695
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005696 if ((tp->tg3_flags3 & TG3_FLG3_SHORT_DMA_BUG) && len <= 8)
5697 would_hit_hwbug = 1;
5698
Matt Carlson0e1406d2009-11-02 12:33:33 +00005699 if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
5700 tg3_4g_overflow_test(mapping, len))
Matt Carlson41588ba2008-04-19 18:12:33 -07005701 would_hit_hwbug = 1;
Matt Carlson0e1406d2009-11-02 12:33:33 +00005702
5703 if ((tp->tg3_flags3 & TG3_FLG3_40BIT_DMA_LIMIT_BUG) &&
5704 tg3_40bit_overflow_test(tp, mapping, len))
5705 would_hit_hwbug = 1;
5706
5707 if (tp->tg3_flags3 & TG3_FLG3_5701_DMA_BUG)
Michael Chanc58ec932005-09-17 00:46:27 -07005708 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005710 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
5712
5713 entry = NEXT_TX(entry);
5714
5715 /* Now loop through additional data fragments, and queue them. */
5716 if (skb_shinfo(skb)->nr_frags > 0) {
5717 unsigned int i, last;
5718
5719 last = skb_shinfo(skb)->nr_frags - 1;
5720 for (i = 0; i <= last; i++) {
5721 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5722
5723 len = frag->size;
Eric Dumazet042a53a2009-06-05 04:04:16 +00005724 mapping = sp->dma_maps[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005726 tnapi->tx_buffers[entry].skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005728 if ((tp->tg3_flags3 & TG3_FLG3_SHORT_DMA_BUG) &&
5729 len <= 8)
5730 would_hit_hwbug = 1;
5731
Matt Carlson0e1406d2009-11-02 12:33:33 +00005732 if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
5733 tg3_4g_overflow_test(mapping, len))
Michael Chanc58ec932005-09-17 00:46:27 -07005734 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735
Matt Carlson0e1406d2009-11-02 12:33:33 +00005736 if ((tp->tg3_flags3 & TG3_FLG3_40BIT_DMA_LIMIT_BUG) &&
5737 tg3_40bit_overflow_test(tp, mapping, len))
Michael Chan72f2afb2006-03-06 19:28:35 -08005738 would_hit_hwbug = 1;
5739
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005741 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742 base_flags, (i == last)|(mss << 1));
5743 else
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005744 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745 base_flags, (i == last));
5746
5747 entry = NEXT_TX(entry);
5748 }
5749 }
5750
5751 if (would_hit_hwbug) {
5752 u32 last_plus_one = entry;
5753 u32 start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005754
Michael Chanc58ec932005-09-17 00:46:27 -07005755 start = entry - 1 - skb_shinfo(skb)->nr_frags;
5756 start &= (TG3_TX_RING_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757
5758 /* If the workaround fails due to memory/mapping
5759 * failure, silently drop this packet.
5760 */
Matt Carlson24f4efd2009-11-13 13:03:35 +00005761 if (tigon3_dma_hwbug_workaround(tnapi, skb, last_plus_one,
Michael Chanc58ec932005-09-17 00:46:27 -07005762 &start, base_flags, mss))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763 goto out_unlock;
5764
5765 entry = start;
5766 }
5767
5768 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00005769 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005770
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005771 tnapi->tx_prod = entry;
5772 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00005773 netif_tx_stop_queue(txq);
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005774 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00005775 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07005776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777
5778out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00005779 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780
5781 return NETDEV_TX_OK;
5782}
5783
5784static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
5785 int new_mtu)
5786{
5787 dev->mtu = new_mtu;
5788
Michael Chanef7f5ec2005-07-25 12:32:25 -07005789 if (new_mtu > ETH_DATA_LEN) {
Michael Chana4e2b342005-10-26 15:46:52 -07005790 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
Michael Chanef7f5ec2005-07-25 12:32:25 -07005791 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
5792 ethtool_op_set_tso(dev, 0);
5793 }
5794 else
5795 tp->tg3_flags |= TG3_FLAG_JUMBO_RING_ENABLE;
5796 } else {
Michael Chana4e2b342005-10-26 15:46:52 -07005797 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
Michael Chanef7f5ec2005-07-25 12:32:25 -07005798 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
Michael Chan0f893dc2005-07-25 12:30:38 -07005799 tp->tg3_flags &= ~TG3_FLAG_JUMBO_RING_ENABLE;
Michael Chanef7f5ec2005-07-25 12:32:25 -07005800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801}
5802
5803static int tg3_change_mtu(struct net_device *dev, int new_mtu)
5804{
5805 struct tg3 *tp = netdev_priv(dev);
Michael Chanb9ec6c12006-07-25 16:37:27 -07005806 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005807
5808 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
5809 return -EINVAL;
5810
5811 if (!netif_running(dev)) {
5812 /* We'll just catch it later when the
5813 * device is up'd.
5814 */
5815 tg3_set_mtu(dev, tp, new_mtu);
5816 return 0;
5817 }
5818
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005819 tg3_phy_stop(tp);
5820
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821 tg3_netif_stop(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07005822
5823 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005824
Michael Chan944d9802005-05-29 14:57:48 -07005825 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826
5827 tg3_set_mtu(dev, tp, new_mtu);
5828
Michael Chanb9ec6c12006-07-25 16:37:27 -07005829 err = tg3_restart_hw(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005830
Michael Chanb9ec6c12006-07-25 16:37:27 -07005831 if (!err)
5832 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005833
David S. Millerf47c11e2005-06-24 20:18:35 -07005834 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005836 if (!err)
5837 tg3_phy_start(tp);
5838
Michael Chanb9ec6c12006-07-25 16:37:27 -07005839 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840}
5841
Matt Carlson21f581a2009-08-28 14:00:25 +00005842static void tg3_rx_prodring_free(struct tg3 *tp,
5843 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005844{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005845 int i;
5846
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005847 if (tpr != &tp->prodring[0]) {
5848 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
5849 i = (i + 1) % TG3_RX_RING_SIZE)
5850 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
5851 tp->rx_pkt_map_sz);
5852
5853 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
5854 for (i = tpr->rx_jmb_cons_idx;
5855 i != tpr->rx_jmb_prod_idx;
5856 i = (i + 1) % TG3_RX_JUMBO_RING_SIZE) {
5857 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
5858 TG3_RX_JMB_MAP_SZ);
5859 }
5860 }
5861
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005862 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005865 for (i = 0; i < TG3_RX_RING_SIZE; i++)
5866 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
5867 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005868
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005869 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005870 for (i = 0; i < TG3_RX_JUMBO_RING_SIZE; i++)
5871 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
5872 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873 }
5874}
5875
5876/* Initialize tx/rx rings for packet processing.
5877 *
5878 * The chip has been shut down and the driver detached from
5879 * the networking, so no interrupts or new tx packets will
5880 * end up in the driver. tp->{tx,}lock are held and thus
5881 * we may not sleep.
5882 */
Matt Carlson21f581a2009-08-28 14:00:25 +00005883static int tg3_rx_prodring_alloc(struct tg3 *tp,
5884 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005885{
Matt Carlson287be122009-08-28 13:58:46 +00005886 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005887
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005888 tpr->rx_std_cons_idx = 0;
5889 tpr->rx_std_prod_idx = 0;
5890 tpr->rx_jmb_cons_idx = 0;
5891 tpr->rx_jmb_prod_idx = 0;
5892
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005893 if (tpr != &tp->prodring[0]) {
5894 memset(&tpr->rx_std_buffers[0], 0, TG3_RX_STD_BUFF_RING_SIZE);
5895 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE)
5896 memset(&tpr->rx_jmb_buffers[0], 0,
5897 TG3_RX_JMB_BUFF_RING_SIZE);
5898 goto done;
5899 }
5900
Linus Torvalds1da177e2005-04-16 15:20:36 -07005901 /* Zero out all descriptors. */
Matt Carlson21f581a2009-08-28 14:00:25 +00005902 memset(tpr->rx_std, 0, TG3_RX_RING_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903
Matt Carlson287be122009-08-28 13:58:46 +00005904 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Michael Chana4e2b342005-10-26 15:46:52 -07005905 if ((tp->tg3_flags2 & TG3_FLG2_5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00005906 tp->dev->mtu > ETH_DATA_LEN)
5907 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
5908 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07005909
Linus Torvalds1da177e2005-04-16 15:20:36 -07005910 /* Initialize invariants of the rings, we only set this
5911 * stuff once. This works because the card does not
5912 * write into the rx buffer posting rings.
5913 */
5914 for (i = 0; i < TG3_RX_RING_SIZE; i++) {
5915 struct tg3_rx_buffer_desc *rxd;
5916
Matt Carlson21f581a2009-08-28 14:00:25 +00005917 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00005918 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005919 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
5920 rxd->opaque = (RXD_OPAQUE_RING_STD |
5921 (i << RXD_OPAQUE_INDEX_SHIFT));
5922 }
5923
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005924 /* Now allocate fresh SKBs for each rx ring. */
5925 for (i = 0; i < tp->rx_pending; i++) {
Matt Carlson86b21e52009-11-13 13:03:45 +00005926 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_STD, i) < 0) {
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005927 printk(KERN_WARNING PFX
5928 "%s: Using a smaller RX standard ring, "
5929 "only %d out of %d buffers were allocated "
5930 "successfully.\n",
5931 tp->dev->name, i, tp->rx_pending);
5932 if (i == 0)
5933 goto initfail;
5934 tp->rx_pending = i;
5935 break;
5936 }
5937 }
5938
5939 if (!(tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE))
5940 goto done;
5941
Matt Carlson21f581a2009-08-28 14:00:25 +00005942 memset(tpr->rx_jmb, 0, TG3_RX_JUMBO_RING_BYTES);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005943
Michael Chan0f893dc2005-07-25 12:30:38 -07005944 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005945 for (i = 0; i < TG3_RX_JUMBO_RING_SIZE; i++) {
5946 struct tg3_rx_buffer_desc *rxd;
5947
Matt Carlson79ed5ac2009-08-28 14:00:55 +00005948 rxd = &tpr->rx_jmb[i].std;
Matt Carlson287be122009-08-28 13:58:46 +00005949 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005950 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
5951 RXD_FLAG_JUMBO;
5952 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
5953 (i << RXD_OPAQUE_INDEX_SHIFT));
5954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955
Linus Torvalds1da177e2005-04-16 15:20:36 -07005956 for (i = 0; i < tp->rx_jumbo_pending; i++) {
Matt Carlson86b21e52009-11-13 13:03:45 +00005957 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_JUMBO,
Matt Carlsonafc081f2009-11-13 13:03:43 +00005958 i) < 0) {
Michael Chan32d8c572006-07-25 16:38:29 -07005959 printk(KERN_WARNING PFX
5960 "%s: Using a smaller RX jumbo ring, "
5961 "only %d out of %d buffers were "
5962 "allocated successfully.\n",
5963 tp->dev->name, i, tp->rx_jumbo_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005964 if (i == 0)
5965 goto initfail;
Michael Chan32d8c572006-07-25 16:38:29 -07005966 tp->rx_jumbo_pending = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005967 break;
Michael Chan32d8c572006-07-25 16:38:29 -07005968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005969 }
5970 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005971
5972done:
Michael Chan32d8c572006-07-25 16:38:29 -07005973 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005974
5975initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00005976 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005977 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005978}
5979
Matt Carlson21f581a2009-08-28 14:00:25 +00005980static void tg3_rx_prodring_fini(struct tg3 *tp,
5981 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982{
Matt Carlson21f581a2009-08-28 14:00:25 +00005983 kfree(tpr->rx_std_buffers);
5984 tpr->rx_std_buffers = NULL;
5985 kfree(tpr->rx_jmb_buffers);
5986 tpr->rx_jmb_buffers = NULL;
5987 if (tpr->rx_std) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005988 pci_free_consistent(tp->pdev, TG3_RX_RING_BYTES,
Matt Carlson21f581a2009-08-28 14:00:25 +00005989 tpr->rx_std, tpr->rx_std_mapping);
5990 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991 }
Matt Carlson21f581a2009-08-28 14:00:25 +00005992 if (tpr->rx_jmb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993 pci_free_consistent(tp->pdev, TG3_RX_JUMBO_RING_BYTES,
Matt Carlson21f581a2009-08-28 14:00:25 +00005994 tpr->rx_jmb, tpr->rx_jmb_mapping);
5995 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005996 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005997}
5998
Matt Carlson21f581a2009-08-28 14:00:25 +00005999static int tg3_rx_prodring_init(struct tg3 *tp,
6000 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006001{
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006002 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE, GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006003 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006004 return -ENOMEM;
6005
Matt Carlson21f581a2009-08-28 14:00:25 +00006006 tpr->rx_std = pci_alloc_consistent(tp->pdev, TG3_RX_RING_BYTES,
6007 &tpr->rx_std_mapping);
6008 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006009 goto err_out;
6010
6011 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006012 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE,
Matt Carlson21f581a2009-08-28 14:00:25 +00006013 GFP_KERNEL);
6014 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006015 goto err_out;
6016
Matt Carlson21f581a2009-08-28 14:00:25 +00006017 tpr->rx_jmb = pci_alloc_consistent(tp->pdev,
6018 TG3_RX_JUMBO_RING_BYTES,
6019 &tpr->rx_jmb_mapping);
6020 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006021 goto err_out;
6022 }
6023
6024 return 0;
6025
6026err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00006027 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006028 return -ENOMEM;
6029}
6030
6031/* Free up pending packets in all rx/tx rings.
6032 *
6033 * The chip has been shut down and the driver detached from
6034 * the networking, so no interrupts or new tx packets will
6035 * end up in the driver. tp->{tx,}lock is not held and we are not
6036 * in an interrupt context and thus may sleep.
6037 */
6038static void tg3_free_rings(struct tg3 *tp)
6039{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006040 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006041
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006042 for (j = 0; j < tp->irq_cnt; j++) {
6043 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006044
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006045 if (!tnapi->tx_buffers)
6046 continue;
6047
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006048 for (i = 0; i < TG3_TX_RING_SIZE; ) {
6049 struct tx_ring_info *txp;
6050 struct sk_buff *skb;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006051
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006052 txp = &tnapi->tx_buffers[i];
6053 skb = txp->skb;
6054
6055 if (skb == NULL) {
6056 i++;
6057 continue;
6058 }
6059
6060 skb_dma_unmap(&tp->pdev->dev, skb, DMA_TO_DEVICE);
6061
6062 txp->skb = NULL;
6063
6064 i += skb_shinfo(skb)->nr_frags + 1;
6065
6066 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006067 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006068
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006069 if (tp->irq_cnt == 1 || j != tp->irq_cnt - 1)
6070 tg3_rx_prodring_free(tp, &tp->prodring[j]);
6071 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006072}
6073
6074/* Initialize tx/rx rings for packet processing.
6075 *
6076 * The chip has been shut down and the driver detached from
6077 * the networking, so no interrupts or new tx packets will
6078 * end up in the driver. tp->{tx,}lock are held and thus
6079 * we may not sleep.
6080 */
6081static int tg3_init_rings(struct tg3 *tp)
6082{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006083 int i;
Matt Carlson72334482009-08-28 14:03:01 +00006084
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006085 /* Free up all the SKBs. */
6086 tg3_free_rings(tp);
6087
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006088 for (i = 0; i < tp->irq_cnt; i++) {
6089 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006090
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006091 tnapi->last_tag = 0;
6092 tnapi->last_irq_tag = 0;
6093 tnapi->hw_status->status = 0;
6094 tnapi->hw_status->status_tag = 0;
6095 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6096
6097 tnapi->tx_prod = 0;
6098 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006099 if (tnapi->tx_ring)
6100 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006101
6102 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006103 if (tnapi->rx_rcb)
6104 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006105
6106 if ((tp->irq_cnt == 1 || i != tp->irq_cnt - 1) &&
6107 tg3_rx_prodring_alloc(tp, &tp->prodring[i]))
6108 return -ENOMEM;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006109 }
Matt Carlson72334482009-08-28 14:03:01 +00006110
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006111 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006112}
6113
6114/*
6115 * Must not be invoked with interrupt sources disabled and
6116 * the hardware shutdown down.
6117 */
6118static void tg3_free_consistent(struct tg3 *tp)
6119{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006120 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006121
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006122 for (i = 0; i < tp->irq_cnt; i++) {
6123 struct tg3_napi *tnapi = &tp->napi[i];
6124
6125 if (tnapi->tx_ring) {
6126 pci_free_consistent(tp->pdev, TG3_TX_RING_BYTES,
6127 tnapi->tx_ring, tnapi->tx_desc_mapping);
6128 tnapi->tx_ring = NULL;
6129 }
6130
6131 kfree(tnapi->tx_buffers);
6132 tnapi->tx_buffers = NULL;
6133
6134 if (tnapi->rx_rcb) {
6135 pci_free_consistent(tp->pdev, TG3_RX_RCB_RING_BYTES(tp),
6136 tnapi->rx_rcb,
6137 tnapi->rx_rcb_mapping);
6138 tnapi->rx_rcb = NULL;
6139 }
6140
6141 if (tnapi->hw_status) {
6142 pci_free_consistent(tp->pdev, TG3_HW_STATUS_SIZE,
6143 tnapi->hw_status,
6144 tnapi->status_mapping);
6145 tnapi->hw_status = NULL;
6146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006147 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006148
Linus Torvalds1da177e2005-04-16 15:20:36 -07006149 if (tp->hw_stats) {
6150 pci_free_consistent(tp->pdev, sizeof(struct tg3_hw_stats),
6151 tp->hw_stats, tp->stats_mapping);
6152 tp->hw_stats = NULL;
6153 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006154
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006155 for (i = 0; i < (tp->irq_cnt == 1 ? 1 : tp->irq_cnt - 1); i++)
6156 tg3_rx_prodring_fini(tp, &tp->prodring[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006157}
6158
6159/*
6160 * Must not be invoked with interrupt sources disabled and
6161 * the hardware shutdown down. Can sleep.
6162 */
6163static int tg3_alloc_consistent(struct tg3 *tp)
6164{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006165 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006166
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006167 for (i = 0; i < (tp->irq_cnt == 1 ? 1 : tp->irq_cnt - 1); i++) {
6168 if (tg3_rx_prodring_init(tp, &tp->prodring[i]))
6169 goto err_out;
6170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006171
Linus Torvalds1da177e2005-04-16 15:20:36 -07006172 tp->hw_stats = pci_alloc_consistent(tp->pdev,
6173 sizeof(struct tg3_hw_stats),
6174 &tp->stats_mapping);
6175 if (!tp->hw_stats)
6176 goto err_out;
6177
Linus Torvalds1da177e2005-04-16 15:20:36 -07006178 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6179
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006180 for (i = 0; i < tp->irq_cnt; i++) {
6181 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006182 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006183
6184 tnapi->hw_status = pci_alloc_consistent(tp->pdev,
6185 TG3_HW_STATUS_SIZE,
6186 &tnapi->status_mapping);
6187 if (!tnapi->hw_status)
6188 goto err_out;
6189
6190 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006191 sblk = tnapi->hw_status;
6192
6193 /*
6194 * When RSS is enabled, the status block format changes
6195 * slightly. The "rx_jumbo_consumer", "reserved",
6196 * and "rx_mini_consumer" members get mapped to the
6197 * other three rx return ring producer indexes.
6198 */
6199 switch (i) {
6200 default:
6201 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
6202 break;
6203 case 2:
6204 tnapi->rx_rcb_prod_idx = &sblk->rx_jumbo_consumer;
6205 break;
6206 case 3:
6207 tnapi->rx_rcb_prod_idx = &sblk->reserved;
6208 break;
6209 case 4:
6210 tnapi->rx_rcb_prod_idx = &sblk->rx_mini_consumer;
6211 break;
6212 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006213
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006214 if (tp->irq_cnt == 1)
6215 tnapi->prodring = &tp->prodring[0];
6216 else if (i)
6217 tnapi->prodring = &tp->prodring[i - 1];
6218
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006219 /*
6220 * If multivector RSS is enabled, vector 0 does not handle
6221 * rx or tx interrupts. Don't allocate any resources for it.
6222 */
6223 if (!i && (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS))
6224 continue;
6225
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006226 tnapi->rx_rcb = pci_alloc_consistent(tp->pdev,
6227 TG3_RX_RCB_RING_BYTES(tp),
6228 &tnapi->rx_rcb_mapping);
6229 if (!tnapi->rx_rcb)
6230 goto err_out;
6231
6232 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
6233
6234 tnapi->tx_buffers = kzalloc(sizeof(struct tx_ring_info) *
6235 TG3_TX_RING_SIZE, GFP_KERNEL);
6236 if (!tnapi->tx_buffers)
6237 goto err_out;
6238
6239 tnapi->tx_ring = pci_alloc_consistent(tp->pdev,
6240 TG3_TX_RING_BYTES,
6241 &tnapi->tx_desc_mapping);
6242 if (!tnapi->tx_ring)
6243 goto err_out;
6244 }
6245
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246 return 0;
6247
6248err_out:
6249 tg3_free_consistent(tp);
6250 return -ENOMEM;
6251}
6252
6253#define MAX_WAIT_CNT 1000
6254
6255/* To stop a block, clear the enable bit and poll till it
6256 * clears. tp->lock is held.
6257 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006258static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006259{
6260 unsigned int i;
6261 u32 val;
6262
6263 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
6264 switch (ofs) {
6265 case RCVLSC_MODE:
6266 case DMAC_MODE:
6267 case MBFREE_MODE:
6268 case BUFMGR_MODE:
6269 case MEMARB_MODE:
6270 /* We can't enable/disable these bits of the
6271 * 5705/5750, just say success.
6272 */
6273 return 0;
6274
6275 default:
6276 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006278 }
6279
6280 val = tr32(ofs);
6281 val &= ~enable_bit;
6282 tw32_f(ofs, val);
6283
6284 for (i = 0; i < MAX_WAIT_CNT; i++) {
6285 udelay(100);
6286 val = tr32(ofs);
6287 if ((val & enable_bit) == 0)
6288 break;
6289 }
6290
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006291 if (i == MAX_WAIT_CNT && !silent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006292 printk(KERN_ERR PFX "tg3_stop_block timed out, "
6293 "ofs=%lx enable_bit=%x\n",
6294 ofs, enable_bit);
6295 return -ENODEV;
6296 }
6297
6298 return 0;
6299}
6300
6301/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006302static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006303{
6304 int i, err;
6305
6306 tg3_disable_ints(tp);
6307
6308 tp->rx_mode &= ~RX_MODE_ENABLE;
6309 tw32_f(MAC_RX_MODE, tp->rx_mode);
6310 udelay(10);
6311
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006312 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
6313 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
6314 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
6315 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
6316 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
6317 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006318
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006319 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
6320 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
6321 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
6322 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
6323 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
6324 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
6325 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006326
6327 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
6328 tw32_f(MAC_MODE, tp->mac_mode);
6329 udelay(40);
6330
6331 tp->tx_mode &= ~TX_MODE_ENABLE;
6332 tw32_f(MAC_TX_MODE, tp->tx_mode);
6333
6334 for (i = 0; i < MAX_WAIT_CNT; i++) {
6335 udelay(100);
6336 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
6337 break;
6338 }
6339 if (i >= MAX_WAIT_CNT) {
6340 printk(KERN_ERR PFX "tg3_abort_hw timed out for %s, "
6341 "TX_MODE_ENABLE will not clear MAC_TX_MODE=%08x\n",
6342 tp->dev->name, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07006343 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006344 }
6345
Michael Chane6de8ad2005-05-05 14:42:41 -07006346 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006347 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
6348 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006349
6350 tw32(FTQ_RESET, 0xffffffff);
6351 tw32(FTQ_RESET, 0x00000000);
6352
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006353 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
6354 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006355
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006356 for (i = 0; i < tp->irq_cnt; i++) {
6357 struct tg3_napi *tnapi = &tp->napi[i];
6358 if (tnapi->hw_status)
6359 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006361 if (tp->hw_stats)
6362 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6363
Linus Torvalds1da177e2005-04-16 15:20:36 -07006364 return err;
6365}
6366
Matt Carlson0d3031d2007-10-10 18:02:43 -07006367static void tg3_ape_send_event(struct tg3 *tp, u32 event)
6368{
6369 int i;
6370 u32 apedata;
6371
6372 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
6373 if (apedata != APE_SEG_SIG_MAGIC)
6374 return;
6375
6376 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
Matt Carlson731fd792008-08-15 14:07:51 -07006377 if (!(apedata & APE_FW_STATUS_READY))
Matt Carlson0d3031d2007-10-10 18:02:43 -07006378 return;
6379
6380 /* Wait for up to 1 millisecond for APE to service previous event. */
6381 for (i = 0; i < 10; i++) {
6382 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
6383 return;
6384
6385 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
6386
6387 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6388 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
6389 event | APE_EVENT_STATUS_EVENT_PENDING);
6390
6391 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
6392
6393 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6394 break;
6395
6396 udelay(100);
6397 }
6398
6399 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6400 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
6401}
6402
6403static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
6404{
6405 u32 event;
6406 u32 apedata;
6407
6408 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
6409 return;
6410
6411 switch (kind) {
6412 case RESET_KIND_INIT:
6413 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
6414 APE_HOST_SEG_SIG_MAGIC);
6415 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
6416 APE_HOST_SEG_LEN_MAGIC);
6417 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
6418 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
6419 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
6420 APE_HOST_DRIVER_ID_MAGIC);
6421 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
6422 APE_HOST_BEHAV_NO_PHYLOCK);
6423
6424 event = APE_EVENT_STATUS_STATE_START;
6425 break;
6426 case RESET_KIND_SHUTDOWN:
Matt Carlsonb2aee152008-11-03 16:51:11 -08006427 /* With the interface we are currently using,
6428 * APE does not track driver state. Wiping
6429 * out the HOST SEGMENT SIGNATURE forces
6430 * the APE to assume OS absent status.
6431 */
6432 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
6433
Matt Carlson0d3031d2007-10-10 18:02:43 -07006434 event = APE_EVENT_STATUS_STATE_UNLOAD;
6435 break;
6436 case RESET_KIND_SUSPEND:
6437 event = APE_EVENT_STATUS_STATE_SUSPEND;
6438 break;
6439 default:
6440 return;
6441 }
6442
6443 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
6444
6445 tg3_ape_send_event(tp, event);
6446}
6447
Michael Chane6af3012005-04-21 17:12:05 -07006448/* tp->lock is held. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006449static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
6450{
David S. Millerf49639e2006-06-09 11:58:36 -07006451 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
6452 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006453
6454 if (tp->tg3_flags2 & TG3_FLG2_ASF_NEW_HANDSHAKE) {
6455 switch (kind) {
6456 case RESET_KIND_INIT:
6457 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6458 DRV_STATE_START);
6459 break;
6460
6461 case RESET_KIND_SHUTDOWN:
6462 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6463 DRV_STATE_UNLOAD);
6464 break;
6465
6466 case RESET_KIND_SUSPEND:
6467 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6468 DRV_STATE_SUSPEND);
6469 break;
6470
6471 default:
6472 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006474 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07006475
6476 if (kind == RESET_KIND_INIT ||
6477 kind == RESET_KIND_SUSPEND)
6478 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006479}
6480
6481/* tp->lock is held. */
6482static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
6483{
6484 if (tp->tg3_flags2 & TG3_FLG2_ASF_NEW_HANDSHAKE) {
6485 switch (kind) {
6486 case RESET_KIND_INIT:
6487 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6488 DRV_STATE_START_DONE);
6489 break;
6490
6491 case RESET_KIND_SHUTDOWN:
6492 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6493 DRV_STATE_UNLOAD_DONE);
6494 break;
6495
6496 default:
6497 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006499 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07006500
6501 if (kind == RESET_KIND_SHUTDOWN)
6502 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006503}
6504
6505/* tp->lock is held. */
6506static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
6507{
6508 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) {
6509 switch (kind) {
6510 case RESET_KIND_INIT:
6511 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6512 DRV_STATE_START);
6513 break;
6514
6515 case RESET_KIND_SHUTDOWN:
6516 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6517 DRV_STATE_UNLOAD);
6518 break;
6519
6520 case RESET_KIND_SUSPEND:
6521 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6522 DRV_STATE_SUSPEND);
6523 break;
6524
6525 default:
6526 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006528 }
6529}
6530
Michael Chan7a6f4362006-09-27 16:03:31 -07006531static int tg3_poll_fw(struct tg3 *tp)
6532{
6533 int i;
6534 u32 val;
6535
Michael Chanb5d37722006-09-27 16:06:21 -07006536 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Gary Zambrano0ccead12006-11-14 16:34:00 -08006537 /* Wait up to 20ms for init done. */
6538 for (i = 0; i < 200; i++) {
Michael Chanb5d37722006-09-27 16:06:21 -07006539 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
6540 return 0;
Gary Zambrano0ccead12006-11-14 16:34:00 -08006541 udelay(100);
Michael Chanb5d37722006-09-27 16:06:21 -07006542 }
6543 return -ENODEV;
6544 }
6545
Michael Chan7a6f4362006-09-27 16:03:31 -07006546 /* Wait for firmware initialization to complete. */
6547 for (i = 0; i < 100000; i++) {
6548 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
6549 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
6550 break;
6551 udelay(10);
6552 }
6553
6554 /* Chip might not be fitted with firmware. Some Sun onboard
6555 * parts are configured like that. So don't signal the timeout
6556 * of the above loop as an error, but do report the lack of
6557 * running firmware once.
6558 */
6559 if (i >= 100000 &&
6560 !(tp->tg3_flags2 & TG3_FLG2_NO_FWARE_REPORTED)) {
6561 tp->tg3_flags2 |= TG3_FLG2_NO_FWARE_REPORTED;
6562
6563 printk(KERN_INFO PFX "%s: No firmware running.\n",
6564 tp->dev->name);
6565 }
6566
6567 return 0;
6568}
6569
Michael Chanee6a99b2007-07-18 21:49:10 -07006570/* Save PCI command register before chip reset */
6571static void tg3_save_pci_state(struct tg3 *tp)
6572{
Matt Carlson8a6eac92007-10-21 16:17:55 -07006573 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07006574}
6575
6576/* Restore PCI state after chip reset */
6577static void tg3_restore_pci_state(struct tg3 *tp)
6578{
6579 u32 val;
6580
6581 /* Re-enable indirect register accesses. */
6582 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
6583 tp->misc_host_ctrl);
6584
6585 /* Set MAX PCI retry to zero. */
6586 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
6587 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
6588 (tp->tg3_flags & TG3_FLAG_PCIX_MODE))
6589 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07006590 /* Allow reads and writes to the APE register and memory space. */
6591 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
6592 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
6593 PCISTATE_ALLOW_APE_SHMEM_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07006594 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
6595
Matt Carlson8a6eac92007-10-21 16:17:55 -07006596 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07006597
Matt Carlsonfcb389d2008-11-03 16:55:44 -08006598 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785) {
6599 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)
6600 pcie_set_readrq(tp->pdev, 4096);
6601 else {
6602 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
6603 tp->pci_cacheline_sz);
6604 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
6605 tp->pci_lat_timer);
6606 }
Michael Chan114342f2007-10-15 02:12:26 -07006607 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08006608
Michael Chanee6a99b2007-07-18 21:49:10 -07006609 /* Make sure PCI-X relaxed ordering bit is clear. */
Matt Carlson52f44902008-11-21 17:17:04 -08006610 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
Matt Carlson9974a352007-10-07 23:27:28 -07006611 u16 pcix_cmd;
6612
6613 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
6614 &pcix_cmd);
6615 pcix_cmd &= ~PCI_X_CMD_ERO;
6616 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
6617 pcix_cmd);
6618 }
Michael Chanee6a99b2007-07-18 21:49:10 -07006619
6620 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
Michael Chanee6a99b2007-07-18 21:49:10 -07006621
6622 /* Chip reset on 5780 will reset MSI enable bit,
6623 * so need to restore it.
6624 */
6625 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
6626 u16 ctrl;
6627
6628 pci_read_config_word(tp->pdev,
6629 tp->msi_cap + PCI_MSI_FLAGS,
6630 &ctrl);
6631 pci_write_config_word(tp->pdev,
6632 tp->msi_cap + PCI_MSI_FLAGS,
6633 ctrl | PCI_MSI_FLAGS_ENABLE);
6634 val = tr32(MSGINT_MODE);
6635 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
6636 }
6637 }
6638}
6639
Linus Torvalds1da177e2005-04-16 15:20:36 -07006640static void tg3_stop_fw(struct tg3 *);
6641
6642/* tp->lock is held. */
6643static int tg3_chip_reset(struct tg3 *tp)
6644{
6645 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07006646 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00006647 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006648
David S. Millerf49639e2006-06-09 11:58:36 -07006649 tg3_nvram_lock(tp);
6650
Matt Carlson77b483f2008-08-15 14:07:24 -07006651 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
6652
David S. Millerf49639e2006-06-09 11:58:36 -07006653 /* No matching tg3_nvram_unlock() after this because
6654 * chip reset below will undo the nvram lock.
6655 */
6656 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006657
Michael Chanee6a99b2007-07-18 21:49:10 -07006658 /* GRC_MISC_CFG core clock reset will clear the memory
6659 * enable bit in PCI register 4 and the MSI enable bit
6660 * on some chips, so we save relevant registers here.
6661 */
6662 tg3_save_pci_state(tp);
6663
Michael Chand9ab5ad2006-03-20 22:27:35 -08006664 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08006665 (tp->tg3_flags3 & TG3_FLG3_5755_PLUS))
Michael Chand9ab5ad2006-03-20 22:27:35 -08006666 tw32(GRC_FASTBOOT_PC, 0);
6667
Linus Torvalds1da177e2005-04-16 15:20:36 -07006668 /*
6669 * We must avoid the readl() that normally takes place.
6670 * It locks machines, causes machine checks, and other
6671 * fun things. So, temporarily disable the 5701
6672 * hardware workaround, while we do the reset.
6673 */
Michael Chan1ee582d2005-08-09 20:16:46 -07006674 write_op = tp->write32;
6675 if (write_op == tg3_write_flush_reg32)
6676 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006677
Michael Chand18edcb2007-03-24 20:57:11 -07006678 /* Prevent the irq handler from reading or writing PCI registers
6679 * during chip reset when the memory enable bit in the PCI command
6680 * register may be cleared. The chip does not generate interrupt
6681 * at this time, but the irq handler may still be called due to irq
6682 * sharing or irqpoll.
6683 */
6684 tp->tg3_flags |= TG3_FLAG_CHIP_RESETTING;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006685 for (i = 0; i < tp->irq_cnt; i++) {
6686 struct tg3_napi *tnapi = &tp->napi[i];
6687 if (tnapi->hw_status) {
6688 tnapi->hw_status->status = 0;
6689 tnapi->hw_status->status_tag = 0;
6690 }
6691 tnapi->last_tag = 0;
6692 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07006693 }
Michael Chand18edcb2007-03-24 20:57:11 -07006694 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00006695
6696 for (i = 0; i < tp->irq_cnt; i++)
6697 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07006698
Matt Carlson255ca312009-08-25 10:07:27 +00006699 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
6700 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
6701 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
6702 }
6703
Linus Torvalds1da177e2005-04-16 15:20:36 -07006704 /* do the reset */
6705 val = GRC_MISC_CFG_CORECLK_RESET;
6706
6707 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
6708 if (tr32(0x7e2c) == 0x60) {
6709 tw32(0x7e2c, 0x20);
6710 }
6711 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
6712 tw32(GRC_MISC_CFG, (1 << 29));
6713 val |= (1 << 29);
6714 }
6715 }
6716
Michael Chanb5d37722006-09-27 16:06:21 -07006717 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
6718 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
6719 tw32(GRC_VCPU_EXT_CTRL,
6720 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
6721 }
6722
Linus Torvalds1da177e2005-04-16 15:20:36 -07006723 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
6724 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
6725 tw32(GRC_MISC_CFG, val);
6726
Michael Chan1ee582d2005-08-09 20:16:46 -07006727 /* restore 5701 hardware bug workaround write method */
6728 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006729
6730 /* Unfortunately, we have to delay before the PCI read back.
6731 * Some 575X chips even will not respond to a PCI cfg access
6732 * when the reset command is given to the chip.
6733 *
6734 * How do these hardware designers expect things to work
6735 * properly if the PCI write is posted for a long period
6736 * of time? It is always necessary to have some method by
6737 * which a register read back can occur to push the write
6738 * out which does the reset.
6739 *
6740 * For most tg3 variants the trick below was working.
6741 * Ho hum...
6742 */
6743 udelay(120);
6744
6745 /* Flush PCI posted writes. The normal MMIO registers
6746 * are inaccessible at this time so this is the only
6747 * way to make this reliably (actually, this is no longer
6748 * the case, see above). I tried to use indirect
6749 * register read/write but this upset some 5701 variants.
6750 */
6751 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
6752
6753 udelay(120);
6754
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006755 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) && tp->pcie_cap) {
Matt Carlsone7126992009-08-25 10:08:16 +00006756 u16 val16;
6757
Linus Torvalds1da177e2005-04-16 15:20:36 -07006758 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
6759 int i;
6760 u32 cfg_val;
6761
6762 /* Wait for link training to complete. */
6763 for (i = 0; i < 5000; i++)
6764 udelay(100);
6765
6766 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
6767 pci_write_config_dword(tp->pdev, 0xc4,
6768 cfg_val | (1 << 15));
6769 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006770
Matt Carlsone7126992009-08-25 10:08:16 +00006771 /* Clear the "no snoop" and "relaxed ordering" bits. */
6772 pci_read_config_word(tp->pdev,
6773 tp->pcie_cap + PCI_EXP_DEVCTL,
6774 &val16);
6775 val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
6776 PCI_EXP_DEVCTL_NOSNOOP_EN);
6777 /*
6778 * Older PCIe devices only support the 128 byte
6779 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006780 */
Matt Carlsone7126992009-08-25 10:08:16 +00006781 if (!(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
6782 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784))
6783 val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006784 pci_write_config_word(tp->pdev,
6785 tp->pcie_cap + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00006786 val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006787
6788 pcie_set_readrq(tp->pdev, 4096);
6789
6790 /* Clear error status */
6791 pci_write_config_word(tp->pdev,
6792 tp->pcie_cap + PCI_EXP_DEVSTA,
6793 PCI_EXP_DEVSTA_CED |
6794 PCI_EXP_DEVSTA_NFED |
6795 PCI_EXP_DEVSTA_FED |
6796 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006797 }
6798
Michael Chanee6a99b2007-07-18 21:49:10 -07006799 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006800
Michael Chand18edcb2007-03-24 20:57:11 -07006801 tp->tg3_flags &= ~TG3_FLAG_CHIP_RESETTING;
6802
Michael Chanee6a99b2007-07-18 21:49:10 -07006803 val = 0;
6804 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
Michael Chan4cf78e42005-07-25 12:29:19 -07006805 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07006806 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006807
6808 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
6809 tg3_stop_fw(tp);
6810 tw32(0x5000, 0x400);
6811 }
6812
6813 tw32(GRC_MODE, tp->grc_mode);
6814
6815 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01006816 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006817
6818 tw32(0xc4, val | (1 << 15));
6819 }
6820
6821 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
6822 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
6823 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
6824 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
6825 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
6826 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
6827 }
6828
6829 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
6830 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
6831 tw32_f(MAC_MODE, tp->mac_mode);
Michael Chan747e8f82005-07-25 12:33:22 -07006832 } else if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES) {
6833 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
6834 tw32_f(MAC_MODE, tp->mac_mode);
Matt Carlson3bda1252008-08-15 14:08:22 -07006835 } else if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
6836 tp->mac_mode &= (MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN);
6837 if (tp->mac_mode & MAC_MODE_APE_TX_EN)
6838 tp->mac_mode |= MAC_MODE_TDE_ENABLE;
6839 tw32_f(MAC_MODE, tp->mac_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006840 } else
6841 tw32_f(MAC_MODE, 0);
6842 udelay(40);
6843
Matt Carlson77b483f2008-08-15 14:07:24 -07006844 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
6845
Michael Chan7a6f4362006-09-27 16:03:31 -07006846 err = tg3_poll_fw(tp);
6847 if (err)
6848 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006849
Matt Carlson0a9140c2009-08-28 12:27:50 +00006850 tg3_mdio_start(tp);
6851
Matt Carlson52cdf852009-11-02 14:25:06 +00006852 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
6853 u8 phy_addr;
6854
6855 phy_addr = tp->phy_addr;
6856 tp->phy_addr = TG3_PHY_PCIE_ADDR;
6857
6858 tg3_writephy(tp, TG3_PCIEPHY_BLOCK_ADDR,
6859 TG3_PCIEPHY_TXB_BLK << TG3_PCIEPHY_BLOCK_SHIFT);
6860 val = TG3_PCIEPHY_TX0CTRL1_TXOCM | TG3_PCIEPHY_TX0CTRL1_RDCTL |
6861 TG3_PCIEPHY_TX0CTRL1_TXCMV | TG3_PCIEPHY_TX0CTRL1_TKSEL |
6862 TG3_PCIEPHY_TX0CTRL1_NB_EN;
6863 tg3_writephy(tp, TG3_PCIEPHY_TX0CTRL1, val);
6864 udelay(10);
6865
6866 tg3_writephy(tp, TG3_PCIEPHY_BLOCK_ADDR,
6867 TG3_PCIEPHY_XGXS_BLK1 << TG3_PCIEPHY_BLOCK_SHIFT);
6868 val = TG3_PCIEPHY_PWRMGMT4_LOWPWR_EN |
6869 TG3_PCIEPHY_PWRMGMT4_L1PLLPD_EN;
6870 tg3_writephy(tp, TG3_PCIEPHY_PWRMGMT4, val);
6871 udelay(10);
6872
6873 tp->phy_addr = phy_addr;
6874 }
6875
Linus Torvalds1da177e2005-04-16 15:20:36 -07006876 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00006877 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
6878 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
6879 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01006880 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006881
6882 tw32(0x7c00, val | (1 << 25));
6883 }
6884
6885 /* Reprobe ASF enable state. */
6886 tp->tg3_flags &= ~TG3_FLAG_ENABLE_ASF;
6887 tp->tg3_flags2 &= ~TG3_FLG2_ASF_NEW_HANDSHAKE;
6888 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
6889 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
6890 u32 nic_cfg;
6891
6892 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
6893 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
6894 tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
Matt Carlson4ba526c2008-08-15 14:10:04 -07006895 tp->last_event_jiffies = jiffies;
John W. Linvillecbf46852005-04-21 17:01:29 -07006896 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006897 tp->tg3_flags2 |= TG3_FLG2_ASF_NEW_HANDSHAKE;
6898 }
6899 }
6900
6901 return 0;
6902}
6903
6904/* tp->lock is held. */
6905static void tg3_stop_fw(struct tg3 *tp)
6906{
Matt Carlson0d3031d2007-10-10 18:02:43 -07006907 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
6908 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07006909 /* Wait for RX cpu to ACK the previous event. */
6910 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006911
6912 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
Matt Carlson4ba526c2008-08-15 14:10:04 -07006913
6914 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006915
Matt Carlson7c5026a2008-05-02 16:49:29 -07006916 /* Wait for RX cpu to ACK this event. */
6917 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006918 }
6919}
6920
6921/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07006922static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006923{
6924 int err;
6925
6926 tg3_stop_fw(tp);
6927
Michael Chan944d9802005-05-29 14:57:48 -07006928 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006929
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006930 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006931 err = tg3_chip_reset(tp);
6932
Matt Carlsondaba2a62009-04-20 06:58:52 +00006933 __tg3_set_mac_addr(tp, 0);
6934
Michael Chan944d9802005-05-29 14:57:48 -07006935 tg3_write_sig_legacy(tp, kind);
6936 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006937
6938 if (err)
6939 return err;
6940
6941 return 0;
6942}
6943
Linus Torvalds1da177e2005-04-16 15:20:36 -07006944#define RX_CPU_SCRATCH_BASE 0x30000
6945#define RX_CPU_SCRATCH_SIZE 0x04000
6946#define TX_CPU_SCRATCH_BASE 0x34000
6947#define TX_CPU_SCRATCH_SIZE 0x04000
6948
6949/* tp->lock is held. */
6950static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
6951{
6952 int i;
6953
Eric Sesterhenn5d9428d2006-04-02 13:52:48 +02006954 BUG_ON(offset == TX_CPU_BASE &&
6955 (tp->tg3_flags2 & TG3_FLG2_5705_PLUS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006956
Michael Chanb5d37722006-09-27 16:06:21 -07006957 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
6958 u32 val = tr32(GRC_VCPU_EXT_CTRL);
6959
6960 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
6961 return 0;
6962 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006963 if (offset == RX_CPU_BASE) {
6964 for (i = 0; i < 10000; i++) {
6965 tw32(offset + CPU_STATE, 0xffffffff);
6966 tw32(offset + CPU_MODE, CPU_MODE_HALT);
6967 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
6968 break;
6969 }
6970
6971 tw32(offset + CPU_STATE, 0xffffffff);
6972 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
6973 udelay(10);
6974 } else {
6975 for (i = 0; i < 10000; i++) {
6976 tw32(offset + CPU_STATE, 0xffffffff);
6977 tw32(offset + CPU_MODE, CPU_MODE_HALT);
6978 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
6979 break;
6980 }
6981 }
6982
6983 if (i >= 10000) {
6984 printk(KERN_ERR PFX "tg3_reset_cpu timed out for %s, "
6985 "and %s CPU\n",
6986 tp->dev->name,
6987 (offset == RX_CPU_BASE ? "RX" : "TX"));
6988 return -ENODEV;
6989 }
Michael Chanec41c7d2006-01-17 02:40:55 -08006990
6991 /* Clear firmware's nvram arbitration. */
6992 if (tp->tg3_flags & TG3_FLAG_NVRAM)
6993 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006994 return 0;
6995}
6996
6997struct fw_info {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006998 unsigned int fw_base;
6999 unsigned int fw_len;
7000 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007001};
7002
7003/* tp->lock is held. */
7004static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base, u32 cpu_scratch_base,
7005 int cpu_scratch_size, struct fw_info *info)
7006{
Michael Chanec41c7d2006-01-17 02:40:55 -08007007 int err, lock_err, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007008 void (*write_op)(struct tg3 *, u32, u32);
7009
7010 if (cpu_base == TX_CPU_BASE &&
7011 (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7012 printk(KERN_ERR PFX "tg3_load_firmware_cpu: Trying to load "
7013 "TX cpu firmware on %s which is 5705.\n",
7014 tp->dev->name);
7015 return -EINVAL;
7016 }
7017
7018 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
7019 write_op = tg3_write_mem;
7020 else
7021 write_op = tg3_write_indirect_reg32;
7022
Michael Chan1b628152005-05-29 14:59:49 -07007023 /* It is possible that bootcode is still loading at this point.
7024 * Get the nvram lock first before halting the cpu.
7025 */
Michael Chanec41c7d2006-01-17 02:40:55 -08007026 lock_err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007027 err = tg3_halt_cpu(tp, cpu_base);
Michael Chanec41c7d2006-01-17 02:40:55 -08007028 if (!lock_err)
7029 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007030 if (err)
7031 goto out;
7032
7033 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
7034 write_op(tp, cpu_scratch_base + i, 0);
7035 tw32(cpu_base + CPU_STATE, 0xffffffff);
7036 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007037 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007038 write_op(tp, (cpu_scratch_base +
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007039 (info->fw_base & 0xffff) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07007040 (i * sizeof(u32))),
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007041 be32_to_cpu(info->fw_data[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007042
7043 err = 0;
7044
7045out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007046 return err;
7047}
7048
7049/* tp->lock is held. */
7050static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
7051{
7052 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007053 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007054 int err, i;
7055
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007056 fw_data = (void *)tp->fw->data;
7057
7058 /* Firmware blob starts with version numbers, followed by
7059 start address and length. We are setting complete length.
7060 length = end_address_of_bss - start_address_of_text.
7061 Remainder is the blob to be loaded contiguously
7062 from start address. */
7063
7064 info.fw_base = be32_to_cpu(fw_data[1]);
7065 info.fw_len = tp->fw->size - 12;
7066 info.fw_data = &fw_data[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007067
7068 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
7069 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
7070 &info);
7071 if (err)
7072 return err;
7073
7074 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
7075 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
7076 &info);
7077 if (err)
7078 return err;
7079
7080 /* Now startup only the RX cpu. */
7081 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007082 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007083
7084 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007085 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007086 break;
7087 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7088 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007089 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007090 udelay(1000);
7091 }
7092 if (i >= 5) {
7093 printk(KERN_ERR PFX "tg3_load_firmware fails for %s "
7094 "to set RX CPU PC, is %08x should be %08x\n",
7095 tp->dev->name, tr32(RX_CPU_BASE + CPU_PC),
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007096 info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007097 return -ENODEV;
7098 }
7099 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7100 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
7101
7102 return 0;
7103}
7104
Linus Torvalds1da177e2005-04-16 15:20:36 -07007105/* 5705 needs a special version of the TSO firmware. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007106
7107/* tp->lock is held. */
7108static int tg3_load_tso_firmware(struct tg3 *tp)
7109{
7110 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007111 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007112 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
7113 int err, i;
7114
7115 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
7116 return 0;
7117
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007118 fw_data = (void *)tp->fw->data;
7119
7120 /* Firmware blob starts with version numbers, followed by
7121 start address and length. We are setting complete length.
7122 length = end_address_of_bss - start_address_of_text.
7123 Remainder is the blob to be loaded contiguously
7124 from start address. */
7125
7126 info.fw_base = be32_to_cpu(fw_data[1]);
7127 cpu_scratch_size = tp->fw_len;
7128 info.fw_len = tp->fw->size - 12;
7129 info.fw_data = &fw_data[3];
7130
Linus Torvalds1da177e2005-04-16 15:20:36 -07007131 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007132 cpu_base = RX_CPU_BASE;
7133 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007134 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007135 cpu_base = TX_CPU_BASE;
7136 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
7137 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
7138 }
7139
7140 err = tg3_load_firmware_cpu(tp, cpu_base,
7141 cpu_scratch_base, cpu_scratch_size,
7142 &info);
7143 if (err)
7144 return err;
7145
7146 /* Now startup the cpu. */
7147 tw32(cpu_base + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007148 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007149
7150 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007151 if (tr32(cpu_base + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007152 break;
7153 tw32(cpu_base + CPU_STATE, 0xffffffff);
7154 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007155 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007156 udelay(1000);
7157 }
7158 if (i >= 5) {
7159 printk(KERN_ERR PFX "tg3_load_tso_firmware fails for %s "
7160 "to set CPU PC, is %08x should be %08x\n",
7161 tp->dev->name, tr32(cpu_base + CPU_PC),
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007162 info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007163 return -ENODEV;
7164 }
7165 tw32(cpu_base + CPU_STATE, 0xffffffff);
7166 tw32_f(cpu_base + CPU_MODE, 0x00000000);
7167 return 0;
7168}
7169
Linus Torvalds1da177e2005-04-16 15:20:36 -07007170
Linus Torvalds1da177e2005-04-16 15:20:36 -07007171static int tg3_set_mac_addr(struct net_device *dev, void *p)
7172{
7173 struct tg3 *tp = netdev_priv(dev);
7174 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07007175 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007176
Michael Chanf9804dd2005-09-27 12:13:10 -07007177 if (!is_valid_ether_addr(addr->sa_data))
7178 return -EINVAL;
7179
Linus Torvalds1da177e2005-04-16 15:20:36 -07007180 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7181
Michael Chane75f7c92006-03-20 21:33:26 -08007182 if (!netif_running(dev))
7183 return 0;
7184
Michael Chan58712ef2006-04-29 18:58:01 -07007185 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) {
Michael Chan986e0ae2007-05-05 12:10:20 -07007186 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07007187
Michael Chan986e0ae2007-05-05 12:10:20 -07007188 addr0_high = tr32(MAC_ADDR_0_HIGH);
7189 addr0_low = tr32(MAC_ADDR_0_LOW);
7190 addr1_high = tr32(MAC_ADDR_1_HIGH);
7191 addr1_low = tr32(MAC_ADDR_1_LOW);
7192
7193 /* Skip MAC addr 1 if ASF is using it. */
7194 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
7195 !(addr1_high == 0 && addr1_low == 0))
7196 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07007197 }
Michael Chan986e0ae2007-05-05 12:10:20 -07007198 spin_lock_bh(&tp->lock);
7199 __tg3_set_mac_addr(tp, skip_mac_1);
7200 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007201
Michael Chanb9ec6c12006-07-25 16:37:27 -07007202 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007203}
7204
7205/* tp->lock is held. */
7206static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
7207 dma_addr_t mapping, u32 maxlen_flags,
7208 u32 nic_addr)
7209{
7210 tg3_write_mem(tp,
7211 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
7212 ((u64) mapping >> 32));
7213 tg3_write_mem(tp,
7214 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
7215 ((u64) mapping & 0xffffffff));
7216 tg3_write_mem(tp,
7217 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
7218 maxlen_flags);
7219
7220 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7221 tg3_write_mem(tp,
7222 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
7223 nic_addr);
7224}
7225
7226static void __tg3_set_rx_mode(struct net_device *);
Michael Chand244c892005-07-05 14:42:33 -07007227static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07007228{
Matt Carlsonb6080e12009-09-01 13:12:00 +00007229 int i;
7230
7231 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSIX)) {
7232 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
7233 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
7234 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
7235
7236 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
7237 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
7238 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
7239 } else {
7240 tw32(HOSTCC_TXCOL_TICKS, 0);
7241 tw32(HOSTCC_TXMAX_FRAMES, 0);
7242 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
7243
7244 tw32(HOSTCC_RXCOL_TICKS, 0);
7245 tw32(HOSTCC_RXMAX_FRAMES, 0);
7246 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07007247 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007248
David S. Miller15f98502005-05-18 22:49:26 -07007249 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7250 u32 val = ec->stats_block_coalesce_usecs;
7251
Matt Carlsonb6080e12009-09-01 13:12:00 +00007252 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
7253 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
7254
David S. Miller15f98502005-05-18 22:49:26 -07007255 if (!netif_carrier_ok(tp->dev))
7256 val = 0;
7257
7258 tw32(HOSTCC_STAT_COAL_TICKS, val);
7259 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007260
7261 for (i = 0; i < tp->irq_cnt - 1; i++) {
7262 u32 reg;
7263
7264 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
7265 tw32(reg, ec->rx_coalesce_usecs);
7266 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
7267 tw32(reg, ec->tx_coalesce_usecs);
7268 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
7269 tw32(reg, ec->rx_max_coalesced_frames);
7270 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
7271 tw32(reg, ec->tx_max_coalesced_frames);
7272 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
7273 tw32(reg, ec->rx_max_coalesced_frames_irq);
7274 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
7275 tw32(reg, ec->tx_max_coalesced_frames_irq);
7276 }
7277
7278 for (; i < tp->irq_max - 1; i++) {
7279 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
7280 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
7281 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
7282 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
7283 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
7284 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
7285 }
David S. Miller15f98502005-05-18 22:49:26 -07007286}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007287
7288/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00007289static void tg3_rings_reset(struct tg3 *tp)
7290{
7291 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007292 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007293 struct tg3_napi *tnapi = &tp->napi[0];
7294
7295 /* Disable all transmit rings but the first. */
7296 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7297 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
7298 else
7299 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7300
7301 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7302 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
7303 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
7304 BDINFO_FLAGS_DISABLED);
7305
7306
7307 /* Disable all receive return rings but the first. */
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007308 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
7309 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
7310 else if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00007311 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
7312 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
7313 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
7314 else
7315 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7316
7317 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7318 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
7319 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
7320 BDINFO_FLAGS_DISABLED);
7321
7322 /* Disable interrupts */
7323 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
7324
7325 /* Zero mailbox registers. */
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007326 if (tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX) {
7327 for (i = 1; i < TG3_IRQ_MAX_VECS; i++) {
7328 tp->napi[i].tx_prod = 0;
7329 tp->napi[i].tx_cons = 0;
7330 tw32_mailbox(tp->napi[i].prodmbox, 0);
7331 tw32_rx_mbox(tp->napi[i].consmbox, 0);
7332 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
7333 }
7334 } else {
7335 tp->napi[0].tx_prod = 0;
7336 tp->napi[0].tx_cons = 0;
7337 tw32_mailbox(tp->napi[0].prodmbox, 0);
7338 tw32_rx_mbox(tp->napi[0].consmbox, 0);
7339 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007340
7341 /* Make sure the NIC-based send BD rings are disabled. */
7342 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7343 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
7344 for (i = 0; i < 16; i++)
7345 tw32_tx_mbox(mbox + i * 8, 0);
7346 }
7347
7348 txrcb = NIC_SRAM_SEND_RCB;
7349 rxrcb = NIC_SRAM_RCV_RET_RCB;
7350
7351 /* Clear status block in ram. */
7352 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7353
7354 /* Set status block DMA address */
7355 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
7356 ((u64) tnapi->status_mapping >> 32));
7357 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
7358 ((u64) tnapi->status_mapping & 0xffffffff));
7359
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007360 if (tnapi->tx_ring) {
7361 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7362 (TG3_TX_RING_SIZE <<
7363 BDINFO_FLAGS_MAXLEN_SHIFT),
7364 NIC_SRAM_TX_BUFFER_DESC);
7365 txrcb += TG3_BDINFO_SIZE;
7366 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007367
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007368 if (tnapi->rx_rcb) {
7369 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
7370 (TG3_RX_RCB_RING_SIZE(tp) <<
7371 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
7372 rxrcb += TG3_BDINFO_SIZE;
7373 }
7374
7375 stblk = HOSTCC_STATBLCK_RING1;
7376
7377 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
7378 u64 mapping = (u64)tnapi->status_mapping;
7379 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
7380 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
7381
7382 /* Clear status block in ram. */
7383 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7384
7385 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7386 (TG3_TX_RING_SIZE <<
7387 BDINFO_FLAGS_MAXLEN_SHIFT),
7388 NIC_SRAM_TX_BUFFER_DESC);
7389
7390 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
7391 (TG3_RX_RCB_RING_SIZE(tp) <<
7392 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
7393
7394 stblk += 8;
7395 txrcb += TG3_BDINFO_SIZE;
7396 rxrcb += TG3_BDINFO_SIZE;
7397 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007398}
7399
7400/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07007401static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007402{
7403 u32 val, rdmac_mode;
7404 int i, err, limit;
Matt Carlson21f581a2009-08-28 14:00:25 +00007405 struct tg3_rx_prodring_set *tpr = &tp->prodring[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007406
7407 tg3_disable_ints(tp);
7408
7409 tg3_stop_fw(tp);
7410
7411 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
7412
7413 if (tp->tg3_flags & TG3_FLAG_INIT_COMPLETE) {
Michael Chane6de8ad2005-05-05 14:42:41 -07007414 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007415 }
7416
Matt Carlsondd477002008-05-25 23:45:58 -07007417 if (reset_phy &&
7418 !(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB))
Michael Chand4d2c552006-03-20 17:47:20 -08007419 tg3_phy_reset(tp);
7420
Linus Torvalds1da177e2005-04-16 15:20:36 -07007421 err = tg3_chip_reset(tp);
7422 if (err)
7423 return err;
7424
7425 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
7426
Matt Carlsonbcb37f62008-11-03 16:52:09 -08007427 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07007428 val = tr32(TG3_CPMU_CTRL);
7429 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
7430 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08007431
7432 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
7433 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
7434 val |= CPMU_LSPD_10MB_MACCLK_6_25;
7435 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
7436
7437 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
7438 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
7439 val |= CPMU_LNK_AWARE_MACCLK_6_25;
7440 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
7441
7442 val = tr32(TG3_CPMU_HST_ACC);
7443 val &= ~CPMU_HST_ACC_MACCLK_MASK;
7444 val |= CPMU_HST_ACC_MACCLK_6_25;
7445 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07007446 }
7447
Matt Carlson33466d92009-04-20 06:57:41 +00007448 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7449 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
7450 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
7451 PCIE_PWR_MGMT_L1_THRESH_4MS;
7452 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00007453
7454 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
7455 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
7456
7457 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00007458
Matt Carlsonf40386c2009-11-02 14:24:02 +00007459 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7460 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00007461 }
7462
Linus Torvalds1da177e2005-04-16 15:20:36 -07007463 /* This works around an issue with Athlon chipsets on
7464 * B3 tigon3 silicon. This bit has no effect on any
7465 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07007466 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007467 */
Matt Carlson795d01c2007-10-07 23:28:17 -07007468 if (!(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT)) {
7469 if (!(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
7470 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
7471 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007473
7474 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
7475 (tp->tg3_flags & TG3_FLAG_PCIX_MODE)) {
7476 val = tr32(TG3PCI_PCISTATE);
7477 val |= PCISTATE_RETRY_SAME_DMA;
7478 tw32(TG3PCI_PCISTATE, val);
7479 }
7480
Matt Carlson0d3031d2007-10-10 18:02:43 -07007481 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
7482 /* Allow reads and writes to the
7483 * APE register and memory space.
7484 */
7485 val = tr32(TG3PCI_PCISTATE);
7486 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
7487 PCISTATE_ALLOW_APE_SHMEM_WR;
7488 tw32(TG3PCI_PCISTATE, val);
7489 }
7490
Linus Torvalds1da177e2005-04-16 15:20:36 -07007491 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
7492 /* Enable some hw fixes. */
7493 val = tr32(TG3PCI_MSI_DATA);
7494 val |= (1 << 26) | (1 << 28) | (1 << 29);
7495 tw32(TG3PCI_MSI_DATA, val);
7496 }
7497
7498 /* Descriptor ring init may make accesses to the
7499 * NIC SRAM area to setup the TX descriptors, so we
7500 * can only do this after the hardware has been
7501 * successfully reset.
7502 */
Michael Chan32d8c572006-07-25 16:38:29 -07007503 err = tg3_init_rings(tp);
7504 if (err)
7505 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007506
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00007507 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
7508 val = tr32(TG3PCI_DMA_RW_CTRL) &
7509 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
7510 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
7511 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
7512 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07007513 /* This value is determined during the probe time DMA
7514 * engine test, tg3_test_dma.
7515 */
7516 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
7517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007518
7519 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
7520 GRC_MODE_4X_NIC_SEND_RINGS |
7521 GRC_MODE_NO_TX_PHDR_CSUM |
7522 GRC_MODE_NO_RX_PHDR_CSUM);
7523 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07007524
7525 /* Pseudo-header checksum is done by hardware logic and not
7526 * the offload processers, so make the chip do the pseudo-
7527 * header checksums on receive. For transmit it is more
7528 * convenient to do the pseudo-header checksum in software
7529 * as Linux does that on transmit for us in all cases.
7530 */
7531 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007532
7533 tw32(GRC_MODE,
7534 tp->grc_mode |
7535 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
7536
7537 /* Setup the timer prescalar register. Clock is always 66Mhz. */
7538 val = tr32(GRC_MISC_CFG);
7539 val &= ~0xff;
7540 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
7541 tw32(GRC_MISC_CFG, val);
7542
7543 /* Initialize MBUF/DESC pool. */
John W. Linvillecbf46852005-04-21 17:01:29 -07007544 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007545 /* Do nothing. */
7546 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
7547 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
7548 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
7549 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
7550 else
7551 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
7552 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
7553 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
7554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007555 else if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) {
7556 int fw_len;
7557
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007558 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007559 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
7560 tw32(BUFMGR_MB_POOL_ADDR,
7561 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
7562 tw32(BUFMGR_MB_POOL_SIZE,
7563 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
7564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007565
Michael Chan0f893dc2005-07-25 12:30:38 -07007566 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007567 tw32(BUFMGR_MB_RDMA_LOW_WATER,
7568 tp->bufmgr_config.mbuf_read_dma_low_water);
7569 tw32(BUFMGR_MB_MACRX_LOW_WATER,
7570 tp->bufmgr_config.mbuf_mac_rx_low_water);
7571 tw32(BUFMGR_MB_HIGH_WATER,
7572 tp->bufmgr_config.mbuf_high_water);
7573 } else {
7574 tw32(BUFMGR_MB_RDMA_LOW_WATER,
7575 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
7576 tw32(BUFMGR_MB_MACRX_LOW_WATER,
7577 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
7578 tw32(BUFMGR_MB_HIGH_WATER,
7579 tp->bufmgr_config.mbuf_high_water_jumbo);
7580 }
7581 tw32(BUFMGR_DMA_LOW_WATER,
7582 tp->bufmgr_config.dma_low_water);
7583 tw32(BUFMGR_DMA_HIGH_WATER,
7584 tp->bufmgr_config.dma_high_water);
7585
7586 tw32(BUFMGR_MODE, BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE);
7587 for (i = 0; i < 2000; i++) {
7588 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
7589 break;
7590 udelay(10);
7591 }
7592 if (i >= 2000) {
7593 printk(KERN_ERR PFX "tg3_reset_hw cannot enable BUFMGR for %s.\n",
7594 tp->dev->name);
7595 return -ENODEV;
7596 }
7597
7598 /* Setup replenish threshold. */
Michael Chanf92905d2006-06-29 20:14:29 -07007599 val = tp->rx_pending / 8;
7600 if (val == 0)
7601 val = 1;
7602 else if (val > tp->rx_std_max_post)
7603 val = tp->rx_std_max_post;
Michael Chanb5d37722006-09-27 16:06:21 -07007604 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7605 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
7606 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
7607
7608 if (val > (TG3_RX_INTERNAL_RING_SZ_5906 / 2))
7609 val = TG3_RX_INTERNAL_RING_SZ_5906 / 2;
7610 }
Michael Chanf92905d2006-06-29 20:14:29 -07007611
7612 tw32(RCVBDI_STD_THRESH, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007613
7614 /* Initialize TG3_BDINFO's at:
7615 * RCVDBDI_STD_BD: standard eth size rx ring
7616 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
7617 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
7618 *
7619 * like so:
7620 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
7621 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
7622 * ring attribute flags
7623 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
7624 *
7625 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
7626 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
7627 *
7628 * The size of each ring is fixed in the firmware, but the location is
7629 * configurable.
7630 */
7631 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00007632 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007633 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00007634 ((u64) tpr->rx_std_mapping & 0xffffffff));
Matt Carlson87668d32009-11-13 13:03:34 +00007635 if (!(tp->tg3_flags3 & TG3_FLG3_5755_PLUS))
7636 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
7637 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007638
Matt Carlsonfdb72b32009-08-28 13:57:12 +00007639 /* Disable the mini ring */
7640 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007641 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
7642 BDINFO_FLAGS_DISABLED);
7643
Matt Carlsonfdb72b32009-08-28 13:57:12 +00007644 /* Program the jumbo buffer descriptor ring control
7645 * blocks on those devices that have them.
7646 */
Matt Carlson8f666b02009-08-28 13:58:24 +00007647 if ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) &&
Matt Carlsonfdb72b32009-08-28 13:57:12 +00007648 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007649 /* Setup replenish threshold. */
7650 tw32(RCVBDI_JUMBO_THRESH, tp->rx_jumbo_pending / 8);
7651
Michael Chan0f893dc2005-07-25 12:30:38 -07007652 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007653 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00007654 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007655 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00007656 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007657 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlson79ed5ac2009-08-28 14:00:55 +00007658 (RX_JUMBO_MAX_SIZE << BDINFO_FLAGS_MAXLEN_SHIFT) |
7659 BDINFO_FLAGS_USE_EXT_RECV);
Matt Carlson87668d32009-11-13 13:03:34 +00007660 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7661 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
7662 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007663 } else {
7664 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
7665 BDINFO_FLAGS_DISABLED);
7666 }
7667
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007668 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
7669 val = (RX_STD_MAX_SIZE_5705 << BDINFO_FLAGS_MAXLEN_SHIFT) |
7670 (RX_STD_MAX_SIZE << 2);
7671 else
7672 val = RX_STD_MAX_SIZE << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00007673 } else
7674 val = RX_STD_MAX_SIZE_5705 << BDINFO_FLAGS_MAXLEN_SHIFT;
7675
7676 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007677
Matt Carlson411da642009-11-13 13:03:46 +00007678 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e62009-11-13 13:03:49 +00007679 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007680
Matt Carlson411da642009-11-13 13:03:46 +00007681 tpr->rx_jmb_prod_idx = (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) ?
Matt Carlson21f581a2009-08-28 14:00:25 +00007682 tp->rx_jumbo_pending : 0;
Matt Carlson66711e62009-11-13 13:03:49 +00007683 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007684
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007685 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
7686 tw32(STD_REPLENISH_LWM, 32);
7687 tw32(JMB_REPLENISH_LWM, 16);
7688 }
7689
Matt Carlson2d31eca2009-09-01 12:53:31 +00007690 tg3_rings_reset(tp);
7691
Linus Torvalds1da177e2005-04-16 15:20:36 -07007692 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07007693 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007694
7695 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00007696 tw32(MAC_RX_MTU_SIZE,
7697 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007698
7699 /* The slot time is changed by tg3_setup_phy if we
7700 * run at gigabit with half duplex.
7701 */
7702 tw32(MAC_TX_LENGTHS,
7703 (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
7704 (6 << TX_LENGTHS_IPG_SHIFT) |
7705 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
7706
7707 /* Receive rules. */
7708 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
7709 tw32(RCVLPC_CONFIG, 0x0181);
7710
7711 /* Calculate RDMAC_MODE setting early, we need it to determine
7712 * the RCVLPC_STATE_ENABLE mask.
7713 */
7714 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
7715 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
7716 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
7717 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
7718 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07007719
Matt Carlson57e69832008-05-25 23:48:31 -07007720 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08007721 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
7722 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07007723 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
7724 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
7725 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
7726
Michael Chan85e94ce2005-04-21 17:05:28 -07007727 /* If statement applies to 5705 and 5750 PCI devices only */
7728 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
7729 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) ||
7730 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007731 if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07007732 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007733 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
7734 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
7735 !(tp->tg3_flags2 & TG3_FLG2_IS_5788)) {
7736 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
7737 }
7738 }
7739
Michael Chan85e94ce2005-04-21 17:05:28 -07007740 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)
7741 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
7742
Linus Torvalds1da177e2005-04-16 15:20:36 -07007743 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
Matt Carlson027455a2008-12-21 20:19:30 -08007744 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
7745
Matt Carlsone849cdc2009-11-13 13:03:38 +00007746 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
7747 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson027455a2008-12-21 20:19:30 -08007748 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
7749 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007750
7751 /* Receive/send statistics. */
Michael Chan16613942006-06-29 20:15:13 -07007752 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
7753 val = tr32(RCVLPC_STATS_ENABLE);
7754 val &= ~RCVLPC_STATSENAB_DACK_FIX;
7755 tw32(RCVLPC_STATS_ENABLE, val);
7756 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
7757 (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007758 val = tr32(RCVLPC_STATS_ENABLE);
7759 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
7760 tw32(RCVLPC_STATS_ENABLE, val);
7761 } else {
7762 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
7763 }
7764 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
7765 tw32(SNDDATAI_STATSENAB, 0xffffff);
7766 tw32(SNDDATAI_STATSCTRL,
7767 (SNDDATAI_SCTRL_ENABLE |
7768 SNDDATAI_SCTRL_FASTUPD));
7769
7770 /* Setup host coalescing engine. */
7771 tw32(HOSTCC_MODE, 0);
7772 for (i = 0; i < 2000; i++) {
7773 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
7774 break;
7775 udelay(10);
7776 }
7777
Michael Chand244c892005-07-05 14:42:33 -07007778 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007779
Linus Torvalds1da177e2005-04-16 15:20:36 -07007780 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7781 /* Status/statistics block address. See tg3_timer,
7782 * the tg3_periodic_fetch_stats call there, and
7783 * tg3_get_stats to see how this works for 5705/5750 chips.
7784 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007785 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
7786 ((u64) tp->stats_mapping >> 32));
7787 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
7788 ((u64) tp->stats_mapping & 0xffffffff));
7789 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00007790
Linus Torvalds1da177e2005-04-16 15:20:36 -07007791 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00007792
7793 /* Clear statistics and status block memory areas */
7794 for (i = NIC_SRAM_STATS_BLK;
7795 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
7796 i += sizeof(u32)) {
7797 tg3_write_mem(tp, i, 0);
7798 udelay(40);
7799 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007800 }
7801
7802 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
7803
7804 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
7805 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
7806 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7807 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
7808
Michael Chanc94e3942005-09-27 12:12:42 -07007809 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES) {
7810 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
7811 /* reset to prevent losing 1st rx packet intermittently */
7812 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
7813 udelay(10);
7814 }
7815
Matt Carlson3bda1252008-08-15 14:08:22 -07007816 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
7817 tp->mac_mode &= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
7818 else
7819 tp->mac_mode = 0;
7820 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Linus Torvalds1da177e2005-04-16 15:20:36 -07007821 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE | MAC_MODE_FHDE_ENABLE;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07007822 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
7823 !(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
7824 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
7825 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007826 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
7827 udelay(40);
7828
Michael Chan314fba32005-04-21 17:07:04 -07007829 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Michael Chan9d26e212006-12-07 00:21:14 -08007830 * If TG3_FLG2_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07007831 * register to preserve the GPIO settings for LOMs. The GPIOs,
7832 * whether used as inputs or outputs, are set by boot code after
7833 * reset.
7834 */
Michael Chan9d26e212006-12-07 00:21:14 -08007835 if (!(tp->tg3_flags2 & TG3_FLG2_IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07007836 u32 gpio_mask;
7837
Michael Chan9d26e212006-12-07 00:21:14 -08007838 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
7839 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
7840 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07007841
7842 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
7843 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
7844 GRC_LCLCTRL_GPIO_OUTPUT3;
7845
Michael Chanaf36e6b2006-03-23 01:28:06 -08007846 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
7847 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
7848
Gary Zambranoaaf84462007-05-05 11:51:45 -07007849 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07007850 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
7851
7852 /* GPIO1 must be driven high for eeprom write protect */
Michael Chan9d26e212006-12-07 00:21:14 -08007853 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT)
7854 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
7855 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07007856 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007857 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
7858 udelay(100);
7859
Matt Carlsonbaf8a942009-09-01 13:13:00 +00007860 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX) {
7861 val = tr32(MSGINT_MODE);
7862 val |= MSGINT_MODE_MULTIVEC_EN | MSGINT_MODE_ENABLE;
7863 tw32(MSGINT_MODE, val);
7864 }
7865
Linus Torvalds1da177e2005-04-16 15:20:36 -07007866 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7867 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
7868 udelay(40);
7869 }
7870
7871 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
7872 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
7873 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
7874 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
7875 WDMAC_MODE_LNGREAD_ENAB);
7876
Michael Chan85e94ce2005-04-21 17:05:28 -07007877 /* If statement applies to 5705 and 5750 PCI devices only */
7878 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
7879 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) ||
7880 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750) {
Matt Carlson29ea0952009-08-25 10:07:54 +00007881 if ((tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07007882 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
7883 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
7884 /* nothing */
7885 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
7886 !(tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
7887 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)) {
7888 val |= WDMAC_MODE_RX_ACCEL;
7889 }
7890 }
7891
Michael Chand9ab5ad2006-03-20 22:27:35 -08007892 /* Enable host coalescing bug fix */
Matt Carlson321d32a2008-11-21 17:22:19 -08007893 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Matt Carlsonf51f3562008-05-25 23:45:08 -07007894 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad2006-03-20 22:27:35 -08007895
Matt Carlson788a0352009-11-02 14:26:03 +00007896 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
7897 val |= WDMAC_MODE_BURST_ALL_DATA;
7898
Linus Torvalds1da177e2005-04-16 15:20:36 -07007899 tw32_f(WDMAC_MODE, val);
7900 udelay(40);
7901
Matt Carlson9974a352007-10-07 23:27:28 -07007902 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
7903 u16 pcix_cmd;
7904
7905 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7906 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007907 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07007908 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
7909 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007910 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07007911 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
7912 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007913 }
Matt Carlson9974a352007-10-07 23:27:28 -07007914 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7915 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007916 }
7917
7918 tw32_f(RDMAC_MODE, rdmac_mode);
7919 udelay(40);
7920
7921 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
7922 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7923 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07007924
7925 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
7926 tw32(SNDDATAC_MODE,
7927 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
7928 else
7929 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
7930
Linus Torvalds1da177e2005-04-16 15:20:36 -07007931 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
7932 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
7933 tw32(RCVDBDI_MODE, RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ);
7934 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007935 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
7936 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00007937 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
7938 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
7939 val |= SNDBDI_MODE_MULTI_TXQ_EN;
7940 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007941 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
7942
7943 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
7944 err = tg3_load_5701_a0_firmware_fix(tp);
7945 if (err)
7946 return err;
7947 }
7948
Linus Torvalds1da177e2005-04-16 15:20:36 -07007949 if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) {
7950 err = tg3_load_tso_firmware(tp);
7951 if (err)
7952 return err;
7953 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007954
7955 tp->tx_mode = TX_MODE_ENABLE;
7956 tw32_f(MAC_TX_MODE, tp->tx_mode);
7957 udelay(100);
7958
Matt Carlsonbaf8a942009-09-01 13:13:00 +00007959 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS) {
7960 u32 reg = MAC_RSS_INDIR_TBL_0;
7961 u8 *ent = (u8 *)&val;
7962
7963 /* Setup the indirection table */
7964 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
7965 int idx = i % sizeof(val);
7966
7967 ent[idx] = i % (tp->irq_cnt - 1);
7968 if (idx == sizeof(val) - 1) {
7969 tw32(reg, val);
7970 reg += 4;
7971 }
7972 }
7973
7974 /* Setup the "secret" hash key. */
7975 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
7976 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
7977 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
7978 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
7979 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
7980 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
7981 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
7982 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
7983 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
7984 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
7985 }
7986
Linus Torvalds1da177e2005-04-16 15:20:36 -07007987 tp->rx_mode = RX_MODE_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08007988 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Michael Chanaf36e6b2006-03-23 01:28:06 -08007989 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
7990
Matt Carlsonbaf8a942009-09-01 13:13:00 +00007991 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)
7992 tp->rx_mode |= RX_MODE_RSS_ENABLE |
7993 RX_MODE_RSS_ITBL_HASH_BITS_7 |
7994 RX_MODE_RSS_IPV6_HASH_EN |
7995 RX_MODE_RSS_TCP_IPV6_HASH_EN |
7996 RX_MODE_RSS_IPV4_HASH_EN |
7997 RX_MODE_RSS_TCP_IPV4_HASH_EN;
7998
Linus Torvalds1da177e2005-04-16 15:20:36 -07007999 tw32_f(MAC_RX_MODE, tp->rx_mode);
8000 udelay(10);
8001
Linus Torvalds1da177e2005-04-16 15:20:36 -07008002 tw32(MAC_LED_CTRL, tp->led_ctrl);
8003
8004 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Michael Chanc94e3942005-09-27 12:12:42 -07008005 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008006 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8007 udelay(10);
8008 }
8009 tw32_f(MAC_RX_MODE, tp->rx_mode);
8010 udelay(10);
8011
8012 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
8013 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
8014 !(tp->tg3_flags2 & TG3_FLG2_SERDES_PREEMPHASIS)) {
8015 /* Set drive transmission level to 1.2V */
8016 /* only if the signal pre-emphasis bit is not set */
8017 val = tr32(MAC_SERDES_CFG);
8018 val &= 0xfffff000;
8019 val |= 0x880;
8020 tw32(MAC_SERDES_CFG, val);
8021 }
8022 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
8023 tw32(MAC_SERDES_CFG, 0x616000);
8024 }
8025
8026 /* Prevent chip from dropping frames when flow control
8027 * is enabled.
8028 */
8029 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, 2);
8030
8031 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
8032 (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)) {
8033 /* Use hardware link auto-negotiation */
8034 tp->tg3_flags2 |= TG3_FLG2_HW_AUTONEG;
8035 }
8036
Michael Chand4d2c552006-03-20 17:47:20 -08008037 if ((tp->tg3_flags2 & TG3_FLG2_MII_SERDES) &&
8038 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)) {
8039 u32 tmp;
8040
8041 tmp = tr32(SERDES_RX_CTRL);
8042 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
8043 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
8044 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
8045 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8046 }
8047
Matt Carlsondd477002008-05-25 23:45:58 -07008048 if (!(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)) {
8049 if (tp->link_config.phy_is_low_power) {
8050 tp->link_config.phy_is_low_power = 0;
8051 tp->link_config.speed = tp->link_config.orig_speed;
8052 tp->link_config.duplex = tp->link_config.orig_duplex;
8053 tp->link_config.autoneg = tp->link_config.orig_autoneg;
8054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008055
Matt Carlsondd477002008-05-25 23:45:58 -07008056 err = tg3_setup_phy(tp, 0);
8057 if (err)
8058 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008059
Matt Carlsondd477002008-05-25 23:45:58 -07008060 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
Matt Carlson7f97a4b2009-08-25 10:10:03 +00008061 !(tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07008062 u32 tmp;
8063
8064 /* Clear CRC stats. */
8065 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
8066 tg3_writephy(tp, MII_TG3_TEST1,
8067 tmp | MII_TG3_TEST1_CRC_EN);
8068 tg3_readphy(tp, 0x14, &tmp);
8069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008070 }
8071 }
8072
8073 __tg3_set_rx_mode(tp->dev);
8074
8075 /* Initialize receive rules. */
8076 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
8077 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
8078 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
8079 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
8080
Michael Chan4cf78e42005-07-25 12:29:19 -07008081 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Michael Chana4e2b342005-10-26 15:46:52 -07008082 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008083 limit = 8;
8084 else
8085 limit = 16;
8086 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF)
8087 limit -= 4;
8088 switch (limit) {
8089 case 16:
8090 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
8091 case 15:
8092 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
8093 case 14:
8094 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
8095 case 13:
8096 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
8097 case 12:
8098 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
8099 case 11:
8100 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
8101 case 10:
8102 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
8103 case 9:
8104 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
8105 case 8:
8106 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
8107 case 7:
8108 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
8109 case 6:
8110 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
8111 case 5:
8112 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
8113 case 4:
8114 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
8115 case 3:
8116 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
8117 case 2:
8118 case 1:
8119
8120 default:
8121 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07008122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008123
Matt Carlson9ce768e2007-10-11 19:49:11 -07008124 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
8125 /* Write our heartbeat update interval to APE. */
8126 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
8127 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07008128
Linus Torvalds1da177e2005-04-16 15:20:36 -07008129 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
8130
Linus Torvalds1da177e2005-04-16 15:20:36 -07008131 return 0;
8132}
8133
8134/* Called at device open time to get the chip ready for
8135 * packet processing. Invoked with tp->lock held.
8136 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008137static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008138{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008139 tg3_switch_clocks(tp);
8140
8141 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
8142
Matt Carlson2f751b62008-08-04 23:17:34 -07008143 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008144}
8145
8146#define TG3_STAT_ADD32(PSTAT, REG) \
8147do { u32 __val = tr32(REG); \
8148 (PSTAT)->low += __val; \
8149 if ((PSTAT)->low < __val) \
8150 (PSTAT)->high += 1; \
8151} while (0)
8152
8153static void tg3_periodic_fetch_stats(struct tg3 *tp)
8154{
8155 struct tg3_hw_stats *sp = tp->hw_stats;
8156
8157 if (!netif_carrier_ok(tp->dev))
8158 return;
8159
8160 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
8161 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
8162 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
8163 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
8164 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
8165 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
8166 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
8167 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
8168 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
8169 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
8170 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
8171 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
8172 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
8173
8174 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
8175 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
8176 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
8177 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
8178 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
8179 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
8180 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
8181 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
8182 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
8183 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
8184 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
8185 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
8186 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
8187 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07008188
8189 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
8190 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
8191 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008192}
8193
8194static void tg3_timer(unsigned long __opaque)
8195{
8196 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008197
Michael Chanf475f162006-03-27 23:20:14 -08008198 if (tp->irq_sync)
8199 goto restart_timer;
8200
David S. Millerf47c11e2005-06-24 20:18:35 -07008201 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008202
David S. Millerfac9b832005-05-18 22:46:34 -07008203 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)) {
8204 /* All of this garbage is because when using non-tagged
8205 * IRQ status the mailbox/status_block protocol the chip
8206 * uses with the cpu is race prone.
8207 */
Matt Carlson898a56f2009-08-28 14:02:40 +00008208 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -07008209 tw32(GRC_LOCAL_CTRL,
8210 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
8211 } else {
8212 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00008213 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -07008214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008215
David S. Millerfac9b832005-05-18 22:46:34 -07008216 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
8217 tp->tg3_flags2 |= TG3_FLG2_RESTART_TIMER;
David S. Millerf47c11e2005-06-24 20:18:35 -07008218 spin_unlock(&tp->lock);
David S. Millerfac9b832005-05-18 22:46:34 -07008219 schedule_work(&tp->reset_task);
8220 return;
8221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008222 }
8223
Linus Torvalds1da177e2005-04-16 15:20:36 -07008224 /* This part only runs once per second. */
8225 if (!--tp->timer_counter) {
David S. Millerfac9b832005-05-18 22:46:34 -07008226 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
8227 tg3_periodic_fetch_stats(tp);
8228
Linus Torvalds1da177e2005-04-16 15:20:36 -07008229 if (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) {
8230 u32 mac_stat;
8231 int phy_event;
8232
8233 mac_stat = tr32(MAC_STATUS);
8234
8235 phy_event = 0;
8236 if (tp->tg3_flags & TG3_FLAG_USE_MI_INTERRUPT) {
8237 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
8238 phy_event = 1;
8239 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
8240 phy_event = 1;
8241
8242 if (phy_event)
8243 tg3_setup_phy(tp, 0);
8244 } else if (tp->tg3_flags & TG3_FLAG_POLL_SERDES) {
8245 u32 mac_stat = tr32(MAC_STATUS);
8246 int need_setup = 0;
8247
8248 if (netif_carrier_ok(tp->dev) &&
8249 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
8250 need_setup = 1;
8251 }
8252 if (! netif_carrier_ok(tp->dev) &&
8253 (mac_stat & (MAC_STATUS_PCS_SYNCED |
8254 MAC_STATUS_SIGNAL_DET))) {
8255 need_setup = 1;
8256 }
8257 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07008258 if (!tp->serdes_counter) {
8259 tw32_f(MAC_MODE,
8260 (tp->mac_mode &
8261 ~MAC_MODE_PORT_MODE_MASK));
8262 udelay(40);
8263 tw32_f(MAC_MODE, tp->mac_mode);
8264 udelay(40);
8265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008266 tg3_setup_phy(tp, 0);
8267 }
Michael Chan747e8f82005-07-25 12:33:22 -07008268 } else if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)
8269 tg3_serdes_parallel_detect(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008270
8271 tp->timer_counter = tp->timer_multiplier;
8272 }
8273
Michael Chan130b8e42006-09-27 16:00:40 -07008274 /* Heartbeat is only sent once every 2 seconds.
8275 *
8276 * The heartbeat is to tell the ASF firmware that the host
8277 * driver is still alive. In the event that the OS crashes,
8278 * ASF needs to reset the hardware to free up the FIFO space
8279 * that may be filled with rx packets destined for the host.
8280 * If the FIFO is full, ASF will no longer function properly.
8281 *
8282 * Unintended resets have been reported on real time kernels
8283 * where the timer doesn't run on time. Netpoll will also have
8284 * same problem.
8285 *
8286 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
8287 * to check the ring condition when the heartbeat is expiring
8288 * before doing the reset. This will prevent most unintended
8289 * resets.
8290 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008291 if (!--tp->asf_counter) {
Matt Carlsonbc7959b2008-08-15 14:08:55 -07008292 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
8293 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07008294 tg3_wait_for_event_ack(tp);
8295
Michael Chanbbadf502006-04-06 21:46:34 -07008296 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07008297 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07008298 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Michael Chan28fbef72005-10-26 15:48:35 -07008299 /* 5 seconds timeout */
Michael Chanbbadf502006-04-06 21:46:34 -07008300 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX, 5);
Matt Carlson4ba526c2008-08-15 14:10:04 -07008301
8302 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008303 }
8304 tp->asf_counter = tp->asf_multiplier;
8305 }
8306
David S. Millerf47c11e2005-06-24 20:18:35 -07008307 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008308
Michael Chanf475f162006-03-27 23:20:14 -08008309restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07008310 tp->timer.expires = jiffies + tp->timer_offset;
8311 add_timer(&tp->timer);
8312}
8313
Matt Carlson4f125f42009-09-01 12:55:02 +00008314static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -08008315{
David Howells7d12e782006-10-05 14:55:46 +01008316 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008317 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +00008318 char *name;
8319 struct tg3_napi *tnapi = &tp->napi[irq_num];
8320
8321 if (tp->irq_cnt == 1)
8322 name = tp->dev->name;
8323 else {
8324 name = &tnapi->irq_lbl[0];
8325 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
8326 name[IFNAMSIZ-1] = 0;
8327 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08008328
Matt Carlson679563f2009-09-01 12:55:46 +00008329 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI_OR_MSIX) {
Michael Chanfcfa0a32006-03-20 22:28:41 -08008330 fn = tg3_msi;
8331 if (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)
8332 fn = tg3_msi_1shot;
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07008333 flags = IRQF_SAMPLE_RANDOM;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008334 } else {
8335 fn = tg3_interrupt;
8336 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)
8337 fn = tg3_interrupt_tagged;
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07008338 flags = IRQF_SHARED | IRQF_SAMPLE_RANDOM;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008339 }
Matt Carlson4f125f42009-09-01 12:55:02 +00008340
8341 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08008342}
8343
Michael Chan79381092005-04-21 17:13:59 -07008344static int tg3_test_interrupt(struct tg3 *tp)
8345{
Matt Carlson09943a12009-08-28 14:01:57 +00008346 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -07008347 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -07008348 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008349 u32 val;
Michael Chan79381092005-04-21 17:13:59 -07008350
Michael Chand4bc3922005-05-29 14:59:20 -07008351 if (!netif_running(dev))
8352 return -ENODEV;
8353
Michael Chan79381092005-04-21 17:13:59 -07008354 tg3_disable_ints(tp);
8355
Matt Carlson4f125f42009-09-01 12:55:02 +00008356 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07008357
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008358 /*
8359 * Turn off MSI one shot mode. Otherwise this test has no
8360 * observable way to know whether the interrupt was delivered.
8361 */
8362 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 &&
8363 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
8364 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
8365 tw32(MSGINT_MODE, val);
8366 }
8367
Matt Carlson4f125f42009-09-01 12:55:02 +00008368 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Matt Carlson09943a12009-08-28 14:01:57 +00008369 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07008370 if (err)
8371 return err;
8372
Matt Carlson898a56f2009-08-28 14:02:40 +00008373 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -07008374 tg3_enable_ints(tp);
8375
8376 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00008377 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -07008378
8379 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -07008380 u32 int_mbox, misc_host_ctrl;
8381
Matt Carlson898a56f2009-08-28 14:02:40 +00008382 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -07008383 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
8384
8385 if ((int_mbox != 0) ||
8386 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
8387 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -07008388 break;
Michael Chanb16250e2006-09-27 16:10:14 -07008389 }
8390
Michael Chan79381092005-04-21 17:13:59 -07008391 msleep(10);
8392 }
8393
8394 tg3_disable_ints(tp);
8395
Matt Carlson4f125f42009-09-01 12:55:02 +00008396 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008397
Matt Carlson4f125f42009-09-01 12:55:02 +00008398 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07008399
8400 if (err)
8401 return err;
8402
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008403 if (intr_ok) {
8404 /* Reenable MSI one shot mode. */
8405 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 &&
8406 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
8407 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
8408 tw32(MSGINT_MODE, val);
8409 }
Michael Chan79381092005-04-21 17:13:59 -07008410 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008411 }
Michael Chan79381092005-04-21 17:13:59 -07008412
8413 return -EIO;
8414}
8415
8416/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
8417 * successfully restored
8418 */
8419static int tg3_test_msi(struct tg3 *tp)
8420{
Michael Chan79381092005-04-21 17:13:59 -07008421 int err;
8422 u16 pci_cmd;
8423
8424 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSI))
8425 return 0;
8426
8427 /* Turn off SERR reporting in case MSI terminates with Master
8428 * Abort.
8429 */
8430 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
8431 pci_write_config_word(tp->pdev, PCI_COMMAND,
8432 pci_cmd & ~PCI_COMMAND_SERR);
8433
8434 err = tg3_test_interrupt(tp);
8435
8436 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
8437
8438 if (!err)
8439 return 0;
8440
8441 /* other failures */
8442 if (err != -EIO)
8443 return err;
8444
8445 /* MSI test failed, go back to INTx mode */
8446 printk(KERN_WARNING PFX "%s: No interrupt was generated using MSI, "
8447 "switching to INTx mode. Please report this failure to "
8448 "the PCI maintainer and include system chipset information.\n",
8449 tp->dev->name);
8450
Matt Carlson4f125f42009-09-01 12:55:02 +00008451 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +00008452
Michael Chan79381092005-04-21 17:13:59 -07008453 pci_disable_msi(tp->pdev);
8454
8455 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI;
8456
Matt Carlson4f125f42009-09-01 12:55:02 +00008457 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07008458 if (err)
8459 return err;
8460
8461 /* Need to reset the chip because the MSI cycle may have terminated
8462 * with Master Abort.
8463 */
David S. Millerf47c11e2005-06-24 20:18:35 -07008464 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07008465
Michael Chan944d9802005-05-29 14:57:48 -07008466 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008467 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07008468
David S. Millerf47c11e2005-06-24 20:18:35 -07008469 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07008470
8471 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +00008472 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -07008473
8474 return err;
8475}
8476
Matt Carlson9e9fd122009-01-19 16:57:45 -08008477static int tg3_request_firmware(struct tg3 *tp)
8478{
8479 const __be32 *fw_data;
8480
8481 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
8482 printk(KERN_ERR "%s: Failed to load firmware \"%s\"\n",
8483 tp->dev->name, tp->fw_needed);
8484 return -ENOENT;
8485 }
8486
8487 fw_data = (void *)tp->fw->data;
8488
8489 /* Firmware blob starts with version numbers, followed by
8490 * start address and _full_ length including BSS sections
8491 * (which must be longer than the actual data, of course
8492 */
8493
8494 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
8495 if (tp->fw_len < (tp->fw->size - 12)) {
8496 printk(KERN_ERR "%s: bogus length %d in \"%s\"\n",
8497 tp->dev->name, tp->fw_len, tp->fw_needed);
8498 release_firmware(tp->fw);
8499 tp->fw = NULL;
8500 return -EINVAL;
8501 }
8502
8503 /* We no longer need firmware; we have it. */
8504 tp->fw_needed = NULL;
8505 return 0;
8506}
8507
Matt Carlson679563f2009-09-01 12:55:46 +00008508static bool tg3_enable_msix(struct tg3 *tp)
8509{
8510 int i, rc, cpus = num_online_cpus();
8511 struct msix_entry msix_ent[tp->irq_max];
8512
8513 if (cpus == 1)
8514 /* Just fallback to the simpler MSI mode. */
8515 return false;
8516
8517 /*
8518 * We want as many rx rings enabled as there are cpus.
8519 * The first MSIX vector only deals with link interrupts, etc,
8520 * so we add one to the number of vectors we are requesting.
8521 */
8522 tp->irq_cnt = min_t(unsigned, cpus + 1, tp->irq_max);
8523
8524 for (i = 0; i < tp->irq_max; i++) {
8525 msix_ent[i].entry = i;
8526 msix_ent[i].vector = 0;
8527 }
8528
8529 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
8530 if (rc != 0) {
8531 if (rc < TG3_RSS_MIN_NUM_MSIX_VECS)
8532 return false;
8533 if (pci_enable_msix(tp->pdev, msix_ent, rc))
8534 return false;
8535 printk(KERN_NOTICE
8536 "%s: Requested %d MSI-X vectors, received %d\n",
8537 tp->dev->name, tp->irq_cnt, rc);
8538 tp->irq_cnt = rc;
8539 }
8540
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008541 tp->tg3_flags3 |= TG3_FLG3_ENABLE_RSS;
8542
Matt Carlson679563f2009-09-01 12:55:46 +00008543 for (i = 0; i < tp->irq_max; i++)
8544 tp->napi[i].irq_vec = msix_ent[i].vector;
8545
Matt Carlsonfe5f5782009-09-01 13:09:39 +00008546 tp->dev->real_num_tx_queues = tp->irq_cnt - 1;
8547
Matt Carlson679563f2009-09-01 12:55:46 +00008548 return true;
8549}
8550
Matt Carlson07b01732009-08-28 14:01:15 +00008551static void tg3_ints_init(struct tg3 *tp)
8552{
Matt Carlson679563f2009-09-01 12:55:46 +00008553 if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSI_OR_MSIX) &&
8554 !(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +00008555 /* All MSI supporting chips should support tagged
8556 * status. Assert that this is the case.
8557 */
Matt Carlson679563f2009-09-01 12:55:46 +00008558 printk(KERN_WARNING PFX "%s: MSI without TAGGED? "
8559 "Not using MSI.\n", tp->dev->name);
8560 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +00008561 }
Matt Carlson4f125f42009-09-01 12:55:02 +00008562
Matt Carlson679563f2009-09-01 12:55:46 +00008563 if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX) && tg3_enable_msix(tp))
8564 tp->tg3_flags2 |= TG3_FLG2_USING_MSIX;
8565 else if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSI) &&
8566 pci_enable_msi(tp->pdev) == 0)
8567 tp->tg3_flags2 |= TG3_FLG2_USING_MSI;
8568
8569 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI_OR_MSIX) {
8570 u32 msi_mode = tr32(MSGINT_MODE);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008571 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
8572 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson679563f2009-09-01 12:55:46 +00008573 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
8574 }
8575defcfg:
8576 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSIX)) {
8577 tp->irq_cnt = 1;
8578 tp->napi[0].irq_vec = tp->pdev->irq;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00008579 tp->dev->real_num_tx_queues = 1;
Matt Carlson679563f2009-09-01 12:55:46 +00008580 }
Matt Carlson07b01732009-08-28 14:01:15 +00008581}
8582
8583static void tg3_ints_fini(struct tg3 *tp)
8584{
Matt Carlson679563f2009-09-01 12:55:46 +00008585 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
8586 pci_disable_msix(tp->pdev);
8587 else if (tp->tg3_flags2 & TG3_FLG2_USING_MSI)
8588 pci_disable_msi(tp->pdev);
8589 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI_OR_MSIX;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008590 tp->tg3_flags3 &= ~TG3_FLG3_ENABLE_RSS;
Matt Carlson07b01732009-08-28 14:01:15 +00008591}
8592
Linus Torvalds1da177e2005-04-16 15:20:36 -07008593static int tg3_open(struct net_device *dev)
8594{
8595 struct tg3 *tp = netdev_priv(dev);
Matt Carlson4f125f42009-09-01 12:55:02 +00008596 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008597
Matt Carlson9e9fd122009-01-19 16:57:45 -08008598 if (tp->fw_needed) {
8599 err = tg3_request_firmware(tp);
8600 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
8601 if (err)
8602 return err;
8603 } else if (err) {
8604 printk(KERN_WARNING "%s: TSO capability disabled.\n",
8605 tp->dev->name);
8606 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
8607 } else if (!(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
8608 printk(KERN_NOTICE "%s: TSO capability restored.\n",
8609 tp->dev->name);
8610 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
8611 }
8612 }
8613
Michael Chanc49a1562006-12-17 17:07:29 -08008614 netif_carrier_off(tp->dev);
8615
Michael Chanbc1c7562006-03-20 17:48:03 -08008616 err = tg3_set_power_state(tp, PCI_D0);
Matt Carlson2f751b62008-08-04 23:17:34 -07008617 if (err)
Michael Chanbc1c7562006-03-20 17:48:03 -08008618 return err;
Matt Carlson2f751b62008-08-04 23:17:34 -07008619
8620 tg3_full_lock(tp, 0);
Michael Chanbc1c7562006-03-20 17:48:03 -08008621
Linus Torvalds1da177e2005-04-16 15:20:36 -07008622 tg3_disable_ints(tp);
8623 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
8624
David S. Millerf47c11e2005-06-24 20:18:35 -07008625 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008626
Matt Carlson679563f2009-09-01 12:55:46 +00008627 /*
8628 * Setup interrupts first so we know how
8629 * many NAPI resources to allocate
8630 */
8631 tg3_ints_init(tp);
8632
Linus Torvalds1da177e2005-04-16 15:20:36 -07008633 /* The placement of this call is tied
8634 * to the setup and use of Host TX descriptors.
8635 */
8636 err = tg3_alloc_consistent(tp);
8637 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00008638 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008639
Matt Carlsonfed97812009-09-01 13:10:19 +00008640 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07008641
Matt Carlson4f125f42009-09-01 12:55:02 +00008642 for (i = 0; i < tp->irq_cnt; i++) {
8643 struct tg3_napi *tnapi = &tp->napi[i];
8644 err = tg3_request_irq(tp, i);
8645 if (err) {
8646 for (i--; i >= 0; i--)
8647 free_irq(tnapi->irq_vec, tnapi);
8648 break;
8649 }
8650 }
Matt Carlson07b01732009-08-28 14:01:15 +00008651
8652 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00008653 goto err_out2;
Matt Carlson07b01732009-08-28 14:01:15 +00008654
David S. Millerf47c11e2005-06-24 20:18:35 -07008655 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008656
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008657 err = tg3_init_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008658 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -07008659 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008660 tg3_free_rings(tp);
8661 } else {
David S. Millerfac9b832005-05-18 22:46:34 -07008662 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)
8663 tp->timer_offset = HZ;
8664 else
8665 tp->timer_offset = HZ / 10;
8666
8667 BUG_ON(tp->timer_offset > HZ);
8668 tp->timer_counter = tp->timer_multiplier =
8669 (HZ / tp->timer_offset);
8670 tp->asf_counter = tp->asf_multiplier =
Michael Chan28fbef72005-10-26 15:48:35 -07008671 ((HZ / tp->timer_offset) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008672
8673 init_timer(&tp->timer);
8674 tp->timer.expires = jiffies + tp->timer_offset;
8675 tp->timer.data = (unsigned long) tp;
8676 tp->timer.function = tg3_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008677 }
8678
David S. Millerf47c11e2005-06-24 20:18:35 -07008679 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008680
Matt Carlson07b01732009-08-28 14:01:15 +00008681 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00008682 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008683
Michael Chan79381092005-04-21 17:13:59 -07008684 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
8685 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -07008686
Michael Chan79381092005-04-21 17:13:59 -07008687 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -07008688 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -07008689 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -07008690 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07008691 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07008692
Matt Carlson679563f2009-09-01 12:55:46 +00008693 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -07008694 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08008695
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008696 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
8697 (tp->tg3_flags2 & TG3_FLG2_USING_MSI) &&
8698 (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)) {
8699 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -08008700
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008701 tw32(PCIE_TRANSACTION_CFG,
8702 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -08008703 }
Michael Chan79381092005-04-21 17:13:59 -07008704 }
8705
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07008706 tg3_phy_start(tp);
8707
David S. Millerf47c11e2005-06-24 20:18:35 -07008708 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008709
Michael Chan79381092005-04-21 17:13:59 -07008710 add_timer(&tp->timer);
8711 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008712 tg3_enable_ints(tp);
8713
David S. Millerf47c11e2005-06-24 20:18:35 -07008714 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008715
Matt Carlsonfe5f5782009-09-01 13:09:39 +00008716 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008717
8718 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +00008719
Matt Carlson679563f2009-09-01 12:55:46 +00008720err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +00008721 for (i = tp->irq_cnt - 1; i >= 0; i--) {
8722 struct tg3_napi *tnapi = &tp->napi[i];
8723 free_irq(tnapi->irq_vec, tnapi);
8724 }
Matt Carlson07b01732009-08-28 14:01:15 +00008725
Matt Carlson679563f2009-09-01 12:55:46 +00008726err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +00008727 tg3_napi_disable(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00008728 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +00008729
8730err_out1:
8731 tg3_ints_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00008732 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008733}
8734
8735#if 0
8736/*static*/ void tg3_dump_state(struct tg3 *tp)
8737{
8738 u32 val32, val32_2, val32_3, val32_4, val32_5;
8739 u16 val16;
8740 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00008741 struct tg3_hw_status *sblk = tp->napi[0]->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008742
8743 pci_read_config_word(tp->pdev, PCI_STATUS, &val16);
8744 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE, &val32);
8745 printk("DEBUG: PCI status [%04x] TG3PCI state[%08x]\n",
8746 val16, val32);
8747
8748 /* MAC block */
8749 printk("DEBUG: MAC_MODE[%08x] MAC_STATUS[%08x]\n",
8750 tr32(MAC_MODE), tr32(MAC_STATUS));
8751 printk(" MAC_EVENT[%08x] MAC_LED_CTRL[%08x]\n",
8752 tr32(MAC_EVENT), tr32(MAC_LED_CTRL));
8753 printk("DEBUG: MAC_TX_MODE[%08x] MAC_TX_STATUS[%08x]\n",
8754 tr32(MAC_TX_MODE), tr32(MAC_TX_STATUS));
8755 printk(" MAC_RX_MODE[%08x] MAC_RX_STATUS[%08x]\n",
8756 tr32(MAC_RX_MODE), tr32(MAC_RX_STATUS));
8757
8758 /* Send data initiator control block */
8759 printk("DEBUG: SNDDATAI_MODE[%08x] SNDDATAI_STATUS[%08x]\n",
8760 tr32(SNDDATAI_MODE), tr32(SNDDATAI_STATUS));
8761 printk(" SNDDATAI_STATSCTRL[%08x]\n",
8762 tr32(SNDDATAI_STATSCTRL));
8763
8764 /* Send data completion control block */
8765 printk("DEBUG: SNDDATAC_MODE[%08x]\n", tr32(SNDDATAC_MODE));
8766
8767 /* Send BD ring selector block */
8768 printk("DEBUG: SNDBDS_MODE[%08x] SNDBDS_STATUS[%08x]\n",
8769 tr32(SNDBDS_MODE), tr32(SNDBDS_STATUS));
8770
8771 /* Send BD initiator control block */
8772 printk("DEBUG: SNDBDI_MODE[%08x] SNDBDI_STATUS[%08x]\n",
8773 tr32(SNDBDI_MODE), tr32(SNDBDI_STATUS));
8774
8775 /* Send BD completion control block */
8776 printk("DEBUG: SNDBDC_MODE[%08x]\n", tr32(SNDBDC_MODE));
8777
8778 /* Receive list placement control block */
8779 printk("DEBUG: RCVLPC_MODE[%08x] RCVLPC_STATUS[%08x]\n",
8780 tr32(RCVLPC_MODE), tr32(RCVLPC_STATUS));
8781 printk(" RCVLPC_STATSCTRL[%08x]\n",
8782 tr32(RCVLPC_STATSCTRL));
8783
8784 /* Receive data and receive BD initiator control block */
8785 printk("DEBUG: RCVDBDI_MODE[%08x] RCVDBDI_STATUS[%08x]\n",
8786 tr32(RCVDBDI_MODE), tr32(RCVDBDI_STATUS));
8787
8788 /* Receive data completion control block */
8789 printk("DEBUG: RCVDCC_MODE[%08x]\n",
8790 tr32(RCVDCC_MODE));
8791
8792 /* Receive BD initiator control block */
8793 printk("DEBUG: RCVBDI_MODE[%08x] RCVBDI_STATUS[%08x]\n",
8794 tr32(RCVBDI_MODE), tr32(RCVBDI_STATUS));
8795
8796 /* Receive BD completion control block */
8797 printk("DEBUG: RCVCC_MODE[%08x] RCVCC_STATUS[%08x]\n",
8798 tr32(RCVCC_MODE), tr32(RCVCC_STATUS));
8799
8800 /* Receive list selector control block */
8801 printk("DEBUG: RCVLSC_MODE[%08x] RCVLSC_STATUS[%08x]\n",
8802 tr32(RCVLSC_MODE), tr32(RCVLSC_STATUS));
8803
8804 /* Mbuf cluster free block */
8805 printk("DEBUG: MBFREE_MODE[%08x] MBFREE_STATUS[%08x]\n",
8806 tr32(MBFREE_MODE), tr32(MBFREE_STATUS));
8807
8808 /* Host coalescing control block */
8809 printk("DEBUG: HOSTCC_MODE[%08x] HOSTCC_STATUS[%08x]\n",
8810 tr32(HOSTCC_MODE), tr32(HOSTCC_STATUS));
8811 printk("DEBUG: HOSTCC_STATS_BLK_HOST_ADDR[%08x%08x]\n",
8812 tr32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH),
8813 tr32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW));
8814 printk("DEBUG: HOSTCC_STATUS_BLK_HOST_ADDR[%08x%08x]\n",
8815 tr32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH),
8816 tr32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW));
8817 printk("DEBUG: HOSTCC_STATS_BLK_NIC_ADDR[%08x]\n",
8818 tr32(HOSTCC_STATS_BLK_NIC_ADDR));
8819 printk("DEBUG: HOSTCC_STATUS_BLK_NIC_ADDR[%08x]\n",
8820 tr32(HOSTCC_STATUS_BLK_NIC_ADDR));
8821
8822 /* Memory arbiter control block */
8823 printk("DEBUG: MEMARB_MODE[%08x] MEMARB_STATUS[%08x]\n",
8824 tr32(MEMARB_MODE), tr32(MEMARB_STATUS));
8825
8826 /* Buffer manager control block */
8827 printk("DEBUG: BUFMGR_MODE[%08x] BUFMGR_STATUS[%08x]\n",
8828 tr32(BUFMGR_MODE), tr32(BUFMGR_STATUS));
8829 printk("DEBUG: BUFMGR_MB_POOL_ADDR[%08x] BUFMGR_MB_POOL_SIZE[%08x]\n",
8830 tr32(BUFMGR_MB_POOL_ADDR), tr32(BUFMGR_MB_POOL_SIZE));
8831 printk("DEBUG: BUFMGR_DMA_DESC_POOL_ADDR[%08x] "
8832 "BUFMGR_DMA_DESC_POOL_SIZE[%08x]\n",
8833 tr32(BUFMGR_DMA_DESC_POOL_ADDR),
8834 tr32(BUFMGR_DMA_DESC_POOL_SIZE));
8835
8836 /* Read DMA control block */
8837 printk("DEBUG: RDMAC_MODE[%08x] RDMAC_STATUS[%08x]\n",
8838 tr32(RDMAC_MODE), tr32(RDMAC_STATUS));
8839
8840 /* Write DMA control block */
8841 printk("DEBUG: WDMAC_MODE[%08x] WDMAC_STATUS[%08x]\n",
8842 tr32(WDMAC_MODE), tr32(WDMAC_STATUS));
8843
8844 /* DMA completion block */
8845 printk("DEBUG: DMAC_MODE[%08x]\n",
8846 tr32(DMAC_MODE));
8847
8848 /* GRC block */
8849 printk("DEBUG: GRC_MODE[%08x] GRC_MISC_CFG[%08x]\n",
8850 tr32(GRC_MODE), tr32(GRC_MISC_CFG));
8851 printk("DEBUG: GRC_LOCAL_CTRL[%08x]\n",
8852 tr32(GRC_LOCAL_CTRL));
8853
8854 /* TG3_BDINFOs */
8855 printk("DEBUG: RCVDBDI_JUMBO_BD[%08x%08x:%08x:%08x]\n",
8856 tr32(RCVDBDI_JUMBO_BD + 0x0),
8857 tr32(RCVDBDI_JUMBO_BD + 0x4),
8858 tr32(RCVDBDI_JUMBO_BD + 0x8),
8859 tr32(RCVDBDI_JUMBO_BD + 0xc));
8860 printk("DEBUG: RCVDBDI_STD_BD[%08x%08x:%08x:%08x]\n",
8861 tr32(RCVDBDI_STD_BD + 0x0),
8862 tr32(RCVDBDI_STD_BD + 0x4),
8863 tr32(RCVDBDI_STD_BD + 0x8),
8864 tr32(RCVDBDI_STD_BD + 0xc));
8865 printk("DEBUG: RCVDBDI_MINI_BD[%08x%08x:%08x:%08x]\n",
8866 tr32(RCVDBDI_MINI_BD + 0x0),
8867 tr32(RCVDBDI_MINI_BD + 0x4),
8868 tr32(RCVDBDI_MINI_BD + 0x8),
8869 tr32(RCVDBDI_MINI_BD + 0xc));
8870
8871 tg3_read_mem(tp, NIC_SRAM_SEND_RCB + 0x0, &val32);
8872 tg3_read_mem(tp, NIC_SRAM_SEND_RCB + 0x4, &val32_2);
8873 tg3_read_mem(tp, NIC_SRAM_SEND_RCB + 0x8, &val32_3);
8874 tg3_read_mem(tp, NIC_SRAM_SEND_RCB + 0xc, &val32_4);
8875 printk("DEBUG: SRAM_SEND_RCB_0[%08x%08x:%08x:%08x]\n",
8876 val32, val32_2, val32_3, val32_4);
8877
8878 tg3_read_mem(tp, NIC_SRAM_RCV_RET_RCB + 0x0, &val32);
8879 tg3_read_mem(tp, NIC_SRAM_RCV_RET_RCB + 0x4, &val32_2);
8880 tg3_read_mem(tp, NIC_SRAM_RCV_RET_RCB + 0x8, &val32_3);
8881 tg3_read_mem(tp, NIC_SRAM_RCV_RET_RCB + 0xc, &val32_4);
8882 printk("DEBUG: SRAM_RCV_RET_RCB_0[%08x%08x:%08x:%08x]\n",
8883 val32, val32_2, val32_3, val32_4);
8884
8885 tg3_read_mem(tp, NIC_SRAM_STATUS_BLK + 0x0, &val32);
8886 tg3_read_mem(tp, NIC_SRAM_STATUS_BLK + 0x4, &val32_2);
8887 tg3_read_mem(tp, NIC_SRAM_STATUS_BLK + 0x8, &val32_3);
8888 tg3_read_mem(tp, NIC_SRAM_STATUS_BLK + 0xc, &val32_4);
8889 tg3_read_mem(tp, NIC_SRAM_STATUS_BLK + 0x10, &val32_5);
8890 printk("DEBUG: SRAM_STATUS_BLK[%08x:%08x:%08x:%08x:%08x]\n",
8891 val32, val32_2, val32_3, val32_4, val32_5);
8892
8893 /* SW status block */
Matt Carlson898a56f2009-08-28 14:02:40 +00008894 printk(KERN_DEBUG
8895 "Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
8896 sblk->status,
8897 sblk->status_tag,
8898 sblk->rx_jumbo_consumer,
8899 sblk->rx_consumer,
8900 sblk->rx_mini_consumer,
8901 sblk->idx[0].rx_producer,
8902 sblk->idx[0].tx_consumer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008903
8904 /* SW statistics block */
8905 printk("DEBUG: Host statistics block [%08x:%08x:%08x:%08x]\n",
8906 ((u32 *)tp->hw_stats)[0],
8907 ((u32 *)tp->hw_stats)[1],
8908 ((u32 *)tp->hw_stats)[2],
8909 ((u32 *)tp->hw_stats)[3]);
8910
8911 /* Mailboxes */
8912 printk("DEBUG: SNDHOST_PROD[%08x%08x] SNDNIC_PROD[%08x%08x]\n",
Michael Chan09ee9292005-08-09 20:17:00 -07008913 tr32_mailbox(MAILBOX_SNDHOST_PROD_IDX_0 + 0x0),
8914 tr32_mailbox(MAILBOX_SNDHOST_PROD_IDX_0 + 0x4),
8915 tr32_mailbox(MAILBOX_SNDNIC_PROD_IDX_0 + 0x0),
8916 tr32_mailbox(MAILBOX_SNDNIC_PROD_IDX_0 + 0x4));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008917
8918 /* NIC side send descriptors. */
8919 for (i = 0; i < 6; i++) {
8920 unsigned long txd;
8921
8922 txd = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_TX_BUFFER_DESC
8923 + (i * sizeof(struct tg3_tx_buffer_desc));
8924 printk("DEBUG: NIC TXD(%d)[%08x:%08x:%08x:%08x]\n",
8925 i,
8926 readl(txd + 0x0), readl(txd + 0x4),
8927 readl(txd + 0x8), readl(txd + 0xc));
8928 }
8929
8930 /* NIC side RX descriptors. */
8931 for (i = 0; i < 6; i++) {
8932 unsigned long rxd;
8933
8934 rxd = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_RX_BUFFER_DESC
8935 + (i * sizeof(struct tg3_rx_buffer_desc));
8936 printk("DEBUG: NIC RXD_STD(%d)[0][%08x:%08x:%08x:%08x]\n",
8937 i,
8938 readl(rxd + 0x0), readl(rxd + 0x4),
8939 readl(rxd + 0x8), readl(rxd + 0xc));
8940 rxd += (4 * sizeof(u32));
8941 printk("DEBUG: NIC RXD_STD(%d)[1][%08x:%08x:%08x:%08x]\n",
8942 i,
8943 readl(rxd + 0x0), readl(rxd + 0x4),
8944 readl(rxd + 0x8), readl(rxd + 0xc));
8945 }
8946
8947 for (i = 0; i < 6; i++) {
8948 unsigned long rxd;
8949
8950 rxd = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_RX_JUMBO_BUFFER_DESC
8951 + (i * sizeof(struct tg3_rx_buffer_desc));
8952 printk("DEBUG: NIC RXD_JUMBO(%d)[0][%08x:%08x:%08x:%08x]\n",
8953 i,
8954 readl(rxd + 0x0), readl(rxd + 0x4),
8955 readl(rxd + 0x8), readl(rxd + 0xc));
8956 rxd += (4 * sizeof(u32));
8957 printk("DEBUG: NIC RXD_JUMBO(%d)[1][%08x:%08x:%08x:%08x]\n",
8958 i,
8959 readl(rxd + 0x0), readl(rxd + 0x4),
8960 readl(rxd + 0x8), readl(rxd + 0xc));
8961 }
8962}
8963#endif
8964
8965static struct net_device_stats *tg3_get_stats(struct net_device *);
8966static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *);
8967
8968static int tg3_close(struct net_device *dev)
8969{
Matt Carlson4f125f42009-09-01 12:55:02 +00008970 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008971 struct tg3 *tp = netdev_priv(dev);
8972
Matt Carlsonfed97812009-09-01 13:10:19 +00008973 tg3_napi_disable(tp);
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07008974 cancel_work_sync(&tp->reset_task);
Michael Chan7faa0062006-02-02 17:29:28 -08008975
Matt Carlsonfe5f5782009-09-01 13:09:39 +00008976 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008977
8978 del_timer_sync(&tp->timer);
8979
Matt Carlson24bb4fb2009-10-05 17:55:29 +00008980 tg3_phy_stop(tp);
8981
David S. Millerf47c11e2005-06-24 20:18:35 -07008982 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008983#if 0
8984 tg3_dump_state(tp);
8985#endif
8986
8987 tg3_disable_ints(tp);
8988
Michael Chan944d9802005-05-29 14:57:48 -07008989 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008990 tg3_free_rings(tp);
Michael Chan5cf64b8a2007-05-05 12:11:21 -07008991 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008992
David S. Millerf47c11e2005-06-24 20:18:35 -07008993 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008994
Matt Carlson4f125f42009-09-01 12:55:02 +00008995 for (i = tp->irq_cnt - 1; i >= 0; i--) {
8996 struct tg3_napi *tnapi = &tp->napi[i];
8997 free_irq(tnapi->irq_vec, tnapi);
8998 }
Matt Carlson07b01732009-08-28 14:01:15 +00008999
9000 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009001
9002 memcpy(&tp->net_stats_prev, tg3_get_stats(tp->dev),
9003 sizeof(tp->net_stats_prev));
9004 memcpy(&tp->estats_prev, tg3_get_estats(tp),
9005 sizeof(tp->estats_prev));
9006
9007 tg3_free_consistent(tp);
9008
Michael Chanbc1c7562006-03-20 17:48:03 -08009009 tg3_set_power_state(tp, PCI_D3hot);
9010
9011 netif_carrier_off(tp->dev);
9012
Linus Torvalds1da177e2005-04-16 15:20:36 -07009013 return 0;
9014}
9015
9016static inline unsigned long get_stat64(tg3_stat64_t *val)
9017{
9018 unsigned long ret;
9019
9020#if (BITS_PER_LONG == 32)
9021 ret = val->low;
9022#else
9023 ret = ((u64)val->high << 32) | ((u64)val->low);
9024#endif
9025 return ret;
9026}
9027
Stefan Buehler816f8b82008-08-15 14:10:54 -07009028static inline u64 get_estat64(tg3_stat64_t *val)
9029{
9030 return ((u64)val->high << 32) | ((u64)val->low);
9031}
9032
Linus Torvalds1da177e2005-04-16 15:20:36 -07009033static unsigned long calc_crc_errors(struct tg3 *tp)
9034{
9035 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9036
9037 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
9038 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
9039 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009040 u32 val;
9041
David S. Millerf47c11e2005-06-24 20:18:35 -07009042 spin_lock_bh(&tp->lock);
Michael Chan569a5df2007-02-13 12:18:15 -08009043 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
9044 tg3_writephy(tp, MII_TG3_TEST1,
9045 val | MII_TG3_TEST1_CRC_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009046 tg3_readphy(tp, 0x14, &val);
9047 } else
9048 val = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07009049 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009050
9051 tp->phy_crc_errors += val;
9052
9053 return tp->phy_crc_errors;
9054 }
9055
9056 return get_stat64(&hw_stats->rx_fcs_errors);
9057}
9058
9059#define ESTAT_ADD(member) \
9060 estats->member = old_estats->member + \
Stefan Buehler816f8b82008-08-15 14:10:54 -07009061 get_estat64(&hw_stats->member)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009062
9063static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *tp)
9064{
9065 struct tg3_ethtool_stats *estats = &tp->estats;
9066 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
9067 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9068
9069 if (!hw_stats)
9070 return old_estats;
9071
9072 ESTAT_ADD(rx_octets);
9073 ESTAT_ADD(rx_fragments);
9074 ESTAT_ADD(rx_ucast_packets);
9075 ESTAT_ADD(rx_mcast_packets);
9076 ESTAT_ADD(rx_bcast_packets);
9077 ESTAT_ADD(rx_fcs_errors);
9078 ESTAT_ADD(rx_align_errors);
9079 ESTAT_ADD(rx_xon_pause_rcvd);
9080 ESTAT_ADD(rx_xoff_pause_rcvd);
9081 ESTAT_ADD(rx_mac_ctrl_rcvd);
9082 ESTAT_ADD(rx_xoff_entered);
9083 ESTAT_ADD(rx_frame_too_long_errors);
9084 ESTAT_ADD(rx_jabbers);
9085 ESTAT_ADD(rx_undersize_packets);
9086 ESTAT_ADD(rx_in_length_errors);
9087 ESTAT_ADD(rx_out_length_errors);
9088 ESTAT_ADD(rx_64_or_less_octet_packets);
9089 ESTAT_ADD(rx_65_to_127_octet_packets);
9090 ESTAT_ADD(rx_128_to_255_octet_packets);
9091 ESTAT_ADD(rx_256_to_511_octet_packets);
9092 ESTAT_ADD(rx_512_to_1023_octet_packets);
9093 ESTAT_ADD(rx_1024_to_1522_octet_packets);
9094 ESTAT_ADD(rx_1523_to_2047_octet_packets);
9095 ESTAT_ADD(rx_2048_to_4095_octet_packets);
9096 ESTAT_ADD(rx_4096_to_8191_octet_packets);
9097 ESTAT_ADD(rx_8192_to_9022_octet_packets);
9098
9099 ESTAT_ADD(tx_octets);
9100 ESTAT_ADD(tx_collisions);
9101 ESTAT_ADD(tx_xon_sent);
9102 ESTAT_ADD(tx_xoff_sent);
9103 ESTAT_ADD(tx_flow_control);
9104 ESTAT_ADD(tx_mac_errors);
9105 ESTAT_ADD(tx_single_collisions);
9106 ESTAT_ADD(tx_mult_collisions);
9107 ESTAT_ADD(tx_deferred);
9108 ESTAT_ADD(tx_excessive_collisions);
9109 ESTAT_ADD(tx_late_collisions);
9110 ESTAT_ADD(tx_collide_2times);
9111 ESTAT_ADD(tx_collide_3times);
9112 ESTAT_ADD(tx_collide_4times);
9113 ESTAT_ADD(tx_collide_5times);
9114 ESTAT_ADD(tx_collide_6times);
9115 ESTAT_ADD(tx_collide_7times);
9116 ESTAT_ADD(tx_collide_8times);
9117 ESTAT_ADD(tx_collide_9times);
9118 ESTAT_ADD(tx_collide_10times);
9119 ESTAT_ADD(tx_collide_11times);
9120 ESTAT_ADD(tx_collide_12times);
9121 ESTAT_ADD(tx_collide_13times);
9122 ESTAT_ADD(tx_collide_14times);
9123 ESTAT_ADD(tx_collide_15times);
9124 ESTAT_ADD(tx_ucast_packets);
9125 ESTAT_ADD(tx_mcast_packets);
9126 ESTAT_ADD(tx_bcast_packets);
9127 ESTAT_ADD(tx_carrier_sense_errors);
9128 ESTAT_ADD(tx_discards);
9129 ESTAT_ADD(tx_errors);
9130
9131 ESTAT_ADD(dma_writeq_full);
9132 ESTAT_ADD(dma_write_prioq_full);
9133 ESTAT_ADD(rxbds_empty);
9134 ESTAT_ADD(rx_discards);
9135 ESTAT_ADD(rx_errors);
9136 ESTAT_ADD(rx_threshold_hit);
9137
9138 ESTAT_ADD(dma_readq_full);
9139 ESTAT_ADD(dma_read_prioq_full);
9140 ESTAT_ADD(tx_comp_queue_full);
9141
9142 ESTAT_ADD(ring_set_send_prod_index);
9143 ESTAT_ADD(ring_status_update);
9144 ESTAT_ADD(nic_irqs);
9145 ESTAT_ADD(nic_avoided_irqs);
9146 ESTAT_ADD(nic_tx_threshold_hit);
9147
9148 return estats;
9149}
9150
9151static struct net_device_stats *tg3_get_stats(struct net_device *dev)
9152{
9153 struct tg3 *tp = netdev_priv(dev);
9154 struct net_device_stats *stats = &tp->net_stats;
9155 struct net_device_stats *old_stats = &tp->net_stats_prev;
9156 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9157
9158 if (!hw_stats)
9159 return old_stats;
9160
9161 stats->rx_packets = old_stats->rx_packets +
9162 get_stat64(&hw_stats->rx_ucast_packets) +
9163 get_stat64(&hw_stats->rx_mcast_packets) +
9164 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009165
Linus Torvalds1da177e2005-04-16 15:20:36 -07009166 stats->tx_packets = old_stats->tx_packets +
9167 get_stat64(&hw_stats->tx_ucast_packets) +
9168 get_stat64(&hw_stats->tx_mcast_packets) +
9169 get_stat64(&hw_stats->tx_bcast_packets);
9170
9171 stats->rx_bytes = old_stats->rx_bytes +
9172 get_stat64(&hw_stats->rx_octets);
9173 stats->tx_bytes = old_stats->tx_bytes +
9174 get_stat64(&hw_stats->tx_octets);
9175
9176 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -07009177 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009178 stats->tx_errors = old_stats->tx_errors +
9179 get_stat64(&hw_stats->tx_errors) +
9180 get_stat64(&hw_stats->tx_mac_errors) +
9181 get_stat64(&hw_stats->tx_carrier_sense_errors) +
9182 get_stat64(&hw_stats->tx_discards);
9183
9184 stats->multicast = old_stats->multicast +
9185 get_stat64(&hw_stats->rx_mcast_packets);
9186 stats->collisions = old_stats->collisions +
9187 get_stat64(&hw_stats->tx_collisions);
9188
9189 stats->rx_length_errors = old_stats->rx_length_errors +
9190 get_stat64(&hw_stats->rx_frame_too_long_errors) +
9191 get_stat64(&hw_stats->rx_undersize_packets);
9192
9193 stats->rx_over_errors = old_stats->rx_over_errors +
9194 get_stat64(&hw_stats->rxbds_empty);
9195 stats->rx_frame_errors = old_stats->rx_frame_errors +
9196 get_stat64(&hw_stats->rx_align_errors);
9197 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
9198 get_stat64(&hw_stats->tx_discards);
9199 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
9200 get_stat64(&hw_stats->tx_carrier_sense_errors);
9201
9202 stats->rx_crc_errors = old_stats->rx_crc_errors +
9203 calc_crc_errors(tp);
9204
John W. Linville4f63b872005-09-12 14:43:18 -07009205 stats->rx_missed_errors = old_stats->rx_missed_errors +
9206 get_stat64(&hw_stats->rx_discards);
9207
Linus Torvalds1da177e2005-04-16 15:20:36 -07009208 return stats;
9209}
9210
9211static inline u32 calc_crc(unsigned char *buf, int len)
9212{
9213 u32 reg;
9214 u32 tmp;
9215 int j, k;
9216
9217 reg = 0xffffffff;
9218
9219 for (j = 0; j < len; j++) {
9220 reg ^= buf[j];
9221
9222 for (k = 0; k < 8; k++) {
9223 tmp = reg & 0x01;
9224
9225 reg >>= 1;
9226
9227 if (tmp) {
9228 reg ^= 0xedb88320;
9229 }
9230 }
9231 }
9232
9233 return ~reg;
9234}
9235
9236static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
9237{
9238 /* accept or reject all multicast frames */
9239 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
9240 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
9241 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
9242 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
9243}
9244
9245static void __tg3_set_rx_mode(struct net_device *dev)
9246{
9247 struct tg3 *tp = netdev_priv(dev);
9248 u32 rx_mode;
9249
9250 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
9251 RX_MODE_KEEP_VLAN_TAG);
9252
9253 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
9254 * flag clear.
9255 */
9256#if TG3_VLAN_TAG_USED
9257 if (!tp->vlgrp &&
9258 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
9259 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9260#else
9261 /* By definition, VLAN is disabled always in this
9262 * case.
9263 */
9264 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
9265 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9266#endif
9267
9268 if (dev->flags & IFF_PROMISC) {
9269 /* Promiscuous mode. */
9270 rx_mode |= RX_MODE_PROMISC;
9271 } else if (dev->flags & IFF_ALLMULTI) {
9272 /* Accept all multicast. */
9273 tg3_set_multi (tp, 1);
9274 } else if (dev->mc_count < 1) {
9275 /* Reject all multicast. */
9276 tg3_set_multi (tp, 0);
9277 } else {
9278 /* Accept one or more multicast(s). */
9279 struct dev_mc_list *mclist;
9280 unsigned int i;
9281 u32 mc_filter[4] = { 0, };
9282 u32 regidx;
9283 u32 bit;
9284 u32 crc;
9285
9286 for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
9287 i++, mclist = mclist->next) {
9288
9289 crc = calc_crc (mclist->dmi_addr, ETH_ALEN);
9290 bit = ~crc & 0x7f;
9291 regidx = (bit & 0x60) >> 5;
9292 bit &= 0x1f;
9293 mc_filter[regidx] |= (1 << bit);
9294 }
9295
9296 tw32(MAC_HASH_REG_0, mc_filter[0]);
9297 tw32(MAC_HASH_REG_1, mc_filter[1]);
9298 tw32(MAC_HASH_REG_2, mc_filter[2]);
9299 tw32(MAC_HASH_REG_3, mc_filter[3]);
9300 }
9301
9302 if (rx_mode != tp->rx_mode) {
9303 tp->rx_mode = rx_mode;
9304 tw32_f(MAC_RX_MODE, rx_mode);
9305 udelay(10);
9306 }
9307}
9308
9309static void tg3_set_rx_mode(struct net_device *dev)
9310{
9311 struct tg3 *tp = netdev_priv(dev);
9312
Michael Chane75f7c92006-03-20 21:33:26 -08009313 if (!netif_running(dev))
9314 return;
9315
David S. Millerf47c11e2005-06-24 20:18:35 -07009316 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009317 __tg3_set_rx_mode(dev);
David S. Millerf47c11e2005-06-24 20:18:35 -07009318 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009319}
9320
9321#define TG3_REGDUMP_LEN (32 * 1024)
9322
9323static int tg3_get_regs_len(struct net_device *dev)
9324{
9325 return TG3_REGDUMP_LEN;
9326}
9327
9328static void tg3_get_regs(struct net_device *dev,
9329 struct ethtool_regs *regs, void *_p)
9330{
9331 u32 *p = _p;
9332 struct tg3 *tp = netdev_priv(dev);
9333 u8 *orig_p = _p;
9334 int i;
9335
9336 regs->version = 0;
9337
9338 memset(p, 0, TG3_REGDUMP_LEN);
9339
Michael Chanbc1c7562006-03-20 17:48:03 -08009340 if (tp->link_config.phy_is_low_power)
9341 return;
9342
David S. Millerf47c11e2005-06-24 20:18:35 -07009343 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009344
9345#define __GET_REG32(reg) (*(p)++ = tr32(reg))
9346#define GET_REG32_LOOP(base,len) \
9347do { p = (u32 *)(orig_p + (base)); \
9348 for (i = 0; i < len; i += 4) \
9349 __GET_REG32((base) + i); \
9350} while (0)
9351#define GET_REG32_1(reg) \
9352do { p = (u32 *)(orig_p + (reg)); \
9353 __GET_REG32((reg)); \
9354} while (0)
9355
9356 GET_REG32_LOOP(TG3PCI_VENDOR, 0xb0);
9357 GET_REG32_LOOP(MAILBOX_INTERRUPT_0, 0x200);
9358 GET_REG32_LOOP(MAC_MODE, 0x4f0);
9359 GET_REG32_LOOP(SNDDATAI_MODE, 0xe0);
9360 GET_REG32_1(SNDDATAC_MODE);
9361 GET_REG32_LOOP(SNDBDS_MODE, 0x80);
9362 GET_REG32_LOOP(SNDBDI_MODE, 0x48);
9363 GET_REG32_1(SNDBDC_MODE);
9364 GET_REG32_LOOP(RCVLPC_MODE, 0x20);
9365 GET_REG32_LOOP(RCVLPC_SELLST_BASE, 0x15c);
9366 GET_REG32_LOOP(RCVDBDI_MODE, 0x0c);
9367 GET_REG32_LOOP(RCVDBDI_JUMBO_BD, 0x3c);
9368 GET_REG32_LOOP(RCVDBDI_BD_PROD_IDX_0, 0x44);
9369 GET_REG32_1(RCVDCC_MODE);
9370 GET_REG32_LOOP(RCVBDI_MODE, 0x20);
9371 GET_REG32_LOOP(RCVCC_MODE, 0x14);
9372 GET_REG32_LOOP(RCVLSC_MODE, 0x08);
9373 GET_REG32_1(MBFREE_MODE);
9374 GET_REG32_LOOP(HOSTCC_MODE, 0x100);
9375 GET_REG32_LOOP(MEMARB_MODE, 0x10);
9376 GET_REG32_LOOP(BUFMGR_MODE, 0x58);
9377 GET_REG32_LOOP(RDMAC_MODE, 0x08);
9378 GET_REG32_LOOP(WDMAC_MODE, 0x08);
Chris Elmquist091465d2005-12-20 13:25:19 -08009379 GET_REG32_1(RX_CPU_MODE);
9380 GET_REG32_1(RX_CPU_STATE);
9381 GET_REG32_1(RX_CPU_PGMCTR);
9382 GET_REG32_1(RX_CPU_HWBKPT);
9383 GET_REG32_1(TX_CPU_MODE);
9384 GET_REG32_1(TX_CPU_STATE);
9385 GET_REG32_1(TX_CPU_PGMCTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009386 GET_REG32_LOOP(GRCMBOX_INTERRUPT_0, 0x110);
9387 GET_REG32_LOOP(FTQ_RESET, 0x120);
9388 GET_REG32_LOOP(MSGINT_MODE, 0x0c);
9389 GET_REG32_1(DMAC_MODE);
9390 GET_REG32_LOOP(GRC_MODE, 0x4c);
9391 if (tp->tg3_flags & TG3_FLAG_NVRAM)
9392 GET_REG32_LOOP(NVRAM_CMD, 0x24);
9393
9394#undef __GET_REG32
9395#undef GET_REG32_LOOP
9396#undef GET_REG32_1
9397
David S. Millerf47c11e2005-06-24 20:18:35 -07009398 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009399}
9400
9401static int tg3_get_eeprom_len(struct net_device *dev)
9402{
9403 struct tg3 *tp = netdev_priv(dev);
9404
9405 return tp->nvram_size;
9406}
9407
Linus Torvalds1da177e2005-04-16 15:20:36 -07009408static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9409{
9410 struct tg3 *tp = netdev_priv(dev);
9411 int ret;
9412 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -08009413 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009414 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009415
Matt Carlsondf259d82009-04-20 06:57:14 +00009416 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM)
9417 return -EINVAL;
9418
Michael Chanbc1c7562006-03-20 17:48:03 -08009419 if (tp->link_config.phy_is_low_power)
9420 return -EAGAIN;
9421
Linus Torvalds1da177e2005-04-16 15:20:36 -07009422 offset = eeprom->offset;
9423 len = eeprom->len;
9424 eeprom->len = 0;
9425
9426 eeprom->magic = TG3_EEPROM_MAGIC;
9427
9428 if (offset & 3) {
9429 /* adjustments to start on required 4 byte boundary */
9430 b_offset = offset & 3;
9431 b_count = 4 - b_offset;
9432 if (b_count > len) {
9433 /* i.e. offset=1 len=2 */
9434 b_count = len;
9435 }
Matt Carlsona9dc5292009-02-25 14:25:30 +00009436 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009437 if (ret)
9438 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009439 memcpy(data, ((char*)&val) + b_offset, b_count);
9440 len -= b_count;
9441 offset += b_count;
9442 eeprom->len += b_count;
9443 }
9444
9445 /* read bytes upto the last 4 byte boundary */
9446 pd = &data[eeprom->len];
9447 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +00009448 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009449 if (ret) {
9450 eeprom->len += i;
9451 return ret;
9452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009453 memcpy(pd + i, &val, 4);
9454 }
9455 eeprom->len += i;
9456
9457 if (len & 3) {
9458 /* read last bytes not ending on 4 byte boundary */
9459 pd = &data[eeprom->len];
9460 b_count = len & 3;
9461 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009462 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009463 if (ret)
9464 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009465 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009466 eeprom->len += b_count;
9467 }
9468 return 0;
9469}
9470
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009471static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009472
9473static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9474{
9475 struct tg3 *tp = netdev_priv(dev);
9476 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009477 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009478 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009479 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009480
Michael Chanbc1c7562006-03-20 17:48:03 -08009481 if (tp->link_config.phy_is_low_power)
9482 return -EAGAIN;
9483
Matt Carlsondf259d82009-04-20 06:57:14 +00009484 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
9485 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009486 return -EINVAL;
9487
9488 offset = eeprom->offset;
9489 len = eeprom->len;
9490
9491 if ((b_offset = (offset & 3))) {
9492 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +00009493 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009494 if (ret)
9495 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009496 len += b_offset;
9497 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -07009498 if (len < 4)
9499 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009500 }
9501
9502 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -07009503 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009504 /* adjustments to end on required 4 byte boundary */
9505 odd_len = 1;
9506 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009507 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009508 if (ret)
9509 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009510 }
9511
9512 buf = data;
9513 if (b_offset || odd_len) {
9514 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009515 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009516 return -ENOMEM;
9517 if (b_offset)
9518 memcpy(buf, &start, 4);
9519 if (odd_len)
9520 memcpy(buf+len-4, &end, 4);
9521 memcpy(buf + b_offset, data, eeprom->len);
9522 }
9523
9524 ret = tg3_nvram_write_block(tp, offset, len, buf);
9525
9526 if (buf != data)
9527 kfree(buf);
9528
9529 return ret;
9530}
9531
9532static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9533{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009534 struct tg3 *tp = netdev_priv(dev);
9535
9536 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009537 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009538 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
9539 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009540 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
9541 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009542 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009543
Linus Torvalds1da177e2005-04-16 15:20:36 -07009544 cmd->supported = (SUPPORTED_Autoneg);
9545
9546 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY))
9547 cmd->supported |= (SUPPORTED_1000baseT_Half |
9548 SUPPORTED_1000baseT_Full);
9549
Karsten Keilef348142006-05-12 12:49:08 -07009550 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009551 cmd->supported |= (SUPPORTED_100baseT_Half |
9552 SUPPORTED_100baseT_Full |
9553 SUPPORTED_10baseT_Half |
9554 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -08009555 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -07009556 cmd->port = PORT_TP;
9557 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009558 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -07009559 cmd->port = PORT_FIBRE;
9560 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009561
Linus Torvalds1da177e2005-04-16 15:20:36 -07009562 cmd->advertising = tp->link_config.advertising;
9563 if (netif_running(dev)) {
9564 cmd->speed = tp->link_config.active_speed;
9565 cmd->duplex = tp->link_config.active_duplex;
9566 }
Matt Carlson882e9792009-09-01 13:21:36 +00009567 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +00009568 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009569 cmd->autoneg = tp->link_config.autoneg;
9570 cmd->maxtxpkt = 0;
9571 cmd->maxrxpkt = 0;
9572 return 0;
9573}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009574
Linus Torvalds1da177e2005-04-16 15:20:36 -07009575static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9576{
9577 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009578
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009579 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009580 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009581 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
9582 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009583 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
9584 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009585 }
9586
Matt Carlson7e5856b2009-02-25 14:23:01 +00009587 if (cmd->autoneg != AUTONEG_ENABLE &&
9588 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -07009589 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +00009590
9591 if (cmd->autoneg == AUTONEG_DISABLE &&
9592 cmd->duplex != DUPLEX_FULL &&
9593 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -07009594 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009595
Matt Carlson7e5856b2009-02-25 14:23:01 +00009596 if (cmd->autoneg == AUTONEG_ENABLE) {
9597 u32 mask = ADVERTISED_Autoneg |
9598 ADVERTISED_Pause |
9599 ADVERTISED_Asym_Pause;
9600
9601 if (!(tp->tg3_flags2 & TG3_FLAG_10_100_ONLY))
9602 mask |= ADVERTISED_1000baseT_Half |
9603 ADVERTISED_1000baseT_Full;
9604
9605 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES))
9606 mask |= ADVERTISED_100baseT_Half |
9607 ADVERTISED_100baseT_Full |
9608 ADVERTISED_10baseT_Half |
9609 ADVERTISED_10baseT_Full |
9610 ADVERTISED_TP;
9611 else
9612 mask |= ADVERTISED_FIBRE;
9613
9614 if (cmd->advertising & ~mask)
9615 return -EINVAL;
9616
9617 mask &= (ADVERTISED_1000baseT_Half |
9618 ADVERTISED_1000baseT_Full |
9619 ADVERTISED_100baseT_Half |
9620 ADVERTISED_100baseT_Full |
9621 ADVERTISED_10baseT_Half |
9622 ADVERTISED_10baseT_Full);
9623
9624 cmd->advertising &= mask;
9625 } else {
9626 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES) {
9627 if (cmd->speed != SPEED_1000)
9628 return -EINVAL;
9629
9630 if (cmd->duplex != DUPLEX_FULL)
9631 return -EINVAL;
9632 } else {
9633 if (cmd->speed != SPEED_100 &&
9634 cmd->speed != SPEED_10)
9635 return -EINVAL;
9636 }
9637 }
9638
David S. Millerf47c11e2005-06-24 20:18:35 -07009639 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009640
9641 tp->link_config.autoneg = cmd->autoneg;
9642 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -07009643 tp->link_config.advertising = (cmd->advertising |
9644 ADVERTISED_Autoneg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009645 tp->link_config.speed = SPEED_INVALID;
9646 tp->link_config.duplex = DUPLEX_INVALID;
9647 } else {
9648 tp->link_config.advertising = 0;
9649 tp->link_config.speed = cmd->speed;
9650 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009651 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009652
Michael Chan24fcad62006-12-17 17:06:46 -08009653 tp->link_config.orig_speed = tp->link_config.speed;
9654 tp->link_config.orig_duplex = tp->link_config.duplex;
9655 tp->link_config.orig_autoneg = tp->link_config.autoneg;
9656
Linus Torvalds1da177e2005-04-16 15:20:36 -07009657 if (netif_running(dev))
9658 tg3_setup_phy(tp, 1);
9659
David S. Millerf47c11e2005-06-24 20:18:35 -07009660 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009661
Linus Torvalds1da177e2005-04-16 15:20:36 -07009662 return 0;
9663}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009664
Linus Torvalds1da177e2005-04-16 15:20:36 -07009665static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
9666{
9667 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009668
Linus Torvalds1da177e2005-04-16 15:20:36 -07009669 strcpy(info->driver, DRV_MODULE_NAME);
9670 strcpy(info->version, DRV_MODULE_VERSION);
Michael Chanc4e65752006-03-20 22:29:32 -08009671 strcpy(info->fw_version, tp->fw_ver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009672 strcpy(info->bus_info, pci_name(tp->pdev));
9673}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009674
Linus Torvalds1da177e2005-04-16 15:20:36 -07009675static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
9676{
9677 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009678
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009679 if ((tp->tg3_flags & TG3_FLAG_WOL_CAP) &&
9680 device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -07009681 wol->supported = WAKE_MAGIC;
9682 else
9683 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009684 wol->wolopts = 0;
Matt Carlson05ac4cb2008-11-03 16:53:46 -08009685 if ((tp->tg3_flags & TG3_FLAG_WOL_ENABLE) &&
9686 device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009687 wol->wolopts = WAKE_MAGIC;
9688 memset(&wol->sopass, 0, sizeof(wol->sopass));
9689}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009690
Linus Torvalds1da177e2005-04-16 15:20:36 -07009691static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
9692{
9693 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009694 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009695
Linus Torvalds1da177e2005-04-16 15:20:36 -07009696 if (wol->wolopts & ~WAKE_MAGIC)
9697 return -EINVAL;
9698 if ((wol->wolopts & WAKE_MAGIC) &&
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009699 !((tp->tg3_flags & TG3_FLAG_WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009700 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009701
David S. Millerf47c11e2005-06-24 20:18:35 -07009702 spin_lock_bh(&tp->lock);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009703 if (wol->wolopts & WAKE_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009704 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009705 device_set_wakeup_enable(dp, true);
9706 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009707 tp->tg3_flags &= ~TG3_FLAG_WOL_ENABLE;
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009708 device_set_wakeup_enable(dp, false);
9709 }
David S. Millerf47c11e2005-06-24 20:18:35 -07009710 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009711
Linus Torvalds1da177e2005-04-16 15:20:36 -07009712 return 0;
9713}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009714
Linus Torvalds1da177e2005-04-16 15:20:36 -07009715static u32 tg3_get_msglevel(struct net_device *dev)
9716{
9717 struct tg3 *tp = netdev_priv(dev);
9718 return tp->msg_enable;
9719}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009720
Linus Torvalds1da177e2005-04-16 15:20:36 -07009721static void tg3_set_msglevel(struct net_device *dev, u32 value)
9722{
9723 struct tg3 *tp = netdev_priv(dev);
9724 tp->msg_enable = value;
9725}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009726
Linus Torvalds1da177e2005-04-16 15:20:36 -07009727static int tg3_set_tso(struct net_device *dev, u32 value)
9728{
9729 struct tg3 *tp = netdev_priv(dev);
9730
9731 if (!(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
9732 if (value)
9733 return -EINVAL;
9734 return 0;
9735 }
Matt Carlson027455a2008-12-21 20:19:30 -08009736 if ((dev->features & NETIF_F_IPV6_CSUM) &&
Matt Carlsone849cdc2009-11-13 13:03:38 +00009737 ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_2) ||
9738 (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3))) {
Matt Carlson9936bcf2007-10-10 18:03:07 -07009739 if (value) {
Michael Chanb0026622006-07-03 19:42:14 -07009740 dev->features |= NETIF_F_TSO6;
Matt Carlsone849cdc2009-11-13 13:03:38 +00009741 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
9742 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -07009743 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
9744 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Matt Carlson321d32a2008-11-21 17:22:19 -08009745 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsone849cdc2009-11-13 13:03:38 +00009746 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson9936bcf2007-10-10 18:03:07 -07009747 dev->features |= NETIF_F_TSO_ECN;
9748 } else
9749 dev->features &= ~(NETIF_F_TSO6 | NETIF_F_TSO_ECN);
Michael Chanb0026622006-07-03 19:42:14 -07009750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009751 return ethtool_op_set_tso(dev, value);
9752}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009753
Linus Torvalds1da177e2005-04-16 15:20:36 -07009754static int tg3_nway_reset(struct net_device *dev)
9755{
9756 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009757 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009758
Linus Torvalds1da177e2005-04-16 15:20:36 -07009759 if (!netif_running(dev))
9760 return -EAGAIN;
9761
Michael Chanc94e3942005-09-27 12:12:42 -07009762 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
9763 return -EINVAL;
9764
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009765 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
9766 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
9767 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009768 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009769 } else {
9770 u32 bmcr;
9771
9772 spin_lock_bh(&tp->lock);
9773 r = -EINVAL;
9774 tg3_readphy(tp, MII_BMCR, &bmcr);
9775 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
9776 ((bmcr & BMCR_ANENABLE) ||
9777 (tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT))) {
9778 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
9779 BMCR_ANENABLE);
9780 r = 0;
9781 }
9782 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009783 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009784
Linus Torvalds1da177e2005-04-16 15:20:36 -07009785 return r;
9786}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009787
Linus Torvalds1da177e2005-04-16 15:20:36 -07009788static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
9789{
9790 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009791
Linus Torvalds1da177e2005-04-16 15:20:36 -07009792 ering->rx_max_pending = TG3_RX_RING_SIZE - 1;
9793 ering->rx_mini_max_pending = 0;
Michael Chan4f81c322006-03-20 21:33:42 -08009794 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE)
9795 ering->rx_jumbo_max_pending = TG3_RX_JUMBO_RING_SIZE - 1;
9796 else
9797 ering->rx_jumbo_max_pending = 0;
9798
9799 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009800
9801 ering->rx_pending = tp->rx_pending;
9802 ering->rx_mini_pending = 0;
Michael Chan4f81c322006-03-20 21:33:42 -08009803 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE)
9804 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
9805 else
9806 ering->rx_jumbo_pending = 0;
9807
Matt Carlsonf3f3f272009-08-28 14:03:21 +00009808 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009809}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009810
Linus Torvalds1da177e2005-04-16 15:20:36 -07009811static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
9812{
9813 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +00009814 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009815
Linus Torvalds1da177e2005-04-16 15:20:36 -07009816 if ((ering->rx_pending > TG3_RX_RING_SIZE - 1) ||
9817 (ering->rx_jumbo_pending > TG3_RX_JUMBO_RING_SIZE - 1) ||
Michael Chanbc3a9252006-10-18 20:55:18 -07009818 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
9819 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Michael Chan7f62ad52007-02-20 23:25:40 -08009820 ((tp->tg3_flags2 & TG3_FLG2_TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -07009821 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009822 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009823
Michael Chanbbe832c2005-06-24 20:20:04 -07009824 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009825 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009826 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -07009827 irq_sync = 1;
9828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009829
Michael Chanbbe832c2005-06-24 20:20:04 -07009830 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009831
Linus Torvalds1da177e2005-04-16 15:20:36 -07009832 tp->rx_pending = ering->rx_pending;
9833
9834 if ((tp->tg3_flags2 & TG3_FLG2_MAX_RXPEND_64) &&
9835 tp->rx_pending > 63)
9836 tp->rx_pending = 63;
9837 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +00009838
9839 for (i = 0; i < TG3_IRQ_MAX_VECS; i++)
9840 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009841
9842 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -07009843 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -07009844 err = tg3_restart_hw(tp, 1);
9845 if (!err)
9846 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009847 }
9848
David S. Millerf47c11e2005-06-24 20:18:35 -07009849 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009850
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009851 if (irq_sync && !err)
9852 tg3_phy_start(tp);
9853
Michael Chanb9ec6c12006-07-25 16:37:27 -07009854 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009855}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009856
Linus Torvalds1da177e2005-04-16 15:20:36 -07009857static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
9858{
9859 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009860
Linus Torvalds1da177e2005-04-16 15:20:36 -07009861 epause->autoneg = (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG) != 0;
Matt Carlson8d018622007-12-20 20:05:44 -08009862
Steve Glendinninge18ce342008-12-16 02:00:00 -08009863 if (tp->link_config.active_flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -08009864 epause->rx_pause = 1;
9865 else
9866 epause->rx_pause = 0;
9867
Steve Glendinninge18ce342008-12-16 02:00:00 -08009868 if (tp->link_config.active_flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -08009869 epause->tx_pause = 1;
9870 else
9871 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009872}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009873
Linus Torvalds1da177e2005-04-16 15:20:36 -07009874static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
9875{
9876 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009877 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009878
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009879 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
9880 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
9881 return -EAGAIN;
9882
9883 if (epause->autoneg) {
9884 u32 newadv;
9885 struct phy_device *phydev;
9886
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009887 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009888
9889 if (epause->rx_pause) {
9890 if (epause->tx_pause)
9891 newadv = ADVERTISED_Pause;
9892 else
9893 newadv = ADVERTISED_Pause |
9894 ADVERTISED_Asym_Pause;
9895 } else if (epause->tx_pause) {
9896 newadv = ADVERTISED_Asym_Pause;
9897 } else
9898 newadv = 0;
9899
9900 if (tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED) {
9901 u32 oldadv = phydev->advertising &
9902 (ADVERTISED_Pause |
9903 ADVERTISED_Asym_Pause);
9904 if (oldadv != newadv) {
9905 phydev->advertising &=
9906 ~(ADVERTISED_Pause |
9907 ADVERTISED_Asym_Pause);
9908 phydev->advertising |= newadv;
9909 err = phy_start_aneg(phydev);
9910 }
9911 } else {
9912 tp->link_config.advertising &=
9913 ~(ADVERTISED_Pause |
9914 ADVERTISED_Asym_Pause);
9915 tp->link_config.advertising |= newadv;
9916 }
9917 } else {
9918 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -08009919 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009920 else
Steve Glendinninge18ce342008-12-16 02:00:00 -08009921 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009922
9923 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -08009924 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009925 else
Steve Glendinninge18ce342008-12-16 02:00:00 -08009926 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009927
9928 if (netif_running(dev))
9929 tg3_setup_flow_control(tp, 0, 0);
9930 }
9931 } else {
9932 int irq_sync = 0;
9933
9934 if (netif_running(dev)) {
9935 tg3_netif_stop(tp);
9936 irq_sync = 1;
9937 }
9938
9939 tg3_full_lock(tp, irq_sync);
9940
9941 if (epause->autoneg)
9942 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
9943 else
9944 tp->tg3_flags &= ~TG3_FLAG_PAUSE_AUTONEG;
9945 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -08009946 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009947 else
Steve Glendinninge18ce342008-12-16 02:00:00 -08009948 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009949 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -08009950 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009951 else
Steve Glendinninge18ce342008-12-16 02:00:00 -08009952 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009953
9954 if (netif_running(dev)) {
9955 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
9956 err = tg3_restart_hw(tp, 1);
9957 if (!err)
9958 tg3_netif_start(tp);
9959 }
9960
9961 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -07009962 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009963
Michael Chanb9ec6c12006-07-25 16:37:27 -07009964 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009965}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009966
Linus Torvalds1da177e2005-04-16 15:20:36 -07009967static u32 tg3_get_rx_csum(struct net_device *dev)
9968{
9969 struct tg3 *tp = netdev_priv(dev);
9970 return (tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) != 0;
9971}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009972
Linus Torvalds1da177e2005-04-16 15:20:36 -07009973static int tg3_set_rx_csum(struct net_device *dev, u32 data)
9974{
9975 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009976
Linus Torvalds1da177e2005-04-16 15:20:36 -07009977 if (tp->tg3_flags & TG3_FLAG_BROKEN_CHECKSUMS) {
9978 if (data != 0)
9979 return -EINVAL;
9980 return 0;
9981 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009982
David S. Millerf47c11e2005-06-24 20:18:35 -07009983 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009984 if (data)
9985 tp->tg3_flags |= TG3_FLAG_RX_CHECKSUMS;
9986 else
9987 tp->tg3_flags &= ~TG3_FLAG_RX_CHECKSUMS;
David S. Millerf47c11e2005-06-24 20:18:35 -07009988 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009989
Linus Torvalds1da177e2005-04-16 15:20:36 -07009990 return 0;
9991}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009992
Linus Torvalds1da177e2005-04-16 15:20:36 -07009993static int tg3_set_tx_csum(struct net_device *dev, u32 data)
9994{
9995 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009996
Linus Torvalds1da177e2005-04-16 15:20:36 -07009997 if (tp->tg3_flags & TG3_FLAG_BROKEN_CHECKSUMS) {
9998 if (data != 0)
9999 return -EINVAL;
10000 return 0;
10001 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010002
Matt Carlson321d32a2008-11-21 17:22:19 -080010003 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Michael Chan6460d942007-07-14 19:07:52 -070010004 ethtool_op_set_tx_ipv6_csum(dev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010005 else
Michael Chan9c27dbd2006-03-20 22:28:27 -080010006 ethtool_op_set_tx_csum(dev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010007
10008 return 0;
10009}
10010
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010011static int tg3_get_sset_count (struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010012{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010013 switch (sset) {
10014 case ETH_SS_TEST:
10015 return TG3_NUM_TEST;
10016 case ETH_SS_STATS:
10017 return TG3_NUM_STATS;
10018 default:
10019 return -EOPNOTSUPP;
10020 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070010021}
10022
Linus Torvalds1da177e2005-04-16 15:20:36 -070010023static void tg3_get_strings (struct net_device *dev, u32 stringset, u8 *buf)
10024{
10025 switch (stringset) {
10026 case ETH_SS_STATS:
10027 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
10028 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070010029 case ETH_SS_TEST:
10030 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
10031 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010032 default:
10033 WARN_ON(1); /* we need a WARN() */
10034 break;
10035 }
10036}
10037
Michael Chan4009a932005-09-05 17:52:54 -070010038static int tg3_phys_id(struct net_device *dev, u32 data)
10039{
10040 struct tg3 *tp = netdev_priv(dev);
10041 int i;
10042
10043 if (!netif_running(tp->dev))
10044 return -EAGAIN;
10045
10046 if (data == 0)
Stephen Hemminger759afc32008-02-23 19:51:59 -080010047 data = UINT_MAX / 2;
Michael Chan4009a932005-09-05 17:52:54 -070010048
10049 for (i = 0; i < (data * 2); i++) {
10050 if ((i % 2) == 0)
10051 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10052 LED_CTRL_1000MBPS_ON |
10053 LED_CTRL_100MBPS_ON |
10054 LED_CTRL_10MBPS_ON |
10055 LED_CTRL_TRAFFIC_OVERRIDE |
10056 LED_CTRL_TRAFFIC_BLINK |
10057 LED_CTRL_TRAFFIC_LED);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010058
Michael Chan4009a932005-09-05 17:52:54 -070010059 else
10060 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10061 LED_CTRL_TRAFFIC_OVERRIDE);
10062
10063 if (msleep_interruptible(500))
10064 break;
10065 }
10066 tw32(MAC_LED_CTRL, tp->led_ctrl);
10067 return 0;
10068}
10069
Linus Torvalds1da177e2005-04-16 15:20:36 -070010070static void tg3_get_ethtool_stats (struct net_device *dev,
10071 struct ethtool_stats *estats, u64 *tmp_stats)
10072{
10073 struct tg3 *tp = netdev_priv(dev);
10074 memcpy(tmp_stats, tg3_get_estats(tp), sizeof(tp->estats));
10075}
10076
Michael Chan566f86a2005-05-29 14:56:58 -070010077#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080010078#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
10079#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
10080#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Michael Chanb16250e2006-09-27 16:10:14 -070010081#define NVRAM_SELFBOOT_HW_SIZE 0x20
10082#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070010083
10084static int tg3_test_nvram(struct tg3 *tp)
10085{
Al Virob9fc7dc2007-12-17 22:59:57 -080010086 u32 csum, magic;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010087 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010088 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070010089
Matt Carlsondf259d82009-04-20 06:57:14 +000010090 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM)
10091 return 0;
10092
Matt Carlsone4f34112009-02-25 14:25:00 +000010093 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080010094 return -EIO;
10095
Michael Chan1b277772006-03-20 22:27:48 -080010096 if (magic == TG3_EEPROM_MAGIC)
10097 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070010098 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080010099 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
10100 TG3_EEPROM_SB_FORMAT_1) {
10101 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
10102 case TG3_EEPROM_SB_REVISION_0:
10103 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
10104 break;
10105 case TG3_EEPROM_SB_REVISION_2:
10106 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
10107 break;
10108 case TG3_EEPROM_SB_REVISION_3:
10109 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
10110 break;
10111 default:
10112 return 0;
10113 }
10114 } else
Michael Chan1b277772006-03-20 22:27:48 -080010115 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070010116 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
10117 size = NVRAM_SELFBOOT_HW_SIZE;
10118 else
Michael Chan1b277772006-03-20 22:27:48 -080010119 return -EIO;
10120
10121 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070010122 if (buf == NULL)
10123 return -ENOMEM;
10124
Michael Chan1b277772006-03-20 22:27:48 -080010125 err = -EIO;
10126 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010127 err = tg3_nvram_read_be32(tp, i, &buf[j]);
10128 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070010129 break;
Michael Chan566f86a2005-05-29 14:56:58 -070010130 }
Michael Chan1b277772006-03-20 22:27:48 -080010131 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070010132 goto out;
10133
Michael Chan1b277772006-03-20 22:27:48 -080010134 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010135 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080010136 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010137 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080010138 u8 *buf8 = (u8 *) buf, csum8 = 0;
10139
Al Virob9fc7dc2007-12-17 22:59:57 -080010140 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080010141 TG3_EEPROM_SB_REVISION_2) {
10142 /* For rev 2, the csum doesn't include the MBA. */
10143 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
10144 csum8 += buf8[i];
10145 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
10146 csum8 += buf8[i];
10147 } else {
10148 for (i = 0; i < size; i++)
10149 csum8 += buf8[i];
10150 }
Michael Chan1b277772006-03-20 22:27:48 -080010151
Adrian Bunkad96b482006-04-05 22:21:04 -070010152 if (csum8 == 0) {
10153 err = 0;
10154 goto out;
10155 }
10156
10157 err = -EIO;
10158 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080010159 }
Michael Chan566f86a2005-05-29 14:56:58 -070010160
Al Virob9fc7dc2007-12-17 22:59:57 -080010161 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010162 TG3_EEPROM_MAGIC_HW) {
10163 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000010164 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070010165 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070010166
10167 /* Separate the parity bits and the data bytes. */
10168 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
10169 if ((i == 0) || (i == 8)) {
10170 int l;
10171 u8 msk;
10172
10173 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
10174 parity[k++] = buf8[i] & msk;
10175 i++;
10176 }
10177 else if (i == 16) {
10178 int l;
10179 u8 msk;
10180
10181 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
10182 parity[k++] = buf8[i] & msk;
10183 i++;
10184
10185 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
10186 parity[k++] = buf8[i] & msk;
10187 i++;
10188 }
10189 data[j++] = buf8[i];
10190 }
10191
10192 err = -EIO;
10193 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
10194 u8 hw8 = hweight8(data[i]);
10195
10196 if ((hw8 & 0x1) && parity[i])
10197 goto out;
10198 else if (!(hw8 & 0x1) && !parity[i])
10199 goto out;
10200 }
10201 err = 0;
10202 goto out;
10203 }
10204
Michael Chan566f86a2005-05-29 14:56:58 -070010205 /* Bootstrap checksum at offset 0x10 */
10206 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlsona9dc5292009-02-25 14:25:30 +000010207 if (csum != be32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070010208 goto out;
10209
10210 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
10211 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlsona9dc5292009-02-25 14:25:30 +000010212 if (csum != be32_to_cpu(buf[0xfc/4]))
10213 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070010214
10215 err = 0;
10216
10217out:
10218 kfree(buf);
10219 return err;
10220}
10221
Michael Chanca430072005-05-29 14:57:23 -070010222#define TG3_SERDES_TIMEOUT_SEC 2
10223#define TG3_COPPER_TIMEOUT_SEC 6
10224
10225static int tg3_test_link(struct tg3 *tp)
10226{
10227 int i, max;
10228
10229 if (!netif_running(tp->dev))
10230 return -ENODEV;
10231
Michael Chan4c987482005-09-05 17:52:38 -070010232 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070010233 max = TG3_SERDES_TIMEOUT_SEC;
10234 else
10235 max = TG3_COPPER_TIMEOUT_SEC;
10236
10237 for (i = 0; i < max; i++) {
10238 if (netif_carrier_ok(tp->dev))
10239 return 0;
10240
10241 if (msleep_interruptible(1000))
10242 break;
10243 }
10244
10245 return -EIO;
10246}
10247
Michael Chana71116d2005-05-29 14:58:11 -070010248/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080010249static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070010250{
Michael Chanb16250e2006-09-27 16:10:14 -070010251 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070010252 u32 offset, read_mask, write_mask, val, save_val, read_val;
10253 static struct {
10254 u16 offset;
10255 u16 flags;
10256#define TG3_FL_5705 0x1
10257#define TG3_FL_NOT_5705 0x2
10258#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070010259#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070010260 u32 read_mask;
10261 u32 write_mask;
10262 } reg_tbl[] = {
10263 /* MAC Control Registers */
10264 { MAC_MODE, TG3_FL_NOT_5705,
10265 0x00000000, 0x00ef6f8c },
10266 { MAC_MODE, TG3_FL_5705,
10267 0x00000000, 0x01ef6b8c },
10268 { MAC_STATUS, TG3_FL_NOT_5705,
10269 0x03800107, 0x00000000 },
10270 { MAC_STATUS, TG3_FL_5705,
10271 0x03800100, 0x00000000 },
10272 { MAC_ADDR_0_HIGH, 0x0000,
10273 0x00000000, 0x0000ffff },
10274 { MAC_ADDR_0_LOW, 0x0000,
10275 0x00000000, 0xffffffff },
10276 { MAC_RX_MTU_SIZE, 0x0000,
10277 0x00000000, 0x0000ffff },
10278 { MAC_TX_MODE, 0x0000,
10279 0x00000000, 0x00000070 },
10280 { MAC_TX_LENGTHS, 0x0000,
10281 0x00000000, 0x00003fff },
10282 { MAC_RX_MODE, TG3_FL_NOT_5705,
10283 0x00000000, 0x000007fc },
10284 { MAC_RX_MODE, TG3_FL_5705,
10285 0x00000000, 0x000007dc },
10286 { MAC_HASH_REG_0, 0x0000,
10287 0x00000000, 0xffffffff },
10288 { MAC_HASH_REG_1, 0x0000,
10289 0x00000000, 0xffffffff },
10290 { MAC_HASH_REG_2, 0x0000,
10291 0x00000000, 0xffffffff },
10292 { MAC_HASH_REG_3, 0x0000,
10293 0x00000000, 0xffffffff },
10294
10295 /* Receive Data and Receive BD Initiator Control Registers. */
10296 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
10297 0x00000000, 0xffffffff },
10298 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
10299 0x00000000, 0xffffffff },
10300 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
10301 0x00000000, 0x00000003 },
10302 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
10303 0x00000000, 0xffffffff },
10304 { RCVDBDI_STD_BD+0, 0x0000,
10305 0x00000000, 0xffffffff },
10306 { RCVDBDI_STD_BD+4, 0x0000,
10307 0x00000000, 0xffffffff },
10308 { RCVDBDI_STD_BD+8, 0x0000,
10309 0x00000000, 0xffff0002 },
10310 { RCVDBDI_STD_BD+0xc, 0x0000,
10311 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010312
Michael Chana71116d2005-05-29 14:58:11 -070010313 /* Receive BD Initiator Control Registers. */
10314 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
10315 0x00000000, 0xffffffff },
10316 { RCVBDI_STD_THRESH, TG3_FL_5705,
10317 0x00000000, 0x000003ff },
10318 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
10319 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010320
Michael Chana71116d2005-05-29 14:58:11 -070010321 /* Host Coalescing Control Registers. */
10322 { HOSTCC_MODE, TG3_FL_NOT_5705,
10323 0x00000000, 0x00000004 },
10324 { HOSTCC_MODE, TG3_FL_5705,
10325 0x00000000, 0x000000f6 },
10326 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
10327 0x00000000, 0xffffffff },
10328 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
10329 0x00000000, 0x000003ff },
10330 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
10331 0x00000000, 0xffffffff },
10332 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
10333 0x00000000, 0x000003ff },
10334 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
10335 0x00000000, 0xffffffff },
10336 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10337 0x00000000, 0x000000ff },
10338 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
10339 0x00000000, 0xffffffff },
10340 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10341 0x00000000, 0x000000ff },
10342 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
10343 0x00000000, 0xffffffff },
10344 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
10345 0x00000000, 0xffffffff },
10346 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10347 0x00000000, 0xffffffff },
10348 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10349 0x00000000, 0x000000ff },
10350 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10351 0x00000000, 0xffffffff },
10352 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10353 0x00000000, 0x000000ff },
10354 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
10355 0x00000000, 0xffffffff },
10356 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
10357 0x00000000, 0xffffffff },
10358 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
10359 0x00000000, 0xffffffff },
10360 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
10361 0x00000000, 0xffffffff },
10362 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
10363 0x00000000, 0xffffffff },
10364 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
10365 0xffffffff, 0x00000000 },
10366 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
10367 0xffffffff, 0x00000000 },
10368
10369 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070010370 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010371 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070010372 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010373 0x00000000, 0x007fffff },
10374 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
10375 0x00000000, 0x0000003f },
10376 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
10377 0x00000000, 0x000001ff },
10378 { BUFMGR_MB_HIGH_WATER, 0x0000,
10379 0x00000000, 0x000001ff },
10380 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
10381 0xffffffff, 0x00000000 },
10382 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
10383 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010384
Michael Chana71116d2005-05-29 14:58:11 -070010385 /* Mailbox Registers */
10386 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
10387 0x00000000, 0x000001ff },
10388 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
10389 0x00000000, 0x000001ff },
10390 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
10391 0x00000000, 0x000007ff },
10392 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
10393 0x00000000, 0x000001ff },
10394
10395 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
10396 };
10397
Michael Chanb16250e2006-09-27 16:10:14 -070010398 is_5705 = is_5750 = 0;
10399 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
Michael Chana71116d2005-05-29 14:58:11 -070010400 is_5705 = 1;
Michael Chanb16250e2006-09-27 16:10:14 -070010401 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
10402 is_5750 = 1;
10403 }
Michael Chana71116d2005-05-29 14:58:11 -070010404
10405 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
10406 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
10407 continue;
10408
10409 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
10410 continue;
10411
10412 if ((tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
10413 (reg_tbl[i].flags & TG3_FL_NOT_5788))
10414 continue;
10415
Michael Chanb16250e2006-09-27 16:10:14 -070010416 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
10417 continue;
10418
Michael Chana71116d2005-05-29 14:58:11 -070010419 offset = (u32) reg_tbl[i].offset;
10420 read_mask = reg_tbl[i].read_mask;
10421 write_mask = reg_tbl[i].write_mask;
10422
10423 /* Save the original register content */
10424 save_val = tr32(offset);
10425
10426 /* Determine the read-only value. */
10427 read_val = save_val & read_mask;
10428
10429 /* Write zero to the register, then make sure the read-only bits
10430 * are not changed and the read/write bits are all zeros.
10431 */
10432 tw32(offset, 0);
10433
10434 val = tr32(offset);
10435
10436 /* Test the read-only and read/write bits. */
10437 if (((val & read_mask) != read_val) || (val & write_mask))
10438 goto out;
10439
10440 /* Write ones to all the bits defined by RdMask and WrMask, then
10441 * make sure the read-only bits are not changed and the
10442 * read/write bits are all ones.
10443 */
10444 tw32(offset, read_mask | write_mask);
10445
10446 val = tr32(offset);
10447
10448 /* Test the read-only bits. */
10449 if ((val & read_mask) != read_val)
10450 goto out;
10451
10452 /* Test the read/write bits. */
10453 if ((val & write_mask) != write_mask)
10454 goto out;
10455
10456 tw32(offset, save_val);
10457 }
10458
10459 return 0;
10460
10461out:
Michael Chan9f88f292006-12-07 00:22:54 -080010462 if (netif_msg_hw(tp))
10463 printk(KERN_ERR PFX "Register test failed at offset %x\n",
10464 offset);
Michael Chana71116d2005-05-29 14:58:11 -070010465 tw32(offset, save_val);
10466 return -EIO;
10467}
10468
Michael Chan7942e1d2005-05-29 14:58:36 -070010469static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
10470{
Arjan van de Venf71e1302006-03-03 21:33:57 -050010471 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070010472 int i;
10473 u32 j;
10474
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020010475 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070010476 for (j = 0; j < len; j += 4) {
10477 u32 val;
10478
10479 tg3_write_mem(tp, offset + j, test_pattern[i]);
10480 tg3_read_mem(tp, offset + j, &val);
10481 if (val != test_pattern[i])
10482 return -EIO;
10483 }
10484 }
10485 return 0;
10486}
10487
10488static int tg3_test_memory(struct tg3 *tp)
10489{
10490 static struct mem_entry {
10491 u32 offset;
10492 u32 len;
10493 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080010494 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070010495 { 0x00002000, 0x1c000},
10496 { 0xffffffff, 0x00000}
10497 }, mem_tbl_5705[] = {
10498 { 0x00000100, 0x0000c},
10499 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070010500 { 0x00004000, 0x00800},
10501 { 0x00006000, 0x01000},
10502 { 0x00008000, 0x02000},
10503 { 0x00010000, 0x0e000},
10504 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080010505 }, mem_tbl_5755[] = {
10506 { 0x00000200, 0x00008},
10507 { 0x00004000, 0x00800},
10508 { 0x00006000, 0x00800},
10509 { 0x00008000, 0x02000},
10510 { 0x00010000, 0x0c000},
10511 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070010512 }, mem_tbl_5906[] = {
10513 { 0x00000200, 0x00008},
10514 { 0x00004000, 0x00400},
10515 { 0x00006000, 0x00400},
10516 { 0x00008000, 0x01000},
10517 { 0x00010000, 0x01000},
10518 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070010519 };
10520 struct mem_entry *mem_tbl;
10521 int err = 0;
10522 int i;
10523
Matt Carlson321d32a2008-11-21 17:22:19 -080010524 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
10525 mem_tbl = mem_tbl_5755;
10526 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
10527 mem_tbl = mem_tbl_5906;
10528 else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
10529 mem_tbl = mem_tbl_5705;
10530 else
Michael Chan7942e1d2005-05-29 14:58:36 -070010531 mem_tbl = mem_tbl_570x;
10532
10533 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
10534 if ((err = tg3_do_mem_test(tp, mem_tbl[i].offset,
10535 mem_tbl[i].len)) != 0)
10536 break;
10537 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010538
Michael Chan7942e1d2005-05-29 14:58:36 -070010539 return err;
10540}
10541
Michael Chan9f40dea2005-09-05 17:53:06 -070010542#define TG3_MAC_LOOPBACK 0
10543#define TG3_PHY_LOOPBACK 1
10544
10545static int tg3_run_loopback(struct tg3 *tp, int loopback_mode)
Michael Chanc76949a2005-05-29 14:58:59 -070010546{
Michael Chan9f40dea2005-09-05 17:53:06 -070010547 u32 mac_mode, rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010548 u32 desc_idx, coal_now;
Michael Chanc76949a2005-05-29 14:58:59 -070010549 struct sk_buff *skb, *rx_skb;
10550 u8 *tx_data;
10551 dma_addr_t map;
10552 int num_pkts, tx_len, rx_len, i, err;
10553 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000010554 struct tg3_napi *tnapi, *rnapi;
Matt Carlson21f581a2009-08-28 14:00:25 +000010555 struct tg3_rx_prodring_set *tpr = &tp->prodring[0];
Michael Chanc76949a2005-05-29 14:58:59 -070010556
Matt Carlson0c1d0e22009-09-01 13:16:33 +000010557 if (tp->irq_cnt > 1) {
10558 tnapi = &tp->napi[1];
10559 rnapi = &tp->napi[1];
10560 } else {
10561 tnapi = &tp->napi[0];
10562 rnapi = &tp->napi[0];
10563 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010564 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000010565
Michael Chan9f40dea2005-09-05 17:53:06 -070010566 if (loopback_mode == TG3_MAC_LOOPBACK) {
Michael Chanc94e3942005-09-27 12:12:42 -070010567 /* HW errata - mac loopback fails in some cases on 5780.
10568 * Normal traffic and PHY loopback are not affected by
10569 * errata.
10570 */
10571 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780)
10572 return 0;
10573
Michael Chan9f40dea2005-09-05 17:53:06 -070010574 mac_mode = (tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK) |
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010575 MAC_MODE_PORT_INT_LPBACK;
10576 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
10577 mac_mode |= MAC_MODE_LINK_POLARITY;
Michael Chan3f7045c2006-09-27 16:02:29 -070010578 if (tp->tg3_flags & TG3_FLAG_10_100_ONLY)
10579 mac_mode |= MAC_MODE_PORT_MODE_MII;
10580 else
10581 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chan9f40dea2005-09-05 17:53:06 -070010582 tw32(MAC_MODE, mac_mode);
10583 } else if (loopback_mode == TG3_PHY_LOOPBACK) {
Michael Chan3f7045c2006-09-27 16:02:29 -070010584 u32 val;
10585
Matt Carlson7f97a4b2009-08-25 10:10:03 +000010586 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
10587 tg3_phy_fet_toggle_apd(tp, false);
Michael Chan5d64ad32006-12-07 00:19:40 -080010588 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED100;
10589 } else
10590 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED1000;
Michael Chan3f7045c2006-09-27 16:02:29 -070010591
Matt Carlson9ef8ca92007-07-11 19:48:29 -070010592 tg3_phy_toggle_automdix(tp, 0);
10593
Michael Chan3f7045c2006-09-27 16:02:29 -070010594 tg3_writephy(tp, MII_BMCR, val);
Michael Chanc94e3942005-09-27 12:12:42 -070010595 udelay(40);
Michael Chan5d64ad32006-12-07 00:19:40 -080010596
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010597 mac_mode = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000010598 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
10599 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
10600 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x1800);
Michael Chan5d64ad32006-12-07 00:19:40 -080010601 mac_mode |= MAC_MODE_PORT_MODE_MII;
10602 } else
10603 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chanb16250e2006-09-27 16:10:14 -070010604
Michael Chanc94e3942005-09-27 12:12:42 -070010605 /* reset to prevent losing 1st rx packet intermittently */
10606 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES) {
10607 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
10608 udelay(10);
10609 tw32_f(MAC_RX_MODE, tp->rx_mode);
10610 }
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010611 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
10612 if ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5401)
10613 mac_mode &= ~MAC_MODE_LINK_POLARITY;
10614 else if ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5411)
10615 mac_mode |= MAC_MODE_LINK_POLARITY;
Michael Chanff18ff02006-03-27 23:17:27 -080010616 tg3_writephy(tp, MII_TG3_EXT_CTRL,
10617 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
10618 }
Michael Chan9f40dea2005-09-05 17:53:06 -070010619 tw32(MAC_MODE, mac_mode);
Michael Chan9f40dea2005-09-05 17:53:06 -070010620 }
10621 else
10622 return -EINVAL;
Michael Chanc76949a2005-05-29 14:58:59 -070010623
10624 err = -EIO;
10625
Michael Chanc76949a2005-05-29 14:58:59 -070010626 tx_len = 1514;
David S. Millera20e9c62006-07-31 22:38:16 -070010627 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070010628 if (!skb)
10629 return -ENOMEM;
10630
Michael Chanc76949a2005-05-29 14:58:59 -070010631 tx_data = skb_put(skb, tx_len);
10632 memcpy(tx_data, tp->dev->dev_addr, 6);
10633 memset(tx_data + 6, 0x0, 8);
10634
10635 tw32(MAC_RX_MTU_SIZE, tx_len + 4);
10636
10637 for (i = 14; i < tx_len; i++)
10638 tx_data[i] = (u8) (i & 0xff);
10639
Matt Carlsona21771d2009-11-02 14:25:31 +000010640 if (skb_dma_map(&tp->pdev->dev, skb, DMA_TO_DEVICE)) {
10641 dev_kfree_skb(skb);
10642 return -EIO;
10643 }
Michael Chanc76949a2005-05-29 14:58:59 -070010644
10645 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010646 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070010647
10648 udelay(10);
10649
Matt Carlson898a56f2009-08-28 14:02:40 +000010650 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070010651
Michael Chanc76949a2005-05-29 14:58:59 -070010652 num_pkts = 0;
10653
Matt Carlsona21771d2009-11-02 14:25:31 +000010654 tg3_set_txd(tnapi, tnapi->tx_prod,
10655 skb_shinfo(skb)->dma_head, tx_len, 0, 1);
Michael Chanc76949a2005-05-29 14:58:59 -070010656
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010657 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070010658 num_pkts++;
10659
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010660 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
10661 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070010662
10663 udelay(10);
10664
Matt Carlson303fc922009-11-02 14:27:34 +000010665 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
10666 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070010667 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010668 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070010669
10670 udelay(10);
10671
Matt Carlson898a56f2009-08-28 14:02:40 +000010672 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
10673 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010674 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070010675 (rx_idx == (rx_start_idx + num_pkts)))
10676 break;
10677 }
10678
Matt Carlsona21771d2009-11-02 14:25:31 +000010679 skb_dma_unmap(&tp->pdev->dev, skb, DMA_TO_DEVICE);
Michael Chanc76949a2005-05-29 14:58:59 -070010680 dev_kfree_skb(skb);
10681
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010682 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070010683 goto out;
10684
10685 if (rx_idx != rx_start_idx + num_pkts)
10686 goto out;
10687
Matt Carlson72334482009-08-28 14:03:01 +000010688 desc = &rnapi->rx_rcb[rx_start_idx];
Michael Chanc76949a2005-05-29 14:58:59 -070010689 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
10690 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
10691 if (opaque_key != RXD_OPAQUE_RING_STD)
10692 goto out;
10693
10694 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
10695 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
10696 goto out;
10697
10698 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) - 4;
10699 if (rx_len != tx_len)
10700 goto out;
10701
Matt Carlson21f581a2009-08-28 14:00:25 +000010702 rx_skb = tpr->rx_std_buffers[desc_idx].skb;
Michael Chanc76949a2005-05-29 14:58:59 -070010703
Matt Carlson21f581a2009-08-28 14:00:25 +000010704 map = pci_unmap_addr(&tpr->rx_std_buffers[desc_idx], mapping);
Michael Chanc76949a2005-05-29 14:58:59 -070010705 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len, PCI_DMA_FROMDEVICE);
10706
10707 for (i = 14; i < tx_len; i++) {
10708 if (*(rx_skb->data + i) != (u8) (i & 0xff))
10709 goto out;
10710 }
10711 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010712
Michael Chanc76949a2005-05-29 14:58:59 -070010713 /* tg3_free_rings will unmap and free the rx_skb */
10714out:
10715 return err;
10716}
10717
Michael Chan9f40dea2005-09-05 17:53:06 -070010718#define TG3_MAC_LOOPBACK_FAILED 1
10719#define TG3_PHY_LOOPBACK_FAILED 2
10720#define TG3_LOOPBACK_FAILED (TG3_MAC_LOOPBACK_FAILED | \
10721 TG3_PHY_LOOPBACK_FAILED)
10722
10723static int tg3_test_loopback(struct tg3 *tp)
10724{
10725 int err = 0;
Matt Carlson9936bcf2007-10-10 18:03:07 -070010726 u32 cpmuctrl = 0;
Michael Chan9f40dea2005-09-05 17:53:06 -070010727
10728 if (!netif_running(tp->dev))
10729 return TG3_LOOPBACK_FAILED;
10730
Michael Chanb9ec6c12006-07-25 16:37:27 -070010731 err = tg3_reset_hw(tp, 1);
10732 if (err)
10733 return TG3_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070010734
Matt Carlson6833c042008-11-21 17:18:59 -080010735 /* Turn off gphy autopowerdown. */
10736 if (tp->tg3_flags3 & TG3_FLG3_PHY_ENABLE_APD)
10737 tg3_phy_toggle_apd(tp, false);
10738
Matt Carlson321d32a2008-11-21 17:22:19 -080010739 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070010740 int i;
10741 u32 status;
10742
10743 tw32(TG3_CPMU_MUTEX_REQ, CPMU_MUTEX_REQ_DRIVER);
10744
10745 /* Wait for up to 40 microseconds to acquire lock. */
10746 for (i = 0; i < 4; i++) {
10747 status = tr32(TG3_CPMU_MUTEX_GNT);
10748 if (status == CPMU_MUTEX_GNT_DRIVER)
10749 break;
10750 udelay(10);
10751 }
10752
10753 if (status != CPMU_MUTEX_GNT_DRIVER)
10754 return TG3_LOOPBACK_FAILED;
10755
Matt Carlsonb2a5c192008-04-03 21:44:44 -070010756 /* Turn off link-based power management. */
Matt Carlsone8750932007-11-12 21:11:51 -080010757 cpmuctrl = tr32(TG3_CPMU_CTRL);
Matt Carlson109115e2008-05-02 16:48:59 -070010758 tw32(TG3_CPMU_CTRL,
10759 cpmuctrl & ~(CPMU_CTRL_LINK_SPEED_MODE |
10760 CPMU_CTRL_LINK_AWARE_MODE));
Matt Carlson9936bcf2007-10-10 18:03:07 -070010761 }
10762
Michael Chan9f40dea2005-09-05 17:53:06 -070010763 if (tg3_run_loopback(tp, TG3_MAC_LOOPBACK))
10764 err |= TG3_MAC_LOOPBACK_FAILED;
Matt Carlson9936bcf2007-10-10 18:03:07 -070010765
Matt Carlson321d32a2008-11-21 17:22:19 -080010766 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070010767 tw32(TG3_CPMU_CTRL, cpmuctrl);
10768
10769 /* Release the mutex */
10770 tw32(TG3_CPMU_MUTEX_GNT, CPMU_MUTEX_GNT_DRIVER);
10771 }
10772
Matt Carlsondd477002008-05-25 23:45:58 -070010773 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
10774 !(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)) {
Michael Chan9f40dea2005-09-05 17:53:06 -070010775 if (tg3_run_loopback(tp, TG3_PHY_LOOPBACK))
10776 err |= TG3_PHY_LOOPBACK_FAILED;
10777 }
10778
Matt Carlson6833c042008-11-21 17:18:59 -080010779 /* Re-enable gphy autopowerdown. */
10780 if (tp->tg3_flags3 & TG3_FLG3_PHY_ENABLE_APD)
10781 tg3_phy_toggle_apd(tp, true);
10782
Michael Chan9f40dea2005-09-05 17:53:06 -070010783 return err;
10784}
10785
Michael Chan4cafd3f2005-05-29 14:56:34 -070010786static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
10787 u64 *data)
10788{
Michael Chan566f86a2005-05-29 14:56:58 -070010789 struct tg3 *tp = netdev_priv(dev);
10790
Michael Chanbc1c7562006-03-20 17:48:03 -080010791 if (tp->link_config.phy_is_low_power)
10792 tg3_set_power_state(tp, PCI_D0);
10793
Michael Chan566f86a2005-05-29 14:56:58 -070010794 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
10795
10796 if (tg3_test_nvram(tp) != 0) {
10797 etest->flags |= ETH_TEST_FL_FAILED;
10798 data[0] = 1;
10799 }
Michael Chanca430072005-05-29 14:57:23 -070010800 if (tg3_test_link(tp) != 0) {
10801 etest->flags |= ETH_TEST_FL_FAILED;
10802 data[1] = 1;
10803 }
Michael Chana71116d2005-05-29 14:58:11 -070010804 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010805 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070010806
Michael Chanbbe832c2005-06-24 20:20:04 -070010807 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010808 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010809 tg3_netif_stop(tp);
10810 irq_sync = 1;
10811 }
10812
10813 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070010814
10815 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080010816 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070010817 tg3_halt_cpu(tp, RX_CPU_BASE);
10818 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
10819 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080010820 if (!err)
10821 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070010822
Michael Chand9ab5ad2006-03-20 22:27:35 -080010823 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)
10824 tg3_phy_reset(tp);
10825
Michael Chana71116d2005-05-29 14:58:11 -070010826 if (tg3_test_registers(tp) != 0) {
10827 etest->flags |= ETH_TEST_FL_FAILED;
10828 data[2] = 1;
10829 }
Michael Chan7942e1d2005-05-29 14:58:36 -070010830 if (tg3_test_memory(tp) != 0) {
10831 etest->flags |= ETH_TEST_FL_FAILED;
10832 data[3] = 1;
10833 }
Michael Chan9f40dea2005-09-05 17:53:06 -070010834 if ((data[4] = tg3_test_loopback(tp)) != 0)
Michael Chanc76949a2005-05-29 14:58:59 -070010835 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070010836
David S. Millerf47c11e2005-06-24 20:18:35 -070010837 tg3_full_unlock(tp);
10838
Michael Chand4bc3922005-05-29 14:59:20 -070010839 if (tg3_test_interrupt(tp) != 0) {
10840 etest->flags |= ETH_TEST_FL_FAILED;
10841 data[5] = 1;
10842 }
David S. Millerf47c11e2005-06-24 20:18:35 -070010843
10844 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070010845
Michael Chana71116d2005-05-29 14:58:11 -070010846 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10847 if (netif_running(dev)) {
10848 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010849 err2 = tg3_restart_hw(tp, 1);
10850 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070010851 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070010852 }
David S. Millerf47c11e2005-06-24 20:18:35 -070010853
10854 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010855
10856 if (irq_sync && !err2)
10857 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070010858 }
Michael Chanbc1c7562006-03-20 17:48:03 -080010859 if (tp->link_config.phy_is_low_power)
10860 tg3_set_power_state(tp, PCI_D3hot);
10861
Michael Chan4cafd3f2005-05-29 14:56:34 -070010862}
10863
Linus Torvalds1da177e2005-04-16 15:20:36 -070010864static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
10865{
10866 struct mii_ioctl_data *data = if_mii(ifr);
10867 struct tg3 *tp = netdev_priv(dev);
10868 int err;
10869
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010870 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010871 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010872 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
10873 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010874 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10875 return phy_mii_ioctl(phydev, data, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010876 }
10877
Linus Torvalds1da177e2005-04-16 15:20:36 -070010878 switch(cmd) {
10879 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000010880 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010881
10882 /* fallthru */
10883 case SIOCGMIIREG: {
10884 u32 mii_regval;
10885
10886 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
10887 break; /* We have no PHY */
10888
Michael Chanbc1c7562006-03-20 17:48:03 -080010889 if (tp->link_config.phy_is_low_power)
10890 return -EAGAIN;
10891
David S. Millerf47c11e2005-06-24 20:18:35 -070010892 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010893 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070010894 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010895
10896 data->val_out = mii_regval;
10897
10898 return err;
10899 }
10900
10901 case SIOCSMIIREG:
10902 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
10903 break; /* We have no PHY */
10904
Michael Chanbc1c7562006-03-20 17:48:03 -080010905 if (tp->link_config.phy_is_low_power)
10906 return -EAGAIN;
10907
David S. Millerf47c11e2005-06-24 20:18:35 -070010908 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010909 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070010910 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010911
10912 return err;
10913
10914 default:
10915 /* do nothing */
10916 break;
10917 }
10918 return -EOPNOTSUPP;
10919}
10920
10921#if TG3_VLAN_TAG_USED
10922static void tg3_vlan_rx_register(struct net_device *dev, struct vlan_group *grp)
10923{
10924 struct tg3 *tp = netdev_priv(dev);
10925
Matt Carlson844b3ee2009-02-25 14:23:56 +000010926 if (!netif_running(dev)) {
10927 tp->vlgrp = grp;
10928 return;
10929 }
10930
10931 tg3_netif_stop(tp);
Michael Chan29315e82006-06-29 20:12:30 -070010932
David S. Millerf47c11e2005-06-24 20:18:35 -070010933 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010934
10935 tp->vlgrp = grp;
10936
10937 /* Update RX_MODE_KEEP_VLAN_TAG bit in RX_MODE register. */
10938 __tg3_set_rx_mode(dev);
10939
Matt Carlson844b3ee2009-02-25 14:23:56 +000010940 tg3_netif_start(tp);
Michael Chan46966542007-07-11 19:47:19 -070010941
10942 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010943}
Linus Torvalds1da177e2005-04-16 15:20:36 -070010944#endif
10945
David S. Miller15f98502005-05-18 22:49:26 -070010946static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
10947{
10948 struct tg3 *tp = netdev_priv(dev);
10949
10950 memcpy(ec, &tp->coal, sizeof(*ec));
10951 return 0;
10952}
10953
Michael Chand244c892005-07-05 14:42:33 -070010954static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
10955{
10956 struct tg3 *tp = netdev_priv(dev);
10957 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
10958 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
10959
10960 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
10961 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
10962 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
10963 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
10964 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
10965 }
10966
10967 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
10968 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
10969 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
10970 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
10971 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
10972 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
10973 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
10974 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
10975 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
10976 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
10977 return -EINVAL;
10978
10979 /* No rx interrupts will be generated if both are zero */
10980 if ((ec->rx_coalesce_usecs == 0) &&
10981 (ec->rx_max_coalesced_frames == 0))
10982 return -EINVAL;
10983
10984 /* No tx interrupts will be generated if both are zero */
10985 if ((ec->tx_coalesce_usecs == 0) &&
10986 (ec->tx_max_coalesced_frames == 0))
10987 return -EINVAL;
10988
10989 /* Only copy relevant parameters, ignore all others. */
10990 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
10991 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
10992 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
10993 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
10994 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
10995 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
10996 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
10997 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
10998 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
10999
11000 if (netif_running(dev)) {
11001 tg3_full_lock(tp, 0);
11002 __tg3_set_coalesce(tp, &tp->coal);
11003 tg3_full_unlock(tp);
11004 }
11005 return 0;
11006}
11007
Jeff Garzik7282d492006-09-13 14:30:00 -040011008static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011009 .get_settings = tg3_get_settings,
11010 .set_settings = tg3_set_settings,
11011 .get_drvinfo = tg3_get_drvinfo,
11012 .get_regs_len = tg3_get_regs_len,
11013 .get_regs = tg3_get_regs,
11014 .get_wol = tg3_get_wol,
11015 .set_wol = tg3_set_wol,
11016 .get_msglevel = tg3_get_msglevel,
11017 .set_msglevel = tg3_set_msglevel,
11018 .nway_reset = tg3_nway_reset,
11019 .get_link = ethtool_op_get_link,
11020 .get_eeprom_len = tg3_get_eeprom_len,
11021 .get_eeprom = tg3_get_eeprom,
11022 .set_eeprom = tg3_set_eeprom,
11023 .get_ringparam = tg3_get_ringparam,
11024 .set_ringparam = tg3_set_ringparam,
11025 .get_pauseparam = tg3_get_pauseparam,
11026 .set_pauseparam = tg3_set_pauseparam,
11027 .get_rx_csum = tg3_get_rx_csum,
11028 .set_rx_csum = tg3_set_rx_csum,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011029 .set_tx_csum = tg3_set_tx_csum,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011030 .set_sg = ethtool_op_set_sg,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011031 .set_tso = tg3_set_tso,
Michael Chan4cafd3f2005-05-29 14:56:34 -070011032 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011033 .get_strings = tg3_get_strings,
Michael Chan4009a932005-09-05 17:52:54 -070011034 .phys_id = tg3_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011035 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070011036 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070011037 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070011038 .get_sset_count = tg3_get_sset_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011039};
11040
11041static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
11042{
Michael Chan1b277772006-03-20 22:27:48 -080011043 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011044
11045 tp->nvram_size = EEPROM_CHIP_SIZE;
11046
Matt Carlsone4f34112009-02-25 14:25:00 +000011047 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011048 return;
11049
Michael Chanb16250e2006-09-27 16:10:14 -070011050 if ((magic != TG3_EEPROM_MAGIC) &&
11051 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
11052 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011053 return;
11054
11055 /*
11056 * Size the chip by reading offsets at increasing powers of two.
11057 * When we encounter our validation signature, we know the addressing
11058 * has wrapped around, and thus have our chip size.
11059 */
Michael Chan1b277772006-03-20 22:27:48 -080011060 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011061
11062 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000011063 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011064 return;
11065
Michael Chan18201802006-03-20 22:29:15 -080011066 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011067 break;
11068
11069 cursize <<= 1;
11070 }
11071
11072 tp->nvram_size = cursize;
11073}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011074
Linus Torvalds1da177e2005-04-16 15:20:36 -070011075static void __devinit tg3_get_nvram_size(struct tg3 *tp)
11076{
11077 u32 val;
11078
Matt Carlsondf259d82009-04-20 06:57:14 +000011079 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
11080 tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080011081 return;
11082
11083 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080011084 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080011085 tg3_get_eeprom_size(tp);
11086 return;
11087 }
11088
Matt Carlson6d348f22009-02-25 14:25:52 +000011089 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011090 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000011091 /* This is confusing. We want to operate on the
11092 * 16-bit value at offset 0xf2. The tg3_nvram_read()
11093 * call will read from NVRAM and byteswap the data
11094 * according to the byteswapping settings for all
11095 * other register accesses. This ensures the data we
11096 * want will always reside in the lower 16-bits.
11097 * However, the data in NVRAM is in LE format, which
11098 * means the data from the NVRAM read will always be
11099 * opposite the endianness of the CPU. The 16-bit
11100 * byteswap then brings the data to CPU endianness.
11101 */
11102 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011103 return;
11104 }
11105 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070011106 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011107}
11108
11109static void __devinit tg3_get_nvram_info(struct tg3 *tp)
11110{
11111 u32 nvcfg1;
11112
11113 nvcfg1 = tr32(NVRAM_CFG1);
11114 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
11115 tp->tg3_flags2 |= TG3_FLG2_FLASH;
Matt Carlson8590a602009-08-28 12:29:16 +000011116 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011117 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11118 tw32(NVRAM_CFG1, nvcfg1);
11119 }
11120
Michael Chan4c987482005-09-05 17:52:38 -070011121 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750) ||
Michael Chana4e2b342005-10-26 15:46:52 -070011122 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011123 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011124 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
11125 tp->nvram_jedecnum = JEDEC_ATMEL;
11126 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
11127 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11128 break;
11129 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
11130 tp->nvram_jedecnum = JEDEC_ATMEL;
11131 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
11132 break;
11133 case FLASH_VENDOR_ATMEL_EEPROM:
11134 tp->nvram_jedecnum = JEDEC_ATMEL;
11135 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11136 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11137 break;
11138 case FLASH_VENDOR_ST:
11139 tp->nvram_jedecnum = JEDEC_ST;
11140 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
11141 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11142 break;
11143 case FLASH_VENDOR_SAIFUN:
11144 tp->nvram_jedecnum = JEDEC_SAIFUN;
11145 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
11146 break;
11147 case FLASH_VENDOR_SST_SMALL:
11148 case FLASH_VENDOR_SST_LARGE:
11149 tp->nvram_jedecnum = JEDEC_SST;
11150 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
11151 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011152 }
Matt Carlson8590a602009-08-28 12:29:16 +000011153 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011154 tp->nvram_jedecnum = JEDEC_ATMEL;
11155 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
11156 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11157 }
11158}
11159
Matt Carlsona1b950d2009-09-01 13:20:17 +000011160static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
11161{
11162 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
11163 case FLASH_5752PAGE_SIZE_256:
11164 tp->nvram_pagesize = 256;
11165 break;
11166 case FLASH_5752PAGE_SIZE_512:
11167 tp->nvram_pagesize = 512;
11168 break;
11169 case FLASH_5752PAGE_SIZE_1K:
11170 tp->nvram_pagesize = 1024;
11171 break;
11172 case FLASH_5752PAGE_SIZE_2K:
11173 tp->nvram_pagesize = 2048;
11174 break;
11175 case FLASH_5752PAGE_SIZE_4K:
11176 tp->nvram_pagesize = 4096;
11177 break;
11178 case FLASH_5752PAGE_SIZE_264:
11179 tp->nvram_pagesize = 264;
11180 break;
11181 case FLASH_5752PAGE_SIZE_528:
11182 tp->nvram_pagesize = 528;
11183 break;
11184 }
11185}
11186
Michael Chan361b4ac2005-04-21 17:11:21 -070011187static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
11188{
11189 u32 nvcfg1;
11190
11191 nvcfg1 = tr32(NVRAM_CFG1);
11192
Michael Chane6af3012005-04-21 17:12:05 -070011193 /* NVRAM protection for TPM */
11194 if (nvcfg1 & (1 << 27))
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011195 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Michael Chane6af3012005-04-21 17:12:05 -070011196
Michael Chan361b4ac2005-04-21 17:11:21 -070011197 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011198 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
11199 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
11200 tp->nvram_jedecnum = JEDEC_ATMEL;
11201 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11202 break;
11203 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11204 tp->nvram_jedecnum = JEDEC_ATMEL;
11205 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11206 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11207 break;
11208 case FLASH_5752VENDOR_ST_M45PE10:
11209 case FLASH_5752VENDOR_ST_M45PE20:
11210 case FLASH_5752VENDOR_ST_M45PE40:
11211 tp->nvram_jedecnum = JEDEC_ST;
11212 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11213 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11214 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070011215 }
11216
11217 if (tp->tg3_flags2 & TG3_FLG2_FLASH) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000011218 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000011219 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070011220 /* For eeprom, set pagesize to maximum eeprom size */
11221 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11222
11223 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11224 tw32(NVRAM_CFG1, nvcfg1);
11225 }
11226}
11227
Michael Chand3c7b882006-03-23 01:28:25 -080011228static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
11229{
Matt Carlson989a9d22007-05-05 11:51:05 -070011230 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080011231
11232 nvcfg1 = tr32(NVRAM_CFG1);
11233
11234 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070011235 if (nvcfg1 & (1 << 27)) {
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011236 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Matt Carlson989a9d22007-05-05 11:51:05 -070011237 protect = 1;
11238 }
Michael Chand3c7b882006-03-23 01:28:25 -080011239
Matt Carlson989a9d22007-05-05 11:51:05 -070011240 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11241 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011242 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11243 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11244 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11245 case FLASH_5755VENDOR_ATMEL_FLASH_5:
11246 tp->nvram_jedecnum = JEDEC_ATMEL;
11247 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11248 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11249 tp->nvram_pagesize = 264;
11250 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
11251 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
11252 tp->nvram_size = (protect ? 0x3e200 :
11253 TG3_NVRAM_SIZE_512KB);
11254 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
11255 tp->nvram_size = (protect ? 0x1f200 :
11256 TG3_NVRAM_SIZE_256KB);
11257 else
11258 tp->nvram_size = (protect ? 0x1f200 :
11259 TG3_NVRAM_SIZE_128KB);
11260 break;
11261 case FLASH_5752VENDOR_ST_M45PE10:
11262 case FLASH_5752VENDOR_ST_M45PE20:
11263 case FLASH_5752VENDOR_ST_M45PE40:
11264 tp->nvram_jedecnum = JEDEC_ST;
11265 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11266 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11267 tp->nvram_pagesize = 256;
11268 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
11269 tp->nvram_size = (protect ?
11270 TG3_NVRAM_SIZE_64KB :
11271 TG3_NVRAM_SIZE_128KB);
11272 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
11273 tp->nvram_size = (protect ?
11274 TG3_NVRAM_SIZE_64KB :
11275 TG3_NVRAM_SIZE_256KB);
11276 else
11277 tp->nvram_size = (protect ?
11278 TG3_NVRAM_SIZE_128KB :
11279 TG3_NVRAM_SIZE_512KB);
11280 break;
Michael Chand3c7b882006-03-23 01:28:25 -080011281 }
11282}
11283
Michael Chan1b277772006-03-20 22:27:48 -080011284static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
11285{
11286 u32 nvcfg1;
11287
11288 nvcfg1 = tr32(NVRAM_CFG1);
11289
11290 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011291 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
11292 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11293 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
11294 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11295 tp->nvram_jedecnum = JEDEC_ATMEL;
11296 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11297 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080011298
Matt Carlson8590a602009-08-28 12:29:16 +000011299 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11300 tw32(NVRAM_CFG1, nvcfg1);
11301 break;
11302 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11303 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11304 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11305 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11306 tp->nvram_jedecnum = JEDEC_ATMEL;
11307 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11308 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11309 tp->nvram_pagesize = 264;
11310 break;
11311 case FLASH_5752VENDOR_ST_M45PE10:
11312 case FLASH_5752VENDOR_ST_M45PE20:
11313 case FLASH_5752VENDOR_ST_M45PE40:
11314 tp->nvram_jedecnum = JEDEC_ST;
11315 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11316 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11317 tp->nvram_pagesize = 256;
11318 break;
Michael Chan1b277772006-03-20 22:27:48 -080011319 }
11320}
11321
Matt Carlson6b91fa02007-10-10 18:01:09 -070011322static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
11323{
11324 u32 nvcfg1, protect = 0;
11325
11326 nvcfg1 = tr32(NVRAM_CFG1);
11327
11328 /* NVRAM protection for TPM */
11329 if (nvcfg1 & (1 << 27)) {
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011330 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011331 protect = 1;
11332 }
11333
11334 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11335 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011336 case FLASH_5761VENDOR_ATMEL_ADB021D:
11337 case FLASH_5761VENDOR_ATMEL_ADB041D:
11338 case FLASH_5761VENDOR_ATMEL_ADB081D:
11339 case FLASH_5761VENDOR_ATMEL_ADB161D:
11340 case FLASH_5761VENDOR_ATMEL_MDB021D:
11341 case FLASH_5761VENDOR_ATMEL_MDB041D:
11342 case FLASH_5761VENDOR_ATMEL_MDB081D:
11343 case FLASH_5761VENDOR_ATMEL_MDB161D:
11344 tp->nvram_jedecnum = JEDEC_ATMEL;
11345 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11346 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11347 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
11348 tp->nvram_pagesize = 256;
11349 break;
11350 case FLASH_5761VENDOR_ST_A_M45PE20:
11351 case FLASH_5761VENDOR_ST_A_M45PE40:
11352 case FLASH_5761VENDOR_ST_A_M45PE80:
11353 case FLASH_5761VENDOR_ST_A_M45PE16:
11354 case FLASH_5761VENDOR_ST_M_M45PE20:
11355 case FLASH_5761VENDOR_ST_M_M45PE40:
11356 case FLASH_5761VENDOR_ST_M_M45PE80:
11357 case FLASH_5761VENDOR_ST_M_M45PE16:
11358 tp->nvram_jedecnum = JEDEC_ST;
11359 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11360 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11361 tp->nvram_pagesize = 256;
11362 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011363 }
11364
11365 if (protect) {
11366 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
11367 } else {
11368 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011369 case FLASH_5761VENDOR_ATMEL_ADB161D:
11370 case FLASH_5761VENDOR_ATMEL_MDB161D:
11371 case FLASH_5761VENDOR_ST_A_M45PE16:
11372 case FLASH_5761VENDOR_ST_M_M45PE16:
11373 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
11374 break;
11375 case FLASH_5761VENDOR_ATMEL_ADB081D:
11376 case FLASH_5761VENDOR_ATMEL_MDB081D:
11377 case FLASH_5761VENDOR_ST_A_M45PE80:
11378 case FLASH_5761VENDOR_ST_M_M45PE80:
11379 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
11380 break;
11381 case FLASH_5761VENDOR_ATMEL_ADB041D:
11382 case FLASH_5761VENDOR_ATMEL_MDB041D:
11383 case FLASH_5761VENDOR_ST_A_M45PE40:
11384 case FLASH_5761VENDOR_ST_M_M45PE40:
11385 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11386 break;
11387 case FLASH_5761VENDOR_ATMEL_ADB021D:
11388 case FLASH_5761VENDOR_ATMEL_MDB021D:
11389 case FLASH_5761VENDOR_ST_A_M45PE20:
11390 case FLASH_5761VENDOR_ST_M_M45PE20:
11391 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11392 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011393 }
11394 }
11395}
11396
Michael Chanb5d37722006-09-27 16:06:21 -070011397static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
11398{
11399 tp->nvram_jedecnum = JEDEC_ATMEL;
11400 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11401 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11402}
11403
Matt Carlson321d32a2008-11-21 17:22:19 -080011404static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
11405{
11406 u32 nvcfg1;
11407
11408 nvcfg1 = tr32(NVRAM_CFG1);
11409
11410 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11411 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11412 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11413 tp->nvram_jedecnum = JEDEC_ATMEL;
11414 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11415 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11416
11417 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11418 tw32(NVRAM_CFG1, nvcfg1);
11419 return;
11420 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11421 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11422 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11423 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11424 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11425 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11426 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
11427 tp->nvram_jedecnum = JEDEC_ATMEL;
11428 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11429 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11430
11431 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11432 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11433 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11434 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11435 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11436 break;
11437 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11438 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11439 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11440 break;
11441 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11442 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
11443 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11444 break;
11445 }
11446 break;
11447 case FLASH_5752VENDOR_ST_M45PE10:
11448 case FLASH_5752VENDOR_ST_M45PE20:
11449 case FLASH_5752VENDOR_ST_M45PE40:
11450 tp->nvram_jedecnum = JEDEC_ST;
11451 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11452 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11453
11454 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11455 case FLASH_5752VENDOR_ST_M45PE10:
11456 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11457 break;
11458 case FLASH_5752VENDOR_ST_M45PE20:
11459 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11460 break;
11461 case FLASH_5752VENDOR_ST_M45PE40:
11462 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11463 break;
11464 }
11465 break;
11466 default:
Matt Carlsondf259d82009-04-20 06:57:14 +000011467 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
Matt Carlson321d32a2008-11-21 17:22:19 -080011468 return;
11469 }
11470
Matt Carlsona1b950d2009-09-01 13:20:17 +000011471 tg3_nvram_get_pagesize(tp, nvcfg1);
11472 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Matt Carlson321d32a2008-11-21 17:22:19 -080011473 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011474}
11475
11476
11477static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
11478{
11479 u32 nvcfg1;
11480
11481 nvcfg1 = tr32(NVRAM_CFG1);
11482
11483 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11484 case FLASH_5717VENDOR_ATMEL_EEPROM:
11485 case FLASH_5717VENDOR_MICRO_EEPROM:
11486 tp->nvram_jedecnum = JEDEC_ATMEL;
11487 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11488 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11489
11490 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11491 tw32(NVRAM_CFG1, nvcfg1);
11492 return;
11493 case FLASH_5717VENDOR_ATMEL_MDB011D:
11494 case FLASH_5717VENDOR_ATMEL_ADB011B:
11495 case FLASH_5717VENDOR_ATMEL_ADB011D:
11496 case FLASH_5717VENDOR_ATMEL_MDB021D:
11497 case FLASH_5717VENDOR_ATMEL_ADB021B:
11498 case FLASH_5717VENDOR_ATMEL_ADB021D:
11499 case FLASH_5717VENDOR_ATMEL_45USPT:
11500 tp->nvram_jedecnum = JEDEC_ATMEL;
11501 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11502 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11503
11504 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11505 case FLASH_5717VENDOR_ATMEL_MDB021D:
11506 case FLASH_5717VENDOR_ATMEL_ADB021B:
11507 case FLASH_5717VENDOR_ATMEL_ADB021D:
11508 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11509 break;
11510 default:
11511 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11512 break;
11513 }
Matt Carlson321d32a2008-11-21 17:22:19 -080011514 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011515 case FLASH_5717VENDOR_ST_M_M25PE10:
11516 case FLASH_5717VENDOR_ST_A_M25PE10:
11517 case FLASH_5717VENDOR_ST_M_M45PE10:
11518 case FLASH_5717VENDOR_ST_A_M45PE10:
11519 case FLASH_5717VENDOR_ST_M_M25PE20:
11520 case FLASH_5717VENDOR_ST_A_M25PE20:
11521 case FLASH_5717VENDOR_ST_M_M45PE20:
11522 case FLASH_5717VENDOR_ST_A_M45PE20:
11523 case FLASH_5717VENDOR_ST_25USPT:
11524 case FLASH_5717VENDOR_ST_45USPT:
11525 tp->nvram_jedecnum = JEDEC_ST;
11526 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11527 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11528
11529 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11530 case FLASH_5717VENDOR_ST_M_M25PE20:
11531 case FLASH_5717VENDOR_ST_A_M25PE20:
11532 case FLASH_5717VENDOR_ST_M_M45PE20:
11533 case FLASH_5717VENDOR_ST_A_M45PE20:
11534 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11535 break;
11536 default:
11537 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11538 break;
11539 }
Matt Carlson321d32a2008-11-21 17:22:19 -080011540 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011541 default:
11542 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
11543 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080011544 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000011545
11546 tg3_nvram_get_pagesize(tp, nvcfg1);
11547 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
11548 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
Matt Carlson321d32a2008-11-21 17:22:19 -080011549}
11550
Linus Torvalds1da177e2005-04-16 15:20:36 -070011551/* Chips other than 5700/5701 use the NVRAM for fetching info. */
11552static void __devinit tg3_nvram_init(struct tg3 *tp)
11553{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011554 tw32_f(GRC_EEPROM_ADDR,
11555 (EEPROM_ADDR_FSM_RESET |
11556 (EEPROM_DEFAULT_CLOCK_PERIOD <<
11557 EEPROM_ADDR_CLKPERD_SHIFT)));
11558
Michael Chan9d57f012006-12-07 00:23:25 -080011559 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011560
11561 /* Enable seeprom accesses. */
11562 tw32_f(GRC_LOCAL_CTRL,
11563 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
11564 udelay(100);
11565
11566 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
11567 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
11568 tp->tg3_flags |= TG3_FLAG_NVRAM;
11569
Michael Chanec41c7d2006-01-17 02:40:55 -080011570 if (tg3_nvram_lock(tp)) {
11571 printk(KERN_WARNING PFX "%s: Cannot get nvarm lock, "
11572 "tg3_nvram_init failed.\n", tp->dev->name);
11573 return;
11574 }
Michael Chane6af3012005-04-21 17:12:05 -070011575 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011576
Matt Carlson989a9d22007-05-05 11:51:05 -070011577 tp->nvram_size = 0;
11578
Michael Chan361b4ac2005-04-21 17:11:21 -070011579 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
11580 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080011581 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
11582 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070011583 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070011584 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
11585 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080011586 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070011587 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
11588 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070011589 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
11590 tg3_get_5906_nvram_info(tp);
Matt Carlson321d32a2008-11-21 17:22:19 -080011591 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
11592 tg3_get_57780_nvram_info(tp);
Matt Carlsona1b950d2009-09-01 13:20:17 +000011593 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
11594 tg3_get_5717_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070011595 else
11596 tg3_get_nvram_info(tp);
11597
Matt Carlson989a9d22007-05-05 11:51:05 -070011598 if (tp->nvram_size == 0)
11599 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011600
Michael Chane6af3012005-04-21 17:12:05 -070011601 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080011602 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011603
11604 } else {
11605 tp->tg3_flags &= ~(TG3_FLAG_NVRAM | TG3_FLAG_NVRAM_BUFFERED);
11606
11607 tg3_get_eeprom_size(tp);
11608 }
11609}
11610
Linus Torvalds1da177e2005-04-16 15:20:36 -070011611static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
11612 u32 offset, u32 len, u8 *buf)
11613{
11614 int i, j, rc = 0;
11615 u32 val;
11616
11617 for (i = 0; i < len; i += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080011618 u32 addr;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011619 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011620
11621 addr = offset + i;
11622
11623 memcpy(&data, buf + i, 4);
11624
Matt Carlson62cedd12009-04-20 14:52:29 -070011625 /*
11626 * The SEEPROM interface expects the data to always be opposite
11627 * the native endian format. We accomplish this by reversing
11628 * all the operations that would have been performed on the
11629 * data from a call to tg3_nvram_read_be32().
11630 */
11631 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011632
11633 val = tr32(GRC_EEPROM_ADDR);
11634 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
11635
11636 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
11637 EEPROM_ADDR_READ);
11638 tw32(GRC_EEPROM_ADDR, val |
11639 (0 << EEPROM_ADDR_DEVID_SHIFT) |
11640 (addr & EEPROM_ADDR_ADDR_MASK) |
11641 EEPROM_ADDR_START |
11642 EEPROM_ADDR_WRITE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011643
Michael Chan9d57f012006-12-07 00:23:25 -080011644 for (j = 0; j < 1000; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011645 val = tr32(GRC_EEPROM_ADDR);
11646
11647 if (val & EEPROM_ADDR_COMPLETE)
11648 break;
Michael Chan9d57f012006-12-07 00:23:25 -080011649 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011650 }
11651 if (!(val & EEPROM_ADDR_COMPLETE)) {
11652 rc = -EBUSY;
11653 break;
11654 }
11655 }
11656
11657 return rc;
11658}
11659
11660/* offset and length are dword aligned */
11661static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
11662 u8 *buf)
11663{
11664 int ret = 0;
11665 u32 pagesize = tp->nvram_pagesize;
11666 u32 pagemask = pagesize - 1;
11667 u32 nvram_cmd;
11668 u8 *tmp;
11669
11670 tmp = kmalloc(pagesize, GFP_KERNEL);
11671 if (tmp == NULL)
11672 return -ENOMEM;
11673
11674 while (len) {
11675 int j;
Michael Chane6af3012005-04-21 17:12:05 -070011676 u32 phy_addr, page_off, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011677
11678 phy_addr = offset & ~pagemask;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011679
Linus Torvalds1da177e2005-04-16 15:20:36 -070011680 for (j = 0; j < pagesize; j += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000011681 ret = tg3_nvram_read_be32(tp, phy_addr + j,
11682 (__be32 *) (tmp + j));
11683 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011684 break;
11685 }
11686 if (ret)
11687 break;
11688
11689 page_off = offset & pagemask;
11690 size = pagesize;
11691 if (len < size)
11692 size = len;
11693
11694 len -= size;
11695
11696 memcpy(tmp + page_off, buf, size);
11697
11698 offset = offset + (pagesize - page_off);
11699
Michael Chane6af3012005-04-21 17:12:05 -070011700 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011701
11702 /*
11703 * Before we can erase the flash page, we need
11704 * to issue a special "write enable" command.
11705 */
11706 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
11707
11708 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
11709 break;
11710
11711 /* Erase the target page */
11712 tw32(NVRAM_ADDR, phy_addr);
11713
11714 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
11715 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
11716
11717 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
11718 break;
11719
11720 /* Issue another write enable to start the write. */
11721 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
11722
11723 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
11724 break;
11725
11726 for (j = 0; j < pagesize; j += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080011727 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011728
Al Virob9fc7dc2007-12-17 22:59:57 -080011729 data = *((__be32 *) (tmp + j));
Matt Carlsona9dc5292009-02-25 14:25:30 +000011730
Al Virob9fc7dc2007-12-17 22:59:57 -080011731 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011732
11733 tw32(NVRAM_ADDR, phy_addr + j);
11734
11735 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
11736 NVRAM_CMD_WR;
11737
11738 if (j == 0)
11739 nvram_cmd |= NVRAM_CMD_FIRST;
11740 else if (j == (pagesize - 4))
11741 nvram_cmd |= NVRAM_CMD_LAST;
11742
11743 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
11744 break;
11745 }
11746 if (ret)
11747 break;
11748 }
11749
11750 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
11751 tg3_nvram_exec_cmd(tp, nvram_cmd);
11752
11753 kfree(tmp);
11754
11755 return ret;
11756}
11757
11758/* offset and length are dword aligned */
11759static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
11760 u8 *buf)
11761{
11762 int i, ret = 0;
11763
11764 for (i = 0; i < len; i += 4, offset += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080011765 u32 page_off, phy_addr, nvram_cmd;
11766 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011767
11768 memcpy(&data, buf + i, 4);
Al Virob9fc7dc2007-12-17 22:59:57 -080011769 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011770
11771 page_off = offset % tp->nvram_pagesize;
11772
Michael Chan18201802006-03-20 22:29:15 -080011773 phy_addr = tg3_nvram_phys_addr(tp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011774
11775 tw32(NVRAM_ADDR, phy_addr);
11776
11777 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
11778
11779 if ((page_off == 0) || (i == 0))
11780 nvram_cmd |= NVRAM_CMD_FIRST;
Michael Chanf6d9a252006-04-29 19:00:24 -070011781 if (page_off == (tp->nvram_pagesize - 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011782 nvram_cmd |= NVRAM_CMD_LAST;
11783
11784 if (i == (len - 4))
11785 nvram_cmd |= NVRAM_CMD_LAST;
11786
Matt Carlson321d32a2008-11-21 17:22:19 -080011787 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
11788 !(tp->tg3_flags3 & TG3_FLG3_5755_PLUS) &&
Michael Chan4c987482005-09-05 17:52:38 -070011789 (tp->nvram_jedecnum == JEDEC_ST) &&
11790 (nvram_cmd & NVRAM_CMD_FIRST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011791
11792 if ((ret = tg3_nvram_exec_cmd(tp,
11793 NVRAM_CMD_WREN | NVRAM_CMD_GO |
11794 NVRAM_CMD_DONE)))
11795
11796 break;
11797 }
11798 if (!(tp->tg3_flags2 & TG3_FLG2_FLASH)) {
11799 /* We always do complete word writes to eeprom. */
11800 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
11801 }
11802
11803 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
11804 break;
11805 }
11806 return ret;
11807}
11808
11809/* offset and length are dword aligned */
11810static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
11811{
11812 int ret;
11813
Linus Torvalds1da177e2005-04-16 15:20:36 -070011814 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT) {
Michael Chan314fba32005-04-21 17:07:04 -070011815 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
11816 ~GRC_LCLCTRL_GPIO_OUTPUT1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011817 udelay(40);
11818 }
11819
11820 if (!(tp->tg3_flags & TG3_FLAG_NVRAM)) {
11821 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
11822 }
11823 else {
11824 u32 grc_mode;
11825
Michael Chanec41c7d2006-01-17 02:40:55 -080011826 ret = tg3_nvram_lock(tp);
11827 if (ret)
11828 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011829
Michael Chane6af3012005-04-21 17:12:05 -070011830 tg3_enable_nvram_access(tp);
11831 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011832 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011833 tw32(NVRAM_WRITE1, 0x406);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011834
11835 grc_mode = tr32(GRC_MODE);
11836 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
11837
11838 if ((tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) ||
11839 !(tp->tg3_flags2 & TG3_FLG2_FLASH)) {
11840
11841 ret = tg3_nvram_write_block_buffered(tp, offset, len,
11842 buf);
11843 }
11844 else {
11845 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
11846 buf);
11847 }
11848
11849 grc_mode = tr32(GRC_MODE);
11850 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
11851
Michael Chane6af3012005-04-21 17:12:05 -070011852 tg3_disable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011853 tg3_nvram_unlock(tp);
11854 }
11855
11856 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT) {
Michael Chan314fba32005-04-21 17:07:04 -070011857 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011858 udelay(40);
11859 }
11860
11861 return ret;
11862}
11863
11864struct subsys_tbl_ent {
11865 u16 subsys_vendor, subsys_devid;
11866 u32 phy_id;
11867};
11868
11869static struct subsys_tbl_ent subsys_id_to_phy_id[] = {
11870 /* Broadcom boards. */
11871 { PCI_VENDOR_ID_BROADCOM, 0x1644, PHY_ID_BCM5401 }, /* BCM95700A6 */
11872 { PCI_VENDOR_ID_BROADCOM, 0x0001, PHY_ID_BCM5701 }, /* BCM95701A5 */
11873 { PCI_VENDOR_ID_BROADCOM, 0x0002, PHY_ID_BCM8002 }, /* BCM95700T6 */
11874 { PCI_VENDOR_ID_BROADCOM, 0x0003, 0 }, /* BCM95700A9 */
11875 { PCI_VENDOR_ID_BROADCOM, 0x0005, PHY_ID_BCM5701 }, /* BCM95701T1 */
11876 { PCI_VENDOR_ID_BROADCOM, 0x0006, PHY_ID_BCM5701 }, /* BCM95701T8 */
11877 { PCI_VENDOR_ID_BROADCOM, 0x0007, 0 }, /* BCM95701A7 */
11878 { PCI_VENDOR_ID_BROADCOM, 0x0008, PHY_ID_BCM5701 }, /* BCM95701A10 */
11879 { PCI_VENDOR_ID_BROADCOM, 0x8008, PHY_ID_BCM5701 }, /* BCM95701A12 */
11880 { PCI_VENDOR_ID_BROADCOM, 0x0009, PHY_ID_BCM5703 }, /* BCM95703Ax1 */
11881 { PCI_VENDOR_ID_BROADCOM, 0x8009, PHY_ID_BCM5703 }, /* BCM95703Ax2 */
11882
11883 /* 3com boards. */
11884 { PCI_VENDOR_ID_3COM, 0x1000, PHY_ID_BCM5401 }, /* 3C996T */
11885 { PCI_VENDOR_ID_3COM, 0x1006, PHY_ID_BCM5701 }, /* 3C996BT */
11886 { PCI_VENDOR_ID_3COM, 0x1004, 0 }, /* 3C996SX */
11887 { PCI_VENDOR_ID_3COM, 0x1007, PHY_ID_BCM5701 }, /* 3C1000T */
11888 { PCI_VENDOR_ID_3COM, 0x1008, PHY_ID_BCM5701 }, /* 3C940BR01 */
11889
11890 /* DELL boards. */
11891 { PCI_VENDOR_ID_DELL, 0x00d1, PHY_ID_BCM5401 }, /* VIPER */
11892 { PCI_VENDOR_ID_DELL, 0x0106, PHY_ID_BCM5401 }, /* JAGUAR */
11893 { PCI_VENDOR_ID_DELL, 0x0109, PHY_ID_BCM5411 }, /* MERLOT */
11894 { PCI_VENDOR_ID_DELL, 0x010a, PHY_ID_BCM5411 }, /* SLIM_MERLOT */
11895
11896 /* Compaq boards. */
11897 { PCI_VENDOR_ID_COMPAQ, 0x007c, PHY_ID_BCM5701 }, /* BANSHEE */
11898 { PCI_VENDOR_ID_COMPAQ, 0x009a, PHY_ID_BCM5701 }, /* BANSHEE_2 */
11899 { PCI_VENDOR_ID_COMPAQ, 0x007d, 0 }, /* CHANGELING */
11900 { PCI_VENDOR_ID_COMPAQ, 0x0085, PHY_ID_BCM5701 }, /* NC7780 */
11901 { PCI_VENDOR_ID_COMPAQ, 0x0099, PHY_ID_BCM5701 }, /* NC7780_2 */
11902
11903 /* IBM boards. */
11904 { PCI_VENDOR_ID_IBM, 0x0281, 0 } /* IBM??? */
11905};
11906
11907static inline struct subsys_tbl_ent *lookup_by_subsys(struct tg3 *tp)
11908{
11909 int i;
11910
11911 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
11912 if ((subsys_id_to_phy_id[i].subsys_vendor ==
11913 tp->pdev->subsystem_vendor) &&
11914 (subsys_id_to_phy_id[i].subsys_devid ==
11915 tp->pdev->subsystem_device))
11916 return &subsys_id_to_phy_id[i];
11917 }
11918 return NULL;
11919}
11920
Michael Chan7d0c41e2005-04-21 17:06:20 -070011921static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011922{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011923 u32 val;
Michael Chancaf636c72006-03-22 01:05:31 -080011924 u16 pmcsr;
11925
11926 /* On some early chips the SRAM cannot be accessed in D3hot state,
11927 * so need make sure we're in D0.
11928 */
11929 pci_read_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, &pmcsr);
11930 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
11931 pci_write_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, pmcsr);
11932 msleep(1);
Michael Chan7d0c41e2005-04-21 17:06:20 -070011933
11934 /* Make sure register accesses (indirect or otherwise)
11935 * will function correctly.
11936 */
11937 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
11938 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011939
David S. Millerf49639e2006-06-09 11:58:36 -070011940 /* The memory arbiter has to be enabled in order for SRAM accesses
11941 * to succeed. Normally on powerup the tg3 chip firmware will make
11942 * sure it is enabled, but other entities such as system netboot
11943 * code might disable it.
11944 */
11945 val = tr32(MEMARB_MODE);
11946 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
11947
Linus Torvalds1da177e2005-04-16 15:20:36 -070011948 tp->phy_id = PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070011949 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
11950
Gary Zambranoa85feb82007-05-05 11:52:19 -070011951 /* Assume an onboard device and WOL capable by default. */
11952 tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT | TG3_FLAG_WOL_CAP;
David S. Miller72b845e2006-03-14 14:11:48 -080011953
Michael Chanb5d37722006-09-27 16:06:21 -070011954 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080011955 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Michael Chanb5d37722006-09-27 16:06:21 -070011956 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080011957 tp->tg3_flags2 |= TG3_FLG2_IS_NIC;
11958 }
Matt Carlson0527ba32007-10-10 18:03:30 -070011959 val = tr32(VCPU_CFGSHDW);
11960 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Matt Carlson8ed5d972007-05-07 00:25:49 -070011961 tp->tg3_flags |= TG3_FLAG_ASPM_WORKAROUND;
Matt Carlson0527ba32007-10-10 18:03:30 -070011962 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Matt Carlson20232762008-12-21 20:18:56 -080011963 (val & VCPU_CFGSHDW_WOL_MAGPKT))
Matt Carlson0527ba32007-10-10 18:03:30 -070011964 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
Matt Carlson05ac4cb2008-11-03 16:53:46 -080011965 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070011966 }
11967
Linus Torvalds1da177e2005-04-16 15:20:36 -070011968 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
11969 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
11970 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070011971 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070011972 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011973
11974 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
11975 tp->nic_sram_data_cfg = nic_cfg;
11976
11977 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
11978 ver >>= NIC_SRAM_DATA_VER_SHIFT;
11979 if ((GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700) &&
11980 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) &&
11981 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703) &&
11982 (ver > 0) && (ver < 0x100))
11983 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
11984
Matt Carlsona9daf362008-05-25 23:49:44 -070011985 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
11986 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
11987
Linus Torvalds1da177e2005-04-16 15:20:36 -070011988 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
11989 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
11990 eeprom_phy_serdes = 1;
11991
11992 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
11993 if (nic_phy_id != 0) {
11994 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
11995 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
11996
11997 eeprom_phy_id = (id1 >> 16) << 10;
11998 eeprom_phy_id |= (id2 & 0xfc00) << 16;
11999 eeprom_phy_id |= (id2 & 0x03ff) << 0;
12000 } else
12001 eeprom_phy_id = 0;
12002
Michael Chan7d0c41e2005-04-21 17:06:20 -070012003 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070012004 if (eeprom_phy_serdes) {
Michael Chana4e2b342005-10-26 15:46:52 -070012005 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
Michael Chan747e8f82005-07-25 12:33:22 -070012006 tp->tg3_flags2 |= TG3_FLG2_MII_SERDES;
12007 else
12008 tp->tg3_flags2 |= TG3_FLG2_PHY_SERDES;
12009 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070012010
John W. Linvillecbf46852005-04-21 17:01:29 -070012011 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012012 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
12013 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070012014 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070012015 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
12016
12017 switch (led_cfg) {
12018 default:
12019 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
12020 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12021 break;
12022
12023 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
12024 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12025 break;
12026
12027 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
12028 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070012029
12030 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
12031 * read on some older 5700/5701 bootcode.
12032 */
12033 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
12034 ASIC_REV_5700 ||
12035 GET_ASIC_REV(tp->pci_chip_rev_id) ==
12036 ASIC_REV_5701)
12037 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12038
Linus Torvalds1da177e2005-04-16 15:20:36 -070012039 break;
12040
12041 case SHASTA_EXT_LED_SHARED:
12042 tp->led_ctrl = LED_CTRL_MODE_SHARED;
12043 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
12044 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
12045 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12046 LED_CTRL_MODE_PHY_2);
12047 break;
12048
12049 case SHASTA_EXT_LED_MAC:
12050 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
12051 break;
12052
12053 case SHASTA_EXT_LED_COMBO:
12054 tp->led_ctrl = LED_CTRL_MODE_COMBO;
12055 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
12056 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12057 LED_CTRL_MODE_PHY_2);
12058 break;
12059
Stephen Hemminger855e1112008-04-16 16:37:28 -070012060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012061
12062 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
12063 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
12064 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
12065 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12066
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012067 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
12068 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080012069
Michael Chan9d26e212006-12-07 00:21:14 -080012070 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012071 tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080012072 if ((tp->pdev->subsystem_vendor ==
12073 PCI_VENDOR_ID_ARIMA) &&
12074 (tp->pdev->subsystem_device == 0x205a ||
12075 tp->pdev->subsystem_device == 0x2063))
12076 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
12077 } else {
David S. Millerf49639e2006-06-09 11:58:36 -070012078 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080012079 tp->tg3_flags2 |= TG3_FLG2_IS_NIC;
12080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012081
12082 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
12083 tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
John W. Linvillecbf46852005-04-21 17:01:29 -070012084 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012085 tp->tg3_flags2 |= TG3_FLG2_ASF_NEW_HANDSHAKE;
12086 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012087
12088 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
12089 (tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
Matt Carlson0d3031d2007-10-10 18:02:43 -070012090 tp->tg3_flags3 |= TG3_FLG3_ENABLE_APE;
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012091
Gary Zambranoa85feb82007-05-05 11:52:19 -070012092 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES &&
12093 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
12094 tp->tg3_flags &= ~TG3_FLAG_WOL_CAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012095
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070012096 if ((tp->tg3_flags & TG3_FLAG_WOL_CAP) &&
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012097 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE))
Matt Carlson0527ba32007-10-10 18:03:30 -070012098 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
12099
Linus Torvalds1da177e2005-04-16 15:20:36 -070012100 if (cfg2 & (1 << 17))
12101 tp->tg3_flags2 |= TG3_FLG2_CAPACITIVE_COUPLING;
12102
12103 /* serdes signal pre-emphasis in register 0x590 set by */
12104 /* bootcode if bit 18 is set */
12105 if (cfg2 & (1 << 18))
12106 tp->tg3_flags2 |= TG3_FLG2_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070012107
Matt Carlson321d32a2008-11-21 17:22:19 -080012108 if (((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
12109 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080012110 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
12111 tp->tg3_flags3 |= TG3_FLG3_PHY_ENABLE_APD;
12112
Matt Carlson8ed5d972007-05-07 00:25:49 -070012113 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
12114 u32 cfg3;
12115
12116 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
12117 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
12118 tp->tg3_flags |= TG3_FLAG_ASPM_WORKAROUND;
12119 }
Matt Carlsona9daf362008-05-25 23:49:44 -070012120
12121 if (cfg4 & NIC_SRAM_RGMII_STD_IBND_DISABLE)
12122 tp->tg3_flags3 |= TG3_FLG3_RGMII_STD_IBND_DISABLE;
12123 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
12124 tp->tg3_flags3 |= TG3_FLG3_RGMII_EXT_IBND_RX_EN;
12125 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
12126 tp->tg3_flags3 |= TG3_FLG3_RGMII_EXT_IBND_TX_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012127 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012128done:
12129 device_init_wakeup(&tp->pdev->dev, tp->tg3_flags & TG3_FLAG_WOL_CAP);
12130 device_set_wakeup_enable(&tp->pdev->dev,
12131 tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
Michael Chan7d0c41e2005-04-21 17:06:20 -070012132}
12133
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012134static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
12135{
12136 int i;
12137 u32 val;
12138
12139 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
12140 tw32(OTP_CTRL, cmd);
12141
12142 /* Wait for up to 1 ms for command to execute. */
12143 for (i = 0; i < 100; i++) {
12144 val = tr32(OTP_STATUS);
12145 if (val & OTP_STATUS_CMD_DONE)
12146 break;
12147 udelay(10);
12148 }
12149
12150 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
12151}
12152
12153/* Read the gphy configuration from the OTP region of the chip. The gphy
12154 * configuration is a 32-bit value that straddles the alignment boundary.
12155 * We do two 32-bit reads and then shift and merge the results.
12156 */
12157static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
12158{
12159 u32 bhalf_otp, thalf_otp;
12160
12161 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
12162
12163 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
12164 return 0;
12165
12166 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
12167
12168 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12169 return 0;
12170
12171 thalf_otp = tr32(OTP_READ_DATA);
12172
12173 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
12174
12175 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12176 return 0;
12177
12178 bhalf_otp = tr32(OTP_READ_DATA);
12179
12180 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
12181}
12182
Michael Chan7d0c41e2005-04-21 17:06:20 -070012183static int __devinit tg3_phy_probe(struct tg3 *tp)
12184{
12185 u32 hw_phy_id_1, hw_phy_id_2;
12186 u32 hw_phy_id, hw_phy_id_masked;
12187 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012188
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012189 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)
12190 return tg3_phy_init(tp);
12191
Linus Torvalds1da177e2005-04-16 15:20:36 -070012192 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010012193 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012194 */
12195 err = 0;
Matt Carlson0d3031d2007-10-10 18:02:43 -070012196 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
12197 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012198 hw_phy_id = hw_phy_id_masked = PHY_ID_INVALID;
12199 } else {
12200 /* Now read the physical PHY_ID from the chip and verify
12201 * that it is sane. If it doesn't look good, we fall back
12202 * to either the hard-coded table based PHY_ID and failing
12203 * that the value found in the eeprom area.
12204 */
12205 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
12206 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
12207
12208 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
12209 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
12210 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
12211
12212 hw_phy_id_masked = hw_phy_id & PHY_ID_MASK;
12213 }
12214
12215 if (!err && KNOWN_PHY_ID(hw_phy_id_masked)) {
12216 tp->phy_id = hw_phy_id;
12217 if (hw_phy_id_masked == PHY_ID_BCM8002)
12218 tp->tg3_flags2 |= TG3_FLG2_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070012219 else
12220 tp->tg3_flags2 &= ~TG3_FLG2_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012221 } else {
Michael Chan7d0c41e2005-04-21 17:06:20 -070012222 if (tp->phy_id != PHY_ID_INVALID) {
12223 /* Do nothing, phy ID already set up in
12224 * tg3_get_eeprom_hw_cfg().
12225 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070012226 } else {
12227 struct subsys_tbl_ent *p;
12228
12229 /* No eeprom signature? Try the hardcoded
12230 * subsys device table.
12231 */
12232 p = lookup_by_subsys(tp);
12233 if (!p)
12234 return -ENODEV;
12235
12236 tp->phy_id = p->phy_id;
12237 if (!tp->phy_id ||
12238 tp->phy_id == PHY_ID_BCM8002)
12239 tp->tg3_flags2 |= TG3_FLG2_PHY_SERDES;
12240 }
12241 }
12242
Michael Chan747e8f82005-07-25 12:33:22 -070012243 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES) &&
Matt Carlson0d3031d2007-10-10 18:02:43 -070012244 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070012245 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chan3600d912006-12-07 00:21:48 -080012246 u32 bmsr, adv_reg, tg3_ctrl, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012247
12248 tg3_readphy(tp, MII_BMSR, &bmsr);
12249 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
12250 (bmsr & BMSR_LSTATUS))
12251 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012252
Linus Torvalds1da177e2005-04-16 15:20:36 -070012253 err = tg3_phy_reset(tp);
12254 if (err)
12255 return err;
12256
12257 adv_reg = (ADVERTISE_10HALF | ADVERTISE_10FULL |
12258 ADVERTISE_100HALF | ADVERTISE_100FULL |
12259 ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
12260 tg3_ctrl = 0;
12261 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY)) {
12262 tg3_ctrl = (MII_TG3_CTRL_ADV_1000_HALF |
12263 MII_TG3_CTRL_ADV_1000_FULL);
12264 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
12265 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
12266 tg3_ctrl |= (MII_TG3_CTRL_AS_MASTER |
12267 MII_TG3_CTRL_ENABLE_AS_MASTER);
12268 }
12269
Michael Chan3600d912006-12-07 00:21:48 -080012270 mask = (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
12271 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
12272 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full);
12273 if (!tg3_copper_is_advertising_all(tp, mask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012274 tg3_writephy(tp, MII_ADVERTISE, adv_reg);
12275
12276 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY))
12277 tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
12278
12279 tg3_writephy(tp, MII_BMCR,
12280 BMCR_ANENABLE | BMCR_ANRESTART);
12281 }
12282 tg3_phy_set_wirespeed(tp);
12283
12284 tg3_writephy(tp, MII_ADVERTISE, adv_reg);
12285 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY))
12286 tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
12287 }
12288
12289skip_phy_reset:
12290 if ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5401) {
12291 err = tg3_init_5401phy_dsp(tp);
12292 if (err)
12293 return err;
12294 }
12295
12296 if (!err && ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5401)) {
12297 err = tg3_init_5401phy_dsp(tp);
12298 }
12299
Michael Chan747e8f82005-07-25 12:33:22 -070012300 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012301 tp->link_config.advertising =
12302 (ADVERTISED_1000baseT_Half |
12303 ADVERTISED_1000baseT_Full |
12304 ADVERTISED_Autoneg |
12305 ADVERTISED_FIBRE);
12306 if (tp->tg3_flags & TG3_FLAG_10_100_ONLY)
12307 tp->link_config.advertising &=
12308 ~(ADVERTISED_1000baseT_Half |
12309 ADVERTISED_1000baseT_Full);
12310
12311 return err;
12312}
12313
12314static void __devinit tg3_read_partno(struct tg3 *tp)
12315{
Matt Carlson6d348f22009-02-25 14:25:52 +000012316 unsigned char vpd_data[256]; /* in little-endian format */
Michael Chanaf2c6a42006-11-07 14:57:51 -080012317 unsigned int i;
Michael Chan1b277772006-03-20 22:27:48 -080012318 u32 magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012319
Matt Carlsondf259d82009-04-20 06:57:14 +000012320 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
12321 tg3_nvram_read(tp, 0x0, &magic))
David S. Millerf49639e2006-06-09 11:58:36 -070012322 goto out_not_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012323
Michael Chan18201802006-03-20 22:29:15 -080012324 if (magic == TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080012325 for (i = 0; i < 256; i += 4) {
12326 u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012327
Matt Carlson6d348f22009-02-25 14:25:52 +000012328 /* The data is in little-endian format in NVRAM.
12329 * Use the big-endian read routines to preserve
12330 * the byte order as it exists in NVRAM.
12331 */
12332 if (tg3_nvram_read_be32(tp, 0x100 + i, &tmp))
Michael Chan1b277772006-03-20 22:27:48 -080012333 goto out_not_found;
12334
Matt Carlson6d348f22009-02-25 14:25:52 +000012335 memcpy(&vpd_data[i], &tmp, sizeof(tmp));
Michael Chan1b277772006-03-20 22:27:48 -080012336 }
12337 } else {
12338 int vpd_cap;
12339
12340 vpd_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_VPD);
12341 for (i = 0; i < 256; i += 4) {
12342 u32 tmp, j = 0;
Al Virob9fc7dc2007-12-17 22:59:57 -080012343 __le32 v;
Michael Chan1b277772006-03-20 22:27:48 -080012344 u16 tmp16;
12345
12346 pci_write_config_word(tp->pdev, vpd_cap + PCI_VPD_ADDR,
12347 i);
12348 while (j++ < 100) {
12349 pci_read_config_word(tp->pdev, vpd_cap +
12350 PCI_VPD_ADDR, &tmp16);
12351 if (tmp16 & 0x8000)
12352 break;
12353 msleep(1);
12354 }
David S. Millerf49639e2006-06-09 11:58:36 -070012355 if (!(tmp16 & 0x8000))
12356 goto out_not_found;
12357
Michael Chan1b277772006-03-20 22:27:48 -080012358 pci_read_config_dword(tp->pdev, vpd_cap + PCI_VPD_DATA,
12359 &tmp);
Al Virob9fc7dc2007-12-17 22:59:57 -080012360 v = cpu_to_le32(tmp);
Matt Carlson6d348f22009-02-25 14:25:52 +000012361 memcpy(&vpd_data[i], &v, sizeof(v));
Michael Chan1b277772006-03-20 22:27:48 -080012362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012363 }
12364
12365 /* Now parse and find the part number. */
Michael Chanaf2c6a42006-11-07 14:57:51 -080012366 for (i = 0; i < 254; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012367 unsigned char val = vpd_data[i];
Michael Chanaf2c6a42006-11-07 14:57:51 -080012368 unsigned int block_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012369
12370 if (val == 0x82 || val == 0x91) {
12371 i = (i + 3 +
12372 (vpd_data[i + 1] +
12373 (vpd_data[i + 2] << 8)));
12374 continue;
12375 }
12376
12377 if (val != 0x90)
12378 goto out_not_found;
12379
12380 block_end = (i + 3 +
12381 (vpd_data[i + 1] +
12382 (vpd_data[i + 2] << 8)));
12383 i += 3;
Michael Chanaf2c6a42006-11-07 14:57:51 -080012384
12385 if (block_end > 256)
12386 goto out_not_found;
12387
12388 while (i < (block_end - 2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012389 if (vpd_data[i + 0] == 'P' &&
12390 vpd_data[i + 1] == 'N') {
12391 int partno_len = vpd_data[i + 2];
12392
Michael Chanaf2c6a42006-11-07 14:57:51 -080012393 i += 3;
12394 if (partno_len > 24 || (partno_len + i) > 256)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012395 goto out_not_found;
12396
12397 memcpy(tp->board_part_number,
Michael Chanaf2c6a42006-11-07 14:57:51 -080012398 &vpd_data[i], partno_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012399
12400 /* Success. */
12401 return;
12402 }
Michael Chanaf2c6a42006-11-07 14:57:51 -080012403 i += 3 + vpd_data[i + 2];
Linus Torvalds1da177e2005-04-16 15:20:36 -070012404 }
12405
12406 /* Part number not found. */
12407 goto out_not_found;
12408 }
12409
12410out_not_found:
Michael Chanb5d37722006-09-27 16:06:21 -070012411 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12412 strcpy(tp->board_part_number, "BCM95906");
Matt Carlsondf259d82009-04-20 06:57:14 +000012413 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12414 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
12415 strcpy(tp->board_part_number, "BCM57780");
12416 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12417 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
12418 strcpy(tp->board_part_number, "BCM57760");
12419 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12420 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
12421 strcpy(tp->board_part_number, "BCM57790");
Matt Carlson5e7ccf22009-08-25 10:08:42 +000012422 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12423 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
12424 strcpy(tp->board_part_number, "BCM57788");
Michael Chanb5d37722006-09-27 16:06:21 -070012425 else
12426 strcpy(tp->board_part_number, "none");
Linus Torvalds1da177e2005-04-16 15:20:36 -070012427}
12428
Matt Carlson9c8a6202007-10-21 16:16:08 -070012429static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
12430{
12431 u32 val;
12432
Matt Carlsone4f34112009-02-25 14:25:00 +000012433 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070012434 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000012435 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070012436 val != 0)
12437 return 0;
12438
12439 return 1;
12440}
12441
Matt Carlsonacd9c112009-02-25 14:26:33 +000012442static void __devinit tg3_read_bc_ver(struct tg3 *tp)
12443{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012444 u32 val, offset, start, ver_offset;
Matt Carlsonacd9c112009-02-25 14:26:33 +000012445 int i;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012446 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000012447
12448 if (tg3_nvram_read(tp, 0xc, &offset) ||
12449 tg3_nvram_read(tp, 0x4, &start))
12450 return;
12451
12452 offset = tg3_nvram_logical_addr(tp, offset);
12453
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012454 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000012455 return;
12456
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012457 if ((val & 0xfc000000) == 0x0c000000) {
12458 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000012459 return;
12460
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012461 if (val == 0)
12462 newver = true;
12463 }
12464
12465 if (newver) {
12466 if (tg3_nvram_read(tp, offset + 8, &ver_offset))
12467 return;
12468
12469 offset = offset + ver_offset - start;
12470 for (i = 0; i < 16; i += 4) {
12471 __be32 v;
12472 if (tg3_nvram_read_be32(tp, offset + i, &v))
12473 return;
12474
12475 memcpy(tp->fw_ver + i, &v, sizeof(v));
12476 }
12477 } else {
12478 u32 major, minor;
12479
12480 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
12481 return;
12482
12483 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
12484 TG3_NVM_BCVER_MAJSFT;
12485 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
12486 snprintf(&tp->fw_ver[0], 32, "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000012487 }
12488}
12489
Matt Carlsona6f6cb12009-02-25 14:27:43 +000012490static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
12491{
12492 u32 val, major, minor;
12493
12494 /* Use native endian representation */
12495 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
12496 return;
12497
12498 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
12499 TG3_NVM_HWSB_CFG1_MAJSFT;
12500 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
12501 TG3_NVM_HWSB_CFG1_MINSFT;
12502
12503 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
12504}
12505
Matt Carlsondfe00d72008-11-21 17:19:41 -080012506static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
12507{
12508 u32 offset, major, minor, build;
12509
12510 tp->fw_ver[0] = 's';
12511 tp->fw_ver[1] = 'b';
12512 tp->fw_ver[2] = '\0';
12513
12514 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
12515 return;
12516
12517 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
12518 case TG3_EEPROM_SB_REVISION_0:
12519 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
12520 break;
12521 case TG3_EEPROM_SB_REVISION_2:
12522 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
12523 break;
12524 case TG3_EEPROM_SB_REVISION_3:
12525 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
12526 break;
12527 default:
12528 return;
12529 }
12530
Matt Carlsone4f34112009-02-25 14:25:00 +000012531 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080012532 return;
12533
12534 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
12535 TG3_EEPROM_SB_EDH_BLD_SHFT;
12536 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
12537 TG3_EEPROM_SB_EDH_MAJ_SHFT;
12538 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
12539
12540 if (minor > 99 || build > 26)
12541 return;
12542
12543 snprintf(&tp->fw_ver[2], 30, " v%d.%02d", major, minor);
12544
12545 if (build > 0) {
12546 tp->fw_ver[8] = 'a' + build - 1;
12547 tp->fw_ver[9] = '\0';
12548 }
12549}
12550
Matt Carlsonacd9c112009-02-25 14:26:33 +000012551static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080012552{
12553 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000012554 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070012555
12556 for (offset = TG3_NVM_DIR_START;
12557 offset < TG3_NVM_DIR_END;
12558 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000012559 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012560 return;
12561
12562 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
12563 break;
12564 }
12565
12566 if (offset == TG3_NVM_DIR_END)
12567 return;
12568
12569 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
12570 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000012571 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012572 return;
12573
Matt Carlsone4f34112009-02-25 14:25:00 +000012574 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070012575 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000012576 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012577 return;
12578
12579 offset += val - start;
12580
Matt Carlsonacd9c112009-02-25 14:26:33 +000012581 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012582
Matt Carlsonacd9c112009-02-25 14:26:33 +000012583 tp->fw_ver[vlen++] = ',';
12584 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070012585
12586 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012587 __be32 v;
12588 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012589 return;
12590
Al Virob9fc7dc2007-12-17 22:59:57 -080012591 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012592
Matt Carlsonacd9c112009-02-25 14:26:33 +000012593 if (vlen > TG3_VER_SIZE - sizeof(v)) {
12594 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012595 break;
12596 }
12597
Matt Carlsonacd9c112009-02-25 14:26:33 +000012598 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
12599 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012600 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000012601}
12602
Matt Carlson7fd76442009-02-25 14:27:20 +000012603static void __devinit tg3_read_dash_ver(struct tg3 *tp)
12604{
12605 int vlen;
12606 u32 apedata;
12607
12608 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) ||
12609 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
12610 return;
12611
12612 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
12613 if (apedata != APE_SEG_SIG_MAGIC)
12614 return;
12615
12616 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
12617 if (!(apedata & APE_FW_STATUS_READY))
12618 return;
12619
12620 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
12621
12622 vlen = strlen(tp->fw_ver);
12623
12624 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " DASH v%d.%d.%d.%d",
12625 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
12626 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
12627 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
12628 (apedata & APE_FW_VERSION_BLDMSK));
12629}
12630
Matt Carlsonacd9c112009-02-25 14:26:33 +000012631static void __devinit tg3_read_fw_ver(struct tg3 *tp)
12632{
12633 u32 val;
12634
Matt Carlsondf259d82009-04-20 06:57:14 +000012635 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) {
12636 tp->fw_ver[0] = 's';
12637 tp->fw_ver[1] = 'b';
12638 tp->fw_ver[2] = '\0';
12639
12640 return;
12641 }
12642
Matt Carlsonacd9c112009-02-25 14:26:33 +000012643 if (tg3_nvram_read(tp, 0, &val))
12644 return;
12645
12646 if (val == TG3_EEPROM_MAGIC)
12647 tg3_read_bc_ver(tp);
12648 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
12649 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000012650 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
12651 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000012652 else
12653 return;
12654
12655 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
12656 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
12657 return;
12658
12659 tg3_read_mgmtfw_ver(tp);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012660
12661 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080012662}
12663
Michael Chan7544b092007-05-05 13:08:32 -070012664static struct pci_dev * __devinit tg3_find_peer(struct tg3 *);
12665
Linus Torvalds1da177e2005-04-16 15:20:36 -070012666static int __devinit tg3_get_invariants(struct tg3 *tp)
12667{
12668 static struct pci_device_id write_reorder_chipsets[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012669 { PCI_DEVICE(PCI_VENDOR_ID_AMD,
12670 PCI_DEVICE_ID_AMD_FE_GATE_700C) },
John W. Linvillec165b002006-07-08 13:28:53 -070012671 { PCI_DEVICE(PCI_VENDOR_ID_AMD,
12672 PCI_DEVICE_ID_AMD_8131_BRIDGE) },
Michael Chan399de502005-10-03 14:02:39 -070012673 { PCI_DEVICE(PCI_VENDOR_ID_VIA,
12674 PCI_DEVICE_ID_VIA_8385_0) },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012675 { },
12676 };
12677 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012678 u32 pci_state_reg, grc_misc_cfg;
12679 u32 val;
12680 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080012681 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012682
Linus Torvalds1da177e2005-04-16 15:20:36 -070012683 /* Force memory write invalidate off. If we leave it on,
12684 * then on 5700_BX chips we have to enable a workaround.
12685 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
12686 * to match the cacheline size. The Broadcom driver have this
12687 * workaround but turns MWI off all the times so never uses
12688 * it. This seems to suggest that the workaround is insufficient.
12689 */
12690 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
12691 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
12692 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
12693
12694 /* It is absolutely critical that TG3PCI_MISC_HOST_CTRL
12695 * has the register indirect write enable bit set before
12696 * we try to access any of the MMIO registers. It is also
12697 * critical that the PCI-X hw workaround situation is decided
12698 * before that as well.
12699 */
12700 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
12701 &misc_ctrl_reg);
12702
12703 tp->pci_chip_rev_id = (misc_ctrl_reg >>
12704 MISC_HOST_CTRL_CHIPREV_SHIFT);
Matt Carlson795d01c2007-10-07 23:28:17 -070012705 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
12706 u32 prod_id_asic_rev;
12707
Matt Carlson5001e2f2009-11-13 13:03:51 +000012708 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
12709 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
12710 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5724)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012711 pci_read_config_dword(tp->pdev,
12712 TG3PCI_GEN2_PRODID_ASICREV,
12713 &prod_id_asic_rev);
12714 else
12715 pci_read_config_dword(tp->pdev, TG3PCI_PRODID_ASICREV,
12716 &prod_id_asic_rev);
12717
Matt Carlson321d32a2008-11-21 17:22:19 -080012718 tp->pci_chip_rev_id = prod_id_asic_rev;
Matt Carlson795d01c2007-10-07 23:28:17 -070012719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012720
Michael Chanff645be2005-04-21 17:09:53 -070012721 /* Wrong chip ID in 5752 A0. This code can be removed later
12722 * as A0 is not in production.
12723 */
12724 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
12725 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
12726
Michael Chan68929142005-08-09 20:17:14 -070012727 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
12728 * we need to disable memory and use config. cycles
12729 * only to access all registers. The 5702/03 chips
12730 * can mistakenly decode the special cycles from the
12731 * ICH chipsets as memory write cycles, causing corruption
12732 * of register and memory space. Only certain ICH bridges
12733 * will drive special cycles with non-zero data during the
12734 * address phase which can fall within the 5703's address
12735 * range. This is not an ICH bug as the PCI spec allows
12736 * non-zero address during special cycles. However, only
12737 * these ICH bridges are known to drive non-zero addresses
12738 * during special cycles.
12739 *
12740 * Since special cycles do not cross PCI bridges, we only
12741 * enable this workaround if the 5703 is on the secondary
12742 * bus of these ICH bridges.
12743 */
12744 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
12745 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
12746 static struct tg3_dev_id {
12747 u32 vendor;
12748 u32 device;
12749 u32 rev;
12750 } ich_chipsets[] = {
12751 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
12752 PCI_ANY_ID },
12753 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
12754 PCI_ANY_ID },
12755 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
12756 0xa },
12757 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
12758 PCI_ANY_ID },
12759 { },
12760 };
12761 struct tg3_dev_id *pci_id = &ich_chipsets[0];
12762 struct pci_dev *bridge = NULL;
12763
12764 while (pci_id->vendor != 0) {
12765 bridge = pci_get_device(pci_id->vendor, pci_id->device,
12766 bridge);
12767 if (!bridge) {
12768 pci_id++;
12769 continue;
12770 }
12771 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070012772 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070012773 continue;
12774 }
12775 if (bridge->subordinate &&
12776 (bridge->subordinate->number ==
12777 tp->pdev->bus->number)) {
12778
12779 tp->tg3_flags2 |= TG3_FLG2_ICH_WORKAROUND;
12780 pci_dev_put(bridge);
12781 break;
12782 }
12783 }
12784 }
12785
Matt Carlson41588ba2008-04-19 18:12:33 -070012786 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
12787 static struct tg3_dev_id {
12788 u32 vendor;
12789 u32 device;
12790 } bridge_chipsets[] = {
12791 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
12792 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
12793 { },
12794 };
12795 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
12796 struct pci_dev *bridge = NULL;
12797
12798 while (pci_id->vendor != 0) {
12799 bridge = pci_get_device(pci_id->vendor,
12800 pci_id->device,
12801 bridge);
12802 if (!bridge) {
12803 pci_id++;
12804 continue;
12805 }
12806 if (bridge->subordinate &&
12807 (bridge->subordinate->number <=
12808 tp->pdev->bus->number) &&
12809 (bridge->subordinate->subordinate >=
12810 tp->pdev->bus->number)) {
12811 tp->tg3_flags3 |= TG3_FLG3_5701_DMA_BUG;
12812 pci_dev_put(bridge);
12813 break;
12814 }
12815 }
12816 }
12817
Michael Chan4a29cc22006-03-19 13:21:12 -080012818 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
12819 * DMA addresses > 40-bit. This bridge may have other additional
12820 * 57xx devices behind it in some 4-port NIC designs for example.
12821 * Any tg3 device found behind the bridge will also need the 40-bit
12822 * DMA workaround.
12823 */
Michael Chana4e2b342005-10-26 15:46:52 -070012824 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
12825 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
12826 tp->tg3_flags2 |= TG3_FLG2_5780_CLASS;
Michael Chan4a29cc22006-03-19 13:21:12 -080012827 tp->tg3_flags |= TG3_FLAG_40BIT_DMA_BUG;
Michael Chan4cf78e42005-07-25 12:29:19 -070012828 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Michael Chana4e2b342005-10-26 15:46:52 -070012829 }
Michael Chan4a29cc22006-03-19 13:21:12 -080012830 else {
12831 struct pci_dev *bridge = NULL;
12832
12833 do {
12834 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
12835 PCI_DEVICE_ID_SERVERWORKS_EPB,
12836 bridge);
12837 if (bridge && bridge->subordinate &&
12838 (bridge->subordinate->number <=
12839 tp->pdev->bus->number) &&
12840 (bridge->subordinate->subordinate >=
12841 tp->pdev->bus->number)) {
12842 tp->tg3_flags |= TG3_FLAG_40BIT_DMA_BUG;
12843 pci_dev_put(bridge);
12844 break;
12845 }
12846 } while (bridge);
12847 }
Michael Chan4cf78e42005-07-25 12:29:19 -070012848
Linus Torvalds1da177e2005-04-16 15:20:36 -070012849 /* Initialize misc host control in PCI block. */
12850 tp->misc_host_ctrl |= (misc_ctrl_reg &
12851 MISC_HOST_CTRL_CHIPREV);
12852 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
12853 tp->misc_host_ctrl);
12854
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012855 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
12856 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
12857 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Michael Chan7544b092007-05-05 13:08:32 -070012858 tp->pdev_peer = tg3_find_peer(tp);
12859
Matt Carlson321d32a2008-11-21 17:22:19 -080012860 /* Intentionally exclude ASIC_REV_5906 */
12861 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Michael Chand9ab5ad2006-03-20 22:27:35 -080012862 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070012863 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070012864 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070012865 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012866 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
12867 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson321d32a2008-11-21 17:22:19 -080012868 tp->tg3_flags3 |= TG3_FLG3_5755_PLUS;
12869
12870 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
12871 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Michael Chanb5d37722006-09-27 16:06:21 -070012872 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080012873 (tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
Michael Chana4e2b342005-10-26 15:46:52 -070012874 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
John W. Linville6708e5c2005-04-21 17:00:52 -070012875 tp->tg3_flags2 |= TG3_FLG2_5750_PLUS;
12876
John W. Linville1b440c562005-04-21 17:03:18 -070012877 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) ||
12878 (tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
12879 tp->tg3_flags2 |= TG3_FLG2_5705_PLUS;
12880
Matt Carlson027455a2008-12-21 20:19:30 -080012881 /* 5700 B0 chips do not support checksumming correctly due
12882 * to hardware bugs.
12883 */
12884 if (tp->pci_chip_rev_id == CHIPREV_ID_5700_B0)
12885 tp->tg3_flags |= TG3_FLAG_BROKEN_CHECKSUMS;
12886 else {
12887 tp->tg3_flags |= TG3_FLAG_RX_CHECKSUMS;
12888 tp->dev->features |= NETIF_F_IP_CSUM | NETIF_F_SG;
12889 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
12890 tp->dev->features |= NETIF_F_IPV6_CSUM;
12891 }
12892
Matt Carlson507399f2009-11-13 13:03:37 +000012893 /* Determine TSO capabilities */
Matt Carlsone849cdc2009-11-13 13:03:38 +000012894 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
12895 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_3;
12896 else if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
12897 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlson507399f2009-11-13 13:03:37 +000012898 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_2;
12899 else if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
12900 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_1 | TG3_FLG2_TSO_BUG;
12901 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
12902 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
12903 tp->tg3_flags2 &= ~TG3_FLG2_TSO_BUG;
12904 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
12905 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
12906 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
12907 tp->tg3_flags2 |= TG3_FLG2_TSO_BUG;
12908 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
12909 tp->fw_needed = FIRMWARE_TG3TSO5;
12910 else
12911 tp->fw_needed = FIRMWARE_TG3TSO;
12912 }
12913
12914 tp->irq_max = 1;
12915
Michael Chan5a6f3072006-03-20 22:28:05 -080012916 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
Michael Chan7544b092007-05-05 13:08:32 -070012917 tp->tg3_flags |= TG3_FLAG_SUPPORT_MSI;
12918 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
12919 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
12920 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
12921 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
12922 tp->pdev_peer == tp->pdev))
12923 tp->tg3_flags &= ~TG3_FLAG_SUPPORT_MSI;
12924
Matt Carlson321d32a2008-11-21 17:22:19 -080012925 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070012926 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanfcfa0a32006-03-20 22:28:41 -080012927 tp->tg3_flags2 |= TG3_FLG2_1SHOT_MSI;
Michael Chan52c0fd82006-06-29 20:15:54 -070012928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012929
Matt Carlson507399f2009-11-13 13:03:37 +000012930 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
12931 tp->tg3_flags |= TG3_FLAG_SUPPORT_MSIX;
12932 tp->irq_max = TG3_IRQ_MAX_VECS;
12933 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012934 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000012935
Matt Carlson615774f2009-11-13 13:03:39 +000012936 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
12937 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12938 tp->tg3_flags3 |= TG3_FLG3_SHORT_DMA_BUG;
12939 else if (!(tp->tg3_flags3 & TG3_FLG3_5755_PLUS)) {
12940 tp->tg3_flags3 |= TG3_FLG3_4G_DMA_BNDRY_BUG;
12941 tp->tg3_flags3 |= TG3_FLG3_40BIT_DMA_LIMIT_BUG;
Matt Carlson0e1406d2009-11-02 12:33:33 +000012942 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012943
Matt Carlsonf51f3562008-05-25 23:45:08 -070012944 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012945 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
12946 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson8f666b02009-08-28 13:58:24 +000012947 tp->tg3_flags |= TG3_FLAG_JUMBO_CAPABLE;
Michael Chan0f893dc2005-07-25 12:30:38 -070012948
Matt Carlson52f44902008-11-21 17:17:04 -080012949 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
12950 &pci_state_reg);
12951
Matt Carlson5e7dfd02008-11-21 17:18:16 -080012952 tp->pcie_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_EXP);
12953 if (tp->pcie_cap != 0) {
12954 u16 lnkctl;
12955
Linus Torvalds1da177e2005-04-16 15:20:36 -070012956 tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS;
Matt Carlson5f5c51e2007-11-12 21:19:37 -080012957
12958 pcie_set_readrq(tp->pdev, 4096);
12959
Matt Carlson5e7dfd02008-11-21 17:18:16 -080012960 pci_read_config_word(tp->pdev,
12961 tp->pcie_cap + PCI_EXP_LNKCTL,
12962 &lnkctl);
12963 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
12964 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanc7835a72006-11-15 21:14:42 -080012965 tp->tg3_flags2 &= ~TG3_FLG2_HW_TSO_2;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080012966 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080012967 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000012968 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
12969 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Matt Carlson5e7dfd02008-11-21 17:18:16 -080012970 tp->tg3_flags3 |= TG3_FLG3_CLKREQ_BUG;
Michael Chanc7835a72006-11-15 21:14:42 -080012971 }
Matt Carlson52f44902008-11-21 17:17:04 -080012972 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Matt Carlsonfcb389d2008-11-03 16:55:44 -080012973 tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS;
Matt Carlson52f44902008-11-21 17:17:04 -080012974 } else if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
12975 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
12976 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
12977 if (!tp->pcix_cap) {
12978 printk(KERN_ERR PFX "Cannot find PCI-X "
12979 "capability, aborting.\n");
12980 return -EIO;
12981 }
12982
12983 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
12984 tp->tg3_flags |= TG3_FLAG_PCIX_MODE;
12985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012986
Michael Chan399de502005-10-03 14:02:39 -070012987 /* If we have an AMD 762 or VIA K8T800 chipset, write
12988 * reordering to the mailbox registers done by the host
12989 * controller can cause major troubles. We read back from
12990 * every mailbox register write to force the writes to be
12991 * posted to the chip in order.
12992 */
12993 if (pci_dev_present(write_reorder_chipsets) &&
12994 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
12995 tp->tg3_flags |= TG3_FLAG_MBOX_WRITE_REORDER;
12996
Matt Carlson69fc4052008-12-21 20:19:57 -080012997 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
12998 &tp->pci_cacheline_sz);
12999 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13000 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013001 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
13002 tp->pci_lat_timer < 64) {
13003 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080013004 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13005 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013006 }
13007
Matt Carlson52f44902008-11-21 17:17:04 -080013008 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
13009 /* 5700 BX chips need to have their TX producer index
13010 * mailboxes written twice to workaround a bug.
13011 */
13012 tp->tg3_flags |= TG3_FLAG_TXD_MBOX_HWBUG;
Matt Carlson9974a352007-10-07 23:27:28 -070013013
Matt Carlson52f44902008-11-21 17:17:04 -080013014 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013015 *
13016 * The workaround is to use indirect register accesses
13017 * for all chip writes not to mailbox registers.
13018 */
Matt Carlson52f44902008-11-21 17:17:04 -080013019 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013020 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013021
13022 tp->tg3_flags |= TG3_FLAG_PCIX_TARGET_HWBUG;
13023
13024 /* The chip can have it's power management PCI config
13025 * space registers clobbered due to this bug.
13026 * So explicitly force the chip into D0 here.
13027 */
Matt Carlson9974a352007-10-07 23:27:28 -070013028 pci_read_config_dword(tp->pdev,
13029 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013030 &pm_reg);
13031 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
13032 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070013033 pci_write_config_dword(tp->pdev,
13034 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013035 pm_reg);
13036
13037 /* Also, force SERR#/PERR# in PCI command. */
13038 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13039 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
13040 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13041 }
13042 }
13043
Linus Torvalds1da177e2005-04-16 15:20:36 -070013044 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
13045 tp->tg3_flags |= TG3_FLAG_PCI_HIGH_SPEED;
13046 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
13047 tp->tg3_flags |= TG3_FLAG_PCI_32BIT;
13048
13049 /* Chip-specific fixup from Broadcom driver */
13050 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
13051 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
13052 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
13053 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
13054 }
13055
Michael Chan1ee582d2005-08-09 20:16:46 -070013056 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070013057 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013058 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070013059 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070013060 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013061 tp->write32_tx_mbox = tg3_write32;
13062 tp->write32_rx_mbox = tg3_write32;
13063
13064 /* Various workaround register access methods */
13065 if (tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG)
13066 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013067 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
13068 ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
13069 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
13070 /*
13071 * Back to back register writes can cause problems on these
13072 * chips, the workaround is to read back all reg writes
13073 * except those to mailbox regs.
13074 *
13075 * See tg3_write_indirect_reg32().
13076 */
Michael Chan1ee582d2005-08-09 20:16:46 -070013077 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013078 }
13079
Michael Chan1ee582d2005-08-09 20:16:46 -070013080 if ((tp->tg3_flags & TG3_FLAG_TXD_MBOX_HWBUG) ||
13081 (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)) {
13082 tp->write32_tx_mbox = tg3_write32_tx_mbox;
13083 if (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)
13084 tp->write32_rx_mbox = tg3_write_flush_reg32;
13085 }
Michael Chan20094932005-08-09 20:16:32 -070013086
Michael Chan68929142005-08-09 20:17:14 -070013087 if (tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND) {
13088 tp->read32 = tg3_read_indirect_reg32;
13089 tp->write32 = tg3_write_indirect_reg32;
13090 tp->read32_mbox = tg3_read_indirect_mbox;
13091 tp->write32_mbox = tg3_write_indirect_mbox;
13092 tp->write32_tx_mbox = tg3_write_indirect_mbox;
13093 tp->write32_rx_mbox = tg3_write_indirect_mbox;
13094
13095 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070013096 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070013097
13098 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13099 pci_cmd &= ~PCI_COMMAND_MEMORY;
13100 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13101 }
Michael Chanb5d37722006-09-27 16:06:21 -070013102 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
13103 tp->read32_mbox = tg3_read32_mbox_5906;
13104 tp->write32_mbox = tg3_write32_mbox_5906;
13105 tp->write32_tx_mbox = tg3_write32_mbox_5906;
13106 tp->write32_rx_mbox = tg3_write32_mbox_5906;
13107 }
Michael Chan68929142005-08-09 20:17:14 -070013108
Michael Chanbbadf502006-04-06 21:46:34 -070013109 if (tp->write32 == tg3_write_indirect_reg32 ||
13110 ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) &&
13111 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070013112 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Michael Chanbbadf502006-04-06 21:46:34 -070013113 tp->tg3_flags |= TG3_FLAG_SRAM_USE_CONFIG;
13114
Michael Chan7d0c41e2005-04-21 17:06:20 -070013115 /* Get eeprom hw config before calling tg3_set_power_state().
Michael Chan9d26e212006-12-07 00:21:14 -080013116 * In particular, the TG3_FLG2_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070013117 * determined before calling tg3_set_power_state() so that
13118 * we know whether or not to switch out of Vaux power.
13119 * When the flag is set, it means that GPIO1 is used for eeprom
13120 * write protect and also implies that it is a LOM where GPIOs
13121 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013122 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070013123 tg3_get_eeprom_hw_cfg(tp);
13124
Matt Carlson0d3031d2007-10-10 18:02:43 -070013125 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
13126 /* Allow reads and writes to the
13127 * APE register and memory space.
13128 */
13129 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
13130 PCISTATE_ALLOW_APE_SHMEM_WR;
13131 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
13132 pci_state_reg);
13133 }
13134
Matt Carlson9936bcf2007-10-10 18:03:07 -070013135 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013136 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013137 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013138 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
13139 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlsond30cdd22007-10-07 23:28:35 -070013140 tp->tg3_flags |= TG3_FLAG_CPMU_PRESENT;
13141
Michael Chan314fba32005-04-21 17:07:04 -070013142 /* Set up tp->grc_local_ctrl before calling tg3_set_power_state().
13143 * GPIO1 driven high will bring 5700's external PHY out of reset.
13144 * It is also used as eeprom write protect on LOMs.
13145 */
13146 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
13147 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
13148 (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT))
13149 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
13150 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070013151 /* Unused GPIO3 must be driven as output on 5752 because there
13152 * are no pull-up resistors on unused GPIO pins.
13153 */
13154 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
13155 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070013156
Matt Carlson321d32a2008-11-21 17:22:19 -080013157 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
13158 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Michael Chanaf36e6b2006-03-23 01:28:06 -080013159 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
13160
Matt Carlson8d519ab2009-04-20 06:58:01 +000013161 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
13162 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070013163 /* Turn off the debug UART. */
13164 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
13165 if (tp->tg3_flags2 & TG3_FLG2_IS_NIC)
13166 /* Keep VMain power. */
13167 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
13168 GRC_LCLCTRL_GPIO_OUTPUT0;
13169 }
13170
Linus Torvalds1da177e2005-04-16 15:20:36 -070013171 /* Force the chip into D0. */
Michael Chanbc1c7562006-03-20 17:48:03 -080013172 err = tg3_set_power_state(tp, PCI_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013173 if (err) {
13174 printk(KERN_ERR PFX "(%s) transition to D0 failed\n",
13175 pci_name(tp->pdev));
13176 return err;
13177 }
13178
Linus Torvalds1da177e2005-04-16 15:20:36 -070013179 /* Derive initial jumbo mode from MTU assigned in
13180 * ether_setup() via the alloc_etherdev() call
13181 */
Michael Chan0f893dc2005-07-25 12:30:38 -070013182 if (tp->dev->mtu > ETH_DATA_LEN &&
Michael Chana4e2b342005-10-26 15:46:52 -070013183 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Michael Chan0f893dc2005-07-25 12:30:38 -070013184 tp->tg3_flags |= TG3_FLAG_JUMBO_RING_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013185
13186 /* Determine WakeOnLan speed to use. */
13187 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13188 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
13189 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
13190 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
13191 tp->tg3_flags &= ~(TG3_FLAG_WOL_SPEED_100MB);
13192 } else {
13193 tp->tg3_flags |= TG3_FLAG_WOL_SPEED_100MB;
13194 }
13195
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013196 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
13197 tp->tg3_flags3 |= TG3_FLG3_PHY_IS_FET;
13198
Linus Torvalds1da177e2005-04-16 15:20:36 -070013199 /* A few boards don't want Ethernet@WireSpeed phy feature */
13200 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
13201 ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
13202 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070013203 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013204 (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) ||
Michael Chan747e8f82005-07-25 12:33:22 -070013205 (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013206 tp->tg3_flags2 |= TG3_FLG2_NO_ETH_WIRE_SPEED;
13207
13208 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
13209 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
13210 tp->tg3_flags2 |= TG3_FLG2_PHY_ADC_BUG;
13211 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
13212 tp->tg3_flags2 |= TG3_FLG2_PHY_5704_A0_BUG;
13213
Matt Carlson321d32a2008-11-21 17:22:19 -080013214 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013215 !(tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080013216 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013217 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
13218 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717) {
Michael Chanc424cb22006-04-29 18:56:34 -070013219 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013220 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013221 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
13222 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080013223 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
13224 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
13225 tp->tg3_flags2 |= TG3_FLG2_PHY_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080013226 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
13227 tp->tg3_flags2 |= TG3_FLG2_PHY_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080013228 } else
Michael Chanc424cb22006-04-29 18:56:34 -070013229 tp->tg3_flags2 |= TG3_FLG2_PHY_BER_BUG;
13230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013231
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013232 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
13233 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
13234 tp->phy_otp = tg3_read_otp_phycfg(tp);
13235 if (tp->phy_otp == 0)
13236 tp->phy_otp = TG3_OTP_DEFAULT;
13237 }
13238
Matt Carlsonf51f3562008-05-25 23:45:08 -070013239 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT)
Matt Carlson8ef21422008-05-02 16:47:53 -070013240 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
13241 else
13242 tp->mi_mode = MAC_MI_MODE_BASE;
13243
Linus Torvalds1da177e2005-04-16 15:20:36 -070013244 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013245 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
13246 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
13247 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
13248
Matt Carlson321d32a2008-11-21 17:22:19 -080013249 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
13250 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson57e69832008-05-25 23:48:31 -070013251 tp->tg3_flags3 |= TG3_FLG3_USE_PHYLIB;
13252
Matt Carlson158d7ab2008-05-29 01:37:54 -070013253 err = tg3_mdio_init(tp);
13254 if (err)
13255 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013256
13257 /* Initialize data/descriptor byte/word swapping. */
13258 val = tr32(GRC_MODE);
13259 val &= GRC_MODE_HOST_STACKUP;
13260 tw32(GRC_MODE, val | tp->grc_mode);
13261
13262 tg3_switch_clocks(tp);
13263
13264 /* Clear this out for sanity. */
13265 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
13266
13267 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
13268 &pci_state_reg);
13269 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
13270 (tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG) == 0) {
13271 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
13272
13273 if (chiprevid == CHIPREV_ID_5701_A0 ||
13274 chiprevid == CHIPREV_ID_5701_B0 ||
13275 chiprevid == CHIPREV_ID_5701_B2 ||
13276 chiprevid == CHIPREV_ID_5701_B5) {
13277 void __iomem *sram_base;
13278
13279 /* Write some dummy words into the SRAM status block
13280 * area, see if it reads back correctly. If the return
13281 * value is bad, force enable the PCIX workaround.
13282 */
13283 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
13284
13285 writel(0x00000000, sram_base);
13286 writel(0x00000000, sram_base + 4);
13287 writel(0xffffffff, sram_base + 4);
13288 if (readl(sram_base) != 0x00000000)
13289 tp->tg3_flags |= TG3_FLAG_PCIX_TARGET_HWBUG;
13290 }
13291 }
13292
13293 udelay(50);
13294 tg3_nvram_init(tp);
13295
13296 grc_misc_cfg = tr32(GRC_MISC_CFG);
13297 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
13298
Linus Torvalds1da177e2005-04-16 15:20:36 -070013299 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
13300 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
13301 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
13302 tp->tg3_flags2 |= TG3_FLG2_IS_5788;
13303
David S. Millerfac9b832005-05-18 22:46:34 -070013304 if (!(tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
13305 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700))
13306 tp->tg3_flags |= TG3_FLAG_TAGGED_STATUS;
13307 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) {
13308 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
13309 HOSTCC_MODE_CLRTICK_TXBD);
13310
13311 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
13312 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13313 tp->misc_host_ctrl);
13314 }
13315
Matt Carlson3bda1252008-08-15 14:08:22 -070013316 /* Preserve the APE MAC_MODE bits */
13317 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
13318 tp->mac_mode = tr32(MAC_MODE) |
13319 MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
13320 else
13321 tp->mac_mode = TG3_DEF_MAC_MODE;
13322
Linus Torvalds1da177e2005-04-16 15:20:36 -070013323 /* these are limited to 10/100 only */
13324 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
13325 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
13326 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
13327 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
13328 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
13329 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
13330 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
13331 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
13332 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
Michael Chan676917d2006-12-07 00:20:22 -080013333 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
13334 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013335 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013336 (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013337 tp->tg3_flags |= TG3_FLAG_10_100_ONLY;
13338
13339 err = tg3_phy_probe(tp);
13340 if (err) {
13341 printk(KERN_ERR PFX "(%s) phy probe failed, err %d\n",
13342 pci_name(tp->pdev), err);
13343 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013344 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013345 }
13346
13347 tg3_read_partno(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080013348 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013349
13350 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
13351 tp->tg3_flags &= ~TG3_FLAG_USE_MI_INTERRUPT;
13352 } else {
13353 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
13354 tp->tg3_flags |= TG3_FLAG_USE_MI_INTERRUPT;
13355 else
13356 tp->tg3_flags &= ~TG3_FLAG_USE_MI_INTERRUPT;
13357 }
13358
13359 /* 5700 {AX,BX} chips have a broken status block link
13360 * change bit implementation, so we must use the
13361 * status register in those cases.
13362 */
13363 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
13364 tp->tg3_flags |= TG3_FLAG_USE_LINKCHG_REG;
13365 else
13366 tp->tg3_flags &= ~TG3_FLAG_USE_LINKCHG_REG;
13367
13368 /* The led_ctrl is set during tg3_phy_probe, here we might
13369 * have to force the link status polling mechanism based
13370 * upon subsystem IDs.
13371 */
13372 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070013373 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070013374 !(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)) {
13375 tp->tg3_flags |= (TG3_FLAG_USE_MI_INTERRUPT |
13376 TG3_FLAG_USE_LINKCHG_REG);
13377 }
13378
13379 /* For all SERDES we poll the MAC status register. */
13380 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
13381 tp->tg3_flags |= TG3_FLAG_POLL_SERDES;
13382 else
13383 tp->tg3_flags &= ~TG3_FLAG_POLL_SERDES;
13384
Matt Carlsonad829262008-11-21 17:16:16 -080013385 tp->rx_offset = NET_IP_ALIGN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013386 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
13387 (tp->tg3_flags & TG3_FLAG_PCIX_MODE) != 0)
13388 tp->rx_offset = 0;
13389
Michael Chanf92905d2006-06-29 20:14:29 -070013390 tp->rx_std_max_post = TG3_RX_RING_SIZE;
13391
13392 /* Increment the rx prod index on the rx std ring by at most
13393 * 8 for these chips to workaround hw errata.
13394 */
13395 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13396 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
13397 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
13398 tp->rx_std_max_post = 8;
13399
Matt Carlson8ed5d972007-05-07 00:25:49 -070013400 if (tp->tg3_flags & TG3_FLAG_ASPM_WORKAROUND)
13401 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
13402 PCIE_PWR_MGMT_L1_THRESH_MSK;
13403
Linus Torvalds1da177e2005-04-16 15:20:36 -070013404 return err;
13405}
13406
David S. Miller49b6e95f2007-03-29 01:38:42 -070013407#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070013408static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
13409{
13410 struct net_device *dev = tp->dev;
13411 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070013412 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070013413 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070013414 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013415
David S. Miller49b6e95f2007-03-29 01:38:42 -070013416 addr = of_get_property(dp, "local-mac-address", &len);
13417 if (addr && len == 6) {
13418 memcpy(dev->dev_addr, addr, 6);
13419 memcpy(dev->perm_addr, dev->dev_addr, 6);
13420 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013421 }
13422 return -ENODEV;
13423}
13424
13425static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
13426{
13427 struct net_device *dev = tp->dev;
13428
13429 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070013430 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013431 return 0;
13432}
13433#endif
13434
13435static int __devinit tg3_get_device_address(struct tg3 *tp)
13436{
13437 struct net_device *dev = tp->dev;
13438 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080013439 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013440
David S. Miller49b6e95f2007-03-29 01:38:42 -070013441#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070013442 if (!tg3_get_macaddr_sparc(tp))
13443 return 0;
13444#endif
13445
13446 mac_offset = 0x7c;
David S. Millerf49639e2006-06-09 11:58:36 -070013447 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) ||
Michael Chana4e2b342005-10-26 15:46:52 -070013448 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013449 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
13450 mac_offset = 0xcc;
13451 if (tg3_nvram_lock(tp))
13452 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
13453 else
13454 tg3_nvram_unlock(tp);
Matt Carlsona1b950d2009-09-01 13:20:17 +000013455 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
13456 if (tr32(TG3_CPMU_STATUS) & TG3_CPMU_STATUS_PCIE_FUNC)
13457 mac_offset = 0xcc;
13458 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070013459 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013460
13461 /* First try to get it from MAC address mailbox. */
13462 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
13463 if ((hi >> 16) == 0x484b) {
13464 dev->dev_addr[0] = (hi >> 8) & 0xff;
13465 dev->dev_addr[1] = (hi >> 0) & 0xff;
13466
13467 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
13468 dev->dev_addr[2] = (lo >> 24) & 0xff;
13469 dev->dev_addr[3] = (lo >> 16) & 0xff;
13470 dev->dev_addr[4] = (lo >> 8) & 0xff;
13471 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013472
Michael Chan008652b2006-03-27 23:14:53 -080013473 /* Some old bootcode may report a 0 MAC address in SRAM */
13474 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
13475 }
13476 if (!addr_ok) {
13477 /* Next, try NVRAM. */
Matt Carlsondf259d82009-04-20 06:57:14 +000013478 if (!(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) &&
13479 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000013480 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070013481 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
13482 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080013483 }
13484 /* Finally just fetch it out of the MAC control regs. */
13485 else {
13486 hi = tr32(MAC_ADDR_0_HIGH);
13487 lo = tr32(MAC_ADDR_0_LOW);
13488
13489 dev->dev_addr[5] = lo & 0xff;
13490 dev->dev_addr[4] = (lo >> 8) & 0xff;
13491 dev->dev_addr[3] = (lo >> 16) & 0xff;
13492 dev->dev_addr[2] = (lo >> 24) & 0xff;
13493 dev->dev_addr[1] = hi & 0xff;
13494 dev->dev_addr[0] = (hi >> 8) & 0xff;
13495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013496 }
13497
13498 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070013499#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070013500 if (!tg3_get_default_macaddr_sparc(tp))
13501 return 0;
13502#endif
13503 return -EINVAL;
13504 }
John W. Linville2ff43692005-09-12 14:44:20 -070013505 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013506 return 0;
13507}
13508
David S. Miller59e6b432005-05-18 22:50:10 -070013509#define BOUNDARY_SINGLE_CACHELINE 1
13510#define BOUNDARY_MULTI_CACHELINE 2
13511
13512static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
13513{
13514 int cacheline_size;
13515 u8 byte;
13516 int goal;
13517
13518 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
13519 if (byte == 0)
13520 cacheline_size = 1024;
13521 else
13522 cacheline_size = (int) byte * 4;
13523
13524 /* On 5703 and later chips, the boundary bits have no
13525 * effect.
13526 */
13527 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13528 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13529 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
13530 goto out;
13531
13532#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
13533 goal = BOUNDARY_MULTI_CACHELINE;
13534#else
13535#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
13536 goal = BOUNDARY_SINGLE_CACHELINE;
13537#else
13538 goal = 0;
13539#endif
13540#endif
13541
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000013542 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
13543 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
13544 goto out;
13545 }
13546
David S. Miller59e6b432005-05-18 22:50:10 -070013547 if (!goal)
13548 goto out;
13549
13550 /* PCI controllers on most RISC systems tend to disconnect
13551 * when a device tries to burst across a cache-line boundary.
13552 * Therefore, letting tg3 do so just wastes PCI bandwidth.
13553 *
13554 * Unfortunately, for PCI-E there are only limited
13555 * write-side controls for this, and thus for reads
13556 * we will still get the disconnects. We'll also waste
13557 * these PCI cycles for both read and write for chips
13558 * other than 5700 and 5701 which do not implement the
13559 * boundary bits.
13560 */
13561 if ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) &&
13562 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)) {
13563 switch (cacheline_size) {
13564 case 16:
13565 case 32:
13566 case 64:
13567 case 128:
13568 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13569 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
13570 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
13571 } else {
13572 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
13573 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
13574 }
13575 break;
13576
13577 case 256:
13578 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
13579 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
13580 break;
13581
13582 default:
13583 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
13584 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
13585 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070013586 }
David S. Miller59e6b432005-05-18 22:50:10 -070013587 } else if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
13588 switch (cacheline_size) {
13589 case 16:
13590 case 32:
13591 case 64:
13592 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13593 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
13594 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
13595 break;
13596 }
13597 /* fallthrough */
13598 case 128:
13599 default:
13600 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
13601 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
13602 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070013603 }
David S. Miller59e6b432005-05-18 22:50:10 -070013604 } else {
13605 switch (cacheline_size) {
13606 case 16:
13607 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13608 val |= (DMA_RWCTRL_READ_BNDRY_16 |
13609 DMA_RWCTRL_WRITE_BNDRY_16);
13610 break;
13611 }
13612 /* fallthrough */
13613 case 32:
13614 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13615 val |= (DMA_RWCTRL_READ_BNDRY_32 |
13616 DMA_RWCTRL_WRITE_BNDRY_32);
13617 break;
13618 }
13619 /* fallthrough */
13620 case 64:
13621 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13622 val |= (DMA_RWCTRL_READ_BNDRY_64 |
13623 DMA_RWCTRL_WRITE_BNDRY_64);
13624 break;
13625 }
13626 /* fallthrough */
13627 case 128:
13628 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13629 val |= (DMA_RWCTRL_READ_BNDRY_128 |
13630 DMA_RWCTRL_WRITE_BNDRY_128);
13631 break;
13632 }
13633 /* fallthrough */
13634 case 256:
13635 val |= (DMA_RWCTRL_READ_BNDRY_256 |
13636 DMA_RWCTRL_WRITE_BNDRY_256);
13637 break;
13638 case 512:
13639 val |= (DMA_RWCTRL_READ_BNDRY_512 |
13640 DMA_RWCTRL_WRITE_BNDRY_512);
13641 break;
13642 case 1024:
13643 default:
13644 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
13645 DMA_RWCTRL_WRITE_BNDRY_1024);
13646 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070013647 }
David S. Miller59e6b432005-05-18 22:50:10 -070013648 }
13649
13650out:
13651 return val;
13652}
13653
Linus Torvalds1da177e2005-04-16 15:20:36 -070013654static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
13655{
13656 struct tg3_internal_buffer_desc test_desc;
13657 u32 sram_dma_descs;
13658 int i, ret;
13659
13660 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
13661
13662 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
13663 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
13664 tw32(RDMAC_STATUS, 0);
13665 tw32(WDMAC_STATUS, 0);
13666
13667 tw32(BUFMGR_MODE, 0);
13668 tw32(FTQ_RESET, 0);
13669
13670 test_desc.addr_hi = ((u64) buf_dma) >> 32;
13671 test_desc.addr_lo = buf_dma & 0xffffffff;
13672 test_desc.nic_mbuf = 0x00002100;
13673 test_desc.len = size;
13674
13675 /*
13676 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
13677 * the *second* time the tg3 driver was getting loaded after an
13678 * initial scan.
13679 *
13680 * Broadcom tells me:
13681 * ...the DMA engine is connected to the GRC block and a DMA
13682 * reset may affect the GRC block in some unpredictable way...
13683 * The behavior of resets to individual blocks has not been tested.
13684 *
13685 * Broadcom noted the GRC reset will also reset all sub-components.
13686 */
13687 if (to_device) {
13688 test_desc.cqid_sqid = (13 << 8) | 2;
13689
13690 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
13691 udelay(40);
13692 } else {
13693 test_desc.cqid_sqid = (16 << 8) | 7;
13694
13695 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
13696 udelay(40);
13697 }
13698 test_desc.flags = 0x00000005;
13699
13700 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
13701 u32 val;
13702
13703 val = *(((u32 *)&test_desc) + i);
13704 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
13705 sram_dma_descs + (i * sizeof(u32)));
13706 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
13707 }
13708 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
13709
13710 if (to_device) {
13711 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
13712 } else {
13713 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
13714 }
13715
13716 ret = -ENODEV;
13717 for (i = 0; i < 40; i++) {
13718 u32 val;
13719
13720 if (to_device)
13721 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
13722 else
13723 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
13724 if ((val & 0xffff) == sram_dma_descs) {
13725 ret = 0;
13726 break;
13727 }
13728
13729 udelay(100);
13730 }
13731
13732 return ret;
13733}
13734
David S. Millerded73402005-05-23 13:59:47 -070013735#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070013736
13737static int __devinit tg3_test_dma(struct tg3 *tp)
13738{
13739 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070013740 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000013741 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013742
13743 buf = pci_alloc_consistent(tp->pdev, TEST_BUFFER_SIZE, &buf_dma);
13744 if (!buf) {
13745 ret = -ENOMEM;
13746 goto out_nofree;
13747 }
13748
13749 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
13750 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
13751
David S. Miller59e6b432005-05-18 22:50:10 -070013752 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013753
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000013754 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
13755 goto out;
13756
Linus Torvalds1da177e2005-04-16 15:20:36 -070013757 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
13758 /* DMA read watermark not used on PCIE */
13759 tp->dma_rwctrl |= 0x00180000;
13760 } else if (!(tp->tg3_flags & TG3_FLAG_PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070013761 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
13762 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013763 tp->dma_rwctrl |= 0x003f0000;
13764 else
13765 tp->dma_rwctrl |= 0x003f000f;
13766 } else {
13767 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
13768 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
13769 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080013770 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013771
Michael Chan4a29cc22006-03-19 13:21:12 -080013772 /* If the 5704 is behind the EPB bridge, we can
13773 * do the less restrictive ONE_DMA workaround for
13774 * better performance.
13775 */
13776 if ((tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG) &&
13777 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
13778 tp->dma_rwctrl |= 0x8000;
13779 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013780 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
13781
Michael Chan49afdeb2007-02-13 12:17:03 -080013782 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
13783 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070013784 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080013785 tp->dma_rwctrl |=
13786 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
13787 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
13788 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070013789 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
13790 /* 5780 always in PCIX mode */
13791 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070013792 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
13793 /* 5714 always in PCIX mode */
13794 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013795 } else {
13796 tp->dma_rwctrl |= 0x001b000f;
13797 }
13798 }
13799
13800 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
13801 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
13802 tp->dma_rwctrl &= 0xfffffff0;
13803
13804 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13805 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
13806 /* Remove this if it causes problems for some boards. */
13807 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
13808
13809 /* On 5700/5701 chips, we need to set this bit.
13810 * Otherwise the chip will issue cacheline transactions
13811 * to streamable DMA memory with not all the byte
13812 * enables turned on. This is an error on several
13813 * RISC PCI controllers, in particular sparc64.
13814 *
13815 * On 5703/5704 chips, this bit has been reassigned
13816 * a different meaning. In particular, it is used
13817 * on those chips to enable a PCI-X workaround.
13818 */
13819 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
13820 }
13821
13822 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
13823
13824#if 0
13825 /* Unneeded, already done by tg3_get_invariants. */
13826 tg3_switch_clocks(tp);
13827#endif
13828
Linus Torvalds1da177e2005-04-16 15:20:36 -070013829 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13830 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
13831 goto out;
13832
David S. Miller59e6b432005-05-18 22:50:10 -070013833 /* It is best to perform DMA test with maximum write burst size
13834 * to expose the 5700/5701 write DMA bug.
13835 */
13836 saved_dma_rwctrl = tp->dma_rwctrl;
13837 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
13838 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
13839
Linus Torvalds1da177e2005-04-16 15:20:36 -070013840 while (1) {
13841 u32 *p = buf, i;
13842
13843 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
13844 p[i] = i;
13845
13846 /* Send the buffer to the chip. */
13847 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
13848 if (ret) {
13849 printk(KERN_ERR "tg3_test_dma() Write the buffer failed %d\n", ret);
13850 break;
13851 }
13852
13853#if 0
13854 /* validate data reached card RAM correctly. */
13855 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
13856 u32 val;
13857 tg3_read_mem(tp, 0x2100 + (i*4), &val);
13858 if (le32_to_cpu(val) != p[i]) {
13859 printk(KERN_ERR " tg3_test_dma() Card buffer corrupted on write! (%d != %d)\n", val, i);
13860 /* ret = -ENODEV here? */
13861 }
13862 p[i] = 0;
13863 }
13864#endif
13865 /* Now read it back. */
13866 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
13867 if (ret) {
13868 printk(KERN_ERR "tg3_test_dma() Read the buffer failed %d\n", ret);
13869
13870 break;
13871 }
13872
13873 /* Verify it. */
13874 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
13875 if (p[i] == i)
13876 continue;
13877
David S. Miller59e6b432005-05-18 22:50:10 -070013878 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
13879 DMA_RWCTRL_WRITE_BNDRY_16) {
13880 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013881 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
13882 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
13883 break;
13884 } else {
13885 printk(KERN_ERR "tg3_test_dma() buffer corrupted on read back! (%d != %d)\n", p[i], i);
13886 ret = -ENODEV;
13887 goto out;
13888 }
13889 }
13890
13891 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
13892 /* Success. */
13893 ret = 0;
13894 break;
13895 }
13896 }
David S. Miller59e6b432005-05-18 22:50:10 -070013897 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
13898 DMA_RWCTRL_WRITE_BNDRY_16) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070013899 static struct pci_device_id dma_wait_state_chipsets[] = {
13900 { PCI_DEVICE(PCI_VENDOR_ID_APPLE,
13901 PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
13902 { },
13903 };
13904
David S. Miller59e6b432005-05-18 22:50:10 -070013905 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070013906 * now look for chipsets that are known to expose the
13907 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070013908 */
Michael Chan6d1cfba2005-06-08 14:13:14 -070013909 if (pci_dev_present(dma_wait_state_chipsets)) {
13910 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
13911 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
13912 }
13913 else
13914 /* Safe to use the calculated DMA boundary. */
13915 tp->dma_rwctrl = saved_dma_rwctrl;
13916
David S. Miller59e6b432005-05-18 22:50:10 -070013917 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
13918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013919
13920out:
13921 pci_free_consistent(tp->pdev, TEST_BUFFER_SIZE, buf, buf_dma);
13922out_nofree:
13923 return ret;
13924}
13925
13926static void __devinit tg3_init_link_config(struct tg3 *tp)
13927{
13928 tp->link_config.advertising =
13929 (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
13930 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
13931 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full |
13932 ADVERTISED_Autoneg | ADVERTISED_MII);
13933 tp->link_config.speed = SPEED_INVALID;
13934 tp->link_config.duplex = DUPLEX_INVALID;
13935 tp->link_config.autoneg = AUTONEG_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013936 tp->link_config.active_speed = SPEED_INVALID;
13937 tp->link_config.active_duplex = DUPLEX_INVALID;
13938 tp->link_config.phy_is_low_power = 0;
13939 tp->link_config.orig_speed = SPEED_INVALID;
13940 tp->link_config.orig_duplex = DUPLEX_INVALID;
13941 tp->link_config.orig_autoneg = AUTONEG_INVALID;
13942}
13943
13944static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
13945{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013946 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS &&
13947 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717) {
Michael Chanfdfec1722005-07-25 12:31:48 -070013948 tp->bufmgr_config.mbuf_read_dma_low_water =
13949 DEFAULT_MB_RDMA_LOW_WATER_5705;
13950 tp->bufmgr_config.mbuf_mac_rx_low_water =
13951 DEFAULT_MB_MACRX_LOW_WATER_5705;
13952 tp->bufmgr_config.mbuf_high_water =
13953 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070013954 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
13955 tp->bufmgr_config.mbuf_mac_rx_low_water =
13956 DEFAULT_MB_MACRX_LOW_WATER_5906;
13957 tp->bufmgr_config.mbuf_high_water =
13958 DEFAULT_MB_HIGH_WATER_5906;
13959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013960
Michael Chanfdfec1722005-07-25 12:31:48 -070013961 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
13962 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
13963 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
13964 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
13965 tp->bufmgr_config.mbuf_high_water_jumbo =
13966 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
13967 } else {
13968 tp->bufmgr_config.mbuf_read_dma_low_water =
13969 DEFAULT_MB_RDMA_LOW_WATER;
13970 tp->bufmgr_config.mbuf_mac_rx_low_water =
13971 DEFAULT_MB_MACRX_LOW_WATER;
13972 tp->bufmgr_config.mbuf_high_water =
13973 DEFAULT_MB_HIGH_WATER;
13974
13975 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
13976 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
13977 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
13978 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
13979 tp->bufmgr_config.mbuf_high_water_jumbo =
13980 DEFAULT_MB_HIGH_WATER_JUMBO;
13981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013982
13983 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
13984 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
13985}
13986
13987static char * __devinit tg3_phy_string(struct tg3 *tp)
13988{
13989 switch (tp->phy_id & PHY_ID_MASK) {
13990 case PHY_ID_BCM5400: return "5400";
13991 case PHY_ID_BCM5401: return "5401";
13992 case PHY_ID_BCM5411: return "5411";
13993 case PHY_ID_BCM5701: return "5701";
13994 case PHY_ID_BCM5703: return "5703";
13995 case PHY_ID_BCM5704: return "5704";
13996 case PHY_ID_BCM5705: return "5705";
13997 case PHY_ID_BCM5750: return "5750";
Michael Chan85e94ce2005-04-21 17:05:28 -070013998 case PHY_ID_BCM5752: return "5752";
Michael Chana4e2b342005-10-26 15:46:52 -070013999 case PHY_ID_BCM5714: return "5714";
Michael Chan4cf78e42005-07-25 12:29:19 -070014000 case PHY_ID_BCM5780: return "5780";
Michael Chanaf36e6b2006-03-23 01:28:06 -080014001 case PHY_ID_BCM5755: return "5755";
Michael Chand9ab5ad2006-03-20 22:27:35 -080014002 case PHY_ID_BCM5787: return "5787";
Matt Carlsond30cdd22007-10-07 23:28:35 -070014003 case PHY_ID_BCM5784: return "5784";
Michael Chan126a3362006-09-27 16:03:07 -070014004 case PHY_ID_BCM5756: return "5722/5756";
Michael Chanb5d37722006-09-27 16:06:21 -070014005 case PHY_ID_BCM5906: return "5906";
Matt Carlson9936bcf2007-10-10 18:03:07 -070014006 case PHY_ID_BCM5761: return "5761";
Matt Carlsonc2060fe2009-11-13 13:03:33 +000014007 case PHY_ID_BCM5717: return "5717";
Linus Torvalds1da177e2005-04-16 15:20:36 -070014008 case PHY_ID_BCM8002: return "8002/serdes";
14009 case 0: return "serdes";
14010 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070014011 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014012}
14013
Michael Chanf9804dd2005-09-27 12:13:10 -070014014static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
14015{
14016 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
14017 strcpy(str, "PCI Express");
14018 return str;
14019 } else if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
14020 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
14021
14022 strcpy(str, "PCIX:");
14023
14024 if ((clock_ctrl == 7) ||
14025 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
14026 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
14027 strcat(str, "133MHz");
14028 else if (clock_ctrl == 0)
14029 strcat(str, "33MHz");
14030 else if (clock_ctrl == 2)
14031 strcat(str, "50MHz");
14032 else if (clock_ctrl == 4)
14033 strcat(str, "66MHz");
14034 else if (clock_ctrl == 6)
14035 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070014036 } else {
14037 strcpy(str, "PCI:");
14038 if (tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED)
14039 strcat(str, "66MHz");
14040 else
14041 strcat(str, "33MHz");
14042 }
14043 if (tp->tg3_flags & TG3_FLAG_PCI_32BIT)
14044 strcat(str, ":32-bit");
14045 else
14046 strcat(str, ":64-bit");
14047 return str;
14048}
14049
Michael Chan8c2dc7e2005-12-19 16:26:02 -080014050static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014051{
14052 struct pci_dev *peer;
14053 unsigned int func, devnr = tp->pdev->devfn & ~7;
14054
14055 for (func = 0; func < 8; func++) {
14056 peer = pci_get_slot(tp->pdev->bus, devnr | func);
14057 if (peer && peer != tp->pdev)
14058 break;
14059 pci_dev_put(peer);
14060 }
Michael Chan16fe9d72005-12-13 21:09:54 -080014061 /* 5704 can be configured in single-port mode, set peer to
14062 * tp->pdev in that case.
14063 */
14064 if (!peer) {
14065 peer = tp->pdev;
14066 return peer;
14067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014068
14069 /*
14070 * We don't need to keep the refcount elevated; there's no way
14071 * to remove one half of this device without removing the other
14072 */
14073 pci_dev_put(peer);
14074
14075 return peer;
14076}
14077
David S. Miller15f98502005-05-18 22:49:26 -070014078static void __devinit tg3_init_coal(struct tg3 *tp)
14079{
14080 struct ethtool_coalesce *ec = &tp->coal;
14081
14082 memset(ec, 0, sizeof(*ec));
14083 ec->cmd = ETHTOOL_GCOALESCE;
14084 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
14085 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
14086 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
14087 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
14088 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
14089 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
14090 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
14091 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
14092 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
14093
14094 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
14095 HOSTCC_MODE_CLRTICK_TXBD)) {
14096 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
14097 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
14098 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
14099 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
14100 }
Michael Chand244c892005-07-05 14:42:33 -070014101
14102 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
14103 ec->rx_coalesce_usecs_irq = 0;
14104 ec->tx_coalesce_usecs_irq = 0;
14105 ec->stats_block_coalesce_usecs = 0;
14106 }
David S. Miller15f98502005-05-18 22:49:26 -070014107}
14108
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080014109static const struct net_device_ops tg3_netdev_ops = {
14110 .ndo_open = tg3_open,
14111 .ndo_stop = tg3_close,
Stephen Hemminger00829822008-11-20 20:14:53 -080014112 .ndo_start_xmit = tg3_start_xmit,
14113 .ndo_get_stats = tg3_get_stats,
14114 .ndo_validate_addr = eth_validate_addr,
14115 .ndo_set_multicast_list = tg3_set_rx_mode,
14116 .ndo_set_mac_address = tg3_set_mac_addr,
14117 .ndo_do_ioctl = tg3_ioctl,
14118 .ndo_tx_timeout = tg3_tx_timeout,
14119 .ndo_change_mtu = tg3_change_mtu,
14120#if TG3_VLAN_TAG_USED
14121 .ndo_vlan_rx_register = tg3_vlan_rx_register,
14122#endif
14123#ifdef CONFIG_NET_POLL_CONTROLLER
14124 .ndo_poll_controller = tg3_poll_controller,
14125#endif
14126};
14127
14128static const struct net_device_ops tg3_netdev_ops_dma_bug = {
14129 .ndo_open = tg3_open,
14130 .ndo_stop = tg3_close,
14131 .ndo_start_xmit = tg3_start_xmit_dma_bug,
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080014132 .ndo_get_stats = tg3_get_stats,
14133 .ndo_validate_addr = eth_validate_addr,
14134 .ndo_set_multicast_list = tg3_set_rx_mode,
14135 .ndo_set_mac_address = tg3_set_mac_addr,
14136 .ndo_do_ioctl = tg3_ioctl,
14137 .ndo_tx_timeout = tg3_tx_timeout,
14138 .ndo_change_mtu = tg3_change_mtu,
14139#if TG3_VLAN_TAG_USED
14140 .ndo_vlan_rx_register = tg3_vlan_rx_register,
14141#endif
14142#ifdef CONFIG_NET_POLL_CONTROLLER
14143 .ndo_poll_controller = tg3_poll_controller,
14144#endif
14145};
14146
Linus Torvalds1da177e2005-04-16 15:20:36 -070014147static int __devinit tg3_init_one(struct pci_dev *pdev,
14148 const struct pci_device_id *ent)
14149{
14150 static int tg3_version_printed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014151 struct net_device *dev;
14152 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000014153 int i, err, pm_cap;
14154 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070014155 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080014156 u64 dma_mask, persist_dma_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014157
14158 if (tg3_version_printed++ == 0)
14159 printk(KERN_INFO "%s", version);
14160
14161 err = pci_enable_device(pdev);
14162 if (err) {
14163 printk(KERN_ERR PFX "Cannot enable PCI device, "
14164 "aborting.\n");
14165 return err;
14166 }
14167
Linus Torvalds1da177e2005-04-16 15:20:36 -070014168 err = pci_request_regions(pdev, DRV_MODULE_NAME);
14169 if (err) {
14170 printk(KERN_ERR PFX "Cannot obtain PCI resources, "
14171 "aborting.\n");
14172 goto err_out_disable_pdev;
14173 }
14174
14175 pci_set_master(pdev);
14176
14177 /* Find power-management capability. */
14178 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
14179 if (pm_cap == 0) {
14180 printk(KERN_ERR PFX "Cannot find PowerManagement capability, "
14181 "aborting.\n");
14182 err = -EIO;
14183 goto err_out_free_res;
14184 }
14185
Matt Carlsonfe5f5782009-09-01 13:09:39 +000014186 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014187 if (!dev) {
14188 printk(KERN_ERR PFX "Etherdev alloc failed, aborting.\n");
14189 err = -ENOMEM;
14190 goto err_out_free_res;
14191 }
14192
Linus Torvalds1da177e2005-04-16 15:20:36 -070014193 SET_NETDEV_DEV(dev, &pdev->dev);
14194
Linus Torvalds1da177e2005-04-16 15:20:36 -070014195#if TG3_VLAN_TAG_USED
14196 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014197#endif
14198
14199 tp = netdev_priv(dev);
14200 tp->pdev = pdev;
14201 tp->dev = dev;
14202 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014203 tp->rx_mode = TG3_DEF_RX_MODE;
14204 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070014205
Linus Torvalds1da177e2005-04-16 15:20:36 -070014206 if (tg3_debug > 0)
14207 tp->msg_enable = tg3_debug;
14208 else
14209 tp->msg_enable = TG3_DEF_MSG_ENABLE;
14210
14211 /* The word/byte swap controls here control register access byte
14212 * swapping. DMA data byte swapping is controlled in the GRC_MODE
14213 * setting below.
14214 */
14215 tp->misc_host_ctrl =
14216 MISC_HOST_CTRL_MASK_PCI_INT |
14217 MISC_HOST_CTRL_WORD_SWAP |
14218 MISC_HOST_CTRL_INDIR_ACCESS |
14219 MISC_HOST_CTRL_PCISTATE_RW;
14220
14221 /* The NONFRM (non-frame) byte/word swap controls take effect
14222 * on descriptor entries, anything which isn't packet data.
14223 *
14224 * The StrongARM chips on the board (one for tx, one for rx)
14225 * are running in big-endian mode.
14226 */
14227 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
14228 GRC_MODE_WSWAP_NONFRM_DATA);
14229#ifdef __BIG_ENDIAN
14230 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
14231#endif
14232 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014233 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000014234 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014235
Matt Carlsond5fe4882008-11-21 17:20:32 -080014236 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010014237 if (!tp->regs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014238 printk(KERN_ERR PFX "Cannot map device registers, "
14239 "aborting.\n");
14240 err = -ENOMEM;
14241 goto err_out_free_dev;
14242 }
14243
14244 tg3_init_link_config(tp);
14245
Linus Torvalds1da177e2005-04-16 15:20:36 -070014246 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
14247 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014248
Linus Torvalds1da177e2005-04-16 15:20:36 -070014249 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014250 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014251 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014252
14253 err = tg3_get_invariants(tp);
14254 if (err) {
14255 printk(KERN_ERR PFX "Problem fetching invariants of chip, "
14256 "aborting.\n");
14257 goto err_out_iounmap;
14258 }
14259
Matt Carlson615774f2009-11-13 13:03:39 +000014260 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) &&
14261 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0)
Stephen Hemminger00829822008-11-20 20:14:53 -080014262 dev->netdev_ops = &tg3_netdev_ops;
14263 else
14264 dev->netdev_ops = &tg3_netdev_ops_dma_bug;
14265
14266
Michael Chan4a29cc22006-03-19 13:21:12 -080014267 /* The EPB bridge inside 5714, 5715, and 5780 and any
14268 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080014269 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
14270 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
14271 * do DMA address check in tg3_start_xmit().
14272 */
Michael Chan4a29cc22006-03-19 13:21:12 -080014273 if (tp->tg3_flags2 & TG3_FLG2_IS_5788)
Yang Hongyang284901a2009-04-06 19:01:15 -070014274 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Michael Chan4a29cc22006-03-19 13:21:12 -080014275 else if (tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070014276 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080014277#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070014278 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080014279#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080014280 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070014281 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080014282
14283 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070014284 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080014285 err = pci_set_dma_mask(pdev, dma_mask);
14286 if (!err) {
14287 dev->features |= NETIF_F_HIGHDMA;
14288 err = pci_set_consistent_dma_mask(pdev,
14289 persist_dma_mask);
14290 if (err < 0) {
14291 printk(KERN_ERR PFX "Unable to obtain 64 bit "
14292 "DMA for consistent allocations\n");
14293 goto err_out_iounmap;
14294 }
14295 }
14296 }
Yang Hongyang284901a2009-04-06 19:01:15 -070014297 if (err || dma_mask == DMA_BIT_MASK(32)) {
14298 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080014299 if (err) {
14300 printk(KERN_ERR PFX "No usable DMA configuration, "
14301 "aborting.\n");
14302 goto err_out_iounmap;
14303 }
14304 }
14305
Michael Chanfdfec1722005-07-25 12:31:48 -070014306 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014307
Matt Carlson507399f2009-11-13 13:03:37 +000014308 /* Selectively allow TSO based on operating conditions */
14309 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO) ||
14310 (tp->fw_needed && !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)))
14311 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
14312 else {
14313 tp->tg3_flags2 &= ~(TG3_FLG2_TSO_CAPABLE | TG3_FLG2_TSO_BUG);
14314 tp->fw_needed = NULL;
14315 }
14316
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014317 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
Matt Carlson9e9fd122009-01-19 16:57:45 -080014318 tp->fw_needed = FIRMWARE_TG3;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014319
Michael Chan4e3a7aa2006-03-20 17:47:44 -080014320 /* TSO is on by default on chips that support hardware TSO.
14321 * Firmware TSO on older chips gives lower performance, so it
14322 * is off by default, but can be enabled using ethtool.
14323 */
Matt Carlsone849cdc2009-11-13 13:03:38 +000014324 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO) &&
14325 (dev->features & NETIF_F_IP_CSUM))
14326 dev->features |= NETIF_F_TSO;
14327
14328 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_2) ||
14329 (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3)) {
14330 if (dev->features & NETIF_F_IPV6_CSUM)
Michael Chanb0026622006-07-03 19:42:14 -070014331 dev->features |= NETIF_F_TSO6;
Matt Carlsone849cdc2009-11-13 13:03:38 +000014332 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
14333 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070014334 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
14335 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014336 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000014337 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson9936bcf2007-10-10 18:03:07 -070014338 dev->features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070014339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014340
Linus Torvalds1da177e2005-04-16 15:20:36 -070014341 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
14342 !(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) &&
14343 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
14344 tp->tg3_flags2 |= TG3_FLG2_MAX_RXPEND_64;
14345 tp->rx_pending = 63;
14346 }
14347
Linus Torvalds1da177e2005-04-16 15:20:36 -070014348 err = tg3_get_device_address(tp);
14349 if (err) {
14350 printk(KERN_ERR PFX "Could not obtain valid ethernet address, "
14351 "aborting.\n");
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014352 goto err_out_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014353 }
14354
Matt Carlson0d3031d2007-10-10 18:02:43 -070014355 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
Matt Carlson63532392008-11-03 16:49:57 -080014356 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
Al Viro79ea13c2008-01-24 02:06:46 -080014357 if (!tp->aperegs) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070014358 printk(KERN_ERR PFX "Cannot map APE registers, "
14359 "aborting.\n");
14360 err = -ENOMEM;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014361 goto err_out_fw;
Matt Carlson0d3031d2007-10-10 18:02:43 -070014362 }
14363
14364 tg3_ape_lock_init(tp);
Matt Carlson7fd76442009-02-25 14:27:20 +000014365
14366 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF)
14367 tg3_read_dash_ver(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070014368 }
14369
Matt Carlsonc88864d2007-11-12 21:07:01 -080014370 /*
14371 * Reset chip in case UNDI or EFI driver did not shutdown
14372 * DMA self test will enable WDMAC and we'll see (spurious)
14373 * pending DMA on the PCI bus at that point.
14374 */
14375 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
14376 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
14377 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
14378 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
14379 }
14380
14381 err = tg3_test_dma(tp);
14382 if (err) {
14383 printk(KERN_ERR PFX "DMA engine test failed, aborting.\n");
14384 goto err_out_apeunmap;
14385 }
14386
Matt Carlsonc88864d2007-11-12 21:07:01 -080014387 /* flow control autonegotiation is default behavior */
14388 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
Steve Glendinninge18ce342008-12-16 02:00:00 -080014389 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlsonc88864d2007-11-12 21:07:01 -080014390
Matt Carlson78f90dc2009-11-13 13:03:42 +000014391 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
14392 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
14393 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
14394 for (i = 0; i < TG3_IRQ_MAX_VECS; i++) {
14395 struct tg3_napi *tnapi = &tp->napi[i];
14396
14397 tnapi->tp = tp;
14398 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
14399
14400 tnapi->int_mbox = intmbx;
14401 if (i < 4)
14402 intmbx += 0x8;
14403 else
14404 intmbx += 0x4;
14405
14406 tnapi->consmbox = rcvmbx;
14407 tnapi->prodmbox = sndmbx;
14408
14409 if (i) {
14410 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
14411 netif_napi_add(dev, &tnapi->napi, tg3_poll_msix, 64);
14412 } else {
14413 tnapi->coal_now = HOSTCC_MODE_NOW;
14414 netif_napi_add(dev, &tnapi->napi, tg3_poll, 64);
14415 }
14416
14417 if (!(tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX))
14418 break;
14419
14420 /*
14421 * If we support MSIX, we'll be using RSS. If we're using
14422 * RSS, the first vector only handles link interrupts and the
14423 * remaining vectors handle rx and tx interrupts. Reuse the
14424 * mailbox values for the next iteration. The values we setup
14425 * above are still useful for the single vectored mode.
14426 */
14427 if (!i)
14428 continue;
14429
14430 rcvmbx += 0x8;
14431
14432 if (sndmbx & 0x4)
14433 sndmbx -= 0x4;
14434 else
14435 sndmbx += 0xc;
14436 }
14437
Matt Carlsonc88864d2007-11-12 21:07:01 -080014438 tg3_init_coal(tp);
14439
Michael Chanc49a1562006-12-17 17:07:29 -080014440 pci_set_drvdata(pdev, dev);
14441
Linus Torvalds1da177e2005-04-16 15:20:36 -070014442 err = register_netdev(dev);
14443 if (err) {
14444 printk(KERN_ERR PFX "Cannot register net device, "
14445 "aborting.\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070014446 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014447 }
14448
Matt Carlsondf59c942008-11-03 16:52:56 -080014449 printk(KERN_INFO "%s: Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070014450 dev->name,
14451 tp->board_part_number,
14452 tp->pci_chip_rev_id,
Michael Chanf9804dd2005-09-27 12:13:10 -070014453 tg3_bus_string(tp, str),
Johannes Berge1749612008-10-27 15:59:26 -070014454 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014455
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000014456 if (tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED) {
14457 struct phy_device *phydev;
14458 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsondf59c942008-11-03 16:52:56 -080014459 printk(KERN_INFO
14460 "%s: attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000014461 tp->dev->name, phydev->drv->name,
14462 dev_name(&phydev->dev));
14463 } else
Matt Carlsondf59c942008-11-03 16:52:56 -080014464 printk(KERN_INFO
14465 "%s: attached PHY is %s (%s Ethernet) (WireSpeed[%d])\n",
14466 tp->dev->name, tg3_phy_string(tp),
14467 ((tp->tg3_flags & TG3_FLAG_10_100_ONLY) ? "10/100Base-TX" :
14468 ((tp->tg3_flags2 & TG3_FLG2_ANY_SERDES) ? "1000Base-SX" :
14469 "10/100/1000Base-T")),
14470 (tp->tg3_flags2 & TG3_FLG2_NO_ETH_WIRE_SPEED) == 0);
14471
14472 printk(KERN_INFO "%s: RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070014473 dev->name,
14474 (tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) != 0,
14475 (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) != 0,
14476 (tp->tg3_flags & TG3_FLAG_USE_MI_INTERRUPT) != 0,
14477 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014478 (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) != 0);
Michael Chan4a29cc22006-03-19 13:21:12 -080014479 printk(KERN_INFO "%s: dma_rwctrl[%08x] dma_mask[%d-bit]\n",
14480 dev->name, tp->dma_rwctrl,
Yang Hongyang284901a2009-04-06 19:01:15 -070014481 (pdev->dma_mask == DMA_BIT_MASK(32)) ? 32 :
Yang Hongyang50cf1562009-04-06 19:01:14 -070014482 (((u64) pdev->dma_mask == DMA_BIT_MASK(40)) ? 40 : 64));
Linus Torvalds1da177e2005-04-16 15:20:36 -070014483
14484 return 0;
14485
Matt Carlson0d3031d2007-10-10 18:02:43 -070014486err_out_apeunmap:
14487 if (tp->aperegs) {
14488 iounmap(tp->aperegs);
14489 tp->aperegs = NULL;
14490 }
14491
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014492err_out_fw:
14493 if (tp->fw)
14494 release_firmware(tp->fw);
14495
Linus Torvalds1da177e2005-04-16 15:20:36 -070014496err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070014497 if (tp->regs) {
14498 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070014499 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070014500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014501
14502err_out_free_dev:
14503 free_netdev(dev);
14504
14505err_out_free_res:
14506 pci_release_regions(pdev);
14507
14508err_out_disable_pdev:
14509 pci_disable_device(pdev);
14510 pci_set_drvdata(pdev, NULL);
14511 return err;
14512}
14513
14514static void __devexit tg3_remove_one(struct pci_dev *pdev)
14515{
14516 struct net_device *dev = pci_get_drvdata(pdev);
14517
14518 if (dev) {
14519 struct tg3 *tp = netdev_priv(dev);
14520
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014521 if (tp->fw)
14522 release_firmware(tp->fw);
14523
Michael Chan7faa0062006-02-02 17:29:28 -080014524 flush_scheduled_work();
Matt Carlson158d7ab2008-05-29 01:37:54 -070014525
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014526 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
14527 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070014528 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014529 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070014530
Linus Torvalds1da177e2005-04-16 15:20:36 -070014531 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070014532 if (tp->aperegs) {
14533 iounmap(tp->aperegs);
14534 tp->aperegs = NULL;
14535 }
Michael Chan68929142005-08-09 20:17:14 -070014536 if (tp->regs) {
14537 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070014538 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070014539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014540 free_netdev(dev);
14541 pci_release_regions(pdev);
14542 pci_disable_device(pdev);
14543 pci_set_drvdata(pdev, NULL);
14544 }
14545}
14546
14547static int tg3_suspend(struct pci_dev *pdev, pm_message_t state)
14548{
14549 struct net_device *dev = pci_get_drvdata(pdev);
14550 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070014551 pci_power_t target_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014552 int err;
14553
Michael Chan3e0c95f2007-08-03 20:56:54 -070014554 /* PCI register 4 needs to be saved whether netif_running() or not.
14555 * MSI address and data need to be saved if using MSI and
14556 * netif_running().
14557 */
14558 pci_save_state(pdev);
14559
Linus Torvalds1da177e2005-04-16 15:20:36 -070014560 if (!netif_running(dev))
14561 return 0;
14562
Michael Chan7faa0062006-02-02 17:29:28 -080014563 flush_scheduled_work();
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014564 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014565 tg3_netif_stop(tp);
14566
14567 del_timer_sync(&tp->timer);
14568
David S. Millerf47c11e2005-06-24 20:18:35 -070014569 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014570 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070014571 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014572
14573 netif_device_detach(dev);
14574
David S. Millerf47c11e2005-06-24 20:18:35 -070014575 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070014576 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan6a9eba12005-12-13 21:08:58 -080014577 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
David S. Millerf47c11e2005-06-24 20:18:35 -070014578 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014579
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070014580 target_state = pdev->pm_cap ? pci_target_state(pdev) : PCI_D3hot;
14581
14582 err = tg3_set_power_state(tp, target_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014583 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014584 int err2;
14585
David S. Millerf47c11e2005-06-24 20:18:35 -070014586 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014587
Michael Chan6a9eba12005-12-13 21:08:58 -080014588 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014589 err2 = tg3_restart_hw(tp, 1);
14590 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070014591 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014592
14593 tp->timer.expires = jiffies + tp->timer_offset;
14594 add_timer(&tp->timer);
14595
14596 netif_device_attach(dev);
14597 tg3_netif_start(tp);
14598
Michael Chanb9ec6c12006-07-25 16:37:27 -070014599out:
David S. Millerf47c11e2005-06-24 20:18:35 -070014600 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014601
14602 if (!err2)
14603 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014604 }
14605
14606 return err;
14607}
14608
14609static int tg3_resume(struct pci_dev *pdev)
14610{
14611 struct net_device *dev = pci_get_drvdata(pdev);
14612 struct tg3 *tp = netdev_priv(dev);
14613 int err;
14614
Michael Chan3e0c95f2007-08-03 20:56:54 -070014615 pci_restore_state(tp->pdev);
14616
Linus Torvalds1da177e2005-04-16 15:20:36 -070014617 if (!netif_running(dev))
14618 return 0;
14619
Michael Chanbc1c7562006-03-20 17:48:03 -080014620 err = tg3_set_power_state(tp, PCI_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014621 if (err)
14622 return err;
14623
14624 netif_device_attach(dev);
14625
David S. Millerf47c11e2005-06-24 20:18:35 -070014626 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014627
Michael Chan6a9eba12005-12-13 21:08:58 -080014628 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Michael Chanb9ec6c12006-07-25 16:37:27 -070014629 err = tg3_restart_hw(tp, 1);
14630 if (err)
14631 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014632
14633 tp->timer.expires = jiffies + tp->timer_offset;
14634 add_timer(&tp->timer);
14635
Linus Torvalds1da177e2005-04-16 15:20:36 -070014636 tg3_netif_start(tp);
14637
Michael Chanb9ec6c12006-07-25 16:37:27 -070014638out:
David S. Millerf47c11e2005-06-24 20:18:35 -070014639 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014640
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014641 if (!err)
14642 tg3_phy_start(tp);
14643
Michael Chanb9ec6c12006-07-25 16:37:27 -070014644 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014645}
14646
14647static struct pci_driver tg3_driver = {
14648 .name = DRV_MODULE_NAME,
14649 .id_table = tg3_pci_tbl,
14650 .probe = tg3_init_one,
14651 .remove = __devexit_p(tg3_remove_one),
14652 .suspend = tg3_suspend,
14653 .resume = tg3_resume
14654};
14655
14656static int __init tg3_init(void)
14657{
Jeff Garzik29917622006-08-19 17:48:59 -040014658 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014659}
14660
14661static void __exit tg3_cleanup(void)
14662{
14663 pci_unregister_driver(&tg3_driver);
14664}
14665
14666module_init(tg3_init);
14667module_exit(tg3_cleanup);