blob: 44ac9e233bef58a2242038f2029a0d1b3cb1509f [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 Carlsoncceea982009-12-03 08:36:25 +000071#define DRV_MODULE_VERSION "3.105"
72#define DRV_MODULE_RELDATE "December 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
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000177static DEFINE_PCI_DEVICE_TABLE(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 Carlsonecf14102010-01-20 16:58:05 +00001563 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
1564 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 &&
1565 (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08001566 return;
1567
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001568 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
1569 tg3_phy_fet_toggle_apd(tp, enable);
1570 return;
1571 }
1572
Matt Carlson6833c042008-11-21 17:18:59 -08001573 reg = MII_TG3_MISC_SHDW_WREN |
1574 MII_TG3_MISC_SHDW_SCR5_SEL |
1575 MII_TG3_MISC_SHDW_SCR5_LPED |
1576 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
1577 MII_TG3_MISC_SHDW_SCR5_SDTL |
1578 MII_TG3_MISC_SHDW_SCR5_C125OE;
1579 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 || !enable)
1580 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
1581
1582 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1583
1584
1585 reg = MII_TG3_MISC_SHDW_WREN |
1586 MII_TG3_MISC_SHDW_APD_SEL |
1587 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
1588 if (enable)
1589 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
1590
1591 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1592}
1593
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001594static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
1595{
1596 u32 phy;
1597
1598 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
1599 (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES))
1600 return;
1601
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001602 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001603 u32 ephy;
1604
Matt Carlson535ef6e2009-08-25 10:09:36 +00001605 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
1606 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
1607
1608 tg3_writephy(tp, MII_TG3_FET_TEST,
1609 ephy | MII_TG3_FET_SHADOW_EN);
1610 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001611 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00001612 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001613 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00001614 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
1615 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001616 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00001617 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001618 }
1619 } else {
1620 phy = MII_TG3_AUXCTL_MISC_RDSEL_MISC |
1621 MII_TG3_AUXCTL_SHDWSEL_MISC;
1622 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, phy) &&
1623 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &phy)) {
1624 if (enable)
1625 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1626 else
1627 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1628 phy |= MII_TG3_AUXCTL_MISC_WREN;
1629 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1630 }
1631 }
1632}
1633
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634static void tg3_phy_set_wirespeed(struct tg3 *tp)
1635{
1636 u32 val;
1637
1638 if (tp->tg3_flags2 & TG3_FLG2_NO_ETH_WIRE_SPEED)
1639 return;
1640
1641 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x7007) &&
1642 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &val))
1643 tg3_writephy(tp, MII_TG3_AUX_CTRL,
1644 (val | (1 << 15) | (1 << 4)));
1645}
1646
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001647static void tg3_phy_apply_otp(struct tg3 *tp)
1648{
1649 u32 otp, phy;
1650
1651 if (!tp->phy_otp)
1652 return;
1653
1654 otp = tp->phy_otp;
1655
1656 /* Enable SM_DSP clock and tx 6dB coding. */
1657 phy = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1658 MII_TG3_AUXCTL_ACTL_SMDSP_ENA |
1659 MII_TG3_AUXCTL_ACTL_TX_6DB;
1660 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1661
1662 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
1663 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
1664 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
1665
1666 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
1667 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
1668 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
1669
1670 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
1671 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
1672 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
1673
1674 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
1675 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
1676
1677 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
1678 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
1679
1680 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
1681 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
1682 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
1683
1684 /* Turn off SM_DSP clock. */
1685 phy = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1686 MII_TG3_AUXCTL_ACTL_TX_6DB;
1687 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1688}
1689
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690static int tg3_wait_macro_done(struct tg3 *tp)
1691{
1692 int limit = 100;
1693
1694 while (limit--) {
1695 u32 tmp32;
1696
1697 if (!tg3_readphy(tp, 0x16, &tmp32)) {
1698 if ((tmp32 & 0x1000) == 0)
1699 break;
1700 }
1701 }
Roel Kluind4675b52009-02-12 16:33:27 -08001702 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 return -EBUSY;
1704
1705 return 0;
1706}
1707
1708static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
1709{
1710 static const u32 test_pat[4][6] = {
1711 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
1712 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
1713 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
1714 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
1715 };
1716 int chan;
1717
1718 for (chan = 0; chan < 4; chan++) {
1719 int i;
1720
1721 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1722 (chan * 0x2000) | 0x0200);
1723 tg3_writephy(tp, 0x16, 0x0002);
1724
1725 for (i = 0; i < 6; i++)
1726 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
1727 test_pat[chan][i]);
1728
1729 tg3_writephy(tp, 0x16, 0x0202);
1730 if (tg3_wait_macro_done(tp)) {
1731 *resetp = 1;
1732 return -EBUSY;
1733 }
1734
1735 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1736 (chan * 0x2000) | 0x0200);
1737 tg3_writephy(tp, 0x16, 0x0082);
1738 if (tg3_wait_macro_done(tp)) {
1739 *resetp = 1;
1740 return -EBUSY;
1741 }
1742
1743 tg3_writephy(tp, 0x16, 0x0802);
1744 if (tg3_wait_macro_done(tp)) {
1745 *resetp = 1;
1746 return -EBUSY;
1747 }
1748
1749 for (i = 0; i < 6; i += 2) {
1750 u32 low, high;
1751
1752 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
1753 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
1754 tg3_wait_macro_done(tp)) {
1755 *resetp = 1;
1756 return -EBUSY;
1757 }
1758 low &= 0x7fff;
1759 high &= 0x000f;
1760 if (low != test_pat[chan][i] ||
1761 high != test_pat[chan][i+1]) {
1762 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
1763 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
1764 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
1765
1766 return -EBUSY;
1767 }
1768 }
1769 }
1770
1771 return 0;
1772}
1773
1774static int tg3_phy_reset_chanpat(struct tg3 *tp)
1775{
1776 int chan;
1777
1778 for (chan = 0; chan < 4; chan++) {
1779 int i;
1780
1781 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1782 (chan * 0x2000) | 0x0200);
1783 tg3_writephy(tp, 0x16, 0x0002);
1784 for (i = 0; i < 6; i++)
1785 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
1786 tg3_writephy(tp, 0x16, 0x0202);
1787 if (tg3_wait_macro_done(tp))
1788 return -EBUSY;
1789 }
1790
1791 return 0;
1792}
1793
1794static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
1795{
1796 u32 reg32, phy9_orig;
1797 int retries, do_phy_reset, err;
1798
1799 retries = 10;
1800 do_phy_reset = 1;
1801 do {
1802 if (do_phy_reset) {
1803 err = tg3_bmcr_reset(tp);
1804 if (err)
1805 return err;
1806 do_phy_reset = 0;
1807 }
1808
1809 /* Disable transmitter and interrupt. */
1810 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
1811 continue;
1812
1813 reg32 |= 0x3000;
1814 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
1815
1816 /* Set full-duplex, 1000 mbps. */
1817 tg3_writephy(tp, MII_BMCR,
1818 BMCR_FULLDPLX | TG3_BMCR_SPEED1000);
1819
1820 /* Set to master mode. */
1821 if (tg3_readphy(tp, MII_TG3_CTRL, &phy9_orig))
1822 continue;
1823
1824 tg3_writephy(tp, MII_TG3_CTRL,
1825 (MII_TG3_CTRL_AS_MASTER |
1826 MII_TG3_CTRL_ENABLE_AS_MASTER));
1827
1828 /* Enable SM_DSP_CLOCK and 6dB. */
1829 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
1830
1831 /* Block the PHY control access. */
1832 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8005);
1833 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0800);
1834
1835 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
1836 if (!err)
1837 break;
1838 } while (--retries);
1839
1840 err = tg3_phy_reset_chanpat(tp);
1841 if (err)
1842 return err;
1843
1844 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8005);
1845 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0000);
1846
1847 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
1848 tg3_writephy(tp, 0x16, 0x0000);
1849
1850 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
1851 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
1852 /* Set Extended packet length bit for jumbo frames */
1853 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4400);
1854 }
1855 else {
1856 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
1857 }
1858
1859 tg3_writephy(tp, MII_TG3_CTRL, phy9_orig);
1860
1861 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
1862 reg32 &= ~0x3000;
1863 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
1864 } else if (!err)
1865 err = -EBUSY;
1866
1867 return err;
1868}
1869
1870/* This will reset the tigon3 PHY if there is no valid
1871 * link unless the FORCE argument is non-zero.
1872 */
1873static int tg3_phy_reset(struct tg3 *tp)
1874{
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001875 u32 cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 u32 phy_status;
1877 int err;
1878
Michael Chan60189dd2006-12-17 17:08:07 -08001879 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
1880 u32 val;
1881
1882 val = tr32(GRC_MISC_CFG);
1883 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
1884 udelay(40);
1885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 err = tg3_readphy(tp, MII_BMSR, &phy_status);
1887 err |= tg3_readphy(tp, MII_BMSR, &phy_status);
1888 if (err != 0)
1889 return -EBUSY;
1890
Michael Chanc8e1e822006-04-29 18:55:17 -07001891 if (netif_running(tp->dev) && netif_carrier_ok(tp->dev)) {
1892 netif_carrier_off(tp->dev);
1893 tg3_link_report(tp);
1894 }
1895
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
1897 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
1898 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
1899 err = tg3_phy_reset_5703_4_5(tp);
1900 if (err)
1901 return err;
1902 goto out;
1903 }
1904
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001905 cpmuctrl = 0;
1906 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
1907 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
1908 cpmuctrl = tr32(TG3_CPMU_CTRL);
1909 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
1910 tw32(TG3_CPMU_CTRL,
1911 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
1912 }
1913
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 err = tg3_bmcr_reset(tp);
1915 if (err)
1916 return err;
1917
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001918 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
1919 u32 phy;
1920
1921 phy = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
1922 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, phy);
1923
1924 tw32(TG3_CPMU_CTRL, cpmuctrl);
1925 }
1926
Matt Carlsonbcb37f62008-11-03 16:52:09 -08001927 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
1928 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08001929 u32 val;
1930
1931 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
1932 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
1933 CPMU_LSPD_1000MB_MACCLK_12_5) {
1934 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
1935 udelay(40);
1936 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
1937 }
1938 }
1939
Matt Carlsonecf14102010-01-20 16:58:05 +00001940 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 &&
1941 (tp->tg3_flags2 & TG3_FLG2_MII_SERDES))
1942 return 0;
1943
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001944 tg3_phy_apply_otp(tp);
1945
Matt Carlson6833c042008-11-21 17:18:59 -08001946 if (tp->tg3_flags3 & TG3_FLG3_PHY_ENABLE_APD)
1947 tg3_phy_toggle_apd(tp, true);
1948 else
1949 tg3_phy_toggle_apd(tp, false);
1950
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951out:
1952 if (tp->tg3_flags2 & TG3_FLG2_PHY_ADC_BUG) {
1953 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
1954 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x201f);
1955 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x2aaa);
1956 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
1957 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0323);
1958 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
1959 }
1960 if (tp->tg3_flags2 & TG3_FLG2_PHY_5704_A0_BUG) {
1961 tg3_writephy(tp, 0x1c, 0x8d68);
1962 tg3_writephy(tp, 0x1c, 0x8d68);
1963 }
1964 if (tp->tg3_flags2 & TG3_FLG2_PHY_BER_BUG) {
1965 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
1966 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
1967 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x310b);
1968 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x201f);
1969 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x9506);
1970 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x401f);
1971 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x14e2);
1972 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
1973 }
Michael Chanc424cb22006-04-29 18:56:34 -07001974 else if (tp->tg3_flags2 & TG3_FLG2_PHY_JITTER_BUG) {
1975 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
1976 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
Michael Chanc1d2a192007-01-08 19:57:20 -08001977 if (tp->tg3_flags2 & TG3_FLG2_PHY_ADJUST_TRIM) {
1978 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
1979 tg3_writephy(tp, MII_TG3_TEST1,
1980 MII_TG3_TEST1_TRIM_EN | 0x4);
1981 } else
1982 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
Michael Chanc424cb22006-04-29 18:56:34 -07001983 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
1984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 /* Set Extended packet length bit (bit 14) on all chips that */
1986 /* support jumbo frames */
1987 if ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5401) {
1988 /* Cannot do read-modify-write on 5401 */
1989 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4c20);
Matt Carlson8f666b02009-08-28 13:58:24 +00001990 } else if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 u32 phy_reg;
1992
1993 /* Set bit 14 with read-modify-write to preserve other bits */
1994 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0007) &&
1995 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &phy_reg))
1996 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy_reg | 0x4000);
1997 }
1998
1999 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2000 * jumbo frames transmission.
2001 */
Matt Carlson8f666b02009-08-28 13:58:24 +00002002 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 u32 phy_reg;
2004
2005 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &phy_reg))
2006 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2007 phy_reg | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
2008 }
2009
Michael Chan715116a2006-09-27 16:09:25 -07002010 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002011 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002012 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002013 }
2014
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002015 tg3_phy_toggle_automdix(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 tg3_phy_set_wirespeed(tp);
2017 return 0;
2018}
2019
2020static void tg3_frob_aux_power(struct tg3 *tp)
2021{
2022 struct tg3 *tp_peer = tp;
2023
Michael Chan9d26e212006-12-07 00:21:14 -08002024 if ((tp->tg3_flags2 & TG3_FLG2_IS_NIC) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 return;
2026
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00002027 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2028 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
2029 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002030 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002032 dev_peer = pci_get_drvdata(tp->pdev_peer);
Michael Chanbc1c7562006-03-20 17:48:03 -08002033 /* remove_one() may have been run on the peer. */
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002034 if (!dev_peer)
Michael Chanbc1c7562006-03-20 17:48:03 -08002035 tp_peer = tp;
2036 else
2037 tp_peer = netdev_priv(dev_peer);
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002038 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
2040 if ((tp->tg3_flags & TG3_FLAG_WOL_ENABLE) != 0 ||
Michael Chan6921d202005-12-13 21:15:53 -08002041 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0 ||
2042 (tp_peer->tg3_flags & TG3_FLAG_WOL_ENABLE) != 0 ||
2043 (tp_peer->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2045 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Michael Chanb401e9e2005-12-19 16:27:04 -08002046 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2047 (GRC_LCLCTRL_GPIO_OE0 |
2048 GRC_LCLCTRL_GPIO_OE1 |
2049 GRC_LCLCTRL_GPIO_OE2 |
2050 GRC_LCLCTRL_GPIO_OUTPUT0 |
2051 GRC_LCLCTRL_GPIO_OUTPUT1),
2052 100);
Matt Carlson8d519ab2009-04-20 06:58:01 +00002053 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2054 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -07002055 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2056 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2057 GRC_LCLCTRL_GPIO_OE1 |
2058 GRC_LCLCTRL_GPIO_OE2 |
2059 GRC_LCLCTRL_GPIO_OUTPUT0 |
2060 GRC_LCLCTRL_GPIO_OUTPUT1 |
2061 tp->grc_local_ctrl;
2062 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2063
2064 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2065 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2066
2067 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2068 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 } else {
2070 u32 no_gpio2;
Michael Chandc56b7d2005-12-19 16:26:28 -08002071 u32 grc_local_ctrl = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
2073 if (tp_peer != tp &&
2074 (tp_peer->tg3_flags & TG3_FLAG_INIT_COMPLETE) != 0)
2075 return;
2076
Michael Chandc56b7d2005-12-19 16:26:28 -08002077 /* Workaround to prevent overdrawing Amps. */
2078 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2079 ASIC_REV_5714) {
2080 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chanb401e9e2005-12-19 16:27:04 -08002081 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2082 grc_local_ctrl, 100);
Michael Chandc56b7d2005-12-19 16:26:28 -08002083 }
2084
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 /* On 5753 and variants, GPIO2 cannot be used. */
2086 no_gpio2 = tp->nic_sram_data_cfg &
2087 NIC_SRAM_DATA_CFG_NO_GPIO2;
2088
Michael Chandc56b7d2005-12-19 16:26:28 -08002089 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 GRC_LCLCTRL_GPIO_OE1 |
2091 GRC_LCLCTRL_GPIO_OE2 |
2092 GRC_LCLCTRL_GPIO_OUTPUT1 |
2093 GRC_LCLCTRL_GPIO_OUTPUT2;
2094 if (no_gpio2) {
2095 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2096 GRC_LCLCTRL_GPIO_OUTPUT2);
2097 }
Michael Chanb401e9e2005-12-19 16:27:04 -08002098 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2099 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
2101 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2102
Michael Chanb401e9e2005-12-19 16:27:04 -08002103 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2104 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
2106 if (!no_gpio2) {
2107 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chanb401e9e2005-12-19 16:27:04 -08002108 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2109 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 }
2111 }
2112 } else {
2113 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
2114 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
2115 if (tp_peer != tp &&
2116 (tp_peer->tg3_flags & TG3_FLAG_INIT_COMPLETE) != 0)
2117 return;
2118
Michael Chanb401e9e2005-12-19 16:27:04 -08002119 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2120 (GRC_LCLCTRL_GPIO_OE1 |
2121 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
Michael Chanb401e9e2005-12-19 16:27:04 -08002123 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2124 GRC_LCLCTRL_GPIO_OE1, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
Michael Chanb401e9e2005-12-19 16:27:04 -08002126 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2127 (GRC_LCLCTRL_GPIO_OE1 |
2128 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 }
2130 }
2131}
2132
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002133static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2134{
2135 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2136 return 1;
2137 else if ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5411) {
2138 if (speed != SPEED_10)
2139 return 1;
2140 } else if (speed == SPEED_10)
2141 return 1;
2142
2143 return 0;
2144}
2145
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146static int tg3_setup_phy(struct tg3 *, int);
2147
2148#define RESET_KIND_SHUTDOWN 0
2149#define RESET_KIND_INIT 1
2150#define RESET_KIND_SUSPEND 2
2151
2152static void tg3_write_sig_post_reset(struct tg3 *, int);
2153static int tg3_halt_cpu(struct tg3 *, u32);
2154
Matt Carlson0a459aa2008-11-03 16:54:15 -08002155static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002156{
Matt Carlsonce057f02007-11-12 21:08:03 -08002157 u32 val;
2158
Michael Chan51297242007-02-13 12:17:57 -08002159 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
2160 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2161 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2162 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2163
2164 sg_dig_ctrl |=
2165 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2166 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2167 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2168 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002169 return;
Michael Chan51297242007-02-13 12:17:57 -08002170 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002171
Michael Chan60189dd2006-12-17 17:08:07 -08002172 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002173 tg3_bmcr_reset(tp);
2174 val = tr32(GRC_MISC_CFG);
2175 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2176 udelay(40);
2177 return;
Matt Carlson0e5f7842009-11-02 14:26:38 +00002178 } else if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
2179 u32 phytest;
2180 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2181 u32 phy;
2182
2183 tg3_writephy(tp, MII_ADVERTISE, 0);
2184 tg3_writephy(tp, MII_BMCR,
2185 BMCR_ANENABLE | BMCR_ANRESTART);
2186
2187 tg3_writephy(tp, MII_TG3_FET_TEST,
2188 phytest | MII_TG3_FET_SHADOW_EN);
2189 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
2190 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
2191 tg3_writephy(tp,
2192 MII_TG3_FET_SHDW_AUXMODE4,
2193 phy);
2194 }
2195 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2196 }
2197 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002198 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07002199 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2200 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002201
2202 tg3_writephy(tp, MII_TG3_AUX_CTRL,
2203 MII_TG3_AUXCTL_SHDWSEL_PWRCTL |
2204 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2205 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2206 MII_TG3_AUXCTL_PCTL_VREG_11V);
Michael Chan715116a2006-09-27 16:09:25 -07002207 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002208
Michael Chan15c3b692006-03-22 01:06:52 -08002209 /* The PHY should not be powered down on some chips because
2210 * of bugs.
2211 */
2212 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2213 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2214 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
2215 (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)))
2216 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08002217
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002218 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2219 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002220 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2221 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2222 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2223 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2224 }
2225
Michael Chan15c3b692006-03-22 01:06:52 -08002226 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2227}
2228
Matt Carlson3f007892008-11-03 16:51:36 -08002229/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002230static int tg3_nvram_lock(struct tg3 *tp)
2231{
2232 if (tp->tg3_flags & TG3_FLAG_NVRAM) {
2233 int i;
2234
2235 if (tp->nvram_lock_cnt == 0) {
2236 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2237 for (i = 0; i < 8000; i++) {
2238 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2239 break;
2240 udelay(20);
2241 }
2242 if (i == 8000) {
2243 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2244 return -ENODEV;
2245 }
2246 }
2247 tp->nvram_lock_cnt++;
2248 }
2249 return 0;
2250}
2251
2252/* tp->lock is held. */
2253static void tg3_nvram_unlock(struct tg3 *tp)
2254{
2255 if (tp->tg3_flags & TG3_FLAG_NVRAM) {
2256 if (tp->nvram_lock_cnt > 0)
2257 tp->nvram_lock_cnt--;
2258 if (tp->nvram_lock_cnt == 0)
2259 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
2260 }
2261}
2262
2263/* tp->lock is held. */
2264static void tg3_enable_nvram_access(struct tg3 *tp)
2265{
2266 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Matt Carlsonf66a29b2009-11-13 13:03:36 +00002267 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002268 u32 nvaccess = tr32(NVRAM_ACCESS);
2269
2270 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
2271 }
2272}
2273
2274/* tp->lock is held. */
2275static void tg3_disable_nvram_access(struct tg3 *tp)
2276{
2277 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Matt Carlsonf66a29b2009-11-13 13:03:36 +00002278 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002279 u32 nvaccess = tr32(NVRAM_ACCESS);
2280
2281 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
2282 }
2283}
2284
2285static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
2286 u32 offset, u32 *val)
2287{
2288 u32 tmp;
2289 int i;
2290
2291 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
2292 return -EINVAL;
2293
2294 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
2295 EEPROM_ADDR_DEVID_MASK |
2296 EEPROM_ADDR_READ);
2297 tw32(GRC_EEPROM_ADDR,
2298 tmp |
2299 (0 << EEPROM_ADDR_DEVID_SHIFT) |
2300 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
2301 EEPROM_ADDR_ADDR_MASK) |
2302 EEPROM_ADDR_READ | EEPROM_ADDR_START);
2303
2304 for (i = 0; i < 1000; i++) {
2305 tmp = tr32(GRC_EEPROM_ADDR);
2306
2307 if (tmp & EEPROM_ADDR_COMPLETE)
2308 break;
2309 msleep(1);
2310 }
2311 if (!(tmp & EEPROM_ADDR_COMPLETE))
2312 return -EBUSY;
2313
Matt Carlson62cedd12009-04-20 14:52:29 -07002314 tmp = tr32(GRC_EEPROM_DATA);
2315
2316 /*
2317 * The data will always be opposite the native endian
2318 * format. Perform a blind byteswap to compensate.
2319 */
2320 *val = swab32(tmp);
2321
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002322 return 0;
2323}
2324
2325#define NVRAM_CMD_TIMEOUT 10000
2326
2327static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
2328{
2329 int i;
2330
2331 tw32(NVRAM_CMD, nvram_cmd);
2332 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
2333 udelay(10);
2334 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
2335 udelay(10);
2336 break;
2337 }
2338 }
2339
2340 if (i == NVRAM_CMD_TIMEOUT)
2341 return -EBUSY;
2342
2343 return 0;
2344}
2345
2346static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
2347{
2348 if ((tp->tg3_flags & TG3_FLAG_NVRAM) &&
2349 (tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) &&
2350 (tp->tg3_flags2 & TG3_FLG2_FLASH) &&
2351 !(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM_ADDR_TRANS) &&
2352 (tp->nvram_jedecnum == JEDEC_ATMEL))
2353
2354 addr = ((addr / tp->nvram_pagesize) <<
2355 ATMEL_AT45DB0X1B_PAGE_POS) +
2356 (addr % tp->nvram_pagesize);
2357
2358 return addr;
2359}
2360
2361static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
2362{
2363 if ((tp->tg3_flags & TG3_FLAG_NVRAM) &&
2364 (tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) &&
2365 (tp->tg3_flags2 & TG3_FLG2_FLASH) &&
2366 !(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM_ADDR_TRANS) &&
2367 (tp->nvram_jedecnum == JEDEC_ATMEL))
2368
2369 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
2370 tp->nvram_pagesize) +
2371 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
2372
2373 return addr;
2374}
2375
Matt Carlsone4f34112009-02-25 14:25:00 +00002376/* NOTE: Data read in from NVRAM is byteswapped according to
2377 * the byteswapping settings for all other register accesses.
2378 * tg3 devices are BE devices, so on a BE machine, the data
2379 * returned will be exactly as it is seen in NVRAM. On a LE
2380 * machine, the 32-bit value will be byteswapped.
2381 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002382static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
2383{
2384 int ret;
2385
2386 if (!(tp->tg3_flags & TG3_FLAG_NVRAM))
2387 return tg3_nvram_read_using_eeprom(tp, offset, val);
2388
2389 offset = tg3_nvram_phys_addr(tp, offset);
2390
2391 if (offset > NVRAM_ADDR_MSK)
2392 return -EINVAL;
2393
2394 ret = tg3_nvram_lock(tp);
2395 if (ret)
2396 return ret;
2397
2398 tg3_enable_nvram_access(tp);
2399
2400 tw32(NVRAM_ADDR, offset);
2401 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
2402 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
2403
2404 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00002405 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002406
2407 tg3_disable_nvram_access(tp);
2408
2409 tg3_nvram_unlock(tp);
2410
2411 return ret;
2412}
2413
Matt Carlsona9dc5292009-02-25 14:25:30 +00002414/* Ensures NVRAM data is in bytestream format. */
2415static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002416{
2417 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00002418 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002419 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00002420 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002421 return res;
2422}
2423
2424/* tp->lock is held. */
Matt Carlson3f007892008-11-03 16:51:36 -08002425static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
2426{
2427 u32 addr_high, addr_low;
2428 int i;
2429
2430 addr_high = ((tp->dev->dev_addr[0] << 8) |
2431 tp->dev->dev_addr[1]);
2432 addr_low = ((tp->dev->dev_addr[2] << 24) |
2433 (tp->dev->dev_addr[3] << 16) |
2434 (tp->dev->dev_addr[4] << 8) |
2435 (tp->dev->dev_addr[5] << 0));
2436 for (i = 0; i < 4; i++) {
2437 if (i == 1 && skip_mac_1)
2438 continue;
2439 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
2440 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
2441 }
2442
2443 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2444 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2445 for (i = 0; i < 12; i++) {
2446 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
2447 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
2448 }
2449 }
2450
2451 addr_high = (tp->dev->dev_addr[0] +
2452 tp->dev->dev_addr[1] +
2453 tp->dev->dev_addr[2] +
2454 tp->dev->dev_addr[3] +
2455 tp->dev->dev_addr[4] +
2456 tp->dev->dev_addr[5]) &
2457 TX_BACKOFF_SEED_MASK;
2458 tw32(MAC_TX_BACKOFF_SEED, addr_high);
2459}
2460
Michael Chanbc1c7562006-03-20 17:48:03 -08002461static int tg3_set_power_state(struct tg3 *tp, pci_power_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462{
2463 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002464 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465
2466 /* Make sure register accesses (indirect or otherwise)
2467 * will function correctly.
2468 */
2469 pci_write_config_dword(tp->pdev,
2470 TG3PCI_MISC_HOST_CTRL,
2471 tp->misc_host_ctrl);
2472
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 switch (state) {
Michael Chanbc1c7562006-03-20 17:48:03 -08002474 case PCI_D0:
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07002475 pci_enable_wake(tp->pdev, state, false);
2476 pci_set_power_state(tp->pdev, PCI_D0);
Michael Chan8c6bda12005-04-21 17:09:08 -07002477
Michael Chan9d26e212006-12-07 00:21:14 -08002478 /* Switch out of Vaux if it is a NIC */
2479 if (tp->tg3_flags2 & TG3_FLG2_IS_NIC)
Michael Chanb401e9e2005-12-19 16:27:04 -08002480 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481
2482 return 0;
2483
Michael Chanbc1c7562006-03-20 17:48:03 -08002484 case PCI_D1:
Michael Chanbc1c7562006-03-20 17:48:03 -08002485 case PCI_D2:
Michael Chanbc1c7562006-03-20 17:48:03 -08002486 case PCI_D3hot:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 break;
2488
2489 default:
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07002490 printk(KERN_ERR PFX "%s: Invalid power state (D%d) requested\n",
2491 tp->dev->name, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002493 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002494
2495 /* Restore the CLKREQ setting. */
2496 if (tp->tg3_flags3 & TG3_FLG3_CLKREQ_BUG) {
2497 u16 lnkctl;
2498
2499 pci_read_config_word(tp->pdev,
2500 tp->pcie_cap + PCI_EXP_LNKCTL,
2501 &lnkctl);
2502 lnkctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
2503 pci_write_config_word(tp->pdev,
2504 tp->pcie_cap + PCI_EXP_LNKCTL,
2505 lnkctl);
2506 }
2507
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
2509 tw32(TG3PCI_MISC_HOST_CTRL,
2510 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
2511
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002512 device_should_wake = pci_pme_capable(tp->pdev, state) &&
2513 device_may_wakeup(&tp->pdev->dev) &&
2514 (tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
2515
Matt Carlsondd477002008-05-25 23:45:58 -07002516 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08002517 do_low_power = false;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002518 if ((tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED) &&
2519 !tp->link_config.phy_is_low_power) {
2520 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002521 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002522
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002523 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002524
2525 tp->link_config.phy_is_low_power = 1;
2526
2527 tp->link_config.orig_speed = phydev->speed;
2528 tp->link_config.orig_duplex = phydev->duplex;
2529 tp->link_config.orig_autoneg = phydev->autoneg;
2530 tp->link_config.orig_advertising = phydev->advertising;
2531
2532 advertising = ADVERTISED_TP |
2533 ADVERTISED_Pause |
2534 ADVERTISED_Autoneg |
2535 ADVERTISED_10baseT_Half;
2536
2537 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002538 device_should_wake) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002539 if (tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB)
2540 advertising |=
2541 ADVERTISED_100baseT_Half |
2542 ADVERTISED_100baseT_Full |
2543 ADVERTISED_10baseT_Full;
2544 else
2545 advertising |= ADVERTISED_10baseT_Full;
2546 }
2547
2548 phydev->advertising = advertising;
2549
2550 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002551
2552 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
2553 if (phyid != TG3_PHY_ID_BCMAC131) {
2554 phyid &= TG3_PHY_OUI_MASK;
Roel Kluinf72b5342009-02-18 17:42:42 -08002555 if (phyid == TG3_PHY_OUI_1 ||
2556 phyid == TG3_PHY_OUI_2 ||
Matt Carlson0a459aa2008-11-03 16:54:15 -08002557 phyid == TG3_PHY_OUI_3)
2558 do_low_power = true;
2559 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002560 }
Matt Carlsondd477002008-05-25 23:45:58 -07002561 } else {
Matt Carlson20232762008-12-21 20:18:56 -08002562 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002563
Matt Carlsondd477002008-05-25 23:45:58 -07002564 if (tp->link_config.phy_is_low_power == 0) {
2565 tp->link_config.phy_is_low_power = 1;
2566 tp->link_config.orig_speed = tp->link_config.speed;
2567 tp->link_config.orig_duplex = tp->link_config.duplex;
2568 tp->link_config.orig_autoneg = tp->link_config.autoneg;
2569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570
Matt Carlsondd477002008-05-25 23:45:58 -07002571 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)) {
2572 tp->link_config.speed = SPEED_10;
2573 tp->link_config.duplex = DUPLEX_HALF;
2574 tp->link_config.autoneg = AUTONEG_ENABLE;
2575 tg3_setup_phy(tp, 0);
2576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 }
2578
Michael Chanb5d37722006-09-27 16:06:21 -07002579 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
2580 u32 val;
2581
2582 val = tr32(GRC_VCPU_EXT_CTRL);
2583 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
2584 } else if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08002585 int i;
2586 u32 val;
2587
2588 for (i = 0; i < 200; i++) {
2589 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
2590 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
2591 break;
2592 msleep(1);
2593 }
2594 }
Gary Zambranoa85feb82007-05-05 11:52:19 -07002595 if (tp->tg3_flags & TG3_FLAG_WOL_CAP)
2596 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
2597 WOL_DRV_STATE_SHUTDOWN |
2598 WOL_DRV_WOL |
2599 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08002600
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002601 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 u32 mac_mode;
2603
2604 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08002605 if (do_low_power) {
Matt Carlsondd477002008-05-25 23:45:58 -07002606 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x5a);
2607 udelay(40);
2608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609
Michael Chan3f7045c2006-09-27 16:02:29 -07002610 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)
2611 mac_mode = MAC_MODE_PORT_MODE_GMII;
2612 else
2613 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002615 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
2616 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2617 ASIC_REV_5700) {
2618 u32 speed = (tp->tg3_flags &
2619 TG3_FLAG_WOL_SPEED_100MB) ?
2620 SPEED_100 : SPEED_10;
2621 if (tg3_5700_link_polarity(tp, speed))
2622 mac_mode |= MAC_MODE_LINK_POLARITY;
2623 else
2624 mac_mode &= ~MAC_MODE_LINK_POLARITY;
2625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 } else {
2627 mac_mode = MAC_MODE_PORT_MODE_TBI;
2628 }
2629
John W. Linvillecbf46852005-04-21 17:01:29 -07002630 if (!(tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 tw32(MAC_LED_CTRL, tp->led_ctrl);
2632
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002633 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
2634 if (((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
2635 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) &&
2636 ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
2637 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)))
2638 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639
Matt Carlson3bda1252008-08-15 14:08:22 -07002640 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
2641 mac_mode |= tp->mac_mode &
2642 (MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN);
2643 if (mac_mode & MAC_MODE_APE_TX_EN)
2644 mac_mode |= MAC_MODE_TDE_ENABLE;
2645 }
2646
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 tw32_f(MAC_MODE, mac_mode);
2648 udelay(100);
2649
2650 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
2651 udelay(10);
2652 }
2653
2654 if (!(tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB) &&
2655 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2656 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
2657 u32 base_val;
2658
2659 base_val = tp->pci_clock_ctrl;
2660 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
2661 CLOCK_CTRL_TXCLK_DISABLE);
2662
Michael Chanb401e9e2005-12-19 16:27:04 -08002663 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
2664 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Michael Chand7b0a852007-02-13 12:17:38 -08002665 } else if ((tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
Matt Carlson795d01c2007-10-07 23:28:17 -07002666 (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
Michael Chand7b0a852007-02-13 12:17:38 -08002667 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)) {
Michael Chan4cf78e42005-07-25 12:29:19 -07002668 /* do nothing */
Michael Chan85e94ce2005-04-21 17:05:28 -07002669 } else if (!((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF))) {
2671 u32 newbits1, newbits2;
2672
2673 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2674 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2675 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
2676 CLOCK_CTRL_TXCLK_DISABLE |
2677 CLOCK_CTRL_ALTCLK);
2678 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2679 } else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
2680 newbits1 = CLOCK_CTRL_625_CORE;
2681 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
2682 } else {
2683 newbits1 = CLOCK_CTRL_ALTCLK;
2684 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2685 }
2686
Michael Chanb401e9e2005-12-19 16:27:04 -08002687 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
2688 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689
Michael Chanb401e9e2005-12-19 16:27:04 -08002690 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
2691 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692
2693 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
2694 u32 newbits3;
2695
2696 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2697 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2698 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
2699 CLOCK_CTRL_TXCLK_DISABLE |
2700 CLOCK_CTRL_44MHZ_CORE);
2701 } else {
2702 newbits3 = CLOCK_CTRL_44MHZ_CORE;
2703 }
2704
Michael Chanb401e9e2005-12-19 16:27:04 -08002705 tw32_wait_f(TG3PCI_CLOCK_CTRL,
2706 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 }
2708 }
2709
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002710 if (!(device_should_wake) &&
Matt Carlson22435842008-11-21 17:21:13 -08002711 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08002712 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08002713
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 tg3_frob_aux_power(tp);
2715
2716 /* Workaround for unstable PLL clock */
2717 if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
2718 (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
2719 u32 val = tr32(0x7d00);
2720
2721 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
2722 tw32(0x7d00, val);
Michael Chan6921d202005-12-13 21:15:53 -08002723 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08002724 int err;
2725
2726 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08002728 if (!err)
2729 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08002730 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 }
2732
Michael Chanbbadf502006-04-06 21:46:34 -07002733 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
2734
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002735 if (device_should_wake)
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07002736 pci_enable_wake(tp->pdev, state, true);
2737
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 /* Finally, set the new power state. */
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07002739 pci_set_power_state(tp->pdev, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 return 0;
2742}
2743
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
2745{
2746 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
2747 case MII_TG3_AUX_STAT_10HALF:
2748 *speed = SPEED_10;
2749 *duplex = DUPLEX_HALF;
2750 break;
2751
2752 case MII_TG3_AUX_STAT_10FULL:
2753 *speed = SPEED_10;
2754 *duplex = DUPLEX_FULL;
2755 break;
2756
2757 case MII_TG3_AUX_STAT_100HALF:
2758 *speed = SPEED_100;
2759 *duplex = DUPLEX_HALF;
2760 break;
2761
2762 case MII_TG3_AUX_STAT_100FULL:
2763 *speed = SPEED_100;
2764 *duplex = DUPLEX_FULL;
2765 break;
2766
2767 case MII_TG3_AUX_STAT_1000HALF:
2768 *speed = SPEED_1000;
2769 *duplex = DUPLEX_HALF;
2770 break;
2771
2772 case MII_TG3_AUX_STAT_1000FULL:
2773 *speed = SPEED_1000;
2774 *duplex = DUPLEX_FULL;
2775 break;
2776
2777 default:
Matt Carlson7f97a4b2009-08-25 10:10:03 +00002778 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07002779 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
2780 SPEED_10;
2781 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
2782 DUPLEX_HALF;
2783 break;
2784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 *speed = SPEED_INVALID;
2786 *duplex = DUPLEX_INVALID;
2787 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789}
2790
2791static void tg3_phy_copper_begin(struct tg3 *tp)
2792{
2793 u32 new_adv;
2794 int i;
2795
2796 if (tp->link_config.phy_is_low_power) {
2797 /* Entering low power mode. Disable gigabit and
2798 * 100baseT advertisements.
2799 */
2800 tg3_writephy(tp, MII_TG3_CTRL, 0);
2801
2802 new_adv = (ADVERTISE_10HALF | ADVERTISE_10FULL |
2803 ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
2804 if (tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB)
2805 new_adv |= (ADVERTISE_100HALF | ADVERTISE_100FULL);
2806
2807 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2808 } else if (tp->link_config.speed == SPEED_INVALID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 if (tp->tg3_flags & TG3_FLAG_10_100_ONLY)
2810 tp->link_config.advertising &=
2811 ~(ADVERTISED_1000baseT_Half |
2812 ADVERTISED_1000baseT_Full);
2813
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002814 new_adv = ADVERTISE_CSMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 if (tp->link_config.advertising & ADVERTISED_10baseT_Half)
2816 new_adv |= ADVERTISE_10HALF;
2817 if (tp->link_config.advertising & ADVERTISED_10baseT_Full)
2818 new_adv |= ADVERTISE_10FULL;
2819 if (tp->link_config.advertising & ADVERTISED_100baseT_Half)
2820 new_adv |= ADVERTISE_100HALF;
2821 if (tp->link_config.advertising & ADVERTISED_100baseT_Full)
2822 new_adv |= ADVERTISE_100FULL;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002823
2824 new_adv |= tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2825
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2827
2828 if (tp->link_config.advertising &
2829 (ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full)) {
2830 new_adv = 0;
2831 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
2832 new_adv |= MII_TG3_CTRL_ADV_1000_HALF;
2833 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
2834 new_adv |= MII_TG3_CTRL_ADV_1000_FULL;
2835 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY) &&
2836 (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2837 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0))
2838 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2839 MII_TG3_CTRL_ENABLE_AS_MASTER);
2840 tg3_writephy(tp, MII_TG3_CTRL, new_adv);
2841 } else {
2842 tg3_writephy(tp, MII_TG3_CTRL, 0);
2843 }
2844 } else {
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002845 new_adv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2846 new_adv |= ADVERTISE_CSMA;
2847
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 /* Asking for a specific link mode. */
2849 if (tp->link_config.speed == SPEED_1000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2851
2852 if (tp->link_config.duplex == DUPLEX_FULL)
2853 new_adv = MII_TG3_CTRL_ADV_1000_FULL;
2854 else
2855 new_adv = MII_TG3_CTRL_ADV_1000_HALF;
2856 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2857 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
2858 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2859 MII_TG3_CTRL_ENABLE_AS_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 if (tp->link_config.speed == SPEED_100) {
2862 if (tp->link_config.duplex == DUPLEX_FULL)
2863 new_adv |= ADVERTISE_100FULL;
2864 else
2865 new_adv |= ADVERTISE_100HALF;
2866 } else {
2867 if (tp->link_config.duplex == DUPLEX_FULL)
2868 new_adv |= ADVERTISE_10FULL;
2869 else
2870 new_adv |= ADVERTISE_10HALF;
2871 }
2872 tg3_writephy(tp, MII_ADVERTISE, new_adv);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002873
2874 new_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002876
2877 tg3_writephy(tp, MII_TG3_CTRL, new_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 }
2879
2880 if (tp->link_config.autoneg == AUTONEG_DISABLE &&
2881 tp->link_config.speed != SPEED_INVALID) {
2882 u32 bmcr, orig_bmcr;
2883
2884 tp->link_config.active_speed = tp->link_config.speed;
2885 tp->link_config.active_duplex = tp->link_config.duplex;
2886
2887 bmcr = 0;
2888 switch (tp->link_config.speed) {
2889 default:
2890 case SPEED_10:
2891 break;
2892
2893 case SPEED_100:
2894 bmcr |= BMCR_SPEED100;
2895 break;
2896
2897 case SPEED_1000:
2898 bmcr |= TG3_BMCR_SPEED1000;
2899 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901
2902 if (tp->link_config.duplex == DUPLEX_FULL)
2903 bmcr |= BMCR_FULLDPLX;
2904
2905 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
2906 (bmcr != orig_bmcr)) {
2907 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
2908 for (i = 0; i < 1500; i++) {
2909 u32 tmp;
2910
2911 udelay(10);
2912 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
2913 tg3_readphy(tp, MII_BMSR, &tmp))
2914 continue;
2915 if (!(tmp & BMSR_LSTATUS)) {
2916 udelay(40);
2917 break;
2918 }
2919 }
2920 tg3_writephy(tp, MII_BMCR, bmcr);
2921 udelay(40);
2922 }
2923 } else {
2924 tg3_writephy(tp, MII_BMCR,
2925 BMCR_ANENABLE | BMCR_ANRESTART);
2926 }
2927}
2928
2929static int tg3_init_5401phy_dsp(struct tg3 *tp)
2930{
2931 int err;
2932
2933 /* Turn off tap power management. */
2934 /* Set Extended packet length bit */
2935 err = tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4c20);
2936
2937 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x0012);
2938 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x1804);
2939
2940 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x0013);
2941 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x1204);
2942
2943 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8006);
2944 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0132);
2945
2946 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8006);
2947 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0232);
2948
2949 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x201f);
2950 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0a20);
2951
2952 udelay(40);
2953
2954 return err;
2955}
2956
Michael Chan3600d912006-12-07 00:21:48 -08002957static int tg3_copper_is_advertising_all(struct tg3 *tp, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958{
Michael Chan3600d912006-12-07 00:21:48 -08002959 u32 adv_reg, all_mask = 0;
2960
2961 if (mask & ADVERTISED_10baseT_Half)
2962 all_mask |= ADVERTISE_10HALF;
2963 if (mask & ADVERTISED_10baseT_Full)
2964 all_mask |= ADVERTISE_10FULL;
2965 if (mask & ADVERTISED_100baseT_Half)
2966 all_mask |= ADVERTISE_100HALF;
2967 if (mask & ADVERTISED_100baseT_Full)
2968 all_mask |= ADVERTISE_100FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969
2970 if (tg3_readphy(tp, MII_ADVERTISE, &adv_reg))
2971 return 0;
2972
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 if ((adv_reg & all_mask) != all_mask)
2974 return 0;
2975 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY)) {
2976 u32 tg3_ctrl;
2977
Michael Chan3600d912006-12-07 00:21:48 -08002978 all_mask = 0;
2979 if (mask & ADVERTISED_1000baseT_Half)
2980 all_mask |= ADVERTISE_1000HALF;
2981 if (mask & ADVERTISED_1000baseT_Full)
2982 all_mask |= ADVERTISE_1000FULL;
2983
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 if (tg3_readphy(tp, MII_TG3_CTRL, &tg3_ctrl))
2985 return 0;
2986
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 if ((tg3_ctrl & all_mask) != all_mask)
2988 return 0;
2989 }
2990 return 1;
2991}
2992
Matt Carlsonef167e22007-12-20 20:10:01 -08002993static int tg3_adv_1000T_flowctrl_ok(struct tg3 *tp, u32 *lcladv, u32 *rmtadv)
2994{
2995 u32 curadv, reqadv;
2996
2997 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
2998 return 1;
2999
3000 curadv = *lcladv & (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
3001 reqadv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
3002
3003 if (tp->link_config.active_duplex == DUPLEX_FULL) {
3004 if (curadv != reqadv)
3005 return 0;
3006
3007 if (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG)
3008 tg3_readphy(tp, MII_LPA, rmtadv);
3009 } else {
3010 /* Reprogram the advertisement register, even if it
3011 * does not affect the current link. If the link
3012 * gets renegotiated in the future, we can save an
3013 * additional renegotiation cycle by advertising
3014 * it correctly in the first place.
3015 */
3016 if (curadv != reqadv) {
3017 *lcladv &= ~(ADVERTISE_PAUSE_CAP |
3018 ADVERTISE_PAUSE_ASYM);
3019 tg3_writephy(tp, MII_ADVERTISE, *lcladv | reqadv);
3020 }
3021 }
3022
3023 return 1;
3024}
3025
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
3027{
3028 int current_link_up;
3029 u32 bmsr, dummy;
Matt Carlsonef167e22007-12-20 20:10:01 -08003030 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 u16 current_speed;
3032 u8 current_duplex;
3033 int i, err;
3034
3035 tw32(MAC_EVENT, 0);
3036
3037 tw32_f(MAC_STATUS,
3038 (MAC_STATUS_SYNC_CHANGED |
3039 MAC_STATUS_CFG_CHANGED |
3040 MAC_STATUS_MI_COMPLETION |
3041 MAC_STATUS_LNKSTATE_CHANGED));
3042 udelay(40);
3043
Matt Carlson8ef21422008-05-02 16:47:53 -07003044 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
3045 tw32_f(MAC_MI_MODE,
3046 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
3047 udelay(80);
3048 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049
3050 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x02);
3051
3052 /* Some third-party PHYs need to be reset on link going
3053 * down.
3054 */
3055 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3056 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
3057 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
3058 netif_carrier_ok(tp->dev)) {
3059 tg3_readphy(tp, MII_BMSR, &bmsr);
3060 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3061 !(bmsr & BMSR_LSTATUS))
3062 force_reset = 1;
3063 }
3064 if (force_reset)
3065 tg3_phy_reset(tp);
3066
3067 if ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5401) {
3068 tg3_readphy(tp, MII_BMSR, &bmsr);
3069 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
3070 !(tp->tg3_flags & TG3_FLAG_INIT_COMPLETE))
3071 bmsr = 0;
3072
3073 if (!(bmsr & BMSR_LSTATUS)) {
3074 err = tg3_init_5401phy_dsp(tp);
3075 if (err)
3076 return err;
3077
3078 tg3_readphy(tp, MII_BMSR, &bmsr);
3079 for (i = 0; i < 1000; i++) {
3080 udelay(10);
3081 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3082 (bmsr & BMSR_LSTATUS)) {
3083 udelay(40);
3084 break;
3085 }
3086 }
3087
3088 if ((tp->phy_id & PHY_ID_REV_MASK) == PHY_REV_BCM5401_B0 &&
3089 !(bmsr & BMSR_LSTATUS) &&
3090 tp->link_config.active_speed == SPEED_1000) {
3091 err = tg3_phy_reset(tp);
3092 if (!err)
3093 err = tg3_init_5401phy_dsp(tp);
3094 if (err)
3095 return err;
3096 }
3097 }
3098 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3099 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
3100 /* 5701 {A0,B0} CRC bug workaround */
3101 tg3_writephy(tp, 0x15, 0x0a75);
3102 tg3_writephy(tp, 0x1c, 0x8c68);
3103 tg3_writephy(tp, 0x1c, 0x8d68);
3104 tg3_writephy(tp, 0x1c, 0x8c68);
3105 }
3106
3107 /* Clear pending interrupts... */
3108 tg3_readphy(tp, MII_TG3_ISTAT, &dummy);
3109 tg3_readphy(tp, MII_TG3_ISTAT, &dummy);
3110
3111 if (tp->tg3_flags & TG3_FLAG_USE_MI_INTERRUPT)
3112 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlson7f97a4b2009-08-25 10:10:03 +00003113 else if (!(tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 tg3_writephy(tp, MII_TG3_IMASK, ~0);
3115
3116 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3117 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3118 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
3119 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3120 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
3121 else
3122 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
3123 }
3124
3125 current_link_up = 0;
3126 current_speed = SPEED_INVALID;
3127 current_duplex = DUPLEX_INVALID;
3128
3129 if (tp->tg3_flags2 & TG3_FLG2_CAPACITIVE_COUPLING) {
3130 u32 val;
3131
3132 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4007);
3133 tg3_readphy(tp, MII_TG3_AUX_CTRL, &val);
3134 if (!(val & (1 << 10))) {
3135 val |= (1 << 10);
3136 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
3137 goto relink;
3138 }
3139 }
3140
3141 bmsr = 0;
3142 for (i = 0; i < 100; i++) {
3143 tg3_readphy(tp, MII_BMSR, &bmsr);
3144 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3145 (bmsr & BMSR_LSTATUS))
3146 break;
3147 udelay(40);
3148 }
3149
3150 if (bmsr & BMSR_LSTATUS) {
3151 u32 aux_stat, bmcr;
3152
3153 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
3154 for (i = 0; i < 2000; i++) {
3155 udelay(10);
3156 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
3157 aux_stat)
3158 break;
3159 }
3160
3161 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
3162 &current_speed,
3163 &current_duplex);
3164
3165 bmcr = 0;
3166 for (i = 0; i < 200; i++) {
3167 tg3_readphy(tp, MII_BMCR, &bmcr);
3168 if (tg3_readphy(tp, MII_BMCR, &bmcr))
3169 continue;
3170 if (bmcr && bmcr != 0x7fff)
3171 break;
3172 udelay(10);
3173 }
3174
Matt Carlsonef167e22007-12-20 20:10:01 -08003175 lcl_adv = 0;
3176 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177
Matt Carlsonef167e22007-12-20 20:10:01 -08003178 tp->link_config.active_speed = current_speed;
3179 tp->link_config.active_duplex = current_duplex;
3180
3181 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3182 if ((bmcr & BMCR_ANENABLE) &&
3183 tg3_copper_is_advertising_all(tp,
3184 tp->link_config.advertising)) {
3185 if (tg3_adv_1000T_flowctrl_ok(tp, &lcl_adv,
3186 &rmt_adv))
3187 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 }
3189 } else {
3190 if (!(bmcr & BMCR_ANENABLE) &&
3191 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08003192 tp->link_config.duplex == current_duplex &&
3193 tp->link_config.flowctrl ==
3194 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 }
3197 }
3198
Matt Carlsonef167e22007-12-20 20:10:01 -08003199 if (current_link_up == 1 &&
3200 tp->link_config.active_duplex == DUPLEX_FULL)
3201 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 }
3203
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204relink:
Michael Chan6921d202005-12-13 21:15:53 -08003205 if (current_link_up == 0 || tp->link_config.phy_is_low_power) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 u32 tmp;
3207
3208 tg3_phy_copper_begin(tp);
3209
3210 tg3_readphy(tp, MII_BMSR, &tmp);
3211 if (!tg3_readphy(tp, MII_BMSR, &tmp) &&
3212 (tmp & BMSR_LSTATUS))
3213 current_link_up = 1;
3214 }
3215
3216 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
3217 if (current_link_up == 1) {
3218 if (tp->link_config.active_speed == SPEED_100 ||
3219 tp->link_config.active_speed == SPEED_10)
3220 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3221 else
3222 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlson7f97a4b2009-08-25 10:10:03 +00003223 } else if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET)
3224 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3225 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
3227
3228 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
3229 if (tp->link_config.active_duplex == DUPLEX_HALF)
3230 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
3231
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003233 if (current_link_up == 1 &&
3234 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003236 else
3237 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 }
3239
3240 /* ??? Without this setting Netgear GA302T PHY does not
3241 * ??? send/receive packets...
3242 */
3243 if ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5411 &&
3244 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
3245 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
3246 tw32_f(MAC_MI_MODE, tp->mi_mode);
3247 udelay(80);
3248 }
3249
3250 tw32_f(MAC_MODE, tp->mac_mode);
3251 udelay(40);
3252
3253 if (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) {
3254 /* Polled via timer. */
3255 tw32_f(MAC_EVENT, 0);
3256 } else {
3257 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
3258 }
3259 udelay(40);
3260
3261 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
3262 current_link_up == 1 &&
3263 tp->link_config.active_speed == SPEED_1000 &&
3264 ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) ||
3265 (tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED))) {
3266 udelay(120);
3267 tw32_f(MAC_STATUS,
3268 (MAC_STATUS_SYNC_CHANGED |
3269 MAC_STATUS_CFG_CHANGED));
3270 udelay(40);
3271 tg3_write_mem(tp,
3272 NIC_SRAM_FIRMWARE_MBOX,
3273 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
3274 }
3275
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003276 /* Prevent send BD corruption. */
3277 if (tp->tg3_flags3 & TG3_FLG3_CLKREQ_BUG) {
3278 u16 oldlnkctl, newlnkctl;
3279
3280 pci_read_config_word(tp->pdev,
3281 tp->pcie_cap + PCI_EXP_LNKCTL,
3282 &oldlnkctl);
3283 if (tp->link_config.active_speed == SPEED_100 ||
3284 tp->link_config.active_speed == SPEED_10)
3285 newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
3286 else
3287 newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
3288 if (newlnkctl != oldlnkctl)
3289 pci_write_config_word(tp->pdev,
3290 tp->pcie_cap + PCI_EXP_LNKCTL,
3291 newlnkctl);
3292 }
3293
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 if (current_link_up != netif_carrier_ok(tp->dev)) {
3295 if (current_link_up)
3296 netif_carrier_on(tp->dev);
3297 else
3298 netif_carrier_off(tp->dev);
3299 tg3_link_report(tp);
3300 }
3301
3302 return 0;
3303}
3304
3305struct tg3_fiber_aneginfo {
3306 int state;
3307#define ANEG_STATE_UNKNOWN 0
3308#define ANEG_STATE_AN_ENABLE 1
3309#define ANEG_STATE_RESTART_INIT 2
3310#define ANEG_STATE_RESTART 3
3311#define ANEG_STATE_DISABLE_LINK_OK 4
3312#define ANEG_STATE_ABILITY_DETECT_INIT 5
3313#define ANEG_STATE_ABILITY_DETECT 6
3314#define ANEG_STATE_ACK_DETECT_INIT 7
3315#define ANEG_STATE_ACK_DETECT 8
3316#define ANEG_STATE_COMPLETE_ACK_INIT 9
3317#define ANEG_STATE_COMPLETE_ACK 10
3318#define ANEG_STATE_IDLE_DETECT_INIT 11
3319#define ANEG_STATE_IDLE_DETECT 12
3320#define ANEG_STATE_LINK_OK 13
3321#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
3322#define ANEG_STATE_NEXT_PAGE_WAIT 15
3323
3324 u32 flags;
3325#define MR_AN_ENABLE 0x00000001
3326#define MR_RESTART_AN 0x00000002
3327#define MR_AN_COMPLETE 0x00000004
3328#define MR_PAGE_RX 0x00000008
3329#define MR_NP_LOADED 0x00000010
3330#define MR_TOGGLE_TX 0x00000020
3331#define MR_LP_ADV_FULL_DUPLEX 0x00000040
3332#define MR_LP_ADV_HALF_DUPLEX 0x00000080
3333#define MR_LP_ADV_SYM_PAUSE 0x00000100
3334#define MR_LP_ADV_ASYM_PAUSE 0x00000200
3335#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
3336#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
3337#define MR_LP_ADV_NEXT_PAGE 0x00001000
3338#define MR_TOGGLE_RX 0x00002000
3339#define MR_NP_RX 0x00004000
3340
3341#define MR_LINK_OK 0x80000000
3342
3343 unsigned long link_time, cur_time;
3344
3345 u32 ability_match_cfg;
3346 int ability_match_count;
3347
3348 char ability_match, idle_match, ack_match;
3349
3350 u32 txconfig, rxconfig;
3351#define ANEG_CFG_NP 0x00000080
3352#define ANEG_CFG_ACK 0x00000040
3353#define ANEG_CFG_RF2 0x00000020
3354#define ANEG_CFG_RF1 0x00000010
3355#define ANEG_CFG_PS2 0x00000001
3356#define ANEG_CFG_PS1 0x00008000
3357#define ANEG_CFG_HD 0x00004000
3358#define ANEG_CFG_FD 0x00002000
3359#define ANEG_CFG_INVAL 0x00001f06
3360
3361};
3362#define ANEG_OK 0
3363#define ANEG_DONE 1
3364#define ANEG_TIMER_ENAB 2
3365#define ANEG_FAILED -1
3366
3367#define ANEG_STATE_SETTLE_TIME 10000
3368
3369static int tg3_fiber_aneg_smachine(struct tg3 *tp,
3370 struct tg3_fiber_aneginfo *ap)
3371{
Matt Carlson5be73b42007-12-20 20:09:29 -08003372 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 unsigned long delta;
3374 u32 rx_cfg_reg;
3375 int ret;
3376
3377 if (ap->state == ANEG_STATE_UNKNOWN) {
3378 ap->rxconfig = 0;
3379 ap->link_time = 0;
3380 ap->cur_time = 0;
3381 ap->ability_match_cfg = 0;
3382 ap->ability_match_count = 0;
3383 ap->ability_match = 0;
3384 ap->idle_match = 0;
3385 ap->ack_match = 0;
3386 }
3387 ap->cur_time++;
3388
3389 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
3390 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
3391
3392 if (rx_cfg_reg != ap->ability_match_cfg) {
3393 ap->ability_match_cfg = rx_cfg_reg;
3394 ap->ability_match = 0;
3395 ap->ability_match_count = 0;
3396 } else {
3397 if (++ap->ability_match_count > 1) {
3398 ap->ability_match = 1;
3399 ap->ability_match_cfg = rx_cfg_reg;
3400 }
3401 }
3402 if (rx_cfg_reg & ANEG_CFG_ACK)
3403 ap->ack_match = 1;
3404 else
3405 ap->ack_match = 0;
3406
3407 ap->idle_match = 0;
3408 } else {
3409 ap->idle_match = 1;
3410 ap->ability_match_cfg = 0;
3411 ap->ability_match_count = 0;
3412 ap->ability_match = 0;
3413 ap->ack_match = 0;
3414
3415 rx_cfg_reg = 0;
3416 }
3417
3418 ap->rxconfig = rx_cfg_reg;
3419 ret = ANEG_OK;
3420
3421 switch(ap->state) {
3422 case ANEG_STATE_UNKNOWN:
3423 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
3424 ap->state = ANEG_STATE_AN_ENABLE;
3425
3426 /* fallthru */
3427 case ANEG_STATE_AN_ENABLE:
3428 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
3429 if (ap->flags & MR_AN_ENABLE) {
3430 ap->link_time = 0;
3431 ap->cur_time = 0;
3432 ap->ability_match_cfg = 0;
3433 ap->ability_match_count = 0;
3434 ap->ability_match = 0;
3435 ap->idle_match = 0;
3436 ap->ack_match = 0;
3437
3438 ap->state = ANEG_STATE_RESTART_INIT;
3439 } else {
3440 ap->state = ANEG_STATE_DISABLE_LINK_OK;
3441 }
3442 break;
3443
3444 case ANEG_STATE_RESTART_INIT:
3445 ap->link_time = ap->cur_time;
3446 ap->flags &= ~(MR_NP_LOADED);
3447 ap->txconfig = 0;
3448 tw32(MAC_TX_AUTO_NEG, 0);
3449 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3450 tw32_f(MAC_MODE, tp->mac_mode);
3451 udelay(40);
3452
3453 ret = ANEG_TIMER_ENAB;
3454 ap->state = ANEG_STATE_RESTART;
3455
3456 /* fallthru */
3457 case ANEG_STATE_RESTART:
3458 delta = ap->cur_time - ap->link_time;
3459 if (delta > ANEG_STATE_SETTLE_TIME) {
3460 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
3461 } else {
3462 ret = ANEG_TIMER_ENAB;
3463 }
3464 break;
3465
3466 case ANEG_STATE_DISABLE_LINK_OK:
3467 ret = ANEG_DONE;
3468 break;
3469
3470 case ANEG_STATE_ABILITY_DETECT_INIT:
3471 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08003472 ap->txconfig = ANEG_CFG_FD;
3473 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3474 if (flowctrl & ADVERTISE_1000XPAUSE)
3475 ap->txconfig |= ANEG_CFG_PS1;
3476 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3477 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3479 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3480 tw32_f(MAC_MODE, tp->mac_mode);
3481 udelay(40);
3482
3483 ap->state = ANEG_STATE_ABILITY_DETECT;
3484 break;
3485
3486 case ANEG_STATE_ABILITY_DETECT:
3487 if (ap->ability_match != 0 && ap->rxconfig != 0) {
3488 ap->state = ANEG_STATE_ACK_DETECT_INIT;
3489 }
3490 break;
3491
3492 case ANEG_STATE_ACK_DETECT_INIT:
3493 ap->txconfig |= ANEG_CFG_ACK;
3494 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3495 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3496 tw32_f(MAC_MODE, tp->mac_mode);
3497 udelay(40);
3498
3499 ap->state = ANEG_STATE_ACK_DETECT;
3500
3501 /* fallthru */
3502 case ANEG_STATE_ACK_DETECT:
3503 if (ap->ack_match != 0) {
3504 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
3505 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
3506 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
3507 } else {
3508 ap->state = ANEG_STATE_AN_ENABLE;
3509 }
3510 } else if (ap->ability_match != 0 &&
3511 ap->rxconfig == 0) {
3512 ap->state = ANEG_STATE_AN_ENABLE;
3513 }
3514 break;
3515
3516 case ANEG_STATE_COMPLETE_ACK_INIT:
3517 if (ap->rxconfig & ANEG_CFG_INVAL) {
3518 ret = ANEG_FAILED;
3519 break;
3520 }
3521 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
3522 MR_LP_ADV_HALF_DUPLEX |
3523 MR_LP_ADV_SYM_PAUSE |
3524 MR_LP_ADV_ASYM_PAUSE |
3525 MR_LP_ADV_REMOTE_FAULT1 |
3526 MR_LP_ADV_REMOTE_FAULT2 |
3527 MR_LP_ADV_NEXT_PAGE |
3528 MR_TOGGLE_RX |
3529 MR_NP_RX);
3530 if (ap->rxconfig & ANEG_CFG_FD)
3531 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
3532 if (ap->rxconfig & ANEG_CFG_HD)
3533 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
3534 if (ap->rxconfig & ANEG_CFG_PS1)
3535 ap->flags |= MR_LP_ADV_SYM_PAUSE;
3536 if (ap->rxconfig & ANEG_CFG_PS2)
3537 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
3538 if (ap->rxconfig & ANEG_CFG_RF1)
3539 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
3540 if (ap->rxconfig & ANEG_CFG_RF2)
3541 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
3542 if (ap->rxconfig & ANEG_CFG_NP)
3543 ap->flags |= MR_LP_ADV_NEXT_PAGE;
3544
3545 ap->link_time = ap->cur_time;
3546
3547 ap->flags ^= (MR_TOGGLE_TX);
3548 if (ap->rxconfig & 0x0008)
3549 ap->flags |= MR_TOGGLE_RX;
3550 if (ap->rxconfig & ANEG_CFG_NP)
3551 ap->flags |= MR_NP_RX;
3552 ap->flags |= MR_PAGE_RX;
3553
3554 ap->state = ANEG_STATE_COMPLETE_ACK;
3555 ret = ANEG_TIMER_ENAB;
3556 break;
3557
3558 case ANEG_STATE_COMPLETE_ACK:
3559 if (ap->ability_match != 0 &&
3560 ap->rxconfig == 0) {
3561 ap->state = ANEG_STATE_AN_ENABLE;
3562 break;
3563 }
3564 delta = ap->cur_time - ap->link_time;
3565 if (delta > ANEG_STATE_SETTLE_TIME) {
3566 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
3567 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3568 } else {
3569 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
3570 !(ap->flags & MR_NP_RX)) {
3571 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3572 } else {
3573 ret = ANEG_FAILED;
3574 }
3575 }
3576 }
3577 break;
3578
3579 case ANEG_STATE_IDLE_DETECT_INIT:
3580 ap->link_time = ap->cur_time;
3581 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3582 tw32_f(MAC_MODE, tp->mac_mode);
3583 udelay(40);
3584
3585 ap->state = ANEG_STATE_IDLE_DETECT;
3586 ret = ANEG_TIMER_ENAB;
3587 break;
3588
3589 case ANEG_STATE_IDLE_DETECT:
3590 if (ap->ability_match != 0 &&
3591 ap->rxconfig == 0) {
3592 ap->state = ANEG_STATE_AN_ENABLE;
3593 break;
3594 }
3595 delta = ap->cur_time - ap->link_time;
3596 if (delta > ANEG_STATE_SETTLE_TIME) {
3597 /* XXX another gem from the Broadcom driver :( */
3598 ap->state = ANEG_STATE_LINK_OK;
3599 }
3600 break;
3601
3602 case ANEG_STATE_LINK_OK:
3603 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
3604 ret = ANEG_DONE;
3605 break;
3606
3607 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
3608 /* ??? unimplemented */
3609 break;
3610
3611 case ANEG_STATE_NEXT_PAGE_WAIT:
3612 /* ??? unimplemented */
3613 break;
3614
3615 default:
3616 ret = ANEG_FAILED;
3617 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619
3620 return ret;
3621}
3622
Matt Carlson5be73b42007-12-20 20:09:29 -08003623static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624{
3625 int res = 0;
3626 struct tg3_fiber_aneginfo aninfo;
3627 int status = ANEG_FAILED;
3628 unsigned int tick;
3629 u32 tmp;
3630
3631 tw32_f(MAC_TX_AUTO_NEG, 0);
3632
3633 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
3634 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
3635 udelay(40);
3636
3637 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
3638 udelay(40);
3639
3640 memset(&aninfo, 0, sizeof(aninfo));
3641 aninfo.flags |= MR_AN_ENABLE;
3642 aninfo.state = ANEG_STATE_UNKNOWN;
3643 aninfo.cur_time = 0;
3644 tick = 0;
3645 while (++tick < 195000) {
3646 status = tg3_fiber_aneg_smachine(tp, &aninfo);
3647 if (status == ANEG_DONE || status == ANEG_FAILED)
3648 break;
3649
3650 udelay(1);
3651 }
3652
3653 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3654 tw32_f(MAC_MODE, tp->mac_mode);
3655 udelay(40);
3656
Matt Carlson5be73b42007-12-20 20:09:29 -08003657 *txflags = aninfo.txconfig;
3658 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659
3660 if (status == ANEG_DONE &&
3661 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
3662 MR_LP_ADV_FULL_DUPLEX)))
3663 res = 1;
3664
3665 return res;
3666}
3667
3668static void tg3_init_bcm8002(struct tg3 *tp)
3669{
3670 u32 mac_status = tr32(MAC_STATUS);
3671 int i;
3672
3673 /* Reset when initting first time or we have a link. */
3674 if ((tp->tg3_flags & TG3_FLAG_INIT_COMPLETE) &&
3675 !(mac_status & MAC_STATUS_PCS_SYNCED))
3676 return;
3677
3678 /* Set PLL lock range. */
3679 tg3_writephy(tp, 0x16, 0x8007);
3680
3681 /* SW reset */
3682 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
3683
3684 /* Wait for reset to complete. */
3685 /* XXX schedule_timeout() ... */
3686 for (i = 0; i < 500; i++)
3687 udelay(10);
3688
3689 /* Config mode; select PMA/Ch 1 regs. */
3690 tg3_writephy(tp, 0x10, 0x8411);
3691
3692 /* Enable auto-lock and comdet, select txclk for tx. */
3693 tg3_writephy(tp, 0x11, 0x0a10);
3694
3695 tg3_writephy(tp, 0x18, 0x00a0);
3696 tg3_writephy(tp, 0x16, 0x41ff);
3697
3698 /* Assert and deassert POR. */
3699 tg3_writephy(tp, 0x13, 0x0400);
3700 udelay(40);
3701 tg3_writephy(tp, 0x13, 0x0000);
3702
3703 tg3_writephy(tp, 0x11, 0x0a50);
3704 udelay(40);
3705 tg3_writephy(tp, 0x11, 0x0a10);
3706
3707 /* Wait for signal to stabilize */
3708 /* XXX schedule_timeout() ... */
3709 for (i = 0; i < 15000; i++)
3710 udelay(10);
3711
3712 /* Deselect the channel register so we can read the PHYID
3713 * later.
3714 */
3715 tg3_writephy(tp, 0x10, 0x8011);
3716}
3717
3718static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
3719{
Matt Carlson82cd3d12007-12-20 20:09:00 -08003720 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 u32 sg_dig_ctrl, sg_dig_status;
3722 u32 serdes_cfg, expected_sg_dig_ctrl;
3723 int workaround, port_a;
3724 int current_link_up;
3725
3726 serdes_cfg = 0;
3727 expected_sg_dig_ctrl = 0;
3728 workaround = 0;
3729 port_a = 1;
3730 current_link_up = 0;
3731
3732 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
3733 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
3734 workaround = 1;
3735 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
3736 port_a = 0;
3737
3738 /* preserve bits 0-11,13,14 for signal pre-emphasis */
3739 /* preserve bits 20-23 for voltage regulator */
3740 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
3741 }
3742
3743 sg_dig_ctrl = tr32(SG_DIG_CTRL);
3744
3745 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003746 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 if (workaround) {
3748 u32 val = serdes_cfg;
3749
3750 if (port_a)
3751 val |= 0xc010000;
3752 else
3753 val |= 0x4010000;
3754 tw32_f(MAC_SERDES_CFG, val);
3755 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003756
3757 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758 }
3759 if (mac_status & MAC_STATUS_PCS_SYNCED) {
3760 tg3_setup_flow_control(tp, 0, 0);
3761 current_link_up = 1;
3762 }
3763 goto out;
3764 }
3765
3766 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003767 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768
Matt Carlson82cd3d12007-12-20 20:09:00 -08003769 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3770 if (flowctrl & ADVERTISE_1000XPAUSE)
3771 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
3772 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3773 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774
3775 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003776 if ((tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT) &&
3777 tp->serdes_counter &&
3778 ((mac_status & (MAC_STATUS_PCS_SYNCED |
3779 MAC_STATUS_RCVD_CFG)) ==
3780 MAC_STATUS_PCS_SYNCED)) {
3781 tp->serdes_counter--;
3782 current_link_up = 1;
3783 goto out;
3784 }
3785restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 if (workaround)
3787 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003788 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 udelay(5);
3790 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
3791
Michael Chan3d3ebe72006-09-27 15:59:15 -07003792 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
3793 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
3795 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003796 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 mac_status = tr32(MAC_STATUS);
3798
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003799 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08003801 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802
Matt Carlson82cd3d12007-12-20 20:09:00 -08003803 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
3804 local_adv |= ADVERTISE_1000XPAUSE;
3805 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
3806 local_adv |= ADVERTISE_1000XPSE_ASYM;
3807
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003808 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003809 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003810 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003811 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812
3813 tg3_setup_flow_control(tp, local_adv, remote_adv);
3814 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003815 tp->serdes_counter = 0;
3816 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003817 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003818 if (tp->serdes_counter)
3819 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 else {
3821 if (workaround) {
3822 u32 val = serdes_cfg;
3823
3824 if (port_a)
3825 val |= 0xc010000;
3826 else
3827 val |= 0x4010000;
3828
3829 tw32_f(MAC_SERDES_CFG, val);
3830 }
3831
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003832 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 udelay(40);
3834
3835 /* Link parallel detection - link is up */
3836 /* only if we have PCS_SYNC and not */
3837 /* receiving config code words */
3838 mac_status = tr32(MAC_STATUS);
3839 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
3840 !(mac_status & MAC_STATUS_RCVD_CFG)) {
3841 tg3_setup_flow_control(tp, 0, 0);
3842 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003843 tp->tg3_flags2 |=
3844 TG3_FLG2_PARALLEL_DETECT;
3845 tp->serdes_counter =
3846 SERDES_PARALLEL_DET_TIMEOUT;
3847 } else
3848 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 }
3850 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07003851 } else {
3852 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
3853 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854 }
3855
3856out:
3857 return current_link_up;
3858}
3859
3860static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
3861{
3862 int current_link_up = 0;
3863
Michael Chan5cf64b8a2007-05-05 12:11:21 -07003864 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866
3867 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08003868 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003870
Matt Carlson5be73b42007-12-20 20:09:29 -08003871 if (fiber_autoneg(tp, &txflags, &rxflags)) {
3872 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873
Matt Carlson5be73b42007-12-20 20:09:29 -08003874 if (txflags & ANEG_CFG_PS1)
3875 local_adv |= ADVERTISE_1000XPAUSE;
3876 if (txflags & ANEG_CFG_PS2)
3877 local_adv |= ADVERTISE_1000XPSE_ASYM;
3878
3879 if (rxflags & MR_LP_ADV_SYM_PAUSE)
3880 remote_adv |= LPA_1000XPAUSE;
3881 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
3882 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883
3884 tg3_setup_flow_control(tp, local_adv, remote_adv);
3885
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 current_link_up = 1;
3887 }
3888 for (i = 0; i < 30; i++) {
3889 udelay(20);
3890 tw32_f(MAC_STATUS,
3891 (MAC_STATUS_SYNC_CHANGED |
3892 MAC_STATUS_CFG_CHANGED));
3893 udelay(40);
3894 if ((tr32(MAC_STATUS) &
3895 (MAC_STATUS_SYNC_CHANGED |
3896 MAC_STATUS_CFG_CHANGED)) == 0)
3897 break;
3898 }
3899
3900 mac_status = tr32(MAC_STATUS);
3901 if (current_link_up == 0 &&
3902 (mac_status & MAC_STATUS_PCS_SYNCED) &&
3903 !(mac_status & MAC_STATUS_RCVD_CFG))
3904 current_link_up = 1;
3905 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08003906 tg3_setup_flow_control(tp, 0, 0);
3907
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 /* Forcing 1000FD link up. */
3909 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910
3911 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
3912 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003913
3914 tw32_f(MAC_MODE, tp->mac_mode);
3915 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 }
3917
3918out:
3919 return current_link_up;
3920}
3921
3922static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
3923{
3924 u32 orig_pause_cfg;
3925 u16 orig_active_speed;
3926 u8 orig_active_duplex;
3927 u32 mac_status;
3928 int current_link_up;
3929 int i;
3930
Matt Carlson8d018622007-12-20 20:05:44 -08003931 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932 orig_active_speed = tp->link_config.active_speed;
3933 orig_active_duplex = tp->link_config.active_duplex;
3934
3935 if (!(tp->tg3_flags2 & TG3_FLG2_HW_AUTONEG) &&
3936 netif_carrier_ok(tp->dev) &&
3937 (tp->tg3_flags & TG3_FLAG_INIT_COMPLETE)) {
3938 mac_status = tr32(MAC_STATUS);
3939 mac_status &= (MAC_STATUS_PCS_SYNCED |
3940 MAC_STATUS_SIGNAL_DET |
3941 MAC_STATUS_CFG_CHANGED |
3942 MAC_STATUS_RCVD_CFG);
3943 if (mac_status == (MAC_STATUS_PCS_SYNCED |
3944 MAC_STATUS_SIGNAL_DET)) {
3945 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
3946 MAC_STATUS_CFG_CHANGED));
3947 return 0;
3948 }
3949 }
3950
3951 tw32_f(MAC_TX_AUTO_NEG, 0);
3952
3953 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
3954 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
3955 tw32_f(MAC_MODE, tp->mac_mode);
3956 udelay(40);
3957
3958 if (tp->phy_id == PHY_ID_BCM8002)
3959 tg3_init_bcm8002(tp);
3960
3961 /* Enable link change event even when serdes polling. */
3962 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
3963 udelay(40);
3964
3965 current_link_up = 0;
3966 mac_status = tr32(MAC_STATUS);
3967
3968 if (tp->tg3_flags2 & TG3_FLG2_HW_AUTONEG)
3969 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
3970 else
3971 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
3972
Matt Carlson898a56f2009-08-28 14:02:40 +00003973 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00003975 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976
3977 for (i = 0; i < 100; i++) {
3978 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
3979 MAC_STATUS_CFG_CHANGED));
3980 udelay(5);
3981 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07003982 MAC_STATUS_CFG_CHANGED |
3983 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 break;
3985 }
3986
3987 mac_status = tr32(MAC_STATUS);
3988 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
3989 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003990 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
3991 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 tw32_f(MAC_MODE, (tp->mac_mode |
3993 MAC_MODE_SEND_CONFIGS));
3994 udelay(1);
3995 tw32_f(MAC_MODE, tp->mac_mode);
3996 }
3997 }
3998
3999 if (current_link_up == 1) {
4000 tp->link_config.active_speed = SPEED_1000;
4001 tp->link_config.active_duplex = DUPLEX_FULL;
4002 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4003 LED_CTRL_LNKLED_OVERRIDE |
4004 LED_CTRL_1000MBPS_ON));
4005 } else {
4006 tp->link_config.active_speed = SPEED_INVALID;
4007 tp->link_config.active_duplex = DUPLEX_INVALID;
4008 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4009 LED_CTRL_LNKLED_OVERRIDE |
4010 LED_CTRL_TRAFFIC_OVERRIDE));
4011 }
4012
4013 if (current_link_up != netif_carrier_ok(tp->dev)) {
4014 if (current_link_up)
4015 netif_carrier_on(tp->dev);
4016 else
4017 netif_carrier_off(tp->dev);
4018 tg3_link_report(tp);
4019 } else {
Matt Carlson8d018622007-12-20 20:05:44 -08004020 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 if (orig_pause_cfg != now_pause_cfg ||
4022 orig_active_speed != tp->link_config.active_speed ||
4023 orig_active_duplex != tp->link_config.active_duplex)
4024 tg3_link_report(tp);
4025 }
4026
4027 return 0;
4028}
4029
Michael Chan747e8f82005-07-25 12:33:22 -07004030static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
4031{
4032 int current_link_up, err = 0;
4033 u32 bmsr, bmcr;
4034 u16 current_speed;
4035 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08004036 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07004037
4038 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4039 tw32_f(MAC_MODE, tp->mac_mode);
4040 udelay(40);
4041
4042 tw32(MAC_EVENT, 0);
4043
4044 tw32_f(MAC_STATUS,
4045 (MAC_STATUS_SYNC_CHANGED |
4046 MAC_STATUS_CFG_CHANGED |
4047 MAC_STATUS_MI_COMPLETION |
4048 MAC_STATUS_LNKSTATE_CHANGED));
4049 udelay(40);
4050
4051 if (force_reset)
4052 tg3_phy_reset(tp);
4053
4054 current_link_up = 0;
4055 current_speed = SPEED_INVALID;
4056 current_duplex = DUPLEX_INVALID;
4057
4058 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4059 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004060 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
4061 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4062 bmsr |= BMSR_LSTATUS;
4063 else
4064 bmsr &= ~BMSR_LSTATUS;
4065 }
Michael Chan747e8f82005-07-25 12:33:22 -07004066
4067 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
4068
4069 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlson2bd3ed02008-06-09 15:39:55 -07004070 (tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004071 /* do nothing, just check for link up at the end */
4072 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4073 u32 adv, new_adv;
4074
4075 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4076 new_adv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
4077 ADVERTISE_1000XPAUSE |
4078 ADVERTISE_1000XPSE_ASYM |
4079 ADVERTISE_SLCT);
4080
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004081 new_adv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Michael Chan747e8f82005-07-25 12:33:22 -07004082
4083 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
4084 new_adv |= ADVERTISE_1000XHALF;
4085 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
4086 new_adv |= ADVERTISE_1000XFULL;
4087
4088 if ((new_adv != adv) || !(bmcr & BMCR_ANENABLE)) {
4089 tg3_writephy(tp, MII_ADVERTISE, new_adv);
4090 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
4091 tg3_writephy(tp, MII_BMCR, bmcr);
4092
4093 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07004094 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Michael Chan747e8f82005-07-25 12:33:22 -07004095 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
4096
4097 return err;
4098 }
4099 } else {
4100 u32 new_bmcr;
4101
4102 bmcr &= ~BMCR_SPEED1000;
4103 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
4104
4105 if (tp->link_config.duplex == DUPLEX_FULL)
4106 new_bmcr |= BMCR_FULLDPLX;
4107
4108 if (new_bmcr != bmcr) {
4109 /* BMCR_SPEED1000 is a reserved bit that needs
4110 * to be set on write.
4111 */
4112 new_bmcr |= BMCR_SPEED1000;
4113
4114 /* Force a linkdown */
4115 if (netif_carrier_ok(tp->dev)) {
4116 u32 adv;
4117
4118 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4119 adv &= ~(ADVERTISE_1000XFULL |
4120 ADVERTISE_1000XHALF |
4121 ADVERTISE_SLCT);
4122 tg3_writephy(tp, MII_ADVERTISE, adv);
4123 tg3_writephy(tp, MII_BMCR, bmcr |
4124 BMCR_ANRESTART |
4125 BMCR_ANENABLE);
4126 udelay(10);
4127 netif_carrier_off(tp->dev);
4128 }
4129 tg3_writephy(tp, MII_BMCR, new_bmcr);
4130 bmcr = new_bmcr;
4131 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4132 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004133 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
4134 ASIC_REV_5714) {
4135 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4136 bmsr |= BMSR_LSTATUS;
4137 else
4138 bmsr &= ~BMSR_LSTATUS;
4139 }
Michael Chan747e8f82005-07-25 12:33:22 -07004140 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
4141 }
4142 }
4143
4144 if (bmsr & BMSR_LSTATUS) {
4145 current_speed = SPEED_1000;
4146 current_link_up = 1;
4147 if (bmcr & BMCR_FULLDPLX)
4148 current_duplex = DUPLEX_FULL;
4149 else
4150 current_duplex = DUPLEX_HALF;
4151
Matt Carlsonef167e22007-12-20 20:10:01 -08004152 local_adv = 0;
4153 remote_adv = 0;
4154
Michael Chan747e8f82005-07-25 12:33:22 -07004155 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08004156 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07004157
4158 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
4159 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
4160 common = local_adv & remote_adv;
4161 if (common & (ADVERTISE_1000XHALF |
4162 ADVERTISE_1000XFULL)) {
4163 if (common & ADVERTISE_1000XFULL)
4164 current_duplex = DUPLEX_FULL;
4165 else
4166 current_duplex = DUPLEX_HALF;
Michael Chan747e8f82005-07-25 12:33:22 -07004167 }
4168 else
4169 current_link_up = 0;
4170 }
4171 }
4172
Matt Carlsonef167e22007-12-20 20:10:01 -08004173 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
4174 tg3_setup_flow_control(tp, local_adv, remote_adv);
4175
Michael Chan747e8f82005-07-25 12:33:22 -07004176 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4177 if (tp->link_config.active_duplex == DUPLEX_HALF)
4178 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4179
4180 tw32_f(MAC_MODE, tp->mac_mode);
4181 udelay(40);
4182
4183 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4184
4185 tp->link_config.active_speed = current_speed;
4186 tp->link_config.active_duplex = current_duplex;
4187
4188 if (current_link_up != netif_carrier_ok(tp->dev)) {
4189 if (current_link_up)
4190 netif_carrier_on(tp->dev);
4191 else {
4192 netif_carrier_off(tp->dev);
4193 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
4194 }
4195 tg3_link_report(tp);
4196 }
4197 return err;
4198}
4199
4200static void tg3_serdes_parallel_detect(struct tg3 *tp)
4201{
Michael Chan3d3ebe72006-09-27 15:59:15 -07004202 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07004203 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07004204 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07004205 return;
4206 }
4207 if (!netif_carrier_ok(tp->dev) &&
4208 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
4209 u32 bmcr;
4210
4211 tg3_readphy(tp, MII_BMCR, &bmcr);
4212 if (bmcr & BMCR_ANENABLE) {
4213 u32 phy1, phy2;
4214
4215 /* Select shadow register 0x1f */
4216 tg3_writephy(tp, 0x1c, 0x7c00);
4217 tg3_readphy(tp, 0x1c, &phy1);
4218
4219 /* Select expansion interrupt status register */
4220 tg3_writephy(tp, 0x17, 0x0f01);
4221 tg3_readphy(tp, 0x15, &phy2);
4222 tg3_readphy(tp, 0x15, &phy2);
4223
4224 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
4225 /* We have signal detect and not receiving
4226 * config code words, link is up by parallel
4227 * detection.
4228 */
4229
4230 bmcr &= ~BMCR_ANENABLE;
4231 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
4232 tg3_writephy(tp, MII_BMCR, bmcr);
4233 tp->tg3_flags2 |= TG3_FLG2_PARALLEL_DETECT;
4234 }
4235 }
4236 }
4237 else if (netif_carrier_ok(tp->dev) &&
4238 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
4239 (tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT)) {
4240 u32 phy2;
4241
4242 /* Select expansion interrupt status register */
4243 tg3_writephy(tp, 0x17, 0x0f01);
4244 tg3_readphy(tp, 0x15, &phy2);
4245 if (phy2 & 0x20) {
4246 u32 bmcr;
4247
4248 /* Config code words received, turn on autoneg. */
4249 tg3_readphy(tp, MII_BMCR, &bmcr);
4250 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
4251
4252 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
4253
4254 }
4255 }
4256}
4257
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258static int tg3_setup_phy(struct tg3 *tp, int force_reset)
4259{
4260 int err;
4261
4262 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
4263 err = tg3_setup_fiber_phy(tp, force_reset);
Michael Chan747e8f82005-07-25 12:33:22 -07004264 } else if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES) {
4265 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 } else {
4267 err = tg3_setup_copper_phy(tp, force_reset);
4268 }
4269
Matt Carlsonbcb37f62008-11-03 16:52:09 -08004270 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08004271 u32 val, scale;
4272
4273 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
4274 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
4275 scale = 65;
4276 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
4277 scale = 6;
4278 else
4279 scale = 12;
4280
4281 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
4282 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
4283 tw32(GRC_MISC_CFG, val);
4284 }
4285
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 if (tp->link_config.active_speed == SPEED_1000 &&
4287 tp->link_config.active_duplex == DUPLEX_HALF)
4288 tw32(MAC_TX_LENGTHS,
4289 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4290 (6 << TX_LENGTHS_IPG_SHIFT) |
4291 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
4292 else
4293 tw32(MAC_TX_LENGTHS,
4294 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4295 (6 << TX_LENGTHS_IPG_SHIFT) |
4296 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
4297
4298 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
4299 if (netif_carrier_ok(tp->dev)) {
4300 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07004301 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 } else {
4303 tw32(HOSTCC_STAT_COAL_TICKS, 0);
4304 }
4305 }
4306
Matt Carlson8ed5d972007-05-07 00:25:49 -07004307 if (tp->tg3_flags & TG3_FLAG_ASPM_WORKAROUND) {
4308 u32 val = tr32(PCIE_PWR_MGMT_THRESH);
4309 if (!netif_carrier_ok(tp->dev))
4310 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
4311 tp->pwrmgmt_thresh;
4312 else
4313 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
4314 tw32(PCIE_PWR_MGMT_THRESH, val);
4315 }
4316
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 return err;
4318}
4319
Michael Chandf3e6542006-05-26 17:48:07 -07004320/* This is called whenever we suspect that the system chipset is re-
4321 * ordering the sequence of MMIO to the tx send mailbox. The symptom
4322 * is bogus tx completions. We try to recover by setting the
4323 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
4324 * in the workqueue.
4325 */
4326static void tg3_tx_recover(struct tg3 *tp)
4327{
4328 BUG_ON((tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER) ||
4329 tp->write32_tx_mbox == tg3_write_indirect_mbox);
4330
4331 printk(KERN_WARNING PFX "%s: The system may be re-ordering memory-"
4332 "mapped I/O cycles to the network device, attempting to "
4333 "recover. Please report the problem to the driver maintainer "
4334 "and include system chipset information.\n", tp->dev->name);
4335
4336 spin_lock(&tp->lock);
Michael Chandf3e6542006-05-26 17:48:07 -07004337 tp->tg3_flags |= TG3_FLAG_TX_RECOVERY_PENDING;
Michael Chandf3e6542006-05-26 17:48:07 -07004338 spin_unlock(&tp->lock);
4339}
4340
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004341static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07004342{
4343 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004344 return tnapi->tx_pending -
4345 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07004346}
4347
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348/* Tigon3 never reports partial packet sends. So we do not
4349 * need special logic to handle SKBs that have not had all
4350 * of their frags sent yet, like SunGEM does.
4351 */
Matt Carlson17375d22009-08-28 14:02:18 +00004352static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353{
Matt Carlson17375d22009-08-28 14:02:18 +00004354 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00004355 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004356 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004357 struct netdev_queue *txq;
4358 int index = tnapi - tp->napi;
4359
Matt Carlson19cfaec2009-12-03 08:36:20 +00004360 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004361 index--;
4362
4363 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364
4365 while (sw_idx != hw_idx) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00004366 struct ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07004368 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369
Michael Chandf3e6542006-05-26 17:48:07 -07004370 if (unlikely(skb == NULL)) {
4371 tg3_tx_recover(tp);
4372 return;
4373 }
4374
Alexander Duyckf4188d82009-12-02 16:48:38 +00004375 pci_unmap_single(tp->pdev,
4376 pci_unmap_addr(ri, mapping),
4377 skb_headlen(skb),
4378 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379
4380 ri->skb = NULL;
4381
4382 sw_idx = NEXT_TX(sw_idx);
4383
4384 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004385 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07004386 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
4387 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00004388
4389 pci_unmap_page(tp->pdev,
4390 pci_unmap_addr(ri, mapping),
4391 skb_shinfo(skb)->frags[i].size,
4392 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 sw_idx = NEXT_TX(sw_idx);
4394 }
4395
David S. Millerf47c11e2005-06-24 20:18:35 -07004396 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07004397
4398 if (unlikely(tx_bug)) {
4399 tg3_tx_recover(tp);
4400 return;
4401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402 }
4403
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004404 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405
Michael Chan1b2a7202006-08-07 21:46:02 -07004406 /* Need to make the tx_cons update visible to tg3_start_xmit()
4407 * before checking for netif_queue_stopped(). Without the
4408 * memory barrier, there is a small possibility that tg3_start_xmit()
4409 * will miss it and cause the queue to be stopped forever.
4410 */
4411 smp_mb();
4412
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004413 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004414 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004415 __netif_tx_lock(txq, smp_processor_id());
4416 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004417 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004418 netif_tx_wake_queue(txq);
4419 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07004420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421}
4422
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004423static void tg3_rx_skb_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
4424{
4425 if (!ri->skb)
4426 return;
4427
4428 pci_unmap_single(tp->pdev, pci_unmap_addr(ri, mapping),
4429 map_sz, PCI_DMA_FROMDEVICE);
4430 dev_kfree_skb_any(ri->skb);
4431 ri->skb = NULL;
4432}
4433
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434/* Returns size of skb allocated or < 0 on error.
4435 *
4436 * We only need to fill in the address because the other members
4437 * of the RX descriptor are invariant, see tg3_init_rings.
4438 *
4439 * Note the purposeful assymetry of cpu vs. chip accesses. For
4440 * posting buffers we only dirty the first cache line of the RX
4441 * descriptor (containing the address). Whereas for the RX status
4442 * buffers the cpu only reads the last cacheline of the RX descriptor
4443 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
4444 */
Matt Carlson86b21e52009-11-13 13:03:45 +00004445static int tg3_alloc_rx_skb(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Matt Carlsona3896162009-11-13 13:03:44 +00004446 u32 opaque_key, u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447{
4448 struct tg3_rx_buffer_desc *desc;
4449 struct ring_info *map, *src_map;
4450 struct sk_buff *skb;
4451 dma_addr_t mapping;
4452 int skb_size, dest_idx;
4453
4454 src_map = NULL;
4455 switch (opaque_key) {
4456 case RXD_OPAQUE_RING_STD:
4457 dest_idx = dest_idx_unmasked % TG3_RX_RING_SIZE;
Matt Carlson21f581a2009-08-28 14:00:25 +00004458 desc = &tpr->rx_std[dest_idx];
4459 map = &tpr->rx_std_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004460 skb_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 break;
4462
4463 case RXD_OPAQUE_RING_JUMBO:
4464 dest_idx = dest_idx_unmasked % TG3_RX_JUMBO_RING_SIZE;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00004465 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00004466 map = &tpr->rx_jmb_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004467 skb_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 break;
4469
4470 default:
4471 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473
4474 /* Do not overwrite any of the map or rp information
4475 * until we are sure we can commit to a new buffer.
4476 *
4477 * Callers depend upon this behavior and assume that
4478 * we leave everything unchanged if we fail.
4479 */
Matt Carlson287be122009-08-28 13:58:46 +00004480 skb = netdev_alloc_skb(tp->dev, skb_size + tp->rx_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 if (skb == NULL)
4482 return -ENOMEM;
4483
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 skb_reserve(skb, tp->rx_offset);
4485
Matt Carlson287be122009-08-28 13:58:46 +00004486 mapping = pci_map_single(tp->pdev, skb->data, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 PCI_DMA_FROMDEVICE);
Matt Carlsona21771d2009-11-02 14:25:31 +00004488 if (pci_dma_mapping_error(tp->pdev, mapping)) {
4489 dev_kfree_skb(skb);
4490 return -EIO;
4491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492
4493 map->skb = skb;
4494 pci_unmap_addr_set(map, mapping, mapping);
4495
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 desc->addr_hi = ((u64)mapping >> 32);
4497 desc->addr_lo = ((u64)mapping & 0xffffffff);
4498
4499 return skb_size;
4500}
4501
4502/* We only need to move over in the address because the other
4503 * members of the RX descriptor are invariant. See notes above
4504 * tg3_alloc_rx_skb for full details.
4505 */
Matt Carlsona3896162009-11-13 13:03:44 +00004506static void tg3_recycle_rx(struct tg3_napi *tnapi,
4507 struct tg3_rx_prodring_set *dpr,
4508 u32 opaque_key, int src_idx,
4509 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510{
Matt Carlson17375d22009-08-28 14:02:18 +00004511 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
4513 struct ring_info *src_map, *dest_map;
4514 int dest_idx;
Matt Carlsona3896162009-11-13 13:03:44 +00004515 struct tg3_rx_prodring_set *spr = &tp->prodring[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516
4517 switch (opaque_key) {
4518 case RXD_OPAQUE_RING_STD:
4519 dest_idx = dest_idx_unmasked % TG3_RX_RING_SIZE;
Matt Carlsona3896162009-11-13 13:03:44 +00004520 dest_desc = &dpr->rx_std[dest_idx];
4521 dest_map = &dpr->rx_std_buffers[dest_idx];
4522 src_desc = &spr->rx_std[src_idx];
4523 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 break;
4525
4526 case RXD_OPAQUE_RING_JUMBO:
4527 dest_idx = dest_idx_unmasked % TG3_RX_JUMBO_RING_SIZE;
Matt Carlsona3896162009-11-13 13:03:44 +00004528 dest_desc = &dpr->rx_jmb[dest_idx].std;
4529 dest_map = &dpr->rx_jmb_buffers[dest_idx];
4530 src_desc = &spr->rx_jmb[src_idx].std;
4531 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 break;
4533
4534 default:
4535 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537
4538 dest_map->skb = src_map->skb;
4539 pci_unmap_addr_set(dest_map, mapping,
4540 pci_unmap_addr(src_map, mapping));
4541 dest_desc->addr_hi = src_desc->addr_hi;
4542 dest_desc->addr_lo = src_desc->addr_lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543 src_map->skb = NULL;
4544}
4545
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546/* The RX ring scheme is composed of multiple rings which post fresh
4547 * buffers to the chip, and one special ring the chip uses to report
4548 * status back to the host.
4549 *
4550 * The special ring reports the status of received packets to the
4551 * host. The chip does not write into the original descriptor the
4552 * RX buffer was obtained from. The chip simply takes the original
4553 * descriptor as provided by the host, updates the status and length
4554 * field, then writes this into the next status ring entry.
4555 *
4556 * Each ring the host uses to post buffers to the chip is described
4557 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
4558 * it is first placed into the on-chip ram. When the packet's length
4559 * is known, it walks down the TG3_BDINFO entries to select the ring.
4560 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
4561 * which is within the range of the new packet's length is chosen.
4562 *
4563 * The "separate ring for rx status" scheme may sound queer, but it makes
4564 * sense from a cache coherency perspective. If only the host writes
4565 * to the buffer post rings, and only the chip writes to the rx status
4566 * rings, then cache lines never move beyond shared-modified state.
4567 * If both the host and chip were to write into the same ring, cache line
4568 * eviction could occur since both entities want it in an exclusive state.
4569 */
Matt Carlson17375d22009-08-28 14:02:18 +00004570static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571{
Matt Carlson17375d22009-08-28 14:02:18 +00004572 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07004573 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00004574 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00004575 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07004576 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 int received;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004578 struct tg3_rx_prodring_set *tpr = tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004580 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 /*
4582 * We need to order the read of hw_idx and the read of
4583 * the opaque cookie.
4584 */
4585 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 work_mask = 0;
4587 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00004588 std_prod_idx = tpr->rx_std_prod_idx;
4589 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00004591 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00004592 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 unsigned int len;
4594 struct sk_buff *skb;
4595 dma_addr_t dma_addr;
4596 u32 opaque_key, desc_idx, *post_ptr;
4597
4598 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
4599 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
4600 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004601 ri = &tp->prodring[0].rx_std_buffers[desc_idx];
Matt Carlson21f581a2009-08-28 14:00:25 +00004602 dma_addr = pci_unmap_addr(ri, mapping);
4603 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00004604 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07004605 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004607 ri = &tp->prodring[0].rx_jmb_buffers[desc_idx];
Matt Carlson21f581a2009-08-28 14:00:25 +00004608 dma_addr = pci_unmap_addr(ri, mapping);
4609 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00004610 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00004611 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613
4614 work_mask |= opaque_key;
4615
4616 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
4617 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
4618 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00004619 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620 desc_idx, *post_ptr);
4621 drop_it_no_recycle:
4622 /* Other statistics kept track of by card. */
4623 tp->net_stats.rx_dropped++;
4624 goto next_pkt;
4625 }
4626
Matt Carlsonad829262008-11-21 17:16:16 -08004627 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
4628 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629
Joe Perches8e95a202009-12-03 07:58:21 +00004630 if (len > RX_COPY_THRESHOLD &&
4631 tp->rx_offset == NET_IP_ALIGN) {
4632 /* rx_offset will likely not equal NET_IP_ALIGN
4633 * if this is a 5701 card running in PCI-X mode
4634 * [see tg3_get_invariants()]
4635 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636 int skb_size;
4637
Matt Carlson86b21e52009-11-13 13:03:45 +00004638 skb_size = tg3_alloc_rx_skb(tp, tpr, opaque_key,
Matt Carlsonafc081f2009-11-13 13:03:43 +00004639 *post_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 if (skb_size < 0)
4641 goto drop_it;
4642
Matt Carlsonafc081f2009-11-13 13:03:43 +00004643 ri->skb = NULL;
4644
Matt Carlson287be122009-08-28 13:58:46 +00004645 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 PCI_DMA_FROMDEVICE);
4647
4648 skb_put(skb, len);
4649 } else {
4650 struct sk_buff *copy_skb;
4651
Matt Carlsona3896162009-11-13 13:03:44 +00004652 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653 desc_idx, *post_ptr);
4654
Matt Carlsonad829262008-11-21 17:16:16 -08004655 copy_skb = netdev_alloc_skb(tp->dev,
4656 len + TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657 if (copy_skb == NULL)
4658 goto drop_it_no_recycle;
4659
Matt Carlsonad829262008-11-21 17:16:16 -08004660 skb_reserve(copy_skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 skb_put(copy_skb, len);
4662 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03004663 skb_copy_from_linear_data(skb, copy_skb->data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
4665
4666 /* We'll reuse the original ring buffer. */
4667 skb = copy_skb;
4668 }
4669
4670 if ((tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) &&
4671 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
4672 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
4673 >> RXD_TCPCSUM_SHIFT) == 0xffff))
4674 skb->ip_summed = CHECKSUM_UNNECESSARY;
4675 else
4676 skb->ip_summed = CHECKSUM_NONE;
4677
4678 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00004679
4680 if (len > (tp->dev->mtu + ETH_HLEN) &&
4681 skb->protocol != htons(ETH_P_8021Q)) {
4682 dev_kfree_skb(skb);
4683 goto next_pkt;
4684 }
4685
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686#if TG3_VLAN_TAG_USED
4687 if (tp->vlgrp != NULL &&
4688 desc->type_flags & RXD_FLAG_VLAN) {
Matt Carlson17375d22009-08-28 14:02:18 +00004689 vlan_gro_receive(&tnapi->napi, tp->vlgrp,
Matt Carlson8ef04422009-08-28 14:01:37 +00004690 desc->err_vlan & RXD_VLAN_MASK, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 } else
4692#endif
Matt Carlson17375d22009-08-28 14:02:18 +00004693 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 received++;
4696 budget--;
4697
4698next_pkt:
4699 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07004700
4701 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
4702 u32 idx = *post_ptr % TG3_RX_RING_SIZE;
Matt Carlson66711e62009-11-13 13:03:49 +00004703 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, idx);
Michael Chanf92905d2006-06-29 20:14:29 -07004704 work_mask &= ~RXD_OPAQUE_RING_STD;
4705 rx_std_posted = 0;
4706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07004708 sw_idx++;
Eric Dumazet6b31a512007-02-06 13:29:21 -08004709 sw_idx &= (TG3_RX_RCB_RING_SIZE(tp) - 1);
Michael Chan52f6d692005-04-25 15:14:32 -07004710
4711 /* Refresh hw_idx to see if there is new work */
4712 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004713 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07004714 rmb();
4715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716 }
4717
4718 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00004719 tnapi->rx_rcb_ptr = sw_idx;
4720 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721
4722 /* Refill RX ring(s). */
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004723 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS) || tnapi == &tp->napi[1]) {
4724 if (work_mask & RXD_OPAQUE_RING_STD) {
4725 tpr->rx_std_prod_idx = std_prod_idx % TG3_RX_RING_SIZE;
4726 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
4727 tpr->rx_std_prod_idx);
4728 }
4729 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
4730 tpr->rx_jmb_prod_idx = jmb_prod_idx %
4731 TG3_RX_JUMBO_RING_SIZE;
4732 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
4733 tpr->rx_jmb_prod_idx);
4734 }
4735 mmiowb();
4736 } else if (work_mask) {
4737 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
4738 * updated before the producer indices can be updated.
4739 */
4740 smp_wmb();
4741
Matt Carlson43619352009-11-13 13:03:47 +00004742 tpr->rx_std_prod_idx = std_prod_idx % TG3_RX_RING_SIZE;
Matt Carlson43619352009-11-13 13:03:47 +00004743 tpr->rx_jmb_prod_idx = jmb_prod_idx % TG3_RX_JUMBO_RING_SIZE;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004744
4745 napi_schedule(&tp->napi[1].napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747
4748 return received;
4749}
4750
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004751static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753 /* handle link change and other phy events */
4754 if (!(tp->tg3_flags &
4755 (TG3_FLAG_USE_LINKCHG_REG |
4756 TG3_FLAG_POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004757 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
4758
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759 if (sblk->status & SD_STATUS_LINK_CHG) {
4760 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004761 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07004762 spin_lock(&tp->lock);
Matt Carlsondd477002008-05-25 23:45:58 -07004763 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
4764 tw32_f(MAC_STATUS,
4765 (MAC_STATUS_SYNC_CHANGED |
4766 MAC_STATUS_CFG_CHANGED |
4767 MAC_STATUS_MI_COMPLETION |
4768 MAC_STATUS_LNKSTATE_CHANGED));
4769 udelay(40);
4770 } else
4771 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07004772 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773 }
4774 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004775}
4776
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004777static void tg3_rx_prodring_xfer(struct tg3 *tp,
4778 struct tg3_rx_prodring_set *dpr,
4779 struct tg3_rx_prodring_set *spr)
4780{
4781 u32 si, di, cpycnt, src_prod_idx;
4782 int i;
4783
4784 while (1) {
4785 src_prod_idx = spr->rx_std_prod_idx;
4786
4787 /* Make sure updates to the rx_std_buffers[] entries and the
4788 * standard producer index are seen in the correct order.
4789 */
4790 smp_rmb();
4791
4792 if (spr->rx_std_cons_idx == src_prod_idx)
4793 break;
4794
4795 if (spr->rx_std_cons_idx < src_prod_idx)
4796 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
4797 else
4798 cpycnt = TG3_RX_RING_SIZE - spr->rx_std_cons_idx;
4799
4800 cpycnt = min(cpycnt, TG3_RX_RING_SIZE - dpr->rx_std_prod_idx);
4801
4802 si = spr->rx_std_cons_idx;
4803 di = dpr->rx_std_prod_idx;
4804
4805 memcpy(&dpr->rx_std_buffers[di],
4806 &spr->rx_std_buffers[si],
4807 cpycnt * sizeof(struct ring_info));
4808
4809 for (i = 0; i < cpycnt; i++, di++, si++) {
4810 struct tg3_rx_buffer_desc *sbd, *dbd;
4811 sbd = &spr->rx_std[si];
4812 dbd = &dpr->rx_std[di];
4813 dbd->addr_hi = sbd->addr_hi;
4814 dbd->addr_lo = sbd->addr_lo;
4815 }
4816
4817 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) %
4818 TG3_RX_RING_SIZE;
4819 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) %
4820 TG3_RX_RING_SIZE;
4821 }
4822
4823 while (1) {
4824 src_prod_idx = spr->rx_jmb_prod_idx;
4825
4826 /* Make sure updates to the rx_jmb_buffers[] entries and
4827 * the jumbo producer index are seen in the correct order.
4828 */
4829 smp_rmb();
4830
4831 if (spr->rx_jmb_cons_idx == src_prod_idx)
4832 break;
4833
4834 if (spr->rx_jmb_cons_idx < src_prod_idx)
4835 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
4836 else
4837 cpycnt = TG3_RX_JUMBO_RING_SIZE - spr->rx_jmb_cons_idx;
4838
4839 cpycnt = min(cpycnt,
4840 TG3_RX_JUMBO_RING_SIZE - dpr->rx_jmb_prod_idx);
4841
4842 si = spr->rx_jmb_cons_idx;
4843 di = dpr->rx_jmb_prod_idx;
4844
4845 memcpy(&dpr->rx_jmb_buffers[di],
4846 &spr->rx_jmb_buffers[si],
4847 cpycnt * sizeof(struct ring_info));
4848
4849 for (i = 0; i < cpycnt; i++, di++, si++) {
4850 struct tg3_rx_buffer_desc *sbd, *dbd;
4851 sbd = &spr->rx_jmb[si].std;
4852 dbd = &dpr->rx_jmb[di].std;
4853 dbd->addr_hi = sbd->addr_hi;
4854 dbd->addr_lo = sbd->addr_lo;
4855 }
4856
4857 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) %
4858 TG3_RX_JUMBO_RING_SIZE;
4859 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) %
4860 TG3_RX_JUMBO_RING_SIZE;
4861 }
4862}
4863
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004864static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
4865{
4866 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867
4868 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004869 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00004870 tg3_tx(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07004871 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
Michael Chan4fd7ab52007-10-12 01:39:50 -07004872 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873 }
4874
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875 /* run RX thread, within the bounds set by NAPI.
4876 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004877 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004879 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00004880 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004882 if ((tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS) && tnapi == &tp->napi[1]) {
4883 int i;
4884 u32 std_prod_idx = tp->prodring[0].rx_std_prod_idx;
4885 u32 jmb_prod_idx = tp->prodring[0].rx_jmb_prod_idx;
4886
4887 for (i = 2; i < tp->irq_cnt; i++)
4888 tg3_rx_prodring_xfer(tp, tnapi->prodring,
4889 tp->napi[i].prodring);
4890
4891 wmb();
4892
4893 if (std_prod_idx != tp->prodring[0].rx_std_prod_idx) {
4894 u32 mbox = TG3_RX_STD_PROD_IDX_REG;
4895 tw32_rx_mbox(mbox, tp->prodring[0].rx_std_prod_idx);
4896 }
4897
4898 if (jmb_prod_idx != tp->prodring[0].rx_jmb_prod_idx) {
4899 u32 mbox = TG3_RX_JMB_PROD_IDX_REG;
4900 tw32_rx_mbox(mbox, tp->prodring[0].rx_jmb_prod_idx);
4901 }
4902
4903 mmiowb();
4904 }
4905
David S. Miller6f535762007-10-11 18:08:29 -07004906 return work_done;
4907}
David S. Millerf7383c22005-05-18 22:50:53 -07004908
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004909static int tg3_poll_msix(struct napi_struct *napi, int budget)
4910{
4911 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
4912 struct tg3 *tp = tnapi->tp;
4913 int work_done = 0;
4914 struct tg3_hw_status *sblk = tnapi->hw_status;
4915
4916 while (1) {
4917 work_done = tg3_poll_work(tnapi, work_done, budget);
4918
4919 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
4920 goto tx_recovery;
4921
4922 if (unlikely(work_done >= budget))
4923 break;
4924
4925 /* tp->last_tag is used in tg3_restart_ints() below
4926 * to tell the hw how much work has been processed,
4927 * so we must read it before checking for more work.
4928 */
4929 tnapi->last_tag = sblk->status_tag;
4930 tnapi->last_irq_tag = tnapi->last_tag;
4931 rmb();
4932
4933 /* check for RX/TX work to do */
4934 if (sblk->idx[0].tx_consumer == tnapi->tx_cons &&
4935 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr) {
4936 napi_complete(napi);
4937 /* Reenable interrupts. */
4938 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
4939 mmiowb();
4940 break;
4941 }
4942 }
4943
4944 return work_done;
4945
4946tx_recovery:
4947 /* work_done is guaranteed to be less than budget. */
4948 napi_complete(napi);
4949 schedule_work(&tp->reset_task);
4950 return work_done;
4951}
4952
David S. Miller6f535762007-10-11 18:08:29 -07004953static int tg3_poll(struct napi_struct *napi, int budget)
4954{
Matt Carlson8ef04422009-08-28 14:01:37 +00004955 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
4956 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07004957 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00004958 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07004959
4960 while (1) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004961 tg3_poll_link(tp);
4962
Matt Carlson17375d22009-08-28 14:02:18 +00004963 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07004964
4965 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
4966 goto tx_recovery;
4967
4968 if (unlikely(work_done >= budget))
4969 break;
4970
Michael Chan4fd7ab52007-10-12 01:39:50 -07004971 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) {
Matt Carlson17375d22009-08-28 14:02:18 +00004972 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07004973 * to tell the hw how much work has been processed,
4974 * so we must read it before checking for more work.
4975 */
Matt Carlson898a56f2009-08-28 14:02:40 +00004976 tnapi->last_tag = sblk->status_tag;
4977 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07004978 rmb();
4979 } else
4980 sblk->status &= ~SD_STATUS_UPDATED;
4981
Matt Carlson17375d22009-08-28 14:02:18 +00004982 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08004983 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00004984 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07004985 break;
4986 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987 }
4988
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004989 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07004990
4991tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07004992 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08004993 napi_complete(napi);
David S. Miller6f535762007-10-11 18:08:29 -07004994 schedule_work(&tp->reset_task);
Michael Chan4fd7ab52007-10-12 01:39:50 -07004995 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996}
4997
David S. Millerf47c11e2005-06-24 20:18:35 -07004998static void tg3_irq_quiesce(struct tg3 *tp)
4999{
Matt Carlson4f125f42009-09-01 12:55:02 +00005000 int i;
5001
David S. Millerf47c11e2005-06-24 20:18:35 -07005002 BUG_ON(tp->irq_sync);
5003
5004 tp->irq_sync = 1;
5005 smp_mb();
5006
Matt Carlson4f125f42009-09-01 12:55:02 +00005007 for (i = 0; i < tp->irq_cnt; i++)
5008 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07005009}
5010
5011static inline int tg3_irq_sync(struct tg3 *tp)
5012{
5013 return tp->irq_sync;
5014}
5015
5016/* Fully shutdown all tg3 driver activity elsewhere in the system.
5017 * If irq_sync is non-zero, then the IRQ handler must be synchronized
5018 * with as well. Most of the time, this is not necessary except when
5019 * shutting down the device.
5020 */
5021static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
5022{
Michael Chan46966542007-07-11 19:47:19 -07005023 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07005024 if (irq_sync)
5025 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07005026}
5027
5028static inline void tg3_full_unlock(struct tg3 *tp)
5029{
David S. Millerf47c11e2005-06-24 20:18:35 -07005030 spin_unlock_bh(&tp->lock);
5031}
5032
Michael Chanfcfa0a32006-03-20 22:28:41 -08005033/* One-shot MSI handler - Chip automatically disables interrupt
5034 * after sending MSI so driver doesn't have to do it.
5035 */
David Howells7d12e782006-10-05 14:55:46 +01005036static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08005037{
Matt Carlson09943a12009-08-28 14:01:57 +00005038 struct tg3_napi *tnapi = dev_id;
5039 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08005040
Matt Carlson898a56f2009-08-28 14:02:40 +00005041 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005042 if (tnapi->rx_rcb)
5043 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005044
5045 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005046 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005047
5048 return IRQ_HANDLED;
5049}
5050
Michael Chan88b06bc22005-04-21 17:13:25 -07005051/* MSI ISR - No need to check for interrupt sharing and no need to
5052 * flush status block and interrupt mailbox. PCI ordering rules
5053 * guarantee that MSI will arrive after the status block.
5054 */
David Howells7d12e782006-10-05 14:55:46 +01005055static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07005056{
Matt Carlson09943a12009-08-28 14:01:57 +00005057 struct tg3_napi *tnapi = dev_id;
5058 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07005059
Matt Carlson898a56f2009-08-28 14:02:40 +00005060 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005061 if (tnapi->rx_rcb)
5062 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07005063 /*
David S. Millerfac9b832005-05-18 22:46:34 -07005064 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07005065 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07005066 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07005067 * NIC to stop sending us irqs, engaging "in-intr-handler"
5068 * event coalescing.
5069 */
5070 tw32_mailbox(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07005071 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005072 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07005073
Michael Chan88b06bc22005-04-21 17:13:25 -07005074 return IRQ_RETVAL(1);
5075}
5076
David Howells7d12e782006-10-05 14:55:46 +01005077static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078{
Matt Carlson09943a12009-08-28 14:01:57 +00005079 struct tg3_napi *tnapi = dev_id;
5080 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005081 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082 unsigned int handled = 1;
5083
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084 /* In INTx mode, it is possible for the interrupt to arrive at
5085 * the CPU before the status block posted prior to the interrupt.
5086 * Reading the PCI State register will confirm whether the
5087 * interrupt is ours and will flush the status block.
5088 */
Michael Chand18edcb2007-03-24 20:57:11 -07005089 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
5090 if ((tp->tg3_flags & TG3_FLAG_CHIP_RESETTING) ||
5091 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5092 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005093 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07005094 }
Michael Chand18edcb2007-03-24 20:57:11 -07005095 }
5096
5097 /*
5098 * Writing any value to intr-mbox-0 clears PCI INTA# and
5099 * chip-internal interrupt pending events.
5100 * Writing non-zero to intr-mbox-0 additional tells the
5101 * NIC to stop sending us irqs, engaging "in-intr-handler"
5102 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005103 *
5104 * Flush the mailbox to de-assert the IRQ immediately to prevent
5105 * spurious interrupts. The flush impacts performance but
5106 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005107 */
Michael Chanc04cb342007-05-07 00:26:15 -07005108 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07005109 if (tg3_irq_sync(tp))
5110 goto out;
5111 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00005112 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00005113 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00005114 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07005115 } else {
5116 /* No work, shared interrupt perhaps? re-enable
5117 * interrupts, and flush that PCI write
5118 */
5119 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
5120 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07005121 }
David S. Millerf47c11e2005-06-24 20:18:35 -07005122out:
David S. Millerfac9b832005-05-18 22:46:34 -07005123 return IRQ_RETVAL(handled);
5124}
5125
David Howells7d12e782006-10-05 14:55:46 +01005126static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07005127{
Matt Carlson09943a12009-08-28 14:01:57 +00005128 struct tg3_napi *tnapi = dev_id;
5129 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005130 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07005131 unsigned int handled = 1;
5132
David S. Millerfac9b832005-05-18 22:46:34 -07005133 /* In INTx mode, it is possible for the interrupt to arrive at
5134 * the CPU before the status block posted prior to the interrupt.
5135 * Reading the PCI State register will confirm whether the
5136 * interrupt is ours and will flush the status block.
5137 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005138 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Michael Chand18edcb2007-03-24 20:57:11 -07005139 if ((tp->tg3_flags & TG3_FLAG_CHIP_RESETTING) ||
5140 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5141 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005142 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143 }
Michael Chand18edcb2007-03-24 20:57:11 -07005144 }
5145
5146 /*
5147 * writing any value to intr-mbox-0 clears PCI INTA# and
5148 * chip-internal interrupt pending events.
5149 * writing non-zero to intr-mbox-0 additional tells the
5150 * NIC to stop sending us irqs, engaging "in-intr-handler"
5151 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005152 *
5153 * Flush the mailbox to de-assert the IRQ immediately to prevent
5154 * spurious interrupts. The flush impacts performance but
5155 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005156 */
Michael Chanc04cb342007-05-07 00:26:15 -07005157 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00005158
5159 /*
5160 * In a shared interrupt configuration, sometimes other devices'
5161 * interrupts will scream. We record the current status tag here
5162 * so that the above check can report that the screaming interrupts
5163 * are unhandled. Eventually they will be silenced.
5164 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005165 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00005166
Michael Chand18edcb2007-03-24 20:57:11 -07005167 if (tg3_irq_sync(tp))
5168 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00005169
Matt Carlson72334482009-08-28 14:03:01 +00005170 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00005171
Matt Carlson09943a12009-08-28 14:01:57 +00005172 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00005173
David S. Millerf47c11e2005-06-24 20:18:35 -07005174out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175 return IRQ_RETVAL(handled);
5176}
5177
Michael Chan79381092005-04-21 17:13:59 -07005178/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01005179static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07005180{
Matt Carlson09943a12009-08-28 14:01:57 +00005181 struct tg3_napi *tnapi = dev_id;
5182 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005183 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07005184
Michael Chanf9804dd2005-09-27 12:13:10 -07005185 if ((sblk->status & SD_STATUS_UPDATED) ||
5186 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07005187 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07005188 return IRQ_RETVAL(1);
5189 }
5190 return IRQ_RETVAL(0);
5191}
5192
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07005193static int tg3_init_hw(struct tg3 *, int);
Michael Chan944d9802005-05-29 14:57:48 -07005194static int tg3_halt(struct tg3 *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195
Michael Chanb9ec6c12006-07-25 16:37:27 -07005196/* Restart hardware after configuration changes, self-test, etc.
5197 * Invoked with tp->lock held.
5198 */
5199static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
Eric Dumazet78c61462008-04-24 23:33:06 -07005200 __releases(tp->lock)
5201 __acquires(tp->lock)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005202{
5203 int err;
5204
5205 err = tg3_init_hw(tp, reset_phy);
5206 if (err) {
5207 printk(KERN_ERR PFX "%s: Failed to re-initialize device, "
5208 "aborting.\n", tp->dev->name);
5209 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
5210 tg3_full_unlock(tp);
5211 del_timer_sync(&tp->timer);
5212 tp->irq_sync = 0;
Matt Carlsonfed97812009-09-01 13:10:19 +00005213 tg3_napi_enable(tp);
Michael Chanb9ec6c12006-07-25 16:37:27 -07005214 dev_close(tp->dev);
5215 tg3_full_lock(tp, 0);
5216 }
5217 return err;
5218}
5219
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220#ifdef CONFIG_NET_POLL_CONTROLLER
5221static void tg3_poll_controller(struct net_device *dev)
5222{
Matt Carlson4f125f42009-09-01 12:55:02 +00005223 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07005224 struct tg3 *tp = netdev_priv(dev);
5225
Matt Carlson4f125f42009-09-01 12:55:02 +00005226 for (i = 0; i < tp->irq_cnt; i++)
5227 tg3_interrupt(tp->napi[i].irq_vec, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228}
5229#endif
5230
David Howellsc4028952006-11-22 14:57:56 +00005231static void tg3_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232{
David Howellsc4028952006-11-22 14:57:56 +00005233 struct tg3 *tp = container_of(work, struct tg3, reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005234 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235 unsigned int restart_timer;
5236
Michael Chan7faa0062006-02-02 17:29:28 -08005237 tg3_full_lock(tp, 0);
Michael Chan7faa0062006-02-02 17:29:28 -08005238
5239 if (!netif_running(tp->dev)) {
Michael Chan7faa0062006-02-02 17:29:28 -08005240 tg3_full_unlock(tp);
5241 return;
5242 }
5243
5244 tg3_full_unlock(tp);
5245
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005246 tg3_phy_stop(tp);
5247
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248 tg3_netif_stop(tp);
5249
David S. Millerf47c11e2005-06-24 20:18:35 -07005250 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251
5252 restart_timer = tp->tg3_flags2 & TG3_FLG2_RESTART_TIMER;
5253 tp->tg3_flags2 &= ~TG3_FLG2_RESTART_TIMER;
5254
Michael Chandf3e6542006-05-26 17:48:07 -07005255 if (tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING) {
5256 tp->write32_tx_mbox = tg3_write32_tx_mbox;
5257 tp->write32_rx_mbox = tg3_write_flush_reg32;
5258 tp->tg3_flags |= TG3_FLAG_MBOX_WRITE_REORDER;
5259 tp->tg3_flags &= ~TG3_FLAG_TX_RECOVERY_PENDING;
5260 }
5261
Michael Chan944d9802005-05-29 14:57:48 -07005262 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005263 err = tg3_init_hw(tp, 1);
5264 if (err)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005265 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266
5267 tg3_netif_start(tp);
5268
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269 if (restart_timer)
5270 mod_timer(&tp->timer, jiffies + 1);
Michael Chan7faa0062006-02-02 17:29:28 -08005271
Michael Chanb9ec6c12006-07-25 16:37:27 -07005272out:
Michael Chan7faa0062006-02-02 17:29:28 -08005273 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005274
5275 if (!err)
5276 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277}
5278
Michael Chanb0408752007-02-13 12:18:30 -08005279static void tg3_dump_short_state(struct tg3 *tp)
5280{
5281 printk(KERN_ERR PFX "DEBUG: MAC_TX_STATUS[%08x] MAC_RX_STATUS[%08x]\n",
5282 tr32(MAC_TX_STATUS), tr32(MAC_RX_STATUS));
5283 printk(KERN_ERR PFX "DEBUG: RDMAC_STATUS[%08x] WDMAC_STATUS[%08x]\n",
5284 tr32(RDMAC_STATUS), tr32(WDMAC_STATUS));
5285}
5286
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287static void tg3_tx_timeout(struct net_device *dev)
5288{
5289 struct tg3 *tp = netdev_priv(dev);
5290
Michael Chanb0408752007-02-13 12:18:30 -08005291 if (netif_msg_tx_err(tp)) {
Michael Chan9f88f292006-12-07 00:22:54 -08005292 printk(KERN_ERR PFX "%s: transmit timed out, resetting\n",
5293 dev->name);
Michael Chanb0408752007-02-13 12:18:30 -08005294 tg3_dump_short_state(tp);
5295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296
5297 schedule_work(&tp->reset_task);
5298}
5299
Michael Chanc58ec932005-09-17 00:46:27 -07005300/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
5301static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
5302{
5303 u32 base = (u32) mapping & 0xffffffff;
5304
5305 return ((base > 0xffffdcc0) &&
5306 (base + len + 8 < base));
5307}
5308
Michael Chan72f2afb2006-03-06 19:28:35 -08005309/* Test for DMA addresses > 40-bit */
5310static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
5311 int len)
5312{
5313#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Michael Chan6728a8e2006-03-27 23:16:49 -08005314 if (tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG)
Yang Hongyang50cf1562009-04-06 19:01:14 -07005315 return (((u64) mapping + len) > DMA_BIT_MASK(40));
Michael Chan72f2afb2006-03-06 19:28:35 -08005316 return 0;
5317#else
5318 return 0;
5319#endif
5320}
5321
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005322static void tg3_set_txd(struct tg3_napi *, int, dma_addr_t, int, u32, u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323
Michael Chan72f2afb2006-03-06 19:28:35 -08005324/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00005325static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
5326 struct sk_buff *skb, u32 last_plus_one,
5327 u32 *start, u32 base_flags, u32 mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328{
Matt Carlson24f4efd2009-11-13 13:03:35 +00005329 struct tg3 *tp = tnapi->tp;
Matt Carlson41588ba2008-04-19 18:12:33 -07005330 struct sk_buff *new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07005331 dma_addr_t new_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332 u32 entry = *start;
Michael Chanc58ec932005-09-17 00:46:27 -07005333 int i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334
Matt Carlson41588ba2008-04-19 18:12:33 -07005335 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
5336 new_skb = skb_copy(skb, GFP_ATOMIC);
5337 else {
5338 int more_headroom = 4 - ((unsigned long)skb->data & 3);
5339
5340 new_skb = skb_copy_expand(skb,
5341 skb_headroom(skb) + more_headroom,
5342 skb_tailroom(skb), GFP_ATOMIC);
5343 }
5344
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07005346 ret = -1;
5347 } else {
5348 /* New SKB is guaranteed to be linear. */
5349 entry = *start;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005350 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
5351 PCI_DMA_TODEVICE);
5352 /* Make sure the mapping succeeded */
5353 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
5354 ret = -1;
5355 dev_kfree_skb(new_skb);
5356 new_skb = NULL;
David S. Miller90079ce2008-09-11 04:52:51 -07005357
Michael Chanc58ec932005-09-17 00:46:27 -07005358 /* Make sure new skb does not cross any 4G boundaries.
5359 * Drop the packet if it does.
5360 */
Alexander Duyckf4188d82009-12-02 16:48:38 +00005361 } else if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
5362 tg3_4g_overflow_test(new_addr, new_skb->len)) {
5363 pci_unmap_single(tp->pdev, new_addr, new_skb->len,
5364 PCI_DMA_TODEVICE);
Michael Chanc58ec932005-09-17 00:46:27 -07005365 ret = -1;
5366 dev_kfree_skb(new_skb);
5367 new_skb = NULL;
5368 } else {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005369 tg3_set_txd(tnapi, entry, new_addr, new_skb->len,
Michael Chanc58ec932005-09-17 00:46:27 -07005370 base_flags, 1 | (mss << 1));
5371 *start = NEXT_TX(entry);
5372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373 }
5374
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375 /* Now clean up the sw ring entries. */
5376 i = 0;
5377 while (entry != last_plus_one) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00005378 int len;
5379
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005380 if (i == 0)
Alexander Duyckf4188d82009-12-02 16:48:38 +00005381 len = skb_headlen(skb);
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005382 else
Alexander Duyckf4188d82009-12-02 16:48:38 +00005383 len = skb_shinfo(skb)->frags[i-1].size;
5384
5385 pci_unmap_single(tp->pdev,
5386 pci_unmap_addr(&tnapi->tx_buffers[entry],
5387 mapping),
5388 len, PCI_DMA_TODEVICE);
5389 if (i == 0) {
5390 tnapi->tx_buffers[entry].skb = new_skb;
5391 pci_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
5392 new_addr);
5393 } else {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005394 tnapi->tx_buffers[entry].skb = NULL;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396 entry = NEXT_TX(entry);
5397 i++;
5398 }
5399
5400 dev_kfree_skb(skb);
5401
Michael Chanc58ec932005-09-17 00:46:27 -07005402 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403}
5404
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005405static void tg3_set_txd(struct tg3_napi *tnapi, int entry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406 dma_addr_t mapping, int len, u32 flags,
5407 u32 mss_and_is_end)
5408{
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005409 struct tg3_tx_buffer_desc *txd = &tnapi->tx_ring[entry];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410 int is_end = (mss_and_is_end & 0x1);
5411 u32 mss = (mss_and_is_end >> 1);
5412 u32 vlan_tag = 0;
5413
5414 if (is_end)
5415 flags |= TXD_FLAG_END;
5416 if (flags & TXD_FLAG_VLAN) {
5417 vlan_tag = flags >> 16;
5418 flags &= 0xffff;
5419 }
5420 vlan_tag |= (mss << TXD_MSS_SHIFT);
5421
5422 txd->addr_hi = ((u64) mapping >> 32);
5423 txd->addr_lo = ((u64) mapping & 0xffffffff);
5424 txd->len_flags = (len << TXD_LEN_SHIFT) | flags;
5425 txd->vlan_tag = vlan_tag << TXD_VLAN_TAG_SHIFT;
5426}
5427
Michael Chan5a6f3072006-03-20 22:28:05 -08005428/* hard_start_xmit for devices that don't have any bugs and
Matt Carlsone849cdc2009-11-13 13:03:38 +00005429 * support TG3_FLG2_HW_TSO_2 and TG3_FLG2_HW_TSO_3 only.
Michael Chan5a6f3072006-03-20 22:28:05 -08005430 */
Stephen Hemminger613573252009-08-31 19:50:58 +00005431static netdev_tx_t tg3_start_xmit(struct sk_buff *skb,
5432 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433{
5434 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435 u32 len, entry, base_flags, mss;
David S. Miller90079ce2008-09-11 04:52:51 -07005436 dma_addr_t mapping;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005437 struct tg3_napi *tnapi;
5438 struct netdev_queue *txq;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005439 unsigned int i, last;
5440
Michael Chan5a6f3072006-03-20 22:28:05 -08005441
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005442 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
5443 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Matt Carlson19cfaec2009-12-03 08:36:20 +00005444 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005445 tnapi++;
Michael Chan5a6f3072006-03-20 22:28:05 -08005446
Michael Chan00b70502006-06-17 21:58:45 -07005447 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005448 * and TX reclaim runs via tp->napi.poll inside of a software
Michael Chan5a6f3072006-03-20 22:28:05 -08005449 * interrupt. Furthermore, IRQ processing runs lockless so we have
5450 * no IRQ context deadlocks to worry about either. Rejoice!
5451 */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005452 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005453 if (!netif_tx_queue_stopped(txq)) {
5454 netif_tx_stop_queue(txq);
Michael Chan5a6f3072006-03-20 22:28:05 -08005455
5456 /* This is a hard error, log it. */
5457 printk(KERN_ERR PFX "%s: BUG! Tx Ring full when "
5458 "queue awake!\n", dev->name);
5459 }
Michael Chan5a6f3072006-03-20 22:28:05 -08005460 return NETDEV_TX_BUSY;
5461 }
5462
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005463 entry = tnapi->tx_prod;
Michael Chan5a6f3072006-03-20 22:28:05 -08005464 base_flags = 0;
Michael Chan5a6f3072006-03-20 22:28:05 -08005465 mss = 0;
Matt Carlsonc13e3712007-05-05 11:50:04 -07005466 if ((mss = skb_shinfo(skb)->gso_size) != 0) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005467 int tcp_opt_len, ip_tcp_len;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005468 u32 hdrlen;
Michael Chan5a6f3072006-03-20 22:28:05 -08005469
5470 if (skb_header_cloned(skb) &&
5471 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
5472 dev_kfree_skb(skb);
5473 goto out_unlock;
5474 }
5475
Michael Chanb0026622006-07-03 19:42:14 -07005476 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005477 hdrlen = skb_headlen(skb) - ETH_HLEN;
Michael Chanb0026622006-07-03 19:42:14 -07005478 else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005479 struct iphdr *iph = ip_hdr(skb);
5480
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07005481 tcp_opt_len = tcp_optlen(skb);
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -03005482 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
Michael Chanb0026622006-07-03 19:42:14 -07005483
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005484 iph->check = 0;
5485 iph->tot_len = htons(mss + ip_tcp_len + tcp_opt_len);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005486 hdrlen = ip_tcp_len + tcp_opt_len;
Michael Chanb0026622006-07-03 19:42:14 -07005487 }
Michael Chan5a6f3072006-03-20 22:28:05 -08005488
Matt Carlsone849cdc2009-11-13 13:03:38 +00005489 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005490 mss |= (hdrlen & 0xc) << 12;
5491 if (hdrlen & 0x10)
5492 base_flags |= 0x00000010;
5493 base_flags |= (hdrlen & 0x3e0) << 5;
5494 } else
5495 mss |= hdrlen << 9;
5496
Michael Chan5a6f3072006-03-20 22:28:05 -08005497 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
5498 TXD_FLAG_CPU_POST_DMA);
5499
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005500 tcp_hdr(skb)->check = 0;
Michael Chan5a6f3072006-03-20 22:28:05 -08005501
Michael Chan5a6f3072006-03-20 22:28:05 -08005502 }
Patrick McHardy84fa7932006-08-29 16:44:56 -07005503 else if (skb->ip_summed == CHECKSUM_PARTIAL)
Michael Chan5a6f3072006-03-20 22:28:05 -08005504 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Michael Chan5a6f3072006-03-20 22:28:05 -08005505#if TG3_VLAN_TAG_USED
5506 if (tp->vlgrp != NULL && vlan_tx_tag_present(skb))
5507 base_flags |= (TXD_FLAG_VLAN |
5508 (vlan_tx_tag_get(skb) << 16));
5509#endif
5510
Alexander Duyckf4188d82009-12-02 16:48:38 +00005511 len = skb_headlen(skb);
5512
5513 /* Queue skb data, a.k.a. the main skb fragment. */
5514 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
5515 if (pci_dma_mapping_error(tp->pdev, mapping)) {
David S. Miller90079ce2008-09-11 04:52:51 -07005516 dev_kfree_skb(skb);
5517 goto out_unlock;
5518 }
5519
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005520 tnapi->tx_buffers[entry].skb = skb;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005521 pci_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005522
Matt Carlsonb703df62009-12-03 08:36:21 +00005523 if ((tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005524 !mss && skb->len > ETH_DATA_LEN)
5525 base_flags |= TXD_FLAG_JMB_PKT;
5526
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005527 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
Michael Chan5a6f3072006-03-20 22:28:05 -08005528 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
5529
5530 entry = NEXT_TX(entry);
5531
5532 /* Now loop through additional data fragments, and queue them. */
5533 if (skb_shinfo(skb)->nr_frags > 0) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005534 last = skb_shinfo(skb)->nr_frags - 1;
5535 for (i = 0; i <= last; i++) {
5536 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5537
5538 len = frag->size;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005539 mapping = pci_map_page(tp->pdev,
5540 frag->page,
5541 frag->page_offset,
5542 len, PCI_DMA_TODEVICE);
5543 if (pci_dma_mapping_error(tp->pdev, mapping))
5544 goto dma_error;
5545
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005546 tnapi->tx_buffers[entry].skb = NULL;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005547 pci_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
5548 mapping);
Michael Chan5a6f3072006-03-20 22:28:05 -08005549
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005550 tg3_set_txd(tnapi, entry, mapping, len,
Michael Chan5a6f3072006-03-20 22:28:05 -08005551 base_flags, (i == last) | (mss << 1));
5552
5553 entry = NEXT_TX(entry);
5554 }
5555 }
5556
5557 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005558 tw32_tx_mbox(tnapi->prodmbox, entry);
Michael Chan5a6f3072006-03-20 22:28:05 -08005559
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005560 tnapi->tx_prod = entry;
5561 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005562 netif_tx_stop_queue(txq);
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005563 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005564 netif_tx_wake_queue(txq);
Michael Chan5a6f3072006-03-20 22:28:05 -08005565 }
5566
5567out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00005568 mmiowb();
Michael Chan5a6f3072006-03-20 22:28:05 -08005569
5570 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005571
5572dma_error:
5573 last = i;
5574 entry = tnapi->tx_prod;
5575 tnapi->tx_buffers[entry].skb = NULL;
5576 pci_unmap_single(tp->pdev,
5577 pci_unmap_addr(&tnapi->tx_buffers[entry], mapping),
5578 skb_headlen(skb),
5579 PCI_DMA_TODEVICE);
5580 for (i = 0; i <= last; i++) {
5581 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5582 entry = NEXT_TX(entry);
5583
5584 pci_unmap_page(tp->pdev,
5585 pci_unmap_addr(&tnapi->tx_buffers[entry],
5586 mapping),
5587 frag->size, PCI_DMA_TODEVICE);
5588 }
5589
5590 dev_kfree_skb(skb);
5591 return NETDEV_TX_OK;
Michael Chan5a6f3072006-03-20 22:28:05 -08005592}
5593
Stephen Hemminger613573252009-08-31 19:50:58 +00005594static netdev_tx_t tg3_start_xmit_dma_bug(struct sk_buff *,
5595 struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07005596
5597/* Use GSO to workaround a rare TSO bug that may be triggered when the
5598 * TSO header is greater than 80 bytes.
5599 */
5600static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
5601{
5602 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005603 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07005604
5605 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005606 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07005607 netif_stop_queue(tp->dev);
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005608 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08005609 return NETDEV_TX_BUSY;
5610
5611 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07005612 }
5613
5614 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07005615 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07005616 goto tg3_tso_bug_end;
5617
5618 do {
5619 nskb = segs;
5620 segs = segs->next;
5621 nskb->next = NULL;
5622 tg3_start_xmit_dma_bug(nskb, tp->dev);
5623 } while (segs);
5624
5625tg3_tso_bug_end:
5626 dev_kfree_skb(skb);
5627
5628 return NETDEV_TX_OK;
5629}
Michael Chan52c0fd82006-06-29 20:15:54 -07005630
Michael Chan5a6f3072006-03-20 22:28:05 -08005631/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
5632 * support TG3_FLG2_HW_TSO_1 or firmware TSO only.
5633 */
Stephen Hemminger613573252009-08-31 19:50:58 +00005634static netdev_tx_t tg3_start_xmit_dma_bug(struct sk_buff *skb,
5635 struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08005636{
5637 struct tg3 *tp = netdev_priv(dev);
Michael Chan5a6f3072006-03-20 22:28:05 -08005638 u32 len, entry, base_flags, mss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639 int would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07005640 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00005641 struct tg3_napi *tnapi;
5642 struct netdev_queue *txq;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005643 unsigned int i, last;
5644
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645
Matt Carlson24f4efd2009-11-13 13:03:35 +00005646 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
5647 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Matt Carlson19cfaec2009-12-03 08:36:20 +00005648 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlson24f4efd2009-11-13 13:03:35 +00005649 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650
Michael Chan00b70502006-06-17 21:58:45 -07005651 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005652 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07005653 * interrupt. Furthermore, IRQ processing runs lockless so we have
5654 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655 */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005656 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00005657 if (!netif_tx_queue_stopped(txq)) {
5658 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08005659
5660 /* This is a hard error, log it. */
5661 printk(KERN_ERR PFX "%s: BUG! Tx Ring full when "
5662 "queue awake!\n", dev->name);
5663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664 return NETDEV_TX_BUSY;
5665 }
5666
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005667 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07005669 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00005671
Matt Carlsonc13e3712007-05-05 11:50:04 -07005672 if ((mss = skb_shinfo(skb)->gso_size) != 0) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005673 struct iphdr *iph;
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005674 u32 tcp_opt_len, ip_tcp_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675
5676 if (skb_header_cloned(skb) &&
5677 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
5678 dev_kfree_skb(skb);
5679 goto out_unlock;
5680 }
5681
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07005682 tcp_opt_len = tcp_optlen(skb);
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -03005683 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005684
Michael Chan52c0fd82006-06-29 20:15:54 -07005685 hdr_len = ip_tcp_len + tcp_opt_len;
5686 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Michael Chan7f62ad52007-02-20 23:25:40 -08005687 (tp->tg3_flags2 & TG3_FLG2_TSO_BUG))
Michael Chan52c0fd82006-06-29 20:15:54 -07005688 return (tg3_tso_bug(tp, skb));
5689
Linus Torvalds1da177e2005-04-16 15:20:36 -07005690 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
5691 TXD_FLAG_CPU_POST_DMA);
5692
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005693 iph = ip_hdr(skb);
5694 iph->check = 0;
5695 iph->tot_len = htons(mss + hdr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005697 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005698 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005699 } else
5700 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
5701 iph->daddr, 0,
5702 IPPROTO_TCP,
5703 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704
Matt Carlson615774f2009-11-13 13:03:39 +00005705 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) {
5706 mss |= (hdr_len & 0xc) << 12;
5707 if (hdr_len & 0x10)
5708 base_flags |= 0x00000010;
5709 base_flags |= (hdr_len & 0x3e0) << 5;
5710 } else if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_2)
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005711 mss |= hdr_len << 9;
5712 else if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_1) ||
5713 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005714 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715 int tsflags;
5716
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005717 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718 mss |= (tsflags << 11);
5719 }
5720 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005721 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722 int tsflags;
5723
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005724 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725 base_flags |= tsflags << 12;
5726 }
5727 }
5728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729#if TG3_VLAN_TAG_USED
5730 if (tp->vlgrp != NULL && vlan_tx_tag_present(skb))
5731 base_flags |= (TXD_FLAG_VLAN |
5732 (vlan_tx_tag_get(skb) << 16));
5733#endif
5734
Matt Carlsonb703df62009-12-03 08:36:21 +00005735 if ((tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) &&
Matt Carlson615774f2009-11-13 13:03:39 +00005736 !mss && skb->len > ETH_DATA_LEN)
5737 base_flags |= TXD_FLAG_JMB_PKT;
5738
Alexander Duyckf4188d82009-12-02 16:48:38 +00005739 len = skb_headlen(skb);
5740
5741 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
5742 if (pci_dma_mapping_error(tp->pdev, mapping)) {
David S. Miller90079ce2008-09-11 04:52:51 -07005743 dev_kfree_skb(skb);
5744 goto out_unlock;
5745 }
5746
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005747 tnapi->tx_buffers[entry].skb = skb;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005748 pci_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749
5750 would_hit_hwbug = 0;
5751
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005752 if ((tp->tg3_flags3 & TG3_FLG3_SHORT_DMA_BUG) && len <= 8)
5753 would_hit_hwbug = 1;
5754
Matt Carlson0e1406d2009-11-02 12:33:33 +00005755 if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
5756 tg3_4g_overflow_test(mapping, len))
Matt Carlson41588ba2008-04-19 18:12:33 -07005757 would_hit_hwbug = 1;
Matt Carlson0e1406d2009-11-02 12:33:33 +00005758
5759 if ((tp->tg3_flags3 & TG3_FLG3_40BIT_DMA_LIMIT_BUG) &&
5760 tg3_40bit_overflow_test(tp, mapping, len))
5761 would_hit_hwbug = 1;
5762
5763 if (tp->tg3_flags3 & TG3_FLG3_5701_DMA_BUG)
Michael Chanc58ec932005-09-17 00:46:27 -07005764 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005766 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
5768
5769 entry = NEXT_TX(entry);
5770
5771 /* Now loop through additional data fragments, and queue them. */
5772 if (skb_shinfo(skb)->nr_frags > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773 last = skb_shinfo(skb)->nr_frags - 1;
5774 for (i = 0; i <= last; i++) {
5775 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5776
5777 len = frag->size;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005778 mapping = pci_map_page(tp->pdev,
5779 frag->page,
5780 frag->page_offset,
5781 len, PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005783 tnapi->tx_buffers[entry].skb = NULL;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005784 pci_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
5785 mapping);
5786 if (pci_dma_mapping_error(tp->pdev, mapping))
5787 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005789 if ((tp->tg3_flags3 & TG3_FLG3_SHORT_DMA_BUG) &&
5790 len <= 8)
5791 would_hit_hwbug = 1;
5792
Matt Carlson0e1406d2009-11-02 12:33:33 +00005793 if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
5794 tg3_4g_overflow_test(mapping, len))
Michael Chanc58ec932005-09-17 00:46:27 -07005795 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796
Matt Carlson0e1406d2009-11-02 12:33:33 +00005797 if ((tp->tg3_flags3 & TG3_FLG3_40BIT_DMA_LIMIT_BUG) &&
5798 tg3_40bit_overflow_test(tp, mapping, len))
Michael Chan72f2afb2006-03-06 19:28:35 -08005799 would_hit_hwbug = 1;
5800
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005802 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005803 base_flags, (i == last)|(mss << 1));
5804 else
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005805 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806 base_flags, (i == last));
5807
5808 entry = NEXT_TX(entry);
5809 }
5810 }
5811
5812 if (would_hit_hwbug) {
5813 u32 last_plus_one = entry;
5814 u32 start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005815
Michael Chanc58ec932005-09-17 00:46:27 -07005816 start = entry - 1 - skb_shinfo(skb)->nr_frags;
5817 start &= (TG3_TX_RING_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005818
5819 /* If the workaround fails due to memory/mapping
5820 * failure, silently drop this packet.
5821 */
Matt Carlson24f4efd2009-11-13 13:03:35 +00005822 if (tigon3_dma_hwbug_workaround(tnapi, skb, last_plus_one,
Michael Chanc58ec932005-09-17 00:46:27 -07005823 &start, base_flags, mss))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005824 goto out_unlock;
5825
5826 entry = start;
5827 }
5828
5829 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00005830 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005832 tnapi->tx_prod = entry;
5833 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00005834 netif_tx_stop_queue(txq);
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005835 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00005836 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07005837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005838
5839out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00005840 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005841
5842 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005843
5844dma_error:
5845 last = i;
5846 entry = tnapi->tx_prod;
5847 tnapi->tx_buffers[entry].skb = NULL;
5848 pci_unmap_single(tp->pdev,
5849 pci_unmap_addr(&tnapi->tx_buffers[entry], mapping),
5850 skb_headlen(skb),
5851 PCI_DMA_TODEVICE);
5852 for (i = 0; i <= last; i++) {
5853 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5854 entry = NEXT_TX(entry);
5855
5856 pci_unmap_page(tp->pdev,
5857 pci_unmap_addr(&tnapi->tx_buffers[entry],
5858 mapping),
5859 frag->size, PCI_DMA_TODEVICE);
5860 }
5861
5862 dev_kfree_skb(skb);
5863 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864}
5865
5866static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
5867 int new_mtu)
5868{
5869 dev->mtu = new_mtu;
5870
Michael Chanef7f5ec2005-07-25 12:32:25 -07005871 if (new_mtu > ETH_DATA_LEN) {
Michael Chana4e2b342005-10-26 15:46:52 -07005872 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
Michael Chanef7f5ec2005-07-25 12:32:25 -07005873 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
5874 ethtool_op_set_tso(dev, 0);
5875 }
5876 else
5877 tp->tg3_flags |= TG3_FLAG_JUMBO_RING_ENABLE;
5878 } else {
Michael Chana4e2b342005-10-26 15:46:52 -07005879 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
Michael Chanef7f5ec2005-07-25 12:32:25 -07005880 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
Michael Chan0f893dc2005-07-25 12:30:38 -07005881 tp->tg3_flags &= ~TG3_FLAG_JUMBO_RING_ENABLE;
Michael Chanef7f5ec2005-07-25 12:32:25 -07005882 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005883}
5884
5885static int tg3_change_mtu(struct net_device *dev, int new_mtu)
5886{
5887 struct tg3 *tp = netdev_priv(dev);
Michael Chanb9ec6c12006-07-25 16:37:27 -07005888 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005889
5890 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
5891 return -EINVAL;
5892
5893 if (!netif_running(dev)) {
5894 /* We'll just catch it later when the
5895 * device is up'd.
5896 */
5897 tg3_set_mtu(dev, tp, new_mtu);
5898 return 0;
5899 }
5900
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005901 tg3_phy_stop(tp);
5902
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903 tg3_netif_stop(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07005904
5905 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005906
Michael Chan944d9802005-05-29 14:57:48 -07005907 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908
5909 tg3_set_mtu(dev, tp, new_mtu);
5910
Michael Chanb9ec6c12006-07-25 16:37:27 -07005911 err = tg3_restart_hw(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005912
Michael Chanb9ec6c12006-07-25 16:37:27 -07005913 if (!err)
5914 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005915
David S. Millerf47c11e2005-06-24 20:18:35 -07005916 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005917
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005918 if (!err)
5919 tg3_phy_start(tp);
5920
Michael Chanb9ec6c12006-07-25 16:37:27 -07005921 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922}
5923
Matt Carlson21f581a2009-08-28 14:00:25 +00005924static void tg3_rx_prodring_free(struct tg3 *tp,
5925 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005926{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005927 int i;
5928
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005929 if (tpr != &tp->prodring[0]) {
5930 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
5931 i = (i + 1) % TG3_RX_RING_SIZE)
5932 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
5933 tp->rx_pkt_map_sz);
5934
5935 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
5936 for (i = tpr->rx_jmb_cons_idx;
5937 i != tpr->rx_jmb_prod_idx;
5938 i = (i + 1) % TG3_RX_JUMBO_RING_SIZE) {
5939 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
5940 TG3_RX_JMB_MAP_SZ);
5941 }
5942 }
5943
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005944 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005947 for (i = 0; i < TG3_RX_RING_SIZE; i++)
5948 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
5949 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005950
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005951 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005952 for (i = 0; i < TG3_RX_JUMBO_RING_SIZE; i++)
5953 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
5954 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955 }
5956}
5957
5958/* Initialize tx/rx rings for packet processing.
5959 *
5960 * The chip has been shut down and the driver detached from
5961 * the networking, so no interrupts or new tx packets will
5962 * end up in the driver. tp->{tx,}lock are held and thus
5963 * we may not sleep.
5964 */
Matt Carlson21f581a2009-08-28 14:00:25 +00005965static int tg3_rx_prodring_alloc(struct tg3 *tp,
5966 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005967{
Matt Carlson287be122009-08-28 13:58:46 +00005968 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005969
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005970 tpr->rx_std_cons_idx = 0;
5971 tpr->rx_std_prod_idx = 0;
5972 tpr->rx_jmb_cons_idx = 0;
5973 tpr->rx_jmb_prod_idx = 0;
5974
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005975 if (tpr != &tp->prodring[0]) {
5976 memset(&tpr->rx_std_buffers[0], 0, TG3_RX_STD_BUFF_RING_SIZE);
5977 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE)
5978 memset(&tpr->rx_jmb_buffers[0], 0,
5979 TG3_RX_JMB_BUFF_RING_SIZE);
5980 goto done;
5981 }
5982
Linus Torvalds1da177e2005-04-16 15:20:36 -07005983 /* Zero out all descriptors. */
Matt Carlson21f581a2009-08-28 14:00:25 +00005984 memset(tpr->rx_std, 0, TG3_RX_RING_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005985
Matt Carlson287be122009-08-28 13:58:46 +00005986 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Michael Chana4e2b342005-10-26 15:46:52 -07005987 if ((tp->tg3_flags2 & TG3_FLG2_5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00005988 tp->dev->mtu > ETH_DATA_LEN)
5989 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
5990 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07005991
Linus Torvalds1da177e2005-04-16 15:20:36 -07005992 /* Initialize invariants of the rings, we only set this
5993 * stuff once. This works because the card does not
5994 * write into the rx buffer posting rings.
5995 */
5996 for (i = 0; i < TG3_RX_RING_SIZE; i++) {
5997 struct tg3_rx_buffer_desc *rxd;
5998
Matt Carlson21f581a2009-08-28 14:00:25 +00005999 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00006000 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006001 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
6002 rxd->opaque = (RXD_OPAQUE_RING_STD |
6003 (i << RXD_OPAQUE_INDEX_SHIFT));
6004 }
6005
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006006 /* Now allocate fresh SKBs for each rx ring. */
6007 for (i = 0; i < tp->rx_pending; i++) {
Matt Carlson86b21e52009-11-13 13:03:45 +00006008 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_STD, i) < 0) {
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006009 printk(KERN_WARNING PFX
6010 "%s: Using a smaller RX standard ring, "
6011 "only %d out of %d buffers were allocated "
6012 "successfully.\n",
6013 tp->dev->name, i, tp->rx_pending);
6014 if (i == 0)
6015 goto initfail;
6016 tp->rx_pending = i;
6017 break;
6018 }
6019 }
6020
6021 if (!(tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE))
6022 goto done;
6023
Matt Carlson21f581a2009-08-28 14:00:25 +00006024 memset(tpr->rx_jmb, 0, TG3_RX_JUMBO_RING_BYTES);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006025
Michael Chan0f893dc2005-07-25 12:30:38 -07006026 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027 for (i = 0; i < TG3_RX_JUMBO_RING_SIZE; i++) {
6028 struct tg3_rx_buffer_desc *rxd;
6029
Matt Carlson79ed5ac2009-08-28 14:00:55 +00006030 rxd = &tpr->rx_jmb[i].std;
Matt Carlson287be122009-08-28 13:58:46 +00006031 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006032 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
6033 RXD_FLAG_JUMBO;
6034 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
6035 (i << RXD_OPAQUE_INDEX_SHIFT));
6036 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006037
Linus Torvalds1da177e2005-04-16 15:20:36 -07006038 for (i = 0; i < tp->rx_jumbo_pending; i++) {
Matt Carlson86b21e52009-11-13 13:03:45 +00006039 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_JUMBO,
Matt Carlsonafc081f2009-11-13 13:03:43 +00006040 i) < 0) {
Michael Chan32d8c572006-07-25 16:38:29 -07006041 printk(KERN_WARNING PFX
6042 "%s: Using a smaller RX jumbo ring, "
6043 "only %d out of %d buffers were "
6044 "allocated successfully.\n",
6045 tp->dev->name, i, tp->rx_jumbo_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006046 if (i == 0)
6047 goto initfail;
Michael Chan32d8c572006-07-25 16:38:29 -07006048 tp->rx_jumbo_pending = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049 break;
Michael Chan32d8c572006-07-25 16:38:29 -07006050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006051 }
6052 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006053
6054done:
Michael Chan32d8c572006-07-25 16:38:29 -07006055 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006056
6057initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00006058 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006059 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006060}
6061
Matt Carlson21f581a2009-08-28 14:00:25 +00006062static void tg3_rx_prodring_fini(struct tg3 *tp,
6063 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006064{
Matt Carlson21f581a2009-08-28 14:00:25 +00006065 kfree(tpr->rx_std_buffers);
6066 tpr->rx_std_buffers = NULL;
6067 kfree(tpr->rx_jmb_buffers);
6068 tpr->rx_jmb_buffers = NULL;
6069 if (tpr->rx_std) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070 pci_free_consistent(tp->pdev, TG3_RX_RING_BYTES,
Matt Carlson21f581a2009-08-28 14:00:25 +00006071 tpr->rx_std, tpr->rx_std_mapping);
6072 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006073 }
Matt Carlson21f581a2009-08-28 14:00:25 +00006074 if (tpr->rx_jmb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075 pci_free_consistent(tp->pdev, TG3_RX_JUMBO_RING_BYTES,
Matt Carlson21f581a2009-08-28 14:00:25 +00006076 tpr->rx_jmb, tpr->rx_jmb_mapping);
6077 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006078 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006079}
6080
Matt Carlson21f581a2009-08-28 14:00:25 +00006081static int tg3_rx_prodring_init(struct tg3 *tp,
6082 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006083{
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006084 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE, GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006085 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006086 return -ENOMEM;
6087
Matt Carlson21f581a2009-08-28 14:00:25 +00006088 tpr->rx_std = pci_alloc_consistent(tp->pdev, TG3_RX_RING_BYTES,
6089 &tpr->rx_std_mapping);
6090 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006091 goto err_out;
6092
6093 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006094 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE,
Matt Carlson21f581a2009-08-28 14:00:25 +00006095 GFP_KERNEL);
6096 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006097 goto err_out;
6098
Matt Carlson21f581a2009-08-28 14:00:25 +00006099 tpr->rx_jmb = pci_alloc_consistent(tp->pdev,
6100 TG3_RX_JUMBO_RING_BYTES,
6101 &tpr->rx_jmb_mapping);
6102 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006103 goto err_out;
6104 }
6105
6106 return 0;
6107
6108err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00006109 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006110 return -ENOMEM;
6111}
6112
6113/* Free up pending packets in all rx/tx rings.
6114 *
6115 * The chip has been shut down and the driver detached from
6116 * the networking, so no interrupts or new tx packets will
6117 * end up in the driver. tp->{tx,}lock is not held and we are not
6118 * in an interrupt context and thus may sleep.
6119 */
6120static void tg3_free_rings(struct tg3 *tp)
6121{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006122 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006123
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006124 for (j = 0; j < tp->irq_cnt; j++) {
6125 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006126
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006127 if (!tnapi->tx_buffers)
6128 continue;
6129
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006130 for (i = 0; i < TG3_TX_RING_SIZE; ) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00006131 struct ring_info *txp;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006132 struct sk_buff *skb;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006133 unsigned int k;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006134
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006135 txp = &tnapi->tx_buffers[i];
6136 skb = txp->skb;
6137
6138 if (skb == NULL) {
6139 i++;
6140 continue;
6141 }
6142
Alexander Duyckf4188d82009-12-02 16:48:38 +00006143 pci_unmap_single(tp->pdev,
6144 pci_unmap_addr(txp, mapping),
6145 skb_headlen(skb),
6146 PCI_DMA_TODEVICE);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006147 txp->skb = NULL;
6148
Alexander Duyckf4188d82009-12-02 16:48:38 +00006149 i++;
6150
6151 for (k = 0; k < skb_shinfo(skb)->nr_frags; k++) {
6152 txp = &tnapi->tx_buffers[i & (TG3_TX_RING_SIZE - 1)];
6153 pci_unmap_page(tp->pdev,
6154 pci_unmap_addr(txp, mapping),
6155 skb_shinfo(skb)->frags[k].size,
6156 PCI_DMA_TODEVICE);
6157 i++;
6158 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006159
6160 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006161 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006162
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006163 if (tp->irq_cnt == 1 || j != tp->irq_cnt - 1)
6164 tg3_rx_prodring_free(tp, &tp->prodring[j]);
6165 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006166}
6167
6168/* Initialize tx/rx rings for packet processing.
6169 *
6170 * The chip has been shut down and the driver detached from
6171 * the networking, so no interrupts or new tx packets will
6172 * end up in the driver. tp->{tx,}lock are held and thus
6173 * we may not sleep.
6174 */
6175static int tg3_init_rings(struct tg3 *tp)
6176{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006177 int i;
Matt Carlson72334482009-08-28 14:03:01 +00006178
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006179 /* Free up all the SKBs. */
6180 tg3_free_rings(tp);
6181
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006182 for (i = 0; i < tp->irq_cnt; i++) {
6183 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006184
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006185 tnapi->last_tag = 0;
6186 tnapi->last_irq_tag = 0;
6187 tnapi->hw_status->status = 0;
6188 tnapi->hw_status->status_tag = 0;
6189 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6190
6191 tnapi->tx_prod = 0;
6192 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006193 if (tnapi->tx_ring)
6194 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006195
6196 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006197 if (tnapi->rx_rcb)
6198 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006199
6200 if ((tp->irq_cnt == 1 || i != tp->irq_cnt - 1) &&
6201 tg3_rx_prodring_alloc(tp, &tp->prodring[i]))
6202 return -ENOMEM;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006203 }
Matt Carlson72334482009-08-28 14:03:01 +00006204
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006205 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006206}
6207
6208/*
6209 * Must not be invoked with interrupt sources disabled and
6210 * the hardware shutdown down.
6211 */
6212static void tg3_free_consistent(struct tg3 *tp)
6213{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006214 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006215
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006216 for (i = 0; i < tp->irq_cnt; i++) {
6217 struct tg3_napi *tnapi = &tp->napi[i];
6218
6219 if (tnapi->tx_ring) {
6220 pci_free_consistent(tp->pdev, TG3_TX_RING_BYTES,
6221 tnapi->tx_ring, tnapi->tx_desc_mapping);
6222 tnapi->tx_ring = NULL;
6223 }
6224
6225 kfree(tnapi->tx_buffers);
6226 tnapi->tx_buffers = NULL;
6227
6228 if (tnapi->rx_rcb) {
6229 pci_free_consistent(tp->pdev, TG3_RX_RCB_RING_BYTES(tp),
6230 tnapi->rx_rcb,
6231 tnapi->rx_rcb_mapping);
6232 tnapi->rx_rcb = NULL;
6233 }
6234
6235 if (tnapi->hw_status) {
6236 pci_free_consistent(tp->pdev, TG3_HW_STATUS_SIZE,
6237 tnapi->hw_status,
6238 tnapi->status_mapping);
6239 tnapi->hw_status = NULL;
6240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006241 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006242
Linus Torvalds1da177e2005-04-16 15:20:36 -07006243 if (tp->hw_stats) {
6244 pci_free_consistent(tp->pdev, sizeof(struct tg3_hw_stats),
6245 tp->hw_stats, tp->stats_mapping);
6246 tp->hw_stats = NULL;
6247 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006248
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006249 for (i = 0; i < (tp->irq_cnt == 1 ? 1 : tp->irq_cnt - 1); i++)
6250 tg3_rx_prodring_fini(tp, &tp->prodring[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006251}
6252
6253/*
6254 * Must not be invoked with interrupt sources disabled and
6255 * the hardware shutdown down. Can sleep.
6256 */
6257static int tg3_alloc_consistent(struct tg3 *tp)
6258{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006259 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006260
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006261 for (i = 0; i < (tp->irq_cnt == 1 ? 1 : tp->irq_cnt - 1); i++) {
6262 if (tg3_rx_prodring_init(tp, &tp->prodring[i]))
6263 goto err_out;
6264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006265
Linus Torvalds1da177e2005-04-16 15:20:36 -07006266 tp->hw_stats = pci_alloc_consistent(tp->pdev,
6267 sizeof(struct tg3_hw_stats),
6268 &tp->stats_mapping);
6269 if (!tp->hw_stats)
6270 goto err_out;
6271
Linus Torvalds1da177e2005-04-16 15:20:36 -07006272 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6273
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006274 for (i = 0; i < tp->irq_cnt; i++) {
6275 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006276 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006277
6278 tnapi->hw_status = pci_alloc_consistent(tp->pdev,
6279 TG3_HW_STATUS_SIZE,
6280 &tnapi->status_mapping);
6281 if (!tnapi->hw_status)
6282 goto err_out;
6283
6284 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006285 sblk = tnapi->hw_status;
6286
Matt Carlson19cfaec2009-12-03 08:36:20 +00006287 /* If multivector TSS is enabled, vector 0 does not handle
6288 * tx interrupts. Don't allocate any resources for it.
6289 */
6290 if ((!i && !(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)) ||
6291 (i && (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS))) {
6292 tnapi->tx_buffers = kzalloc(sizeof(struct ring_info) *
6293 TG3_TX_RING_SIZE,
6294 GFP_KERNEL);
6295 if (!tnapi->tx_buffers)
6296 goto err_out;
6297
6298 tnapi->tx_ring = pci_alloc_consistent(tp->pdev,
6299 TG3_TX_RING_BYTES,
6300 &tnapi->tx_desc_mapping);
6301 if (!tnapi->tx_ring)
6302 goto err_out;
6303 }
6304
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006305 /*
6306 * When RSS is enabled, the status block format changes
6307 * slightly. The "rx_jumbo_consumer", "reserved",
6308 * and "rx_mini_consumer" members get mapped to the
6309 * other three rx return ring producer indexes.
6310 */
6311 switch (i) {
6312 default:
6313 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
6314 break;
6315 case 2:
6316 tnapi->rx_rcb_prod_idx = &sblk->rx_jumbo_consumer;
6317 break;
6318 case 3:
6319 tnapi->rx_rcb_prod_idx = &sblk->reserved;
6320 break;
6321 case 4:
6322 tnapi->rx_rcb_prod_idx = &sblk->rx_mini_consumer;
6323 break;
6324 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006325
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006326 if (tp->irq_cnt == 1)
6327 tnapi->prodring = &tp->prodring[0];
6328 else if (i)
6329 tnapi->prodring = &tp->prodring[i - 1];
6330
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006331 /*
6332 * If multivector RSS is enabled, vector 0 does not handle
6333 * rx or tx interrupts. Don't allocate any resources for it.
6334 */
6335 if (!i && (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS))
6336 continue;
6337
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006338 tnapi->rx_rcb = pci_alloc_consistent(tp->pdev,
6339 TG3_RX_RCB_RING_BYTES(tp),
6340 &tnapi->rx_rcb_mapping);
6341 if (!tnapi->rx_rcb)
6342 goto err_out;
6343
6344 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006345 }
6346
Linus Torvalds1da177e2005-04-16 15:20:36 -07006347 return 0;
6348
6349err_out:
6350 tg3_free_consistent(tp);
6351 return -ENOMEM;
6352}
6353
6354#define MAX_WAIT_CNT 1000
6355
6356/* To stop a block, clear the enable bit and poll till it
6357 * clears. tp->lock is held.
6358 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006359static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006360{
6361 unsigned int i;
6362 u32 val;
6363
6364 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
6365 switch (ofs) {
6366 case RCVLSC_MODE:
6367 case DMAC_MODE:
6368 case MBFREE_MODE:
6369 case BUFMGR_MODE:
6370 case MEMARB_MODE:
6371 /* We can't enable/disable these bits of the
6372 * 5705/5750, just say success.
6373 */
6374 return 0;
6375
6376 default:
6377 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006379 }
6380
6381 val = tr32(ofs);
6382 val &= ~enable_bit;
6383 tw32_f(ofs, val);
6384
6385 for (i = 0; i < MAX_WAIT_CNT; i++) {
6386 udelay(100);
6387 val = tr32(ofs);
6388 if ((val & enable_bit) == 0)
6389 break;
6390 }
6391
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006392 if (i == MAX_WAIT_CNT && !silent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006393 printk(KERN_ERR PFX "tg3_stop_block timed out, "
6394 "ofs=%lx enable_bit=%x\n",
6395 ofs, enable_bit);
6396 return -ENODEV;
6397 }
6398
6399 return 0;
6400}
6401
6402/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006403static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006404{
6405 int i, err;
6406
6407 tg3_disable_ints(tp);
6408
6409 tp->rx_mode &= ~RX_MODE_ENABLE;
6410 tw32_f(MAC_RX_MODE, tp->rx_mode);
6411 udelay(10);
6412
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006413 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
6414 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
6415 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
6416 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
6417 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
6418 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006419
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006420 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
6421 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
6422 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
6423 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
6424 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
6425 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
6426 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006427
6428 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
6429 tw32_f(MAC_MODE, tp->mac_mode);
6430 udelay(40);
6431
6432 tp->tx_mode &= ~TX_MODE_ENABLE;
6433 tw32_f(MAC_TX_MODE, tp->tx_mode);
6434
6435 for (i = 0; i < MAX_WAIT_CNT; i++) {
6436 udelay(100);
6437 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
6438 break;
6439 }
6440 if (i >= MAX_WAIT_CNT) {
6441 printk(KERN_ERR PFX "tg3_abort_hw timed out for %s, "
6442 "TX_MODE_ENABLE will not clear MAC_TX_MODE=%08x\n",
6443 tp->dev->name, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07006444 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006445 }
6446
Michael Chane6de8ad2005-05-05 14:42:41 -07006447 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006448 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
6449 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006450
6451 tw32(FTQ_RESET, 0xffffffff);
6452 tw32(FTQ_RESET, 0x00000000);
6453
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006454 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
6455 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006456
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006457 for (i = 0; i < tp->irq_cnt; i++) {
6458 struct tg3_napi *tnapi = &tp->napi[i];
6459 if (tnapi->hw_status)
6460 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006462 if (tp->hw_stats)
6463 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6464
Linus Torvalds1da177e2005-04-16 15:20:36 -07006465 return err;
6466}
6467
Matt Carlson0d3031d2007-10-10 18:02:43 -07006468static void tg3_ape_send_event(struct tg3 *tp, u32 event)
6469{
6470 int i;
6471 u32 apedata;
6472
6473 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
6474 if (apedata != APE_SEG_SIG_MAGIC)
6475 return;
6476
6477 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
Matt Carlson731fd792008-08-15 14:07:51 -07006478 if (!(apedata & APE_FW_STATUS_READY))
Matt Carlson0d3031d2007-10-10 18:02:43 -07006479 return;
6480
6481 /* Wait for up to 1 millisecond for APE to service previous event. */
6482 for (i = 0; i < 10; i++) {
6483 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
6484 return;
6485
6486 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
6487
6488 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6489 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
6490 event | APE_EVENT_STATUS_EVENT_PENDING);
6491
6492 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
6493
6494 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6495 break;
6496
6497 udelay(100);
6498 }
6499
6500 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6501 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
6502}
6503
6504static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
6505{
6506 u32 event;
6507 u32 apedata;
6508
6509 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
6510 return;
6511
6512 switch (kind) {
6513 case RESET_KIND_INIT:
6514 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
6515 APE_HOST_SEG_SIG_MAGIC);
6516 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
6517 APE_HOST_SEG_LEN_MAGIC);
6518 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
6519 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
6520 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
6521 APE_HOST_DRIVER_ID_MAGIC);
6522 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
6523 APE_HOST_BEHAV_NO_PHYLOCK);
6524
6525 event = APE_EVENT_STATUS_STATE_START;
6526 break;
6527 case RESET_KIND_SHUTDOWN:
Matt Carlsonb2aee152008-11-03 16:51:11 -08006528 /* With the interface we are currently using,
6529 * APE does not track driver state. Wiping
6530 * out the HOST SEGMENT SIGNATURE forces
6531 * the APE to assume OS absent status.
6532 */
6533 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
6534
Matt Carlson0d3031d2007-10-10 18:02:43 -07006535 event = APE_EVENT_STATUS_STATE_UNLOAD;
6536 break;
6537 case RESET_KIND_SUSPEND:
6538 event = APE_EVENT_STATUS_STATE_SUSPEND;
6539 break;
6540 default:
6541 return;
6542 }
6543
6544 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
6545
6546 tg3_ape_send_event(tp, event);
6547}
6548
Michael Chane6af3012005-04-21 17:12:05 -07006549/* tp->lock is held. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006550static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
6551{
David S. Millerf49639e2006-06-09 11:58:36 -07006552 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
6553 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006554
6555 if (tp->tg3_flags2 & TG3_FLG2_ASF_NEW_HANDSHAKE) {
6556 switch (kind) {
6557 case RESET_KIND_INIT:
6558 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6559 DRV_STATE_START);
6560 break;
6561
6562 case RESET_KIND_SHUTDOWN:
6563 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6564 DRV_STATE_UNLOAD);
6565 break;
6566
6567 case RESET_KIND_SUSPEND:
6568 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6569 DRV_STATE_SUSPEND);
6570 break;
6571
6572 default:
6573 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006575 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07006576
6577 if (kind == RESET_KIND_INIT ||
6578 kind == RESET_KIND_SUSPEND)
6579 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006580}
6581
6582/* tp->lock is held. */
6583static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
6584{
6585 if (tp->tg3_flags2 & TG3_FLG2_ASF_NEW_HANDSHAKE) {
6586 switch (kind) {
6587 case RESET_KIND_INIT:
6588 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6589 DRV_STATE_START_DONE);
6590 break;
6591
6592 case RESET_KIND_SHUTDOWN:
6593 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6594 DRV_STATE_UNLOAD_DONE);
6595 break;
6596
6597 default:
6598 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006599 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006600 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07006601
6602 if (kind == RESET_KIND_SHUTDOWN)
6603 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006604}
6605
6606/* tp->lock is held. */
6607static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
6608{
6609 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) {
6610 switch (kind) {
6611 case RESET_KIND_INIT:
6612 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6613 DRV_STATE_START);
6614 break;
6615
6616 case RESET_KIND_SHUTDOWN:
6617 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6618 DRV_STATE_UNLOAD);
6619 break;
6620
6621 case RESET_KIND_SUSPEND:
6622 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6623 DRV_STATE_SUSPEND);
6624 break;
6625
6626 default:
6627 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006629 }
6630}
6631
Michael Chan7a6f4362006-09-27 16:03:31 -07006632static int tg3_poll_fw(struct tg3 *tp)
6633{
6634 int i;
6635 u32 val;
6636
Michael Chanb5d37722006-09-27 16:06:21 -07006637 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Gary Zambrano0ccead12006-11-14 16:34:00 -08006638 /* Wait up to 20ms for init done. */
6639 for (i = 0; i < 200; i++) {
Michael Chanb5d37722006-09-27 16:06:21 -07006640 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
6641 return 0;
Gary Zambrano0ccead12006-11-14 16:34:00 -08006642 udelay(100);
Michael Chanb5d37722006-09-27 16:06:21 -07006643 }
6644 return -ENODEV;
6645 }
6646
Michael Chan7a6f4362006-09-27 16:03:31 -07006647 /* Wait for firmware initialization to complete. */
6648 for (i = 0; i < 100000; i++) {
6649 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
6650 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
6651 break;
6652 udelay(10);
6653 }
6654
6655 /* Chip might not be fitted with firmware. Some Sun onboard
6656 * parts are configured like that. So don't signal the timeout
6657 * of the above loop as an error, but do report the lack of
6658 * running firmware once.
6659 */
6660 if (i >= 100000 &&
6661 !(tp->tg3_flags2 & TG3_FLG2_NO_FWARE_REPORTED)) {
6662 tp->tg3_flags2 |= TG3_FLG2_NO_FWARE_REPORTED;
6663
6664 printk(KERN_INFO PFX "%s: No firmware running.\n",
6665 tp->dev->name);
6666 }
6667
6668 return 0;
6669}
6670
Michael Chanee6a99b2007-07-18 21:49:10 -07006671/* Save PCI command register before chip reset */
6672static void tg3_save_pci_state(struct tg3 *tp)
6673{
Matt Carlson8a6eac92007-10-21 16:17:55 -07006674 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07006675}
6676
6677/* Restore PCI state after chip reset */
6678static void tg3_restore_pci_state(struct tg3 *tp)
6679{
6680 u32 val;
6681
6682 /* Re-enable indirect register accesses. */
6683 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
6684 tp->misc_host_ctrl);
6685
6686 /* Set MAX PCI retry to zero. */
6687 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
6688 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
6689 (tp->tg3_flags & TG3_FLAG_PCIX_MODE))
6690 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07006691 /* Allow reads and writes to the APE register and memory space. */
6692 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
6693 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
6694 PCISTATE_ALLOW_APE_SHMEM_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07006695 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
6696
Matt Carlson8a6eac92007-10-21 16:17:55 -07006697 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07006698
Matt Carlsonfcb389d2008-11-03 16:55:44 -08006699 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785) {
6700 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)
6701 pcie_set_readrq(tp->pdev, 4096);
6702 else {
6703 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
6704 tp->pci_cacheline_sz);
6705 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
6706 tp->pci_lat_timer);
6707 }
Michael Chan114342f2007-10-15 02:12:26 -07006708 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08006709
Michael Chanee6a99b2007-07-18 21:49:10 -07006710 /* Make sure PCI-X relaxed ordering bit is clear. */
Matt Carlson52f44902008-11-21 17:17:04 -08006711 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
Matt Carlson9974a352007-10-07 23:27:28 -07006712 u16 pcix_cmd;
6713
6714 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
6715 &pcix_cmd);
6716 pcix_cmd &= ~PCI_X_CMD_ERO;
6717 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
6718 pcix_cmd);
6719 }
Michael Chanee6a99b2007-07-18 21:49:10 -07006720
6721 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
Michael Chanee6a99b2007-07-18 21:49:10 -07006722
6723 /* Chip reset on 5780 will reset MSI enable bit,
6724 * so need to restore it.
6725 */
6726 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
6727 u16 ctrl;
6728
6729 pci_read_config_word(tp->pdev,
6730 tp->msi_cap + PCI_MSI_FLAGS,
6731 &ctrl);
6732 pci_write_config_word(tp->pdev,
6733 tp->msi_cap + PCI_MSI_FLAGS,
6734 ctrl | PCI_MSI_FLAGS_ENABLE);
6735 val = tr32(MSGINT_MODE);
6736 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
6737 }
6738 }
6739}
6740
Linus Torvalds1da177e2005-04-16 15:20:36 -07006741static void tg3_stop_fw(struct tg3 *);
6742
6743/* tp->lock is held. */
6744static int tg3_chip_reset(struct tg3 *tp)
6745{
6746 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07006747 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00006748 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006749
David S. Millerf49639e2006-06-09 11:58:36 -07006750 tg3_nvram_lock(tp);
6751
Matt Carlson77b483f2008-08-15 14:07:24 -07006752 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
6753
David S. Millerf49639e2006-06-09 11:58:36 -07006754 /* No matching tg3_nvram_unlock() after this because
6755 * chip reset below will undo the nvram lock.
6756 */
6757 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006758
Michael Chanee6a99b2007-07-18 21:49:10 -07006759 /* GRC_MISC_CFG core clock reset will clear the memory
6760 * enable bit in PCI register 4 and the MSI enable bit
6761 * on some chips, so we save relevant registers here.
6762 */
6763 tg3_save_pci_state(tp);
6764
Michael Chand9ab5ad2006-03-20 22:27:35 -08006765 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08006766 (tp->tg3_flags3 & TG3_FLG3_5755_PLUS))
Michael Chand9ab5ad2006-03-20 22:27:35 -08006767 tw32(GRC_FASTBOOT_PC, 0);
6768
Linus Torvalds1da177e2005-04-16 15:20:36 -07006769 /*
6770 * We must avoid the readl() that normally takes place.
6771 * It locks machines, causes machine checks, and other
6772 * fun things. So, temporarily disable the 5701
6773 * hardware workaround, while we do the reset.
6774 */
Michael Chan1ee582d2005-08-09 20:16:46 -07006775 write_op = tp->write32;
6776 if (write_op == tg3_write_flush_reg32)
6777 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006778
Michael Chand18edcb2007-03-24 20:57:11 -07006779 /* Prevent the irq handler from reading or writing PCI registers
6780 * during chip reset when the memory enable bit in the PCI command
6781 * register may be cleared. The chip does not generate interrupt
6782 * at this time, but the irq handler may still be called due to irq
6783 * sharing or irqpoll.
6784 */
6785 tp->tg3_flags |= TG3_FLAG_CHIP_RESETTING;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006786 for (i = 0; i < tp->irq_cnt; i++) {
6787 struct tg3_napi *tnapi = &tp->napi[i];
6788 if (tnapi->hw_status) {
6789 tnapi->hw_status->status = 0;
6790 tnapi->hw_status->status_tag = 0;
6791 }
6792 tnapi->last_tag = 0;
6793 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07006794 }
Michael Chand18edcb2007-03-24 20:57:11 -07006795 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00006796
6797 for (i = 0; i < tp->irq_cnt; i++)
6798 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07006799
Matt Carlson255ca312009-08-25 10:07:27 +00006800 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
6801 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
6802 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
6803 }
6804
Linus Torvalds1da177e2005-04-16 15:20:36 -07006805 /* do the reset */
6806 val = GRC_MISC_CFG_CORECLK_RESET;
6807
6808 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
6809 if (tr32(0x7e2c) == 0x60) {
6810 tw32(0x7e2c, 0x20);
6811 }
6812 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
6813 tw32(GRC_MISC_CFG, (1 << 29));
6814 val |= (1 << 29);
6815 }
6816 }
6817
Michael Chanb5d37722006-09-27 16:06:21 -07006818 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
6819 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
6820 tw32(GRC_VCPU_EXT_CTRL,
6821 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
6822 }
6823
Linus Torvalds1da177e2005-04-16 15:20:36 -07006824 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
6825 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
6826 tw32(GRC_MISC_CFG, val);
6827
Michael Chan1ee582d2005-08-09 20:16:46 -07006828 /* restore 5701 hardware bug workaround write method */
6829 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006830
6831 /* Unfortunately, we have to delay before the PCI read back.
6832 * Some 575X chips even will not respond to a PCI cfg access
6833 * when the reset command is given to the chip.
6834 *
6835 * How do these hardware designers expect things to work
6836 * properly if the PCI write is posted for a long period
6837 * of time? It is always necessary to have some method by
6838 * which a register read back can occur to push the write
6839 * out which does the reset.
6840 *
6841 * For most tg3 variants the trick below was working.
6842 * Ho hum...
6843 */
6844 udelay(120);
6845
6846 /* Flush PCI posted writes. The normal MMIO registers
6847 * are inaccessible at this time so this is the only
6848 * way to make this reliably (actually, this is no longer
6849 * the case, see above). I tried to use indirect
6850 * register read/write but this upset some 5701 variants.
6851 */
6852 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
6853
6854 udelay(120);
6855
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006856 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) && tp->pcie_cap) {
Matt Carlsone7126992009-08-25 10:08:16 +00006857 u16 val16;
6858
Linus Torvalds1da177e2005-04-16 15:20:36 -07006859 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
6860 int i;
6861 u32 cfg_val;
6862
6863 /* Wait for link training to complete. */
6864 for (i = 0; i < 5000; i++)
6865 udelay(100);
6866
6867 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
6868 pci_write_config_dword(tp->pdev, 0xc4,
6869 cfg_val | (1 << 15));
6870 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006871
Matt Carlsone7126992009-08-25 10:08:16 +00006872 /* Clear the "no snoop" and "relaxed ordering" bits. */
6873 pci_read_config_word(tp->pdev,
6874 tp->pcie_cap + PCI_EXP_DEVCTL,
6875 &val16);
6876 val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
6877 PCI_EXP_DEVCTL_NOSNOOP_EN);
6878 /*
6879 * Older PCIe devices only support the 128 byte
6880 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006881 */
Matt Carlsone7126992009-08-25 10:08:16 +00006882 if (!(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
6883 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784))
6884 val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006885 pci_write_config_word(tp->pdev,
6886 tp->pcie_cap + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00006887 val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006888
6889 pcie_set_readrq(tp->pdev, 4096);
6890
6891 /* Clear error status */
6892 pci_write_config_word(tp->pdev,
6893 tp->pcie_cap + PCI_EXP_DEVSTA,
6894 PCI_EXP_DEVSTA_CED |
6895 PCI_EXP_DEVSTA_NFED |
6896 PCI_EXP_DEVSTA_FED |
6897 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006898 }
6899
Michael Chanee6a99b2007-07-18 21:49:10 -07006900 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006901
Michael Chand18edcb2007-03-24 20:57:11 -07006902 tp->tg3_flags &= ~TG3_FLAG_CHIP_RESETTING;
6903
Michael Chanee6a99b2007-07-18 21:49:10 -07006904 val = 0;
6905 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
Michael Chan4cf78e42005-07-25 12:29:19 -07006906 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07006907 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006908
6909 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
6910 tg3_stop_fw(tp);
6911 tw32(0x5000, 0x400);
6912 }
6913
6914 tw32(GRC_MODE, tp->grc_mode);
6915
6916 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01006917 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006918
6919 tw32(0xc4, val | (1 << 15));
6920 }
6921
6922 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
6923 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
6924 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
6925 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
6926 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
6927 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
6928 }
6929
6930 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
6931 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
6932 tw32_f(MAC_MODE, tp->mac_mode);
Michael Chan747e8f82005-07-25 12:33:22 -07006933 } else if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES) {
6934 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
6935 tw32_f(MAC_MODE, tp->mac_mode);
Matt Carlson3bda1252008-08-15 14:08:22 -07006936 } else if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
6937 tp->mac_mode &= (MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN);
6938 if (tp->mac_mode & MAC_MODE_APE_TX_EN)
6939 tp->mac_mode |= MAC_MODE_TDE_ENABLE;
6940 tw32_f(MAC_MODE, tp->mac_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006941 } else
6942 tw32_f(MAC_MODE, 0);
6943 udelay(40);
6944
Matt Carlson77b483f2008-08-15 14:07:24 -07006945 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
6946
Michael Chan7a6f4362006-09-27 16:03:31 -07006947 err = tg3_poll_fw(tp);
6948 if (err)
6949 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006950
Matt Carlson0a9140c2009-08-28 12:27:50 +00006951 tg3_mdio_start(tp);
6952
Matt Carlson52cdf852009-11-02 14:25:06 +00006953 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
6954 u8 phy_addr;
6955
6956 phy_addr = tp->phy_addr;
6957 tp->phy_addr = TG3_PHY_PCIE_ADDR;
6958
6959 tg3_writephy(tp, TG3_PCIEPHY_BLOCK_ADDR,
6960 TG3_PCIEPHY_TXB_BLK << TG3_PCIEPHY_BLOCK_SHIFT);
6961 val = TG3_PCIEPHY_TX0CTRL1_TXOCM | TG3_PCIEPHY_TX0CTRL1_RDCTL |
6962 TG3_PCIEPHY_TX0CTRL1_TXCMV | TG3_PCIEPHY_TX0CTRL1_TKSEL |
6963 TG3_PCIEPHY_TX0CTRL1_NB_EN;
6964 tg3_writephy(tp, TG3_PCIEPHY_TX0CTRL1, val);
6965 udelay(10);
6966
6967 tg3_writephy(tp, TG3_PCIEPHY_BLOCK_ADDR,
6968 TG3_PCIEPHY_XGXS_BLK1 << TG3_PCIEPHY_BLOCK_SHIFT);
6969 val = TG3_PCIEPHY_PWRMGMT4_LOWPWR_EN |
6970 TG3_PCIEPHY_PWRMGMT4_L1PLLPD_EN;
6971 tg3_writephy(tp, TG3_PCIEPHY_PWRMGMT4, val);
6972 udelay(10);
6973
6974 tp->phy_addr = phy_addr;
6975 }
6976
Linus Torvalds1da177e2005-04-16 15:20:36 -07006977 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00006978 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
6979 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonb703df62009-12-03 08:36:21 +00006980 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
6981 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57765) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01006982 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006983
6984 tw32(0x7c00, val | (1 << 25));
6985 }
6986
6987 /* Reprobe ASF enable state. */
6988 tp->tg3_flags &= ~TG3_FLAG_ENABLE_ASF;
6989 tp->tg3_flags2 &= ~TG3_FLG2_ASF_NEW_HANDSHAKE;
6990 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
6991 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
6992 u32 nic_cfg;
6993
6994 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
6995 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
6996 tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
Matt Carlson4ba526c2008-08-15 14:10:04 -07006997 tp->last_event_jiffies = jiffies;
John W. Linvillecbf46852005-04-21 17:01:29 -07006998 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006999 tp->tg3_flags2 |= TG3_FLG2_ASF_NEW_HANDSHAKE;
7000 }
7001 }
7002
7003 return 0;
7004}
7005
7006/* tp->lock is held. */
7007static void tg3_stop_fw(struct tg3 *tp)
7008{
Matt Carlson0d3031d2007-10-10 18:02:43 -07007009 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
7010 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07007011 /* Wait for RX cpu to ACK the previous event. */
7012 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007013
7014 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007015
7016 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007017
Matt Carlson7c5026a2008-05-02 16:49:29 -07007018 /* Wait for RX cpu to ACK this event. */
7019 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007020 }
7021}
7022
7023/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07007024static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007025{
7026 int err;
7027
7028 tg3_stop_fw(tp);
7029
Michael Chan944d9802005-05-29 14:57:48 -07007030 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007031
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007032 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007033 err = tg3_chip_reset(tp);
7034
Matt Carlsondaba2a62009-04-20 06:58:52 +00007035 __tg3_set_mac_addr(tp, 0);
7036
Michael Chan944d9802005-05-29 14:57:48 -07007037 tg3_write_sig_legacy(tp, kind);
7038 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007039
7040 if (err)
7041 return err;
7042
7043 return 0;
7044}
7045
Linus Torvalds1da177e2005-04-16 15:20:36 -07007046#define RX_CPU_SCRATCH_BASE 0x30000
7047#define RX_CPU_SCRATCH_SIZE 0x04000
7048#define TX_CPU_SCRATCH_BASE 0x34000
7049#define TX_CPU_SCRATCH_SIZE 0x04000
7050
7051/* tp->lock is held. */
7052static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
7053{
7054 int i;
7055
Eric Sesterhenn5d9428d2006-04-02 13:52:48 +02007056 BUG_ON(offset == TX_CPU_BASE &&
7057 (tp->tg3_flags2 & TG3_FLG2_5705_PLUS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007058
Michael Chanb5d37722006-09-27 16:06:21 -07007059 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7060 u32 val = tr32(GRC_VCPU_EXT_CTRL);
7061
7062 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
7063 return 0;
7064 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007065 if (offset == RX_CPU_BASE) {
7066 for (i = 0; i < 10000; i++) {
7067 tw32(offset + CPU_STATE, 0xffffffff);
7068 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7069 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7070 break;
7071 }
7072
7073 tw32(offset + CPU_STATE, 0xffffffff);
7074 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
7075 udelay(10);
7076 } else {
7077 for (i = 0; i < 10000; i++) {
7078 tw32(offset + CPU_STATE, 0xffffffff);
7079 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7080 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7081 break;
7082 }
7083 }
7084
7085 if (i >= 10000) {
7086 printk(KERN_ERR PFX "tg3_reset_cpu timed out for %s, "
7087 "and %s CPU\n",
7088 tp->dev->name,
7089 (offset == RX_CPU_BASE ? "RX" : "TX"));
7090 return -ENODEV;
7091 }
Michael Chanec41c7d2006-01-17 02:40:55 -08007092
7093 /* Clear firmware's nvram arbitration. */
7094 if (tp->tg3_flags & TG3_FLAG_NVRAM)
7095 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007096 return 0;
7097}
7098
7099struct fw_info {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007100 unsigned int fw_base;
7101 unsigned int fw_len;
7102 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007103};
7104
7105/* tp->lock is held. */
7106static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base, u32 cpu_scratch_base,
7107 int cpu_scratch_size, struct fw_info *info)
7108{
Michael Chanec41c7d2006-01-17 02:40:55 -08007109 int err, lock_err, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007110 void (*write_op)(struct tg3 *, u32, u32);
7111
7112 if (cpu_base == TX_CPU_BASE &&
7113 (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7114 printk(KERN_ERR PFX "tg3_load_firmware_cpu: Trying to load "
7115 "TX cpu firmware on %s which is 5705.\n",
7116 tp->dev->name);
7117 return -EINVAL;
7118 }
7119
7120 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
7121 write_op = tg3_write_mem;
7122 else
7123 write_op = tg3_write_indirect_reg32;
7124
Michael Chan1b628152005-05-29 14:59:49 -07007125 /* It is possible that bootcode is still loading at this point.
7126 * Get the nvram lock first before halting the cpu.
7127 */
Michael Chanec41c7d2006-01-17 02:40:55 -08007128 lock_err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007129 err = tg3_halt_cpu(tp, cpu_base);
Michael Chanec41c7d2006-01-17 02:40:55 -08007130 if (!lock_err)
7131 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007132 if (err)
7133 goto out;
7134
7135 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
7136 write_op(tp, cpu_scratch_base + i, 0);
7137 tw32(cpu_base + CPU_STATE, 0xffffffff);
7138 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007139 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007140 write_op(tp, (cpu_scratch_base +
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007141 (info->fw_base & 0xffff) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07007142 (i * sizeof(u32))),
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007143 be32_to_cpu(info->fw_data[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007144
7145 err = 0;
7146
7147out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007148 return err;
7149}
7150
7151/* tp->lock is held. */
7152static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
7153{
7154 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007155 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007156 int err, i;
7157
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007158 fw_data = (void *)tp->fw->data;
7159
7160 /* Firmware blob starts with version numbers, followed by
7161 start address and length. We are setting complete length.
7162 length = end_address_of_bss - start_address_of_text.
7163 Remainder is the blob to be loaded contiguously
7164 from start address. */
7165
7166 info.fw_base = be32_to_cpu(fw_data[1]);
7167 info.fw_len = tp->fw->size - 12;
7168 info.fw_data = &fw_data[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007169
7170 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
7171 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
7172 &info);
7173 if (err)
7174 return err;
7175
7176 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
7177 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
7178 &info);
7179 if (err)
7180 return err;
7181
7182 /* Now startup only the RX cpu. */
7183 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007184 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007185
7186 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007187 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007188 break;
7189 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7190 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007191 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007192 udelay(1000);
7193 }
7194 if (i >= 5) {
7195 printk(KERN_ERR PFX "tg3_load_firmware fails for %s "
7196 "to set RX CPU PC, is %08x should be %08x\n",
7197 tp->dev->name, tr32(RX_CPU_BASE + CPU_PC),
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007198 info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007199 return -ENODEV;
7200 }
7201 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7202 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
7203
7204 return 0;
7205}
7206
Linus Torvalds1da177e2005-04-16 15:20:36 -07007207/* 5705 needs a special version of the TSO firmware. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007208
7209/* tp->lock is held. */
7210static int tg3_load_tso_firmware(struct tg3 *tp)
7211{
7212 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007213 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007214 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
7215 int err, i;
7216
7217 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
7218 return 0;
7219
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007220 fw_data = (void *)tp->fw->data;
7221
7222 /* Firmware blob starts with version numbers, followed by
7223 start address and length. We are setting complete length.
7224 length = end_address_of_bss - start_address_of_text.
7225 Remainder is the blob to be loaded contiguously
7226 from start address. */
7227
7228 info.fw_base = be32_to_cpu(fw_data[1]);
7229 cpu_scratch_size = tp->fw_len;
7230 info.fw_len = tp->fw->size - 12;
7231 info.fw_data = &fw_data[3];
7232
Linus Torvalds1da177e2005-04-16 15:20:36 -07007233 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007234 cpu_base = RX_CPU_BASE;
7235 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007236 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007237 cpu_base = TX_CPU_BASE;
7238 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
7239 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
7240 }
7241
7242 err = tg3_load_firmware_cpu(tp, cpu_base,
7243 cpu_scratch_base, cpu_scratch_size,
7244 &info);
7245 if (err)
7246 return err;
7247
7248 /* Now startup the cpu. */
7249 tw32(cpu_base + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007250 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007251
7252 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007253 if (tr32(cpu_base + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007254 break;
7255 tw32(cpu_base + CPU_STATE, 0xffffffff);
7256 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007257 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007258 udelay(1000);
7259 }
7260 if (i >= 5) {
7261 printk(KERN_ERR PFX "tg3_load_tso_firmware fails for %s "
7262 "to set CPU PC, is %08x should be %08x\n",
7263 tp->dev->name, tr32(cpu_base + CPU_PC),
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007264 info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007265 return -ENODEV;
7266 }
7267 tw32(cpu_base + CPU_STATE, 0xffffffff);
7268 tw32_f(cpu_base + CPU_MODE, 0x00000000);
7269 return 0;
7270}
7271
Linus Torvalds1da177e2005-04-16 15:20:36 -07007272
Linus Torvalds1da177e2005-04-16 15:20:36 -07007273static int tg3_set_mac_addr(struct net_device *dev, void *p)
7274{
7275 struct tg3 *tp = netdev_priv(dev);
7276 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07007277 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007278
Michael Chanf9804dd2005-09-27 12:13:10 -07007279 if (!is_valid_ether_addr(addr->sa_data))
7280 return -EINVAL;
7281
Linus Torvalds1da177e2005-04-16 15:20:36 -07007282 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7283
Michael Chane75f7c92006-03-20 21:33:26 -08007284 if (!netif_running(dev))
7285 return 0;
7286
Michael Chan58712ef2006-04-29 18:58:01 -07007287 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) {
Michael Chan986e0ae2007-05-05 12:10:20 -07007288 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07007289
Michael Chan986e0ae2007-05-05 12:10:20 -07007290 addr0_high = tr32(MAC_ADDR_0_HIGH);
7291 addr0_low = tr32(MAC_ADDR_0_LOW);
7292 addr1_high = tr32(MAC_ADDR_1_HIGH);
7293 addr1_low = tr32(MAC_ADDR_1_LOW);
7294
7295 /* Skip MAC addr 1 if ASF is using it. */
7296 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
7297 !(addr1_high == 0 && addr1_low == 0))
7298 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07007299 }
Michael Chan986e0ae2007-05-05 12:10:20 -07007300 spin_lock_bh(&tp->lock);
7301 __tg3_set_mac_addr(tp, skip_mac_1);
7302 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007303
Michael Chanb9ec6c12006-07-25 16:37:27 -07007304 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007305}
7306
7307/* tp->lock is held. */
7308static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
7309 dma_addr_t mapping, u32 maxlen_flags,
7310 u32 nic_addr)
7311{
7312 tg3_write_mem(tp,
7313 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
7314 ((u64) mapping >> 32));
7315 tg3_write_mem(tp,
7316 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
7317 ((u64) mapping & 0xffffffff));
7318 tg3_write_mem(tp,
7319 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
7320 maxlen_flags);
7321
7322 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7323 tg3_write_mem(tp,
7324 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
7325 nic_addr);
7326}
7327
7328static void __tg3_set_rx_mode(struct net_device *);
Michael Chand244c892005-07-05 14:42:33 -07007329static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07007330{
Matt Carlsonb6080e12009-09-01 13:12:00 +00007331 int i;
7332
Matt Carlson19cfaec2009-12-03 08:36:20 +00007333 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007334 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
7335 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
7336 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007337 } else {
7338 tw32(HOSTCC_TXCOL_TICKS, 0);
7339 tw32(HOSTCC_TXMAX_FRAMES, 0);
7340 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007341 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007342
Matt Carlson19cfaec2009-12-03 08:36:20 +00007343 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSIX)) {
7344 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
7345 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
7346 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
7347 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007348 tw32(HOSTCC_RXCOL_TICKS, 0);
7349 tw32(HOSTCC_RXMAX_FRAMES, 0);
7350 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07007351 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007352
David S. Miller15f98502005-05-18 22:49:26 -07007353 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7354 u32 val = ec->stats_block_coalesce_usecs;
7355
Matt Carlsonb6080e12009-09-01 13:12:00 +00007356 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
7357 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
7358
David S. Miller15f98502005-05-18 22:49:26 -07007359 if (!netif_carrier_ok(tp->dev))
7360 val = 0;
7361
7362 tw32(HOSTCC_STAT_COAL_TICKS, val);
7363 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007364
7365 for (i = 0; i < tp->irq_cnt - 1; i++) {
7366 u32 reg;
7367
7368 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
7369 tw32(reg, ec->rx_coalesce_usecs);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007370 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
7371 tw32(reg, ec->rx_max_coalesced_frames);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007372 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
7373 tw32(reg, ec->rx_max_coalesced_frames_irq);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007374
7375 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS) {
7376 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
7377 tw32(reg, ec->tx_coalesce_usecs);
7378 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
7379 tw32(reg, ec->tx_max_coalesced_frames);
7380 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
7381 tw32(reg, ec->tx_max_coalesced_frames_irq);
7382 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007383 }
7384
7385 for (; i < tp->irq_max - 1; i++) {
7386 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007387 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007388 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007389
7390 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS) {
7391 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
7392 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
7393 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
7394 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007395 }
David S. Miller15f98502005-05-18 22:49:26 -07007396}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007397
7398/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00007399static void tg3_rings_reset(struct tg3 *tp)
7400{
7401 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007402 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007403 struct tg3_napi *tnapi = &tp->napi[0];
7404
7405 /* Disable all transmit rings but the first. */
7406 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7407 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00007408 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
7409 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007410 else
7411 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7412
7413 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7414 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
7415 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
7416 BDINFO_FLAGS_DISABLED);
7417
7418
7419 /* Disable all receive return rings but the first. */
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007420 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
7421 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
7422 else if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00007423 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00007424 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
7425 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson2d31eca2009-09-01 12:53:31 +00007426 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
7427 else
7428 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7429
7430 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7431 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
7432 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
7433 BDINFO_FLAGS_DISABLED);
7434
7435 /* Disable interrupts */
7436 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
7437
7438 /* Zero mailbox registers. */
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007439 if (tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX) {
7440 for (i = 1; i < TG3_IRQ_MAX_VECS; i++) {
7441 tp->napi[i].tx_prod = 0;
7442 tp->napi[i].tx_cons = 0;
7443 tw32_mailbox(tp->napi[i].prodmbox, 0);
7444 tw32_rx_mbox(tp->napi[i].consmbox, 0);
7445 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
7446 }
7447 } else {
7448 tp->napi[0].tx_prod = 0;
7449 tp->napi[0].tx_cons = 0;
7450 tw32_mailbox(tp->napi[0].prodmbox, 0);
7451 tw32_rx_mbox(tp->napi[0].consmbox, 0);
7452 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007453
7454 /* Make sure the NIC-based send BD rings are disabled. */
7455 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7456 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
7457 for (i = 0; i < 16; i++)
7458 tw32_tx_mbox(mbox + i * 8, 0);
7459 }
7460
7461 txrcb = NIC_SRAM_SEND_RCB;
7462 rxrcb = NIC_SRAM_RCV_RET_RCB;
7463
7464 /* Clear status block in ram. */
7465 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7466
7467 /* Set status block DMA address */
7468 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
7469 ((u64) tnapi->status_mapping >> 32));
7470 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
7471 ((u64) tnapi->status_mapping & 0xffffffff));
7472
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007473 if (tnapi->tx_ring) {
7474 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7475 (TG3_TX_RING_SIZE <<
7476 BDINFO_FLAGS_MAXLEN_SHIFT),
7477 NIC_SRAM_TX_BUFFER_DESC);
7478 txrcb += TG3_BDINFO_SIZE;
7479 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007480
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007481 if (tnapi->rx_rcb) {
7482 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
7483 (TG3_RX_RCB_RING_SIZE(tp) <<
7484 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
7485 rxrcb += TG3_BDINFO_SIZE;
7486 }
7487
7488 stblk = HOSTCC_STATBLCK_RING1;
7489
7490 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
7491 u64 mapping = (u64)tnapi->status_mapping;
7492 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
7493 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
7494
7495 /* Clear status block in ram. */
7496 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7497
Matt Carlson19cfaec2009-12-03 08:36:20 +00007498 if (tnapi->tx_ring) {
7499 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7500 (TG3_TX_RING_SIZE <<
7501 BDINFO_FLAGS_MAXLEN_SHIFT),
7502 NIC_SRAM_TX_BUFFER_DESC);
7503 txrcb += TG3_BDINFO_SIZE;
7504 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007505
7506 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
7507 (TG3_RX_RCB_RING_SIZE(tp) <<
7508 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
7509
7510 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007511 rxrcb += TG3_BDINFO_SIZE;
7512 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007513}
7514
7515/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07007516static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007517{
7518 u32 val, rdmac_mode;
7519 int i, err, limit;
Matt Carlson21f581a2009-08-28 14:00:25 +00007520 struct tg3_rx_prodring_set *tpr = &tp->prodring[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007521
7522 tg3_disable_ints(tp);
7523
7524 tg3_stop_fw(tp);
7525
7526 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
7527
7528 if (tp->tg3_flags & TG3_FLAG_INIT_COMPLETE) {
Michael Chane6de8ad2005-05-05 14:42:41 -07007529 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007530 }
7531
Matt Carlsondd477002008-05-25 23:45:58 -07007532 if (reset_phy &&
7533 !(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB))
Michael Chand4d2c552006-03-20 17:47:20 -08007534 tg3_phy_reset(tp);
7535
Linus Torvalds1da177e2005-04-16 15:20:36 -07007536 err = tg3_chip_reset(tp);
7537 if (err)
7538 return err;
7539
7540 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
7541
Matt Carlsonbcb37f62008-11-03 16:52:09 -08007542 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07007543 val = tr32(TG3_CPMU_CTRL);
7544 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
7545 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08007546
7547 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
7548 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
7549 val |= CPMU_LSPD_10MB_MACCLK_6_25;
7550 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
7551
7552 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
7553 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
7554 val |= CPMU_LNK_AWARE_MACCLK_6_25;
7555 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
7556
7557 val = tr32(TG3_CPMU_HST_ACC);
7558 val &= ~CPMU_HST_ACC_MACCLK_MASK;
7559 val |= CPMU_HST_ACC_MACCLK_6_25;
7560 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07007561 }
7562
Matt Carlson33466d92009-04-20 06:57:41 +00007563 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7564 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
7565 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
7566 PCIE_PWR_MGMT_L1_THRESH_4MS;
7567 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00007568
7569 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
7570 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
7571
7572 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00007573
Matt Carlsonf40386c2009-11-02 14:24:02 +00007574 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7575 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00007576 }
7577
Matt Carlson614b0592010-01-20 16:58:02 +00007578 if (tp->tg3_flags3 & TG3_FLG3_L1PLLPD_EN) {
7579 u32 grc_mode = tr32(GRC_MODE);
7580
7581 /* Access the lower 1K of PL PCIE block registers. */
7582 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
7583 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
7584
7585 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
7586 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
7587 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
7588
7589 tw32(GRC_MODE, grc_mode);
7590 }
7591
Linus Torvalds1da177e2005-04-16 15:20:36 -07007592 /* This works around an issue with Athlon chipsets on
7593 * B3 tigon3 silicon. This bit has no effect on any
7594 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07007595 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007596 */
Matt Carlson795d01c2007-10-07 23:28:17 -07007597 if (!(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT)) {
7598 if (!(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
7599 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
7600 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007602
7603 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
7604 (tp->tg3_flags & TG3_FLAG_PCIX_MODE)) {
7605 val = tr32(TG3PCI_PCISTATE);
7606 val |= PCISTATE_RETRY_SAME_DMA;
7607 tw32(TG3PCI_PCISTATE, val);
7608 }
7609
Matt Carlson0d3031d2007-10-10 18:02:43 -07007610 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
7611 /* Allow reads and writes to the
7612 * APE register and memory space.
7613 */
7614 val = tr32(TG3PCI_PCISTATE);
7615 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
7616 PCISTATE_ALLOW_APE_SHMEM_WR;
7617 tw32(TG3PCI_PCISTATE, val);
7618 }
7619
Linus Torvalds1da177e2005-04-16 15:20:36 -07007620 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
7621 /* Enable some hw fixes. */
7622 val = tr32(TG3PCI_MSI_DATA);
7623 val |= (1 << 26) | (1 << 28) | (1 << 29);
7624 tw32(TG3PCI_MSI_DATA, val);
7625 }
7626
7627 /* Descriptor ring init may make accesses to the
7628 * NIC SRAM area to setup the TX descriptors, so we
7629 * can only do this after the hardware has been
7630 * successfully reset.
7631 */
Michael Chan32d8c572006-07-25 16:38:29 -07007632 err = tg3_init_rings(tp);
7633 if (err)
7634 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007635
Matt Carlsonb703df62009-12-03 08:36:21 +00007636 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
7637 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00007638 val = tr32(TG3PCI_DMA_RW_CTRL) &
7639 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
7640 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
7641 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
7642 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07007643 /* This value is determined during the probe time DMA
7644 * engine test, tg3_test_dma.
7645 */
7646 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
7647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007648
7649 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
7650 GRC_MODE_4X_NIC_SEND_RINGS |
7651 GRC_MODE_NO_TX_PHDR_CSUM |
7652 GRC_MODE_NO_RX_PHDR_CSUM);
7653 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07007654
7655 /* Pseudo-header checksum is done by hardware logic and not
7656 * the offload processers, so make the chip do the pseudo-
7657 * header checksums on receive. For transmit it is more
7658 * convenient to do the pseudo-header checksum in software
7659 * as Linux does that on transmit for us in all cases.
7660 */
7661 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007662
7663 tw32(GRC_MODE,
7664 tp->grc_mode |
7665 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
7666
7667 /* Setup the timer prescalar register. Clock is always 66Mhz. */
7668 val = tr32(GRC_MISC_CFG);
7669 val &= ~0xff;
7670 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
7671 tw32(GRC_MISC_CFG, val);
7672
7673 /* Initialize MBUF/DESC pool. */
John W. Linvillecbf46852005-04-21 17:01:29 -07007674 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007675 /* Do nothing. */
7676 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
7677 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
7678 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
7679 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
7680 else
7681 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
7682 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
7683 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
7684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007685 else if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) {
7686 int fw_len;
7687
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007688 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007689 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
7690 tw32(BUFMGR_MB_POOL_ADDR,
7691 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
7692 tw32(BUFMGR_MB_POOL_SIZE,
7693 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
7694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007695
Michael Chan0f893dc2005-07-25 12:30:38 -07007696 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007697 tw32(BUFMGR_MB_RDMA_LOW_WATER,
7698 tp->bufmgr_config.mbuf_read_dma_low_water);
7699 tw32(BUFMGR_MB_MACRX_LOW_WATER,
7700 tp->bufmgr_config.mbuf_mac_rx_low_water);
7701 tw32(BUFMGR_MB_HIGH_WATER,
7702 tp->bufmgr_config.mbuf_high_water);
7703 } else {
7704 tw32(BUFMGR_MB_RDMA_LOW_WATER,
7705 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
7706 tw32(BUFMGR_MB_MACRX_LOW_WATER,
7707 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
7708 tw32(BUFMGR_MB_HIGH_WATER,
7709 tp->bufmgr_config.mbuf_high_water_jumbo);
7710 }
7711 tw32(BUFMGR_DMA_LOW_WATER,
7712 tp->bufmgr_config.dma_low_water);
7713 tw32(BUFMGR_DMA_HIGH_WATER,
7714 tp->bufmgr_config.dma_high_water);
7715
7716 tw32(BUFMGR_MODE, BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE);
7717 for (i = 0; i < 2000; i++) {
7718 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
7719 break;
7720 udelay(10);
7721 }
7722 if (i >= 2000) {
7723 printk(KERN_ERR PFX "tg3_reset_hw cannot enable BUFMGR for %s.\n",
7724 tp->dev->name);
7725 return -ENODEV;
7726 }
7727
7728 /* Setup replenish threshold. */
Michael Chanf92905d2006-06-29 20:14:29 -07007729 val = tp->rx_pending / 8;
7730 if (val == 0)
7731 val = 1;
7732 else if (val > tp->rx_std_max_post)
7733 val = tp->rx_std_max_post;
Michael Chanb5d37722006-09-27 16:06:21 -07007734 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7735 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
7736 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
7737
7738 if (val > (TG3_RX_INTERNAL_RING_SZ_5906 / 2))
7739 val = TG3_RX_INTERNAL_RING_SZ_5906 / 2;
7740 }
Michael Chanf92905d2006-06-29 20:14:29 -07007741
7742 tw32(RCVBDI_STD_THRESH, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007743
7744 /* Initialize TG3_BDINFO's at:
7745 * RCVDBDI_STD_BD: standard eth size rx ring
7746 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
7747 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
7748 *
7749 * like so:
7750 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
7751 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
7752 * ring attribute flags
7753 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
7754 *
7755 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
7756 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
7757 *
7758 * The size of each ring is fixed in the firmware, but the location is
7759 * configurable.
7760 */
7761 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00007762 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007763 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00007764 ((u64) tpr->rx_std_mapping & 0xffffffff));
Matt Carlson87668d32009-11-13 13:03:34 +00007765 if (!(tp->tg3_flags3 & TG3_FLG3_5755_PLUS))
7766 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
7767 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007768
Matt Carlsonfdb72b32009-08-28 13:57:12 +00007769 /* Disable the mini ring */
7770 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007771 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
7772 BDINFO_FLAGS_DISABLED);
7773
Matt Carlsonfdb72b32009-08-28 13:57:12 +00007774 /* Program the jumbo buffer descriptor ring control
7775 * blocks on those devices that have them.
7776 */
Matt Carlson8f666b02009-08-28 13:58:24 +00007777 if ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) &&
Matt Carlsonfdb72b32009-08-28 13:57:12 +00007778 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007779 /* Setup replenish threshold. */
7780 tw32(RCVBDI_JUMBO_THRESH, tp->rx_jumbo_pending / 8);
7781
Michael Chan0f893dc2005-07-25 12:30:38 -07007782 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007783 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00007784 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007785 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00007786 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007787 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlson79ed5ac2009-08-28 14:00:55 +00007788 (RX_JUMBO_MAX_SIZE << BDINFO_FLAGS_MAXLEN_SHIFT) |
7789 BDINFO_FLAGS_USE_EXT_RECV);
Matt Carlson87668d32009-11-13 13:03:34 +00007790 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7791 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
7792 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007793 } else {
7794 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
7795 BDINFO_FLAGS_DISABLED);
7796 }
7797
Matt Carlsonb703df62009-12-03 08:36:21 +00007798 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
7799 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007800 val = (RX_STD_MAX_SIZE_5705 << BDINFO_FLAGS_MAXLEN_SHIFT) |
7801 (RX_STD_MAX_SIZE << 2);
7802 else
7803 val = RX_STD_MAX_SIZE << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00007804 } else
7805 val = RX_STD_MAX_SIZE_5705 << BDINFO_FLAGS_MAXLEN_SHIFT;
7806
7807 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007808
Matt Carlson411da642009-11-13 13:03:46 +00007809 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e62009-11-13 13:03:49 +00007810 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007811
Matt Carlson411da642009-11-13 13:03:46 +00007812 tpr->rx_jmb_prod_idx = (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) ?
Matt Carlson21f581a2009-08-28 14:00:25 +00007813 tp->rx_jumbo_pending : 0;
Matt Carlson66711e62009-11-13 13:03:49 +00007814 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007815
Matt Carlsonb703df62009-12-03 08:36:21 +00007816 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
7817 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007818 tw32(STD_REPLENISH_LWM, 32);
7819 tw32(JMB_REPLENISH_LWM, 16);
7820 }
7821
Matt Carlson2d31eca2009-09-01 12:53:31 +00007822 tg3_rings_reset(tp);
7823
Linus Torvalds1da177e2005-04-16 15:20:36 -07007824 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07007825 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007826
7827 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00007828 tw32(MAC_RX_MTU_SIZE,
7829 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007830
7831 /* The slot time is changed by tg3_setup_phy if we
7832 * run at gigabit with half duplex.
7833 */
7834 tw32(MAC_TX_LENGTHS,
7835 (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
7836 (6 << TX_LENGTHS_IPG_SHIFT) |
7837 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
7838
7839 /* Receive rules. */
7840 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
7841 tw32(RCVLPC_CONFIG, 0x0181);
7842
7843 /* Calculate RDMAC_MODE setting early, we need it to determine
7844 * the RCVLPC_STATE_ENABLE mask.
7845 */
7846 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
7847 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
7848 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
7849 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
7850 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07007851
Matt Carlson57e69832008-05-25 23:48:31 -07007852 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08007853 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
7854 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07007855 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
7856 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
7857 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
7858
Michael Chan85e94ce2005-04-21 17:05:28 -07007859 /* If statement applies to 5705 and 5750 PCI devices only */
7860 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
7861 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) ||
7862 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007863 if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07007864 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007865 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
7866 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
7867 !(tp->tg3_flags2 & TG3_FLG2_IS_5788)) {
7868 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
7869 }
7870 }
7871
Michael Chan85e94ce2005-04-21 17:05:28 -07007872 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)
7873 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
7874
Linus Torvalds1da177e2005-04-16 15:20:36 -07007875 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
Matt Carlson027455a2008-12-21 20:19:30 -08007876 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
7877
Matt Carlsone849cdc2009-11-13 13:03:38 +00007878 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
7879 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson027455a2008-12-21 20:19:30 -08007880 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
7881 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007882
7883 /* Receive/send statistics. */
Michael Chan16613942006-06-29 20:15:13 -07007884 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
7885 val = tr32(RCVLPC_STATS_ENABLE);
7886 val &= ~RCVLPC_STATSENAB_DACK_FIX;
7887 tw32(RCVLPC_STATS_ENABLE, val);
7888 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
7889 (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007890 val = tr32(RCVLPC_STATS_ENABLE);
7891 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
7892 tw32(RCVLPC_STATS_ENABLE, val);
7893 } else {
7894 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
7895 }
7896 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
7897 tw32(SNDDATAI_STATSENAB, 0xffffff);
7898 tw32(SNDDATAI_STATSCTRL,
7899 (SNDDATAI_SCTRL_ENABLE |
7900 SNDDATAI_SCTRL_FASTUPD));
7901
7902 /* Setup host coalescing engine. */
7903 tw32(HOSTCC_MODE, 0);
7904 for (i = 0; i < 2000; i++) {
7905 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
7906 break;
7907 udelay(10);
7908 }
7909
Michael Chand244c892005-07-05 14:42:33 -07007910 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007911
Linus Torvalds1da177e2005-04-16 15:20:36 -07007912 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7913 /* Status/statistics block address. See tg3_timer,
7914 * the tg3_periodic_fetch_stats call there, and
7915 * tg3_get_stats to see how this works for 5705/5750 chips.
7916 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007917 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
7918 ((u64) tp->stats_mapping >> 32));
7919 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
7920 ((u64) tp->stats_mapping & 0xffffffff));
7921 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00007922
Linus Torvalds1da177e2005-04-16 15:20:36 -07007923 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00007924
7925 /* Clear statistics and status block memory areas */
7926 for (i = NIC_SRAM_STATS_BLK;
7927 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
7928 i += sizeof(u32)) {
7929 tg3_write_mem(tp, i, 0);
7930 udelay(40);
7931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007932 }
7933
7934 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
7935
7936 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
7937 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
7938 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7939 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
7940
Michael Chanc94e3942005-09-27 12:12:42 -07007941 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES) {
7942 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
7943 /* reset to prevent losing 1st rx packet intermittently */
7944 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
7945 udelay(10);
7946 }
7947
Matt Carlson3bda1252008-08-15 14:08:22 -07007948 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
7949 tp->mac_mode &= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
7950 else
7951 tp->mac_mode = 0;
7952 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Linus Torvalds1da177e2005-04-16 15:20:36 -07007953 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE | MAC_MODE_FHDE_ENABLE;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07007954 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
7955 !(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
7956 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
7957 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007958 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
7959 udelay(40);
7960
Michael Chan314fba32005-04-21 17:07:04 -07007961 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Michael Chan9d26e212006-12-07 00:21:14 -08007962 * If TG3_FLG2_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07007963 * register to preserve the GPIO settings for LOMs. The GPIOs,
7964 * whether used as inputs or outputs, are set by boot code after
7965 * reset.
7966 */
Michael Chan9d26e212006-12-07 00:21:14 -08007967 if (!(tp->tg3_flags2 & TG3_FLG2_IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07007968 u32 gpio_mask;
7969
Michael Chan9d26e212006-12-07 00:21:14 -08007970 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
7971 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
7972 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07007973
7974 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
7975 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
7976 GRC_LCLCTRL_GPIO_OUTPUT3;
7977
Michael Chanaf36e6b2006-03-23 01:28:06 -08007978 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
7979 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
7980
Gary Zambranoaaf84462007-05-05 11:51:45 -07007981 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07007982 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
7983
7984 /* GPIO1 must be driven high for eeprom write protect */
Michael Chan9d26e212006-12-07 00:21:14 -08007985 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT)
7986 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
7987 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07007988 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007989 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
7990 udelay(100);
7991
Matt Carlsonbaf8a942009-09-01 13:13:00 +00007992 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX) {
7993 val = tr32(MSGINT_MODE);
7994 val |= MSGINT_MODE_MULTIVEC_EN | MSGINT_MODE_ENABLE;
7995 tw32(MSGINT_MODE, val);
7996 }
7997
Linus Torvalds1da177e2005-04-16 15:20:36 -07007998 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7999 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
8000 udelay(40);
8001 }
8002
8003 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
8004 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
8005 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
8006 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
8007 WDMAC_MODE_LNGREAD_ENAB);
8008
Michael Chan85e94ce2005-04-21 17:05:28 -07008009 /* If statement applies to 5705 and 5750 PCI devices only */
8010 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8011 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) ||
8012 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750) {
Matt Carlson29ea0952009-08-25 10:07:54 +00008013 if ((tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008014 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
8015 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
8016 /* nothing */
8017 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
8018 !(tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
8019 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)) {
8020 val |= WDMAC_MODE_RX_ACCEL;
8021 }
8022 }
8023
Michael Chand9ab5ad2006-03-20 22:27:35 -08008024 /* Enable host coalescing bug fix */
Matt Carlson321d32a2008-11-21 17:22:19 -08008025 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Matt Carlsonf51f3562008-05-25 23:45:08 -07008026 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad2006-03-20 22:27:35 -08008027
Matt Carlson788a0352009-11-02 14:26:03 +00008028 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
8029 val |= WDMAC_MODE_BURST_ALL_DATA;
8030
Linus Torvalds1da177e2005-04-16 15:20:36 -07008031 tw32_f(WDMAC_MODE, val);
8032 udelay(40);
8033
Matt Carlson9974a352007-10-07 23:27:28 -07008034 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
8035 u16 pcix_cmd;
8036
8037 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8038 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008039 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07008040 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
8041 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008042 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07008043 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
8044 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008045 }
Matt Carlson9974a352007-10-07 23:27:28 -07008046 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8047 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008048 }
8049
8050 tw32_f(RDMAC_MODE, rdmac_mode);
8051 udelay(40);
8052
8053 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
8054 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
8055 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07008056
8057 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
8058 tw32(SNDDATAC_MODE,
8059 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
8060 else
8061 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
8062
Linus Torvalds1da177e2005-04-16 15:20:36 -07008063 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
8064 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
8065 tw32(RCVDBDI_MODE, RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ);
8066 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008067 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
8068 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008069 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Matt Carlson19cfaec2009-12-03 08:36:20 +00008070 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008071 val |= SNDBDI_MODE_MULTI_TXQ_EN;
8072 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008073 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
8074
8075 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
8076 err = tg3_load_5701_a0_firmware_fix(tp);
8077 if (err)
8078 return err;
8079 }
8080
Linus Torvalds1da177e2005-04-16 15:20:36 -07008081 if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) {
8082 err = tg3_load_tso_firmware(tp);
8083 if (err)
8084 return err;
8085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008086
8087 tp->tx_mode = TX_MODE_ENABLE;
8088 tw32_f(MAC_TX_MODE, tp->tx_mode);
8089 udelay(100);
8090
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008091 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS) {
8092 u32 reg = MAC_RSS_INDIR_TBL_0;
8093 u8 *ent = (u8 *)&val;
8094
8095 /* Setup the indirection table */
8096 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
8097 int idx = i % sizeof(val);
8098
8099 ent[idx] = i % (tp->irq_cnt - 1);
8100 if (idx == sizeof(val) - 1) {
8101 tw32(reg, val);
8102 reg += 4;
8103 }
8104 }
8105
8106 /* Setup the "secret" hash key. */
8107 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
8108 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
8109 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
8110 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
8111 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
8112 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
8113 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
8114 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
8115 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
8116 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
8117 }
8118
Linus Torvalds1da177e2005-04-16 15:20:36 -07008119 tp->rx_mode = RX_MODE_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08008120 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Michael Chanaf36e6b2006-03-23 01:28:06 -08008121 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
8122
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008123 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)
8124 tp->rx_mode |= RX_MODE_RSS_ENABLE |
8125 RX_MODE_RSS_ITBL_HASH_BITS_7 |
8126 RX_MODE_RSS_IPV6_HASH_EN |
8127 RX_MODE_RSS_TCP_IPV6_HASH_EN |
8128 RX_MODE_RSS_IPV4_HASH_EN |
8129 RX_MODE_RSS_TCP_IPV4_HASH_EN;
8130
Linus Torvalds1da177e2005-04-16 15:20:36 -07008131 tw32_f(MAC_RX_MODE, tp->rx_mode);
8132 udelay(10);
8133
Linus Torvalds1da177e2005-04-16 15:20:36 -07008134 tw32(MAC_LED_CTRL, tp->led_ctrl);
8135
8136 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Michael Chanc94e3942005-09-27 12:12:42 -07008137 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008138 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8139 udelay(10);
8140 }
8141 tw32_f(MAC_RX_MODE, tp->rx_mode);
8142 udelay(10);
8143
8144 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
8145 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
8146 !(tp->tg3_flags2 & TG3_FLG2_SERDES_PREEMPHASIS)) {
8147 /* Set drive transmission level to 1.2V */
8148 /* only if the signal pre-emphasis bit is not set */
8149 val = tr32(MAC_SERDES_CFG);
8150 val &= 0xfffff000;
8151 val |= 0x880;
8152 tw32(MAC_SERDES_CFG, val);
8153 }
8154 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
8155 tw32(MAC_SERDES_CFG, 0x616000);
8156 }
8157
8158 /* Prevent chip from dropping frames when flow control
8159 * is enabled.
8160 */
Matt Carlson666bc832010-01-20 16:58:03 +00008161 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8162 val = 1;
8163 else
8164 val = 2;
8165 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008166
8167 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
8168 (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)) {
8169 /* Use hardware link auto-negotiation */
8170 tp->tg3_flags2 |= TG3_FLG2_HW_AUTONEG;
8171 }
8172
Michael Chand4d2c552006-03-20 17:47:20 -08008173 if ((tp->tg3_flags2 & TG3_FLG2_MII_SERDES) &&
8174 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)) {
8175 u32 tmp;
8176
8177 tmp = tr32(SERDES_RX_CTRL);
8178 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
8179 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
8180 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
8181 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8182 }
8183
Matt Carlsondd477002008-05-25 23:45:58 -07008184 if (!(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)) {
8185 if (tp->link_config.phy_is_low_power) {
8186 tp->link_config.phy_is_low_power = 0;
8187 tp->link_config.speed = tp->link_config.orig_speed;
8188 tp->link_config.duplex = tp->link_config.orig_duplex;
8189 tp->link_config.autoneg = tp->link_config.orig_autoneg;
8190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008191
Matt Carlsondd477002008-05-25 23:45:58 -07008192 err = tg3_setup_phy(tp, 0);
8193 if (err)
8194 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008195
Matt Carlsondd477002008-05-25 23:45:58 -07008196 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
Matt Carlson7f97a4b2009-08-25 10:10:03 +00008197 !(tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07008198 u32 tmp;
8199
8200 /* Clear CRC stats. */
8201 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
8202 tg3_writephy(tp, MII_TG3_TEST1,
8203 tmp | MII_TG3_TEST1_CRC_EN);
8204 tg3_readphy(tp, 0x14, &tmp);
8205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008206 }
8207 }
8208
8209 __tg3_set_rx_mode(tp->dev);
8210
8211 /* Initialize receive rules. */
8212 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
8213 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
8214 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
8215 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
8216
Michael Chan4cf78e42005-07-25 12:29:19 -07008217 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Michael Chana4e2b342005-10-26 15:46:52 -07008218 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008219 limit = 8;
8220 else
8221 limit = 16;
8222 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF)
8223 limit -= 4;
8224 switch (limit) {
8225 case 16:
8226 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
8227 case 15:
8228 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
8229 case 14:
8230 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
8231 case 13:
8232 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
8233 case 12:
8234 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
8235 case 11:
8236 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
8237 case 10:
8238 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
8239 case 9:
8240 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
8241 case 8:
8242 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
8243 case 7:
8244 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
8245 case 6:
8246 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
8247 case 5:
8248 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
8249 case 4:
8250 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
8251 case 3:
8252 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
8253 case 2:
8254 case 1:
8255
8256 default:
8257 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07008258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008259
Matt Carlson9ce768e2007-10-11 19:49:11 -07008260 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
8261 /* Write our heartbeat update interval to APE. */
8262 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
8263 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07008264
Linus Torvalds1da177e2005-04-16 15:20:36 -07008265 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
8266
Linus Torvalds1da177e2005-04-16 15:20:36 -07008267 return 0;
8268}
8269
8270/* Called at device open time to get the chip ready for
8271 * packet processing. Invoked with tp->lock held.
8272 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008273static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008274{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008275 tg3_switch_clocks(tp);
8276
8277 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
8278
Matt Carlson2f751b62008-08-04 23:17:34 -07008279 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008280}
8281
8282#define TG3_STAT_ADD32(PSTAT, REG) \
8283do { u32 __val = tr32(REG); \
8284 (PSTAT)->low += __val; \
8285 if ((PSTAT)->low < __val) \
8286 (PSTAT)->high += 1; \
8287} while (0)
8288
8289static void tg3_periodic_fetch_stats(struct tg3 *tp)
8290{
8291 struct tg3_hw_stats *sp = tp->hw_stats;
8292
8293 if (!netif_carrier_ok(tp->dev))
8294 return;
8295
8296 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
8297 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
8298 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
8299 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
8300 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
8301 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
8302 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
8303 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
8304 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
8305 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
8306 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
8307 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
8308 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
8309
8310 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
8311 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
8312 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
8313 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
8314 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
8315 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
8316 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
8317 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
8318 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
8319 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
8320 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
8321 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
8322 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
8323 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07008324
8325 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
8326 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
8327 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008328}
8329
8330static void tg3_timer(unsigned long __opaque)
8331{
8332 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008333
Michael Chanf475f162006-03-27 23:20:14 -08008334 if (tp->irq_sync)
8335 goto restart_timer;
8336
David S. Millerf47c11e2005-06-24 20:18:35 -07008337 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008338
David S. Millerfac9b832005-05-18 22:46:34 -07008339 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)) {
8340 /* All of this garbage is because when using non-tagged
8341 * IRQ status the mailbox/status_block protocol the chip
8342 * uses with the cpu is race prone.
8343 */
Matt Carlson898a56f2009-08-28 14:02:40 +00008344 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -07008345 tw32(GRC_LOCAL_CTRL,
8346 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
8347 } else {
8348 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00008349 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -07008350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008351
David S. Millerfac9b832005-05-18 22:46:34 -07008352 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
8353 tp->tg3_flags2 |= TG3_FLG2_RESTART_TIMER;
David S. Millerf47c11e2005-06-24 20:18:35 -07008354 spin_unlock(&tp->lock);
David S. Millerfac9b832005-05-18 22:46:34 -07008355 schedule_work(&tp->reset_task);
8356 return;
8357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008358 }
8359
Linus Torvalds1da177e2005-04-16 15:20:36 -07008360 /* This part only runs once per second. */
8361 if (!--tp->timer_counter) {
David S. Millerfac9b832005-05-18 22:46:34 -07008362 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
8363 tg3_periodic_fetch_stats(tp);
8364
Linus Torvalds1da177e2005-04-16 15:20:36 -07008365 if (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) {
8366 u32 mac_stat;
8367 int phy_event;
8368
8369 mac_stat = tr32(MAC_STATUS);
8370
8371 phy_event = 0;
8372 if (tp->tg3_flags & TG3_FLAG_USE_MI_INTERRUPT) {
8373 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
8374 phy_event = 1;
8375 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
8376 phy_event = 1;
8377
8378 if (phy_event)
8379 tg3_setup_phy(tp, 0);
8380 } else if (tp->tg3_flags & TG3_FLAG_POLL_SERDES) {
8381 u32 mac_stat = tr32(MAC_STATUS);
8382 int need_setup = 0;
8383
8384 if (netif_carrier_ok(tp->dev) &&
8385 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
8386 need_setup = 1;
8387 }
8388 if (! netif_carrier_ok(tp->dev) &&
8389 (mac_stat & (MAC_STATUS_PCS_SYNCED |
8390 MAC_STATUS_SIGNAL_DET))) {
8391 need_setup = 1;
8392 }
8393 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07008394 if (!tp->serdes_counter) {
8395 tw32_f(MAC_MODE,
8396 (tp->mac_mode &
8397 ~MAC_MODE_PORT_MODE_MASK));
8398 udelay(40);
8399 tw32_f(MAC_MODE, tp->mac_mode);
8400 udelay(40);
8401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008402 tg3_setup_phy(tp, 0);
8403 }
Michael Chan747e8f82005-07-25 12:33:22 -07008404 } else if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)
8405 tg3_serdes_parallel_detect(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008406
8407 tp->timer_counter = tp->timer_multiplier;
8408 }
8409
Michael Chan130b8e42006-09-27 16:00:40 -07008410 /* Heartbeat is only sent once every 2 seconds.
8411 *
8412 * The heartbeat is to tell the ASF firmware that the host
8413 * driver is still alive. In the event that the OS crashes,
8414 * ASF needs to reset the hardware to free up the FIFO space
8415 * that may be filled with rx packets destined for the host.
8416 * If the FIFO is full, ASF will no longer function properly.
8417 *
8418 * Unintended resets have been reported on real time kernels
8419 * where the timer doesn't run on time. Netpoll will also have
8420 * same problem.
8421 *
8422 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
8423 * to check the ring condition when the heartbeat is expiring
8424 * before doing the reset. This will prevent most unintended
8425 * resets.
8426 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008427 if (!--tp->asf_counter) {
Matt Carlsonbc7959b2008-08-15 14:08:55 -07008428 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
8429 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07008430 tg3_wait_for_event_ack(tp);
8431
Michael Chanbbadf502006-04-06 21:46:34 -07008432 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07008433 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07008434 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Michael Chan28fbef72005-10-26 15:48:35 -07008435 /* 5 seconds timeout */
Michael Chanbbadf502006-04-06 21:46:34 -07008436 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX, 5);
Matt Carlson4ba526c2008-08-15 14:10:04 -07008437
8438 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008439 }
8440 tp->asf_counter = tp->asf_multiplier;
8441 }
8442
David S. Millerf47c11e2005-06-24 20:18:35 -07008443 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008444
Michael Chanf475f162006-03-27 23:20:14 -08008445restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07008446 tp->timer.expires = jiffies + tp->timer_offset;
8447 add_timer(&tp->timer);
8448}
8449
Matt Carlson4f125f42009-09-01 12:55:02 +00008450static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -08008451{
David Howells7d12e782006-10-05 14:55:46 +01008452 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008453 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +00008454 char *name;
8455 struct tg3_napi *tnapi = &tp->napi[irq_num];
8456
8457 if (tp->irq_cnt == 1)
8458 name = tp->dev->name;
8459 else {
8460 name = &tnapi->irq_lbl[0];
8461 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
8462 name[IFNAMSIZ-1] = 0;
8463 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08008464
Matt Carlson679563f2009-09-01 12:55:46 +00008465 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI_OR_MSIX) {
Michael Chanfcfa0a32006-03-20 22:28:41 -08008466 fn = tg3_msi;
8467 if (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)
8468 fn = tg3_msi_1shot;
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07008469 flags = IRQF_SAMPLE_RANDOM;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008470 } else {
8471 fn = tg3_interrupt;
8472 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)
8473 fn = tg3_interrupt_tagged;
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07008474 flags = IRQF_SHARED | IRQF_SAMPLE_RANDOM;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008475 }
Matt Carlson4f125f42009-09-01 12:55:02 +00008476
8477 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08008478}
8479
Michael Chan79381092005-04-21 17:13:59 -07008480static int tg3_test_interrupt(struct tg3 *tp)
8481{
Matt Carlson09943a12009-08-28 14:01:57 +00008482 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -07008483 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -07008484 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008485 u32 val;
Michael Chan79381092005-04-21 17:13:59 -07008486
Michael Chand4bc3922005-05-29 14:59:20 -07008487 if (!netif_running(dev))
8488 return -ENODEV;
8489
Michael Chan79381092005-04-21 17:13:59 -07008490 tg3_disable_ints(tp);
8491
Matt Carlson4f125f42009-09-01 12:55:02 +00008492 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07008493
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008494 /*
8495 * Turn off MSI one shot mode. Otherwise this test has no
8496 * observable way to know whether the interrupt was delivered.
8497 */
Matt Carlsonb703df62009-12-03 08:36:21 +00008498 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
8499 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008500 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
8501 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
8502 tw32(MSGINT_MODE, val);
8503 }
8504
Matt Carlson4f125f42009-09-01 12:55:02 +00008505 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Matt Carlson09943a12009-08-28 14:01:57 +00008506 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07008507 if (err)
8508 return err;
8509
Matt Carlson898a56f2009-08-28 14:02:40 +00008510 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -07008511 tg3_enable_ints(tp);
8512
8513 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00008514 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -07008515
8516 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -07008517 u32 int_mbox, misc_host_ctrl;
8518
Matt Carlson898a56f2009-08-28 14:02:40 +00008519 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -07008520 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
8521
8522 if ((int_mbox != 0) ||
8523 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
8524 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -07008525 break;
Michael Chanb16250e2006-09-27 16:10:14 -07008526 }
8527
Michael Chan79381092005-04-21 17:13:59 -07008528 msleep(10);
8529 }
8530
8531 tg3_disable_ints(tp);
8532
Matt Carlson4f125f42009-09-01 12:55:02 +00008533 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008534
Matt Carlson4f125f42009-09-01 12:55:02 +00008535 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07008536
8537 if (err)
8538 return err;
8539
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008540 if (intr_ok) {
8541 /* Reenable MSI one shot mode. */
Matt Carlsonb703df62009-12-03 08:36:21 +00008542 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
8543 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008544 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
8545 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
8546 tw32(MSGINT_MODE, val);
8547 }
Michael Chan79381092005-04-21 17:13:59 -07008548 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008549 }
Michael Chan79381092005-04-21 17:13:59 -07008550
8551 return -EIO;
8552}
8553
8554/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
8555 * successfully restored
8556 */
8557static int tg3_test_msi(struct tg3 *tp)
8558{
Michael Chan79381092005-04-21 17:13:59 -07008559 int err;
8560 u16 pci_cmd;
8561
8562 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSI))
8563 return 0;
8564
8565 /* Turn off SERR reporting in case MSI terminates with Master
8566 * Abort.
8567 */
8568 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
8569 pci_write_config_word(tp->pdev, PCI_COMMAND,
8570 pci_cmd & ~PCI_COMMAND_SERR);
8571
8572 err = tg3_test_interrupt(tp);
8573
8574 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
8575
8576 if (!err)
8577 return 0;
8578
8579 /* other failures */
8580 if (err != -EIO)
8581 return err;
8582
8583 /* MSI test failed, go back to INTx mode */
8584 printk(KERN_WARNING PFX "%s: No interrupt was generated using MSI, "
8585 "switching to INTx mode. Please report this failure to "
8586 "the PCI maintainer and include system chipset information.\n",
8587 tp->dev->name);
8588
Matt Carlson4f125f42009-09-01 12:55:02 +00008589 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +00008590
Michael Chan79381092005-04-21 17:13:59 -07008591 pci_disable_msi(tp->pdev);
8592
8593 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI;
8594
Matt Carlson4f125f42009-09-01 12:55:02 +00008595 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07008596 if (err)
8597 return err;
8598
8599 /* Need to reset the chip because the MSI cycle may have terminated
8600 * with Master Abort.
8601 */
David S. Millerf47c11e2005-06-24 20:18:35 -07008602 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07008603
Michael Chan944d9802005-05-29 14:57:48 -07008604 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008605 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07008606
David S. Millerf47c11e2005-06-24 20:18:35 -07008607 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07008608
8609 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +00008610 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -07008611
8612 return err;
8613}
8614
Matt Carlson9e9fd122009-01-19 16:57:45 -08008615static int tg3_request_firmware(struct tg3 *tp)
8616{
8617 const __be32 *fw_data;
8618
8619 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
8620 printk(KERN_ERR "%s: Failed to load firmware \"%s\"\n",
8621 tp->dev->name, tp->fw_needed);
8622 return -ENOENT;
8623 }
8624
8625 fw_data = (void *)tp->fw->data;
8626
8627 /* Firmware blob starts with version numbers, followed by
8628 * start address and _full_ length including BSS sections
8629 * (which must be longer than the actual data, of course
8630 */
8631
8632 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
8633 if (tp->fw_len < (tp->fw->size - 12)) {
8634 printk(KERN_ERR "%s: bogus length %d in \"%s\"\n",
8635 tp->dev->name, tp->fw_len, tp->fw_needed);
8636 release_firmware(tp->fw);
8637 tp->fw = NULL;
8638 return -EINVAL;
8639 }
8640
8641 /* We no longer need firmware; we have it. */
8642 tp->fw_needed = NULL;
8643 return 0;
8644}
8645
Matt Carlson679563f2009-09-01 12:55:46 +00008646static bool tg3_enable_msix(struct tg3 *tp)
8647{
8648 int i, rc, cpus = num_online_cpus();
8649 struct msix_entry msix_ent[tp->irq_max];
8650
8651 if (cpus == 1)
8652 /* Just fallback to the simpler MSI mode. */
8653 return false;
8654
8655 /*
8656 * We want as many rx rings enabled as there are cpus.
8657 * The first MSIX vector only deals with link interrupts, etc,
8658 * so we add one to the number of vectors we are requesting.
8659 */
8660 tp->irq_cnt = min_t(unsigned, cpus + 1, tp->irq_max);
8661
8662 for (i = 0; i < tp->irq_max; i++) {
8663 msix_ent[i].entry = i;
8664 msix_ent[i].vector = 0;
8665 }
8666
8667 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
8668 if (rc != 0) {
8669 if (rc < TG3_RSS_MIN_NUM_MSIX_VECS)
8670 return false;
8671 if (pci_enable_msix(tp->pdev, msix_ent, rc))
8672 return false;
8673 printk(KERN_NOTICE
8674 "%s: Requested %d MSI-X vectors, received %d\n",
8675 tp->dev->name, tp->irq_cnt, rc);
8676 tp->irq_cnt = rc;
8677 }
8678
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008679 tp->tg3_flags3 |= TG3_FLG3_ENABLE_RSS;
8680
Matt Carlson679563f2009-09-01 12:55:46 +00008681 for (i = 0; i < tp->irq_max; i++)
8682 tp->napi[i].irq_vec = msix_ent[i].vector;
8683
Matt Carlson19cfaec2009-12-03 08:36:20 +00008684 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
8685 tp->tg3_flags3 |= TG3_FLG3_ENABLE_TSS;
8686 tp->dev->real_num_tx_queues = tp->irq_cnt - 1;
8687 } else
8688 tp->dev->real_num_tx_queues = 1;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00008689
Matt Carlson679563f2009-09-01 12:55:46 +00008690 return true;
8691}
8692
Matt Carlson07b01732009-08-28 14:01:15 +00008693static void tg3_ints_init(struct tg3 *tp)
8694{
Matt Carlson679563f2009-09-01 12:55:46 +00008695 if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSI_OR_MSIX) &&
8696 !(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +00008697 /* All MSI supporting chips should support tagged
8698 * status. Assert that this is the case.
8699 */
Matt Carlson679563f2009-09-01 12:55:46 +00008700 printk(KERN_WARNING PFX "%s: MSI without TAGGED? "
8701 "Not using MSI.\n", tp->dev->name);
8702 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +00008703 }
Matt Carlson4f125f42009-09-01 12:55:02 +00008704
Matt Carlson679563f2009-09-01 12:55:46 +00008705 if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX) && tg3_enable_msix(tp))
8706 tp->tg3_flags2 |= TG3_FLG2_USING_MSIX;
8707 else if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSI) &&
8708 pci_enable_msi(tp->pdev) == 0)
8709 tp->tg3_flags2 |= TG3_FLG2_USING_MSI;
8710
8711 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI_OR_MSIX) {
8712 u32 msi_mode = tr32(MSGINT_MODE);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008713 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
8714 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson679563f2009-09-01 12:55:46 +00008715 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
8716 }
8717defcfg:
8718 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSIX)) {
8719 tp->irq_cnt = 1;
8720 tp->napi[0].irq_vec = tp->pdev->irq;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00008721 tp->dev->real_num_tx_queues = 1;
Matt Carlson679563f2009-09-01 12:55:46 +00008722 }
Matt Carlson07b01732009-08-28 14:01:15 +00008723}
8724
8725static void tg3_ints_fini(struct tg3 *tp)
8726{
Matt Carlson679563f2009-09-01 12:55:46 +00008727 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
8728 pci_disable_msix(tp->pdev);
8729 else if (tp->tg3_flags2 & TG3_FLG2_USING_MSI)
8730 pci_disable_msi(tp->pdev);
8731 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI_OR_MSIX;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008732 tp->tg3_flags3 &= ~TG3_FLG3_ENABLE_RSS;
Matt Carlson07b01732009-08-28 14:01:15 +00008733}
8734
Linus Torvalds1da177e2005-04-16 15:20:36 -07008735static int tg3_open(struct net_device *dev)
8736{
8737 struct tg3 *tp = netdev_priv(dev);
Matt Carlson4f125f42009-09-01 12:55:02 +00008738 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008739
Matt Carlson9e9fd122009-01-19 16:57:45 -08008740 if (tp->fw_needed) {
8741 err = tg3_request_firmware(tp);
8742 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
8743 if (err)
8744 return err;
8745 } else if (err) {
8746 printk(KERN_WARNING "%s: TSO capability disabled.\n",
8747 tp->dev->name);
8748 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
8749 } else if (!(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
8750 printk(KERN_NOTICE "%s: TSO capability restored.\n",
8751 tp->dev->name);
8752 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
8753 }
8754 }
8755
Michael Chanc49a1562006-12-17 17:07:29 -08008756 netif_carrier_off(tp->dev);
8757
Michael Chanbc1c7562006-03-20 17:48:03 -08008758 err = tg3_set_power_state(tp, PCI_D0);
Matt Carlson2f751b62008-08-04 23:17:34 -07008759 if (err)
Michael Chanbc1c7562006-03-20 17:48:03 -08008760 return err;
Matt Carlson2f751b62008-08-04 23:17:34 -07008761
8762 tg3_full_lock(tp, 0);
Michael Chanbc1c7562006-03-20 17:48:03 -08008763
Linus Torvalds1da177e2005-04-16 15:20:36 -07008764 tg3_disable_ints(tp);
8765 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
8766
David S. Millerf47c11e2005-06-24 20:18:35 -07008767 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008768
Matt Carlson679563f2009-09-01 12:55:46 +00008769 /*
8770 * Setup interrupts first so we know how
8771 * many NAPI resources to allocate
8772 */
8773 tg3_ints_init(tp);
8774
Linus Torvalds1da177e2005-04-16 15:20:36 -07008775 /* The placement of this call is tied
8776 * to the setup and use of Host TX descriptors.
8777 */
8778 err = tg3_alloc_consistent(tp);
8779 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00008780 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008781
Matt Carlsonfed97812009-09-01 13:10:19 +00008782 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07008783
Matt Carlson4f125f42009-09-01 12:55:02 +00008784 for (i = 0; i < tp->irq_cnt; i++) {
8785 struct tg3_napi *tnapi = &tp->napi[i];
8786 err = tg3_request_irq(tp, i);
8787 if (err) {
8788 for (i--; i >= 0; i--)
8789 free_irq(tnapi->irq_vec, tnapi);
8790 break;
8791 }
8792 }
Matt Carlson07b01732009-08-28 14:01:15 +00008793
8794 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00008795 goto err_out2;
Matt Carlson07b01732009-08-28 14:01:15 +00008796
David S. Millerf47c11e2005-06-24 20:18:35 -07008797 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008798
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008799 err = tg3_init_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008800 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -07008801 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008802 tg3_free_rings(tp);
8803 } else {
David S. Millerfac9b832005-05-18 22:46:34 -07008804 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)
8805 tp->timer_offset = HZ;
8806 else
8807 tp->timer_offset = HZ / 10;
8808
8809 BUG_ON(tp->timer_offset > HZ);
8810 tp->timer_counter = tp->timer_multiplier =
8811 (HZ / tp->timer_offset);
8812 tp->asf_counter = tp->asf_multiplier =
Michael Chan28fbef72005-10-26 15:48:35 -07008813 ((HZ / tp->timer_offset) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008814
8815 init_timer(&tp->timer);
8816 tp->timer.expires = jiffies + tp->timer_offset;
8817 tp->timer.data = (unsigned long) tp;
8818 tp->timer.function = tg3_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008819 }
8820
David S. Millerf47c11e2005-06-24 20:18:35 -07008821 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008822
Matt Carlson07b01732009-08-28 14:01:15 +00008823 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00008824 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008825
Michael Chan79381092005-04-21 17:13:59 -07008826 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
8827 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -07008828
Michael Chan79381092005-04-21 17:13:59 -07008829 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -07008830 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -07008831 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -07008832 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07008833 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07008834
Matt Carlson679563f2009-09-01 12:55:46 +00008835 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -07008836 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08008837
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008838 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
Matt Carlsonb703df62009-12-03 08:36:21 +00008839 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57765 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008840 (tp->tg3_flags2 & TG3_FLG2_USING_MSI) &&
8841 (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)) {
8842 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -08008843
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008844 tw32(PCIE_TRANSACTION_CFG,
8845 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -08008846 }
Michael Chan79381092005-04-21 17:13:59 -07008847 }
8848
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07008849 tg3_phy_start(tp);
8850
David S. Millerf47c11e2005-06-24 20:18:35 -07008851 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008852
Michael Chan79381092005-04-21 17:13:59 -07008853 add_timer(&tp->timer);
8854 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008855 tg3_enable_ints(tp);
8856
David S. Millerf47c11e2005-06-24 20:18:35 -07008857 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008858
Matt Carlsonfe5f5782009-09-01 13:09:39 +00008859 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008860
8861 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +00008862
Matt Carlson679563f2009-09-01 12:55:46 +00008863err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +00008864 for (i = tp->irq_cnt - 1; i >= 0; i--) {
8865 struct tg3_napi *tnapi = &tp->napi[i];
8866 free_irq(tnapi->irq_vec, tnapi);
8867 }
Matt Carlson07b01732009-08-28 14:01:15 +00008868
Matt Carlson679563f2009-09-01 12:55:46 +00008869err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +00008870 tg3_napi_disable(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00008871 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +00008872
8873err_out1:
8874 tg3_ints_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00008875 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008876}
8877
8878#if 0
8879/*static*/ void tg3_dump_state(struct tg3 *tp)
8880{
8881 u32 val32, val32_2, val32_3, val32_4, val32_5;
8882 u16 val16;
8883 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00008884 struct tg3_hw_status *sblk = tp->napi[0]->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008885
8886 pci_read_config_word(tp->pdev, PCI_STATUS, &val16);
8887 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE, &val32);
8888 printk("DEBUG: PCI status [%04x] TG3PCI state[%08x]\n",
8889 val16, val32);
8890
8891 /* MAC block */
8892 printk("DEBUG: MAC_MODE[%08x] MAC_STATUS[%08x]\n",
8893 tr32(MAC_MODE), tr32(MAC_STATUS));
8894 printk(" MAC_EVENT[%08x] MAC_LED_CTRL[%08x]\n",
8895 tr32(MAC_EVENT), tr32(MAC_LED_CTRL));
8896 printk("DEBUG: MAC_TX_MODE[%08x] MAC_TX_STATUS[%08x]\n",
8897 tr32(MAC_TX_MODE), tr32(MAC_TX_STATUS));
8898 printk(" MAC_RX_MODE[%08x] MAC_RX_STATUS[%08x]\n",
8899 tr32(MAC_RX_MODE), tr32(MAC_RX_STATUS));
8900
8901 /* Send data initiator control block */
8902 printk("DEBUG: SNDDATAI_MODE[%08x] SNDDATAI_STATUS[%08x]\n",
8903 tr32(SNDDATAI_MODE), tr32(SNDDATAI_STATUS));
8904 printk(" SNDDATAI_STATSCTRL[%08x]\n",
8905 tr32(SNDDATAI_STATSCTRL));
8906
8907 /* Send data completion control block */
8908 printk("DEBUG: SNDDATAC_MODE[%08x]\n", tr32(SNDDATAC_MODE));
8909
8910 /* Send BD ring selector block */
8911 printk("DEBUG: SNDBDS_MODE[%08x] SNDBDS_STATUS[%08x]\n",
8912 tr32(SNDBDS_MODE), tr32(SNDBDS_STATUS));
8913
8914 /* Send BD initiator control block */
8915 printk("DEBUG: SNDBDI_MODE[%08x] SNDBDI_STATUS[%08x]\n",
8916 tr32(SNDBDI_MODE), tr32(SNDBDI_STATUS));
8917
8918 /* Send BD completion control block */
8919 printk("DEBUG: SNDBDC_MODE[%08x]\n", tr32(SNDBDC_MODE));
8920
8921 /* Receive list placement control block */
8922 printk("DEBUG: RCVLPC_MODE[%08x] RCVLPC_STATUS[%08x]\n",
8923 tr32(RCVLPC_MODE), tr32(RCVLPC_STATUS));
8924 printk(" RCVLPC_STATSCTRL[%08x]\n",
8925 tr32(RCVLPC_STATSCTRL));
8926
8927 /* Receive data and receive BD initiator control block */
8928 printk("DEBUG: RCVDBDI_MODE[%08x] RCVDBDI_STATUS[%08x]\n",
8929 tr32(RCVDBDI_MODE), tr32(RCVDBDI_STATUS));
8930
8931 /* Receive data completion control block */
8932 printk("DEBUG: RCVDCC_MODE[%08x]\n",
8933 tr32(RCVDCC_MODE));
8934
8935 /* Receive BD initiator control block */
8936 printk("DEBUG: RCVBDI_MODE[%08x] RCVBDI_STATUS[%08x]\n",
8937 tr32(RCVBDI_MODE), tr32(RCVBDI_STATUS));
8938
8939 /* Receive BD completion control block */
8940 printk("DEBUG: RCVCC_MODE[%08x] RCVCC_STATUS[%08x]\n",
8941 tr32(RCVCC_MODE), tr32(RCVCC_STATUS));
8942
8943 /* Receive list selector control block */
8944 printk("DEBUG: RCVLSC_MODE[%08x] RCVLSC_STATUS[%08x]\n",
8945 tr32(RCVLSC_MODE), tr32(RCVLSC_STATUS));
8946
8947 /* Mbuf cluster free block */
8948 printk("DEBUG: MBFREE_MODE[%08x] MBFREE_STATUS[%08x]\n",
8949 tr32(MBFREE_MODE), tr32(MBFREE_STATUS));
8950
8951 /* Host coalescing control block */
8952 printk("DEBUG: HOSTCC_MODE[%08x] HOSTCC_STATUS[%08x]\n",
8953 tr32(HOSTCC_MODE), tr32(HOSTCC_STATUS));
8954 printk("DEBUG: HOSTCC_STATS_BLK_HOST_ADDR[%08x%08x]\n",
8955 tr32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH),
8956 tr32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW));
8957 printk("DEBUG: HOSTCC_STATUS_BLK_HOST_ADDR[%08x%08x]\n",
8958 tr32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH),
8959 tr32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW));
8960 printk("DEBUG: HOSTCC_STATS_BLK_NIC_ADDR[%08x]\n",
8961 tr32(HOSTCC_STATS_BLK_NIC_ADDR));
8962 printk("DEBUG: HOSTCC_STATUS_BLK_NIC_ADDR[%08x]\n",
8963 tr32(HOSTCC_STATUS_BLK_NIC_ADDR));
8964
8965 /* Memory arbiter control block */
8966 printk("DEBUG: MEMARB_MODE[%08x] MEMARB_STATUS[%08x]\n",
8967 tr32(MEMARB_MODE), tr32(MEMARB_STATUS));
8968
8969 /* Buffer manager control block */
8970 printk("DEBUG: BUFMGR_MODE[%08x] BUFMGR_STATUS[%08x]\n",
8971 tr32(BUFMGR_MODE), tr32(BUFMGR_STATUS));
8972 printk("DEBUG: BUFMGR_MB_POOL_ADDR[%08x] BUFMGR_MB_POOL_SIZE[%08x]\n",
8973 tr32(BUFMGR_MB_POOL_ADDR), tr32(BUFMGR_MB_POOL_SIZE));
8974 printk("DEBUG: BUFMGR_DMA_DESC_POOL_ADDR[%08x] "
8975 "BUFMGR_DMA_DESC_POOL_SIZE[%08x]\n",
8976 tr32(BUFMGR_DMA_DESC_POOL_ADDR),
8977 tr32(BUFMGR_DMA_DESC_POOL_SIZE));
8978
8979 /* Read DMA control block */
8980 printk("DEBUG: RDMAC_MODE[%08x] RDMAC_STATUS[%08x]\n",
8981 tr32(RDMAC_MODE), tr32(RDMAC_STATUS));
8982
8983 /* Write DMA control block */
8984 printk("DEBUG: WDMAC_MODE[%08x] WDMAC_STATUS[%08x]\n",
8985 tr32(WDMAC_MODE), tr32(WDMAC_STATUS));
8986
8987 /* DMA completion block */
8988 printk("DEBUG: DMAC_MODE[%08x]\n",
8989 tr32(DMAC_MODE));
8990
8991 /* GRC block */
8992 printk("DEBUG: GRC_MODE[%08x] GRC_MISC_CFG[%08x]\n",
8993 tr32(GRC_MODE), tr32(GRC_MISC_CFG));
8994 printk("DEBUG: GRC_LOCAL_CTRL[%08x]\n",
8995 tr32(GRC_LOCAL_CTRL));
8996
8997 /* TG3_BDINFOs */
8998 printk("DEBUG: RCVDBDI_JUMBO_BD[%08x%08x:%08x:%08x]\n",
8999 tr32(RCVDBDI_JUMBO_BD + 0x0),
9000 tr32(RCVDBDI_JUMBO_BD + 0x4),
9001 tr32(RCVDBDI_JUMBO_BD + 0x8),
9002 tr32(RCVDBDI_JUMBO_BD + 0xc));
9003 printk("DEBUG: RCVDBDI_STD_BD[%08x%08x:%08x:%08x]\n",
9004 tr32(RCVDBDI_STD_BD + 0x0),
9005 tr32(RCVDBDI_STD_BD + 0x4),
9006 tr32(RCVDBDI_STD_BD + 0x8),
9007 tr32(RCVDBDI_STD_BD + 0xc));
9008 printk("DEBUG: RCVDBDI_MINI_BD[%08x%08x:%08x:%08x]\n",
9009 tr32(RCVDBDI_MINI_BD + 0x0),
9010 tr32(RCVDBDI_MINI_BD + 0x4),
9011 tr32(RCVDBDI_MINI_BD + 0x8),
9012 tr32(RCVDBDI_MINI_BD + 0xc));
9013
9014 tg3_read_mem(tp, NIC_SRAM_SEND_RCB + 0x0, &val32);
9015 tg3_read_mem(tp, NIC_SRAM_SEND_RCB + 0x4, &val32_2);
9016 tg3_read_mem(tp, NIC_SRAM_SEND_RCB + 0x8, &val32_3);
9017 tg3_read_mem(tp, NIC_SRAM_SEND_RCB + 0xc, &val32_4);
9018 printk("DEBUG: SRAM_SEND_RCB_0[%08x%08x:%08x:%08x]\n",
9019 val32, val32_2, val32_3, val32_4);
9020
9021 tg3_read_mem(tp, NIC_SRAM_RCV_RET_RCB + 0x0, &val32);
9022 tg3_read_mem(tp, NIC_SRAM_RCV_RET_RCB + 0x4, &val32_2);
9023 tg3_read_mem(tp, NIC_SRAM_RCV_RET_RCB + 0x8, &val32_3);
9024 tg3_read_mem(tp, NIC_SRAM_RCV_RET_RCB + 0xc, &val32_4);
9025 printk("DEBUG: SRAM_RCV_RET_RCB_0[%08x%08x:%08x:%08x]\n",
9026 val32, val32_2, val32_3, val32_4);
9027
9028 tg3_read_mem(tp, NIC_SRAM_STATUS_BLK + 0x0, &val32);
9029 tg3_read_mem(tp, NIC_SRAM_STATUS_BLK + 0x4, &val32_2);
9030 tg3_read_mem(tp, NIC_SRAM_STATUS_BLK + 0x8, &val32_3);
9031 tg3_read_mem(tp, NIC_SRAM_STATUS_BLK + 0xc, &val32_4);
9032 tg3_read_mem(tp, NIC_SRAM_STATUS_BLK + 0x10, &val32_5);
9033 printk("DEBUG: SRAM_STATUS_BLK[%08x:%08x:%08x:%08x:%08x]\n",
9034 val32, val32_2, val32_3, val32_4, val32_5);
9035
9036 /* SW status block */
Matt Carlson898a56f2009-08-28 14:02:40 +00009037 printk(KERN_DEBUG
9038 "Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
9039 sblk->status,
9040 sblk->status_tag,
9041 sblk->rx_jumbo_consumer,
9042 sblk->rx_consumer,
9043 sblk->rx_mini_consumer,
9044 sblk->idx[0].rx_producer,
9045 sblk->idx[0].tx_consumer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009046
9047 /* SW statistics block */
9048 printk("DEBUG: Host statistics block [%08x:%08x:%08x:%08x]\n",
9049 ((u32 *)tp->hw_stats)[0],
9050 ((u32 *)tp->hw_stats)[1],
9051 ((u32 *)tp->hw_stats)[2],
9052 ((u32 *)tp->hw_stats)[3]);
9053
9054 /* Mailboxes */
9055 printk("DEBUG: SNDHOST_PROD[%08x%08x] SNDNIC_PROD[%08x%08x]\n",
Michael Chan09ee9292005-08-09 20:17:00 -07009056 tr32_mailbox(MAILBOX_SNDHOST_PROD_IDX_0 + 0x0),
9057 tr32_mailbox(MAILBOX_SNDHOST_PROD_IDX_0 + 0x4),
9058 tr32_mailbox(MAILBOX_SNDNIC_PROD_IDX_0 + 0x0),
9059 tr32_mailbox(MAILBOX_SNDNIC_PROD_IDX_0 + 0x4));
Linus Torvalds1da177e2005-04-16 15:20:36 -07009060
9061 /* NIC side send descriptors. */
9062 for (i = 0; i < 6; i++) {
9063 unsigned long txd;
9064
9065 txd = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_TX_BUFFER_DESC
9066 + (i * sizeof(struct tg3_tx_buffer_desc));
9067 printk("DEBUG: NIC TXD(%d)[%08x:%08x:%08x:%08x]\n",
9068 i,
9069 readl(txd + 0x0), readl(txd + 0x4),
9070 readl(txd + 0x8), readl(txd + 0xc));
9071 }
9072
9073 /* NIC side RX descriptors. */
9074 for (i = 0; i < 6; i++) {
9075 unsigned long rxd;
9076
9077 rxd = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_RX_BUFFER_DESC
9078 + (i * sizeof(struct tg3_rx_buffer_desc));
9079 printk("DEBUG: NIC RXD_STD(%d)[0][%08x:%08x:%08x:%08x]\n",
9080 i,
9081 readl(rxd + 0x0), readl(rxd + 0x4),
9082 readl(rxd + 0x8), readl(rxd + 0xc));
9083 rxd += (4 * sizeof(u32));
9084 printk("DEBUG: NIC RXD_STD(%d)[1][%08x:%08x:%08x:%08x]\n",
9085 i,
9086 readl(rxd + 0x0), readl(rxd + 0x4),
9087 readl(rxd + 0x8), readl(rxd + 0xc));
9088 }
9089
9090 for (i = 0; i < 6; i++) {
9091 unsigned long rxd;
9092
9093 rxd = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_RX_JUMBO_BUFFER_DESC
9094 + (i * sizeof(struct tg3_rx_buffer_desc));
9095 printk("DEBUG: NIC RXD_JUMBO(%d)[0][%08x:%08x:%08x:%08x]\n",
9096 i,
9097 readl(rxd + 0x0), readl(rxd + 0x4),
9098 readl(rxd + 0x8), readl(rxd + 0xc));
9099 rxd += (4 * sizeof(u32));
9100 printk("DEBUG: NIC RXD_JUMBO(%d)[1][%08x:%08x:%08x:%08x]\n",
9101 i,
9102 readl(rxd + 0x0), readl(rxd + 0x4),
9103 readl(rxd + 0x8), readl(rxd + 0xc));
9104 }
9105}
9106#endif
9107
9108static struct net_device_stats *tg3_get_stats(struct net_device *);
9109static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *);
9110
9111static int tg3_close(struct net_device *dev)
9112{
Matt Carlson4f125f42009-09-01 12:55:02 +00009113 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009114 struct tg3 *tp = netdev_priv(dev);
9115
Matt Carlsonfed97812009-09-01 13:10:19 +00009116 tg3_napi_disable(tp);
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07009117 cancel_work_sync(&tp->reset_task);
Michael Chan7faa0062006-02-02 17:29:28 -08009118
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009119 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009120
9121 del_timer_sync(&tp->timer);
9122
Matt Carlson24bb4fb2009-10-05 17:55:29 +00009123 tg3_phy_stop(tp);
9124
David S. Millerf47c11e2005-06-24 20:18:35 -07009125 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009126#if 0
9127 tg3_dump_state(tp);
9128#endif
9129
9130 tg3_disable_ints(tp);
9131
Michael Chan944d9802005-05-29 14:57:48 -07009132 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009133 tg3_free_rings(tp);
Michael Chan5cf64b8a2007-05-05 12:11:21 -07009134 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009135
David S. Millerf47c11e2005-06-24 20:18:35 -07009136 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009137
Matt Carlson4f125f42009-09-01 12:55:02 +00009138 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9139 struct tg3_napi *tnapi = &tp->napi[i];
9140 free_irq(tnapi->irq_vec, tnapi);
9141 }
Matt Carlson07b01732009-08-28 14:01:15 +00009142
9143 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009144
9145 memcpy(&tp->net_stats_prev, tg3_get_stats(tp->dev),
9146 sizeof(tp->net_stats_prev));
9147 memcpy(&tp->estats_prev, tg3_get_estats(tp),
9148 sizeof(tp->estats_prev));
9149
9150 tg3_free_consistent(tp);
9151
Michael Chanbc1c7562006-03-20 17:48:03 -08009152 tg3_set_power_state(tp, PCI_D3hot);
9153
9154 netif_carrier_off(tp->dev);
9155
Linus Torvalds1da177e2005-04-16 15:20:36 -07009156 return 0;
9157}
9158
9159static inline unsigned long get_stat64(tg3_stat64_t *val)
9160{
9161 unsigned long ret;
9162
9163#if (BITS_PER_LONG == 32)
9164 ret = val->low;
9165#else
9166 ret = ((u64)val->high << 32) | ((u64)val->low);
9167#endif
9168 return ret;
9169}
9170
Stefan Buehler816f8b82008-08-15 14:10:54 -07009171static inline u64 get_estat64(tg3_stat64_t *val)
9172{
9173 return ((u64)val->high << 32) | ((u64)val->low);
9174}
9175
Linus Torvalds1da177e2005-04-16 15:20:36 -07009176static unsigned long calc_crc_errors(struct tg3 *tp)
9177{
9178 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9179
9180 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
9181 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
9182 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009183 u32 val;
9184
David S. Millerf47c11e2005-06-24 20:18:35 -07009185 spin_lock_bh(&tp->lock);
Michael Chan569a5df2007-02-13 12:18:15 -08009186 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
9187 tg3_writephy(tp, MII_TG3_TEST1,
9188 val | MII_TG3_TEST1_CRC_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009189 tg3_readphy(tp, 0x14, &val);
9190 } else
9191 val = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07009192 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009193
9194 tp->phy_crc_errors += val;
9195
9196 return tp->phy_crc_errors;
9197 }
9198
9199 return get_stat64(&hw_stats->rx_fcs_errors);
9200}
9201
9202#define ESTAT_ADD(member) \
9203 estats->member = old_estats->member + \
Stefan Buehler816f8b82008-08-15 14:10:54 -07009204 get_estat64(&hw_stats->member)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009205
9206static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *tp)
9207{
9208 struct tg3_ethtool_stats *estats = &tp->estats;
9209 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
9210 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9211
9212 if (!hw_stats)
9213 return old_estats;
9214
9215 ESTAT_ADD(rx_octets);
9216 ESTAT_ADD(rx_fragments);
9217 ESTAT_ADD(rx_ucast_packets);
9218 ESTAT_ADD(rx_mcast_packets);
9219 ESTAT_ADD(rx_bcast_packets);
9220 ESTAT_ADD(rx_fcs_errors);
9221 ESTAT_ADD(rx_align_errors);
9222 ESTAT_ADD(rx_xon_pause_rcvd);
9223 ESTAT_ADD(rx_xoff_pause_rcvd);
9224 ESTAT_ADD(rx_mac_ctrl_rcvd);
9225 ESTAT_ADD(rx_xoff_entered);
9226 ESTAT_ADD(rx_frame_too_long_errors);
9227 ESTAT_ADD(rx_jabbers);
9228 ESTAT_ADD(rx_undersize_packets);
9229 ESTAT_ADD(rx_in_length_errors);
9230 ESTAT_ADD(rx_out_length_errors);
9231 ESTAT_ADD(rx_64_or_less_octet_packets);
9232 ESTAT_ADD(rx_65_to_127_octet_packets);
9233 ESTAT_ADD(rx_128_to_255_octet_packets);
9234 ESTAT_ADD(rx_256_to_511_octet_packets);
9235 ESTAT_ADD(rx_512_to_1023_octet_packets);
9236 ESTAT_ADD(rx_1024_to_1522_octet_packets);
9237 ESTAT_ADD(rx_1523_to_2047_octet_packets);
9238 ESTAT_ADD(rx_2048_to_4095_octet_packets);
9239 ESTAT_ADD(rx_4096_to_8191_octet_packets);
9240 ESTAT_ADD(rx_8192_to_9022_octet_packets);
9241
9242 ESTAT_ADD(tx_octets);
9243 ESTAT_ADD(tx_collisions);
9244 ESTAT_ADD(tx_xon_sent);
9245 ESTAT_ADD(tx_xoff_sent);
9246 ESTAT_ADD(tx_flow_control);
9247 ESTAT_ADD(tx_mac_errors);
9248 ESTAT_ADD(tx_single_collisions);
9249 ESTAT_ADD(tx_mult_collisions);
9250 ESTAT_ADD(tx_deferred);
9251 ESTAT_ADD(tx_excessive_collisions);
9252 ESTAT_ADD(tx_late_collisions);
9253 ESTAT_ADD(tx_collide_2times);
9254 ESTAT_ADD(tx_collide_3times);
9255 ESTAT_ADD(tx_collide_4times);
9256 ESTAT_ADD(tx_collide_5times);
9257 ESTAT_ADD(tx_collide_6times);
9258 ESTAT_ADD(tx_collide_7times);
9259 ESTAT_ADD(tx_collide_8times);
9260 ESTAT_ADD(tx_collide_9times);
9261 ESTAT_ADD(tx_collide_10times);
9262 ESTAT_ADD(tx_collide_11times);
9263 ESTAT_ADD(tx_collide_12times);
9264 ESTAT_ADD(tx_collide_13times);
9265 ESTAT_ADD(tx_collide_14times);
9266 ESTAT_ADD(tx_collide_15times);
9267 ESTAT_ADD(tx_ucast_packets);
9268 ESTAT_ADD(tx_mcast_packets);
9269 ESTAT_ADD(tx_bcast_packets);
9270 ESTAT_ADD(tx_carrier_sense_errors);
9271 ESTAT_ADD(tx_discards);
9272 ESTAT_ADD(tx_errors);
9273
9274 ESTAT_ADD(dma_writeq_full);
9275 ESTAT_ADD(dma_write_prioq_full);
9276 ESTAT_ADD(rxbds_empty);
9277 ESTAT_ADD(rx_discards);
9278 ESTAT_ADD(rx_errors);
9279 ESTAT_ADD(rx_threshold_hit);
9280
9281 ESTAT_ADD(dma_readq_full);
9282 ESTAT_ADD(dma_read_prioq_full);
9283 ESTAT_ADD(tx_comp_queue_full);
9284
9285 ESTAT_ADD(ring_set_send_prod_index);
9286 ESTAT_ADD(ring_status_update);
9287 ESTAT_ADD(nic_irqs);
9288 ESTAT_ADD(nic_avoided_irqs);
9289 ESTAT_ADD(nic_tx_threshold_hit);
9290
9291 return estats;
9292}
9293
9294static struct net_device_stats *tg3_get_stats(struct net_device *dev)
9295{
9296 struct tg3 *tp = netdev_priv(dev);
9297 struct net_device_stats *stats = &tp->net_stats;
9298 struct net_device_stats *old_stats = &tp->net_stats_prev;
9299 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9300
9301 if (!hw_stats)
9302 return old_stats;
9303
9304 stats->rx_packets = old_stats->rx_packets +
9305 get_stat64(&hw_stats->rx_ucast_packets) +
9306 get_stat64(&hw_stats->rx_mcast_packets) +
9307 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009308
Linus Torvalds1da177e2005-04-16 15:20:36 -07009309 stats->tx_packets = old_stats->tx_packets +
9310 get_stat64(&hw_stats->tx_ucast_packets) +
9311 get_stat64(&hw_stats->tx_mcast_packets) +
9312 get_stat64(&hw_stats->tx_bcast_packets);
9313
9314 stats->rx_bytes = old_stats->rx_bytes +
9315 get_stat64(&hw_stats->rx_octets);
9316 stats->tx_bytes = old_stats->tx_bytes +
9317 get_stat64(&hw_stats->tx_octets);
9318
9319 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -07009320 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009321 stats->tx_errors = old_stats->tx_errors +
9322 get_stat64(&hw_stats->tx_errors) +
9323 get_stat64(&hw_stats->tx_mac_errors) +
9324 get_stat64(&hw_stats->tx_carrier_sense_errors) +
9325 get_stat64(&hw_stats->tx_discards);
9326
9327 stats->multicast = old_stats->multicast +
9328 get_stat64(&hw_stats->rx_mcast_packets);
9329 stats->collisions = old_stats->collisions +
9330 get_stat64(&hw_stats->tx_collisions);
9331
9332 stats->rx_length_errors = old_stats->rx_length_errors +
9333 get_stat64(&hw_stats->rx_frame_too_long_errors) +
9334 get_stat64(&hw_stats->rx_undersize_packets);
9335
9336 stats->rx_over_errors = old_stats->rx_over_errors +
9337 get_stat64(&hw_stats->rxbds_empty);
9338 stats->rx_frame_errors = old_stats->rx_frame_errors +
9339 get_stat64(&hw_stats->rx_align_errors);
9340 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
9341 get_stat64(&hw_stats->tx_discards);
9342 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
9343 get_stat64(&hw_stats->tx_carrier_sense_errors);
9344
9345 stats->rx_crc_errors = old_stats->rx_crc_errors +
9346 calc_crc_errors(tp);
9347
John W. Linville4f63b872005-09-12 14:43:18 -07009348 stats->rx_missed_errors = old_stats->rx_missed_errors +
9349 get_stat64(&hw_stats->rx_discards);
9350
Linus Torvalds1da177e2005-04-16 15:20:36 -07009351 return stats;
9352}
9353
9354static inline u32 calc_crc(unsigned char *buf, int len)
9355{
9356 u32 reg;
9357 u32 tmp;
9358 int j, k;
9359
9360 reg = 0xffffffff;
9361
9362 for (j = 0; j < len; j++) {
9363 reg ^= buf[j];
9364
9365 for (k = 0; k < 8; k++) {
9366 tmp = reg & 0x01;
9367
9368 reg >>= 1;
9369
9370 if (tmp) {
9371 reg ^= 0xedb88320;
9372 }
9373 }
9374 }
9375
9376 return ~reg;
9377}
9378
9379static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
9380{
9381 /* accept or reject all multicast frames */
9382 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
9383 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
9384 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
9385 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
9386}
9387
9388static void __tg3_set_rx_mode(struct net_device *dev)
9389{
9390 struct tg3 *tp = netdev_priv(dev);
9391 u32 rx_mode;
9392
9393 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
9394 RX_MODE_KEEP_VLAN_TAG);
9395
9396 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
9397 * flag clear.
9398 */
9399#if TG3_VLAN_TAG_USED
9400 if (!tp->vlgrp &&
9401 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
9402 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9403#else
9404 /* By definition, VLAN is disabled always in this
9405 * case.
9406 */
9407 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
9408 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9409#endif
9410
9411 if (dev->flags & IFF_PROMISC) {
9412 /* Promiscuous mode. */
9413 rx_mode |= RX_MODE_PROMISC;
9414 } else if (dev->flags & IFF_ALLMULTI) {
9415 /* Accept all multicast. */
9416 tg3_set_multi (tp, 1);
9417 } else if (dev->mc_count < 1) {
9418 /* Reject all multicast. */
9419 tg3_set_multi (tp, 0);
9420 } else {
9421 /* Accept one or more multicast(s). */
9422 struct dev_mc_list *mclist;
9423 unsigned int i;
9424 u32 mc_filter[4] = { 0, };
9425 u32 regidx;
9426 u32 bit;
9427 u32 crc;
9428
9429 for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
9430 i++, mclist = mclist->next) {
9431
9432 crc = calc_crc (mclist->dmi_addr, ETH_ALEN);
9433 bit = ~crc & 0x7f;
9434 regidx = (bit & 0x60) >> 5;
9435 bit &= 0x1f;
9436 mc_filter[regidx] |= (1 << bit);
9437 }
9438
9439 tw32(MAC_HASH_REG_0, mc_filter[0]);
9440 tw32(MAC_HASH_REG_1, mc_filter[1]);
9441 tw32(MAC_HASH_REG_2, mc_filter[2]);
9442 tw32(MAC_HASH_REG_3, mc_filter[3]);
9443 }
9444
9445 if (rx_mode != tp->rx_mode) {
9446 tp->rx_mode = rx_mode;
9447 tw32_f(MAC_RX_MODE, rx_mode);
9448 udelay(10);
9449 }
9450}
9451
9452static void tg3_set_rx_mode(struct net_device *dev)
9453{
9454 struct tg3 *tp = netdev_priv(dev);
9455
Michael Chane75f7c92006-03-20 21:33:26 -08009456 if (!netif_running(dev))
9457 return;
9458
David S. Millerf47c11e2005-06-24 20:18:35 -07009459 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009460 __tg3_set_rx_mode(dev);
David S. Millerf47c11e2005-06-24 20:18:35 -07009461 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009462}
9463
9464#define TG3_REGDUMP_LEN (32 * 1024)
9465
9466static int tg3_get_regs_len(struct net_device *dev)
9467{
9468 return TG3_REGDUMP_LEN;
9469}
9470
9471static void tg3_get_regs(struct net_device *dev,
9472 struct ethtool_regs *regs, void *_p)
9473{
9474 u32 *p = _p;
9475 struct tg3 *tp = netdev_priv(dev);
9476 u8 *orig_p = _p;
9477 int i;
9478
9479 regs->version = 0;
9480
9481 memset(p, 0, TG3_REGDUMP_LEN);
9482
Michael Chanbc1c7562006-03-20 17:48:03 -08009483 if (tp->link_config.phy_is_low_power)
9484 return;
9485
David S. Millerf47c11e2005-06-24 20:18:35 -07009486 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009487
9488#define __GET_REG32(reg) (*(p)++ = tr32(reg))
9489#define GET_REG32_LOOP(base,len) \
9490do { p = (u32 *)(orig_p + (base)); \
9491 for (i = 0; i < len; i += 4) \
9492 __GET_REG32((base) + i); \
9493} while (0)
9494#define GET_REG32_1(reg) \
9495do { p = (u32 *)(orig_p + (reg)); \
9496 __GET_REG32((reg)); \
9497} while (0)
9498
9499 GET_REG32_LOOP(TG3PCI_VENDOR, 0xb0);
9500 GET_REG32_LOOP(MAILBOX_INTERRUPT_0, 0x200);
9501 GET_REG32_LOOP(MAC_MODE, 0x4f0);
9502 GET_REG32_LOOP(SNDDATAI_MODE, 0xe0);
9503 GET_REG32_1(SNDDATAC_MODE);
9504 GET_REG32_LOOP(SNDBDS_MODE, 0x80);
9505 GET_REG32_LOOP(SNDBDI_MODE, 0x48);
9506 GET_REG32_1(SNDBDC_MODE);
9507 GET_REG32_LOOP(RCVLPC_MODE, 0x20);
9508 GET_REG32_LOOP(RCVLPC_SELLST_BASE, 0x15c);
9509 GET_REG32_LOOP(RCVDBDI_MODE, 0x0c);
9510 GET_REG32_LOOP(RCVDBDI_JUMBO_BD, 0x3c);
9511 GET_REG32_LOOP(RCVDBDI_BD_PROD_IDX_0, 0x44);
9512 GET_REG32_1(RCVDCC_MODE);
9513 GET_REG32_LOOP(RCVBDI_MODE, 0x20);
9514 GET_REG32_LOOP(RCVCC_MODE, 0x14);
9515 GET_REG32_LOOP(RCVLSC_MODE, 0x08);
9516 GET_REG32_1(MBFREE_MODE);
9517 GET_REG32_LOOP(HOSTCC_MODE, 0x100);
9518 GET_REG32_LOOP(MEMARB_MODE, 0x10);
9519 GET_REG32_LOOP(BUFMGR_MODE, 0x58);
9520 GET_REG32_LOOP(RDMAC_MODE, 0x08);
9521 GET_REG32_LOOP(WDMAC_MODE, 0x08);
Chris Elmquist091465d2005-12-20 13:25:19 -08009522 GET_REG32_1(RX_CPU_MODE);
9523 GET_REG32_1(RX_CPU_STATE);
9524 GET_REG32_1(RX_CPU_PGMCTR);
9525 GET_REG32_1(RX_CPU_HWBKPT);
9526 GET_REG32_1(TX_CPU_MODE);
9527 GET_REG32_1(TX_CPU_STATE);
9528 GET_REG32_1(TX_CPU_PGMCTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009529 GET_REG32_LOOP(GRCMBOX_INTERRUPT_0, 0x110);
9530 GET_REG32_LOOP(FTQ_RESET, 0x120);
9531 GET_REG32_LOOP(MSGINT_MODE, 0x0c);
9532 GET_REG32_1(DMAC_MODE);
9533 GET_REG32_LOOP(GRC_MODE, 0x4c);
9534 if (tp->tg3_flags & TG3_FLAG_NVRAM)
9535 GET_REG32_LOOP(NVRAM_CMD, 0x24);
9536
9537#undef __GET_REG32
9538#undef GET_REG32_LOOP
9539#undef GET_REG32_1
9540
David S. Millerf47c11e2005-06-24 20:18:35 -07009541 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009542}
9543
9544static int tg3_get_eeprom_len(struct net_device *dev)
9545{
9546 struct tg3 *tp = netdev_priv(dev);
9547
9548 return tp->nvram_size;
9549}
9550
Linus Torvalds1da177e2005-04-16 15:20:36 -07009551static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9552{
9553 struct tg3 *tp = netdev_priv(dev);
9554 int ret;
9555 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -08009556 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009557 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009558
Matt Carlsondf259d82009-04-20 06:57:14 +00009559 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM)
9560 return -EINVAL;
9561
Michael Chanbc1c7562006-03-20 17:48:03 -08009562 if (tp->link_config.phy_is_low_power)
9563 return -EAGAIN;
9564
Linus Torvalds1da177e2005-04-16 15:20:36 -07009565 offset = eeprom->offset;
9566 len = eeprom->len;
9567 eeprom->len = 0;
9568
9569 eeprom->magic = TG3_EEPROM_MAGIC;
9570
9571 if (offset & 3) {
9572 /* adjustments to start on required 4 byte boundary */
9573 b_offset = offset & 3;
9574 b_count = 4 - b_offset;
9575 if (b_count > len) {
9576 /* i.e. offset=1 len=2 */
9577 b_count = len;
9578 }
Matt Carlsona9dc5292009-02-25 14:25:30 +00009579 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009580 if (ret)
9581 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009582 memcpy(data, ((char*)&val) + b_offset, b_count);
9583 len -= b_count;
9584 offset += b_count;
9585 eeprom->len += b_count;
9586 }
9587
9588 /* read bytes upto the last 4 byte boundary */
9589 pd = &data[eeprom->len];
9590 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +00009591 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009592 if (ret) {
9593 eeprom->len += i;
9594 return ret;
9595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009596 memcpy(pd + i, &val, 4);
9597 }
9598 eeprom->len += i;
9599
9600 if (len & 3) {
9601 /* read last bytes not ending on 4 byte boundary */
9602 pd = &data[eeprom->len];
9603 b_count = len & 3;
9604 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009605 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009606 if (ret)
9607 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009608 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009609 eeprom->len += b_count;
9610 }
9611 return 0;
9612}
9613
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009614static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009615
9616static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9617{
9618 struct tg3 *tp = netdev_priv(dev);
9619 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009620 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009621 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009622 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009623
Michael Chanbc1c7562006-03-20 17:48:03 -08009624 if (tp->link_config.phy_is_low_power)
9625 return -EAGAIN;
9626
Matt Carlsondf259d82009-04-20 06:57:14 +00009627 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
9628 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009629 return -EINVAL;
9630
9631 offset = eeprom->offset;
9632 len = eeprom->len;
9633
9634 if ((b_offset = (offset & 3))) {
9635 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +00009636 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009637 if (ret)
9638 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009639 len += b_offset;
9640 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -07009641 if (len < 4)
9642 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009643 }
9644
9645 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -07009646 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009647 /* adjustments to end on required 4 byte boundary */
9648 odd_len = 1;
9649 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009650 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009651 if (ret)
9652 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009653 }
9654
9655 buf = data;
9656 if (b_offset || odd_len) {
9657 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009658 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009659 return -ENOMEM;
9660 if (b_offset)
9661 memcpy(buf, &start, 4);
9662 if (odd_len)
9663 memcpy(buf+len-4, &end, 4);
9664 memcpy(buf + b_offset, data, eeprom->len);
9665 }
9666
9667 ret = tg3_nvram_write_block(tp, offset, len, buf);
9668
9669 if (buf != data)
9670 kfree(buf);
9671
9672 return ret;
9673}
9674
9675static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9676{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009677 struct tg3 *tp = netdev_priv(dev);
9678
9679 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009680 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009681 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
9682 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009683 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
9684 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009685 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009686
Linus Torvalds1da177e2005-04-16 15:20:36 -07009687 cmd->supported = (SUPPORTED_Autoneg);
9688
9689 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY))
9690 cmd->supported |= (SUPPORTED_1000baseT_Half |
9691 SUPPORTED_1000baseT_Full);
9692
Karsten Keilef348142006-05-12 12:49:08 -07009693 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009694 cmd->supported |= (SUPPORTED_100baseT_Half |
9695 SUPPORTED_100baseT_Full |
9696 SUPPORTED_10baseT_Half |
9697 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -08009698 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -07009699 cmd->port = PORT_TP;
9700 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009701 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -07009702 cmd->port = PORT_FIBRE;
9703 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009704
Linus Torvalds1da177e2005-04-16 15:20:36 -07009705 cmd->advertising = tp->link_config.advertising;
9706 if (netif_running(dev)) {
9707 cmd->speed = tp->link_config.active_speed;
9708 cmd->duplex = tp->link_config.active_duplex;
9709 }
Matt Carlson882e9792009-09-01 13:21:36 +00009710 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +00009711 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009712 cmd->autoneg = tp->link_config.autoneg;
9713 cmd->maxtxpkt = 0;
9714 cmd->maxrxpkt = 0;
9715 return 0;
9716}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009717
Linus Torvalds1da177e2005-04-16 15:20:36 -07009718static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9719{
9720 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009721
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009722 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009723 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009724 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
9725 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009726 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
9727 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009728 }
9729
Matt Carlson7e5856b2009-02-25 14:23:01 +00009730 if (cmd->autoneg != AUTONEG_ENABLE &&
9731 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -07009732 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +00009733
9734 if (cmd->autoneg == AUTONEG_DISABLE &&
9735 cmd->duplex != DUPLEX_FULL &&
9736 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -07009737 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009738
Matt Carlson7e5856b2009-02-25 14:23:01 +00009739 if (cmd->autoneg == AUTONEG_ENABLE) {
9740 u32 mask = ADVERTISED_Autoneg |
9741 ADVERTISED_Pause |
9742 ADVERTISED_Asym_Pause;
9743
9744 if (!(tp->tg3_flags2 & TG3_FLAG_10_100_ONLY))
9745 mask |= ADVERTISED_1000baseT_Half |
9746 ADVERTISED_1000baseT_Full;
9747
9748 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES))
9749 mask |= ADVERTISED_100baseT_Half |
9750 ADVERTISED_100baseT_Full |
9751 ADVERTISED_10baseT_Half |
9752 ADVERTISED_10baseT_Full |
9753 ADVERTISED_TP;
9754 else
9755 mask |= ADVERTISED_FIBRE;
9756
9757 if (cmd->advertising & ~mask)
9758 return -EINVAL;
9759
9760 mask &= (ADVERTISED_1000baseT_Half |
9761 ADVERTISED_1000baseT_Full |
9762 ADVERTISED_100baseT_Half |
9763 ADVERTISED_100baseT_Full |
9764 ADVERTISED_10baseT_Half |
9765 ADVERTISED_10baseT_Full);
9766
9767 cmd->advertising &= mask;
9768 } else {
9769 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES) {
9770 if (cmd->speed != SPEED_1000)
9771 return -EINVAL;
9772
9773 if (cmd->duplex != DUPLEX_FULL)
9774 return -EINVAL;
9775 } else {
9776 if (cmd->speed != SPEED_100 &&
9777 cmd->speed != SPEED_10)
9778 return -EINVAL;
9779 }
9780 }
9781
David S. Millerf47c11e2005-06-24 20:18:35 -07009782 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009783
9784 tp->link_config.autoneg = cmd->autoneg;
9785 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -07009786 tp->link_config.advertising = (cmd->advertising |
9787 ADVERTISED_Autoneg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009788 tp->link_config.speed = SPEED_INVALID;
9789 tp->link_config.duplex = DUPLEX_INVALID;
9790 } else {
9791 tp->link_config.advertising = 0;
9792 tp->link_config.speed = cmd->speed;
9793 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009794 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009795
Michael Chan24fcad62006-12-17 17:06:46 -08009796 tp->link_config.orig_speed = tp->link_config.speed;
9797 tp->link_config.orig_duplex = tp->link_config.duplex;
9798 tp->link_config.orig_autoneg = tp->link_config.autoneg;
9799
Linus Torvalds1da177e2005-04-16 15:20:36 -07009800 if (netif_running(dev))
9801 tg3_setup_phy(tp, 1);
9802
David S. Millerf47c11e2005-06-24 20:18:35 -07009803 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009804
Linus Torvalds1da177e2005-04-16 15:20:36 -07009805 return 0;
9806}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009807
Linus Torvalds1da177e2005-04-16 15:20:36 -07009808static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
9809{
9810 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009811
Linus Torvalds1da177e2005-04-16 15:20:36 -07009812 strcpy(info->driver, DRV_MODULE_NAME);
9813 strcpy(info->version, DRV_MODULE_VERSION);
Michael Chanc4e65752006-03-20 22:29:32 -08009814 strcpy(info->fw_version, tp->fw_ver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009815 strcpy(info->bus_info, pci_name(tp->pdev));
9816}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009817
Linus Torvalds1da177e2005-04-16 15:20:36 -07009818static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
9819{
9820 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009821
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009822 if ((tp->tg3_flags & TG3_FLAG_WOL_CAP) &&
9823 device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -07009824 wol->supported = WAKE_MAGIC;
9825 else
9826 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009827 wol->wolopts = 0;
Matt Carlson05ac4cb2008-11-03 16:53:46 -08009828 if ((tp->tg3_flags & TG3_FLAG_WOL_ENABLE) &&
9829 device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009830 wol->wolopts = WAKE_MAGIC;
9831 memset(&wol->sopass, 0, sizeof(wol->sopass));
9832}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009833
Linus Torvalds1da177e2005-04-16 15:20:36 -07009834static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
9835{
9836 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009837 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009838
Linus Torvalds1da177e2005-04-16 15:20:36 -07009839 if (wol->wolopts & ~WAKE_MAGIC)
9840 return -EINVAL;
9841 if ((wol->wolopts & WAKE_MAGIC) &&
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009842 !((tp->tg3_flags & TG3_FLAG_WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009843 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009844
David S. Millerf47c11e2005-06-24 20:18:35 -07009845 spin_lock_bh(&tp->lock);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009846 if (wol->wolopts & WAKE_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009847 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009848 device_set_wakeup_enable(dp, true);
9849 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009850 tp->tg3_flags &= ~TG3_FLAG_WOL_ENABLE;
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009851 device_set_wakeup_enable(dp, false);
9852 }
David S. Millerf47c11e2005-06-24 20:18:35 -07009853 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009854
Linus Torvalds1da177e2005-04-16 15:20:36 -07009855 return 0;
9856}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009857
Linus Torvalds1da177e2005-04-16 15:20:36 -07009858static u32 tg3_get_msglevel(struct net_device *dev)
9859{
9860 struct tg3 *tp = netdev_priv(dev);
9861 return tp->msg_enable;
9862}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009863
Linus Torvalds1da177e2005-04-16 15:20:36 -07009864static void tg3_set_msglevel(struct net_device *dev, u32 value)
9865{
9866 struct tg3 *tp = netdev_priv(dev);
9867 tp->msg_enable = value;
9868}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009869
Linus Torvalds1da177e2005-04-16 15:20:36 -07009870static int tg3_set_tso(struct net_device *dev, u32 value)
9871{
9872 struct tg3 *tp = netdev_priv(dev);
9873
9874 if (!(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
9875 if (value)
9876 return -EINVAL;
9877 return 0;
9878 }
Matt Carlson027455a2008-12-21 20:19:30 -08009879 if ((dev->features & NETIF_F_IPV6_CSUM) &&
Matt Carlsone849cdc2009-11-13 13:03:38 +00009880 ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_2) ||
9881 (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3))) {
Matt Carlson9936bcf2007-10-10 18:03:07 -07009882 if (value) {
Michael Chanb0026622006-07-03 19:42:14 -07009883 dev->features |= NETIF_F_TSO6;
Matt Carlsone849cdc2009-11-13 13:03:38 +00009884 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
9885 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -07009886 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
9887 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Matt Carlson321d32a2008-11-21 17:22:19 -08009888 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsone849cdc2009-11-13 13:03:38 +00009889 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson9936bcf2007-10-10 18:03:07 -07009890 dev->features |= NETIF_F_TSO_ECN;
9891 } else
9892 dev->features &= ~(NETIF_F_TSO6 | NETIF_F_TSO_ECN);
Michael Chanb0026622006-07-03 19:42:14 -07009893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009894 return ethtool_op_set_tso(dev, value);
9895}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009896
Linus Torvalds1da177e2005-04-16 15:20:36 -07009897static int tg3_nway_reset(struct net_device *dev)
9898{
9899 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009900 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009901
Linus Torvalds1da177e2005-04-16 15:20:36 -07009902 if (!netif_running(dev))
9903 return -EAGAIN;
9904
Michael Chanc94e3942005-09-27 12:12:42 -07009905 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
9906 return -EINVAL;
9907
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009908 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
9909 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
9910 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009911 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009912 } else {
9913 u32 bmcr;
9914
9915 spin_lock_bh(&tp->lock);
9916 r = -EINVAL;
9917 tg3_readphy(tp, MII_BMCR, &bmcr);
9918 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
9919 ((bmcr & BMCR_ANENABLE) ||
9920 (tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT))) {
9921 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
9922 BMCR_ANENABLE);
9923 r = 0;
9924 }
9925 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009926 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009927
Linus Torvalds1da177e2005-04-16 15:20:36 -07009928 return r;
9929}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009930
Linus Torvalds1da177e2005-04-16 15:20:36 -07009931static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
9932{
9933 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009934
Linus Torvalds1da177e2005-04-16 15:20:36 -07009935 ering->rx_max_pending = TG3_RX_RING_SIZE - 1;
9936 ering->rx_mini_max_pending = 0;
Michael Chan4f81c322006-03-20 21:33:42 -08009937 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE)
9938 ering->rx_jumbo_max_pending = TG3_RX_JUMBO_RING_SIZE - 1;
9939 else
9940 ering->rx_jumbo_max_pending = 0;
9941
9942 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009943
9944 ering->rx_pending = tp->rx_pending;
9945 ering->rx_mini_pending = 0;
Michael Chan4f81c322006-03-20 21:33:42 -08009946 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE)
9947 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
9948 else
9949 ering->rx_jumbo_pending = 0;
9950
Matt Carlsonf3f3f272009-08-28 14:03:21 +00009951 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009952}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009953
Linus Torvalds1da177e2005-04-16 15:20:36 -07009954static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
9955{
9956 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +00009957 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009958
Linus Torvalds1da177e2005-04-16 15:20:36 -07009959 if ((ering->rx_pending > TG3_RX_RING_SIZE - 1) ||
9960 (ering->rx_jumbo_pending > TG3_RX_JUMBO_RING_SIZE - 1) ||
Michael Chanbc3a9252006-10-18 20:55:18 -07009961 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
9962 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Michael Chan7f62ad52007-02-20 23:25:40 -08009963 ((tp->tg3_flags2 & TG3_FLG2_TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -07009964 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009965 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009966
Michael Chanbbe832c2005-06-24 20:20:04 -07009967 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009968 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009969 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -07009970 irq_sync = 1;
9971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009972
Michael Chanbbe832c2005-06-24 20:20:04 -07009973 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009974
Linus Torvalds1da177e2005-04-16 15:20:36 -07009975 tp->rx_pending = ering->rx_pending;
9976
9977 if ((tp->tg3_flags2 & TG3_FLG2_MAX_RXPEND_64) &&
9978 tp->rx_pending > 63)
9979 tp->rx_pending = 63;
9980 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +00009981
9982 for (i = 0; i < TG3_IRQ_MAX_VECS; i++)
9983 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009984
9985 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -07009986 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -07009987 err = tg3_restart_hw(tp, 1);
9988 if (!err)
9989 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009990 }
9991
David S. Millerf47c11e2005-06-24 20:18:35 -07009992 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009993
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009994 if (irq_sync && !err)
9995 tg3_phy_start(tp);
9996
Michael Chanb9ec6c12006-07-25 16:37:27 -07009997 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009998}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009999
Linus Torvalds1da177e2005-04-16 15:20:36 -070010000static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10001{
10002 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010003
Linus Torvalds1da177e2005-04-16 15:20:36 -070010004 epause->autoneg = (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG) != 0;
Matt Carlson8d018622007-12-20 20:05:44 -080010005
Steve Glendinninge18ce342008-12-16 02:00:00 -080010006 if (tp->link_config.active_flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080010007 epause->rx_pause = 1;
10008 else
10009 epause->rx_pause = 0;
10010
Steve Glendinninge18ce342008-12-16 02:00:00 -080010011 if (tp->link_config.active_flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080010012 epause->tx_pause = 1;
10013 else
10014 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010015}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010016
Linus Torvalds1da177e2005-04-16 15:20:36 -070010017static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10018{
10019 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010020 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010021
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010022 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
10023 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
10024 return -EAGAIN;
10025
10026 if (epause->autoneg) {
10027 u32 newadv;
10028 struct phy_device *phydev;
10029
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010030 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010031
10032 if (epause->rx_pause) {
10033 if (epause->tx_pause)
10034 newadv = ADVERTISED_Pause;
10035 else
10036 newadv = ADVERTISED_Pause |
10037 ADVERTISED_Asym_Pause;
10038 } else if (epause->tx_pause) {
10039 newadv = ADVERTISED_Asym_Pause;
10040 } else
10041 newadv = 0;
10042
10043 if (tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED) {
10044 u32 oldadv = phydev->advertising &
10045 (ADVERTISED_Pause |
10046 ADVERTISED_Asym_Pause);
10047 if (oldadv != newadv) {
10048 phydev->advertising &=
10049 ~(ADVERTISED_Pause |
10050 ADVERTISED_Asym_Pause);
10051 phydev->advertising |= newadv;
10052 err = phy_start_aneg(phydev);
10053 }
10054 } else {
10055 tp->link_config.advertising &=
10056 ~(ADVERTISED_Pause |
10057 ADVERTISED_Asym_Pause);
10058 tp->link_config.advertising |= newadv;
10059 }
10060 } else {
10061 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010062 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010063 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010064 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010065
10066 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010067 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010068 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010069 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010070
10071 if (netif_running(dev))
10072 tg3_setup_flow_control(tp, 0, 0);
10073 }
10074 } else {
10075 int irq_sync = 0;
10076
10077 if (netif_running(dev)) {
10078 tg3_netif_stop(tp);
10079 irq_sync = 1;
10080 }
10081
10082 tg3_full_lock(tp, irq_sync);
10083
10084 if (epause->autoneg)
10085 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
10086 else
10087 tp->tg3_flags &= ~TG3_FLAG_PAUSE_AUTONEG;
10088 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010089 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010090 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010091 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010092 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010093 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010094 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010095 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010096
10097 if (netif_running(dev)) {
10098 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10099 err = tg3_restart_hw(tp, 1);
10100 if (!err)
10101 tg3_netif_start(tp);
10102 }
10103
10104 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010106
Michael Chanb9ec6c12006-07-25 16:37:27 -070010107 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010108}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010109
Linus Torvalds1da177e2005-04-16 15:20:36 -070010110static u32 tg3_get_rx_csum(struct net_device *dev)
10111{
10112 struct tg3 *tp = netdev_priv(dev);
10113 return (tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) != 0;
10114}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010115
Linus Torvalds1da177e2005-04-16 15:20:36 -070010116static int tg3_set_rx_csum(struct net_device *dev, u32 data)
10117{
10118 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010119
Linus Torvalds1da177e2005-04-16 15:20:36 -070010120 if (tp->tg3_flags & TG3_FLAG_BROKEN_CHECKSUMS) {
10121 if (data != 0)
10122 return -EINVAL;
10123 return 0;
10124 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010125
David S. Millerf47c11e2005-06-24 20:18:35 -070010126 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010127 if (data)
10128 tp->tg3_flags |= TG3_FLAG_RX_CHECKSUMS;
10129 else
10130 tp->tg3_flags &= ~TG3_FLAG_RX_CHECKSUMS;
David S. Millerf47c11e2005-06-24 20:18:35 -070010131 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010132
Linus Torvalds1da177e2005-04-16 15:20:36 -070010133 return 0;
10134}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010135
Linus Torvalds1da177e2005-04-16 15:20:36 -070010136static int tg3_set_tx_csum(struct net_device *dev, u32 data)
10137{
10138 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010139
Linus Torvalds1da177e2005-04-16 15:20:36 -070010140 if (tp->tg3_flags & TG3_FLAG_BROKEN_CHECKSUMS) {
10141 if (data != 0)
10142 return -EINVAL;
10143 return 0;
10144 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010145
Matt Carlson321d32a2008-11-21 17:22:19 -080010146 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Michael Chan6460d942007-07-14 19:07:52 -070010147 ethtool_op_set_tx_ipv6_csum(dev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010148 else
Michael Chan9c27dbd2006-03-20 22:28:27 -080010149 ethtool_op_set_tx_csum(dev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010150
10151 return 0;
10152}
10153
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010154static int tg3_get_sset_count (struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010155{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010156 switch (sset) {
10157 case ETH_SS_TEST:
10158 return TG3_NUM_TEST;
10159 case ETH_SS_STATS:
10160 return TG3_NUM_STATS;
10161 default:
10162 return -EOPNOTSUPP;
10163 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070010164}
10165
Linus Torvalds1da177e2005-04-16 15:20:36 -070010166static void tg3_get_strings (struct net_device *dev, u32 stringset, u8 *buf)
10167{
10168 switch (stringset) {
10169 case ETH_SS_STATS:
10170 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
10171 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070010172 case ETH_SS_TEST:
10173 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
10174 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010175 default:
10176 WARN_ON(1); /* we need a WARN() */
10177 break;
10178 }
10179}
10180
Michael Chan4009a932005-09-05 17:52:54 -070010181static int tg3_phys_id(struct net_device *dev, u32 data)
10182{
10183 struct tg3 *tp = netdev_priv(dev);
10184 int i;
10185
10186 if (!netif_running(tp->dev))
10187 return -EAGAIN;
10188
10189 if (data == 0)
Stephen Hemminger759afc32008-02-23 19:51:59 -080010190 data = UINT_MAX / 2;
Michael Chan4009a932005-09-05 17:52:54 -070010191
10192 for (i = 0; i < (data * 2); i++) {
10193 if ((i % 2) == 0)
10194 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10195 LED_CTRL_1000MBPS_ON |
10196 LED_CTRL_100MBPS_ON |
10197 LED_CTRL_10MBPS_ON |
10198 LED_CTRL_TRAFFIC_OVERRIDE |
10199 LED_CTRL_TRAFFIC_BLINK |
10200 LED_CTRL_TRAFFIC_LED);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010201
Michael Chan4009a932005-09-05 17:52:54 -070010202 else
10203 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10204 LED_CTRL_TRAFFIC_OVERRIDE);
10205
10206 if (msleep_interruptible(500))
10207 break;
10208 }
10209 tw32(MAC_LED_CTRL, tp->led_ctrl);
10210 return 0;
10211}
10212
Linus Torvalds1da177e2005-04-16 15:20:36 -070010213static void tg3_get_ethtool_stats (struct net_device *dev,
10214 struct ethtool_stats *estats, u64 *tmp_stats)
10215{
10216 struct tg3 *tp = netdev_priv(dev);
10217 memcpy(tmp_stats, tg3_get_estats(tp), sizeof(tp->estats));
10218}
10219
Michael Chan566f86a2005-05-29 14:56:58 -070010220#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080010221#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
10222#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
10223#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Michael Chanb16250e2006-09-27 16:10:14 -070010224#define NVRAM_SELFBOOT_HW_SIZE 0x20
10225#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070010226
10227static int tg3_test_nvram(struct tg3 *tp)
10228{
Al Virob9fc7dc2007-12-17 22:59:57 -080010229 u32 csum, magic;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010230 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010231 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070010232
Matt Carlsondf259d82009-04-20 06:57:14 +000010233 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM)
10234 return 0;
10235
Matt Carlsone4f34112009-02-25 14:25:00 +000010236 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080010237 return -EIO;
10238
Michael Chan1b277772006-03-20 22:27:48 -080010239 if (magic == TG3_EEPROM_MAGIC)
10240 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070010241 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080010242 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
10243 TG3_EEPROM_SB_FORMAT_1) {
10244 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
10245 case TG3_EEPROM_SB_REVISION_0:
10246 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
10247 break;
10248 case TG3_EEPROM_SB_REVISION_2:
10249 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
10250 break;
10251 case TG3_EEPROM_SB_REVISION_3:
10252 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
10253 break;
10254 default:
10255 return 0;
10256 }
10257 } else
Michael Chan1b277772006-03-20 22:27:48 -080010258 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070010259 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
10260 size = NVRAM_SELFBOOT_HW_SIZE;
10261 else
Michael Chan1b277772006-03-20 22:27:48 -080010262 return -EIO;
10263
10264 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070010265 if (buf == NULL)
10266 return -ENOMEM;
10267
Michael Chan1b277772006-03-20 22:27:48 -080010268 err = -EIO;
10269 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010270 err = tg3_nvram_read_be32(tp, i, &buf[j]);
10271 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070010272 break;
Michael Chan566f86a2005-05-29 14:56:58 -070010273 }
Michael Chan1b277772006-03-20 22:27:48 -080010274 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070010275 goto out;
10276
Michael Chan1b277772006-03-20 22:27:48 -080010277 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010278 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080010279 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010280 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080010281 u8 *buf8 = (u8 *) buf, csum8 = 0;
10282
Al Virob9fc7dc2007-12-17 22:59:57 -080010283 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080010284 TG3_EEPROM_SB_REVISION_2) {
10285 /* For rev 2, the csum doesn't include the MBA. */
10286 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
10287 csum8 += buf8[i];
10288 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
10289 csum8 += buf8[i];
10290 } else {
10291 for (i = 0; i < size; i++)
10292 csum8 += buf8[i];
10293 }
Michael Chan1b277772006-03-20 22:27:48 -080010294
Adrian Bunkad96b482006-04-05 22:21:04 -070010295 if (csum8 == 0) {
10296 err = 0;
10297 goto out;
10298 }
10299
10300 err = -EIO;
10301 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080010302 }
Michael Chan566f86a2005-05-29 14:56:58 -070010303
Al Virob9fc7dc2007-12-17 22:59:57 -080010304 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010305 TG3_EEPROM_MAGIC_HW) {
10306 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000010307 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070010308 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070010309
10310 /* Separate the parity bits and the data bytes. */
10311 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
10312 if ((i == 0) || (i == 8)) {
10313 int l;
10314 u8 msk;
10315
10316 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
10317 parity[k++] = buf8[i] & msk;
10318 i++;
10319 }
10320 else if (i == 16) {
10321 int l;
10322 u8 msk;
10323
10324 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
10325 parity[k++] = buf8[i] & msk;
10326 i++;
10327
10328 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
10329 parity[k++] = buf8[i] & msk;
10330 i++;
10331 }
10332 data[j++] = buf8[i];
10333 }
10334
10335 err = -EIO;
10336 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
10337 u8 hw8 = hweight8(data[i]);
10338
10339 if ((hw8 & 0x1) && parity[i])
10340 goto out;
10341 else if (!(hw8 & 0x1) && !parity[i])
10342 goto out;
10343 }
10344 err = 0;
10345 goto out;
10346 }
10347
Michael Chan566f86a2005-05-29 14:56:58 -070010348 /* Bootstrap checksum at offset 0x10 */
10349 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlsona9dc5292009-02-25 14:25:30 +000010350 if (csum != be32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070010351 goto out;
10352
10353 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
10354 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlsona9dc5292009-02-25 14:25:30 +000010355 if (csum != be32_to_cpu(buf[0xfc/4]))
10356 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070010357
10358 err = 0;
10359
10360out:
10361 kfree(buf);
10362 return err;
10363}
10364
Michael Chanca430072005-05-29 14:57:23 -070010365#define TG3_SERDES_TIMEOUT_SEC 2
10366#define TG3_COPPER_TIMEOUT_SEC 6
10367
10368static int tg3_test_link(struct tg3 *tp)
10369{
10370 int i, max;
10371
10372 if (!netif_running(tp->dev))
10373 return -ENODEV;
10374
Michael Chan4c987482005-09-05 17:52:38 -070010375 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070010376 max = TG3_SERDES_TIMEOUT_SEC;
10377 else
10378 max = TG3_COPPER_TIMEOUT_SEC;
10379
10380 for (i = 0; i < max; i++) {
10381 if (netif_carrier_ok(tp->dev))
10382 return 0;
10383
10384 if (msleep_interruptible(1000))
10385 break;
10386 }
10387
10388 return -EIO;
10389}
10390
Michael Chana71116d2005-05-29 14:58:11 -070010391/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080010392static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070010393{
Michael Chanb16250e2006-09-27 16:10:14 -070010394 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070010395 u32 offset, read_mask, write_mask, val, save_val, read_val;
10396 static struct {
10397 u16 offset;
10398 u16 flags;
10399#define TG3_FL_5705 0x1
10400#define TG3_FL_NOT_5705 0x2
10401#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070010402#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070010403 u32 read_mask;
10404 u32 write_mask;
10405 } reg_tbl[] = {
10406 /* MAC Control Registers */
10407 { MAC_MODE, TG3_FL_NOT_5705,
10408 0x00000000, 0x00ef6f8c },
10409 { MAC_MODE, TG3_FL_5705,
10410 0x00000000, 0x01ef6b8c },
10411 { MAC_STATUS, TG3_FL_NOT_5705,
10412 0x03800107, 0x00000000 },
10413 { MAC_STATUS, TG3_FL_5705,
10414 0x03800100, 0x00000000 },
10415 { MAC_ADDR_0_HIGH, 0x0000,
10416 0x00000000, 0x0000ffff },
10417 { MAC_ADDR_0_LOW, 0x0000,
10418 0x00000000, 0xffffffff },
10419 { MAC_RX_MTU_SIZE, 0x0000,
10420 0x00000000, 0x0000ffff },
10421 { MAC_TX_MODE, 0x0000,
10422 0x00000000, 0x00000070 },
10423 { MAC_TX_LENGTHS, 0x0000,
10424 0x00000000, 0x00003fff },
10425 { MAC_RX_MODE, TG3_FL_NOT_5705,
10426 0x00000000, 0x000007fc },
10427 { MAC_RX_MODE, TG3_FL_5705,
10428 0x00000000, 0x000007dc },
10429 { MAC_HASH_REG_0, 0x0000,
10430 0x00000000, 0xffffffff },
10431 { MAC_HASH_REG_1, 0x0000,
10432 0x00000000, 0xffffffff },
10433 { MAC_HASH_REG_2, 0x0000,
10434 0x00000000, 0xffffffff },
10435 { MAC_HASH_REG_3, 0x0000,
10436 0x00000000, 0xffffffff },
10437
10438 /* Receive Data and Receive BD Initiator Control Registers. */
10439 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
10440 0x00000000, 0xffffffff },
10441 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
10442 0x00000000, 0xffffffff },
10443 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
10444 0x00000000, 0x00000003 },
10445 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
10446 0x00000000, 0xffffffff },
10447 { RCVDBDI_STD_BD+0, 0x0000,
10448 0x00000000, 0xffffffff },
10449 { RCVDBDI_STD_BD+4, 0x0000,
10450 0x00000000, 0xffffffff },
10451 { RCVDBDI_STD_BD+8, 0x0000,
10452 0x00000000, 0xffff0002 },
10453 { RCVDBDI_STD_BD+0xc, 0x0000,
10454 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010455
Michael Chana71116d2005-05-29 14:58:11 -070010456 /* Receive BD Initiator Control Registers. */
10457 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
10458 0x00000000, 0xffffffff },
10459 { RCVBDI_STD_THRESH, TG3_FL_5705,
10460 0x00000000, 0x000003ff },
10461 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
10462 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010463
Michael Chana71116d2005-05-29 14:58:11 -070010464 /* Host Coalescing Control Registers. */
10465 { HOSTCC_MODE, TG3_FL_NOT_5705,
10466 0x00000000, 0x00000004 },
10467 { HOSTCC_MODE, TG3_FL_5705,
10468 0x00000000, 0x000000f6 },
10469 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
10470 0x00000000, 0xffffffff },
10471 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
10472 0x00000000, 0x000003ff },
10473 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
10474 0x00000000, 0xffffffff },
10475 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
10476 0x00000000, 0x000003ff },
10477 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
10478 0x00000000, 0xffffffff },
10479 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10480 0x00000000, 0x000000ff },
10481 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
10482 0x00000000, 0xffffffff },
10483 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10484 0x00000000, 0x000000ff },
10485 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
10486 0x00000000, 0xffffffff },
10487 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
10488 0x00000000, 0xffffffff },
10489 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10490 0x00000000, 0xffffffff },
10491 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10492 0x00000000, 0x000000ff },
10493 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10494 0x00000000, 0xffffffff },
10495 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10496 0x00000000, 0x000000ff },
10497 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
10498 0x00000000, 0xffffffff },
10499 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
10500 0x00000000, 0xffffffff },
10501 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
10502 0x00000000, 0xffffffff },
10503 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
10504 0x00000000, 0xffffffff },
10505 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
10506 0x00000000, 0xffffffff },
10507 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
10508 0xffffffff, 0x00000000 },
10509 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
10510 0xffffffff, 0x00000000 },
10511
10512 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070010513 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010514 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070010515 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010516 0x00000000, 0x007fffff },
10517 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
10518 0x00000000, 0x0000003f },
10519 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
10520 0x00000000, 0x000001ff },
10521 { BUFMGR_MB_HIGH_WATER, 0x0000,
10522 0x00000000, 0x000001ff },
10523 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
10524 0xffffffff, 0x00000000 },
10525 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
10526 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010527
Michael Chana71116d2005-05-29 14:58:11 -070010528 /* Mailbox Registers */
10529 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
10530 0x00000000, 0x000001ff },
10531 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
10532 0x00000000, 0x000001ff },
10533 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
10534 0x00000000, 0x000007ff },
10535 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
10536 0x00000000, 0x000001ff },
10537
10538 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
10539 };
10540
Michael Chanb16250e2006-09-27 16:10:14 -070010541 is_5705 = is_5750 = 0;
10542 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
Michael Chana71116d2005-05-29 14:58:11 -070010543 is_5705 = 1;
Michael Chanb16250e2006-09-27 16:10:14 -070010544 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
10545 is_5750 = 1;
10546 }
Michael Chana71116d2005-05-29 14:58:11 -070010547
10548 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
10549 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
10550 continue;
10551
10552 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
10553 continue;
10554
10555 if ((tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
10556 (reg_tbl[i].flags & TG3_FL_NOT_5788))
10557 continue;
10558
Michael Chanb16250e2006-09-27 16:10:14 -070010559 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
10560 continue;
10561
Michael Chana71116d2005-05-29 14:58:11 -070010562 offset = (u32) reg_tbl[i].offset;
10563 read_mask = reg_tbl[i].read_mask;
10564 write_mask = reg_tbl[i].write_mask;
10565
10566 /* Save the original register content */
10567 save_val = tr32(offset);
10568
10569 /* Determine the read-only value. */
10570 read_val = save_val & read_mask;
10571
10572 /* Write zero to the register, then make sure the read-only bits
10573 * are not changed and the read/write bits are all zeros.
10574 */
10575 tw32(offset, 0);
10576
10577 val = tr32(offset);
10578
10579 /* Test the read-only and read/write bits. */
10580 if (((val & read_mask) != read_val) || (val & write_mask))
10581 goto out;
10582
10583 /* Write ones to all the bits defined by RdMask and WrMask, then
10584 * make sure the read-only bits are not changed and the
10585 * read/write bits are all ones.
10586 */
10587 tw32(offset, read_mask | write_mask);
10588
10589 val = tr32(offset);
10590
10591 /* Test the read-only bits. */
10592 if ((val & read_mask) != read_val)
10593 goto out;
10594
10595 /* Test the read/write bits. */
10596 if ((val & write_mask) != write_mask)
10597 goto out;
10598
10599 tw32(offset, save_val);
10600 }
10601
10602 return 0;
10603
10604out:
Michael Chan9f88f292006-12-07 00:22:54 -080010605 if (netif_msg_hw(tp))
10606 printk(KERN_ERR PFX "Register test failed at offset %x\n",
10607 offset);
Michael Chana71116d2005-05-29 14:58:11 -070010608 tw32(offset, save_val);
10609 return -EIO;
10610}
10611
Michael Chan7942e1d2005-05-29 14:58:36 -070010612static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
10613{
Arjan van de Venf71e1302006-03-03 21:33:57 -050010614 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070010615 int i;
10616 u32 j;
10617
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020010618 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070010619 for (j = 0; j < len; j += 4) {
10620 u32 val;
10621
10622 tg3_write_mem(tp, offset + j, test_pattern[i]);
10623 tg3_read_mem(tp, offset + j, &val);
10624 if (val != test_pattern[i])
10625 return -EIO;
10626 }
10627 }
10628 return 0;
10629}
10630
10631static int tg3_test_memory(struct tg3 *tp)
10632{
10633 static struct mem_entry {
10634 u32 offset;
10635 u32 len;
10636 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080010637 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070010638 { 0x00002000, 0x1c000},
10639 { 0xffffffff, 0x00000}
10640 }, mem_tbl_5705[] = {
10641 { 0x00000100, 0x0000c},
10642 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070010643 { 0x00004000, 0x00800},
10644 { 0x00006000, 0x01000},
10645 { 0x00008000, 0x02000},
10646 { 0x00010000, 0x0e000},
10647 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080010648 }, mem_tbl_5755[] = {
10649 { 0x00000200, 0x00008},
10650 { 0x00004000, 0x00800},
10651 { 0x00006000, 0x00800},
10652 { 0x00008000, 0x02000},
10653 { 0x00010000, 0x0c000},
10654 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070010655 }, mem_tbl_5906[] = {
10656 { 0x00000200, 0x00008},
10657 { 0x00004000, 0x00400},
10658 { 0x00006000, 0x00400},
10659 { 0x00008000, 0x01000},
10660 { 0x00010000, 0x01000},
10661 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000010662 }, mem_tbl_5717[] = {
10663 { 0x00000200, 0x00008},
10664 { 0x00010000, 0x0a000},
10665 { 0x00020000, 0x13c00},
10666 { 0xffffffff, 0x00000}
10667 }, mem_tbl_57765[] = {
10668 { 0x00000200, 0x00008},
10669 { 0x00004000, 0x00800},
10670 { 0x00006000, 0x09800},
10671 { 0x00010000, 0x0a000},
10672 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070010673 };
10674 struct mem_entry *mem_tbl;
10675 int err = 0;
10676 int i;
10677
Matt Carlson8b5a6c42010-01-20 16:58:06 +000010678 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
10679 mem_tbl = mem_tbl_5717;
10680 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
10681 mem_tbl = mem_tbl_57765;
10682 else if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Matt Carlson321d32a2008-11-21 17:22:19 -080010683 mem_tbl = mem_tbl_5755;
10684 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
10685 mem_tbl = mem_tbl_5906;
10686 else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
10687 mem_tbl = mem_tbl_5705;
10688 else
Michael Chan7942e1d2005-05-29 14:58:36 -070010689 mem_tbl = mem_tbl_570x;
10690
10691 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
10692 if ((err = tg3_do_mem_test(tp, mem_tbl[i].offset,
10693 mem_tbl[i].len)) != 0)
10694 break;
10695 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010696
Michael Chan7942e1d2005-05-29 14:58:36 -070010697 return err;
10698}
10699
Michael Chan9f40dea2005-09-05 17:53:06 -070010700#define TG3_MAC_LOOPBACK 0
10701#define TG3_PHY_LOOPBACK 1
10702
10703static int tg3_run_loopback(struct tg3 *tp, int loopback_mode)
Michael Chanc76949a2005-05-29 14:58:59 -070010704{
Michael Chan9f40dea2005-09-05 17:53:06 -070010705 u32 mac_mode, rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010706 u32 desc_idx, coal_now;
Michael Chanc76949a2005-05-29 14:58:59 -070010707 struct sk_buff *skb, *rx_skb;
10708 u8 *tx_data;
10709 dma_addr_t map;
10710 int num_pkts, tx_len, rx_len, i, err;
10711 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000010712 struct tg3_napi *tnapi, *rnapi;
Matt Carlson21f581a2009-08-28 14:00:25 +000010713 struct tg3_rx_prodring_set *tpr = &tp->prodring[0];
Michael Chanc76949a2005-05-29 14:58:59 -070010714
Matt Carlson0c1d0e22009-09-01 13:16:33 +000010715 if (tp->irq_cnt > 1) {
10716 tnapi = &tp->napi[1];
10717 rnapi = &tp->napi[1];
10718 } else {
10719 tnapi = &tp->napi[0];
10720 rnapi = &tp->napi[0];
10721 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010722 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000010723
Michael Chan9f40dea2005-09-05 17:53:06 -070010724 if (loopback_mode == TG3_MAC_LOOPBACK) {
Michael Chanc94e3942005-09-27 12:12:42 -070010725 /* HW errata - mac loopback fails in some cases on 5780.
10726 * Normal traffic and PHY loopback are not affected by
10727 * errata.
10728 */
10729 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780)
10730 return 0;
10731
Michael Chan9f40dea2005-09-05 17:53:06 -070010732 mac_mode = (tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK) |
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010733 MAC_MODE_PORT_INT_LPBACK;
10734 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
10735 mac_mode |= MAC_MODE_LINK_POLARITY;
Michael Chan3f7045c2006-09-27 16:02:29 -070010736 if (tp->tg3_flags & TG3_FLAG_10_100_ONLY)
10737 mac_mode |= MAC_MODE_PORT_MODE_MII;
10738 else
10739 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chan9f40dea2005-09-05 17:53:06 -070010740 tw32(MAC_MODE, mac_mode);
10741 } else if (loopback_mode == TG3_PHY_LOOPBACK) {
Michael Chan3f7045c2006-09-27 16:02:29 -070010742 u32 val;
10743
Matt Carlson7f97a4b2009-08-25 10:10:03 +000010744 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
10745 tg3_phy_fet_toggle_apd(tp, false);
Michael Chan5d64ad32006-12-07 00:19:40 -080010746 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED100;
10747 } else
10748 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED1000;
Michael Chan3f7045c2006-09-27 16:02:29 -070010749
Matt Carlson9ef8ca92007-07-11 19:48:29 -070010750 tg3_phy_toggle_automdix(tp, 0);
10751
Michael Chan3f7045c2006-09-27 16:02:29 -070010752 tg3_writephy(tp, MII_BMCR, val);
Michael Chanc94e3942005-09-27 12:12:42 -070010753 udelay(40);
Michael Chan5d64ad32006-12-07 00:19:40 -080010754
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010755 mac_mode = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000010756 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
10757 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
10758 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x1800);
Michael Chan5d64ad32006-12-07 00:19:40 -080010759 mac_mode |= MAC_MODE_PORT_MODE_MII;
10760 } else
10761 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chanb16250e2006-09-27 16:10:14 -070010762
Michael Chanc94e3942005-09-27 12:12:42 -070010763 /* reset to prevent losing 1st rx packet intermittently */
10764 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES) {
10765 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
10766 udelay(10);
10767 tw32_f(MAC_RX_MODE, tp->rx_mode);
10768 }
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010769 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
10770 if ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5401)
10771 mac_mode &= ~MAC_MODE_LINK_POLARITY;
10772 else if ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5411)
10773 mac_mode |= MAC_MODE_LINK_POLARITY;
Michael Chanff18ff02006-03-27 23:17:27 -080010774 tg3_writephy(tp, MII_TG3_EXT_CTRL,
10775 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
10776 }
Michael Chan9f40dea2005-09-05 17:53:06 -070010777 tw32(MAC_MODE, mac_mode);
Michael Chan9f40dea2005-09-05 17:53:06 -070010778 }
10779 else
10780 return -EINVAL;
Michael Chanc76949a2005-05-29 14:58:59 -070010781
10782 err = -EIO;
10783
Michael Chanc76949a2005-05-29 14:58:59 -070010784 tx_len = 1514;
David S. Millera20e9c62006-07-31 22:38:16 -070010785 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070010786 if (!skb)
10787 return -ENOMEM;
10788
Michael Chanc76949a2005-05-29 14:58:59 -070010789 tx_data = skb_put(skb, tx_len);
10790 memcpy(tx_data, tp->dev->dev_addr, 6);
10791 memset(tx_data + 6, 0x0, 8);
10792
10793 tw32(MAC_RX_MTU_SIZE, tx_len + 4);
10794
10795 for (i = 14; i < tx_len; i++)
10796 tx_data[i] = (u8) (i & 0xff);
10797
Alexander Duyckf4188d82009-12-02 16:48:38 +000010798 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
10799 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000010800 dev_kfree_skb(skb);
10801 return -EIO;
10802 }
Michael Chanc76949a2005-05-29 14:58:59 -070010803
10804 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010805 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070010806
10807 udelay(10);
10808
Matt Carlson898a56f2009-08-28 14:02:40 +000010809 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070010810
Michael Chanc76949a2005-05-29 14:58:59 -070010811 num_pkts = 0;
10812
Alexander Duyckf4188d82009-12-02 16:48:38 +000010813 tg3_set_txd(tnapi, tnapi->tx_prod, map, tx_len, 0, 1);
Michael Chanc76949a2005-05-29 14:58:59 -070010814
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010815 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070010816 num_pkts++;
10817
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010818 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
10819 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070010820
10821 udelay(10);
10822
Matt Carlson303fc922009-11-02 14:27:34 +000010823 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
10824 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070010825 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010826 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070010827
10828 udelay(10);
10829
Matt Carlson898a56f2009-08-28 14:02:40 +000010830 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
10831 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010832 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070010833 (rx_idx == (rx_start_idx + num_pkts)))
10834 break;
10835 }
10836
Alexander Duyckf4188d82009-12-02 16:48:38 +000010837 pci_unmap_single(tp->pdev, map, tx_len, PCI_DMA_TODEVICE);
Michael Chanc76949a2005-05-29 14:58:59 -070010838 dev_kfree_skb(skb);
10839
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010840 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070010841 goto out;
10842
10843 if (rx_idx != rx_start_idx + num_pkts)
10844 goto out;
10845
Matt Carlson72334482009-08-28 14:03:01 +000010846 desc = &rnapi->rx_rcb[rx_start_idx];
Michael Chanc76949a2005-05-29 14:58:59 -070010847 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
10848 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
10849 if (opaque_key != RXD_OPAQUE_RING_STD)
10850 goto out;
10851
10852 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
10853 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
10854 goto out;
10855
10856 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) - 4;
10857 if (rx_len != tx_len)
10858 goto out;
10859
Matt Carlson21f581a2009-08-28 14:00:25 +000010860 rx_skb = tpr->rx_std_buffers[desc_idx].skb;
Michael Chanc76949a2005-05-29 14:58:59 -070010861
Matt Carlson21f581a2009-08-28 14:00:25 +000010862 map = pci_unmap_addr(&tpr->rx_std_buffers[desc_idx], mapping);
Michael Chanc76949a2005-05-29 14:58:59 -070010863 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len, PCI_DMA_FROMDEVICE);
10864
10865 for (i = 14; i < tx_len; i++) {
10866 if (*(rx_skb->data + i) != (u8) (i & 0xff))
10867 goto out;
10868 }
10869 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010870
Michael Chanc76949a2005-05-29 14:58:59 -070010871 /* tg3_free_rings will unmap and free the rx_skb */
10872out:
10873 return err;
10874}
10875
Michael Chan9f40dea2005-09-05 17:53:06 -070010876#define TG3_MAC_LOOPBACK_FAILED 1
10877#define TG3_PHY_LOOPBACK_FAILED 2
10878#define TG3_LOOPBACK_FAILED (TG3_MAC_LOOPBACK_FAILED | \
10879 TG3_PHY_LOOPBACK_FAILED)
10880
10881static int tg3_test_loopback(struct tg3 *tp)
10882{
10883 int err = 0;
Matt Carlson9936bcf2007-10-10 18:03:07 -070010884 u32 cpmuctrl = 0;
Michael Chan9f40dea2005-09-05 17:53:06 -070010885
10886 if (!netif_running(tp->dev))
10887 return TG3_LOOPBACK_FAILED;
10888
Michael Chanb9ec6c12006-07-25 16:37:27 -070010889 err = tg3_reset_hw(tp, 1);
10890 if (err)
10891 return TG3_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070010892
Matt Carlson6833c042008-11-21 17:18:59 -080010893 /* Turn off gphy autopowerdown. */
10894 if (tp->tg3_flags3 & TG3_FLG3_PHY_ENABLE_APD)
10895 tg3_phy_toggle_apd(tp, false);
10896
Matt Carlson321d32a2008-11-21 17:22:19 -080010897 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070010898 int i;
10899 u32 status;
10900
10901 tw32(TG3_CPMU_MUTEX_REQ, CPMU_MUTEX_REQ_DRIVER);
10902
10903 /* Wait for up to 40 microseconds to acquire lock. */
10904 for (i = 0; i < 4; i++) {
10905 status = tr32(TG3_CPMU_MUTEX_GNT);
10906 if (status == CPMU_MUTEX_GNT_DRIVER)
10907 break;
10908 udelay(10);
10909 }
10910
10911 if (status != CPMU_MUTEX_GNT_DRIVER)
10912 return TG3_LOOPBACK_FAILED;
10913
Matt Carlsonb2a5c192008-04-03 21:44:44 -070010914 /* Turn off link-based power management. */
Matt Carlsone8750932007-11-12 21:11:51 -080010915 cpmuctrl = tr32(TG3_CPMU_CTRL);
Matt Carlson109115e2008-05-02 16:48:59 -070010916 tw32(TG3_CPMU_CTRL,
10917 cpmuctrl & ~(CPMU_CTRL_LINK_SPEED_MODE |
10918 CPMU_CTRL_LINK_AWARE_MODE));
Matt Carlson9936bcf2007-10-10 18:03:07 -070010919 }
10920
Michael Chan9f40dea2005-09-05 17:53:06 -070010921 if (tg3_run_loopback(tp, TG3_MAC_LOOPBACK))
10922 err |= TG3_MAC_LOOPBACK_FAILED;
Matt Carlson9936bcf2007-10-10 18:03:07 -070010923
Matt Carlson321d32a2008-11-21 17:22:19 -080010924 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070010925 tw32(TG3_CPMU_CTRL, cpmuctrl);
10926
10927 /* Release the mutex */
10928 tw32(TG3_CPMU_MUTEX_GNT, CPMU_MUTEX_GNT_DRIVER);
10929 }
10930
Matt Carlsondd477002008-05-25 23:45:58 -070010931 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
10932 !(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)) {
Michael Chan9f40dea2005-09-05 17:53:06 -070010933 if (tg3_run_loopback(tp, TG3_PHY_LOOPBACK))
10934 err |= TG3_PHY_LOOPBACK_FAILED;
10935 }
10936
Matt Carlson6833c042008-11-21 17:18:59 -080010937 /* Re-enable gphy autopowerdown. */
10938 if (tp->tg3_flags3 & TG3_FLG3_PHY_ENABLE_APD)
10939 tg3_phy_toggle_apd(tp, true);
10940
Michael Chan9f40dea2005-09-05 17:53:06 -070010941 return err;
10942}
10943
Michael Chan4cafd3f2005-05-29 14:56:34 -070010944static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
10945 u64 *data)
10946{
Michael Chan566f86a2005-05-29 14:56:58 -070010947 struct tg3 *tp = netdev_priv(dev);
10948
Michael Chanbc1c7562006-03-20 17:48:03 -080010949 if (tp->link_config.phy_is_low_power)
10950 tg3_set_power_state(tp, PCI_D0);
10951
Michael Chan566f86a2005-05-29 14:56:58 -070010952 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
10953
10954 if (tg3_test_nvram(tp) != 0) {
10955 etest->flags |= ETH_TEST_FL_FAILED;
10956 data[0] = 1;
10957 }
Michael Chanca430072005-05-29 14:57:23 -070010958 if (tg3_test_link(tp) != 0) {
10959 etest->flags |= ETH_TEST_FL_FAILED;
10960 data[1] = 1;
10961 }
Michael Chana71116d2005-05-29 14:58:11 -070010962 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010963 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070010964
Michael Chanbbe832c2005-06-24 20:20:04 -070010965 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010966 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010967 tg3_netif_stop(tp);
10968 irq_sync = 1;
10969 }
10970
10971 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070010972
10973 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080010974 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070010975 tg3_halt_cpu(tp, RX_CPU_BASE);
10976 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
10977 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080010978 if (!err)
10979 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070010980
Michael Chand9ab5ad2006-03-20 22:27:35 -080010981 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)
10982 tg3_phy_reset(tp);
10983
Michael Chana71116d2005-05-29 14:58:11 -070010984 if (tg3_test_registers(tp) != 0) {
10985 etest->flags |= ETH_TEST_FL_FAILED;
10986 data[2] = 1;
10987 }
Michael Chan7942e1d2005-05-29 14:58:36 -070010988 if (tg3_test_memory(tp) != 0) {
10989 etest->flags |= ETH_TEST_FL_FAILED;
10990 data[3] = 1;
10991 }
Michael Chan9f40dea2005-09-05 17:53:06 -070010992 if ((data[4] = tg3_test_loopback(tp)) != 0)
Michael Chanc76949a2005-05-29 14:58:59 -070010993 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070010994
David S. Millerf47c11e2005-06-24 20:18:35 -070010995 tg3_full_unlock(tp);
10996
Michael Chand4bc3922005-05-29 14:59:20 -070010997 if (tg3_test_interrupt(tp) != 0) {
10998 etest->flags |= ETH_TEST_FL_FAILED;
10999 data[5] = 1;
11000 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011001
11002 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070011003
Michael Chana71116d2005-05-29 14:58:11 -070011004 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
11005 if (netif_running(dev)) {
11006 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011007 err2 = tg3_restart_hw(tp, 1);
11008 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070011009 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011010 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011011
11012 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011013
11014 if (irq_sync && !err2)
11015 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011016 }
Michael Chanbc1c7562006-03-20 17:48:03 -080011017 if (tp->link_config.phy_is_low_power)
11018 tg3_set_power_state(tp, PCI_D3hot);
11019
Michael Chan4cafd3f2005-05-29 14:56:34 -070011020}
11021
Linus Torvalds1da177e2005-04-16 15:20:36 -070011022static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
11023{
11024 struct mii_ioctl_data *data = if_mii(ifr);
11025 struct tg3 *tp = netdev_priv(dev);
11026 int err;
11027
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011028 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011029 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011030 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
11031 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011032 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
11033 return phy_mii_ioctl(phydev, data, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011034 }
11035
Linus Torvalds1da177e2005-04-16 15:20:36 -070011036 switch(cmd) {
11037 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000011038 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011039
11040 /* fallthru */
11041 case SIOCGMIIREG: {
11042 u32 mii_regval;
11043
11044 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
11045 break; /* We have no PHY */
11046
Michael Chanbc1c7562006-03-20 17:48:03 -080011047 if (tp->link_config.phy_is_low_power)
11048 return -EAGAIN;
11049
David S. Millerf47c11e2005-06-24 20:18:35 -070011050 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011051 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070011052 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011053
11054 data->val_out = mii_regval;
11055
11056 return err;
11057 }
11058
11059 case SIOCSMIIREG:
11060 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
11061 break; /* We have no PHY */
11062
Michael Chanbc1c7562006-03-20 17:48:03 -080011063 if (tp->link_config.phy_is_low_power)
11064 return -EAGAIN;
11065
David S. Millerf47c11e2005-06-24 20:18:35 -070011066 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011067 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070011068 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011069
11070 return err;
11071
11072 default:
11073 /* do nothing */
11074 break;
11075 }
11076 return -EOPNOTSUPP;
11077}
11078
11079#if TG3_VLAN_TAG_USED
11080static void tg3_vlan_rx_register(struct net_device *dev, struct vlan_group *grp)
11081{
11082 struct tg3 *tp = netdev_priv(dev);
11083
Matt Carlson844b3ee2009-02-25 14:23:56 +000011084 if (!netif_running(dev)) {
11085 tp->vlgrp = grp;
11086 return;
11087 }
11088
11089 tg3_netif_stop(tp);
Michael Chan29315e82006-06-29 20:12:30 -070011090
David S. Millerf47c11e2005-06-24 20:18:35 -070011091 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011092
11093 tp->vlgrp = grp;
11094
11095 /* Update RX_MODE_KEEP_VLAN_TAG bit in RX_MODE register. */
11096 __tg3_set_rx_mode(dev);
11097
Matt Carlson844b3ee2009-02-25 14:23:56 +000011098 tg3_netif_start(tp);
Michael Chan46966542007-07-11 19:47:19 -070011099
11100 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011101}
Linus Torvalds1da177e2005-04-16 15:20:36 -070011102#endif
11103
David S. Miller15f98502005-05-18 22:49:26 -070011104static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11105{
11106 struct tg3 *tp = netdev_priv(dev);
11107
11108 memcpy(ec, &tp->coal, sizeof(*ec));
11109 return 0;
11110}
11111
Michael Chand244c892005-07-05 14:42:33 -070011112static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11113{
11114 struct tg3 *tp = netdev_priv(dev);
11115 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
11116 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
11117
11118 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
11119 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
11120 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
11121 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
11122 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
11123 }
11124
11125 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
11126 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
11127 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
11128 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
11129 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
11130 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
11131 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
11132 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
11133 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
11134 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
11135 return -EINVAL;
11136
11137 /* No rx interrupts will be generated if both are zero */
11138 if ((ec->rx_coalesce_usecs == 0) &&
11139 (ec->rx_max_coalesced_frames == 0))
11140 return -EINVAL;
11141
11142 /* No tx interrupts will be generated if both are zero */
11143 if ((ec->tx_coalesce_usecs == 0) &&
11144 (ec->tx_max_coalesced_frames == 0))
11145 return -EINVAL;
11146
11147 /* Only copy relevant parameters, ignore all others. */
11148 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
11149 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
11150 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
11151 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
11152 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
11153 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
11154 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
11155 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
11156 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
11157
11158 if (netif_running(dev)) {
11159 tg3_full_lock(tp, 0);
11160 __tg3_set_coalesce(tp, &tp->coal);
11161 tg3_full_unlock(tp);
11162 }
11163 return 0;
11164}
11165
Jeff Garzik7282d492006-09-13 14:30:00 -040011166static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011167 .get_settings = tg3_get_settings,
11168 .set_settings = tg3_set_settings,
11169 .get_drvinfo = tg3_get_drvinfo,
11170 .get_regs_len = tg3_get_regs_len,
11171 .get_regs = tg3_get_regs,
11172 .get_wol = tg3_get_wol,
11173 .set_wol = tg3_set_wol,
11174 .get_msglevel = tg3_get_msglevel,
11175 .set_msglevel = tg3_set_msglevel,
11176 .nway_reset = tg3_nway_reset,
11177 .get_link = ethtool_op_get_link,
11178 .get_eeprom_len = tg3_get_eeprom_len,
11179 .get_eeprom = tg3_get_eeprom,
11180 .set_eeprom = tg3_set_eeprom,
11181 .get_ringparam = tg3_get_ringparam,
11182 .set_ringparam = tg3_set_ringparam,
11183 .get_pauseparam = tg3_get_pauseparam,
11184 .set_pauseparam = tg3_set_pauseparam,
11185 .get_rx_csum = tg3_get_rx_csum,
11186 .set_rx_csum = tg3_set_rx_csum,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011187 .set_tx_csum = tg3_set_tx_csum,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011188 .set_sg = ethtool_op_set_sg,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011189 .set_tso = tg3_set_tso,
Michael Chan4cafd3f2005-05-29 14:56:34 -070011190 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011191 .get_strings = tg3_get_strings,
Michael Chan4009a932005-09-05 17:52:54 -070011192 .phys_id = tg3_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011193 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070011194 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070011195 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070011196 .get_sset_count = tg3_get_sset_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011197};
11198
11199static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
11200{
Michael Chan1b277772006-03-20 22:27:48 -080011201 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011202
11203 tp->nvram_size = EEPROM_CHIP_SIZE;
11204
Matt Carlsone4f34112009-02-25 14:25:00 +000011205 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011206 return;
11207
Michael Chanb16250e2006-09-27 16:10:14 -070011208 if ((magic != TG3_EEPROM_MAGIC) &&
11209 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
11210 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011211 return;
11212
11213 /*
11214 * Size the chip by reading offsets at increasing powers of two.
11215 * When we encounter our validation signature, we know the addressing
11216 * has wrapped around, and thus have our chip size.
11217 */
Michael Chan1b277772006-03-20 22:27:48 -080011218 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011219
11220 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000011221 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011222 return;
11223
Michael Chan18201802006-03-20 22:29:15 -080011224 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011225 break;
11226
11227 cursize <<= 1;
11228 }
11229
11230 tp->nvram_size = cursize;
11231}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011232
Linus Torvalds1da177e2005-04-16 15:20:36 -070011233static void __devinit tg3_get_nvram_size(struct tg3 *tp)
11234{
11235 u32 val;
11236
Matt Carlsondf259d82009-04-20 06:57:14 +000011237 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
11238 tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080011239 return;
11240
11241 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080011242 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080011243 tg3_get_eeprom_size(tp);
11244 return;
11245 }
11246
Matt Carlson6d348f22009-02-25 14:25:52 +000011247 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011248 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000011249 /* This is confusing. We want to operate on the
11250 * 16-bit value at offset 0xf2. The tg3_nvram_read()
11251 * call will read from NVRAM and byteswap the data
11252 * according to the byteswapping settings for all
11253 * other register accesses. This ensures the data we
11254 * want will always reside in the lower 16-bits.
11255 * However, the data in NVRAM is in LE format, which
11256 * means the data from the NVRAM read will always be
11257 * opposite the endianness of the CPU. The 16-bit
11258 * byteswap then brings the data to CPU endianness.
11259 */
11260 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011261 return;
11262 }
11263 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070011264 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011265}
11266
11267static void __devinit tg3_get_nvram_info(struct tg3 *tp)
11268{
11269 u32 nvcfg1;
11270
11271 nvcfg1 = tr32(NVRAM_CFG1);
11272 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
11273 tp->tg3_flags2 |= TG3_FLG2_FLASH;
Matt Carlson8590a602009-08-28 12:29:16 +000011274 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011275 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11276 tw32(NVRAM_CFG1, nvcfg1);
11277 }
11278
Michael Chan4c987482005-09-05 17:52:38 -070011279 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750) ||
Michael Chana4e2b342005-10-26 15:46:52 -070011280 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011281 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011282 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
11283 tp->nvram_jedecnum = JEDEC_ATMEL;
11284 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
11285 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11286 break;
11287 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
11288 tp->nvram_jedecnum = JEDEC_ATMEL;
11289 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
11290 break;
11291 case FLASH_VENDOR_ATMEL_EEPROM:
11292 tp->nvram_jedecnum = JEDEC_ATMEL;
11293 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11294 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11295 break;
11296 case FLASH_VENDOR_ST:
11297 tp->nvram_jedecnum = JEDEC_ST;
11298 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
11299 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11300 break;
11301 case FLASH_VENDOR_SAIFUN:
11302 tp->nvram_jedecnum = JEDEC_SAIFUN;
11303 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
11304 break;
11305 case FLASH_VENDOR_SST_SMALL:
11306 case FLASH_VENDOR_SST_LARGE:
11307 tp->nvram_jedecnum = JEDEC_SST;
11308 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
11309 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011310 }
Matt Carlson8590a602009-08-28 12:29:16 +000011311 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011312 tp->nvram_jedecnum = JEDEC_ATMEL;
11313 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
11314 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11315 }
11316}
11317
Matt Carlsona1b950d2009-09-01 13:20:17 +000011318static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
11319{
11320 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
11321 case FLASH_5752PAGE_SIZE_256:
11322 tp->nvram_pagesize = 256;
11323 break;
11324 case FLASH_5752PAGE_SIZE_512:
11325 tp->nvram_pagesize = 512;
11326 break;
11327 case FLASH_5752PAGE_SIZE_1K:
11328 tp->nvram_pagesize = 1024;
11329 break;
11330 case FLASH_5752PAGE_SIZE_2K:
11331 tp->nvram_pagesize = 2048;
11332 break;
11333 case FLASH_5752PAGE_SIZE_4K:
11334 tp->nvram_pagesize = 4096;
11335 break;
11336 case FLASH_5752PAGE_SIZE_264:
11337 tp->nvram_pagesize = 264;
11338 break;
11339 case FLASH_5752PAGE_SIZE_528:
11340 tp->nvram_pagesize = 528;
11341 break;
11342 }
11343}
11344
Michael Chan361b4ac2005-04-21 17:11:21 -070011345static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
11346{
11347 u32 nvcfg1;
11348
11349 nvcfg1 = tr32(NVRAM_CFG1);
11350
Michael Chane6af3012005-04-21 17:12:05 -070011351 /* NVRAM protection for TPM */
11352 if (nvcfg1 & (1 << 27))
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011353 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Michael Chane6af3012005-04-21 17:12:05 -070011354
Michael Chan361b4ac2005-04-21 17:11:21 -070011355 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011356 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
11357 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
11358 tp->nvram_jedecnum = JEDEC_ATMEL;
11359 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11360 break;
11361 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11362 tp->nvram_jedecnum = JEDEC_ATMEL;
11363 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11364 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11365 break;
11366 case FLASH_5752VENDOR_ST_M45PE10:
11367 case FLASH_5752VENDOR_ST_M45PE20:
11368 case FLASH_5752VENDOR_ST_M45PE40:
11369 tp->nvram_jedecnum = JEDEC_ST;
11370 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11371 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11372 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070011373 }
11374
11375 if (tp->tg3_flags2 & TG3_FLG2_FLASH) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000011376 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000011377 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070011378 /* For eeprom, set pagesize to maximum eeprom size */
11379 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11380
11381 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11382 tw32(NVRAM_CFG1, nvcfg1);
11383 }
11384}
11385
Michael Chand3c7b882006-03-23 01:28:25 -080011386static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
11387{
Matt Carlson989a9d22007-05-05 11:51:05 -070011388 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080011389
11390 nvcfg1 = tr32(NVRAM_CFG1);
11391
11392 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070011393 if (nvcfg1 & (1 << 27)) {
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011394 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Matt Carlson989a9d22007-05-05 11:51:05 -070011395 protect = 1;
11396 }
Michael Chand3c7b882006-03-23 01:28:25 -080011397
Matt Carlson989a9d22007-05-05 11:51:05 -070011398 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11399 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011400 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11401 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11402 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11403 case FLASH_5755VENDOR_ATMEL_FLASH_5:
11404 tp->nvram_jedecnum = JEDEC_ATMEL;
11405 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11406 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11407 tp->nvram_pagesize = 264;
11408 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
11409 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
11410 tp->nvram_size = (protect ? 0x3e200 :
11411 TG3_NVRAM_SIZE_512KB);
11412 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
11413 tp->nvram_size = (protect ? 0x1f200 :
11414 TG3_NVRAM_SIZE_256KB);
11415 else
11416 tp->nvram_size = (protect ? 0x1f200 :
11417 TG3_NVRAM_SIZE_128KB);
11418 break;
11419 case FLASH_5752VENDOR_ST_M45PE10:
11420 case FLASH_5752VENDOR_ST_M45PE20:
11421 case FLASH_5752VENDOR_ST_M45PE40:
11422 tp->nvram_jedecnum = JEDEC_ST;
11423 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11424 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11425 tp->nvram_pagesize = 256;
11426 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
11427 tp->nvram_size = (protect ?
11428 TG3_NVRAM_SIZE_64KB :
11429 TG3_NVRAM_SIZE_128KB);
11430 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
11431 tp->nvram_size = (protect ?
11432 TG3_NVRAM_SIZE_64KB :
11433 TG3_NVRAM_SIZE_256KB);
11434 else
11435 tp->nvram_size = (protect ?
11436 TG3_NVRAM_SIZE_128KB :
11437 TG3_NVRAM_SIZE_512KB);
11438 break;
Michael Chand3c7b882006-03-23 01:28:25 -080011439 }
11440}
11441
Michael Chan1b277772006-03-20 22:27:48 -080011442static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
11443{
11444 u32 nvcfg1;
11445
11446 nvcfg1 = tr32(NVRAM_CFG1);
11447
11448 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011449 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
11450 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11451 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
11452 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11453 tp->nvram_jedecnum = JEDEC_ATMEL;
11454 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11455 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080011456
Matt Carlson8590a602009-08-28 12:29:16 +000011457 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11458 tw32(NVRAM_CFG1, nvcfg1);
11459 break;
11460 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11461 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11462 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11463 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11464 tp->nvram_jedecnum = JEDEC_ATMEL;
11465 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11466 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11467 tp->nvram_pagesize = 264;
11468 break;
11469 case FLASH_5752VENDOR_ST_M45PE10:
11470 case FLASH_5752VENDOR_ST_M45PE20:
11471 case FLASH_5752VENDOR_ST_M45PE40:
11472 tp->nvram_jedecnum = JEDEC_ST;
11473 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11474 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11475 tp->nvram_pagesize = 256;
11476 break;
Michael Chan1b277772006-03-20 22:27:48 -080011477 }
11478}
11479
Matt Carlson6b91fa02007-10-10 18:01:09 -070011480static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
11481{
11482 u32 nvcfg1, protect = 0;
11483
11484 nvcfg1 = tr32(NVRAM_CFG1);
11485
11486 /* NVRAM protection for TPM */
11487 if (nvcfg1 & (1 << 27)) {
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011488 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011489 protect = 1;
11490 }
11491
11492 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11493 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011494 case FLASH_5761VENDOR_ATMEL_ADB021D:
11495 case FLASH_5761VENDOR_ATMEL_ADB041D:
11496 case FLASH_5761VENDOR_ATMEL_ADB081D:
11497 case FLASH_5761VENDOR_ATMEL_ADB161D:
11498 case FLASH_5761VENDOR_ATMEL_MDB021D:
11499 case FLASH_5761VENDOR_ATMEL_MDB041D:
11500 case FLASH_5761VENDOR_ATMEL_MDB081D:
11501 case FLASH_5761VENDOR_ATMEL_MDB161D:
11502 tp->nvram_jedecnum = JEDEC_ATMEL;
11503 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11504 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11505 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
11506 tp->nvram_pagesize = 256;
11507 break;
11508 case FLASH_5761VENDOR_ST_A_M45PE20:
11509 case FLASH_5761VENDOR_ST_A_M45PE40:
11510 case FLASH_5761VENDOR_ST_A_M45PE80:
11511 case FLASH_5761VENDOR_ST_A_M45PE16:
11512 case FLASH_5761VENDOR_ST_M_M45PE20:
11513 case FLASH_5761VENDOR_ST_M_M45PE40:
11514 case FLASH_5761VENDOR_ST_M_M45PE80:
11515 case FLASH_5761VENDOR_ST_M_M45PE16:
11516 tp->nvram_jedecnum = JEDEC_ST;
11517 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11518 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11519 tp->nvram_pagesize = 256;
11520 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011521 }
11522
11523 if (protect) {
11524 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
11525 } else {
11526 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011527 case FLASH_5761VENDOR_ATMEL_ADB161D:
11528 case FLASH_5761VENDOR_ATMEL_MDB161D:
11529 case FLASH_5761VENDOR_ST_A_M45PE16:
11530 case FLASH_5761VENDOR_ST_M_M45PE16:
11531 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
11532 break;
11533 case FLASH_5761VENDOR_ATMEL_ADB081D:
11534 case FLASH_5761VENDOR_ATMEL_MDB081D:
11535 case FLASH_5761VENDOR_ST_A_M45PE80:
11536 case FLASH_5761VENDOR_ST_M_M45PE80:
11537 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
11538 break;
11539 case FLASH_5761VENDOR_ATMEL_ADB041D:
11540 case FLASH_5761VENDOR_ATMEL_MDB041D:
11541 case FLASH_5761VENDOR_ST_A_M45PE40:
11542 case FLASH_5761VENDOR_ST_M_M45PE40:
11543 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11544 break;
11545 case FLASH_5761VENDOR_ATMEL_ADB021D:
11546 case FLASH_5761VENDOR_ATMEL_MDB021D:
11547 case FLASH_5761VENDOR_ST_A_M45PE20:
11548 case FLASH_5761VENDOR_ST_M_M45PE20:
11549 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11550 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011551 }
11552 }
11553}
11554
Michael Chanb5d37722006-09-27 16:06:21 -070011555static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
11556{
11557 tp->nvram_jedecnum = JEDEC_ATMEL;
11558 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11559 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11560}
11561
Matt Carlson321d32a2008-11-21 17:22:19 -080011562static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
11563{
11564 u32 nvcfg1;
11565
11566 nvcfg1 = tr32(NVRAM_CFG1);
11567
11568 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11569 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11570 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11571 tp->nvram_jedecnum = JEDEC_ATMEL;
11572 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11573 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11574
11575 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11576 tw32(NVRAM_CFG1, nvcfg1);
11577 return;
11578 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11579 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11580 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11581 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11582 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11583 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11584 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
11585 tp->nvram_jedecnum = JEDEC_ATMEL;
11586 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11587 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11588
11589 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11590 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11591 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11592 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11593 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11594 break;
11595 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11596 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11597 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11598 break;
11599 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11600 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
11601 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11602 break;
11603 }
11604 break;
11605 case FLASH_5752VENDOR_ST_M45PE10:
11606 case FLASH_5752VENDOR_ST_M45PE20:
11607 case FLASH_5752VENDOR_ST_M45PE40:
11608 tp->nvram_jedecnum = JEDEC_ST;
11609 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11610 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11611
11612 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11613 case FLASH_5752VENDOR_ST_M45PE10:
11614 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11615 break;
11616 case FLASH_5752VENDOR_ST_M45PE20:
11617 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11618 break;
11619 case FLASH_5752VENDOR_ST_M45PE40:
11620 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11621 break;
11622 }
11623 break;
11624 default:
Matt Carlsondf259d82009-04-20 06:57:14 +000011625 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
Matt Carlson321d32a2008-11-21 17:22:19 -080011626 return;
11627 }
11628
Matt Carlsona1b950d2009-09-01 13:20:17 +000011629 tg3_nvram_get_pagesize(tp, nvcfg1);
11630 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Matt Carlson321d32a2008-11-21 17:22:19 -080011631 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011632}
11633
11634
11635static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
11636{
11637 u32 nvcfg1;
11638
11639 nvcfg1 = tr32(NVRAM_CFG1);
11640
11641 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11642 case FLASH_5717VENDOR_ATMEL_EEPROM:
11643 case FLASH_5717VENDOR_MICRO_EEPROM:
11644 tp->nvram_jedecnum = JEDEC_ATMEL;
11645 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11646 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11647
11648 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11649 tw32(NVRAM_CFG1, nvcfg1);
11650 return;
11651 case FLASH_5717VENDOR_ATMEL_MDB011D:
11652 case FLASH_5717VENDOR_ATMEL_ADB011B:
11653 case FLASH_5717VENDOR_ATMEL_ADB011D:
11654 case FLASH_5717VENDOR_ATMEL_MDB021D:
11655 case FLASH_5717VENDOR_ATMEL_ADB021B:
11656 case FLASH_5717VENDOR_ATMEL_ADB021D:
11657 case FLASH_5717VENDOR_ATMEL_45USPT:
11658 tp->nvram_jedecnum = JEDEC_ATMEL;
11659 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11660 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11661
11662 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11663 case FLASH_5717VENDOR_ATMEL_MDB021D:
11664 case FLASH_5717VENDOR_ATMEL_ADB021B:
11665 case FLASH_5717VENDOR_ATMEL_ADB021D:
11666 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11667 break;
11668 default:
11669 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11670 break;
11671 }
Matt Carlson321d32a2008-11-21 17:22:19 -080011672 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011673 case FLASH_5717VENDOR_ST_M_M25PE10:
11674 case FLASH_5717VENDOR_ST_A_M25PE10:
11675 case FLASH_5717VENDOR_ST_M_M45PE10:
11676 case FLASH_5717VENDOR_ST_A_M45PE10:
11677 case FLASH_5717VENDOR_ST_M_M25PE20:
11678 case FLASH_5717VENDOR_ST_A_M25PE20:
11679 case FLASH_5717VENDOR_ST_M_M45PE20:
11680 case FLASH_5717VENDOR_ST_A_M45PE20:
11681 case FLASH_5717VENDOR_ST_25USPT:
11682 case FLASH_5717VENDOR_ST_45USPT:
11683 tp->nvram_jedecnum = JEDEC_ST;
11684 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11685 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11686
11687 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11688 case FLASH_5717VENDOR_ST_M_M25PE20:
11689 case FLASH_5717VENDOR_ST_A_M25PE20:
11690 case FLASH_5717VENDOR_ST_M_M45PE20:
11691 case FLASH_5717VENDOR_ST_A_M45PE20:
11692 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11693 break;
11694 default:
11695 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11696 break;
11697 }
Matt Carlson321d32a2008-11-21 17:22:19 -080011698 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011699 default:
11700 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
11701 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080011702 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000011703
11704 tg3_nvram_get_pagesize(tp, nvcfg1);
11705 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
11706 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
Matt Carlson321d32a2008-11-21 17:22:19 -080011707}
11708
Linus Torvalds1da177e2005-04-16 15:20:36 -070011709/* Chips other than 5700/5701 use the NVRAM for fetching info. */
11710static void __devinit tg3_nvram_init(struct tg3 *tp)
11711{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011712 tw32_f(GRC_EEPROM_ADDR,
11713 (EEPROM_ADDR_FSM_RESET |
11714 (EEPROM_DEFAULT_CLOCK_PERIOD <<
11715 EEPROM_ADDR_CLKPERD_SHIFT)));
11716
Michael Chan9d57f012006-12-07 00:23:25 -080011717 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011718
11719 /* Enable seeprom accesses. */
11720 tw32_f(GRC_LOCAL_CTRL,
11721 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
11722 udelay(100);
11723
11724 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
11725 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
11726 tp->tg3_flags |= TG3_FLAG_NVRAM;
11727
Michael Chanec41c7d2006-01-17 02:40:55 -080011728 if (tg3_nvram_lock(tp)) {
11729 printk(KERN_WARNING PFX "%s: Cannot get nvarm lock, "
11730 "tg3_nvram_init failed.\n", tp->dev->name);
11731 return;
11732 }
Michael Chane6af3012005-04-21 17:12:05 -070011733 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011734
Matt Carlson989a9d22007-05-05 11:51:05 -070011735 tp->nvram_size = 0;
11736
Michael Chan361b4ac2005-04-21 17:11:21 -070011737 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
11738 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080011739 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
11740 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070011741 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070011742 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
11743 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080011744 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070011745 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
11746 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070011747 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
11748 tg3_get_5906_nvram_info(tp);
Matt Carlsonb703df62009-12-03 08:36:21 +000011749 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
11750 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson321d32a2008-11-21 17:22:19 -080011751 tg3_get_57780_nvram_info(tp);
Matt Carlsona1b950d2009-09-01 13:20:17 +000011752 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
11753 tg3_get_5717_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070011754 else
11755 tg3_get_nvram_info(tp);
11756
Matt Carlson989a9d22007-05-05 11:51:05 -070011757 if (tp->nvram_size == 0)
11758 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011759
Michael Chane6af3012005-04-21 17:12:05 -070011760 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080011761 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011762
11763 } else {
11764 tp->tg3_flags &= ~(TG3_FLAG_NVRAM | TG3_FLAG_NVRAM_BUFFERED);
11765
11766 tg3_get_eeprom_size(tp);
11767 }
11768}
11769
Linus Torvalds1da177e2005-04-16 15:20:36 -070011770static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
11771 u32 offset, u32 len, u8 *buf)
11772{
11773 int i, j, rc = 0;
11774 u32 val;
11775
11776 for (i = 0; i < len; i += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080011777 u32 addr;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011778 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011779
11780 addr = offset + i;
11781
11782 memcpy(&data, buf + i, 4);
11783
Matt Carlson62cedd12009-04-20 14:52:29 -070011784 /*
11785 * The SEEPROM interface expects the data to always be opposite
11786 * the native endian format. We accomplish this by reversing
11787 * all the operations that would have been performed on the
11788 * data from a call to tg3_nvram_read_be32().
11789 */
11790 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011791
11792 val = tr32(GRC_EEPROM_ADDR);
11793 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
11794
11795 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
11796 EEPROM_ADDR_READ);
11797 tw32(GRC_EEPROM_ADDR, val |
11798 (0 << EEPROM_ADDR_DEVID_SHIFT) |
11799 (addr & EEPROM_ADDR_ADDR_MASK) |
11800 EEPROM_ADDR_START |
11801 EEPROM_ADDR_WRITE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011802
Michael Chan9d57f012006-12-07 00:23:25 -080011803 for (j = 0; j < 1000; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011804 val = tr32(GRC_EEPROM_ADDR);
11805
11806 if (val & EEPROM_ADDR_COMPLETE)
11807 break;
Michael Chan9d57f012006-12-07 00:23:25 -080011808 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011809 }
11810 if (!(val & EEPROM_ADDR_COMPLETE)) {
11811 rc = -EBUSY;
11812 break;
11813 }
11814 }
11815
11816 return rc;
11817}
11818
11819/* offset and length are dword aligned */
11820static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
11821 u8 *buf)
11822{
11823 int ret = 0;
11824 u32 pagesize = tp->nvram_pagesize;
11825 u32 pagemask = pagesize - 1;
11826 u32 nvram_cmd;
11827 u8 *tmp;
11828
11829 tmp = kmalloc(pagesize, GFP_KERNEL);
11830 if (tmp == NULL)
11831 return -ENOMEM;
11832
11833 while (len) {
11834 int j;
Michael Chane6af3012005-04-21 17:12:05 -070011835 u32 phy_addr, page_off, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011836
11837 phy_addr = offset & ~pagemask;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011838
Linus Torvalds1da177e2005-04-16 15:20:36 -070011839 for (j = 0; j < pagesize; j += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000011840 ret = tg3_nvram_read_be32(tp, phy_addr + j,
11841 (__be32 *) (tmp + j));
11842 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011843 break;
11844 }
11845 if (ret)
11846 break;
11847
11848 page_off = offset & pagemask;
11849 size = pagesize;
11850 if (len < size)
11851 size = len;
11852
11853 len -= size;
11854
11855 memcpy(tmp + page_off, buf, size);
11856
11857 offset = offset + (pagesize - page_off);
11858
Michael Chane6af3012005-04-21 17:12:05 -070011859 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011860
11861 /*
11862 * Before we can erase the flash page, we need
11863 * to issue a special "write enable" command.
11864 */
11865 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
11866
11867 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
11868 break;
11869
11870 /* Erase the target page */
11871 tw32(NVRAM_ADDR, phy_addr);
11872
11873 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
11874 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
11875
11876 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
11877 break;
11878
11879 /* Issue another write enable to start the write. */
11880 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
11881
11882 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
11883 break;
11884
11885 for (j = 0; j < pagesize; j += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080011886 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011887
Al Virob9fc7dc2007-12-17 22:59:57 -080011888 data = *((__be32 *) (tmp + j));
Matt Carlsona9dc5292009-02-25 14:25:30 +000011889
Al Virob9fc7dc2007-12-17 22:59:57 -080011890 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011891
11892 tw32(NVRAM_ADDR, phy_addr + j);
11893
11894 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
11895 NVRAM_CMD_WR;
11896
11897 if (j == 0)
11898 nvram_cmd |= NVRAM_CMD_FIRST;
11899 else if (j == (pagesize - 4))
11900 nvram_cmd |= NVRAM_CMD_LAST;
11901
11902 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
11903 break;
11904 }
11905 if (ret)
11906 break;
11907 }
11908
11909 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
11910 tg3_nvram_exec_cmd(tp, nvram_cmd);
11911
11912 kfree(tmp);
11913
11914 return ret;
11915}
11916
11917/* offset and length are dword aligned */
11918static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
11919 u8 *buf)
11920{
11921 int i, ret = 0;
11922
11923 for (i = 0; i < len; i += 4, offset += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080011924 u32 page_off, phy_addr, nvram_cmd;
11925 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011926
11927 memcpy(&data, buf + i, 4);
Al Virob9fc7dc2007-12-17 22:59:57 -080011928 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011929
11930 page_off = offset % tp->nvram_pagesize;
11931
Michael Chan18201802006-03-20 22:29:15 -080011932 phy_addr = tg3_nvram_phys_addr(tp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011933
11934 tw32(NVRAM_ADDR, phy_addr);
11935
11936 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
11937
11938 if ((page_off == 0) || (i == 0))
11939 nvram_cmd |= NVRAM_CMD_FIRST;
Michael Chanf6d9a252006-04-29 19:00:24 -070011940 if (page_off == (tp->nvram_pagesize - 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011941 nvram_cmd |= NVRAM_CMD_LAST;
11942
11943 if (i == (len - 4))
11944 nvram_cmd |= NVRAM_CMD_LAST;
11945
Matt Carlson321d32a2008-11-21 17:22:19 -080011946 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
11947 !(tp->tg3_flags3 & TG3_FLG3_5755_PLUS) &&
Michael Chan4c987482005-09-05 17:52:38 -070011948 (tp->nvram_jedecnum == JEDEC_ST) &&
11949 (nvram_cmd & NVRAM_CMD_FIRST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011950
11951 if ((ret = tg3_nvram_exec_cmd(tp,
11952 NVRAM_CMD_WREN | NVRAM_CMD_GO |
11953 NVRAM_CMD_DONE)))
11954
11955 break;
11956 }
11957 if (!(tp->tg3_flags2 & TG3_FLG2_FLASH)) {
11958 /* We always do complete word writes to eeprom. */
11959 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
11960 }
11961
11962 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
11963 break;
11964 }
11965 return ret;
11966}
11967
11968/* offset and length are dword aligned */
11969static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
11970{
11971 int ret;
11972
Linus Torvalds1da177e2005-04-16 15:20:36 -070011973 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT) {
Michael Chan314fba32005-04-21 17:07:04 -070011974 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
11975 ~GRC_LCLCTRL_GPIO_OUTPUT1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011976 udelay(40);
11977 }
11978
11979 if (!(tp->tg3_flags & TG3_FLAG_NVRAM)) {
11980 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
11981 }
11982 else {
11983 u32 grc_mode;
11984
Michael Chanec41c7d2006-01-17 02:40:55 -080011985 ret = tg3_nvram_lock(tp);
11986 if (ret)
11987 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011988
Michael Chane6af3012005-04-21 17:12:05 -070011989 tg3_enable_nvram_access(tp);
11990 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011991 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011992 tw32(NVRAM_WRITE1, 0x406);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011993
11994 grc_mode = tr32(GRC_MODE);
11995 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
11996
11997 if ((tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) ||
11998 !(tp->tg3_flags2 & TG3_FLG2_FLASH)) {
11999
12000 ret = tg3_nvram_write_block_buffered(tp, offset, len,
12001 buf);
12002 }
12003 else {
12004 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
12005 buf);
12006 }
12007
12008 grc_mode = tr32(GRC_MODE);
12009 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
12010
Michael Chane6af3012005-04-21 17:12:05 -070012011 tg3_disable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012012 tg3_nvram_unlock(tp);
12013 }
12014
12015 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT) {
Michael Chan314fba32005-04-21 17:07:04 -070012016 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012017 udelay(40);
12018 }
12019
12020 return ret;
12021}
12022
12023struct subsys_tbl_ent {
12024 u16 subsys_vendor, subsys_devid;
12025 u32 phy_id;
12026};
12027
12028static struct subsys_tbl_ent subsys_id_to_phy_id[] = {
12029 /* Broadcom boards. */
12030 { PCI_VENDOR_ID_BROADCOM, 0x1644, PHY_ID_BCM5401 }, /* BCM95700A6 */
12031 { PCI_VENDOR_ID_BROADCOM, 0x0001, PHY_ID_BCM5701 }, /* BCM95701A5 */
12032 { PCI_VENDOR_ID_BROADCOM, 0x0002, PHY_ID_BCM8002 }, /* BCM95700T6 */
12033 { PCI_VENDOR_ID_BROADCOM, 0x0003, 0 }, /* BCM95700A9 */
12034 { PCI_VENDOR_ID_BROADCOM, 0x0005, PHY_ID_BCM5701 }, /* BCM95701T1 */
12035 { PCI_VENDOR_ID_BROADCOM, 0x0006, PHY_ID_BCM5701 }, /* BCM95701T8 */
12036 { PCI_VENDOR_ID_BROADCOM, 0x0007, 0 }, /* BCM95701A7 */
12037 { PCI_VENDOR_ID_BROADCOM, 0x0008, PHY_ID_BCM5701 }, /* BCM95701A10 */
12038 { PCI_VENDOR_ID_BROADCOM, 0x8008, PHY_ID_BCM5701 }, /* BCM95701A12 */
12039 { PCI_VENDOR_ID_BROADCOM, 0x0009, PHY_ID_BCM5703 }, /* BCM95703Ax1 */
12040 { PCI_VENDOR_ID_BROADCOM, 0x8009, PHY_ID_BCM5703 }, /* BCM95703Ax2 */
12041
12042 /* 3com boards. */
12043 { PCI_VENDOR_ID_3COM, 0x1000, PHY_ID_BCM5401 }, /* 3C996T */
12044 { PCI_VENDOR_ID_3COM, 0x1006, PHY_ID_BCM5701 }, /* 3C996BT */
12045 { PCI_VENDOR_ID_3COM, 0x1004, 0 }, /* 3C996SX */
12046 { PCI_VENDOR_ID_3COM, 0x1007, PHY_ID_BCM5701 }, /* 3C1000T */
12047 { PCI_VENDOR_ID_3COM, 0x1008, PHY_ID_BCM5701 }, /* 3C940BR01 */
12048
12049 /* DELL boards. */
12050 { PCI_VENDOR_ID_DELL, 0x00d1, PHY_ID_BCM5401 }, /* VIPER */
12051 { PCI_VENDOR_ID_DELL, 0x0106, PHY_ID_BCM5401 }, /* JAGUAR */
12052 { PCI_VENDOR_ID_DELL, 0x0109, PHY_ID_BCM5411 }, /* MERLOT */
12053 { PCI_VENDOR_ID_DELL, 0x010a, PHY_ID_BCM5411 }, /* SLIM_MERLOT */
12054
12055 /* Compaq boards. */
12056 { PCI_VENDOR_ID_COMPAQ, 0x007c, PHY_ID_BCM5701 }, /* BANSHEE */
12057 { PCI_VENDOR_ID_COMPAQ, 0x009a, PHY_ID_BCM5701 }, /* BANSHEE_2 */
12058 { PCI_VENDOR_ID_COMPAQ, 0x007d, 0 }, /* CHANGELING */
12059 { PCI_VENDOR_ID_COMPAQ, 0x0085, PHY_ID_BCM5701 }, /* NC7780 */
12060 { PCI_VENDOR_ID_COMPAQ, 0x0099, PHY_ID_BCM5701 }, /* NC7780_2 */
12061
12062 /* IBM boards. */
12063 { PCI_VENDOR_ID_IBM, 0x0281, 0 } /* IBM??? */
12064};
12065
12066static inline struct subsys_tbl_ent *lookup_by_subsys(struct tg3 *tp)
12067{
12068 int i;
12069
12070 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
12071 if ((subsys_id_to_phy_id[i].subsys_vendor ==
12072 tp->pdev->subsystem_vendor) &&
12073 (subsys_id_to_phy_id[i].subsys_devid ==
12074 tp->pdev->subsystem_device))
12075 return &subsys_id_to_phy_id[i];
12076 }
12077 return NULL;
12078}
12079
Michael Chan7d0c41e2005-04-21 17:06:20 -070012080static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012081{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012082 u32 val;
Michael Chancaf636c72006-03-22 01:05:31 -080012083 u16 pmcsr;
12084
12085 /* On some early chips the SRAM cannot be accessed in D3hot state,
12086 * so need make sure we're in D0.
12087 */
12088 pci_read_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, &pmcsr);
12089 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
12090 pci_write_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, pmcsr);
12091 msleep(1);
Michael Chan7d0c41e2005-04-21 17:06:20 -070012092
12093 /* Make sure register accesses (indirect or otherwise)
12094 * will function correctly.
12095 */
12096 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
12097 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012098
David S. Millerf49639e2006-06-09 11:58:36 -070012099 /* The memory arbiter has to be enabled in order for SRAM accesses
12100 * to succeed. Normally on powerup the tg3 chip firmware will make
12101 * sure it is enabled, but other entities such as system netboot
12102 * code might disable it.
12103 */
12104 val = tr32(MEMARB_MODE);
12105 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
12106
Linus Torvalds1da177e2005-04-16 15:20:36 -070012107 tp->phy_id = PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012108 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12109
Gary Zambranoa85feb82007-05-05 11:52:19 -070012110 /* Assume an onboard device and WOL capable by default. */
12111 tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT | TG3_FLAG_WOL_CAP;
David S. Miller72b845e2006-03-14 14:11:48 -080012112
Michael Chanb5d37722006-09-27 16:06:21 -070012113 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080012114 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Michael Chanb5d37722006-09-27 16:06:21 -070012115 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080012116 tp->tg3_flags2 |= TG3_FLG2_IS_NIC;
12117 }
Matt Carlson0527ba32007-10-10 18:03:30 -070012118 val = tr32(VCPU_CFGSHDW);
12119 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Matt Carlson8ed5d972007-05-07 00:25:49 -070012120 tp->tg3_flags |= TG3_FLAG_ASPM_WORKAROUND;
Matt Carlson0527ba32007-10-10 18:03:30 -070012121 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Matt Carlson20232762008-12-21 20:18:56 -080012122 (val & VCPU_CFGSHDW_WOL_MAGPKT))
Matt Carlson0527ba32007-10-10 18:03:30 -070012123 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012124 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070012125 }
12126
Linus Torvalds1da177e2005-04-16 15:20:36 -070012127 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
12128 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
12129 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070012130 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012131 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012132
12133 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
12134 tp->nic_sram_data_cfg = nic_cfg;
12135
12136 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
12137 ver >>= NIC_SRAM_DATA_VER_SHIFT;
12138 if ((GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700) &&
12139 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) &&
12140 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703) &&
12141 (ver > 0) && (ver < 0x100))
12142 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
12143
Matt Carlsona9daf362008-05-25 23:49:44 -070012144 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
12145 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
12146
Linus Torvalds1da177e2005-04-16 15:20:36 -070012147 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
12148 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
12149 eeprom_phy_serdes = 1;
12150
12151 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
12152 if (nic_phy_id != 0) {
12153 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
12154 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
12155
12156 eeprom_phy_id = (id1 >> 16) << 10;
12157 eeprom_phy_id |= (id2 & 0xfc00) << 16;
12158 eeprom_phy_id |= (id2 & 0x03ff) << 0;
12159 } else
12160 eeprom_phy_id = 0;
12161
Michael Chan7d0c41e2005-04-21 17:06:20 -070012162 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070012163 if (eeprom_phy_serdes) {
Michael Chana4e2b342005-10-26 15:46:52 -070012164 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
Michael Chan747e8f82005-07-25 12:33:22 -070012165 tp->tg3_flags2 |= TG3_FLG2_MII_SERDES;
12166 else
12167 tp->tg3_flags2 |= TG3_FLG2_PHY_SERDES;
12168 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070012169
John W. Linvillecbf46852005-04-21 17:01:29 -070012170 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012171 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
12172 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070012173 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070012174 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
12175
12176 switch (led_cfg) {
12177 default:
12178 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
12179 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12180 break;
12181
12182 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
12183 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12184 break;
12185
12186 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
12187 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070012188
12189 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
12190 * read on some older 5700/5701 bootcode.
12191 */
12192 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
12193 ASIC_REV_5700 ||
12194 GET_ASIC_REV(tp->pci_chip_rev_id) ==
12195 ASIC_REV_5701)
12196 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12197
Linus Torvalds1da177e2005-04-16 15:20:36 -070012198 break;
12199
12200 case SHASTA_EXT_LED_SHARED:
12201 tp->led_ctrl = LED_CTRL_MODE_SHARED;
12202 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
12203 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
12204 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12205 LED_CTRL_MODE_PHY_2);
12206 break;
12207
12208 case SHASTA_EXT_LED_MAC:
12209 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
12210 break;
12211
12212 case SHASTA_EXT_LED_COMBO:
12213 tp->led_ctrl = LED_CTRL_MODE_COMBO;
12214 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
12215 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12216 LED_CTRL_MODE_PHY_2);
12217 break;
12218
Stephen Hemminger855e1112008-04-16 16:37:28 -070012219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012220
12221 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
12222 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
12223 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
12224 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12225
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012226 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
12227 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080012228
Michael Chan9d26e212006-12-07 00:21:14 -080012229 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012230 tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080012231 if ((tp->pdev->subsystem_vendor ==
12232 PCI_VENDOR_ID_ARIMA) &&
12233 (tp->pdev->subsystem_device == 0x205a ||
12234 tp->pdev->subsystem_device == 0x2063))
12235 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
12236 } else {
David S. Millerf49639e2006-06-09 11:58:36 -070012237 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080012238 tp->tg3_flags2 |= TG3_FLG2_IS_NIC;
12239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012240
12241 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
12242 tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
John W. Linvillecbf46852005-04-21 17:01:29 -070012243 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012244 tp->tg3_flags2 |= TG3_FLG2_ASF_NEW_HANDSHAKE;
12245 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012246
12247 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
12248 (tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
Matt Carlson0d3031d2007-10-10 18:02:43 -070012249 tp->tg3_flags3 |= TG3_FLG3_ENABLE_APE;
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012250
Gary Zambranoa85feb82007-05-05 11:52:19 -070012251 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES &&
12252 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
12253 tp->tg3_flags &= ~TG3_FLAG_WOL_CAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012254
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070012255 if ((tp->tg3_flags & TG3_FLAG_WOL_CAP) &&
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012256 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE))
Matt Carlson0527ba32007-10-10 18:03:30 -070012257 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
12258
Linus Torvalds1da177e2005-04-16 15:20:36 -070012259 if (cfg2 & (1 << 17))
12260 tp->tg3_flags2 |= TG3_FLG2_CAPACITIVE_COUPLING;
12261
12262 /* serdes signal pre-emphasis in register 0x590 set by */
12263 /* bootcode if bit 18 is set */
12264 if (cfg2 & (1 << 18))
12265 tp->tg3_flags2 |= TG3_FLG2_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070012266
Matt Carlson321d32a2008-11-21 17:22:19 -080012267 if (((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
12268 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080012269 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
12270 tp->tg3_flags3 |= TG3_FLG3_PHY_ENABLE_APD;
12271
Matt Carlson8ed5d972007-05-07 00:25:49 -070012272 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
12273 u32 cfg3;
12274
12275 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
12276 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
12277 tp->tg3_flags |= TG3_FLAG_ASPM_WORKAROUND;
12278 }
Matt Carlsona9daf362008-05-25 23:49:44 -070012279
12280 if (cfg4 & NIC_SRAM_RGMII_STD_IBND_DISABLE)
12281 tp->tg3_flags3 |= TG3_FLG3_RGMII_STD_IBND_DISABLE;
12282 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
12283 tp->tg3_flags3 |= TG3_FLG3_RGMII_EXT_IBND_RX_EN;
12284 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
12285 tp->tg3_flags3 |= TG3_FLG3_RGMII_EXT_IBND_TX_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012286 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012287done:
12288 device_init_wakeup(&tp->pdev->dev, tp->tg3_flags & TG3_FLAG_WOL_CAP);
12289 device_set_wakeup_enable(&tp->pdev->dev,
12290 tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
Michael Chan7d0c41e2005-04-21 17:06:20 -070012291}
12292
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012293static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
12294{
12295 int i;
12296 u32 val;
12297
12298 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
12299 tw32(OTP_CTRL, cmd);
12300
12301 /* Wait for up to 1 ms for command to execute. */
12302 for (i = 0; i < 100; i++) {
12303 val = tr32(OTP_STATUS);
12304 if (val & OTP_STATUS_CMD_DONE)
12305 break;
12306 udelay(10);
12307 }
12308
12309 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
12310}
12311
12312/* Read the gphy configuration from the OTP region of the chip. The gphy
12313 * configuration is a 32-bit value that straddles the alignment boundary.
12314 * We do two 32-bit reads and then shift and merge the results.
12315 */
12316static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
12317{
12318 u32 bhalf_otp, thalf_otp;
12319
12320 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
12321
12322 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
12323 return 0;
12324
12325 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
12326
12327 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12328 return 0;
12329
12330 thalf_otp = tr32(OTP_READ_DATA);
12331
12332 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
12333
12334 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12335 return 0;
12336
12337 bhalf_otp = tr32(OTP_READ_DATA);
12338
12339 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
12340}
12341
Michael Chan7d0c41e2005-04-21 17:06:20 -070012342static int __devinit tg3_phy_probe(struct tg3 *tp)
12343{
12344 u32 hw_phy_id_1, hw_phy_id_2;
12345 u32 hw_phy_id, hw_phy_id_masked;
12346 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012347
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012348 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)
12349 return tg3_phy_init(tp);
12350
Linus Torvalds1da177e2005-04-16 15:20:36 -070012351 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010012352 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012353 */
12354 err = 0;
Matt Carlson0d3031d2007-10-10 18:02:43 -070012355 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
12356 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012357 hw_phy_id = hw_phy_id_masked = PHY_ID_INVALID;
12358 } else {
12359 /* Now read the physical PHY_ID from the chip and verify
12360 * that it is sane. If it doesn't look good, we fall back
12361 * to either the hard-coded table based PHY_ID and failing
12362 * that the value found in the eeprom area.
12363 */
12364 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
12365 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
12366
12367 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
12368 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
12369 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
12370
12371 hw_phy_id_masked = hw_phy_id & PHY_ID_MASK;
12372 }
12373
12374 if (!err && KNOWN_PHY_ID(hw_phy_id_masked)) {
12375 tp->phy_id = hw_phy_id;
12376 if (hw_phy_id_masked == PHY_ID_BCM8002)
12377 tp->tg3_flags2 |= TG3_FLG2_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070012378 else
12379 tp->tg3_flags2 &= ~TG3_FLG2_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012380 } else {
Michael Chan7d0c41e2005-04-21 17:06:20 -070012381 if (tp->phy_id != PHY_ID_INVALID) {
12382 /* Do nothing, phy ID already set up in
12383 * tg3_get_eeprom_hw_cfg().
12384 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070012385 } else {
12386 struct subsys_tbl_ent *p;
12387
12388 /* No eeprom signature? Try the hardcoded
12389 * subsys device table.
12390 */
12391 p = lookup_by_subsys(tp);
12392 if (!p)
12393 return -ENODEV;
12394
12395 tp->phy_id = p->phy_id;
12396 if (!tp->phy_id ||
12397 tp->phy_id == PHY_ID_BCM8002)
12398 tp->tg3_flags2 |= TG3_FLG2_PHY_SERDES;
12399 }
12400 }
12401
Michael Chan747e8f82005-07-25 12:33:22 -070012402 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES) &&
Matt Carlson0d3031d2007-10-10 18:02:43 -070012403 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070012404 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chan3600d912006-12-07 00:21:48 -080012405 u32 bmsr, adv_reg, tg3_ctrl, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012406
12407 tg3_readphy(tp, MII_BMSR, &bmsr);
12408 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
12409 (bmsr & BMSR_LSTATUS))
12410 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012411
Linus Torvalds1da177e2005-04-16 15:20:36 -070012412 err = tg3_phy_reset(tp);
12413 if (err)
12414 return err;
12415
12416 adv_reg = (ADVERTISE_10HALF | ADVERTISE_10FULL |
12417 ADVERTISE_100HALF | ADVERTISE_100FULL |
12418 ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
12419 tg3_ctrl = 0;
12420 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY)) {
12421 tg3_ctrl = (MII_TG3_CTRL_ADV_1000_HALF |
12422 MII_TG3_CTRL_ADV_1000_FULL);
12423 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
12424 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
12425 tg3_ctrl |= (MII_TG3_CTRL_AS_MASTER |
12426 MII_TG3_CTRL_ENABLE_AS_MASTER);
12427 }
12428
Michael Chan3600d912006-12-07 00:21:48 -080012429 mask = (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
12430 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
12431 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full);
12432 if (!tg3_copper_is_advertising_all(tp, mask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012433 tg3_writephy(tp, MII_ADVERTISE, adv_reg);
12434
12435 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY))
12436 tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
12437
12438 tg3_writephy(tp, MII_BMCR,
12439 BMCR_ANENABLE | BMCR_ANRESTART);
12440 }
12441 tg3_phy_set_wirespeed(tp);
12442
12443 tg3_writephy(tp, MII_ADVERTISE, adv_reg);
12444 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY))
12445 tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
12446 }
12447
12448skip_phy_reset:
12449 if ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5401) {
12450 err = tg3_init_5401phy_dsp(tp);
12451 if (err)
12452 return err;
12453 }
12454
12455 if (!err && ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5401)) {
12456 err = tg3_init_5401phy_dsp(tp);
12457 }
12458
Michael Chan747e8f82005-07-25 12:33:22 -070012459 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012460 tp->link_config.advertising =
12461 (ADVERTISED_1000baseT_Half |
12462 ADVERTISED_1000baseT_Full |
12463 ADVERTISED_Autoneg |
12464 ADVERTISED_FIBRE);
12465 if (tp->tg3_flags & TG3_FLAG_10_100_ONLY)
12466 tp->link_config.advertising &=
12467 ~(ADVERTISED_1000baseT_Half |
12468 ADVERTISED_1000baseT_Full);
12469
12470 return err;
12471}
12472
12473static void __devinit tg3_read_partno(struct tg3 *tp)
12474{
Matt Carlson141518c2009-12-03 08:36:22 +000012475 unsigned char vpd_data[TG3_NVM_VPD_LEN]; /* in little-endian format */
Michael Chanaf2c6a42006-11-07 14:57:51 -080012476 unsigned int i;
Michael Chan1b277772006-03-20 22:27:48 -080012477 u32 magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012478
Matt Carlsondf259d82009-04-20 06:57:14 +000012479 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
12480 tg3_nvram_read(tp, 0x0, &magic))
David S. Millerf49639e2006-06-09 11:58:36 -070012481 goto out_not_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012482
Michael Chan18201802006-03-20 22:29:15 -080012483 if (magic == TG3_EEPROM_MAGIC) {
Matt Carlson141518c2009-12-03 08:36:22 +000012484 for (i = 0; i < TG3_NVM_VPD_LEN; i += 4) {
Michael Chan1b277772006-03-20 22:27:48 -080012485 u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012486
Matt Carlson6d348f22009-02-25 14:25:52 +000012487 /* The data is in little-endian format in NVRAM.
12488 * Use the big-endian read routines to preserve
12489 * the byte order as it exists in NVRAM.
12490 */
Matt Carlson141518c2009-12-03 08:36:22 +000012491 if (tg3_nvram_read_be32(tp, TG3_NVM_VPD_OFF + i, &tmp))
Michael Chan1b277772006-03-20 22:27:48 -080012492 goto out_not_found;
12493
Matt Carlson6d348f22009-02-25 14:25:52 +000012494 memcpy(&vpd_data[i], &tmp, sizeof(tmp));
Michael Chan1b277772006-03-20 22:27:48 -080012495 }
12496 } else {
Matt Carlson94c982b2009-12-03 08:36:23 +000012497 ssize_t cnt;
12498 unsigned int pos = 0, i = 0;
Michael Chan1b277772006-03-20 22:27:48 -080012499
Matt Carlson94c982b2009-12-03 08:36:23 +000012500 for (; pos < TG3_NVM_VPD_LEN && i < 3; i++, pos += cnt) {
12501 cnt = pci_read_vpd(tp->pdev, pos,
12502 TG3_NVM_VPD_LEN - pos,
12503 &vpd_data[pos]);
12504 if (cnt == -ETIMEDOUT || -EINTR)
12505 cnt = 0;
12506 else if (cnt < 0)
David S. Millerf49639e2006-06-09 11:58:36 -070012507 goto out_not_found;
Michael Chan1b277772006-03-20 22:27:48 -080012508 }
Matt Carlson94c982b2009-12-03 08:36:23 +000012509 if (pos != TG3_NVM_VPD_LEN)
12510 goto out_not_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012511 }
12512
12513 /* Now parse and find the part number. */
Matt Carlson141518c2009-12-03 08:36:22 +000012514 for (i = 0; i < TG3_NVM_VPD_LEN - 2; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012515 unsigned char val = vpd_data[i];
Michael Chanaf2c6a42006-11-07 14:57:51 -080012516 unsigned int block_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012517
12518 if (val == 0x82 || val == 0x91) {
12519 i = (i + 3 +
12520 (vpd_data[i + 1] +
12521 (vpd_data[i + 2] << 8)));
12522 continue;
12523 }
12524
12525 if (val != 0x90)
12526 goto out_not_found;
12527
12528 block_end = (i + 3 +
12529 (vpd_data[i + 1] +
12530 (vpd_data[i + 2] << 8)));
12531 i += 3;
Michael Chanaf2c6a42006-11-07 14:57:51 -080012532
Matt Carlson141518c2009-12-03 08:36:22 +000012533 if (block_end > TG3_NVM_VPD_LEN)
Michael Chanaf2c6a42006-11-07 14:57:51 -080012534 goto out_not_found;
12535
12536 while (i < (block_end - 2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012537 if (vpd_data[i + 0] == 'P' &&
12538 vpd_data[i + 1] == 'N') {
12539 int partno_len = vpd_data[i + 2];
12540
Michael Chanaf2c6a42006-11-07 14:57:51 -080012541 i += 3;
Matt Carlson141518c2009-12-03 08:36:22 +000012542 if (partno_len > TG3_BPN_SIZE ||
12543 (partno_len + i) > TG3_NVM_VPD_LEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012544 goto out_not_found;
12545
12546 memcpy(tp->board_part_number,
Michael Chanaf2c6a42006-11-07 14:57:51 -080012547 &vpd_data[i], partno_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012548
12549 /* Success. */
12550 return;
12551 }
Michael Chanaf2c6a42006-11-07 14:57:51 -080012552 i += 3 + vpd_data[i + 2];
Linus Torvalds1da177e2005-04-16 15:20:36 -070012553 }
12554
12555 /* Part number not found. */
12556 goto out_not_found;
12557 }
12558
12559out_not_found:
Michael Chanb5d37722006-09-27 16:06:21 -070012560 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12561 strcpy(tp->board_part_number, "BCM95906");
Matt Carlsondf259d82009-04-20 06:57:14 +000012562 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12563 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
12564 strcpy(tp->board_part_number, "BCM57780");
12565 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12566 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
12567 strcpy(tp->board_part_number, "BCM57760");
12568 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12569 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
12570 strcpy(tp->board_part_number, "BCM57790");
Matt Carlson5e7ccf22009-08-25 10:08:42 +000012571 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12572 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
12573 strcpy(tp->board_part_number, "BCM57788");
Matt Carlsonb703df62009-12-03 08:36:21 +000012574 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
12575 strcpy(tp->board_part_number, "BCM57765");
Michael Chanb5d37722006-09-27 16:06:21 -070012576 else
12577 strcpy(tp->board_part_number, "none");
Linus Torvalds1da177e2005-04-16 15:20:36 -070012578}
12579
Matt Carlson9c8a6202007-10-21 16:16:08 -070012580static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
12581{
12582 u32 val;
12583
Matt Carlsone4f34112009-02-25 14:25:00 +000012584 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070012585 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000012586 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070012587 val != 0)
12588 return 0;
12589
12590 return 1;
12591}
12592
Matt Carlsonacd9c112009-02-25 14:26:33 +000012593static void __devinit tg3_read_bc_ver(struct tg3 *tp)
12594{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012595 u32 val, offset, start, ver_offset;
Matt Carlsonacd9c112009-02-25 14:26:33 +000012596 int i;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012597 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000012598
12599 if (tg3_nvram_read(tp, 0xc, &offset) ||
12600 tg3_nvram_read(tp, 0x4, &start))
12601 return;
12602
12603 offset = tg3_nvram_logical_addr(tp, offset);
12604
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012605 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000012606 return;
12607
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012608 if ((val & 0xfc000000) == 0x0c000000) {
12609 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000012610 return;
12611
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012612 if (val == 0)
12613 newver = true;
12614 }
12615
12616 if (newver) {
12617 if (tg3_nvram_read(tp, offset + 8, &ver_offset))
12618 return;
12619
12620 offset = offset + ver_offset - start;
12621 for (i = 0; i < 16; i += 4) {
12622 __be32 v;
12623 if (tg3_nvram_read_be32(tp, offset + i, &v))
12624 return;
12625
12626 memcpy(tp->fw_ver + i, &v, sizeof(v));
12627 }
12628 } else {
12629 u32 major, minor;
12630
12631 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
12632 return;
12633
12634 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
12635 TG3_NVM_BCVER_MAJSFT;
12636 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
12637 snprintf(&tp->fw_ver[0], 32, "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000012638 }
12639}
12640
Matt Carlsona6f6cb12009-02-25 14:27:43 +000012641static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
12642{
12643 u32 val, major, minor;
12644
12645 /* Use native endian representation */
12646 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
12647 return;
12648
12649 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
12650 TG3_NVM_HWSB_CFG1_MAJSFT;
12651 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
12652 TG3_NVM_HWSB_CFG1_MINSFT;
12653
12654 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
12655}
12656
Matt Carlsondfe00d72008-11-21 17:19:41 -080012657static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
12658{
12659 u32 offset, major, minor, build;
12660
12661 tp->fw_ver[0] = 's';
12662 tp->fw_ver[1] = 'b';
12663 tp->fw_ver[2] = '\0';
12664
12665 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
12666 return;
12667
12668 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
12669 case TG3_EEPROM_SB_REVISION_0:
12670 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
12671 break;
12672 case TG3_EEPROM_SB_REVISION_2:
12673 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
12674 break;
12675 case TG3_EEPROM_SB_REVISION_3:
12676 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
12677 break;
12678 default:
12679 return;
12680 }
12681
Matt Carlsone4f34112009-02-25 14:25:00 +000012682 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080012683 return;
12684
12685 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
12686 TG3_EEPROM_SB_EDH_BLD_SHFT;
12687 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
12688 TG3_EEPROM_SB_EDH_MAJ_SHFT;
12689 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
12690
12691 if (minor > 99 || build > 26)
12692 return;
12693
12694 snprintf(&tp->fw_ver[2], 30, " v%d.%02d", major, minor);
12695
12696 if (build > 0) {
12697 tp->fw_ver[8] = 'a' + build - 1;
12698 tp->fw_ver[9] = '\0';
12699 }
12700}
12701
Matt Carlsonacd9c112009-02-25 14:26:33 +000012702static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080012703{
12704 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000012705 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070012706
12707 for (offset = TG3_NVM_DIR_START;
12708 offset < TG3_NVM_DIR_END;
12709 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000012710 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012711 return;
12712
12713 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
12714 break;
12715 }
12716
12717 if (offset == TG3_NVM_DIR_END)
12718 return;
12719
12720 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
12721 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000012722 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012723 return;
12724
Matt Carlsone4f34112009-02-25 14:25:00 +000012725 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070012726 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000012727 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012728 return;
12729
12730 offset += val - start;
12731
Matt Carlsonacd9c112009-02-25 14:26:33 +000012732 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012733
Matt Carlsonacd9c112009-02-25 14:26:33 +000012734 tp->fw_ver[vlen++] = ',';
12735 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070012736
12737 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012738 __be32 v;
12739 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012740 return;
12741
Al Virob9fc7dc2007-12-17 22:59:57 -080012742 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012743
Matt Carlsonacd9c112009-02-25 14:26:33 +000012744 if (vlen > TG3_VER_SIZE - sizeof(v)) {
12745 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012746 break;
12747 }
12748
Matt Carlsonacd9c112009-02-25 14:26:33 +000012749 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
12750 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012751 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000012752}
12753
Matt Carlson7fd76442009-02-25 14:27:20 +000012754static void __devinit tg3_read_dash_ver(struct tg3 *tp)
12755{
12756 int vlen;
12757 u32 apedata;
12758
12759 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) ||
12760 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
12761 return;
12762
12763 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
12764 if (apedata != APE_SEG_SIG_MAGIC)
12765 return;
12766
12767 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
12768 if (!(apedata & APE_FW_STATUS_READY))
12769 return;
12770
12771 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
12772
12773 vlen = strlen(tp->fw_ver);
12774
12775 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " DASH v%d.%d.%d.%d",
12776 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
12777 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
12778 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
12779 (apedata & APE_FW_VERSION_BLDMSK));
12780}
12781
Matt Carlsonacd9c112009-02-25 14:26:33 +000012782static void __devinit tg3_read_fw_ver(struct tg3 *tp)
12783{
12784 u32 val;
12785
Matt Carlsondf259d82009-04-20 06:57:14 +000012786 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) {
12787 tp->fw_ver[0] = 's';
12788 tp->fw_ver[1] = 'b';
12789 tp->fw_ver[2] = '\0';
12790
12791 return;
12792 }
12793
Matt Carlsonacd9c112009-02-25 14:26:33 +000012794 if (tg3_nvram_read(tp, 0, &val))
12795 return;
12796
12797 if (val == TG3_EEPROM_MAGIC)
12798 tg3_read_bc_ver(tp);
12799 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
12800 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000012801 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
12802 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000012803 else
12804 return;
12805
12806 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
12807 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
12808 return;
12809
12810 tg3_read_mgmtfw_ver(tp);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012811
12812 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080012813}
12814
Michael Chan7544b092007-05-05 13:08:32 -070012815static struct pci_dev * __devinit tg3_find_peer(struct tg3 *);
12816
Linus Torvalds1da177e2005-04-16 15:20:36 -070012817static int __devinit tg3_get_invariants(struct tg3 *tp)
12818{
12819 static struct pci_device_id write_reorder_chipsets[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012820 { PCI_DEVICE(PCI_VENDOR_ID_AMD,
12821 PCI_DEVICE_ID_AMD_FE_GATE_700C) },
John W. Linvillec165b002006-07-08 13:28:53 -070012822 { PCI_DEVICE(PCI_VENDOR_ID_AMD,
12823 PCI_DEVICE_ID_AMD_8131_BRIDGE) },
Michael Chan399de502005-10-03 14:02:39 -070012824 { PCI_DEVICE(PCI_VENDOR_ID_VIA,
12825 PCI_DEVICE_ID_VIA_8385_0) },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012826 { },
12827 };
12828 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012829 u32 pci_state_reg, grc_misc_cfg;
12830 u32 val;
12831 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080012832 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012833
Linus Torvalds1da177e2005-04-16 15:20:36 -070012834 /* Force memory write invalidate off. If we leave it on,
12835 * then on 5700_BX chips we have to enable a workaround.
12836 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
12837 * to match the cacheline size. The Broadcom driver have this
12838 * workaround but turns MWI off all the times so never uses
12839 * it. This seems to suggest that the workaround is insufficient.
12840 */
12841 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
12842 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
12843 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
12844
12845 /* It is absolutely critical that TG3PCI_MISC_HOST_CTRL
12846 * has the register indirect write enable bit set before
12847 * we try to access any of the MMIO registers. It is also
12848 * critical that the PCI-X hw workaround situation is decided
12849 * before that as well.
12850 */
12851 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
12852 &misc_ctrl_reg);
12853
12854 tp->pci_chip_rev_id = (misc_ctrl_reg >>
12855 MISC_HOST_CTRL_CHIPREV_SHIFT);
Matt Carlson795d01c2007-10-07 23:28:17 -070012856 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
12857 u32 prod_id_asic_rev;
12858
Matt Carlson5001e2f2009-11-13 13:03:51 +000012859 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
12860 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
12861 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5724)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012862 pci_read_config_dword(tp->pdev,
12863 TG3PCI_GEN2_PRODID_ASICREV,
12864 &prod_id_asic_rev);
Matt Carlsonb703df62009-12-03 08:36:21 +000012865 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
12866 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
12867 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
12868 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
12869 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
12870 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
12871 pci_read_config_dword(tp->pdev,
12872 TG3PCI_GEN15_PRODID_ASICREV,
12873 &prod_id_asic_rev);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012874 else
12875 pci_read_config_dword(tp->pdev, TG3PCI_PRODID_ASICREV,
12876 &prod_id_asic_rev);
12877
Matt Carlson321d32a2008-11-21 17:22:19 -080012878 tp->pci_chip_rev_id = prod_id_asic_rev;
Matt Carlson795d01c2007-10-07 23:28:17 -070012879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012880
Michael Chanff645be2005-04-21 17:09:53 -070012881 /* Wrong chip ID in 5752 A0. This code can be removed later
12882 * as A0 is not in production.
12883 */
12884 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
12885 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
12886
Michael Chan68929142005-08-09 20:17:14 -070012887 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
12888 * we need to disable memory and use config. cycles
12889 * only to access all registers. The 5702/03 chips
12890 * can mistakenly decode the special cycles from the
12891 * ICH chipsets as memory write cycles, causing corruption
12892 * of register and memory space. Only certain ICH bridges
12893 * will drive special cycles with non-zero data during the
12894 * address phase which can fall within the 5703's address
12895 * range. This is not an ICH bug as the PCI spec allows
12896 * non-zero address during special cycles. However, only
12897 * these ICH bridges are known to drive non-zero addresses
12898 * during special cycles.
12899 *
12900 * Since special cycles do not cross PCI bridges, we only
12901 * enable this workaround if the 5703 is on the secondary
12902 * bus of these ICH bridges.
12903 */
12904 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
12905 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
12906 static struct tg3_dev_id {
12907 u32 vendor;
12908 u32 device;
12909 u32 rev;
12910 } ich_chipsets[] = {
12911 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
12912 PCI_ANY_ID },
12913 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
12914 PCI_ANY_ID },
12915 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
12916 0xa },
12917 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
12918 PCI_ANY_ID },
12919 { },
12920 };
12921 struct tg3_dev_id *pci_id = &ich_chipsets[0];
12922 struct pci_dev *bridge = NULL;
12923
12924 while (pci_id->vendor != 0) {
12925 bridge = pci_get_device(pci_id->vendor, pci_id->device,
12926 bridge);
12927 if (!bridge) {
12928 pci_id++;
12929 continue;
12930 }
12931 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070012932 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070012933 continue;
12934 }
12935 if (bridge->subordinate &&
12936 (bridge->subordinate->number ==
12937 tp->pdev->bus->number)) {
12938
12939 tp->tg3_flags2 |= TG3_FLG2_ICH_WORKAROUND;
12940 pci_dev_put(bridge);
12941 break;
12942 }
12943 }
12944 }
12945
Matt Carlson41588ba2008-04-19 18:12:33 -070012946 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
12947 static struct tg3_dev_id {
12948 u32 vendor;
12949 u32 device;
12950 } bridge_chipsets[] = {
12951 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
12952 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
12953 { },
12954 };
12955 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
12956 struct pci_dev *bridge = NULL;
12957
12958 while (pci_id->vendor != 0) {
12959 bridge = pci_get_device(pci_id->vendor,
12960 pci_id->device,
12961 bridge);
12962 if (!bridge) {
12963 pci_id++;
12964 continue;
12965 }
12966 if (bridge->subordinate &&
12967 (bridge->subordinate->number <=
12968 tp->pdev->bus->number) &&
12969 (bridge->subordinate->subordinate >=
12970 tp->pdev->bus->number)) {
12971 tp->tg3_flags3 |= TG3_FLG3_5701_DMA_BUG;
12972 pci_dev_put(bridge);
12973 break;
12974 }
12975 }
12976 }
12977
Michael Chan4a29cc22006-03-19 13:21:12 -080012978 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
12979 * DMA addresses > 40-bit. This bridge may have other additional
12980 * 57xx devices behind it in some 4-port NIC designs for example.
12981 * Any tg3 device found behind the bridge will also need the 40-bit
12982 * DMA workaround.
12983 */
Michael Chana4e2b342005-10-26 15:46:52 -070012984 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
12985 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
12986 tp->tg3_flags2 |= TG3_FLG2_5780_CLASS;
Michael Chan4a29cc22006-03-19 13:21:12 -080012987 tp->tg3_flags |= TG3_FLAG_40BIT_DMA_BUG;
Michael Chan4cf78e42005-07-25 12:29:19 -070012988 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Michael Chana4e2b342005-10-26 15:46:52 -070012989 }
Michael Chan4a29cc22006-03-19 13:21:12 -080012990 else {
12991 struct pci_dev *bridge = NULL;
12992
12993 do {
12994 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
12995 PCI_DEVICE_ID_SERVERWORKS_EPB,
12996 bridge);
12997 if (bridge && bridge->subordinate &&
12998 (bridge->subordinate->number <=
12999 tp->pdev->bus->number) &&
13000 (bridge->subordinate->subordinate >=
13001 tp->pdev->bus->number)) {
13002 tp->tg3_flags |= TG3_FLAG_40BIT_DMA_BUG;
13003 pci_dev_put(bridge);
13004 break;
13005 }
13006 } while (bridge);
13007 }
Michael Chan4cf78e42005-07-25 12:29:19 -070013008
Linus Torvalds1da177e2005-04-16 15:20:36 -070013009 /* Initialize misc host control in PCI block. */
13010 tp->misc_host_ctrl |= (misc_ctrl_reg &
13011 MISC_HOST_CTRL_CHIPREV);
13012 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13013 tp->misc_host_ctrl);
13014
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013015 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
13016 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
13017 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Michael Chan7544b092007-05-05 13:08:32 -070013018 tp->pdev_peer = tg3_find_peer(tp);
13019
Matt Carlson321d32a2008-11-21 17:22:19 -080013020 /* Intentionally exclude ASIC_REV_5906 */
13021 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Michael Chand9ab5ad2006-03-20 22:27:35 -080013022 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013023 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013024 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013025 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013026 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlsonb703df62009-12-03 08:36:21 +000013027 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13028 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson321d32a2008-11-21 17:22:19 -080013029 tp->tg3_flags3 |= TG3_FLG3_5755_PLUS;
13030
13031 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13032 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Michael Chanb5d37722006-09-27 16:06:21 -070013033 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013034 (tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
Michael Chana4e2b342005-10-26 15:46:52 -070013035 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
John W. Linville6708e5c2005-04-21 17:00:52 -070013036 tp->tg3_flags2 |= TG3_FLG2_5750_PLUS;
13037
John W. Linville1b440c562005-04-21 17:03:18 -070013038 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) ||
13039 (tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
13040 tp->tg3_flags2 |= TG3_FLG2_5705_PLUS;
13041
Matt Carlson027455a2008-12-21 20:19:30 -080013042 /* 5700 B0 chips do not support checksumming correctly due
13043 * to hardware bugs.
13044 */
13045 if (tp->pci_chip_rev_id == CHIPREV_ID_5700_B0)
13046 tp->tg3_flags |= TG3_FLAG_BROKEN_CHECKSUMS;
13047 else {
13048 tp->tg3_flags |= TG3_FLAG_RX_CHECKSUMS;
13049 tp->dev->features |= NETIF_F_IP_CSUM | NETIF_F_SG;
13050 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
13051 tp->dev->features |= NETIF_F_IPV6_CSUM;
13052 }
13053
Matt Carlson507399f2009-11-13 13:03:37 +000013054 /* Determine TSO capabilities */
Matt Carlsonb703df62009-12-03 08:36:21 +000013055 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13056 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlsone849cdc2009-11-13 13:03:38 +000013057 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_3;
13058 else if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
13059 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlson507399f2009-11-13 13:03:37 +000013060 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_2;
13061 else if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
13062 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_1 | TG3_FLG2_TSO_BUG;
13063 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
13064 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
13065 tp->tg3_flags2 &= ~TG3_FLG2_TSO_BUG;
13066 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13067 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13068 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
13069 tp->tg3_flags2 |= TG3_FLG2_TSO_BUG;
13070 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
13071 tp->fw_needed = FIRMWARE_TG3TSO5;
13072 else
13073 tp->fw_needed = FIRMWARE_TG3TSO;
13074 }
13075
13076 tp->irq_max = 1;
13077
Michael Chan5a6f3072006-03-20 22:28:05 -080013078 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
Michael Chan7544b092007-05-05 13:08:32 -070013079 tp->tg3_flags |= TG3_FLAG_SUPPORT_MSI;
13080 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
13081 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
13082 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
13083 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
13084 tp->pdev_peer == tp->pdev))
13085 tp->tg3_flags &= ~TG3_FLAG_SUPPORT_MSI;
13086
Matt Carlson321d32a2008-11-21 17:22:19 -080013087 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070013088 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanfcfa0a32006-03-20 22:28:41 -080013089 tp->tg3_flags2 |= TG3_FLG2_1SHOT_MSI;
Michael Chan52c0fd82006-06-29 20:15:54 -070013090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013091
Matt Carlsonb703df62009-12-03 08:36:21 +000013092 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13093 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
Matt Carlson507399f2009-11-13 13:03:37 +000013094 tp->tg3_flags |= TG3_FLAG_SUPPORT_MSIX;
13095 tp->irq_max = TG3_IRQ_MAX_VECS;
13096 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013097 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000013098
Matt Carlson615774f2009-11-13 13:03:39 +000013099 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13100 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
13101 tp->tg3_flags3 |= TG3_FLG3_SHORT_DMA_BUG;
13102 else if (!(tp->tg3_flags3 & TG3_FLG3_5755_PLUS)) {
13103 tp->tg3_flags3 |= TG3_FLG3_4G_DMA_BNDRY_BUG;
13104 tp->tg3_flags3 |= TG3_FLG3_40BIT_DMA_LIMIT_BUG;
Matt Carlson0e1406d2009-11-02 12:33:33 +000013105 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013106
Matt Carlsonb703df62009-12-03 08:36:21 +000013107 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13108 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
13109 tp->tg3_flags3 |= TG3_FLG3_USE_JUMBO_BDFLAG;
13110
Matt Carlsonf51f3562008-05-25 23:45:08 -070013111 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013112 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
Matt Carlsonb703df62009-12-03 08:36:21 +000013113 (tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG))
Matt Carlson8f666b02009-08-28 13:58:24 +000013114 tp->tg3_flags |= TG3_FLAG_JUMBO_CAPABLE;
Michael Chan0f893dc2005-07-25 12:30:38 -070013115
Matt Carlson52f44902008-11-21 17:17:04 -080013116 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
13117 &pci_state_reg);
13118
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013119 tp->pcie_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_EXP);
13120 if (tp->pcie_cap != 0) {
13121 u16 lnkctl;
13122
Linus Torvalds1da177e2005-04-16 15:20:36 -070013123 tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS;
Matt Carlson5f5c51e2007-11-12 21:19:37 -080013124
13125 pcie_set_readrq(tp->pdev, 4096);
13126
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013127 pci_read_config_word(tp->pdev,
13128 tp->pcie_cap + PCI_EXP_LNKCTL,
13129 &lnkctl);
13130 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
13131 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanc7835a72006-11-15 21:14:42 -080013132 tp->tg3_flags2 &= ~TG3_FLG2_HW_TSO_2;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013133 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013134 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000013135 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
13136 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013137 tp->tg3_flags3 |= TG3_FLG3_CLKREQ_BUG;
Matt Carlson614b0592010-01-20 16:58:02 +000013138 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
13139 tp->tg3_flags3 |= TG3_FLG3_L1PLLPD_EN;
Michael Chanc7835a72006-11-15 21:14:42 -080013140 }
Matt Carlson52f44902008-11-21 17:17:04 -080013141 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Matt Carlsonfcb389d2008-11-03 16:55:44 -080013142 tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS;
Matt Carlson52f44902008-11-21 17:17:04 -080013143 } else if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
13144 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
13145 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
13146 if (!tp->pcix_cap) {
13147 printk(KERN_ERR PFX "Cannot find PCI-X "
13148 "capability, aborting.\n");
13149 return -EIO;
13150 }
13151
13152 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
13153 tp->tg3_flags |= TG3_FLAG_PCIX_MODE;
13154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013155
Michael Chan399de502005-10-03 14:02:39 -070013156 /* If we have an AMD 762 or VIA K8T800 chipset, write
13157 * reordering to the mailbox registers done by the host
13158 * controller can cause major troubles. We read back from
13159 * every mailbox register write to force the writes to be
13160 * posted to the chip in order.
13161 */
13162 if (pci_dev_present(write_reorder_chipsets) &&
13163 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
13164 tp->tg3_flags |= TG3_FLAG_MBOX_WRITE_REORDER;
13165
Matt Carlson69fc4052008-12-21 20:19:57 -080013166 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
13167 &tp->pci_cacheline_sz);
13168 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13169 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013170 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
13171 tp->pci_lat_timer < 64) {
13172 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080013173 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13174 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013175 }
13176
Matt Carlson52f44902008-11-21 17:17:04 -080013177 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
13178 /* 5700 BX chips need to have their TX producer index
13179 * mailboxes written twice to workaround a bug.
13180 */
13181 tp->tg3_flags |= TG3_FLAG_TXD_MBOX_HWBUG;
Matt Carlson9974a352007-10-07 23:27:28 -070013182
Matt Carlson52f44902008-11-21 17:17:04 -080013183 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013184 *
13185 * The workaround is to use indirect register accesses
13186 * for all chip writes not to mailbox registers.
13187 */
Matt Carlson52f44902008-11-21 17:17:04 -080013188 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013189 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013190
13191 tp->tg3_flags |= TG3_FLAG_PCIX_TARGET_HWBUG;
13192
13193 /* The chip can have it's power management PCI config
13194 * space registers clobbered due to this bug.
13195 * So explicitly force the chip into D0 here.
13196 */
Matt Carlson9974a352007-10-07 23:27:28 -070013197 pci_read_config_dword(tp->pdev,
13198 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013199 &pm_reg);
13200 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
13201 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070013202 pci_write_config_dword(tp->pdev,
13203 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013204 pm_reg);
13205
13206 /* Also, force SERR#/PERR# in PCI command. */
13207 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13208 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
13209 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13210 }
13211 }
13212
Linus Torvalds1da177e2005-04-16 15:20:36 -070013213 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
13214 tp->tg3_flags |= TG3_FLAG_PCI_HIGH_SPEED;
13215 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
13216 tp->tg3_flags |= TG3_FLAG_PCI_32BIT;
13217
13218 /* Chip-specific fixup from Broadcom driver */
13219 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
13220 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
13221 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
13222 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
13223 }
13224
Michael Chan1ee582d2005-08-09 20:16:46 -070013225 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070013226 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013227 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070013228 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070013229 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013230 tp->write32_tx_mbox = tg3_write32;
13231 tp->write32_rx_mbox = tg3_write32;
13232
13233 /* Various workaround register access methods */
13234 if (tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG)
13235 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013236 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
13237 ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
13238 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
13239 /*
13240 * Back to back register writes can cause problems on these
13241 * chips, the workaround is to read back all reg writes
13242 * except those to mailbox regs.
13243 *
13244 * See tg3_write_indirect_reg32().
13245 */
Michael Chan1ee582d2005-08-09 20:16:46 -070013246 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013247 }
13248
Michael Chan1ee582d2005-08-09 20:16:46 -070013249 if ((tp->tg3_flags & TG3_FLAG_TXD_MBOX_HWBUG) ||
13250 (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)) {
13251 tp->write32_tx_mbox = tg3_write32_tx_mbox;
13252 if (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)
13253 tp->write32_rx_mbox = tg3_write_flush_reg32;
13254 }
Michael Chan20094932005-08-09 20:16:32 -070013255
Michael Chan68929142005-08-09 20:17:14 -070013256 if (tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND) {
13257 tp->read32 = tg3_read_indirect_reg32;
13258 tp->write32 = tg3_write_indirect_reg32;
13259 tp->read32_mbox = tg3_read_indirect_mbox;
13260 tp->write32_mbox = tg3_write_indirect_mbox;
13261 tp->write32_tx_mbox = tg3_write_indirect_mbox;
13262 tp->write32_rx_mbox = tg3_write_indirect_mbox;
13263
13264 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070013265 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070013266
13267 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13268 pci_cmd &= ~PCI_COMMAND_MEMORY;
13269 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13270 }
Michael Chanb5d37722006-09-27 16:06:21 -070013271 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
13272 tp->read32_mbox = tg3_read32_mbox_5906;
13273 tp->write32_mbox = tg3_write32_mbox_5906;
13274 tp->write32_tx_mbox = tg3_write32_mbox_5906;
13275 tp->write32_rx_mbox = tg3_write32_mbox_5906;
13276 }
Michael Chan68929142005-08-09 20:17:14 -070013277
Michael Chanbbadf502006-04-06 21:46:34 -070013278 if (tp->write32 == tg3_write_indirect_reg32 ||
13279 ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) &&
13280 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070013281 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Michael Chanbbadf502006-04-06 21:46:34 -070013282 tp->tg3_flags |= TG3_FLAG_SRAM_USE_CONFIG;
13283
Michael Chan7d0c41e2005-04-21 17:06:20 -070013284 /* Get eeprom hw config before calling tg3_set_power_state().
Michael Chan9d26e212006-12-07 00:21:14 -080013285 * In particular, the TG3_FLG2_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070013286 * determined before calling tg3_set_power_state() so that
13287 * we know whether or not to switch out of Vaux power.
13288 * When the flag is set, it means that GPIO1 is used for eeprom
13289 * write protect and also implies that it is a LOM where GPIOs
13290 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013291 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070013292 tg3_get_eeprom_hw_cfg(tp);
13293
Matt Carlson0d3031d2007-10-10 18:02:43 -070013294 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
13295 /* Allow reads and writes to the
13296 * APE register and memory space.
13297 */
13298 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
13299 PCISTATE_ALLOW_APE_SHMEM_WR;
13300 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
13301 pci_state_reg);
13302 }
13303
Matt Carlson9936bcf2007-10-10 18:03:07 -070013304 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013305 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013306 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013307 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlsonb703df62009-12-03 08:36:21 +000013308 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13309 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlsond30cdd22007-10-07 23:28:35 -070013310 tp->tg3_flags |= TG3_FLAG_CPMU_PRESENT;
13311
Michael Chan314fba32005-04-21 17:07:04 -070013312 /* Set up tp->grc_local_ctrl before calling tg3_set_power_state().
13313 * GPIO1 driven high will bring 5700's external PHY out of reset.
13314 * It is also used as eeprom write protect on LOMs.
13315 */
13316 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
13317 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
13318 (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT))
13319 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
13320 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070013321 /* Unused GPIO3 must be driven as output on 5752 because there
13322 * are no pull-up resistors on unused GPIO pins.
13323 */
13324 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
13325 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070013326
Matt Carlson321d32a2008-11-21 17:22:19 -080013327 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
13328 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Michael Chanaf36e6b2006-03-23 01:28:06 -080013329 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
13330
Matt Carlson8d519ab2009-04-20 06:58:01 +000013331 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
13332 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070013333 /* Turn off the debug UART. */
13334 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
13335 if (tp->tg3_flags2 & TG3_FLG2_IS_NIC)
13336 /* Keep VMain power. */
13337 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
13338 GRC_LCLCTRL_GPIO_OUTPUT0;
13339 }
13340
Linus Torvalds1da177e2005-04-16 15:20:36 -070013341 /* Force the chip into D0. */
Michael Chanbc1c7562006-03-20 17:48:03 -080013342 err = tg3_set_power_state(tp, PCI_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013343 if (err) {
13344 printk(KERN_ERR PFX "(%s) transition to D0 failed\n",
13345 pci_name(tp->pdev));
13346 return err;
13347 }
13348
Linus Torvalds1da177e2005-04-16 15:20:36 -070013349 /* Derive initial jumbo mode from MTU assigned in
13350 * ether_setup() via the alloc_etherdev() call
13351 */
Michael Chan0f893dc2005-07-25 12:30:38 -070013352 if (tp->dev->mtu > ETH_DATA_LEN &&
Michael Chana4e2b342005-10-26 15:46:52 -070013353 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Michael Chan0f893dc2005-07-25 12:30:38 -070013354 tp->tg3_flags |= TG3_FLAG_JUMBO_RING_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013355
13356 /* Determine WakeOnLan speed to use. */
13357 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13358 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
13359 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
13360 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
13361 tp->tg3_flags &= ~(TG3_FLAG_WOL_SPEED_100MB);
13362 } else {
13363 tp->tg3_flags |= TG3_FLAG_WOL_SPEED_100MB;
13364 }
13365
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013366 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
13367 tp->tg3_flags3 |= TG3_FLG3_PHY_IS_FET;
13368
Linus Torvalds1da177e2005-04-16 15:20:36 -070013369 /* A few boards don't want Ethernet@WireSpeed phy feature */
13370 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
13371 ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
13372 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070013373 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013374 (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) ||
Michael Chan747e8f82005-07-25 12:33:22 -070013375 (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013376 tp->tg3_flags2 |= TG3_FLG2_NO_ETH_WIRE_SPEED;
13377
13378 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
13379 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
13380 tp->tg3_flags2 |= TG3_FLG2_PHY_ADC_BUG;
13381 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
13382 tp->tg3_flags2 |= TG3_FLG2_PHY_5704_A0_BUG;
13383
Matt Carlson321d32a2008-11-21 17:22:19 -080013384 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013385 !(tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080013386 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013387 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
Matt Carlsonb703df62009-12-03 08:36:21 +000013388 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
13389 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57765) {
Michael Chanc424cb22006-04-29 18:56:34 -070013390 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013391 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013392 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
13393 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080013394 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
13395 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
13396 tp->tg3_flags2 |= TG3_FLG2_PHY_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080013397 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
13398 tp->tg3_flags2 |= TG3_FLG2_PHY_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080013399 } else
Michael Chanc424cb22006-04-29 18:56:34 -070013400 tp->tg3_flags2 |= TG3_FLG2_PHY_BER_BUG;
13401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013402
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013403 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
13404 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
13405 tp->phy_otp = tg3_read_otp_phycfg(tp);
13406 if (tp->phy_otp == 0)
13407 tp->phy_otp = TG3_OTP_DEFAULT;
13408 }
13409
Matt Carlsonf51f3562008-05-25 23:45:08 -070013410 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT)
Matt Carlson8ef21422008-05-02 16:47:53 -070013411 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
13412 else
13413 tp->mi_mode = MAC_MI_MODE_BASE;
13414
Linus Torvalds1da177e2005-04-16 15:20:36 -070013415 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013416 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
13417 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
13418 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
13419
Matt Carlson321d32a2008-11-21 17:22:19 -080013420 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
13421 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson57e69832008-05-25 23:48:31 -070013422 tp->tg3_flags3 |= TG3_FLG3_USE_PHYLIB;
13423
Matt Carlson158d7ab2008-05-29 01:37:54 -070013424 err = tg3_mdio_init(tp);
13425 if (err)
13426 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013427
13428 /* Initialize data/descriptor byte/word swapping. */
13429 val = tr32(GRC_MODE);
13430 val &= GRC_MODE_HOST_STACKUP;
13431 tw32(GRC_MODE, val | tp->grc_mode);
13432
13433 tg3_switch_clocks(tp);
13434
13435 /* Clear this out for sanity. */
13436 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
13437
13438 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
13439 &pci_state_reg);
13440 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
13441 (tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG) == 0) {
13442 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
13443
13444 if (chiprevid == CHIPREV_ID_5701_A0 ||
13445 chiprevid == CHIPREV_ID_5701_B0 ||
13446 chiprevid == CHIPREV_ID_5701_B2 ||
13447 chiprevid == CHIPREV_ID_5701_B5) {
13448 void __iomem *sram_base;
13449
13450 /* Write some dummy words into the SRAM status block
13451 * area, see if it reads back correctly. If the return
13452 * value is bad, force enable the PCIX workaround.
13453 */
13454 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
13455
13456 writel(0x00000000, sram_base);
13457 writel(0x00000000, sram_base + 4);
13458 writel(0xffffffff, sram_base + 4);
13459 if (readl(sram_base) != 0x00000000)
13460 tp->tg3_flags |= TG3_FLAG_PCIX_TARGET_HWBUG;
13461 }
13462 }
13463
13464 udelay(50);
13465 tg3_nvram_init(tp);
13466
13467 grc_misc_cfg = tr32(GRC_MISC_CFG);
13468 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
13469
Linus Torvalds1da177e2005-04-16 15:20:36 -070013470 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
13471 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
13472 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
13473 tp->tg3_flags2 |= TG3_FLG2_IS_5788;
13474
David S. Millerfac9b832005-05-18 22:46:34 -070013475 if (!(tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
13476 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700))
13477 tp->tg3_flags |= TG3_FLAG_TAGGED_STATUS;
13478 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) {
13479 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
13480 HOSTCC_MODE_CLRTICK_TXBD);
13481
13482 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
13483 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13484 tp->misc_host_ctrl);
13485 }
13486
Matt Carlson3bda1252008-08-15 14:08:22 -070013487 /* Preserve the APE MAC_MODE bits */
13488 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
13489 tp->mac_mode = tr32(MAC_MODE) |
13490 MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
13491 else
13492 tp->mac_mode = TG3_DEF_MAC_MODE;
13493
Linus Torvalds1da177e2005-04-16 15:20:36 -070013494 /* these are limited to 10/100 only */
13495 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
13496 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
13497 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
13498 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
13499 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
13500 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
13501 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
13502 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
13503 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
Michael Chan676917d2006-12-07 00:20:22 -080013504 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
13505 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013506 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013507 (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013508 tp->tg3_flags |= TG3_FLAG_10_100_ONLY;
13509
13510 err = tg3_phy_probe(tp);
13511 if (err) {
13512 printk(KERN_ERR PFX "(%s) phy probe failed, err %d\n",
13513 pci_name(tp->pdev), err);
13514 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013515 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013516 }
13517
13518 tg3_read_partno(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080013519 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013520
13521 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
13522 tp->tg3_flags &= ~TG3_FLAG_USE_MI_INTERRUPT;
13523 } else {
13524 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
13525 tp->tg3_flags |= TG3_FLAG_USE_MI_INTERRUPT;
13526 else
13527 tp->tg3_flags &= ~TG3_FLAG_USE_MI_INTERRUPT;
13528 }
13529
13530 /* 5700 {AX,BX} chips have a broken status block link
13531 * change bit implementation, so we must use the
13532 * status register in those cases.
13533 */
13534 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
13535 tp->tg3_flags |= TG3_FLAG_USE_LINKCHG_REG;
13536 else
13537 tp->tg3_flags &= ~TG3_FLAG_USE_LINKCHG_REG;
13538
13539 /* The led_ctrl is set during tg3_phy_probe, here we might
13540 * have to force the link status polling mechanism based
13541 * upon subsystem IDs.
13542 */
13543 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070013544 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070013545 !(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)) {
13546 tp->tg3_flags |= (TG3_FLAG_USE_MI_INTERRUPT |
13547 TG3_FLAG_USE_LINKCHG_REG);
13548 }
13549
13550 /* For all SERDES we poll the MAC status register. */
13551 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
13552 tp->tg3_flags |= TG3_FLAG_POLL_SERDES;
13553 else
13554 tp->tg3_flags &= ~TG3_FLAG_POLL_SERDES;
13555
Matt Carlsonad829262008-11-21 17:16:16 -080013556 tp->rx_offset = NET_IP_ALIGN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013557 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
13558 (tp->tg3_flags & TG3_FLAG_PCIX_MODE) != 0)
13559 tp->rx_offset = 0;
13560
Michael Chanf92905d2006-06-29 20:14:29 -070013561 tp->rx_std_max_post = TG3_RX_RING_SIZE;
13562
13563 /* Increment the rx prod index on the rx std ring by at most
13564 * 8 for these chips to workaround hw errata.
13565 */
13566 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13567 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
13568 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
13569 tp->rx_std_max_post = 8;
13570
Matt Carlson8ed5d972007-05-07 00:25:49 -070013571 if (tp->tg3_flags & TG3_FLAG_ASPM_WORKAROUND)
13572 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
13573 PCIE_PWR_MGMT_L1_THRESH_MSK;
13574
Linus Torvalds1da177e2005-04-16 15:20:36 -070013575 return err;
13576}
13577
David S. Miller49b6e95f2007-03-29 01:38:42 -070013578#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070013579static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
13580{
13581 struct net_device *dev = tp->dev;
13582 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070013583 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070013584 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070013585 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013586
David S. Miller49b6e95f2007-03-29 01:38:42 -070013587 addr = of_get_property(dp, "local-mac-address", &len);
13588 if (addr && len == 6) {
13589 memcpy(dev->dev_addr, addr, 6);
13590 memcpy(dev->perm_addr, dev->dev_addr, 6);
13591 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013592 }
13593 return -ENODEV;
13594}
13595
13596static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
13597{
13598 struct net_device *dev = tp->dev;
13599
13600 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070013601 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013602 return 0;
13603}
13604#endif
13605
13606static int __devinit tg3_get_device_address(struct tg3 *tp)
13607{
13608 struct net_device *dev = tp->dev;
13609 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080013610 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013611
David S. Miller49b6e95f2007-03-29 01:38:42 -070013612#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070013613 if (!tg3_get_macaddr_sparc(tp))
13614 return 0;
13615#endif
13616
13617 mac_offset = 0x7c;
David S. Millerf49639e2006-06-09 11:58:36 -070013618 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) ||
Michael Chana4e2b342005-10-26 15:46:52 -070013619 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013620 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
13621 mac_offset = 0xcc;
13622 if (tg3_nvram_lock(tp))
13623 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
13624 else
13625 tg3_nvram_unlock(tp);
Matt Carlsona1b950d2009-09-01 13:20:17 +000013626 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
13627 if (tr32(TG3_CPMU_STATUS) & TG3_CPMU_STATUS_PCIE_FUNC)
13628 mac_offset = 0xcc;
13629 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070013630 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013631
13632 /* First try to get it from MAC address mailbox. */
13633 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
13634 if ((hi >> 16) == 0x484b) {
13635 dev->dev_addr[0] = (hi >> 8) & 0xff;
13636 dev->dev_addr[1] = (hi >> 0) & 0xff;
13637
13638 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
13639 dev->dev_addr[2] = (lo >> 24) & 0xff;
13640 dev->dev_addr[3] = (lo >> 16) & 0xff;
13641 dev->dev_addr[4] = (lo >> 8) & 0xff;
13642 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013643
Michael Chan008652b2006-03-27 23:14:53 -080013644 /* Some old bootcode may report a 0 MAC address in SRAM */
13645 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
13646 }
13647 if (!addr_ok) {
13648 /* Next, try NVRAM. */
Matt Carlsondf259d82009-04-20 06:57:14 +000013649 if (!(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) &&
13650 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000013651 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070013652 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
13653 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080013654 }
13655 /* Finally just fetch it out of the MAC control regs. */
13656 else {
13657 hi = tr32(MAC_ADDR_0_HIGH);
13658 lo = tr32(MAC_ADDR_0_LOW);
13659
13660 dev->dev_addr[5] = lo & 0xff;
13661 dev->dev_addr[4] = (lo >> 8) & 0xff;
13662 dev->dev_addr[3] = (lo >> 16) & 0xff;
13663 dev->dev_addr[2] = (lo >> 24) & 0xff;
13664 dev->dev_addr[1] = hi & 0xff;
13665 dev->dev_addr[0] = (hi >> 8) & 0xff;
13666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013667 }
13668
13669 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070013670#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070013671 if (!tg3_get_default_macaddr_sparc(tp))
13672 return 0;
13673#endif
13674 return -EINVAL;
13675 }
John W. Linville2ff43692005-09-12 14:44:20 -070013676 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013677 return 0;
13678}
13679
David S. Miller59e6b432005-05-18 22:50:10 -070013680#define BOUNDARY_SINGLE_CACHELINE 1
13681#define BOUNDARY_MULTI_CACHELINE 2
13682
13683static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
13684{
13685 int cacheline_size;
13686 u8 byte;
13687 int goal;
13688
13689 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
13690 if (byte == 0)
13691 cacheline_size = 1024;
13692 else
13693 cacheline_size = (int) byte * 4;
13694
13695 /* On 5703 and later chips, the boundary bits have no
13696 * effect.
13697 */
13698 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13699 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13700 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
13701 goto out;
13702
13703#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
13704 goal = BOUNDARY_MULTI_CACHELINE;
13705#else
13706#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
13707 goal = BOUNDARY_SINGLE_CACHELINE;
13708#else
13709 goal = 0;
13710#endif
13711#endif
13712
Matt Carlsonb703df62009-12-03 08:36:21 +000013713 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13714 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000013715 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
13716 goto out;
13717 }
13718
David S. Miller59e6b432005-05-18 22:50:10 -070013719 if (!goal)
13720 goto out;
13721
13722 /* PCI controllers on most RISC systems tend to disconnect
13723 * when a device tries to burst across a cache-line boundary.
13724 * Therefore, letting tg3 do so just wastes PCI bandwidth.
13725 *
13726 * Unfortunately, for PCI-E there are only limited
13727 * write-side controls for this, and thus for reads
13728 * we will still get the disconnects. We'll also waste
13729 * these PCI cycles for both read and write for chips
13730 * other than 5700 and 5701 which do not implement the
13731 * boundary bits.
13732 */
13733 if ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) &&
13734 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)) {
13735 switch (cacheline_size) {
13736 case 16:
13737 case 32:
13738 case 64:
13739 case 128:
13740 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13741 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
13742 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
13743 } else {
13744 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
13745 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
13746 }
13747 break;
13748
13749 case 256:
13750 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
13751 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
13752 break;
13753
13754 default:
13755 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
13756 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
13757 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070013758 }
David S. Miller59e6b432005-05-18 22:50:10 -070013759 } else if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
13760 switch (cacheline_size) {
13761 case 16:
13762 case 32:
13763 case 64:
13764 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13765 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
13766 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
13767 break;
13768 }
13769 /* fallthrough */
13770 case 128:
13771 default:
13772 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
13773 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
13774 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070013775 }
David S. Miller59e6b432005-05-18 22:50:10 -070013776 } else {
13777 switch (cacheline_size) {
13778 case 16:
13779 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13780 val |= (DMA_RWCTRL_READ_BNDRY_16 |
13781 DMA_RWCTRL_WRITE_BNDRY_16);
13782 break;
13783 }
13784 /* fallthrough */
13785 case 32:
13786 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13787 val |= (DMA_RWCTRL_READ_BNDRY_32 |
13788 DMA_RWCTRL_WRITE_BNDRY_32);
13789 break;
13790 }
13791 /* fallthrough */
13792 case 64:
13793 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13794 val |= (DMA_RWCTRL_READ_BNDRY_64 |
13795 DMA_RWCTRL_WRITE_BNDRY_64);
13796 break;
13797 }
13798 /* fallthrough */
13799 case 128:
13800 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13801 val |= (DMA_RWCTRL_READ_BNDRY_128 |
13802 DMA_RWCTRL_WRITE_BNDRY_128);
13803 break;
13804 }
13805 /* fallthrough */
13806 case 256:
13807 val |= (DMA_RWCTRL_READ_BNDRY_256 |
13808 DMA_RWCTRL_WRITE_BNDRY_256);
13809 break;
13810 case 512:
13811 val |= (DMA_RWCTRL_READ_BNDRY_512 |
13812 DMA_RWCTRL_WRITE_BNDRY_512);
13813 break;
13814 case 1024:
13815 default:
13816 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
13817 DMA_RWCTRL_WRITE_BNDRY_1024);
13818 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070013819 }
David S. Miller59e6b432005-05-18 22:50:10 -070013820 }
13821
13822out:
13823 return val;
13824}
13825
Linus Torvalds1da177e2005-04-16 15:20:36 -070013826static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
13827{
13828 struct tg3_internal_buffer_desc test_desc;
13829 u32 sram_dma_descs;
13830 int i, ret;
13831
13832 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
13833
13834 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
13835 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
13836 tw32(RDMAC_STATUS, 0);
13837 tw32(WDMAC_STATUS, 0);
13838
13839 tw32(BUFMGR_MODE, 0);
13840 tw32(FTQ_RESET, 0);
13841
13842 test_desc.addr_hi = ((u64) buf_dma) >> 32;
13843 test_desc.addr_lo = buf_dma & 0xffffffff;
13844 test_desc.nic_mbuf = 0x00002100;
13845 test_desc.len = size;
13846
13847 /*
13848 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
13849 * the *second* time the tg3 driver was getting loaded after an
13850 * initial scan.
13851 *
13852 * Broadcom tells me:
13853 * ...the DMA engine is connected to the GRC block and a DMA
13854 * reset may affect the GRC block in some unpredictable way...
13855 * The behavior of resets to individual blocks has not been tested.
13856 *
13857 * Broadcom noted the GRC reset will also reset all sub-components.
13858 */
13859 if (to_device) {
13860 test_desc.cqid_sqid = (13 << 8) | 2;
13861
13862 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
13863 udelay(40);
13864 } else {
13865 test_desc.cqid_sqid = (16 << 8) | 7;
13866
13867 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
13868 udelay(40);
13869 }
13870 test_desc.flags = 0x00000005;
13871
13872 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
13873 u32 val;
13874
13875 val = *(((u32 *)&test_desc) + i);
13876 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
13877 sram_dma_descs + (i * sizeof(u32)));
13878 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
13879 }
13880 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
13881
13882 if (to_device) {
13883 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
13884 } else {
13885 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
13886 }
13887
13888 ret = -ENODEV;
13889 for (i = 0; i < 40; i++) {
13890 u32 val;
13891
13892 if (to_device)
13893 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
13894 else
13895 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
13896 if ((val & 0xffff) == sram_dma_descs) {
13897 ret = 0;
13898 break;
13899 }
13900
13901 udelay(100);
13902 }
13903
13904 return ret;
13905}
13906
David S. Millerded73402005-05-23 13:59:47 -070013907#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070013908
13909static int __devinit tg3_test_dma(struct tg3 *tp)
13910{
13911 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070013912 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000013913 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013914
13915 buf = pci_alloc_consistent(tp->pdev, TEST_BUFFER_SIZE, &buf_dma);
13916 if (!buf) {
13917 ret = -ENOMEM;
13918 goto out_nofree;
13919 }
13920
13921 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
13922 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
13923
David S. Miller59e6b432005-05-18 22:50:10 -070013924 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013925
Matt Carlsonb703df62009-12-03 08:36:21 +000013926 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13927 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000013928 goto out;
13929
Linus Torvalds1da177e2005-04-16 15:20:36 -070013930 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
13931 /* DMA read watermark not used on PCIE */
13932 tp->dma_rwctrl |= 0x00180000;
13933 } else if (!(tp->tg3_flags & TG3_FLAG_PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070013934 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
13935 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013936 tp->dma_rwctrl |= 0x003f0000;
13937 else
13938 tp->dma_rwctrl |= 0x003f000f;
13939 } else {
13940 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
13941 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
13942 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080013943 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013944
Michael Chan4a29cc22006-03-19 13:21:12 -080013945 /* If the 5704 is behind the EPB bridge, we can
13946 * do the less restrictive ONE_DMA workaround for
13947 * better performance.
13948 */
13949 if ((tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG) &&
13950 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
13951 tp->dma_rwctrl |= 0x8000;
13952 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013953 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
13954
Michael Chan49afdeb2007-02-13 12:17:03 -080013955 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
13956 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070013957 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080013958 tp->dma_rwctrl |=
13959 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
13960 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
13961 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070013962 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
13963 /* 5780 always in PCIX mode */
13964 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070013965 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
13966 /* 5714 always in PCIX mode */
13967 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013968 } else {
13969 tp->dma_rwctrl |= 0x001b000f;
13970 }
13971 }
13972
13973 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
13974 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
13975 tp->dma_rwctrl &= 0xfffffff0;
13976
13977 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13978 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
13979 /* Remove this if it causes problems for some boards. */
13980 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
13981
13982 /* On 5700/5701 chips, we need to set this bit.
13983 * Otherwise the chip will issue cacheline transactions
13984 * to streamable DMA memory with not all the byte
13985 * enables turned on. This is an error on several
13986 * RISC PCI controllers, in particular sparc64.
13987 *
13988 * On 5703/5704 chips, this bit has been reassigned
13989 * a different meaning. In particular, it is used
13990 * on those chips to enable a PCI-X workaround.
13991 */
13992 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
13993 }
13994
13995 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
13996
13997#if 0
13998 /* Unneeded, already done by tg3_get_invariants. */
13999 tg3_switch_clocks(tp);
14000#endif
14001
Linus Torvalds1da177e2005-04-16 15:20:36 -070014002 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14003 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
14004 goto out;
14005
David S. Miller59e6b432005-05-18 22:50:10 -070014006 /* It is best to perform DMA test with maximum write burst size
14007 * to expose the 5700/5701 write DMA bug.
14008 */
14009 saved_dma_rwctrl = tp->dma_rwctrl;
14010 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14011 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14012
Linus Torvalds1da177e2005-04-16 15:20:36 -070014013 while (1) {
14014 u32 *p = buf, i;
14015
14016 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
14017 p[i] = i;
14018
14019 /* Send the buffer to the chip. */
14020 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
14021 if (ret) {
14022 printk(KERN_ERR "tg3_test_dma() Write the buffer failed %d\n", ret);
14023 break;
14024 }
14025
14026#if 0
14027 /* validate data reached card RAM correctly. */
14028 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14029 u32 val;
14030 tg3_read_mem(tp, 0x2100 + (i*4), &val);
14031 if (le32_to_cpu(val) != p[i]) {
14032 printk(KERN_ERR " tg3_test_dma() Card buffer corrupted on write! (%d != %d)\n", val, i);
14033 /* ret = -ENODEV here? */
14034 }
14035 p[i] = 0;
14036 }
14037#endif
14038 /* Now read it back. */
14039 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
14040 if (ret) {
14041 printk(KERN_ERR "tg3_test_dma() Read the buffer failed %d\n", ret);
14042
14043 break;
14044 }
14045
14046 /* Verify it. */
14047 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14048 if (p[i] == i)
14049 continue;
14050
David S. Miller59e6b432005-05-18 22:50:10 -070014051 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14052 DMA_RWCTRL_WRITE_BNDRY_16) {
14053 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014054 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
14055 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14056 break;
14057 } else {
14058 printk(KERN_ERR "tg3_test_dma() buffer corrupted on read back! (%d != %d)\n", p[i], i);
14059 ret = -ENODEV;
14060 goto out;
14061 }
14062 }
14063
14064 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
14065 /* Success. */
14066 ret = 0;
14067 break;
14068 }
14069 }
David S. Miller59e6b432005-05-18 22:50:10 -070014070 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14071 DMA_RWCTRL_WRITE_BNDRY_16) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014072 static struct pci_device_id dma_wait_state_chipsets[] = {
14073 { PCI_DEVICE(PCI_VENDOR_ID_APPLE,
14074 PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
14075 { },
14076 };
14077
David S. Miller59e6b432005-05-18 22:50:10 -070014078 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070014079 * now look for chipsets that are known to expose the
14080 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070014081 */
Michael Chan6d1cfba2005-06-08 14:13:14 -070014082 if (pci_dev_present(dma_wait_state_chipsets)) {
14083 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14084 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
14085 }
14086 else
14087 /* Safe to use the calculated DMA boundary. */
14088 tp->dma_rwctrl = saved_dma_rwctrl;
14089
David S. Miller59e6b432005-05-18 22:50:10 -070014090 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014092
14093out:
14094 pci_free_consistent(tp->pdev, TEST_BUFFER_SIZE, buf, buf_dma);
14095out_nofree:
14096 return ret;
14097}
14098
14099static void __devinit tg3_init_link_config(struct tg3 *tp)
14100{
14101 tp->link_config.advertising =
14102 (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
14103 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
14104 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full |
14105 ADVERTISED_Autoneg | ADVERTISED_MII);
14106 tp->link_config.speed = SPEED_INVALID;
14107 tp->link_config.duplex = DUPLEX_INVALID;
14108 tp->link_config.autoneg = AUTONEG_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014109 tp->link_config.active_speed = SPEED_INVALID;
14110 tp->link_config.active_duplex = DUPLEX_INVALID;
14111 tp->link_config.phy_is_low_power = 0;
14112 tp->link_config.orig_speed = SPEED_INVALID;
14113 tp->link_config.orig_duplex = DUPLEX_INVALID;
14114 tp->link_config.orig_autoneg = AUTONEG_INVALID;
14115}
14116
14117static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
14118{
Matt Carlson666bc832010-01-20 16:58:03 +000014119 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14120 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
14121 tp->bufmgr_config.mbuf_read_dma_low_water =
14122 DEFAULT_MB_RDMA_LOW_WATER_5705;
14123 tp->bufmgr_config.mbuf_mac_rx_low_water =
14124 DEFAULT_MB_MACRX_LOW_WATER_57765;
14125 tp->bufmgr_config.mbuf_high_water =
14126 DEFAULT_MB_HIGH_WATER_57765;
14127
14128 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14129 DEFAULT_MB_RDMA_LOW_WATER_5705;
14130 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14131 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
14132 tp->bufmgr_config.mbuf_high_water_jumbo =
14133 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
14134 } else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
Michael Chanfdfec172005-07-25 12:31:48 -070014135 tp->bufmgr_config.mbuf_read_dma_low_water =
14136 DEFAULT_MB_RDMA_LOW_WATER_5705;
14137 tp->bufmgr_config.mbuf_mac_rx_low_water =
14138 DEFAULT_MB_MACRX_LOW_WATER_5705;
14139 tp->bufmgr_config.mbuf_high_water =
14140 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070014141 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
14142 tp->bufmgr_config.mbuf_mac_rx_low_water =
14143 DEFAULT_MB_MACRX_LOW_WATER_5906;
14144 tp->bufmgr_config.mbuf_high_water =
14145 DEFAULT_MB_HIGH_WATER_5906;
14146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014147
Michael Chanfdfec172005-07-25 12:31:48 -070014148 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14149 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
14150 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14151 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
14152 tp->bufmgr_config.mbuf_high_water_jumbo =
14153 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
14154 } else {
14155 tp->bufmgr_config.mbuf_read_dma_low_water =
14156 DEFAULT_MB_RDMA_LOW_WATER;
14157 tp->bufmgr_config.mbuf_mac_rx_low_water =
14158 DEFAULT_MB_MACRX_LOW_WATER;
14159 tp->bufmgr_config.mbuf_high_water =
14160 DEFAULT_MB_HIGH_WATER;
14161
14162 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14163 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
14164 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14165 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
14166 tp->bufmgr_config.mbuf_high_water_jumbo =
14167 DEFAULT_MB_HIGH_WATER_JUMBO;
14168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014169
14170 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
14171 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
14172}
14173
14174static char * __devinit tg3_phy_string(struct tg3 *tp)
14175{
14176 switch (tp->phy_id & PHY_ID_MASK) {
14177 case PHY_ID_BCM5400: return "5400";
14178 case PHY_ID_BCM5401: return "5401";
14179 case PHY_ID_BCM5411: return "5411";
14180 case PHY_ID_BCM5701: return "5701";
14181 case PHY_ID_BCM5703: return "5703";
14182 case PHY_ID_BCM5704: return "5704";
14183 case PHY_ID_BCM5705: return "5705";
14184 case PHY_ID_BCM5750: return "5750";
Michael Chan85e94ce2005-04-21 17:05:28 -070014185 case PHY_ID_BCM5752: return "5752";
Michael Chana4e2b342005-10-26 15:46:52 -070014186 case PHY_ID_BCM5714: return "5714";
Michael Chan4cf78e42005-07-25 12:29:19 -070014187 case PHY_ID_BCM5780: return "5780";
Michael Chanaf36e6b2006-03-23 01:28:06 -080014188 case PHY_ID_BCM5755: return "5755";
Michael Chand9ab5ad2006-03-20 22:27:35 -080014189 case PHY_ID_BCM5787: return "5787";
Matt Carlsond30cdd22007-10-07 23:28:35 -070014190 case PHY_ID_BCM5784: return "5784";
Michael Chan126a3362006-09-27 16:03:07 -070014191 case PHY_ID_BCM5756: return "5722/5756";
Michael Chanb5d37722006-09-27 16:06:21 -070014192 case PHY_ID_BCM5906: return "5906";
Matt Carlson9936bcf2007-10-10 18:03:07 -070014193 case PHY_ID_BCM5761: return "5761";
Matt Carlson9b952f52010-01-20 16:58:04 +000014194 case PHY_ID_BCM5718C: return "5718C";
14195 case PHY_ID_BCM5718S: return "5718S";
Linus Torvalds1da177e2005-04-16 15:20:36 -070014196 case PHY_ID_BCM8002: return "8002/serdes";
14197 case 0: return "serdes";
14198 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070014199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014200}
14201
Michael Chanf9804dd2005-09-27 12:13:10 -070014202static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
14203{
14204 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
14205 strcpy(str, "PCI Express");
14206 return str;
14207 } else if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
14208 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
14209
14210 strcpy(str, "PCIX:");
14211
14212 if ((clock_ctrl == 7) ||
14213 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
14214 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
14215 strcat(str, "133MHz");
14216 else if (clock_ctrl == 0)
14217 strcat(str, "33MHz");
14218 else if (clock_ctrl == 2)
14219 strcat(str, "50MHz");
14220 else if (clock_ctrl == 4)
14221 strcat(str, "66MHz");
14222 else if (clock_ctrl == 6)
14223 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070014224 } else {
14225 strcpy(str, "PCI:");
14226 if (tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED)
14227 strcat(str, "66MHz");
14228 else
14229 strcat(str, "33MHz");
14230 }
14231 if (tp->tg3_flags & TG3_FLAG_PCI_32BIT)
14232 strcat(str, ":32-bit");
14233 else
14234 strcat(str, ":64-bit");
14235 return str;
14236}
14237
Michael Chan8c2dc7e2005-12-19 16:26:02 -080014238static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014239{
14240 struct pci_dev *peer;
14241 unsigned int func, devnr = tp->pdev->devfn & ~7;
14242
14243 for (func = 0; func < 8; func++) {
14244 peer = pci_get_slot(tp->pdev->bus, devnr | func);
14245 if (peer && peer != tp->pdev)
14246 break;
14247 pci_dev_put(peer);
14248 }
Michael Chan16fe9d72005-12-13 21:09:54 -080014249 /* 5704 can be configured in single-port mode, set peer to
14250 * tp->pdev in that case.
14251 */
14252 if (!peer) {
14253 peer = tp->pdev;
14254 return peer;
14255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014256
14257 /*
14258 * We don't need to keep the refcount elevated; there's no way
14259 * to remove one half of this device without removing the other
14260 */
14261 pci_dev_put(peer);
14262
14263 return peer;
14264}
14265
David S. Miller15f98502005-05-18 22:49:26 -070014266static void __devinit tg3_init_coal(struct tg3 *tp)
14267{
14268 struct ethtool_coalesce *ec = &tp->coal;
14269
14270 memset(ec, 0, sizeof(*ec));
14271 ec->cmd = ETHTOOL_GCOALESCE;
14272 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
14273 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
14274 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
14275 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
14276 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
14277 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
14278 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
14279 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
14280 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
14281
14282 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
14283 HOSTCC_MODE_CLRTICK_TXBD)) {
14284 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
14285 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
14286 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
14287 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
14288 }
Michael Chand244c892005-07-05 14:42:33 -070014289
14290 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
14291 ec->rx_coalesce_usecs_irq = 0;
14292 ec->tx_coalesce_usecs_irq = 0;
14293 ec->stats_block_coalesce_usecs = 0;
14294 }
David S. Miller15f98502005-05-18 22:49:26 -070014295}
14296
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080014297static const struct net_device_ops tg3_netdev_ops = {
14298 .ndo_open = tg3_open,
14299 .ndo_stop = tg3_close,
Stephen Hemminger00829822008-11-20 20:14:53 -080014300 .ndo_start_xmit = tg3_start_xmit,
14301 .ndo_get_stats = tg3_get_stats,
14302 .ndo_validate_addr = eth_validate_addr,
14303 .ndo_set_multicast_list = tg3_set_rx_mode,
14304 .ndo_set_mac_address = tg3_set_mac_addr,
14305 .ndo_do_ioctl = tg3_ioctl,
14306 .ndo_tx_timeout = tg3_tx_timeout,
14307 .ndo_change_mtu = tg3_change_mtu,
14308#if TG3_VLAN_TAG_USED
14309 .ndo_vlan_rx_register = tg3_vlan_rx_register,
14310#endif
14311#ifdef CONFIG_NET_POLL_CONTROLLER
14312 .ndo_poll_controller = tg3_poll_controller,
14313#endif
14314};
14315
14316static const struct net_device_ops tg3_netdev_ops_dma_bug = {
14317 .ndo_open = tg3_open,
14318 .ndo_stop = tg3_close,
14319 .ndo_start_xmit = tg3_start_xmit_dma_bug,
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080014320 .ndo_get_stats = tg3_get_stats,
14321 .ndo_validate_addr = eth_validate_addr,
14322 .ndo_set_multicast_list = tg3_set_rx_mode,
14323 .ndo_set_mac_address = tg3_set_mac_addr,
14324 .ndo_do_ioctl = tg3_ioctl,
14325 .ndo_tx_timeout = tg3_tx_timeout,
14326 .ndo_change_mtu = tg3_change_mtu,
14327#if TG3_VLAN_TAG_USED
14328 .ndo_vlan_rx_register = tg3_vlan_rx_register,
14329#endif
14330#ifdef CONFIG_NET_POLL_CONTROLLER
14331 .ndo_poll_controller = tg3_poll_controller,
14332#endif
14333};
14334
Linus Torvalds1da177e2005-04-16 15:20:36 -070014335static int __devinit tg3_init_one(struct pci_dev *pdev,
14336 const struct pci_device_id *ent)
14337{
14338 static int tg3_version_printed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014339 struct net_device *dev;
14340 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000014341 int i, err, pm_cap;
14342 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070014343 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080014344 u64 dma_mask, persist_dma_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014345
14346 if (tg3_version_printed++ == 0)
14347 printk(KERN_INFO "%s", version);
14348
14349 err = pci_enable_device(pdev);
14350 if (err) {
14351 printk(KERN_ERR PFX "Cannot enable PCI device, "
14352 "aborting.\n");
14353 return err;
14354 }
14355
Linus Torvalds1da177e2005-04-16 15:20:36 -070014356 err = pci_request_regions(pdev, DRV_MODULE_NAME);
14357 if (err) {
14358 printk(KERN_ERR PFX "Cannot obtain PCI resources, "
14359 "aborting.\n");
14360 goto err_out_disable_pdev;
14361 }
14362
14363 pci_set_master(pdev);
14364
14365 /* Find power-management capability. */
14366 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
14367 if (pm_cap == 0) {
14368 printk(KERN_ERR PFX "Cannot find PowerManagement capability, "
14369 "aborting.\n");
14370 err = -EIO;
14371 goto err_out_free_res;
14372 }
14373
Matt Carlsonfe5f5782009-09-01 13:09:39 +000014374 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014375 if (!dev) {
14376 printk(KERN_ERR PFX "Etherdev alloc failed, aborting.\n");
14377 err = -ENOMEM;
14378 goto err_out_free_res;
14379 }
14380
Linus Torvalds1da177e2005-04-16 15:20:36 -070014381 SET_NETDEV_DEV(dev, &pdev->dev);
14382
Linus Torvalds1da177e2005-04-16 15:20:36 -070014383#if TG3_VLAN_TAG_USED
14384 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014385#endif
14386
14387 tp = netdev_priv(dev);
14388 tp->pdev = pdev;
14389 tp->dev = dev;
14390 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014391 tp->rx_mode = TG3_DEF_RX_MODE;
14392 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070014393
Linus Torvalds1da177e2005-04-16 15:20:36 -070014394 if (tg3_debug > 0)
14395 tp->msg_enable = tg3_debug;
14396 else
14397 tp->msg_enable = TG3_DEF_MSG_ENABLE;
14398
14399 /* The word/byte swap controls here control register access byte
14400 * swapping. DMA data byte swapping is controlled in the GRC_MODE
14401 * setting below.
14402 */
14403 tp->misc_host_ctrl =
14404 MISC_HOST_CTRL_MASK_PCI_INT |
14405 MISC_HOST_CTRL_WORD_SWAP |
14406 MISC_HOST_CTRL_INDIR_ACCESS |
14407 MISC_HOST_CTRL_PCISTATE_RW;
14408
14409 /* The NONFRM (non-frame) byte/word swap controls take effect
14410 * on descriptor entries, anything which isn't packet data.
14411 *
14412 * The StrongARM chips on the board (one for tx, one for rx)
14413 * are running in big-endian mode.
14414 */
14415 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
14416 GRC_MODE_WSWAP_NONFRM_DATA);
14417#ifdef __BIG_ENDIAN
14418 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
14419#endif
14420 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014421 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000014422 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014423
Matt Carlsond5fe4882008-11-21 17:20:32 -080014424 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010014425 if (!tp->regs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014426 printk(KERN_ERR PFX "Cannot map device registers, "
14427 "aborting.\n");
14428 err = -ENOMEM;
14429 goto err_out_free_dev;
14430 }
14431
14432 tg3_init_link_config(tp);
14433
Linus Torvalds1da177e2005-04-16 15:20:36 -070014434 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
14435 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014436
Linus Torvalds1da177e2005-04-16 15:20:36 -070014437 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014438 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014439 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014440
14441 err = tg3_get_invariants(tp);
14442 if (err) {
14443 printk(KERN_ERR PFX "Problem fetching invariants of chip, "
14444 "aborting.\n");
14445 goto err_out_iounmap;
14446 }
14447
Matt Carlson615774f2009-11-13 13:03:39 +000014448 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) &&
14449 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0)
Stephen Hemminger00829822008-11-20 20:14:53 -080014450 dev->netdev_ops = &tg3_netdev_ops;
14451 else
14452 dev->netdev_ops = &tg3_netdev_ops_dma_bug;
14453
14454
Michael Chan4a29cc22006-03-19 13:21:12 -080014455 /* The EPB bridge inside 5714, 5715, and 5780 and any
14456 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080014457 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
14458 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
14459 * do DMA address check in tg3_start_xmit().
14460 */
Michael Chan4a29cc22006-03-19 13:21:12 -080014461 if (tp->tg3_flags2 & TG3_FLG2_IS_5788)
Yang Hongyang284901a92009-04-06 19:01:15 -070014462 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Michael Chan4a29cc22006-03-19 13:21:12 -080014463 else if (tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070014464 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080014465#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070014466 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080014467#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080014468 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070014469 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080014470
14471 /* Configure DMA attributes. */
Yang Hongyang284901a92009-04-06 19:01:15 -070014472 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080014473 err = pci_set_dma_mask(pdev, dma_mask);
14474 if (!err) {
14475 dev->features |= NETIF_F_HIGHDMA;
14476 err = pci_set_consistent_dma_mask(pdev,
14477 persist_dma_mask);
14478 if (err < 0) {
14479 printk(KERN_ERR PFX "Unable to obtain 64 bit "
14480 "DMA for consistent allocations\n");
14481 goto err_out_iounmap;
14482 }
14483 }
14484 }
Yang Hongyang284901a92009-04-06 19:01:15 -070014485 if (err || dma_mask == DMA_BIT_MASK(32)) {
14486 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080014487 if (err) {
14488 printk(KERN_ERR PFX "No usable DMA configuration, "
14489 "aborting.\n");
14490 goto err_out_iounmap;
14491 }
14492 }
14493
Michael Chanfdfec172005-07-25 12:31:48 -070014494 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014495
Matt Carlson507399f2009-11-13 13:03:37 +000014496 /* Selectively allow TSO based on operating conditions */
14497 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO) ||
14498 (tp->fw_needed && !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)))
14499 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
14500 else {
14501 tp->tg3_flags2 &= ~(TG3_FLG2_TSO_CAPABLE | TG3_FLG2_TSO_BUG);
14502 tp->fw_needed = NULL;
14503 }
14504
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014505 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
Matt Carlson9e9fd122009-01-19 16:57:45 -080014506 tp->fw_needed = FIRMWARE_TG3;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014507
Michael Chan4e3a7aa2006-03-20 17:47:44 -080014508 /* TSO is on by default on chips that support hardware TSO.
14509 * Firmware TSO on older chips gives lower performance, so it
14510 * is off by default, but can be enabled using ethtool.
14511 */
Matt Carlsone849cdc2009-11-13 13:03:38 +000014512 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO) &&
14513 (dev->features & NETIF_F_IP_CSUM))
14514 dev->features |= NETIF_F_TSO;
14515
14516 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_2) ||
14517 (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3)) {
14518 if (dev->features & NETIF_F_IPV6_CSUM)
Michael Chanb0026622006-07-03 19:42:14 -070014519 dev->features |= NETIF_F_TSO6;
Matt Carlsone849cdc2009-11-13 13:03:38 +000014520 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
14521 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070014522 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
14523 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014524 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000014525 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson9936bcf2007-10-10 18:03:07 -070014526 dev->features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070014527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014528
Linus Torvalds1da177e2005-04-16 15:20:36 -070014529 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
14530 !(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) &&
14531 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
14532 tp->tg3_flags2 |= TG3_FLG2_MAX_RXPEND_64;
14533 tp->rx_pending = 63;
14534 }
14535
Linus Torvalds1da177e2005-04-16 15:20:36 -070014536 err = tg3_get_device_address(tp);
14537 if (err) {
14538 printk(KERN_ERR PFX "Could not obtain valid ethernet address, "
14539 "aborting.\n");
Matt Carlson026a6c22009-12-03 08:36:24 +000014540 goto err_out_iounmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014541 }
14542
Matt Carlson0d3031d2007-10-10 18:02:43 -070014543 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
Matt Carlson63532392008-11-03 16:49:57 -080014544 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
Al Viro79ea13c2008-01-24 02:06:46 -080014545 if (!tp->aperegs) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070014546 printk(KERN_ERR PFX "Cannot map APE registers, "
14547 "aborting.\n");
14548 err = -ENOMEM;
Matt Carlson026a6c22009-12-03 08:36:24 +000014549 goto err_out_iounmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070014550 }
14551
14552 tg3_ape_lock_init(tp);
Matt Carlson7fd76442009-02-25 14:27:20 +000014553
14554 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF)
14555 tg3_read_dash_ver(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070014556 }
14557
Matt Carlsonc88864d2007-11-12 21:07:01 -080014558 /*
14559 * Reset chip in case UNDI or EFI driver did not shutdown
14560 * DMA self test will enable WDMAC and we'll see (spurious)
14561 * pending DMA on the PCI bus at that point.
14562 */
14563 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
14564 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
14565 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
14566 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
14567 }
14568
14569 err = tg3_test_dma(tp);
14570 if (err) {
14571 printk(KERN_ERR PFX "DMA engine test failed, aborting.\n");
14572 goto err_out_apeunmap;
14573 }
14574
Matt Carlsonc88864d2007-11-12 21:07:01 -080014575 /* flow control autonegotiation is default behavior */
14576 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
Steve Glendinninge18ce342008-12-16 02:00:00 -080014577 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlsonc88864d2007-11-12 21:07:01 -080014578
Matt Carlson78f90dc2009-11-13 13:03:42 +000014579 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
14580 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
14581 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
14582 for (i = 0; i < TG3_IRQ_MAX_VECS; i++) {
14583 struct tg3_napi *tnapi = &tp->napi[i];
14584
14585 tnapi->tp = tp;
14586 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
14587
14588 tnapi->int_mbox = intmbx;
14589 if (i < 4)
14590 intmbx += 0x8;
14591 else
14592 intmbx += 0x4;
14593
14594 tnapi->consmbox = rcvmbx;
14595 tnapi->prodmbox = sndmbx;
14596
14597 if (i) {
14598 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
14599 netif_napi_add(dev, &tnapi->napi, tg3_poll_msix, 64);
14600 } else {
14601 tnapi->coal_now = HOSTCC_MODE_NOW;
14602 netif_napi_add(dev, &tnapi->napi, tg3_poll, 64);
14603 }
14604
14605 if (!(tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX))
14606 break;
14607
14608 /*
14609 * If we support MSIX, we'll be using RSS. If we're using
14610 * RSS, the first vector only handles link interrupts and the
14611 * remaining vectors handle rx and tx interrupts. Reuse the
14612 * mailbox values for the next iteration. The values we setup
14613 * above are still useful for the single vectored mode.
14614 */
14615 if (!i)
14616 continue;
14617
14618 rcvmbx += 0x8;
14619
14620 if (sndmbx & 0x4)
14621 sndmbx -= 0x4;
14622 else
14623 sndmbx += 0xc;
14624 }
14625
Matt Carlsonc88864d2007-11-12 21:07:01 -080014626 tg3_init_coal(tp);
14627
Michael Chanc49a1562006-12-17 17:07:29 -080014628 pci_set_drvdata(pdev, dev);
14629
Linus Torvalds1da177e2005-04-16 15:20:36 -070014630 err = register_netdev(dev);
14631 if (err) {
14632 printk(KERN_ERR PFX "Cannot register net device, "
14633 "aborting.\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070014634 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014635 }
14636
Matt Carlsondf59c942008-11-03 16:52:56 -080014637 printk(KERN_INFO "%s: Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070014638 dev->name,
14639 tp->board_part_number,
14640 tp->pci_chip_rev_id,
Michael Chanf9804dd2005-09-27 12:13:10 -070014641 tg3_bus_string(tp, str),
Johannes Berge1749612008-10-27 15:59:26 -070014642 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014643
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000014644 if (tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED) {
14645 struct phy_device *phydev;
14646 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsondf59c942008-11-03 16:52:56 -080014647 printk(KERN_INFO
14648 "%s: attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000014649 tp->dev->name, phydev->drv->name,
14650 dev_name(&phydev->dev));
14651 } else
Matt Carlsondf59c942008-11-03 16:52:56 -080014652 printk(KERN_INFO
14653 "%s: attached PHY is %s (%s Ethernet) (WireSpeed[%d])\n",
14654 tp->dev->name, tg3_phy_string(tp),
14655 ((tp->tg3_flags & TG3_FLAG_10_100_ONLY) ? "10/100Base-TX" :
14656 ((tp->tg3_flags2 & TG3_FLG2_ANY_SERDES) ? "1000Base-SX" :
14657 "10/100/1000Base-T")),
14658 (tp->tg3_flags2 & TG3_FLG2_NO_ETH_WIRE_SPEED) == 0);
14659
14660 printk(KERN_INFO "%s: RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070014661 dev->name,
14662 (tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) != 0,
14663 (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) != 0,
14664 (tp->tg3_flags & TG3_FLAG_USE_MI_INTERRUPT) != 0,
14665 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014666 (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) != 0);
Michael Chan4a29cc22006-03-19 13:21:12 -080014667 printk(KERN_INFO "%s: dma_rwctrl[%08x] dma_mask[%d-bit]\n",
14668 dev->name, tp->dma_rwctrl,
Yang Hongyang284901a92009-04-06 19:01:15 -070014669 (pdev->dma_mask == DMA_BIT_MASK(32)) ? 32 :
Yang Hongyang50cf1562009-04-06 19:01:14 -070014670 (((u64) pdev->dma_mask == DMA_BIT_MASK(40)) ? 40 : 64));
Linus Torvalds1da177e2005-04-16 15:20:36 -070014671
14672 return 0;
14673
Matt Carlson0d3031d2007-10-10 18:02:43 -070014674err_out_apeunmap:
14675 if (tp->aperegs) {
14676 iounmap(tp->aperegs);
14677 tp->aperegs = NULL;
14678 }
14679
Linus Torvalds1da177e2005-04-16 15:20:36 -070014680err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070014681 if (tp->regs) {
14682 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070014683 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070014684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014685
14686err_out_free_dev:
14687 free_netdev(dev);
14688
14689err_out_free_res:
14690 pci_release_regions(pdev);
14691
14692err_out_disable_pdev:
14693 pci_disable_device(pdev);
14694 pci_set_drvdata(pdev, NULL);
14695 return err;
14696}
14697
14698static void __devexit tg3_remove_one(struct pci_dev *pdev)
14699{
14700 struct net_device *dev = pci_get_drvdata(pdev);
14701
14702 if (dev) {
14703 struct tg3 *tp = netdev_priv(dev);
14704
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014705 if (tp->fw)
14706 release_firmware(tp->fw);
14707
Michael Chan7faa0062006-02-02 17:29:28 -080014708 flush_scheduled_work();
Matt Carlson158d7ab2008-05-29 01:37:54 -070014709
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014710 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
14711 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070014712 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014713 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070014714
Linus Torvalds1da177e2005-04-16 15:20:36 -070014715 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070014716 if (tp->aperegs) {
14717 iounmap(tp->aperegs);
14718 tp->aperegs = NULL;
14719 }
Michael Chan68929142005-08-09 20:17:14 -070014720 if (tp->regs) {
14721 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070014722 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070014723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014724 free_netdev(dev);
14725 pci_release_regions(pdev);
14726 pci_disable_device(pdev);
14727 pci_set_drvdata(pdev, NULL);
14728 }
14729}
14730
14731static int tg3_suspend(struct pci_dev *pdev, pm_message_t state)
14732{
14733 struct net_device *dev = pci_get_drvdata(pdev);
14734 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070014735 pci_power_t target_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014736 int err;
14737
Michael Chan3e0c95f2007-08-03 20:56:54 -070014738 /* PCI register 4 needs to be saved whether netif_running() or not.
14739 * MSI address and data need to be saved if using MSI and
14740 * netif_running().
14741 */
14742 pci_save_state(pdev);
14743
Linus Torvalds1da177e2005-04-16 15:20:36 -070014744 if (!netif_running(dev))
14745 return 0;
14746
Michael Chan7faa0062006-02-02 17:29:28 -080014747 flush_scheduled_work();
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014748 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014749 tg3_netif_stop(tp);
14750
14751 del_timer_sync(&tp->timer);
14752
David S. Millerf47c11e2005-06-24 20:18:35 -070014753 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014754 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070014755 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014756
14757 netif_device_detach(dev);
14758
David S. Millerf47c11e2005-06-24 20:18:35 -070014759 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070014760 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan6a9eba12005-12-13 21:08:58 -080014761 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
David S. Millerf47c11e2005-06-24 20:18:35 -070014762 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014763
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070014764 target_state = pdev->pm_cap ? pci_target_state(pdev) : PCI_D3hot;
14765
14766 err = tg3_set_power_state(tp, target_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014767 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014768 int err2;
14769
David S. Millerf47c11e2005-06-24 20:18:35 -070014770 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014771
Michael Chan6a9eba12005-12-13 21:08:58 -080014772 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014773 err2 = tg3_restart_hw(tp, 1);
14774 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070014775 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014776
14777 tp->timer.expires = jiffies + tp->timer_offset;
14778 add_timer(&tp->timer);
14779
14780 netif_device_attach(dev);
14781 tg3_netif_start(tp);
14782
Michael Chanb9ec6c12006-07-25 16:37:27 -070014783out:
David S. Millerf47c11e2005-06-24 20:18:35 -070014784 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014785
14786 if (!err2)
14787 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014788 }
14789
14790 return err;
14791}
14792
14793static int tg3_resume(struct pci_dev *pdev)
14794{
14795 struct net_device *dev = pci_get_drvdata(pdev);
14796 struct tg3 *tp = netdev_priv(dev);
14797 int err;
14798
Michael Chan3e0c95f2007-08-03 20:56:54 -070014799 pci_restore_state(tp->pdev);
14800
Linus Torvalds1da177e2005-04-16 15:20:36 -070014801 if (!netif_running(dev))
14802 return 0;
14803
Michael Chanbc1c7562006-03-20 17:48:03 -080014804 err = tg3_set_power_state(tp, PCI_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014805 if (err)
14806 return err;
14807
14808 netif_device_attach(dev);
14809
David S. Millerf47c11e2005-06-24 20:18:35 -070014810 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014811
Michael Chan6a9eba12005-12-13 21:08:58 -080014812 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Michael Chanb9ec6c12006-07-25 16:37:27 -070014813 err = tg3_restart_hw(tp, 1);
14814 if (err)
14815 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014816
14817 tp->timer.expires = jiffies + tp->timer_offset;
14818 add_timer(&tp->timer);
14819
Linus Torvalds1da177e2005-04-16 15:20:36 -070014820 tg3_netif_start(tp);
14821
Michael Chanb9ec6c12006-07-25 16:37:27 -070014822out:
David S. Millerf47c11e2005-06-24 20:18:35 -070014823 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014824
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014825 if (!err)
14826 tg3_phy_start(tp);
14827
Michael Chanb9ec6c12006-07-25 16:37:27 -070014828 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014829}
14830
14831static struct pci_driver tg3_driver = {
14832 .name = DRV_MODULE_NAME,
14833 .id_table = tg3_pci_tbl,
14834 .probe = tg3_init_one,
14835 .remove = __devexit_p(tg3_remove_one),
14836 .suspend = tg3_suspend,
14837 .resume = tg3_resume
14838};
14839
14840static int __init tg3_init(void)
14841{
Jeff Garzik29917622006-08-19 17:48:59 -040014842 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014843}
14844
14845static void __exit tg3_cleanup(void)
14846{
14847 pci_unregister_driver(&tg3_driver);
14848}
14849
14850module_init(tg3_init);
14851module_exit(tg3_cleanup);