blob: 78102023aa93ed360428670a0af2c762613ff86f [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 Carlson66711e662009-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 Buytenhek298cf9b2008-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 Buytenhek298cf9b2008-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 Buytenhek298cf9b2008-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 Buytenhek298cf9b2008-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 Buytenhek298cf9b2008-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 Buytenhek298cf9b2008-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 Buytenhek298cf9b2008-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
Matt Carlson334355a2010-01-20 16:58:10 +00002024 /* The GPIOs do something completely different on 57765. */
2025 if ((tp->tg3_flags2 & TG3_FLG2_IS_NIC) == 0 ||
2026 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 return;
2028
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00002029 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2030 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
2031 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002032 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002034 dev_peer = pci_get_drvdata(tp->pdev_peer);
Michael Chanbc1c7562006-03-20 17:48:03 -08002035 /* remove_one() may have been run on the peer. */
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002036 if (!dev_peer)
Michael Chanbc1c7562006-03-20 17:48:03 -08002037 tp_peer = tp;
2038 else
2039 tp_peer = netdev_priv(dev_peer);
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
2042 if ((tp->tg3_flags & TG3_FLAG_WOL_ENABLE) != 0 ||
Michael Chan6921d202005-12-13 21:15:53 -08002043 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0 ||
2044 (tp_peer->tg3_flags & TG3_FLAG_WOL_ENABLE) != 0 ||
2045 (tp_peer->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2047 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Michael Chanb401e9e2005-12-19 16:27:04 -08002048 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2049 (GRC_LCLCTRL_GPIO_OE0 |
2050 GRC_LCLCTRL_GPIO_OE1 |
2051 GRC_LCLCTRL_GPIO_OE2 |
2052 GRC_LCLCTRL_GPIO_OUTPUT0 |
2053 GRC_LCLCTRL_GPIO_OUTPUT1),
2054 100);
Matt Carlson8d519ab2009-04-20 06:58:01 +00002055 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2056 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -07002057 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2058 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2059 GRC_LCLCTRL_GPIO_OE1 |
2060 GRC_LCLCTRL_GPIO_OE2 |
2061 GRC_LCLCTRL_GPIO_OUTPUT0 |
2062 GRC_LCLCTRL_GPIO_OUTPUT1 |
2063 tp->grc_local_ctrl;
2064 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2065
2066 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2067 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2068
2069 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2070 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 } else {
2072 u32 no_gpio2;
Michael Chandc56b7d2005-12-19 16:26:28 -08002073 u32 grc_local_ctrl = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
2075 if (tp_peer != tp &&
2076 (tp_peer->tg3_flags & TG3_FLAG_INIT_COMPLETE) != 0)
2077 return;
2078
Michael Chandc56b7d2005-12-19 16:26:28 -08002079 /* Workaround to prevent overdrawing Amps. */
2080 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2081 ASIC_REV_5714) {
2082 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chanb401e9e2005-12-19 16:27:04 -08002083 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2084 grc_local_ctrl, 100);
Michael Chandc56b7d2005-12-19 16:26:28 -08002085 }
2086
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 /* On 5753 and variants, GPIO2 cannot be used. */
2088 no_gpio2 = tp->nic_sram_data_cfg &
2089 NIC_SRAM_DATA_CFG_NO_GPIO2;
2090
Michael Chandc56b7d2005-12-19 16:26:28 -08002091 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 GRC_LCLCTRL_GPIO_OE1 |
2093 GRC_LCLCTRL_GPIO_OE2 |
2094 GRC_LCLCTRL_GPIO_OUTPUT1 |
2095 GRC_LCLCTRL_GPIO_OUTPUT2;
2096 if (no_gpio2) {
2097 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2098 GRC_LCLCTRL_GPIO_OUTPUT2);
2099 }
Michael Chanb401e9e2005-12-19 16:27:04 -08002100 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2101 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
2103 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2104
Michael Chanb401e9e2005-12-19 16:27:04 -08002105 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2106 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
2108 if (!no_gpio2) {
2109 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chanb401e9e2005-12-19 16:27:04 -08002110 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2111 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 }
2113 }
2114 } else {
2115 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
2116 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
2117 if (tp_peer != tp &&
2118 (tp_peer->tg3_flags & TG3_FLAG_INIT_COMPLETE) != 0)
2119 return;
2120
Michael Chanb401e9e2005-12-19 16:27:04 -08002121 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2122 (GRC_LCLCTRL_GPIO_OE1 |
2123 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
Michael Chanb401e9e2005-12-19 16:27:04 -08002125 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2126 GRC_LCLCTRL_GPIO_OE1, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
Michael Chanb401e9e2005-12-19 16:27:04 -08002128 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2129 (GRC_LCLCTRL_GPIO_OE1 |
2130 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 }
2132 }
2133}
2134
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002135static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2136{
2137 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2138 return 1;
2139 else if ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5411) {
2140 if (speed != SPEED_10)
2141 return 1;
2142 } else if (speed == SPEED_10)
2143 return 1;
2144
2145 return 0;
2146}
2147
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148static int tg3_setup_phy(struct tg3 *, int);
2149
2150#define RESET_KIND_SHUTDOWN 0
2151#define RESET_KIND_INIT 1
2152#define RESET_KIND_SUSPEND 2
2153
2154static void tg3_write_sig_post_reset(struct tg3 *, int);
2155static int tg3_halt_cpu(struct tg3 *, u32);
2156
Matt Carlson0a459aa2008-11-03 16:54:15 -08002157static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002158{
Matt Carlsonce057f02007-11-12 21:08:03 -08002159 u32 val;
2160
Michael Chan51297242007-02-13 12:17:57 -08002161 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
2162 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2163 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2164 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2165
2166 sg_dig_ctrl |=
2167 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2168 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2169 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2170 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002171 return;
Michael Chan51297242007-02-13 12:17:57 -08002172 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002173
Michael Chan60189dd2006-12-17 17:08:07 -08002174 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002175 tg3_bmcr_reset(tp);
2176 val = tr32(GRC_MISC_CFG);
2177 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2178 udelay(40);
2179 return;
Matt Carlson0e5f7842009-11-02 14:26:38 +00002180 } else if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
2181 u32 phytest;
2182 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2183 u32 phy;
2184
2185 tg3_writephy(tp, MII_ADVERTISE, 0);
2186 tg3_writephy(tp, MII_BMCR,
2187 BMCR_ANENABLE | BMCR_ANRESTART);
2188
2189 tg3_writephy(tp, MII_TG3_FET_TEST,
2190 phytest | MII_TG3_FET_SHADOW_EN);
2191 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
2192 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
2193 tg3_writephy(tp,
2194 MII_TG3_FET_SHDW_AUXMODE4,
2195 phy);
2196 }
2197 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2198 }
2199 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002200 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07002201 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2202 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002203
2204 tg3_writephy(tp, MII_TG3_AUX_CTRL,
2205 MII_TG3_AUXCTL_SHDWSEL_PWRCTL |
2206 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2207 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2208 MII_TG3_AUXCTL_PCTL_VREG_11V);
Michael Chan715116a2006-09-27 16:09:25 -07002209 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002210
Michael Chan15c3b692006-03-22 01:06:52 -08002211 /* The PHY should not be powered down on some chips because
2212 * of bugs.
2213 */
2214 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2215 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2216 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
2217 (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)))
2218 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08002219
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002220 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2221 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002222 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2223 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2224 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2225 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2226 }
2227
Michael Chan15c3b692006-03-22 01:06:52 -08002228 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2229}
2230
Matt Carlson3f007892008-11-03 16:51:36 -08002231/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002232static int tg3_nvram_lock(struct tg3 *tp)
2233{
2234 if (tp->tg3_flags & TG3_FLAG_NVRAM) {
2235 int i;
2236
2237 if (tp->nvram_lock_cnt == 0) {
2238 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2239 for (i = 0; i < 8000; i++) {
2240 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2241 break;
2242 udelay(20);
2243 }
2244 if (i == 8000) {
2245 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2246 return -ENODEV;
2247 }
2248 }
2249 tp->nvram_lock_cnt++;
2250 }
2251 return 0;
2252}
2253
2254/* tp->lock is held. */
2255static void tg3_nvram_unlock(struct tg3 *tp)
2256{
2257 if (tp->tg3_flags & TG3_FLAG_NVRAM) {
2258 if (tp->nvram_lock_cnt > 0)
2259 tp->nvram_lock_cnt--;
2260 if (tp->nvram_lock_cnt == 0)
2261 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
2262 }
2263}
2264
2265/* tp->lock is held. */
2266static void tg3_enable_nvram_access(struct tg3 *tp)
2267{
2268 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Matt Carlsonf66a29b2009-11-13 13:03:36 +00002269 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002270 u32 nvaccess = tr32(NVRAM_ACCESS);
2271
2272 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
2273 }
2274}
2275
2276/* tp->lock is held. */
2277static void tg3_disable_nvram_access(struct tg3 *tp)
2278{
2279 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Matt Carlsonf66a29b2009-11-13 13:03:36 +00002280 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002281 u32 nvaccess = tr32(NVRAM_ACCESS);
2282
2283 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
2284 }
2285}
2286
2287static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
2288 u32 offset, u32 *val)
2289{
2290 u32 tmp;
2291 int i;
2292
2293 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
2294 return -EINVAL;
2295
2296 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
2297 EEPROM_ADDR_DEVID_MASK |
2298 EEPROM_ADDR_READ);
2299 tw32(GRC_EEPROM_ADDR,
2300 tmp |
2301 (0 << EEPROM_ADDR_DEVID_SHIFT) |
2302 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
2303 EEPROM_ADDR_ADDR_MASK) |
2304 EEPROM_ADDR_READ | EEPROM_ADDR_START);
2305
2306 for (i = 0; i < 1000; i++) {
2307 tmp = tr32(GRC_EEPROM_ADDR);
2308
2309 if (tmp & EEPROM_ADDR_COMPLETE)
2310 break;
2311 msleep(1);
2312 }
2313 if (!(tmp & EEPROM_ADDR_COMPLETE))
2314 return -EBUSY;
2315
Matt Carlson62cedd12009-04-20 14:52:29 -07002316 tmp = tr32(GRC_EEPROM_DATA);
2317
2318 /*
2319 * The data will always be opposite the native endian
2320 * format. Perform a blind byteswap to compensate.
2321 */
2322 *val = swab32(tmp);
2323
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002324 return 0;
2325}
2326
2327#define NVRAM_CMD_TIMEOUT 10000
2328
2329static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
2330{
2331 int i;
2332
2333 tw32(NVRAM_CMD, nvram_cmd);
2334 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
2335 udelay(10);
2336 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
2337 udelay(10);
2338 break;
2339 }
2340 }
2341
2342 if (i == NVRAM_CMD_TIMEOUT)
2343 return -EBUSY;
2344
2345 return 0;
2346}
2347
2348static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
2349{
2350 if ((tp->tg3_flags & TG3_FLAG_NVRAM) &&
2351 (tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) &&
2352 (tp->tg3_flags2 & TG3_FLG2_FLASH) &&
2353 !(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM_ADDR_TRANS) &&
2354 (tp->nvram_jedecnum == JEDEC_ATMEL))
2355
2356 addr = ((addr / tp->nvram_pagesize) <<
2357 ATMEL_AT45DB0X1B_PAGE_POS) +
2358 (addr % tp->nvram_pagesize);
2359
2360 return addr;
2361}
2362
2363static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
2364{
2365 if ((tp->tg3_flags & TG3_FLAG_NVRAM) &&
2366 (tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) &&
2367 (tp->tg3_flags2 & TG3_FLG2_FLASH) &&
2368 !(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM_ADDR_TRANS) &&
2369 (tp->nvram_jedecnum == JEDEC_ATMEL))
2370
2371 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
2372 tp->nvram_pagesize) +
2373 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
2374
2375 return addr;
2376}
2377
Matt Carlsone4f34112009-02-25 14:25:00 +00002378/* NOTE: Data read in from NVRAM is byteswapped according to
2379 * the byteswapping settings for all other register accesses.
2380 * tg3 devices are BE devices, so on a BE machine, the data
2381 * returned will be exactly as it is seen in NVRAM. On a LE
2382 * machine, the 32-bit value will be byteswapped.
2383 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002384static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
2385{
2386 int ret;
2387
2388 if (!(tp->tg3_flags & TG3_FLAG_NVRAM))
2389 return tg3_nvram_read_using_eeprom(tp, offset, val);
2390
2391 offset = tg3_nvram_phys_addr(tp, offset);
2392
2393 if (offset > NVRAM_ADDR_MSK)
2394 return -EINVAL;
2395
2396 ret = tg3_nvram_lock(tp);
2397 if (ret)
2398 return ret;
2399
2400 tg3_enable_nvram_access(tp);
2401
2402 tw32(NVRAM_ADDR, offset);
2403 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
2404 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
2405
2406 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00002407 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002408
2409 tg3_disable_nvram_access(tp);
2410
2411 tg3_nvram_unlock(tp);
2412
2413 return ret;
2414}
2415
Matt Carlsona9dc5292009-02-25 14:25:30 +00002416/* Ensures NVRAM data is in bytestream format. */
2417static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002418{
2419 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00002420 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002421 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00002422 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002423 return res;
2424}
2425
2426/* tp->lock is held. */
Matt Carlson3f007892008-11-03 16:51:36 -08002427static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
2428{
2429 u32 addr_high, addr_low;
2430 int i;
2431
2432 addr_high = ((tp->dev->dev_addr[0] << 8) |
2433 tp->dev->dev_addr[1]);
2434 addr_low = ((tp->dev->dev_addr[2] << 24) |
2435 (tp->dev->dev_addr[3] << 16) |
2436 (tp->dev->dev_addr[4] << 8) |
2437 (tp->dev->dev_addr[5] << 0));
2438 for (i = 0; i < 4; i++) {
2439 if (i == 1 && skip_mac_1)
2440 continue;
2441 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
2442 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
2443 }
2444
2445 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2446 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2447 for (i = 0; i < 12; i++) {
2448 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
2449 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
2450 }
2451 }
2452
2453 addr_high = (tp->dev->dev_addr[0] +
2454 tp->dev->dev_addr[1] +
2455 tp->dev->dev_addr[2] +
2456 tp->dev->dev_addr[3] +
2457 tp->dev->dev_addr[4] +
2458 tp->dev->dev_addr[5]) &
2459 TX_BACKOFF_SEED_MASK;
2460 tw32(MAC_TX_BACKOFF_SEED, addr_high);
2461}
2462
Michael Chanbc1c7562006-03-20 17:48:03 -08002463static int tg3_set_power_state(struct tg3 *tp, pci_power_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464{
2465 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002466 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467
2468 /* Make sure register accesses (indirect or otherwise)
2469 * will function correctly.
2470 */
2471 pci_write_config_dword(tp->pdev,
2472 TG3PCI_MISC_HOST_CTRL,
2473 tp->misc_host_ctrl);
2474
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 switch (state) {
Michael Chanbc1c7562006-03-20 17:48:03 -08002476 case PCI_D0:
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07002477 pci_enable_wake(tp->pdev, state, false);
2478 pci_set_power_state(tp->pdev, PCI_D0);
Michael Chan8c6bda12005-04-21 17:09:08 -07002479
Michael Chan9d26e212006-12-07 00:21:14 -08002480 /* Switch out of Vaux if it is a NIC */
2481 if (tp->tg3_flags2 & TG3_FLG2_IS_NIC)
Michael Chanb401e9e2005-12-19 16:27:04 -08002482 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
2484 return 0;
2485
Michael Chanbc1c7562006-03-20 17:48:03 -08002486 case PCI_D1:
Michael Chanbc1c7562006-03-20 17:48:03 -08002487 case PCI_D2:
Michael Chanbc1c7562006-03-20 17:48:03 -08002488 case PCI_D3hot:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 break;
2490
2491 default:
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07002492 printk(KERN_ERR PFX "%s: Invalid power state (D%d) requested\n",
2493 tp->dev->name, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002495 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002496
2497 /* Restore the CLKREQ setting. */
2498 if (tp->tg3_flags3 & TG3_FLG3_CLKREQ_BUG) {
2499 u16 lnkctl;
2500
2501 pci_read_config_word(tp->pdev,
2502 tp->pcie_cap + PCI_EXP_LNKCTL,
2503 &lnkctl);
2504 lnkctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
2505 pci_write_config_word(tp->pdev,
2506 tp->pcie_cap + PCI_EXP_LNKCTL,
2507 lnkctl);
2508 }
2509
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
2511 tw32(TG3PCI_MISC_HOST_CTRL,
2512 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
2513
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002514 device_should_wake = pci_pme_capable(tp->pdev, state) &&
2515 device_may_wakeup(&tp->pdev->dev) &&
2516 (tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
2517
Matt Carlsondd477002008-05-25 23:45:58 -07002518 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08002519 do_low_power = false;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002520 if ((tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED) &&
2521 !tp->link_config.phy_is_low_power) {
2522 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002523 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002524
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002525 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002526
2527 tp->link_config.phy_is_low_power = 1;
2528
2529 tp->link_config.orig_speed = phydev->speed;
2530 tp->link_config.orig_duplex = phydev->duplex;
2531 tp->link_config.orig_autoneg = phydev->autoneg;
2532 tp->link_config.orig_advertising = phydev->advertising;
2533
2534 advertising = ADVERTISED_TP |
2535 ADVERTISED_Pause |
2536 ADVERTISED_Autoneg |
2537 ADVERTISED_10baseT_Half;
2538
2539 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002540 device_should_wake) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002541 if (tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB)
2542 advertising |=
2543 ADVERTISED_100baseT_Half |
2544 ADVERTISED_100baseT_Full |
2545 ADVERTISED_10baseT_Full;
2546 else
2547 advertising |= ADVERTISED_10baseT_Full;
2548 }
2549
2550 phydev->advertising = advertising;
2551
2552 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002553
2554 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
2555 if (phyid != TG3_PHY_ID_BCMAC131) {
2556 phyid &= TG3_PHY_OUI_MASK;
Roel Kluinf72b5342009-02-18 17:42:42 -08002557 if (phyid == TG3_PHY_OUI_1 ||
2558 phyid == TG3_PHY_OUI_2 ||
Matt Carlson0a459aa2008-11-03 16:54:15 -08002559 phyid == TG3_PHY_OUI_3)
2560 do_low_power = true;
2561 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002562 }
Matt Carlsondd477002008-05-25 23:45:58 -07002563 } else {
Matt Carlson20232762008-12-21 20:18:56 -08002564 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002565
Matt Carlsondd477002008-05-25 23:45:58 -07002566 if (tp->link_config.phy_is_low_power == 0) {
2567 tp->link_config.phy_is_low_power = 1;
2568 tp->link_config.orig_speed = tp->link_config.speed;
2569 tp->link_config.orig_duplex = tp->link_config.duplex;
2570 tp->link_config.orig_autoneg = tp->link_config.autoneg;
2571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572
Matt Carlsondd477002008-05-25 23:45:58 -07002573 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)) {
2574 tp->link_config.speed = SPEED_10;
2575 tp->link_config.duplex = DUPLEX_HALF;
2576 tp->link_config.autoneg = AUTONEG_ENABLE;
2577 tg3_setup_phy(tp, 0);
2578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 }
2580
Michael Chanb5d37722006-09-27 16:06:21 -07002581 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
2582 u32 val;
2583
2584 val = tr32(GRC_VCPU_EXT_CTRL);
2585 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
2586 } else if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08002587 int i;
2588 u32 val;
2589
2590 for (i = 0; i < 200; i++) {
2591 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
2592 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
2593 break;
2594 msleep(1);
2595 }
2596 }
Gary Zambranoa85feb82007-05-05 11:52:19 -07002597 if (tp->tg3_flags & TG3_FLAG_WOL_CAP)
2598 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
2599 WOL_DRV_STATE_SHUTDOWN |
2600 WOL_DRV_WOL |
2601 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08002602
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002603 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 u32 mac_mode;
2605
2606 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08002607 if (do_low_power) {
Matt Carlsondd477002008-05-25 23:45:58 -07002608 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x5a);
2609 udelay(40);
2610 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611
Michael Chan3f7045c2006-09-27 16:02:29 -07002612 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)
2613 mac_mode = MAC_MODE_PORT_MODE_GMII;
2614 else
2615 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002617 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
2618 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2619 ASIC_REV_5700) {
2620 u32 speed = (tp->tg3_flags &
2621 TG3_FLAG_WOL_SPEED_100MB) ?
2622 SPEED_100 : SPEED_10;
2623 if (tg3_5700_link_polarity(tp, speed))
2624 mac_mode |= MAC_MODE_LINK_POLARITY;
2625 else
2626 mac_mode &= ~MAC_MODE_LINK_POLARITY;
2627 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 } else {
2629 mac_mode = MAC_MODE_PORT_MODE_TBI;
2630 }
2631
John W. Linvillecbf46852005-04-21 17:01:29 -07002632 if (!(tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 tw32(MAC_LED_CTRL, tp->led_ctrl);
2634
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002635 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
2636 if (((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
2637 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) &&
2638 ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
2639 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)))
2640 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641
Matt Carlson3bda1252008-08-15 14:08:22 -07002642 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
2643 mac_mode |= tp->mac_mode &
2644 (MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN);
2645 if (mac_mode & MAC_MODE_APE_TX_EN)
2646 mac_mode |= MAC_MODE_TDE_ENABLE;
2647 }
2648
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 tw32_f(MAC_MODE, mac_mode);
2650 udelay(100);
2651
2652 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
2653 udelay(10);
2654 }
2655
2656 if (!(tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB) &&
2657 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2658 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
2659 u32 base_val;
2660
2661 base_val = tp->pci_clock_ctrl;
2662 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
2663 CLOCK_CTRL_TXCLK_DISABLE);
2664
Michael Chanb401e9e2005-12-19 16:27:04 -08002665 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
2666 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Michael Chand7b0a852007-02-13 12:17:38 -08002667 } else if ((tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
Matt Carlson795d01c2007-10-07 23:28:17 -07002668 (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
Michael Chand7b0a852007-02-13 12:17:38 -08002669 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)) {
Michael Chan4cf78e42005-07-25 12:29:19 -07002670 /* do nothing */
Michael Chan85e94ce2005-04-21 17:05:28 -07002671 } else if (!((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF))) {
2673 u32 newbits1, newbits2;
2674
2675 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2676 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2677 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
2678 CLOCK_CTRL_TXCLK_DISABLE |
2679 CLOCK_CTRL_ALTCLK);
2680 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2681 } else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
2682 newbits1 = CLOCK_CTRL_625_CORE;
2683 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
2684 } else {
2685 newbits1 = CLOCK_CTRL_ALTCLK;
2686 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2687 }
2688
Michael Chanb401e9e2005-12-19 16:27:04 -08002689 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
2690 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691
Michael Chanb401e9e2005-12-19 16:27:04 -08002692 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
2693 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694
2695 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
2696 u32 newbits3;
2697
2698 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2699 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2700 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
2701 CLOCK_CTRL_TXCLK_DISABLE |
2702 CLOCK_CTRL_44MHZ_CORE);
2703 } else {
2704 newbits3 = CLOCK_CTRL_44MHZ_CORE;
2705 }
2706
Michael Chanb401e9e2005-12-19 16:27:04 -08002707 tw32_wait_f(TG3PCI_CLOCK_CTRL,
2708 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 }
2710 }
2711
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002712 if (!(device_should_wake) &&
Matt Carlson22435842008-11-21 17:21:13 -08002713 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08002714 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08002715
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 tg3_frob_aux_power(tp);
2717
2718 /* Workaround for unstable PLL clock */
2719 if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
2720 (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
2721 u32 val = tr32(0x7d00);
2722
2723 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
2724 tw32(0x7d00, val);
Michael Chan6921d202005-12-13 21:15:53 -08002725 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08002726 int err;
2727
2728 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08002730 if (!err)
2731 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08002732 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 }
2734
Michael Chanbbadf502006-04-06 21:46:34 -07002735 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
2736
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002737 if (device_should_wake)
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07002738 pci_enable_wake(tp->pdev, state, true);
2739
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 /* Finally, set the new power state. */
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07002741 pci_set_power_state(tp->pdev, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 return 0;
2744}
2745
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
2747{
2748 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
2749 case MII_TG3_AUX_STAT_10HALF:
2750 *speed = SPEED_10;
2751 *duplex = DUPLEX_HALF;
2752 break;
2753
2754 case MII_TG3_AUX_STAT_10FULL:
2755 *speed = SPEED_10;
2756 *duplex = DUPLEX_FULL;
2757 break;
2758
2759 case MII_TG3_AUX_STAT_100HALF:
2760 *speed = SPEED_100;
2761 *duplex = DUPLEX_HALF;
2762 break;
2763
2764 case MII_TG3_AUX_STAT_100FULL:
2765 *speed = SPEED_100;
2766 *duplex = DUPLEX_FULL;
2767 break;
2768
2769 case MII_TG3_AUX_STAT_1000HALF:
2770 *speed = SPEED_1000;
2771 *duplex = DUPLEX_HALF;
2772 break;
2773
2774 case MII_TG3_AUX_STAT_1000FULL:
2775 *speed = SPEED_1000;
2776 *duplex = DUPLEX_FULL;
2777 break;
2778
2779 default:
Matt Carlson7f97a4b2009-08-25 10:10:03 +00002780 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07002781 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
2782 SPEED_10;
2783 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
2784 DUPLEX_HALF;
2785 break;
2786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 *speed = SPEED_INVALID;
2788 *duplex = DUPLEX_INVALID;
2789 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791}
2792
2793static void tg3_phy_copper_begin(struct tg3 *tp)
2794{
2795 u32 new_adv;
2796 int i;
2797
2798 if (tp->link_config.phy_is_low_power) {
2799 /* Entering low power mode. Disable gigabit and
2800 * 100baseT advertisements.
2801 */
2802 tg3_writephy(tp, MII_TG3_CTRL, 0);
2803
2804 new_adv = (ADVERTISE_10HALF | ADVERTISE_10FULL |
2805 ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
2806 if (tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB)
2807 new_adv |= (ADVERTISE_100HALF | ADVERTISE_100FULL);
2808
2809 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2810 } else if (tp->link_config.speed == SPEED_INVALID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 if (tp->tg3_flags & TG3_FLAG_10_100_ONLY)
2812 tp->link_config.advertising &=
2813 ~(ADVERTISED_1000baseT_Half |
2814 ADVERTISED_1000baseT_Full);
2815
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002816 new_adv = ADVERTISE_CSMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 if (tp->link_config.advertising & ADVERTISED_10baseT_Half)
2818 new_adv |= ADVERTISE_10HALF;
2819 if (tp->link_config.advertising & ADVERTISED_10baseT_Full)
2820 new_adv |= ADVERTISE_10FULL;
2821 if (tp->link_config.advertising & ADVERTISED_100baseT_Half)
2822 new_adv |= ADVERTISE_100HALF;
2823 if (tp->link_config.advertising & ADVERTISED_100baseT_Full)
2824 new_adv |= ADVERTISE_100FULL;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002825
2826 new_adv |= tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2827
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2829
2830 if (tp->link_config.advertising &
2831 (ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full)) {
2832 new_adv = 0;
2833 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
2834 new_adv |= MII_TG3_CTRL_ADV_1000_HALF;
2835 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
2836 new_adv |= MII_TG3_CTRL_ADV_1000_FULL;
2837 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY) &&
2838 (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2839 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0))
2840 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2841 MII_TG3_CTRL_ENABLE_AS_MASTER);
2842 tg3_writephy(tp, MII_TG3_CTRL, new_adv);
2843 } else {
2844 tg3_writephy(tp, MII_TG3_CTRL, 0);
2845 }
2846 } else {
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002847 new_adv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2848 new_adv |= ADVERTISE_CSMA;
2849
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 /* Asking for a specific link mode. */
2851 if (tp->link_config.speed == SPEED_1000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2853
2854 if (tp->link_config.duplex == DUPLEX_FULL)
2855 new_adv = MII_TG3_CTRL_ADV_1000_FULL;
2856 else
2857 new_adv = MII_TG3_CTRL_ADV_1000_HALF;
2858 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2859 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
2860 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2861 MII_TG3_CTRL_ENABLE_AS_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 if (tp->link_config.speed == SPEED_100) {
2864 if (tp->link_config.duplex == DUPLEX_FULL)
2865 new_adv |= ADVERTISE_100FULL;
2866 else
2867 new_adv |= ADVERTISE_100HALF;
2868 } else {
2869 if (tp->link_config.duplex == DUPLEX_FULL)
2870 new_adv |= ADVERTISE_10FULL;
2871 else
2872 new_adv |= ADVERTISE_10HALF;
2873 }
2874 tg3_writephy(tp, MII_ADVERTISE, new_adv);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002875
2876 new_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002878
2879 tg3_writephy(tp, MII_TG3_CTRL, new_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 }
2881
2882 if (tp->link_config.autoneg == AUTONEG_DISABLE &&
2883 tp->link_config.speed != SPEED_INVALID) {
2884 u32 bmcr, orig_bmcr;
2885
2886 tp->link_config.active_speed = tp->link_config.speed;
2887 tp->link_config.active_duplex = tp->link_config.duplex;
2888
2889 bmcr = 0;
2890 switch (tp->link_config.speed) {
2891 default:
2892 case SPEED_10:
2893 break;
2894
2895 case SPEED_100:
2896 bmcr |= BMCR_SPEED100;
2897 break;
2898
2899 case SPEED_1000:
2900 bmcr |= TG3_BMCR_SPEED1000;
2901 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002902 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903
2904 if (tp->link_config.duplex == DUPLEX_FULL)
2905 bmcr |= BMCR_FULLDPLX;
2906
2907 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
2908 (bmcr != orig_bmcr)) {
2909 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
2910 for (i = 0; i < 1500; i++) {
2911 u32 tmp;
2912
2913 udelay(10);
2914 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
2915 tg3_readphy(tp, MII_BMSR, &tmp))
2916 continue;
2917 if (!(tmp & BMSR_LSTATUS)) {
2918 udelay(40);
2919 break;
2920 }
2921 }
2922 tg3_writephy(tp, MII_BMCR, bmcr);
2923 udelay(40);
2924 }
2925 } else {
2926 tg3_writephy(tp, MII_BMCR,
2927 BMCR_ANENABLE | BMCR_ANRESTART);
2928 }
2929}
2930
2931static int tg3_init_5401phy_dsp(struct tg3 *tp)
2932{
2933 int err;
2934
2935 /* Turn off tap power management. */
2936 /* Set Extended packet length bit */
2937 err = tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4c20);
2938
2939 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x0012);
2940 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x1804);
2941
2942 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x0013);
2943 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x1204);
2944
2945 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8006);
2946 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0132);
2947
2948 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8006);
2949 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0232);
2950
2951 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x201f);
2952 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0a20);
2953
2954 udelay(40);
2955
2956 return err;
2957}
2958
Michael Chan3600d912006-12-07 00:21:48 -08002959static int tg3_copper_is_advertising_all(struct tg3 *tp, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960{
Michael Chan3600d912006-12-07 00:21:48 -08002961 u32 adv_reg, all_mask = 0;
2962
2963 if (mask & ADVERTISED_10baseT_Half)
2964 all_mask |= ADVERTISE_10HALF;
2965 if (mask & ADVERTISED_10baseT_Full)
2966 all_mask |= ADVERTISE_10FULL;
2967 if (mask & ADVERTISED_100baseT_Half)
2968 all_mask |= ADVERTISE_100HALF;
2969 if (mask & ADVERTISED_100baseT_Full)
2970 all_mask |= ADVERTISE_100FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971
2972 if (tg3_readphy(tp, MII_ADVERTISE, &adv_reg))
2973 return 0;
2974
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 if ((adv_reg & all_mask) != all_mask)
2976 return 0;
2977 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY)) {
2978 u32 tg3_ctrl;
2979
Michael Chan3600d912006-12-07 00:21:48 -08002980 all_mask = 0;
2981 if (mask & ADVERTISED_1000baseT_Half)
2982 all_mask |= ADVERTISE_1000HALF;
2983 if (mask & ADVERTISED_1000baseT_Full)
2984 all_mask |= ADVERTISE_1000FULL;
2985
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 if (tg3_readphy(tp, MII_TG3_CTRL, &tg3_ctrl))
2987 return 0;
2988
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 if ((tg3_ctrl & all_mask) != all_mask)
2990 return 0;
2991 }
2992 return 1;
2993}
2994
Matt Carlsonef167e22007-12-20 20:10:01 -08002995static int tg3_adv_1000T_flowctrl_ok(struct tg3 *tp, u32 *lcladv, u32 *rmtadv)
2996{
2997 u32 curadv, reqadv;
2998
2999 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
3000 return 1;
3001
3002 curadv = *lcladv & (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
3003 reqadv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
3004
3005 if (tp->link_config.active_duplex == DUPLEX_FULL) {
3006 if (curadv != reqadv)
3007 return 0;
3008
3009 if (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG)
3010 tg3_readphy(tp, MII_LPA, rmtadv);
3011 } else {
3012 /* Reprogram the advertisement register, even if it
3013 * does not affect the current link. If the link
3014 * gets renegotiated in the future, we can save an
3015 * additional renegotiation cycle by advertising
3016 * it correctly in the first place.
3017 */
3018 if (curadv != reqadv) {
3019 *lcladv &= ~(ADVERTISE_PAUSE_CAP |
3020 ADVERTISE_PAUSE_ASYM);
3021 tg3_writephy(tp, MII_ADVERTISE, *lcladv | reqadv);
3022 }
3023 }
3024
3025 return 1;
3026}
3027
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
3029{
3030 int current_link_up;
3031 u32 bmsr, dummy;
Matt Carlsonef167e22007-12-20 20:10:01 -08003032 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 u16 current_speed;
3034 u8 current_duplex;
3035 int i, err;
3036
3037 tw32(MAC_EVENT, 0);
3038
3039 tw32_f(MAC_STATUS,
3040 (MAC_STATUS_SYNC_CHANGED |
3041 MAC_STATUS_CFG_CHANGED |
3042 MAC_STATUS_MI_COMPLETION |
3043 MAC_STATUS_LNKSTATE_CHANGED));
3044 udelay(40);
3045
Matt Carlson8ef21422008-05-02 16:47:53 -07003046 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
3047 tw32_f(MAC_MI_MODE,
3048 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
3049 udelay(80);
3050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051
3052 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x02);
3053
3054 /* Some third-party PHYs need to be reset on link going
3055 * down.
3056 */
3057 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3058 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
3059 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
3060 netif_carrier_ok(tp->dev)) {
3061 tg3_readphy(tp, MII_BMSR, &bmsr);
3062 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3063 !(bmsr & BMSR_LSTATUS))
3064 force_reset = 1;
3065 }
3066 if (force_reset)
3067 tg3_phy_reset(tp);
3068
3069 if ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5401) {
3070 tg3_readphy(tp, MII_BMSR, &bmsr);
3071 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
3072 !(tp->tg3_flags & TG3_FLAG_INIT_COMPLETE))
3073 bmsr = 0;
3074
3075 if (!(bmsr & BMSR_LSTATUS)) {
3076 err = tg3_init_5401phy_dsp(tp);
3077 if (err)
3078 return err;
3079
3080 tg3_readphy(tp, MII_BMSR, &bmsr);
3081 for (i = 0; i < 1000; i++) {
3082 udelay(10);
3083 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3084 (bmsr & BMSR_LSTATUS)) {
3085 udelay(40);
3086 break;
3087 }
3088 }
3089
3090 if ((tp->phy_id & PHY_ID_REV_MASK) == PHY_REV_BCM5401_B0 &&
3091 !(bmsr & BMSR_LSTATUS) &&
3092 tp->link_config.active_speed == SPEED_1000) {
3093 err = tg3_phy_reset(tp);
3094 if (!err)
3095 err = tg3_init_5401phy_dsp(tp);
3096 if (err)
3097 return err;
3098 }
3099 }
3100 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3101 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
3102 /* 5701 {A0,B0} CRC bug workaround */
3103 tg3_writephy(tp, 0x15, 0x0a75);
3104 tg3_writephy(tp, 0x1c, 0x8c68);
3105 tg3_writephy(tp, 0x1c, 0x8d68);
3106 tg3_writephy(tp, 0x1c, 0x8c68);
3107 }
3108
3109 /* Clear pending interrupts... */
3110 tg3_readphy(tp, MII_TG3_ISTAT, &dummy);
3111 tg3_readphy(tp, MII_TG3_ISTAT, &dummy);
3112
3113 if (tp->tg3_flags & TG3_FLAG_USE_MI_INTERRUPT)
3114 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlson7f97a4b2009-08-25 10:10:03 +00003115 else if (!(tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 tg3_writephy(tp, MII_TG3_IMASK, ~0);
3117
3118 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3119 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3120 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
3121 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3122 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
3123 else
3124 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
3125 }
3126
3127 current_link_up = 0;
3128 current_speed = SPEED_INVALID;
3129 current_duplex = DUPLEX_INVALID;
3130
3131 if (tp->tg3_flags2 & TG3_FLG2_CAPACITIVE_COUPLING) {
3132 u32 val;
3133
3134 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4007);
3135 tg3_readphy(tp, MII_TG3_AUX_CTRL, &val);
3136 if (!(val & (1 << 10))) {
3137 val |= (1 << 10);
3138 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
3139 goto relink;
3140 }
3141 }
3142
3143 bmsr = 0;
3144 for (i = 0; i < 100; i++) {
3145 tg3_readphy(tp, MII_BMSR, &bmsr);
3146 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3147 (bmsr & BMSR_LSTATUS))
3148 break;
3149 udelay(40);
3150 }
3151
3152 if (bmsr & BMSR_LSTATUS) {
3153 u32 aux_stat, bmcr;
3154
3155 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
3156 for (i = 0; i < 2000; i++) {
3157 udelay(10);
3158 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
3159 aux_stat)
3160 break;
3161 }
3162
3163 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
3164 &current_speed,
3165 &current_duplex);
3166
3167 bmcr = 0;
3168 for (i = 0; i < 200; i++) {
3169 tg3_readphy(tp, MII_BMCR, &bmcr);
3170 if (tg3_readphy(tp, MII_BMCR, &bmcr))
3171 continue;
3172 if (bmcr && bmcr != 0x7fff)
3173 break;
3174 udelay(10);
3175 }
3176
Matt Carlsonef167e22007-12-20 20:10:01 -08003177 lcl_adv = 0;
3178 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179
Matt Carlsonef167e22007-12-20 20:10:01 -08003180 tp->link_config.active_speed = current_speed;
3181 tp->link_config.active_duplex = current_duplex;
3182
3183 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3184 if ((bmcr & BMCR_ANENABLE) &&
3185 tg3_copper_is_advertising_all(tp,
3186 tp->link_config.advertising)) {
3187 if (tg3_adv_1000T_flowctrl_ok(tp, &lcl_adv,
3188 &rmt_adv))
3189 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 }
3191 } else {
3192 if (!(bmcr & BMCR_ANENABLE) &&
3193 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08003194 tp->link_config.duplex == current_duplex &&
3195 tp->link_config.flowctrl ==
3196 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 }
3199 }
3200
Matt Carlsonef167e22007-12-20 20:10:01 -08003201 if (current_link_up == 1 &&
3202 tp->link_config.active_duplex == DUPLEX_FULL)
3203 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 }
3205
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206relink:
Michael Chan6921d202005-12-13 21:15:53 -08003207 if (current_link_up == 0 || tp->link_config.phy_is_low_power) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 u32 tmp;
3209
3210 tg3_phy_copper_begin(tp);
3211
3212 tg3_readphy(tp, MII_BMSR, &tmp);
3213 if (!tg3_readphy(tp, MII_BMSR, &tmp) &&
3214 (tmp & BMSR_LSTATUS))
3215 current_link_up = 1;
3216 }
3217
3218 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
3219 if (current_link_up == 1) {
3220 if (tp->link_config.active_speed == SPEED_100 ||
3221 tp->link_config.active_speed == SPEED_10)
3222 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3223 else
3224 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlson7f97a4b2009-08-25 10:10:03 +00003225 } else if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET)
3226 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3227 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
3229
3230 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
3231 if (tp->link_config.active_duplex == DUPLEX_HALF)
3232 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
3233
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003235 if (current_link_up == 1 &&
3236 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003238 else
3239 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 }
3241
3242 /* ??? Without this setting Netgear GA302T PHY does not
3243 * ??? send/receive packets...
3244 */
3245 if ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5411 &&
3246 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
3247 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
3248 tw32_f(MAC_MI_MODE, tp->mi_mode);
3249 udelay(80);
3250 }
3251
3252 tw32_f(MAC_MODE, tp->mac_mode);
3253 udelay(40);
3254
3255 if (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) {
3256 /* Polled via timer. */
3257 tw32_f(MAC_EVENT, 0);
3258 } else {
3259 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
3260 }
3261 udelay(40);
3262
3263 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
3264 current_link_up == 1 &&
3265 tp->link_config.active_speed == SPEED_1000 &&
3266 ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) ||
3267 (tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED))) {
3268 udelay(120);
3269 tw32_f(MAC_STATUS,
3270 (MAC_STATUS_SYNC_CHANGED |
3271 MAC_STATUS_CFG_CHANGED));
3272 udelay(40);
3273 tg3_write_mem(tp,
3274 NIC_SRAM_FIRMWARE_MBOX,
3275 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
3276 }
3277
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003278 /* Prevent send BD corruption. */
3279 if (tp->tg3_flags3 & TG3_FLG3_CLKREQ_BUG) {
3280 u16 oldlnkctl, newlnkctl;
3281
3282 pci_read_config_word(tp->pdev,
3283 tp->pcie_cap + PCI_EXP_LNKCTL,
3284 &oldlnkctl);
3285 if (tp->link_config.active_speed == SPEED_100 ||
3286 tp->link_config.active_speed == SPEED_10)
3287 newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
3288 else
3289 newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
3290 if (newlnkctl != oldlnkctl)
3291 pci_write_config_word(tp->pdev,
3292 tp->pcie_cap + PCI_EXP_LNKCTL,
3293 newlnkctl);
3294 }
3295
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 if (current_link_up != netif_carrier_ok(tp->dev)) {
3297 if (current_link_up)
3298 netif_carrier_on(tp->dev);
3299 else
3300 netif_carrier_off(tp->dev);
3301 tg3_link_report(tp);
3302 }
3303
3304 return 0;
3305}
3306
3307struct tg3_fiber_aneginfo {
3308 int state;
3309#define ANEG_STATE_UNKNOWN 0
3310#define ANEG_STATE_AN_ENABLE 1
3311#define ANEG_STATE_RESTART_INIT 2
3312#define ANEG_STATE_RESTART 3
3313#define ANEG_STATE_DISABLE_LINK_OK 4
3314#define ANEG_STATE_ABILITY_DETECT_INIT 5
3315#define ANEG_STATE_ABILITY_DETECT 6
3316#define ANEG_STATE_ACK_DETECT_INIT 7
3317#define ANEG_STATE_ACK_DETECT 8
3318#define ANEG_STATE_COMPLETE_ACK_INIT 9
3319#define ANEG_STATE_COMPLETE_ACK 10
3320#define ANEG_STATE_IDLE_DETECT_INIT 11
3321#define ANEG_STATE_IDLE_DETECT 12
3322#define ANEG_STATE_LINK_OK 13
3323#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
3324#define ANEG_STATE_NEXT_PAGE_WAIT 15
3325
3326 u32 flags;
3327#define MR_AN_ENABLE 0x00000001
3328#define MR_RESTART_AN 0x00000002
3329#define MR_AN_COMPLETE 0x00000004
3330#define MR_PAGE_RX 0x00000008
3331#define MR_NP_LOADED 0x00000010
3332#define MR_TOGGLE_TX 0x00000020
3333#define MR_LP_ADV_FULL_DUPLEX 0x00000040
3334#define MR_LP_ADV_HALF_DUPLEX 0x00000080
3335#define MR_LP_ADV_SYM_PAUSE 0x00000100
3336#define MR_LP_ADV_ASYM_PAUSE 0x00000200
3337#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
3338#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
3339#define MR_LP_ADV_NEXT_PAGE 0x00001000
3340#define MR_TOGGLE_RX 0x00002000
3341#define MR_NP_RX 0x00004000
3342
3343#define MR_LINK_OK 0x80000000
3344
3345 unsigned long link_time, cur_time;
3346
3347 u32 ability_match_cfg;
3348 int ability_match_count;
3349
3350 char ability_match, idle_match, ack_match;
3351
3352 u32 txconfig, rxconfig;
3353#define ANEG_CFG_NP 0x00000080
3354#define ANEG_CFG_ACK 0x00000040
3355#define ANEG_CFG_RF2 0x00000020
3356#define ANEG_CFG_RF1 0x00000010
3357#define ANEG_CFG_PS2 0x00000001
3358#define ANEG_CFG_PS1 0x00008000
3359#define ANEG_CFG_HD 0x00004000
3360#define ANEG_CFG_FD 0x00002000
3361#define ANEG_CFG_INVAL 0x00001f06
3362
3363};
3364#define ANEG_OK 0
3365#define ANEG_DONE 1
3366#define ANEG_TIMER_ENAB 2
3367#define ANEG_FAILED -1
3368
3369#define ANEG_STATE_SETTLE_TIME 10000
3370
3371static int tg3_fiber_aneg_smachine(struct tg3 *tp,
3372 struct tg3_fiber_aneginfo *ap)
3373{
Matt Carlson5be73b42007-12-20 20:09:29 -08003374 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 unsigned long delta;
3376 u32 rx_cfg_reg;
3377 int ret;
3378
3379 if (ap->state == ANEG_STATE_UNKNOWN) {
3380 ap->rxconfig = 0;
3381 ap->link_time = 0;
3382 ap->cur_time = 0;
3383 ap->ability_match_cfg = 0;
3384 ap->ability_match_count = 0;
3385 ap->ability_match = 0;
3386 ap->idle_match = 0;
3387 ap->ack_match = 0;
3388 }
3389 ap->cur_time++;
3390
3391 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
3392 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
3393
3394 if (rx_cfg_reg != ap->ability_match_cfg) {
3395 ap->ability_match_cfg = rx_cfg_reg;
3396 ap->ability_match = 0;
3397 ap->ability_match_count = 0;
3398 } else {
3399 if (++ap->ability_match_count > 1) {
3400 ap->ability_match = 1;
3401 ap->ability_match_cfg = rx_cfg_reg;
3402 }
3403 }
3404 if (rx_cfg_reg & ANEG_CFG_ACK)
3405 ap->ack_match = 1;
3406 else
3407 ap->ack_match = 0;
3408
3409 ap->idle_match = 0;
3410 } else {
3411 ap->idle_match = 1;
3412 ap->ability_match_cfg = 0;
3413 ap->ability_match_count = 0;
3414 ap->ability_match = 0;
3415 ap->ack_match = 0;
3416
3417 rx_cfg_reg = 0;
3418 }
3419
3420 ap->rxconfig = rx_cfg_reg;
3421 ret = ANEG_OK;
3422
3423 switch(ap->state) {
3424 case ANEG_STATE_UNKNOWN:
3425 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
3426 ap->state = ANEG_STATE_AN_ENABLE;
3427
3428 /* fallthru */
3429 case ANEG_STATE_AN_ENABLE:
3430 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
3431 if (ap->flags & MR_AN_ENABLE) {
3432 ap->link_time = 0;
3433 ap->cur_time = 0;
3434 ap->ability_match_cfg = 0;
3435 ap->ability_match_count = 0;
3436 ap->ability_match = 0;
3437 ap->idle_match = 0;
3438 ap->ack_match = 0;
3439
3440 ap->state = ANEG_STATE_RESTART_INIT;
3441 } else {
3442 ap->state = ANEG_STATE_DISABLE_LINK_OK;
3443 }
3444 break;
3445
3446 case ANEG_STATE_RESTART_INIT:
3447 ap->link_time = ap->cur_time;
3448 ap->flags &= ~(MR_NP_LOADED);
3449 ap->txconfig = 0;
3450 tw32(MAC_TX_AUTO_NEG, 0);
3451 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3452 tw32_f(MAC_MODE, tp->mac_mode);
3453 udelay(40);
3454
3455 ret = ANEG_TIMER_ENAB;
3456 ap->state = ANEG_STATE_RESTART;
3457
3458 /* fallthru */
3459 case ANEG_STATE_RESTART:
3460 delta = ap->cur_time - ap->link_time;
3461 if (delta > ANEG_STATE_SETTLE_TIME) {
3462 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
3463 } else {
3464 ret = ANEG_TIMER_ENAB;
3465 }
3466 break;
3467
3468 case ANEG_STATE_DISABLE_LINK_OK:
3469 ret = ANEG_DONE;
3470 break;
3471
3472 case ANEG_STATE_ABILITY_DETECT_INIT:
3473 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08003474 ap->txconfig = ANEG_CFG_FD;
3475 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3476 if (flowctrl & ADVERTISE_1000XPAUSE)
3477 ap->txconfig |= ANEG_CFG_PS1;
3478 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3479 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3481 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3482 tw32_f(MAC_MODE, tp->mac_mode);
3483 udelay(40);
3484
3485 ap->state = ANEG_STATE_ABILITY_DETECT;
3486 break;
3487
3488 case ANEG_STATE_ABILITY_DETECT:
3489 if (ap->ability_match != 0 && ap->rxconfig != 0) {
3490 ap->state = ANEG_STATE_ACK_DETECT_INIT;
3491 }
3492 break;
3493
3494 case ANEG_STATE_ACK_DETECT_INIT:
3495 ap->txconfig |= ANEG_CFG_ACK;
3496 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3497 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3498 tw32_f(MAC_MODE, tp->mac_mode);
3499 udelay(40);
3500
3501 ap->state = ANEG_STATE_ACK_DETECT;
3502
3503 /* fallthru */
3504 case ANEG_STATE_ACK_DETECT:
3505 if (ap->ack_match != 0) {
3506 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
3507 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
3508 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
3509 } else {
3510 ap->state = ANEG_STATE_AN_ENABLE;
3511 }
3512 } else if (ap->ability_match != 0 &&
3513 ap->rxconfig == 0) {
3514 ap->state = ANEG_STATE_AN_ENABLE;
3515 }
3516 break;
3517
3518 case ANEG_STATE_COMPLETE_ACK_INIT:
3519 if (ap->rxconfig & ANEG_CFG_INVAL) {
3520 ret = ANEG_FAILED;
3521 break;
3522 }
3523 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
3524 MR_LP_ADV_HALF_DUPLEX |
3525 MR_LP_ADV_SYM_PAUSE |
3526 MR_LP_ADV_ASYM_PAUSE |
3527 MR_LP_ADV_REMOTE_FAULT1 |
3528 MR_LP_ADV_REMOTE_FAULT2 |
3529 MR_LP_ADV_NEXT_PAGE |
3530 MR_TOGGLE_RX |
3531 MR_NP_RX);
3532 if (ap->rxconfig & ANEG_CFG_FD)
3533 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
3534 if (ap->rxconfig & ANEG_CFG_HD)
3535 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
3536 if (ap->rxconfig & ANEG_CFG_PS1)
3537 ap->flags |= MR_LP_ADV_SYM_PAUSE;
3538 if (ap->rxconfig & ANEG_CFG_PS2)
3539 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
3540 if (ap->rxconfig & ANEG_CFG_RF1)
3541 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
3542 if (ap->rxconfig & ANEG_CFG_RF2)
3543 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
3544 if (ap->rxconfig & ANEG_CFG_NP)
3545 ap->flags |= MR_LP_ADV_NEXT_PAGE;
3546
3547 ap->link_time = ap->cur_time;
3548
3549 ap->flags ^= (MR_TOGGLE_TX);
3550 if (ap->rxconfig & 0x0008)
3551 ap->flags |= MR_TOGGLE_RX;
3552 if (ap->rxconfig & ANEG_CFG_NP)
3553 ap->flags |= MR_NP_RX;
3554 ap->flags |= MR_PAGE_RX;
3555
3556 ap->state = ANEG_STATE_COMPLETE_ACK;
3557 ret = ANEG_TIMER_ENAB;
3558 break;
3559
3560 case ANEG_STATE_COMPLETE_ACK:
3561 if (ap->ability_match != 0 &&
3562 ap->rxconfig == 0) {
3563 ap->state = ANEG_STATE_AN_ENABLE;
3564 break;
3565 }
3566 delta = ap->cur_time - ap->link_time;
3567 if (delta > ANEG_STATE_SETTLE_TIME) {
3568 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
3569 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3570 } else {
3571 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
3572 !(ap->flags & MR_NP_RX)) {
3573 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3574 } else {
3575 ret = ANEG_FAILED;
3576 }
3577 }
3578 }
3579 break;
3580
3581 case ANEG_STATE_IDLE_DETECT_INIT:
3582 ap->link_time = ap->cur_time;
3583 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3584 tw32_f(MAC_MODE, tp->mac_mode);
3585 udelay(40);
3586
3587 ap->state = ANEG_STATE_IDLE_DETECT;
3588 ret = ANEG_TIMER_ENAB;
3589 break;
3590
3591 case ANEG_STATE_IDLE_DETECT:
3592 if (ap->ability_match != 0 &&
3593 ap->rxconfig == 0) {
3594 ap->state = ANEG_STATE_AN_ENABLE;
3595 break;
3596 }
3597 delta = ap->cur_time - ap->link_time;
3598 if (delta > ANEG_STATE_SETTLE_TIME) {
3599 /* XXX another gem from the Broadcom driver :( */
3600 ap->state = ANEG_STATE_LINK_OK;
3601 }
3602 break;
3603
3604 case ANEG_STATE_LINK_OK:
3605 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
3606 ret = ANEG_DONE;
3607 break;
3608
3609 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
3610 /* ??? unimplemented */
3611 break;
3612
3613 case ANEG_STATE_NEXT_PAGE_WAIT:
3614 /* ??? unimplemented */
3615 break;
3616
3617 default:
3618 ret = ANEG_FAILED;
3619 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003620 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621
3622 return ret;
3623}
3624
Matt Carlson5be73b42007-12-20 20:09:29 -08003625static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626{
3627 int res = 0;
3628 struct tg3_fiber_aneginfo aninfo;
3629 int status = ANEG_FAILED;
3630 unsigned int tick;
3631 u32 tmp;
3632
3633 tw32_f(MAC_TX_AUTO_NEG, 0);
3634
3635 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
3636 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
3637 udelay(40);
3638
3639 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
3640 udelay(40);
3641
3642 memset(&aninfo, 0, sizeof(aninfo));
3643 aninfo.flags |= MR_AN_ENABLE;
3644 aninfo.state = ANEG_STATE_UNKNOWN;
3645 aninfo.cur_time = 0;
3646 tick = 0;
3647 while (++tick < 195000) {
3648 status = tg3_fiber_aneg_smachine(tp, &aninfo);
3649 if (status == ANEG_DONE || status == ANEG_FAILED)
3650 break;
3651
3652 udelay(1);
3653 }
3654
3655 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3656 tw32_f(MAC_MODE, tp->mac_mode);
3657 udelay(40);
3658
Matt Carlson5be73b42007-12-20 20:09:29 -08003659 *txflags = aninfo.txconfig;
3660 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661
3662 if (status == ANEG_DONE &&
3663 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
3664 MR_LP_ADV_FULL_DUPLEX)))
3665 res = 1;
3666
3667 return res;
3668}
3669
3670static void tg3_init_bcm8002(struct tg3 *tp)
3671{
3672 u32 mac_status = tr32(MAC_STATUS);
3673 int i;
3674
3675 /* Reset when initting first time or we have a link. */
3676 if ((tp->tg3_flags & TG3_FLAG_INIT_COMPLETE) &&
3677 !(mac_status & MAC_STATUS_PCS_SYNCED))
3678 return;
3679
3680 /* Set PLL lock range. */
3681 tg3_writephy(tp, 0x16, 0x8007);
3682
3683 /* SW reset */
3684 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
3685
3686 /* Wait for reset to complete. */
3687 /* XXX schedule_timeout() ... */
3688 for (i = 0; i < 500; i++)
3689 udelay(10);
3690
3691 /* Config mode; select PMA/Ch 1 regs. */
3692 tg3_writephy(tp, 0x10, 0x8411);
3693
3694 /* Enable auto-lock and comdet, select txclk for tx. */
3695 tg3_writephy(tp, 0x11, 0x0a10);
3696
3697 tg3_writephy(tp, 0x18, 0x00a0);
3698 tg3_writephy(tp, 0x16, 0x41ff);
3699
3700 /* Assert and deassert POR. */
3701 tg3_writephy(tp, 0x13, 0x0400);
3702 udelay(40);
3703 tg3_writephy(tp, 0x13, 0x0000);
3704
3705 tg3_writephy(tp, 0x11, 0x0a50);
3706 udelay(40);
3707 tg3_writephy(tp, 0x11, 0x0a10);
3708
3709 /* Wait for signal to stabilize */
3710 /* XXX schedule_timeout() ... */
3711 for (i = 0; i < 15000; i++)
3712 udelay(10);
3713
3714 /* Deselect the channel register so we can read the PHYID
3715 * later.
3716 */
3717 tg3_writephy(tp, 0x10, 0x8011);
3718}
3719
3720static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
3721{
Matt Carlson82cd3d12007-12-20 20:09:00 -08003722 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 u32 sg_dig_ctrl, sg_dig_status;
3724 u32 serdes_cfg, expected_sg_dig_ctrl;
3725 int workaround, port_a;
3726 int current_link_up;
3727
3728 serdes_cfg = 0;
3729 expected_sg_dig_ctrl = 0;
3730 workaround = 0;
3731 port_a = 1;
3732 current_link_up = 0;
3733
3734 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
3735 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
3736 workaround = 1;
3737 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
3738 port_a = 0;
3739
3740 /* preserve bits 0-11,13,14 for signal pre-emphasis */
3741 /* preserve bits 20-23 for voltage regulator */
3742 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
3743 }
3744
3745 sg_dig_ctrl = tr32(SG_DIG_CTRL);
3746
3747 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003748 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 if (workaround) {
3750 u32 val = serdes_cfg;
3751
3752 if (port_a)
3753 val |= 0xc010000;
3754 else
3755 val |= 0x4010000;
3756 tw32_f(MAC_SERDES_CFG, val);
3757 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003758
3759 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 }
3761 if (mac_status & MAC_STATUS_PCS_SYNCED) {
3762 tg3_setup_flow_control(tp, 0, 0);
3763 current_link_up = 1;
3764 }
3765 goto out;
3766 }
3767
3768 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003769 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770
Matt Carlson82cd3d12007-12-20 20:09:00 -08003771 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3772 if (flowctrl & ADVERTISE_1000XPAUSE)
3773 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
3774 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3775 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776
3777 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003778 if ((tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT) &&
3779 tp->serdes_counter &&
3780 ((mac_status & (MAC_STATUS_PCS_SYNCED |
3781 MAC_STATUS_RCVD_CFG)) ==
3782 MAC_STATUS_PCS_SYNCED)) {
3783 tp->serdes_counter--;
3784 current_link_up = 1;
3785 goto out;
3786 }
3787restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 if (workaround)
3789 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003790 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 udelay(5);
3792 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
3793
Michael Chan3d3ebe72006-09-27 15:59:15 -07003794 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
3795 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
3797 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003798 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 mac_status = tr32(MAC_STATUS);
3800
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003801 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08003803 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804
Matt Carlson82cd3d12007-12-20 20:09:00 -08003805 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
3806 local_adv |= ADVERTISE_1000XPAUSE;
3807 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
3808 local_adv |= ADVERTISE_1000XPSE_ASYM;
3809
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003810 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003811 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003812 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003813 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814
3815 tg3_setup_flow_control(tp, local_adv, remote_adv);
3816 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003817 tp->serdes_counter = 0;
3818 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003819 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003820 if (tp->serdes_counter)
3821 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 else {
3823 if (workaround) {
3824 u32 val = serdes_cfg;
3825
3826 if (port_a)
3827 val |= 0xc010000;
3828 else
3829 val |= 0x4010000;
3830
3831 tw32_f(MAC_SERDES_CFG, val);
3832 }
3833
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003834 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835 udelay(40);
3836
3837 /* Link parallel detection - link is up */
3838 /* only if we have PCS_SYNC and not */
3839 /* receiving config code words */
3840 mac_status = tr32(MAC_STATUS);
3841 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
3842 !(mac_status & MAC_STATUS_RCVD_CFG)) {
3843 tg3_setup_flow_control(tp, 0, 0);
3844 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003845 tp->tg3_flags2 |=
3846 TG3_FLG2_PARALLEL_DETECT;
3847 tp->serdes_counter =
3848 SERDES_PARALLEL_DET_TIMEOUT;
3849 } else
3850 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 }
3852 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07003853 } else {
3854 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
3855 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856 }
3857
3858out:
3859 return current_link_up;
3860}
3861
3862static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
3863{
3864 int current_link_up = 0;
3865
Michael Chan5cf64b8a2007-05-05 12:11:21 -07003866 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868
3869 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08003870 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003872
Matt Carlson5be73b42007-12-20 20:09:29 -08003873 if (fiber_autoneg(tp, &txflags, &rxflags)) {
3874 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875
Matt Carlson5be73b42007-12-20 20:09:29 -08003876 if (txflags & ANEG_CFG_PS1)
3877 local_adv |= ADVERTISE_1000XPAUSE;
3878 if (txflags & ANEG_CFG_PS2)
3879 local_adv |= ADVERTISE_1000XPSE_ASYM;
3880
3881 if (rxflags & MR_LP_ADV_SYM_PAUSE)
3882 remote_adv |= LPA_1000XPAUSE;
3883 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
3884 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885
3886 tg3_setup_flow_control(tp, local_adv, remote_adv);
3887
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888 current_link_up = 1;
3889 }
3890 for (i = 0; i < 30; i++) {
3891 udelay(20);
3892 tw32_f(MAC_STATUS,
3893 (MAC_STATUS_SYNC_CHANGED |
3894 MAC_STATUS_CFG_CHANGED));
3895 udelay(40);
3896 if ((tr32(MAC_STATUS) &
3897 (MAC_STATUS_SYNC_CHANGED |
3898 MAC_STATUS_CFG_CHANGED)) == 0)
3899 break;
3900 }
3901
3902 mac_status = tr32(MAC_STATUS);
3903 if (current_link_up == 0 &&
3904 (mac_status & MAC_STATUS_PCS_SYNCED) &&
3905 !(mac_status & MAC_STATUS_RCVD_CFG))
3906 current_link_up = 1;
3907 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08003908 tg3_setup_flow_control(tp, 0, 0);
3909
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 /* Forcing 1000FD link up. */
3911 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912
3913 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
3914 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003915
3916 tw32_f(MAC_MODE, tp->mac_mode);
3917 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 }
3919
3920out:
3921 return current_link_up;
3922}
3923
3924static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
3925{
3926 u32 orig_pause_cfg;
3927 u16 orig_active_speed;
3928 u8 orig_active_duplex;
3929 u32 mac_status;
3930 int current_link_up;
3931 int i;
3932
Matt Carlson8d018622007-12-20 20:05:44 -08003933 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 orig_active_speed = tp->link_config.active_speed;
3935 orig_active_duplex = tp->link_config.active_duplex;
3936
3937 if (!(tp->tg3_flags2 & TG3_FLG2_HW_AUTONEG) &&
3938 netif_carrier_ok(tp->dev) &&
3939 (tp->tg3_flags & TG3_FLAG_INIT_COMPLETE)) {
3940 mac_status = tr32(MAC_STATUS);
3941 mac_status &= (MAC_STATUS_PCS_SYNCED |
3942 MAC_STATUS_SIGNAL_DET |
3943 MAC_STATUS_CFG_CHANGED |
3944 MAC_STATUS_RCVD_CFG);
3945 if (mac_status == (MAC_STATUS_PCS_SYNCED |
3946 MAC_STATUS_SIGNAL_DET)) {
3947 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
3948 MAC_STATUS_CFG_CHANGED));
3949 return 0;
3950 }
3951 }
3952
3953 tw32_f(MAC_TX_AUTO_NEG, 0);
3954
3955 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
3956 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
3957 tw32_f(MAC_MODE, tp->mac_mode);
3958 udelay(40);
3959
3960 if (tp->phy_id == PHY_ID_BCM8002)
3961 tg3_init_bcm8002(tp);
3962
3963 /* Enable link change event even when serdes polling. */
3964 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
3965 udelay(40);
3966
3967 current_link_up = 0;
3968 mac_status = tr32(MAC_STATUS);
3969
3970 if (tp->tg3_flags2 & TG3_FLG2_HW_AUTONEG)
3971 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
3972 else
3973 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
3974
Matt Carlson898a56f2009-08-28 14:02:40 +00003975 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00003977 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978
3979 for (i = 0; i < 100; i++) {
3980 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
3981 MAC_STATUS_CFG_CHANGED));
3982 udelay(5);
3983 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07003984 MAC_STATUS_CFG_CHANGED |
3985 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 break;
3987 }
3988
3989 mac_status = tr32(MAC_STATUS);
3990 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
3991 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003992 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
3993 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 tw32_f(MAC_MODE, (tp->mac_mode |
3995 MAC_MODE_SEND_CONFIGS));
3996 udelay(1);
3997 tw32_f(MAC_MODE, tp->mac_mode);
3998 }
3999 }
4000
4001 if (current_link_up == 1) {
4002 tp->link_config.active_speed = SPEED_1000;
4003 tp->link_config.active_duplex = DUPLEX_FULL;
4004 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4005 LED_CTRL_LNKLED_OVERRIDE |
4006 LED_CTRL_1000MBPS_ON));
4007 } else {
4008 tp->link_config.active_speed = SPEED_INVALID;
4009 tp->link_config.active_duplex = DUPLEX_INVALID;
4010 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4011 LED_CTRL_LNKLED_OVERRIDE |
4012 LED_CTRL_TRAFFIC_OVERRIDE));
4013 }
4014
4015 if (current_link_up != netif_carrier_ok(tp->dev)) {
4016 if (current_link_up)
4017 netif_carrier_on(tp->dev);
4018 else
4019 netif_carrier_off(tp->dev);
4020 tg3_link_report(tp);
4021 } else {
Matt Carlson8d018622007-12-20 20:05:44 -08004022 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 if (orig_pause_cfg != now_pause_cfg ||
4024 orig_active_speed != tp->link_config.active_speed ||
4025 orig_active_duplex != tp->link_config.active_duplex)
4026 tg3_link_report(tp);
4027 }
4028
4029 return 0;
4030}
4031
Michael Chan747e8f82005-07-25 12:33:22 -07004032static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
4033{
4034 int current_link_up, err = 0;
4035 u32 bmsr, bmcr;
4036 u16 current_speed;
4037 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08004038 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07004039
4040 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4041 tw32_f(MAC_MODE, tp->mac_mode);
4042 udelay(40);
4043
4044 tw32(MAC_EVENT, 0);
4045
4046 tw32_f(MAC_STATUS,
4047 (MAC_STATUS_SYNC_CHANGED |
4048 MAC_STATUS_CFG_CHANGED |
4049 MAC_STATUS_MI_COMPLETION |
4050 MAC_STATUS_LNKSTATE_CHANGED));
4051 udelay(40);
4052
4053 if (force_reset)
4054 tg3_phy_reset(tp);
4055
4056 current_link_up = 0;
4057 current_speed = SPEED_INVALID;
4058 current_duplex = DUPLEX_INVALID;
4059
4060 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4061 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004062 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
4063 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4064 bmsr |= BMSR_LSTATUS;
4065 else
4066 bmsr &= ~BMSR_LSTATUS;
4067 }
Michael Chan747e8f82005-07-25 12:33:22 -07004068
4069 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
4070
4071 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlson2bd3ed02008-06-09 15:39:55 -07004072 (tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004073 /* do nothing, just check for link up at the end */
4074 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4075 u32 adv, new_adv;
4076
4077 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4078 new_adv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
4079 ADVERTISE_1000XPAUSE |
4080 ADVERTISE_1000XPSE_ASYM |
4081 ADVERTISE_SLCT);
4082
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004083 new_adv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Michael Chan747e8f82005-07-25 12:33:22 -07004084
4085 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
4086 new_adv |= ADVERTISE_1000XHALF;
4087 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
4088 new_adv |= ADVERTISE_1000XFULL;
4089
4090 if ((new_adv != adv) || !(bmcr & BMCR_ANENABLE)) {
4091 tg3_writephy(tp, MII_ADVERTISE, new_adv);
4092 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
4093 tg3_writephy(tp, MII_BMCR, bmcr);
4094
4095 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07004096 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Michael Chan747e8f82005-07-25 12:33:22 -07004097 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
4098
4099 return err;
4100 }
4101 } else {
4102 u32 new_bmcr;
4103
4104 bmcr &= ~BMCR_SPEED1000;
4105 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
4106
4107 if (tp->link_config.duplex == DUPLEX_FULL)
4108 new_bmcr |= BMCR_FULLDPLX;
4109
4110 if (new_bmcr != bmcr) {
4111 /* BMCR_SPEED1000 is a reserved bit that needs
4112 * to be set on write.
4113 */
4114 new_bmcr |= BMCR_SPEED1000;
4115
4116 /* Force a linkdown */
4117 if (netif_carrier_ok(tp->dev)) {
4118 u32 adv;
4119
4120 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4121 adv &= ~(ADVERTISE_1000XFULL |
4122 ADVERTISE_1000XHALF |
4123 ADVERTISE_SLCT);
4124 tg3_writephy(tp, MII_ADVERTISE, adv);
4125 tg3_writephy(tp, MII_BMCR, bmcr |
4126 BMCR_ANRESTART |
4127 BMCR_ANENABLE);
4128 udelay(10);
4129 netif_carrier_off(tp->dev);
4130 }
4131 tg3_writephy(tp, MII_BMCR, new_bmcr);
4132 bmcr = new_bmcr;
4133 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4134 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004135 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
4136 ASIC_REV_5714) {
4137 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4138 bmsr |= BMSR_LSTATUS;
4139 else
4140 bmsr &= ~BMSR_LSTATUS;
4141 }
Michael Chan747e8f82005-07-25 12:33:22 -07004142 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
4143 }
4144 }
4145
4146 if (bmsr & BMSR_LSTATUS) {
4147 current_speed = SPEED_1000;
4148 current_link_up = 1;
4149 if (bmcr & BMCR_FULLDPLX)
4150 current_duplex = DUPLEX_FULL;
4151 else
4152 current_duplex = DUPLEX_HALF;
4153
Matt Carlsonef167e22007-12-20 20:10:01 -08004154 local_adv = 0;
4155 remote_adv = 0;
4156
Michael Chan747e8f82005-07-25 12:33:22 -07004157 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08004158 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07004159
4160 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
4161 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
4162 common = local_adv & remote_adv;
4163 if (common & (ADVERTISE_1000XHALF |
4164 ADVERTISE_1000XFULL)) {
4165 if (common & ADVERTISE_1000XFULL)
4166 current_duplex = DUPLEX_FULL;
4167 else
4168 current_duplex = DUPLEX_HALF;
Michael Chan747e8f82005-07-25 12:33:22 -07004169 }
4170 else
4171 current_link_up = 0;
4172 }
4173 }
4174
Matt Carlsonef167e22007-12-20 20:10:01 -08004175 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
4176 tg3_setup_flow_control(tp, local_adv, remote_adv);
4177
Michael Chan747e8f82005-07-25 12:33:22 -07004178 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4179 if (tp->link_config.active_duplex == DUPLEX_HALF)
4180 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4181
4182 tw32_f(MAC_MODE, tp->mac_mode);
4183 udelay(40);
4184
4185 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4186
4187 tp->link_config.active_speed = current_speed;
4188 tp->link_config.active_duplex = current_duplex;
4189
4190 if (current_link_up != netif_carrier_ok(tp->dev)) {
4191 if (current_link_up)
4192 netif_carrier_on(tp->dev);
4193 else {
4194 netif_carrier_off(tp->dev);
4195 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
4196 }
4197 tg3_link_report(tp);
4198 }
4199 return err;
4200}
4201
4202static void tg3_serdes_parallel_detect(struct tg3 *tp)
4203{
Michael Chan3d3ebe72006-09-27 15:59:15 -07004204 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07004205 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07004206 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07004207 return;
4208 }
4209 if (!netif_carrier_ok(tp->dev) &&
4210 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
4211 u32 bmcr;
4212
4213 tg3_readphy(tp, MII_BMCR, &bmcr);
4214 if (bmcr & BMCR_ANENABLE) {
4215 u32 phy1, phy2;
4216
4217 /* Select shadow register 0x1f */
4218 tg3_writephy(tp, 0x1c, 0x7c00);
4219 tg3_readphy(tp, 0x1c, &phy1);
4220
4221 /* Select expansion interrupt status register */
4222 tg3_writephy(tp, 0x17, 0x0f01);
4223 tg3_readphy(tp, 0x15, &phy2);
4224 tg3_readphy(tp, 0x15, &phy2);
4225
4226 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
4227 /* We have signal detect and not receiving
4228 * config code words, link is up by parallel
4229 * detection.
4230 */
4231
4232 bmcr &= ~BMCR_ANENABLE;
4233 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
4234 tg3_writephy(tp, MII_BMCR, bmcr);
4235 tp->tg3_flags2 |= TG3_FLG2_PARALLEL_DETECT;
4236 }
4237 }
4238 }
4239 else if (netif_carrier_ok(tp->dev) &&
4240 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
4241 (tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT)) {
4242 u32 phy2;
4243
4244 /* Select expansion interrupt status register */
4245 tg3_writephy(tp, 0x17, 0x0f01);
4246 tg3_readphy(tp, 0x15, &phy2);
4247 if (phy2 & 0x20) {
4248 u32 bmcr;
4249
4250 /* Config code words received, turn on autoneg. */
4251 tg3_readphy(tp, MII_BMCR, &bmcr);
4252 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
4253
4254 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
4255
4256 }
4257 }
4258}
4259
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260static int tg3_setup_phy(struct tg3 *tp, int force_reset)
4261{
4262 int err;
4263
4264 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
4265 err = tg3_setup_fiber_phy(tp, force_reset);
Michael Chan747e8f82005-07-25 12:33:22 -07004266 } else if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES) {
4267 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 } else {
4269 err = tg3_setup_copper_phy(tp, force_reset);
4270 }
4271
Matt Carlsonbcb37f62008-11-03 16:52:09 -08004272 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08004273 u32 val, scale;
4274
4275 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
4276 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
4277 scale = 65;
4278 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
4279 scale = 6;
4280 else
4281 scale = 12;
4282
4283 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
4284 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
4285 tw32(GRC_MISC_CFG, val);
4286 }
4287
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 if (tp->link_config.active_speed == SPEED_1000 &&
4289 tp->link_config.active_duplex == DUPLEX_HALF)
4290 tw32(MAC_TX_LENGTHS,
4291 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4292 (6 << TX_LENGTHS_IPG_SHIFT) |
4293 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
4294 else
4295 tw32(MAC_TX_LENGTHS,
4296 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4297 (6 << TX_LENGTHS_IPG_SHIFT) |
4298 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
4299
4300 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
4301 if (netif_carrier_ok(tp->dev)) {
4302 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07004303 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 } else {
4305 tw32(HOSTCC_STAT_COAL_TICKS, 0);
4306 }
4307 }
4308
Matt Carlson8ed5d972007-05-07 00:25:49 -07004309 if (tp->tg3_flags & TG3_FLAG_ASPM_WORKAROUND) {
4310 u32 val = tr32(PCIE_PWR_MGMT_THRESH);
4311 if (!netif_carrier_ok(tp->dev))
4312 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
4313 tp->pwrmgmt_thresh;
4314 else
4315 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
4316 tw32(PCIE_PWR_MGMT_THRESH, val);
4317 }
4318
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 return err;
4320}
4321
Michael Chandf3e6542006-05-26 17:48:07 -07004322/* This is called whenever we suspect that the system chipset is re-
4323 * ordering the sequence of MMIO to the tx send mailbox. The symptom
4324 * is bogus tx completions. We try to recover by setting the
4325 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
4326 * in the workqueue.
4327 */
4328static void tg3_tx_recover(struct tg3 *tp)
4329{
4330 BUG_ON((tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER) ||
4331 tp->write32_tx_mbox == tg3_write_indirect_mbox);
4332
4333 printk(KERN_WARNING PFX "%s: The system may be re-ordering memory-"
4334 "mapped I/O cycles to the network device, attempting to "
4335 "recover. Please report the problem to the driver maintainer "
4336 "and include system chipset information.\n", tp->dev->name);
4337
4338 spin_lock(&tp->lock);
Michael Chandf3e6542006-05-26 17:48:07 -07004339 tp->tg3_flags |= TG3_FLAG_TX_RECOVERY_PENDING;
Michael Chandf3e6542006-05-26 17:48:07 -07004340 spin_unlock(&tp->lock);
4341}
4342
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004343static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07004344{
4345 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004346 return tnapi->tx_pending -
4347 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07004348}
4349
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350/* Tigon3 never reports partial packet sends. So we do not
4351 * need special logic to handle SKBs that have not had all
4352 * of their frags sent yet, like SunGEM does.
4353 */
Matt Carlson17375d22009-08-28 14:02:18 +00004354static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355{
Matt Carlson17375d22009-08-28 14:02:18 +00004356 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00004357 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004358 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004359 struct netdev_queue *txq;
4360 int index = tnapi - tp->napi;
4361
Matt Carlson19cfaec2009-12-03 08:36:20 +00004362 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004363 index--;
4364
4365 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366
4367 while (sw_idx != hw_idx) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00004368 struct ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07004370 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371
Michael Chandf3e6542006-05-26 17:48:07 -07004372 if (unlikely(skb == NULL)) {
4373 tg3_tx_recover(tp);
4374 return;
4375 }
4376
Alexander Duyckf4188d82009-12-02 16:48:38 +00004377 pci_unmap_single(tp->pdev,
4378 pci_unmap_addr(ri, mapping),
4379 skb_headlen(skb),
4380 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381
4382 ri->skb = NULL;
4383
4384 sw_idx = NEXT_TX(sw_idx);
4385
4386 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004387 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07004388 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
4389 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00004390
4391 pci_unmap_page(tp->pdev,
4392 pci_unmap_addr(ri, mapping),
4393 skb_shinfo(skb)->frags[i].size,
4394 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 sw_idx = NEXT_TX(sw_idx);
4396 }
4397
David S. Millerf47c11e2005-06-24 20:18:35 -07004398 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07004399
4400 if (unlikely(tx_bug)) {
4401 tg3_tx_recover(tp);
4402 return;
4403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 }
4405
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004406 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407
Michael Chan1b2a7202006-08-07 21:46:02 -07004408 /* Need to make the tx_cons update visible to tg3_start_xmit()
4409 * before checking for netif_queue_stopped(). Without the
4410 * memory barrier, there is a small possibility that tg3_start_xmit()
4411 * will miss it and cause the queue to be stopped forever.
4412 */
4413 smp_mb();
4414
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004415 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004416 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004417 __netif_tx_lock(txq, smp_processor_id());
4418 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004419 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004420 netif_tx_wake_queue(txq);
4421 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07004422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423}
4424
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004425static void tg3_rx_skb_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
4426{
4427 if (!ri->skb)
4428 return;
4429
4430 pci_unmap_single(tp->pdev, pci_unmap_addr(ri, mapping),
4431 map_sz, PCI_DMA_FROMDEVICE);
4432 dev_kfree_skb_any(ri->skb);
4433 ri->skb = NULL;
4434}
4435
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436/* Returns size of skb allocated or < 0 on error.
4437 *
4438 * We only need to fill in the address because the other members
4439 * of the RX descriptor are invariant, see tg3_init_rings.
4440 *
4441 * Note the purposeful assymetry of cpu vs. chip accesses. For
4442 * posting buffers we only dirty the first cache line of the RX
4443 * descriptor (containing the address). Whereas for the RX status
4444 * buffers the cpu only reads the last cacheline of the RX descriptor
4445 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
4446 */
Matt Carlson86b21e52009-11-13 13:03:45 +00004447static int tg3_alloc_rx_skb(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Matt Carlsona3896162009-11-13 13:03:44 +00004448 u32 opaque_key, u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449{
4450 struct tg3_rx_buffer_desc *desc;
4451 struct ring_info *map, *src_map;
4452 struct sk_buff *skb;
4453 dma_addr_t mapping;
4454 int skb_size, dest_idx;
4455
4456 src_map = NULL;
4457 switch (opaque_key) {
4458 case RXD_OPAQUE_RING_STD:
4459 dest_idx = dest_idx_unmasked % TG3_RX_RING_SIZE;
Matt Carlson21f581a2009-08-28 14:00:25 +00004460 desc = &tpr->rx_std[dest_idx];
4461 map = &tpr->rx_std_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004462 skb_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 break;
4464
4465 case RXD_OPAQUE_RING_JUMBO:
4466 dest_idx = dest_idx_unmasked % TG3_RX_JUMBO_RING_SIZE;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00004467 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00004468 map = &tpr->rx_jmb_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004469 skb_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 break;
4471
4472 default:
4473 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475
4476 /* Do not overwrite any of the map or rp information
4477 * until we are sure we can commit to a new buffer.
4478 *
4479 * Callers depend upon this behavior and assume that
4480 * we leave everything unchanged if we fail.
4481 */
Matt Carlson287be122009-08-28 13:58:46 +00004482 skb = netdev_alloc_skb(tp->dev, skb_size + tp->rx_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 if (skb == NULL)
4484 return -ENOMEM;
4485
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 skb_reserve(skb, tp->rx_offset);
4487
Matt Carlson287be122009-08-28 13:58:46 +00004488 mapping = pci_map_single(tp->pdev, skb->data, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 PCI_DMA_FROMDEVICE);
Matt Carlsona21771d2009-11-02 14:25:31 +00004490 if (pci_dma_mapping_error(tp->pdev, mapping)) {
4491 dev_kfree_skb(skb);
4492 return -EIO;
4493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494
4495 map->skb = skb;
4496 pci_unmap_addr_set(map, mapping, mapping);
4497
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 desc->addr_hi = ((u64)mapping >> 32);
4499 desc->addr_lo = ((u64)mapping & 0xffffffff);
4500
4501 return skb_size;
4502}
4503
4504/* We only need to move over in the address because the other
4505 * members of the RX descriptor are invariant. See notes above
4506 * tg3_alloc_rx_skb for full details.
4507 */
Matt Carlsona3896162009-11-13 13:03:44 +00004508static void tg3_recycle_rx(struct tg3_napi *tnapi,
4509 struct tg3_rx_prodring_set *dpr,
4510 u32 opaque_key, int src_idx,
4511 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512{
Matt Carlson17375d22009-08-28 14:02:18 +00004513 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
4515 struct ring_info *src_map, *dest_map;
4516 int dest_idx;
Matt Carlsona3896162009-11-13 13:03:44 +00004517 struct tg3_rx_prodring_set *spr = &tp->prodring[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518
4519 switch (opaque_key) {
4520 case RXD_OPAQUE_RING_STD:
4521 dest_idx = dest_idx_unmasked % TG3_RX_RING_SIZE;
Matt Carlsona3896162009-11-13 13:03:44 +00004522 dest_desc = &dpr->rx_std[dest_idx];
4523 dest_map = &dpr->rx_std_buffers[dest_idx];
4524 src_desc = &spr->rx_std[src_idx];
4525 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 break;
4527
4528 case RXD_OPAQUE_RING_JUMBO:
4529 dest_idx = dest_idx_unmasked % TG3_RX_JUMBO_RING_SIZE;
Matt Carlsona3896162009-11-13 13:03:44 +00004530 dest_desc = &dpr->rx_jmb[dest_idx].std;
4531 dest_map = &dpr->rx_jmb_buffers[dest_idx];
4532 src_desc = &spr->rx_jmb[src_idx].std;
4533 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 break;
4535
4536 default:
4537 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004538 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539
4540 dest_map->skb = src_map->skb;
4541 pci_unmap_addr_set(dest_map, mapping,
4542 pci_unmap_addr(src_map, mapping));
4543 dest_desc->addr_hi = src_desc->addr_hi;
4544 dest_desc->addr_lo = src_desc->addr_lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545 src_map->skb = NULL;
4546}
4547
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548/* The RX ring scheme is composed of multiple rings which post fresh
4549 * buffers to the chip, and one special ring the chip uses to report
4550 * status back to the host.
4551 *
4552 * The special ring reports the status of received packets to the
4553 * host. The chip does not write into the original descriptor the
4554 * RX buffer was obtained from. The chip simply takes the original
4555 * descriptor as provided by the host, updates the status and length
4556 * field, then writes this into the next status ring entry.
4557 *
4558 * Each ring the host uses to post buffers to the chip is described
4559 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
4560 * it is first placed into the on-chip ram. When the packet's length
4561 * is known, it walks down the TG3_BDINFO entries to select the ring.
4562 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
4563 * which is within the range of the new packet's length is chosen.
4564 *
4565 * The "separate ring for rx status" scheme may sound queer, but it makes
4566 * sense from a cache coherency perspective. If only the host writes
4567 * to the buffer post rings, and only the chip writes to the rx status
4568 * rings, then cache lines never move beyond shared-modified state.
4569 * If both the host and chip were to write into the same ring, cache line
4570 * eviction could occur since both entities want it in an exclusive state.
4571 */
Matt Carlson17375d22009-08-28 14:02:18 +00004572static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573{
Matt Carlson17375d22009-08-28 14:02:18 +00004574 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07004575 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00004576 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00004577 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07004578 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 int received;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004580 struct tg3_rx_prodring_set *tpr = tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004582 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583 /*
4584 * We need to order the read of hw_idx and the read of
4585 * the opaque cookie.
4586 */
4587 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588 work_mask = 0;
4589 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00004590 std_prod_idx = tpr->rx_std_prod_idx;
4591 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00004593 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00004594 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595 unsigned int len;
4596 struct sk_buff *skb;
4597 dma_addr_t dma_addr;
4598 u32 opaque_key, desc_idx, *post_ptr;
4599
4600 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
4601 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
4602 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004603 ri = &tp->prodring[0].rx_std_buffers[desc_idx];
Matt Carlson21f581a2009-08-28 14:00:25 +00004604 dma_addr = pci_unmap_addr(ri, mapping);
4605 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00004606 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07004607 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004609 ri = &tp->prodring[0].rx_jmb_buffers[desc_idx];
Matt Carlson21f581a2009-08-28 14:00:25 +00004610 dma_addr = pci_unmap_addr(ri, mapping);
4611 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00004612 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00004613 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615
4616 work_mask |= opaque_key;
4617
4618 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
4619 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
4620 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00004621 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 desc_idx, *post_ptr);
4623 drop_it_no_recycle:
4624 /* Other statistics kept track of by card. */
4625 tp->net_stats.rx_dropped++;
4626 goto next_pkt;
4627 }
4628
Matt Carlsonad829262008-11-21 17:16:16 -08004629 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
4630 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631
Joe Perches8e95a202009-12-03 07:58:21 +00004632 if (len > RX_COPY_THRESHOLD &&
4633 tp->rx_offset == NET_IP_ALIGN) {
4634 /* rx_offset will likely not equal NET_IP_ALIGN
4635 * if this is a 5701 card running in PCI-X mode
4636 * [see tg3_get_invariants()]
4637 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638 int skb_size;
4639
Matt Carlson86b21e52009-11-13 13:03:45 +00004640 skb_size = tg3_alloc_rx_skb(tp, tpr, opaque_key,
Matt Carlsonafc081f2009-11-13 13:03:43 +00004641 *post_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 if (skb_size < 0)
4643 goto drop_it;
4644
Matt Carlsonafc081f2009-11-13 13:03:43 +00004645 ri->skb = NULL;
4646
Matt Carlson287be122009-08-28 13:58:46 +00004647 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 PCI_DMA_FROMDEVICE);
4649
4650 skb_put(skb, len);
4651 } else {
4652 struct sk_buff *copy_skb;
4653
Matt Carlsona3896162009-11-13 13:03:44 +00004654 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 desc_idx, *post_ptr);
4656
Matt Carlsonad829262008-11-21 17:16:16 -08004657 copy_skb = netdev_alloc_skb(tp->dev,
4658 len + TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659 if (copy_skb == NULL)
4660 goto drop_it_no_recycle;
4661
Matt Carlsonad829262008-11-21 17:16:16 -08004662 skb_reserve(copy_skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 skb_put(copy_skb, len);
4664 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03004665 skb_copy_from_linear_data(skb, copy_skb->data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
4667
4668 /* We'll reuse the original ring buffer. */
4669 skb = copy_skb;
4670 }
4671
4672 if ((tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) &&
4673 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
4674 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
4675 >> RXD_TCPCSUM_SHIFT) == 0xffff))
4676 skb->ip_summed = CHECKSUM_UNNECESSARY;
4677 else
4678 skb->ip_summed = CHECKSUM_NONE;
4679
4680 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00004681
4682 if (len > (tp->dev->mtu + ETH_HLEN) &&
4683 skb->protocol != htons(ETH_P_8021Q)) {
4684 dev_kfree_skb(skb);
4685 goto next_pkt;
4686 }
4687
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688#if TG3_VLAN_TAG_USED
4689 if (tp->vlgrp != NULL &&
4690 desc->type_flags & RXD_FLAG_VLAN) {
Matt Carlson17375d22009-08-28 14:02:18 +00004691 vlan_gro_receive(&tnapi->napi, tp->vlgrp,
Matt Carlson8ef04422009-08-28 14:01:37 +00004692 desc->err_vlan & RXD_VLAN_MASK, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693 } else
4694#endif
Matt Carlson17375d22009-08-28 14:02:18 +00004695 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 received++;
4698 budget--;
4699
4700next_pkt:
4701 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07004702
4703 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
4704 u32 idx = *post_ptr % TG3_RX_RING_SIZE;
Matt Carlson66711e662009-11-13 13:03:49 +00004705 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, idx);
Michael Chanf92905d2006-06-29 20:14:29 -07004706 work_mask &= ~RXD_OPAQUE_RING_STD;
4707 rx_std_posted = 0;
4708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07004710 sw_idx++;
Eric Dumazet6b31a512007-02-06 13:29:21 -08004711 sw_idx &= (TG3_RX_RCB_RING_SIZE(tp) - 1);
Michael Chan52f6d692005-04-25 15:14:32 -07004712
4713 /* Refresh hw_idx to see if there is new work */
4714 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004715 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07004716 rmb();
4717 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718 }
4719
4720 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00004721 tnapi->rx_rcb_ptr = sw_idx;
4722 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723
4724 /* Refill RX ring(s). */
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004725 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS) || tnapi == &tp->napi[1]) {
4726 if (work_mask & RXD_OPAQUE_RING_STD) {
4727 tpr->rx_std_prod_idx = std_prod_idx % TG3_RX_RING_SIZE;
4728 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
4729 tpr->rx_std_prod_idx);
4730 }
4731 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
4732 tpr->rx_jmb_prod_idx = jmb_prod_idx %
4733 TG3_RX_JUMBO_RING_SIZE;
4734 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
4735 tpr->rx_jmb_prod_idx);
4736 }
4737 mmiowb();
4738 } else if (work_mask) {
4739 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
4740 * updated before the producer indices can be updated.
4741 */
4742 smp_wmb();
4743
Matt Carlson43619352009-11-13 13:03:47 +00004744 tpr->rx_std_prod_idx = std_prod_idx % TG3_RX_RING_SIZE;
Matt Carlson43619352009-11-13 13:03:47 +00004745 tpr->rx_jmb_prod_idx = jmb_prod_idx % TG3_RX_JUMBO_RING_SIZE;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004746
4747 napi_schedule(&tp->napi[1].napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749
4750 return received;
4751}
4752
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004753static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 /* handle link change and other phy events */
4756 if (!(tp->tg3_flags &
4757 (TG3_FLAG_USE_LINKCHG_REG |
4758 TG3_FLAG_POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004759 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
4760
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761 if (sblk->status & SD_STATUS_LINK_CHG) {
4762 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004763 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07004764 spin_lock(&tp->lock);
Matt Carlsondd477002008-05-25 23:45:58 -07004765 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
4766 tw32_f(MAC_STATUS,
4767 (MAC_STATUS_SYNC_CHANGED |
4768 MAC_STATUS_CFG_CHANGED |
4769 MAC_STATUS_MI_COMPLETION |
4770 MAC_STATUS_LNKSTATE_CHANGED));
4771 udelay(40);
4772 } else
4773 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07004774 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 }
4776 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004777}
4778
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004779static void tg3_rx_prodring_xfer(struct tg3 *tp,
4780 struct tg3_rx_prodring_set *dpr,
4781 struct tg3_rx_prodring_set *spr)
4782{
4783 u32 si, di, cpycnt, src_prod_idx;
4784 int i;
4785
4786 while (1) {
4787 src_prod_idx = spr->rx_std_prod_idx;
4788
4789 /* Make sure updates to the rx_std_buffers[] entries and the
4790 * standard producer index are seen in the correct order.
4791 */
4792 smp_rmb();
4793
4794 if (spr->rx_std_cons_idx == src_prod_idx)
4795 break;
4796
4797 if (spr->rx_std_cons_idx < src_prod_idx)
4798 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
4799 else
4800 cpycnt = TG3_RX_RING_SIZE - spr->rx_std_cons_idx;
4801
4802 cpycnt = min(cpycnt, TG3_RX_RING_SIZE - dpr->rx_std_prod_idx);
4803
4804 si = spr->rx_std_cons_idx;
4805 di = dpr->rx_std_prod_idx;
4806
4807 memcpy(&dpr->rx_std_buffers[di],
4808 &spr->rx_std_buffers[si],
4809 cpycnt * sizeof(struct ring_info));
4810
4811 for (i = 0; i < cpycnt; i++, di++, si++) {
4812 struct tg3_rx_buffer_desc *sbd, *dbd;
4813 sbd = &spr->rx_std[si];
4814 dbd = &dpr->rx_std[di];
4815 dbd->addr_hi = sbd->addr_hi;
4816 dbd->addr_lo = sbd->addr_lo;
4817 }
4818
4819 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) %
4820 TG3_RX_RING_SIZE;
4821 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) %
4822 TG3_RX_RING_SIZE;
4823 }
4824
4825 while (1) {
4826 src_prod_idx = spr->rx_jmb_prod_idx;
4827
4828 /* Make sure updates to the rx_jmb_buffers[] entries and
4829 * the jumbo producer index are seen in the correct order.
4830 */
4831 smp_rmb();
4832
4833 if (spr->rx_jmb_cons_idx == src_prod_idx)
4834 break;
4835
4836 if (spr->rx_jmb_cons_idx < src_prod_idx)
4837 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
4838 else
4839 cpycnt = TG3_RX_JUMBO_RING_SIZE - spr->rx_jmb_cons_idx;
4840
4841 cpycnt = min(cpycnt,
4842 TG3_RX_JUMBO_RING_SIZE - dpr->rx_jmb_prod_idx);
4843
4844 si = spr->rx_jmb_cons_idx;
4845 di = dpr->rx_jmb_prod_idx;
4846
4847 memcpy(&dpr->rx_jmb_buffers[di],
4848 &spr->rx_jmb_buffers[si],
4849 cpycnt * sizeof(struct ring_info));
4850
4851 for (i = 0; i < cpycnt; i++, di++, si++) {
4852 struct tg3_rx_buffer_desc *sbd, *dbd;
4853 sbd = &spr->rx_jmb[si].std;
4854 dbd = &dpr->rx_jmb[di].std;
4855 dbd->addr_hi = sbd->addr_hi;
4856 dbd->addr_lo = sbd->addr_lo;
4857 }
4858
4859 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) %
4860 TG3_RX_JUMBO_RING_SIZE;
4861 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) %
4862 TG3_RX_JUMBO_RING_SIZE;
4863 }
4864}
4865
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004866static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
4867{
4868 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869
4870 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004871 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00004872 tg3_tx(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07004873 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
Michael Chan4fd7ab52007-10-12 01:39:50 -07004874 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875 }
4876
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877 /* run RX thread, within the bounds set by NAPI.
4878 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004879 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004881 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00004882 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004884 if ((tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS) && tnapi == &tp->napi[1]) {
4885 int i;
4886 u32 std_prod_idx = tp->prodring[0].rx_std_prod_idx;
4887 u32 jmb_prod_idx = tp->prodring[0].rx_jmb_prod_idx;
4888
4889 for (i = 2; i < tp->irq_cnt; i++)
4890 tg3_rx_prodring_xfer(tp, tnapi->prodring,
4891 tp->napi[i].prodring);
4892
4893 wmb();
4894
4895 if (std_prod_idx != tp->prodring[0].rx_std_prod_idx) {
4896 u32 mbox = TG3_RX_STD_PROD_IDX_REG;
4897 tw32_rx_mbox(mbox, tp->prodring[0].rx_std_prod_idx);
4898 }
4899
4900 if (jmb_prod_idx != tp->prodring[0].rx_jmb_prod_idx) {
4901 u32 mbox = TG3_RX_JMB_PROD_IDX_REG;
4902 tw32_rx_mbox(mbox, tp->prodring[0].rx_jmb_prod_idx);
4903 }
4904
4905 mmiowb();
4906 }
4907
David S. Miller6f535762007-10-11 18:08:29 -07004908 return work_done;
4909}
David S. Millerf7383c22005-05-18 22:50:53 -07004910
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004911static int tg3_poll_msix(struct napi_struct *napi, int budget)
4912{
4913 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
4914 struct tg3 *tp = tnapi->tp;
4915 int work_done = 0;
4916 struct tg3_hw_status *sblk = tnapi->hw_status;
4917
4918 while (1) {
4919 work_done = tg3_poll_work(tnapi, work_done, budget);
4920
4921 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
4922 goto tx_recovery;
4923
4924 if (unlikely(work_done >= budget))
4925 break;
4926
4927 /* tp->last_tag is used in tg3_restart_ints() below
4928 * to tell the hw how much work has been processed,
4929 * so we must read it before checking for more work.
4930 */
4931 tnapi->last_tag = sblk->status_tag;
4932 tnapi->last_irq_tag = tnapi->last_tag;
4933 rmb();
4934
4935 /* check for RX/TX work to do */
4936 if (sblk->idx[0].tx_consumer == tnapi->tx_cons &&
4937 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr) {
4938 napi_complete(napi);
4939 /* Reenable interrupts. */
4940 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
4941 mmiowb();
4942 break;
4943 }
4944 }
4945
4946 return work_done;
4947
4948tx_recovery:
4949 /* work_done is guaranteed to be less than budget. */
4950 napi_complete(napi);
4951 schedule_work(&tp->reset_task);
4952 return work_done;
4953}
4954
David S. Miller6f535762007-10-11 18:08:29 -07004955static int tg3_poll(struct napi_struct *napi, int budget)
4956{
Matt Carlson8ef04422009-08-28 14:01:37 +00004957 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
4958 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07004959 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00004960 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07004961
4962 while (1) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004963 tg3_poll_link(tp);
4964
Matt Carlson17375d22009-08-28 14:02:18 +00004965 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07004966
4967 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
4968 goto tx_recovery;
4969
4970 if (unlikely(work_done >= budget))
4971 break;
4972
Michael Chan4fd7ab52007-10-12 01:39:50 -07004973 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) {
Matt Carlson17375d22009-08-28 14:02:18 +00004974 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07004975 * to tell the hw how much work has been processed,
4976 * so we must read it before checking for more work.
4977 */
Matt Carlson898a56f2009-08-28 14:02:40 +00004978 tnapi->last_tag = sblk->status_tag;
4979 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07004980 rmb();
4981 } else
4982 sblk->status &= ~SD_STATUS_UPDATED;
4983
Matt Carlson17375d22009-08-28 14:02:18 +00004984 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08004985 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00004986 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07004987 break;
4988 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989 }
4990
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004991 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07004992
4993tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07004994 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08004995 napi_complete(napi);
David S. Miller6f535762007-10-11 18:08:29 -07004996 schedule_work(&tp->reset_task);
Michael Chan4fd7ab52007-10-12 01:39:50 -07004997 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998}
4999
David S. Millerf47c11e2005-06-24 20:18:35 -07005000static void tg3_irq_quiesce(struct tg3 *tp)
5001{
Matt Carlson4f125f42009-09-01 12:55:02 +00005002 int i;
5003
David S. Millerf47c11e2005-06-24 20:18:35 -07005004 BUG_ON(tp->irq_sync);
5005
5006 tp->irq_sync = 1;
5007 smp_mb();
5008
Matt Carlson4f125f42009-09-01 12:55:02 +00005009 for (i = 0; i < tp->irq_cnt; i++)
5010 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07005011}
5012
5013static inline int tg3_irq_sync(struct tg3 *tp)
5014{
5015 return tp->irq_sync;
5016}
5017
5018/* Fully shutdown all tg3 driver activity elsewhere in the system.
5019 * If irq_sync is non-zero, then the IRQ handler must be synchronized
5020 * with as well. Most of the time, this is not necessary except when
5021 * shutting down the device.
5022 */
5023static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
5024{
Michael Chan46966542007-07-11 19:47:19 -07005025 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07005026 if (irq_sync)
5027 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07005028}
5029
5030static inline void tg3_full_unlock(struct tg3 *tp)
5031{
David S. Millerf47c11e2005-06-24 20:18:35 -07005032 spin_unlock_bh(&tp->lock);
5033}
5034
Michael Chanfcfa0a32006-03-20 22:28:41 -08005035/* One-shot MSI handler - Chip automatically disables interrupt
5036 * after sending MSI so driver doesn't have to do it.
5037 */
David Howells7d12e782006-10-05 14:55:46 +01005038static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08005039{
Matt Carlson09943a12009-08-28 14:01:57 +00005040 struct tg3_napi *tnapi = dev_id;
5041 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08005042
Matt Carlson898a56f2009-08-28 14:02:40 +00005043 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005044 if (tnapi->rx_rcb)
5045 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005046
5047 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005048 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005049
5050 return IRQ_HANDLED;
5051}
5052
Michael Chan88b06bc22005-04-21 17:13:25 -07005053/* MSI ISR - No need to check for interrupt sharing and no need to
5054 * flush status block and interrupt mailbox. PCI ordering rules
5055 * guarantee that MSI will arrive after the status block.
5056 */
David Howells7d12e782006-10-05 14:55:46 +01005057static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07005058{
Matt Carlson09943a12009-08-28 14:01:57 +00005059 struct tg3_napi *tnapi = dev_id;
5060 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07005061
Matt Carlson898a56f2009-08-28 14:02:40 +00005062 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005063 if (tnapi->rx_rcb)
5064 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07005065 /*
David S. Millerfac9b832005-05-18 22:46:34 -07005066 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07005067 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07005068 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07005069 * NIC to stop sending us irqs, engaging "in-intr-handler"
5070 * event coalescing.
5071 */
5072 tw32_mailbox(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07005073 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005074 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07005075
Michael Chan88b06bc22005-04-21 17:13:25 -07005076 return IRQ_RETVAL(1);
5077}
5078
David Howells7d12e782006-10-05 14:55:46 +01005079static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080{
Matt Carlson09943a12009-08-28 14:01:57 +00005081 struct tg3_napi *tnapi = dev_id;
5082 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005083 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084 unsigned int handled = 1;
5085
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086 /* In INTx mode, it is possible for the interrupt to arrive at
5087 * the CPU before the status block posted prior to the interrupt.
5088 * Reading the PCI State register will confirm whether the
5089 * interrupt is ours and will flush the status block.
5090 */
Michael Chand18edcb2007-03-24 20:57:11 -07005091 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
5092 if ((tp->tg3_flags & TG3_FLAG_CHIP_RESETTING) ||
5093 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5094 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005095 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07005096 }
Michael Chand18edcb2007-03-24 20:57:11 -07005097 }
5098
5099 /*
5100 * Writing any value to intr-mbox-0 clears PCI INTA# and
5101 * chip-internal interrupt pending events.
5102 * Writing non-zero to intr-mbox-0 additional tells the
5103 * NIC to stop sending us irqs, engaging "in-intr-handler"
5104 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005105 *
5106 * Flush the mailbox to de-assert the IRQ immediately to prevent
5107 * spurious interrupts. The flush impacts performance but
5108 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005109 */
Michael Chanc04cb342007-05-07 00:26:15 -07005110 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07005111 if (tg3_irq_sync(tp))
5112 goto out;
5113 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00005114 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00005115 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00005116 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07005117 } else {
5118 /* No work, shared interrupt perhaps? re-enable
5119 * interrupts, and flush that PCI write
5120 */
5121 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
5122 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07005123 }
David S. Millerf47c11e2005-06-24 20:18:35 -07005124out:
David S. Millerfac9b832005-05-18 22:46:34 -07005125 return IRQ_RETVAL(handled);
5126}
5127
David Howells7d12e782006-10-05 14:55:46 +01005128static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07005129{
Matt Carlson09943a12009-08-28 14:01:57 +00005130 struct tg3_napi *tnapi = dev_id;
5131 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005132 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07005133 unsigned int handled = 1;
5134
David S. Millerfac9b832005-05-18 22:46:34 -07005135 /* In INTx mode, it is possible for the interrupt to arrive at
5136 * the CPU before the status block posted prior to the interrupt.
5137 * Reading the PCI State register will confirm whether the
5138 * interrupt is ours and will flush the status block.
5139 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005140 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Michael Chand18edcb2007-03-24 20:57:11 -07005141 if ((tp->tg3_flags & TG3_FLAG_CHIP_RESETTING) ||
5142 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5143 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005144 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145 }
Michael Chand18edcb2007-03-24 20:57:11 -07005146 }
5147
5148 /*
5149 * writing any value to intr-mbox-0 clears PCI INTA# and
5150 * chip-internal interrupt pending events.
5151 * writing non-zero to intr-mbox-0 additional tells the
5152 * NIC to stop sending us irqs, engaging "in-intr-handler"
5153 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005154 *
5155 * Flush the mailbox to de-assert the IRQ immediately to prevent
5156 * spurious interrupts. The flush impacts performance but
5157 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005158 */
Michael Chanc04cb342007-05-07 00:26:15 -07005159 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00005160
5161 /*
5162 * In a shared interrupt configuration, sometimes other devices'
5163 * interrupts will scream. We record the current status tag here
5164 * so that the above check can report that the screaming interrupts
5165 * are unhandled. Eventually they will be silenced.
5166 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005167 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00005168
Michael Chand18edcb2007-03-24 20:57:11 -07005169 if (tg3_irq_sync(tp))
5170 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00005171
Matt Carlson72334482009-08-28 14:03:01 +00005172 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00005173
Matt Carlson09943a12009-08-28 14:01:57 +00005174 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00005175
David S. Millerf47c11e2005-06-24 20:18:35 -07005176out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177 return IRQ_RETVAL(handled);
5178}
5179
Michael Chan79381092005-04-21 17:13:59 -07005180/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01005181static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07005182{
Matt Carlson09943a12009-08-28 14:01:57 +00005183 struct tg3_napi *tnapi = dev_id;
5184 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005185 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07005186
Michael Chanf9804dd2005-09-27 12:13:10 -07005187 if ((sblk->status & SD_STATUS_UPDATED) ||
5188 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07005189 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07005190 return IRQ_RETVAL(1);
5191 }
5192 return IRQ_RETVAL(0);
5193}
5194
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07005195static int tg3_init_hw(struct tg3 *, int);
Michael Chan944d9802005-05-29 14:57:48 -07005196static int tg3_halt(struct tg3 *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197
Michael Chanb9ec6c12006-07-25 16:37:27 -07005198/* Restart hardware after configuration changes, self-test, etc.
5199 * Invoked with tp->lock held.
5200 */
5201static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
Eric Dumazet78c61462008-04-24 23:33:06 -07005202 __releases(tp->lock)
5203 __acquires(tp->lock)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005204{
5205 int err;
5206
5207 err = tg3_init_hw(tp, reset_phy);
5208 if (err) {
5209 printk(KERN_ERR PFX "%s: Failed to re-initialize device, "
5210 "aborting.\n", tp->dev->name);
5211 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
5212 tg3_full_unlock(tp);
5213 del_timer_sync(&tp->timer);
5214 tp->irq_sync = 0;
Matt Carlsonfed97812009-09-01 13:10:19 +00005215 tg3_napi_enable(tp);
Michael Chanb9ec6c12006-07-25 16:37:27 -07005216 dev_close(tp->dev);
5217 tg3_full_lock(tp, 0);
5218 }
5219 return err;
5220}
5221
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222#ifdef CONFIG_NET_POLL_CONTROLLER
5223static void tg3_poll_controller(struct net_device *dev)
5224{
Matt Carlson4f125f42009-09-01 12:55:02 +00005225 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07005226 struct tg3 *tp = netdev_priv(dev);
5227
Matt Carlson4f125f42009-09-01 12:55:02 +00005228 for (i = 0; i < tp->irq_cnt; i++)
5229 tg3_interrupt(tp->napi[i].irq_vec, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230}
5231#endif
5232
David Howellsc4028952006-11-22 14:57:56 +00005233static void tg3_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234{
David Howellsc4028952006-11-22 14:57:56 +00005235 struct tg3 *tp = container_of(work, struct tg3, reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005236 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237 unsigned int restart_timer;
5238
Michael Chan7faa0062006-02-02 17:29:28 -08005239 tg3_full_lock(tp, 0);
Michael Chan7faa0062006-02-02 17:29:28 -08005240
5241 if (!netif_running(tp->dev)) {
Michael Chan7faa0062006-02-02 17:29:28 -08005242 tg3_full_unlock(tp);
5243 return;
5244 }
5245
5246 tg3_full_unlock(tp);
5247
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005248 tg3_phy_stop(tp);
5249
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250 tg3_netif_stop(tp);
5251
David S. Millerf47c11e2005-06-24 20:18:35 -07005252 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253
5254 restart_timer = tp->tg3_flags2 & TG3_FLG2_RESTART_TIMER;
5255 tp->tg3_flags2 &= ~TG3_FLG2_RESTART_TIMER;
5256
Michael Chandf3e6542006-05-26 17:48:07 -07005257 if (tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING) {
5258 tp->write32_tx_mbox = tg3_write32_tx_mbox;
5259 tp->write32_rx_mbox = tg3_write_flush_reg32;
5260 tp->tg3_flags |= TG3_FLAG_MBOX_WRITE_REORDER;
5261 tp->tg3_flags &= ~TG3_FLAG_TX_RECOVERY_PENDING;
5262 }
5263
Michael Chan944d9802005-05-29 14:57:48 -07005264 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005265 err = tg3_init_hw(tp, 1);
5266 if (err)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005267 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268
5269 tg3_netif_start(tp);
5270
Linus Torvalds1da177e2005-04-16 15:20:36 -07005271 if (restart_timer)
5272 mod_timer(&tp->timer, jiffies + 1);
Michael Chan7faa0062006-02-02 17:29:28 -08005273
Michael Chanb9ec6c12006-07-25 16:37:27 -07005274out:
Michael Chan7faa0062006-02-02 17:29:28 -08005275 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005276
5277 if (!err)
5278 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279}
5280
Michael Chanb0408752007-02-13 12:18:30 -08005281static void tg3_dump_short_state(struct tg3 *tp)
5282{
5283 printk(KERN_ERR PFX "DEBUG: MAC_TX_STATUS[%08x] MAC_RX_STATUS[%08x]\n",
5284 tr32(MAC_TX_STATUS), tr32(MAC_RX_STATUS));
5285 printk(KERN_ERR PFX "DEBUG: RDMAC_STATUS[%08x] WDMAC_STATUS[%08x]\n",
5286 tr32(RDMAC_STATUS), tr32(WDMAC_STATUS));
5287}
5288
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289static void tg3_tx_timeout(struct net_device *dev)
5290{
5291 struct tg3 *tp = netdev_priv(dev);
5292
Michael Chanb0408752007-02-13 12:18:30 -08005293 if (netif_msg_tx_err(tp)) {
Michael Chan9f88f292006-12-07 00:22:54 -08005294 printk(KERN_ERR PFX "%s: transmit timed out, resetting\n",
5295 dev->name);
Michael Chanb0408752007-02-13 12:18:30 -08005296 tg3_dump_short_state(tp);
5297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298
5299 schedule_work(&tp->reset_task);
5300}
5301
Michael Chanc58ec932005-09-17 00:46:27 -07005302/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
5303static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
5304{
5305 u32 base = (u32) mapping & 0xffffffff;
5306
5307 return ((base > 0xffffdcc0) &&
5308 (base + len + 8 < base));
5309}
5310
Michael Chan72f2afb2006-03-06 19:28:35 -08005311/* Test for DMA addresses > 40-bit */
5312static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
5313 int len)
5314{
5315#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Michael Chan6728a8e2006-03-27 23:16:49 -08005316 if (tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG)
Yang Hongyang50cf1562009-04-06 19:01:14 -07005317 return (((u64) mapping + len) > DMA_BIT_MASK(40));
Michael Chan72f2afb2006-03-06 19:28:35 -08005318 return 0;
5319#else
5320 return 0;
5321#endif
5322}
5323
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005324static void tg3_set_txd(struct tg3_napi *, int, dma_addr_t, int, u32, u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325
Michael Chan72f2afb2006-03-06 19:28:35 -08005326/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00005327static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
5328 struct sk_buff *skb, u32 last_plus_one,
5329 u32 *start, u32 base_flags, u32 mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330{
Matt Carlson24f4efd2009-11-13 13:03:35 +00005331 struct tg3 *tp = tnapi->tp;
Matt Carlson41588ba12008-04-19 18:12:33 -07005332 struct sk_buff *new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07005333 dma_addr_t new_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334 u32 entry = *start;
Michael Chanc58ec932005-09-17 00:46:27 -07005335 int i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336
Matt Carlson41588ba12008-04-19 18:12:33 -07005337 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
5338 new_skb = skb_copy(skb, GFP_ATOMIC);
5339 else {
5340 int more_headroom = 4 - ((unsigned long)skb->data & 3);
5341
5342 new_skb = skb_copy_expand(skb,
5343 skb_headroom(skb) + more_headroom,
5344 skb_tailroom(skb), GFP_ATOMIC);
5345 }
5346
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07005348 ret = -1;
5349 } else {
5350 /* New SKB is guaranteed to be linear. */
5351 entry = *start;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005352 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
5353 PCI_DMA_TODEVICE);
5354 /* Make sure the mapping succeeded */
5355 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
5356 ret = -1;
5357 dev_kfree_skb(new_skb);
5358 new_skb = NULL;
David S. Miller90079ce2008-09-11 04:52:51 -07005359
Michael Chanc58ec932005-09-17 00:46:27 -07005360 /* Make sure new skb does not cross any 4G boundaries.
5361 * Drop the packet if it does.
5362 */
Alexander Duyckf4188d82009-12-02 16:48:38 +00005363 } else if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
5364 tg3_4g_overflow_test(new_addr, new_skb->len)) {
5365 pci_unmap_single(tp->pdev, new_addr, new_skb->len,
5366 PCI_DMA_TODEVICE);
Michael Chanc58ec932005-09-17 00:46:27 -07005367 ret = -1;
5368 dev_kfree_skb(new_skb);
5369 new_skb = NULL;
5370 } else {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005371 tg3_set_txd(tnapi, entry, new_addr, new_skb->len,
Michael Chanc58ec932005-09-17 00:46:27 -07005372 base_flags, 1 | (mss << 1));
5373 *start = NEXT_TX(entry);
5374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375 }
5376
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377 /* Now clean up the sw ring entries. */
5378 i = 0;
5379 while (entry != last_plus_one) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00005380 int len;
5381
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005382 if (i == 0)
Alexander Duyckf4188d82009-12-02 16:48:38 +00005383 len = skb_headlen(skb);
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005384 else
Alexander Duyckf4188d82009-12-02 16:48:38 +00005385 len = skb_shinfo(skb)->frags[i-1].size;
5386
5387 pci_unmap_single(tp->pdev,
5388 pci_unmap_addr(&tnapi->tx_buffers[entry],
5389 mapping),
5390 len, PCI_DMA_TODEVICE);
5391 if (i == 0) {
5392 tnapi->tx_buffers[entry].skb = new_skb;
5393 pci_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
5394 new_addr);
5395 } else {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005396 tnapi->tx_buffers[entry].skb = NULL;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398 entry = NEXT_TX(entry);
5399 i++;
5400 }
5401
5402 dev_kfree_skb(skb);
5403
Michael Chanc58ec932005-09-17 00:46:27 -07005404 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405}
5406
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005407static void tg3_set_txd(struct tg3_napi *tnapi, int entry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408 dma_addr_t mapping, int len, u32 flags,
5409 u32 mss_and_is_end)
5410{
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005411 struct tg3_tx_buffer_desc *txd = &tnapi->tx_ring[entry];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412 int is_end = (mss_and_is_end & 0x1);
5413 u32 mss = (mss_and_is_end >> 1);
5414 u32 vlan_tag = 0;
5415
5416 if (is_end)
5417 flags |= TXD_FLAG_END;
5418 if (flags & TXD_FLAG_VLAN) {
5419 vlan_tag = flags >> 16;
5420 flags &= 0xffff;
5421 }
5422 vlan_tag |= (mss << TXD_MSS_SHIFT);
5423
5424 txd->addr_hi = ((u64) mapping >> 32);
5425 txd->addr_lo = ((u64) mapping & 0xffffffff);
5426 txd->len_flags = (len << TXD_LEN_SHIFT) | flags;
5427 txd->vlan_tag = vlan_tag << TXD_VLAN_TAG_SHIFT;
5428}
5429
Michael Chan5a6f3072006-03-20 22:28:05 -08005430/* hard_start_xmit for devices that don't have any bugs and
Matt Carlsone849cdc2009-11-13 13:03:38 +00005431 * support TG3_FLG2_HW_TSO_2 and TG3_FLG2_HW_TSO_3 only.
Michael Chan5a6f3072006-03-20 22:28:05 -08005432 */
Stephen Hemminger613573252009-08-31 19:50:58 +00005433static netdev_tx_t tg3_start_xmit(struct sk_buff *skb,
5434 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435{
5436 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437 u32 len, entry, base_flags, mss;
David S. Miller90079ce2008-09-11 04:52:51 -07005438 dma_addr_t mapping;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005439 struct tg3_napi *tnapi;
5440 struct netdev_queue *txq;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005441 unsigned int i, last;
5442
Michael Chan5a6f3072006-03-20 22:28:05 -08005443
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005444 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
5445 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Matt Carlson19cfaec2009-12-03 08:36:20 +00005446 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005447 tnapi++;
Michael Chan5a6f3072006-03-20 22:28:05 -08005448
Michael Chan00b70502006-06-17 21:58:45 -07005449 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005450 * and TX reclaim runs via tp->napi.poll inside of a software
Michael Chan5a6f3072006-03-20 22:28:05 -08005451 * interrupt. Furthermore, IRQ processing runs lockless so we have
5452 * no IRQ context deadlocks to worry about either. Rejoice!
5453 */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005454 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005455 if (!netif_tx_queue_stopped(txq)) {
5456 netif_tx_stop_queue(txq);
Michael Chan5a6f3072006-03-20 22:28:05 -08005457
5458 /* This is a hard error, log it. */
5459 printk(KERN_ERR PFX "%s: BUG! Tx Ring full when "
5460 "queue awake!\n", dev->name);
5461 }
Michael Chan5a6f3072006-03-20 22:28:05 -08005462 return NETDEV_TX_BUSY;
5463 }
5464
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005465 entry = tnapi->tx_prod;
Michael Chan5a6f3072006-03-20 22:28:05 -08005466 base_flags = 0;
Michael Chan5a6f3072006-03-20 22:28:05 -08005467 mss = 0;
Matt Carlsonc13e3712007-05-05 11:50:04 -07005468 if ((mss = skb_shinfo(skb)->gso_size) != 0) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005469 int tcp_opt_len, ip_tcp_len;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005470 u32 hdrlen;
Michael Chan5a6f3072006-03-20 22:28:05 -08005471
5472 if (skb_header_cloned(skb) &&
5473 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
5474 dev_kfree_skb(skb);
5475 goto out_unlock;
5476 }
5477
Michael Chanb0026622006-07-03 19:42:14 -07005478 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005479 hdrlen = skb_headlen(skb) - ETH_HLEN;
Michael Chanb0026622006-07-03 19:42:14 -07005480 else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005481 struct iphdr *iph = ip_hdr(skb);
5482
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07005483 tcp_opt_len = tcp_optlen(skb);
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -03005484 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
Michael Chanb0026622006-07-03 19:42:14 -07005485
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005486 iph->check = 0;
5487 iph->tot_len = htons(mss + ip_tcp_len + tcp_opt_len);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005488 hdrlen = ip_tcp_len + tcp_opt_len;
Michael Chanb0026622006-07-03 19:42:14 -07005489 }
Michael Chan5a6f3072006-03-20 22:28:05 -08005490
Matt Carlsone849cdc2009-11-13 13:03:38 +00005491 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005492 mss |= (hdrlen & 0xc) << 12;
5493 if (hdrlen & 0x10)
5494 base_flags |= 0x00000010;
5495 base_flags |= (hdrlen & 0x3e0) << 5;
5496 } else
5497 mss |= hdrlen << 9;
5498
Michael Chan5a6f3072006-03-20 22:28:05 -08005499 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
5500 TXD_FLAG_CPU_POST_DMA);
5501
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005502 tcp_hdr(skb)->check = 0;
Michael Chan5a6f3072006-03-20 22:28:05 -08005503
Michael Chan5a6f3072006-03-20 22:28:05 -08005504 }
Patrick McHardy84fa7932006-08-29 16:44:56 -07005505 else if (skb->ip_summed == CHECKSUM_PARTIAL)
Michael Chan5a6f3072006-03-20 22:28:05 -08005506 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Michael Chan5a6f3072006-03-20 22:28:05 -08005507#if TG3_VLAN_TAG_USED
5508 if (tp->vlgrp != NULL && vlan_tx_tag_present(skb))
5509 base_flags |= (TXD_FLAG_VLAN |
5510 (vlan_tx_tag_get(skb) << 16));
5511#endif
5512
Alexander Duyckf4188d82009-12-02 16:48:38 +00005513 len = skb_headlen(skb);
5514
5515 /* Queue skb data, a.k.a. the main skb fragment. */
5516 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
5517 if (pci_dma_mapping_error(tp->pdev, mapping)) {
David S. Miller90079ce2008-09-11 04:52:51 -07005518 dev_kfree_skb(skb);
5519 goto out_unlock;
5520 }
5521
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005522 tnapi->tx_buffers[entry].skb = skb;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005523 pci_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005524
Matt Carlsonb703df62009-12-03 08:36:21 +00005525 if ((tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005526 !mss && skb->len > ETH_DATA_LEN)
5527 base_flags |= TXD_FLAG_JMB_PKT;
5528
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005529 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
Michael Chan5a6f3072006-03-20 22:28:05 -08005530 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
5531
5532 entry = NEXT_TX(entry);
5533
5534 /* Now loop through additional data fragments, and queue them. */
5535 if (skb_shinfo(skb)->nr_frags > 0) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005536 last = skb_shinfo(skb)->nr_frags - 1;
5537 for (i = 0; i <= last; i++) {
5538 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5539
5540 len = frag->size;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005541 mapping = pci_map_page(tp->pdev,
5542 frag->page,
5543 frag->page_offset,
5544 len, PCI_DMA_TODEVICE);
5545 if (pci_dma_mapping_error(tp->pdev, mapping))
5546 goto dma_error;
5547
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005548 tnapi->tx_buffers[entry].skb = NULL;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005549 pci_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
5550 mapping);
Michael Chan5a6f3072006-03-20 22:28:05 -08005551
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005552 tg3_set_txd(tnapi, entry, mapping, len,
Michael Chan5a6f3072006-03-20 22:28:05 -08005553 base_flags, (i == last) | (mss << 1));
5554
5555 entry = NEXT_TX(entry);
5556 }
5557 }
5558
5559 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005560 tw32_tx_mbox(tnapi->prodmbox, entry);
Michael Chan5a6f3072006-03-20 22:28:05 -08005561
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005562 tnapi->tx_prod = entry;
5563 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005564 netif_tx_stop_queue(txq);
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005565 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005566 netif_tx_wake_queue(txq);
Michael Chan5a6f3072006-03-20 22:28:05 -08005567 }
5568
5569out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00005570 mmiowb();
Michael Chan5a6f3072006-03-20 22:28:05 -08005571
5572 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005573
5574dma_error:
5575 last = i;
5576 entry = tnapi->tx_prod;
5577 tnapi->tx_buffers[entry].skb = NULL;
5578 pci_unmap_single(tp->pdev,
5579 pci_unmap_addr(&tnapi->tx_buffers[entry], mapping),
5580 skb_headlen(skb),
5581 PCI_DMA_TODEVICE);
5582 for (i = 0; i <= last; i++) {
5583 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5584 entry = NEXT_TX(entry);
5585
5586 pci_unmap_page(tp->pdev,
5587 pci_unmap_addr(&tnapi->tx_buffers[entry],
5588 mapping),
5589 frag->size, PCI_DMA_TODEVICE);
5590 }
5591
5592 dev_kfree_skb(skb);
5593 return NETDEV_TX_OK;
Michael Chan5a6f3072006-03-20 22:28:05 -08005594}
5595
Stephen Hemminger613573252009-08-31 19:50:58 +00005596static netdev_tx_t tg3_start_xmit_dma_bug(struct sk_buff *,
5597 struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07005598
5599/* Use GSO to workaround a rare TSO bug that may be triggered when the
5600 * TSO header is greater than 80 bytes.
5601 */
5602static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
5603{
5604 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005605 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07005606
5607 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005608 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07005609 netif_stop_queue(tp->dev);
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005610 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08005611 return NETDEV_TX_BUSY;
5612
5613 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07005614 }
5615
5616 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07005617 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07005618 goto tg3_tso_bug_end;
5619
5620 do {
5621 nskb = segs;
5622 segs = segs->next;
5623 nskb->next = NULL;
5624 tg3_start_xmit_dma_bug(nskb, tp->dev);
5625 } while (segs);
5626
5627tg3_tso_bug_end:
5628 dev_kfree_skb(skb);
5629
5630 return NETDEV_TX_OK;
5631}
Michael Chan52c0fd82006-06-29 20:15:54 -07005632
Michael Chan5a6f3072006-03-20 22:28:05 -08005633/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
5634 * support TG3_FLG2_HW_TSO_1 or firmware TSO only.
5635 */
Stephen Hemminger613573252009-08-31 19:50:58 +00005636static netdev_tx_t tg3_start_xmit_dma_bug(struct sk_buff *skb,
5637 struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08005638{
5639 struct tg3 *tp = netdev_priv(dev);
Michael Chan5a6f3072006-03-20 22:28:05 -08005640 u32 len, entry, base_flags, mss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641 int would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07005642 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00005643 struct tg3_napi *tnapi;
5644 struct netdev_queue *txq;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005645 unsigned int i, last;
5646
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647
Matt Carlson24f4efd2009-11-13 13:03:35 +00005648 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
5649 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Matt Carlson19cfaec2009-12-03 08:36:20 +00005650 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlson24f4efd2009-11-13 13:03:35 +00005651 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652
Michael Chan00b70502006-06-17 21:58:45 -07005653 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005654 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07005655 * interrupt. Furthermore, IRQ processing runs lockless so we have
5656 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657 */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005658 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00005659 if (!netif_tx_queue_stopped(txq)) {
5660 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08005661
5662 /* This is a hard error, log it. */
5663 printk(KERN_ERR PFX "%s: BUG! Tx Ring full when "
5664 "queue awake!\n", dev->name);
5665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666 return NETDEV_TX_BUSY;
5667 }
5668
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005669 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07005671 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00005673
Matt Carlsonc13e3712007-05-05 11:50:04 -07005674 if ((mss = skb_shinfo(skb)->gso_size) != 0) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005675 struct iphdr *iph;
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005676 u32 tcp_opt_len, ip_tcp_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677
5678 if (skb_header_cloned(skb) &&
5679 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
5680 dev_kfree_skb(skb);
5681 goto out_unlock;
5682 }
5683
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07005684 tcp_opt_len = tcp_optlen(skb);
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -03005685 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686
Michael Chan52c0fd82006-06-29 20:15:54 -07005687 hdr_len = ip_tcp_len + tcp_opt_len;
5688 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Michael Chan7f62ad52007-02-20 23:25:40 -08005689 (tp->tg3_flags2 & TG3_FLG2_TSO_BUG))
Michael Chan52c0fd82006-06-29 20:15:54 -07005690 return (tg3_tso_bug(tp, skb));
5691
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
5693 TXD_FLAG_CPU_POST_DMA);
5694
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005695 iph = ip_hdr(skb);
5696 iph->check = 0;
5697 iph->tot_len = htons(mss + hdr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005698 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005699 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005701 } else
5702 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
5703 iph->daddr, 0,
5704 IPPROTO_TCP,
5705 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706
Matt Carlson615774f2009-11-13 13:03:39 +00005707 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) {
5708 mss |= (hdr_len & 0xc) << 12;
5709 if (hdr_len & 0x10)
5710 base_flags |= 0x00000010;
5711 base_flags |= (hdr_len & 0x3e0) << 5;
5712 } else if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_2)
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005713 mss |= hdr_len << 9;
5714 else if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_1) ||
5715 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005716 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717 int tsflags;
5718
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005719 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720 mss |= (tsflags << 11);
5721 }
5722 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005723 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724 int tsflags;
5725
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005726 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727 base_flags |= tsflags << 12;
5728 }
5729 }
5730 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731#if TG3_VLAN_TAG_USED
5732 if (tp->vlgrp != NULL && vlan_tx_tag_present(skb))
5733 base_flags |= (TXD_FLAG_VLAN |
5734 (vlan_tx_tag_get(skb) << 16));
5735#endif
5736
Matt Carlsonb703df62009-12-03 08:36:21 +00005737 if ((tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) &&
Matt Carlson615774f2009-11-13 13:03:39 +00005738 !mss && skb->len > ETH_DATA_LEN)
5739 base_flags |= TXD_FLAG_JMB_PKT;
5740
Alexander Duyckf4188d82009-12-02 16:48:38 +00005741 len = skb_headlen(skb);
5742
5743 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
5744 if (pci_dma_mapping_error(tp->pdev, mapping)) {
David S. Miller90079ce2008-09-11 04:52:51 -07005745 dev_kfree_skb(skb);
5746 goto out_unlock;
5747 }
5748
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005749 tnapi->tx_buffers[entry].skb = skb;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005750 pci_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751
5752 would_hit_hwbug = 0;
5753
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005754 if ((tp->tg3_flags3 & TG3_FLG3_SHORT_DMA_BUG) && len <= 8)
5755 would_hit_hwbug = 1;
5756
Matt Carlson0e1406d2009-11-02 12:33:33 +00005757 if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
5758 tg3_4g_overflow_test(mapping, len))
Matt Carlson41588ba12008-04-19 18:12:33 -07005759 would_hit_hwbug = 1;
Matt Carlson0e1406d2009-11-02 12:33:33 +00005760
5761 if ((tp->tg3_flags3 & TG3_FLG3_40BIT_DMA_LIMIT_BUG) &&
5762 tg3_40bit_overflow_test(tp, mapping, len))
5763 would_hit_hwbug = 1;
5764
5765 if (tp->tg3_flags3 & TG3_FLG3_5701_DMA_BUG)
Michael Chanc58ec932005-09-17 00:46:27 -07005766 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005768 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
5770
5771 entry = NEXT_TX(entry);
5772
5773 /* Now loop through additional data fragments, and queue them. */
5774 if (skb_shinfo(skb)->nr_frags > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775 last = skb_shinfo(skb)->nr_frags - 1;
5776 for (i = 0; i <= last; i++) {
5777 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5778
5779 len = frag->size;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005780 mapping = pci_map_page(tp->pdev,
5781 frag->page,
5782 frag->page_offset,
5783 len, PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005785 tnapi->tx_buffers[entry].skb = NULL;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005786 pci_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
5787 mapping);
5788 if (pci_dma_mapping_error(tp->pdev, mapping))
5789 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005791 if ((tp->tg3_flags3 & TG3_FLG3_SHORT_DMA_BUG) &&
5792 len <= 8)
5793 would_hit_hwbug = 1;
5794
Matt Carlson0e1406d2009-11-02 12:33:33 +00005795 if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
5796 tg3_4g_overflow_test(mapping, len))
Michael Chanc58ec932005-09-17 00:46:27 -07005797 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798
Matt Carlson0e1406d2009-11-02 12:33:33 +00005799 if ((tp->tg3_flags3 & TG3_FLG3_40BIT_DMA_LIMIT_BUG) &&
5800 tg3_40bit_overflow_test(tp, mapping, len))
Michael Chan72f2afb2006-03-06 19:28:35 -08005801 would_hit_hwbug = 1;
5802
Linus Torvalds1da177e2005-04-16 15:20:36 -07005803 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005804 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805 base_flags, (i == last)|(mss << 1));
5806 else
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005807 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808 base_flags, (i == last));
5809
5810 entry = NEXT_TX(entry);
5811 }
5812 }
5813
5814 if (would_hit_hwbug) {
5815 u32 last_plus_one = entry;
5816 u32 start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817
Michael Chanc58ec932005-09-17 00:46:27 -07005818 start = entry - 1 - skb_shinfo(skb)->nr_frags;
5819 start &= (TG3_TX_RING_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820
5821 /* If the workaround fails due to memory/mapping
5822 * failure, silently drop this packet.
5823 */
Matt Carlson24f4efd2009-11-13 13:03:35 +00005824 if (tigon3_dma_hwbug_workaround(tnapi, skb, last_plus_one,
Michael Chanc58ec932005-09-17 00:46:27 -07005825 &start, base_flags, mss))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826 goto out_unlock;
5827
5828 entry = start;
5829 }
5830
5831 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00005832 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005833
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005834 tnapi->tx_prod = entry;
5835 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00005836 netif_tx_stop_queue(txq);
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005837 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00005838 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07005839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840
5841out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00005842 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843
5844 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005845
5846dma_error:
5847 last = i;
5848 entry = tnapi->tx_prod;
5849 tnapi->tx_buffers[entry].skb = NULL;
5850 pci_unmap_single(tp->pdev,
5851 pci_unmap_addr(&tnapi->tx_buffers[entry], mapping),
5852 skb_headlen(skb),
5853 PCI_DMA_TODEVICE);
5854 for (i = 0; i <= last; i++) {
5855 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5856 entry = NEXT_TX(entry);
5857
5858 pci_unmap_page(tp->pdev,
5859 pci_unmap_addr(&tnapi->tx_buffers[entry],
5860 mapping),
5861 frag->size, PCI_DMA_TODEVICE);
5862 }
5863
5864 dev_kfree_skb(skb);
5865 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005866}
5867
5868static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
5869 int new_mtu)
5870{
5871 dev->mtu = new_mtu;
5872
Michael Chanef7f5ec2005-07-25 12:32:25 -07005873 if (new_mtu > ETH_DATA_LEN) {
Michael Chana4e2b342005-10-26 15:46:52 -07005874 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
Michael Chanef7f5ec2005-07-25 12:32:25 -07005875 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
5876 ethtool_op_set_tso(dev, 0);
5877 }
5878 else
5879 tp->tg3_flags |= TG3_FLAG_JUMBO_RING_ENABLE;
5880 } else {
Michael Chana4e2b342005-10-26 15:46:52 -07005881 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
Michael Chanef7f5ec2005-07-25 12:32:25 -07005882 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
Michael Chan0f893dc2005-07-25 12:30:38 -07005883 tp->tg3_flags &= ~TG3_FLAG_JUMBO_RING_ENABLE;
Michael Chanef7f5ec2005-07-25 12:32:25 -07005884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005885}
5886
5887static int tg3_change_mtu(struct net_device *dev, int new_mtu)
5888{
5889 struct tg3 *tp = netdev_priv(dev);
Michael Chanb9ec6c12006-07-25 16:37:27 -07005890 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891
5892 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
5893 return -EINVAL;
5894
5895 if (!netif_running(dev)) {
5896 /* We'll just catch it later when the
5897 * device is up'd.
5898 */
5899 tg3_set_mtu(dev, tp, new_mtu);
5900 return 0;
5901 }
5902
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005903 tg3_phy_stop(tp);
5904
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905 tg3_netif_stop(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07005906
5907 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908
Michael Chan944d9802005-05-29 14:57:48 -07005909 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005910
5911 tg3_set_mtu(dev, tp, new_mtu);
5912
Michael Chanb9ec6c12006-07-25 16:37:27 -07005913 err = tg3_restart_hw(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914
Michael Chanb9ec6c12006-07-25 16:37:27 -07005915 if (!err)
5916 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005917
David S. Millerf47c11e2005-06-24 20:18:35 -07005918 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005919
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005920 if (!err)
5921 tg3_phy_start(tp);
5922
Michael Chanb9ec6c12006-07-25 16:37:27 -07005923 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005924}
5925
Matt Carlson21f581a2009-08-28 14:00:25 +00005926static void tg3_rx_prodring_free(struct tg3 *tp,
5927 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005928{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005929 int i;
5930
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005931 if (tpr != &tp->prodring[0]) {
5932 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
5933 i = (i + 1) % TG3_RX_RING_SIZE)
5934 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
5935 tp->rx_pkt_map_sz);
5936
5937 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
5938 for (i = tpr->rx_jmb_cons_idx;
5939 i != tpr->rx_jmb_prod_idx;
5940 i = (i + 1) % TG3_RX_JUMBO_RING_SIZE) {
5941 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
5942 TG3_RX_JMB_MAP_SZ);
5943 }
5944 }
5945
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005946 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005949 for (i = 0; i < TG3_RX_RING_SIZE; i++)
5950 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
5951 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005952
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005953 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005954 for (i = 0; i < TG3_RX_JUMBO_RING_SIZE; i++)
5955 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
5956 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005957 }
5958}
5959
5960/* Initialize tx/rx rings for packet processing.
5961 *
5962 * The chip has been shut down and the driver detached from
5963 * the networking, so no interrupts or new tx packets will
5964 * end up in the driver. tp->{tx,}lock are held and thus
5965 * we may not sleep.
5966 */
Matt Carlson21f581a2009-08-28 14:00:25 +00005967static int tg3_rx_prodring_alloc(struct tg3 *tp,
5968 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005969{
Matt Carlson287be122009-08-28 13:58:46 +00005970 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005971
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005972 tpr->rx_std_cons_idx = 0;
5973 tpr->rx_std_prod_idx = 0;
5974 tpr->rx_jmb_cons_idx = 0;
5975 tpr->rx_jmb_prod_idx = 0;
5976
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005977 if (tpr != &tp->prodring[0]) {
5978 memset(&tpr->rx_std_buffers[0], 0, TG3_RX_STD_BUFF_RING_SIZE);
5979 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE)
5980 memset(&tpr->rx_jmb_buffers[0], 0,
5981 TG3_RX_JMB_BUFF_RING_SIZE);
5982 goto done;
5983 }
5984
Linus Torvalds1da177e2005-04-16 15:20:36 -07005985 /* Zero out all descriptors. */
Matt Carlson21f581a2009-08-28 14:00:25 +00005986 memset(tpr->rx_std, 0, TG3_RX_RING_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005987
Matt Carlson287be122009-08-28 13:58:46 +00005988 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Michael Chana4e2b342005-10-26 15:46:52 -07005989 if ((tp->tg3_flags2 & TG3_FLG2_5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00005990 tp->dev->mtu > ETH_DATA_LEN)
5991 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
5992 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad42005-07-25 12:31:17 -07005993
Linus Torvalds1da177e2005-04-16 15:20:36 -07005994 /* Initialize invariants of the rings, we only set this
5995 * stuff once. This works because the card does not
5996 * write into the rx buffer posting rings.
5997 */
5998 for (i = 0; i < TG3_RX_RING_SIZE; i++) {
5999 struct tg3_rx_buffer_desc *rxd;
6000
Matt Carlson21f581a2009-08-28 14:00:25 +00006001 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00006002 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006003 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
6004 rxd->opaque = (RXD_OPAQUE_RING_STD |
6005 (i << RXD_OPAQUE_INDEX_SHIFT));
6006 }
6007
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006008 /* Now allocate fresh SKBs for each rx ring. */
6009 for (i = 0; i < tp->rx_pending; i++) {
Matt Carlson86b21e52009-11-13 13:03:45 +00006010 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_STD, i) < 0) {
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006011 printk(KERN_WARNING PFX
6012 "%s: Using a smaller RX standard ring, "
6013 "only %d out of %d buffers were allocated "
6014 "successfully.\n",
6015 tp->dev->name, i, tp->rx_pending);
6016 if (i == 0)
6017 goto initfail;
6018 tp->rx_pending = i;
6019 break;
6020 }
6021 }
6022
6023 if (!(tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE))
6024 goto done;
6025
Matt Carlson21f581a2009-08-28 14:00:25 +00006026 memset(tpr->rx_jmb, 0, TG3_RX_JUMBO_RING_BYTES);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006027
Michael Chan0f893dc2005-07-25 12:30:38 -07006028 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006029 for (i = 0; i < TG3_RX_JUMBO_RING_SIZE; i++) {
6030 struct tg3_rx_buffer_desc *rxd;
6031
Matt Carlson79ed5ac2009-08-28 14:00:55 +00006032 rxd = &tpr->rx_jmb[i].std;
Matt Carlson287be122009-08-28 13:58:46 +00006033 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
6035 RXD_FLAG_JUMBO;
6036 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
6037 (i << RXD_OPAQUE_INDEX_SHIFT));
6038 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040 for (i = 0; i < tp->rx_jumbo_pending; i++) {
Matt Carlson86b21e52009-11-13 13:03:45 +00006041 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_JUMBO,
Matt Carlsonafc081f2009-11-13 13:03:43 +00006042 i) < 0) {
Michael Chan32d8c572006-07-25 16:38:29 -07006043 printk(KERN_WARNING PFX
6044 "%s: Using a smaller RX jumbo ring, "
6045 "only %d out of %d buffers were "
6046 "allocated successfully.\n",
6047 tp->dev->name, i, tp->rx_jumbo_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006048 if (i == 0)
6049 goto initfail;
Michael Chan32d8c572006-07-25 16:38:29 -07006050 tp->rx_jumbo_pending = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006051 break;
Michael Chan32d8c572006-07-25 16:38:29 -07006052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053 }
6054 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006055
6056done:
Michael Chan32d8c572006-07-25 16:38:29 -07006057 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006058
6059initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00006060 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006061 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062}
6063
Matt Carlson21f581a2009-08-28 14:00:25 +00006064static void tg3_rx_prodring_fini(struct tg3 *tp,
6065 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006066{
Matt Carlson21f581a2009-08-28 14:00:25 +00006067 kfree(tpr->rx_std_buffers);
6068 tpr->rx_std_buffers = NULL;
6069 kfree(tpr->rx_jmb_buffers);
6070 tpr->rx_jmb_buffers = NULL;
6071 if (tpr->rx_std) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072 pci_free_consistent(tp->pdev, TG3_RX_RING_BYTES,
Matt Carlson21f581a2009-08-28 14:00:25 +00006073 tpr->rx_std, tpr->rx_std_mapping);
6074 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075 }
Matt Carlson21f581a2009-08-28 14:00:25 +00006076 if (tpr->rx_jmb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006077 pci_free_consistent(tp->pdev, TG3_RX_JUMBO_RING_BYTES,
Matt Carlson21f581a2009-08-28 14:00:25 +00006078 tpr->rx_jmb, tpr->rx_jmb_mapping);
6079 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006081}
6082
Matt Carlson21f581a2009-08-28 14:00:25 +00006083static int tg3_rx_prodring_init(struct tg3 *tp,
6084 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006085{
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006086 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE, GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006087 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006088 return -ENOMEM;
6089
Matt Carlson21f581a2009-08-28 14:00:25 +00006090 tpr->rx_std = pci_alloc_consistent(tp->pdev, TG3_RX_RING_BYTES,
6091 &tpr->rx_std_mapping);
6092 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006093 goto err_out;
6094
6095 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006096 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE,
Matt Carlson21f581a2009-08-28 14:00:25 +00006097 GFP_KERNEL);
6098 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006099 goto err_out;
6100
Matt Carlson21f581a2009-08-28 14:00:25 +00006101 tpr->rx_jmb = pci_alloc_consistent(tp->pdev,
6102 TG3_RX_JUMBO_RING_BYTES,
6103 &tpr->rx_jmb_mapping);
6104 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006105 goto err_out;
6106 }
6107
6108 return 0;
6109
6110err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00006111 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006112 return -ENOMEM;
6113}
6114
6115/* Free up pending packets in all rx/tx rings.
6116 *
6117 * The chip has been shut down and the driver detached from
6118 * the networking, so no interrupts or new tx packets will
6119 * end up in the driver. tp->{tx,}lock is not held and we are not
6120 * in an interrupt context and thus may sleep.
6121 */
6122static void tg3_free_rings(struct tg3 *tp)
6123{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006124 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006125
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006126 for (j = 0; j < tp->irq_cnt; j++) {
6127 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006128
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006129 if (!tnapi->tx_buffers)
6130 continue;
6131
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006132 for (i = 0; i < TG3_TX_RING_SIZE; ) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00006133 struct ring_info *txp;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006134 struct sk_buff *skb;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006135 unsigned int k;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006136
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006137 txp = &tnapi->tx_buffers[i];
6138 skb = txp->skb;
6139
6140 if (skb == NULL) {
6141 i++;
6142 continue;
6143 }
6144
Alexander Duyckf4188d82009-12-02 16:48:38 +00006145 pci_unmap_single(tp->pdev,
6146 pci_unmap_addr(txp, mapping),
6147 skb_headlen(skb),
6148 PCI_DMA_TODEVICE);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006149 txp->skb = NULL;
6150
Alexander Duyckf4188d82009-12-02 16:48:38 +00006151 i++;
6152
6153 for (k = 0; k < skb_shinfo(skb)->nr_frags; k++) {
6154 txp = &tnapi->tx_buffers[i & (TG3_TX_RING_SIZE - 1)];
6155 pci_unmap_page(tp->pdev,
6156 pci_unmap_addr(txp, mapping),
6157 skb_shinfo(skb)->frags[k].size,
6158 PCI_DMA_TODEVICE);
6159 i++;
6160 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006161
6162 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006163 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006164
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006165 if (tp->irq_cnt == 1 || j != tp->irq_cnt - 1)
6166 tg3_rx_prodring_free(tp, &tp->prodring[j]);
6167 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006168}
6169
6170/* Initialize tx/rx rings for packet processing.
6171 *
6172 * The chip has been shut down and the driver detached from
6173 * the networking, so no interrupts or new tx packets will
6174 * end up in the driver. tp->{tx,}lock are held and thus
6175 * we may not sleep.
6176 */
6177static int tg3_init_rings(struct tg3 *tp)
6178{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006179 int i;
Matt Carlson72334482009-08-28 14:03:01 +00006180
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006181 /* Free up all the SKBs. */
6182 tg3_free_rings(tp);
6183
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006184 for (i = 0; i < tp->irq_cnt; i++) {
6185 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006186
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006187 tnapi->last_tag = 0;
6188 tnapi->last_irq_tag = 0;
6189 tnapi->hw_status->status = 0;
6190 tnapi->hw_status->status_tag = 0;
6191 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6192
6193 tnapi->tx_prod = 0;
6194 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006195 if (tnapi->tx_ring)
6196 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006197
6198 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006199 if (tnapi->rx_rcb)
6200 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006201
6202 if ((tp->irq_cnt == 1 || i != tp->irq_cnt - 1) &&
6203 tg3_rx_prodring_alloc(tp, &tp->prodring[i]))
6204 return -ENOMEM;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006205 }
Matt Carlson72334482009-08-28 14:03:01 +00006206
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006207 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006208}
6209
6210/*
6211 * Must not be invoked with interrupt sources disabled and
6212 * the hardware shutdown down.
6213 */
6214static void tg3_free_consistent(struct tg3 *tp)
6215{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006216 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006217
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006218 for (i = 0; i < tp->irq_cnt; i++) {
6219 struct tg3_napi *tnapi = &tp->napi[i];
6220
6221 if (tnapi->tx_ring) {
6222 pci_free_consistent(tp->pdev, TG3_TX_RING_BYTES,
6223 tnapi->tx_ring, tnapi->tx_desc_mapping);
6224 tnapi->tx_ring = NULL;
6225 }
6226
6227 kfree(tnapi->tx_buffers);
6228 tnapi->tx_buffers = NULL;
6229
6230 if (tnapi->rx_rcb) {
6231 pci_free_consistent(tp->pdev, TG3_RX_RCB_RING_BYTES(tp),
6232 tnapi->rx_rcb,
6233 tnapi->rx_rcb_mapping);
6234 tnapi->rx_rcb = NULL;
6235 }
6236
6237 if (tnapi->hw_status) {
6238 pci_free_consistent(tp->pdev, TG3_HW_STATUS_SIZE,
6239 tnapi->hw_status,
6240 tnapi->status_mapping);
6241 tnapi->hw_status = NULL;
6242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006243 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006244
Linus Torvalds1da177e2005-04-16 15:20:36 -07006245 if (tp->hw_stats) {
6246 pci_free_consistent(tp->pdev, sizeof(struct tg3_hw_stats),
6247 tp->hw_stats, tp->stats_mapping);
6248 tp->hw_stats = NULL;
6249 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006250
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006251 for (i = 0; i < (tp->irq_cnt == 1 ? 1 : tp->irq_cnt - 1); i++)
6252 tg3_rx_prodring_fini(tp, &tp->prodring[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006253}
6254
6255/*
6256 * Must not be invoked with interrupt sources disabled and
6257 * the hardware shutdown down. Can sleep.
6258 */
6259static int tg3_alloc_consistent(struct tg3 *tp)
6260{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006261 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006262
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006263 for (i = 0; i < (tp->irq_cnt == 1 ? 1 : tp->irq_cnt - 1); i++) {
6264 if (tg3_rx_prodring_init(tp, &tp->prodring[i]))
6265 goto err_out;
6266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006267
Linus Torvalds1da177e2005-04-16 15:20:36 -07006268 tp->hw_stats = pci_alloc_consistent(tp->pdev,
6269 sizeof(struct tg3_hw_stats),
6270 &tp->stats_mapping);
6271 if (!tp->hw_stats)
6272 goto err_out;
6273
Linus Torvalds1da177e2005-04-16 15:20:36 -07006274 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6275
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006276 for (i = 0; i < tp->irq_cnt; i++) {
6277 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006278 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006279
6280 tnapi->hw_status = pci_alloc_consistent(tp->pdev,
6281 TG3_HW_STATUS_SIZE,
6282 &tnapi->status_mapping);
6283 if (!tnapi->hw_status)
6284 goto err_out;
6285
6286 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006287 sblk = tnapi->hw_status;
6288
Matt Carlson19cfaec2009-12-03 08:36:20 +00006289 /* If multivector TSS is enabled, vector 0 does not handle
6290 * tx interrupts. Don't allocate any resources for it.
6291 */
6292 if ((!i && !(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)) ||
6293 (i && (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS))) {
6294 tnapi->tx_buffers = kzalloc(sizeof(struct ring_info) *
6295 TG3_TX_RING_SIZE,
6296 GFP_KERNEL);
6297 if (!tnapi->tx_buffers)
6298 goto err_out;
6299
6300 tnapi->tx_ring = pci_alloc_consistent(tp->pdev,
6301 TG3_TX_RING_BYTES,
6302 &tnapi->tx_desc_mapping);
6303 if (!tnapi->tx_ring)
6304 goto err_out;
6305 }
6306
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006307 /*
6308 * When RSS is enabled, the status block format changes
6309 * slightly. The "rx_jumbo_consumer", "reserved",
6310 * and "rx_mini_consumer" members get mapped to the
6311 * other three rx return ring producer indexes.
6312 */
6313 switch (i) {
6314 default:
6315 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
6316 break;
6317 case 2:
6318 tnapi->rx_rcb_prod_idx = &sblk->rx_jumbo_consumer;
6319 break;
6320 case 3:
6321 tnapi->rx_rcb_prod_idx = &sblk->reserved;
6322 break;
6323 case 4:
6324 tnapi->rx_rcb_prod_idx = &sblk->rx_mini_consumer;
6325 break;
6326 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006327
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006328 if (tp->irq_cnt == 1)
6329 tnapi->prodring = &tp->prodring[0];
6330 else if (i)
6331 tnapi->prodring = &tp->prodring[i - 1];
6332
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006333 /*
6334 * If multivector RSS is enabled, vector 0 does not handle
6335 * rx or tx interrupts. Don't allocate any resources for it.
6336 */
6337 if (!i && (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS))
6338 continue;
6339
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006340 tnapi->rx_rcb = pci_alloc_consistent(tp->pdev,
6341 TG3_RX_RCB_RING_BYTES(tp),
6342 &tnapi->rx_rcb_mapping);
6343 if (!tnapi->rx_rcb)
6344 goto err_out;
6345
6346 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006347 }
6348
Linus Torvalds1da177e2005-04-16 15:20:36 -07006349 return 0;
6350
6351err_out:
6352 tg3_free_consistent(tp);
6353 return -ENOMEM;
6354}
6355
6356#define MAX_WAIT_CNT 1000
6357
6358/* To stop a block, clear the enable bit and poll till it
6359 * clears. tp->lock is held.
6360 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006361static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006362{
6363 unsigned int i;
6364 u32 val;
6365
6366 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
6367 switch (ofs) {
6368 case RCVLSC_MODE:
6369 case DMAC_MODE:
6370 case MBFREE_MODE:
6371 case BUFMGR_MODE:
6372 case MEMARB_MODE:
6373 /* We can't enable/disable these bits of the
6374 * 5705/5750, just say success.
6375 */
6376 return 0;
6377
6378 default:
6379 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006381 }
6382
6383 val = tr32(ofs);
6384 val &= ~enable_bit;
6385 tw32_f(ofs, val);
6386
6387 for (i = 0; i < MAX_WAIT_CNT; i++) {
6388 udelay(100);
6389 val = tr32(ofs);
6390 if ((val & enable_bit) == 0)
6391 break;
6392 }
6393
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006394 if (i == MAX_WAIT_CNT && !silent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006395 printk(KERN_ERR PFX "tg3_stop_block timed out, "
6396 "ofs=%lx enable_bit=%x\n",
6397 ofs, enable_bit);
6398 return -ENODEV;
6399 }
6400
6401 return 0;
6402}
6403
6404/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006405static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006406{
6407 int i, err;
6408
6409 tg3_disable_ints(tp);
6410
6411 tp->rx_mode &= ~RX_MODE_ENABLE;
6412 tw32_f(MAC_RX_MODE, tp->rx_mode);
6413 udelay(10);
6414
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006415 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
6416 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
6417 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
6418 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
6419 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
6420 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006421
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006422 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
6423 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
6424 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
6425 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
6426 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
6427 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
6428 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006429
6430 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
6431 tw32_f(MAC_MODE, tp->mac_mode);
6432 udelay(40);
6433
6434 tp->tx_mode &= ~TX_MODE_ENABLE;
6435 tw32_f(MAC_TX_MODE, tp->tx_mode);
6436
6437 for (i = 0; i < MAX_WAIT_CNT; i++) {
6438 udelay(100);
6439 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
6440 break;
6441 }
6442 if (i >= MAX_WAIT_CNT) {
6443 printk(KERN_ERR PFX "tg3_abort_hw timed out for %s, "
6444 "TX_MODE_ENABLE will not clear MAC_TX_MODE=%08x\n",
6445 tp->dev->name, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07006446 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006447 }
6448
Michael Chane6de8ad2005-05-05 14:42:41 -07006449 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006450 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
6451 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006452
6453 tw32(FTQ_RESET, 0xffffffff);
6454 tw32(FTQ_RESET, 0x00000000);
6455
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006456 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
6457 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006458
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006459 for (i = 0; i < tp->irq_cnt; i++) {
6460 struct tg3_napi *tnapi = &tp->napi[i];
6461 if (tnapi->hw_status)
6462 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006464 if (tp->hw_stats)
6465 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6466
Linus Torvalds1da177e2005-04-16 15:20:36 -07006467 return err;
6468}
6469
Matt Carlson0d3031d2007-10-10 18:02:43 -07006470static void tg3_ape_send_event(struct tg3 *tp, u32 event)
6471{
6472 int i;
6473 u32 apedata;
6474
6475 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
6476 if (apedata != APE_SEG_SIG_MAGIC)
6477 return;
6478
6479 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
Matt Carlson731fd792008-08-15 14:07:51 -07006480 if (!(apedata & APE_FW_STATUS_READY))
Matt Carlson0d3031d2007-10-10 18:02:43 -07006481 return;
6482
6483 /* Wait for up to 1 millisecond for APE to service previous event. */
6484 for (i = 0; i < 10; i++) {
6485 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
6486 return;
6487
6488 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
6489
6490 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6491 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
6492 event | APE_EVENT_STATUS_EVENT_PENDING);
6493
6494 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
6495
6496 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6497 break;
6498
6499 udelay(100);
6500 }
6501
6502 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6503 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
6504}
6505
6506static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
6507{
6508 u32 event;
6509 u32 apedata;
6510
6511 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
6512 return;
6513
6514 switch (kind) {
6515 case RESET_KIND_INIT:
6516 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
6517 APE_HOST_SEG_SIG_MAGIC);
6518 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
6519 APE_HOST_SEG_LEN_MAGIC);
6520 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
6521 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
6522 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
6523 APE_HOST_DRIVER_ID_MAGIC);
6524 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
6525 APE_HOST_BEHAV_NO_PHYLOCK);
6526
6527 event = APE_EVENT_STATUS_STATE_START;
6528 break;
6529 case RESET_KIND_SHUTDOWN:
Matt Carlsonb2aee152008-11-03 16:51:11 -08006530 /* With the interface we are currently using,
6531 * APE does not track driver state. Wiping
6532 * out the HOST SEGMENT SIGNATURE forces
6533 * the APE to assume OS absent status.
6534 */
6535 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
6536
Matt Carlson0d3031d2007-10-10 18:02:43 -07006537 event = APE_EVENT_STATUS_STATE_UNLOAD;
6538 break;
6539 case RESET_KIND_SUSPEND:
6540 event = APE_EVENT_STATUS_STATE_SUSPEND;
6541 break;
6542 default:
6543 return;
6544 }
6545
6546 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
6547
6548 tg3_ape_send_event(tp, event);
6549}
6550
Michael Chane6af3012005-04-21 17:12:05 -07006551/* tp->lock is held. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006552static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
6553{
David S. Millerf49639e2006-06-09 11:58:36 -07006554 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
6555 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006556
6557 if (tp->tg3_flags2 & TG3_FLG2_ASF_NEW_HANDSHAKE) {
6558 switch (kind) {
6559 case RESET_KIND_INIT:
6560 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6561 DRV_STATE_START);
6562 break;
6563
6564 case RESET_KIND_SHUTDOWN:
6565 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6566 DRV_STATE_UNLOAD);
6567 break;
6568
6569 case RESET_KIND_SUSPEND:
6570 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6571 DRV_STATE_SUSPEND);
6572 break;
6573
6574 default:
6575 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006577 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07006578
6579 if (kind == RESET_KIND_INIT ||
6580 kind == RESET_KIND_SUSPEND)
6581 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006582}
6583
6584/* tp->lock is held. */
6585static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
6586{
6587 if (tp->tg3_flags2 & TG3_FLG2_ASF_NEW_HANDSHAKE) {
6588 switch (kind) {
6589 case RESET_KIND_INIT:
6590 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6591 DRV_STATE_START_DONE);
6592 break;
6593
6594 case RESET_KIND_SHUTDOWN:
6595 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6596 DRV_STATE_UNLOAD_DONE);
6597 break;
6598
6599 default:
6600 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006602 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07006603
6604 if (kind == RESET_KIND_SHUTDOWN)
6605 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006606}
6607
6608/* tp->lock is held. */
6609static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
6610{
6611 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) {
6612 switch (kind) {
6613 case RESET_KIND_INIT:
6614 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6615 DRV_STATE_START);
6616 break;
6617
6618 case RESET_KIND_SHUTDOWN:
6619 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6620 DRV_STATE_UNLOAD);
6621 break;
6622
6623 case RESET_KIND_SUSPEND:
6624 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6625 DRV_STATE_SUSPEND);
6626 break;
6627
6628 default:
6629 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006630 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006631 }
6632}
6633
Michael Chan7a6f4362006-09-27 16:03:31 -07006634static int tg3_poll_fw(struct tg3 *tp)
6635{
6636 int i;
6637 u32 val;
6638
Michael Chanb5d37722006-09-27 16:06:21 -07006639 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Gary Zambrano0ccead12006-11-14 16:34:00 -08006640 /* Wait up to 20ms for init done. */
6641 for (i = 0; i < 200; i++) {
Michael Chanb5d37722006-09-27 16:06:21 -07006642 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
6643 return 0;
Gary Zambrano0ccead12006-11-14 16:34:00 -08006644 udelay(100);
Michael Chanb5d37722006-09-27 16:06:21 -07006645 }
6646 return -ENODEV;
6647 }
6648
Michael Chan7a6f4362006-09-27 16:03:31 -07006649 /* Wait for firmware initialization to complete. */
6650 for (i = 0; i < 100000; i++) {
6651 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
6652 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
6653 break;
6654 udelay(10);
6655 }
6656
6657 /* Chip might not be fitted with firmware. Some Sun onboard
6658 * parts are configured like that. So don't signal the timeout
6659 * of the above loop as an error, but do report the lack of
6660 * running firmware once.
6661 */
6662 if (i >= 100000 &&
6663 !(tp->tg3_flags2 & TG3_FLG2_NO_FWARE_REPORTED)) {
6664 tp->tg3_flags2 |= TG3_FLG2_NO_FWARE_REPORTED;
6665
6666 printk(KERN_INFO PFX "%s: No firmware running.\n",
6667 tp->dev->name);
6668 }
6669
6670 return 0;
6671}
6672
Michael Chanee6a99b2007-07-18 21:49:10 -07006673/* Save PCI command register before chip reset */
6674static void tg3_save_pci_state(struct tg3 *tp)
6675{
Matt Carlson8a6eac92007-10-21 16:17:55 -07006676 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07006677}
6678
6679/* Restore PCI state after chip reset */
6680static void tg3_restore_pci_state(struct tg3 *tp)
6681{
6682 u32 val;
6683
6684 /* Re-enable indirect register accesses. */
6685 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
6686 tp->misc_host_ctrl);
6687
6688 /* Set MAX PCI retry to zero. */
6689 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
6690 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
6691 (tp->tg3_flags & TG3_FLAG_PCIX_MODE))
6692 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07006693 /* Allow reads and writes to the APE register and memory space. */
6694 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
6695 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
6696 PCISTATE_ALLOW_APE_SHMEM_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07006697 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
6698
Matt Carlson8a6eac92007-10-21 16:17:55 -07006699 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07006700
Matt Carlsonfcb389d2008-11-03 16:55:44 -08006701 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785) {
6702 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)
6703 pcie_set_readrq(tp->pdev, 4096);
6704 else {
6705 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
6706 tp->pci_cacheline_sz);
6707 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
6708 tp->pci_lat_timer);
6709 }
Michael Chan114342f2007-10-15 02:12:26 -07006710 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08006711
Michael Chanee6a99b2007-07-18 21:49:10 -07006712 /* Make sure PCI-X relaxed ordering bit is clear. */
Matt Carlson52f44902008-11-21 17:17:04 -08006713 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
Matt Carlson9974a352007-10-07 23:27:28 -07006714 u16 pcix_cmd;
6715
6716 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
6717 &pcix_cmd);
6718 pcix_cmd &= ~PCI_X_CMD_ERO;
6719 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
6720 pcix_cmd);
6721 }
Michael Chanee6a99b2007-07-18 21:49:10 -07006722
6723 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
Michael Chanee6a99b2007-07-18 21:49:10 -07006724
6725 /* Chip reset on 5780 will reset MSI enable bit,
6726 * so need to restore it.
6727 */
6728 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
6729 u16 ctrl;
6730
6731 pci_read_config_word(tp->pdev,
6732 tp->msi_cap + PCI_MSI_FLAGS,
6733 &ctrl);
6734 pci_write_config_word(tp->pdev,
6735 tp->msi_cap + PCI_MSI_FLAGS,
6736 ctrl | PCI_MSI_FLAGS_ENABLE);
6737 val = tr32(MSGINT_MODE);
6738 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
6739 }
6740 }
6741}
6742
Linus Torvalds1da177e2005-04-16 15:20:36 -07006743static void tg3_stop_fw(struct tg3 *);
6744
6745/* tp->lock is held. */
6746static int tg3_chip_reset(struct tg3 *tp)
6747{
6748 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07006749 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00006750 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006751
David S. Millerf49639e2006-06-09 11:58:36 -07006752 tg3_nvram_lock(tp);
6753
Matt Carlson77b483f2008-08-15 14:07:24 -07006754 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
6755
David S. Millerf49639e2006-06-09 11:58:36 -07006756 /* No matching tg3_nvram_unlock() after this because
6757 * chip reset below will undo the nvram lock.
6758 */
6759 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006760
Michael Chanee6a99b2007-07-18 21:49:10 -07006761 /* GRC_MISC_CFG core clock reset will clear the memory
6762 * enable bit in PCI register 4 and the MSI enable bit
6763 * on some chips, so we save relevant registers here.
6764 */
6765 tg3_save_pci_state(tp);
6766
Michael Chand9ab5ad12006-03-20 22:27:35 -08006767 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08006768 (tp->tg3_flags3 & TG3_FLG3_5755_PLUS))
Michael Chand9ab5ad12006-03-20 22:27:35 -08006769 tw32(GRC_FASTBOOT_PC, 0);
6770
Linus Torvalds1da177e2005-04-16 15:20:36 -07006771 /*
6772 * We must avoid the readl() that normally takes place.
6773 * It locks machines, causes machine checks, and other
6774 * fun things. So, temporarily disable the 5701
6775 * hardware workaround, while we do the reset.
6776 */
Michael Chan1ee582d2005-08-09 20:16:46 -07006777 write_op = tp->write32;
6778 if (write_op == tg3_write_flush_reg32)
6779 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006780
Michael Chand18edcb2007-03-24 20:57:11 -07006781 /* Prevent the irq handler from reading or writing PCI registers
6782 * during chip reset when the memory enable bit in the PCI command
6783 * register may be cleared. The chip does not generate interrupt
6784 * at this time, but the irq handler may still be called due to irq
6785 * sharing or irqpoll.
6786 */
6787 tp->tg3_flags |= TG3_FLAG_CHIP_RESETTING;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006788 for (i = 0; i < tp->irq_cnt; i++) {
6789 struct tg3_napi *tnapi = &tp->napi[i];
6790 if (tnapi->hw_status) {
6791 tnapi->hw_status->status = 0;
6792 tnapi->hw_status->status_tag = 0;
6793 }
6794 tnapi->last_tag = 0;
6795 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07006796 }
Michael Chand18edcb2007-03-24 20:57:11 -07006797 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00006798
6799 for (i = 0; i < tp->irq_cnt; i++)
6800 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07006801
Matt Carlson255ca312009-08-25 10:07:27 +00006802 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
6803 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
6804 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
6805 }
6806
Linus Torvalds1da177e2005-04-16 15:20:36 -07006807 /* do the reset */
6808 val = GRC_MISC_CFG_CORECLK_RESET;
6809
6810 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
6811 if (tr32(0x7e2c) == 0x60) {
6812 tw32(0x7e2c, 0x20);
6813 }
6814 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
6815 tw32(GRC_MISC_CFG, (1 << 29));
6816 val |= (1 << 29);
6817 }
6818 }
6819
Michael Chanb5d37722006-09-27 16:06:21 -07006820 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
6821 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
6822 tw32(GRC_VCPU_EXT_CTRL,
6823 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
6824 }
6825
Linus Torvalds1da177e2005-04-16 15:20:36 -07006826 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
6827 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
6828 tw32(GRC_MISC_CFG, val);
6829
Michael Chan1ee582d2005-08-09 20:16:46 -07006830 /* restore 5701 hardware bug workaround write method */
6831 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006832
6833 /* Unfortunately, we have to delay before the PCI read back.
6834 * Some 575X chips even will not respond to a PCI cfg access
6835 * when the reset command is given to the chip.
6836 *
6837 * How do these hardware designers expect things to work
6838 * properly if the PCI write is posted for a long period
6839 * of time? It is always necessary to have some method by
6840 * which a register read back can occur to push the write
6841 * out which does the reset.
6842 *
6843 * For most tg3 variants the trick below was working.
6844 * Ho hum...
6845 */
6846 udelay(120);
6847
6848 /* Flush PCI posted writes. The normal MMIO registers
6849 * are inaccessible at this time so this is the only
6850 * way to make this reliably (actually, this is no longer
6851 * the case, see above). I tried to use indirect
6852 * register read/write but this upset some 5701 variants.
6853 */
6854 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
6855
6856 udelay(120);
6857
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006858 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) && tp->pcie_cap) {
Matt Carlsone7126992009-08-25 10:08:16 +00006859 u16 val16;
6860
Linus Torvalds1da177e2005-04-16 15:20:36 -07006861 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
6862 int i;
6863 u32 cfg_val;
6864
6865 /* Wait for link training to complete. */
6866 for (i = 0; i < 5000; i++)
6867 udelay(100);
6868
6869 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
6870 pci_write_config_dword(tp->pdev, 0xc4,
6871 cfg_val | (1 << 15));
6872 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006873
Matt Carlsone7126992009-08-25 10:08:16 +00006874 /* Clear the "no snoop" and "relaxed ordering" bits. */
6875 pci_read_config_word(tp->pdev,
6876 tp->pcie_cap + PCI_EXP_DEVCTL,
6877 &val16);
6878 val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
6879 PCI_EXP_DEVCTL_NOSNOOP_EN);
6880 /*
6881 * Older PCIe devices only support the 128 byte
6882 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006883 */
Matt Carlsone7126992009-08-25 10:08:16 +00006884 if (!(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
6885 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784))
6886 val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006887 pci_write_config_word(tp->pdev,
6888 tp->pcie_cap + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00006889 val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006890
6891 pcie_set_readrq(tp->pdev, 4096);
6892
6893 /* Clear error status */
6894 pci_write_config_word(tp->pdev,
6895 tp->pcie_cap + PCI_EXP_DEVSTA,
6896 PCI_EXP_DEVSTA_CED |
6897 PCI_EXP_DEVSTA_NFED |
6898 PCI_EXP_DEVSTA_FED |
6899 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006900 }
6901
Michael Chanee6a99b2007-07-18 21:49:10 -07006902 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006903
Michael Chand18edcb2007-03-24 20:57:11 -07006904 tp->tg3_flags &= ~TG3_FLAG_CHIP_RESETTING;
6905
Michael Chanee6a99b2007-07-18 21:49:10 -07006906 val = 0;
6907 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
Michael Chan4cf78e42005-07-25 12:29:19 -07006908 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07006909 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006910
6911 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
6912 tg3_stop_fw(tp);
6913 tw32(0x5000, 0x400);
6914 }
6915
6916 tw32(GRC_MODE, tp->grc_mode);
6917
6918 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01006919 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006920
6921 tw32(0xc4, val | (1 << 15));
6922 }
6923
6924 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
6925 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
6926 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
6927 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
6928 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
6929 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
6930 }
6931
6932 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
6933 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
6934 tw32_f(MAC_MODE, tp->mac_mode);
Michael Chan747e8f82005-07-25 12:33:22 -07006935 } else if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES) {
6936 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
6937 tw32_f(MAC_MODE, tp->mac_mode);
Matt Carlson3bda1252008-08-15 14:08:22 -07006938 } else if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
6939 tp->mac_mode &= (MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN);
6940 if (tp->mac_mode & MAC_MODE_APE_TX_EN)
6941 tp->mac_mode |= MAC_MODE_TDE_ENABLE;
6942 tw32_f(MAC_MODE, tp->mac_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006943 } else
6944 tw32_f(MAC_MODE, 0);
6945 udelay(40);
6946
Matt Carlson77b483f2008-08-15 14:07:24 -07006947 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
6948
Michael Chan7a6f4362006-09-27 16:03:31 -07006949 err = tg3_poll_fw(tp);
6950 if (err)
6951 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006952
Matt Carlson0a9140c2009-08-28 12:27:50 +00006953 tg3_mdio_start(tp);
6954
Matt Carlson52cdf852009-11-02 14:25:06 +00006955 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
6956 u8 phy_addr;
6957
6958 phy_addr = tp->phy_addr;
6959 tp->phy_addr = TG3_PHY_PCIE_ADDR;
6960
6961 tg3_writephy(tp, TG3_PCIEPHY_BLOCK_ADDR,
6962 TG3_PCIEPHY_TXB_BLK << TG3_PCIEPHY_BLOCK_SHIFT);
6963 val = TG3_PCIEPHY_TX0CTRL1_TXOCM | TG3_PCIEPHY_TX0CTRL1_RDCTL |
6964 TG3_PCIEPHY_TX0CTRL1_TXCMV | TG3_PCIEPHY_TX0CTRL1_TKSEL |
6965 TG3_PCIEPHY_TX0CTRL1_NB_EN;
6966 tg3_writephy(tp, TG3_PCIEPHY_TX0CTRL1, val);
6967 udelay(10);
6968
6969 tg3_writephy(tp, TG3_PCIEPHY_BLOCK_ADDR,
6970 TG3_PCIEPHY_XGXS_BLK1 << TG3_PCIEPHY_BLOCK_SHIFT);
6971 val = TG3_PCIEPHY_PWRMGMT4_LOWPWR_EN |
6972 TG3_PCIEPHY_PWRMGMT4_L1PLLPD_EN;
6973 tg3_writephy(tp, TG3_PCIEPHY_PWRMGMT4, val);
6974 udelay(10);
6975
6976 tp->phy_addr = phy_addr;
6977 }
6978
Linus Torvalds1da177e2005-04-16 15:20:36 -07006979 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00006980 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
6981 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonb703df62009-12-03 08:36:21 +00006982 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
6983 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57765) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01006984 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006985
6986 tw32(0x7c00, val | (1 << 25));
6987 }
6988
6989 /* Reprobe ASF enable state. */
6990 tp->tg3_flags &= ~TG3_FLAG_ENABLE_ASF;
6991 tp->tg3_flags2 &= ~TG3_FLG2_ASF_NEW_HANDSHAKE;
6992 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
6993 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
6994 u32 nic_cfg;
6995
6996 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
6997 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
6998 tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
Matt Carlson4ba526c2008-08-15 14:10:04 -07006999 tp->last_event_jiffies = jiffies;
John W. Linvillecbf46852005-04-21 17:01:29 -07007000 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007001 tp->tg3_flags2 |= TG3_FLG2_ASF_NEW_HANDSHAKE;
7002 }
7003 }
7004
7005 return 0;
7006}
7007
7008/* tp->lock is held. */
7009static void tg3_stop_fw(struct tg3 *tp)
7010{
Matt Carlson0d3031d2007-10-10 18:02:43 -07007011 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
7012 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07007013 /* Wait for RX cpu to ACK the previous event. */
7014 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007015
7016 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007017
7018 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007019
Matt Carlson7c5026a2008-05-02 16:49:29 -07007020 /* Wait for RX cpu to ACK this event. */
7021 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007022 }
7023}
7024
7025/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07007026static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007027{
7028 int err;
7029
7030 tg3_stop_fw(tp);
7031
Michael Chan944d9802005-05-29 14:57:48 -07007032 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007033
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007034 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007035 err = tg3_chip_reset(tp);
7036
Matt Carlsondaba2a62009-04-20 06:58:52 +00007037 __tg3_set_mac_addr(tp, 0);
7038
Michael Chan944d9802005-05-29 14:57:48 -07007039 tg3_write_sig_legacy(tp, kind);
7040 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007041
7042 if (err)
7043 return err;
7044
7045 return 0;
7046}
7047
Linus Torvalds1da177e2005-04-16 15:20:36 -07007048#define RX_CPU_SCRATCH_BASE 0x30000
7049#define RX_CPU_SCRATCH_SIZE 0x04000
7050#define TX_CPU_SCRATCH_BASE 0x34000
7051#define TX_CPU_SCRATCH_SIZE 0x04000
7052
7053/* tp->lock is held. */
7054static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
7055{
7056 int i;
7057
Eric Sesterhenn5d9428d2006-04-02 13:52:48 +02007058 BUG_ON(offset == TX_CPU_BASE &&
7059 (tp->tg3_flags2 & TG3_FLG2_5705_PLUS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007060
Michael Chanb5d37722006-09-27 16:06:21 -07007061 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7062 u32 val = tr32(GRC_VCPU_EXT_CTRL);
7063
7064 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
7065 return 0;
7066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007067 if (offset == RX_CPU_BASE) {
7068 for (i = 0; i < 10000; i++) {
7069 tw32(offset + CPU_STATE, 0xffffffff);
7070 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7071 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7072 break;
7073 }
7074
7075 tw32(offset + CPU_STATE, 0xffffffff);
7076 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
7077 udelay(10);
7078 } else {
7079 for (i = 0; i < 10000; i++) {
7080 tw32(offset + CPU_STATE, 0xffffffff);
7081 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7082 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7083 break;
7084 }
7085 }
7086
7087 if (i >= 10000) {
7088 printk(KERN_ERR PFX "tg3_reset_cpu timed out for %s, "
7089 "and %s CPU\n",
7090 tp->dev->name,
7091 (offset == RX_CPU_BASE ? "RX" : "TX"));
7092 return -ENODEV;
7093 }
Michael Chanec41c7d2006-01-17 02:40:55 -08007094
7095 /* Clear firmware's nvram arbitration. */
7096 if (tp->tg3_flags & TG3_FLAG_NVRAM)
7097 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007098 return 0;
7099}
7100
7101struct fw_info {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007102 unsigned int fw_base;
7103 unsigned int fw_len;
7104 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007105};
7106
7107/* tp->lock is held. */
7108static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base, u32 cpu_scratch_base,
7109 int cpu_scratch_size, struct fw_info *info)
7110{
Michael Chanec41c7d2006-01-17 02:40:55 -08007111 int err, lock_err, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007112 void (*write_op)(struct tg3 *, u32, u32);
7113
7114 if (cpu_base == TX_CPU_BASE &&
7115 (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7116 printk(KERN_ERR PFX "tg3_load_firmware_cpu: Trying to load "
7117 "TX cpu firmware on %s which is 5705.\n",
7118 tp->dev->name);
7119 return -EINVAL;
7120 }
7121
7122 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
7123 write_op = tg3_write_mem;
7124 else
7125 write_op = tg3_write_indirect_reg32;
7126
Michael Chan1b628152005-05-29 14:59:49 -07007127 /* It is possible that bootcode is still loading at this point.
7128 * Get the nvram lock first before halting the cpu.
7129 */
Michael Chanec41c7d2006-01-17 02:40:55 -08007130 lock_err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007131 err = tg3_halt_cpu(tp, cpu_base);
Michael Chanec41c7d2006-01-17 02:40:55 -08007132 if (!lock_err)
7133 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007134 if (err)
7135 goto out;
7136
7137 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
7138 write_op(tp, cpu_scratch_base + i, 0);
7139 tw32(cpu_base + CPU_STATE, 0xffffffff);
7140 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007141 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007142 write_op(tp, (cpu_scratch_base +
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007143 (info->fw_base & 0xffff) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07007144 (i * sizeof(u32))),
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007145 be32_to_cpu(info->fw_data[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007146
7147 err = 0;
7148
7149out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007150 return err;
7151}
7152
7153/* tp->lock is held. */
7154static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
7155{
7156 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007157 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007158 int err, i;
7159
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007160 fw_data = (void *)tp->fw->data;
7161
7162 /* Firmware blob starts with version numbers, followed by
7163 start address and length. We are setting complete length.
7164 length = end_address_of_bss - start_address_of_text.
7165 Remainder is the blob to be loaded contiguously
7166 from start address. */
7167
7168 info.fw_base = be32_to_cpu(fw_data[1]);
7169 info.fw_len = tp->fw->size - 12;
7170 info.fw_data = &fw_data[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007171
7172 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
7173 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
7174 &info);
7175 if (err)
7176 return err;
7177
7178 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
7179 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
7180 &info);
7181 if (err)
7182 return err;
7183
7184 /* Now startup only the RX cpu. */
7185 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007186 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007187
7188 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007189 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007190 break;
7191 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7192 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007193 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007194 udelay(1000);
7195 }
7196 if (i >= 5) {
7197 printk(KERN_ERR PFX "tg3_load_firmware fails for %s "
7198 "to set RX CPU PC, is %08x should be %08x\n",
7199 tp->dev->name, tr32(RX_CPU_BASE + CPU_PC),
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007200 info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007201 return -ENODEV;
7202 }
7203 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7204 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
7205
7206 return 0;
7207}
7208
Linus Torvalds1da177e2005-04-16 15:20:36 -07007209/* 5705 needs a special version of the TSO firmware. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007210
7211/* tp->lock is held. */
7212static int tg3_load_tso_firmware(struct tg3 *tp)
7213{
7214 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007215 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007216 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
7217 int err, i;
7218
7219 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
7220 return 0;
7221
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007222 fw_data = (void *)tp->fw->data;
7223
7224 /* Firmware blob starts with version numbers, followed by
7225 start address and length. We are setting complete length.
7226 length = end_address_of_bss - start_address_of_text.
7227 Remainder is the blob to be loaded contiguously
7228 from start address. */
7229
7230 info.fw_base = be32_to_cpu(fw_data[1]);
7231 cpu_scratch_size = tp->fw_len;
7232 info.fw_len = tp->fw->size - 12;
7233 info.fw_data = &fw_data[3];
7234
Linus Torvalds1da177e2005-04-16 15:20:36 -07007235 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007236 cpu_base = RX_CPU_BASE;
7237 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007238 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007239 cpu_base = TX_CPU_BASE;
7240 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
7241 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
7242 }
7243
7244 err = tg3_load_firmware_cpu(tp, cpu_base,
7245 cpu_scratch_base, cpu_scratch_size,
7246 &info);
7247 if (err)
7248 return err;
7249
7250 /* Now startup the cpu. */
7251 tw32(cpu_base + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007252 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007253
7254 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007255 if (tr32(cpu_base + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007256 break;
7257 tw32(cpu_base + CPU_STATE, 0xffffffff);
7258 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007259 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007260 udelay(1000);
7261 }
7262 if (i >= 5) {
7263 printk(KERN_ERR PFX "tg3_load_tso_firmware fails for %s "
7264 "to set CPU PC, is %08x should be %08x\n",
7265 tp->dev->name, tr32(cpu_base + CPU_PC),
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007266 info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007267 return -ENODEV;
7268 }
7269 tw32(cpu_base + CPU_STATE, 0xffffffff);
7270 tw32_f(cpu_base + CPU_MODE, 0x00000000);
7271 return 0;
7272}
7273
Linus Torvalds1da177e2005-04-16 15:20:36 -07007274
Linus Torvalds1da177e2005-04-16 15:20:36 -07007275static int tg3_set_mac_addr(struct net_device *dev, void *p)
7276{
7277 struct tg3 *tp = netdev_priv(dev);
7278 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07007279 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007280
Michael Chanf9804dd2005-09-27 12:13:10 -07007281 if (!is_valid_ether_addr(addr->sa_data))
7282 return -EINVAL;
7283
Linus Torvalds1da177e2005-04-16 15:20:36 -07007284 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7285
Michael Chane75f7c92006-03-20 21:33:26 -08007286 if (!netif_running(dev))
7287 return 0;
7288
Michael Chan58712ef2006-04-29 18:58:01 -07007289 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) {
Michael Chan986e0ae2007-05-05 12:10:20 -07007290 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07007291
Michael Chan986e0ae2007-05-05 12:10:20 -07007292 addr0_high = tr32(MAC_ADDR_0_HIGH);
7293 addr0_low = tr32(MAC_ADDR_0_LOW);
7294 addr1_high = tr32(MAC_ADDR_1_HIGH);
7295 addr1_low = tr32(MAC_ADDR_1_LOW);
7296
7297 /* Skip MAC addr 1 if ASF is using it. */
7298 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
7299 !(addr1_high == 0 && addr1_low == 0))
7300 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07007301 }
Michael Chan986e0ae2007-05-05 12:10:20 -07007302 spin_lock_bh(&tp->lock);
7303 __tg3_set_mac_addr(tp, skip_mac_1);
7304 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007305
Michael Chanb9ec6c12006-07-25 16:37:27 -07007306 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007307}
7308
7309/* tp->lock is held. */
7310static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
7311 dma_addr_t mapping, u32 maxlen_flags,
7312 u32 nic_addr)
7313{
7314 tg3_write_mem(tp,
7315 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
7316 ((u64) mapping >> 32));
7317 tg3_write_mem(tp,
7318 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
7319 ((u64) mapping & 0xffffffff));
7320 tg3_write_mem(tp,
7321 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
7322 maxlen_flags);
7323
7324 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7325 tg3_write_mem(tp,
7326 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
7327 nic_addr);
7328}
7329
7330static void __tg3_set_rx_mode(struct net_device *);
Michael Chand244c892005-07-05 14:42:33 -07007331static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07007332{
Matt Carlsonb6080e12009-09-01 13:12:00 +00007333 int i;
7334
Matt Carlson19cfaec2009-12-03 08:36:20 +00007335 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007336 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
7337 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
7338 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007339 } else {
7340 tw32(HOSTCC_TXCOL_TICKS, 0);
7341 tw32(HOSTCC_TXMAX_FRAMES, 0);
7342 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007343 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007344
Matt Carlson19cfaec2009-12-03 08:36:20 +00007345 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSIX)) {
7346 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
7347 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
7348 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
7349 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007350 tw32(HOSTCC_RXCOL_TICKS, 0);
7351 tw32(HOSTCC_RXMAX_FRAMES, 0);
7352 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07007353 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007354
David S. Miller15f98502005-05-18 22:49:26 -07007355 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7356 u32 val = ec->stats_block_coalesce_usecs;
7357
Matt Carlsonb6080e12009-09-01 13:12:00 +00007358 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
7359 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
7360
David S. Miller15f98502005-05-18 22:49:26 -07007361 if (!netif_carrier_ok(tp->dev))
7362 val = 0;
7363
7364 tw32(HOSTCC_STAT_COAL_TICKS, val);
7365 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007366
7367 for (i = 0; i < tp->irq_cnt - 1; i++) {
7368 u32 reg;
7369
7370 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
7371 tw32(reg, ec->rx_coalesce_usecs);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007372 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
7373 tw32(reg, ec->rx_max_coalesced_frames);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007374 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
7375 tw32(reg, ec->rx_max_coalesced_frames_irq);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007376
7377 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS) {
7378 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
7379 tw32(reg, ec->tx_coalesce_usecs);
7380 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
7381 tw32(reg, ec->tx_max_coalesced_frames);
7382 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
7383 tw32(reg, ec->tx_max_coalesced_frames_irq);
7384 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007385 }
7386
7387 for (; i < tp->irq_max - 1; i++) {
7388 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007389 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007390 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007391
7392 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS) {
7393 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
7394 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
7395 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
7396 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007397 }
David S. Miller15f98502005-05-18 22:49:26 -07007398}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007399
7400/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00007401static void tg3_rings_reset(struct tg3 *tp)
7402{
7403 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007404 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007405 struct tg3_napi *tnapi = &tp->napi[0];
7406
7407 /* Disable all transmit rings but the first. */
7408 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7409 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00007410 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
7411 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007412 else
7413 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7414
7415 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7416 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
7417 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
7418 BDINFO_FLAGS_DISABLED);
7419
7420
7421 /* Disable all receive return rings but the first. */
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007422 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
7423 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
7424 else if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00007425 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00007426 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
7427 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson2d31eca2009-09-01 12:53:31 +00007428 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
7429 else
7430 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7431
7432 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7433 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
7434 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
7435 BDINFO_FLAGS_DISABLED);
7436
7437 /* Disable interrupts */
7438 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
7439
7440 /* Zero mailbox registers. */
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007441 if (tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX) {
7442 for (i = 1; i < TG3_IRQ_MAX_VECS; i++) {
7443 tp->napi[i].tx_prod = 0;
7444 tp->napi[i].tx_cons = 0;
Matt Carlsonc2353a32010-01-20 16:58:08 +00007445 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
7446 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007447 tw32_rx_mbox(tp->napi[i].consmbox, 0);
7448 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
7449 }
Matt Carlsonc2353a32010-01-20 16:58:08 +00007450 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS))
7451 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007452 } else {
7453 tp->napi[0].tx_prod = 0;
7454 tp->napi[0].tx_cons = 0;
7455 tw32_mailbox(tp->napi[0].prodmbox, 0);
7456 tw32_rx_mbox(tp->napi[0].consmbox, 0);
7457 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007458
7459 /* Make sure the NIC-based send BD rings are disabled. */
7460 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7461 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
7462 for (i = 0; i < 16; i++)
7463 tw32_tx_mbox(mbox + i * 8, 0);
7464 }
7465
7466 txrcb = NIC_SRAM_SEND_RCB;
7467 rxrcb = NIC_SRAM_RCV_RET_RCB;
7468
7469 /* Clear status block in ram. */
7470 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7471
7472 /* Set status block DMA address */
7473 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
7474 ((u64) tnapi->status_mapping >> 32));
7475 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
7476 ((u64) tnapi->status_mapping & 0xffffffff));
7477
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007478 if (tnapi->tx_ring) {
7479 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7480 (TG3_TX_RING_SIZE <<
7481 BDINFO_FLAGS_MAXLEN_SHIFT),
7482 NIC_SRAM_TX_BUFFER_DESC);
7483 txrcb += TG3_BDINFO_SIZE;
7484 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007485
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007486 if (tnapi->rx_rcb) {
7487 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
7488 (TG3_RX_RCB_RING_SIZE(tp) <<
7489 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
7490 rxrcb += TG3_BDINFO_SIZE;
7491 }
7492
7493 stblk = HOSTCC_STATBLCK_RING1;
7494
7495 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
7496 u64 mapping = (u64)tnapi->status_mapping;
7497 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
7498 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
7499
7500 /* Clear status block in ram. */
7501 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7502
Matt Carlson19cfaec2009-12-03 08:36:20 +00007503 if (tnapi->tx_ring) {
7504 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7505 (TG3_TX_RING_SIZE <<
7506 BDINFO_FLAGS_MAXLEN_SHIFT),
7507 NIC_SRAM_TX_BUFFER_DESC);
7508 txrcb += TG3_BDINFO_SIZE;
7509 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007510
7511 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
7512 (TG3_RX_RCB_RING_SIZE(tp) <<
7513 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
7514
7515 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007516 rxrcb += TG3_BDINFO_SIZE;
7517 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007518}
7519
7520/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07007521static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007522{
7523 u32 val, rdmac_mode;
7524 int i, err, limit;
Matt Carlson21f581a2009-08-28 14:00:25 +00007525 struct tg3_rx_prodring_set *tpr = &tp->prodring[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007526
7527 tg3_disable_ints(tp);
7528
7529 tg3_stop_fw(tp);
7530
7531 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
7532
7533 if (tp->tg3_flags & TG3_FLAG_INIT_COMPLETE) {
Michael Chane6de8ad2005-05-05 14:42:41 -07007534 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007535 }
7536
Matt Carlsondd477002008-05-25 23:45:58 -07007537 if (reset_phy &&
7538 !(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB))
Michael Chand4d2c552006-03-20 17:47:20 -08007539 tg3_phy_reset(tp);
7540
Linus Torvalds1da177e2005-04-16 15:20:36 -07007541 err = tg3_chip_reset(tp);
7542 if (err)
7543 return err;
7544
7545 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
7546
Matt Carlsonbcb37f62008-11-03 16:52:09 -08007547 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07007548 val = tr32(TG3_CPMU_CTRL);
7549 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
7550 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08007551
7552 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
7553 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
7554 val |= CPMU_LSPD_10MB_MACCLK_6_25;
7555 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
7556
7557 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
7558 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
7559 val |= CPMU_LNK_AWARE_MACCLK_6_25;
7560 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
7561
7562 val = tr32(TG3_CPMU_HST_ACC);
7563 val &= ~CPMU_HST_ACC_MACCLK_MASK;
7564 val |= CPMU_HST_ACC_MACCLK_6_25;
7565 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07007566 }
7567
Matt Carlson33466d932009-04-20 06:57:41 +00007568 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7569 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
7570 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
7571 PCIE_PWR_MGMT_L1_THRESH_4MS;
7572 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00007573
7574 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
7575 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
7576
7577 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d932009-04-20 06:57:41 +00007578
Matt Carlsonf40386c2009-11-02 14:24:02 +00007579 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7580 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00007581 }
7582
Matt Carlson614b0592010-01-20 16:58:02 +00007583 if (tp->tg3_flags3 & TG3_FLG3_L1PLLPD_EN) {
7584 u32 grc_mode = tr32(GRC_MODE);
7585
7586 /* Access the lower 1K of PL PCIE block registers. */
7587 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
7588 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
7589
7590 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
7591 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
7592 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
7593
7594 tw32(GRC_MODE, grc_mode);
7595 }
7596
Linus Torvalds1da177e2005-04-16 15:20:36 -07007597 /* This works around an issue with Athlon chipsets on
7598 * B3 tigon3 silicon. This bit has no effect on any
7599 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07007600 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007601 */
Matt Carlson795d01c2007-10-07 23:28:17 -07007602 if (!(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT)) {
7603 if (!(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
7604 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
7605 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7606 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007607
7608 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
7609 (tp->tg3_flags & TG3_FLAG_PCIX_MODE)) {
7610 val = tr32(TG3PCI_PCISTATE);
7611 val |= PCISTATE_RETRY_SAME_DMA;
7612 tw32(TG3PCI_PCISTATE, val);
7613 }
7614
Matt Carlson0d3031d2007-10-10 18:02:43 -07007615 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
7616 /* Allow reads and writes to the
7617 * APE register and memory space.
7618 */
7619 val = tr32(TG3PCI_PCISTATE);
7620 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
7621 PCISTATE_ALLOW_APE_SHMEM_WR;
7622 tw32(TG3PCI_PCISTATE, val);
7623 }
7624
Linus Torvalds1da177e2005-04-16 15:20:36 -07007625 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
7626 /* Enable some hw fixes. */
7627 val = tr32(TG3PCI_MSI_DATA);
7628 val |= (1 << 26) | (1 << 28) | (1 << 29);
7629 tw32(TG3PCI_MSI_DATA, val);
7630 }
7631
7632 /* Descriptor ring init may make accesses to the
7633 * NIC SRAM area to setup the TX descriptors, so we
7634 * can only do this after the hardware has been
7635 * successfully reset.
7636 */
Michael Chan32d8c572006-07-25 16:38:29 -07007637 err = tg3_init_rings(tp);
7638 if (err)
7639 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007640
Matt Carlsonb703df62009-12-03 08:36:21 +00007641 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
7642 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00007643 val = tr32(TG3PCI_DMA_RW_CTRL) &
7644 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
7645 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
7646 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
7647 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07007648 /* This value is determined during the probe time DMA
7649 * engine test, tg3_test_dma.
7650 */
7651 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
7652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007653
7654 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
7655 GRC_MODE_4X_NIC_SEND_RINGS |
7656 GRC_MODE_NO_TX_PHDR_CSUM |
7657 GRC_MODE_NO_RX_PHDR_CSUM);
7658 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07007659
7660 /* Pseudo-header checksum is done by hardware logic and not
7661 * the offload processers, so make the chip do the pseudo-
7662 * header checksums on receive. For transmit it is more
7663 * convenient to do the pseudo-header checksum in software
7664 * as Linux does that on transmit for us in all cases.
7665 */
7666 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007667
7668 tw32(GRC_MODE,
7669 tp->grc_mode |
7670 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
7671
7672 /* Setup the timer prescalar register. Clock is always 66Mhz. */
7673 val = tr32(GRC_MISC_CFG);
7674 val &= ~0xff;
7675 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
7676 tw32(GRC_MISC_CFG, val);
7677
7678 /* Initialize MBUF/DESC pool. */
John W. Linvillecbf46852005-04-21 17:01:29 -07007679 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007680 /* Do nothing. */
7681 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
7682 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
7683 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
7684 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
7685 else
7686 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
7687 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
7688 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
7689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007690 else if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) {
7691 int fw_len;
7692
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007693 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007694 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
7695 tw32(BUFMGR_MB_POOL_ADDR,
7696 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
7697 tw32(BUFMGR_MB_POOL_SIZE,
7698 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
7699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007700
Michael Chan0f893dc2005-07-25 12:30:38 -07007701 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007702 tw32(BUFMGR_MB_RDMA_LOW_WATER,
7703 tp->bufmgr_config.mbuf_read_dma_low_water);
7704 tw32(BUFMGR_MB_MACRX_LOW_WATER,
7705 tp->bufmgr_config.mbuf_mac_rx_low_water);
7706 tw32(BUFMGR_MB_HIGH_WATER,
7707 tp->bufmgr_config.mbuf_high_water);
7708 } else {
7709 tw32(BUFMGR_MB_RDMA_LOW_WATER,
7710 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
7711 tw32(BUFMGR_MB_MACRX_LOW_WATER,
7712 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
7713 tw32(BUFMGR_MB_HIGH_WATER,
7714 tp->bufmgr_config.mbuf_high_water_jumbo);
7715 }
7716 tw32(BUFMGR_DMA_LOW_WATER,
7717 tp->bufmgr_config.dma_low_water);
7718 tw32(BUFMGR_DMA_HIGH_WATER,
7719 tp->bufmgr_config.dma_high_water);
7720
7721 tw32(BUFMGR_MODE, BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE);
7722 for (i = 0; i < 2000; i++) {
7723 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
7724 break;
7725 udelay(10);
7726 }
7727 if (i >= 2000) {
7728 printk(KERN_ERR PFX "tg3_reset_hw cannot enable BUFMGR for %s.\n",
7729 tp->dev->name);
7730 return -ENODEV;
7731 }
7732
7733 /* Setup replenish threshold. */
Michael Chanf92905d2006-06-29 20:14:29 -07007734 val = tp->rx_pending / 8;
7735 if (val == 0)
7736 val = 1;
7737 else if (val > tp->rx_std_max_post)
7738 val = tp->rx_std_max_post;
Michael Chanb5d37722006-09-27 16:06:21 -07007739 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7740 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
7741 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
7742
7743 if (val > (TG3_RX_INTERNAL_RING_SZ_5906 / 2))
7744 val = TG3_RX_INTERNAL_RING_SZ_5906 / 2;
7745 }
Michael Chanf92905d2006-06-29 20:14:29 -07007746
7747 tw32(RCVBDI_STD_THRESH, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007748
7749 /* Initialize TG3_BDINFO's at:
7750 * RCVDBDI_STD_BD: standard eth size rx ring
7751 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
7752 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
7753 *
7754 * like so:
7755 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
7756 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
7757 * ring attribute flags
7758 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
7759 *
7760 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
7761 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
7762 *
7763 * The size of each ring is fixed in the firmware, but the location is
7764 * configurable.
7765 */
7766 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00007767 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007768 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00007769 ((u64) tpr->rx_std_mapping & 0xffffffff));
Matt Carlson87668d32009-11-13 13:03:34 +00007770 if (!(tp->tg3_flags3 & TG3_FLG3_5755_PLUS))
7771 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
7772 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007773
Matt Carlsonfdb72b32009-08-28 13:57:12 +00007774 /* Disable the mini ring */
7775 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007776 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
7777 BDINFO_FLAGS_DISABLED);
7778
Matt Carlsonfdb72b32009-08-28 13:57:12 +00007779 /* Program the jumbo buffer descriptor ring control
7780 * blocks on those devices that have them.
7781 */
Matt Carlson8f666b02009-08-28 13:58:24 +00007782 if ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) &&
Matt Carlsonfdb72b32009-08-28 13:57:12 +00007783 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007784 /* Setup replenish threshold. */
7785 tw32(RCVBDI_JUMBO_THRESH, tp->rx_jumbo_pending / 8);
7786
Michael Chan0f893dc2005-07-25 12:30:38 -07007787 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007788 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00007789 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007790 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00007791 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007792 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlson79ed5ac2009-08-28 14:00:55 +00007793 (RX_JUMBO_MAX_SIZE << BDINFO_FLAGS_MAXLEN_SHIFT) |
7794 BDINFO_FLAGS_USE_EXT_RECV);
Matt Carlson5fd68fb2010-01-20 16:58:07 +00007795 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
Matt Carlson87668d32009-11-13 13:03:34 +00007796 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
7797 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007798 } else {
7799 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
7800 BDINFO_FLAGS_DISABLED);
7801 }
7802
Matt Carlsonb703df62009-12-03 08:36:21 +00007803 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
7804 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007805 val = (RX_STD_MAX_SIZE_5705 << BDINFO_FLAGS_MAXLEN_SHIFT) |
7806 (RX_STD_MAX_SIZE << 2);
7807 else
7808 val = RX_STD_MAX_SIZE << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00007809 } else
7810 val = RX_STD_MAX_SIZE_5705 << BDINFO_FLAGS_MAXLEN_SHIFT;
7811
7812 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007813
Matt Carlson411da642009-11-13 13:03:46 +00007814 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e662009-11-13 13:03:49 +00007815 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007816
Matt Carlson411da642009-11-13 13:03:46 +00007817 tpr->rx_jmb_prod_idx = (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) ?
Matt Carlson21f581a2009-08-28 14:00:25 +00007818 tp->rx_jumbo_pending : 0;
Matt Carlson66711e662009-11-13 13:03:49 +00007819 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007820
Matt Carlsonb703df62009-12-03 08:36:21 +00007821 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
7822 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007823 tw32(STD_REPLENISH_LWM, 32);
7824 tw32(JMB_REPLENISH_LWM, 16);
7825 }
7826
Matt Carlson2d31eca2009-09-01 12:53:31 +00007827 tg3_rings_reset(tp);
7828
Linus Torvalds1da177e2005-04-16 15:20:36 -07007829 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07007830 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007831
7832 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00007833 tw32(MAC_RX_MTU_SIZE,
7834 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007835
7836 /* The slot time is changed by tg3_setup_phy if we
7837 * run at gigabit with half duplex.
7838 */
7839 tw32(MAC_TX_LENGTHS,
7840 (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
7841 (6 << TX_LENGTHS_IPG_SHIFT) |
7842 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
7843
7844 /* Receive rules. */
7845 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
7846 tw32(RCVLPC_CONFIG, 0x0181);
7847
7848 /* Calculate RDMAC_MODE setting early, we need it to determine
7849 * the RCVLPC_STATE_ENABLE mask.
7850 */
7851 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
7852 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
7853 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
7854 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
7855 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07007856
Matt Carlson57e69832008-05-25 23:48:31 -07007857 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08007858 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
7859 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07007860 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
7861 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
7862 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
7863
Michael Chan85e94ce2005-04-21 17:05:28 -07007864 /* If statement applies to 5705 and 5750 PCI devices only */
7865 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
7866 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) ||
7867 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007868 if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07007869 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007870 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
7871 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
7872 !(tp->tg3_flags2 & TG3_FLG2_IS_5788)) {
7873 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
7874 }
7875 }
7876
Michael Chan85e94ce2005-04-21 17:05:28 -07007877 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)
7878 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
7879
Linus Torvalds1da177e2005-04-16 15:20:36 -07007880 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
Matt Carlson027455a2008-12-21 20:19:30 -08007881 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
7882
Matt Carlsone849cdc2009-11-13 13:03:38 +00007883 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
7884 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson027455a2008-12-21 20:19:30 -08007885 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
7886 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007887
7888 /* Receive/send statistics. */
Michael Chan16613942006-06-29 20:15:13 -07007889 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
7890 val = tr32(RCVLPC_STATS_ENABLE);
7891 val &= ~RCVLPC_STATSENAB_DACK_FIX;
7892 tw32(RCVLPC_STATS_ENABLE, val);
7893 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
7894 (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007895 val = tr32(RCVLPC_STATS_ENABLE);
7896 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
7897 tw32(RCVLPC_STATS_ENABLE, val);
7898 } else {
7899 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
7900 }
7901 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
7902 tw32(SNDDATAI_STATSENAB, 0xffffff);
7903 tw32(SNDDATAI_STATSCTRL,
7904 (SNDDATAI_SCTRL_ENABLE |
7905 SNDDATAI_SCTRL_FASTUPD));
7906
7907 /* Setup host coalescing engine. */
7908 tw32(HOSTCC_MODE, 0);
7909 for (i = 0; i < 2000; i++) {
7910 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
7911 break;
7912 udelay(10);
7913 }
7914
Michael Chand244c892005-07-05 14:42:33 -07007915 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007916
Linus Torvalds1da177e2005-04-16 15:20:36 -07007917 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7918 /* Status/statistics block address. See tg3_timer,
7919 * the tg3_periodic_fetch_stats call there, and
7920 * tg3_get_stats to see how this works for 5705/5750 chips.
7921 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007922 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
7923 ((u64) tp->stats_mapping >> 32));
7924 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
7925 ((u64) tp->stats_mapping & 0xffffffff));
7926 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00007927
Linus Torvalds1da177e2005-04-16 15:20:36 -07007928 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00007929
7930 /* Clear statistics and status block memory areas */
7931 for (i = NIC_SRAM_STATS_BLK;
7932 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
7933 i += sizeof(u32)) {
7934 tg3_write_mem(tp, i, 0);
7935 udelay(40);
7936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007937 }
7938
7939 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
7940
7941 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
7942 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
7943 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7944 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
7945
Michael Chanc94e3942005-09-27 12:12:42 -07007946 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES) {
7947 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
7948 /* reset to prevent losing 1st rx packet intermittently */
7949 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
7950 udelay(10);
7951 }
7952
Matt Carlson3bda1252008-08-15 14:08:22 -07007953 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
7954 tp->mac_mode &= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
7955 else
7956 tp->mac_mode = 0;
7957 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Linus Torvalds1da177e2005-04-16 15:20:36 -07007958 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE | MAC_MODE_FHDE_ENABLE;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07007959 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
7960 !(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
7961 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
7962 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007963 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
7964 udelay(40);
7965
Michael Chan314fba32005-04-21 17:07:04 -07007966 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Michael Chan9d26e212006-12-07 00:21:14 -08007967 * If TG3_FLG2_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07007968 * register to preserve the GPIO settings for LOMs. The GPIOs,
7969 * whether used as inputs or outputs, are set by boot code after
7970 * reset.
7971 */
Michael Chan9d26e212006-12-07 00:21:14 -08007972 if (!(tp->tg3_flags2 & TG3_FLG2_IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07007973 u32 gpio_mask;
7974
Michael Chan9d26e212006-12-07 00:21:14 -08007975 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
7976 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
7977 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07007978
7979 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
7980 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
7981 GRC_LCLCTRL_GPIO_OUTPUT3;
7982
Michael Chanaf36e6b2006-03-23 01:28:06 -08007983 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
7984 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
7985
Gary Zambranoaaf84462007-05-05 11:51:45 -07007986 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07007987 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
7988
7989 /* GPIO1 must be driven high for eeprom write protect */
Michael Chan9d26e212006-12-07 00:21:14 -08007990 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT)
7991 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
7992 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07007993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007994 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
7995 udelay(100);
7996
Matt Carlsonbaf8a942009-09-01 13:13:00 +00007997 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX) {
7998 val = tr32(MSGINT_MODE);
7999 val |= MSGINT_MODE_MULTIVEC_EN | MSGINT_MODE_ENABLE;
8000 tw32(MSGINT_MODE, val);
8001 }
8002
Linus Torvalds1da177e2005-04-16 15:20:36 -07008003 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
8004 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
8005 udelay(40);
8006 }
8007
8008 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
8009 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
8010 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
8011 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
8012 WDMAC_MODE_LNGREAD_ENAB);
8013
Michael Chan85e94ce2005-04-21 17:05:28 -07008014 /* If statement applies to 5705 and 5750 PCI devices only */
8015 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8016 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) ||
8017 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750) {
Matt Carlson29ea0952009-08-25 10:07:54 +00008018 if ((tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008019 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
8020 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
8021 /* nothing */
8022 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
8023 !(tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
8024 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)) {
8025 val |= WDMAC_MODE_RX_ACCEL;
8026 }
8027 }
8028
Michael Chand9ab5ad12006-03-20 22:27:35 -08008029 /* Enable host coalescing bug fix */
Matt Carlson321d32a2008-11-21 17:22:19 -08008030 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Matt Carlsonf51f3562008-05-25 23:45:08 -07008031 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad12006-03-20 22:27:35 -08008032
Matt Carlson788a0352009-11-02 14:26:03 +00008033 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
8034 val |= WDMAC_MODE_BURST_ALL_DATA;
8035
Linus Torvalds1da177e2005-04-16 15:20:36 -07008036 tw32_f(WDMAC_MODE, val);
8037 udelay(40);
8038
Matt Carlson9974a352007-10-07 23:27:28 -07008039 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
8040 u16 pcix_cmd;
8041
8042 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8043 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008044 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07008045 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
8046 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008047 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07008048 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
8049 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008050 }
Matt Carlson9974a352007-10-07 23:27:28 -07008051 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8052 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008053 }
8054
8055 tw32_f(RDMAC_MODE, rdmac_mode);
8056 udelay(40);
8057
8058 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
8059 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
8060 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07008061
8062 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
8063 tw32(SNDDATAC_MODE,
8064 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
8065 else
8066 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
8067
Linus Torvalds1da177e2005-04-16 15:20:36 -07008068 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
8069 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
8070 tw32(RCVDBDI_MODE, RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ);
8071 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008072 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
8073 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008074 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Matt Carlson19cfaec2009-12-03 08:36:20 +00008075 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008076 val |= SNDBDI_MODE_MULTI_TXQ_EN;
8077 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008078 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
8079
8080 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
8081 err = tg3_load_5701_a0_firmware_fix(tp);
8082 if (err)
8083 return err;
8084 }
8085
Linus Torvalds1da177e2005-04-16 15:20:36 -07008086 if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) {
8087 err = tg3_load_tso_firmware(tp);
8088 if (err)
8089 return err;
8090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008091
8092 tp->tx_mode = TX_MODE_ENABLE;
8093 tw32_f(MAC_TX_MODE, tp->tx_mode);
8094 udelay(100);
8095
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008096 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS) {
8097 u32 reg = MAC_RSS_INDIR_TBL_0;
8098 u8 *ent = (u8 *)&val;
8099
8100 /* Setup the indirection table */
8101 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
8102 int idx = i % sizeof(val);
8103
8104 ent[idx] = i % (tp->irq_cnt - 1);
8105 if (idx == sizeof(val) - 1) {
8106 tw32(reg, val);
8107 reg += 4;
8108 }
8109 }
8110
8111 /* Setup the "secret" hash key. */
8112 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
8113 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
8114 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
8115 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
8116 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
8117 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
8118 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
8119 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
8120 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
8121 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
8122 }
8123
Linus Torvalds1da177e2005-04-16 15:20:36 -07008124 tp->rx_mode = RX_MODE_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08008125 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Michael Chanaf36e6b2006-03-23 01:28:06 -08008126 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
8127
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008128 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)
8129 tp->rx_mode |= RX_MODE_RSS_ENABLE |
8130 RX_MODE_RSS_ITBL_HASH_BITS_7 |
8131 RX_MODE_RSS_IPV6_HASH_EN |
8132 RX_MODE_RSS_TCP_IPV6_HASH_EN |
8133 RX_MODE_RSS_IPV4_HASH_EN |
8134 RX_MODE_RSS_TCP_IPV4_HASH_EN;
8135
Linus Torvalds1da177e2005-04-16 15:20:36 -07008136 tw32_f(MAC_RX_MODE, tp->rx_mode);
8137 udelay(10);
8138
Linus Torvalds1da177e2005-04-16 15:20:36 -07008139 tw32(MAC_LED_CTRL, tp->led_ctrl);
8140
8141 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Michael Chanc94e3942005-09-27 12:12:42 -07008142 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008143 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8144 udelay(10);
8145 }
8146 tw32_f(MAC_RX_MODE, tp->rx_mode);
8147 udelay(10);
8148
8149 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
8150 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
8151 !(tp->tg3_flags2 & TG3_FLG2_SERDES_PREEMPHASIS)) {
8152 /* Set drive transmission level to 1.2V */
8153 /* only if the signal pre-emphasis bit is not set */
8154 val = tr32(MAC_SERDES_CFG);
8155 val &= 0xfffff000;
8156 val |= 0x880;
8157 tw32(MAC_SERDES_CFG, val);
8158 }
8159 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
8160 tw32(MAC_SERDES_CFG, 0x616000);
8161 }
8162
8163 /* Prevent chip from dropping frames when flow control
8164 * is enabled.
8165 */
Matt Carlson666bc832010-01-20 16:58:03 +00008166 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8167 val = 1;
8168 else
8169 val = 2;
8170 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008171
8172 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
8173 (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)) {
8174 /* Use hardware link auto-negotiation */
8175 tp->tg3_flags2 |= TG3_FLG2_HW_AUTONEG;
8176 }
8177
Michael Chand4d2c552006-03-20 17:47:20 -08008178 if ((tp->tg3_flags2 & TG3_FLG2_MII_SERDES) &&
8179 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)) {
8180 u32 tmp;
8181
8182 tmp = tr32(SERDES_RX_CTRL);
8183 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
8184 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
8185 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
8186 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8187 }
8188
Matt Carlsondd477002008-05-25 23:45:58 -07008189 if (!(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)) {
8190 if (tp->link_config.phy_is_low_power) {
8191 tp->link_config.phy_is_low_power = 0;
8192 tp->link_config.speed = tp->link_config.orig_speed;
8193 tp->link_config.duplex = tp->link_config.orig_duplex;
8194 tp->link_config.autoneg = tp->link_config.orig_autoneg;
8195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008196
Matt Carlsondd477002008-05-25 23:45:58 -07008197 err = tg3_setup_phy(tp, 0);
8198 if (err)
8199 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008200
Matt Carlsondd477002008-05-25 23:45:58 -07008201 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
Matt Carlson7f97a4b2009-08-25 10:10:03 +00008202 !(tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07008203 u32 tmp;
8204
8205 /* Clear CRC stats. */
8206 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
8207 tg3_writephy(tp, MII_TG3_TEST1,
8208 tmp | MII_TG3_TEST1_CRC_EN);
8209 tg3_readphy(tp, 0x14, &tmp);
8210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008211 }
8212 }
8213
8214 __tg3_set_rx_mode(tp->dev);
8215
8216 /* Initialize receive rules. */
8217 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
8218 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
8219 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
8220 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
8221
Michael Chan4cf78e42005-07-25 12:29:19 -07008222 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Michael Chana4e2b342005-10-26 15:46:52 -07008223 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008224 limit = 8;
8225 else
8226 limit = 16;
8227 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF)
8228 limit -= 4;
8229 switch (limit) {
8230 case 16:
8231 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
8232 case 15:
8233 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
8234 case 14:
8235 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
8236 case 13:
8237 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
8238 case 12:
8239 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
8240 case 11:
8241 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
8242 case 10:
8243 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
8244 case 9:
8245 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
8246 case 8:
8247 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
8248 case 7:
8249 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
8250 case 6:
8251 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
8252 case 5:
8253 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
8254 case 4:
8255 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
8256 case 3:
8257 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
8258 case 2:
8259 case 1:
8260
8261 default:
8262 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07008263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008264
Matt Carlson9ce768e2007-10-11 19:49:11 -07008265 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
8266 /* Write our heartbeat update interval to APE. */
8267 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
8268 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07008269
Linus Torvalds1da177e2005-04-16 15:20:36 -07008270 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
8271
Linus Torvalds1da177e2005-04-16 15:20:36 -07008272 return 0;
8273}
8274
8275/* Called at device open time to get the chip ready for
8276 * packet processing. Invoked with tp->lock held.
8277 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008278static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008279{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008280 tg3_switch_clocks(tp);
8281
8282 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
8283
Matt Carlson2f751b62008-08-04 23:17:34 -07008284 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008285}
8286
8287#define TG3_STAT_ADD32(PSTAT, REG) \
8288do { u32 __val = tr32(REG); \
8289 (PSTAT)->low += __val; \
8290 if ((PSTAT)->low < __val) \
8291 (PSTAT)->high += 1; \
8292} while (0)
8293
8294static void tg3_periodic_fetch_stats(struct tg3 *tp)
8295{
8296 struct tg3_hw_stats *sp = tp->hw_stats;
8297
8298 if (!netif_carrier_ok(tp->dev))
8299 return;
8300
8301 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
8302 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
8303 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
8304 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
8305 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
8306 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
8307 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
8308 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
8309 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
8310 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
8311 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
8312 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
8313 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
8314
8315 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
8316 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
8317 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
8318 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
8319 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
8320 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
8321 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
8322 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
8323 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
8324 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
8325 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
8326 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
8327 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
8328 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07008329
8330 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
8331 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
8332 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008333}
8334
8335static void tg3_timer(unsigned long __opaque)
8336{
8337 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008338
Michael Chanf475f162006-03-27 23:20:14 -08008339 if (tp->irq_sync)
8340 goto restart_timer;
8341
David S. Millerf47c11e2005-06-24 20:18:35 -07008342 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008343
David S. Millerfac9b832005-05-18 22:46:34 -07008344 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)) {
8345 /* All of this garbage is because when using non-tagged
8346 * IRQ status the mailbox/status_block protocol the chip
8347 * uses with the cpu is race prone.
8348 */
Matt Carlson898a56f2009-08-28 14:02:40 +00008349 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -07008350 tw32(GRC_LOCAL_CTRL,
8351 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
8352 } else {
8353 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00008354 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -07008355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008356
David S. Millerfac9b832005-05-18 22:46:34 -07008357 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
8358 tp->tg3_flags2 |= TG3_FLG2_RESTART_TIMER;
David S. Millerf47c11e2005-06-24 20:18:35 -07008359 spin_unlock(&tp->lock);
David S. Millerfac9b832005-05-18 22:46:34 -07008360 schedule_work(&tp->reset_task);
8361 return;
8362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008363 }
8364
Linus Torvalds1da177e2005-04-16 15:20:36 -07008365 /* This part only runs once per second. */
8366 if (!--tp->timer_counter) {
David S. Millerfac9b832005-05-18 22:46:34 -07008367 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
8368 tg3_periodic_fetch_stats(tp);
8369
Linus Torvalds1da177e2005-04-16 15:20:36 -07008370 if (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) {
8371 u32 mac_stat;
8372 int phy_event;
8373
8374 mac_stat = tr32(MAC_STATUS);
8375
8376 phy_event = 0;
8377 if (tp->tg3_flags & TG3_FLAG_USE_MI_INTERRUPT) {
8378 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
8379 phy_event = 1;
8380 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
8381 phy_event = 1;
8382
8383 if (phy_event)
8384 tg3_setup_phy(tp, 0);
8385 } else if (tp->tg3_flags & TG3_FLAG_POLL_SERDES) {
8386 u32 mac_stat = tr32(MAC_STATUS);
8387 int need_setup = 0;
8388
8389 if (netif_carrier_ok(tp->dev) &&
8390 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
8391 need_setup = 1;
8392 }
8393 if (! netif_carrier_ok(tp->dev) &&
8394 (mac_stat & (MAC_STATUS_PCS_SYNCED |
8395 MAC_STATUS_SIGNAL_DET))) {
8396 need_setup = 1;
8397 }
8398 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07008399 if (!tp->serdes_counter) {
8400 tw32_f(MAC_MODE,
8401 (tp->mac_mode &
8402 ~MAC_MODE_PORT_MODE_MASK));
8403 udelay(40);
8404 tw32_f(MAC_MODE, tp->mac_mode);
8405 udelay(40);
8406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008407 tg3_setup_phy(tp, 0);
8408 }
Michael Chan747e8f82005-07-25 12:33:22 -07008409 } else if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)
8410 tg3_serdes_parallel_detect(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008411
8412 tp->timer_counter = tp->timer_multiplier;
8413 }
8414
Michael Chan130b8e42006-09-27 16:00:40 -07008415 /* Heartbeat is only sent once every 2 seconds.
8416 *
8417 * The heartbeat is to tell the ASF firmware that the host
8418 * driver is still alive. In the event that the OS crashes,
8419 * ASF needs to reset the hardware to free up the FIFO space
8420 * that may be filled with rx packets destined for the host.
8421 * If the FIFO is full, ASF will no longer function properly.
8422 *
8423 * Unintended resets have been reported on real time kernels
8424 * where the timer doesn't run on time. Netpoll will also have
8425 * same problem.
8426 *
8427 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
8428 * to check the ring condition when the heartbeat is expiring
8429 * before doing the reset. This will prevent most unintended
8430 * resets.
8431 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008432 if (!--tp->asf_counter) {
Matt Carlsonbc7959b2008-08-15 14:08:55 -07008433 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
8434 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07008435 tg3_wait_for_event_ack(tp);
8436
Michael Chanbbadf502006-04-06 21:46:34 -07008437 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07008438 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07008439 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Michael Chan28fbef72005-10-26 15:48:35 -07008440 /* 5 seconds timeout */
Michael Chanbbadf502006-04-06 21:46:34 -07008441 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX, 5);
Matt Carlson4ba526c2008-08-15 14:10:04 -07008442
8443 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008444 }
8445 tp->asf_counter = tp->asf_multiplier;
8446 }
8447
David S. Millerf47c11e2005-06-24 20:18:35 -07008448 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008449
Michael Chanf475f162006-03-27 23:20:14 -08008450restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07008451 tp->timer.expires = jiffies + tp->timer_offset;
8452 add_timer(&tp->timer);
8453}
8454
Matt Carlson4f125f42009-09-01 12:55:02 +00008455static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -08008456{
David Howells7d12e782006-10-05 14:55:46 +01008457 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008458 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +00008459 char *name;
8460 struct tg3_napi *tnapi = &tp->napi[irq_num];
8461
8462 if (tp->irq_cnt == 1)
8463 name = tp->dev->name;
8464 else {
8465 name = &tnapi->irq_lbl[0];
8466 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
8467 name[IFNAMSIZ-1] = 0;
8468 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08008469
Matt Carlson679563f2009-09-01 12:55:46 +00008470 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI_OR_MSIX) {
Michael Chanfcfa0a32006-03-20 22:28:41 -08008471 fn = tg3_msi;
8472 if (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)
8473 fn = tg3_msi_1shot;
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07008474 flags = IRQF_SAMPLE_RANDOM;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008475 } else {
8476 fn = tg3_interrupt;
8477 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)
8478 fn = tg3_interrupt_tagged;
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07008479 flags = IRQF_SHARED | IRQF_SAMPLE_RANDOM;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008480 }
Matt Carlson4f125f42009-09-01 12:55:02 +00008481
8482 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08008483}
8484
Michael Chan79381092005-04-21 17:13:59 -07008485static int tg3_test_interrupt(struct tg3 *tp)
8486{
Matt Carlson09943a12009-08-28 14:01:57 +00008487 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -07008488 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -07008489 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008490 u32 val;
Michael Chan79381092005-04-21 17:13:59 -07008491
Michael Chand4bc3922005-05-29 14:59:20 -07008492 if (!netif_running(dev))
8493 return -ENODEV;
8494
Michael Chan79381092005-04-21 17:13:59 -07008495 tg3_disable_ints(tp);
8496
Matt Carlson4f125f42009-09-01 12:55:02 +00008497 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07008498
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008499 /*
8500 * Turn off MSI one shot mode. Otherwise this test has no
8501 * observable way to know whether the interrupt was delivered.
8502 */
Matt Carlsonb703df62009-12-03 08:36:21 +00008503 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
8504 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008505 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
8506 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
8507 tw32(MSGINT_MODE, val);
8508 }
8509
Matt Carlson4f125f42009-09-01 12:55:02 +00008510 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Matt Carlson09943a12009-08-28 14:01:57 +00008511 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07008512 if (err)
8513 return err;
8514
Matt Carlson898a56f2009-08-28 14:02:40 +00008515 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -07008516 tg3_enable_ints(tp);
8517
8518 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00008519 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -07008520
8521 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -07008522 u32 int_mbox, misc_host_ctrl;
8523
Matt Carlson898a56f2009-08-28 14:02:40 +00008524 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -07008525 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
8526
8527 if ((int_mbox != 0) ||
8528 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
8529 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -07008530 break;
Michael Chanb16250e2006-09-27 16:10:14 -07008531 }
8532
Michael Chan79381092005-04-21 17:13:59 -07008533 msleep(10);
8534 }
8535
8536 tg3_disable_ints(tp);
8537
Matt Carlson4f125f42009-09-01 12:55:02 +00008538 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008539
Matt Carlson4f125f42009-09-01 12:55:02 +00008540 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07008541
8542 if (err)
8543 return err;
8544
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008545 if (intr_ok) {
8546 /* Reenable MSI one shot mode. */
Matt Carlsonb703df62009-12-03 08:36:21 +00008547 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
8548 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008549 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
8550 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
8551 tw32(MSGINT_MODE, val);
8552 }
Michael Chan79381092005-04-21 17:13:59 -07008553 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008554 }
Michael Chan79381092005-04-21 17:13:59 -07008555
8556 return -EIO;
8557}
8558
8559/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
8560 * successfully restored
8561 */
8562static int tg3_test_msi(struct tg3 *tp)
8563{
Michael Chan79381092005-04-21 17:13:59 -07008564 int err;
8565 u16 pci_cmd;
8566
8567 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSI))
8568 return 0;
8569
8570 /* Turn off SERR reporting in case MSI terminates with Master
8571 * Abort.
8572 */
8573 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
8574 pci_write_config_word(tp->pdev, PCI_COMMAND,
8575 pci_cmd & ~PCI_COMMAND_SERR);
8576
8577 err = tg3_test_interrupt(tp);
8578
8579 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
8580
8581 if (!err)
8582 return 0;
8583
8584 /* other failures */
8585 if (err != -EIO)
8586 return err;
8587
8588 /* MSI test failed, go back to INTx mode */
8589 printk(KERN_WARNING PFX "%s: No interrupt was generated using MSI, "
8590 "switching to INTx mode. Please report this failure to "
8591 "the PCI maintainer and include system chipset information.\n",
8592 tp->dev->name);
8593
Matt Carlson4f125f42009-09-01 12:55:02 +00008594 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +00008595
Michael Chan79381092005-04-21 17:13:59 -07008596 pci_disable_msi(tp->pdev);
8597
8598 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI;
8599
Matt Carlson4f125f42009-09-01 12:55:02 +00008600 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07008601 if (err)
8602 return err;
8603
8604 /* Need to reset the chip because the MSI cycle may have terminated
8605 * with Master Abort.
8606 */
David S. Millerf47c11e2005-06-24 20:18:35 -07008607 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07008608
Michael Chan944d9802005-05-29 14:57:48 -07008609 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008610 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07008611
David S. Millerf47c11e2005-06-24 20:18:35 -07008612 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07008613
8614 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +00008615 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -07008616
8617 return err;
8618}
8619
Matt Carlson9e9fd122009-01-19 16:57:45 -08008620static int tg3_request_firmware(struct tg3 *tp)
8621{
8622 const __be32 *fw_data;
8623
8624 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
8625 printk(KERN_ERR "%s: Failed to load firmware \"%s\"\n",
8626 tp->dev->name, tp->fw_needed);
8627 return -ENOENT;
8628 }
8629
8630 fw_data = (void *)tp->fw->data;
8631
8632 /* Firmware blob starts with version numbers, followed by
8633 * start address and _full_ length including BSS sections
8634 * (which must be longer than the actual data, of course
8635 */
8636
8637 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
8638 if (tp->fw_len < (tp->fw->size - 12)) {
8639 printk(KERN_ERR "%s: bogus length %d in \"%s\"\n",
8640 tp->dev->name, tp->fw_len, tp->fw_needed);
8641 release_firmware(tp->fw);
8642 tp->fw = NULL;
8643 return -EINVAL;
8644 }
8645
8646 /* We no longer need firmware; we have it. */
8647 tp->fw_needed = NULL;
8648 return 0;
8649}
8650
Matt Carlson679563f2009-09-01 12:55:46 +00008651static bool tg3_enable_msix(struct tg3 *tp)
8652{
8653 int i, rc, cpus = num_online_cpus();
8654 struct msix_entry msix_ent[tp->irq_max];
8655
8656 if (cpus == 1)
8657 /* Just fallback to the simpler MSI mode. */
8658 return false;
8659
8660 /*
8661 * We want as many rx rings enabled as there are cpus.
8662 * The first MSIX vector only deals with link interrupts, etc,
8663 * so we add one to the number of vectors we are requesting.
8664 */
8665 tp->irq_cnt = min_t(unsigned, cpus + 1, tp->irq_max);
8666
8667 for (i = 0; i < tp->irq_max; i++) {
8668 msix_ent[i].entry = i;
8669 msix_ent[i].vector = 0;
8670 }
8671
8672 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
8673 if (rc != 0) {
8674 if (rc < TG3_RSS_MIN_NUM_MSIX_VECS)
8675 return false;
8676 if (pci_enable_msix(tp->pdev, msix_ent, rc))
8677 return false;
8678 printk(KERN_NOTICE
8679 "%s: Requested %d MSI-X vectors, received %d\n",
8680 tp->dev->name, tp->irq_cnt, rc);
8681 tp->irq_cnt = rc;
8682 }
8683
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008684 tp->tg3_flags3 |= TG3_FLG3_ENABLE_RSS;
8685
Matt Carlson679563f2009-09-01 12:55:46 +00008686 for (i = 0; i < tp->irq_max; i++)
8687 tp->napi[i].irq_vec = msix_ent[i].vector;
8688
Matt Carlson19cfaec2009-12-03 08:36:20 +00008689 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
8690 tp->tg3_flags3 |= TG3_FLG3_ENABLE_TSS;
8691 tp->dev->real_num_tx_queues = tp->irq_cnt - 1;
8692 } else
8693 tp->dev->real_num_tx_queues = 1;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00008694
Matt Carlson679563f2009-09-01 12:55:46 +00008695 return true;
8696}
8697
Matt Carlson07b01732009-08-28 14:01:15 +00008698static void tg3_ints_init(struct tg3 *tp)
8699{
Matt Carlson679563f2009-09-01 12:55:46 +00008700 if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSI_OR_MSIX) &&
8701 !(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +00008702 /* All MSI supporting chips should support tagged
8703 * status. Assert that this is the case.
8704 */
Matt Carlson679563f2009-09-01 12:55:46 +00008705 printk(KERN_WARNING PFX "%s: MSI without TAGGED? "
8706 "Not using MSI.\n", tp->dev->name);
8707 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +00008708 }
Matt Carlson4f125f42009-09-01 12:55:02 +00008709
Matt Carlson679563f2009-09-01 12:55:46 +00008710 if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX) && tg3_enable_msix(tp))
8711 tp->tg3_flags2 |= TG3_FLG2_USING_MSIX;
8712 else if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSI) &&
8713 pci_enable_msi(tp->pdev) == 0)
8714 tp->tg3_flags2 |= TG3_FLG2_USING_MSI;
8715
8716 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI_OR_MSIX) {
8717 u32 msi_mode = tr32(MSGINT_MODE);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008718 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
8719 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson679563f2009-09-01 12:55:46 +00008720 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
8721 }
8722defcfg:
8723 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSIX)) {
8724 tp->irq_cnt = 1;
8725 tp->napi[0].irq_vec = tp->pdev->irq;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00008726 tp->dev->real_num_tx_queues = 1;
Matt Carlson679563f2009-09-01 12:55:46 +00008727 }
Matt Carlson07b01732009-08-28 14:01:15 +00008728}
8729
8730static void tg3_ints_fini(struct tg3 *tp)
8731{
Matt Carlson679563f2009-09-01 12:55:46 +00008732 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
8733 pci_disable_msix(tp->pdev);
8734 else if (tp->tg3_flags2 & TG3_FLG2_USING_MSI)
8735 pci_disable_msi(tp->pdev);
8736 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI_OR_MSIX;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008737 tp->tg3_flags3 &= ~TG3_FLG3_ENABLE_RSS;
Matt Carlson07b01732009-08-28 14:01:15 +00008738}
8739
Linus Torvalds1da177e2005-04-16 15:20:36 -07008740static int tg3_open(struct net_device *dev)
8741{
8742 struct tg3 *tp = netdev_priv(dev);
Matt Carlson4f125f42009-09-01 12:55:02 +00008743 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008744
Matt Carlson9e9fd122009-01-19 16:57:45 -08008745 if (tp->fw_needed) {
8746 err = tg3_request_firmware(tp);
8747 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
8748 if (err)
8749 return err;
8750 } else if (err) {
8751 printk(KERN_WARNING "%s: TSO capability disabled.\n",
8752 tp->dev->name);
8753 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
8754 } else if (!(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
8755 printk(KERN_NOTICE "%s: TSO capability restored.\n",
8756 tp->dev->name);
8757 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
8758 }
8759 }
8760
Michael Chanc49a1562006-12-17 17:07:29 -08008761 netif_carrier_off(tp->dev);
8762
Michael Chanbc1c7562006-03-20 17:48:03 -08008763 err = tg3_set_power_state(tp, PCI_D0);
Matt Carlson2f751b62008-08-04 23:17:34 -07008764 if (err)
Michael Chanbc1c7562006-03-20 17:48:03 -08008765 return err;
Matt Carlson2f751b62008-08-04 23:17:34 -07008766
8767 tg3_full_lock(tp, 0);
Michael Chanbc1c7562006-03-20 17:48:03 -08008768
Linus Torvalds1da177e2005-04-16 15:20:36 -07008769 tg3_disable_ints(tp);
8770 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
8771
David S. Millerf47c11e2005-06-24 20:18:35 -07008772 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008773
Matt Carlson679563f2009-09-01 12:55:46 +00008774 /*
8775 * Setup interrupts first so we know how
8776 * many NAPI resources to allocate
8777 */
8778 tg3_ints_init(tp);
8779
Linus Torvalds1da177e2005-04-16 15:20:36 -07008780 /* The placement of this call is tied
8781 * to the setup and use of Host TX descriptors.
8782 */
8783 err = tg3_alloc_consistent(tp);
8784 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00008785 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008786
Matt Carlsonfed97812009-09-01 13:10:19 +00008787 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07008788
Matt Carlson4f125f42009-09-01 12:55:02 +00008789 for (i = 0; i < tp->irq_cnt; i++) {
8790 struct tg3_napi *tnapi = &tp->napi[i];
8791 err = tg3_request_irq(tp, i);
8792 if (err) {
8793 for (i--; i >= 0; i--)
8794 free_irq(tnapi->irq_vec, tnapi);
8795 break;
8796 }
8797 }
Matt Carlson07b01732009-08-28 14:01:15 +00008798
8799 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00008800 goto err_out2;
Matt Carlson07b01732009-08-28 14:01:15 +00008801
David S. Millerf47c11e2005-06-24 20:18:35 -07008802 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008803
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008804 err = tg3_init_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008805 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -07008806 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008807 tg3_free_rings(tp);
8808 } else {
David S. Millerfac9b832005-05-18 22:46:34 -07008809 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)
8810 tp->timer_offset = HZ;
8811 else
8812 tp->timer_offset = HZ / 10;
8813
8814 BUG_ON(tp->timer_offset > HZ);
8815 tp->timer_counter = tp->timer_multiplier =
8816 (HZ / tp->timer_offset);
8817 tp->asf_counter = tp->asf_multiplier =
Michael Chan28fbef72005-10-26 15:48:35 -07008818 ((HZ / tp->timer_offset) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008819
8820 init_timer(&tp->timer);
8821 tp->timer.expires = jiffies + tp->timer_offset;
8822 tp->timer.data = (unsigned long) tp;
8823 tp->timer.function = tg3_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008824 }
8825
David S. Millerf47c11e2005-06-24 20:18:35 -07008826 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008827
Matt Carlson07b01732009-08-28 14:01:15 +00008828 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00008829 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008830
Michael Chan79381092005-04-21 17:13:59 -07008831 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
8832 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -07008833
Michael Chan79381092005-04-21 17:13:59 -07008834 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -07008835 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -07008836 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -07008837 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07008838 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07008839
Matt Carlson679563f2009-09-01 12:55:46 +00008840 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -07008841 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08008842
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008843 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
Matt Carlsonb703df62009-12-03 08:36:21 +00008844 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57765 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008845 (tp->tg3_flags2 & TG3_FLG2_USING_MSI) &&
8846 (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)) {
8847 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -08008848
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008849 tw32(PCIE_TRANSACTION_CFG,
8850 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -08008851 }
Michael Chan79381092005-04-21 17:13:59 -07008852 }
8853
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07008854 tg3_phy_start(tp);
8855
David S. Millerf47c11e2005-06-24 20:18:35 -07008856 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008857
Michael Chan79381092005-04-21 17:13:59 -07008858 add_timer(&tp->timer);
8859 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008860 tg3_enable_ints(tp);
8861
David S. Millerf47c11e2005-06-24 20:18:35 -07008862 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008863
Matt Carlsonfe5f5782009-09-01 13:09:39 +00008864 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008865
8866 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +00008867
Matt Carlson679563f2009-09-01 12:55:46 +00008868err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +00008869 for (i = tp->irq_cnt - 1; i >= 0; i--) {
8870 struct tg3_napi *tnapi = &tp->napi[i];
8871 free_irq(tnapi->irq_vec, tnapi);
8872 }
Matt Carlson07b01732009-08-28 14:01:15 +00008873
Matt Carlson679563f2009-09-01 12:55:46 +00008874err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +00008875 tg3_napi_disable(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00008876 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +00008877
8878err_out1:
8879 tg3_ints_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00008880 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008881}
8882
8883#if 0
8884/*static*/ void tg3_dump_state(struct tg3 *tp)
8885{
8886 u32 val32, val32_2, val32_3, val32_4, val32_5;
8887 u16 val16;
8888 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00008889 struct tg3_hw_status *sblk = tp->napi[0]->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008890
8891 pci_read_config_word(tp->pdev, PCI_STATUS, &val16);
8892 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE, &val32);
8893 printk("DEBUG: PCI status [%04x] TG3PCI state[%08x]\n",
8894 val16, val32);
8895
8896 /* MAC block */
8897 printk("DEBUG: MAC_MODE[%08x] MAC_STATUS[%08x]\n",
8898 tr32(MAC_MODE), tr32(MAC_STATUS));
8899 printk(" MAC_EVENT[%08x] MAC_LED_CTRL[%08x]\n",
8900 tr32(MAC_EVENT), tr32(MAC_LED_CTRL));
8901 printk("DEBUG: MAC_TX_MODE[%08x] MAC_TX_STATUS[%08x]\n",
8902 tr32(MAC_TX_MODE), tr32(MAC_TX_STATUS));
8903 printk(" MAC_RX_MODE[%08x] MAC_RX_STATUS[%08x]\n",
8904 tr32(MAC_RX_MODE), tr32(MAC_RX_STATUS));
8905
8906 /* Send data initiator control block */
8907 printk("DEBUG: SNDDATAI_MODE[%08x] SNDDATAI_STATUS[%08x]\n",
8908 tr32(SNDDATAI_MODE), tr32(SNDDATAI_STATUS));
8909 printk(" SNDDATAI_STATSCTRL[%08x]\n",
8910 tr32(SNDDATAI_STATSCTRL));
8911
8912 /* Send data completion control block */
8913 printk("DEBUG: SNDDATAC_MODE[%08x]\n", tr32(SNDDATAC_MODE));
8914
8915 /* Send BD ring selector block */
8916 printk("DEBUG: SNDBDS_MODE[%08x] SNDBDS_STATUS[%08x]\n",
8917 tr32(SNDBDS_MODE), tr32(SNDBDS_STATUS));
8918
8919 /* Send BD initiator control block */
8920 printk("DEBUG: SNDBDI_MODE[%08x] SNDBDI_STATUS[%08x]\n",
8921 tr32(SNDBDI_MODE), tr32(SNDBDI_STATUS));
8922
8923 /* Send BD completion control block */
8924 printk("DEBUG: SNDBDC_MODE[%08x]\n", tr32(SNDBDC_MODE));
8925
8926 /* Receive list placement control block */
8927 printk("DEBUG: RCVLPC_MODE[%08x] RCVLPC_STATUS[%08x]\n",
8928 tr32(RCVLPC_MODE), tr32(RCVLPC_STATUS));
8929 printk(" RCVLPC_STATSCTRL[%08x]\n",
8930 tr32(RCVLPC_STATSCTRL));
8931
8932 /* Receive data and receive BD initiator control block */
8933 printk("DEBUG: RCVDBDI_MODE[%08x] RCVDBDI_STATUS[%08x]\n",
8934 tr32(RCVDBDI_MODE), tr32(RCVDBDI_STATUS));
8935
8936 /* Receive data completion control block */
8937 printk("DEBUG: RCVDCC_MODE[%08x]\n",
8938 tr32(RCVDCC_MODE));
8939
8940 /* Receive BD initiator control block */
8941 printk("DEBUG: RCVBDI_MODE[%08x] RCVBDI_STATUS[%08x]\n",
8942 tr32(RCVBDI_MODE), tr32(RCVBDI_STATUS));
8943
8944 /* Receive BD completion control block */
8945 printk("DEBUG: RCVCC_MODE[%08x] RCVCC_STATUS[%08x]\n",
8946 tr32(RCVCC_MODE), tr32(RCVCC_STATUS));
8947
8948 /* Receive list selector control block */
8949 printk("DEBUG: RCVLSC_MODE[%08x] RCVLSC_STATUS[%08x]\n",
8950 tr32(RCVLSC_MODE), tr32(RCVLSC_STATUS));
8951
8952 /* Mbuf cluster free block */
8953 printk("DEBUG: MBFREE_MODE[%08x] MBFREE_STATUS[%08x]\n",
8954 tr32(MBFREE_MODE), tr32(MBFREE_STATUS));
8955
8956 /* Host coalescing control block */
8957 printk("DEBUG: HOSTCC_MODE[%08x] HOSTCC_STATUS[%08x]\n",
8958 tr32(HOSTCC_MODE), tr32(HOSTCC_STATUS));
8959 printk("DEBUG: HOSTCC_STATS_BLK_HOST_ADDR[%08x%08x]\n",
8960 tr32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH),
8961 tr32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW));
8962 printk("DEBUG: HOSTCC_STATUS_BLK_HOST_ADDR[%08x%08x]\n",
8963 tr32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH),
8964 tr32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW));
8965 printk("DEBUG: HOSTCC_STATS_BLK_NIC_ADDR[%08x]\n",
8966 tr32(HOSTCC_STATS_BLK_NIC_ADDR));
8967 printk("DEBUG: HOSTCC_STATUS_BLK_NIC_ADDR[%08x]\n",
8968 tr32(HOSTCC_STATUS_BLK_NIC_ADDR));
8969
8970 /* Memory arbiter control block */
8971 printk("DEBUG: MEMARB_MODE[%08x] MEMARB_STATUS[%08x]\n",
8972 tr32(MEMARB_MODE), tr32(MEMARB_STATUS));
8973
8974 /* Buffer manager control block */
8975 printk("DEBUG: BUFMGR_MODE[%08x] BUFMGR_STATUS[%08x]\n",
8976 tr32(BUFMGR_MODE), tr32(BUFMGR_STATUS));
8977 printk("DEBUG: BUFMGR_MB_POOL_ADDR[%08x] BUFMGR_MB_POOL_SIZE[%08x]\n",
8978 tr32(BUFMGR_MB_POOL_ADDR), tr32(BUFMGR_MB_POOL_SIZE));
8979 printk("DEBUG: BUFMGR_DMA_DESC_POOL_ADDR[%08x] "
8980 "BUFMGR_DMA_DESC_POOL_SIZE[%08x]\n",
8981 tr32(BUFMGR_DMA_DESC_POOL_ADDR),
8982 tr32(BUFMGR_DMA_DESC_POOL_SIZE));
8983
8984 /* Read DMA control block */
8985 printk("DEBUG: RDMAC_MODE[%08x] RDMAC_STATUS[%08x]\n",
8986 tr32(RDMAC_MODE), tr32(RDMAC_STATUS));
8987
8988 /* Write DMA control block */
8989 printk("DEBUG: WDMAC_MODE[%08x] WDMAC_STATUS[%08x]\n",
8990 tr32(WDMAC_MODE), tr32(WDMAC_STATUS));
8991
8992 /* DMA completion block */
8993 printk("DEBUG: DMAC_MODE[%08x]\n",
8994 tr32(DMAC_MODE));
8995
8996 /* GRC block */
8997 printk("DEBUG: GRC_MODE[%08x] GRC_MISC_CFG[%08x]\n",
8998 tr32(GRC_MODE), tr32(GRC_MISC_CFG));
8999 printk("DEBUG: GRC_LOCAL_CTRL[%08x]\n",
9000 tr32(GRC_LOCAL_CTRL));
9001
9002 /* TG3_BDINFOs */
9003 printk("DEBUG: RCVDBDI_JUMBO_BD[%08x%08x:%08x:%08x]\n",
9004 tr32(RCVDBDI_JUMBO_BD + 0x0),
9005 tr32(RCVDBDI_JUMBO_BD + 0x4),
9006 tr32(RCVDBDI_JUMBO_BD + 0x8),
9007 tr32(RCVDBDI_JUMBO_BD + 0xc));
9008 printk("DEBUG: RCVDBDI_STD_BD[%08x%08x:%08x:%08x]\n",
9009 tr32(RCVDBDI_STD_BD + 0x0),
9010 tr32(RCVDBDI_STD_BD + 0x4),
9011 tr32(RCVDBDI_STD_BD + 0x8),
9012 tr32(RCVDBDI_STD_BD + 0xc));
9013 printk("DEBUG: RCVDBDI_MINI_BD[%08x%08x:%08x:%08x]\n",
9014 tr32(RCVDBDI_MINI_BD + 0x0),
9015 tr32(RCVDBDI_MINI_BD + 0x4),
9016 tr32(RCVDBDI_MINI_BD + 0x8),
9017 tr32(RCVDBDI_MINI_BD + 0xc));
9018
9019 tg3_read_mem(tp, NIC_SRAM_SEND_RCB + 0x0, &val32);
9020 tg3_read_mem(tp, NIC_SRAM_SEND_RCB + 0x4, &val32_2);
9021 tg3_read_mem(tp, NIC_SRAM_SEND_RCB + 0x8, &val32_3);
9022 tg3_read_mem(tp, NIC_SRAM_SEND_RCB + 0xc, &val32_4);
9023 printk("DEBUG: SRAM_SEND_RCB_0[%08x%08x:%08x:%08x]\n",
9024 val32, val32_2, val32_3, val32_4);
9025
9026 tg3_read_mem(tp, NIC_SRAM_RCV_RET_RCB + 0x0, &val32);
9027 tg3_read_mem(tp, NIC_SRAM_RCV_RET_RCB + 0x4, &val32_2);
9028 tg3_read_mem(tp, NIC_SRAM_RCV_RET_RCB + 0x8, &val32_3);
9029 tg3_read_mem(tp, NIC_SRAM_RCV_RET_RCB + 0xc, &val32_4);
9030 printk("DEBUG: SRAM_RCV_RET_RCB_0[%08x%08x:%08x:%08x]\n",
9031 val32, val32_2, val32_3, val32_4);
9032
9033 tg3_read_mem(tp, NIC_SRAM_STATUS_BLK + 0x0, &val32);
9034 tg3_read_mem(tp, NIC_SRAM_STATUS_BLK + 0x4, &val32_2);
9035 tg3_read_mem(tp, NIC_SRAM_STATUS_BLK + 0x8, &val32_3);
9036 tg3_read_mem(tp, NIC_SRAM_STATUS_BLK + 0xc, &val32_4);
9037 tg3_read_mem(tp, NIC_SRAM_STATUS_BLK + 0x10, &val32_5);
9038 printk("DEBUG: SRAM_STATUS_BLK[%08x:%08x:%08x:%08x:%08x]\n",
9039 val32, val32_2, val32_3, val32_4, val32_5);
9040
9041 /* SW status block */
Matt Carlson898a56f2009-08-28 14:02:40 +00009042 printk(KERN_DEBUG
9043 "Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
9044 sblk->status,
9045 sblk->status_tag,
9046 sblk->rx_jumbo_consumer,
9047 sblk->rx_consumer,
9048 sblk->rx_mini_consumer,
9049 sblk->idx[0].rx_producer,
9050 sblk->idx[0].tx_consumer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009051
9052 /* SW statistics block */
9053 printk("DEBUG: Host statistics block [%08x:%08x:%08x:%08x]\n",
9054 ((u32 *)tp->hw_stats)[0],
9055 ((u32 *)tp->hw_stats)[1],
9056 ((u32 *)tp->hw_stats)[2],
9057 ((u32 *)tp->hw_stats)[3]);
9058
9059 /* Mailboxes */
9060 printk("DEBUG: SNDHOST_PROD[%08x%08x] SNDNIC_PROD[%08x%08x]\n",
Michael Chan09ee9292005-08-09 20:17:00 -07009061 tr32_mailbox(MAILBOX_SNDHOST_PROD_IDX_0 + 0x0),
9062 tr32_mailbox(MAILBOX_SNDHOST_PROD_IDX_0 + 0x4),
9063 tr32_mailbox(MAILBOX_SNDNIC_PROD_IDX_0 + 0x0),
9064 tr32_mailbox(MAILBOX_SNDNIC_PROD_IDX_0 + 0x4));
Linus Torvalds1da177e2005-04-16 15:20:36 -07009065
9066 /* NIC side send descriptors. */
9067 for (i = 0; i < 6; i++) {
9068 unsigned long txd;
9069
9070 txd = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_TX_BUFFER_DESC
9071 + (i * sizeof(struct tg3_tx_buffer_desc));
9072 printk("DEBUG: NIC TXD(%d)[%08x:%08x:%08x:%08x]\n",
9073 i,
9074 readl(txd + 0x0), readl(txd + 0x4),
9075 readl(txd + 0x8), readl(txd + 0xc));
9076 }
9077
9078 /* NIC side RX descriptors. */
9079 for (i = 0; i < 6; i++) {
9080 unsigned long rxd;
9081
9082 rxd = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_RX_BUFFER_DESC
9083 + (i * sizeof(struct tg3_rx_buffer_desc));
9084 printk("DEBUG: NIC RXD_STD(%d)[0][%08x:%08x:%08x:%08x]\n",
9085 i,
9086 readl(rxd + 0x0), readl(rxd + 0x4),
9087 readl(rxd + 0x8), readl(rxd + 0xc));
9088 rxd += (4 * sizeof(u32));
9089 printk("DEBUG: NIC RXD_STD(%d)[1][%08x:%08x:%08x:%08x]\n",
9090 i,
9091 readl(rxd + 0x0), readl(rxd + 0x4),
9092 readl(rxd + 0x8), readl(rxd + 0xc));
9093 }
9094
9095 for (i = 0; i < 6; i++) {
9096 unsigned long rxd;
9097
9098 rxd = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_RX_JUMBO_BUFFER_DESC
9099 + (i * sizeof(struct tg3_rx_buffer_desc));
9100 printk("DEBUG: NIC RXD_JUMBO(%d)[0][%08x:%08x:%08x:%08x]\n",
9101 i,
9102 readl(rxd + 0x0), readl(rxd + 0x4),
9103 readl(rxd + 0x8), readl(rxd + 0xc));
9104 rxd += (4 * sizeof(u32));
9105 printk("DEBUG: NIC RXD_JUMBO(%d)[1][%08x:%08x:%08x:%08x]\n",
9106 i,
9107 readl(rxd + 0x0), readl(rxd + 0x4),
9108 readl(rxd + 0x8), readl(rxd + 0xc));
9109 }
9110}
9111#endif
9112
9113static struct net_device_stats *tg3_get_stats(struct net_device *);
9114static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *);
9115
9116static int tg3_close(struct net_device *dev)
9117{
Matt Carlson4f125f42009-09-01 12:55:02 +00009118 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009119 struct tg3 *tp = netdev_priv(dev);
9120
Matt Carlsonfed97812009-09-01 13:10:19 +00009121 tg3_napi_disable(tp);
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07009122 cancel_work_sync(&tp->reset_task);
Michael Chan7faa0062006-02-02 17:29:28 -08009123
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009124 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009125
9126 del_timer_sync(&tp->timer);
9127
Matt Carlson24bb4fb2009-10-05 17:55:29 +00009128 tg3_phy_stop(tp);
9129
David S. Millerf47c11e2005-06-24 20:18:35 -07009130 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009131#if 0
9132 tg3_dump_state(tp);
9133#endif
9134
9135 tg3_disable_ints(tp);
9136
Michael Chan944d9802005-05-29 14:57:48 -07009137 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009138 tg3_free_rings(tp);
Michael Chan5cf64b8a2007-05-05 12:11:21 -07009139 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009140
David S. Millerf47c11e2005-06-24 20:18:35 -07009141 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009142
Matt Carlson4f125f42009-09-01 12:55:02 +00009143 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9144 struct tg3_napi *tnapi = &tp->napi[i];
9145 free_irq(tnapi->irq_vec, tnapi);
9146 }
Matt Carlson07b01732009-08-28 14:01:15 +00009147
9148 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009149
9150 memcpy(&tp->net_stats_prev, tg3_get_stats(tp->dev),
9151 sizeof(tp->net_stats_prev));
9152 memcpy(&tp->estats_prev, tg3_get_estats(tp),
9153 sizeof(tp->estats_prev));
9154
9155 tg3_free_consistent(tp);
9156
Michael Chanbc1c7562006-03-20 17:48:03 -08009157 tg3_set_power_state(tp, PCI_D3hot);
9158
9159 netif_carrier_off(tp->dev);
9160
Linus Torvalds1da177e2005-04-16 15:20:36 -07009161 return 0;
9162}
9163
9164static inline unsigned long get_stat64(tg3_stat64_t *val)
9165{
9166 unsigned long ret;
9167
9168#if (BITS_PER_LONG == 32)
9169 ret = val->low;
9170#else
9171 ret = ((u64)val->high << 32) | ((u64)val->low);
9172#endif
9173 return ret;
9174}
9175
Stefan Buehler816f8b82008-08-15 14:10:54 -07009176static inline u64 get_estat64(tg3_stat64_t *val)
9177{
9178 return ((u64)val->high << 32) | ((u64)val->low);
9179}
9180
Linus Torvalds1da177e2005-04-16 15:20:36 -07009181static unsigned long calc_crc_errors(struct tg3 *tp)
9182{
9183 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9184
9185 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
9186 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
9187 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009188 u32 val;
9189
David S. Millerf47c11e2005-06-24 20:18:35 -07009190 spin_lock_bh(&tp->lock);
Michael Chan569a5df2007-02-13 12:18:15 -08009191 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
9192 tg3_writephy(tp, MII_TG3_TEST1,
9193 val | MII_TG3_TEST1_CRC_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009194 tg3_readphy(tp, 0x14, &val);
9195 } else
9196 val = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07009197 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009198
9199 tp->phy_crc_errors += val;
9200
9201 return tp->phy_crc_errors;
9202 }
9203
9204 return get_stat64(&hw_stats->rx_fcs_errors);
9205}
9206
9207#define ESTAT_ADD(member) \
9208 estats->member = old_estats->member + \
Stefan Buehler816f8b82008-08-15 14:10:54 -07009209 get_estat64(&hw_stats->member)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009210
9211static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *tp)
9212{
9213 struct tg3_ethtool_stats *estats = &tp->estats;
9214 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
9215 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9216
9217 if (!hw_stats)
9218 return old_estats;
9219
9220 ESTAT_ADD(rx_octets);
9221 ESTAT_ADD(rx_fragments);
9222 ESTAT_ADD(rx_ucast_packets);
9223 ESTAT_ADD(rx_mcast_packets);
9224 ESTAT_ADD(rx_bcast_packets);
9225 ESTAT_ADD(rx_fcs_errors);
9226 ESTAT_ADD(rx_align_errors);
9227 ESTAT_ADD(rx_xon_pause_rcvd);
9228 ESTAT_ADD(rx_xoff_pause_rcvd);
9229 ESTAT_ADD(rx_mac_ctrl_rcvd);
9230 ESTAT_ADD(rx_xoff_entered);
9231 ESTAT_ADD(rx_frame_too_long_errors);
9232 ESTAT_ADD(rx_jabbers);
9233 ESTAT_ADD(rx_undersize_packets);
9234 ESTAT_ADD(rx_in_length_errors);
9235 ESTAT_ADD(rx_out_length_errors);
9236 ESTAT_ADD(rx_64_or_less_octet_packets);
9237 ESTAT_ADD(rx_65_to_127_octet_packets);
9238 ESTAT_ADD(rx_128_to_255_octet_packets);
9239 ESTAT_ADD(rx_256_to_511_octet_packets);
9240 ESTAT_ADD(rx_512_to_1023_octet_packets);
9241 ESTAT_ADD(rx_1024_to_1522_octet_packets);
9242 ESTAT_ADD(rx_1523_to_2047_octet_packets);
9243 ESTAT_ADD(rx_2048_to_4095_octet_packets);
9244 ESTAT_ADD(rx_4096_to_8191_octet_packets);
9245 ESTAT_ADD(rx_8192_to_9022_octet_packets);
9246
9247 ESTAT_ADD(tx_octets);
9248 ESTAT_ADD(tx_collisions);
9249 ESTAT_ADD(tx_xon_sent);
9250 ESTAT_ADD(tx_xoff_sent);
9251 ESTAT_ADD(tx_flow_control);
9252 ESTAT_ADD(tx_mac_errors);
9253 ESTAT_ADD(tx_single_collisions);
9254 ESTAT_ADD(tx_mult_collisions);
9255 ESTAT_ADD(tx_deferred);
9256 ESTAT_ADD(tx_excessive_collisions);
9257 ESTAT_ADD(tx_late_collisions);
9258 ESTAT_ADD(tx_collide_2times);
9259 ESTAT_ADD(tx_collide_3times);
9260 ESTAT_ADD(tx_collide_4times);
9261 ESTAT_ADD(tx_collide_5times);
9262 ESTAT_ADD(tx_collide_6times);
9263 ESTAT_ADD(tx_collide_7times);
9264 ESTAT_ADD(tx_collide_8times);
9265 ESTAT_ADD(tx_collide_9times);
9266 ESTAT_ADD(tx_collide_10times);
9267 ESTAT_ADD(tx_collide_11times);
9268 ESTAT_ADD(tx_collide_12times);
9269 ESTAT_ADD(tx_collide_13times);
9270 ESTAT_ADD(tx_collide_14times);
9271 ESTAT_ADD(tx_collide_15times);
9272 ESTAT_ADD(tx_ucast_packets);
9273 ESTAT_ADD(tx_mcast_packets);
9274 ESTAT_ADD(tx_bcast_packets);
9275 ESTAT_ADD(tx_carrier_sense_errors);
9276 ESTAT_ADD(tx_discards);
9277 ESTAT_ADD(tx_errors);
9278
9279 ESTAT_ADD(dma_writeq_full);
9280 ESTAT_ADD(dma_write_prioq_full);
9281 ESTAT_ADD(rxbds_empty);
9282 ESTAT_ADD(rx_discards);
9283 ESTAT_ADD(rx_errors);
9284 ESTAT_ADD(rx_threshold_hit);
9285
9286 ESTAT_ADD(dma_readq_full);
9287 ESTAT_ADD(dma_read_prioq_full);
9288 ESTAT_ADD(tx_comp_queue_full);
9289
9290 ESTAT_ADD(ring_set_send_prod_index);
9291 ESTAT_ADD(ring_status_update);
9292 ESTAT_ADD(nic_irqs);
9293 ESTAT_ADD(nic_avoided_irqs);
9294 ESTAT_ADD(nic_tx_threshold_hit);
9295
9296 return estats;
9297}
9298
9299static struct net_device_stats *tg3_get_stats(struct net_device *dev)
9300{
9301 struct tg3 *tp = netdev_priv(dev);
9302 struct net_device_stats *stats = &tp->net_stats;
9303 struct net_device_stats *old_stats = &tp->net_stats_prev;
9304 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9305
9306 if (!hw_stats)
9307 return old_stats;
9308
9309 stats->rx_packets = old_stats->rx_packets +
9310 get_stat64(&hw_stats->rx_ucast_packets) +
9311 get_stat64(&hw_stats->rx_mcast_packets) +
9312 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009313
Linus Torvalds1da177e2005-04-16 15:20:36 -07009314 stats->tx_packets = old_stats->tx_packets +
9315 get_stat64(&hw_stats->tx_ucast_packets) +
9316 get_stat64(&hw_stats->tx_mcast_packets) +
9317 get_stat64(&hw_stats->tx_bcast_packets);
9318
9319 stats->rx_bytes = old_stats->rx_bytes +
9320 get_stat64(&hw_stats->rx_octets);
9321 stats->tx_bytes = old_stats->tx_bytes +
9322 get_stat64(&hw_stats->tx_octets);
9323
9324 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -07009325 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009326 stats->tx_errors = old_stats->tx_errors +
9327 get_stat64(&hw_stats->tx_errors) +
9328 get_stat64(&hw_stats->tx_mac_errors) +
9329 get_stat64(&hw_stats->tx_carrier_sense_errors) +
9330 get_stat64(&hw_stats->tx_discards);
9331
9332 stats->multicast = old_stats->multicast +
9333 get_stat64(&hw_stats->rx_mcast_packets);
9334 stats->collisions = old_stats->collisions +
9335 get_stat64(&hw_stats->tx_collisions);
9336
9337 stats->rx_length_errors = old_stats->rx_length_errors +
9338 get_stat64(&hw_stats->rx_frame_too_long_errors) +
9339 get_stat64(&hw_stats->rx_undersize_packets);
9340
9341 stats->rx_over_errors = old_stats->rx_over_errors +
9342 get_stat64(&hw_stats->rxbds_empty);
9343 stats->rx_frame_errors = old_stats->rx_frame_errors +
9344 get_stat64(&hw_stats->rx_align_errors);
9345 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
9346 get_stat64(&hw_stats->tx_discards);
9347 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
9348 get_stat64(&hw_stats->tx_carrier_sense_errors);
9349
9350 stats->rx_crc_errors = old_stats->rx_crc_errors +
9351 calc_crc_errors(tp);
9352
John W. Linville4f63b872005-09-12 14:43:18 -07009353 stats->rx_missed_errors = old_stats->rx_missed_errors +
9354 get_stat64(&hw_stats->rx_discards);
9355
Linus Torvalds1da177e2005-04-16 15:20:36 -07009356 return stats;
9357}
9358
9359static inline u32 calc_crc(unsigned char *buf, int len)
9360{
9361 u32 reg;
9362 u32 tmp;
9363 int j, k;
9364
9365 reg = 0xffffffff;
9366
9367 for (j = 0; j < len; j++) {
9368 reg ^= buf[j];
9369
9370 for (k = 0; k < 8; k++) {
9371 tmp = reg & 0x01;
9372
9373 reg >>= 1;
9374
9375 if (tmp) {
9376 reg ^= 0xedb88320;
9377 }
9378 }
9379 }
9380
9381 return ~reg;
9382}
9383
9384static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
9385{
9386 /* accept or reject all multicast frames */
9387 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
9388 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
9389 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
9390 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
9391}
9392
9393static void __tg3_set_rx_mode(struct net_device *dev)
9394{
9395 struct tg3 *tp = netdev_priv(dev);
9396 u32 rx_mode;
9397
9398 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
9399 RX_MODE_KEEP_VLAN_TAG);
9400
9401 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
9402 * flag clear.
9403 */
9404#if TG3_VLAN_TAG_USED
9405 if (!tp->vlgrp &&
9406 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
9407 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9408#else
9409 /* By definition, VLAN is disabled always in this
9410 * case.
9411 */
9412 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
9413 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9414#endif
9415
9416 if (dev->flags & IFF_PROMISC) {
9417 /* Promiscuous mode. */
9418 rx_mode |= RX_MODE_PROMISC;
9419 } else if (dev->flags & IFF_ALLMULTI) {
9420 /* Accept all multicast. */
9421 tg3_set_multi (tp, 1);
9422 } else if (dev->mc_count < 1) {
9423 /* Reject all multicast. */
9424 tg3_set_multi (tp, 0);
9425 } else {
9426 /* Accept one or more multicast(s). */
9427 struct dev_mc_list *mclist;
9428 unsigned int i;
9429 u32 mc_filter[4] = { 0, };
9430 u32 regidx;
9431 u32 bit;
9432 u32 crc;
9433
9434 for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
9435 i++, mclist = mclist->next) {
9436
9437 crc = calc_crc (mclist->dmi_addr, ETH_ALEN);
9438 bit = ~crc & 0x7f;
9439 regidx = (bit & 0x60) >> 5;
9440 bit &= 0x1f;
9441 mc_filter[regidx] |= (1 << bit);
9442 }
9443
9444 tw32(MAC_HASH_REG_0, mc_filter[0]);
9445 tw32(MAC_HASH_REG_1, mc_filter[1]);
9446 tw32(MAC_HASH_REG_2, mc_filter[2]);
9447 tw32(MAC_HASH_REG_3, mc_filter[3]);
9448 }
9449
9450 if (rx_mode != tp->rx_mode) {
9451 tp->rx_mode = rx_mode;
9452 tw32_f(MAC_RX_MODE, rx_mode);
9453 udelay(10);
9454 }
9455}
9456
9457static void tg3_set_rx_mode(struct net_device *dev)
9458{
9459 struct tg3 *tp = netdev_priv(dev);
9460
Michael Chane75f7c92006-03-20 21:33:26 -08009461 if (!netif_running(dev))
9462 return;
9463
David S. Millerf47c11e2005-06-24 20:18:35 -07009464 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009465 __tg3_set_rx_mode(dev);
David S. Millerf47c11e2005-06-24 20:18:35 -07009466 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009467}
9468
9469#define TG3_REGDUMP_LEN (32 * 1024)
9470
9471static int tg3_get_regs_len(struct net_device *dev)
9472{
9473 return TG3_REGDUMP_LEN;
9474}
9475
9476static void tg3_get_regs(struct net_device *dev,
9477 struct ethtool_regs *regs, void *_p)
9478{
9479 u32 *p = _p;
9480 struct tg3 *tp = netdev_priv(dev);
9481 u8 *orig_p = _p;
9482 int i;
9483
9484 regs->version = 0;
9485
9486 memset(p, 0, TG3_REGDUMP_LEN);
9487
Michael Chanbc1c7562006-03-20 17:48:03 -08009488 if (tp->link_config.phy_is_low_power)
9489 return;
9490
David S. Millerf47c11e2005-06-24 20:18:35 -07009491 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009492
9493#define __GET_REG32(reg) (*(p)++ = tr32(reg))
9494#define GET_REG32_LOOP(base,len) \
9495do { p = (u32 *)(orig_p + (base)); \
9496 for (i = 0; i < len; i += 4) \
9497 __GET_REG32((base) + i); \
9498} while (0)
9499#define GET_REG32_1(reg) \
9500do { p = (u32 *)(orig_p + (reg)); \
9501 __GET_REG32((reg)); \
9502} while (0)
9503
9504 GET_REG32_LOOP(TG3PCI_VENDOR, 0xb0);
9505 GET_REG32_LOOP(MAILBOX_INTERRUPT_0, 0x200);
9506 GET_REG32_LOOP(MAC_MODE, 0x4f0);
9507 GET_REG32_LOOP(SNDDATAI_MODE, 0xe0);
9508 GET_REG32_1(SNDDATAC_MODE);
9509 GET_REG32_LOOP(SNDBDS_MODE, 0x80);
9510 GET_REG32_LOOP(SNDBDI_MODE, 0x48);
9511 GET_REG32_1(SNDBDC_MODE);
9512 GET_REG32_LOOP(RCVLPC_MODE, 0x20);
9513 GET_REG32_LOOP(RCVLPC_SELLST_BASE, 0x15c);
9514 GET_REG32_LOOP(RCVDBDI_MODE, 0x0c);
9515 GET_REG32_LOOP(RCVDBDI_JUMBO_BD, 0x3c);
9516 GET_REG32_LOOP(RCVDBDI_BD_PROD_IDX_0, 0x44);
9517 GET_REG32_1(RCVDCC_MODE);
9518 GET_REG32_LOOP(RCVBDI_MODE, 0x20);
9519 GET_REG32_LOOP(RCVCC_MODE, 0x14);
9520 GET_REG32_LOOP(RCVLSC_MODE, 0x08);
9521 GET_REG32_1(MBFREE_MODE);
9522 GET_REG32_LOOP(HOSTCC_MODE, 0x100);
9523 GET_REG32_LOOP(MEMARB_MODE, 0x10);
9524 GET_REG32_LOOP(BUFMGR_MODE, 0x58);
9525 GET_REG32_LOOP(RDMAC_MODE, 0x08);
9526 GET_REG32_LOOP(WDMAC_MODE, 0x08);
Chris Elmquist091465d2005-12-20 13:25:19 -08009527 GET_REG32_1(RX_CPU_MODE);
9528 GET_REG32_1(RX_CPU_STATE);
9529 GET_REG32_1(RX_CPU_PGMCTR);
9530 GET_REG32_1(RX_CPU_HWBKPT);
9531 GET_REG32_1(TX_CPU_MODE);
9532 GET_REG32_1(TX_CPU_STATE);
9533 GET_REG32_1(TX_CPU_PGMCTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009534 GET_REG32_LOOP(GRCMBOX_INTERRUPT_0, 0x110);
9535 GET_REG32_LOOP(FTQ_RESET, 0x120);
9536 GET_REG32_LOOP(MSGINT_MODE, 0x0c);
9537 GET_REG32_1(DMAC_MODE);
9538 GET_REG32_LOOP(GRC_MODE, 0x4c);
9539 if (tp->tg3_flags & TG3_FLAG_NVRAM)
9540 GET_REG32_LOOP(NVRAM_CMD, 0x24);
9541
9542#undef __GET_REG32
9543#undef GET_REG32_LOOP
9544#undef GET_REG32_1
9545
David S. Millerf47c11e2005-06-24 20:18:35 -07009546 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009547}
9548
9549static int tg3_get_eeprom_len(struct net_device *dev)
9550{
9551 struct tg3 *tp = netdev_priv(dev);
9552
9553 return tp->nvram_size;
9554}
9555
Linus Torvalds1da177e2005-04-16 15:20:36 -07009556static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9557{
9558 struct tg3 *tp = netdev_priv(dev);
9559 int ret;
9560 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -08009561 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009562 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009563
Matt Carlsondf259d82009-04-20 06:57:14 +00009564 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM)
9565 return -EINVAL;
9566
Michael Chanbc1c7562006-03-20 17:48:03 -08009567 if (tp->link_config.phy_is_low_power)
9568 return -EAGAIN;
9569
Linus Torvalds1da177e2005-04-16 15:20:36 -07009570 offset = eeprom->offset;
9571 len = eeprom->len;
9572 eeprom->len = 0;
9573
9574 eeprom->magic = TG3_EEPROM_MAGIC;
9575
9576 if (offset & 3) {
9577 /* adjustments to start on required 4 byte boundary */
9578 b_offset = offset & 3;
9579 b_count = 4 - b_offset;
9580 if (b_count > len) {
9581 /* i.e. offset=1 len=2 */
9582 b_count = len;
9583 }
Matt Carlsona9dc5292009-02-25 14:25:30 +00009584 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009585 if (ret)
9586 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009587 memcpy(data, ((char*)&val) + b_offset, b_count);
9588 len -= b_count;
9589 offset += b_count;
9590 eeprom->len += b_count;
9591 }
9592
9593 /* read bytes upto the last 4 byte boundary */
9594 pd = &data[eeprom->len];
9595 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +00009596 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009597 if (ret) {
9598 eeprom->len += i;
9599 return ret;
9600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009601 memcpy(pd + i, &val, 4);
9602 }
9603 eeprom->len += i;
9604
9605 if (len & 3) {
9606 /* read last bytes not ending on 4 byte boundary */
9607 pd = &data[eeprom->len];
9608 b_count = len & 3;
9609 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009610 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009611 if (ret)
9612 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009613 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009614 eeprom->len += b_count;
9615 }
9616 return 0;
9617}
9618
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009619static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009620
9621static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9622{
9623 struct tg3 *tp = netdev_priv(dev);
9624 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009625 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009626 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009627 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009628
Michael Chanbc1c7562006-03-20 17:48:03 -08009629 if (tp->link_config.phy_is_low_power)
9630 return -EAGAIN;
9631
Matt Carlsondf259d82009-04-20 06:57:14 +00009632 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
9633 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009634 return -EINVAL;
9635
9636 offset = eeprom->offset;
9637 len = eeprom->len;
9638
9639 if ((b_offset = (offset & 3))) {
9640 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +00009641 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009642 if (ret)
9643 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009644 len += b_offset;
9645 offset &= ~3;
Michael Chan1c8594b42005-04-21 17:12:46 -07009646 if (len < 4)
9647 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009648 }
9649
9650 odd_len = 0;
Michael Chan1c8594b42005-04-21 17:12:46 -07009651 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009652 /* adjustments to end on required 4 byte boundary */
9653 odd_len = 1;
9654 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009655 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009656 if (ret)
9657 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009658 }
9659
9660 buf = data;
9661 if (b_offset || odd_len) {
9662 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009663 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009664 return -ENOMEM;
9665 if (b_offset)
9666 memcpy(buf, &start, 4);
9667 if (odd_len)
9668 memcpy(buf+len-4, &end, 4);
9669 memcpy(buf + b_offset, data, eeprom->len);
9670 }
9671
9672 ret = tg3_nvram_write_block(tp, offset, len, buf);
9673
9674 if (buf != data)
9675 kfree(buf);
9676
9677 return ret;
9678}
9679
9680static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9681{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009682 struct tg3 *tp = netdev_priv(dev);
9683
9684 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009685 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009686 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
9687 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009688 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
9689 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009690 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009691
Linus Torvalds1da177e2005-04-16 15:20:36 -07009692 cmd->supported = (SUPPORTED_Autoneg);
9693
9694 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY))
9695 cmd->supported |= (SUPPORTED_1000baseT_Half |
9696 SUPPORTED_1000baseT_Full);
9697
Karsten Keilef348142006-05-12 12:49:08 -07009698 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009699 cmd->supported |= (SUPPORTED_100baseT_Half |
9700 SUPPORTED_100baseT_Full |
9701 SUPPORTED_10baseT_Half |
9702 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -08009703 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -07009704 cmd->port = PORT_TP;
9705 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009706 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -07009707 cmd->port = PORT_FIBRE;
9708 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009709
Linus Torvalds1da177e2005-04-16 15:20:36 -07009710 cmd->advertising = tp->link_config.advertising;
9711 if (netif_running(dev)) {
9712 cmd->speed = tp->link_config.active_speed;
9713 cmd->duplex = tp->link_config.active_duplex;
9714 }
Matt Carlson882e9792009-09-01 13:21:36 +00009715 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +00009716 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009717 cmd->autoneg = tp->link_config.autoneg;
9718 cmd->maxtxpkt = 0;
9719 cmd->maxrxpkt = 0;
9720 return 0;
9721}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009722
Linus Torvalds1da177e2005-04-16 15:20:36 -07009723static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9724{
9725 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009726
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009727 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009728 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009729 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
9730 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009731 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
9732 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009733 }
9734
Matt Carlson7e5856b2009-02-25 14:23:01 +00009735 if (cmd->autoneg != AUTONEG_ENABLE &&
9736 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -07009737 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +00009738
9739 if (cmd->autoneg == AUTONEG_DISABLE &&
9740 cmd->duplex != DUPLEX_FULL &&
9741 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -07009742 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009743
Matt Carlson7e5856b2009-02-25 14:23:01 +00009744 if (cmd->autoneg == AUTONEG_ENABLE) {
9745 u32 mask = ADVERTISED_Autoneg |
9746 ADVERTISED_Pause |
9747 ADVERTISED_Asym_Pause;
9748
9749 if (!(tp->tg3_flags2 & TG3_FLAG_10_100_ONLY))
9750 mask |= ADVERTISED_1000baseT_Half |
9751 ADVERTISED_1000baseT_Full;
9752
9753 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES))
9754 mask |= ADVERTISED_100baseT_Half |
9755 ADVERTISED_100baseT_Full |
9756 ADVERTISED_10baseT_Half |
9757 ADVERTISED_10baseT_Full |
9758 ADVERTISED_TP;
9759 else
9760 mask |= ADVERTISED_FIBRE;
9761
9762 if (cmd->advertising & ~mask)
9763 return -EINVAL;
9764
9765 mask &= (ADVERTISED_1000baseT_Half |
9766 ADVERTISED_1000baseT_Full |
9767 ADVERTISED_100baseT_Half |
9768 ADVERTISED_100baseT_Full |
9769 ADVERTISED_10baseT_Half |
9770 ADVERTISED_10baseT_Full);
9771
9772 cmd->advertising &= mask;
9773 } else {
9774 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES) {
9775 if (cmd->speed != SPEED_1000)
9776 return -EINVAL;
9777
9778 if (cmd->duplex != DUPLEX_FULL)
9779 return -EINVAL;
9780 } else {
9781 if (cmd->speed != SPEED_100 &&
9782 cmd->speed != SPEED_10)
9783 return -EINVAL;
9784 }
9785 }
9786
David S. Millerf47c11e2005-06-24 20:18:35 -07009787 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009788
9789 tp->link_config.autoneg = cmd->autoneg;
9790 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -07009791 tp->link_config.advertising = (cmd->advertising |
9792 ADVERTISED_Autoneg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009793 tp->link_config.speed = SPEED_INVALID;
9794 tp->link_config.duplex = DUPLEX_INVALID;
9795 } else {
9796 tp->link_config.advertising = 0;
9797 tp->link_config.speed = cmd->speed;
9798 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009799 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009800
Michael Chan24fcad62006-12-17 17:06:46 -08009801 tp->link_config.orig_speed = tp->link_config.speed;
9802 tp->link_config.orig_duplex = tp->link_config.duplex;
9803 tp->link_config.orig_autoneg = tp->link_config.autoneg;
9804
Linus Torvalds1da177e2005-04-16 15:20:36 -07009805 if (netif_running(dev))
9806 tg3_setup_phy(tp, 1);
9807
David S. Millerf47c11e2005-06-24 20:18:35 -07009808 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009809
Linus Torvalds1da177e2005-04-16 15:20:36 -07009810 return 0;
9811}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009812
Linus Torvalds1da177e2005-04-16 15:20:36 -07009813static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
9814{
9815 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009816
Linus Torvalds1da177e2005-04-16 15:20:36 -07009817 strcpy(info->driver, DRV_MODULE_NAME);
9818 strcpy(info->version, DRV_MODULE_VERSION);
Michael Chanc4e65752006-03-20 22:29:32 -08009819 strcpy(info->fw_version, tp->fw_ver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009820 strcpy(info->bus_info, pci_name(tp->pdev));
9821}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009822
Linus Torvalds1da177e2005-04-16 15:20:36 -07009823static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
9824{
9825 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009826
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009827 if ((tp->tg3_flags & TG3_FLAG_WOL_CAP) &&
9828 device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -07009829 wol->supported = WAKE_MAGIC;
9830 else
9831 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009832 wol->wolopts = 0;
Matt Carlson05ac4cb2008-11-03 16:53:46 -08009833 if ((tp->tg3_flags & TG3_FLAG_WOL_ENABLE) &&
9834 device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009835 wol->wolopts = WAKE_MAGIC;
9836 memset(&wol->sopass, 0, sizeof(wol->sopass));
9837}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009838
Linus Torvalds1da177e2005-04-16 15:20:36 -07009839static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
9840{
9841 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009842 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009843
Linus Torvalds1da177e2005-04-16 15:20:36 -07009844 if (wol->wolopts & ~WAKE_MAGIC)
9845 return -EINVAL;
9846 if ((wol->wolopts & WAKE_MAGIC) &&
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009847 !((tp->tg3_flags & TG3_FLAG_WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009848 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009849
David S. Millerf47c11e2005-06-24 20:18:35 -07009850 spin_lock_bh(&tp->lock);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009851 if (wol->wolopts & WAKE_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009852 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009853 device_set_wakeup_enable(dp, true);
9854 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009855 tp->tg3_flags &= ~TG3_FLAG_WOL_ENABLE;
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009856 device_set_wakeup_enable(dp, false);
9857 }
David S. Millerf47c11e2005-06-24 20:18:35 -07009858 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009859
Linus Torvalds1da177e2005-04-16 15:20:36 -07009860 return 0;
9861}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009862
Linus Torvalds1da177e2005-04-16 15:20:36 -07009863static u32 tg3_get_msglevel(struct net_device *dev)
9864{
9865 struct tg3 *tp = netdev_priv(dev);
9866 return tp->msg_enable;
9867}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009868
Linus Torvalds1da177e2005-04-16 15:20:36 -07009869static void tg3_set_msglevel(struct net_device *dev, u32 value)
9870{
9871 struct tg3 *tp = netdev_priv(dev);
9872 tp->msg_enable = value;
9873}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009874
Linus Torvalds1da177e2005-04-16 15:20:36 -07009875static int tg3_set_tso(struct net_device *dev, u32 value)
9876{
9877 struct tg3 *tp = netdev_priv(dev);
9878
9879 if (!(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
9880 if (value)
9881 return -EINVAL;
9882 return 0;
9883 }
Matt Carlson027455a2008-12-21 20:19:30 -08009884 if ((dev->features & NETIF_F_IPV6_CSUM) &&
Matt Carlsone849cdc2009-11-13 13:03:38 +00009885 ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_2) ||
9886 (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3))) {
Matt Carlson9936bcf2007-10-10 18:03:07 -07009887 if (value) {
Michael Chanb0026622006-07-03 19:42:14 -07009888 dev->features |= NETIF_F_TSO6;
Matt Carlsone849cdc2009-11-13 13:03:38 +00009889 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
9890 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -07009891 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
9892 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Matt Carlson321d32a2008-11-21 17:22:19 -08009893 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsone849cdc2009-11-13 13:03:38 +00009894 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson9936bcf2007-10-10 18:03:07 -07009895 dev->features |= NETIF_F_TSO_ECN;
9896 } else
9897 dev->features &= ~(NETIF_F_TSO6 | NETIF_F_TSO_ECN);
Michael Chanb0026622006-07-03 19:42:14 -07009898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009899 return ethtool_op_set_tso(dev, value);
9900}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009901
Linus Torvalds1da177e2005-04-16 15:20:36 -07009902static int tg3_nway_reset(struct net_device *dev)
9903{
9904 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009905 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009906
Linus Torvalds1da177e2005-04-16 15:20:36 -07009907 if (!netif_running(dev))
9908 return -EAGAIN;
9909
Michael Chanc94e3942005-09-27 12:12:42 -07009910 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
9911 return -EINVAL;
9912
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009913 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
9914 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
9915 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009916 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009917 } else {
9918 u32 bmcr;
9919
9920 spin_lock_bh(&tp->lock);
9921 r = -EINVAL;
9922 tg3_readphy(tp, MII_BMCR, &bmcr);
9923 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
9924 ((bmcr & BMCR_ANENABLE) ||
9925 (tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT))) {
9926 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
9927 BMCR_ANENABLE);
9928 r = 0;
9929 }
9930 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009931 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009932
Linus Torvalds1da177e2005-04-16 15:20:36 -07009933 return r;
9934}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009935
Linus Torvalds1da177e2005-04-16 15:20:36 -07009936static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
9937{
9938 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009939
Linus Torvalds1da177e2005-04-16 15:20:36 -07009940 ering->rx_max_pending = TG3_RX_RING_SIZE - 1;
9941 ering->rx_mini_max_pending = 0;
Michael Chan4f81c322006-03-20 21:33:42 -08009942 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE)
9943 ering->rx_jumbo_max_pending = TG3_RX_JUMBO_RING_SIZE - 1;
9944 else
9945 ering->rx_jumbo_max_pending = 0;
9946
9947 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009948
9949 ering->rx_pending = tp->rx_pending;
9950 ering->rx_mini_pending = 0;
Michael Chan4f81c322006-03-20 21:33:42 -08009951 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE)
9952 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
9953 else
9954 ering->rx_jumbo_pending = 0;
9955
Matt Carlsonf3f3f272009-08-28 14:03:21 +00009956 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009957}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009958
Linus Torvalds1da177e2005-04-16 15:20:36 -07009959static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
9960{
9961 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +00009962 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009963
Linus Torvalds1da177e2005-04-16 15:20:36 -07009964 if ((ering->rx_pending > TG3_RX_RING_SIZE - 1) ||
9965 (ering->rx_jumbo_pending > TG3_RX_JUMBO_RING_SIZE - 1) ||
Michael Chanbc3a9252006-10-18 20:55:18 -07009966 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
9967 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Michael Chan7f62ad52007-02-20 23:25:40 -08009968 ((tp->tg3_flags2 & TG3_FLG2_TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -07009969 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009970 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009971
Michael Chanbbe832c2005-06-24 20:20:04 -07009972 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009973 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009974 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -07009975 irq_sync = 1;
9976 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009977
Michael Chanbbe832c2005-06-24 20:20:04 -07009978 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009979
Linus Torvalds1da177e2005-04-16 15:20:36 -07009980 tp->rx_pending = ering->rx_pending;
9981
9982 if ((tp->tg3_flags2 & TG3_FLG2_MAX_RXPEND_64) &&
9983 tp->rx_pending > 63)
9984 tp->rx_pending = 63;
9985 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +00009986
9987 for (i = 0; i < TG3_IRQ_MAX_VECS; i++)
9988 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009989
9990 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -07009991 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -07009992 err = tg3_restart_hw(tp, 1);
9993 if (!err)
9994 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009995 }
9996
David S. Millerf47c11e2005-06-24 20:18:35 -07009997 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009998
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009999 if (irq_sync && !err)
10000 tg3_phy_start(tp);
10001
Michael Chanb9ec6c12006-07-25 16:37:27 -070010002 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010003}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010004
Linus Torvalds1da177e2005-04-16 15:20:36 -070010005static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10006{
10007 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010008
Linus Torvalds1da177e2005-04-16 15:20:36 -070010009 epause->autoneg = (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG) != 0;
Matt Carlson8d018622007-12-20 20:05:44 -080010010
Steve Glendinninge18ce342008-12-16 02:00:00 -080010011 if (tp->link_config.active_flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080010012 epause->rx_pause = 1;
10013 else
10014 epause->rx_pause = 0;
10015
Steve Glendinninge18ce342008-12-16 02:00:00 -080010016 if (tp->link_config.active_flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080010017 epause->tx_pause = 1;
10018 else
10019 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010020}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010021
Linus Torvalds1da177e2005-04-16 15:20:36 -070010022static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10023{
10024 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010025 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010026
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010027 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
10028 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
10029 return -EAGAIN;
10030
10031 if (epause->autoneg) {
10032 u32 newadv;
10033 struct phy_device *phydev;
10034
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010035 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010036
10037 if (epause->rx_pause) {
10038 if (epause->tx_pause)
10039 newadv = ADVERTISED_Pause;
10040 else
10041 newadv = ADVERTISED_Pause |
10042 ADVERTISED_Asym_Pause;
10043 } else if (epause->tx_pause) {
10044 newadv = ADVERTISED_Asym_Pause;
10045 } else
10046 newadv = 0;
10047
10048 if (tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED) {
10049 u32 oldadv = phydev->advertising &
10050 (ADVERTISED_Pause |
10051 ADVERTISED_Asym_Pause);
10052 if (oldadv != newadv) {
10053 phydev->advertising &=
10054 ~(ADVERTISED_Pause |
10055 ADVERTISED_Asym_Pause);
10056 phydev->advertising |= newadv;
10057 err = phy_start_aneg(phydev);
10058 }
10059 } else {
10060 tp->link_config.advertising &=
10061 ~(ADVERTISED_Pause |
10062 ADVERTISED_Asym_Pause);
10063 tp->link_config.advertising |= newadv;
10064 }
10065 } else {
10066 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010067 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010068 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010069 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010070
10071 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010072 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010073 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010074 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010075
10076 if (netif_running(dev))
10077 tg3_setup_flow_control(tp, 0, 0);
10078 }
10079 } else {
10080 int irq_sync = 0;
10081
10082 if (netif_running(dev)) {
10083 tg3_netif_stop(tp);
10084 irq_sync = 1;
10085 }
10086
10087 tg3_full_lock(tp, irq_sync);
10088
10089 if (epause->autoneg)
10090 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
10091 else
10092 tp->tg3_flags &= ~TG3_FLAG_PAUSE_AUTONEG;
10093 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010094 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010095 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010096 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010097 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010098 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010099 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010100 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010101
10102 if (netif_running(dev)) {
10103 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10104 err = tg3_restart_hw(tp, 1);
10105 if (!err)
10106 tg3_netif_start(tp);
10107 }
10108
10109 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010111
Michael Chanb9ec6c12006-07-25 16:37:27 -070010112 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010113}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010114
Linus Torvalds1da177e2005-04-16 15:20:36 -070010115static u32 tg3_get_rx_csum(struct net_device *dev)
10116{
10117 struct tg3 *tp = netdev_priv(dev);
10118 return (tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) != 0;
10119}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010120
Linus Torvalds1da177e2005-04-16 15:20:36 -070010121static int tg3_set_rx_csum(struct net_device *dev, u32 data)
10122{
10123 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010124
Linus Torvalds1da177e2005-04-16 15:20:36 -070010125 if (tp->tg3_flags & TG3_FLAG_BROKEN_CHECKSUMS) {
10126 if (data != 0)
10127 return -EINVAL;
10128 return 0;
10129 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010130
David S. Millerf47c11e2005-06-24 20:18:35 -070010131 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010132 if (data)
10133 tp->tg3_flags |= TG3_FLAG_RX_CHECKSUMS;
10134 else
10135 tp->tg3_flags &= ~TG3_FLAG_RX_CHECKSUMS;
David S. Millerf47c11e2005-06-24 20:18:35 -070010136 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010137
Linus Torvalds1da177e2005-04-16 15:20:36 -070010138 return 0;
10139}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010140
Linus Torvalds1da177e2005-04-16 15:20:36 -070010141static int tg3_set_tx_csum(struct net_device *dev, u32 data)
10142{
10143 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010144
Linus Torvalds1da177e2005-04-16 15:20:36 -070010145 if (tp->tg3_flags & TG3_FLAG_BROKEN_CHECKSUMS) {
10146 if (data != 0)
10147 return -EINVAL;
10148 return 0;
10149 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010150
Matt Carlson321d32a2008-11-21 17:22:19 -080010151 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Michael Chan6460d942007-07-14 19:07:52 -070010152 ethtool_op_set_tx_ipv6_csum(dev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010153 else
Michael Chan9c27dbd2006-03-20 22:28:27 -080010154 ethtool_op_set_tx_csum(dev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010155
10156 return 0;
10157}
10158
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010159static int tg3_get_sset_count (struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010160{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010161 switch (sset) {
10162 case ETH_SS_TEST:
10163 return TG3_NUM_TEST;
10164 case ETH_SS_STATS:
10165 return TG3_NUM_STATS;
10166 default:
10167 return -EOPNOTSUPP;
10168 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070010169}
10170
Linus Torvalds1da177e2005-04-16 15:20:36 -070010171static void tg3_get_strings (struct net_device *dev, u32 stringset, u8 *buf)
10172{
10173 switch (stringset) {
10174 case ETH_SS_STATS:
10175 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
10176 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070010177 case ETH_SS_TEST:
10178 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
10179 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010180 default:
10181 WARN_ON(1); /* we need a WARN() */
10182 break;
10183 }
10184}
10185
Michael Chan4009a932005-09-05 17:52:54 -070010186static int tg3_phys_id(struct net_device *dev, u32 data)
10187{
10188 struct tg3 *tp = netdev_priv(dev);
10189 int i;
10190
10191 if (!netif_running(tp->dev))
10192 return -EAGAIN;
10193
10194 if (data == 0)
Stephen Hemminger759afc32008-02-23 19:51:59 -080010195 data = UINT_MAX / 2;
Michael Chan4009a932005-09-05 17:52:54 -070010196
10197 for (i = 0; i < (data * 2); i++) {
10198 if ((i % 2) == 0)
10199 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10200 LED_CTRL_1000MBPS_ON |
10201 LED_CTRL_100MBPS_ON |
10202 LED_CTRL_10MBPS_ON |
10203 LED_CTRL_TRAFFIC_OVERRIDE |
10204 LED_CTRL_TRAFFIC_BLINK |
10205 LED_CTRL_TRAFFIC_LED);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010206
Michael Chan4009a932005-09-05 17:52:54 -070010207 else
10208 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10209 LED_CTRL_TRAFFIC_OVERRIDE);
10210
10211 if (msleep_interruptible(500))
10212 break;
10213 }
10214 tw32(MAC_LED_CTRL, tp->led_ctrl);
10215 return 0;
10216}
10217
Linus Torvalds1da177e2005-04-16 15:20:36 -070010218static void tg3_get_ethtool_stats (struct net_device *dev,
10219 struct ethtool_stats *estats, u64 *tmp_stats)
10220{
10221 struct tg3 *tp = netdev_priv(dev);
10222 memcpy(tmp_stats, tg3_get_estats(tp), sizeof(tp->estats));
10223}
10224
Michael Chan566f86a2005-05-29 14:56:58 -070010225#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080010226#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
10227#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
10228#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Michael Chanb16250e2006-09-27 16:10:14 -070010229#define NVRAM_SELFBOOT_HW_SIZE 0x20
10230#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070010231
10232static int tg3_test_nvram(struct tg3 *tp)
10233{
Al Virob9fc7dc2007-12-17 22:59:57 -080010234 u32 csum, magic;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010235 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010236 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070010237
Matt Carlsondf259d82009-04-20 06:57:14 +000010238 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM)
10239 return 0;
10240
Matt Carlsone4f34112009-02-25 14:25:00 +000010241 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080010242 return -EIO;
10243
Michael Chan1b277772006-03-20 22:27:48 -080010244 if (magic == TG3_EEPROM_MAGIC)
10245 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070010246 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080010247 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
10248 TG3_EEPROM_SB_FORMAT_1) {
10249 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
10250 case TG3_EEPROM_SB_REVISION_0:
10251 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
10252 break;
10253 case TG3_EEPROM_SB_REVISION_2:
10254 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
10255 break;
10256 case TG3_EEPROM_SB_REVISION_3:
10257 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
10258 break;
10259 default:
10260 return 0;
10261 }
10262 } else
Michael Chan1b277772006-03-20 22:27:48 -080010263 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070010264 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
10265 size = NVRAM_SELFBOOT_HW_SIZE;
10266 else
Michael Chan1b277772006-03-20 22:27:48 -080010267 return -EIO;
10268
10269 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070010270 if (buf == NULL)
10271 return -ENOMEM;
10272
Michael Chan1b277772006-03-20 22:27:48 -080010273 err = -EIO;
10274 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010275 err = tg3_nvram_read_be32(tp, i, &buf[j]);
10276 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070010277 break;
Michael Chan566f86a2005-05-29 14:56:58 -070010278 }
Michael Chan1b277772006-03-20 22:27:48 -080010279 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070010280 goto out;
10281
Michael Chan1b277772006-03-20 22:27:48 -080010282 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010283 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080010284 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010285 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080010286 u8 *buf8 = (u8 *) buf, csum8 = 0;
10287
Al Virob9fc7dc2007-12-17 22:59:57 -080010288 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080010289 TG3_EEPROM_SB_REVISION_2) {
10290 /* For rev 2, the csum doesn't include the MBA. */
10291 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
10292 csum8 += buf8[i];
10293 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
10294 csum8 += buf8[i];
10295 } else {
10296 for (i = 0; i < size; i++)
10297 csum8 += buf8[i];
10298 }
Michael Chan1b277772006-03-20 22:27:48 -080010299
Adrian Bunkad96b482006-04-05 22:21:04 -070010300 if (csum8 == 0) {
10301 err = 0;
10302 goto out;
10303 }
10304
10305 err = -EIO;
10306 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080010307 }
Michael Chan566f86a2005-05-29 14:56:58 -070010308
Al Virob9fc7dc2007-12-17 22:59:57 -080010309 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010310 TG3_EEPROM_MAGIC_HW) {
10311 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000010312 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070010313 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070010314
10315 /* Separate the parity bits and the data bytes. */
10316 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
10317 if ((i == 0) || (i == 8)) {
10318 int l;
10319 u8 msk;
10320
10321 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
10322 parity[k++] = buf8[i] & msk;
10323 i++;
10324 }
10325 else if (i == 16) {
10326 int l;
10327 u8 msk;
10328
10329 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
10330 parity[k++] = buf8[i] & msk;
10331 i++;
10332
10333 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
10334 parity[k++] = buf8[i] & msk;
10335 i++;
10336 }
10337 data[j++] = buf8[i];
10338 }
10339
10340 err = -EIO;
10341 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
10342 u8 hw8 = hweight8(data[i]);
10343
10344 if ((hw8 & 0x1) && parity[i])
10345 goto out;
10346 else if (!(hw8 & 0x1) && !parity[i])
10347 goto out;
10348 }
10349 err = 0;
10350 goto out;
10351 }
10352
Michael Chan566f86a2005-05-29 14:56:58 -070010353 /* Bootstrap checksum at offset 0x10 */
10354 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlsona9dc5292009-02-25 14:25:30 +000010355 if (csum != be32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070010356 goto out;
10357
10358 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
10359 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlsona9dc5292009-02-25 14:25:30 +000010360 if (csum != be32_to_cpu(buf[0xfc/4]))
10361 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070010362
10363 err = 0;
10364
10365out:
10366 kfree(buf);
10367 return err;
10368}
10369
Michael Chanca430072005-05-29 14:57:23 -070010370#define TG3_SERDES_TIMEOUT_SEC 2
10371#define TG3_COPPER_TIMEOUT_SEC 6
10372
10373static int tg3_test_link(struct tg3 *tp)
10374{
10375 int i, max;
10376
10377 if (!netif_running(tp->dev))
10378 return -ENODEV;
10379
Michael Chan4c987482005-09-05 17:52:38 -070010380 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070010381 max = TG3_SERDES_TIMEOUT_SEC;
10382 else
10383 max = TG3_COPPER_TIMEOUT_SEC;
10384
10385 for (i = 0; i < max; i++) {
10386 if (netif_carrier_ok(tp->dev))
10387 return 0;
10388
10389 if (msleep_interruptible(1000))
10390 break;
10391 }
10392
10393 return -EIO;
10394}
10395
Michael Chana71116d2005-05-29 14:58:11 -070010396/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080010397static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070010398{
Michael Chanb16250e2006-09-27 16:10:14 -070010399 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070010400 u32 offset, read_mask, write_mask, val, save_val, read_val;
10401 static struct {
10402 u16 offset;
10403 u16 flags;
10404#define TG3_FL_5705 0x1
10405#define TG3_FL_NOT_5705 0x2
10406#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070010407#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070010408 u32 read_mask;
10409 u32 write_mask;
10410 } reg_tbl[] = {
10411 /* MAC Control Registers */
10412 { MAC_MODE, TG3_FL_NOT_5705,
10413 0x00000000, 0x00ef6f8c },
10414 { MAC_MODE, TG3_FL_5705,
10415 0x00000000, 0x01ef6b8c },
10416 { MAC_STATUS, TG3_FL_NOT_5705,
10417 0x03800107, 0x00000000 },
10418 { MAC_STATUS, TG3_FL_5705,
10419 0x03800100, 0x00000000 },
10420 { MAC_ADDR_0_HIGH, 0x0000,
10421 0x00000000, 0x0000ffff },
10422 { MAC_ADDR_0_LOW, 0x0000,
10423 0x00000000, 0xffffffff },
10424 { MAC_RX_MTU_SIZE, 0x0000,
10425 0x00000000, 0x0000ffff },
10426 { MAC_TX_MODE, 0x0000,
10427 0x00000000, 0x00000070 },
10428 { MAC_TX_LENGTHS, 0x0000,
10429 0x00000000, 0x00003fff },
10430 { MAC_RX_MODE, TG3_FL_NOT_5705,
10431 0x00000000, 0x000007fc },
10432 { MAC_RX_MODE, TG3_FL_5705,
10433 0x00000000, 0x000007dc },
10434 { MAC_HASH_REG_0, 0x0000,
10435 0x00000000, 0xffffffff },
10436 { MAC_HASH_REG_1, 0x0000,
10437 0x00000000, 0xffffffff },
10438 { MAC_HASH_REG_2, 0x0000,
10439 0x00000000, 0xffffffff },
10440 { MAC_HASH_REG_3, 0x0000,
10441 0x00000000, 0xffffffff },
10442
10443 /* Receive Data and Receive BD Initiator Control Registers. */
10444 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
10445 0x00000000, 0xffffffff },
10446 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
10447 0x00000000, 0xffffffff },
10448 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
10449 0x00000000, 0x00000003 },
10450 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
10451 0x00000000, 0xffffffff },
10452 { RCVDBDI_STD_BD+0, 0x0000,
10453 0x00000000, 0xffffffff },
10454 { RCVDBDI_STD_BD+4, 0x0000,
10455 0x00000000, 0xffffffff },
10456 { RCVDBDI_STD_BD+8, 0x0000,
10457 0x00000000, 0xffff0002 },
10458 { RCVDBDI_STD_BD+0xc, 0x0000,
10459 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010460
Michael Chana71116d2005-05-29 14:58:11 -070010461 /* Receive BD Initiator Control Registers. */
10462 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
10463 0x00000000, 0xffffffff },
10464 { RCVBDI_STD_THRESH, TG3_FL_5705,
10465 0x00000000, 0x000003ff },
10466 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
10467 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010468
Michael Chana71116d2005-05-29 14:58:11 -070010469 /* Host Coalescing Control Registers. */
10470 { HOSTCC_MODE, TG3_FL_NOT_5705,
10471 0x00000000, 0x00000004 },
10472 { HOSTCC_MODE, TG3_FL_5705,
10473 0x00000000, 0x000000f6 },
10474 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
10475 0x00000000, 0xffffffff },
10476 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
10477 0x00000000, 0x000003ff },
10478 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
10479 0x00000000, 0xffffffff },
10480 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
10481 0x00000000, 0x000003ff },
10482 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
10483 0x00000000, 0xffffffff },
10484 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10485 0x00000000, 0x000000ff },
10486 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
10487 0x00000000, 0xffffffff },
10488 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10489 0x00000000, 0x000000ff },
10490 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
10491 0x00000000, 0xffffffff },
10492 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
10493 0x00000000, 0xffffffff },
10494 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10495 0x00000000, 0xffffffff },
10496 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10497 0x00000000, 0x000000ff },
10498 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10499 0x00000000, 0xffffffff },
10500 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10501 0x00000000, 0x000000ff },
10502 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
10503 0x00000000, 0xffffffff },
10504 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
10505 0x00000000, 0xffffffff },
10506 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
10507 0x00000000, 0xffffffff },
10508 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
10509 0x00000000, 0xffffffff },
10510 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
10511 0x00000000, 0xffffffff },
10512 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
10513 0xffffffff, 0x00000000 },
10514 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
10515 0xffffffff, 0x00000000 },
10516
10517 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070010518 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010519 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070010520 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010521 0x00000000, 0x007fffff },
10522 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
10523 0x00000000, 0x0000003f },
10524 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
10525 0x00000000, 0x000001ff },
10526 { BUFMGR_MB_HIGH_WATER, 0x0000,
10527 0x00000000, 0x000001ff },
10528 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
10529 0xffffffff, 0x00000000 },
10530 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
10531 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010532
Michael Chana71116d2005-05-29 14:58:11 -070010533 /* Mailbox Registers */
10534 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
10535 0x00000000, 0x000001ff },
10536 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
10537 0x00000000, 0x000001ff },
10538 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
10539 0x00000000, 0x000007ff },
10540 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
10541 0x00000000, 0x000001ff },
10542
10543 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
10544 };
10545
Michael Chanb16250e2006-09-27 16:10:14 -070010546 is_5705 = is_5750 = 0;
10547 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
Michael Chana71116d2005-05-29 14:58:11 -070010548 is_5705 = 1;
Michael Chanb16250e2006-09-27 16:10:14 -070010549 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
10550 is_5750 = 1;
10551 }
Michael Chana71116d2005-05-29 14:58:11 -070010552
10553 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
10554 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
10555 continue;
10556
10557 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
10558 continue;
10559
10560 if ((tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
10561 (reg_tbl[i].flags & TG3_FL_NOT_5788))
10562 continue;
10563
Michael Chanb16250e2006-09-27 16:10:14 -070010564 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
10565 continue;
10566
Michael Chana71116d2005-05-29 14:58:11 -070010567 offset = (u32) reg_tbl[i].offset;
10568 read_mask = reg_tbl[i].read_mask;
10569 write_mask = reg_tbl[i].write_mask;
10570
10571 /* Save the original register content */
10572 save_val = tr32(offset);
10573
10574 /* Determine the read-only value. */
10575 read_val = save_val & read_mask;
10576
10577 /* Write zero to the register, then make sure the read-only bits
10578 * are not changed and the read/write bits are all zeros.
10579 */
10580 tw32(offset, 0);
10581
10582 val = tr32(offset);
10583
10584 /* Test the read-only and read/write bits. */
10585 if (((val & read_mask) != read_val) || (val & write_mask))
10586 goto out;
10587
10588 /* Write ones to all the bits defined by RdMask and WrMask, then
10589 * make sure the read-only bits are not changed and the
10590 * read/write bits are all ones.
10591 */
10592 tw32(offset, read_mask | write_mask);
10593
10594 val = tr32(offset);
10595
10596 /* Test the read-only bits. */
10597 if ((val & read_mask) != read_val)
10598 goto out;
10599
10600 /* Test the read/write bits. */
10601 if ((val & write_mask) != write_mask)
10602 goto out;
10603
10604 tw32(offset, save_val);
10605 }
10606
10607 return 0;
10608
10609out:
Michael Chan9f88f292006-12-07 00:22:54 -080010610 if (netif_msg_hw(tp))
10611 printk(KERN_ERR PFX "Register test failed at offset %x\n",
10612 offset);
Michael Chana71116d2005-05-29 14:58:11 -070010613 tw32(offset, save_val);
10614 return -EIO;
10615}
10616
Michael Chan7942e1d2005-05-29 14:58:36 -070010617static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
10618{
Arjan van de Venf71e1302006-03-03 21:33:57 -050010619 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070010620 int i;
10621 u32 j;
10622
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020010623 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070010624 for (j = 0; j < len; j += 4) {
10625 u32 val;
10626
10627 tg3_write_mem(tp, offset + j, test_pattern[i]);
10628 tg3_read_mem(tp, offset + j, &val);
10629 if (val != test_pattern[i])
10630 return -EIO;
10631 }
10632 }
10633 return 0;
10634}
10635
10636static int tg3_test_memory(struct tg3 *tp)
10637{
10638 static struct mem_entry {
10639 u32 offset;
10640 u32 len;
10641 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080010642 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070010643 { 0x00002000, 0x1c000},
10644 { 0xffffffff, 0x00000}
10645 }, mem_tbl_5705[] = {
10646 { 0x00000100, 0x0000c},
10647 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070010648 { 0x00004000, 0x00800},
10649 { 0x00006000, 0x01000},
10650 { 0x00008000, 0x02000},
10651 { 0x00010000, 0x0e000},
10652 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080010653 }, mem_tbl_5755[] = {
10654 { 0x00000200, 0x00008},
10655 { 0x00004000, 0x00800},
10656 { 0x00006000, 0x00800},
10657 { 0x00008000, 0x02000},
10658 { 0x00010000, 0x0c000},
10659 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070010660 }, mem_tbl_5906[] = {
10661 { 0x00000200, 0x00008},
10662 { 0x00004000, 0x00400},
10663 { 0x00006000, 0x00400},
10664 { 0x00008000, 0x01000},
10665 { 0x00010000, 0x01000},
10666 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000010667 }, mem_tbl_5717[] = {
10668 { 0x00000200, 0x00008},
10669 { 0x00010000, 0x0a000},
10670 { 0x00020000, 0x13c00},
10671 { 0xffffffff, 0x00000}
10672 }, mem_tbl_57765[] = {
10673 { 0x00000200, 0x00008},
10674 { 0x00004000, 0x00800},
10675 { 0x00006000, 0x09800},
10676 { 0x00010000, 0x0a000},
10677 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070010678 };
10679 struct mem_entry *mem_tbl;
10680 int err = 0;
10681 int i;
10682
Matt Carlson8b5a6c42010-01-20 16:58:06 +000010683 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
10684 mem_tbl = mem_tbl_5717;
10685 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
10686 mem_tbl = mem_tbl_57765;
10687 else if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Matt Carlson321d32a2008-11-21 17:22:19 -080010688 mem_tbl = mem_tbl_5755;
10689 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
10690 mem_tbl = mem_tbl_5906;
10691 else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
10692 mem_tbl = mem_tbl_5705;
10693 else
Michael Chan7942e1d2005-05-29 14:58:36 -070010694 mem_tbl = mem_tbl_570x;
10695
10696 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
10697 if ((err = tg3_do_mem_test(tp, mem_tbl[i].offset,
10698 mem_tbl[i].len)) != 0)
10699 break;
10700 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010701
Michael Chan7942e1d2005-05-29 14:58:36 -070010702 return err;
10703}
10704
Michael Chan9f40dea2005-09-05 17:53:06 -070010705#define TG3_MAC_LOOPBACK 0
10706#define TG3_PHY_LOOPBACK 1
10707
10708static int tg3_run_loopback(struct tg3 *tp, int loopback_mode)
Michael Chanc76949a2005-05-29 14:58:59 -070010709{
Michael Chan9f40dea2005-09-05 17:53:06 -070010710 u32 mac_mode, rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010711 u32 desc_idx, coal_now;
Michael Chanc76949a2005-05-29 14:58:59 -070010712 struct sk_buff *skb, *rx_skb;
10713 u8 *tx_data;
10714 dma_addr_t map;
10715 int num_pkts, tx_len, rx_len, i, err;
10716 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000010717 struct tg3_napi *tnapi, *rnapi;
Matt Carlson21f581a2009-08-28 14:00:25 +000010718 struct tg3_rx_prodring_set *tpr = &tp->prodring[0];
Michael Chanc76949a2005-05-29 14:58:59 -070010719
Matt Carlson0c1d0e22009-09-01 13:16:33 +000010720 if (tp->irq_cnt > 1) {
10721 tnapi = &tp->napi[1];
10722 rnapi = &tp->napi[1];
10723 } else {
10724 tnapi = &tp->napi[0];
10725 rnapi = &tp->napi[0];
10726 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010727 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000010728
Michael Chan9f40dea2005-09-05 17:53:06 -070010729 if (loopback_mode == TG3_MAC_LOOPBACK) {
Michael Chanc94e3942005-09-27 12:12:42 -070010730 /* HW errata - mac loopback fails in some cases on 5780.
10731 * Normal traffic and PHY loopback are not affected by
10732 * errata.
10733 */
10734 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780)
10735 return 0;
10736
Michael Chan9f40dea2005-09-05 17:53:06 -070010737 mac_mode = (tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK) |
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010738 MAC_MODE_PORT_INT_LPBACK;
10739 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
10740 mac_mode |= MAC_MODE_LINK_POLARITY;
Michael Chan3f7045c2006-09-27 16:02:29 -070010741 if (tp->tg3_flags & TG3_FLAG_10_100_ONLY)
10742 mac_mode |= MAC_MODE_PORT_MODE_MII;
10743 else
10744 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chan9f40dea2005-09-05 17:53:06 -070010745 tw32(MAC_MODE, mac_mode);
10746 } else if (loopback_mode == TG3_PHY_LOOPBACK) {
Michael Chan3f7045c2006-09-27 16:02:29 -070010747 u32 val;
10748
Matt Carlson7f97a4b2009-08-25 10:10:03 +000010749 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
10750 tg3_phy_fet_toggle_apd(tp, false);
Michael Chan5d64ad32006-12-07 00:19:40 -080010751 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED100;
10752 } else
10753 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED1000;
Michael Chan3f7045c2006-09-27 16:02:29 -070010754
Matt Carlson9ef8ca92007-07-11 19:48:29 -070010755 tg3_phy_toggle_automdix(tp, 0);
10756
Michael Chan3f7045c2006-09-27 16:02:29 -070010757 tg3_writephy(tp, MII_BMCR, val);
Michael Chanc94e3942005-09-27 12:12:42 -070010758 udelay(40);
Michael Chan5d64ad32006-12-07 00:19:40 -080010759
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010760 mac_mode = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000010761 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
10762 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
10763 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x1800);
Michael Chan5d64ad32006-12-07 00:19:40 -080010764 mac_mode |= MAC_MODE_PORT_MODE_MII;
10765 } else
10766 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chanb16250e2006-09-27 16:10:14 -070010767
Michael Chanc94e3942005-09-27 12:12:42 -070010768 /* reset to prevent losing 1st rx packet intermittently */
10769 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES) {
10770 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
10771 udelay(10);
10772 tw32_f(MAC_RX_MODE, tp->rx_mode);
10773 }
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010774 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
10775 if ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5401)
10776 mac_mode &= ~MAC_MODE_LINK_POLARITY;
10777 else if ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5411)
10778 mac_mode |= MAC_MODE_LINK_POLARITY;
Michael Chanff18ff02006-03-27 23:17:27 -080010779 tg3_writephy(tp, MII_TG3_EXT_CTRL,
10780 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
10781 }
Michael Chan9f40dea2005-09-05 17:53:06 -070010782 tw32(MAC_MODE, mac_mode);
Michael Chan9f40dea2005-09-05 17:53:06 -070010783 }
10784 else
10785 return -EINVAL;
Michael Chanc76949a2005-05-29 14:58:59 -070010786
10787 err = -EIO;
10788
Michael Chanc76949a2005-05-29 14:58:59 -070010789 tx_len = 1514;
David S. Millera20e9c62006-07-31 22:38:16 -070010790 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070010791 if (!skb)
10792 return -ENOMEM;
10793
Michael Chanc76949a2005-05-29 14:58:59 -070010794 tx_data = skb_put(skb, tx_len);
10795 memcpy(tx_data, tp->dev->dev_addr, 6);
10796 memset(tx_data + 6, 0x0, 8);
10797
10798 tw32(MAC_RX_MTU_SIZE, tx_len + 4);
10799
10800 for (i = 14; i < tx_len; i++)
10801 tx_data[i] = (u8) (i & 0xff);
10802
Alexander Duyckf4188d82009-12-02 16:48:38 +000010803 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
10804 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000010805 dev_kfree_skb(skb);
10806 return -EIO;
10807 }
Michael Chanc76949a2005-05-29 14:58:59 -070010808
10809 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010810 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070010811
10812 udelay(10);
10813
Matt Carlson898a56f2009-08-28 14:02:40 +000010814 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070010815
Michael Chanc76949a2005-05-29 14:58:59 -070010816 num_pkts = 0;
10817
Alexander Duyckf4188d82009-12-02 16:48:38 +000010818 tg3_set_txd(tnapi, tnapi->tx_prod, map, tx_len, 0, 1);
Michael Chanc76949a2005-05-29 14:58:59 -070010819
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010820 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070010821 num_pkts++;
10822
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010823 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
10824 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070010825
10826 udelay(10);
10827
Matt Carlson303fc922009-11-02 14:27:34 +000010828 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
10829 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070010830 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010831 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070010832
10833 udelay(10);
10834
Matt Carlson898a56f2009-08-28 14:02:40 +000010835 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
10836 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010837 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070010838 (rx_idx == (rx_start_idx + num_pkts)))
10839 break;
10840 }
10841
Alexander Duyckf4188d82009-12-02 16:48:38 +000010842 pci_unmap_single(tp->pdev, map, tx_len, PCI_DMA_TODEVICE);
Michael Chanc76949a2005-05-29 14:58:59 -070010843 dev_kfree_skb(skb);
10844
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010845 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070010846 goto out;
10847
10848 if (rx_idx != rx_start_idx + num_pkts)
10849 goto out;
10850
Matt Carlson72334482009-08-28 14:03:01 +000010851 desc = &rnapi->rx_rcb[rx_start_idx];
Michael Chanc76949a2005-05-29 14:58:59 -070010852 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
10853 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
10854 if (opaque_key != RXD_OPAQUE_RING_STD)
10855 goto out;
10856
10857 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
10858 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
10859 goto out;
10860
10861 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) - 4;
10862 if (rx_len != tx_len)
10863 goto out;
10864
Matt Carlson21f581a2009-08-28 14:00:25 +000010865 rx_skb = tpr->rx_std_buffers[desc_idx].skb;
Michael Chanc76949a2005-05-29 14:58:59 -070010866
Matt Carlson21f581a2009-08-28 14:00:25 +000010867 map = pci_unmap_addr(&tpr->rx_std_buffers[desc_idx], mapping);
Michael Chanc76949a2005-05-29 14:58:59 -070010868 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len, PCI_DMA_FROMDEVICE);
10869
10870 for (i = 14; i < tx_len; i++) {
10871 if (*(rx_skb->data + i) != (u8) (i & 0xff))
10872 goto out;
10873 }
10874 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010875
Michael Chanc76949a2005-05-29 14:58:59 -070010876 /* tg3_free_rings will unmap and free the rx_skb */
10877out:
10878 return err;
10879}
10880
Michael Chan9f40dea2005-09-05 17:53:06 -070010881#define TG3_MAC_LOOPBACK_FAILED 1
10882#define TG3_PHY_LOOPBACK_FAILED 2
10883#define TG3_LOOPBACK_FAILED (TG3_MAC_LOOPBACK_FAILED | \
10884 TG3_PHY_LOOPBACK_FAILED)
10885
10886static int tg3_test_loopback(struct tg3 *tp)
10887{
10888 int err = 0;
Matt Carlson9936bcf2007-10-10 18:03:07 -070010889 u32 cpmuctrl = 0;
Michael Chan9f40dea2005-09-05 17:53:06 -070010890
10891 if (!netif_running(tp->dev))
10892 return TG3_LOOPBACK_FAILED;
10893
Michael Chanb9ec6c12006-07-25 16:37:27 -070010894 err = tg3_reset_hw(tp, 1);
10895 if (err)
10896 return TG3_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070010897
Matt Carlson6833c042008-11-21 17:18:59 -080010898 /* Turn off gphy autopowerdown. */
10899 if (tp->tg3_flags3 & TG3_FLG3_PHY_ENABLE_APD)
10900 tg3_phy_toggle_apd(tp, false);
10901
Matt Carlson321d32a2008-11-21 17:22:19 -080010902 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070010903 int i;
10904 u32 status;
10905
10906 tw32(TG3_CPMU_MUTEX_REQ, CPMU_MUTEX_REQ_DRIVER);
10907
10908 /* Wait for up to 40 microseconds to acquire lock. */
10909 for (i = 0; i < 4; i++) {
10910 status = tr32(TG3_CPMU_MUTEX_GNT);
10911 if (status == CPMU_MUTEX_GNT_DRIVER)
10912 break;
10913 udelay(10);
10914 }
10915
10916 if (status != CPMU_MUTEX_GNT_DRIVER)
10917 return TG3_LOOPBACK_FAILED;
10918
Matt Carlsonb2a5c192008-04-03 21:44:44 -070010919 /* Turn off link-based power management. */
Matt Carlsone8750932007-11-12 21:11:51 -080010920 cpmuctrl = tr32(TG3_CPMU_CTRL);
Matt Carlson109115e2008-05-02 16:48:59 -070010921 tw32(TG3_CPMU_CTRL,
10922 cpmuctrl & ~(CPMU_CTRL_LINK_SPEED_MODE |
10923 CPMU_CTRL_LINK_AWARE_MODE));
Matt Carlson9936bcf2007-10-10 18:03:07 -070010924 }
10925
Michael Chan9f40dea2005-09-05 17:53:06 -070010926 if (tg3_run_loopback(tp, TG3_MAC_LOOPBACK))
10927 err |= TG3_MAC_LOOPBACK_FAILED;
Matt Carlson9936bcf2007-10-10 18:03:07 -070010928
Matt Carlson321d32a2008-11-21 17:22:19 -080010929 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070010930 tw32(TG3_CPMU_CTRL, cpmuctrl);
10931
10932 /* Release the mutex */
10933 tw32(TG3_CPMU_MUTEX_GNT, CPMU_MUTEX_GNT_DRIVER);
10934 }
10935
Matt Carlsondd477002008-05-25 23:45:58 -070010936 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
10937 !(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)) {
Michael Chan9f40dea2005-09-05 17:53:06 -070010938 if (tg3_run_loopback(tp, TG3_PHY_LOOPBACK))
10939 err |= TG3_PHY_LOOPBACK_FAILED;
10940 }
10941
Matt Carlson6833c042008-11-21 17:18:59 -080010942 /* Re-enable gphy autopowerdown. */
10943 if (tp->tg3_flags3 & TG3_FLG3_PHY_ENABLE_APD)
10944 tg3_phy_toggle_apd(tp, true);
10945
Michael Chan9f40dea2005-09-05 17:53:06 -070010946 return err;
10947}
10948
Michael Chan4cafd3f2005-05-29 14:56:34 -070010949static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
10950 u64 *data)
10951{
Michael Chan566f86a2005-05-29 14:56:58 -070010952 struct tg3 *tp = netdev_priv(dev);
10953
Michael Chanbc1c7562006-03-20 17:48:03 -080010954 if (tp->link_config.phy_is_low_power)
10955 tg3_set_power_state(tp, PCI_D0);
10956
Michael Chan566f86a2005-05-29 14:56:58 -070010957 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
10958
10959 if (tg3_test_nvram(tp) != 0) {
10960 etest->flags |= ETH_TEST_FL_FAILED;
10961 data[0] = 1;
10962 }
Michael Chanca430072005-05-29 14:57:23 -070010963 if (tg3_test_link(tp) != 0) {
10964 etest->flags |= ETH_TEST_FL_FAILED;
10965 data[1] = 1;
10966 }
Michael Chana71116d2005-05-29 14:58:11 -070010967 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010968 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070010969
Michael Chanbbe832c2005-06-24 20:20:04 -070010970 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010971 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010972 tg3_netif_stop(tp);
10973 irq_sync = 1;
10974 }
10975
10976 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070010977
10978 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080010979 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070010980 tg3_halt_cpu(tp, RX_CPU_BASE);
10981 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
10982 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080010983 if (!err)
10984 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070010985
Michael Chand9ab5ad12006-03-20 22:27:35 -080010986 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)
10987 tg3_phy_reset(tp);
10988
Michael Chana71116d2005-05-29 14:58:11 -070010989 if (tg3_test_registers(tp) != 0) {
10990 etest->flags |= ETH_TEST_FL_FAILED;
10991 data[2] = 1;
10992 }
Michael Chan7942e1d2005-05-29 14:58:36 -070010993 if (tg3_test_memory(tp) != 0) {
10994 etest->flags |= ETH_TEST_FL_FAILED;
10995 data[3] = 1;
10996 }
Michael Chan9f40dea2005-09-05 17:53:06 -070010997 if ((data[4] = tg3_test_loopback(tp)) != 0)
Michael Chanc76949a2005-05-29 14:58:59 -070010998 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070010999
David S. Millerf47c11e2005-06-24 20:18:35 -070011000 tg3_full_unlock(tp);
11001
Michael Chand4bc3922005-05-29 14:59:20 -070011002 if (tg3_test_interrupt(tp) != 0) {
11003 etest->flags |= ETH_TEST_FL_FAILED;
11004 data[5] = 1;
11005 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011006
11007 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070011008
Michael Chana71116d2005-05-29 14:58:11 -070011009 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
11010 if (netif_running(dev)) {
11011 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011012 err2 = tg3_restart_hw(tp, 1);
11013 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070011014 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011015 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011016
11017 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011018
11019 if (irq_sync && !err2)
11020 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011021 }
Michael Chanbc1c7562006-03-20 17:48:03 -080011022 if (tp->link_config.phy_is_low_power)
11023 tg3_set_power_state(tp, PCI_D3hot);
11024
Michael Chan4cafd3f2005-05-29 14:56:34 -070011025}
11026
Linus Torvalds1da177e2005-04-16 15:20:36 -070011027static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
11028{
11029 struct mii_ioctl_data *data = if_mii(ifr);
11030 struct tg3 *tp = netdev_priv(dev);
11031 int err;
11032
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011033 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011034 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011035 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
11036 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011037 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
11038 return phy_mii_ioctl(phydev, data, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011039 }
11040
Linus Torvalds1da177e2005-04-16 15:20:36 -070011041 switch(cmd) {
11042 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000011043 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011044
11045 /* fallthru */
11046 case SIOCGMIIREG: {
11047 u32 mii_regval;
11048
11049 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
11050 break; /* We have no PHY */
11051
Michael Chanbc1c7562006-03-20 17:48:03 -080011052 if (tp->link_config.phy_is_low_power)
11053 return -EAGAIN;
11054
David S. Millerf47c11e2005-06-24 20:18:35 -070011055 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011056 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070011057 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011058
11059 data->val_out = mii_regval;
11060
11061 return err;
11062 }
11063
11064 case SIOCSMIIREG:
11065 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
11066 break; /* We have no PHY */
11067
Michael Chanbc1c7562006-03-20 17:48:03 -080011068 if (tp->link_config.phy_is_low_power)
11069 return -EAGAIN;
11070
David S. Millerf47c11e2005-06-24 20:18:35 -070011071 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011072 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070011073 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011074
11075 return err;
11076
11077 default:
11078 /* do nothing */
11079 break;
11080 }
11081 return -EOPNOTSUPP;
11082}
11083
11084#if TG3_VLAN_TAG_USED
11085static void tg3_vlan_rx_register(struct net_device *dev, struct vlan_group *grp)
11086{
11087 struct tg3 *tp = netdev_priv(dev);
11088
Matt Carlson844b3ee2009-02-25 14:23:56 +000011089 if (!netif_running(dev)) {
11090 tp->vlgrp = grp;
11091 return;
11092 }
11093
11094 tg3_netif_stop(tp);
Michael Chan29315e82006-06-29 20:12:30 -070011095
David S. Millerf47c11e2005-06-24 20:18:35 -070011096 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011097
11098 tp->vlgrp = grp;
11099
11100 /* Update RX_MODE_KEEP_VLAN_TAG bit in RX_MODE register. */
11101 __tg3_set_rx_mode(dev);
11102
Matt Carlson844b3ee2009-02-25 14:23:56 +000011103 tg3_netif_start(tp);
Michael Chan46966542007-07-11 19:47:19 -070011104
11105 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011106}
Linus Torvalds1da177e2005-04-16 15:20:36 -070011107#endif
11108
David S. Miller15f98502005-05-18 22:49:26 -070011109static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11110{
11111 struct tg3 *tp = netdev_priv(dev);
11112
11113 memcpy(ec, &tp->coal, sizeof(*ec));
11114 return 0;
11115}
11116
Michael Chand244c892005-07-05 14:42:33 -070011117static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11118{
11119 struct tg3 *tp = netdev_priv(dev);
11120 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
11121 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
11122
11123 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
11124 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
11125 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
11126 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
11127 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
11128 }
11129
11130 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
11131 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
11132 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
11133 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
11134 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
11135 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
11136 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
11137 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
11138 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
11139 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
11140 return -EINVAL;
11141
11142 /* No rx interrupts will be generated if both are zero */
11143 if ((ec->rx_coalesce_usecs == 0) &&
11144 (ec->rx_max_coalesced_frames == 0))
11145 return -EINVAL;
11146
11147 /* No tx interrupts will be generated if both are zero */
11148 if ((ec->tx_coalesce_usecs == 0) &&
11149 (ec->tx_max_coalesced_frames == 0))
11150 return -EINVAL;
11151
11152 /* Only copy relevant parameters, ignore all others. */
11153 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
11154 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
11155 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
11156 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
11157 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
11158 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
11159 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
11160 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
11161 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
11162
11163 if (netif_running(dev)) {
11164 tg3_full_lock(tp, 0);
11165 __tg3_set_coalesce(tp, &tp->coal);
11166 tg3_full_unlock(tp);
11167 }
11168 return 0;
11169}
11170
Jeff Garzik7282d492006-09-13 14:30:00 -040011171static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011172 .get_settings = tg3_get_settings,
11173 .set_settings = tg3_set_settings,
11174 .get_drvinfo = tg3_get_drvinfo,
11175 .get_regs_len = tg3_get_regs_len,
11176 .get_regs = tg3_get_regs,
11177 .get_wol = tg3_get_wol,
11178 .set_wol = tg3_set_wol,
11179 .get_msglevel = tg3_get_msglevel,
11180 .set_msglevel = tg3_set_msglevel,
11181 .nway_reset = tg3_nway_reset,
11182 .get_link = ethtool_op_get_link,
11183 .get_eeprom_len = tg3_get_eeprom_len,
11184 .get_eeprom = tg3_get_eeprom,
11185 .set_eeprom = tg3_set_eeprom,
11186 .get_ringparam = tg3_get_ringparam,
11187 .set_ringparam = tg3_set_ringparam,
11188 .get_pauseparam = tg3_get_pauseparam,
11189 .set_pauseparam = tg3_set_pauseparam,
11190 .get_rx_csum = tg3_get_rx_csum,
11191 .set_rx_csum = tg3_set_rx_csum,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011192 .set_tx_csum = tg3_set_tx_csum,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011193 .set_sg = ethtool_op_set_sg,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011194 .set_tso = tg3_set_tso,
Michael Chan4cafd3f2005-05-29 14:56:34 -070011195 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011196 .get_strings = tg3_get_strings,
Michael Chan4009a932005-09-05 17:52:54 -070011197 .phys_id = tg3_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011198 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070011199 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070011200 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070011201 .get_sset_count = tg3_get_sset_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011202};
11203
11204static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
11205{
Michael Chan1b277772006-03-20 22:27:48 -080011206 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011207
11208 tp->nvram_size = EEPROM_CHIP_SIZE;
11209
Matt Carlsone4f34112009-02-25 14:25:00 +000011210 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011211 return;
11212
Michael Chanb16250e2006-09-27 16:10:14 -070011213 if ((magic != TG3_EEPROM_MAGIC) &&
11214 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
11215 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011216 return;
11217
11218 /*
11219 * Size the chip by reading offsets at increasing powers of two.
11220 * When we encounter our validation signature, we know the addressing
11221 * has wrapped around, and thus have our chip size.
11222 */
Michael Chan1b277772006-03-20 22:27:48 -080011223 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011224
11225 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000011226 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011227 return;
11228
Michael Chan18201802006-03-20 22:29:15 -080011229 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011230 break;
11231
11232 cursize <<= 1;
11233 }
11234
11235 tp->nvram_size = cursize;
11236}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011237
Linus Torvalds1da177e2005-04-16 15:20:36 -070011238static void __devinit tg3_get_nvram_size(struct tg3 *tp)
11239{
11240 u32 val;
11241
Matt Carlsondf259d82009-04-20 06:57:14 +000011242 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
11243 tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080011244 return;
11245
11246 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080011247 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080011248 tg3_get_eeprom_size(tp);
11249 return;
11250 }
11251
Matt Carlson6d348f22009-02-25 14:25:52 +000011252 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011253 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000011254 /* This is confusing. We want to operate on the
11255 * 16-bit value at offset 0xf2. The tg3_nvram_read()
11256 * call will read from NVRAM and byteswap the data
11257 * according to the byteswapping settings for all
11258 * other register accesses. This ensures the data we
11259 * want will always reside in the lower 16-bits.
11260 * However, the data in NVRAM is in LE format, which
11261 * means the data from the NVRAM read will always be
11262 * opposite the endianness of the CPU. The 16-bit
11263 * byteswap then brings the data to CPU endianness.
11264 */
11265 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011266 return;
11267 }
11268 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070011269 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011270}
11271
11272static void __devinit tg3_get_nvram_info(struct tg3 *tp)
11273{
11274 u32 nvcfg1;
11275
11276 nvcfg1 = tr32(NVRAM_CFG1);
11277 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
11278 tp->tg3_flags2 |= TG3_FLG2_FLASH;
Matt Carlson8590a602009-08-28 12:29:16 +000011279 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011280 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11281 tw32(NVRAM_CFG1, nvcfg1);
11282 }
11283
Michael Chan4c987482005-09-05 17:52:38 -070011284 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750) ||
Michael Chana4e2b342005-10-26 15:46:52 -070011285 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011286 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011287 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
11288 tp->nvram_jedecnum = JEDEC_ATMEL;
11289 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
11290 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11291 break;
11292 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
11293 tp->nvram_jedecnum = JEDEC_ATMEL;
11294 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
11295 break;
11296 case FLASH_VENDOR_ATMEL_EEPROM:
11297 tp->nvram_jedecnum = JEDEC_ATMEL;
11298 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11299 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11300 break;
11301 case FLASH_VENDOR_ST:
11302 tp->nvram_jedecnum = JEDEC_ST;
11303 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
11304 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11305 break;
11306 case FLASH_VENDOR_SAIFUN:
11307 tp->nvram_jedecnum = JEDEC_SAIFUN;
11308 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
11309 break;
11310 case FLASH_VENDOR_SST_SMALL:
11311 case FLASH_VENDOR_SST_LARGE:
11312 tp->nvram_jedecnum = JEDEC_SST;
11313 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
11314 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011315 }
Matt Carlson8590a602009-08-28 12:29:16 +000011316 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011317 tp->nvram_jedecnum = JEDEC_ATMEL;
11318 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
11319 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11320 }
11321}
11322
Matt Carlsona1b950d2009-09-01 13:20:17 +000011323static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
11324{
11325 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
11326 case FLASH_5752PAGE_SIZE_256:
11327 tp->nvram_pagesize = 256;
11328 break;
11329 case FLASH_5752PAGE_SIZE_512:
11330 tp->nvram_pagesize = 512;
11331 break;
11332 case FLASH_5752PAGE_SIZE_1K:
11333 tp->nvram_pagesize = 1024;
11334 break;
11335 case FLASH_5752PAGE_SIZE_2K:
11336 tp->nvram_pagesize = 2048;
11337 break;
11338 case FLASH_5752PAGE_SIZE_4K:
11339 tp->nvram_pagesize = 4096;
11340 break;
11341 case FLASH_5752PAGE_SIZE_264:
11342 tp->nvram_pagesize = 264;
11343 break;
11344 case FLASH_5752PAGE_SIZE_528:
11345 tp->nvram_pagesize = 528;
11346 break;
11347 }
11348}
11349
Michael Chan361b4ac2005-04-21 17:11:21 -070011350static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
11351{
11352 u32 nvcfg1;
11353
11354 nvcfg1 = tr32(NVRAM_CFG1);
11355
Michael Chane6af3012005-04-21 17:12:05 -070011356 /* NVRAM protection for TPM */
11357 if (nvcfg1 & (1 << 27))
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011358 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Michael Chane6af3012005-04-21 17:12:05 -070011359
Michael Chan361b4ac2005-04-21 17:11:21 -070011360 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011361 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
11362 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
11363 tp->nvram_jedecnum = JEDEC_ATMEL;
11364 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11365 break;
11366 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11367 tp->nvram_jedecnum = JEDEC_ATMEL;
11368 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11369 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11370 break;
11371 case FLASH_5752VENDOR_ST_M45PE10:
11372 case FLASH_5752VENDOR_ST_M45PE20:
11373 case FLASH_5752VENDOR_ST_M45PE40:
11374 tp->nvram_jedecnum = JEDEC_ST;
11375 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11376 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11377 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070011378 }
11379
11380 if (tp->tg3_flags2 & TG3_FLG2_FLASH) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000011381 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000011382 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070011383 /* For eeprom, set pagesize to maximum eeprom size */
11384 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11385
11386 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11387 tw32(NVRAM_CFG1, nvcfg1);
11388 }
11389}
11390
Michael Chand3c7b882006-03-23 01:28:25 -080011391static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
11392{
Matt Carlson989a9d22007-05-05 11:51:05 -070011393 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080011394
11395 nvcfg1 = tr32(NVRAM_CFG1);
11396
11397 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070011398 if (nvcfg1 & (1 << 27)) {
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011399 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Matt Carlson989a9d22007-05-05 11:51:05 -070011400 protect = 1;
11401 }
Michael Chand3c7b882006-03-23 01:28:25 -080011402
Matt Carlson989a9d22007-05-05 11:51:05 -070011403 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11404 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011405 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11406 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11407 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11408 case FLASH_5755VENDOR_ATMEL_FLASH_5:
11409 tp->nvram_jedecnum = JEDEC_ATMEL;
11410 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11411 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11412 tp->nvram_pagesize = 264;
11413 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
11414 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
11415 tp->nvram_size = (protect ? 0x3e200 :
11416 TG3_NVRAM_SIZE_512KB);
11417 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
11418 tp->nvram_size = (protect ? 0x1f200 :
11419 TG3_NVRAM_SIZE_256KB);
11420 else
11421 tp->nvram_size = (protect ? 0x1f200 :
11422 TG3_NVRAM_SIZE_128KB);
11423 break;
11424 case FLASH_5752VENDOR_ST_M45PE10:
11425 case FLASH_5752VENDOR_ST_M45PE20:
11426 case FLASH_5752VENDOR_ST_M45PE40:
11427 tp->nvram_jedecnum = JEDEC_ST;
11428 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11429 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11430 tp->nvram_pagesize = 256;
11431 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
11432 tp->nvram_size = (protect ?
11433 TG3_NVRAM_SIZE_64KB :
11434 TG3_NVRAM_SIZE_128KB);
11435 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
11436 tp->nvram_size = (protect ?
11437 TG3_NVRAM_SIZE_64KB :
11438 TG3_NVRAM_SIZE_256KB);
11439 else
11440 tp->nvram_size = (protect ?
11441 TG3_NVRAM_SIZE_128KB :
11442 TG3_NVRAM_SIZE_512KB);
11443 break;
Michael Chand3c7b882006-03-23 01:28:25 -080011444 }
11445}
11446
Michael Chan1b277772006-03-20 22:27:48 -080011447static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
11448{
11449 u32 nvcfg1;
11450
11451 nvcfg1 = tr32(NVRAM_CFG1);
11452
11453 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011454 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
11455 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11456 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
11457 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11458 tp->nvram_jedecnum = JEDEC_ATMEL;
11459 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11460 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080011461
Matt Carlson8590a602009-08-28 12:29:16 +000011462 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11463 tw32(NVRAM_CFG1, nvcfg1);
11464 break;
11465 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11466 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11467 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11468 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11469 tp->nvram_jedecnum = JEDEC_ATMEL;
11470 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11471 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11472 tp->nvram_pagesize = 264;
11473 break;
11474 case FLASH_5752VENDOR_ST_M45PE10:
11475 case FLASH_5752VENDOR_ST_M45PE20:
11476 case FLASH_5752VENDOR_ST_M45PE40:
11477 tp->nvram_jedecnum = JEDEC_ST;
11478 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11479 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11480 tp->nvram_pagesize = 256;
11481 break;
Michael Chan1b277772006-03-20 22:27:48 -080011482 }
11483}
11484
Matt Carlson6b91fa02007-10-10 18:01:09 -070011485static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
11486{
11487 u32 nvcfg1, protect = 0;
11488
11489 nvcfg1 = tr32(NVRAM_CFG1);
11490
11491 /* NVRAM protection for TPM */
11492 if (nvcfg1 & (1 << 27)) {
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011493 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011494 protect = 1;
11495 }
11496
11497 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11498 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011499 case FLASH_5761VENDOR_ATMEL_ADB021D:
11500 case FLASH_5761VENDOR_ATMEL_ADB041D:
11501 case FLASH_5761VENDOR_ATMEL_ADB081D:
11502 case FLASH_5761VENDOR_ATMEL_ADB161D:
11503 case FLASH_5761VENDOR_ATMEL_MDB021D:
11504 case FLASH_5761VENDOR_ATMEL_MDB041D:
11505 case FLASH_5761VENDOR_ATMEL_MDB081D:
11506 case FLASH_5761VENDOR_ATMEL_MDB161D:
11507 tp->nvram_jedecnum = JEDEC_ATMEL;
11508 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11509 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11510 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
11511 tp->nvram_pagesize = 256;
11512 break;
11513 case FLASH_5761VENDOR_ST_A_M45PE20:
11514 case FLASH_5761VENDOR_ST_A_M45PE40:
11515 case FLASH_5761VENDOR_ST_A_M45PE80:
11516 case FLASH_5761VENDOR_ST_A_M45PE16:
11517 case FLASH_5761VENDOR_ST_M_M45PE20:
11518 case FLASH_5761VENDOR_ST_M_M45PE40:
11519 case FLASH_5761VENDOR_ST_M_M45PE80:
11520 case FLASH_5761VENDOR_ST_M_M45PE16:
11521 tp->nvram_jedecnum = JEDEC_ST;
11522 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11523 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11524 tp->nvram_pagesize = 256;
11525 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011526 }
11527
11528 if (protect) {
11529 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
11530 } else {
11531 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011532 case FLASH_5761VENDOR_ATMEL_ADB161D:
11533 case FLASH_5761VENDOR_ATMEL_MDB161D:
11534 case FLASH_5761VENDOR_ST_A_M45PE16:
11535 case FLASH_5761VENDOR_ST_M_M45PE16:
11536 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
11537 break;
11538 case FLASH_5761VENDOR_ATMEL_ADB081D:
11539 case FLASH_5761VENDOR_ATMEL_MDB081D:
11540 case FLASH_5761VENDOR_ST_A_M45PE80:
11541 case FLASH_5761VENDOR_ST_M_M45PE80:
11542 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
11543 break;
11544 case FLASH_5761VENDOR_ATMEL_ADB041D:
11545 case FLASH_5761VENDOR_ATMEL_MDB041D:
11546 case FLASH_5761VENDOR_ST_A_M45PE40:
11547 case FLASH_5761VENDOR_ST_M_M45PE40:
11548 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11549 break;
11550 case FLASH_5761VENDOR_ATMEL_ADB021D:
11551 case FLASH_5761VENDOR_ATMEL_MDB021D:
11552 case FLASH_5761VENDOR_ST_A_M45PE20:
11553 case FLASH_5761VENDOR_ST_M_M45PE20:
11554 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11555 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011556 }
11557 }
11558}
11559
Michael Chanb5d37722006-09-27 16:06:21 -070011560static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
11561{
11562 tp->nvram_jedecnum = JEDEC_ATMEL;
11563 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11564 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11565}
11566
Matt Carlson321d32a2008-11-21 17:22:19 -080011567static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
11568{
11569 u32 nvcfg1;
11570
11571 nvcfg1 = tr32(NVRAM_CFG1);
11572
11573 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11574 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11575 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11576 tp->nvram_jedecnum = JEDEC_ATMEL;
11577 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11578 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11579
11580 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11581 tw32(NVRAM_CFG1, nvcfg1);
11582 return;
11583 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11584 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11585 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11586 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11587 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11588 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11589 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
11590 tp->nvram_jedecnum = JEDEC_ATMEL;
11591 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11592 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11593
11594 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11595 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11596 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11597 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11598 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11599 break;
11600 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11601 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11602 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11603 break;
11604 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11605 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
11606 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11607 break;
11608 }
11609 break;
11610 case FLASH_5752VENDOR_ST_M45PE10:
11611 case FLASH_5752VENDOR_ST_M45PE20:
11612 case FLASH_5752VENDOR_ST_M45PE40:
11613 tp->nvram_jedecnum = JEDEC_ST;
11614 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11615 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11616
11617 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11618 case FLASH_5752VENDOR_ST_M45PE10:
11619 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11620 break;
11621 case FLASH_5752VENDOR_ST_M45PE20:
11622 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11623 break;
11624 case FLASH_5752VENDOR_ST_M45PE40:
11625 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11626 break;
11627 }
11628 break;
11629 default:
Matt Carlsondf259d82009-04-20 06:57:14 +000011630 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
Matt Carlson321d32a2008-11-21 17:22:19 -080011631 return;
11632 }
11633
Matt Carlsona1b950d2009-09-01 13:20:17 +000011634 tg3_nvram_get_pagesize(tp, nvcfg1);
11635 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Matt Carlson321d32a2008-11-21 17:22:19 -080011636 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011637}
11638
11639
11640static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
11641{
11642 u32 nvcfg1;
11643
11644 nvcfg1 = tr32(NVRAM_CFG1);
11645
11646 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11647 case FLASH_5717VENDOR_ATMEL_EEPROM:
11648 case FLASH_5717VENDOR_MICRO_EEPROM:
11649 tp->nvram_jedecnum = JEDEC_ATMEL;
11650 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11651 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11652
11653 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11654 tw32(NVRAM_CFG1, nvcfg1);
11655 return;
11656 case FLASH_5717VENDOR_ATMEL_MDB011D:
11657 case FLASH_5717VENDOR_ATMEL_ADB011B:
11658 case FLASH_5717VENDOR_ATMEL_ADB011D:
11659 case FLASH_5717VENDOR_ATMEL_MDB021D:
11660 case FLASH_5717VENDOR_ATMEL_ADB021B:
11661 case FLASH_5717VENDOR_ATMEL_ADB021D:
11662 case FLASH_5717VENDOR_ATMEL_45USPT:
11663 tp->nvram_jedecnum = JEDEC_ATMEL;
11664 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11665 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11666
11667 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11668 case FLASH_5717VENDOR_ATMEL_MDB021D:
11669 case FLASH_5717VENDOR_ATMEL_ADB021B:
11670 case FLASH_5717VENDOR_ATMEL_ADB021D:
11671 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11672 break;
11673 default:
11674 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11675 break;
11676 }
Matt Carlson321d32a2008-11-21 17:22:19 -080011677 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011678 case FLASH_5717VENDOR_ST_M_M25PE10:
11679 case FLASH_5717VENDOR_ST_A_M25PE10:
11680 case FLASH_5717VENDOR_ST_M_M45PE10:
11681 case FLASH_5717VENDOR_ST_A_M45PE10:
11682 case FLASH_5717VENDOR_ST_M_M25PE20:
11683 case FLASH_5717VENDOR_ST_A_M25PE20:
11684 case FLASH_5717VENDOR_ST_M_M45PE20:
11685 case FLASH_5717VENDOR_ST_A_M45PE20:
11686 case FLASH_5717VENDOR_ST_25USPT:
11687 case FLASH_5717VENDOR_ST_45USPT:
11688 tp->nvram_jedecnum = JEDEC_ST;
11689 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11690 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11691
11692 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11693 case FLASH_5717VENDOR_ST_M_M25PE20:
11694 case FLASH_5717VENDOR_ST_A_M25PE20:
11695 case FLASH_5717VENDOR_ST_M_M45PE20:
11696 case FLASH_5717VENDOR_ST_A_M45PE20:
11697 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11698 break;
11699 default:
11700 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11701 break;
11702 }
Matt Carlson321d32a2008-11-21 17:22:19 -080011703 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011704 default:
11705 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
11706 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080011707 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000011708
11709 tg3_nvram_get_pagesize(tp, nvcfg1);
11710 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
11711 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
Matt Carlson321d32a2008-11-21 17:22:19 -080011712}
11713
Linus Torvalds1da177e2005-04-16 15:20:36 -070011714/* Chips other than 5700/5701 use the NVRAM for fetching info. */
11715static void __devinit tg3_nvram_init(struct tg3 *tp)
11716{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011717 tw32_f(GRC_EEPROM_ADDR,
11718 (EEPROM_ADDR_FSM_RESET |
11719 (EEPROM_DEFAULT_CLOCK_PERIOD <<
11720 EEPROM_ADDR_CLKPERD_SHIFT)));
11721
Michael Chan9d57f012006-12-07 00:23:25 -080011722 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011723
11724 /* Enable seeprom accesses. */
11725 tw32_f(GRC_LOCAL_CTRL,
11726 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
11727 udelay(100);
11728
11729 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
11730 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
11731 tp->tg3_flags |= TG3_FLAG_NVRAM;
11732
Michael Chanec41c7d2006-01-17 02:40:55 -080011733 if (tg3_nvram_lock(tp)) {
11734 printk(KERN_WARNING PFX "%s: Cannot get nvarm lock, "
11735 "tg3_nvram_init failed.\n", tp->dev->name);
11736 return;
11737 }
Michael Chane6af3012005-04-21 17:12:05 -070011738 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011739
Matt Carlson989a9d22007-05-05 11:51:05 -070011740 tp->nvram_size = 0;
11741
Michael Chan361b4ac2005-04-21 17:11:21 -070011742 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
11743 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080011744 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
11745 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070011746 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070011747 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
11748 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080011749 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070011750 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
11751 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070011752 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
11753 tg3_get_5906_nvram_info(tp);
Matt Carlsonb703df62009-12-03 08:36:21 +000011754 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
11755 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson321d32a2008-11-21 17:22:19 -080011756 tg3_get_57780_nvram_info(tp);
Matt Carlsona1b950d2009-09-01 13:20:17 +000011757 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
11758 tg3_get_5717_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070011759 else
11760 tg3_get_nvram_info(tp);
11761
Matt Carlson989a9d22007-05-05 11:51:05 -070011762 if (tp->nvram_size == 0)
11763 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011764
Michael Chane6af3012005-04-21 17:12:05 -070011765 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080011766 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011767
11768 } else {
11769 tp->tg3_flags &= ~(TG3_FLAG_NVRAM | TG3_FLAG_NVRAM_BUFFERED);
11770
11771 tg3_get_eeprom_size(tp);
11772 }
11773}
11774
Linus Torvalds1da177e2005-04-16 15:20:36 -070011775static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
11776 u32 offset, u32 len, u8 *buf)
11777{
11778 int i, j, rc = 0;
11779 u32 val;
11780
11781 for (i = 0; i < len; i += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080011782 u32 addr;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011783 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011784
11785 addr = offset + i;
11786
11787 memcpy(&data, buf + i, 4);
11788
Matt Carlson62cedd12009-04-20 14:52:29 -070011789 /*
11790 * The SEEPROM interface expects the data to always be opposite
11791 * the native endian format. We accomplish this by reversing
11792 * all the operations that would have been performed on the
11793 * data from a call to tg3_nvram_read_be32().
11794 */
11795 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011796
11797 val = tr32(GRC_EEPROM_ADDR);
11798 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
11799
11800 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
11801 EEPROM_ADDR_READ);
11802 tw32(GRC_EEPROM_ADDR, val |
11803 (0 << EEPROM_ADDR_DEVID_SHIFT) |
11804 (addr & EEPROM_ADDR_ADDR_MASK) |
11805 EEPROM_ADDR_START |
11806 EEPROM_ADDR_WRITE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011807
Michael Chan9d57f012006-12-07 00:23:25 -080011808 for (j = 0; j < 1000; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011809 val = tr32(GRC_EEPROM_ADDR);
11810
11811 if (val & EEPROM_ADDR_COMPLETE)
11812 break;
Michael Chan9d57f012006-12-07 00:23:25 -080011813 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011814 }
11815 if (!(val & EEPROM_ADDR_COMPLETE)) {
11816 rc = -EBUSY;
11817 break;
11818 }
11819 }
11820
11821 return rc;
11822}
11823
11824/* offset and length are dword aligned */
11825static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
11826 u8 *buf)
11827{
11828 int ret = 0;
11829 u32 pagesize = tp->nvram_pagesize;
11830 u32 pagemask = pagesize - 1;
11831 u32 nvram_cmd;
11832 u8 *tmp;
11833
11834 tmp = kmalloc(pagesize, GFP_KERNEL);
11835 if (tmp == NULL)
11836 return -ENOMEM;
11837
11838 while (len) {
11839 int j;
Michael Chane6af3012005-04-21 17:12:05 -070011840 u32 phy_addr, page_off, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011841
11842 phy_addr = offset & ~pagemask;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011843
Linus Torvalds1da177e2005-04-16 15:20:36 -070011844 for (j = 0; j < pagesize; j += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000011845 ret = tg3_nvram_read_be32(tp, phy_addr + j,
11846 (__be32 *) (tmp + j));
11847 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011848 break;
11849 }
11850 if (ret)
11851 break;
11852
11853 page_off = offset & pagemask;
11854 size = pagesize;
11855 if (len < size)
11856 size = len;
11857
11858 len -= size;
11859
11860 memcpy(tmp + page_off, buf, size);
11861
11862 offset = offset + (pagesize - page_off);
11863
Michael Chane6af3012005-04-21 17:12:05 -070011864 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011865
11866 /*
11867 * Before we can erase the flash page, we need
11868 * to issue a special "write enable" command.
11869 */
11870 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
11871
11872 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
11873 break;
11874
11875 /* Erase the target page */
11876 tw32(NVRAM_ADDR, phy_addr);
11877
11878 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
11879 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
11880
11881 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
11882 break;
11883
11884 /* Issue another write enable to start the write. */
11885 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
11886
11887 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
11888 break;
11889
11890 for (j = 0; j < pagesize; j += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080011891 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011892
Al Virob9fc7dc2007-12-17 22:59:57 -080011893 data = *((__be32 *) (tmp + j));
Matt Carlsona9dc5292009-02-25 14:25:30 +000011894
Al Virob9fc7dc2007-12-17 22:59:57 -080011895 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011896
11897 tw32(NVRAM_ADDR, phy_addr + j);
11898
11899 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
11900 NVRAM_CMD_WR;
11901
11902 if (j == 0)
11903 nvram_cmd |= NVRAM_CMD_FIRST;
11904 else if (j == (pagesize - 4))
11905 nvram_cmd |= NVRAM_CMD_LAST;
11906
11907 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
11908 break;
11909 }
11910 if (ret)
11911 break;
11912 }
11913
11914 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
11915 tg3_nvram_exec_cmd(tp, nvram_cmd);
11916
11917 kfree(tmp);
11918
11919 return ret;
11920}
11921
11922/* offset and length are dword aligned */
11923static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
11924 u8 *buf)
11925{
11926 int i, ret = 0;
11927
11928 for (i = 0; i < len; i += 4, offset += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080011929 u32 page_off, phy_addr, nvram_cmd;
11930 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011931
11932 memcpy(&data, buf + i, 4);
Al Virob9fc7dc2007-12-17 22:59:57 -080011933 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011934
11935 page_off = offset % tp->nvram_pagesize;
11936
Michael Chan18201802006-03-20 22:29:15 -080011937 phy_addr = tg3_nvram_phys_addr(tp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011938
11939 tw32(NVRAM_ADDR, phy_addr);
11940
11941 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
11942
11943 if ((page_off == 0) || (i == 0))
11944 nvram_cmd |= NVRAM_CMD_FIRST;
Michael Chanf6d9a252006-04-29 19:00:24 -070011945 if (page_off == (tp->nvram_pagesize - 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011946 nvram_cmd |= NVRAM_CMD_LAST;
11947
11948 if (i == (len - 4))
11949 nvram_cmd |= NVRAM_CMD_LAST;
11950
Matt Carlson321d32a2008-11-21 17:22:19 -080011951 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
11952 !(tp->tg3_flags3 & TG3_FLG3_5755_PLUS) &&
Michael Chan4c987482005-09-05 17:52:38 -070011953 (tp->nvram_jedecnum == JEDEC_ST) &&
11954 (nvram_cmd & NVRAM_CMD_FIRST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011955
11956 if ((ret = tg3_nvram_exec_cmd(tp,
11957 NVRAM_CMD_WREN | NVRAM_CMD_GO |
11958 NVRAM_CMD_DONE)))
11959
11960 break;
11961 }
11962 if (!(tp->tg3_flags2 & TG3_FLG2_FLASH)) {
11963 /* We always do complete word writes to eeprom. */
11964 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
11965 }
11966
11967 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
11968 break;
11969 }
11970 return ret;
11971}
11972
11973/* offset and length are dword aligned */
11974static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
11975{
11976 int ret;
11977
Linus Torvalds1da177e2005-04-16 15:20:36 -070011978 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT) {
Michael Chan314fba32005-04-21 17:07:04 -070011979 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
11980 ~GRC_LCLCTRL_GPIO_OUTPUT1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011981 udelay(40);
11982 }
11983
11984 if (!(tp->tg3_flags & TG3_FLAG_NVRAM)) {
11985 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
11986 }
11987 else {
11988 u32 grc_mode;
11989
Michael Chanec41c7d2006-01-17 02:40:55 -080011990 ret = tg3_nvram_lock(tp);
11991 if (ret)
11992 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011993
Michael Chane6af3012005-04-21 17:12:05 -070011994 tg3_enable_nvram_access(tp);
11995 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011996 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011997 tw32(NVRAM_WRITE1, 0x406);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011998
11999 grc_mode = tr32(GRC_MODE);
12000 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
12001
12002 if ((tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) ||
12003 !(tp->tg3_flags2 & TG3_FLG2_FLASH)) {
12004
12005 ret = tg3_nvram_write_block_buffered(tp, offset, len,
12006 buf);
12007 }
12008 else {
12009 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
12010 buf);
12011 }
12012
12013 grc_mode = tr32(GRC_MODE);
12014 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
12015
Michael Chane6af3012005-04-21 17:12:05 -070012016 tg3_disable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012017 tg3_nvram_unlock(tp);
12018 }
12019
12020 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT) {
Michael Chan314fba32005-04-21 17:07:04 -070012021 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012022 udelay(40);
12023 }
12024
12025 return ret;
12026}
12027
12028struct subsys_tbl_ent {
12029 u16 subsys_vendor, subsys_devid;
12030 u32 phy_id;
12031};
12032
12033static struct subsys_tbl_ent subsys_id_to_phy_id[] = {
12034 /* Broadcom boards. */
12035 { PCI_VENDOR_ID_BROADCOM, 0x1644, PHY_ID_BCM5401 }, /* BCM95700A6 */
12036 { PCI_VENDOR_ID_BROADCOM, 0x0001, PHY_ID_BCM5701 }, /* BCM95701A5 */
12037 { PCI_VENDOR_ID_BROADCOM, 0x0002, PHY_ID_BCM8002 }, /* BCM95700T6 */
12038 { PCI_VENDOR_ID_BROADCOM, 0x0003, 0 }, /* BCM95700A9 */
12039 { PCI_VENDOR_ID_BROADCOM, 0x0005, PHY_ID_BCM5701 }, /* BCM95701T1 */
12040 { PCI_VENDOR_ID_BROADCOM, 0x0006, PHY_ID_BCM5701 }, /* BCM95701T8 */
12041 { PCI_VENDOR_ID_BROADCOM, 0x0007, 0 }, /* BCM95701A7 */
12042 { PCI_VENDOR_ID_BROADCOM, 0x0008, PHY_ID_BCM5701 }, /* BCM95701A10 */
12043 { PCI_VENDOR_ID_BROADCOM, 0x8008, PHY_ID_BCM5701 }, /* BCM95701A12 */
12044 { PCI_VENDOR_ID_BROADCOM, 0x0009, PHY_ID_BCM5703 }, /* BCM95703Ax1 */
12045 { PCI_VENDOR_ID_BROADCOM, 0x8009, PHY_ID_BCM5703 }, /* BCM95703Ax2 */
12046
12047 /* 3com boards. */
12048 { PCI_VENDOR_ID_3COM, 0x1000, PHY_ID_BCM5401 }, /* 3C996T */
12049 { PCI_VENDOR_ID_3COM, 0x1006, PHY_ID_BCM5701 }, /* 3C996BT */
12050 { PCI_VENDOR_ID_3COM, 0x1004, 0 }, /* 3C996SX */
12051 { PCI_VENDOR_ID_3COM, 0x1007, PHY_ID_BCM5701 }, /* 3C1000T */
12052 { PCI_VENDOR_ID_3COM, 0x1008, PHY_ID_BCM5701 }, /* 3C940BR01 */
12053
12054 /* DELL boards. */
12055 { PCI_VENDOR_ID_DELL, 0x00d1, PHY_ID_BCM5401 }, /* VIPER */
12056 { PCI_VENDOR_ID_DELL, 0x0106, PHY_ID_BCM5401 }, /* JAGUAR */
12057 { PCI_VENDOR_ID_DELL, 0x0109, PHY_ID_BCM5411 }, /* MERLOT */
12058 { PCI_VENDOR_ID_DELL, 0x010a, PHY_ID_BCM5411 }, /* SLIM_MERLOT */
12059
12060 /* Compaq boards. */
12061 { PCI_VENDOR_ID_COMPAQ, 0x007c, PHY_ID_BCM5701 }, /* BANSHEE */
12062 { PCI_VENDOR_ID_COMPAQ, 0x009a, PHY_ID_BCM5701 }, /* BANSHEE_2 */
12063 { PCI_VENDOR_ID_COMPAQ, 0x007d, 0 }, /* CHANGELING */
12064 { PCI_VENDOR_ID_COMPAQ, 0x0085, PHY_ID_BCM5701 }, /* NC7780 */
12065 { PCI_VENDOR_ID_COMPAQ, 0x0099, PHY_ID_BCM5701 }, /* NC7780_2 */
12066
12067 /* IBM boards. */
12068 { PCI_VENDOR_ID_IBM, 0x0281, 0 } /* IBM??? */
12069};
12070
12071static inline struct subsys_tbl_ent *lookup_by_subsys(struct tg3 *tp)
12072{
12073 int i;
12074
12075 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
12076 if ((subsys_id_to_phy_id[i].subsys_vendor ==
12077 tp->pdev->subsystem_vendor) &&
12078 (subsys_id_to_phy_id[i].subsys_devid ==
12079 tp->pdev->subsystem_device))
12080 return &subsys_id_to_phy_id[i];
12081 }
12082 return NULL;
12083}
12084
Michael Chan7d0c41e2005-04-21 17:06:20 -070012085static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012086{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012087 u32 val;
Michael Chancaf636c72006-03-22 01:05:31 -080012088 u16 pmcsr;
12089
12090 /* On some early chips the SRAM cannot be accessed in D3hot state,
12091 * so need make sure we're in D0.
12092 */
12093 pci_read_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, &pmcsr);
12094 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
12095 pci_write_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, pmcsr);
12096 msleep(1);
Michael Chan7d0c41e2005-04-21 17:06:20 -070012097
12098 /* Make sure register accesses (indirect or otherwise)
12099 * will function correctly.
12100 */
12101 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
12102 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012103
David S. Millerf49639e2006-06-09 11:58:36 -070012104 /* The memory arbiter has to be enabled in order for SRAM accesses
12105 * to succeed. Normally on powerup the tg3 chip firmware will make
12106 * sure it is enabled, but other entities such as system netboot
12107 * code might disable it.
12108 */
12109 val = tr32(MEMARB_MODE);
12110 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
12111
Linus Torvalds1da177e2005-04-16 15:20:36 -070012112 tp->phy_id = PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012113 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12114
Gary Zambranoa85feb82007-05-05 11:52:19 -070012115 /* Assume an onboard device and WOL capable by default. */
12116 tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT | TG3_FLAG_WOL_CAP;
David S. Miller72b845e2006-03-14 14:11:48 -080012117
Michael Chanb5d37722006-09-27 16:06:21 -070012118 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080012119 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Michael Chanb5d37722006-09-27 16:06:21 -070012120 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080012121 tp->tg3_flags2 |= TG3_FLG2_IS_NIC;
12122 }
Matt Carlson0527ba32007-10-10 18:03:30 -070012123 val = tr32(VCPU_CFGSHDW);
12124 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Matt Carlson8ed5d972007-05-07 00:25:49 -070012125 tp->tg3_flags |= TG3_FLAG_ASPM_WORKAROUND;
Matt Carlson0527ba32007-10-10 18:03:30 -070012126 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Matt Carlson20232762008-12-21 20:18:56 -080012127 (val & VCPU_CFGSHDW_WOL_MAGPKT))
Matt Carlson0527ba32007-10-10 18:03:30 -070012128 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012129 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070012130 }
12131
Linus Torvalds1da177e2005-04-16 15:20:36 -070012132 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
12133 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
12134 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070012135 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012136 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012137
12138 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
12139 tp->nic_sram_data_cfg = nic_cfg;
12140
12141 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
12142 ver >>= NIC_SRAM_DATA_VER_SHIFT;
12143 if ((GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700) &&
12144 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) &&
12145 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703) &&
12146 (ver > 0) && (ver < 0x100))
12147 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
12148
Matt Carlsona9daf362008-05-25 23:49:44 -070012149 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
12150 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
12151
Linus Torvalds1da177e2005-04-16 15:20:36 -070012152 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
12153 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
12154 eeprom_phy_serdes = 1;
12155
12156 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
12157 if (nic_phy_id != 0) {
12158 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
12159 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
12160
12161 eeprom_phy_id = (id1 >> 16) << 10;
12162 eeprom_phy_id |= (id2 & 0xfc00) << 16;
12163 eeprom_phy_id |= (id2 & 0x03ff) << 0;
12164 } else
12165 eeprom_phy_id = 0;
12166
Michael Chan7d0c41e2005-04-21 17:06:20 -070012167 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070012168 if (eeprom_phy_serdes) {
Michael Chana4e2b342005-10-26 15:46:52 -070012169 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
Michael Chan747e8f82005-07-25 12:33:22 -070012170 tp->tg3_flags2 |= TG3_FLG2_MII_SERDES;
12171 else
12172 tp->tg3_flags2 |= TG3_FLG2_PHY_SERDES;
12173 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070012174
John W. Linvillecbf46852005-04-21 17:01:29 -070012175 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012176 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
12177 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070012178 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070012179 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
12180
12181 switch (led_cfg) {
12182 default:
12183 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
12184 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12185 break;
12186
12187 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
12188 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12189 break;
12190
12191 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
12192 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070012193
12194 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
12195 * read on some older 5700/5701 bootcode.
12196 */
12197 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
12198 ASIC_REV_5700 ||
12199 GET_ASIC_REV(tp->pci_chip_rev_id) ==
12200 ASIC_REV_5701)
12201 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12202
Linus Torvalds1da177e2005-04-16 15:20:36 -070012203 break;
12204
12205 case SHASTA_EXT_LED_SHARED:
12206 tp->led_ctrl = LED_CTRL_MODE_SHARED;
12207 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
12208 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
12209 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12210 LED_CTRL_MODE_PHY_2);
12211 break;
12212
12213 case SHASTA_EXT_LED_MAC:
12214 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
12215 break;
12216
12217 case SHASTA_EXT_LED_COMBO:
12218 tp->led_ctrl = LED_CTRL_MODE_COMBO;
12219 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
12220 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12221 LED_CTRL_MODE_PHY_2);
12222 break;
12223
Stephen Hemminger855e1112008-04-16 16:37:28 -070012224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012225
12226 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
12227 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
12228 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
12229 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12230
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012231 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
12232 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080012233
Michael Chan9d26e212006-12-07 00:21:14 -080012234 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012235 tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080012236 if ((tp->pdev->subsystem_vendor ==
12237 PCI_VENDOR_ID_ARIMA) &&
12238 (tp->pdev->subsystem_device == 0x205a ||
12239 tp->pdev->subsystem_device == 0x2063))
12240 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
12241 } else {
David S. Millerf49639e2006-06-09 11:58:36 -070012242 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080012243 tp->tg3_flags2 |= TG3_FLG2_IS_NIC;
12244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012245
12246 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
12247 tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
John W. Linvillecbf46852005-04-21 17:01:29 -070012248 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012249 tp->tg3_flags2 |= TG3_FLG2_ASF_NEW_HANDSHAKE;
12250 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012251
12252 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
12253 (tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
Matt Carlson0d3031d2007-10-10 18:02:43 -070012254 tp->tg3_flags3 |= TG3_FLG3_ENABLE_APE;
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012255
Gary Zambranoa85feb82007-05-05 11:52:19 -070012256 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES &&
12257 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
12258 tp->tg3_flags &= ~TG3_FLAG_WOL_CAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012259
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070012260 if ((tp->tg3_flags & TG3_FLAG_WOL_CAP) &&
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012261 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE))
Matt Carlson0527ba32007-10-10 18:03:30 -070012262 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
12263
Linus Torvalds1da177e2005-04-16 15:20:36 -070012264 if (cfg2 & (1 << 17))
12265 tp->tg3_flags2 |= TG3_FLG2_CAPACITIVE_COUPLING;
12266
12267 /* serdes signal pre-emphasis in register 0x590 set by */
12268 /* bootcode if bit 18 is set */
12269 if (cfg2 & (1 << 18))
12270 tp->tg3_flags2 |= TG3_FLG2_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070012271
Matt Carlson321d32a2008-11-21 17:22:19 -080012272 if (((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
12273 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080012274 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
12275 tp->tg3_flags3 |= TG3_FLG3_PHY_ENABLE_APD;
12276
Matt Carlson8ed5d972007-05-07 00:25:49 -070012277 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
12278 u32 cfg3;
12279
12280 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
12281 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
12282 tp->tg3_flags |= TG3_FLAG_ASPM_WORKAROUND;
12283 }
Matt Carlsona9daf362008-05-25 23:49:44 -070012284
12285 if (cfg4 & NIC_SRAM_RGMII_STD_IBND_DISABLE)
12286 tp->tg3_flags3 |= TG3_FLG3_RGMII_STD_IBND_DISABLE;
12287 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
12288 tp->tg3_flags3 |= TG3_FLG3_RGMII_EXT_IBND_RX_EN;
12289 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
12290 tp->tg3_flags3 |= TG3_FLG3_RGMII_EXT_IBND_TX_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012291 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012292done:
12293 device_init_wakeup(&tp->pdev->dev, tp->tg3_flags & TG3_FLAG_WOL_CAP);
12294 device_set_wakeup_enable(&tp->pdev->dev,
12295 tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
Michael Chan7d0c41e2005-04-21 17:06:20 -070012296}
12297
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012298static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
12299{
12300 int i;
12301 u32 val;
12302
12303 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
12304 tw32(OTP_CTRL, cmd);
12305
12306 /* Wait for up to 1 ms for command to execute. */
12307 for (i = 0; i < 100; i++) {
12308 val = tr32(OTP_STATUS);
12309 if (val & OTP_STATUS_CMD_DONE)
12310 break;
12311 udelay(10);
12312 }
12313
12314 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
12315}
12316
12317/* Read the gphy configuration from the OTP region of the chip. The gphy
12318 * configuration is a 32-bit value that straddles the alignment boundary.
12319 * We do two 32-bit reads and then shift and merge the results.
12320 */
12321static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
12322{
12323 u32 bhalf_otp, thalf_otp;
12324
12325 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
12326
12327 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
12328 return 0;
12329
12330 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
12331
12332 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12333 return 0;
12334
12335 thalf_otp = tr32(OTP_READ_DATA);
12336
12337 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
12338
12339 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12340 return 0;
12341
12342 bhalf_otp = tr32(OTP_READ_DATA);
12343
12344 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
12345}
12346
Michael Chan7d0c41e2005-04-21 17:06:20 -070012347static int __devinit tg3_phy_probe(struct tg3 *tp)
12348{
12349 u32 hw_phy_id_1, hw_phy_id_2;
12350 u32 hw_phy_id, hw_phy_id_masked;
12351 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012352
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012353 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)
12354 return tg3_phy_init(tp);
12355
Linus Torvalds1da177e2005-04-16 15:20:36 -070012356 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010012357 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012358 */
12359 err = 0;
Matt Carlson0d3031d2007-10-10 18:02:43 -070012360 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
12361 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012362 hw_phy_id = hw_phy_id_masked = PHY_ID_INVALID;
12363 } else {
12364 /* Now read the physical PHY_ID from the chip and verify
12365 * that it is sane. If it doesn't look good, we fall back
12366 * to either the hard-coded table based PHY_ID and failing
12367 * that the value found in the eeprom area.
12368 */
12369 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
12370 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
12371
12372 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
12373 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
12374 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
12375
12376 hw_phy_id_masked = hw_phy_id & PHY_ID_MASK;
12377 }
12378
12379 if (!err && KNOWN_PHY_ID(hw_phy_id_masked)) {
12380 tp->phy_id = hw_phy_id;
12381 if (hw_phy_id_masked == PHY_ID_BCM8002)
12382 tp->tg3_flags2 |= TG3_FLG2_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070012383 else
12384 tp->tg3_flags2 &= ~TG3_FLG2_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012385 } else {
Michael Chan7d0c41e2005-04-21 17:06:20 -070012386 if (tp->phy_id != PHY_ID_INVALID) {
12387 /* Do nothing, phy ID already set up in
12388 * tg3_get_eeprom_hw_cfg().
12389 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070012390 } else {
12391 struct subsys_tbl_ent *p;
12392
12393 /* No eeprom signature? Try the hardcoded
12394 * subsys device table.
12395 */
12396 p = lookup_by_subsys(tp);
12397 if (!p)
12398 return -ENODEV;
12399
12400 tp->phy_id = p->phy_id;
12401 if (!tp->phy_id ||
12402 tp->phy_id == PHY_ID_BCM8002)
12403 tp->tg3_flags2 |= TG3_FLG2_PHY_SERDES;
12404 }
12405 }
12406
Michael Chan747e8f82005-07-25 12:33:22 -070012407 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES) &&
Matt Carlson0d3031d2007-10-10 18:02:43 -070012408 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070012409 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chan3600d912006-12-07 00:21:48 -080012410 u32 bmsr, adv_reg, tg3_ctrl, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012411
12412 tg3_readphy(tp, MII_BMSR, &bmsr);
12413 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
12414 (bmsr & BMSR_LSTATUS))
12415 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012416
Linus Torvalds1da177e2005-04-16 15:20:36 -070012417 err = tg3_phy_reset(tp);
12418 if (err)
12419 return err;
12420
12421 adv_reg = (ADVERTISE_10HALF | ADVERTISE_10FULL |
12422 ADVERTISE_100HALF | ADVERTISE_100FULL |
12423 ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
12424 tg3_ctrl = 0;
12425 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY)) {
12426 tg3_ctrl = (MII_TG3_CTRL_ADV_1000_HALF |
12427 MII_TG3_CTRL_ADV_1000_FULL);
12428 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
12429 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
12430 tg3_ctrl |= (MII_TG3_CTRL_AS_MASTER |
12431 MII_TG3_CTRL_ENABLE_AS_MASTER);
12432 }
12433
Michael Chan3600d912006-12-07 00:21:48 -080012434 mask = (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
12435 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
12436 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full);
12437 if (!tg3_copper_is_advertising_all(tp, mask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012438 tg3_writephy(tp, MII_ADVERTISE, adv_reg);
12439
12440 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY))
12441 tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
12442
12443 tg3_writephy(tp, MII_BMCR,
12444 BMCR_ANENABLE | BMCR_ANRESTART);
12445 }
12446 tg3_phy_set_wirespeed(tp);
12447
12448 tg3_writephy(tp, MII_ADVERTISE, adv_reg);
12449 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY))
12450 tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
12451 }
12452
12453skip_phy_reset:
12454 if ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5401) {
12455 err = tg3_init_5401phy_dsp(tp);
12456 if (err)
12457 return err;
12458 }
12459
12460 if (!err && ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5401)) {
12461 err = tg3_init_5401phy_dsp(tp);
12462 }
12463
Michael Chan747e8f82005-07-25 12:33:22 -070012464 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012465 tp->link_config.advertising =
12466 (ADVERTISED_1000baseT_Half |
12467 ADVERTISED_1000baseT_Full |
12468 ADVERTISED_Autoneg |
12469 ADVERTISED_FIBRE);
12470 if (tp->tg3_flags & TG3_FLAG_10_100_ONLY)
12471 tp->link_config.advertising &=
12472 ~(ADVERTISED_1000baseT_Half |
12473 ADVERTISED_1000baseT_Full);
12474
12475 return err;
12476}
12477
12478static void __devinit tg3_read_partno(struct tg3 *tp)
12479{
Matt Carlson141518c2009-12-03 08:36:22 +000012480 unsigned char vpd_data[TG3_NVM_VPD_LEN]; /* in little-endian format */
Michael Chanaf2c6a42006-11-07 14:57:51 -080012481 unsigned int i;
Michael Chan1b277772006-03-20 22:27:48 -080012482 u32 magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012483
Matt Carlsondf259d82009-04-20 06:57:14 +000012484 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
12485 tg3_nvram_read(tp, 0x0, &magic))
David S. Millerf49639e2006-06-09 11:58:36 -070012486 goto out_not_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012487
Michael Chan18201802006-03-20 22:29:15 -080012488 if (magic == TG3_EEPROM_MAGIC) {
Matt Carlson141518c2009-12-03 08:36:22 +000012489 for (i = 0; i < TG3_NVM_VPD_LEN; i += 4) {
Michael Chan1b277772006-03-20 22:27:48 -080012490 u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012491
Matt Carlson6d348f22009-02-25 14:25:52 +000012492 /* The data is in little-endian format in NVRAM.
12493 * Use the big-endian read routines to preserve
12494 * the byte order as it exists in NVRAM.
12495 */
Matt Carlson141518c2009-12-03 08:36:22 +000012496 if (tg3_nvram_read_be32(tp, TG3_NVM_VPD_OFF + i, &tmp))
Michael Chan1b277772006-03-20 22:27:48 -080012497 goto out_not_found;
12498
Matt Carlson6d348f22009-02-25 14:25:52 +000012499 memcpy(&vpd_data[i], &tmp, sizeof(tmp));
Michael Chan1b277772006-03-20 22:27:48 -080012500 }
12501 } else {
Matt Carlson94c982b2009-12-03 08:36:23 +000012502 ssize_t cnt;
12503 unsigned int pos = 0, i = 0;
Michael Chan1b277772006-03-20 22:27:48 -080012504
Matt Carlson94c982b2009-12-03 08:36:23 +000012505 for (; pos < TG3_NVM_VPD_LEN && i < 3; i++, pos += cnt) {
12506 cnt = pci_read_vpd(tp->pdev, pos,
12507 TG3_NVM_VPD_LEN - pos,
12508 &vpd_data[pos]);
12509 if (cnt == -ETIMEDOUT || -EINTR)
12510 cnt = 0;
12511 else if (cnt < 0)
David S. Millerf49639e2006-06-09 11:58:36 -070012512 goto out_not_found;
Michael Chan1b277772006-03-20 22:27:48 -080012513 }
Matt Carlson94c982b2009-12-03 08:36:23 +000012514 if (pos != TG3_NVM_VPD_LEN)
12515 goto out_not_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012516 }
12517
12518 /* Now parse and find the part number. */
Matt Carlson141518c2009-12-03 08:36:22 +000012519 for (i = 0; i < TG3_NVM_VPD_LEN - 2; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012520 unsigned char val = vpd_data[i];
Michael Chanaf2c6a42006-11-07 14:57:51 -080012521 unsigned int block_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012522
12523 if (val == 0x82 || val == 0x91) {
12524 i = (i + 3 +
12525 (vpd_data[i + 1] +
12526 (vpd_data[i + 2] << 8)));
12527 continue;
12528 }
12529
12530 if (val != 0x90)
12531 goto out_not_found;
12532
12533 block_end = (i + 3 +
12534 (vpd_data[i + 1] +
12535 (vpd_data[i + 2] << 8)));
12536 i += 3;
Michael Chanaf2c6a42006-11-07 14:57:51 -080012537
Matt Carlson141518c2009-12-03 08:36:22 +000012538 if (block_end > TG3_NVM_VPD_LEN)
Michael Chanaf2c6a42006-11-07 14:57:51 -080012539 goto out_not_found;
12540
12541 while (i < (block_end - 2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012542 if (vpd_data[i + 0] == 'P' &&
12543 vpd_data[i + 1] == 'N') {
12544 int partno_len = vpd_data[i + 2];
12545
Michael Chanaf2c6a42006-11-07 14:57:51 -080012546 i += 3;
Matt Carlson141518c2009-12-03 08:36:22 +000012547 if (partno_len > TG3_BPN_SIZE ||
12548 (partno_len + i) > TG3_NVM_VPD_LEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012549 goto out_not_found;
12550
12551 memcpy(tp->board_part_number,
Michael Chanaf2c6a42006-11-07 14:57:51 -080012552 &vpd_data[i], partno_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012553
12554 /* Success. */
12555 return;
12556 }
Michael Chanaf2c6a42006-11-07 14:57:51 -080012557 i += 3 + vpd_data[i + 2];
Linus Torvalds1da177e2005-04-16 15:20:36 -070012558 }
12559
12560 /* Part number not found. */
12561 goto out_not_found;
12562 }
12563
12564out_not_found:
Michael Chanb5d37722006-09-27 16:06:21 -070012565 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12566 strcpy(tp->board_part_number, "BCM95906");
Matt Carlsondf259d82009-04-20 06:57:14 +000012567 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12568 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
12569 strcpy(tp->board_part_number, "BCM57780");
12570 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12571 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
12572 strcpy(tp->board_part_number, "BCM57760");
12573 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12574 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
12575 strcpy(tp->board_part_number, "BCM57790");
Matt Carlson5e7ccf22009-08-25 10:08:42 +000012576 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12577 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
12578 strcpy(tp->board_part_number, "BCM57788");
Matt Carlsonb703df62009-12-03 08:36:21 +000012579 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
12580 strcpy(tp->board_part_number, "BCM57765");
Michael Chanb5d37722006-09-27 16:06:21 -070012581 else
12582 strcpy(tp->board_part_number, "none");
Linus Torvalds1da177e2005-04-16 15:20:36 -070012583}
12584
Matt Carlson9c8a6202007-10-21 16:16:08 -070012585static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
12586{
12587 u32 val;
12588
Matt Carlsone4f34112009-02-25 14:25:00 +000012589 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070012590 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000012591 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070012592 val != 0)
12593 return 0;
12594
12595 return 1;
12596}
12597
Matt Carlsonacd9c112009-02-25 14:26:33 +000012598static void __devinit tg3_read_bc_ver(struct tg3 *tp)
12599{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012600 u32 val, offset, start, ver_offset;
Matt Carlsonacd9c112009-02-25 14:26:33 +000012601 int i;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012602 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000012603
12604 if (tg3_nvram_read(tp, 0xc, &offset) ||
12605 tg3_nvram_read(tp, 0x4, &start))
12606 return;
12607
12608 offset = tg3_nvram_logical_addr(tp, offset);
12609
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012610 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000012611 return;
12612
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012613 if ((val & 0xfc000000) == 0x0c000000) {
12614 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000012615 return;
12616
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012617 if (val == 0)
12618 newver = true;
12619 }
12620
12621 if (newver) {
12622 if (tg3_nvram_read(tp, offset + 8, &ver_offset))
12623 return;
12624
12625 offset = offset + ver_offset - start;
12626 for (i = 0; i < 16; i += 4) {
12627 __be32 v;
12628 if (tg3_nvram_read_be32(tp, offset + i, &v))
12629 return;
12630
12631 memcpy(tp->fw_ver + i, &v, sizeof(v));
12632 }
12633 } else {
12634 u32 major, minor;
12635
12636 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
12637 return;
12638
12639 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
12640 TG3_NVM_BCVER_MAJSFT;
12641 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
12642 snprintf(&tp->fw_ver[0], 32, "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000012643 }
12644}
12645
Matt Carlsona6f6cb12009-02-25 14:27:43 +000012646static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
12647{
12648 u32 val, major, minor;
12649
12650 /* Use native endian representation */
12651 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
12652 return;
12653
12654 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
12655 TG3_NVM_HWSB_CFG1_MAJSFT;
12656 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
12657 TG3_NVM_HWSB_CFG1_MINSFT;
12658
12659 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
12660}
12661
Matt Carlsondfe00d72008-11-21 17:19:41 -080012662static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
12663{
12664 u32 offset, major, minor, build;
12665
12666 tp->fw_ver[0] = 's';
12667 tp->fw_ver[1] = 'b';
12668 tp->fw_ver[2] = '\0';
12669
12670 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
12671 return;
12672
12673 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
12674 case TG3_EEPROM_SB_REVISION_0:
12675 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
12676 break;
12677 case TG3_EEPROM_SB_REVISION_2:
12678 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
12679 break;
12680 case TG3_EEPROM_SB_REVISION_3:
12681 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
12682 break;
12683 default:
12684 return;
12685 }
12686
Matt Carlsone4f34112009-02-25 14:25:00 +000012687 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080012688 return;
12689
12690 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
12691 TG3_EEPROM_SB_EDH_BLD_SHFT;
12692 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
12693 TG3_EEPROM_SB_EDH_MAJ_SHFT;
12694 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
12695
12696 if (minor > 99 || build > 26)
12697 return;
12698
12699 snprintf(&tp->fw_ver[2], 30, " v%d.%02d", major, minor);
12700
12701 if (build > 0) {
12702 tp->fw_ver[8] = 'a' + build - 1;
12703 tp->fw_ver[9] = '\0';
12704 }
12705}
12706
Matt Carlsonacd9c112009-02-25 14:26:33 +000012707static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080012708{
12709 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000012710 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070012711
12712 for (offset = TG3_NVM_DIR_START;
12713 offset < TG3_NVM_DIR_END;
12714 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000012715 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012716 return;
12717
12718 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
12719 break;
12720 }
12721
12722 if (offset == TG3_NVM_DIR_END)
12723 return;
12724
12725 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
12726 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000012727 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012728 return;
12729
Matt Carlsone4f34112009-02-25 14:25:00 +000012730 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070012731 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000012732 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012733 return;
12734
12735 offset += val - start;
12736
Matt Carlsonacd9c112009-02-25 14:26:33 +000012737 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012738
Matt Carlsonacd9c112009-02-25 14:26:33 +000012739 tp->fw_ver[vlen++] = ',';
12740 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070012741
12742 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012743 __be32 v;
12744 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012745 return;
12746
Al Virob9fc7dc2007-12-17 22:59:57 -080012747 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012748
Matt Carlsonacd9c112009-02-25 14:26:33 +000012749 if (vlen > TG3_VER_SIZE - sizeof(v)) {
12750 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012751 break;
12752 }
12753
Matt Carlsonacd9c112009-02-25 14:26:33 +000012754 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
12755 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012756 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000012757}
12758
Matt Carlson7fd76442009-02-25 14:27:20 +000012759static void __devinit tg3_read_dash_ver(struct tg3 *tp)
12760{
12761 int vlen;
12762 u32 apedata;
12763
12764 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) ||
12765 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
12766 return;
12767
12768 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
12769 if (apedata != APE_SEG_SIG_MAGIC)
12770 return;
12771
12772 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
12773 if (!(apedata & APE_FW_STATUS_READY))
12774 return;
12775
12776 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
12777
12778 vlen = strlen(tp->fw_ver);
12779
12780 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " DASH v%d.%d.%d.%d",
12781 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
12782 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
12783 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
12784 (apedata & APE_FW_VERSION_BLDMSK));
12785}
12786
Matt Carlsonacd9c112009-02-25 14:26:33 +000012787static void __devinit tg3_read_fw_ver(struct tg3 *tp)
12788{
12789 u32 val;
12790
Matt Carlsondf259d82009-04-20 06:57:14 +000012791 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) {
12792 tp->fw_ver[0] = 's';
12793 tp->fw_ver[1] = 'b';
12794 tp->fw_ver[2] = '\0';
12795
12796 return;
12797 }
12798
Matt Carlsonacd9c112009-02-25 14:26:33 +000012799 if (tg3_nvram_read(tp, 0, &val))
12800 return;
12801
12802 if (val == TG3_EEPROM_MAGIC)
12803 tg3_read_bc_ver(tp);
12804 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
12805 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000012806 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
12807 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000012808 else
12809 return;
12810
12811 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
12812 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
12813 return;
12814
12815 tg3_read_mgmtfw_ver(tp);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012816
12817 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080012818}
12819
Michael Chan7544b092007-05-05 13:08:32 -070012820static struct pci_dev * __devinit tg3_find_peer(struct tg3 *);
12821
Linus Torvalds1da177e2005-04-16 15:20:36 -070012822static int __devinit tg3_get_invariants(struct tg3 *tp)
12823{
12824 static struct pci_device_id write_reorder_chipsets[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012825 { PCI_DEVICE(PCI_VENDOR_ID_AMD,
12826 PCI_DEVICE_ID_AMD_FE_GATE_700C) },
John W. Linvillec165b002006-07-08 13:28:53 -070012827 { PCI_DEVICE(PCI_VENDOR_ID_AMD,
12828 PCI_DEVICE_ID_AMD_8131_BRIDGE) },
Michael Chan399de502005-10-03 14:02:39 -070012829 { PCI_DEVICE(PCI_VENDOR_ID_VIA,
12830 PCI_DEVICE_ID_VIA_8385_0) },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012831 { },
12832 };
12833 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012834 u32 pci_state_reg, grc_misc_cfg;
12835 u32 val;
12836 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080012837 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012838
Linus Torvalds1da177e2005-04-16 15:20:36 -070012839 /* Force memory write invalidate off. If we leave it on,
12840 * then on 5700_BX chips we have to enable a workaround.
12841 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
12842 * to match the cacheline size. The Broadcom driver have this
12843 * workaround but turns MWI off all the times so never uses
12844 * it. This seems to suggest that the workaround is insufficient.
12845 */
12846 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
12847 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
12848 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
12849
12850 /* It is absolutely critical that TG3PCI_MISC_HOST_CTRL
12851 * has the register indirect write enable bit set before
12852 * we try to access any of the MMIO registers. It is also
12853 * critical that the PCI-X hw workaround situation is decided
12854 * before that as well.
12855 */
12856 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
12857 &misc_ctrl_reg);
12858
12859 tp->pci_chip_rev_id = (misc_ctrl_reg >>
12860 MISC_HOST_CTRL_CHIPREV_SHIFT);
Matt Carlson795d01c2007-10-07 23:28:17 -070012861 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
12862 u32 prod_id_asic_rev;
12863
Matt Carlson5001e2f2009-11-13 13:03:51 +000012864 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
12865 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
12866 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5724)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012867 pci_read_config_dword(tp->pdev,
12868 TG3PCI_GEN2_PRODID_ASICREV,
12869 &prod_id_asic_rev);
Matt Carlsonb703df62009-12-03 08:36:21 +000012870 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
12871 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
12872 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
12873 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
12874 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
12875 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
12876 pci_read_config_dword(tp->pdev,
12877 TG3PCI_GEN15_PRODID_ASICREV,
12878 &prod_id_asic_rev);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012879 else
12880 pci_read_config_dword(tp->pdev, TG3PCI_PRODID_ASICREV,
12881 &prod_id_asic_rev);
12882
Matt Carlson321d32a2008-11-21 17:22:19 -080012883 tp->pci_chip_rev_id = prod_id_asic_rev;
Matt Carlson795d01c2007-10-07 23:28:17 -070012884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012885
Michael Chanff645be2005-04-21 17:09:53 -070012886 /* Wrong chip ID in 5752 A0. This code can be removed later
12887 * as A0 is not in production.
12888 */
12889 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
12890 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
12891
Michael Chan68929142005-08-09 20:17:14 -070012892 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
12893 * we need to disable memory and use config. cycles
12894 * only to access all registers. The 5702/03 chips
12895 * can mistakenly decode the special cycles from the
12896 * ICH chipsets as memory write cycles, causing corruption
12897 * of register and memory space. Only certain ICH bridges
12898 * will drive special cycles with non-zero data during the
12899 * address phase which can fall within the 5703's address
12900 * range. This is not an ICH bug as the PCI spec allows
12901 * non-zero address during special cycles. However, only
12902 * these ICH bridges are known to drive non-zero addresses
12903 * during special cycles.
12904 *
12905 * Since special cycles do not cross PCI bridges, we only
12906 * enable this workaround if the 5703 is on the secondary
12907 * bus of these ICH bridges.
12908 */
12909 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
12910 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
12911 static struct tg3_dev_id {
12912 u32 vendor;
12913 u32 device;
12914 u32 rev;
12915 } ich_chipsets[] = {
12916 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
12917 PCI_ANY_ID },
12918 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
12919 PCI_ANY_ID },
12920 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
12921 0xa },
12922 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
12923 PCI_ANY_ID },
12924 { },
12925 };
12926 struct tg3_dev_id *pci_id = &ich_chipsets[0];
12927 struct pci_dev *bridge = NULL;
12928
12929 while (pci_id->vendor != 0) {
12930 bridge = pci_get_device(pci_id->vendor, pci_id->device,
12931 bridge);
12932 if (!bridge) {
12933 pci_id++;
12934 continue;
12935 }
12936 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070012937 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070012938 continue;
12939 }
12940 if (bridge->subordinate &&
12941 (bridge->subordinate->number ==
12942 tp->pdev->bus->number)) {
12943
12944 tp->tg3_flags2 |= TG3_FLG2_ICH_WORKAROUND;
12945 pci_dev_put(bridge);
12946 break;
12947 }
12948 }
12949 }
12950
Matt Carlson41588ba12008-04-19 18:12:33 -070012951 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
12952 static struct tg3_dev_id {
12953 u32 vendor;
12954 u32 device;
12955 } bridge_chipsets[] = {
12956 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
12957 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
12958 { },
12959 };
12960 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
12961 struct pci_dev *bridge = NULL;
12962
12963 while (pci_id->vendor != 0) {
12964 bridge = pci_get_device(pci_id->vendor,
12965 pci_id->device,
12966 bridge);
12967 if (!bridge) {
12968 pci_id++;
12969 continue;
12970 }
12971 if (bridge->subordinate &&
12972 (bridge->subordinate->number <=
12973 tp->pdev->bus->number) &&
12974 (bridge->subordinate->subordinate >=
12975 tp->pdev->bus->number)) {
12976 tp->tg3_flags3 |= TG3_FLG3_5701_DMA_BUG;
12977 pci_dev_put(bridge);
12978 break;
12979 }
12980 }
12981 }
12982
Michael Chan4a29cc22006-03-19 13:21:12 -080012983 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
12984 * DMA addresses > 40-bit. This bridge may have other additional
12985 * 57xx devices behind it in some 4-port NIC designs for example.
12986 * Any tg3 device found behind the bridge will also need the 40-bit
12987 * DMA workaround.
12988 */
Michael Chana4e2b342005-10-26 15:46:52 -070012989 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
12990 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
12991 tp->tg3_flags2 |= TG3_FLG2_5780_CLASS;
Michael Chan4a29cc22006-03-19 13:21:12 -080012992 tp->tg3_flags |= TG3_FLAG_40BIT_DMA_BUG;
Michael Chan4cf78e42005-07-25 12:29:19 -070012993 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Michael Chana4e2b342005-10-26 15:46:52 -070012994 }
Michael Chan4a29cc22006-03-19 13:21:12 -080012995 else {
12996 struct pci_dev *bridge = NULL;
12997
12998 do {
12999 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
13000 PCI_DEVICE_ID_SERVERWORKS_EPB,
13001 bridge);
13002 if (bridge && bridge->subordinate &&
13003 (bridge->subordinate->number <=
13004 tp->pdev->bus->number) &&
13005 (bridge->subordinate->subordinate >=
13006 tp->pdev->bus->number)) {
13007 tp->tg3_flags |= TG3_FLAG_40BIT_DMA_BUG;
13008 pci_dev_put(bridge);
13009 break;
13010 }
13011 } while (bridge);
13012 }
Michael Chan4cf78e42005-07-25 12:29:19 -070013013
Linus Torvalds1da177e2005-04-16 15:20:36 -070013014 /* Initialize misc host control in PCI block. */
13015 tp->misc_host_ctrl |= (misc_ctrl_reg &
13016 MISC_HOST_CTRL_CHIPREV);
13017 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13018 tp->misc_host_ctrl);
13019
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013020 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
13021 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
13022 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Michael Chan7544b092007-05-05 13:08:32 -070013023 tp->pdev_peer = tg3_find_peer(tp);
13024
Matt Carlson321d32a2008-11-21 17:22:19 -080013025 /* Intentionally exclude ASIC_REV_5906 */
13026 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Michael Chand9ab5ad12006-03-20 22:27:35 -080013027 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013028 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013029 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013030 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013031 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlsonb703df62009-12-03 08:36:21 +000013032 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13033 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson321d32a2008-11-21 17:22:19 -080013034 tp->tg3_flags3 |= TG3_FLG3_5755_PLUS;
13035
13036 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13037 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Michael Chanb5d37722006-09-27 16:06:21 -070013038 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013039 (tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
Michael Chana4e2b342005-10-26 15:46:52 -070013040 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
John W. Linville6708e5c2005-04-21 17:00:52 -070013041 tp->tg3_flags2 |= TG3_FLG2_5750_PLUS;
13042
John W. Linville1b440c562005-04-21 17:03:18 -070013043 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) ||
13044 (tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
13045 tp->tg3_flags2 |= TG3_FLG2_5705_PLUS;
13046
Matt Carlson027455a2008-12-21 20:19:30 -080013047 /* 5700 B0 chips do not support checksumming correctly due
13048 * to hardware bugs.
13049 */
13050 if (tp->pci_chip_rev_id == CHIPREV_ID_5700_B0)
13051 tp->tg3_flags |= TG3_FLAG_BROKEN_CHECKSUMS;
13052 else {
13053 tp->tg3_flags |= TG3_FLAG_RX_CHECKSUMS;
13054 tp->dev->features |= NETIF_F_IP_CSUM | NETIF_F_SG;
13055 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
13056 tp->dev->features |= NETIF_F_IPV6_CSUM;
13057 }
13058
Matt Carlson507399f2009-11-13 13:03:37 +000013059 /* Determine TSO capabilities */
Matt Carlsonb703df62009-12-03 08:36:21 +000013060 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13061 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlsone849cdc2009-11-13 13:03:38 +000013062 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_3;
13063 else if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
13064 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlson507399f2009-11-13 13:03:37 +000013065 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_2;
13066 else if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
13067 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_1 | TG3_FLG2_TSO_BUG;
13068 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
13069 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
13070 tp->tg3_flags2 &= ~TG3_FLG2_TSO_BUG;
13071 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13072 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13073 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
13074 tp->tg3_flags2 |= TG3_FLG2_TSO_BUG;
13075 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
13076 tp->fw_needed = FIRMWARE_TG3TSO5;
13077 else
13078 tp->fw_needed = FIRMWARE_TG3TSO;
13079 }
13080
13081 tp->irq_max = 1;
13082
Michael Chan5a6f3072006-03-20 22:28:05 -080013083 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
Michael Chan7544b092007-05-05 13:08:32 -070013084 tp->tg3_flags |= TG3_FLAG_SUPPORT_MSI;
13085 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
13086 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
13087 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
13088 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
13089 tp->pdev_peer == tp->pdev))
13090 tp->tg3_flags &= ~TG3_FLAG_SUPPORT_MSI;
13091
Matt Carlson321d32a2008-11-21 17:22:19 -080013092 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070013093 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanfcfa0a32006-03-20 22:28:41 -080013094 tp->tg3_flags2 |= TG3_FLG2_1SHOT_MSI;
Michael Chan52c0fd82006-06-29 20:15:54 -070013095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013096
Matt Carlsonb703df62009-12-03 08:36:21 +000013097 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13098 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
Matt Carlson507399f2009-11-13 13:03:37 +000013099 tp->tg3_flags |= TG3_FLAG_SUPPORT_MSIX;
13100 tp->irq_max = TG3_IRQ_MAX_VECS;
13101 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013102 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000013103
Matt Carlson615774f2009-11-13 13:03:39 +000013104 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13105 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
13106 tp->tg3_flags3 |= TG3_FLG3_SHORT_DMA_BUG;
13107 else if (!(tp->tg3_flags3 & TG3_FLG3_5755_PLUS)) {
13108 tp->tg3_flags3 |= TG3_FLG3_4G_DMA_BNDRY_BUG;
13109 tp->tg3_flags3 |= TG3_FLG3_40BIT_DMA_LIMIT_BUG;
Matt Carlson0e1406d2009-11-02 12:33:33 +000013110 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013111
Matt Carlsonb703df62009-12-03 08:36:21 +000013112 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13113 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
13114 tp->tg3_flags3 |= TG3_FLG3_USE_JUMBO_BDFLAG;
13115
Matt Carlsonf51f3562008-05-25 23:45:08 -070013116 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013117 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
Matt Carlsonb703df62009-12-03 08:36:21 +000013118 (tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG))
Matt Carlson8f666b02009-08-28 13:58:24 +000013119 tp->tg3_flags |= TG3_FLAG_JUMBO_CAPABLE;
Michael Chan0f893dc2005-07-25 12:30:38 -070013120
Matt Carlson52f44902008-11-21 17:17:04 -080013121 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
13122 &pci_state_reg);
13123
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013124 tp->pcie_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_EXP);
13125 if (tp->pcie_cap != 0) {
13126 u16 lnkctl;
13127
Linus Torvalds1da177e2005-04-16 15:20:36 -070013128 tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS;
Matt Carlson5f5c51e2007-11-12 21:19:37 -080013129
13130 pcie_set_readrq(tp->pdev, 4096);
13131
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013132 pci_read_config_word(tp->pdev,
13133 tp->pcie_cap + PCI_EXP_LNKCTL,
13134 &lnkctl);
13135 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
13136 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanc7835a72006-11-15 21:14:42 -080013137 tp->tg3_flags2 &= ~TG3_FLG2_HW_TSO_2;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013138 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013139 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000013140 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
13141 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013142 tp->tg3_flags3 |= TG3_FLG3_CLKREQ_BUG;
Matt Carlson614b0592010-01-20 16:58:02 +000013143 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
13144 tp->tg3_flags3 |= TG3_FLG3_L1PLLPD_EN;
Michael Chanc7835a72006-11-15 21:14:42 -080013145 }
Matt Carlson52f44902008-11-21 17:17:04 -080013146 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Matt Carlsonfcb389d2008-11-03 16:55:44 -080013147 tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS;
Matt Carlson52f44902008-11-21 17:17:04 -080013148 } else if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
13149 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
13150 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
13151 if (!tp->pcix_cap) {
13152 printk(KERN_ERR PFX "Cannot find PCI-X "
13153 "capability, aborting.\n");
13154 return -EIO;
13155 }
13156
13157 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
13158 tp->tg3_flags |= TG3_FLAG_PCIX_MODE;
13159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013160
Michael Chan399de502005-10-03 14:02:39 -070013161 /* If we have an AMD 762 or VIA K8T800 chipset, write
13162 * reordering to the mailbox registers done by the host
13163 * controller can cause major troubles. We read back from
13164 * every mailbox register write to force the writes to be
13165 * posted to the chip in order.
13166 */
13167 if (pci_dev_present(write_reorder_chipsets) &&
13168 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
13169 tp->tg3_flags |= TG3_FLAG_MBOX_WRITE_REORDER;
13170
Matt Carlson69fc4052008-12-21 20:19:57 -080013171 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
13172 &tp->pci_cacheline_sz);
13173 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13174 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013175 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
13176 tp->pci_lat_timer < 64) {
13177 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080013178 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13179 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013180 }
13181
Matt Carlson52f44902008-11-21 17:17:04 -080013182 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
13183 /* 5700 BX chips need to have their TX producer index
13184 * mailboxes written twice to workaround a bug.
13185 */
13186 tp->tg3_flags |= TG3_FLAG_TXD_MBOX_HWBUG;
Matt Carlson9974a352007-10-07 23:27:28 -070013187
Matt Carlson52f44902008-11-21 17:17:04 -080013188 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013189 *
13190 * The workaround is to use indirect register accesses
13191 * for all chip writes not to mailbox registers.
13192 */
Matt Carlson52f44902008-11-21 17:17:04 -080013193 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013194 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013195
13196 tp->tg3_flags |= TG3_FLAG_PCIX_TARGET_HWBUG;
13197
13198 /* The chip can have it's power management PCI config
13199 * space registers clobbered due to this bug.
13200 * So explicitly force the chip into D0 here.
13201 */
Matt Carlson9974a352007-10-07 23:27:28 -070013202 pci_read_config_dword(tp->pdev,
13203 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013204 &pm_reg);
13205 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
13206 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070013207 pci_write_config_dword(tp->pdev,
13208 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013209 pm_reg);
13210
13211 /* Also, force SERR#/PERR# in PCI command. */
13212 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13213 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
13214 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13215 }
13216 }
13217
Linus Torvalds1da177e2005-04-16 15:20:36 -070013218 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
13219 tp->tg3_flags |= TG3_FLAG_PCI_HIGH_SPEED;
13220 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
13221 tp->tg3_flags |= TG3_FLAG_PCI_32BIT;
13222
13223 /* Chip-specific fixup from Broadcom driver */
13224 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
13225 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
13226 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
13227 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
13228 }
13229
Michael Chan1ee582d2005-08-09 20:16:46 -070013230 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070013231 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013232 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070013233 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070013234 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013235 tp->write32_tx_mbox = tg3_write32;
13236 tp->write32_rx_mbox = tg3_write32;
13237
13238 /* Various workaround register access methods */
13239 if (tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG)
13240 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013241 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
13242 ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
13243 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
13244 /*
13245 * Back to back register writes can cause problems on these
13246 * chips, the workaround is to read back all reg writes
13247 * except those to mailbox regs.
13248 *
13249 * See tg3_write_indirect_reg32().
13250 */
Michael Chan1ee582d2005-08-09 20:16:46 -070013251 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013252 }
13253
Michael Chan1ee582d2005-08-09 20:16:46 -070013254 if ((tp->tg3_flags & TG3_FLAG_TXD_MBOX_HWBUG) ||
13255 (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)) {
13256 tp->write32_tx_mbox = tg3_write32_tx_mbox;
13257 if (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)
13258 tp->write32_rx_mbox = tg3_write_flush_reg32;
13259 }
Michael Chan20094932005-08-09 20:16:32 -070013260
Michael Chan68929142005-08-09 20:17:14 -070013261 if (tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND) {
13262 tp->read32 = tg3_read_indirect_reg32;
13263 tp->write32 = tg3_write_indirect_reg32;
13264 tp->read32_mbox = tg3_read_indirect_mbox;
13265 tp->write32_mbox = tg3_write_indirect_mbox;
13266 tp->write32_tx_mbox = tg3_write_indirect_mbox;
13267 tp->write32_rx_mbox = tg3_write_indirect_mbox;
13268
13269 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070013270 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070013271
13272 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13273 pci_cmd &= ~PCI_COMMAND_MEMORY;
13274 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13275 }
Michael Chanb5d37722006-09-27 16:06:21 -070013276 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
13277 tp->read32_mbox = tg3_read32_mbox_5906;
13278 tp->write32_mbox = tg3_write32_mbox_5906;
13279 tp->write32_tx_mbox = tg3_write32_mbox_5906;
13280 tp->write32_rx_mbox = tg3_write32_mbox_5906;
13281 }
Michael Chan68929142005-08-09 20:17:14 -070013282
Michael Chanbbadf502006-04-06 21:46:34 -070013283 if (tp->write32 == tg3_write_indirect_reg32 ||
13284 ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) &&
13285 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070013286 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Michael Chanbbadf502006-04-06 21:46:34 -070013287 tp->tg3_flags |= TG3_FLAG_SRAM_USE_CONFIG;
13288
Michael Chan7d0c41e2005-04-21 17:06:20 -070013289 /* Get eeprom hw config before calling tg3_set_power_state().
Michael Chan9d26e212006-12-07 00:21:14 -080013290 * In particular, the TG3_FLG2_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070013291 * determined before calling tg3_set_power_state() so that
13292 * we know whether or not to switch out of Vaux power.
13293 * When the flag is set, it means that GPIO1 is used for eeprom
13294 * write protect and also implies that it is a LOM where GPIOs
13295 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013296 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070013297 tg3_get_eeprom_hw_cfg(tp);
13298
Matt Carlson0d3031d2007-10-10 18:02:43 -070013299 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
13300 /* Allow reads and writes to the
13301 * APE register and memory space.
13302 */
13303 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
13304 PCISTATE_ALLOW_APE_SHMEM_WR;
13305 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
13306 pci_state_reg);
13307 }
13308
Matt Carlson9936bcf2007-10-10 18:03:07 -070013309 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013310 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013311 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013312 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlsonb703df62009-12-03 08:36:21 +000013313 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13314 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlsond30cdd22007-10-07 23:28:35 -070013315 tp->tg3_flags |= TG3_FLAG_CPMU_PRESENT;
13316
Michael Chan314fba32005-04-21 17:07:04 -070013317 /* Set up tp->grc_local_ctrl before calling tg3_set_power_state().
13318 * GPIO1 driven high will bring 5700's external PHY out of reset.
13319 * It is also used as eeprom write protect on LOMs.
13320 */
13321 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
13322 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
13323 (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT))
13324 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
13325 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070013326 /* Unused GPIO3 must be driven as output on 5752 because there
13327 * are no pull-up resistors on unused GPIO pins.
13328 */
13329 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
13330 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070013331
Matt Carlson321d32a2008-11-21 17:22:19 -080013332 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsoncb4ed1f2010-01-20 16:58:09 +000013333 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
13334 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Michael Chanaf36e6b2006-03-23 01:28:06 -080013335 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
13336
Matt Carlson8d519ab2009-04-20 06:58:01 +000013337 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
13338 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070013339 /* Turn off the debug UART. */
13340 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
13341 if (tp->tg3_flags2 & TG3_FLG2_IS_NIC)
13342 /* Keep VMain power. */
13343 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
13344 GRC_LCLCTRL_GPIO_OUTPUT0;
13345 }
13346
Linus Torvalds1da177e2005-04-16 15:20:36 -070013347 /* Force the chip into D0. */
Michael Chanbc1c7562006-03-20 17:48:03 -080013348 err = tg3_set_power_state(tp, PCI_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013349 if (err) {
13350 printk(KERN_ERR PFX "(%s) transition to D0 failed\n",
13351 pci_name(tp->pdev));
13352 return err;
13353 }
13354
Linus Torvalds1da177e2005-04-16 15:20:36 -070013355 /* Derive initial jumbo mode from MTU assigned in
13356 * ether_setup() via the alloc_etherdev() call
13357 */
Michael Chan0f893dc2005-07-25 12:30:38 -070013358 if (tp->dev->mtu > ETH_DATA_LEN &&
Michael Chana4e2b342005-10-26 15:46:52 -070013359 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Michael Chan0f893dc2005-07-25 12:30:38 -070013360 tp->tg3_flags |= TG3_FLAG_JUMBO_RING_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013361
13362 /* Determine WakeOnLan speed to use. */
13363 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13364 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
13365 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
13366 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
13367 tp->tg3_flags &= ~(TG3_FLAG_WOL_SPEED_100MB);
13368 } else {
13369 tp->tg3_flags |= TG3_FLAG_WOL_SPEED_100MB;
13370 }
13371
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013372 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
13373 tp->tg3_flags3 |= TG3_FLG3_PHY_IS_FET;
13374
Linus Torvalds1da177e2005-04-16 15:20:36 -070013375 /* A few boards don't want Ethernet@WireSpeed phy feature */
13376 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
13377 ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
13378 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070013379 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013380 (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) ||
Michael Chan747e8f82005-07-25 12:33:22 -070013381 (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013382 tp->tg3_flags2 |= TG3_FLG2_NO_ETH_WIRE_SPEED;
13383
13384 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
13385 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
13386 tp->tg3_flags2 |= TG3_FLG2_PHY_ADC_BUG;
13387 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
13388 tp->tg3_flags2 |= TG3_FLG2_PHY_5704_A0_BUG;
13389
Matt Carlson321d32a2008-11-21 17:22:19 -080013390 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013391 !(tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080013392 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013393 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
Matt Carlsonb703df62009-12-03 08:36:21 +000013394 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
13395 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57765) {
Michael Chanc424cb22006-04-29 18:56:34 -070013396 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013397 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013398 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
13399 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080013400 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
13401 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
13402 tp->tg3_flags2 |= TG3_FLG2_PHY_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080013403 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
13404 tp->tg3_flags2 |= TG3_FLG2_PHY_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080013405 } else
Michael Chanc424cb22006-04-29 18:56:34 -070013406 tp->tg3_flags2 |= TG3_FLG2_PHY_BER_BUG;
13407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013408
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013409 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
13410 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
13411 tp->phy_otp = tg3_read_otp_phycfg(tp);
13412 if (tp->phy_otp == 0)
13413 tp->phy_otp = TG3_OTP_DEFAULT;
13414 }
13415
Matt Carlsonf51f3562008-05-25 23:45:08 -070013416 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT)
Matt Carlson8ef21422008-05-02 16:47:53 -070013417 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
13418 else
13419 tp->mi_mode = MAC_MI_MODE_BASE;
13420
Linus Torvalds1da177e2005-04-16 15:20:36 -070013421 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013422 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
13423 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
13424 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
13425
Matt Carlson321d32a2008-11-21 17:22:19 -080013426 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
13427 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson57e69832008-05-25 23:48:31 -070013428 tp->tg3_flags3 |= TG3_FLG3_USE_PHYLIB;
13429
Matt Carlson158d7ab2008-05-29 01:37:54 -070013430 err = tg3_mdio_init(tp);
13431 if (err)
13432 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013433
13434 /* Initialize data/descriptor byte/word swapping. */
13435 val = tr32(GRC_MODE);
13436 val &= GRC_MODE_HOST_STACKUP;
13437 tw32(GRC_MODE, val | tp->grc_mode);
13438
13439 tg3_switch_clocks(tp);
13440
13441 /* Clear this out for sanity. */
13442 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
13443
13444 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
13445 &pci_state_reg);
13446 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
13447 (tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG) == 0) {
13448 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
13449
13450 if (chiprevid == CHIPREV_ID_5701_A0 ||
13451 chiprevid == CHIPREV_ID_5701_B0 ||
13452 chiprevid == CHIPREV_ID_5701_B2 ||
13453 chiprevid == CHIPREV_ID_5701_B5) {
13454 void __iomem *sram_base;
13455
13456 /* Write some dummy words into the SRAM status block
13457 * area, see if it reads back correctly. If the return
13458 * value is bad, force enable the PCIX workaround.
13459 */
13460 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
13461
13462 writel(0x00000000, sram_base);
13463 writel(0x00000000, sram_base + 4);
13464 writel(0xffffffff, sram_base + 4);
13465 if (readl(sram_base) != 0x00000000)
13466 tp->tg3_flags |= TG3_FLAG_PCIX_TARGET_HWBUG;
13467 }
13468 }
13469
13470 udelay(50);
13471 tg3_nvram_init(tp);
13472
13473 grc_misc_cfg = tr32(GRC_MISC_CFG);
13474 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
13475
Linus Torvalds1da177e2005-04-16 15:20:36 -070013476 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
13477 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
13478 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
13479 tp->tg3_flags2 |= TG3_FLG2_IS_5788;
13480
David S. Millerfac9b832005-05-18 22:46:34 -070013481 if (!(tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
13482 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700))
13483 tp->tg3_flags |= TG3_FLAG_TAGGED_STATUS;
13484 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) {
13485 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
13486 HOSTCC_MODE_CLRTICK_TXBD);
13487
13488 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
13489 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13490 tp->misc_host_ctrl);
13491 }
13492
Matt Carlson3bda1252008-08-15 14:08:22 -070013493 /* Preserve the APE MAC_MODE bits */
13494 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
13495 tp->mac_mode = tr32(MAC_MODE) |
13496 MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
13497 else
13498 tp->mac_mode = TG3_DEF_MAC_MODE;
13499
Linus Torvalds1da177e2005-04-16 15:20:36 -070013500 /* these are limited to 10/100 only */
13501 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
13502 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
13503 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
13504 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
13505 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
13506 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
13507 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
13508 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
13509 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
Michael Chan676917d2006-12-07 00:20:22 -080013510 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
13511 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013512 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013513 (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013514 tp->tg3_flags |= TG3_FLAG_10_100_ONLY;
13515
13516 err = tg3_phy_probe(tp);
13517 if (err) {
13518 printk(KERN_ERR PFX "(%s) phy probe failed, err %d\n",
13519 pci_name(tp->pdev), err);
13520 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013521 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013522 }
13523
13524 tg3_read_partno(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080013525 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013526
13527 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
13528 tp->tg3_flags &= ~TG3_FLAG_USE_MI_INTERRUPT;
13529 } else {
13530 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
13531 tp->tg3_flags |= TG3_FLAG_USE_MI_INTERRUPT;
13532 else
13533 tp->tg3_flags &= ~TG3_FLAG_USE_MI_INTERRUPT;
13534 }
13535
13536 /* 5700 {AX,BX} chips have a broken status block link
13537 * change bit implementation, so we must use the
13538 * status register in those cases.
13539 */
13540 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
13541 tp->tg3_flags |= TG3_FLAG_USE_LINKCHG_REG;
13542 else
13543 tp->tg3_flags &= ~TG3_FLAG_USE_LINKCHG_REG;
13544
13545 /* The led_ctrl is set during tg3_phy_probe, here we might
13546 * have to force the link status polling mechanism based
13547 * upon subsystem IDs.
13548 */
13549 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070013550 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070013551 !(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)) {
13552 tp->tg3_flags |= (TG3_FLAG_USE_MI_INTERRUPT |
13553 TG3_FLAG_USE_LINKCHG_REG);
13554 }
13555
13556 /* For all SERDES we poll the MAC status register. */
13557 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
13558 tp->tg3_flags |= TG3_FLAG_POLL_SERDES;
13559 else
13560 tp->tg3_flags &= ~TG3_FLAG_POLL_SERDES;
13561
Matt Carlsonad829262008-11-21 17:16:16 -080013562 tp->rx_offset = NET_IP_ALIGN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013563 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
13564 (tp->tg3_flags & TG3_FLAG_PCIX_MODE) != 0)
13565 tp->rx_offset = 0;
13566
Michael Chanf92905d2006-06-29 20:14:29 -070013567 tp->rx_std_max_post = TG3_RX_RING_SIZE;
13568
13569 /* Increment the rx prod index on the rx std ring by at most
13570 * 8 for these chips to workaround hw errata.
13571 */
13572 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13573 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
13574 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
13575 tp->rx_std_max_post = 8;
13576
Matt Carlson8ed5d972007-05-07 00:25:49 -070013577 if (tp->tg3_flags & TG3_FLAG_ASPM_WORKAROUND)
13578 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
13579 PCIE_PWR_MGMT_L1_THRESH_MSK;
13580
Linus Torvalds1da177e2005-04-16 15:20:36 -070013581 return err;
13582}
13583
David S. Miller49b6e95f2007-03-29 01:38:42 -070013584#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070013585static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
13586{
13587 struct net_device *dev = tp->dev;
13588 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070013589 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070013590 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070013591 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013592
David S. Miller49b6e95f2007-03-29 01:38:42 -070013593 addr = of_get_property(dp, "local-mac-address", &len);
13594 if (addr && len == 6) {
13595 memcpy(dev->dev_addr, addr, 6);
13596 memcpy(dev->perm_addr, dev->dev_addr, 6);
13597 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013598 }
13599 return -ENODEV;
13600}
13601
13602static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
13603{
13604 struct net_device *dev = tp->dev;
13605
13606 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070013607 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013608 return 0;
13609}
13610#endif
13611
13612static int __devinit tg3_get_device_address(struct tg3 *tp)
13613{
13614 struct net_device *dev = tp->dev;
13615 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080013616 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013617
David S. Miller49b6e95f2007-03-29 01:38:42 -070013618#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070013619 if (!tg3_get_macaddr_sparc(tp))
13620 return 0;
13621#endif
13622
13623 mac_offset = 0x7c;
David S. Millerf49639e2006-06-09 11:58:36 -070013624 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) ||
Michael Chana4e2b342005-10-26 15:46:52 -070013625 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013626 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
13627 mac_offset = 0xcc;
13628 if (tg3_nvram_lock(tp))
13629 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
13630 else
13631 tg3_nvram_unlock(tp);
Matt Carlsona1b950d2009-09-01 13:20:17 +000013632 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
13633 if (tr32(TG3_CPMU_STATUS) & TG3_CPMU_STATUS_PCIE_FUNC)
13634 mac_offset = 0xcc;
13635 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070013636 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013637
13638 /* First try to get it from MAC address mailbox. */
13639 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
13640 if ((hi >> 16) == 0x484b) {
13641 dev->dev_addr[0] = (hi >> 8) & 0xff;
13642 dev->dev_addr[1] = (hi >> 0) & 0xff;
13643
13644 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
13645 dev->dev_addr[2] = (lo >> 24) & 0xff;
13646 dev->dev_addr[3] = (lo >> 16) & 0xff;
13647 dev->dev_addr[4] = (lo >> 8) & 0xff;
13648 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013649
Michael Chan008652b2006-03-27 23:14:53 -080013650 /* Some old bootcode may report a 0 MAC address in SRAM */
13651 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
13652 }
13653 if (!addr_ok) {
13654 /* Next, try NVRAM. */
Matt Carlsondf259d82009-04-20 06:57:14 +000013655 if (!(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) &&
13656 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000013657 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070013658 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
13659 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080013660 }
13661 /* Finally just fetch it out of the MAC control regs. */
13662 else {
13663 hi = tr32(MAC_ADDR_0_HIGH);
13664 lo = tr32(MAC_ADDR_0_LOW);
13665
13666 dev->dev_addr[5] = lo & 0xff;
13667 dev->dev_addr[4] = (lo >> 8) & 0xff;
13668 dev->dev_addr[3] = (lo >> 16) & 0xff;
13669 dev->dev_addr[2] = (lo >> 24) & 0xff;
13670 dev->dev_addr[1] = hi & 0xff;
13671 dev->dev_addr[0] = (hi >> 8) & 0xff;
13672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013673 }
13674
13675 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070013676#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070013677 if (!tg3_get_default_macaddr_sparc(tp))
13678 return 0;
13679#endif
13680 return -EINVAL;
13681 }
John W. Linville2ff43692005-09-12 14:44:20 -070013682 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013683 return 0;
13684}
13685
David S. Miller59e6b432005-05-18 22:50:10 -070013686#define BOUNDARY_SINGLE_CACHELINE 1
13687#define BOUNDARY_MULTI_CACHELINE 2
13688
13689static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
13690{
13691 int cacheline_size;
13692 u8 byte;
13693 int goal;
13694
13695 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
13696 if (byte == 0)
13697 cacheline_size = 1024;
13698 else
13699 cacheline_size = (int) byte * 4;
13700
13701 /* On 5703 and later chips, the boundary bits have no
13702 * effect.
13703 */
13704 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13705 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13706 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
13707 goto out;
13708
13709#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
13710 goal = BOUNDARY_MULTI_CACHELINE;
13711#else
13712#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
13713 goal = BOUNDARY_SINGLE_CACHELINE;
13714#else
13715 goal = 0;
13716#endif
13717#endif
13718
Matt Carlsonb703df62009-12-03 08:36:21 +000013719 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13720 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000013721 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
13722 goto out;
13723 }
13724
David S. Miller59e6b432005-05-18 22:50:10 -070013725 if (!goal)
13726 goto out;
13727
13728 /* PCI controllers on most RISC systems tend to disconnect
13729 * when a device tries to burst across a cache-line boundary.
13730 * Therefore, letting tg3 do so just wastes PCI bandwidth.
13731 *
13732 * Unfortunately, for PCI-E there are only limited
13733 * write-side controls for this, and thus for reads
13734 * we will still get the disconnects. We'll also waste
13735 * these PCI cycles for both read and write for chips
13736 * other than 5700 and 5701 which do not implement the
13737 * boundary bits.
13738 */
13739 if ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) &&
13740 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)) {
13741 switch (cacheline_size) {
13742 case 16:
13743 case 32:
13744 case 64:
13745 case 128:
13746 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13747 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
13748 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
13749 } else {
13750 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
13751 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
13752 }
13753 break;
13754
13755 case 256:
13756 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
13757 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
13758 break;
13759
13760 default:
13761 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
13762 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
13763 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070013764 }
David S. Miller59e6b432005-05-18 22:50:10 -070013765 } else if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
13766 switch (cacheline_size) {
13767 case 16:
13768 case 32:
13769 case 64:
13770 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13771 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
13772 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
13773 break;
13774 }
13775 /* fallthrough */
13776 case 128:
13777 default:
13778 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
13779 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
13780 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070013781 }
David S. Miller59e6b432005-05-18 22:50:10 -070013782 } else {
13783 switch (cacheline_size) {
13784 case 16:
13785 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13786 val |= (DMA_RWCTRL_READ_BNDRY_16 |
13787 DMA_RWCTRL_WRITE_BNDRY_16);
13788 break;
13789 }
13790 /* fallthrough */
13791 case 32:
13792 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13793 val |= (DMA_RWCTRL_READ_BNDRY_32 |
13794 DMA_RWCTRL_WRITE_BNDRY_32);
13795 break;
13796 }
13797 /* fallthrough */
13798 case 64:
13799 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13800 val |= (DMA_RWCTRL_READ_BNDRY_64 |
13801 DMA_RWCTRL_WRITE_BNDRY_64);
13802 break;
13803 }
13804 /* fallthrough */
13805 case 128:
13806 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13807 val |= (DMA_RWCTRL_READ_BNDRY_128 |
13808 DMA_RWCTRL_WRITE_BNDRY_128);
13809 break;
13810 }
13811 /* fallthrough */
13812 case 256:
13813 val |= (DMA_RWCTRL_READ_BNDRY_256 |
13814 DMA_RWCTRL_WRITE_BNDRY_256);
13815 break;
13816 case 512:
13817 val |= (DMA_RWCTRL_READ_BNDRY_512 |
13818 DMA_RWCTRL_WRITE_BNDRY_512);
13819 break;
13820 case 1024:
13821 default:
13822 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
13823 DMA_RWCTRL_WRITE_BNDRY_1024);
13824 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070013825 }
David S. Miller59e6b432005-05-18 22:50:10 -070013826 }
13827
13828out:
13829 return val;
13830}
13831
Linus Torvalds1da177e2005-04-16 15:20:36 -070013832static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
13833{
13834 struct tg3_internal_buffer_desc test_desc;
13835 u32 sram_dma_descs;
13836 int i, ret;
13837
13838 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
13839
13840 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
13841 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
13842 tw32(RDMAC_STATUS, 0);
13843 tw32(WDMAC_STATUS, 0);
13844
13845 tw32(BUFMGR_MODE, 0);
13846 tw32(FTQ_RESET, 0);
13847
13848 test_desc.addr_hi = ((u64) buf_dma) >> 32;
13849 test_desc.addr_lo = buf_dma & 0xffffffff;
13850 test_desc.nic_mbuf = 0x00002100;
13851 test_desc.len = size;
13852
13853 /*
13854 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
13855 * the *second* time the tg3 driver was getting loaded after an
13856 * initial scan.
13857 *
13858 * Broadcom tells me:
13859 * ...the DMA engine is connected to the GRC block and a DMA
13860 * reset may affect the GRC block in some unpredictable way...
13861 * The behavior of resets to individual blocks has not been tested.
13862 *
13863 * Broadcom noted the GRC reset will also reset all sub-components.
13864 */
13865 if (to_device) {
13866 test_desc.cqid_sqid = (13 << 8) | 2;
13867
13868 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
13869 udelay(40);
13870 } else {
13871 test_desc.cqid_sqid = (16 << 8) | 7;
13872
13873 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
13874 udelay(40);
13875 }
13876 test_desc.flags = 0x00000005;
13877
13878 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
13879 u32 val;
13880
13881 val = *(((u32 *)&test_desc) + i);
13882 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
13883 sram_dma_descs + (i * sizeof(u32)));
13884 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
13885 }
13886 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
13887
13888 if (to_device) {
13889 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
13890 } else {
13891 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
13892 }
13893
13894 ret = -ENODEV;
13895 for (i = 0; i < 40; i++) {
13896 u32 val;
13897
13898 if (to_device)
13899 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
13900 else
13901 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
13902 if ((val & 0xffff) == sram_dma_descs) {
13903 ret = 0;
13904 break;
13905 }
13906
13907 udelay(100);
13908 }
13909
13910 return ret;
13911}
13912
David S. Millerded73402005-05-23 13:59:47 -070013913#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070013914
13915static int __devinit tg3_test_dma(struct tg3 *tp)
13916{
13917 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070013918 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000013919 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013920
13921 buf = pci_alloc_consistent(tp->pdev, TEST_BUFFER_SIZE, &buf_dma);
13922 if (!buf) {
13923 ret = -ENOMEM;
13924 goto out_nofree;
13925 }
13926
13927 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
13928 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
13929
David S. Miller59e6b432005-05-18 22:50:10 -070013930 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013931
Matt Carlsonb703df62009-12-03 08:36:21 +000013932 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13933 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000013934 goto out;
13935
Linus Torvalds1da177e2005-04-16 15:20:36 -070013936 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
13937 /* DMA read watermark not used on PCIE */
13938 tp->dma_rwctrl |= 0x00180000;
13939 } else if (!(tp->tg3_flags & TG3_FLAG_PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070013940 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
13941 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013942 tp->dma_rwctrl |= 0x003f0000;
13943 else
13944 tp->dma_rwctrl |= 0x003f000f;
13945 } else {
13946 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
13947 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
13948 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080013949 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013950
Michael Chan4a29cc22006-03-19 13:21:12 -080013951 /* If the 5704 is behind the EPB bridge, we can
13952 * do the less restrictive ONE_DMA workaround for
13953 * better performance.
13954 */
13955 if ((tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG) &&
13956 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
13957 tp->dma_rwctrl |= 0x8000;
13958 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013959 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
13960
Michael Chan49afdeb2007-02-13 12:17:03 -080013961 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
13962 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070013963 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080013964 tp->dma_rwctrl |=
13965 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
13966 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
13967 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070013968 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
13969 /* 5780 always in PCIX mode */
13970 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070013971 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
13972 /* 5714 always in PCIX mode */
13973 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013974 } else {
13975 tp->dma_rwctrl |= 0x001b000f;
13976 }
13977 }
13978
13979 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
13980 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
13981 tp->dma_rwctrl &= 0xfffffff0;
13982
13983 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13984 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
13985 /* Remove this if it causes problems for some boards. */
13986 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
13987
13988 /* On 5700/5701 chips, we need to set this bit.
13989 * Otherwise the chip will issue cacheline transactions
13990 * to streamable DMA memory with not all the byte
13991 * enables turned on. This is an error on several
13992 * RISC PCI controllers, in particular sparc64.
13993 *
13994 * On 5703/5704 chips, this bit has been reassigned
13995 * a different meaning. In particular, it is used
13996 * on those chips to enable a PCI-X workaround.
13997 */
13998 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
13999 }
14000
14001 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14002
14003#if 0
14004 /* Unneeded, already done by tg3_get_invariants. */
14005 tg3_switch_clocks(tp);
14006#endif
14007
Linus Torvalds1da177e2005-04-16 15:20:36 -070014008 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14009 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
14010 goto out;
14011
David S. Miller59e6b432005-05-18 22:50:10 -070014012 /* It is best to perform DMA test with maximum write burst size
14013 * to expose the 5700/5701 write DMA bug.
14014 */
14015 saved_dma_rwctrl = tp->dma_rwctrl;
14016 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14017 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14018
Linus Torvalds1da177e2005-04-16 15:20:36 -070014019 while (1) {
14020 u32 *p = buf, i;
14021
14022 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
14023 p[i] = i;
14024
14025 /* Send the buffer to the chip. */
14026 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
14027 if (ret) {
14028 printk(KERN_ERR "tg3_test_dma() Write the buffer failed %d\n", ret);
14029 break;
14030 }
14031
14032#if 0
14033 /* validate data reached card RAM correctly. */
14034 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14035 u32 val;
14036 tg3_read_mem(tp, 0x2100 + (i*4), &val);
14037 if (le32_to_cpu(val) != p[i]) {
14038 printk(KERN_ERR " tg3_test_dma() Card buffer corrupted on write! (%d != %d)\n", val, i);
14039 /* ret = -ENODEV here? */
14040 }
14041 p[i] = 0;
14042 }
14043#endif
14044 /* Now read it back. */
14045 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
14046 if (ret) {
14047 printk(KERN_ERR "tg3_test_dma() Read the buffer failed %d\n", ret);
14048
14049 break;
14050 }
14051
14052 /* Verify it. */
14053 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14054 if (p[i] == i)
14055 continue;
14056
David S. Miller59e6b432005-05-18 22:50:10 -070014057 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14058 DMA_RWCTRL_WRITE_BNDRY_16) {
14059 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014060 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
14061 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14062 break;
14063 } else {
14064 printk(KERN_ERR "tg3_test_dma() buffer corrupted on read back! (%d != %d)\n", p[i], i);
14065 ret = -ENODEV;
14066 goto out;
14067 }
14068 }
14069
14070 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
14071 /* Success. */
14072 ret = 0;
14073 break;
14074 }
14075 }
David S. Miller59e6b432005-05-18 22:50:10 -070014076 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14077 DMA_RWCTRL_WRITE_BNDRY_16) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014078 static struct pci_device_id dma_wait_state_chipsets[] = {
14079 { PCI_DEVICE(PCI_VENDOR_ID_APPLE,
14080 PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
14081 { },
14082 };
14083
David S. Miller59e6b432005-05-18 22:50:10 -070014084 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070014085 * now look for chipsets that are known to expose the
14086 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070014087 */
Michael Chan6d1cfba2005-06-08 14:13:14 -070014088 if (pci_dev_present(dma_wait_state_chipsets)) {
14089 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14090 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
14091 }
14092 else
14093 /* Safe to use the calculated DMA boundary. */
14094 tp->dma_rwctrl = saved_dma_rwctrl;
14095
David S. Miller59e6b432005-05-18 22:50:10 -070014096 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14097 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014098
14099out:
14100 pci_free_consistent(tp->pdev, TEST_BUFFER_SIZE, buf, buf_dma);
14101out_nofree:
14102 return ret;
14103}
14104
14105static void __devinit tg3_init_link_config(struct tg3 *tp)
14106{
14107 tp->link_config.advertising =
14108 (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
14109 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
14110 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full |
14111 ADVERTISED_Autoneg | ADVERTISED_MII);
14112 tp->link_config.speed = SPEED_INVALID;
14113 tp->link_config.duplex = DUPLEX_INVALID;
14114 tp->link_config.autoneg = AUTONEG_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014115 tp->link_config.active_speed = SPEED_INVALID;
14116 tp->link_config.active_duplex = DUPLEX_INVALID;
14117 tp->link_config.phy_is_low_power = 0;
14118 tp->link_config.orig_speed = SPEED_INVALID;
14119 tp->link_config.orig_duplex = DUPLEX_INVALID;
14120 tp->link_config.orig_autoneg = AUTONEG_INVALID;
14121}
14122
14123static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
14124{
Matt Carlson666bc832010-01-20 16:58:03 +000014125 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14126 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
14127 tp->bufmgr_config.mbuf_read_dma_low_water =
14128 DEFAULT_MB_RDMA_LOW_WATER_5705;
14129 tp->bufmgr_config.mbuf_mac_rx_low_water =
14130 DEFAULT_MB_MACRX_LOW_WATER_57765;
14131 tp->bufmgr_config.mbuf_high_water =
14132 DEFAULT_MB_HIGH_WATER_57765;
14133
14134 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14135 DEFAULT_MB_RDMA_LOW_WATER_5705;
14136 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14137 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
14138 tp->bufmgr_config.mbuf_high_water_jumbo =
14139 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
14140 } else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
Michael Chanfdfec1722005-07-25 12:31:48 -070014141 tp->bufmgr_config.mbuf_read_dma_low_water =
14142 DEFAULT_MB_RDMA_LOW_WATER_5705;
14143 tp->bufmgr_config.mbuf_mac_rx_low_water =
14144 DEFAULT_MB_MACRX_LOW_WATER_5705;
14145 tp->bufmgr_config.mbuf_high_water =
14146 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070014147 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
14148 tp->bufmgr_config.mbuf_mac_rx_low_water =
14149 DEFAULT_MB_MACRX_LOW_WATER_5906;
14150 tp->bufmgr_config.mbuf_high_water =
14151 DEFAULT_MB_HIGH_WATER_5906;
14152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014153
Michael Chanfdfec1722005-07-25 12:31:48 -070014154 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14155 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
14156 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14157 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
14158 tp->bufmgr_config.mbuf_high_water_jumbo =
14159 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
14160 } else {
14161 tp->bufmgr_config.mbuf_read_dma_low_water =
14162 DEFAULT_MB_RDMA_LOW_WATER;
14163 tp->bufmgr_config.mbuf_mac_rx_low_water =
14164 DEFAULT_MB_MACRX_LOW_WATER;
14165 tp->bufmgr_config.mbuf_high_water =
14166 DEFAULT_MB_HIGH_WATER;
14167
14168 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14169 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
14170 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14171 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
14172 tp->bufmgr_config.mbuf_high_water_jumbo =
14173 DEFAULT_MB_HIGH_WATER_JUMBO;
14174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014175
14176 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
14177 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
14178}
14179
14180static char * __devinit tg3_phy_string(struct tg3 *tp)
14181{
14182 switch (tp->phy_id & PHY_ID_MASK) {
14183 case PHY_ID_BCM5400: return "5400";
14184 case PHY_ID_BCM5401: return "5401";
14185 case PHY_ID_BCM5411: return "5411";
14186 case PHY_ID_BCM5701: return "5701";
14187 case PHY_ID_BCM5703: return "5703";
14188 case PHY_ID_BCM5704: return "5704";
14189 case PHY_ID_BCM5705: return "5705";
14190 case PHY_ID_BCM5750: return "5750";
Michael Chan85e94ce2005-04-21 17:05:28 -070014191 case PHY_ID_BCM5752: return "5752";
Michael Chana4e2b342005-10-26 15:46:52 -070014192 case PHY_ID_BCM5714: return "5714";
Michael Chan4cf78e42005-07-25 12:29:19 -070014193 case PHY_ID_BCM5780: return "5780";
Michael Chanaf36e6b2006-03-23 01:28:06 -080014194 case PHY_ID_BCM5755: return "5755";
Michael Chand9ab5ad12006-03-20 22:27:35 -080014195 case PHY_ID_BCM5787: return "5787";
Matt Carlsond30cdd22007-10-07 23:28:35 -070014196 case PHY_ID_BCM5784: return "5784";
Michael Chan126a3362006-09-27 16:03:07 -070014197 case PHY_ID_BCM5756: return "5722/5756";
Michael Chanb5d37722006-09-27 16:06:21 -070014198 case PHY_ID_BCM5906: return "5906";
Matt Carlson9936bcf2007-10-10 18:03:07 -070014199 case PHY_ID_BCM5761: return "5761";
Matt Carlson9b952f52010-01-20 16:58:04 +000014200 case PHY_ID_BCM5718C: return "5718C";
14201 case PHY_ID_BCM5718S: return "5718S";
Linus Torvalds1da177e2005-04-16 15:20:36 -070014202 case PHY_ID_BCM8002: return "8002/serdes";
14203 case 0: return "serdes";
14204 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070014205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014206}
14207
Michael Chanf9804dd2005-09-27 12:13:10 -070014208static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
14209{
14210 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
14211 strcpy(str, "PCI Express");
14212 return str;
14213 } else if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
14214 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
14215
14216 strcpy(str, "PCIX:");
14217
14218 if ((clock_ctrl == 7) ||
14219 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
14220 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
14221 strcat(str, "133MHz");
14222 else if (clock_ctrl == 0)
14223 strcat(str, "33MHz");
14224 else if (clock_ctrl == 2)
14225 strcat(str, "50MHz");
14226 else if (clock_ctrl == 4)
14227 strcat(str, "66MHz");
14228 else if (clock_ctrl == 6)
14229 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070014230 } else {
14231 strcpy(str, "PCI:");
14232 if (tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED)
14233 strcat(str, "66MHz");
14234 else
14235 strcat(str, "33MHz");
14236 }
14237 if (tp->tg3_flags & TG3_FLAG_PCI_32BIT)
14238 strcat(str, ":32-bit");
14239 else
14240 strcat(str, ":64-bit");
14241 return str;
14242}
14243
Michael Chan8c2dc7e2005-12-19 16:26:02 -080014244static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014245{
14246 struct pci_dev *peer;
14247 unsigned int func, devnr = tp->pdev->devfn & ~7;
14248
14249 for (func = 0; func < 8; func++) {
14250 peer = pci_get_slot(tp->pdev->bus, devnr | func);
14251 if (peer && peer != tp->pdev)
14252 break;
14253 pci_dev_put(peer);
14254 }
Michael Chan16fe9d72005-12-13 21:09:54 -080014255 /* 5704 can be configured in single-port mode, set peer to
14256 * tp->pdev in that case.
14257 */
14258 if (!peer) {
14259 peer = tp->pdev;
14260 return peer;
14261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014262
14263 /*
14264 * We don't need to keep the refcount elevated; there's no way
14265 * to remove one half of this device without removing the other
14266 */
14267 pci_dev_put(peer);
14268
14269 return peer;
14270}
14271
David S. Miller15f98502005-05-18 22:49:26 -070014272static void __devinit tg3_init_coal(struct tg3 *tp)
14273{
14274 struct ethtool_coalesce *ec = &tp->coal;
14275
14276 memset(ec, 0, sizeof(*ec));
14277 ec->cmd = ETHTOOL_GCOALESCE;
14278 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
14279 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
14280 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
14281 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
14282 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
14283 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
14284 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
14285 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
14286 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
14287
14288 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
14289 HOSTCC_MODE_CLRTICK_TXBD)) {
14290 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
14291 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
14292 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
14293 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
14294 }
Michael Chand244c892005-07-05 14:42:33 -070014295
14296 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
14297 ec->rx_coalesce_usecs_irq = 0;
14298 ec->tx_coalesce_usecs_irq = 0;
14299 ec->stats_block_coalesce_usecs = 0;
14300 }
David S. Miller15f98502005-05-18 22:49:26 -070014301}
14302
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080014303static const struct net_device_ops tg3_netdev_ops = {
14304 .ndo_open = tg3_open,
14305 .ndo_stop = tg3_close,
Stephen Hemminger00829822008-11-20 20:14:53 -080014306 .ndo_start_xmit = tg3_start_xmit,
14307 .ndo_get_stats = tg3_get_stats,
14308 .ndo_validate_addr = eth_validate_addr,
14309 .ndo_set_multicast_list = tg3_set_rx_mode,
14310 .ndo_set_mac_address = tg3_set_mac_addr,
14311 .ndo_do_ioctl = tg3_ioctl,
14312 .ndo_tx_timeout = tg3_tx_timeout,
14313 .ndo_change_mtu = tg3_change_mtu,
14314#if TG3_VLAN_TAG_USED
14315 .ndo_vlan_rx_register = tg3_vlan_rx_register,
14316#endif
14317#ifdef CONFIG_NET_POLL_CONTROLLER
14318 .ndo_poll_controller = tg3_poll_controller,
14319#endif
14320};
14321
14322static const struct net_device_ops tg3_netdev_ops_dma_bug = {
14323 .ndo_open = tg3_open,
14324 .ndo_stop = tg3_close,
14325 .ndo_start_xmit = tg3_start_xmit_dma_bug,
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080014326 .ndo_get_stats = tg3_get_stats,
14327 .ndo_validate_addr = eth_validate_addr,
14328 .ndo_set_multicast_list = tg3_set_rx_mode,
14329 .ndo_set_mac_address = tg3_set_mac_addr,
14330 .ndo_do_ioctl = tg3_ioctl,
14331 .ndo_tx_timeout = tg3_tx_timeout,
14332 .ndo_change_mtu = tg3_change_mtu,
14333#if TG3_VLAN_TAG_USED
14334 .ndo_vlan_rx_register = tg3_vlan_rx_register,
14335#endif
14336#ifdef CONFIG_NET_POLL_CONTROLLER
14337 .ndo_poll_controller = tg3_poll_controller,
14338#endif
14339};
14340
Linus Torvalds1da177e2005-04-16 15:20:36 -070014341static int __devinit tg3_init_one(struct pci_dev *pdev,
14342 const struct pci_device_id *ent)
14343{
14344 static int tg3_version_printed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014345 struct net_device *dev;
14346 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000014347 int i, err, pm_cap;
14348 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070014349 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080014350 u64 dma_mask, persist_dma_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014351
14352 if (tg3_version_printed++ == 0)
14353 printk(KERN_INFO "%s", version);
14354
14355 err = pci_enable_device(pdev);
14356 if (err) {
14357 printk(KERN_ERR PFX "Cannot enable PCI device, "
14358 "aborting.\n");
14359 return err;
14360 }
14361
Linus Torvalds1da177e2005-04-16 15:20:36 -070014362 err = pci_request_regions(pdev, DRV_MODULE_NAME);
14363 if (err) {
14364 printk(KERN_ERR PFX "Cannot obtain PCI resources, "
14365 "aborting.\n");
14366 goto err_out_disable_pdev;
14367 }
14368
14369 pci_set_master(pdev);
14370
14371 /* Find power-management capability. */
14372 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
14373 if (pm_cap == 0) {
14374 printk(KERN_ERR PFX "Cannot find PowerManagement capability, "
14375 "aborting.\n");
14376 err = -EIO;
14377 goto err_out_free_res;
14378 }
14379
Matt Carlsonfe5f5782009-09-01 13:09:39 +000014380 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014381 if (!dev) {
14382 printk(KERN_ERR PFX "Etherdev alloc failed, aborting.\n");
14383 err = -ENOMEM;
14384 goto err_out_free_res;
14385 }
14386
Linus Torvalds1da177e2005-04-16 15:20:36 -070014387 SET_NETDEV_DEV(dev, &pdev->dev);
14388
Linus Torvalds1da177e2005-04-16 15:20:36 -070014389#if TG3_VLAN_TAG_USED
14390 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014391#endif
14392
14393 tp = netdev_priv(dev);
14394 tp->pdev = pdev;
14395 tp->dev = dev;
14396 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014397 tp->rx_mode = TG3_DEF_RX_MODE;
14398 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070014399
Linus Torvalds1da177e2005-04-16 15:20:36 -070014400 if (tg3_debug > 0)
14401 tp->msg_enable = tg3_debug;
14402 else
14403 tp->msg_enable = TG3_DEF_MSG_ENABLE;
14404
14405 /* The word/byte swap controls here control register access byte
14406 * swapping. DMA data byte swapping is controlled in the GRC_MODE
14407 * setting below.
14408 */
14409 tp->misc_host_ctrl =
14410 MISC_HOST_CTRL_MASK_PCI_INT |
14411 MISC_HOST_CTRL_WORD_SWAP |
14412 MISC_HOST_CTRL_INDIR_ACCESS |
14413 MISC_HOST_CTRL_PCISTATE_RW;
14414
14415 /* The NONFRM (non-frame) byte/word swap controls take effect
14416 * on descriptor entries, anything which isn't packet data.
14417 *
14418 * The StrongARM chips on the board (one for tx, one for rx)
14419 * are running in big-endian mode.
14420 */
14421 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
14422 GRC_MODE_WSWAP_NONFRM_DATA);
14423#ifdef __BIG_ENDIAN
14424 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
14425#endif
14426 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014427 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000014428 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014429
Matt Carlsond5fe4882008-11-21 17:20:32 -080014430 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010014431 if (!tp->regs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014432 printk(KERN_ERR PFX "Cannot map device registers, "
14433 "aborting.\n");
14434 err = -ENOMEM;
14435 goto err_out_free_dev;
14436 }
14437
14438 tg3_init_link_config(tp);
14439
Linus Torvalds1da177e2005-04-16 15:20:36 -070014440 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
14441 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014442
Linus Torvalds1da177e2005-04-16 15:20:36 -070014443 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014444 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014445 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014446
14447 err = tg3_get_invariants(tp);
14448 if (err) {
14449 printk(KERN_ERR PFX "Problem fetching invariants of chip, "
14450 "aborting.\n");
14451 goto err_out_iounmap;
14452 }
14453
Matt Carlson615774f2009-11-13 13:03:39 +000014454 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) &&
14455 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0)
Stephen Hemminger00829822008-11-20 20:14:53 -080014456 dev->netdev_ops = &tg3_netdev_ops;
14457 else
14458 dev->netdev_ops = &tg3_netdev_ops_dma_bug;
14459
14460
Michael Chan4a29cc22006-03-19 13:21:12 -080014461 /* The EPB bridge inside 5714, 5715, and 5780 and any
14462 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080014463 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
14464 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
14465 * do DMA address check in tg3_start_xmit().
14466 */
Michael Chan4a29cc22006-03-19 13:21:12 -080014467 if (tp->tg3_flags2 & TG3_FLG2_IS_5788)
Yang Hongyang284901a2009-04-06 19:01:15 -070014468 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Michael Chan4a29cc22006-03-19 13:21:12 -080014469 else if (tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070014470 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080014471#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070014472 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080014473#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080014474 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070014475 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080014476
14477 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070014478 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080014479 err = pci_set_dma_mask(pdev, dma_mask);
14480 if (!err) {
14481 dev->features |= NETIF_F_HIGHDMA;
14482 err = pci_set_consistent_dma_mask(pdev,
14483 persist_dma_mask);
14484 if (err < 0) {
14485 printk(KERN_ERR PFX "Unable to obtain 64 bit "
14486 "DMA for consistent allocations\n");
14487 goto err_out_iounmap;
14488 }
14489 }
14490 }
Yang Hongyang284901a2009-04-06 19:01:15 -070014491 if (err || dma_mask == DMA_BIT_MASK(32)) {
14492 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080014493 if (err) {
14494 printk(KERN_ERR PFX "No usable DMA configuration, "
14495 "aborting.\n");
14496 goto err_out_iounmap;
14497 }
14498 }
14499
Michael Chanfdfec1722005-07-25 12:31:48 -070014500 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014501
Matt Carlson507399f2009-11-13 13:03:37 +000014502 /* Selectively allow TSO based on operating conditions */
14503 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO) ||
14504 (tp->fw_needed && !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)))
14505 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
14506 else {
14507 tp->tg3_flags2 &= ~(TG3_FLG2_TSO_CAPABLE | TG3_FLG2_TSO_BUG);
14508 tp->fw_needed = NULL;
14509 }
14510
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014511 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
Matt Carlson9e9fd122009-01-19 16:57:45 -080014512 tp->fw_needed = FIRMWARE_TG3;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014513
Michael Chan4e3a7aa2006-03-20 17:47:44 -080014514 /* TSO is on by default on chips that support hardware TSO.
14515 * Firmware TSO on older chips gives lower performance, so it
14516 * is off by default, but can be enabled using ethtool.
14517 */
Matt Carlsone849cdc2009-11-13 13:03:38 +000014518 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO) &&
14519 (dev->features & NETIF_F_IP_CSUM))
14520 dev->features |= NETIF_F_TSO;
14521
14522 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_2) ||
14523 (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3)) {
14524 if (dev->features & NETIF_F_IPV6_CSUM)
Michael Chanb0026622006-07-03 19:42:14 -070014525 dev->features |= NETIF_F_TSO6;
Matt Carlsone849cdc2009-11-13 13:03:38 +000014526 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
14527 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070014528 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
14529 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014530 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000014531 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson9936bcf2007-10-10 18:03:07 -070014532 dev->features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070014533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014534
Linus Torvalds1da177e2005-04-16 15:20:36 -070014535 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
14536 !(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) &&
14537 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
14538 tp->tg3_flags2 |= TG3_FLG2_MAX_RXPEND_64;
14539 tp->rx_pending = 63;
14540 }
14541
Linus Torvalds1da177e2005-04-16 15:20:36 -070014542 err = tg3_get_device_address(tp);
14543 if (err) {
14544 printk(KERN_ERR PFX "Could not obtain valid ethernet address, "
14545 "aborting.\n");
Matt Carlson026a6c22009-12-03 08:36:24 +000014546 goto err_out_iounmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014547 }
14548
Matt Carlson0d3031d2007-10-10 18:02:43 -070014549 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
Matt Carlson63532392008-11-03 16:49:57 -080014550 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
Al Viro79ea13c2008-01-24 02:06:46 -080014551 if (!tp->aperegs) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070014552 printk(KERN_ERR PFX "Cannot map APE registers, "
14553 "aborting.\n");
14554 err = -ENOMEM;
Matt Carlson026a6c22009-12-03 08:36:24 +000014555 goto err_out_iounmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070014556 }
14557
14558 tg3_ape_lock_init(tp);
Matt Carlson7fd76442009-02-25 14:27:20 +000014559
14560 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF)
14561 tg3_read_dash_ver(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070014562 }
14563
Matt Carlsonc88864d2007-11-12 21:07:01 -080014564 /*
14565 * Reset chip in case UNDI or EFI driver did not shutdown
14566 * DMA self test will enable WDMAC and we'll see (spurious)
14567 * pending DMA on the PCI bus at that point.
14568 */
14569 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
14570 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
14571 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
14572 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
14573 }
14574
14575 err = tg3_test_dma(tp);
14576 if (err) {
14577 printk(KERN_ERR PFX "DMA engine test failed, aborting.\n");
14578 goto err_out_apeunmap;
14579 }
14580
Matt Carlsonc88864d2007-11-12 21:07:01 -080014581 /* flow control autonegotiation is default behavior */
14582 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
Steve Glendinninge18ce342008-12-16 02:00:00 -080014583 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlsonc88864d2007-11-12 21:07:01 -080014584
Matt Carlson78f90dc2009-11-13 13:03:42 +000014585 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
14586 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
14587 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
14588 for (i = 0; i < TG3_IRQ_MAX_VECS; i++) {
14589 struct tg3_napi *tnapi = &tp->napi[i];
14590
14591 tnapi->tp = tp;
14592 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
14593
14594 tnapi->int_mbox = intmbx;
14595 if (i < 4)
14596 intmbx += 0x8;
14597 else
14598 intmbx += 0x4;
14599
14600 tnapi->consmbox = rcvmbx;
14601 tnapi->prodmbox = sndmbx;
14602
14603 if (i) {
14604 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
14605 netif_napi_add(dev, &tnapi->napi, tg3_poll_msix, 64);
14606 } else {
14607 tnapi->coal_now = HOSTCC_MODE_NOW;
14608 netif_napi_add(dev, &tnapi->napi, tg3_poll, 64);
14609 }
14610
14611 if (!(tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX))
14612 break;
14613
14614 /*
14615 * If we support MSIX, we'll be using RSS. If we're using
14616 * RSS, the first vector only handles link interrupts and the
14617 * remaining vectors handle rx and tx interrupts. Reuse the
14618 * mailbox values for the next iteration. The values we setup
14619 * above are still useful for the single vectored mode.
14620 */
14621 if (!i)
14622 continue;
14623
14624 rcvmbx += 0x8;
14625
14626 if (sndmbx & 0x4)
14627 sndmbx -= 0x4;
14628 else
14629 sndmbx += 0xc;
14630 }
14631
Matt Carlsonc88864d2007-11-12 21:07:01 -080014632 tg3_init_coal(tp);
14633
Michael Chanc49a1562006-12-17 17:07:29 -080014634 pci_set_drvdata(pdev, dev);
14635
Linus Torvalds1da177e2005-04-16 15:20:36 -070014636 err = register_netdev(dev);
14637 if (err) {
14638 printk(KERN_ERR PFX "Cannot register net device, "
14639 "aborting.\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070014640 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014641 }
14642
Matt Carlsondf59c942008-11-03 16:52:56 -080014643 printk(KERN_INFO "%s: Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070014644 dev->name,
14645 tp->board_part_number,
14646 tp->pci_chip_rev_id,
Michael Chanf9804dd2005-09-27 12:13:10 -070014647 tg3_bus_string(tp, str),
Johannes Berge1749612008-10-27 15:59:26 -070014648 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014649
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000014650 if (tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED) {
14651 struct phy_device *phydev;
14652 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsondf59c942008-11-03 16:52:56 -080014653 printk(KERN_INFO
14654 "%s: attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000014655 tp->dev->name, phydev->drv->name,
14656 dev_name(&phydev->dev));
14657 } else
Matt Carlsondf59c942008-11-03 16:52:56 -080014658 printk(KERN_INFO
14659 "%s: attached PHY is %s (%s Ethernet) (WireSpeed[%d])\n",
14660 tp->dev->name, tg3_phy_string(tp),
14661 ((tp->tg3_flags & TG3_FLAG_10_100_ONLY) ? "10/100Base-TX" :
14662 ((tp->tg3_flags2 & TG3_FLG2_ANY_SERDES) ? "1000Base-SX" :
14663 "10/100/1000Base-T")),
14664 (tp->tg3_flags2 & TG3_FLG2_NO_ETH_WIRE_SPEED) == 0);
14665
14666 printk(KERN_INFO "%s: RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070014667 dev->name,
14668 (tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) != 0,
14669 (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) != 0,
14670 (tp->tg3_flags & TG3_FLAG_USE_MI_INTERRUPT) != 0,
14671 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014672 (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) != 0);
Michael Chan4a29cc22006-03-19 13:21:12 -080014673 printk(KERN_INFO "%s: dma_rwctrl[%08x] dma_mask[%d-bit]\n",
14674 dev->name, tp->dma_rwctrl,
Yang Hongyang284901a2009-04-06 19:01:15 -070014675 (pdev->dma_mask == DMA_BIT_MASK(32)) ? 32 :
Yang Hongyang50cf1562009-04-06 19:01:14 -070014676 (((u64) pdev->dma_mask == DMA_BIT_MASK(40)) ? 40 : 64));
Linus Torvalds1da177e2005-04-16 15:20:36 -070014677
14678 return 0;
14679
Matt Carlson0d3031d2007-10-10 18:02:43 -070014680err_out_apeunmap:
14681 if (tp->aperegs) {
14682 iounmap(tp->aperegs);
14683 tp->aperegs = NULL;
14684 }
14685
Linus Torvalds1da177e2005-04-16 15:20:36 -070014686err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070014687 if (tp->regs) {
14688 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070014689 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070014690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014691
14692err_out_free_dev:
14693 free_netdev(dev);
14694
14695err_out_free_res:
14696 pci_release_regions(pdev);
14697
14698err_out_disable_pdev:
14699 pci_disable_device(pdev);
14700 pci_set_drvdata(pdev, NULL);
14701 return err;
14702}
14703
14704static void __devexit tg3_remove_one(struct pci_dev *pdev)
14705{
14706 struct net_device *dev = pci_get_drvdata(pdev);
14707
14708 if (dev) {
14709 struct tg3 *tp = netdev_priv(dev);
14710
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014711 if (tp->fw)
14712 release_firmware(tp->fw);
14713
Michael Chan7faa0062006-02-02 17:29:28 -080014714 flush_scheduled_work();
Matt Carlson158d7ab2008-05-29 01:37:54 -070014715
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014716 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
14717 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070014718 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014719 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070014720
Linus Torvalds1da177e2005-04-16 15:20:36 -070014721 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070014722 if (tp->aperegs) {
14723 iounmap(tp->aperegs);
14724 tp->aperegs = NULL;
14725 }
Michael Chan68929142005-08-09 20:17:14 -070014726 if (tp->regs) {
14727 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070014728 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070014729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014730 free_netdev(dev);
14731 pci_release_regions(pdev);
14732 pci_disable_device(pdev);
14733 pci_set_drvdata(pdev, NULL);
14734 }
14735}
14736
14737static int tg3_suspend(struct pci_dev *pdev, pm_message_t state)
14738{
14739 struct net_device *dev = pci_get_drvdata(pdev);
14740 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070014741 pci_power_t target_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014742 int err;
14743
Michael Chan3e0c95f2007-08-03 20:56:54 -070014744 /* PCI register 4 needs to be saved whether netif_running() or not.
14745 * MSI address and data need to be saved if using MSI and
14746 * netif_running().
14747 */
14748 pci_save_state(pdev);
14749
Linus Torvalds1da177e2005-04-16 15:20:36 -070014750 if (!netif_running(dev))
14751 return 0;
14752
Michael Chan7faa0062006-02-02 17:29:28 -080014753 flush_scheduled_work();
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014754 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014755 tg3_netif_stop(tp);
14756
14757 del_timer_sync(&tp->timer);
14758
David S. Millerf47c11e2005-06-24 20:18:35 -070014759 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014760 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070014761 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014762
14763 netif_device_detach(dev);
14764
David S. Millerf47c11e2005-06-24 20:18:35 -070014765 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070014766 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan6a9eba12005-12-13 21:08:58 -080014767 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
David S. Millerf47c11e2005-06-24 20:18:35 -070014768 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014769
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070014770 target_state = pdev->pm_cap ? pci_target_state(pdev) : PCI_D3hot;
14771
14772 err = tg3_set_power_state(tp, target_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014773 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014774 int err2;
14775
David S. Millerf47c11e2005-06-24 20:18:35 -070014776 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014777
Michael Chan6a9eba12005-12-13 21:08:58 -080014778 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014779 err2 = tg3_restart_hw(tp, 1);
14780 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070014781 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014782
14783 tp->timer.expires = jiffies + tp->timer_offset;
14784 add_timer(&tp->timer);
14785
14786 netif_device_attach(dev);
14787 tg3_netif_start(tp);
14788
Michael Chanb9ec6c12006-07-25 16:37:27 -070014789out:
David S. Millerf47c11e2005-06-24 20:18:35 -070014790 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014791
14792 if (!err2)
14793 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014794 }
14795
14796 return err;
14797}
14798
14799static int tg3_resume(struct pci_dev *pdev)
14800{
14801 struct net_device *dev = pci_get_drvdata(pdev);
14802 struct tg3 *tp = netdev_priv(dev);
14803 int err;
14804
Michael Chan3e0c95f2007-08-03 20:56:54 -070014805 pci_restore_state(tp->pdev);
14806
Linus Torvalds1da177e2005-04-16 15:20:36 -070014807 if (!netif_running(dev))
14808 return 0;
14809
Michael Chanbc1c7562006-03-20 17:48:03 -080014810 err = tg3_set_power_state(tp, PCI_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014811 if (err)
14812 return err;
14813
14814 netif_device_attach(dev);
14815
David S. Millerf47c11e2005-06-24 20:18:35 -070014816 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014817
Michael Chan6a9eba12005-12-13 21:08:58 -080014818 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Michael Chanb9ec6c12006-07-25 16:37:27 -070014819 err = tg3_restart_hw(tp, 1);
14820 if (err)
14821 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014822
14823 tp->timer.expires = jiffies + tp->timer_offset;
14824 add_timer(&tp->timer);
14825
Linus Torvalds1da177e2005-04-16 15:20:36 -070014826 tg3_netif_start(tp);
14827
Michael Chanb9ec6c12006-07-25 16:37:27 -070014828out:
David S. Millerf47c11e2005-06-24 20:18:35 -070014829 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014830
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014831 if (!err)
14832 tg3_phy_start(tp);
14833
Michael Chanb9ec6c12006-07-25 16:37:27 -070014834 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014835}
14836
14837static struct pci_driver tg3_driver = {
14838 .name = DRV_MODULE_NAME,
14839 .id_table = tg3_pci_tbl,
14840 .probe = tg3_init_one,
14841 .remove = __devexit_p(tg3_remove_one),
14842 .suspend = tg3_suspend,
14843 .resume = tg3_resume
14844};
14845
14846static int __init tg3_init(void)
14847{
Jeff Garzik29917622006-08-19 17:48:59 -040014848 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014849}
14850
14851static void __exit tg3_cleanup(void)
14852{
14853 pci_unregister_driver(&tg3_driver);
14854}
14855
14856module_init(tg3_init);
14857module_exit(tg3_cleanup);