blob: 3e893231fef32c2266ae7ec4c9b9982cbd9aef97 [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 Carlsonba5b0bf2010-01-12 10:11:40 +00007 * Copyright (C) 2005-2010 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"
Matt Carlson7ae554e2010-04-05 10:19:30 +000070#define DRV_MODULE_VERSION "3.109"
71#define DRV_MODULE_RELDATE "April 2, 2010"
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73#define TG3_DEF_MAC_MODE 0
74#define TG3_DEF_RX_MODE 0
75#define TG3_DEF_TX_MODE 0
76#define TG3_DEF_MSG_ENABLE \
77 (NETIF_MSG_DRV | \
78 NETIF_MSG_PROBE | \
79 NETIF_MSG_LINK | \
80 NETIF_MSG_TIMER | \
81 NETIF_MSG_IFDOWN | \
82 NETIF_MSG_IFUP | \
83 NETIF_MSG_RX_ERR | \
84 NETIF_MSG_TX_ERR)
85
86/* length of time before we decide the hardware is borked,
87 * and dev->tx_timeout() should be called to fix the problem
88 */
89#define TG3_TX_TIMEOUT (5 * HZ)
90
91/* hardware minimum and maximum for a single frame's data payload */
92#define TG3_MIN_MTU 60
93#define TG3_MAX_MTU(tp) \
Matt Carlson8f666b02009-08-28 13:58:24 +000094 ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) ? 9000 : 1500)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96/* These numbers seem to be hard coded in the NIC firmware somehow.
97 * You can't change the ring sizes, but you can change where you place
98 * them in the NIC onboard memory.
99 */
100#define TG3_RX_RING_SIZE 512
101#define TG3_DEF_RX_RING_PENDING 200
102#define TG3_RX_JUMBO_RING_SIZE 256
103#define TG3_DEF_RX_JUMBO_RING_PENDING 100
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000104#define TG3_RSS_INDIR_TBL_SIZE 128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106/* Do not place this n-ring entries value into the tp struct itself,
107 * we really want to expose these constants to GCC so that modulo et
108 * al. operations are done with shifts and masks instead of with
109 * hw multiply/modulo instructions. Another solution would be to
110 * replace things like '% foo' with '& (foo - 1)'.
111 */
112#define TG3_RX_RCB_RING_SIZE(tp) \
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000113 (((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) && \
Matt Carlson5ea1c502009-09-11 16:50:16 -0700114 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) ? 1024 : 512)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116#define TG3_TX_RING_SIZE 512
117#define TG3_DEF_TX_RING_PENDING (TG3_TX_RING_SIZE - 1)
118
119#define TG3_RX_RING_BYTES (sizeof(struct tg3_rx_buffer_desc) * \
120 TG3_RX_RING_SIZE)
Matt Carlson79ed5ac2009-08-28 14:00:55 +0000121#define TG3_RX_JUMBO_RING_BYTES (sizeof(struct tg3_ext_rx_buffer_desc) * \
122 TG3_RX_JUMBO_RING_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123#define TG3_RX_RCB_RING_BYTES(tp) (sizeof(struct tg3_rx_buffer_desc) * \
Matt Carlson79ed5ac2009-08-28 14:00:55 +0000124 TG3_RX_RCB_RING_SIZE(tp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125#define TG3_TX_RING_BYTES (sizeof(struct tg3_tx_buffer_desc) * \
126 TG3_TX_RING_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127#define NEXT_TX(N) (((N) + 1) & (TG3_TX_RING_SIZE - 1))
128
Matt Carlson287be122009-08-28 13:58:46 +0000129#define TG3_DMA_BYTE_ENAB 64
130
131#define TG3_RX_STD_DMA_SZ 1536
132#define TG3_RX_JMB_DMA_SZ 9046
133
134#define TG3_RX_DMA_TO_MAP_SZ(x) ((x) + TG3_DMA_BYTE_ENAB)
135
136#define TG3_RX_STD_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_STD_DMA_SZ)
137#define TG3_RX_JMB_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_JMB_DMA_SZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000139#define TG3_RX_STD_BUFF_RING_SIZE \
140 (sizeof(struct ring_info) * TG3_RX_RING_SIZE)
141
142#define TG3_RX_JMB_BUFF_RING_SIZE \
143 (sizeof(struct ring_info) * TG3_RX_JUMBO_RING_SIZE)
144
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000145#define TG3_RSS_MIN_NUM_MSIX_VECS 2
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147/* minimum number of free TX descriptors required to wake up TX process */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000148#define TG3_TX_WAKEUP_THRESH(tnapi) ((tnapi)->tx_pending / 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Matt Carlsonad829262008-11-21 17:16:16 -0800150#define TG3_RAW_IP_ALIGN 2
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152/* number of ETHTOOL_GSTATS u64's */
153#define TG3_NUM_STATS (sizeof(struct tg3_ethtool_stats)/sizeof(u64))
154
Michael Chan4cafd3f2005-05-29 14:56:34 -0700155#define TG3_NUM_TEST 6
156
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000157#define TG3_FW_UPDATE_TIMEOUT_SEC 5
158
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800159#define FIRMWARE_TG3 "tigon/tg3.bin"
160#define FIRMWARE_TG3TSO "tigon/tg3_tso.bin"
161#define FIRMWARE_TG3TSO5 "tigon/tg3_tso5.bin"
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163static char version[] __devinitdata =
Joe Perches05dbe002010-02-17 19:44:19 +0000164 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166MODULE_AUTHOR("David S. Miller (davem@redhat.com) and Jeff Garzik (jgarzik@pobox.com)");
167MODULE_DESCRIPTION("Broadcom Tigon3 ethernet driver");
168MODULE_LICENSE("GPL");
169MODULE_VERSION(DRV_MODULE_VERSION);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800170MODULE_FIRMWARE(FIRMWARE_TG3);
171MODULE_FIRMWARE(FIRMWARE_TG3TSO);
172MODULE_FIRMWARE(FIRMWARE_TG3TSO5);
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174static int tg3_debug = -1; /* -1 == use TG3_DEF_MSG_ENABLE as value */
175module_param(tg3_debug, int, 0);
176MODULE_PARM_DESC(tg3_debug, "Tigon3 bitmapped debugging message enable value");
177
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000178static DEFINE_PCI_DEVICE_TABLE(tg3_pci_tbl) = {
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700179 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5700)},
180 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5701)},
181 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702)},
182 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703)},
183 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704)},
184 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702FE)},
185 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705)},
186 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705_2)},
187 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M)},
188 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M_2)},
189 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702X)},
190 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703X)},
191 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S)},
192 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702A3)},
193 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703A3)},
194 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5782)},
195 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5788)},
196 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5789)},
197 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901)},
198 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901_2)},
199 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S_2)},
200 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705F)},
201 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5720)},
202 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5721)},
Michael Chan126a3362006-09-27 16:03:07 -0700203 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5722)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700204 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5750)},
205 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751)},
206 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5750M)},
207 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751M)},
208 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751F)},
209 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752)},
210 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752M)},
211 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753)},
212 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753M)},
213 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753F)},
214 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754)},
215 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754M)},
216 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755)},
217 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755M)},
Michael Chan126a3362006-09-27 16:03:07 -0700218 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5756)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700219 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5786)},
220 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787)},
221 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787M)},
Michael Chan676917d2006-12-07 00:20:22 -0800222 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787F)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700223 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714)},
224 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714S)},
225 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715)},
226 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715S)},
227 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780)},
228 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780S)},
229 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5781)},
Michael Chanb5d37722006-09-27 16:06:21 -0700230 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906)},
231 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906M)},
Matt Carlsond30cdd22007-10-07 23:28:35 -0700232 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5784)},
233 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5764)},
Matt Carlson6c7af272007-10-21 16:12:02 -0700234 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5723)},
Matt Carlson9936bcf2007-10-10 18:03:07 -0700235 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761)},
236 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761E)},
Matt Carlsonc88e6682008-11-03 16:49:18 -0800237 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761S)},
238 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761SE)},
Matt Carlson2befdce2009-08-28 12:28:45 +0000239 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_G)},
240 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_F)},
Matt Carlson321d32a2008-11-21 17:22:19 -0800241 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780)},
242 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57760)},
243 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57790)},
Matt Carlson5e7ccf22009-08-25 10:08:42 +0000244 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57788)},
Matt Carlson5001e2f2009-11-13 13:03:51 +0000245 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717)},
246 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5718)},
247 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5724)},
Matt Carlsonb0f75222010-01-20 16:58:11 +0000248 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57781)},
249 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57785)},
250 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57761)},
251 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57765)},
252 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57791)},
253 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57795)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700254 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9DXX)},
255 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9MXX)},
256 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1000)},
257 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1001)},
258 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1003)},
259 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC9100)},
260 {PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_TIGON3)},
261 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262};
263
264MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
265
Andreas Mohr50da8592006-08-14 23:54:30 -0700266static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 const char string[ETH_GSTRING_LEN];
268} ethtool_stats_keys[TG3_NUM_STATS] = {
269 { "rx_octets" },
270 { "rx_fragments" },
271 { "rx_ucast_packets" },
272 { "rx_mcast_packets" },
273 { "rx_bcast_packets" },
274 { "rx_fcs_errors" },
275 { "rx_align_errors" },
276 { "rx_xon_pause_rcvd" },
277 { "rx_xoff_pause_rcvd" },
278 { "rx_mac_ctrl_rcvd" },
279 { "rx_xoff_entered" },
280 { "rx_frame_too_long_errors" },
281 { "rx_jabbers" },
282 { "rx_undersize_packets" },
283 { "rx_in_length_errors" },
284 { "rx_out_length_errors" },
285 { "rx_64_or_less_octet_packets" },
286 { "rx_65_to_127_octet_packets" },
287 { "rx_128_to_255_octet_packets" },
288 { "rx_256_to_511_octet_packets" },
289 { "rx_512_to_1023_octet_packets" },
290 { "rx_1024_to_1522_octet_packets" },
291 { "rx_1523_to_2047_octet_packets" },
292 { "rx_2048_to_4095_octet_packets" },
293 { "rx_4096_to_8191_octet_packets" },
294 { "rx_8192_to_9022_octet_packets" },
295
296 { "tx_octets" },
297 { "tx_collisions" },
298
299 { "tx_xon_sent" },
300 { "tx_xoff_sent" },
301 { "tx_flow_control" },
302 { "tx_mac_errors" },
303 { "tx_single_collisions" },
304 { "tx_mult_collisions" },
305 { "tx_deferred" },
306 { "tx_excessive_collisions" },
307 { "tx_late_collisions" },
308 { "tx_collide_2times" },
309 { "tx_collide_3times" },
310 { "tx_collide_4times" },
311 { "tx_collide_5times" },
312 { "tx_collide_6times" },
313 { "tx_collide_7times" },
314 { "tx_collide_8times" },
315 { "tx_collide_9times" },
316 { "tx_collide_10times" },
317 { "tx_collide_11times" },
318 { "tx_collide_12times" },
319 { "tx_collide_13times" },
320 { "tx_collide_14times" },
321 { "tx_collide_15times" },
322 { "tx_ucast_packets" },
323 { "tx_mcast_packets" },
324 { "tx_bcast_packets" },
325 { "tx_carrier_sense_errors" },
326 { "tx_discards" },
327 { "tx_errors" },
328
329 { "dma_writeq_full" },
330 { "dma_write_prioq_full" },
331 { "rxbds_empty" },
332 { "rx_discards" },
333 { "rx_errors" },
334 { "rx_threshold_hit" },
335
336 { "dma_readq_full" },
337 { "dma_read_prioq_full" },
338 { "tx_comp_queue_full" },
339
340 { "ring_set_send_prod_index" },
341 { "ring_status_update" },
342 { "nic_irqs" },
343 { "nic_avoided_irqs" },
344 { "nic_tx_threshold_hit" }
345};
346
Andreas Mohr50da8592006-08-14 23:54:30 -0700347static const struct {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700348 const char string[ETH_GSTRING_LEN];
349} ethtool_test_keys[TG3_NUM_TEST] = {
350 { "nvram test (online) " },
351 { "link test (online) " },
352 { "register test (offline)" },
353 { "memory test (offline)" },
354 { "loopback test (offline)" },
355 { "interrupt test (offline)" },
356};
357
Michael Chanb401e9e2005-12-19 16:27:04 -0800358static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
359{
360 writel(val, tp->regs + off);
361}
362
363static u32 tg3_read32(struct tg3 *tp, u32 off)
364{
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400365 return (readl(tp->regs + off));
Michael Chanb401e9e2005-12-19 16:27:04 -0800366}
367
Matt Carlson0d3031d2007-10-10 18:02:43 -0700368static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
369{
370 writel(val, tp->aperegs + off);
371}
372
373static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
374{
375 return (readl(tp->aperegs + off));
376}
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
379{
Michael Chan68929142005-08-09 20:17:14 -0700380 unsigned long flags;
381
382 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700383 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
384 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
Michael Chan68929142005-08-09 20:17:14 -0700385 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700386}
387
388static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
389{
390 writel(val, tp->regs + off);
391 readl(tp->regs + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
393
Michael Chan68929142005-08-09 20:17:14 -0700394static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
395{
396 unsigned long flags;
397 u32 val;
398
399 spin_lock_irqsave(&tp->indirect_lock, flags);
400 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
401 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
402 spin_unlock_irqrestore(&tp->indirect_lock, flags);
403 return val;
404}
405
406static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
407{
408 unsigned long flags;
409
410 if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
411 pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
412 TG3_64BIT_REG_LOW, val);
413 return;
414 }
Matt Carlson66711e62009-11-13 13:03:49 +0000415 if (off == TG3_RX_STD_PROD_IDX_REG) {
Michael Chan68929142005-08-09 20:17:14 -0700416 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
417 TG3_64BIT_REG_LOW, val);
418 return;
419 }
420
421 spin_lock_irqsave(&tp->indirect_lock, flags);
422 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
423 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
424 spin_unlock_irqrestore(&tp->indirect_lock, flags);
425
426 /* In indirect mode when disabling interrupts, we also need
427 * to clear the interrupt bit in the GRC local ctrl register.
428 */
429 if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
430 (val == 0x1)) {
431 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
432 tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
433 }
434}
435
436static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
437{
438 unsigned long flags;
439 u32 val;
440
441 spin_lock_irqsave(&tp->indirect_lock, flags);
442 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
443 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
444 spin_unlock_irqrestore(&tp->indirect_lock, flags);
445 return val;
446}
447
Michael Chanb401e9e2005-12-19 16:27:04 -0800448/* usec_wait specifies the wait time in usec when writing to certain registers
449 * where it is unsafe to read back the register without some delay.
450 * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
451 * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
452 */
453static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
Michael Chanb401e9e2005-12-19 16:27:04 -0800455 if ((tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG) ||
456 (tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND))
457 /* Non-posted methods */
458 tp->write32(tp, off, val);
459 else {
460 /* Posted method */
461 tg3_write32(tp, off, val);
462 if (usec_wait)
463 udelay(usec_wait);
464 tp->read32(tp, off);
465 }
466 /* Wait again after the read for the posted method to guarantee that
467 * the wait time is met.
468 */
469 if (usec_wait)
470 udelay(usec_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}
472
Michael Chan09ee9292005-08-09 20:17:00 -0700473static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
474{
475 tp->write32_mbox(tp, off, val);
Michael Chan68929142005-08-09 20:17:14 -0700476 if (!(tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER) &&
477 !(tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND))
478 tp->read32_mbox(tp, off);
Michael Chan09ee9292005-08-09 20:17:00 -0700479}
480
Michael Chan20094932005-08-09 20:16:32 -0700481static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
483 void __iomem *mbox = tp->regs + off;
484 writel(val, mbox);
485 if (tp->tg3_flags & TG3_FLAG_TXD_MBOX_HWBUG)
486 writel(val, mbox);
487 if (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)
488 readl(mbox);
489}
490
Michael Chanb5d37722006-09-27 16:06:21 -0700491static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
492{
493 return (readl(tp->regs + off + GRCMBOX_BASE));
494}
495
496static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
497{
498 writel(val, tp->regs + off + GRCMBOX_BASE);
499}
500
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000501#define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700502#define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000503#define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
504#define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
505#define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
Michael Chan20094932005-08-09 20:16:32 -0700506
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000507#define tw32(reg, val) tp->write32(tp, reg, val)
508#define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
509#define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
510#define tr32(reg) tp->read32(tp, reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
513{
Michael Chan68929142005-08-09 20:17:14 -0700514 unsigned long flags;
515
Michael Chanb5d37722006-09-27 16:06:21 -0700516 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) &&
517 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
518 return;
519
Michael Chan68929142005-08-09 20:17:14 -0700520 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chanbbadf502006-04-06 21:46:34 -0700521 if (tp->tg3_flags & TG3_FLAG_SRAM_USE_CONFIG) {
522 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
523 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Michael Chanbbadf502006-04-06 21:46:34 -0700525 /* Always leave this as zero. */
526 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
527 } else {
528 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
529 tw32_f(TG3PCI_MEM_WIN_DATA, val);
530
531 /* Always leave this as zero. */
532 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
533 }
Michael Chan68929142005-08-09 20:17:14 -0700534 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535}
536
537static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
538{
Michael Chan68929142005-08-09 20:17:14 -0700539 unsigned long flags;
540
Michael Chanb5d37722006-09-27 16:06:21 -0700541 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) &&
542 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
543 *val = 0;
544 return;
545 }
546
Michael Chan68929142005-08-09 20:17:14 -0700547 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chanbbadf502006-04-06 21:46:34 -0700548 if (tp->tg3_flags & TG3_FLAG_SRAM_USE_CONFIG) {
549 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
550 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Michael Chanbbadf502006-04-06 21:46:34 -0700552 /* Always leave this as zero. */
553 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
554 } else {
555 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
556 *val = tr32(TG3PCI_MEM_WIN_DATA);
557
558 /* Always leave this as zero. */
559 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
560 }
Michael Chan68929142005-08-09 20:17:14 -0700561 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
563
Matt Carlson0d3031d2007-10-10 18:02:43 -0700564static void tg3_ape_lock_init(struct tg3 *tp)
565{
566 int i;
567
568 /* Make sure the driver hasn't any stale locks. */
569 for (i = 0; i < 8; i++)
570 tg3_ape_write32(tp, TG3_APE_LOCK_GRANT + 4 * i,
571 APE_LOCK_GRANT_DRIVER);
572}
573
574static int tg3_ape_lock(struct tg3 *tp, int locknum)
575{
576 int i, off;
577 int ret = 0;
578 u32 status;
579
580 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
581 return 0;
582
583 switch (locknum) {
Matt Carlson33f401a2010-04-05 10:19:27 +0000584 case TG3_APE_LOCK_GRC:
585 case TG3_APE_LOCK_MEM:
586 break;
587 default:
588 return -EINVAL;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700589 }
590
591 off = 4 * locknum;
592
593 tg3_ape_write32(tp, TG3_APE_LOCK_REQ + off, APE_LOCK_REQ_DRIVER);
594
595 /* Wait for up to 1 millisecond to acquire lock. */
596 for (i = 0; i < 100; i++) {
597 status = tg3_ape_read32(tp, TG3_APE_LOCK_GRANT + off);
598 if (status == APE_LOCK_GRANT_DRIVER)
599 break;
600 udelay(10);
601 }
602
603 if (status != APE_LOCK_GRANT_DRIVER) {
604 /* Revoke the lock request. */
605 tg3_ape_write32(tp, TG3_APE_LOCK_GRANT + off,
606 APE_LOCK_GRANT_DRIVER);
607
608 ret = -EBUSY;
609 }
610
611 return ret;
612}
613
614static void tg3_ape_unlock(struct tg3 *tp, int locknum)
615{
616 int off;
617
618 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
619 return;
620
621 switch (locknum) {
Matt Carlson33f401a2010-04-05 10:19:27 +0000622 case TG3_APE_LOCK_GRC:
623 case TG3_APE_LOCK_MEM:
624 break;
625 default:
626 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700627 }
628
629 off = 4 * locknum;
630 tg3_ape_write32(tp, TG3_APE_LOCK_GRANT + off, APE_LOCK_GRANT_DRIVER);
631}
632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633static void tg3_disable_ints(struct tg3 *tp)
634{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000635 int i;
636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 tw32(TG3PCI_MISC_HOST_CTRL,
638 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000639 for (i = 0; i < tp->irq_max; i++)
640 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643static void tg3_enable_ints(struct tg3 *tp)
644{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000645 int i;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000646
Michael Chanbbe832c2005-06-24 20:20:04 -0700647 tp->irq_sync = 0;
648 wmb();
649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 tw32(TG3PCI_MISC_HOST_CTRL,
651 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000652
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000653 tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000654 for (i = 0; i < tp->irq_cnt; i++) {
655 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000656
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000657 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
658 if (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)
659 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
660
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000661 tp->coal_now |= tnapi->coal_now;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000662 }
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000663
664 /* Force an initial interrupt */
665 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) &&
666 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
667 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
668 else
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000669 tw32(HOSTCC_MODE, tp->coal_now);
670
671 tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672}
673
Matt Carlson17375d22009-08-28 14:02:18 +0000674static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
Michael Chan04237dd2005-04-25 15:17:17 -0700675{
Matt Carlson17375d22009-08-28 14:02:18 +0000676 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +0000677 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan04237dd2005-04-25 15:17:17 -0700678 unsigned int work_exists = 0;
679
680 /* check for phy events */
681 if (!(tp->tg3_flags &
682 (TG3_FLAG_USE_LINKCHG_REG |
683 TG3_FLAG_POLL_SERDES))) {
684 if (sblk->status & SD_STATUS_LINK_CHG)
685 work_exists = 1;
686 }
687 /* check for RX/TX work to do */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000688 if (sblk->idx[0].tx_consumer != tnapi->tx_cons ||
Matt Carlson8d9d7cf2009-09-01 13:19:05 +0000689 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -0700690 work_exists = 1;
691
692 return work_exists;
693}
694
Matt Carlson17375d22009-08-28 14:02:18 +0000695/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -0700696 * similar to tg3_enable_ints, but it accurately determines whether there
697 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400698 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 */
Matt Carlson17375d22009-08-28 14:02:18 +0000700static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
Matt Carlson17375d22009-08-28 14:02:18 +0000702 struct tg3 *tp = tnapi->tp;
703
Matt Carlson898a56f2009-08-28 14:02:40 +0000704 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 mmiowb();
706
David S. Millerfac9b832005-05-18 22:46:34 -0700707 /* When doing tagged status, this work check is unnecessary.
708 * The last_tag we write above tells the chip which piece of
709 * work we've completed.
710 */
711 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) &&
Matt Carlson17375d22009-08-28 14:02:18 +0000712 tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -0700713 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +0000714 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715}
716
Matt Carlsonfed97812009-09-01 13:10:19 +0000717static void tg3_napi_disable(struct tg3 *tp)
718{
719 int i;
720
721 for (i = tp->irq_cnt - 1; i >= 0; i--)
722 napi_disable(&tp->napi[i].napi);
723}
724
725static void tg3_napi_enable(struct tg3 *tp)
726{
727 int i;
728
729 for (i = 0; i < tp->irq_cnt; i++)
730 napi_enable(&tp->napi[i].napi);
731}
732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733static inline void tg3_netif_stop(struct tg3 *tp)
734{
Michael Chanbbe832c2005-06-24 20:20:04 -0700735 tp->dev->trans_start = jiffies; /* prevent tx timeout */
Matt Carlsonfed97812009-09-01 13:10:19 +0000736 tg3_napi_disable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 netif_tx_disable(tp->dev);
738}
739
740static inline void tg3_netif_start(struct tg3 *tp)
741{
Matt Carlsonfe5f5782009-09-01 13:09:39 +0000742 /* NOTE: unconditional netif_tx_wake_all_queues is only
743 * appropriate so long as all callers are assured to
744 * have free tx slots (such as after tg3_init_hw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 */
Matt Carlsonfe5f5782009-09-01 13:09:39 +0000746 netif_tx_wake_all_queues(tp->dev);
747
Matt Carlsonfed97812009-09-01 13:10:19 +0000748 tg3_napi_enable(tp);
749 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
David S. Millerf47c11e2005-06-24 20:18:35 -0700750 tg3_enable_ints(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751}
752
753static void tg3_switch_clocks(struct tg3 *tp)
754{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000755 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 u32 orig_clock_ctrl;
757
Matt Carlson795d01c2007-10-07 23:28:17 -0700758 if ((tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
759 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -0700760 return;
761
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000762 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 orig_clock_ctrl = clock_ctrl;
765 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
766 CLOCK_CTRL_CLKRUN_OENABLE |
767 0x1f);
768 tp->pci_clock_ctrl = clock_ctrl;
769
770 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
771 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800772 tw32_wait_f(TG3PCI_CLOCK_CTRL,
773 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 }
775 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800776 tw32_wait_f(TG3PCI_CLOCK_CTRL,
777 clock_ctrl |
778 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
779 40);
780 tw32_wait_f(TG3PCI_CLOCK_CTRL,
781 clock_ctrl | (CLOCK_CTRL_ALTCLK),
782 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 }
Michael Chanb401e9e2005-12-19 16:27:04 -0800784 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785}
786
787#define PHY_BUSY_LOOPS 5000
788
789static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
790{
791 u32 frame_val;
792 unsigned int loops;
793 int ret;
794
795 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
796 tw32_f(MAC_MI_MODE,
797 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
798 udelay(80);
799 }
800
801 *val = 0x0;
802
Matt Carlson882e9792009-09-01 13:21:36 +0000803 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 MI_COM_PHY_ADDR_MASK);
805 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
806 MI_COM_REG_ADDR_MASK);
807 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 tw32_f(MAC_MI_COM, frame_val);
810
811 loops = PHY_BUSY_LOOPS;
812 while (loops != 0) {
813 udelay(10);
814 frame_val = tr32(MAC_MI_COM);
815
816 if ((frame_val & MI_COM_BUSY) == 0) {
817 udelay(5);
818 frame_val = tr32(MAC_MI_COM);
819 break;
820 }
821 loops -= 1;
822 }
823
824 ret = -EBUSY;
825 if (loops != 0) {
826 *val = frame_val & MI_COM_DATA_MASK;
827 ret = 0;
828 }
829
830 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
831 tw32_f(MAC_MI_MODE, tp->mi_mode);
832 udelay(80);
833 }
834
835 return ret;
836}
837
838static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
839{
840 u32 frame_val;
841 unsigned int loops;
842 int ret;
843
Matt Carlson7f97a4b2009-08-25 10:10:03 +0000844 if ((tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) &&
Michael Chanb5d37722006-09-27 16:06:21 -0700845 (reg == MII_TG3_CTRL || reg == MII_TG3_AUX_CTRL))
846 return 0;
847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
849 tw32_f(MAC_MI_MODE,
850 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
851 udelay(80);
852 }
853
Matt Carlson882e9792009-09-01 13:21:36 +0000854 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 MI_COM_PHY_ADDR_MASK);
856 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
857 MI_COM_REG_ADDR_MASK);
858 frame_val |= (val & MI_COM_DATA_MASK);
859 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 tw32_f(MAC_MI_COM, frame_val);
862
863 loops = PHY_BUSY_LOOPS;
864 while (loops != 0) {
865 udelay(10);
866 frame_val = tr32(MAC_MI_COM);
867 if ((frame_val & MI_COM_BUSY) == 0) {
868 udelay(5);
869 frame_val = tr32(MAC_MI_COM);
870 break;
871 }
872 loops -= 1;
873 }
874
875 ret = -EBUSY;
876 if (loops != 0)
877 ret = 0;
878
879 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
880 tw32_f(MAC_MI_MODE, tp->mi_mode);
881 udelay(80);
882 }
883
884 return ret;
885}
886
Matt Carlson95e28692008-05-25 23:44:14 -0700887static int tg3_bmcr_reset(struct tg3 *tp)
888{
889 u32 phy_control;
890 int limit, err;
891
892 /* OK, reset it, and poll the BMCR_RESET bit until it
893 * clears or we time out.
894 */
895 phy_control = BMCR_RESET;
896 err = tg3_writephy(tp, MII_BMCR, phy_control);
897 if (err != 0)
898 return -EBUSY;
899
900 limit = 5000;
901 while (limit--) {
902 err = tg3_readphy(tp, MII_BMCR, &phy_control);
903 if (err != 0)
904 return -EBUSY;
905
906 if ((phy_control & BMCR_RESET) == 0) {
907 udelay(40);
908 break;
909 }
910 udelay(10);
911 }
Roel Kluind4675b52009-02-12 16:33:27 -0800912 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -0700913 return -EBUSY;
914
915 return 0;
916}
917
Matt Carlson158d7ab2008-05-29 01:37:54 -0700918static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
919{
Francois Romieu3d165432009-01-19 16:56:50 -0800920 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700921 u32 val;
922
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000923 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -0700924
925 if (tg3_readphy(tp, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000926 val = -EIO;
927
928 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -0700929
930 return val;
931}
932
933static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
934{
Francois Romieu3d165432009-01-19 16:56:50 -0800935 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000936 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700937
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000938 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -0700939
940 if (tg3_writephy(tp, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000941 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700942
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000943 spin_unlock_bh(&tp->lock);
944
945 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700946}
947
948static int tg3_mdio_reset(struct mii_bus *bp)
949{
950 return 0;
951}
952
Matt Carlson9c61d6b2008-11-03 16:54:56 -0800953static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -0700954{
955 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800956 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -0700957
Matt Carlson3f0e3ad2009-11-02 14:24:36 +0000958 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800959 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +0000960 case PHY_ID_BCM50610:
961 case PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800962 val = MAC_PHYCFG2_50610_LED_MODES;
963 break;
Matt Carlson6a443a02010-02-17 15:17:04 +0000964 case PHY_ID_BCMAC131:
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800965 val = MAC_PHYCFG2_AC131_LED_MODES;
966 break;
Matt Carlson6a443a02010-02-17 15:17:04 +0000967 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800968 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
969 break;
Matt Carlson6a443a02010-02-17 15:17:04 +0000970 case PHY_ID_RTL8201E:
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800971 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
972 break;
973 default:
Matt Carlsona9daf362008-05-25 23:49:44 -0700974 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800975 }
976
977 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
978 tw32(MAC_PHYCFG2, val);
979
980 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +0000981 val &= ~(MAC_PHYCFG1_RGMII_INT |
982 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
983 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800984 tw32(MAC_PHYCFG1, val);
985
986 return;
987 }
988
Matt Carlson14417062010-02-17 15:16:59 +0000989 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE))
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800990 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
991 MAC_PHYCFG2_FMODE_MASK_MASK |
992 MAC_PHYCFG2_GMODE_MASK_MASK |
993 MAC_PHYCFG2_ACT_MASK_MASK |
994 MAC_PHYCFG2_QUAL_MASK_MASK |
995 MAC_PHYCFG2_INBAND_ENABLE;
996
997 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -0700998
Matt Carlsonbb85fbb2009-08-25 10:09:07 +0000999 val = tr32(MAC_PHYCFG1);
1000 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
1001 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
Matt Carlson14417062010-02-17 15:16:59 +00001002 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE)) {
Matt Carlsona9daf362008-05-25 23:49:44 -07001003 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
1004 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
1005 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
1006 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1007 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001008 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1009 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1010 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001011
Matt Carlsona9daf362008-05-25 23:49:44 -07001012 val = tr32(MAC_EXT_RGMII_MODE);
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 MAC_RGMII_MODE_TX_ENABLE |
1018 MAC_RGMII_MODE_TX_LOWPWR |
1019 MAC_RGMII_MODE_TX_RESET);
Matt Carlson14417062010-02-17 15:16:59 +00001020 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE)) {
Matt Carlsona9daf362008-05-25 23:49:44 -07001021 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
1022 val |= MAC_RGMII_MODE_RX_INT_B |
1023 MAC_RGMII_MODE_RX_QUALITY |
1024 MAC_RGMII_MODE_RX_ACTIVITY |
1025 MAC_RGMII_MODE_RX_ENG_DET;
1026 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
1027 val |= MAC_RGMII_MODE_TX_ENABLE |
1028 MAC_RGMII_MODE_TX_LOWPWR |
1029 MAC_RGMII_MODE_TX_RESET;
1030 }
1031 tw32(MAC_EXT_RGMII_MODE, val);
1032}
1033
Matt Carlson158d7ab2008-05-29 01:37:54 -07001034static void tg3_mdio_start(struct tg3 *tp)
1035{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001036 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1037 tw32_f(MAC_MI_MODE, tp->mi_mode);
1038 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001039
Matt Carlson9ea48182010-02-17 15:17:01 +00001040 if ((tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED) &&
1041 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1042 tg3_mdio_config_5785(tp);
1043}
1044
1045static int tg3_mdio_init(struct tg3 *tp)
1046{
1047 int i;
1048 u32 reg;
1049 struct phy_device *phydev;
1050
Matt Carlson882e9792009-09-01 13:21:36 +00001051 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
1052 u32 funcnum, is_serdes;
1053
1054 funcnum = tr32(TG3_CPMU_STATUS) & TG3_CPMU_STATUS_PCIE_FUNC;
1055 if (funcnum)
1056 tp->phy_addr = 2;
1057 else
1058 tp->phy_addr = 1;
1059
Matt Carlsond1ec96a2010-01-12 10:11:38 +00001060 if (tp->pci_chip_rev_id != CHIPREV_ID_5717_A0)
1061 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1062 else
1063 is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1064 TG3_CPMU_PHY_STRAP_IS_SERDES;
Matt Carlson882e9792009-09-01 13:21:36 +00001065 if (is_serdes)
1066 tp->phy_addr += 7;
1067 } else
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001068 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001069
Matt Carlson158d7ab2008-05-29 01:37:54 -07001070 tg3_mdio_start(tp);
1071
1072 if (!(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) ||
1073 (tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED))
1074 return 0;
1075
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001076 tp->mdio_bus = mdiobus_alloc();
1077 if (tp->mdio_bus == NULL)
1078 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001079
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001080 tp->mdio_bus->name = "tg3 mdio bus";
1081 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001082 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001083 tp->mdio_bus->priv = tp;
1084 tp->mdio_bus->parent = &tp->pdev->dev;
1085 tp->mdio_bus->read = &tg3_mdio_read;
1086 tp->mdio_bus->write = &tg3_mdio_write;
1087 tp->mdio_bus->reset = &tg3_mdio_reset;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001088 tp->mdio_bus->phy_mask = ~(1 << TG3_PHY_MII_ADDR);
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001089 tp->mdio_bus->irq = &tp->mdio_irq[0];
Matt Carlson158d7ab2008-05-29 01:37:54 -07001090
1091 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001092 tp->mdio_bus->irq[i] = PHY_POLL;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001093
1094 /* The bus registration will look for all the PHYs on the mdio bus.
1095 * Unfortunately, it does not ensure the PHY is powered up before
1096 * accessing the PHY ID registers. A chip reset is the
1097 * quickest way to bring the device back to an operational state..
1098 */
1099 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1100 tg3_bmcr_reset(tp);
1101
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001102 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001103 if (i) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001104 dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001105 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001106 return i;
1107 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001108
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001109 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsona9daf362008-05-25 23:49:44 -07001110
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001111 if (!phydev || !phydev->drv) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001112 dev_warn(&tp->pdev->dev, "No PHY devices\n");
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001113 mdiobus_unregister(tp->mdio_bus);
1114 mdiobus_free(tp->mdio_bus);
1115 return -ENODEV;
1116 }
1117
1118 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001119 case PHY_ID_BCM57780:
Matt Carlson321d32a2008-11-21 17:22:19 -08001120 phydev->interface = PHY_INTERFACE_MODE_GMII;
Matt Carlsonc704dc22009-11-02 14:32:12 +00001121 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08001122 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001123 case PHY_ID_BCM50610:
1124 case PHY_ID_BCM50610M:
Matt Carlson32e5a8d2009-11-02 14:31:39 +00001125 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001126 PHY_BRCM_RX_REFCLK_UNUSED |
Matt Carlson52fae082009-11-02 14:32:38 +00001127 PHY_BRCM_DIS_TXCRXC_NOENRGY |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001128 PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson14417062010-02-17 15:16:59 +00001129 if (tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE)
Matt Carlsona9daf362008-05-25 23:49:44 -07001130 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
1131 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
1132 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
1133 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
1134 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001135 /* fallthru */
Matt Carlson6a443a02010-02-17 15:17:04 +00001136 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001137 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001138 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001139 case PHY_ID_RTL8201E:
1140 case PHY_ID_BCMAC131:
Matt Carlsona9daf362008-05-25 23:49:44 -07001141 phydev->interface = PHY_INTERFACE_MODE_MII;
Matt Carlsoncdd4e09d2009-11-02 14:31:11 +00001142 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001143 tp->tg3_flags3 |= TG3_FLG3_PHY_IS_FET;
Matt Carlsona9daf362008-05-25 23:49:44 -07001144 break;
1145 }
1146
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001147 tp->tg3_flags3 |= TG3_FLG3_MDIOBUS_INITED;
1148
1149 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1150 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001151
1152 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001153}
1154
1155static void tg3_mdio_fini(struct tg3 *tp)
1156{
1157 if (tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED) {
1158 tp->tg3_flags3 &= ~TG3_FLG3_MDIOBUS_INITED;
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001159 mdiobus_unregister(tp->mdio_bus);
1160 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001161 }
1162}
1163
Matt Carlson95e28692008-05-25 23:44:14 -07001164/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001165static inline void tg3_generate_fw_event(struct tg3 *tp)
1166{
1167 u32 val;
1168
1169 val = tr32(GRC_RX_CPU_EVENT);
1170 val |= GRC_RX_CPU_DRIVER_EVENT;
1171 tw32_f(GRC_RX_CPU_EVENT, val);
1172
1173 tp->last_event_jiffies = jiffies;
1174}
1175
1176#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1177
1178/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001179static void tg3_wait_for_event_ack(struct tg3 *tp)
1180{
1181 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001182 unsigned int delay_cnt;
1183 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001184
Matt Carlson4ba526c2008-08-15 14:10:04 -07001185 /* If enough time has passed, no wait is necessary. */
1186 time_remain = (long)(tp->last_event_jiffies + 1 +
1187 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1188 (long)jiffies;
1189 if (time_remain < 0)
1190 return;
1191
1192 /* Check if we can shorten the wait time. */
1193 delay_cnt = jiffies_to_usecs(time_remain);
1194 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1195 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1196 delay_cnt = (delay_cnt >> 3) + 1;
1197
1198 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001199 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1200 break;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001201 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001202 }
1203}
1204
1205/* tp->lock is held. */
1206static void tg3_ump_link_report(struct tg3 *tp)
1207{
1208 u32 reg;
1209 u32 val;
1210
1211 if (!(tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
1212 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
1213 return;
1214
1215 tg3_wait_for_event_ack(tp);
1216
1217 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1218
1219 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1220
1221 val = 0;
1222 if (!tg3_readphy(tp, MII_BMCR, &reg))
1223 val = reg << 16;
1224 if (!tg3_readphy(tp, MII_BMSR, &reg))
1225 val |= (reg & 0xffff);
1226 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX, val);
1227
1228 val = 0;
1229 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1230 val = reg << 16;
1231 if (!tg3_readphy(tp, MII_LPA, &reg))
1232 val |= (reg & 0xffff);
1233 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 4, val);
1234
1235 val = 0;
1236 if (!(tp->tg3_flags2 & TG3_FLG2_MII_SERDES)) {
1237 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1238 val = reg << 16;
1239 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1240 val |= (reg & 0xffff);
1241 }
1242 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 8, val);
1243
1244 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1245 val = reg << 16;
1246 else
1247 val = 0;
1248 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 12, val);
1249
Matt Carlson4ba526c2008-08-15 14:10:04 -07001250 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001251}
1252
1253static void tg3_link_report(struct tg3 *tp)
1254{
1255 if (!netif_carrier_ok(tp->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001256 netif_info(tp, link, tp->dev, "Link is down\n");
Matt Carlson95e28692008-05-25 23:44:14 -07001257 tg3_ump_link_report(tp);
1258 } else if (netif_msg_link(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001259 netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
1260 (tp->link_config.active_speed == SPEED_1000 ?
1261 1000 :
1262 (tp->link_config.active_speed == SPEED_100 ?
1263 100 : 10)),
1264 (tp->link_config.active_duplex == DUPLEX_FULL ?
1265 "full" : "half"));
Matt Carlson95e28692008-05-25 23:44:14 -07001266
Joe Perches05dbe002010-02-17 19:44:19 +00001267 netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
1268 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
1269 "on" : "off",
1270 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
1271 "on" : "off");
Matt Carlson95e28692008-05-25 23:44:14 -07001272 tg3_ump_link_report(tp);
1273 }
1274}
1275
1276static u16 tg3_advert_flowctrl_1000T(u8 flow_ctrl)
1277{
1278 u16 miireg;
1279
Steve Glendinninge18ce342008-12-16 02:00:00 -08001280 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001281 miireg = ADVERTISE_PAUSE_CAP;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001282 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001283 miireg = ADVERTISE_PAUSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001284 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001285 miireg = ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1286 else
1287 miireg = 0;
1288
1289 return miireg;
1290}
1291
1292static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1293{
1294 u16 miireg;
1295
Steve Glendinninge18ce342008-12-16 02:00:00 -08001296 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001297 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001298 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001299 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001300 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001301 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1302 else
1303 miireg = 0;
1304
1305 return miireg;
1306}
1307
Matt Carlson95e28692008-05-25 23:44:14 -07001308static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1309{
1310 u8 cap = 0;
1311
1312 if (lcladv & ADVERTISE_1000XPAUSE) {
1313 if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1314 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001315 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001316 else if (rmtadv & LPA_1000XPAUSE_ASYM)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001317 cap = FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001318 } else {
1319 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001320 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001321 }
1322 } else if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1323 if ((rmtadv & LPA_1000XPAUSE) && (rmtadv & LPA_1000XPAUSE_ASYM))
Steve Glendinninge18ce342008-12-16 02:00:00 -08001324 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001325 }
1326
1327 return cap;
1328}
1329
Matt Carlsonf51f3562008-05-25 23:45:08 -07001330static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001331{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001332 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001333 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001334 u32 old_rx_mode = tp->rx_mode;
1335 u32 old_tx_mode = tp->tx_mode;
1336
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001337 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001338 autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001339 else
1340 autoneg = tp->link_config.autoneg;
1341
1342 if (autoneg == AUTONEG_ENABLE &&
Matt Carlson95e28692008-05-25 23:44:14 -07001343 (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG)) {
1344 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001345 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001346 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001347 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001348 } else
1349 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001350
Matt Carlsonf51f3562008-05-25 23:45:08 -07001351 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001352
Steve Glendinninge18ce342008-12-16 02:00:00 -08001353 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001354 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1355 else
1356 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1357
Matt Carlsonf51f3562008-05-25 23:45:08 -07001358 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001359 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001360
Steve Glendinninge18ce342008-12-16 02:00:00 -08001361 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001362 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1363 else
1364 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1365
Matt Carlsonf51f3562008-05-25 23:45:08 -07001366 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001367 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001368}
1369
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001370static void tg3_adjust_link(struct net_device *dev)
1371{
1372 u8 oldflowctrl, linkmesg = 0;
1373 u32 mac_mode, lcl_adv, rmt_adv;
1374 struct tg3 *tp = netdev_priv(dev);
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001375 struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001376
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001377 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001378
1379 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
1380 MAC_MODE_HALF_DUPLEX);
1381
1382 oldflowctrl = tp->link_config.active_flowctrl;
1383
1384 if (phydev->link) {
1385 lcl_adv = 0;
1386 rmt_adv = 0;
1387
1388 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
1389 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001390 else if (phydev->speed == SPEED_1000 ||
1391 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001392 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001393 else
1394 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001395
1396 if (phydev->duplex == DUPLEX_HALF)
1397 mac_mode |= MAC_MODE_HALF_DUPLEX;
1398 else {
1399 lcl_adv = tg3_advert_flowctrl_1000T(
1400 tp->link_config.flowctrl);
1401
1402 if (phydev->pause)
1403 rmt_adv = LPA_PAUSE_CAP;
1404 if (phydev->asym_pause)
1405 rmt_adv |= LPA_PAUSE_ASYM;
1406 }
1407
1408 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
1409 } else
1410 mac_mode |= MAC_MODE_PORT_MODE_GMII;
1411
1412 if (mac_mode != tp->mac_mode) {
1413 tp->mac_mode = mac_mode;
1414 tw32_f(MAC_MODE, tp->mac_mode);
1415 udelay(40);
1416 }
1417
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001418 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
1419 if (phydev->speed == SPEED_10)
1420 tw32(MAC_MI_STAT,
1421 MAC_MI_STAT_10MBPS_MODE |
1422 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1423 else
1424 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1425 }
1426
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001427 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
1428 tw32(MAC_TX_LENGTHS,
1429 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1430 (6 << TX_LENGTHS_IPG_SHIFT) |
1431 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
1432 else
1433 tw32(MAC_TX_LENGTHS,
1434 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1435 (6 << TX_LENGTHS_IPG_SHIFT) |
1436 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
1437
1438 if ((phydev->link && tp->link_config.active_speed == SPEED_INVALID) ||
1439 (!phydev->link && tp->link_config.active_speed != SPEED_INVALID) ||
1440 phydev->speed != tp->link_config.active_speed ||
1441 phydev->duplex != tp->link_config.active_duplex ||
1442 oldflowctrl != tp->link_config.active_flowctrl)
Matt Carlsonc6cdf432010-04-05 10:19:26 +00001443 linkmesg = 1;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001444
1445 tp->link_config.active_speed = phydev->speed;
1446 tp->link_config.active_duplex = phydev->duplex;
1447
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001448 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001449
1450 if (linkmesg)
1451 tg3_link_report(tp);
1452}
1453
1454static int tg3_phy_init(struct tg3 *tp)
1455{
1456 struct phy_device *phydev;
1457
1458 if (tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED)
1459 return 0;
1460
1461 /* Bring the PHY back to a known state. */
1462 tg3_bmcr_reset(tp);
1463
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001464 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001465
1466 /* Attach the MAC to the PHY. */
Kay Sieversfb28ad32008-11-10 13:55:14 -08001467 phydev = phy_connect(tp->dev, dev_name(&phydev->dev), tg3_adjust_link,
Matt Carlsona9daf362008-05-25 23:49:44 -07001468 phydev->dev_flags, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001469 if (IS_ERR(phydev)) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001470 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001471 return PTR_ERR(phydev);
1472 }
1473
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001474 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001475 switch (phydev->interface) {
1476 case PHY_INTERFACE_MODE_GMII:
1477 case PHY_INTERFACE_MODE_RGMII:
Matt Carlson321d32a2008-11-21 17:22:19 -08001478 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY)) {
1479 phydev->supported &= (PHY_GBIT_FEATURES |
1480 SUPPORTED_Pause |
1481 SUPPORTED_Asym_Pause);
1482 break;
1483 }
1484 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001485 case PHY_INTERFACE_MODE_MII:
1486 phydev->supported &= (PHY_BASIC_FEATURES |
1487 SUPPORTED_Pause |
1488 SUPPORTED_Asym_Pause);
1489 break;
1490 default:
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001491 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001492 return -EINVAL;
1493 }
1494
1495 tp->tg3_flags3 |= TG3_FLG3_PHY_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001496
1497 phydev->advertising = phydev->supported;
1498
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001499 return 0;
1500}
1501
1502static void tg3_phy_start(struct tg3 *tp)
1503{
1504 struct phy_device *phydev;
1505
1506 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
1507 return;
1508
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001509 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001510
1511 if (tp->link_config.phy_is_low_power) {
1512 tp->link_config.phy_is_low_power = 0;
1513 phydev->speed = tp->link_config.orig_speed;
1514 phydev->duplex = tp->link_config.orig_duplex;
1515 phydev->autoneg = tp->link_config.orig_autoneg;
1516 phydev->advertising = tp->link_config.orig_advertising;
1517 }
1518
1519 phy_start(phydev);
1520
1521 phy_start_aneg(phydev);
1522}
1523
1524static void tg3_phy_stop(struct tg3 *tp)
1525{
1526 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
1527 return;
1528
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001529 phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001530}
1531
1532static void tg3_phy_fini(struct tg3 *tp)
1533{
1534 if (tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001535 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001536 tp->tg3_flags3 &= ~TG3_FLG3_PHY_CONNECTED;
1537 }
1538}
1539
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001540static void tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
1541{
1542 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1543 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
1544}
1545
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001546static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
1547{
1548 u32 phytest;
1549
1550 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
1551 u32 phy;
1552
1553 tg3_writephy(tp, MII_TG3_FET_TEST,
1554 phytest | MII_TG3_FET_SHADOW_EN);
1555 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
1556 if (enable)
1557 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
1558 else
1559 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
1560 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
1561 }
1562 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
1563 }
1564}
1565
Matt Carlson6833c042008-11-21 17:18:59 -08001566static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
1567{
1568 u32 reg;
1569
Matt Carlsonecf14102010-01-20 16:58:05 +00001570 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
1571 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 &&
1572 (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08001573 return;
1574
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001575 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
1576 tg3_phy_fet_toggle_apd(tp, enable);
1577 return;
1578 }
1579
Matt Carlson6833c042008-11-21 17:18:59 -08001580 reg = MII_TG3_MISC_SHDW_WREN |
1581 MII_TG3_MISC_SHDW_SCR5_SEL |
1582 MII_TG3_MISC_SHDW_SCR5_LPED |
1583 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
1584 MII_TG3_MISC_SHDW_SCR5_SDTL |
1585 MII_TG3_MISC_SHDW_SCR5_C125OE;
1586 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 || !enable)
1587 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
1588
1589 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1590
1591
1592 reg = MII_TG3_MISC_SHDW_WREN |
1593 MII_TG3_MISC_SHDW_APD_SEL |
1594 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
1595 if (enable)
1596 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
1597
1598 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1599}
1600
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001601static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
1602{
1603 u32 phy;
1604
1605 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
1606 (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES))
1607 return;
1608
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001609 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001610 u32 ephy;
1611
Matt Carlson535ef6e2009-08-25 10:09:36 +00001612 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
1613 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
1614
1615 tg3_writephy(tp, MII_TG3_FET_TEST,
1616 ephy | MII_TG3_FET_SHADOW_EN);
1617 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001618 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00001619 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001620 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00001621 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
1622 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001623 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00001624 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001625 }
1626 } else {
1627 phy = MII_TG3_AUXCTL_MISC_RDSEL_MISC |
1628 MII_TG3_AUXCTL_SHDWSEL_MISC;
1629 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, phy) &&
1630 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &phy)) {
1631 if (enable)
1632 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1633 else
1634 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1635 phy |= MII_TG3_AUXCTL_MISC_WREN;
1636 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1637 }
1638 }
1639}
1640
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641static void tg3_phy_set_wirespeed(struct tg3 *tp)
1642{
1643 u32 val;
1644
1645 if (tp->tg3_flags2 & TG3_FLG2_NO_ETH_WIRE_SPEED)
1646 return;
1647
1648 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x7007) &&
1649 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &val))
1650 tg3_writephy(tp, MII_TG3_AUX_CTRL,
1651 (val | (1 << 15) | (1 << 4)));
1652}
1653
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001654static void tg3_phy_apply_otp(struct tg3 *tp)
1655{
1656 u32 otp, phy;
1657
1658 if (!tp->phy_otp)
1659 return;
1660
1661 otp = tp->phy_otp;
1662
1663 /* Enable SM_DSP clock and tx 6dB coding. */
1664 phy = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1665 MII_TG3_AUXCTL_ACTL_SMDSP_ENA |
1666 MII_TG3_AUXCTL_ACTL_TX_6DB;
1667 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1668
1669 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
1670 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
1671 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
1672
1673 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
1674 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
1675 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
1676
1677 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
1678 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
1679 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
1680
1681 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
1682 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
1683
1684 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
1685 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
1686
1687 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
1688 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
1689 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
1690
1691 /* Turn off SM_DSP clock. */
1692 phy = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1693 MII_TG3_AUXCTL_ACTL_TX_6DB;
1694 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1695}
1696
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697static int tg3_wait_macro_done(struct tg3 *tp)
1698{
1699 int limit = 100;
1700
1701 while (limit--) {
1702 u32 tmp32;
1703
1704 if (!tg3_readphy(tp, 0x16, &tmp32)) {
1705 if ((tmp32 & 0x1000) == 0)
1706 break;
1707 }
1708 }
Roel Kluind4675b52009-02-12 16:33:27 -08001709 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 return -EBUSY;
1711
1712 return 0;
1713}
1714
1715static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
1716{
1717 static const u32 test_pat[4][6] = {
1718 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
1719 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
1720 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
1721 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
1722 };
1723 int chan;
1724
1725 for (chan = 0; chan < 4; chan++) {
1726 int i;
1727
1728 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1729 (chan * 0x2000) | 0x0200);
1730 tg3_writephy(tp, 0x16, 0x0002);
1731
1732 for (i = 0; i < 6; i++)
1733 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
1734 test_pat[chan][i]);
1735
1736 tg3_writephy(tp, 0x16, 0x0202);
1737 if (tg3_wait_macro_done(tp)) {
1738 *resetp = 1;
1739 return -EBUSY;
1740 }
1741
1742 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1743 (chan * 0x2000) | 0x0200);
1744 tg3_writephy(tp, 0x16, 0x0082);
1745 if (tg3_wait_macro_done(tp)) {
1746 *resetp = 1;
1747 return -EBUSY;
1748 }
1749
1750 tg3_writephy(tp, 0x16, 0x0802);
1751 if (tg3_wait_macro_done(tp)) {
1752 *resetp = 1;
1753 return -EBUSY;
1754 }
1755
1756 for (i = 0; i < 6; i += 2) {
1757 u32 low, high;
1758
1759 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
1760 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
1761 tg3_wait_macro_done(tp)) {
1762 *resetp = 1;
1763 return -EBUSY;
1764 }
1765 low &= 0x7fff;
1766 high &= 0x000f;
1767 if (low != test_pat[chan][i] ||
1768 high != test_pat[chan][i+1]) {
1769 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
1770 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
1771 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
1772
1773 return -EBUSY;
1774 }
1775 }
1776 }
1777
1778 return 0;
1779}
1780
1781static int tg3_phy_reset_chanpat(struct tg3 *tp)
1782{
1783 int chan;
1784
1785 for (chan = 0; chan < 4; chan++) {
1786 int i;
1787
1788 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1789 (chan * 0x2000) | 0x0200);
1790 tg3_writephy(tp, 0x16, 0x0002);
1791 for (i = 0; i < 6; i++)
1792 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
1793 tg3_writephy(tp, 0x16, 0x0202);
1794 if (tg3_wait_macro_done(tp))
1795 return -EBUSY;
1796 }
1797
1798 return 0;
1799}
1800
1801static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
1802{
1803 u32 reg32, phy9_orig;
1804 int retries, do_phy_reset, err;
1805
1806 retries = 10;
1807 do_phy_reset = 1;
1808 do {
1809 if (do_phy_reset) {
1810 err = tg3_bmcr_reset(tp);
1811 if (err)
1812 return err;
1813 do_phy_reset = 0;
1814 }
1815
1816 /* Disable transmitter and interrupt. */
1817 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
1818 continue;
1819
1820 reg32 |= 0x3000;
1821 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
1822
1823 /* Set full-duplex, 1000 mbps. */
1824 tg3_writephy(tp, MII_BMCR,
1825 BMCR_FULLDPLX | TG3_BMCR_SPEED1000);
1826
1827 /* Set to master mode. */
1828 if (tg3_readphy(tp, MII_TG3_CTRL, &phy9_orig))
1829 continue;
1830
1831 tg3_writephy(tp, MII_TG3_CTRL,
1832 (MII_TG3_CTRL_AS_MASTER |
1833 MII_TG3_CTRL_ENABLE_AS_MASTER));
1834
1835 /* Enable SM_DSP_CLOCK and 6dB. */
1836 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
1837
1838 /* Block the PHY control access. */
1839 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8005);
1840 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0800);
1841
1842 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
1843 if (!err)
1844 break;
1845 } while (--retries);
1846
1847 err = tg3_phy_reset_chanpat(tp);
1848 if (err)
1849 return err;
1850
1851 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8005);
1852 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0000);
1853
1854 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
1855 tg3_writephy(tp, 0x16, 0x0000);
1856
1857 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
1858 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
1859 /* Set Extended packet length bit for jumbo frames */
1860 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4400);
Matt Carlson859a588792010-04-05 10:19:28 +00001861 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
1863 }
1864
1865 tg3_writephy(tp, MII_TG3_CTRL, phy9_orig);
1866
1867 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
1868 reg32 &= ~0x3000;
1869 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
1870 } else if (!err)
1871 err = -EBUSY;
1872
1873 return err;
1874}
1875
1876/* This will reset the tigon3 PHY if there is no valid
1877 * link unless the FORCE argument is non-zero.
1878 */
1879static int tg3_phy_reset(struct tg3 *tp)
1880{
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001881 u32 cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 u32 phy_status;
1883 int err;
1884
Michael Chan60189dd2006-12-17 17:08:07 -08001885 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
1886 u32 val;
1887
1888 val = tr32(GRC_MISC_CFG);
1889 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
1890 udelay(40);
1891 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 err = tg3_readphy(tp, MII_BMSR, &phy_status);
1893 err |= tg3_readphy(tp, MII_BMSR, &phy_status);
1894 if (err != 0)
1895 return -EBUSY;
1896
Michael Chanc8e1e822006-04-29 18:55:17 -07001897 if (netif_running(tp->dev) && netif_carrier_ok(tp->dev)) {
1898 netif_carrier_off(tp->dev);
1899 tg3_link_report(tp);
1900 }
1901
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
1903 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
1904 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
1905 err = tg3_phy_reset_5703_4_5(tp);
1906 if (err)
1907 return err;
1908 goto out;
1909 }
1910
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001911 cpmuctrl = 0;
1912 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
1913 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
1914 cpmuctrl = tr32(TG3_CPMU_CTRL);
1915 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
1916 tw32(TG3_CPMU_CTRL,
1917 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
1918 }
1919
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 err = tg3_bmcr_reset(tp);
1921 if (err)
1922 return err;
1923
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001924 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
1925 u32 phy;
1926
1927 phy = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
1928 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, phy);
1929
1930 tw32(TG3_CPMU_CTRL, cpmuctrl);
1931 }
1932
Matt Carlsonbcb37f62008-11-03 16:52:09 -08001933 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
1934 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08001935 u32 val;
1936
1937 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
1938 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
1939 CPMU_LSPD_1000MB_MACCLK_12_5) {
1940 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
1941 udelay(40);
1942 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
1943 }
1944 }
1945
Matt Carlsonecf14102010-01-20 16:58:05 +00001946 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 &&
1947 (tp->tg3_flags2 & TG3_FLG2_MII_SERDES))
1948 return 0;
1949
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001950 tg3_phy_apply_otp(tp);
1951
Matt Carlson6833c042008-11-21 17:18:59 -08001952 if (tp->tg3_flags3 & TG3_FLG3_PHY_ENABLE_APD)
1953 tg3_phy_toggle_apd(tp, true);
1954 else
1955 tg3_phy_toggle_apd(tp, false);
1956
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957out:
1958 if (tp->tg3_flags2 & TG3_FLG2_PHY_ADC_BUG) {
1959 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
1960 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x201f);
1961 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x2aaa);
1962 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
1963 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0323);
1964 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
1965 }
1966 if (tp->tg3_flags2 & TG3_FLG2_PHY_5704_A0_BUG) {
1967 tg3_writephy(tp, 0x1c, 0x8d68);
1968 tg3_writephy(tp, 0x1c, 0x8d68);
1969 }
1970 if (tp->tg3_flags2 & TG3_FLG2_PHY_BER_BUG) {
1971 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
1972 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
1973 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x310b);
1974 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x201f);
1975 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x9506);
1976 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x401f);
1977 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x14e2);
1978 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
Matt Carlson859a588792010-04-05 10:19:28 +00001979 } else if (tp->tg3_flags2 & TG3_FLG2_PHY_JITTER_BUG) {
Michael Chanc424cb22006-04-29 18:56:34 -07001980 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
1981 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
Michael Chanc1d2a192007-01-08 19:57:20 -08001982 if (tp->tg3_flags2 & TG3_FLG2_PHY_ADJUST_TRIM) {
1983 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
1984 tg3_writephy(tp, MII_TG3_TEST1,
1985 MII_TG3_TEST1_TRIM_EN | 0x4);
1986 } else
1987 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
Michael Chanc424cb22006-04-29 18:56:34 -07001988 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
1989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 /* Set Extended packet length bit (bit 14) on all chips that */
1991 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00001992 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 /* Cannot do read-modify-write on 5401 */
1994 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4c20);
Matt Carlson8f666b02009-08-28 13:58:24 +00001995 } else if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 u32 phy_reg;
1997
1998 /* Set bit 14 with read-modify-write to preserve other bits */
1999 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0007) &&
2000 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &phy_reg))
2001 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy_reg | 0x4000);
2002 }
2003
2004 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2005 * jumbo frames transmission.
2006 */
Matt Carlson8f666b02009-08-28 13:58:24 +00002007 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 u32 phy_reg;
2009
2010 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &phy_reg))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002011 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2012 phy_reg | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 }
2014
Michael Chan715116a2006-09-27 16:09:25 -07002015 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002016 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002017 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002018 }
2019
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002020 tg3_phy_toggle_automdix(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 tg3_phy_set_wirespeed(tp);
2022 return 0;
2023}
2024
2025static void tg3_frob_aux_power(struct tg3 *tp)
2026{
2027 struct tg3 *tp_peer = tp;
2028
Matt Carlson334355a2010-01-20 16:58:10 +00002029 /* The GPIOs do something completely different on 57765. */
2030 if ((tp->tg3_flags2 & TG3_FLG2_IS_NIC) == 0 ||
2031 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 return;
2033
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00002034 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2035 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
2036 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002037 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002039 dev_peer = pci_get_drvdata(tp->pdev_peer);
Michael Chanbc1c7562006-03-20 17:48:03 -08002040 /* remove_one() may have been run on the peer. */
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002041 if (!dev_peer)
Michael Chanbc1c7562006-03-20 17:48:03 -08002042 tp_peer = tp;
2043 else
2044 tp_peer = netdev_priv(dev_peer);
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
2047 if ((tp->tg3_flags & TG3_FLAG_WOL_ENABLE) != 0 ||
Michael Chan6921d202005-12-13 21:15:53 -08002048 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0 ||
2049 (tp_peer->tg3_flags & TG3_FLAG_WOL_ENABLE) != 0 ||
2050 (tp_peer->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2052 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Michael Chanb401e9e2005-12-19 16:27:04 -08002053 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2054 (GRC_LCLCTRL_GPIO_OE0 |
2055 GRC_LCLCTRL_GPIO_OE1 |
2056 GRC_LCLCTRL_GPIO_OE2 |
2057 GRC_LCLCTRL_GPIO_OUTPUT0 |
2058 GRC_LCLCTRL_GPIO_OUTPUT1),
2059 100);
Matt Carlson8d519ab2009-04-20 06:58:01 +00002060 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2061 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -07002062 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2063 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2064 GRC_LCLCTRL_GPIO_OE1 |
2065 GRC_LCLCTRL_GPIO_OE2 |
2066 GRC_LCLCTRL_GPIO_OUTPUT0 |
2067 GRC_LCLCTRL_GPIO_OUTPUT1 |
2068 tp->grc_local_ctrl;
2069 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2070
2071 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2072 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2073
2074 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2075 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 } else {
2077 u32 no_gpio2;
Michael Chandc56b7d2005-12-19 16:26:28 -08002078 u32 grc_local_ctrl = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
2080 if (tp_peer != tp &&
2081 (tp_peer->tg3_flags & TG3_FLAG_INIT_COMPLETE) != 0)
2082 return;
2083
Michael Chandc56b7d2005-12-19 16:26:28 -08002084 /* Workaround to prevent overdrawing Amps. */
2085 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2086 ASIC_REV_5714) {
2087 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chanb401e9e2005-12-19 16:27:04 -08002088 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2089 grc_local_ctrl, 100);
Michael Chandc56b7d2005-12-19 16:26:28 -08002090 }
2091
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 /* On 5753 and variants, GPIO2 cannot be used. */
2093 no_gpio2 = tp->nic_sram_data_cfg &
2094 NIC_SRAM_DATA_CFG_NO_GPIO2;
2095
Michael Chandc56b7d2005-12-19 16:26:28 -08002096 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 GRC_LCLCTRL_GPIO_OE1 |
2098 GRC_LCLCTRL_GPIO_OE2 |
2099 GRC_LCLCTRL_GPIO_OUTPUT1 |
2100 GRC_LCLCTRL_GPIO_OUTPUT2;
2101 if (no_gpio2) {
2102 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2103 GRC_LCLCTRL_GPIO_OUTPUT2);
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 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2109
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 if (!no_gpio2) {
2114 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chanb401e9e2005-12-19 16:27:04 -08002115 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2116 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 }
2118 }
2119 } else {
2120 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
2121 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
2122 if (tp_peer != tp &&
2123 (tp_peer->tg3_flags & TG3_FLAG_INIT_COMPLETE) != 0)
2124 return;
2125
Michael Chanb401e9e2005-12-19 16:27:04 -08002126 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2127 (GRC_LCLCTRL_GPIO_OE1 |
2128 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
Michael Chanb401e9e2005-12-19 16:27:04 -08002130 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2131 GRC_LCLCTRL_GPIO_OE1, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
Michael Chanb401e9e2005-12-19 16:27:04 -08002133 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2134 (GRC_LCLCTRL_GPIO_OE1 |
2135 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 }
2137 }
2138}
2139
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002140static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2141{
2142 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2143 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00002144 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002145 if (speed != SPEED_10)
2146 return 1;
2147 } else if (speed == SPEED_10)
2148 return 1;
2149
2150 return 0;
2151}
2152
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153static int tg3_setup_phy(struct tg3 *, int);
2154
2155#define RESET_KIND_SHUTDOWN 0
2156#define RESET_KIND_INIT 1
2157#define RESET_KIND_SUSPEND 2
2158
2159static void tg3_write_sig_post_reset(struct tg3 *, int);
2160static int tg3_halt_cpu(struct tg3 *, u32);
2161
Matt Carlson0a459aa2008-11-03 16:54:15 -08002162static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002163{
Matt Carlsonce057f02007-11-12 21:08:03 -08002164 u32 val;
2165
Michael Chan51297242007-02-13 12:17:57 -08002166 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
2167 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2168 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2169 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2170
2171 sg_dig_ctrl |=
2172 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2173 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2174 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2175 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002176 return;
Michael Chan51297242007-02-13 12:17:57 -08002177 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002178
Michael Chan60189dd2006-12-17 17:08:07 -08002179 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002180 tg3_bmcr_reset(tp);
2181 val = tr32(GRC_MISC_CFG);
2182 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2183 udelay(40);
2184 return;
Matt Carlson0e5f7842009-11-02 14:26:38 +00002185 } else if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
2186 u32 phytest;
2187 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2188 u32 phy;
2189
2190 tg3_writephy(tp, MII_ADVERTISE, 0);
2191 tg3_writephy(tp, MII_BMCR,
2192 BMCR_ANENABLE | BMCR_ANRESTART);
2193
2194 tg3_writephy(tp, MII_TG3_FET_TEST,
2195 phytest | MII_TG3_FET_SHADOW_EN);
2196 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
2197 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
2198 tg3_writephy(tp,
2199 MII_TG3_FET_SHDW_AUXMODE4,
2200 phy);
2201 }
2202 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2203 }
2204 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002205 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07002206 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2207 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002208
2209 tg3_writephy(tp, MII_TG3_AUX_CTRL,
2210 MII_TG3_AUXCTL_SHDWSEL_PWRCTL |
2211 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2212 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2213 MII_TG3_AUXCTL_PCTL_VREG_11V);
Michael Chan715116a2006-09-27 16:09:25 -07002214 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002215
Michael Chan15c3b692006-03-22 01:06:52 -08002216 /* The PHY should not be powered down on some chips because
2217 * of bugs.
2218 */
2219 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2220 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2221 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
2222 (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)))
2223 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08002224
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002225 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2226 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002227 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2228 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2229 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2230 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2231 }
2232
Michael Chan15c3b692006-03-22 01:06:52 -08002233 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2234}
2235
Matt Carlson3f007892008-11-03 16:51:36 -08002236/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002237static int tg3_nvram_lock(struct tg3 *tp)
2238{
2239 if (tp->tg3_flags & TG3_FLAG_NVRAM) {
2240 int i;
2241
2242 if (tp->nvram_lock_cnt == 0) {
2243 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2244 for (i = 0; i < 8000; i++) {
2245 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2246 break;
2247 udelay(20);
2248 }
2249 if (i == 8000) {
2250 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2251 return -ENODEV;
2252 }
2253 }
2254 tp->nvram_lock_cnt++;
2255 }
2256 return 0;
2257}
2258
2259/* tp->lock is held. */
2260static void tg3_nvram_unlock(struct tg3 *tp)
2261{
2262 if (tp->tg3_flags & TG3_FLAG_NVRAM) {
2263 if (tp->nvram_lock_cnt > 0)
2264 tp->nvram_lock_cnt--;
2265 if (tp->nvram_lock_cnt == 0)
2266 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
2267 }
2268}
2269
2270/* tp->lock is held. */
2271static void tg3_enable_nvram_access(struct tg3 *tp)
2272{
2273 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Matt Carlsonf66a29b2009-11-13 13:03:36 +00002274 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002275 u32 nvaccess = tr32(NVRAM_ACCESS);
2276
2277 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
2278 }
2279}
2280
2281/* tp->lock is held. */
2282static void tg3_disable_nvram_access(struct tg3 *tp)
2283{
2284 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Matt Carlsonf66a29b2009-11-13 13:03:36 +00002285 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002286 u32 nvaccess = tr32(NVRAM_ACCESS);
2287
2288 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
2289 }
2290}
2291
2292static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
2293 u32 offset, u32 *val)
2294{
2295 u32 tmp;
2296 int i;
2297
2298 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
2299 return -EINVAL;
2300
2301 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
2302 EEPROM_ADDR_DEVID_MASK |
2303 EEPROM_ADDR_READ);
2304 tw32(GRC_EEPROM_ADDR,
2305 tmp |
2306 (0 << EEPROM_ADDR_DEVID_SHIFT) |
2307 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
2308 EEPROM_ADDR_ADDR_MASK) |
2309 EEPROM_ADDR_READ | EEPROM_ADDR_START);
2310
2311 for (i = 0; i < 1000; i++) {
2312 tmp = tr32(GRC_EEPROM_ADDR);
2313
2314 if (tmp & EEPROM_ADDR_COMPLETE)
2315 break;
2316 msleep(1);
2317 }
2318 if (!(tmp & EEPROM_ADDR_COMPLETE))
2319 return -EBUSY;
2320
Matt Carlson62cedd12009-04-20 14:52:29 -07002321 tmp = tr32(GRC_EEPROM_DATA);
2322
2323 /*
2324 * The data will always be opposite the native endian
2325 * format. Perform a blind byteswap to compensate.
2326 */
2327 *val = swab32(tmp);
2328
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002329 return 0;
2330}
2331
2332#define NVRAM_CMD_TIMEOUT 10000
2333
2334static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
2335{
2336 int i;
2337
2338 tw32(NVRAM_CMD, nvram_cmd);
2339 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
2340 udelay(10);
2341 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
2342 udelay(10);
2343 break;
2344 }
2345 }
2346
2347 if (i == NVRAM_CMD_TIMEOUT)
2348 return -EBUSY;
2349
2350 return 0;
2351}
2352
2353static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
2354{
2355 if ((tp->tg3_flags & TG3_FLAG_NVRAM) &&
2356 (tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) &&
2357 (tp->tg3_flags2 & TG3_FLG2_FLASH) &&
2358 !(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM_ADDR_TRANS) &&
2359 (tp->nvram_jedecnum == JEDEC_ATMEL))
2360
2361 addr = ((addr / tp->nvram_pagesize) <<
2362 ATMEL_AT45DB0X1B_PAGE_POS) +
2363 (addr % tp->nvram_pagesize);
2364
2365 return addr;
2366}
2367
2368static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
2369{
2370 if ((tp->tg3_flags & TG3_FLAG_NVRAM) &&
2371 (tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) &&
2372 (tp->tg3_flags2 & TG3_FLG2_FLASH) &&
2373 !(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM_ADDR_TRANS) &&
2374 (tp->nvram_jedecnum == JEDEC_ATMEL))
2375
2376 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
2377 tp->nvram_pagesize) +
2378 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
2379
2380 return addr;
2381}
2382
Matt Carlsone4f34112009-02-25 14:25:00 +00002383/* NOTE: Data read in from NVRAM is byteswapped according to
2384 * the byteswapping settings for all other register accesses.
2385 * tg3 devices are BE devices, so on a BE machine, the data
2386 * returned will be exactly as it is seen in NVRAM. On a LE
2387 * machine, the 32-bit value will be byteswapped.
2388 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002389static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
2390{
2391 int ret;
2392
2393 if (!(tp->tg3_flags & TG3_FLAG_NVRAM))
2394 return tg3_nvram_read_using_eeprom(tp, offset, val);
2395
2396 offset = tg3_nvram_phys_addr(tp, offset);
2397
2398 if (offset > NVRAM_ADDR_MSK)
2399 return -EINVAL;
2400
2401 ret = tg3_nvram_lock(tp);
2402 if (ret)
2403 return ret;
2404
2405 tg3_enable_nvram_access(tp);
2406
2407 tw32(NVRAM_ADDR, offset);
2408 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
2409 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
2410
2411 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00002412 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002413
2414 tg3_disable_nvram_access(tp);
2415
2416 tg3_nvram_unlock(tp);
2417
2418 return ret;
2419}
2420
Matt Carlsona9dc5292009-02-25 14:25:30 +00002421/* Ensures NVRAM data is in bytestream format. */
2422static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002423{
2424 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00002425 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002426 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00002427 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002428 return res;
2429}
2430
2431/* tp->lock is held. */
Matt Carlson3f007892008-11-03 16:51:36 -08002432static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
2433{
2434 u32 addr_high, addr_low;
2435 int i;
2436
2437 addr_high = ((tp->dev->dev_addr[0] << 8) |
2438 tp->dev->dev_addr[1]);
2439 addr_low = ((tp->dev->dev_addr[2] << 24) |
2440 (tp->dev->dev_addr[3] << 16) |
2441 (tp->dev->dev_addr[4] << 8) |
2442 (tp->dev->dev_addr[5] << 0));
2443 for (i = 0; i < 4; i++) {
2444 if (i == 1 && skip_mac_1)
2445 continue;
2446 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
2447 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
2448 }
2449
2450 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2451 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2452 for (i = 0; i < 12; i++) {
2453 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
2454 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
2455 }
2456 }
2457
2458 addr_high = (tp->dev->dev_addr[0] +
2459 tp->dev->dev_addr[1] +
2460 tp->dev->dev_addr[2] +
2461 tp->dev->dev_addr[3] +
2462 tp->dev->dev_addr[4] +
2463 tp->dev->dev_addr[5]) &
2464 TX_BACKOFF_SEED_MASK;
2465 tw32(MAC_TX_BACKOFF_SEED, addr_high);
2466}
2467
Michael Chanbc1c7562006-03-20 17:48:03 -08002468static int tg3_set_power_state(struct tg3 *tp, pci_power_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469{
2470 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002471 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472
2473 /* Make sure register accesses (indirect or otherwise)
2474 * will function correctly.
2475 */
2476 pci_write_config_dword(tp->pdev,
2477 TG3PCI_MISC_HOST_CTRL,
2478 tp->misc_host_ctrl);
2479
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 switch (state) {
Michael Chanbc1c7562006-03-20 17:48:03 -08002481 case PCI_D0:
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07002482 pci_enable_wake(tp->pdev, state, false);
2483 pci_set_power_state(tp->pdev, PCI_D0);
Michael Chan8c6bda12005-04-21 17:09:08 -07002484
Michael Chan9d26e212006-12-07 00:21:14 -08002485 /* Switch out of Vaux if it is a NIC */
2486 if (tp->tg3_flags2 & TG3_FLG2_IS_NIC)
Michael Chanb401e9e2005-12-19 16:27:04 -08002487 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
2489 return 0;
2490
Michael Chanbc1c7562006-03-20 17:48:03 -08002491 case PCI_D1:
Michael Chanbc1c7562006-03-20 17:48:03 -08002492 case PCI_D2:
Michael Chanbc1c7562006-03-20 17:48:03 -08002493 case PCI_D3hot:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 break;
2495
2496 default:
Joe Perches05dbe002010-02-17 19:44:19 +00002497 netdev_err(tp->dev, "Invalid power state (D%d) requested\n",
2498 state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002500 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002501
2502 /* Restore the CLKREQ setting. */
2503 if (tp->tg3_flags3 & TG3_FLG3_CLKREQ_BUG) {
2504 u16 lnkctl;
2505
2506 pci_read_config_word(tp->pdev,
2507 tp->pcie_cap + PCI_EXP_LNKCTL,
2508 &lnkctl);
2509 lnkctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
2510 pci_write_config_word(tp->pdev,
2511 tp->pcie_cap + PCI_EXP_LNKCTL,
2512 lnkctl);
2513 }
2514
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
2516 tw32(TG3PCI_MISC_HOST_CTRL,
2517 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
2518
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002519 device_should_wake = pci_pme_capable(tp->pdev, state) &&
2520 device_may_wakeup(&tp->pdev->dev) &&
2521 (tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
2522
Matt Carlsondd477002008-05-25 23:45:58 -07002523 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08002524 do_low_power = false;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002525 if ((tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED) &&
2526 !tp->link_config.phy_is_low_power) {
2527 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002528 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002529
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002530 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002531
2532 tp->link_config.phy_is_low_power = 1;
2533
2534 tp->link_config.orig_speed = phydev->speed;
2535 tp->link_config.orig_duplex = phydev->duplex;
2536 tp->link_config.orig_autoneg = phydev->autoneg;
2537 tp->link_config.orig_advertising = phydev->advertising;
2538
2539 advertising = ADVERTISED_TP |
2540 ADVERTISED_Pause |
2541 ADVERTISED_Autoneg |
2542 ADVERTISED_10baseT_Half;
2543
2544 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002545 device_should_wake) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002546 if (tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB)
2547 advertising |=
2548 ADVERTISED_100baseT_Half |
2549 ADVERTISED_100baseT_Full |
2550 ADVERTISED_10baseT_Full;
2551 else
2552 advertising |= ADVERTISED_10baseT_Full;
2553 }
2554
2555 phydev->advertising = advertising;
2556
2557 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002558
2559 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00002560 if (phyid != PHY_ID_BCMAC131) {
2561 phyid &= PHY_BCM_OUI_MASK;
2562 if (phyid == PHY_BCM_OUI_1 ||
2563 phyid == PHY_BCM_OUI_2 ||
2564 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08002565 do_low_power = true;
2566 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002567 }
Matt Carlsondd477002008-05-25 23:45:58 -07002568 } else {
Matt Carlson20232762008-12-21 20:18:56 -08002569 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002570
Matt Carlsondd477002008-05-25 23:45:58 -07002571 if (tp->link_config.phy_is_low_power == 0) {
2572 tp->link_config.phy_is_low_power = 1;
2573 tp->link_config.orig_speed = tp->link_config.speed;
2574 tp->link_config.orig_duplex = tp->link_config.duplex;
2575 tp->link_config.orig_autoneg = tp->link_config.autoneg;
2576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577
Matt Carlsondd477002008-05-25 23:45:58 -07002578 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)) {
2579 tp->link_config.speed = SPEED_10;
2580 tp->link_config.duplex = DUPLEX_HALF;
2581 tp->link_config.autoneg = AUTONEG_ENABLE;
2582 tg3_setup_phy(tp, 0);
2583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 }
2585
Michael Chanb5d37722006-09-27 16:06:21 -07002586 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
2587 u32 val;
2588
2589 val = tr32(GRC_VCPU_EXT_CTRL);
2590 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
2591 } else if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08002592 int i;
2593 u32 val;
2594
2595 for (i = 0; i < 200; i++) {
2596 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
2597 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
2598 break;
2599 msleep(1);
2600 }
2601 }
Gary Zambranoa85feb82007-05-05 11:52:19 -07002602 if (tp->tg3_flags & TG3_FLAG_WOL_CAP)
2603 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
2604 WOL_DRV_STATE_SHUTDOWN |
2605 WOL_DRV_WOL |
2606 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08002607
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002608 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 u32 mac_mode;
2610
2611 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08002612 if (do_low_power) {
Matt Carlsondd477002008-05-25 23:45:58 -07002613 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x5a);
2614 udelay(40);
2615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616
Michael Chan3f7045c2006-09-27 16:02:29 -07002617 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)
2618 mac_mode = MAC_MODE_PORT_MODE_GMII;
2619 else
2620 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002622 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
2623 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2624 ASIC_REV_5700) {
2625 u32 speed = (tp->tg3_flags &
2626 TG3_FLAG_WOL_SPEED_100MB) ?
2627 SPEED_100 : SPEED_10;
2628 if (tg3_5700_link_polarity(tp, speed))
2629 mac_mode |= MAC_MODE_LINK_POLARITY;
2630 else
2631 mac_mode &= ~MAC_MODE_LINK_POLARITY;
2632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 } else {
2634 mac_mode = MAC_MODE_PORT_MODE_TBI;
2635 }
2636
John W. Linvillecbf46852005-04-21 17:01:29 -07002637 if (!(tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 tw32(MAC_LED_CTRL, tp->led_ctrl);
2639
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002640 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
2641 if (((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
2642 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) &&
2643 ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
2644 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)))
2645 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646
Matt Carlson3bda1252008-08-15 14:08:22 -07002647 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
2648 mac_mode |= tp->mac_mode &
2649 (MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN);
2650 if (mac_mode & MAC_MODE_APE_TX_EN)
2651 mac_mode |= MAC_MODE_TDE_ENABLE;
2652 }
2653
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 tw32_f(MAC_MODE, mac_mode);
2655 udelay(100);
2656
2657 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
2658 udelay(10);
2659 }
2660
2661 if (!(tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB) &&
2662 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2663 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
2664 u32 base_val;
2665
2666 base_val = tp->pci_clock_ctrl;
2667 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
2668 CLOCK_CTRL_TXCLK_DISABLE);
2669
Michael Chanb401e9e2005-12-19 16:27:04 -08002670 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
2671 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Michael Chand7b0a852007-02-13 12:17:38 -08002672 } else if ((tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
Matt Carlson795d01c2007-10-07 23:28:17 -07002673 (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
Michael Chand7b0a852007-02-13 12:17:38 -08002674 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)) {
Michael Chan4cf78e42005-07-25 12:29:19 -07002675 /* do nothing */
Michael Chan85e94ce2005-04-21 17:05:28 -07002676 } else if (!((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF))) {
2678 u32 newbits1, newbits2;
2679
2680 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2681 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2682 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
2683 CLOCK_CTRL_TXCLK_DISABLE |
2684 CLOCK_CTRL_ALTCLK);
2685 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2686 } else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
2687 newbits1 = CLOCK_CTRL_625_CORE;
2688 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
2689 } else {
2690 newbits1 = CLOCK_CTRL_ALTCLK;
2691 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2692 }
2693
Michael Chanb401e9e2005-12-19 16:27:04 -08002694 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
2695 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696
Michael Chanb401e9e2005-12-19 16:27:04 -08002697 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
2698 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699
2700 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
2701 u32 newbits3;
2702
2703 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2704 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2705 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
2706 CLOCK_CTRL_TXCLK_DISABLE |
2707 CLOCK_CTRL_44MHZ_CORE);
2708 } else {
2709 newbits3 = CLOCK_CTRL_44MHZ_CORE;
2710 }
2711
Michael Chanb401e9e2005-12-19 16:27:04 -08002712 tw32_wait_f(TG3PCI_CLOCK_CTRL,
2713 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 }
2715 }
2716
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002717 if (!(device_should_wake) &&
Matt Carlson22435842008-11-21 17:21:13 -08002718 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08002719 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08002720
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 tg3_frob_aux_power(tp);
2722
2723 /* Workaround for unstable PLL clock */
2724 if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
2725 (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
2726 u32 val = tr32(0x7d00);
2727
2728 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
2729 tw32(0x7d00, val);
Michael Chan6921d202005-12-13 21:15:53 -08002730 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08002731 int err;
2732
2733 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08002735 if (!err)
2736 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08002737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 }
2739
Michael Chanbbadf502006-04-06 21:46:34 -07002740 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
2741
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002742 if (device_should_wake)
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07002743 pci_enable_wake(tp->pdev, state, true);
2744
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 /* Finally, set the new power state. */
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07002746 pci_set_power_state(tp->pdev, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 return 0;
2749}
2750
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
2752{
2753 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
2754 case MII_TG3_AUX_STAT_10HALF:
2755 *speed = SPEED_10;
2756 *duplex = DUPLEX_HALF;
2757 break;
2758
2759 case MII_TG3_AUX_STAT_10FULL:
2760 *speed = SPEED_10;
2761 *duplex = DUPLEX_FULL;
2762 break;
2763
2764 case MII_TG3_AUX_STAT_100HALF:
2765 *speed = SPEED_100;
2766 *duplex = DUPLEX_HALF;
2767 break;
2768
2769 case MII_TG3_AUX_STAT_100FULL:
2770 *speed = SPEED_100;
2771 *duplex = DUPLEX_FULL;
2772 break;
2773
2774 case MII_TG3_AUX_STAT_1000HALF:
2775 *speed = SPEED_1000;
2776 *duplex = DUPLEX_HALF;
2777 break;
2778
2779 case MII_TG3_AUX_STAT_1000FULL:
2780 *speed = SPEED_1000;
2781 *duplex = DUPLEX_FULL;
2782 break;
2783
2784 default:
Matt Carlson7f97a4b2009-08-25 10:10:03 +00002785 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07002786 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
2787 SPEED_10;
2788 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
2789 DUPLEX_HALF;
2790 break;
2791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 *speed = SPEED_INVALID;
2793 *duplex = DUPLEX_INVALID;
2794 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796}
2797
2798static void tg3_phy_copper_begin(struct tg3 *tp)
2799{
2800 u32 new_adv;
2801 int i;
2802
2803 if (tp->link_config.phy_is_low_power) {
2804 /* Entering low power mode. Disable gigabit and
2805 * 100baseT advertisements.
2806 */
2807 tg3_writephy(tp, MII_TG3_CTRL, 0);
2808
2809 new_adv = (ADVERTISE_10HALF | ADVERTISE_10FULL |
2810 ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
2811 if (tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB)
2812 new_adv |= (ADVERTISE_100HALF | ADVERTISE_100FULL);
2813
2814 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2815 } else if (tp->link_config.speed == SPEED_INVALID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 if (tp->tg3_flags & TG3_FLAG_10_100_ONLY)
2817 tp->link_config.advertising &=
2818 ~(ADVERTISED_1000baseT_Half |
2819 ADVERTISED_1000baseT_Full);
2820
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002821 new_adv = ADVERTISE_CSMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 if (tp->link_config.advertising & ADVERTISED_10baseT_Half)
2823 new_adv |= ADVERTISE_10HALF;
2824 if (tp->link_config.advertising & ADVERTISED_10baseT_Full)
2825 new_adv |= ADVERTISE_10FULL;
2826 if (tp->link_config.advertising & ADVERTISED_100baseT_Half)
2827 new_adv |= ADVERTISE_100HALF;
2828 if (tp->link_config.advertising & ADVERTISED_100baseT_Full)
2829 new_adv |= ADVERTISE_100FULL;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002830
2831 new_adv |= tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2832
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2834
2835 if (tp->link_config.advertising &
2836 (ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full)) {
2837 new_adv = 0;
2838 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
2839 new_adv |= MII_TG3_CTRL_ADV_1000_HALF;
2840 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
2841 new_adv |= MII_TG3_CTRL_ADV_1000_FULL;
2842 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY) &&
2843 (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2844 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0))
2845 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2846 MII_TG3_CTRL_ENABLE_AS_MASTER);
2847 tg3_writephy(tp, MII_TG3_CTRL, new_adv);
2848 } else {
2849 tg3_writephy(tp, MII_TG3_CTRL, 0);
2850 }
2851 } else {
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002852 new_adv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2853 new_adv |= ADVERTISE_CSMA;
2854
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 /* Asking for a specific link mode. */
2856 if (tp->link_config.speed == SPEED_1000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2858
2859 if (tp->link_config.duplex == DUPLEX_FULL)
2860 new_adv = MII_TG3_CTRL_ADV_1000_FULL;
2861 else
2862 new_adv = MII_TG3_CTRL_ADV_1000_HALF;
2863 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2864 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
2865 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2866 MII_TG3_CTRL_ENABLE_AS_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 if (tp->link_config.speed == SPEED_100) {
2869 if (tp->link_config.duplex == DUPLEX_FULL)
2870 new_adv |= ADVERTISE_100FULL;
2871 else
2872 new_adv |= ADVERTISE_100HALF;
2873 } else {
2874 if (tp->link_config.duplex == DUPLEX_FULL)
2875 new_adv |= ADVERTISE_10FULL;
2876 else
2877 new_adv |= ADVERTISE_10HALF;
2878 }
2879 tg3_writephy(tp, MII_ADVERTISE, new_adv);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002880
2881 new_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002883
2884 tg3_writephy(tp, MII_TG3_CTRL, new_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 }
2886
2887 if (tp->link_config.autoneg == AUTONEG_DISABLE &&
2888 tp->link_config.speed != SPEED_INVALID) {
2889 u32 bmcr, orig_bmcr;
2890
2891 tp->link_config.active_speed = tp->link_config.speed;
2892 tp->link_config.active_duplex = tp->link_config.duplex;
2893
2894 bmcr = 0;
2895 switch (tp->link_config.speed) {
2896 default:
2897 case SPEED_10:
2898 break;
2899
2900 case SPEED_100:
2901 bmcr |= BMCR_SPEED100;
2902 break;
2903
2904 case SPEED_1000:
2905 bmcr |= TG3_BMCR_SPEED1000;
2906 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908
2909 if (tp->link_config.duplex == DUPLEX_FULL)
2910 bmcr |= BMCR_FULLDPLX;
2911
2912 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
2913 (bmcr != orig_bmcr)) {
2914 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
2915 for (i = 0; i < 1500; i++) {
2916 u32 tmp;
2917
2918 udelay(10);
2919 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
2920 tg3_readphy(tp, MII_BMSR, &tmp))
2921 continue;
2922 if (!(tmp & BMSR_LSTATUS)) {
2923 udelay(40);
2924 break;
2925 }
2926 }
2927 tg3_writephy(tp, MII_BMCR, bmcr);
2928 udelay(40);
2929 }
2930 } else {
2931 tg3_writephy(tp, MII_BMCR,
2932 BMCR_ANENABLE | BMCR_ANRESTART);
2933 }
2934}
2935
2936static int tg3_init_5401phy_dsp(struct tg3 *tp)
2937{
2938 int err;
2939
2940 /* Turn off tap power management. */
2941 /* Set Extended packet length bit */
2942 err = tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4c20);
2943
2944 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x0012);
2945 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x1804);
2946
2947 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x0013);
2948 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x1204);
2949
2950 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8006);
2951 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0132);
2952
2953 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8006);
2954 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0232);
2955
2956 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x201f);
2957 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0a20);
2958
2959 udelay(40);
2960
2961 return err;
2962}
2963
Michael Chan3600d912006-12-07 00:21:48 -08002964static int tg3_copper_is_advertising_all(struct tg3 *tp, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965{
Michael Chan3600d912006-12-07 00:21:48 -08002966 u32 adv_reg, all_mask = 0;
2967
2968 if (mask & ADVERTISED_10baseT_Half)
2969 all_mask |= ADVERTISE_10HALF;
2970 if (mask & ADVERTISED_10baseT_Full)
2971 all_mask |= ADVERTISE_10FULL;
2972 if (mask & ADVERTISED_100baseT_Half)
2973 all_mask |= ADVERTISE_100HALF;
2974 if (mask & ADVERTISED_100baseT_Full)
2975 all_mask |= ADVERTISE_100FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976
2977 if (tg3_readphy(tp, MII_ADVERTISE, &adv_reg))
2978 return 0;
2979
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 if ((adv_reg & all_mask) != all_mask)
2981 return 0;
2982 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY)) {
2983 u32 tg3_ctrl;
2984
Michael Chan3600d912006-12-07 00:21:48 -08002985 all_mask = 0;
2986 if (mask & ADVERTISED_1000baseT_Half)
2987 all_mask |= ADVERTISE_1000HALF;
2988 if (mask & ADVERTISED_1000baseT_Full)
2989 all_mask |= ADVERTISE_1000FULL;
2990
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 if (tg3_readphy(tp, MII_TG3_CTRL, &tg3_ctrl))
2992 return 0;
2993
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 if ((tg3_ctrl & all_mask) != all_mask)
2995 return 0;
2996 }
2997 return 1;
2998}
2999
Matt Carlsonef167e22007-12-20 20:10:01 -08003000static int tg3_adv_1000T_flowctrl_ok(struct tg3 *tp, u32 *lcladv, u32 *rmtadv)
3001{
3002 u32 curadv, reqadv;
3003
3004 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
3005 return 1;
3006
3007 curadv = *lcladv & (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
3008 reqadv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
3009
3010 if (tp->link_config.active_duplex == DUPLEX_FULL) {
3011 if (curadv != reqadv)
3012 return 0;
3013
3014 if (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG)
3015 tg3_readphy(tp, MII_LPA, rmtadv);
3016 } else {
3017 /* Reprogram the advertisement register, even if it
3018 * does not affect the current link. If the link
3019 * gets renegotiated in the future, we can save an
3020 * additional renegotiation cycle by advertising
3021 * it correctly in the first place.
3022 */
3023 if (curadv != reqadv) {
3024 *lcladv &= ~(ADVERTISE_PAUSE_CAP |
3025 ADVERTISE_PAUSE_ASYM);
3026 tg3_writephy(tp, MII_ADVERTISE, *lcladv | reqadv);
3027 }
3028 }
3029
3030 return 1;
3031}
3032
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
3034{
3035 int current_link_up;
3036 u32 bmsr, dummy;
Matt Carlsonef167e22007-12-20 20:10:01 -08003037 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 u16 current_speed;
3039 u8 current_duplex;
3040 int i, err;
3041
3042 tw32(MAC_EVENT, 0);
3043
3044 tw32_f(MAC_STATUS,
3045 (MAC_STATUS_SYNC_CHANGED |
3046 MAC_STATUS_CFG_CHANGED |
3047 MAC_STATUS_MI_COMPLETION |
3048 MAC_STATUS_LNKSTATE_CHANGED));
3049 udelay(40);
3050
Matt Carlson8ef21422008-05-02 16:47:53 -07003051 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
3052 tw32_f(MAC_MI_MODE,
3053 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
3054 udelay(80);
3055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056
3057 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x02);
3058
3059 /* Some third-party PHYs need to be reset on link going
3060 * down.
3061 */
3062 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3063 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
3064 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
3065 netif_carrier_ok(tp->dev)) {
3066 tg3_readphy(tp, MII_BMSR, &bmsr);
3067 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3068 !(bmsr & BMSR_LSTATUS))
3069 force_reset = 1;
3070 }
3071 if (force_reset)
3072 tg3_phy_reset(tp);
3073
Matt Carlson79eb6902010-02-17 15:17:03 +00003074 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 tg3_readphy(tp, MII_BMSR, &bmsr);
3076 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
3077 !(tp->tg3_flags & TG3_FLAG_INIT_COMPLETE))
3078 bmsr = 0;
3079
3080 if (!(bmsr & BMSR_LSTATUS)) {
3081 err = tg3_init_5401phy_dsp(tp);
3082 if (err)
3083 return err;
3084
3085 tg3_readphy(tp, MII_BMSR, &bmsr);
3086 for (i = 0; i < 1000; i++) {
3087 udelay(10);
3088 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3089 (bmsr & BMSR_LSTATUS)) {
3090 udelay(40);
3091 break;
3092 }
3093 }
3094
Matt Carlson79eb6902010-02-17 15:17:03 +00003095 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
3096 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 !(bmsr & BMSR_LSTATUS) &&
3098 tp->link_config.active_speed == SPEED_1000) {
3099 err = tg3_phy_reset(tp);
3100 if (!err)
3101 err = tg3_init_5401phy_dsp(tp);
3102 if (err)
3103 return err;
3104 }
3105 }
3106 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3107 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
3108 /* 5701 {A0,B0} CRC bug workaround */
3109 tg3_writephy(tp, 0x15, 0x0a75);
3110 tg3_writephy(tp, 0x1c, 0x8c68);
3111 tg3_writephy(tp, 0x1c, 0x8d68);
3112 tg3_writephy(tp, 0x1c, 0x8c68);
3113 }
3114
3115 /* Clear pending interrupts... */
3116 tg3_readphy(tp, MII_TG3_ISTAT, &dummy);
3117 tg3_readphy(tp, MII_TG3_ISTAT, &dummy);
3118
3119 if (tp->tg3_flags & TG3_FLAG_USE_MI_INTERRUPT)
3120 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlson7f97a4b2009-08-25 10:10:03 +00003121 else if (!(tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 tg3_writephy(tp, MII_TG3_IMASK, ~0);
3123
3124 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3125 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3126 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
3127 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3128 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
3129 else
3130 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
3131 }
3132
3133 current_link_up = 0;
3134 current_speed = SPEED_INVALID;
3135 current_duplex = DUPLEX_INVALID;
3136
3137 if (tp->tg3_flags2 & TG3_FLG2_CAPACITIVE_COUPLING) {
3138 u32 val;
3139
3140 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4007);
3141 tg3_readphy(tp, MII_TG3_AUX_CTRL, &val);
3142 if (!(val & (1 << 10))) {
3143 val |= (1 << 10);
3144 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
3145 goto relink;
3146 }
3147 }
3148
3149 bmsr = 0;
3150 for (i = 0; i < 100; i++) {
3151 tg3_readphy(tp, MII_BMSR, &bmsr);
3152 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3153 (bmsr & BMSR_LSTATUS))
3154 break;
3155 udelay(40);
3156 }
3157
3158 if (bmsr & BMSR_LSTATUS) {
3159 u32 aux_stat, bmcr;
3160
3161 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
3162 for (i = 0; i < 2000; i++) {
3163 udelay(10);
3164 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
3165 aux_stat)
3166 break;
3167 }
3168
3169 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
3170 &current_speed,
3171 &current_duplex);
3172
3173 bmcr = 0;
3174 for (i = 0; i < 200; i++) {
3175 tg3_readphy(tp, MII_BMCR, &bmcr);
3176 if (tg3_readphy(tp, MII_BMCR, &bmcr))
3177 continue;
3178 if (bmcr && bmcr != 0x7fff)
3179 break;
3180 udelay(10);
3181 }
3182
Matt Carlsonef167e22007-12-20 20:10:01 -08003183 lcl_adv = 0;
3184 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185
Matt Carlsonef167e22007-12-20 20:10:01 -08003186 tp->link_config.active_speed = current_speed;
3187 tp->link_config.active_duplex = current_duplex;
3188
3189 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3190 if ((bmcr & BMCR_ANENABLE) &&
3191 tg3_copper_is_advertising_all(tp,
3192 tp->link_config.advertising)) {
3193 if (tg3_adv_1000T_flowctrl_ok(tp, &lcl_adv,
3194 &rmt_adv))
3195 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 }
3197 } else {
3198 if (!(bmcr & BMCR_ANENABLE) &&
3199 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08003200 tp->link_config.duplex == current_duplex &&
3201 tp->link_config.flowctrl ==
3202 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 }
3205 }
3206
Matt Carlsonef167e22007-12-20 20:10:01 -08003207 if (current_link_up == 1 &&
3208 tp->link_config.active_duplex == DUPLEX_FULL)
3209 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 }
3211
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212relink:
Michael Chan6921d202005-12-13 21:15:53 -08003213 if (current_link_up == 0 || tp->link_config.phy_is_low_power) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 u32 tmp;
3215
3216 tg3_phy_copper_begin(tp);
3217
3218 tg3_readphy(tp, MII_BMSR, &tmp);
3219 if (!tg3_readphy(tp, MII_BMSR, &tmp) &&
3220 (tmp & BMSR_LSTATUS))
3221 current_link_up = 1;
3222 }
3223
3224 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
3225 if (current_link_up == 1) {
3226 if (tp->link_config.active_speed == SPEED_100 ||
3227 tp->link_config.active_speed == SPEED_10)
3228 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3229 else
3230 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlson7f97a4b2009-08-25 10:10:03 +00003231 } else if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET)
3232 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3233 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
3235
3236 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
3237 if (tp->link_config.active_duplex == DUPLEX_HALF)
3238 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
3239
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003241 if (current_link_up == 1 &&
3242 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003244 else
3245 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 }
3247
3248 /* ??? Without this setting Netgear GA302T PHY does not
3249 * ??? send/receive packets...
3250 */
Matt Carlson79eb6902010-02-17 15:17:03 +00003251 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
3253 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
3254 tw32_f(MAC_MI_MODE, tp->mi_mode);
3255 udelay(80);
3256 }
3257
3258 tw32_f(MAC_MODE, tp->mac_mode);
3259 udelay(40);
3260
3261 if (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) {
3262 /* Polled via timer. */
3263 tw32_f(MAC_EVENT, 0);
3264 } else {
3265 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
3266 }
3267 udelay(40);
3268
3269 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
3270 current_link_up == 1 &&
3271 tp->link_config.active_speed == SPEED_1000 &&
3272 ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) ||
3273 (tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED))) {
3274 udelay(120);
3275 tw32_f(MAC_STATUS,
3276 (MAC_STATUS_SYNC_CHANGED |
3277 MAC_STATUS_CFG_CHANGED));
3278 udelay(40);
3279 tg3_write_mem(tp,
3280 NIC_SRAM_FIRMWARE_MBOX,
3281 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
3282 }
3283
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003284 /* Prevent send BD corruption. */
3285 if (tp->tg3_flags3 & TG3_FLG3_CLKREQ_BUG) {
3286 u16 oldlnkctl, newlnkctl;
3287
3288 pci_read_config_word(tp->pdev,
3289 tp->pcie_cap + PCI_EXP_LNKCTL,
3290 &oldlnkctl);
3291 if (tp->link_config.active_speed == SPEED_100 ||
3292 tp->link_config.active_speed == SPEED_10)
3293 newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
3294 else
3295 newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
3296 if (newlnkctl != oldlnkctl)
3297 pci_write_config_word(tp->pdev,
3298 tp->pcie_cap + PCI_EXP_LNKCTL,
3299 newlnkctl);
3300 }
3301
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 if (current_link_up != netif_carrier_ok(tp->dev)) {
3303 if (current_link_up)
3304 netif_carrier_on(tp->dev);
3305 else
3306 netif_carrier_off(tp->dev);
3307 tg3_link_report(tp);
3308 }
3309
3310 return 0;
3311}
3312
3313struct tg3_fiber_aneginfo {
3314 int state;
3315#define ANEG_STATE_UNKNOWN 0
3316#define ANEG_STATE_AN_ENABLE 1
3317#define ANEG_STATE_RESTART_INIT 2
3318#define ANEG_STATE_RESTART 3
3319#define ANEG_STATE_DISABLE_LINK_OK 4
3320#define ANEG_STATE_ABILITY_DETECT_INIT 5
3321#define ANEG_STATE_ABILITY_DETECT 6
3322#define ANEG_STATE_ACK_DETECT_INIT 7
3323#define ANEG_STATE_ACK_DETECT 8
3324#define ANEG_STATE_COMPLETE_ACK_INIT 9
3325#define ANEG_STATE_COMPLETE_ACK 10
3326#define ANEG_STATE_IDLE_DETECT_INIT 11
3327#define ANEG_STATE_IDLE_DETECT 12
3328#define ANEG_STATE_LINK_OK 13
3329#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
3330#define ANEG_STATE_NEXT_PAGE_WAIT 15
3331
3332 u32 flags;
3333#define MR_AN_ENABLE 0x00000001
3334#define MR_RESTART_AN 0x00000002
3335#define MR_AN_COMPLETE 0x00000004
3336#define MR_PAGE_RX 0x00000008
3337#define MR_NP_LOADED 0x00000010
3338#define MR_TOGGLE_TX 0x00000020
3339#define MR_LP_ADV_FULL_DUPLEX 0x00000040
3340#define MR_LP_ADV_HALF_DUPLEX 0x00000080
3341#define MR_LP_ADV_SYM_PAUSE 0x00000100
3342#define MR_LP_ADV_ASYM_PAUSE 0x00000200
3343#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
3344#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
3345#define MR_LP_ADV_NEXT_PAGE 0x00001000
3346#define MR_TOGGLE_RX 0x00002000
3347#define MR_NP_RX 0x00004000
3348
3349#define MR_LINK_OK 0x80000000
3350
3351 unsigned long link_time, cur_time;
3352
3353 u32 ability_match_cfg;
3354 int ability_match_count;
3355
3356 char ability_match, idle_match, ack_match;
3357
3358 u32 txconfig, rxconfig;
3359#define ANEG_CFG_NP 0x00000080
3360#define ANEG_CFG_ACK 0x00000040
3361#define ANEG_CFG_RF2 0x00000020
3362#define ANEG_CFG_RF1 0x00000010
3363#define ANEG_CFG_PS2 0x00000001
3364#define ANEG_CFG_PS1 0x00008000
3365#define ANEG_CFG_HD 0x00004000
3366#define ANEG_CFG_FD 0x00002000
3367#define ANEG_CFG_INVAL 0x00001f06
3368
3369};
3370#define ANEG_OK 0
3371#define ANEG_DONE 1
3372#define ANEG_TIMER_ENAB 2
3373#define ANEG_FAILED -1
3374
3375#define ANEG_STATE_SETTLE_TIME 10000
3376
3377static int tg3_fiber_aneg_smachine(struct tg3 *tp,
3378 struct tg3_fiber_aneginfo *ap)
3379{
Matt Carlson5be73b42007-12-20 20:09:29 -08003380 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 unsigned long delta;
3382 u32 rx_cfg_reg;
3383 int ret;
3384
3385 if (ap->state == ANEG_STATE_UNKNOWN) {
3386 ap->rxconfig = 0;
3387 ap->link_time = 0;
3388 ap->cur_time = 0;
3389 ap->ability_match_cfg = 0;
3390 ap->ability_match_count = 0;
3391 ap->ability_match = 0;
3392 ap->idle_match = 0;
3393 ap->ack_match = 0;
3394 }
3395 ap->cur_time++;
3396
3397 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
3398 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
3399
3400 if (rx_cfg_reg != ap->ability_match_cfg) {
3401 ap->ability_match_cfg = rx_cfg_reg;
3402 ap->ability_match = 0;
3403 ap->ability_match_count = 0;
3404 } else {
3405 if (++ap->ability_match_count > 1) {
3406 ap->ability_match = 1;
3407 ap->ability_match_cfg = rx_cfg_reg;
3408 }
3409 }
3410 if (rx_cfg_reg & ANEG_CFG_ACK)
3411 ap->ack_match = 1;
3412 else
3413 ap->ack_match = 0;
3414
3415 ap->idle_match = 0;
3416 } else {
3417 ap->idle_match = 1;
3418 ap->ability_match_cfg = 0;
3419 ap->ability_match_count = 0;
3420 ap->ability_match = 0;
3421 ap->ack_match = 0;
3422
3423 rx_cfg_reg = 0;
3424 }
3425
3426 ap->rxconfig = rx_cfg_reg;
3427 ret = ANEG_OK;
3428
Matt Carlson33f401a2010-04-05 10:19:27 +00003429 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 case ANEG_STATE_UNKNOWN:
3431 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
3432 ap->state = ANEG_STATE_AN_ENABLE;
3433
3434 /* fallthru */
3435 case ANEG_STATE_AN_ENABLE:
3436 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
3437 if (ap->flags & MR_AN_ENABLE) {
3438 ap->link_time = 0;
3439 ap->cur_time = 0;
3440 ap->ability_match_cfg = 0;
3441 ap->ability_match_count = 0;
3442 ap->ability_match = 0;
3443 ap->idle_match = 0;
3444 ap->ack_match = 0;
3445
3446 ap->state = ANEG_STATE_RESTART_INIT;
3447 } else {
3448 ap->state = ANEG_STATE_DISABLE_LINK_OK;
3449 }
3450 break;
3451
3452 case ANEG_STATE_RESTART_INIT:
3453 ap->link_time = ap->cur_time;
3454 ap->flags &= ~(MR_NP_LOADED);
3455 ap->txconfig = 0;
3456 tw32(MAC_TX_AUTO_NEG, 0);
3457 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3458 tw32_f(MAC_MODE, tp->mac_mode);
3459 udelay(40);
3460
3461 ret = ANEG_TIMER_ENAB;
3462 ap->state = ANEG_STATE_RESTART;
3463
3464 /* fallthru */
3465 case ANEG_STATE_RESTART:
3466 delta = ap->cur_time - ap->link_time;
Matt Carlson859a588792010-04-05 10:19:28 +00003467 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a588792010-04-05 10:19:28 +00003469 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 break;
3472
3473 case ANEG_STATE_DISABLE_LINK_OK:
3474 ret = ANEG_DONE;
3475 break;
3476
3477 case ANEG_STATE_ABILITY_DETECT_INIT:
3478 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08003479 ap->txconfig = ANEG_CFG_FD;
3480 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3481 if (flowctrl & ADVERTISE_1000XPAUSE)
3482 ap->txconfig |= ANEG_CFG_PS1;
3483 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3484 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3486 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3487 tw32_f(MAC_MODE, tp->mac_mode);
3488 udelay(40);
3489
3490 ap->state = ANEG_STATE_ABILITY_DETECT;
3491 break;
3492
3493 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a588792010-04-05 10:19:28 +00003494 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 break;
3497
3498 case ANEG_STATE_ACK_DETECT_INIT:
3499 ap->txconfig |= ANEG_CFG_ACK;
3500 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3501 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3502 tw32_f(MAC_MODE, tp->mac_mode);
3503 udelay(40);
3504
3505 ap->state = ANEG_STATE_ACK_DETECT;
3506
3507 /* fallthru */
3508 case ANEG_STATE_ACK_DETECT:
3509 if (ap->ack_match != 0) {
3510 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
3511 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
3512 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
3513 } else {
3514 ap->state = ANEG_STATE_AN_ENABLE;
3515 }
3516 } else if (ap->ability_match != 0 &&
3517 ap->rxconfig == 0) {
3518 ap->state = ANEG_STATE_AN_ENABLE;
3519 }
3520 break;
3521
3522 case ANEG_STATE_COMPLETE_ACK_INIT:
3523 if (ap->rxconfig & ANEG_CFG_INVAL) {
3524 ret = ANEG_FAILED;
3525 break;
3526 }
3527 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
3528 MR_LP_ADV_HALF_DUPLEX |
3529 MR_LP_ADV_SYM_PAUSE |
3530 MR_LP_ADV_ASYM_PAUSE |
3531 MR_LP_ADV_REMOTE_FAULT1 |
3532 MR_LP_ADV_REMOTE_FAULT2 |
3533 MR_LP_ADV_NEXT_PAGE |
3534 MR_TOGGLE_RX |
3535 MR_NP_RX);
3536 if (ap->rxconfig & ANEG_CFG_FD)
3537 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
3538 if (ap->rxconfig & ANEG_CFG_HD)
3539 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
3540 if (ap->rxconfig & ANEG_CFG_PS1)
3541 ap->flags |= MR_LP_ADV_SYM_PAUSE;
3542 if (ap->rxconfig & ANEG_CFG_PS2)
3543 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
3544 if (ap->rxconfig & ANEG_CFG_RF1)
3545 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
3546 if (ap->rxconfig & ANEG_CFG_RF2)
3547 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
3548 if (ap->rxconfig & ANEG_CFG_NP)
3549 ap->flags |= MR_LP_ADV_NEXT_PAGE;
3550
3551 ap->link_time = ap->cur_time;
3552
3553 ap->flags ^= (MR_TOGGLE_TX);
3554 if (ap->rxconfig & 0x0008)
3555 ap->flags |= MR_TOGGLE_RX;
3556 if (ap->rxconfig & ANEG_CFG_NP)
3557 ap->flags |= MR_NP_RX;
3558 ap->flags |= MR_PAGE_RX;
3559
3560 ap->state = ANEG_STATE_COMPLETE_ACK;
3561 ret = ANEG_TIMER_ENAB;
3562 break;
3563
3564 case ANEG_STATE_COMPLETE_ACK:
3565 if (ap->ability_match != 0 &&
3566 ap->rxconfig == 0) {
3567 ap->state = ANEG_STATE_AN_ENABLE;
3568 break;
3569 }
3570 delta = ap->cur_time - ap->link_time;
3571 if (delta > ANEG_STATE_SETTLE_TIME) {
3572 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
3573 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3574 } else {
3575 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
3576 !(ap->flags & MR_NP_RX)) {
3577 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3578 } else {
3579 ret = ANEG_FAILED;
3580 }
3581 }
3582 }
3583 break;
3584
3585 case ANEG_STATE_IDLE_DETECT_INIT:
3586 ap->link_time = ap->cur_time;
3587 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3588 tw32_f(MAC_MODE, tp->mac_mode);
3589 udelay(40);
3590
3591 ap->state = ANEG_STATE_IDLE_DETECT;
3592 ret = ANEG_TIMER_ENAB;
3593 break;
3594
3595 case ANEG_STATE_IDLE_DETECT:
3596 if (ap->ability_match != 0 &&
3597 ap->rxconfig == 0) {
3598 ap->state = ANEG_STATE_AN_ENABLE;
3599 break;
3600 }
3601 delta = ap->cur_time - ap->link_time;
3602 if (delta > ANEG_STATE_SETTLE_TIME) {
3603 /* XXX another gem from the Broadcom driver :( */
3604 ap->state = ANEG_STATE_LINK_OK;
3605 }
3606 break;
3607
3608 case ANEG_STATE_LINK_OK:
3609 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
3610 ret = ANEG_DONE;
3611 break;
3612
3613 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
3614 /* ??? unimplemented */
3615 break;
3616
3617 case ANEG_STATE_NEXT_PAGE_WAIT:
3618 /* ??? unimplemented */
3619 break;
3620
3621 default:
3622 ret = ANEG_FAILED;
3623 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625
3626 return ret;
3627}
3628
Matt Carlson5be73b42007-12-20 20:09:29 -08003629static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630{
3631 int res = 0;
3632 struct tg3_fiber_aneginfo aninfo;
3633 int status = ANEG_FAILED;
3634 unsigned int tick;
3635 u32 tmp;
3636
3637 tw32_f(MAC_TX_AUTO_NEG, 0);
3638
3639 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
3640 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
3641 udelay(40);
3642
3643 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
3644 udelay(40);
3645
3646 memset(&aninfo, 0, sizeof(aninfo));
3647 aninfo.flags |= MR_AN_ENABLE;
3648 aninfo.state = ANEG_STATE_UNKNOWN;
3649 aninfo.cur_time = 0;
3650 tick = 0;
3651 while (++tick < 195000) {
3652 status = tg3_fiber_aneg_smachine(tp, &aninfo);
3653 if (status == ANEG_DONE || status == ANEG_FAILED)
3654 break;
3655
3656 udelay(1);
3657 }
3658
3659 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3660 tw32_f(MAC_MODE, tp->mac_mode);
3661 udelay(40);
3662
Matt Carlson5be73b42007-12-20 20:09:29 -08003663 *txflags = aninfo.txconfig;
3664 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665
3666 if (status == ANEG_DONE &&
3667 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
3668 MR_LP_ADV_FULL_DUPLEX)))
3669 res = 1;
3670
3671 return res;
3672}
3673
3674static void tg3_init_bcm8002(struct tg3 *tp)
3675{
3676 u32 mac_status = tr32(MAC_STATUS);
3677 int i;
3678
3679 /* Reset when initting first time or we have a link. */
3680 if ((tp->tg3_flags & TG3_FLAG_INIT_COMPLETE) &&
3681 !(mac_status & MAC_STATUS_PCS_SYNCED))
3682 return;
3683
3684 /* Set PLL lock range. */
3685 tg3_writephy(tp, 0x16, 0x8007);
3686
3687 /* SW reset */
3688 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
3689
3690 /* Wait for reset to complete. */
3691 /* XXX schedule_timeout() ... */
3692 for (i = 0; i < 500; i++)
3693 udelay(10);
3694
3695 /* Config mode; select PMA/Ch 1 regs. */
3696 tg3_writephy(tp, 0x10, 0x8411);
3697
3698 /* Enable auto-lock and comdet, select txclk for tx. */
3699 tg3_writephy(tp, 0x11, 0x0a10);
3700
3701 tg3_writephy(tp, 0x18, 0x00a0);
3702 tg3_writephy(tp, 0x16, 0x41ff);
3703
3704 /* Assert and deassert POR. */
3705 tg3_writephy(tp, 0x13, 0x0400);
3706 udelay(40);
3707 tg3_writephy(tp, 0x13, 0x0000);
3708
3709 tg3_writephy(tp, 0x11, 0x0a50);
3710 udelay(40);
3711 tg3_writephy(tp, 0x11, 0x0a10);
3712
3713 /* Wait for signal to stabilize */
3714 /* XXX schedule_timeout() ... */
3715 for (i = 0; i < 15000; i++)
3716 udelay(10);
3717
3718 /* Deselect the channel register so we can read the PHYID
3719 * later.
3720 */
3721 tg3_writephy(tp, 0x10, 0x8011);
3722}
3723
3724static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
3725{
Matt Carlson82cd3d12007-12-20 20:09:00 -08003726 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 u32 sg_dig_ctrl, sg_dig_status;
3728 u32 serdes_cfg, expected_sg_dig_ctrl;
3729 int workaround, port_a;
3730 int current_link_up;
3731
3732 serdes_cfg = 0;
3733 expected_sg_dig_ctrl = 0;
3734 workaround = 0;
3735 port_a = 1;
3736 current_link_up = 0;
3737
3738 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
3739 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
3740 workaround = 1;
3741 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
3742 port_a = 0;
3743
3744 /* preserve bits 0-11,13,14 for signal pre-emphasis */
3745 /* preserve bits 20-23 for voltage regulator */
3746 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
3747 }
3748
3749 sg_dig_ctrl = tr32(SG_DIG_CTRL);
3750
3751 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003752 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 if (workaround) {
3754 u32 val = serdes_cfg;
3755
3756 if (port_a)
3757 val |= 0xc010000;
3758 else
3759 val |= 0x4010000;
3760 tw32_f(MAC_SERDES_CFG, val);
3761 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003762
3763 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 }
3765 if (mac_status & MAC_STATUS_PCS_SYNCED) {
3766 tg3_setup_flow_control(tp, 0, 0);
3767 current_link_up = 1;
3768 }
3769 goto out;
3770 }
3771
3772 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003773 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774
Matt Carlson82cd3d12007-12-20 20:09:00 -08003775 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3776 if (flowctrl & ADVERTISE_1000XPAUSE)
3777 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
3778 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3779 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780
3781 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003782 if ((tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT) &&
3783 tp->serdes_counter &&
3784 ((mac_status & (MAC_STATUS_PCS_SYNCED |
3785 MAC_STATUS_RCVD_CFG)) ==
3786 MAC_STATUS_PCS_SYNCED)) {
3787 tp->serdes_counter--;
3788 current_link_up = 1;
3789 goto out;
3790 }
3791restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 if (workaround)
3793 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003794 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 udelay(5);
3796 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
3797
Michael Chan3d3ebe72006-09-27 15:59:15 -07003798 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
3799 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
3801 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003802 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 mac_status = tr32(MAC_STATUS);
3804
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003805 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08003807 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808
Matt Carlson82cd3d12007-12-20 20:09:00 -08003809 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
3810 local_adv |= ADVERTISE_1000XPAUSE;
3811 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
3812 local_adv |= ADVERTISE_1000XPSE_ASYM;
3813
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003814 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003815 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003816 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003817 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818
3819 tg3_setup_flow_control(tp, local_adv, remote_adv);
3820 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003821 tp->serdes_counter = 0;
3822 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003823 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003824 if (tp->serdes_counter)
3825 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 else {
3827 if (workaround) {
3828 u32 val = serdes_cfg;
3829
3830 if (port_a)
3831 val |= 0xc010000;
3832 else
3833 val |= 0x4010000;
3834
3835 tw32_f(MAC_SERDES_CFG, val);
3836 }
3837
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003838 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 udelay(40);
3840
3841 /* Link parallel detection - link is up */
3842 /* only if we have PCS_SYNC and not */
3843 /* receiving config code words */
3844 mac_status = tr32(MAC_STATUS);
3845 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
3846 !(mac_status & MAC_STATUS_RCVD_CFG)) {
3847 tg3_setup_flow_control(tp, 0, 0);
3848 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003849 tp->tg3_flags2 |=
3850 TG3_FLG2_PARALLEL_DETECT;
3851 tp->serdes_counter =
3852 SERDES_PARALLEL_DET_TIMEOUT;
3853 } else
3854 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 }
3856 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07003857 } else {
3858 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
3859 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860 }
3861
3862out:
3863 return current_link_up;
3864}
3865
3866static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
3867{
3868 int current_link_up = 0;
3869
Michael Chan5cf64b8a2007-05-05 12:11:21 -07003870 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872
3873 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08003874 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003876
Matt Carlson5be73b42007-12-20 20:09:29 -08003877 if (fiber_autoneg(tp, &txflags, &rxflags)) {
3878 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879
Matt Carlson5be73b42007-12-20 20:09:29 -08003880 if (txflags & ANEG_CFG_PS1)
3881 local_adv |= ADVERTISE_1000XPAUSE;
3882 if (txflags & ANEG_CFG_PS2)
3883 local_adv |= ADVERTISE_1000XPSE_ASYM;
3884
3885 if (rxflags & MR_LP_ADV_SYM_PAUSE)
3886 remote_adv |= LPA_1000XPAUSE;
3887 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
3888 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889
3890 tg3_setup_flow_control(tp, local_adv, remote_adv);
3891
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 current_link_up = 1;
3893 }
3894 for (i = 0; i < 30; i++) {
3895 udelay(20);
3896 tw32_f(MAC_STATUS,
3897 (MAC_STATUS_SYNC_CHANGED |
3898 MAC_STATUS_CFG_CHANGED));
3899 udelay(40);
3900 if ((tr32(MAC_STATUS) &
3901 (MAC_STATUS_SYNC_CHANGED |
3902 MAC_STATUS_CFG_CHANGED)) == 0)
3903 break;
3904 }
3905
3906 mac_status = tr32(MAC_STATUS);
3907 if (current_link_up == 0 &&
3908 (mac_status & MAC_STATUS_PCS_SYNCED) &&
3909 !(mac_status & MAC_STATUS_RCVD_CFG))
3910 current_link_up = 1;
3911 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08003912 tg3_setup_flow_control(tp, 0, 0);
3913
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 /* Forcing 1000FD link up. */
3915 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916
3917 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
3918 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003919
3920 tw32_f(MAC_MODE, tp->mac_mode);
3921 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922 }
3923
3924out:
3925 return current_link_up;
3926}
3927
3928static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
3929{
3930 u32 orig_pause_cfg;
3931 u16 orig_active_speed;
3932 u8 orig_active_duplex;
3933 u32 mac_status;
3934 int current_link_up;
3935 int i;
3936
Matt Carlson8d018622007-12-20 20:05:44 -08003937 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 orig_active_speed = tp->link_config.active_speed;
3939 orig_active_duplex = tp->link_config.active_duplex;
3940
3941 if (!(tp->tg3_flags2 & TG3_FLG2_HW_AUTONEG) &&
3942 netif_carrier_ok(tp->dev) &&
3943 (tp->tg3_flags & TG3_FLAG_INIT_COMPLETE)) {
3944 mac_status = tr32(MAC_STATUS);
3945 mac_status &= (MAC_STATUS_PCS_SYNCED |
3946 MAC_STATUS_SIGNAL_DET |
3947 MAC_STATUS_CFG_CHANGED |
3948 MAC_STATUS_RCVD_CFG);
3949 if (mac_status == (MAC_STATUS_PCS_SYNCED |
3950 MAC_STATUS_SIGNAL_DET)) {
3951 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
3952 MAC_STATUS_CFG_CHANGED));
3953 return 0;
3954 }
3955 }
3956
3957 tw32_f(MAC_TX_AUTO_NEG, 0);
3958
3959 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
3960 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
3961 tw32_f(MAC_MODE, tp->mac_mode);
3962 udelay(40);
3963
Matt Carlson79eb6902010-02-17 15:17:03 +00003964 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 tg3_init_bcm8002(tp);
3966
3967 /* Enable link change event even when serdes polling. */
3968 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
3969 udelay(40);
3970
3971 current_link_up = 0;
3972 mac_status = tr32(MAC_STATUS);
3973
3974 if (tp->tg3_flags2 & TG3_FLG2_HW_AUTONEG)
3975 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
3976 else
3977 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
3978
Matt Carlson898a56f2009-08-28 14:02:40 +00003979 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00003981 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982
3983 for (i = 0; i < 100; i++) {
3984 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
3985 MAC_STATUS_CFG_CHANGED));
3986 udelay(5);
3987 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07003988 MAC_STATUS_CFG_CHANGED |
3989 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990 break;
3991 }
3992
3993 mac_status = tr32(MAC_STATUS);
3994 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
3995 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003996 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
3997 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 tw32_f(MAC_MODE, (tp->mac_mode |
3999 MAC_MODE_SEND_CONFIGS));
4000 udelay(1);
4001 tw32_f(MAC_MODE, tp->mac_mode);
4002 }
4003 }
4004
4005 if (current_link_up == 1) {
4006 tp->link_config.active_speed = SPEED_1000;
4007 tp->link_config.active_duplex = DUPLEX_FULL;
4008 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4009 LED_CTRL_LNKLED_OVERRIDE |
4010 LED_CTRL_1000MBPS_ON));
4011 } else {
4012 tp->link_config.active_speed = SPEED_INVALID;
4013 tp->link_config.active_duplex = DUPLEX_INVALID;
4014 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4015 LED_CTRL_LNKLED_OVERRIDE |
4016 LED_CTRL_TRAFFIC_OVERRIDE));
4017 }
4018
4019 if (current_link_up != netif_carrier_ok(tp->dev)) {
4020 if (current_link_up)
4021 netif_carrier_on(tp->dev);
4022 else
4023 netif_carrier_off(tp->dev);
4024 tg3_link_report(tp);
4025 } else {
Matt Carlson8d018622007-12-20 20:05:44 -08004026 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 if (orig_pause_cfg != now_pause_cfg ||
4028 orig_active_speed != tp->link_config.active_speed ||
4029 orig_active_duplex != tp->link_config.active_duplex)
4030 tg3_link_report(tp);
4031 }
4032
4033 return 0;
4034}
4035
Michael Chan747e8f82005-07-25 12:33:22 -07004036static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
4037{
4038 int current_link_up, err = 0;
4039 u32 bmsr, bmcr;
4040 u16 current_speed;
4041 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08004042 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07004043
4044 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4045 tw32_f(MAC_MODE, tp->mac_mode);
4046 udelay(40);
4047
4048 tw32(MAC_EVENT, 0);
4049
4050 tw32_f(MAC_STATUS,
4051 (MAC_STATUS_SYNC_CHANGED |
4052 MAC_STATUS_CFG_CHANGED |
4053 MAC_STATUS_MI_COMPLETION |
4054 MAC_STATUS_LNKSTATE_CHANGED));
4055 udelay(40);
4056
4057 if (force_reset)
4058 tg3_phy_reset(tp);
4059
4060 current_link_up = 0;
4061 current_speed = SPEED_INVALID;
4062 current_duplex = DUPLEX_INVALID;
4063
4064 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4065 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004066 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
4067 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4068 bmsr |= BMSR_LSTATUS;
4069 else
4070 bmsr &= ~BMSR_LSTATUS;
4071 }
Michael Chan747e8f82005-07-25 12:33:22 -07004072
4073 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
4074
4075 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlson2bd3ed02008-06-09 15:39:55 -07004076 (tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004077 /* do nothing, just check for link up at the end */
4078 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4079 u32 adv, new_adv;
4080
4081 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4082 new_adv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
4083 ADVERTISE_1000XPAUSE |
4084 ADVERTISE_1000XPSE_ASYM |
4085 ADVERTISE_SLCT);
4086
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004087 new_adv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Michael Chan747e8f82005-07-25 12:33:22 -07004088
4089 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
4090 new_adv |= ADVERTISE_1000XHALF;
4091 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
4092 new_adv |= ADVERTISE_1000XFULL;
4093
4094 if ((new_adv != adv) || !(bmcr & BMCR_ANENABLE)) {
4095 tg3_writephy(tp, MII_ADVERTISE, new_adv);
4096 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
4097 tg3_writephy(tp, MII_BMCR, bmcr);
4098
4099 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07004100 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Michael Chan747e8f82005-07-25 12:33:22 -07004101 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
4102
4103 return err;
4104 }
4105 } else {
4106 u32 new_bmcr;
4107
4108 bmcr &= ~BMCR_SPEED1000;
4109 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
4110
4111 if (tp->link_config.duplex == DUPLEX_FULL)
4112 new_bmcr |= BMCR_FULLDPLX;
4113
4114 if (new_bmcr != bmcr) {
4115 /* BMCR_SPEED1000 is a reserved bit that needs
4116 * to be set on write.
4117 */
4118 new_bmcr |= BMCR_SPEED1000;
4119
4120 /* Force a linkdown */
4121 if (netif_carrier_ok(tp->dev)) {
4122 u32 adv;
4123
4124 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4125 adv &= ~(ADVERTISE_1000XFULL |
4126 ADVERTISE_1000XHALF |
4127 ADVERTISE_SLCT);
4128 tg3_writephy(tp, MII_ADVERTISE, adv);
4129 tg3_writephy(tp, MII_BMCR, bmcr |
4130 BMCR_ANRESTART |
4131 BMCR_ANENABLE);
4132 udelay(10);
4133 netif_carrier_off(tp->dev);
4134 }
4135 tg3_writephy(tp, MII_BMCR, new_bmcr);
4136 bmcr = new_bmcr;
4137 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4138 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004139 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
4140 ASIC_REV_5714) {
4141 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4142 bmsr |= BMSR_LSTATUS;
4143 else
4144 bmsr &= ~BMSR_LSTATUS;
4145 }
Michael Chan747e8f82005-07-25 12:33:22 -07004146 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
4147 }
4148 }
4149
4150 if (bmsr & BMSR_LSTATUS) {
4151 current_speed = SPEED_1000;
4152 current_link_up = 1;
4153 if (bmcr & BMCR_FULLDPLX)
4154 current_duplex = DUPLEX_FULL;
4155 else
4156 current_duplex = DUPLEX_HALF;
4157
Matt Carlsonef167e22007-12-20 20:10:01 -08004158 local_adv = 0;
4159 remote_adv = 0;
4160
Michael Chan747e8f82005-07-25 12:33:22 -07004161 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08004162 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07004163
4164 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
4165 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
4166 common = local_adv & remote_adv;
4167 if (common & (ADVERTISE_1000XHALF |
4168 ADVERTISE_1000XFULL)) {
4169 if (common & ADVERTISE_1000XFULL)
4170 current_duplex = DUPLEX_FULL;
4171 else
4172 current_duplex = DUPLEX_HALF;
Matt Carlson859a588792010-04-05 10:19:28 +00004173 } else {
Michael Chan747e8f82005-07-25 12:33:22 -07004174 current_link_up = 0;
Matt Carlson859a588792010-04-05 10:19:28 +00004175 }
Michael Chan747e8f82005-07-25 12:33:22 -07004176 }
4177 }
4178
Matt Carlsonef167e22007-12-20 20:10:01 -08004179 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
4180 tg3_setup_flow_control(tp, local_adv, remote_adv);
4181
Michael Chan747e8f82005-07-25 12:33:22 -07004182 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4183 if (tp->link_config.active_duplex == DUPLEX_HALF)
4184 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4185
4186 tw32_f(MAC_MODE, tp->mac_mode);
4187 udelay(40);
4188
4189 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4190
4191 tp->link_config.active_speed = current_speed;
4192 tp->link_config.active_duplex = current_duplex;
4193
4194 if (current_link_up != netif_carrier_ok(tp->dev)) {
4195 if (current_link_up)
4196 netif_carrier_on(tp->dev);
4197 else {
4198 netif_carrier_off(tp->dev);
4199 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
4200 }
4201 tg3_link_report(tp);
4202 }
4203 return err;
4204}
4205
4206static void tg3_serdes_parallel_detect(struct tg3 *tp)
4207{
Michael Chan3d3ebe72006-09-27 15:59:15 -07004208 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07004209 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07004210 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07004211 return;
4212 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00004213
Michael Chan747e8f82005-07-25 12:33:22 -07004214 if (!netif_carrier_ok(tp->dev) &&
4215 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
4216 u32 bmcr;
4217
4218 tg3_readphy(tp, MII_BMCR, &bmcr);
4219 if (bmcr & BMCR_ANENABLE) {
4220 u32 phy1, phy2;
4221
4222 /* Select shadow register 0x1f */
4223 tg3_writephy(tp, 0x1c, 0x7c00);
4224 tg3_readphy(tp, 0x1c, &phy1);
4225
4226 /* Select expansion interrupt status register */
4227 tg3_writephy(tp, 0x17, 0x0f01);
4228 tg3_readphy(tp, 0x15, &phy2);
4229 tg3_readphy(tp, 0x15, &phy2);
4230
4231 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
4232 /* We have signal detect and not receiving
4233 * config code words, link is up by parallel
4234 * detection.
4235 */
4236
4237 bmcr &= ~BMCR_ANENABLE;
4238 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
4239 tg3_writephy(tp, MII_BMCR, bmcr);
4240 tp->tg3_flags2 |= TG3_FLG2_PARALLEL_DETECT;
4241 }
4242 }
Matt Carlson859a588792010-04-05 10:19:28 +00004243 } else if (netif_carrier_ok(tp->dev) &&
4244 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
4245 (tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004246 u32 phy2;
4247
4248 /* Select expansion interrupt status register */
4249 tg3_writephy(tp, 0x17, 0x0f01);
4250 tg3_readphy(tp, 0x15, &phy2);
4251 if (phy2 & 0x20) {
4252 u32 bmcr;
4253
4254 /* Config code words received, turn on autoneg. */
4255 tg3_readphy(tp, MII_BMCR, &bmcr);
4256 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
4257
4258 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
4259
4260 }
4261 }
4262}
4263
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264static int tg3_setup_phy(struct tg3 *tp, int force_reset)
4265{
4266 int err;
4267
Matt Carlson859a588792010-04-05 10:19:28 +00004268 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlson859a588792010-04-05 10:19:28 +00004270 else if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07004271 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a588792010-04-05 10:19:28 +00004272 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274
Matt Carlsonbcb37f62008-11-03 16:52:09 -08004275 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08004276 u32 val, scale;
4277
4278 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
4279 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
4280 scale = 65;
4281 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
4282 scale = 6;
4283 else
4284 scale = 12;
4285
4286 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
4287 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
4288 tw32(GRC_MISC_CFG, val);
4289 }
4290
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 if (tp->link_config.active_speed == SPEED_1000 &&
4292 tp->link_config.active_duplex == DUPLEX_HALF)
4293 tw32(MAC_TX_LENGTHS,
4294 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4295 (6 << TX_LENGTHS_IPG_SHIFT) |
4296 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
4297 else
4298 tw32(MAC_TX_LENGTHS,
4299 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4300 (6 << TX_LENGTHS_IPG_SHIFT) |
4301 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
4302
4303 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
4304 if (netif_carrier_ok(tp->dev)) {
4305 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07004306 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 } else {
4308 tw32(HOSTCC_STAT_COAL_TICKS, 0);
4309 }
4310 }
4311
Matt Carlson8ed5d972007-05-07 00:25:49 -07004312 if (tp->tg3_flags & TG3_FLAG_ASPM_WORKAROUND) {
4313 u32 val = tr32(PCIE_PWR_MGMT_THRESH);
4314 if (!netif_carrier_ok(tp->dev))
4315 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
4316 tp->pwrmgmt_thresh;
4317 else
4318 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
4319 tw32(PCIE_PWR_MGMT_THRESH, val);
4320 }
4321
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 return err;
4323}
4324
Michael Chandf3e6542006-05-26 17:48:07 -07004325/* This is called whenever we suspect that the system chipset is re-
4326 * ordering the sequence of MMIO to the tx send mailbox. The symptom
4327 * is bogus tx completions. We try to recover by setting the
4328 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
4329 * in the workqueue.
4330 */
4331static void tg3_tx_recover(struct tg3 *tp)
4332{
4333 BUG_ON((tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER) ||
4334 tp->write32_tx_mbox == tg3_write_indirect_mbox);
4335
Matt Carlson5129c3a2010-04-05 10:19:23 +00004336 netdev_warn(tp->dev,
4337 "The system may be re-ordering memory-mapped I/O "
4338 "cycles to the network device, attempting to recover. "
4339 "Please report the problem to the driver maintainer "
4340 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07004341
4342 spin_lock(&tp->lock);
Michael Chandf3e6542006-05-26 17:48:07 -07004343 tp->tg3_flags |= TG3_FLAG_TX_RECOVERY_PENDING;
Michael Chandf3e6542006-05-26 17:48:07 -07004344 spin_unlock(&tp->lock);
4345}
4346
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004347static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07004348{
4349 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004350 return tnapi->tx_pending -
4351 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07004352}
4353
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354/* Tigon3 never reports partial packet sends. So we do not
4355 * need special logic to handle SKBs that have not had all
4356 * of their frags sent yet, like SunGEM does.
4357 */
Matt Carlson17375d22009-08-28 14:02:18 +00004358static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359{
Matt Carlson17375d22009-08-28 14:02:18 +00004360 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00004361 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004362 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004363 struct netdev_queue *txq;
4364 int index = tnapi - tp->napi;
4365
Matt Carlson19cfaec2009-12-03 08:36:20 +00004366 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004367 index--;
4368
4369 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370
4371 while (sw_idx != hw_idx) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00004372 struct ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07004374 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375
Michael Chandf3e6542006-05-26 17:48:07 -07004376 if (unlikely(skb == NULL)) {
4377 tg3_tx_recover(tp);
4378 return;
4379 }
4380
Alexander Duyckf4188d82009-12-02 16:48:38 +00004381 pci_unmap_single(tp->pdev,
4382 pci_unmap_addr(ri, mapping),
4383 skb_headlen(skb),
4384 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385
4386 ri->skb = NULL;
4387
4388 sw_idx = NEXT_TX(sw_idx);
4389
4390 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004391 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07004392 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
4393 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00004394
4395 pci_unmap_page(tp->pdev,
4396 pci_unmap_addr(ri, mapping),
4397 skb_shinfo(skb)->frags[i].size,
4398 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 sw_idx = NEXT_TX(sw_idx);
4400 }
4401
David S. Millerf47c11e2005-06-24 20:18:35 -07004402 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07004403
4404 if (unlikely(tx_bug)) {
4405 tg3_tx_recover(tp);
4406 return;
4407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408 }
4409
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004410 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411
Michael Chan1b2a7202006-08-07 21:46:02 -07004412 /* Need to make the tx_cons update visible to tg3_start_xmit()
4413 * before checking for netif_queue_stopped(). Without the
4414 * memory barrier, there is a small possibility that tg3_start_xmit()
4415 * will miss it and cause the queue to be stopped forever.
4416 */
4417 smp_mb();
4418
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004419 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004420 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004421 __netif_tx_lock(txq, smp_processor_id());
4422 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004423 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004424 netif_tx_wake_queue(txq);
4425 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07004426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427}
4428
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004429static void tg3_rx_skb_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
4430{
4431 if (!ri->skb)
4432 return;
4433
4434 pci_unmap_single(tp->pdev, pci_unmap_addr(ri, mapping),
4435 map_sz, PCI_DMA_FROMDEVICE);
4436 dev_kfree_skb_any(ri->skb);
4437 ri->skb = NULL;
4438}
4439
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440/* Returns size of skb allocated or < 0 on error.
4441 *
4442 * We only need to fill in the address because the other members
4443 * of the RX descriptor are invariant, see tg3_init_rings.
4444 *
4445 * Note the purposeful assymetry of cpu vs. chip accesses. For
4446 * posting buffers we only dirty the first cache line of the RX
4447 * descriptor (containing the address). Whereas for the RX status
4448 * buffers the cpu only reads the last cacheline of the RX descriptor
4449 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
4450 */
Matt Carlson86b21e52009-11-13 13:03:45 +00004451static int tg3_alloc_rx_skb(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Matt Carlsona3896162009-11-13 13:03:44 +00004452 u32 opaque_key, u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453{
4454 struct tg3_rx_buffer_desc *desc;
4455 struct ring_info *map, *src_map;
4456 struct sk_buff *skb;
4457 dma_addr_t mapping;
4458 int skb_size, dest_idx;
4459
4460 src_map = NULL;
4461 switch (opaque_key) {
4462 case RXD_OPAQUE_RING_STD:
4463 dest_idx = dest_idx_unmasked % TG3_RX_RING_SIZE;
Matt Carlson21f581a2009-08-28 14:00:25 +00004464 desc = &tpr->rx_std[dest_idx];
4465 map = &tpr->rx_std_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004466 skb_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467 break;
4468
4469 case RXD_OPAQUE_RING_JUMBO:
4470 dest_idx = dest_idx_unmasked % TG3_RX_JUMBO_RING_SIZE;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00004471 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00004472 map = &tpr->rx_jmb_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004473 skb_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 break;
4475
4476 default:
4477 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479
4480 /* Do not overwrite any of the map or rp information
4481 * until we are sure we can commit to a new buffer.
4482 *
4483 * Callers depend upon this behavior and assume that
4484 * we leave everything unchanged if we fail.
4485 */
Matt Carlson287be122009-08-28 13:58:46 +00004486 skb = netdev_alloc_skb(tp->dev, skb_size + tp->rx_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 if (skb == NULL)
4488 return -ENOMEM;
4489
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 skb_reserve(skb, tp->rx_offset);
4491
Matt Carlson287be122009-08-28 13:58:46 +00004492 mapping = pci_map_single(tp->pdev, skb->data, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 PCI_DMA_FROMDEVICE);
Matt Carlsona21771d2009-11-02 14:25:31 +00004494 if (pci_dma_mapping_error(tp->pdev, mapping)) {
4495 dev_kfree_skb(skb);
4496 return -EIO;
4497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498
4499 map->skb = skb;
4500 pci_unmap_addr_set(map, mapping, mapping);
4501
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 desc->addr_hi = ((u64)mapping >> 32);
4503 desc->addr_lo = ((u64)mapping & 0xffffffff);
4504
4505 return skb_size;
4506}
4507
4508/* We only need to move over in the address because the other
4509 * members of the RX descriptor are invariant. See notes above
4510 * tg3_alloc_rx_skb for full details.
4511 */
Matt Carlsona3896162009-11-13 13:03:44 +00004512static void tg3_recycle_rx(struct tg3_napi *tnapi,
4513 struct tg3_rx_prodring_set *dpr,
4514 u32 opaque_key, int src_idx,
4515 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516{
Matt Carlson17375d22009-08-28 14:02:18 +00004517 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
4519 struct ring_info *src_map, *dest_map;
Matt Carlsona3896162009-11-13 13:03:44 +00004520 struct tg3_rx_prodring_set *spr = &tp->prodring[0];
Matt Carlsonc6cdf432010-04-05 10:19:26 +00004521 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522
4523 switch (opaque_key) {
4524 case RXD_OPAQUE_RING_STD:
4525 dest_idx = dest_idx_unmasked % TG3_RX_RING_SIZE;
Matt Carlsona3896162009-11-13 13:03:44 +00004526 dest_desc = &dpr->rx_std[dest_idx];
4527 dest_map = &dpr->rx_std_buffers[dest_idx];
4528 src_desc = &spr->rx_std[src_idx];
4529 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530 break;
4531
4532 case RXD_OPAQUE_RING_JUMBO:
4533 dest_idx = dest_idx_unmasked % TG3_RX_JUMBO_RING_SIZE;
Matt Carlsona3896162009-11-13 13:03:44 +00004534 dest_desc = &dpr->rx_jmb[dest_idx].std;
4535 dest_map = &dpr->rx_jmb_buffers[dest_idx];
4536 src_desc = &spr->rx_jmb[src_idx].std;
4537 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 break;
4539
4540 default:
4541 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543
4544 dest_map->skb = src_map->skb;
4545 pci_unmap_addr_set(dest_map, mapping,
4546 pci_unmap_addr(src_map, mapping));
4547 dest_desc->addr_hi = src_desc->addr_hi;
4548 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00004549
4550 /* Ensure that the update to the skb happens after the physical
4551 * addresses have been transferred to the new BD location.
4552 */
4553 smp_wmb();
4554
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555 src_map->skb = NULL;
4556}
4557
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558/* The RX ring scheme is composed of multiple rings which post fresh
4559 * buffers to the chip, and one special ring the chip uses to report
4560 * status back to the host.
4561 *
4562 * The special ring reports the status of received packets to the
4563 * host. The chip does not write into the original descriptor the
4564 * RX buffer was obtained from. The chip simply takes the original
4565 * descriptor as provided by the host, updates the status and length
4566 * field, then writes this into the next status ring entry.
4567 *
4568 * Each ring the host uses to post buffers to the chip is described
4569 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
4570 * it is first placed into the on-chip ram. When the packet's length
4571 * is known, it walks down the TG3_BDINFO entries to select the ring.
4572 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
4573 * which is within the range of the new packet's length is chosen.
4574 *
4575 * The "separate ring for rx status" scheme may sound queer, but it makes
4576 * sense from a cache coherency perspective. If only the host writes
4577 * to the buffer post rings, and only the chip writes to the rx status
4578 * rings, then cache lines never move beyond shared-modified state.
4579 * If both the host and chip were to write into the same ring, cache line
4580 * eviction could occur since both entities want it in an exclusive state.
4581 */
Matt Carlson17375d22009-08-28 14:02:18 +00004582static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583{
Matt Carlson17375d22009-08-28 14:02:18 +00004584 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07004585 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00004586 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00004587 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07004588 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589 int received;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004590 struct tg3_rx_prodring_set *tpr = tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004592 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 /*
4594 * We need to order the read of hw_idx and the read of
4595 * the opaque cookie.
4596 */
4597 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598 work_mask = 0;
4599 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00004600 std_prod_idx = tpr->rx_std_prod_idx;
4601 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00004603 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00004604 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605 unsigned int len;
4606 struct sk_buff *skb;
4607 dma_addr_t dma_addr;
4608 u32 opaque_key, desc_idx, *post_ptr;
4609
4610 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
4611 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
4612 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004613 ri = &tp->prodring[0].rx_std_buffers[desc_idx];
Matt Carlson21f581a2009-08-28 14:00:25 +00004614 dma_addr = pci_unmap_addr(ri, mapping);
4615 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00004616 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07004617 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004619 ri = &tp->prodring[0].rx_jmb_buffers[desc_idx];
Matt Carlson21f581a2009-08-28 14:00:25 +00004620 dma_addr = pci_unmap_addr(ri, mapping);
4621 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00004622 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00004623 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625
4626 work_mask |= opaque_key;
4627
4628 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
4629 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
4630 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00004631 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632 desc_idx, *post_ptr);
4633 drop_it_no_recycle:
4634 /* Other statistics kept track of by card. */
4635 tp->net_stats.rx_dropped++;
4636 goto next_pkt;
4637 }
4638
Matt Carlsonad829262008-11-21 17:16:16 -08004639 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
4640 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641
Joe Perches8e95a202009-12-03 07:58:21 +00004642 if (len > RX_COPY_THRESHOLD &&
4643 tp->rx_offset == NET_IP_ALIGN) {
4644 /* rx_offset will likely not equal NET_IP_ALIGN
4645 * if this is a 5701 card running in PCI-X mode
4646 * [see tg3_get_invariants()]
4647 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 int skb_size;
4649
Matt Carlson86b21e52009-11-13 13:03:45 +00004650 skb_size = tg3_alloc_rx_skb(tp, tpr, opaque_key,
Matt Carlsonafc081f2009-11-13 13:03:43 +00004651 *post_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652 if (skb_size < 0)
4653 goto drop_it;
4654
Matt Carlson287be122009-08-28 13:58:46 +00004655 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 PCI_DMA_FROMDEVICE);
4657
Matt Carlson61e800c2010-02-17 15:16:54 +00004658 /* Ensure that the update to the skb happens
4659 * after the usage of the old DMA mapping.
4660 */
4661 smp_wmb();
4662
4663 ri->skb = NULL;
4664
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 skb_put(skb, len);
4666 } else {
4667 struct sk_buff *copy_skb;
4668
Matt Carlsona3896162009-11-13 13:03:44 +00004669 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670 desc_idx, *post_ptr);
4671
Matt Carlsonad829262008-11-21 17:16:16 -08004672 copy_skb = netdev_alloc_skb(tp->dev,
4673 len + TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 if (copy_skb == NULL)
4675 goto drop_it_no_recycle;
4676
Matt Carlsonad829262008-11-21 17:16:16 -08004677 skb_reserve(copy_skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 skb_put(copy_skb, len);
4679 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03004680 skb_copy_from_linear_data(skb, copy_skb->data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
4682
4683 /* We'll reuse the original ring buffer. */
4684 skb = copy_skb;
4685 }
4686
4687 if ((tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) &&
4688 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
4689 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
4690 >> RXD_TCPCSUM_SHIFT) == 0xffff))
4691 skb->ip_summed = CHECKSUM_UNNECESSARY;
4692 else
4693 skb->ip_summed = CHECKSUM_NONE;
4694
4695 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00004696
4697 if (len > (tp->dev->mtu + ETH_HLEN) &&
4698 skb->protocol != htons(ETH_P_8021Q)) {
4699 dev_kfree_skb(skb);
4700 goto next_pkt;
4701 }
4702
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703#if TG3_VLAN_TAG_USED
4704 if (tp->vlgrp != NULL &&
4705 desc->type_flags & RXD_FLAG_VLAN) {
Matt Carlson17375d22009-08-28 14:02:18 +00004706 vlan_gro_receive(&tnapi->napi, tp->vlgrp,
Matt Carlson8ef04422009-08-28 14:01:37 +00004707 desc->err_vlan & RXD_VLAN_MASK, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708 } else
4709#endif
Matt Carlson17375d22009-08-28 14:02:18 +00004710 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 received++;
4713 budget--;
4714
4715next_pkt:
4716 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07004717
4718 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson86cfe4f2010-01-12 10:11:37 +00004719 tpr->rx_std_prod_idx = std_prod_idx % TG3_RX_RING_SIZE;
4720 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
4721 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07004722 work_mask &= ~RXD_OPAQUE_RING_STD;
4723 rx_std_posted = 0;
4724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07004726 sw_idx++;
Eric Dumazet6b31a512007-02-06 13:29:21 -08004727 sw_idx &= (TG3_RX_RCB_RING_SIZE(tp) - 1);
Michael Chan52f6d692005-04-25 15:14:32 -07004728
4729 /* Refresh hw_idx to see if there is new work */
4730 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004731 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07004732 rmb();
4733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734 }
4735
4736 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00004737 tnapi->rx_rcb_ptr = sw_idx;
4738 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739
4740 /* Refill RX ring(s). */
Matt Carlsone4af1af2010-02-12 14:47:05 +00004741 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004742 if (work_mask & RXD_OPAQUE_RING_STD) {
4743 tpr->rx_std_prod_idx = std_prod_idx % TG3_RX_RING_SIZE;
4744 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
4745 tpr->rx_std_prod_idx);
4746 }
4747 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
4748 tpr->rx_jmb_prod_idx = jmb_prod_idx %
4749 TG3_RX_JUMBO_RING_SIZE;
4750 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
4751 tpr->rx_jmb_prod_idx);
4752 }
4753 mmiowb();
4754 } else if (work_mask) {
4755 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
4756 * updated before the producer indices can be updated.
4757 */
4758 smp_wmb();
4759
Matt Carlson43619352009-11-13 13:03:47 +00004760 tpr->rx_std_prod_idx = std_prod_idx % TG3_RX_RING_SIZE;
Matt Carlson43619352009-11-13 13:03:47 +00004761 tpr->rx_jmb_prod_idx = jmb_prod_idx % TG3_RX_JUMBO_RING_SIZE;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004762
Matt Carlsone4af1af2010-02-12 14:47:05 +00004763 if (tnapi != &tp->napi[1])
4764 napi_schedule(&tp->napi[1].napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766
4767 return received;
4768}
4769
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004770static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 /* handle link change and other phy events */
4773 if (!(tp->tg3_flags &
4774 (TG3_FLAG_USE_LINKCHG_REG |
4775 TG3_FLAG_POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004776 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
4777
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778 if (sblk->status & SD_STATUS_LINK_CHG) {
4779 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004780 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07004781 spin_lock(&tp->lock);
Matt Carlsondd477002008-05-25 23:45:58 -07004782 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
4783 tw32_f(MAC_STATUS,
4784 (MAC_STATUS_SYNC_CHANGED |
4785 MAC_STATUS_CFG_CHANGED |
4786 MAC_STATUS_MI_COMPLETION |
4787 MAC_STATUS_LNKSTATE_CHANGED));
4788 udelay(40);
4789 } else
4790 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07004791 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792 }
4793 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004794}
4795
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004796static int tg3_rx_prodring_xfer(struct tg3 *tp,
4797 struct tg3_rx_prodring_set *dpr,
4798 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004799{
4800 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004801 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004802
4803 while (1) {
4804 src_prod_idx = spr->rx_std_prod_idx;
4805
4806 /* Make sure updates to the rx_std_buffers[] entries and the
4807 * standard producer index are seen in the correct order.
4808 */
4809 smp_rmb();
4810
4811 if (spr->rx_std_cons_idx == src_prod_idx)
4812 break;
4813
4814 if (spr->rx_std_cons_idx < src_prod_idx)
4815 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
4816 else
4817 cpycnt = TG3_RX_RING_SIZE - spr->rx_std_cons_idx;
4818
4819 cpycnt = min(cpycnt, TG3_RX_RING_SIZE - dpr->rx_std_prod_idx);
4820
4821 si = spr->rx_std_cons_idx;
4822 di = dpr->rx_std_prod_idx;
4823
Matt Carlsone92967b2010-02-12 14:47:06 +00004824 for (i = di; i < di + cpycnt; i++) {
4825 if (dpr->rx_std_buffers[i].skb) {
4826 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004827 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00004828 break;
4829 }
4830 }
4831
4832 if (!cpycnt)
4833 break;
4834
4835 /* Ensure that updates to the rx_std_buffers ring and the
4836 * shadowed hardware producer ring from tg3_recycle_skb() are
4837 * ordered correctly WRT the skb check above.
4838 */
4839 smp_rmb();
4840
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004841 memcpy(&dpr->rx_std_buffers[di],
4842 &spr->rx_std_buffers[si],
4843 cpycnt * sizeof(struct ring_info));
4844
4845 for (i = 0; i < cpycnt; i++, di++, si++) {
4846 struct tg3_rx_buffer_desc *sbd, *dbd;
4847 sbd = &spr->rx_std[si];
4848 dbd = &dpr->rx_std[di];
4849 dbd->addr_hi = sbd->addr_hi;
4850 dbd->addr_lo = sbd->addr_lo;
4851 }
4852
4853 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) %
4854 TG3_RX_RING_SIZE;
4855 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) %
4856 TG3_RX_RING_SIZE;
4857 }
4858
4859 while (1) {
4860 src_prod_idx = spr->rx_jmb_prod_idx;
4861
4862 /* Make sure updates to the rx_jmb_buffers[] entries and
4863 * the jumbo producer index are seen in the correct order.
4864 */
4865 smp_rmb();
4866
4867 if (spr->rx_jmb_cons_idx == src_prod_idx)
4868 break;
4869
4870 if (spr->rx_jmb_cons_idx < src_prod_idx)
4871 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
4872 else
4873 cpycnt = TG3_RX_JUMBO_RING_SIZE - spr->rx_jmb_cons_idx;
4874
4875 cpycnt = min(cpycnt,
4876 TG3_RX_JUMBO_RING_SIZE - dpr->rx_jmb_prod_idx);
4877
4878 si = spr->rx_jmb_cons_idx;
4879 di = dpr->rx_jmb_prod_idx;
4880
Matt Carlsone92967b2010-02-12 14:47:06 +00004881 for (i = di; i < di + cpycnt; i++) {
4882 if (dpr->rx_jmb_buffers[i].skb) {
4883 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004884 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00004885 break;
4886 }
4887 }
4888
4889 if (!cpycnt)
4890 break;
4891
4892 /* Ensure that updates to the rx_jmb_buffers ring and the
4893 * shadowed hardware producer ring from tg3_recycle_skb() are
4894 * ordered correctly WRT the skb check above.
4895 */
4896 smp_rmb();
4897
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004898 memcpy(&dpr->rx_jmb_buffers[di],
4899 &spr->rx_jmb_buffers[si],
4900 cpycnt * sizeof(struct ring_info));
4901
4902 for (i = 0; i < cpycnt; i++, di++, si++) {
4903 struct tg3_rx_buffer_desc *sbd, *dbd;
4904 sbd = &spr->rx_jmb[si].std;
4905 dbd = &dpr->rx_jmb[di].std;
4906 dbd->addr_hi = sbd->addr_hi;
4907 dbd->addr_lo = sbd->addr_lo;
4908 }
4909
4910 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) %
4911 TG3_RX_JUMBO_RING_SIZE;
4912 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) %
4913 TG3_RX_JUMBO_RING_SIZE;
4914 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004915
4916 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004917}
4918
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004919static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
4920{
4921 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922
4923 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004924 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00004925 tg3_tx(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07004926 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
Michael Chan4fd7ab52007-10-12 01:39:50 -07004927 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928 }
4929
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930 /* run RX thread, within the bounds set by NAPI.
4931 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004932 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004934 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00004935 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004937 if ((tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00004938 struct tg3_rx_prodring_set *dpr = &tp->prodring[0];
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004939 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00004940 u32 std_prod_idx = dpr->rx_std_prod_idx;
4941 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004942
Matt Carlsone4af1af2010-02-12 14:47:05 +00004943 for (i = 1; i < tp->irq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004944 err |= tg3_rx_prodring_xfer(tp, dpr,
4945 tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004946
4947 wmb();
4948
Matt Carlsone4af1af2010-02-12 14:47:05 +00004949 if (std_prod_idx != dpr->rx_std_prod_idx)
4950 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
4951 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004952
Matt Carlsone4af1af2010-02-12 14:47:05 +00004953 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
4954 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
4955 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004956
4957 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004958
4959 if (err)
4960 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004961 }
4962
David S. Miller6f535762007-10-11 18:08:29 -07004963 return work_done;
4964}
David S. Millerf7383c22005-05-18 22:50:53 -07004965
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004966static int tg3_poll_msix(struct napi_struct *napi, int budget)
4967{
4968 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
4969 struct tg3 *tp = tnapi->tp;
4970 int work_done = 0;
4971 struct tg3_hw_status *sblk = tnapi->hw_status;
4972
4973 while (1) {
4974 work_done = tg3_poll_work(tnapi, work_done, budget);
4975
4976 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
4977 goto tx_recovery;
4978
4979 if (unlikely(work_done >= budget))
4980 break;
4981
Matt Carlsonc6cdf432010-04-05 10:19:26 +00004982 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004983 * to tell the hw how much work has been processed,
4984 * so we must read it before checking for more work.
4985 */
4986 tnapi->last_tag = sblk->status_tag;
4987 tnapi->last_irq_tag = tnapi->last_tag;
4988 rmb();
4989
4990 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00004991 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
4992 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004993 napi_complete(napi);
4994 /* Reenable interrupts. */
4995 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
4996 mmiowb();
4997 break;
4998 }
4999 }
5000
5001 return work_done;
5002
5003tx_recovery:
5004 /* work_done is guaranteed to be less than budget. */
5005 napi_complete(napi);
5006 schedule_work(&tp->reset_task);
5007 return work_done;
5008}
5009
David S. Miller6f535762007-10-11 18:08:29 -07005010static int tg3_poll(struct napi_struct *napi, int budget)
5011{
Matt Carlson8ef04422009-08-28 14:01:37 +00005012 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5013 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07005014 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00005015 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07005016
5017 while (1) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005018 tg3_poll_link(tp);
5019
Matt Carlson17375d22009-08-28 14:02:18 +00005020 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07005021
5022 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
5023 goto tx_recovery;
5024
5025 if (unlikely(work_done >= budget))
5026 break;
5027
Michael Chan4fd7ab52007-10-12 01:39:50 -07005028 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) {
Matt Carlson17375d22009-08-28 14:02:18 +00005029 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07005030 * to tell the hw how much work has been processed,
5031 * so we must read it before checking for more work.
5032 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005033 tnapi->last_tag = sblk->status_tag;
5034 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07005035 rmb();
5036 } else
5037 sblk->status &= ~SD_STATUS_UPDATED;
5038
Matt Carlson17375d22009-08-28 14:02:18 +00005039 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005040 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00005041 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07005042 break;
5043 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044 }
5045
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005046 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07005047
5048tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07005049 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08005050 napi_complete(napi);
David S. Miller6f535762007-10-11 18:08:29 -07005051 schedule_work(&tp->reset_task);
Michael Chan4fd7ab52007-10-12 01:39:50 -07005052 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053}
5054
David S. Millerf47c11e2005-06-24 20:18:35 -07005055static void tg3_irq_quiesce(struct tg3 *tp)
5056{
Matt Carlson4f125f42009-09-01 12:55:02 +00005057 int i;
5058
David S. Millerf47c11e2005-06-24 20:18:35 -07005059 BUG_ON(tp->irq_sync);
5060
5061 tp->irq_sync = 1;
5062 smp_mb();
5063
Matt Carlson4f125f42009-09-01 12:55:02 +00005064 for (i = 0; i < tp->irq_cnt; i++)
5065 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07005066}
5067
5068static inline int tg3_irq_sync(struct tg3 *tp)
5069{
5070 return tp->irq_sync;
5071}
5072
5073/* Fully shutdown all tg3 driver activity elsewhere in the system.
5074 * If irq_sync is non-zero, then the IRQ handler must be synchronized
5075 * with as well. Most of the time, this is not necessary except when
5076 * shutting down the device.
5077 */
5078static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
5079{
Michael Chan46966542007-07-11 19:47:19 -07005080 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07005081 if (irq_sync)
5082 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07005083}
5084
5085static inline void tg3_full_unlock(struct tg3 *tp)
5086{
David S. Millerf47c11e2005-06-24 20:18:35 -07005087 spin_unlock_bh(&tp->lock);
5088}
5089
Michael Chanfcfa0a32006-03-20 22:28:41 -08005090/* One-shot MSI handler - Chip automatically disables interrupt
5091 * after sending MSI so driver doesn't have to do it.
5092 */
David Howells7d12e782006-10-05 14:55:46 +01005093static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08005094{
Matt Carlson09943a12009-08-28 14:01:57 +00005095 struct tg3_napi *tnapi = dev_id;
5096 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08005097
Matt Carlson898a56f2009-08-28 14:02:40 +00005098 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005099 if (tnapi->rx_rcb)
5100 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005101
5102 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005103 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005104
5105 return IRQ_HANDLED;
5106}
5107
Michael Chan88b06bc22005-04-21 17:13:25 -07005108/* MSI ISR - No need to check for interrupt sharing and no need to
5109 * flush status block and interrupt mailbox. PCI ordering rules
5110 * guarantee that MSI will arrive after the status block.
5111 */
David Howells7d12e782006-10-05 14:55:46 +01005112static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07005113{
Matt Carlson09943a12009-08-28 14:01:57 +00005114 struct tg3_napi *tnapi = dev_id;
5115 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07005116
Matt Carlson898a56f2009-08-28 14:02:40 +00005117 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005118 if (tnapi->rx_rcb)
5119 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07005120 /*
David S. Millerfac9b832005-05-18 22:46:34 -07005121 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07005122 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07005123 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07005124 * NIC to stop sending us irqs, engaging "in-intr-handler"
5125 * event coalescing.
5126 */
5127 tw32_mailbox(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07005128 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005129 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07005130
Michael Chan88b06bc22005-04-21 17:13:25 -07005131 return IRQ_RETVAL(1);
5132}
5133
David Howells7d12e782006-10-05 14:55:46 +01005134static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135{
Matt Carlson09943a12009-08-28 14:01:57 +00005136 struct tg3_napi *tnapi = dev_id;
5137 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005138 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139 unsigned int handled = 1;
5140
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141 /* In INTx mode, it is possible for the interrupt to arrive at
5142 * the CPU before the status block posted prior to the interrupt.
5143 * Reading the PCI State register will confirm whether the
5144 * interrupt is ours and will flush the status block.
5145 */
Michael Chand18edcb2007-03-24 20:57:11 -07005146 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
5147 if ((tp->tg3_flags & TG3_FLAG_CHIP_RESETTING) ||
5148 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5149 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005150 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07005151 }
Michael Chand18edcb2007-03-24 20:57:11 -07005152 }
5153
5154 /*
5155 * Writing any value to intr-mbox-0 clears PCI INTA# and
5156 * chip-internal interrupt pending events.
5157 * Writing non-zero to intr-mbox-0 additional tells the
5158 * NIC to stop sending us irqs, engaging "in-intr-handler"
5159 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005160 *
5161 * Flush the mailbox to de-assert the IRQ immediately to prevent
5162 * spurious interrupts. The flush impacts performance but
5163 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005164 */
Michael Chanc04cb342007-05-07 00:26:15 -07005165 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07005166 if (tg3_irq_sync(tp))
5167 goto out;
5168 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00005169 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00005170 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00005171 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07005172 } else {
5173 /* No work, shared interrupt perhaps? re-enable
5174 * interrupts, and flush that PCI write
5175 */
5176 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
5177 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07005178 }
David S. Millerf47c11e2005-06-24 20:18:35 -07005179out:
David S. Millerfac9b832005-05-18 22:46:34 -07005180 return IRQ_RETVAL(handled);
5181}
5182
David Howells7d12e782006-10-05 14:55:46 +01005183static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07005184{
Matt Carlson09943a12009-08-28 14:01:57 +00005185 struct tg3_napi *tnapi = dev_id;
5186 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005187 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07005188 unsigned int handled = 1;
5189
David S. Millerfac9b832005-05-18 22:46:34 -07005190 /* In INTx mode, it is possible for the interrupt to arrive at
5191 * the CPU before the status block posted prior to the interrupt.
5192 * Reading the PCI State register will confirm whether the
5193 * interrupt is ours and will flush the status block.
5194 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005195 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Michael Chand18edcb2007-03-24 20:57:11 -07005196 if ((tp->tg3_flags & TG3_FLAG_CHIP_RESETTING) ||
5197 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5198 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005199 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200 }
Michael Chand18edcb2007-03-24 20:57:11 -07005201 }
5202
5203 /*
5204 * writing any value to intr-mbox-0 clears PCI INTA# and
5205 * chip-internal interrupt pending events.
5206 * writing non-zero to intr-mbox-0 additional tells the
5207 * NIC to stop sending us irqs, engaging "in-intr-handler"
5208 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005209 *
5210 * Flush the mailbox to de-assert the IRQ immediately to prevent
5211 * spurious interrupts. The flush impacts performance but
5212 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005213 */
Michael Chanc04cb342007-05-07 00:26:15 -07005214 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00005215
5216 /*
5217 * In a shared interrupt configuration, sometimes other devices'
5218 * interrupts will scream. We record the current status tag here
5219 * so that the above check can report that the screaming interrupts
5220 * are unhandled. Eventually they will be silenced.
5221 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005222 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00005223
Michael Chand18edcb2007-03-24 20:57:11 -07005224 if (tg3_irq_sync(tp))
5225 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00005226
Matt Carlson72334482009-08-28 14:03:01 +00005227 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00005228
Matt Carlson09943a12009-08-28 14:01:57 +00005229 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00005230
David S. Millerf47c11e2005-06-24 20:18:35 -07005231out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232 return IRQ_RETVAL(handled);
5233}
5234
Michael Chan79381092005-04-21 17:13:59 -07005235/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01005236static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07005237{
Matt Carlson09943a12009-08-28 14:01:57 +00005238 struct tg3_napi *tnapi = dev_id;
5239 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005240 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07005241
Michael Chanf9804dd2005-09-27 12:13:10 -07005242 if ((sblk->status & SD_STATUS_UPDATED) ||
5243 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07005244 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07005245 return IRQ_RETVAL(1);
5246 }
5247 return IRQ_RETVAL(0);
5248}
5249
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07005250static int tg3_init_hw(struct tg3 *, int);
Michael Chan944d9802005-05-29 14:57:48 -07005251static int tg3_halt(struct tg3 *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252
Michael Chanb9ec6c12006-07-25 16:37:27 -07005253/* Restart hardware after configuration changes, self-test, etc.
5254 * Invoked with tp->lock held.
5255 */
5256static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
Eric Dumazet78c61462008-04-24 23:33:06 -07005257 __releases(tp->lock)
5258 __acquires(tp->lock)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005259{
5260 int err;
5261
5262 err = tg3_init_hw(tp, reset_phy);
5263 if (err) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00005264 netdev_err(tp->dev,
5265 "Failed to re-initialize device, aborting\n");
Michael Chanb9ec6c12006-07-25 16:37:27 -07005266 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
5267 tg3_full_unlock(tp);
5268 del_timer_sync(&tp->timer);
5269 tp->irq_sync = 0;
Matt Carlsonfed97812009-09-01 13:10:19 +00005270 tg3_napi_enable(tp);
Michael Chanb9ec6c12006-07-25 16:37:27 -07005271 dev_close(tp->dev);
5272 tg3_full_lock(tp, 0);
5273 }
5274 return err;
5275}
5276
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277#ifdef CONFIG_NET_POLL_CONTROLLER
5278static void tg3_poll_controller(struct net_device *dev)
5279{
Matt Carlson4f125f42009-09-01 12:55:02 +00005280 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07005281 struct tg3 *tp = netdev_priv(dev);
5282
Matt Carlson4f125f42009-09-01 12:55:02 +00005283 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00005284 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285}
5286#endif
5287
David Howellsc4028952006-11-22 14:57:56 +00005288static void tg3_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289{
David Howellsc4028952006-11-22 14:57:56 +00005290 struct tg3 *tp = container_of(work, struct tg3, reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005291 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292 unsigned int restart_timer;
5293
Michael Chan7faa0062006-02-02 17:29:28 -08005294 tg3_full_lock(tp, 0);
Michael Chan7faa0062006-02-02 17:29:28 -08005295
5296 if (!netif_running(tp->dev)) {
Michael Chan7faa0062006-02-02 17:29:28 -08005297 tg3_full_unlock(tp);
5298 return;
5299 }
5300
5301 tg3_full_unlock(tp);
5302
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005303 tg3_phy_stop(tp);
5304
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305 tg3_netif_stop(tp);
5306
David S. Millerf47c11e2005-06-24 20:18:35 -07005307 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308
5309 restart_timer = tp->tg3_flags2 & TG3_FLG2_RESTART_TIMER;
5310 tp->tg3_flags2 &= ~TG3_FLG2_RESTART_TIMER;
5311
Michael Chandf3e6542006-05-26 17:48:07 -07005312 if (tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING) {
5313 tp->write32_tx_mbox = tg3_write32_tx_mbox;
5314 tp->write32_rx_mbox = tg3_write_flush_reg32;
5315 tp->tg3_flags |= TG3_FLAG_MBOX_WRITE_REORDER;
5316 tp->tg3_flags &= ~TG3_FLAG_TX_RECOVERY_PENDING;
5317 }
5318
Michael Chan944d9802005-05-29 14:57:48 -07005319 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005320 err = tg3_init_hw(tp, 1);
5321 if (err)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005322 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323
5324 tg3_netif_start(tp);
5325
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326 if (restart_timer)
5327 mod_timer(&tp->timer, jiffies + 1);
Michael Chan7faa0062006-02-02 17:29:28 -08005328
Michael Chanb9ec6c12006-07-25 16:37:27 -07005329out:
Michael Chan7faa0062006-02-02 17:29:28 -08005330 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005331
5332 if (!err)
5333 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334}
5335
Michael Chanb0408752007-02-13 12:18:30 -08005336static void tg3_dump_short_state(struct tg3 *tp)
5337{
Joe Perches05dbe002010-02-17 19:44:19 +00005338 netdev_err(tp->dev, "DEBUG: MAC_TX_STATUS[%08x] MAC_RX_STATUS[%08x]\n",
5339 tr32(MAC_TX_STATUS), tr32(MAC_RX_STATUS));
5340 netdev_err(tp->dev, "DEBUG: RDMAC_STATUS[%08x] WDMAC_STATUS[%08x]\n",
5341 tr32(RDMAC_STATUS), tr32(WDMAC_STATUS));
Michael Chanb0408752007-02-13 12:18:30 -08005342}
5343
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344static void tg3_tx_timeout(struct net_device *dev)
5345{
5346 struct tg3 *tp = netdev_priv(dev);
5347
Michael Chanb0408752007-02-13 12:18:30 -08005348 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00005349 netdev_err(dev, "transmit timed out, resetting\n");
Michael Chanb0408752007-02-13 12:18:30 -08005350 tg3_dump_short_state(tp);
5351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352
5353 schedule_work(&tp->reset_task);
5354}
5355
Michael Chanc58ec932005-09-17 00:46:27 -07005356/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
5357static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
5358{
5359 u32 base = (u32) mapping & 0xffffffff;
5360
5361 return ((base > 0xffffdcc0) &&
5362 (base + len + 8 < base));
5363}
5364
Michael Chan72f2afb2006-03-06 19:28:35 -08005365/* Test for DMA addresses > 40-bit */
5366static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
5367 int len)
5368{
5369#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Michael Chan6728a8e2006-03-27 23:16:49 -08005370 if (tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG)
Yang Hongyang50cf1562009-04-06 19:01:14 -07005371 return (((u64) mapping + len) > DMA_BIT_MASK(40));
Michael Chan72f2afb2006-03-06 19:28:35 -08005372 return 0;
5373#else
5374 return 0;
5375#endif
5376}
5377
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005378static void tg3_set_txd(struct tg3_napi *, int, dma_addr_t, int, u32, u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379
Michael Chan72f2afb2006-03-06 19:28:35 -08005380/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00005381static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
5382 struct sk_buff *skb, u32 last_plus_one,
5383 u32 *start, u32 base_flags, u32 mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384{
Matt Carlson24f4efd2009-11-13 13:03:35 +00005385 struct tg3 *tp = tnapi->tp;
Matt Carlson41588ba2008-04-19 18:12:33 -07005386 struct sk_buff *new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07005387 dma_addr_t new_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388 u32 entry = *start;
Michael Chanc58ec932005-09-17 00:46:27 -07005389 int i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390
Matt Carlson41588ba2008-04-19 18:12:33 -07005391 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
5392 new_skb = skb_copy(skb, GFP_ATOMIC);
5393 else {
5394 int more_headroom = 4 - ((unsigned long)skb->data & 3);
5395
5396 new_skb = skb_copy_expand(skb,
5397 skb_headroom(skb) + more_headroom,
5398 skb_tailroom(skb), GFP_ATOMIC);
5399 }
5400
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07005402 ret = -1;
5403 } else {
5404 /* New SKB is guaranteed to be linear. */
5405 entry = *start;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005406 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
5407 PCI_DMA_TODEVICE);
5408 /* Make sure the mapping succeeded */
5409 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
5410 ret = -1;
5411 dev_kfree_skb(new_skb);
5412 new_skb = NULL;
David S. Miller90079ce2008-09-11 04:52:51 -07005413
Michael Chanc58ec932005-09-17 00:46:27 -07005414 /* Make sure new skb does not cross any 4G boundaries.
5415 * Drop the packet if it does.
5416 */
Alexander Duyckf4188d82009-12-02 16:48:38 +00005417 } else if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
5418 tg3_4g_overflow_test(new_addr, new_skb->len)) {
5419 pci_unmap_single(tp->pdev, new_addr, new_skb->len,
5420 PCI_DMA_TODEVICE);
Michael Chanc58ec932005-09-17 00:46:27 -07005421 ret = -1;
5422 dev_kfree_skb(new_skb);
5423 new_skb = NULL;
5424 } else {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005425 tg3_set_txd(tnapi, entry, new_addr, new_skb->len,
Michael Chanc58ec932005-09-17 00:46:27 -07005426 base_flags, 1 | (mss << 1));
5427 *start = NEXT_TX(entry);
5428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429 }
5430
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 /* Now clean up the sw ring entries. */
5432 i = 0;
5433 while (entry != last_plus_one) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00005434 int len;
5435
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005436 if (i == 0)
Alexander Duyckf4188d82009-12-02 16:48:38 +00005437 len = skb_headlen(skb);
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005438 else
Alexander Duyckf4188d82009-12-02 16:48:38 +00005439 len = skb_shinfo(skb)->frags[i-1].size;
5440
5441 pci_unmap_single(tp->pdev,
5442 pci_unmap_addr(&tnapi->tx_buffers[entry],
5443 mapping),
5444 len, PCI_DMA_TODEVICE);
5445 if (i == 0) {
5446 tnapi->tx_buffers[entry].skb = new_skb;
5447 pci_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
5448 new_addr);
5449 } else {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005450 tnapi->tx_buffers[entry].skb = NULL;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452 entry = NEXT_TX(entry);
5453 i++;
5454 }
5455
5456 dev_kfree_skb(skb);
5457
Michael Chanc58ec932005-09-17 00:46:27 -07005458 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459}
5460
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005461static void tg3_set_txd(struct tg3_napi *tnapi, int entry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462 dma_addr_t mapping, int len, u32 flags,
5463 u32 mss_and_is_end)
5464{
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005465 struct tg3_tx_buffer_desc *txd = &tnapi->tx_ring[entry];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466 int is_end = (mss_and_is_end & 0x1);
5467 u32 mss = (mss_and_is_end >> 1);
5468 u32 vlan_tag = 0;
5469
5470 if (is_end)
5471 flags |= TXD_FLAG_END;
5472 if (flags & TXD_FLAG_VLAN) {
5473 vlan_tag = flags >> 16;
5474 flags &= 0xffff;
5475 }
5476 vlan_tag |= (mss << TXD_MSS_SHIFT);
5477
5478 txd->addr_hi = ((u64) mapping >> 32);
5479 txd->addr_lo = ((u64) mapping & 0xffffffff);
5480 txd->len_flags = (len << TXD_LEN_SHIFT) | flags;
5481 txd->vlan_tag = vlan_tag << TXD_VLAN_TAG_SHIFT;
5482}
5483
Michael Chan5a6f3072006-03-20 22:28:05 -08005484/* hard_start_xmit for devices that don't have any bugs and
Matt Carlsone849cdc2009-11-13 13:03:38 +00005485 * support TG3_FLG2_HW_TSO_2 and TG3_FLG2_HW_TSO_3 only.
Michael Chan5a6f3072006-03-20 22:28:05 -08005486 */
Stephen Hemminger613573252009-08-31 19:50:58 +00005487static netdev_tx_t tg3_start_xmit(struct sk_buff *skb,
5488 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489{
5490 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491 u32 len, entry, base_flags, mss;
David S. Miller90079ce2008-09-11 04:52:51 -07005492 dma_addr_t mapping;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005493 struct tg3_napi *tnapi;
5494 struct netdev_queue *txq;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005495 unsigned int i, last;
5496
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005497 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
5498 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Matt Carlson19cfaec2009-12-03 08:36:20 +00005499 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005500 tnapi++;
Michael Chan5a6f3072006-03-20 22:28:05 -08005501
Michael Chan00b70502006-06-17 21:58:45 -07005502 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005503 * and TX reclaim runs via tp->napi.poll inside of a software
Michael Chan5a6f3072006-03-20 22:28:05 -08005504 * interrupt. Furthermore, IRQ processing runs lockless so we have
5505 * no IRQ context deadlocks to worry about either. Rejoice!
5506 */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005507 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005508 if (!netif_tx_queue_stopped(txq)) {
5509 netif_tx_stop_queue(txq);
Michael Chan5a6f3072006-03-20 22:28:05 -08005510
5511 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00005512 netdev_err(dev,
5513 "BUG! Tx Ring full when queue awake!\n");
Michael Chan5a6f3072006-03-20 22:28:05 -08005514 }
Michael Chan5a6f3072006-03-20 22:28:05 -08005515 return NETDEV_TX_BUSY;
5516 }
5517
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005518 entry = tnapi->tx_prod;
Michael Chan5a6f3072006-03-20 22:28:05 -08005519 base_flags = 0;
Michael Chan5a6f3072006-03-20 22:28:05 -08005520 mss = 0;
Matt Carlsonc13e3712007-05-05 11:50:04 -07005521 if ((mss = skb_shinfo(skb)->gso_size) != 0) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005522 int tcp_opt_len, ip_tcp_len;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005523 u32 hdrlen;
Michael Chan5a6f3072006-03-20 22:28:05 -08005524
5525 if (skb_header_cloned(skb) &&
5526 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
5527 dev_kfree_skb(skb);
5528 goto out_unlock;
5529 }
5530
Michael Chanb0026622006-07-03 19:42:14 -07005531 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005532 hdrlen = skb_headlen(skb) - ETH_HLEN;
Michael Chanb0026622006-07-03 19:42:14 -07005533 else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005534 struct iphdr *iph = ip_hdr(skb);
5535
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07005536 tcp_opt_len = tcp_optlen(skb);
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -03005537 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
Michael Chanb0026622006-07-03 19:42:14 -07005538
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005539 iph->check = 0;
5540 iph->tot_len = htons(mss + ip_tcp_len + tcp_opt_len);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005541 hdrlen = ip_tcp_len + tcp_opt_len;
Michael Chanb0026622006-07-03 19:42:14 -07005542 }
Michael Chan5a6f3072006-03-20 22:28:05 -08005543
Matt Carlsone849cdc2009-11-13 13:03:38 +00005544 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005545 mss |= (hdrlen & 0xc) << 12;
5546 if (hdrlen & 0x10)
5547 base_flags |= 0x00000010;
5548 base_flags |= (hdrlen & 0x3e0) << 5;
5549 } else
5550 mss |= hdrlen << 9;
5551
Michael Chan5a6f3072006-03-20 22:28:05 -08005552 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
5553 TXD_FLAG_CPU_POST_DMA);
5554
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005555 tcp_hdr(skb)->check = 0;
Michael Chan5a6f3072006-03-20 22:28:05 -08005556
Matt Carlson859a588792010-04-05 10:19:28 +00005557 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005558 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson859a588792010-04-05 10:19:28 +00005559 }
5560
Michael Chan5a6f3072006-03-20 22:28:05 -08005561#if TG3_VLAN_TAG_USED
5562 if (tp->vlgrp != NULL && vlan_tx_tag_present(skb))
5563 base_flags |= (TXD_FLAG_VLAN |
5564 (vlan_tx_tag_get(skb) << 16));
5565#endif
5566
Alexander Duyckf4188d82009-12-02 16:48:38 +00005567 len = skb_headlen(skb);
5568
5569 /* Queue skb data, a.k.a. the main skb fragment. */
5570 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
5571 if (pci_dma_mapping_error(tp->pdev, mapping)) {
David S. Miller90079ce2008-09-11 04:52:51 -07005572 dev_kfree_skb(skb);
5573 goto out_unlock;
5574 }
5575
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005576 tnapi->tx_buffers[entry].skb = skb;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005577 pci_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005578
Matt Carlsonb703df62009-12-03 08:36:21 +00005579 if ((tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005580 !mss && skb->len > ETH_DATA_LEN)
5581 base_flags |= TXD_FLAG_JMB_PKT;
5582
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005583 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
Michael Chan5a6f3072006-03-20 22:28:05 -08005584 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
5585
5586 entry = NEXT_TX(entry);
5587
5588 /* Now loop through additional data fragments, and queue them. */
5589 if (skb_shinfo(skb)->nr_frags > 0) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005590 last = skb_shinfo(skb)->nr_frags - 1;
5591 for (i = 0; i <= last; i++) {
5592 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5593
5594 len = frag->size;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005595 mapping = pci_map_page(tp->pdev,
5596 frag->page,
5597 frag->page_offset,
5598 len, PCI_DMA_TODEVICE);
5599 if (pci_dma_mapping_error(tp->pdev, mapping))
5600 goto dma_error;
5601
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005602 tnapi->tx_buffers[entry].skb = NULL;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005603 pci_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
5604 mapping);
Michael Chan5a6f3072006-03-20 22:28:05 -08005605
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005606 tg3_set_txd(tnapi, entry, mapping, len,
Michael Chan5a6f3072006-03-20 22:28:05 -08005607 base_flags, (i == last) | (mss << 1));
5608
5609 entry = NEXT_TX(entry);
5610 }
5611 }
5612
5613 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005614 tw32_tx_mbox(tnapi->prodmbox, entry);
Michael Chan5a6f3072006-03-20 22:28:05 -08005615
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005616 tnapi->tx_prod = entry;
5617 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005618 netif_tx_stop_queue(txq);
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005619 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005620 netif_tx_wake_queue(txq);
Michael Chan5a6f3072006-03-20 22:28:05 -08005621 }
5622
5623out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00005624 mmiowb();
Michael Chan5a6f3072006-03-20 22:28:05 -08005625
5626 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005627
5628dma_error:
5629 last = i;
5630 entry = tnapi->tx_prod;
5631 tnapi->tx_buffers[entry].skb = NULL;
5632 pci_unmap_single(tp->pdev,
5633 pci_unmap_addr(&tnapi->tx_buffers[entry], mapping),
5634 skb_headlen(skb),
5635 PCI_DMA_TODEVICE);
5636 for (i = 0; i <= last; i++) {
5637 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5638 entry = NEXT_TX(entry);
5639
5640 pci_unmap_page(tp->pdev,
5641 pci_unmap_addr(&tnapi->tx_buffers[entry],
5642 mapping),
5643 frag->size, PCI_DMA_TODEVICE);
5644 }
5645
5646 dev_kfree_skb(skb);
5647 return NETDEV_TX_OK;
Michael Chan5a6f3072006-03-20 22:28:05 -08005648}
5649
Stephen Hemminger613573252009-08-31 19:50:58 +00005650static netdev_tx_t tg3_start_xmit_dma_bug(struct sk_buff *,
5651 struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07005652
5653/* Use GSO to workaround a rare TSO bug that may be triggered when the
5654 * TSO header is greater than 80 bytes.
5655 */
5656static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
5657{
5658 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005659 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07005660
5661 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005662 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07005663 netif_stop_queue(tp->dev);
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005664 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08005665 return NETDEV_TX_BUSY;
5666
5667 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07005668 }
5669
5670 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07005671 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07005672 goto tg3_tso_bug_end;
5673
5674 do {
5675 nskb = segs;
5676 segs = segs->next;
5677 nskb->next = NULL;
5678 tg3_start_xmit_dma_bug(nskb, tp->dev);
5679 } while (segs);
5680
5681tg3_tso_bug_end:
5682 dev_kfree_skb(skb);
5683
5684 return NETDEV_TX_OK;
5685}
Michael Chan52c0fd82006-06-29 20:15:54 -07005686
Michael Chan5a6f3072006-03-20 22:28:05 -08005687/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
5688 * support TG3_FLG2_HW_TSO_1 or firmware TSO only.
5689 */
Stephen Hemminger613573252009-08-31 19:50:58 +00005690static netdev_tx_t tg3_start_xmit_dma_bug(struct sk_buff *skb,
5691 struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08005692{
5693 struct tg3 *tp = netdev_priv(dev);
Michael Chan5a6f3072006-03-20 22:28:05 -08005694 u32 len, entry, base_flags, mss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695 int would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07005696 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00005697 struct tg3_napi *tnapi;
5698 struct netdev_queue *txq;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005699 unsigned int i, last;
5700
Matt Carlson24f4efd2009-11-13 13:03:35 +00005701 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
5702 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Matt Carlson19cfaec2009-12-03 08:36:20 +00005703 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlson24f4efd2009-11-13 13:03:35 +00005704 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705
Michael Chan00b70502006-06-17 21:58:45 -07005706 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005707 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07005708 * interrupt. Furthermore, IRQ processing runs lockless so we have
5709 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710 */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005711 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00005712 if (!netif_tx_queue_stopped(txq)) {
5713 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08005714
5715 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00005716 netdev_err(dev,
5717 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08005718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719 return NETDEV_TX_BUSY;
5720 }
5721
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005722 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07005724 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00005726
Matt Carlsonc13e3712007-05-05 11:50:04 -07005727 if ((mss = skb_shinfo(skb)->gso_size) != 0) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005728 struct iphdr *iph;
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005729 u32 tcp_opt_len, ip_tcp_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730
5731 if (skb_header_cloned(skb) &&
5732 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
5733 dev_kfree_skb(skb);
5734 goto out_unlock;
5735 }
5736
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07005737 tcp_opt_len = tcp_optlen(skb);
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -03005738 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739
Michael Chan52c0fd82006-06-29 20:15:54 -07005740 hdr_len = ip_tcp_len + tcp_opt_len;
5741 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Michael Chan7f62ad52007-02-20 23:25:40 -08005742 (tp->tg3_flags2 & TG3_FLG2_TSO_BUG))
Michael Chan52c0fd82006-06-29 20:15:54 -07005743 return (tg3_tso_bug(tp, skb));
5744
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
5746 TXD_FLAG_CPU_POST_DMA);
5747
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005748 iph = ip_hdr(skb);
5749 iph->check = 0;
5750 iph->tot_len = htons(mss + hdr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005752 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005753 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005754 } else
5755 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
5756 iph->daddr, 0,
5757 IPPROTO_TCP,
5758 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759
Matt Carlson615774f2009-11-13 13:03:39 +00005760 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) {
5761 mss |= (hdr_len & 0xc) << 12;
5762 if (hdr_len & 0x10)
5763 base_flags |= 0x00000010;
5764 base_flags |= (hdr_len & 0x3e0) << 5;
5765 } else if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_2)
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005766 mss |= hdr_len << 9;
5767 else if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_1) ||
5768 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005769 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005770 int tsflags;
5771
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005772 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773 mss |= (tsflags << 11);
5774 }
5775 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005776 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777 int tsflags;
5778
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005779 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780 base_flags |= tsflags << 12;
5781 }
5782 }
5783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784#if TG3_VLAN_TAG_USED
5785 if (tp->vlgrp != NULL && vlan_tx_tag_present(skb))
5786 base_flags |= (TXD_FLAG_VLAN |
5787 (vlan_tx_tag_get(skb) << 16));
5788#endif
5789
Matt Carlsonb703df62009-12-03 08:36:21 +00005790 if ((tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) &&
Matt Carlson615774f2009-11-13 13:03:39 +00005791 !mss && skb->len > ETH_DATA_LEN)
5792 base_flags |= TXD_FLAG_JMB_PKT;
5793
Alexander Duyckf4188d82009-12-02 16:48:38 +00005794 len = skb_headlen(skb);
5795
5796 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
5797 if (pci_dma_mapping_error(tp->pdev, mapping)) {
David S. Miller90079ce2008-09-11 04:52:51 -07005798 dev_kfree_skb(skb);
5799 goto out_unlock;
5800 }
5801
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005802 tnapi->tx_buffers[entry].skb = skb;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005803 pci_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804
5805 would_hit_hwbug = 0;
5806
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005807 if ((tp->tg3_flags3 & TG3_FLG3_SHORT_DMA_BUG) && len <= 8)
5808 would_hit_hwbug = 1;
5809
Matt Carlson0e1406d2009-11-02 12:33:33 +00005810 if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
5811 tg3_4g_overflow_test(mapping, len))
Matt Carlson41588ba2008-04-19 18:12:33 -07005812 would_hit_hwbug = 1;
Matt Carlson0e1406d2009-11-02 12:33:33 +00005813
5814 if ((tp->tg3_flags3 & TG3_FLG3_40BIT_DMA_LIMIT_BUG) &&
5815 tg3_40bit_overflow_test(tp, mapping, len))
5816 would_hit_hwbug = 1;
5817
5818 if (tp->tg3_flags3 & TG3_FLG3_5701_DMA_BUG)
Michael Chanc58ec932005-09-17 00:46:27 -07005819 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005821 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005822 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
5823
5824 entry = NEXT_TX(entry);
5825
5826 /* Now loop through additional data fragments, and queue them. */
5827 if (skb_shinfo(skb)->nr_frags > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828 last = skb_shinfo(skb)->nr_frags - 1;
5829 for (i = 0; i <= last; i++) {
5830 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5831
5832 len = frag->size;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005833 mapping = pci_map_page(tp->pdev,
5834 frag->page,
5835 frag->page_offset,
5836 len, PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005837
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005838 tnapi->tx_buffers[entry].skb = NULL;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005839 pci_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
5840 mapping);
5841 if (pci_dma_mapping_error(tp->pdev, mapping))
5842 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005844 if ((tp->tg3_flags3 & TG3_FLG3_SHORT_DMA_BUG) &&
5845 len <= 8)
5846 would_hit_hwbug = 1;
5847
Matt Carlson0e1406d2009-11-02 12:33:33 +00005848 if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
5849 tg3_4g_overflow_test(mapping, len))
Michael Chanc58ec932005-09-17 00:46:27 -07005850 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851
Matt Carlson0e1406d2009-11-02 12:33:33 +00005852 if ((tp->tg3_flags3 & TG3_FLG3_40BIT_DMA_LIMIT_BUG) &&
5853 tg3_40bit_overflow_test(tp, mapping, len))
Michael Chan72f2afb2006-03-06 19:28:35 -08005854 would_hit_hwbug = 1;
5855
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005857 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005858 base_flags, (i == last)|(mss << 1));
5859 else
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005860 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005861 base_flags, (i == last));
5862
5863 entry = NEXT_TX(entry);
5864 }
5865 }
5866
5867 if (would_hit_hwbug) {
5868 u32 last_plus_one = entry;
5869 u32 start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870
Michael Chanc58ec932005-09-17 00:46:27 -07005871 start = entry - 1 - skb_shinfo(skb)->nr_frags;
5872 start &= (TG3_TX_RING_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873
5874 /* If the workaround fails due to memory/mapping
5875 * failure, silently drop this packet.
5876 */
Matt Carlson24f4efd2009-11-13 13:03:35 +00005877 if (tigon3_dma_hwbug_workaround(tnapi, skb, last_plus_one,
Michael Chanc58ec932005-09-17 00:46:27 -07005878 &start, base_flags, mss))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005879 goto out_unlock;
5880
5881 entry = start;
5882 }
5883
5884 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00005885 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005886
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005887 tnapi->tx_prod = entry;
5888 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00005889 netif_tx_stop_queue(txq);
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005890 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00005891 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07005892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005893
5894out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00005895 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005896
5897 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005898
5899dma_error:
5900 last = i;
5901 entry = tnapi->tx_prod;
5902 tnapi->tx_buffers[entry].skb = NULL;
5903 pci_unmap_single(tp->pdev,
5904 pci_unmap_addr(&tnapi->tx_buffers[entry], mapping),
5905 skb_headlen(skb),
5906 PCI_DMA_TODEVICE);
5907 for (i = 0; i <= last; i++) {
5908 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5909 entry = NEXT_TX(entry);
5910
5911 pci_unmap_page(tp->pdev,
5912 pci_unmap_addr(&tnapi->tx_buffers[entry],
5913 mapping),
5914 frag->size, PCI_DMA_TODEVICE);
5915 }
5916
5917 dev_kfree_skb(skb);
5918 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005919}
5920
5921static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
5922 int new_mtu)
5923{
5924 dev->mtu = new_mtu;
5925
Michael Chanef7f5ec2005-07-25 12:32:25 -07005926 if (new_mtu > ETH_DATA_LEN) {
Michael Chana4e2b342005-10-26 15:46:52 -07005927 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
Michael Chanef7f5ec2005-07-25 12:32:25 -07005928 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
5929 ethtool_op_set_tso(dev, 0);
Matt Carlson859a588792010-04-05 10:19:28 +00005930 } else {
Michael Chanef7f5ec2005-07-25 12:32:25 -07005931 tp->tg3_flags |= TG3_FLAG_JUMBO_RING_ENABLE;
Matt Carlson859a588792010-04-05 10:19:28 +00005932 }
Michael Chanef7f5ec2005-07-25 12:32:25 -07005933 } else {
Michael Chana4e2b342005-10-26 15:46:52 -07005934 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
Michael Chanef7f5ec2005-07-25 12:32:25 -07005935 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
Michael Chan0f893dc2005-07-25 12:30:38 -07005936 tp->tg3_flags &= ~TG3_FLAG_JUMBO_RING_ENABLE;
Michael Chanef7f5ec2005-07-25 12:32:25 -07005937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005938}
5939
5940static int tg3_change_mtu(struct net_device *dev, int new_mtu)
5941{
5942 struct tg3 *tp = netdev_priv(dev);
Michael Chanb9ec6c12006-07-25 16:37:27 -07005943 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944
5945 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
5946 return -EINVAL;
5947
5948 if (!netif_running(dev)) {
5949 /* We'll just catch it later when the
5950 * device is up'd.
5951 */
5952 tg3_set_mtu(dev, tp, new_mtu);
5953 return 0;
5954 }
5955
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005956 tg3_phy_stop(tp);
5957
Linus Torvalds1da177e2005-04-16 15:20:36 -07005958 tg3_netif_stop(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07005959
5960 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005961
Michael Chan944d9802005-05-29 14:57:48 -07005962 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005963
5964 tg3_set_mtu(dev, tp, new_mtu);
5965
Michael Chanb9ec6c12006-07-25 16:37:27 -07005966 err = tg3_restart_hw(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005967
Michael Chanb9ec6c12006-07-25 16:37:27 -07005968 if (!err)
5969 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005970
David S. Millerf47c11e2005-06-24 20:18:35 -07005971 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005972
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005973 if (!err)
5974 tg3_phy_start(tp);
5975
Michael Chanb9ec6c12006-07-25 16:37:27 -07005976 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977}
5978
Matt Carlson21f581a2009-08-28 14:00:25 +00005979static void tg3_rx_prodring_free(struct tg3 *tp,
5980 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005981{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982 int i;
5983
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005984 if (tpr != &tp->prodring[0]) {
5985 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
5986 i = (i + 1) % TG3_RX_RING_SIZE)
5987 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
5988 tp->rx_pkt_map_sz);
5989
5990 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
5991 for (i = tpr->rx_jmb_cons_idx;
5992 i != tpr->rx_jmb_prod_idx;
5993 i = (i + 1) % TG3_RX_JUMBO_RING_SIZE) {
5994 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
5995 TG3_RX_JMB_MAP_SZ);
5996 }
5997 }
5998
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005999 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006001
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006002 for (i = 0; i < TG3_RX_RING_SIZE; i++)
6003 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6004 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006005
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006006 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006007 for (i = 0; i < TG3_RX_JUMBO_RING_SIZE; i++)
6008 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6009 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010 }
6011}
6012
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006013/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006014 *
6015 * The chip has been shut down and the driver detached from
6016 * the networking, so no interrupts or new tx packets will
6017 * end up in the driver. tp->{tx,}lock are held and thus
6018 * we may not sleep.
6019 */
Matt Carlson21f581a2009-08-28 14:00:25 +00006020static int tg3_rx_prodring_alloc(struct tg3 *tp,
6021 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022{
Matt Carlson287be122009-08-28 13:58:46 +00006023 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006024
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006025 tpr->rx_std_cons_idx = 0;
6026 tpr->rx_std_prod_idx = 0;
6027 tpr->rx_jmb_cons_idx = 0;
6028 tpr->rx_jmb_prod_idx = 0;
6029
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006030 if (tpr != &tp->prodring[0]) {
6031 memset(&tpr->rx_std_buffers[0], 0, TG3_RX_STD_BUFF_RING_SIZE);
6032 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE)
6033 memset(&tpr->rx_jmb_buffers[0], 0,
6034 TG3_RX_JMB_BUFF_RING_SIZE);
6035 goto done;
6036 }
6037
Linus Torvalds1da177e2005-04-16 15:20:36 -07006038 /* Zero out all descriptors. */
Matt Carlson21f581a2009-08-28 14:00:25 +00006039 memset(tpr->rx_std, 0, TG3_RX_RING_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040
Matt Carlson287be122009-08-28 13:58:46 +00006041 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Michael Chana4e2b342005-10-26 15:46:52 -07006042 if ((tp->tg3_flags2 & TG3_FLG2_5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00006043 tp->dev->mtu > ETH_DATA_LEN)
6044 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
6045 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07006046
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047 /* Initialize invariants of the rings, we only set this
6048 * stuff once. This works because the card does not
6049 * write into the rx buffer posting rings.
6050 */
6051 for (i = 0; i < TG3_RX_RING_SIZE; i++) {
6052 struct tg3_rx_buffer_desc *rxd;
6053
Matt Carlson21f581a2009-08-28 14:00:25 +00006054 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00006055 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006056 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
6057 rxd->opaque = (RXD_OPAQUE_RING_STD |
6058 (i << RXD_OPAQUE_INDEX_SHIFT));
6059 }
6060
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006061 /* Now allocate fresh SKBs for each rx ring. */
6062 for (i = 0; i < tp->rx_pending; i++) {
Matt Carlson86b21e52009-11-13 13:03:45 +00006063 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_STD, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006064 netdev_warn(tp->dev,
6065 "Using a smaller RX standard ring. Only "
6066 "%d out of %d buffers were allocated "
6067 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006068 if (i == 0)
6069 goto initfail;
6070 tp->rx_pending = i;
6071 break;
6072 }
6073 }
6074
6075 if (!(tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE))
6076 goto done;
6077
Matt Carlson21f581a2009-08-28 14:00:25 +00006078 memset(tpr->rx_jmb, 0, TG3_RX_JUMBO_RING_BYTES);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006079
Matt Carlson0d86df82010-02-17 15:17:00 +00006080 if (!(tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE))
6081 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082
Matt Carlson0d86df82010-02-17 15:17:00 +00006083 for (i = 0; i < TG3_RX_JUMBO_RING_SIZE; i++) {
6084 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006085
Matt Carlson0d86df82010-02-17 15:17:00 +00006086 rxd = &tpr->rx_jmb[i].std;
6087 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
6088 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
6089 RXD_FLAG_JUMBO;
6090 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
6091 (i << RXD_OPAQUE_INDEX_SHIFT));
6092 }
6093
6094 for (i = 0; i < tp->rx_jumbo_pending; i++) {
6095 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_JUMBO, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006096 netdev_warn(tp->dev,
6097 "Using a smaller RX jumbo ring. Only %d "
6098 "out of %d buffers were allocated "
6099 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00006100 if (i == 0)
6101 goto initfail;
6102 tp->rx_jumbo_pending = i;
6103 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104 }
6105 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006106
6107done:
Michael Chan32d8c572006-07-25 16:38:29 -07006108 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006109
6110initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00006111 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006112 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006113}
6114
Matt Carlson21f581a2009-08-28 14:00:25 +00006115static void tg3_rx_prodring_fini(struct tg3 *tp,
6116 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006117{
Matt Carlson21f581a2009-08-28 14:00:25 +00006118 kfree(tpr->rx_std_buffers);
6119 tpr->rx_std_buffers = NULL;
6120 kfree(tpr->rx_jmb_buffers);
6121 tpr->rx_jmb_buffers = NULL;
6122 if (tpr->rx_std) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006123 pci_free_consistent(tp->pdev, TG3_RX_RING_BYTES,
Matt Carlson21f581a2009-08-28 14:00:25 +00006124 tpr->rx_std, tpr->rx_std_mapping);
6125 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006126 }
Matt Carlson21f581a2009-08-28 14:00:25 +00006127 if (tpr->rx_jmb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006128 pci_free_consistent(tp->pdev, TG3_RX_JUMBO_RING_BYTES,
Matt Carlson21f581a2009-08-28 14:00:25 +00006129 tpr->rx_jmb, tpr->rx_jmb_mapping);
6130 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006131 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006132}
6133
Matt Carlson21f581a2009-08-28 14:00:25 +00006134static int tg3_rx_prodring_init(struct tg3 *tp,
6135 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006136{
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006137 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE, GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006138 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006139 return -ENOMEM;
6140
Matt Carlson21f581a2009-08-28 14:00:25 +00006141 tpr->rx_std = pci_alloc_consistent(tp->pdev, TG3_RX_RING_BYTES,
6142 &tpr->rx_std_mapping);
6143 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006144 goto err_out;
6145
6146 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006147 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE,
Matt Carlson21f581a2009-08-28 14:00:25 +00006148 GFP_KERNEL);
6149 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006150 goto err_out;
6151
Matt Carlson21f581a2009-08-28 14:00:25 +00006152 tpr->rx_jmb = pci_alloc_consistent(tp->pdev,
6153 TG3_RX_JUMBO_RING_BYTES,
6154 &tpr->rx_jmb_mapping);
6155 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006156 goto err_out;
6157 }
6158
6159 return 0;
6160
6161err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00006162 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006163 return -ENOMEM;
6164}
6165
6166/* Free up pending packets in all rx/tx rings.
6167 *
6168 * The chip has been shut down and the driver detached from
6169 * the networking, so no interrupts or new tx packets will
6170 * end up in the driver. tp->{tx,}lock is not held and we are not
6171 * in an interrupt context and thus may sleep.
6172 */
6173static void tg3_free_rings(struct tg3 *tp)
6174{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006175 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006176
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006177 for (j = 0; j < tp->irq_cnt; j++) {
6178 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006179
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006180 if (!tnapi->tx_buffers)
6181 continue;
6182
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006183 for (i = 0; i < TG3_TX_RING_SIZE; ) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00006184 struct ring_info *txp;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006185 struct sk_buff *skb;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006186 unsigned int k;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006187
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006188 txp = &tnapi->tx_buffers[i];
6189 skb = txp->skb;
6190
6191 if (skb == NULL) {
6192 i++;
6193 continue;
6194 }
6195
Alexander Duyckf4188d82009-12-02 16:48:38 +00006196 pci_unmap_single(tp->pdev,
6197 pci_unmap_addr(txp, mapping),
6198 skb_headlen(skb),
6199 PCI_DMA_TODEVICE);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006200 txp->skb = NULL;
6201
Alexander Duyckf4188d82009-12-02 16:48:38 +00006202 i++;
6203
6204 for (k = 0; k < skb_shinfo(skb)->nr_frags; k++) {
6205 txp = &tnapi->tx_buffers[i & (TG3_TX_RING_SIZE - 1)];
6206 pci_unmap_page(tp->pdev,
6207 pci_unmap_addr(txp, mapping),
6208 skb_shinfo(skb)->frags[k].size,
6209 PCI_DMA_TODEVICE);
6210 i++;
6211 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006212
6213 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006214 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006215
Matt Carlsone4af1af2010-02-12 14:47:05 +00006216 tg3_rx_prodring_free(tp, &tp->prodring[j]);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006217 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006218}
6219
6220/* Initialize tx/rx rings for packet processing.
6221 *
6222 * The chip has been shut down and the driver detached from
6223 * the networking, so no interrupts or new tx packets will
6224 * end up in the driver. tp->{tx,}lock are held and thus
6225 * we may not sleep.
6226 */
6227static int tg3_init_rings(struct tg3 *tp)
6228{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006229 int i;
Matt Carlson72334482009-08-28 14:03:01 +00006230
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006231 /* Free up all the SKBs. */
6232 tg3_free_rings(tp);
6233
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006234 for (i = 0; i < tp->irq_cnt; i++) {
6235 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006236
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006237 tnapi->last_tag = 0;
6238 tnapi->last_irq_tag = 0;
6239 tnapi->hw_status->status = 0;
6240 tnapi->hw_status->status_tag = 0;
6241 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6242
6243 tnapi->tx_prod = 0;
6244 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006245 if (tnapi->tx_ring)
6246 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006247
6248 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006249 if (tnapi->rx_rcb)
6250 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006251
Matt Carlsone4af1af2010-02-12 14:47:05 +00006252 if (tg3_rx_prodring_alloc(tp, &tp->prodring[i])) {
6253 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006254 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006255 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006256 }
Matt Carlson72334482009-08-28 14:03:01 +00006257
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006258 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006259}
6260
6261/*
6262 * Must not be invoked with interrupt sources disabled and
6263 * the hardware shutdown down.
6264 */
6265static void tg3_free_consistent(struct tg3 *tp)
6266{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006267 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006268
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006269 for (i = 0; i < tp->irq_cnt; i++) {
6270 struct tg3_napi *tnapi = &tp->napi[i];
6271
6272 if (tnapi->tx_ring) {
6273 pci_free_consistent(tp->pdev, TG3_TX_RING_BYTES,
6274 tnapi->tx_ring, tnapi->tx_desc_mapping);
6275 tnapi->tx_ring = NULL;
6276 }
6277
6278 kfree(tnapi->tx_buffers);
6279 tnapi->tx_buffers = NULL;
6280
6281 if (tnapi->rx_rcb) {
6282 pci_free_consistent(tp->pdev, TG3_RX_RCB_RING_BYTES(tp),
6283 tnapi->rx_rcb,
6284 tnapi->rx_rcb_mapping);
6285 tnapi->rx_rcb = NULL;
6286 }
6287
6288 if (tnapi->hw_status) {
6289 pci_free_consistent(tp->pdev, TG3_HW_STATUS_SIZE,
6290 tnapi->hw_status,
6291 tnapi->status_mapping);
6292 tnapi->hw_status = NULL;
6293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006294 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006295
Linus Torvalds1da177e2005-04-16 15:20:36 -07006296 if (tp->hw_stats) {
6297 pci_free_consistent(tp->pdev, sizeof(struct tg3_hw_stats),
6298 tp->hw_stats, tp->stats_mapping);
6299 tp->hw_stats = NULL;
6300 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006301
Matt Carlsone4af1af2010-02-12 14:47:05 +00006302 for (i = 0; i < tp->irq_cnt; i++)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006303 tg3_rx_prodring_fini(tp, &tp->prodring[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006304}
6305
6306/*
6307 * Must not be invoked with interrupt sources disabled and
6308 * the hardware shutdown down. Can sleep.
6309 */
6310static int tg3_alloc_consistent(struct tg3 *tp)
6311{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006312 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006313
Matt Carlsone4af1af2010-02-12 14:47:05 +00006314 for (i = 0; i < tp->irq_cnt; i++) {
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006315 if (tg3_rx_prodring_init(tp, &tp->prodring[i]))
6316 goto err_out;
6317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006318
Linus Torvalds1da177e2005-04-16 15:20:36 -07006319 tp->hw_stats = pci_alloc_consistent(tp->pdev,
6320 sizeof(struct tg3_hw_stats),
6321 &tp->stats_mapping);
6322 if (!tp->hw_stats)
6323 goto err_out;
6324
Linus Torvalds1da177e2005-04-16 15:20:36 -07006325 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6326
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006327 for (i = 0; i < tp->irq_cnt; i++) {
6328 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006329 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006330
6331 tnapi->hw_status = pci_alloc_consistent(tp->pdev,
6332 TG3_HW_STATUS_SIZE,
6333 &tnapi->status_mapping);
6334 if (!tnapi->hw_status)
6335 goto err_out;
6336
6337 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006338 sblk = tnapi->hw_status;
6339
Matt Carlson19cfaec2009-12-03 08:36:20 +00006340 /* If multivector TSS is enabled, vector 0 does not handle
6341 * tx interrupts. Don't allocate any resources for it.
6342 */
6343 if ((!i && !(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)) ||
6344 (i && (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS))) {
6345 tnapi->tx_buffers = kzalloc(sizeof(struct ring_info) *
6346 TG3_TX_RING_SIZE,
6347 GFP_KERNEL);
6348 if (!tnapi->tx_buffers)
6349 goto err_out;
6350
6351 tnapi->tx_ring = pci_alloc_consistent(tp->pdev,
6352 TG3_TX_RING_BYTES,
6353 &tnapi->tx_desc_mapping);
6354 if (!tnapi->tx_ring)
6355 goto err_out;
6356 }
6357
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006358 /*
6359 * When RSS is enabled, the status block format changes
6360 * slightly. The "rx_jumbo_consumer", "reserved",
6361 * and "rx_mini_consumer" members get mapped to the
6362 * other three rx return ring producer indexes.
6363 */
6364 switch (i) {
6365 default:
6366 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
6367 break;
6368 case 2:
6369 tnapi->rx_rcb_prod_idx = &sblk->rx_jumbo_consumer;
6370 break;
6371 case 3:
6372 tnapi->rx_rcb_prod_idx = &sblk->reserved;
6373 break;
6374 case 4:
6375 tnapi->rx_rcb_prod_idx = &sblk->rx_mini_consumer;
6376 break;
6377 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006378
Matt Carlsone4af1af2010-02-12 14:47:05 +00006379 tnapi->prodring = &tp->prodring[i];
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006380
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006381 /*
6382 * If multivector RSS is enabled, vector 0 does not handle
6383 * rx or tx interrupts. Don't allocate any resources for it.
6384 */
6385 if (!i && (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS))
6386 continue;
6387
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006388 tnapi->rx_rcb = pci_alloc_consistent(tp->pdev,
6389 TG3_RX_RCB_RING_BYTES(tp),
6390 &tnapi->rx_rcb_mapping);
6391 if (!tnapi->rx_rcb)
6392 goto err_out;
6393
6394 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006395 }
6396
Linus Torvalds1da177e2005-04-16 15:20:36 -07006397 return 0;
6398
6399err_out:
6400 tg3_free_consistent(tp);
6401 return -ENOMEM;
6402}
6403
6404#define MAX_WAIT_CNT 1000
6405
6406/* To stop a block, clear the enable bit and poll till it
6407 * clears. tp->lock is held.
6408 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006409static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006410{
6411 unsigned int i;
6412 u32 val;
6413
6414 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
6415 switch (ofs) {
6416 case RCVLSC_MODE:
6417 case DMAC_MODE:
6418 case MBFREE_MODE:
6419 case BUFMGR_MODE:
6420 case MEMARB_MODE:
6421 /* We can't enable/disable these bits of the
6422 * 5705/5750, just say success.
6423 */
6424 return 0;
6425
6426 default:
6427 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006429 }
6430
6431 val = tr32(ofs);
6432 val &= ~enable_bit;
6433 tw32_f(ofs, val);
6434
6435 for (i = 0; i < MAX_WAIT_CNT; i++) {
6436 udelay(100);
6437 val = tr32(ofs);
6438 if ((val & enable_bit) == 0)
6439 break;
6440 }
6441
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006442 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00006443 dev_err(&tp->pdev->dev,
6444 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
6445 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006446 return -ENODEV;
6447 }
6448
6449 return 0;
6450}
6451
6452/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006453static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006454{
6455 int i, err;
6456
6457 tg3_disable_ints(tp);
6458
6459 tp->rx_mode &= ~RX_MODE_ENABLE;
6460 tw32_f(MAC_RX_MODE, tp->rx_mode);
6461 udelay(10);
6462
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006463 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
6464 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
6465 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
6466 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
6467 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
6468 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006469
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006470 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
6471 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
6472 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
6473 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
6474 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
6475 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
6476 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006477
6478 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
6479 tw32_f(MAC_MODE, tp->mac_mode);
6480 udelay(40);
6481
6482 tp->tx_mode &= ~TX_MODE_ENABLE;
6483 tw32_f(MAC_TX_MODE, tp->tx_mode);
6484
6485 for (i = 0; i < MAX_WAIT_CNT; i++) {
6486 udelay(100);
6487 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
6488 break;
6489 }
6490 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00006491 dev_err(&tp->pdev->dev,
6492 "%s timed out, TX_MODE_ENABLE will not clear "
6493 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07006494 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006495 }
6496
Michael Chane6de8ad2005-05-05 14:42:41 -07006497 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006498 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
6499 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006500
6501 tw32(FTQ_RESET, 0xffffffff);
6502 tw32(FTQ_RESET, 0x00000000);
6503
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006504 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
6505 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006506
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006507 for (i = 0; i < tp->irq_cnt; i++) {
6508 struct tg3_napi *tnapi = &tp->napi[i];
6509 if (tnapi->hw_status)
6510 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006512 if (tp->hw_stats)
6513 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6514
Linus Torvalds1da177e2005-04-16 15:20:36 -07006515 return err;
6516}
6517
Matt Carlson0d3031d2007-10-10 18:02:43 -07006518static void tg3_ape_send_event(struct tg3 *tp, u32 event)
6519{
6520 int i;
6521 u32 apedata;
6522
6523 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
6524 if (apedata != APE_SEG_SIG_MAGIC)
6525 return;
6526
6527 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
Matt Carlson731fd792008-08-15 14:07:51 -07006528 if (!(apedata & APE_FW_STATUS_READY))
Matt Carlson0d3031d2007-10-10 18:02:43 -07006529 return;
6530
6531 /* Wait for up to 1 millisecond for APE to service previous event. */
6532 for (i = 0; i < 10; i++) {
6533 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
6534 return;
6535
6536 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
6537
6538 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6539 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
6540 event | APE_EVENT_STATUS_EVENT_PENDING);
6541
6542 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
6543
6544 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6545 break;
6546
6547 udelay(100);
6548 }
6549
6550 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6551 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
6552}
6553
6554static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
6555{
6556 u32 event;
6557 u32 apedata;
6558
6559 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
6560 return;
6561
6562 switch (kind) {
Matt Carlson33f401a2010-04-05 10:19:27 +00006563 case RESET_KIND_INIT:
6564 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
6565 APE_HOST_SEG_SIG_MAGIC);
6566 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
6567 APE_HOST_SEG_LEN_MAGIC);
6568 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
6569 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
6570 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
6571 APE_HOST_DRIVER_ID_MAGIC);
6572 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
6573 APE_HOST_BEHAV_NO_PHYLOCK);
Matt Carlson0d3031d2007-10-10 18:02:43 -07006574
Matt Carlson33f401a2010-04-05 10:19:27 +00006575 event = APE_EVENT_STATUS_STATE_START;
6576 break;
6577 case RESET_KIND_SHUTDOWN:
6578 /* With the interface we are currently using,
6579 * APE does not track driver state. Wiping
6580 * out the HOST SEGMENT SIGNATURE forces
6581 * the APE to assume OS absent status.
6582 */
6583 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
Matt Carlsonb2aee152008-11-03 16:51:11 -08006584
Matt Carlson33f401a2010-04-05 10:19:27 +00006585 event = APE_EVENT_STATUS_STATE_UNLOAD;
6586 break;
6587 case RESET_KIND_SUSPEND:
6588 event = APE_EVENT_STATUS_STATE_SUSPEND;
6589 break;
6590 default:
6591 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -07006592 }
6593
6594 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
6595
6596 tg3_ape_send_event(tp, event);
6597}
6598
Michael Chane6af3012005-04-21 17:12:05 -07006599/* tp->lock is held. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006600static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
6601{
David S. Millerf49639e2006-06-09 11:58:36 -07006602 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
6603 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006604
6605 if (tp->tg3_flags2 & TG3_FLG2_ASF_NEW_HANDSHAKE) {
6606 switch (kind) {
6607 case RESET_KIND_INIT:
6608 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6609 DRV_STATE_START);
6610 break;
6611
6612 case RESET_KIND_SHUTDOWN:
6613 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6614 DRV_STATE_UNLOAD);
6615 break;
6616
6617 case RESET_KIND_SUSPEND:
6618 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6619 DRV_STATE_SUSPEND);
6620 break;
6621
6622 default:
6623 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006625 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07006626
6627 if (kind == RESET_KIND_INIT ||
6628 kind == RESET_KIND_SUSPEND)
6629 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006630}
6631
6632/* tp->lock is held. */
6633static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
6634{
6635 if (tp->tg3_flags2 & TG3_FLG2_ASF_NEW_HANDSHAKE) {
6636 switch (kind) {
6637 case RESET_KIND_INIT:
6638 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6639 DRV_STATE_START_DONE);
6640 break;
6641
6642 case RESET_KIND_SHUTDOWN:
6643 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6644 DRV_STATE_UNLOAD_DONE);
6645 break;
6646
6647 default:
6648 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006650 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07006651
6652 if (kind == RESET_KIND_SHUTDOWN)
6653 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006654}
6655
6656/* tp->lock is held. */
6657static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
6658{
6659 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) {
6660 switch (kind) {
6661 case RESET_KIND_INIT:
6662 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6663 DRV_STATE_START);
6664 break;
6665
6666 case RESET_KIND_SHUTDOWN:
6667 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6668 DRV_STATE_UNLOAD);
6669 break;
6670
6671 case RESET_KIND_SUSPEND:
6672 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6673 DRV_STATE_SUSPEND);
6674 break;
6675
6676 default:
6677 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006679 }
6680}
6681
Michael Chan7a6f4362006-09-27 16:03:31 -07006682static int tg3_poll_fw(struct tg3 *tp)
6683{
6684 int i;
6685 u32 val;
6686
Michael Chanb5d37722006-09-27 16:06:21 -07006687 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Gary Zambrano0ccead12006-11-14 16:34:00 -08006688 /* Wait up to 20ms for init done. */
6689 for (i = 0; i < 200; i++) {
Michael Chanb5d37722006-09-27 16:06:21 -07006690 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
6691 return 0;
Gary Zambrano0ccead12006-11-14 16:34:00 -08006692 udelay(100);
Michael Chanb5d37722006-09-27 16:06:21 -07006693 }
6694 return -ENODEV;
6695 }
6696
Michael Chan7a6f4362006-09-27 16:03:31 -07006697 /* Wait for firmware initialization to complete. */
6698 for (i = 0; i < 100000; i++) {
6699 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
6700 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
6701 break;
6702 udelay(10);
6703 }
6704
6705 /* Chip might not be fitted with firmware. Some Sun onboard
6706 * parts are configured like that. So don't signal the timeout
6707 * of the above loop as an error, but do report the lack of
6708 * running firmware once.
6709 */
6710 if (i >= 100000 &&
6711 !(tp->tg3_flags2 & TG3_FLG2_NO_FWARE_REPORTED)) {
6712 tp->tg3_flags2 |= TG3_FLG2_NO_FWARE_REPORTED;
6713
Joe Perches05dbe002010-02-17 19:44:19 +00006714 netdev_info(tp->dev, "No firmware running\n");
Michael Chan7a6f4362006-09-27 16:03:31 -07006715 }
6716
Matt Carlson6b10c162010-02-12 14:47:08 +00006717 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
6718 /* The 57765 A0 needs a little more
6719 * time to do some important work.
6720 */
6721 mdelay(10);
6722 }
6723
Michael Chan7a6f4362006-09-27 16:03:31 -07006724 return 0;
6725}
6726
Michael Chanee6a99b2007-07-18 21:49:10 -07006727/* Save PCI command register before chip reset */
6728static void tg3_save_pci_state(struct tg3 *tp)
6729{
Matt Carlson8a6eac92007-10-21 16:17:55 -07006730 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07006731}
6732
6733/* Restore PCI state after chip reset */
6734static void tg3_restore_pci_state(struct tg3 *tp)
6735{
6736 u32 val;
6737
6738 /* Re-enable indirect register accesses. */
6739 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
6740 tp->misc_host_ctrl);
6741
6742 /* Set MAX PCI retry to zero. */
6743 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
6744 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
6745 (tp->tg3_flags & TG3_FLAG_PCIX_MODE))
6746 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07006747 /* Allow reads and writes to the APE register and memory space. */
6748 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
6749 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
6750 PCISTATE_ALLOW_APE_SHMEM_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07006751 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
6752
Matt Carlson8a6eac92007-10-21 16:17:55 -07006753 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07006754
Matt Carlsonfcb389d2008-11-03 16:55:44 -08006755 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785) {
6756 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)
6757 pcie_set_readrq(tp->pdev, 4096);
6758 else {
6759 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
6760 tp->pci_cacheline_sz);
6761 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
6762 tp->pci_lat_timer);
6763 }
Michael Chan114342f2007-10-15 02:12:26 -07006764 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08006765
Michael Chanee6a99b2007-07-18 21:49:10 -07006766 /* Make sure PCI-X relaxed ordering bit is clear. */
Matt Carlson52f44902008-11-21 17:17:04 -08006767 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
Matt Carlson9974a352007-10-07 23:27:28 -07006768 u16 pcix_cmd;
6769
6770 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
6771 &pcix_cmd);
6772 pcix_cmd &= ~PCI_X_CMD_ERO;
6773 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
6774 pcix_cmd);
6775 }
Michael Chanee6a99b2007-07-18 21:49:10 -07006776
6777 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
Michael Chanee6a99b2007-07-18 21:49:10 -07006778
6779 /* Chip reset on 5780 will reset MSI enable bit,
6780 * so need to restore it.
6781 */
6782 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
6783 u16 ctrl;
6784
6785 pci_read_config_word(tp->pdev,
6786 tp->msi_cap + PCI_MSI_FLAGS,
6787 &ctrl);
6788 pci_write_config_word(tp->pdev,
6789 tp->msi_cap + PCI_MSI_FLAGS,
6790 ctrl | PCI_MSI_FLAGS_ENABLE);
6791 val = tr32(MSGINT_MODE);
6792 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
6793 }
6794 }
6795}
6796
Linus Torvalds1da177e2005-04-16 15:20:36 -07006797static void tg3_stop_fw(struct tg3 *);
6798
6799/* tp->lock is held. */
6800static int tg3_chip_reset(struct tg3 *tp)
6801{
6802 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07006803 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00006804 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006805
David S. Millerf49639e2006-06-09 11:58:36 -07006806 tg3_nvram_lock(tp);
6807
Matt Carlson77b483f2008-08-15 14:07:24 -07006808 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
6809
David S. Millerf49639e2006-06-09 11:58:36 -07006810 /* No matching tg3_nvram_unlock() after this because
6811 * chip reset below will undo the nvram lock.
6812 */
6813 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006814
Michael Chanee6a99b2007-07-18 21:49:10 -07006815 /* GRC_MISC_CFG core clock reset will clear the memory
6816 * enable bit in PCI register 4 and the MSI enable bit
6817 * on some chips, so we save relevant registers here.
6818 */
6819 tg3_save_pci_state(tp);
6820
Michael Chand9ab5ad2006-03-20 22:27:35 -08006821 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08006822 (tp->tg3_flags3 & TG3_FLG3_5755_PLUS))
Michael Chand9ab5ad2006-03-20 22:27:35 -08006823 tw32(GRC_FASTBOOT_PC, 0);
6824
Linus Torvalds1da177e2005-04-16 15:20:36 -07006825 /*
6826 * We must avoid the readl() that normally takes place.
6827 * It locks machines, causes machine checks, and other
6828 * fun things. So, temporarily disable the 5701
6829 * hardware workaround, while we do the reset.
6830 */
Michael Chan1ee582d2005-08-09 20:16:46 -07006831 write_op = tp->write32;
6832 if (write_op == tg3_write_flush_reg32)
6833 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006834
Michael Chand18edcb2007-03-24 20:57:11 -07006835 /* Prevent the irq handler from reading or writing PCI registers
6836 * during chip reset when the memory enable bit in the PCI command
6837 * register may be cleared. The chip does not generate interrupt
6838 * at this time, but the irq handler may still be called due to irq
6839 * sharing or irqpoll.
6840 */
6841 tp->tg3_flags |= TG3_FLAG_CHIP_RESETTING;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006842 for (i = 0; i < tp->irq_cnt; i++) {
6843 struct tg3_napi *tnapi = &tp->napi[i];
6844 if (tnapi->hw_status) {
6845 tnapi->hw_status->status = 0;
6846 tnapi->hw_status->status_tag = 0;
6847 }
6848 tnapi->last_tag = 0;
6849 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07006850 }
Michael Chand18edcb2007-03-24 20:57:11 -07006851 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00006852
6853 for (i = 0; i < tp->irq_cnt; i++)
6854 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07006855
Matt Carlson255ca312009-08-25 10:07:27 +00006856 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
6857 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
6858 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
6859 }
6860
Linus Torvalds1da177e2005-04-16 15:20:36 -07006861 /* do the reset */
6862 val = GRC_MISC_CFG_CORECLK_RESET;
6863
6864 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
6865 if (tr32(0x7e2c) == 0x60) {
6866 tw32(0x7e2c, 0x20);
6867 }
6868 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
6869 tw32(GRC_MISC_CFG, (1 << 29));
6870 val |= (1 << 29);
6871 }
6872 }
6873
Michael Chanb5d37722006-09-27 16:06:21 -07006874 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
6875 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
6876 tw32(GRC_VCPU_EXT_CTRL,
6877 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
6878 }
6879
Linus Torvalds1da177e2005-04-16 15:20:36 -07006880 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
6881 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
6882 tw32(GRC_MISC_CFG, val);
6883
Michael Chan1ee582d2005-08-09 20:16:46 -07006884 /* restore 5701 hardware bug workaround write method */
6885 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006886
6887 /* Unfortunately, we have to delay before the PCI read back.
6888 * Some 575X chips even will not respond to a PCI cfg access
6889 * when the reset command is given to the chip.
6890 *
6891 * How do these hardware designers expect things to work
6892 * properly if the PCI write is posted for a long period
6893 * of time? It is always necessary to have some method by
6894 * which a register read back can occur to push the write
6895 * out which does the reset.
6896 *
6897 * For most tg3 variants the trick below was working.
6898 * Ho hum...
6899 */
6900 udelay(120);
6901
6902 /* Flush PCI posted writes. The normal MMIO registers
6903 * are inaccessible at this time so this is the only
6904 * way to make this reliably (actually, this is no longer
6905 * the case, see above). I tried to use indirect
6906 * register read/write but this upset some 5701 variants.
6907 */
6908 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
6909
6910 udelay(120);
6911
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006912 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) && tp->pcie_cap) {
Matt Carlsone7126992009-08-25 10:08:16 +00006913 u16 val16;
6914
Linus Torvalds1da177e2005-04-16 15:20:36 -07006915 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
6916 int i;
6917 u32 cfg_val;
6918
6919 /* Wait for link training to complete. */
6920 for (i = 0; i < 5000; i++)
6921 udelay(100);
6922
6923 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
6924 pci_write_config_dword(tp->pdev, 0xc4,
6925 cfg_val | (1 << 15));
6926 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006927
Matt Carlsone7126992009-08-25 10:08:16 +00006928 /* Clear the "no snoop" and "relaxed ordering" bits. */
6929 pci_read_config_word(tp->pdev,
6930 tp->pcie_cap + PCI_EXP_DEVCTL,
6931 &val16);
6932 val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
6933 PCI_EXP_DEVCTL_NOSNOOP_EN);
6934 /*
6935 * Older PCIe devices only support the 128 byte
6936 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006937 */
Matt Carlsone7126992009-08-25 10:08:16 +00006938 if (!(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
6939 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784))
6940 val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006941 pci_write_config_word(tp->pdev,
6942 tp->pcie_cap + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00006943 val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006944
6945 pcie_set_readrq(tp->pdev, 4096);
6946
6947 /* Clear error status */
6948 pci_write_config_word(tp->pdev,
6949 tp->pcie_cap + PCI_EXP_DEVSTA,
6950 PCI_EXP_DEVSTA_CED |
6951 PCI_EXP_DEVSTA_NFED |
6952 PCI_EXP_DEVSTA_FED |
6953 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006954 }
6955
Michael Chanee6a99b2007-07-18 21:49:10 -07006956 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006957
Michael Chand18edcb2007-03-24 20:57:11 -07006958 tp->tg3_flags &= ~TG3_FLAG_CHIP_RESETTING;
6959
Michael Chanee6a99b2007-07-18 21:49:10 -07006960 val = 0;
6961 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
Michael Chan4cf78e42005-07-25 12:29:19 -07006962 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07006963 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006964
6965 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
6966 tg3_stop_fw(tp);
6967 tw32(0x5000, 0x400);
6968 }
6969
6970 tw32(GRC_MODE, tp->grc_mode);
6971
6972 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01006973 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006974
6975 tw32(0xc4, val | (1 << 15));
6976 }
6977
6978 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
6979 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
6980 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
6981 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
6982 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
6983 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
6984 }
6985
6986 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
6987 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
6988 tw32_f(MAC_MODE, tp->mac_mode);
Michael Chan747e8f82005-07-25 12:33:22 -07006989 } else if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES) {
6990 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
6991 tw32_f(MAC_MODE, tp->mac_mode);
Matt Carlson3bda1252008-08-15 14:08:22 -07006992 } else if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
6993 tp->mac_mode &= (MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN);
6994 if (tp->mac_mode & MAC_MODE_APE_TX_EN)
6995 tp->mac_mode |= MAC_MODE_TDE_ENABLE;
6996 tw32_f(MAC_MODE, tp->mac_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006997 } else
6998 tw32_f(MAC_MODE, 0);
6999 udelay(40);
7000
Matt Carlson77b483f2008-08-15 14:07:24 -07007001 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
7002
Michael Chan7a6f4362006-09-27 16:03:31 -07007003 err = tg3_poll_fw(tp);
7004 if (err)
7005 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007006
Matt Carlson0a9140c2009-08-28 12:27:50 +00007007 tg3_mdio_start(tp);
7008
Matt Carlson52cdf852009-11-02 14:25:06 +00007009 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7010 u8 phy_addr;
7011
7012 phy_addr = tp->phy_addr;
7013 tp->phy_addr = TG3_PHY_PCIE_ADDR;
7014
7015 tg3_writephy(tp, TG3_PCIEPHY_BLOCK_ADDR,
7016 TG3_PCIEPHY_TXB_BLK << TG3_PCIEPHY_BLOCK_SHIFT);
7017 val = TG3_PCIEPHY_TX0CTRL1_TXOCM | TG3_PCIEPHY_TX0CTRL1_RDCTL |
7018 TG3_PCIEPHY_TX0CTRL1_TXCMV | TG3_PCIEPHY_TX0CTRL1_TKSEL |
7019 TG3_PCIEPHY_TX0CTRL1_NB_EN;
7020 tg3_writephy(tp, TG3_PCIEPHY_TX0CTRL1, val);
7021 udelay(10);
7022
7023 tg3_writephy(tp, TG3_PCIEPHY_BLOCK_ADDR,
7024 TG3_PCIEPHY_XGXS_BLK1 << TG3_PCIEPHY_BLOCK_SHIFT);
7025 val = TG3_PCIEPHY_PWRMGMT4_LOWPWR_EN |
7026 TG3_PCIEPHY_PWRMGMT4_L1PLLPD_EN;
7027 tg3_writephy(tp, TG3_PCIEPHY_PWRMGMT4, val);
7028 udelay(10);
7029
7030 tp->phy_addr = phy_addr;
7031 }
7032
Linus Torvalds1da177e2005-04-16 15:20:36 -07007033 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007034 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
7035 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonb703df62009-12-03 08:36:21 +00007036 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
7037 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57765) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007038 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007039
7040 tw32(0x7c00, val | (1 << 25));
7041 }
7042
7043 /* Reprobe ASF enable state. */
7044 tp->tg3_flags &= ~TG3_FLAG_ENABLE_ASF;
7045 tp->tg3_flags2 &= ~TG3_FLG2_ASF_NEW_HANDSHAKE;
7046 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
7047 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
7048 u32 nic_cfg;
7049
7050 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
7051 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
7052 tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
Matt Carlson4ba526c2008-08-15 14:10:04 -07007053 tp->last_event_jiffies = jiffies;
John W. Linvillecbf46852005-04-21 17:01:29 -07007054 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007055 tp->tg3_flags2 |= TG3_FLG2_ASF_NEW_HANDSHAKE;
7056 }
7057 }
7058
7059 return 0;
7060}
7061
7062/* tp->lock is held. */
7063static void tg3_stop_fw(struct tg3 *tp)
7064{
Matt Carlson0d3031d2007-10-10 18:02:43 -07007065 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
7066 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07007067 /* Wait for RX cpu to ACK the previous event. */
7068 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007069
7070 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007071
7072 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007073
Matt Carlson7c5026a2008-05-02 16:49:29 -07007074 /* Wait for RX cpu to ACK this event. */
7075 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007076 }
7077}
7078
7079/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07007080static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007081{
7082 int err;
7083
7084 tg3_stop_fw(tp);
7085
Michael Chan944d9802005-05-29 14:57:48 -07007086 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007087
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007088 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007089 err = tg3_chip_reset(tp);
7090
Matt Carlsondaba2a62009-04-20 06:58:52 +00007091 __tg3_set_mac_addr(tp, 0);
7092
Michael Chan944d9802005-05-29 14:57:48 -07007093 tg3_write_sig_legacy(tp, kind);
7094 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007095
7096 if (err)
7097 return err;
7098
7099 return 0;
7100}
7101
Linus Torvalds1da177e2005-04-16 15:20:36 -07007102#define RX_CPU_SCRATCH_BASE 0x30000
7103#define RX_CPU_SCRATCH_SIZE 0x04000
7104#define TX_CPU_SCRATCH_BASE 0x34000
7105#define TX_CPU_SCRATCH_SIZE 0x04000
7106
7107/* tp->lock is held. */
7108static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
7109{
7110 int i;
7111
Eric Sesterhenn5d9428d2006-04-02 13:52:48 +02007112 BUG_ON(offset == TX_CPU_BASE &&
7113 (tp->tg3_flags2 & TG3_FLG2_5705_PLUS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007114
Michael Chanb5d37722006-09-27 16:06:21 -07007115 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7116 u32 val = tr32(GRC_VCPU_EXT_CTRL);
7117
7118 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
7119 return 0;
7120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007121 if (offset == RX_CPU_BASE) {
7122 for (i = 0; i < 10000; i++) {
7123 tw32(offset + CPU_STATE, 0xffffffff);
7124 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7125 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7126 break;
7127 }
7128
7129 tw32(offset + CPU_STATE, 0xffffffff);
7130 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
7131 udelay(10);
7132 } else {
7133 for (i = 0; i < 10000; i++) {
7134 tw32(offset + CPU_STATE, 0xffffffff);
7135 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7136 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7137 break;
7138 }
7139 }
7140
7141 if (i >= 10000) {
Joe Perches05dbe002010-02-17 19:44:19 +00007142 netdev_err(tp->dev, "%s timed out, %s CPU\n",
7143 __func__, offset == RX_CPU_BASE ? "RX" : "TX");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007144 return -ENODEV;
7145 }
Michael Chanec41c7d2006-01-17 02:40:55 -08007146
7147 /* Clear firmware's nvram arbitration. */
7148 if (tp->tg3_flags & TG3_FLAG_NVRAM)
7149 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007150 return 0;
7151}
7152
7153struct fw_info {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007154 unsigned int fw_base;
7155 unsigned int fw_len;
7156 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007157};
7158
7159/* tp->lock is held. */
7160static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base, u32 cpu_scratch_base,
7161 int cpu_scratch_size, struct fw_info *info)
7162{
Michael Chanec41c7d2006-01-17 02:40:55 -08007163 int err, lock_err, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007164 void (*write_op)(struct tg3 *, u32, u32);
7165
7166 if (cpu_base == TX_CPU_BASE &&
7167 (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007168 netdev_err(tp->dev,
7169 "%s: Trying to load TX cpu firmware which is 5705\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007170 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007171 return -EINVAL;
7172 }
7173
7174 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
7175 write_op = tg3_write_mem;
7176 else
7177 write_op = tg3_write_indirect_reg32;
7178
Michael Chan1b628152005-05-29 14:59:49 -07007179 /* It is possible that bootcode is still loading at this point.
7180 * Get the nvram lock first before halting the cpu.
7181 */
Michael Chanec41c7d2006-01-17 02:40:55 -08007182 lock_err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007183 err = tg3_halt_cpu(tp, cpu_base);
Michael Chanec41c7d2006-01-17 02:40:55 -08007184 if (!lock_err)
7185 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007186 if (err)
7187 goto out;
7188
7189 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
7190 write_op(tp, cpu_scratch_base + i, 0);
7191 tw32(cpu_base + CPU_STATE, 0xffffffff);
7192 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007193 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007194 write_op(tp, (cpu_scratch_base +
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007195 (info->fw_base & 0xffff) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07007196 (i * sizeof(u32))),
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007197 be32_to_cpu(info->fw_data[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007198
7199 err = 0;
7200
7201out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007202 return err;
7203}
7204
7205/* tp->lock is held. */
7206static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
7207{
7208 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007209 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007210 int err, i;
7211
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007212 fw_data = (void *)tp->fw->data;
7213
7214 /* Firmware blob starts with version numbers, followed by
7215 start address and length. We are setting complete length.
7216 length = end_address_of_bss - start_address_of_text.
7217 Remainder is the blob to be loaded contiguously
7218 from start address. */
7219
7220 info.fw_base = be32_to_cpu(fw_data[1]);
7221 info.fw_len = tp->fw->size - 12;
7222 info.fw_data = &fw_data[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007223
7224 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
7225 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
7226 &info);
7227 if (err)
7228 return err;
7229
7230 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
7231 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
7232 &info);
7233 if (err)
7234 return err;
7235
7236 /* Now startup only the RX cpu. */
7237 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007238 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007239
7240 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007241 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007242 break;
7243 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7244 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007245 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007246 udelay(1000);
7247 }
7248 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007249 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
7250 "should be %08x\n", __func__,
Joe Perches05dbe002010-02-17 19:44:19 +00007251 tr32(RX_CPU_BASE + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007252 return -ENODEV;
7253 }
7254 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7255 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
7256
7257 return 0;
7258}
7259
Linus Torvalds1da177e2005-04-16 15:20:36 -07007260/* 5705 needs a special version of the TSO firmware. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007261
7262/* tp->lock is held. */
7263static int tg3_load_tso_firmware(struct tg3 *tp)
7264{
7265 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007266 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007267 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
7268 int err, i;
7269
7270 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
7271 return 0;
7272
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007273 fw_data = (void *)tp->fw->data;
7274
7275 /* Firmware blob starts with version numbers, followed by
7276 start address and length. We are setting complete length.
7277 length = end_address_of_bss - start_address_of_text.
7278 Remainder is the blob to be loaded contiguously
7279 from start address. */
7280
7281 info.fw_base = be32_to_cpu(fw_data[1]);
7282 cpu_scratch_size = tp->fw_len;
7283 info.fw_len = tp->fw->size - 12;
7284 info.fw_data = &fw_data[3];
7285
Linus Torvalds1da177e2005-04-16 15:20:36 -07007286 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007287 cpu_base = RX_CPU_BASE;
7288 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007289 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007290 cpu_base = TX_CPU_BASE;
7291 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
7292 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
7293 }
7294
7295 err = tg3_load_firmware_cpu(tp, cpu_base,
7296 cpu_scratch_base, cpu_scratch_size,
7297 &info);
7298 if (err)
7299 return err;
7300
7301 /* Now startup the cpu. */
7302 tw32(cpu_base + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007303 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007304
7305 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007306 if (tr32(cpu_base + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007307 break;
7308 tw32(cpu_base + CPU_STATE, 0xffffffff);
7309 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007310 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007311 udelay(1000);
7312 }
7313 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007314 netdev_err(tp->dev,
7315 "%s fails to set CPU PC, is %08x should be %08x\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007316 __func__, tr32(cpu_base + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007317 return -ENODEV;
7318 }
7319 tw32(cpu_base + CPU_STATE, 0xffffffff);
7320 tw32_f(cpu_base + CPU_MODE, 0x00000000);
7321 return 0;
7322}
7323
Linus Torvalds1da177e2005-04-16 15:20:36 -07007324
Linus Torvalds1da177e2005-04-16 15:20:36 -07007325static int tg3_set_mac_addr(struct net_device *dev, void *p)
7326{
7327 struct tg3 *tp = netdev_priv(dev);
7328 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07007329 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007330
Michael Chanf9804dd2005-09-27 12:13:10 -07007331 if (!is_valid_ether_addr(addr->sa_data))
7332 return -EINVAL;
7333
Linus Torvalds1da177e2005-04-16 15:20:36 -07007334 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7335
Michael Chane75f7c92006-03-20 21:33:26 -08007336 if (!netif_running(dev))
7337 return 0;
7338
Michael Chan58712ef2006-04-29 18:58:01 -07007339 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) {
Michael Chan986e0ae2007-05-05 12:10:20 -07007340 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07007341
Michael Chan986e0ae2007-05-05 12:10:20 -07007342 addr0_high = tr32(MAC_ADDR_0_HIGH);
7343 addr0_low = tr32(MAC_ADDR_0_LOW);
7344 addr1_high = tr32(MAC_ADDR_1_HIGH);
7345 addr1_low = tr32(MAC_ADDR_1_LOW);
7346
7347 /* Skip MAC addr 1 if ASF is using it. */
7348 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
7349 !(addr1_high == 0 && addr1_low == 0))
7350 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07007351 }
Michael Chan986e0ae2007-05-05 12:10:20 -07007352 spin_lock_bh(&tp->lock);
7353 __tg3_set_mac_addr(tp, skip_mac_1);
7354 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007355
Michael Chanb9ec6c12006-07-25 16:37:27 -07007356 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007357}
7358
7359/* tp->lock is held. */
7360static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
7361 dma_addr_t mapping, u32 maxlen_flags,
7362 u32 nic_addr)
7363{
7364 tg3_write_mem(tp,
7365 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
7366 ((u64) mapping >> 32));
7367 tg3_write_mem(tp,
7368 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
7369 ((u64) mapping & 0xffffffff));
7370 tg3_write_mem(tp,
7371 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
7372 maxlen_flags);
7373
7374 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7375 tg3_write_mem(tp,
7376 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
7377 nic_addr);
7378}
7379
7380static void __tg3_set_rx_mode(struct net_device *);
Michael Chand244c892005-07-05 14:42:33 -07007381static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07007382{
Matt Carlsonb6080e12009-09-01 13:12:00 +00007383 int i;
7384
Matt Carlson19cfaec2009-12-03 08:36:20 +00007385 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007386 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
7387 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
7388 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007389 } else {
7390 tw32(HOSTCC_TXCOL_TICKS, 0);
7391 tw32(HOSTCC_TXMAX_FRAMES, 0);
7392 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007393 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007394
Matt Carlson19cfaec2009-12-03 08:36:20 +00007395 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSIX)) {
7396 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
7397 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
7398 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
7399 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007400 tw32(HOSTCC_RXCOL_TICKS, 0);
7401 tw32(HOSTCC_RXMAX_FRAMES, 0);
7402 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07007403 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007404
David S. Miller15f98502005-05-18 22:49:26 -07007405 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7406 u32 val = ec->stats_block_coalesce_usecs;
7407
Matt Carlsonb6080e12009-09-01 13:12:00 +00007408 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
7409 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
7410
David S. Miller15f98502005-05-18 22:49:26 -07007411 if (!netif_carrier_ok(tp->dev))
7412 val = 0;
7413
7414 tw32(HOSTCC_STAT_COAL_TICKS, val);
7415 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007416
7417 for (i = 0; i < tp->irq_cnt - 1; i++) {
7418 u32 reg;
7419
7420 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
7421 tw32(reg, ec->rx_coalesce_usecs);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007422 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
7423 tw32(reg, ec->rx_max_coalesced_frames);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007424 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
7425 tw32(reg, ec->rx_max_coalesced_frames_irq);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007426
7427 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS) {
7428 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
7429 tw32(reg, ec->tx_coalesce_usecs);
7430 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
7431 tw32(reg, ec->tx_max_coalesced_frames);
7432 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
7433 tw32(reg, ec->tx_max_coalesced_frames_irq);
7434 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007435 }
7436
7437 for (; i < tp->irq_max - 1; i++) {
7438 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007439 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007440 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007441
7442 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS) {
7443 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
7444 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
7445 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
7446 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007447 }
David S. Miller15f98502005-05-18 22:49:26 -07007448}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007449
7450/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00007451static void tg3_rings_reset(struct tg3 *tp)
7452{
7453 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007454 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007455 struct tg3_napi *tnapi = &tp->napi[0];
7456
7457 /* Disable all transmit rings but the first. */
7458 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7459 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00007460 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
7461 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007462 else
7463 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7464
7465 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7466 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
7467 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
7468 BDINFO_FLAGS_DISABLED);
7469
7470
7471 /* Disable all receive return rings but the first. */
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007472 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
7473 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
7474 else if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00007475 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00007476 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
7477 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson2d31eca2009-09-01 12:53:31 +00007478 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
7479 else
7480 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7481
7482 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7483 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
7484 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
7485 BDINFO_FLAGS_DISABLED);
7486
7487 /* Disable interrupts */
7488 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
7489
7490 /* Zero mailbox registers. */
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007491 if (tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX) {
7492 for (i = 1; i < TG3_IRQ_MAX_VECS; i++) {
7493 tp->napi[i].tx_prod = 0;
7494 tp->napi[i].tx_cons = 0;
Matt Carlsonc2353a32010-01-20 16:58:08 +00007495 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
7496 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007497 tw32_rx_mbox(tp->napi[i].consmbox, 0);
7498 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
7499 }
Matt Carlsonc2353a32010-01-20 16:58:08 +00007500 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS))
7501 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007502 } else {
7503 tp->napi[0].tx_prod = 0;
7504 tp->napi[0].tx_cons = 0;
7505 tw32_mailbox(tp->napi[0].prodmbox, 0);
7506 tw32_rx_mbox(tp->napi[0].consmbox, 0);
7507 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007508
7509 /* Make sure the NIC-based send BD rings are disabled. */
7510 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7511 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
7512 for (i = 0; i < 16; i++)
7513 tw32_tx_mbox(mbox + i * 8, 0);
7514 }
7515
7516 txrcb = NIC_SRAM_SEND_RCB;
7517 rxrcb = NIC_SRAM_RCV_RET_RCB;
7518
7519 /* Clear status block in ram. */
7520 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7521
7522 /* Set status block DMA address */
7523 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
7524 ((u64) tnapi->status_mapping >> 32));
7525 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
7526 ((u64) tnapi->status_mapping & 0xffffffff));
7527
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007528 if (tnapi->tx_ring) {
7529 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7530 (TG3_TX_RING_SIZE <<
7531 BDINFO_FLAGS_MAXLEN_SHIFT),
7532 NIC_SRAM_TX_BUFFER_DESC);
7533 txrcb += TG3_BDINFO_SIZE;
7534 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007535
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007536 if (tnapi->rx_rcb) {
7537 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
7538 (TG3_RX_RCB_RING_SIZE(tp) <<
7539 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
7540 rxrcb += TG3_BDINFO_SIZE;
7541 }
7542
7543 stblk = HOSTCC_STATBLCK_RING1;
7544
7545 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
7546 u64 mapping = (u64)tnapi->status_mapping;
7547 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
7548 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
7549
7550 /* Clear status block in ram. */
7551 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7552
Matt Carlson19cfaec2009-12-03 08:36:20 +00007553 if (tnapi->tx_ring) {
7554 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7555 (TG3_TX_RING_SIZE <<
7556 BDINFO_FLAGS_MAXLEN_SHIFT),
7557 NIC_SRAM_TX_BUFFER_DESC);
7558 txrcb += TG3_BDINFO_SIZE;
7559 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007560
7561 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
7562 (TG3_RX_RCB_RING_SIZE(tp) <<
7563 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
7564
7565 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007566 rxrcb += TG3_BDINFO_SIZE;
7567 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007568}
7569
7570/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07007571static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007572{
7573 u32 val, rdmac_mode;
7574 int i, err, limit;
Matt Carlson21f581a2009-08-28 14:00:25 +00007575 struct tg3_rx_prodring_set *tpr = &tp->prodring[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007576
7577 tg3_disable_ints(tp);
7578
7579 tg3_stop_fw(tp);
7580
7581 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
7582
Matt Carlson859a588792010-04-05 10:19:28 +00007583 if (tp->tg3_flags & TG3_FLAG_INIT_COMPLETE)
Michael Chane6de8ad2005-05-05 14:42:41 -07007584 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007585
Matt Carlson603f1172010-02-12 14:47:10 +00007586 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08007587 tg3_phy_reset(tp);
7588
Linus Torvalds1da177e2005-04-16 15:20:36 -07007589 err = tg3_chip_reset(tp);
7590 if (err)
7591 return err;
7592
7593 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
7594
Matt Carlsonbcb37f62008-11-03 16:52:09 -08007595 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07007596 val = tr32(TG3_CPMU_CTRL);
7597 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
7598 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08007599
7600 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
7601 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
7602 val |= CPMU_LSPD_10MB_MACCLK_6_25;
7603 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
7604
7605 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
7606 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
7607 val |= CPMU_LNK_AWARE_MACCLK_6_25;
7608 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
7609
7610 val = tr32(TG3_CPMU_HST_ACC);
7611 val &= ~CPMU_HST_ACC_MACCLK_MASK;
7612 val |= CPMU_HST_ACC_MACCLK_6_25;
7613 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07007614 }
7615
Matt Carlson33466d92009-04-20 06:57:41 +00007616 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7617 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
7618 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
7619 PCIE_PWR_MGMT_L1_THRESH_4MS;
7620 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00007621
7622 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
7623 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
7624
7625 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00007626
Matt Carlsonf40386c2009-11-02 14:24:02 +00007627 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7628 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00007629 }
7630
Matt Carlson614b0592010-01-20 16:58:02 +00007631 if (tp->tg3_flags3 & TG3_FLG3_L1PLLPD_EN) {
7632 u32 grc_mode = tr32(GRC_MODE);
7633
7634 /* Access the lower 1K of PL PCIE block registers. */
7635 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
7636 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
7637
7638 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
7639 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
7640 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
7641
7642 tw32(GRC_MODE, grc_mode);
7643 }
7644
Matt Carlsoncea46462010-04-12 06:58:24 +00007645 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
7646 u32 grc_mode = tr32(GRC_MODE);
7647
7648 /* Access the lower 1K of PL PCIE block registers. */
7649 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
7650 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
7651
7652 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5);
7653 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
7654 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
7655
7656 tw32(GRC_MODE, grc_mode);
Matt Carlsona977dbe2010-04-12 06:58:26 +00007657
7658 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
7659 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
7660 val |= CPMU_LSPD_10MB_MACCLK_6_25;
7661 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00007662 }
7663
Linus Torvalds1da177e2005-04-16 15:20:36 -07007664 /* This works around an issue with Athlon chipsets on
7665 * B3 tigon3 silicon. This bit has no effect on any
7666 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07007667 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007668 */
Matt Carlson795d01c2007-10-07 23:28:17 -07007669 if (!(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT)) {
7670 if (!(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
7671 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
7672 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007674
7675 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
7676 (tp->tg3_flags & TG3_FLAG_PCIX_MODE)) {
7677 val = tr32(TG3PCI_PCISTATE);
7678 val |= PCISTATE_RETRY_SAME_DMA;
7679 tw32(TG3PCI_PCISTATE, val);
7680 }
7681
Matt Carlson0d3031d2007-10-10 18:02:43 -07007682 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
7683 /* Allow reads and writes to the
7684 * APE register and memory space.
7685 */
7686 val = tr32(TG3PCI_PCISTATE);
7687 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
7688 PCISTATE_ALLOW_APE_SHMEM_WR;
7689 tw32(TG3PCI_PCISTATE, val);
7690 }
7691
Linus Torvalds1da177e2005-04-16 15:20:36 -07007692 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
7693 /* Enable some hw fixes. */
7694 val = tr32(TG3PCI_MSI_DATA);
7695 val |= (1 << 26) | (1 << 28) | (1 << 29);
7696 tw32(TG3PCI_MSI_DATA, val);
7697 }
7698
7699 /* Descriptor ring init may make accesses to the
7700 * NIC SRAM area to setup the TX descriptors, so we
7701 * can only do this after the hardware has been
7702 * successfully reset.
7703 */
Michael Chan32d8c572006-07-25 16:38:29 -07007704 err = tg3_init_rings(tp);
7705 if (err)
7706 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007707
Matt Carlsonb703df62009-12-03 08:36:21 +00007708 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
7709 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00007710 val = tr32(TG3PCI_DMA_RW_CTRL) &
7711 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Matt Carlson1a319022010-04-12 06:58:25 +00007712 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
7713 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00007714 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
7715 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
7716 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07007717 /* This value is determined during the probe time DMA
7718 * engine test, tg3_test_dma.
7719 */
7720 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
7721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007722
7723 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
7724 GRC_MODE_4X_NIC_SEND_RINGS |
7725 GRC_MODE_NO_TX_PHDR_CSUM |
7726 GRC_MODE_NO_RX_PHDR_CSUM);
7727 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07007728
7729 /* Pseudo-header checksum is done by hardware logic and not
7730 * the offload processers, so make the chip do the pseudo-
7731 * header checksums on receive. For transmit it is more
7732 * convenient to do the pseudo-header checksum in software
7733 * as Linux does that on transmit for us in all cases.
7734 */
7735 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007736
7737 tw32(GRC_MODE,
7738 tp->grc_mode |
7739 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
7740
7741 /* Setup the timer prescalar register. Clock is always 66Mhz. */
7742 val = tr32(GRC_MISC_CFG);
7743 val &= ~0xff;
7744 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
7745 tw32(GRC_MISC_CFG, val);
7746
7747 /* Initialize MBUF/DESC pool. */
John W. Linvillecbf46852005-04-21 17:01:29 -07007748 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007749 /* Do nothing. */
7750 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
7751 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
7752 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
7753 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
7754 else
7755 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
7756 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
7757 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Matt Carlson859a588792010-04-05 10:19:28 +00007758 } else if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007759 int fw_len;
7760
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007761 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007762 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
7763 tw32(BUFMGR_MB_POOL_ADDR,
7764 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
7765 tw32(BUFMGR_MB_POOL_SIZE,
7766 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
7767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007768
Michael Chan0f893dc2005-07-25 12:30:38 -07007769 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007770 tw32(BUFMGR_MB_RDMA_LOW_WATER,
7771 tp->bufmgr_config.mbuf_read_dma_low_water);
7772 tw32(BUFMGR_MB_MACRX_LOW_WATER,
7773 tp->bufmgr_config.mbuf_mac_rx_low_water);
7774 tw32(BUFMGR_MB_HIGH_WATER,
7775 tp->bufmgr_config.mbuf_high_water);
7776 } else {
7777 tw32(BUFMGR_MB_RDMA_LOW_WATER,
7778 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
7779 tw32(BUFMGR_MB_MACRX_LOW_WATER,
7780 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
7781 tw32(BUFMGR_MB_HIGH_WATER,
7782 tp->bufmgr_config.mbuf_high_water_jumbo);
7783 }
7784 tw32(BUFMGR_DMA_LOW_WATER,
7785 tp->bufmgr_config.dma_low_water);
7786 tw32(BUFMGR_DMA_HIGH_WATER,
7787 tp->bufmgr_config.dma_high_water);
7788
7789 tw32(BUFMGR_MODE, BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE);
7790 for (i = 0; i < 2000; i++) {
7791 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
7792 break;
7793 udelay(10);
7794 }
7795 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00007796 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007797 return -ENODEV;
7798 }
7799
7800 /* Setup replenish threshold. */
Michael Chanf92905d2006-06-29 20:14:29 -07007801 val = tp->rx_pending / 8;
7802 if (val == 0)
7803 val = 1;
7804 else if (val > tp->rx_std_max_post)
7805 val = tp->rx_std_max_post;
Michael Chanb5d37722006-09-27 16:06:21 -07007806 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7807 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
7808 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
7809
7810 if (val > (TG3_RX_INTERNAL_RING_SZ_5906 / 2))
7811 val = TG3_RX_INTERNAL_RING_SZ_5906 / 2;
7812 }
Michael Chanf92905d2006-06-29 20:14:29 -07007813
7814 tw32(RCVBDI_STD_THRESH, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007815
7816 /* Initialize TG3_BDINFO's at:
7817 * RCVDBDI_STD_BD: standard eth size rx ring
7818 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
7819 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
7820 *
7821 * like so:
7822 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
7823 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
7824 * ring attribute flags
7825 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
7826 *
7827 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
7828 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
7829 *
7830 * The size of each ring is fixed in the firmware, but the location is
7831 * configurable.
7832 */
7833 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00007834 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007835 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00007836 ((u64) tpr->rx_std_mapping & 0xffffffff));
Matt Carlson13fa95b02010-01-12 10:11:36 +00007837 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
Matt Carlson87668d32009-11-13 13:03:34 +00007838 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
7839 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007840
Matt Carlsonfdb72b32009-08-28 13:57:12 +00007841 /* Disable the mini ring */
7842 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007843 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
7844 BDINFO_FLAGS_DISABLED);
7845
Matt Carlsonfdb72b32009-08-28 13:57:12 +00007846 /* Program the jumbo buffer descriptor ring control
7847 * blocks on those devices that have them.
7848 */
Matt Carlson8f666b02009-08-28 13:58:24 +00007849 if ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) &&
Matt Carlsonfdb72b32009-08-28 13:57:12 +00007850 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007851 /* Setup replenish threshold. */
7852 tw32(RCVBDI_JUMBO_THRESH, tp->rx_jumbo_pending / 8);
7853
Michael Chan0f893dc2005-07-25 12:30:38 -07007854 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007855 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00007856 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007857 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00007858 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007859 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlson79ed5ac2009-08-28 14:00:55 +00007860 (RX_JUMBO_MAX_SIZE << BDINFO_FLAGS_MAXLEN_SHIFT) |
7861 BDINFO_FLAGS_USE_EXT_RECV);
Matt Carlson5fd68fb2010-01-20 16:58:07 +00007862 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
Matt Carlson87668d32009-11-13 13:03:34 +00007863 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
7864 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007865 } else {
7866 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
7867 BDINFO_FLAGS_DISABLED);
7868 }
7869
Matt Carlsonb703df62009-12-03 08:36:21 +00007870 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
7871 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007872 val = (RX_STD_MAX_SIZE_5705 << BDINFO_FLAGS_MAXLEN_SHIFT) |
7873 (RX_STD_MAX_SIZE << 2);
7874 else
7875 val = RX_STD_MAX_SIZE << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00007876 } else
7877 val = RX_STD_MAX_SIZE_5705 << BDINFO_FLAGS_MAXLEN_SHIFT;
7878
7879 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007880
Matt Carlson411da642009-11-13 13:03:46 +00007881 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e62009-11-13 13:03:49 +00007882 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007883
Matt Carlson411da642009-11-13 13:03:46 +00007884 tpr->rx_jmb_prod_idx = (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) ?
Matt Carlson21f581a2009-08-28 14:00:25 +00007885 tp->rx_jumbo_pending : 0;
Matt Carlson66711e62009-11-13 13:03:49 +00007886 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007887
Matt Carlsonb703df62009-12-03 08:36:21 +00007888 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
7889 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007890 tw32(STD_REPLENISH_LWM, 32);
7891 tw32(JMB_REPLENISH_LWM, 16);
7892 }
7893
Matt Carlson2d31eca2009-09-01 12:53:31 +00007894 tg3_rings_reset(tp);
7895
Linus Torvalds1da177e2005-04-16 15:20:36 -07007896 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07007897 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007898
7899 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00007900 tw32(MAC_RX_MTU_SIZE,
7901 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007902
7903 /* The slot time is changed by tg3_setup_phy if we
7904 * run at gigabit with half duplex.
7905 */
7906 tw32(MAC_TX_LENGTHS,
7907 (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
7908 (6 << TX_LENGTHS_IPG_SHIFT) |
7909 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
7910
7911 /* Receive rules. */
7912 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
7913 tw32(RCVLPC_CONFIG, 0x0181);
7914
7915 /* Calculate RDMAC_MODE setting early, we need it to determine
7916 * the RCVLPC_STATE_ENABLE mask.
7917 */
7918 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
7919 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
7920 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
7921 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
7922 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07007923
Matt Carlson0339e4e2010-02-12 14:47:09 +00007924 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
7925 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
7926
Matt Carlson57e69832008-05-25 23:48:31 -07007927 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08007928 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
7929 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07007930 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
7931 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
7932 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
7933
Michael Chan85e94ce2005-04-21 17:05:28 -07007934 /* If statement applies to 5705 and 5750 PCI devices only */
7935 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
7936 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) ||
7937 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007938 if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07007939 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007940 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
7941 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
7942 !(tp->tg3_flags2 & TG3_FLG2_IS_5788)) {
7943 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
7944 }
7945 }
7946
Michael Chan85e94ce2005-04-21 17:05:28 -07007947 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)
7948 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
7949
Linus Torvalds1da177e2005-04-16 15:20:36 -07007950 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
Matt Carlson027455a2008-12-21 20:19:30 -08007951 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
7952
Matt Carlsone849cdc2009-11-13 13:03:38 +00007953 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
7954 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson027455a2008-12-21 20:19:30 -08007955 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
7956 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007957
7958 /* Receive/send statistics. */
Michael Chan16613942006-06-29 20:15:13 -07007959 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
7960 val = tr32(RCVLPC_STATS_ENABLE);
7961 val &= ~RCVLPC_STATSENAB_DACK_FIX;
7962 tw32(RCVLPC_STATS_ENABLE, val);
7963 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
7964 (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007965 val = tr32(RCVLPC_STATS_ENABLE);
7966 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
7967 tw32(RCVLPC_STATS_ENABLE, val);
7968 } else {
7969 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
7970 }
7971 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
7972 tw32(SNDDATAI_STATSENAB, 0xffffff);
7973 tw32(SNDDATAI_STATSCTRL,
7974 (SNDDATAI_SCTRL_ENABLE |
7975 SNDDATAI_SCTRL_FASTUPD));
7976
7977 /* Setup host coalescing engine. */
7978 tw32(HOSTCC_MODE, 0);
7979 for (i = 0; i < 2000; i++) {
7980 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
7981 break;
7982 udelay(10);
7983 }
7984
Michael Chand244c892005-07-05 14:42:33 -07007985 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007986
Linus Torvalds1da177e2005-04-16 15:20:36 -07007987 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7988 /* Status/statistics block address. See tg3_timer,
7989 * the tg3_periodic_fetch_stats call there, and
7990 * tg3_get_stats to see how this works for 5705/5750 chips.
7991 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007992 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
7993 ((u64) tp->stats_mapping >> 32));
7994 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
7995 ((u64) tp->stats_mapping & 0xffffffff));
7996 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00007997
Linus Torvalds1da177e2005-04-16 15:20:36 -07007998 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00007999
8000 /* Clear statistics and status block memory areas */
8001 for (i = NIC_SRAM_STATS_BLK;
8002 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
8003 i += sizeof(u32)) {
8004 tg3_write_mem(tp, i, 0);
8005 udelay(40);
8006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008007 }
8008
8009 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
8010
8011 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
8012 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
8013 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
8014 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
8015
Michael Chanc94e3942005-09-27 12:12:42 -07008016 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES) {
8017 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
8018 /* reset to prevent losing 1st rx packet intermittently */
8019 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8020 udelay(10);
8021 }
8022
Matt Carlson3bda1252008-08-15 14:08:22 -07008023 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
8024 tp->mac_mode &= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
8025 else
8026 tp->mac_mode = 0;
8027 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Linus Torvalds1da177e2005-04-16 15:20:36 -07008028 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE | MAC_MODE_FHDE_ENABLE;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07008029 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
8030 !(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
8031 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
8032 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008033 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
8034 udelay(40);
8035
Michael Chan314fba32005-04-21 17:07:04 -07008036 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Michael Chan9d26e212006-12-07 00:21:14 -08008037 * If TG3_FLG2_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07008038 * register to preserve the GPIO settings for LOMs. The GPIOs,
8039 * whether used as inputs or outputs, are set by boot code after
8040 * reset.
8041 */
Michael Chan9d26e212006-12-07 00:21:14 -08008042 if (!(tp->tg3_flags2 & TG3_FLG2_IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07008043 u32 gpio_mask;
8044
Michael Chan9d26e212006-12-07 00:21:14 -08008045 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
8046 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
8047 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07008048
8049 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
8050 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
8051 GRC_LCLCTRL_GPIO_OUTPUT3;
8052
Michael Chanaf36e6b2006-03-23 01:28:06 -08008053 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
8054 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
8055
Gary Zambranoaaf84462007-05-05 11:51:45 -07008056 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07008057 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
8058
8059 /* GPIO1 must be driven high for eeprom write protect */
Michael Chan9d26e212006-12-07 00:21:14 -08008060 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT)
8061 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
8062 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07008063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008064 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8065 udelay(100);
8066
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008067 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX) {
8068 val = tr32(MSGINT_MODE);
8069 val |= MSGINT_MODE_MULTIVEC_EN | MSGINT_MODE_ENABLE;
8070 tw32(MSGINT_MODE, val);
8071 }
8072
Linus Torvalds1da177e2005-04-16 15:20:36 -07008073 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
8074 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
8075 udelay(40);
8076 }
8077
8078 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
8079 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
8080 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
8081 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
8082 WDMAC_MODE_LNGREAD_ENAB);
8083
Michael Chan85e94ce2005-04-21 17:05:28 -07008084 /* If statement applies to 5705 and 5750 PCI devices only */
8085 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8086 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) ||
8087 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750) {
Matt Carlson29ea0952009-08-25 10:07:54 +00008088 if ((tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008089 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
8090 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
8091 /* nothing */
8092 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
8093 !(tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
8094 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)) {
8095 val |= WDMAC_MODE_RX_ACCEL;
8096 }
8097 }
8098
Michael Chand9ab5ad2006-03-20 22:27:35 -08008099 /* Enable host coalescing bug fix */
Matt Carlson321d32a2008-11-21 17:22:19 -08008100 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Matt Carlsonf51f3562008-05-25 23:45:08 -07008101 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad2006-03-20 22:27:35 -08008102
Matt Carlson788a0352009-11-02 14:26:03 +00008103 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
8104 val |= WDMAC_MODE_BURST_ALL_DATA;
8105
Linus Torvalds1da177e2005-04-16 15:20:36 -07008106 tw32_f(WDMAC_MODE, val);
8107 udelay(40);
8108
Matt Carlson9974a352007-10-07 23:27:28 -07008109 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
8110 u16 pcix_cmd;
8111
8112 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8113 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008114 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07008115 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
8116 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008117 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07008118 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
8119 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008120 }
Matt Carlson9974a352007-10-07 23:27:28 -07008121 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8122 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008123 }
8124
8125 tw32_f(RDMAC_MODE, rdmac_mode);
8126 udelay(40);
8127
8128 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
8129 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
8130 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07008131
8132 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
8133 tw32(SNDDATAC_MODE,
8134 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
8135 else
8136 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
8137
Linus Torvalds1da177e2005-04-16 15:20:36 -07008138 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
8139 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
8140 tw32(RCVDBDI_MODE, RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ);
8141 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008142 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
8143 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008144 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Matt Carlson19cfaec2009-12-03 08:36:20 +00008145 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008146 val |= SNDBDI_MODE_MULTI_TXQ_EN;
8147 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008148 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
8149
8150 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
8151 err = tg3_load_5701_a0_firmware_fix(tp);
8152 if (err)
8153 return err;
8154 }
8155
Linus Torvalds1da177e2005-04-16 15:20:36 -07008156 if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) {
8157 err = tg3_load_tso_firmware(tp);
8158 if (err)
8159 return err;
8160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008161
8162 tp->tx_mode = TX_MODE_ENABLE;
8163 tw32_f(MAC_TX_MODE, tp->tx_mode);
8164 udelay(100);
8165
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008166 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS) {
8167 u32 reg = MAC_RSS_INDIR_TBL_0;
8168 u8 *ent = (u8 *)&val;
8169
8170 /* Setup the indirection table */
8171 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
8172 int idx = i % sizeof(val);
8173
8174 ent[idx] = i % (tp->irq_cnt - 1);
8175 if (idx == sizeof(val) - 1) {
8176 tw32(reg, val);
8177 reg += 4;
8178 }
8179 }
8180
8181 /* Setup the "secret" hash key. */
8182 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
8183 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
8184 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
8185 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
8186 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
8187 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
8188 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
8189 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
8190 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
8191 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
8192 }
8193
Linus Torvalds1da177e2005-04-16 15:20:36 -07008194 tp->rx_mode = RX_MODE_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08008195 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Michael Chanaf36e6b2006-03-23 01:28:06 -08008196 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
8197
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008198 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)
8199 tp->rx_mode |= RX_MODE_RSS_ENABLE |
8200 RX_MODE_RSS_ITBL_HASH_BITS_7 |
8201 RX_MODE_RSS_IPV6_HASH_EN |
8202 RX_MODE_RSS_TCP_IPV6_HASH_EN |
8203 RX_MODE_RSS_IPV4_HASH_EN |
8204 RX_MODE_RSS_TCP_IPV4_HASH_EN;
8205
Linus Torvalds1da177e2005-04-16 15:20:36 -07008206 tw32_f(MAC_RX_MODE, tp->rx_mode);
8207 udelay(10);
8208
Linus Torvalds1da177e2005-04-16 15:20:36 -07008209 tw32(MAC_LED_CTRL, tp->led_ctrl);
8210
8211 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Michael Chanc94e3942005-09-27 12:12:42 -07008212 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008213 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8214 udelay(10);
8215 }
8216 tw32_f(MAC_RX_MODE, tp->rx_mode);
8217 udelay(10);
8218
8219 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
8220 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
8221 !(tp->tg3_flags2 & TG3_FLG2_SERDES_PREEMPHASIS)) {
8222 /* Set drive transmission level to 1.2V */
8223 /* only if the signal pre-emphasis bit is not set */
8224 val = tr32(MAC_SERDES_CFG);
8225 val &= 0xfffff000;
8226 val |= 0x880;
8227 tw32(MAC_SERDES_CFG, val);
8228 }
8229 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
8230 tw32(MAC_SERDES_CFG, 0x616000);
8231 }
8232
8233 /* Prevent chip from dropping frames when flow control
8234 * is enabled.
8235 */
Matt Carlson666bc832010-01-20 16:58:03 +00008236 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8237 val = 1;
8238 else
8239 val = 2;
8240 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008241
8242 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
8243 (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)) {
8244 /* Use hardware link auto-negotiation */
8245 tp->tg3_flags2 |= TG3_FLG2_HW_AUTONEG;
8246 }
8247
Michael Chand4d2c552006-03-20 17:47:20 -08008248 if ((tp->tg3_flags2 & TG3_FLG2_MII_SERDES) &&
8249 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)) {
8250 u32 tmp;
8251
8252 tmp = tr32(SERDES_RX_CTRL);
8253 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
8254 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
8255 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
8256 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8257 }
8258
Matt Carlsondd477002008-05-25 23:45:58 -07008259 if (!(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)) {
8260 if (tp->link_config.phy_is_low_power) {
8261 tp->link_config.phy_is_low_power = 0;
8262 tp->link_config.speed = tp->link_config.orig_speed;
8263 tp->link_config.duplex = tp->link_config.orig_duplex;
8264 tp->link_config.autoneg = tp->link_config.orig_autoneg;
8265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008266
Matt Carlsondd477002008-05-25 23:45:58 -07008267 err = tg3_setup_phy(tp, 0);
8268 if (err)
8269 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008270
Matt Carlsondd477002008-05-25 23:45:58 -07008271 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
Matt Carlson7f97a4b2009-08-25 10:10:03 +00008272 !(tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07008273 u32 tmp;
8274
8275 /* Clear CRC stats. */
8276 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
8277 tg3_writephy(tp, MII_TG3_TEST1,
8278 tmp | MII_TG3_TEST1_CRC_EN);
8279 tg3_readphy(tp, 0x14, &tmp);
8280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008281 }
8282 }
8283
8284 __tg3_set_rx_mode(tp->dev);
8285
8286 /* Initialize receive rules. */
8287 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
8288 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
8289 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
8290 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
8291
Michael Chan4cf78e42005-07-25 12:29:19 -07008292 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Michael Chana4e2b342005-10-26 15:46:52 -07008293 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008294 limit = 8;
8295 else
8296 limit = 16;
8297 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF)
8298 limit -= 4;
8299 switch (limit) {
8300 case 16:
8301 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
8302 case 15:
8303 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
8304 case 14:
8305 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
8306 case 13:
8307 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
8308 case 12:
8309 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
8310 case 11:
8311 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
8312 case 10:
8313 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
8314 case 9:
8315 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
8316 case 8:
8317 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
8318 case 7:
8319 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
8320 case 6:
8321 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
8322 case 5:
8323 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
8324 case 4:
8325 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
8326 case 3:
8327 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
8328 case 2:
8329 case 1:
8330
8331 default:
8332 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07008333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008334
Matt Carlson9ce768e2007-10-11 19:49:11 -07008335 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
8336 /* Write our heartbeat update interval to APE. */
8337 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
8338 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07008339
Linus Torvalds1da177e2005-04-16 15:20:36 -07008340 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
8341
Linus Torvalds1da177e2005-04-16 15:20:36 -07008342 return 0;
8343}
8344
8345/* Called at device open time to get the chip ready for
8346 * packet processing. Invoked with tp->lock held.
8347 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008348static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008349{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008350 tg3_switch_clocks(tp);
8351
8352 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
8353
Matt Carlson2f751b62008-08-04 23:17:34 -07008354 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008355}
8356
8357#define TG3_STAT_ADD32(PSTAT, REG) \
8358do { u32 __val = tr32(REG); \
8359 (PSTAT)->low += __val; \
8360 if ((PSTAT)->low < __val) \
8361 (PSTAT)->high += 1; \
8362} while (0)
8363
8364static void tg3_periodic_fetch_stats(struct tg3 *tp)
8365{
8366 struct tg3_hw_stats *sp = tp->hw_stats;
8367
8368 if (!netif_carrier_ok(tp->dev))
8369 return;
8370
8371 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
8372 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
8373 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
8374 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
8375 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
8376 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
8377 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
8378 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
8379 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
8380 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
8381 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
8382 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
8383 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
8384
8385 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
8386 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
8387 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
8388 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
8389 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
8390 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
8391 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
8392 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
8393 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
8394 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
8395 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
8396 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
8397 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
8398 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07008399
8400 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
8401 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
8402 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008403}
8404
8405static void tg3_timer(unsigned long __opaque)
8406{
8407 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008408
Michael Chanf475f162006-03-27 23:20:14 -08008409 if (tp->irq_sync)
8410 goto restart_timer;
8411
David S. Millerf47c11e2005-06-24 20:18:35 -07008412 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008413
David S. Millerfac9b832005-05-18 22:46:34 -07008414 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)) {
8415 /* All of this garbage is because when using non-tagged
8416 * IRQ status the mailbox/status_block protocol the chip
8417 * uses with the cpu is race prone.
8418 */
Matt Carlson898a56f2009-08-28 14:02:40 +00008419 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -07008420 tw32(GRC_LOCAL_CTRL,
8421 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
8422 } else {
8423 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00008424 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -07008425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008426
David S. Millerfac9b832005-05-18 22:46:34 -07008427 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
8428 tp->tg3_flags2 |= TG3_FLG2_RESTART_TIMER;
David S. Millerf47c11e2005-06-24 20:18:35 -07008429 spin_unlock(&tp->lock);
David S. Millerfac9b832005-05-18 22:46:34 -07008430 schedule_work(&tp->reset_task);
8431 return;
8432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008433 }
8434
Linus Torvalds1da177e2005-04-16 15:20:36 -07008435 /* This part only runs once per second. */
8436 if (!--tp->timer_counter) {
David S. Millerfac9b832005-05-18 22:46:34 -07008437 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
8438 tg3_periodic_fetch_stats(tp);
8439
Linus Torvalds1da177e2005-04-16 15:20:36 -07008440 if (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) {
8441 u32 mac_stat;
8442 int phy_event;
8443
8444 mac_stat = tr32(MAC_STATUS);
8445
8446 phy_event = 0;
8447 if (tp->tg3_flags & TG3_FLAG_USE_MI_INTERRUPT) {
8448 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
8449 phy_event = 1;
8450 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
8451 phy_event = 1;
8452
8453 if (phy_event)
8454 tg3_setup_phy(tp, 0);
8455 } else if (tp->tg3_flags & TG3_FLAG_POLL_SERDES) {
8456 u32 mac_stat = tr32(MAC_STATUS);
8457 int need_setup = 0;
8458
8459 if (netif_carrier_ok(tp->dev) &&
8460 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
8461 need_setup = 1;
8462 }
8463 if (! netif_carrier_ok(tp->dev) &&
8464 (mac_stat & (MAC_STATUS_PCS_SYNCED |
8465 MAC_STATUS_SIGNAL_DET))) {
8466 need_setup = 1;
8467 }
8468 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07008469 if (!tp->serdes_counter) {
8470 tw32_f(MAC_MODE,
8471 (tp->mac_mode &
8472 ~MAC_MODE_PORT_MODE_MASK));
8473 udelay(40);
8474 tw32_f(MAC_MODE, tp->mac_mode);
8475 udelay(40);
8476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008477 tg3_setup_phy(tp, 0);
8478 }
Michael Chan747e8f82005-07-25 12:33:22 -07008479 } else if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)
8480 tg3_serdes_parallel_detect(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008481
8482 tp->timer_counter = tp->timer_multiplier;
8483 }
8484
Michael Chan130b8e42006-09-27 16:00:40 -07008485 /* Heartbeat is only sent once every 2 seconds.
8486 *
8487 * The heartbeat is to tell the ASF firmware that the host
8488 * driver is still alive. In the event that the OS crashes,
8489 * ASF needs to reset the hardware to free up the FIFO space
8490 * that may be filled with rx packets destined for the host.
8491 * If the FIFO is full, ASF will no longer function properly.
8492 *
8493 * Unintended resets have been reported on real time kernels
8494 * where the timer doesn't run on time. Netpoll will also have
8495 * same problem.
8496 *
8497 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
8498 * to check the ring condition when the heartbeat is expiring
8499 * before doing the reset. This will prevent most unintended
8500 * resets.
8501 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008502 if (!--tp->asf_counter) {
Matt Carlsonbc7959b2008-08-15 14:08:55 -07008503 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
8504 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07008505 tg3_wait_for_event_ack(tp);
8506
Michael Chanbbadf502006-04-06 21:46:34 -07008507 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07008508 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07008509 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +00008510 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
8511 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -07008512
8513 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008514 }
8515 tp->asf_counter = tp->asf_multiplier;
8516 }
8517
David S. Millerf47c11e2005-06-24 20:18:35 -07008518 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008519
Michael Chanf475f162006-03-27 23:20:14 -08008520restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07008521 tp->timer.expires = jiffies + tp->timer_offset;
8522 add_timer(&tp->timer);
8523}
8524
Matt Carlson4f125f42009-09-01 12:55:02 +00008525static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -08008526{
David Howells7d12e782006-10-05 14:55:46 +01008527 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008528 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +00008529 char *name;
8530 struct tg3_napi *tnapi = &tp->napi[irq_num];
8531
8532 if (tp->irq_cnt == 1)
8533 name = tp->dev->name;
8534 else {
8535 name = &tnapi->irq_lbl[0];
8536 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
8537 name[IFNAMSIZ-1] = 0;
8538 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08008539
Matt Carlson679563f2009-09-01 12:55:46 +00008540 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI_OR_MSIX) {
Michael Chanfcfa0a32006-03-20 22:28:41 -08008541 fn = tg3_msi;
8542 if (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)
8543 fn = tg3_msi_1shot;
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07008544 flags = IRQF_SAMPLE_RANDOM;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008545 } else {
8546 fn = tg3_interrupt;
8547 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)
8548 fn = tg3_interrupt_tagged;
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07008549 flags = IRQF_SHARED | IRQF_SAMPLE_RANDOM;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008550 }
Matt Carlson4f125f42009-09-01 12:55:02 +00008551
8552 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08008553}
8554
Michael Chan79381092005-04-21 17:13:59 -07008555static int tg3_test_interrupt(struct tg3 *tp)
8556{
Matt Carlson09943a12009-08-28 14:01:57 +00008557 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -07008558 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -07008559 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008560 u32 val;
Michael Chan79381092005-04-21 17:13:59 -07008561
Michael Chand4bc3922005-05-29 14:59:20 -07008562 if (!netif_running(dev))
8563 return -ENODEV;
8564
Michael Chan79381092005-04-21 17:13:59 -07008565 tg3_disable_ints(tp);
8566
Matt Carlson4f125f42009-09-01 12:55:02 +00008567 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07008568
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008569 /*
8570 * Turn off MSI one shot mode. Otherwise this test has no
8571 * observable way to know whether the interrupt was delivered.
8572 */
Matt Carlsonb703df62009-12-03 08:36:21 +00008573 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
8574 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008575 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
8576 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
8577 tw32(MSGINT_MODE, val);
8578 }
8579
Matt Carlson4f125f42009-09-01 12:55:02 +00008580 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Matt Carlson09943a12009-08-28 14:01:57 +00008581 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07008582 if (err)
8583 return err;
8584
Matt Carlson898a56f2009-08-28 14:02:40 +00008585 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -07008586 tg3_enable_ints(tp);
8587
8588 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00008589 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -07008590
8591 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -07008592 u32 int_mbox, misc_host_ctrl;
8593
Matt Carlson898a56f2009-08-28 14:02:40 +00008594 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -07008595 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
8596
8597 if ((int_mbox != 0) ||
8598 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
8599 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -07008600 break;
Michael Chanb16250e2006-09-27 16:10:14 -07008601 }
8602
Michael Chan79381092005-04-21 17:13:59 -07008603 msleep(10);
8604 }
8605
8606 tg3_disable_ints(tp);
8607
Matt Carlson4f125f42009-09-01 12:55:02 +00008608 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008609
Matt Carlson4f125f42009-09-01 12:55:02 +00008610 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07008611
8612 if (err)
8613 return err;
8614
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008615 if (intr_ok) {
8616 /* Reenable MSI one shot mode. */
Matt Carlsonb703df62009-12-03 08:36:21 +00008617 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
8618 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008619 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
8620 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
8621 tw32(MSGINT_MODE, val);
8622 }
Michael Chan79381092005-04-21 17:13:59 -07008623 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008624 }
Michael Chan79381092005-04-21 17:13:59 -07008625
8626 return -EIO;
8627}
8628
8629/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
8630 * successfully restored
8631 */
8632static int tg3_test_msi(struct tg3 *tp)
8633{
Michael Chan79381092005-04-21 17:13:59 -07008634 int err;
8635 u16 pci_cmd;
8636
8637 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSI))
8638 return 0;
8639
8640 /* Turn off SERR reporting in case MSI terminates with Master
8641 * Abort.
8642 */
8643 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
8644 pci_write_config_word(tp->pdev, PCI_COMMAND,
8645 pci_cmd & ~PCI_COMMAND_SERR);
8646
8647 err = tg3_test_interrupt(tp);
8648
8649 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
8650
8651 if (!err)
8652 return 0;
8653
8654 /* other failures */
8655 if (err != -EIO)
8656 return err;
8657
8658 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +00008659 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
8660 "to INTx mode. Please report this failure to the PCI "
8661 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -07008662
Matt Carlson4f125f42009-09-01 12:55:02 +00008663 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +00008664
Michael Chan79381092005-04-21 17:13:59 -07008665 pci_disable_msi(tp->pdev);
8666
8667 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI;
8668
Matt Carlson4f125f42009-09-01 12:55:02 +00008669 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07008670 if (err)
8671 return err;
8672
8673 /* Need to reset the chip because the MSI cycle may have terminated
8674 * with Master Abort.
8675 */
David S. Millerf47c11e2005-06-24 20:18:35 -07008676 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07008677
Michael Chan944d9802005-05-29 14:57:48 -07008678 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008679 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07008680
David S. Millerf47c11e2005-06-24 20:18:35 -07008681 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07008682
8683 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +00008684 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -07008685
8686 return err;
8687}
8688
Matt Carlson9e9fd122009-01-19 16:57:45 -08008689static int tg3_request_firmware(struct tg3 *tp)
8690{
8691 const __be32 *fw_data;
8692
8693 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00008694 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
8695 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08008696 return -ENOENT;
8697 }
8698
8699 fw_data = (void *)tp->fw->data;
8700
8701 /* Firmware blob starts with version numbers, followed by
8702 * start address and _full_ length including BSS sections
8703 * (which must be longer than the actual data, of course
8704 */
8705
8706 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
8707 if (tp->fw_len < (tp->fw->size - 12)) {
Joe Perches05dbe002010-02-17 19:44:19 +00008708 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
8709 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08008710 release_firmware(tp->fw);
8711 tp->fw = NULL;
8712 return -EINVAL;
8713 }
8714
8715 /* We no longer need firmware; we have it. */
8716 tp->fw_needed = NULL;
8717 return 0;
8718}
8719
Matt Carlson679563f2009-09-01 12:55:46 +00008720static bool tg3_enable_msix(struct tg3 *tp)
8721{
8722 int i, rc, cpus = num_online_cpus();
8723 struct msix_entry msix_ent[tp->irq_max];
8724
8725 if (cpus == 1)
8726 /* Just fallback to the simpler MSI mode. */
8727 return false;
8728
8729 /*
8730 * We want as many rx rings enabled as there are cpus.
8731 * The first MSIX vector only deals with link interrupts, etc,
8732 * so we add one to the number of vectors we are requesting.
8733 */
8734 tp->irq_cnt = min_t(unsigned, cpus + 1, tp->irq_max);
8735
8736 for (i = 0; i < tp->irq_max; i++) {
8737 msix_ent[i].entry = i;
8738 msix_ent[i].vector = 0;
8739 }
8740
8741 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
8742 if (rc != 0) {
8743 if (rc < TG3_RSS_MIN_NUM_MSIX_VECS)
8744 return false;
8745 if (pci_enable_msix(tp->pdev, msix_ent, rc))
8746 return false;
Joe Perches05dbe002010-02-17 19:44:19 +00008747 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
8748 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +00008749 tp->irq_cnt = rc;
8750 }
8751
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008752 tp->tg3_flags3 |= TG3_FLG3_ENABLE_RSS;
8753
Matt Carlson679563f2009-09-01 12:55:46 +00008754 for (i = 0; i < tp->irq_max; i++)
8755 tp->napi[i].irq_vec = msix_ent[i].vector;
8756
Matt Carlson19cfaec2009-12-03 08:36:20 +00008757 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
8758 tp->tg3_flags3 |= TG3_FLG3_ENABLE_TSS;
8759 tp->dev->real_num_tx_queues = tp->irq_cnt - 1;
8760 } else
8761 tp->dev->real_num_tx_queues = 1;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00008762
Matt Carlson679563f2009-09-01 12:55:46 +00008763 return true;
8764}
8765
Matt Carlson07b01732009-08-28 14:01:15 +00008766static void tg3_ints_init(struct tg3 *tp)
8767{
Matt Carlson679563f2009-09-01 12:55:46 +00008768 if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSI_OR_MSIX) &&
8769 !(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +00008770 /* All MSI supporting chips should support tagged
8771 * status. Assert that this is the case.
8772 */
Matt Carlson5129c3a2010-04-05 10:19:23 +00008773 netdev_warn(tp->dev,
8774 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +00008775 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +00008776 }
Matt Carlson4f125f42009-09-01 12:55:02 +00008777
Matt Carlson679563f2009-09-01 12:55:46 +00008778 if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX) && tg3_enable_msix(tp))
8779 tp->tg3_flags2 |= TG3_FLG2_USING_MSIX;
8780 else if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSI) &&
8781 pci_enable_msi(tp->pdev) == 0)
8782 tp->tg3_flags2 |= TG3_FLG2_USING_MSI;
8783
8784 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI_OR_MSIX) {
8785 u32 msi_mode = tr32(MSGINT_MODE);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008786 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
8787 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson679563f2009-09-01 12:55:46 +00008788 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
8789 }
8790defcfg:
8791 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSIX)) {
8792 tp->irq_cnt = 1;
8793 tp->napi[0].irq_vec = tp->pdev->irq;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00008794 tp->dev->real_num_tx_queues = 1;
Matt Carlson679563f2009-09-01 12:55:46 +00008795 }
Matt Carlson07b01732009-08-28 14:01:15 +00008796}
8797
8798static void tg3_ints_fini(struct tg3 *tp)
8799{
Matt Carlson679563f2009-09-01 12:55:46 +00008800 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
8801 pci_disable_msix(tp->pdev);
8802 else if (tp->tg3_flags2 & TG3_FLG2_USING_MSI)
8803 pci_disable_msi(tp->pdev);
8804 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI_OR_MSIX;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008805 tp->tg3_flags3 &= ~TG3_FLG3_ENABLE_RSS;
Matt Carlson07b01732009-08-28 14:01:15 +00008806}
8807
Linus Torvalds1da177e2005-04-16 15:20:36 -07008808static int tg3_open(struct net_device *dev)
8809{
8810 struct tg3 *tp = netdev_priv(dev);
Matt Carlson4f125f42009-09-01 12:55:02 +00008811 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008812
Matt Carlson9e9fd122009-01-19 16:57:45 -08008813 if (tp->fw_needed) {
8814 err = tg3_request_firmware(tp);
8815 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
8816 if (err)
8817 return err;
8818 } else if (err) {
Joe Perches05dbe002010-02-17 19:44:19 +00008819 netdev_warn(tp->dev, "TSO capability disabled\n");
Matt Carlson9e9fd122009-01-19 16:57:45 -08008820 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
8821 } else if (!(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
Joe Perches05dbe002010-02-17 19:44:19 +00008822 netdev_notice(tp->dev, "TSO capability restored\n");
Matt Carlson9e9fd122009-01-19 16:57:45 -08008823 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
8824 }
8825 }
8826
Michael Chanc49a1562006-12-17 17:07:29 -08008827 netif_carrier_off(tp->dev);
8828
Michael Chanbc1c7562006-03-20 17:48:03 -08008829 err = tg3_set_power_state(tp, PCI_D0);
Matt Carlson2f751b62008-08-04 23:17:34 -07008830 if (err)
Michael Chanbc1c7562006-03-20 17:48:03 -08008831 return err;
Matt Carlson2f751b62008-08-04 23:17:34 -07008832
8833 tg3_full_lock(tp, 0);
Michael Chanbc1c7562006-03-20 17:48:03 -08008834
Linus Torvalds1da177e2005-04-16 15:20:36 -07008835 tg3_disable_ints(tp);
8836 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
8837
David S. Millerf47c11e2005-06-24 20:18:35 -07008838 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008839
Matt Carlson679563f2009-09-01 12:55:46 +00008840 /*
8841 * Setup interrupts first so we know how
8842 * many NAPI resources to allocate
8843 */
8844 tg3_ints_init(tp);
8845
Linus Torvalds1da177e2005-04-16 15:20:36 -07008846 /* The placement of this call is tied
8847 * to the setup and use of Host TX descriptors.
8848 */
8849 err = tg3_alloc_consistent(tp);
8850 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00008851 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008852
Matt Carlsonfed97812009-09-01 13:10:19 +00008853 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07008854
Matt Carlson4f125f42009-09-01 12:55:02 +00008855 for (i = 0; i < tp->irq_cnt; i++) {
8856 struct tg3_napi *tnapi = &tp->napi[i];
8857 err = tg3_request_irq(tp, i);
8858 if (err) {
8859 for (i--; i >= 0; i--)
8860 free_irq(tnapi->irq_vec, tnapi);
8861 break;
8862 }
8863 }
Matt Carlson07b01732009-08-28 14:01:15 +00008864
8865 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00008866 goto err_out2;
Matt Carlson07b01732009-08-28 14:01:15 +00008867
David S. Millerf47c11e2005-06-24 20:18:35 -07008868 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008869
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008870 err = tg3_init_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008871 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -07008872 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008873 tg3_free_rings(tp);
8874 } else {
David S. Millerfac9b832005-05-18 22:46:34 -07008875 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)
8876 tp->timer_offset = HZ;
8877 else
8878 tp->timer_offset = HZ / 10;
8879
8880 BUG_ON(tp->timer_offset > HZ);
8881 tp->timer_counter = tp->timer_multiplier =
8882 (HZ / tp->timer_offset);
8883 tp->asf_counter = tp->asf_multiplier =
Michael Chan28fbef72005-10-26 15:48:35 -07008884 ((HZ / tp->timer_offset) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008885
8886 init_timer(&tp->timer);
8887 tp->timer.expires = jiffies + tp->timer_offset;
8888 tp->timer.data = (unsigned long) tp;
8889 tp->timer.function = tg3_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008890 }
8891
David S. Millerf47c11e2005-06-24 20:18:35 -07008892 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008893
Matt Carlson07b01732009-08-28 14:01:15 +00008894 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00008895 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008896
Michael Chan79381092005-04-21 17:13:59 -07008897 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
8898 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -07008899
Michael Chan79381092005-04-21 17:13:59 -07008900 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -07008901 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -07008902 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -07008903 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07008904 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07008905
Matt Carlson679563f2009-09-01 12:55:46 +00008906 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -07008907 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08008908
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008909 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
Matt Carlsonb703df62009-12-03 08:36:21 +00008910 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57765 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008911 (tp->tg3_flags2 & TG3_FLG2_USING_MSI) &&
8912 (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)) {
8913 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -08008914
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008915 tw32(PCIE_TRANSACTION_CFG,
8916 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -08008917 }
Michael Chan79381092005-04-21 17:13:59 -07008918 }
8919
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07008920 tg3_phy_start(tp);
8921
David S. Millerf47c11e2005-06-24 20:18:35 -07008922 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008923
Michael Chan79381092005-04-21 17:13:59 -07008924 add_timer(&tp->timer);
8925 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008926 tg3_enable_ints(tp);
8927
David S. Millerf47c11e2005-06-24 20:18:35 -07008928 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008929
Matt Carlsonfe5f5782009-09-01 13:09:39 +00008930 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008931
8932 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +00008933
Matt Carlson679563f2009-09-01 12:55:46 +00008934err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +00008935 for (i = tp->irq_cnt - 1; i >= 0; i--) {
8936 struct tg3_napi *tnapi = &tp->napi[i];
8937 free_irq(tnapi->irq_vec, tnapi);
8938 }
Matt Carlson07b01732009-08-28 14:01:15 +00008939
Matt Carlson679563f2009-09-01 12:55:46 +00008940err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +00008941 tg3_napi_disable(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00008942 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +00008943
8944err_out1:
8945 tg3_ints_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00008946 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008947}
8948
Linus Torvalds1da177e2005-04-16 15:20:36 -07008949static struct net_device_stats *tg3_get_stats(struct net_device *);
8950static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *);
8951
8952static int tg3_close(struct net_device *dev)
8953{
Matt Carlson4f125f42009-09-01 12:55:02 +00008954 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008955 struct tg3 *tp = netdev_priv(dev);
8956
Matt Carlsonfed97812009-09-01 13:10:19 +00008957 tg3_napi_disable(tp);
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07008958 cancel_work_sync(&tp->reset_task);
Michael Chan7faa0062006-02-02 17:29:28 -08008959
Matt Carlsonfe5f5782009-09-01 13:09:39 +00008960 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008961
8962 del_timer_sync(&tp->timer);
8963
Matt Carlson24bb4fb2009-10-05 17:55:29 +00008964 tg3_phy_stop(tp);
8965
David S. Millerf47c11e2005-06-24 20:18:35 -07008966 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008967
8968 tg3_disable_ints(tp);
8969
Michael Chan944d9802005-05-29 14:57:48 -07008970 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008971 tg3_free_rings(tp);
Michael Chan5cf64b8a2007-05-05 12:11:21 -07008972 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008973
David S. Millerf47c11e2005-06-24 20:18:35 -07008974 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008975
Matt Carlson4f125f42009-09-01 12:55:02 +00008976 for (i = tp->irq_cnt - 1; i >= 0; i--) {
8977 struct tg3_napi *tnapi = &tp->napi[i];
8978 free_irq(tnapi->irq_vec, tnapi);
8979 }
Matt Carlson07b01732009-08-28 14:01:15 +00008980
8981 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008982
8983 memcpy(&tp->net_stats_prev, tg3_get_stats(tp->dev),
8984 sizeof(tp->net_stats_prev));
8985 memcpy(&tp->estats_prev, tg3_get_estats(tp),
8986 sizeof(tp->estats_prev));
8987
8988 tg3_free_consistent(tp);
8989
Michael Chanbc1c7562006-03-20 17:48:03 -08008990 tg3_set_power_state(tp, PCI_D3hot);
8991
8992 netif_carrier_off(tp->dev);
8993
Linus Torvalds1da177e2005-04-16 15:20:36 -07008994 return 0;
8995}
8996
8997static inline unsigned long get_stat64(tg3_stat64_t *val)
8998{
8999 unsigned long ret;
9000
9001#if (BITS_PER_LONG == 32)
9002 ret = val->low;
9003#else
9004 ret = ((u64)val->high << 32) | ((u64)val->low);
9005#endif
9006 return ret;
9007}
9008
Stefan Buehler816f8b82008-08-15 14:10:54 -07009009static inline u64 get_estat64(tg3_stat64_t *val)
9010{
9011 return ((u64)val->high << 32) | ((u64)val->low);
9012}
9013
Linus Torvalds1da177e2005-04-16 15:20:36 -07009014static unsigned long calc_crc_errors(struct tg3 *tp)
9015{
9016 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9017
9018 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
9019 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
9020 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009021 u32 val;
9022
David S. Millerf47c11e2005-06-24 20:18:35 -07009023 spin_lock_bh(&tp->lock);
Michael Chan569a5df2007-02-13 12:18:15 -08009024 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
9025 tg3_writephy(tp, MII_TG3_TEST1,
9026 val | MII_TG3_TEST1_CRC_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009027 tg3_readphy(tp, 0x14, &val);
9028 } else
9029 val = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07009030 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009031
9032 tp->phy_crc_errors += val;
9033
9034 return tp->phy_crc_errors;
9035 }
9036
9037 return get_stat64(&hw_stats->rx_fcs_errors);
9038}
9039
9040#define ESTAT_ADD(member) \
9041 estats->member = old_estats->member + \
Stefan Buehler816f8b82008-08-15 14:10:54 -07009042 get_estat64(&hw_stats->member)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009043
9044static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *tp)
9045{
9046 struct tg3_ethtool_stats *estats = &tp->estats;
9047 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
9048 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9049
9050 if (!hw_stats)
9051 return old_estats;
9052
9053 ESTAT_ADD(rx_octets);
9054 ESTAT_ADD(rx_fragments);
9055 ESTAT_ADD(rx_ucast_packets);
9056 ESTAT_ADD(rx_mcast_packets);
9057 ESTAT_ADD(rx_bcast_packets);
9058 ESTAT_ADD(rx_fcs_errors);
9059 ESTAT_ADD(rx_align_errors);
9060 ESTAT_ADD(rx_xon_pause_rcvd);
9061 ESTAT_ADD(rx_xoff_pause_rcvd);
9062 ESTAT_ADD(rx_mac_ctrl_rcvd);
9063 ESTAT_ADD(rx_xoff_entered);
9064 ESTAT_ADD(rx_frame_too_long_errors);
9065 ESTAT_ADD(rx_jabbers);
9066 ESTAT_ADD(rx_undersize_packets);
9067 ESTAT_ADD(rx_in_length_errors);
9068 ESTAT_ADD(rx_out_length_errors);
9069 ESTAT_ADD(rx_64_or_less_octet_packets);
9070 ESTAT_ADD(rx_65_to_127_octet_packets);
9071 ESTAT_ADD(rx_128_to_255_octet_packets);
9072 ESTAT_ADD(rx_256_to_511_octet_packets);
9073 ESTAT_ADD(rx_512_to_1023_octet_packets);
9074 ESTAT_ADD(rx_1024_to_1522_octet_packets);
9075 ESTAT_ADD(rx_1523_to_2047_octet_packets);
9076 ESTAT_ADD(rx_2048_to_4095_octet_packets);
9077 ESTAT_ADD(rx_4096_to_8191_octet_packets);
9078 ESTAT_ADD(rx_8192_to_9022_octet_packets);
9079
9080 ESTAT_ADD(tx_octets);
9081 ESTAT_ADD(tx_collisions);
9082 ESTAT_ADD(tx_xon_sent);
9083 ESTAT_ADD(tx_xoff_sent);
9084 ESTAT_ADD(tx_flow_control);
9085 ESTAT_ADD(tx_mac_errors);
9086 ESTAT_ADD(tx_single_collisions);
9087 ESTAT_ADD(tx_mult_collisions);
9088 ESTAT_ADD(tx_deferred);
9089 ESTAT_ADD(tx_excessive_collisions);
9090 ESTAT_ADD(tx_late_collisions);
9091 ESTAT_ADD(tx_collide_2times);
9092 ESTAT_ADD(tx_collide_3times);
9093 ESTAT_ADD(tx_collide_4times);
9094 ESTAT_ADD(tx_collide_5times);
9095 ESTAT_ADD(tx_collide_6times);
9096 ESTAT_ADD(tx_collide_7times);
9097 ESTAT_ADD(tx_collide_8times);
9098 ESTAT_ADD(tx_collide_9times);
9099 ESTAT_ADD(tx_collide_10times);
9100 ESTAT_ADD(tx_collide_11times);
9101 ESTAT_ADD(tx_collide_12times);
9102 ESTAT_ADD(tx_collide_13times);
9103 ESTAT_ADD(tx_collide_14times);
9104 ESTAT_ADD(tx_collide_15times);
9105 ESTAT_ADD(tx_ucast_packets);
9106 ESTAT_ADD(tx_mcast_packets);
9107 ESTAT_ADD(tx_bcast_packets);
9108 ESTAT_ADD(tx_carrier_sense_errors);
9109 ESTAT_ADD(tx_discards);
9110 ESTAT_ADD(tx_errors);
9111
9112 ESTAT_ADD(dma_writeq_full);
9113 ESTAT_ADD(dma_write_prioq_full);
9114 ESTAT_ADD(rxbds_empty);
9115 ESTAT_ADD(rx_discards);
9116 ESTAT_ADD(rx_errors);
9117 ESTAT_ADD(rx_threshold_hit);
9118
9119 ESTAT_ADD(dma_readq_full);
9120 ESTAT_ADD(dma_read_prioq_full);
9121 ESTAT_ADD(tx_comp_queue_full);
9122
9123 ESTAT_ADD(ring_set_send_prod_index);
9124 ESTAT_ADD(ring_status_update);
9125 ESTAT_ADD(nic_irqs);
9126 ESTAT_ADD(nic_avoided_irqs);
9127 ESTAT_ADD(nic_tx_threshold_hit);
9128
9129 return estats;
9130}
9131
9132static struct net_device_stats *tg3_get_stats(struct net_device *dev)
9133{
9134 struct tg3 *tp = netdev_priv(dev);
9135 struct net_device_stats *stats = &tp->net_stats;
9136 struct net_device_stats *old_stats = &tp->net_stats_prev;
9137 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9138
9139 if (!hw_stats)
9140 return old_stats;
9141
9142 stats->rx_packets = old_stats->rx_packets +
9143 get_stat64(&hw_stats->rx_ucast_packets) +
9144 get_stat64(&hw_stats->rx_mcast_packets) +
9145 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009146
Linus Torvalds1da177e2005-04-16 15:20:36 -07009147 stats->tx_packets = old_stats->tx_packets +
9148 get_stat64(&hw_stats->tx_ucast_packets) +
9149 get_stat64(&hw_stats->tx_mcast_packets) +
9150 get_stat64(&hw_stats->tx_bcast_packets);
9151
9152 stats->rx_bytes = old_stats->rx_bytes +
9153 get_stat64(&hw_stats->rx_octets);
9154 stats->tx_bytes = old_stats->tx_bytes +
9155 get_stat64(&hw_stats->tx_octets);
9156
9157 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -07009158 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009159 stats->tx_errors = old_stats->tx_errors +
9160 get_stat64(&hw_stats->tx_errors) +
9161 get_stat64(&hw_stats->tx_mac_errors) +
9162 get_stat64(&hw_stats->tx_carrier_sense_errors) +
9163 get_stat64(&hw_stats->tx_discards);
9164
9165 stats->multicast = old_stats->multicast +
9166 get_stat64(&hw_stats->rx_mcast_packets);
9167 stats->collisions = old_stats->collisions +
9168 get_stat64(&hw_stats->tx_collisions);
9169
9170 stats->rx_length_errors = old_stats->rx_length_errors +
9171 get_stat64(&hw_stats->rx_frame_too_long_errors) +
9172 get_stat64(&hw_stats->rx_undersize_packets);
9173
9174 stats->rx_over_errors = old_stats->rx_over_errors +
9175 get_stat64(&hw_stats->rxbds_empty);
9176 stats->rx_frame_errors = old_stats->rx_frame_errors +
9177 get_stat64(&hw_stats->rx_align_errors);
9178 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
9179 get_stat64(&hw_stats->tx_discards);
9180 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
9181 get_stat64(&hw_stats->tx_carrier_sense_errors);
9182
9183 stats->rx_crc_errors = old_stats->rx_crc_errors +
9184 calc_crc_errors(tp);
9185
John W. Linville4f63b872005-09-12 14:43:18 -07009186 stats->rx_missed_errors = old_stats->rx_missed_errors +
9187 get_stat64(&hw_stats->rx_discards);
9188
Linus Torvalds1da177e2005-04-16 15:20:36 -07009189 return stats;
9190}
9191
9192static inline u32 calc_crc(unsigned char *buf, int len)
9193{
9194 u32 reg;
9195 u32 tmp;
9196 int j, k;
9197
9198 reg = 0xffffffff;
9199
9200 for (j = 0; j < len; j++) {
9201 reg ^= buf[j];
9202
9203 for (k = 0; k < 8; k++) {
9204 tmp = reg & 0x01;
9205
9206 reg >>= 1;
9207
Matt Carlson859a588792010-04-05 10:19:28 +00009208 if (tmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009209 reg ^= 0xedb88320;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009210 }
9211 }
9212
9213 return ~reg;
9214}
9215
9216static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
9217{
9218 /* accept or reject all multicast frames */
9219 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
9220 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
9221 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
9222 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
9223}
9224
9225static void __tg3_set_rx_mode(struct net_device *dev)
9226{
9227 struct tg3 *tp = netdev_priv(dev);
9228 u32 rx_mode;
9229
9230 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
9231 RX_MODE_KEEP_VLAN_TAG);
9232
9233 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
9234 * flag clear.
9235 */
9236#if TG3_VLAN_TAG_USED
9237 if (!tp->vlgrp &&
9238 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
9239 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9240#else
9241 /* By definition, VLAN is disabled always in this
9242 * case.
9243 */
9244 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
9245 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9246#endif
9247
9248 if (dev->flags & IFF_PROMISC) {
9249 /* Promiscuous mode. */
9250 rx_mode |= RX_MODE_PROMISC;
9251 } else if (dev->flags & IFF_ALLMULTI) {
9252 /* Accept all multicast. */
9253 tg3_set_multi (tp, 1);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00009254 } else if (netdev_mc_empty(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009255 /* Reject all multicast. */
9256 tg3_set_multi (tp, 0);
9257 } else {
9258 /* Accept one or more multicast(s). */
Jiri Pirko22bedad32010-04-01 21:22:57 +00009259 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009260 u32 mc_filter[4] = { 0, };
9261 u32 regidx;
9262 u32 bit;
9263 u32 crc;
9264
Jiri Pirko22bedad32010-04-01 21:22:57 +00009265 netdev_for_each_mc_addr(ha, dev) {
9266 crc = calc_crc(ha->addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009267 bit = ~crc & 0x7f;
9268 regidx = (bit & 0x60) >> 5;
9269 bit &= 0x1f;
9270 mc_filter[regidx] |= (1 << bit);
9271 }
9272
9273 tw32(MAC_HASH_REG_0, mc_filter[0]);
9274 tw32(MAC_HASH_REG_1, mc_filter[1]);
9275 tw32(MAC_HASH_REG_2, mc_filter[2]);
9276 tw32(MAC_HASH_REG_3, mc_filter[3]);
9277 }
9278
9279 if (rx_mode != tp->rx_mode) {
9280 tp->rx_mode = rx_mode;
9281 tw32_f(MAC_RX_MODE, rx_mode);
9282 udelay(10);
9283 }
9284}
9285
9286static void tg3_set_rx_mode(struct net_device *dev)
9287{
9288 struct tg3 *tp = netdev_priv(dev);
9289
Michael Chane75f7c92006-03-20 21:33:26 -08009290 if (!netif_running(dev))
9291 return;
9292
David S. Millerf47c11e2005-06-24 20:18:35 -07009293 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009294 __tg3_set_rx_mode(dev);
David S. Millerf47c11e2005-06-24 20:18:35 -07009295 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009296}
9297
9298#define TG3_REGDUMP_LEN (32 * 1024)
9299
9300static int tg3_get_regs_len(struct net_device *dev)
9301{
9302 return TG3_REGDUMP_LEN;
9303}
9304
9305static void tg3_get_regs(struct net_device *dev,
9306 struct ethtool_regs *regs, void *_p)
9307{
9308 u32 *p = _p;
9309 struct tg3 *tp = netdev_priv(dev);
9310 u8 *orig_p = _p;
9311 int i;
9312
9313 regs->version = 0;
9314
9315 memset(p, 0, TG3_REGDUMP_LEN);
9316
Michael Chanbc1c7562006-03-20 17:48:03 -08009317 if (tp->link_config.phy_is_low_power)
9318 return;
9319
David S. Millerf47c11e2005-06-24 20:18:35 -07009320 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009321
9322#define __GET_REG32(reg) (*(p)++ = tr32(reg))
9323#define GET_REG32_LOOP(base,len) \
9324do { p = (u32 *)(orig_p + (base)); \
9325 for (i = 0; i < len; i += 4) \
9326 __GET_REG32((base) + i); \
9327} while (0)
9328#define GET_REG32_1(reg) \
9329do { p = (u32 *)(orig_p + (reg)); \
9330 __GET_REG32((reg)); \
9331} while (0)
9332
9333 GET_REG32_LOOP(TG3PCI_VENDOR, 0xb0);
9334 GET_REG32_LOOP(MAILBOX_INTERRUPT_0, 0x200);
9335 GET_REG32_LOOP(MAC_MODE, 0x4f0);
9336 GET_REG32_LOOP(SNDDATAI_MODE, 0xe0);
9337 GET_REG32_1(SNDDATAC_MODE);
9338 GET_REG32_LOOP(SNDBDS_MODE, 0x80);
9339 GET_REG32_LOOP(SNDBDI_MODE, 0x48);
9340 GET_REG32_1(SNDBDC_MODE);
9341 GET_REG32_LOOP(RCVLPC_MODE, 0x20);
9342 GET_REG32_LOOP(RCVLPC_SELLST_BASE, 0x15c);
9343 GET_REG32_LOOP(RCVDBDI_MODE, 0x0c);
9344 GET_REG32_LOOP(RCVDBDI_JUMBO_BD, 0x3c);
9345 GET_REG32_LOOP(RCVDBDI_BD_PROD_IDX_0, 0x44);
9346 GET_REG32_1(RCVDCC_MODE);
9347 GET_REG32_LOOP(RCVBDI_MODE, 0x20);
9348 GET_REG32_LOOP(RCVCC_MODE, 0x14);
9349 GET_REG32_LOOP(RCVLSC_MODE, 0x08);
9350 GET_REG32_1(MBFREE_MODE);
9351 GET_REG32_LOOP(HOSTCC_MODE, 0x100);
9352 GET_REG32_LOOP(MEMARB_MODE, 0x10);
9353 GET_REG32_LOOP(BUFMGR_MODE, 0x58);
9354 GET_REG32_LOOP(RDMAC_MODE, 0x08);
9355 GET_REG32_LOOP(WDMAC_MODE, 0x08);
Chris Elmquist091465d2005-12-20 13:25:19 -08009356 GET_REG32_1(RX_CPU_MODE);
9357 GET_REG32_1(RX_CPU_STATE);
9358 GET_REG32_1(RX_CPU_PGMCTR);
9359 GET_REG32_1(RX_CPU_HWBKPT);
9360 GET_REG32_1(TX_CPU_MODE);
9361 GET_REG32_1(TX_CPU_STATE);
9362 GET_REG32_1(TX_CPU_PGMCTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009363 GET_REG32_LOOP(GRCMBOX_INTERRUPT_0, 0x110);
9364 GET_REG32_LOOP(FTQ_RESET, 0x120);
9365 GET_REG32_LOOP(MSGINT_MODE, 0x0c);
9366 GET_REG32_1(DMAC_MODE);
9367 GET_REG32_LOOP(GRC_MODE, 0x4c);
9368 if (tp->tg3_flags & TG3_FLAG_NVRAM)
9369 GET_REG32_LOOP(NVRAM_CMD, 0x24);
9370
9371#undef __GET_REG32
9372#undef GET_REG32_LOOP
9373#undef GET_REG32_1
9374
David S. Millerf47c11e2005-06-24 20:18:35 -07009375 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009376}
9377
9378static int tg3_get_eeprom_len(struct net_device *dev)
9379{
9380 struct tg3 *tp = netdev_priv(dev);
9381
9382 return tp->nvram_size;
9383}
9384
Linus Torvalds1da177e2005-04-16 15:20:36 -07009385static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9386{
9387 struct tg3 *tp = netdev_priv(dev);
9388 int ret;
9389 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -08009390 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009391 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009392
Matt Carlsondf259d82009-04-20 06:57:14 +00009393 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM)
9394 return -EINVAL;
9395
Michael Chanbc1c7562006-03-20 17:48:03 -08009396 if (tp->link_config.phy_is_low_power)
9397 return -EAGAIN;
9398
Linus Torvalds1da177e2005-04-16 15:20:36 -07009399 offset = eeprom->offset;
9400 len = eeprom->len;
9401 eeprom->len = 0;
9402
9403 eeprom->magic = TG3_EEPROM_MAGIC;
9404
9405 if (offset & 3) {
9406 /* adjustments to start on required 4 byte boundary */
9407 b_offset = offset & 3;
9408 b_count = 4 - b_offset;
9409 if (b_count > len) {
9410 /* i.e. offset=1 len=2 */
9411 b_count = len;
9412 }
Matt Carlsona9dc5292009-02-25 14:25:30 +00009413 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009414 if (ret)
9415 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009416 memcpy(data, ((char*)&val) + b_offset, b_count);
9417 len -= b_count;
9418 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00009419 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009420 }
9421
9422 /* read bytes upto the last 4 byte boundary */
9423 pd = &data[eeprom->len];
9424 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +00009425 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009426 if (ret) {
9427 eeprom->len += i;
9428 return ret;
9429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009430 memcpy(pd + i, &val, 4);
9431 }
9432 eeprom->len += i;
9433
9434 if (len & 3) {
9435 /* read last bytes not ending on 4 byte boundary */
9436 pd = &data[eeprom->len];
9437 b_count = len & 3;
9438 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009439 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009440 if (ret)
9441 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009442 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009443 eeprom->len += b_count;
9444 }
9445 return 0;
9446}
9447
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009448static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009449
9450static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9451{
9452 struct tg3 *tp = netdev_priv(dev);
9453 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009454 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009455 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009456 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009457
Michael Chanbc1c7562006-03-20 17:48:03 -08009458 if (tp->link_config.phy_is_low_power)
9459 return -EAGAIN;
9460
Matt Carlsondf259d82009-04-20 06:57:14 +00009461 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
9462 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009463 return -EINVAL;
9464
9465 offset = eeprom->offset;
9466 len = eeprom->len;
9467
9468 if ((b_offset = (offset & 3))) {
9469 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +00009470 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009471 if (ret)
9472 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009473 len += b_offset;
9474 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -07009475 if (len < 4)
9476 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009477 }
9478
9479 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -07009480 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009481 /* adjustments to end on required 4 byte boundary */
9482 odd_len = 1;
9483 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009484 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009485 if (ret)
9486 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009487 }
9488
9489 buf = data;
9490 if (b_offset || odd_len) {
9491 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009492 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009493 return -ENOMEM;
9494 if (b_offset)
9495 memcpy(buf, &start, 4);
9496 if (odd_len)
9497 memcpy(buf+len-4, &end, 4);
9498 memcpy(buf + b_offset, data, eeprom->len);
9499 }
9500
9501 ret = tg3_nvram_write_block(tp, offset, len, buf);
9502
9503 if (buf != data)
9504 kfree(buf);
9505
9506 return ret;
9507}
9508
9509static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9510{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009511 struct tg3 *tp = netdev_priv(dev);
9512
9513 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009514 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009515 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
9516 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009517 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
9518 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009519 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009520
Linus Torvalds1da177e2005-04-16 15:20:36 -07009521 cmd->supported = (SUPPORTED_Autoneg);
9522
9523 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY))
9524 cmd->supported |= (SUPPORTED_1000baseT_Half |
9525 SUPPORTED_1000baseT_Full);
9526
Karsten Keilef348142006-05-12 12:49:08 -07009527 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009528 cmd->supported |= (SUPPORTED_100baseT_Half |
9529 SUPPORTED_100baseT_Full |
9530 SUPPORTED_10baseT_Half |
9531 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -08009532 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -07009533 cmd->port = PORT_TP;
9534 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009535 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -07009536 cmd->port = PORT_FIBRE;
9537 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009538
Linus Torvalds1da177e2005-04-16 15:20:36 -07009539 cmd->advertising = tp->link_config.advertising;
9540 if (netif_running(dev)) {
9541 cmd->speed = tp->link_config.active_speed;
9542 cmd->duplex = tp->link_config.active_duplex;
9543 }
Matt Carlson882e9792009-09-01 13:21:36 +00009544 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +00009545 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009546 cmd->autoneg = tp->link_config.autoneg;
9547 cmd->maxtxpkt = 0;
9548 cmd->maxrxpkt = 0;
9549 return 0;
9550}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009551
Linus Torvalds1da177e2005-04-16 15:20:36 -07009552static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9553{
9554 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009555
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009556 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009557 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009558 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
9559 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009560 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
9561 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009562 }
9563
Matt Carlson7e5856b2009-02-25 14:23:01 +00009564 if (cmd->autoneg != AUTONEG_ENABLE &&
9565 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -07009566 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +00009567
9568 if (cmd->autoneg == AUTONEG_DISABLE &&
9569 cmd->duplex != DUPLEX_FULL &&
9570 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -07009571 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009572
Matt Carlson7e5856b2009-02-25 14:23:01 +00009573 if (cmd->autoneg == AUTONEG_ENABLE) {
9574 u32 mask = ADVERTISED_Autoneg |
9575 ADVERTISED_Pause |
9576 ADVERTISED_Asym_Pause;
9577
Julia Lawall3f07d122010-03-13 12:22:16 -08009578 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +00009579 mask |= ADVERTISED_1000baseT_Half |
9580 ADVERTISED_1000baseT_Full;
9581
9582 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES))
9583 mask |= ADVERTISED_100baseT_Half |
9584 ADVERTISED_100baseT_Full |
9585 ADVERTISED_10baseT_Half |
9586 ADVERTISED_10baseT_Full |
9587 ADVERTISED_TP;
9588 else
9589 mask |= ADVERTISED_FIBRE;
9590
9591 if (cmd->advertising & ~mask)
9592 return -EINVAL;
9593
9594 mask &= (ADVERTISED_1000baseT_Half |
9595 ADVERTISED_1000baseT_Full |
9596 ADVERTISED_100baseT_Half |
9597 ADVERTISED_100baseT_Full |
9598 ADVERTISED_10baseT_Half |
9599 ADVERTISED_10baseT_Full);
9600
9601 cmd->advertising &= mask;
9602 } else {
9603 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES) {
9604 if (cmd->speed != SPEED_1000)
9605 return -EINVAL;
9606
9607 if (cmd->duplex != DUPLEX_FULL)
9608 return -EINVAL;
9609 } else {
9610 if (cmd->speed != SPEED_100 &&
9611 cmd->speed != SPEED_10)
9612 return -EINVAL;
9613 }
9614 }
9615
David S. Millerf47c11e2005-06-24 20:18:35 -07009616 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009617
9618 tp->link_config.autoneg = cmd->autoneg;
9619 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -07009620 tp->link_config.advertising = (cmd->advertising |
9621 ADVERTISED_Autoneg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009622 tp->link_config.speed = SPEED_INVALID;
9623 tp->link_config.duplex = DUPLEX_INVALID;
9624 } else {
9625 tp->link_config.advertising = 0;
9626 tp->link_config.speed = cmd->speed;
9627 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009628 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009629
Michael Chan24fcad62006-12-17 17:06:46 -08009630 tp->link_config.orig_speed = tp->link_config.speed;
9631 tp->link_config.orig_duplex = tp->link_config.duplex;
9632 tp->link_config.orig_autoneg = tp->link_config.autoneg;
9633
Linus Torvalds1da177e2005-04-16 15:20:36 -07009634 if (netif_running(dev))
9635 tg3_setup_phy(tp, 1);
9636
David S. Millerf47c11e2005-06-24 20:18:35 -07009637 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009638
Linus Torvalds1da177e2005-04-16 15:20:36 -07009639 return 0;
9640}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009641
Linus Torvalds1da177e2005-04-16 15:20:36 -07009642static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
9643{
9644 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009645
Linus Torvalds1da177e2005-04-16 15:20:36 -07009646 strcpy(info->driver, DRV_MODULE_NAME);
9647 strcpy(info->version, DRV_MODULE_VERSION);
Michael Chanc4e65752006-03-20 22:29:32 -08009648 strcpy(info->fw_version, tp->fw_ver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009649 strcpy(info->bus_info, pci_name(tp->pdev));
9650}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009651
Linus Torvalds1da177e2005-04-16 15:20:36 -07009652static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
9653{
9654 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009655
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009656 if ((tp->tg3_flags & TG3_FLAG_WOL_CAP) &&
9657 device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -07009658 wol->supported = WAKE_MAGIC;
9659 else
9660 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009661 wol->wolopts = 0;
Matt Carlson05ac4cb2008-11-03 16:53:46 -08009662 if ((tp->tg3_flags & TG3_FLAG_WOL_ENABLE) &&
9663 device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009664 wol->wolopts = WAKE_MAGIC;
9665 memset(&wol->sopass, 0, sizeof(wol->sopass));
9666}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009667
Linus Torvalds1da177e2005-04-16 15:20:36 -07009668static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
9669{
9670 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009671 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009672
Linus Torvalds1da177e2005-04-16 15:20:36 -07009673 if (wol->wolopts & ~WAKE_MAGIC)
9674 return -EINVAL;
9675 if ((wol->wolopts & WAKE_MAGIC) &&
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009676 !((tp->tg3_flags & TG3_FLAG_WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009677 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009678
David S. Millerf47c11e2005-06-24 20:18:35 -07009679 spin_lock_bh(&tp->lock);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009680 if (wol->wolopts & WAKE_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009681 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009682 device_set_wakeup_enable(dp, true);
9683 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009684 tp->tg3_flags &= ~TG3_FLAG_WOL_ENABLE;
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009685 device_set_wakeup_enable(dp, false);
9686 }
David S. Millerf47c11e2005-06-24 20:18:35 -07009687 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009688
Linus Torvalds1da177e2005-04-16 15:20:36 -07009689 return 0;
9690}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009691
Linus Torvalds1da177e2005-04-16 15:20:36 -07009692static u32 tg3_get_msglevel(struct net_device *dev)
9693{
9694 struct tg3 *tp = netdev_priv(dev);
9695 return tp->msg_enable;
9696}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009697
Linus Torvalds1da177e2005-04-16 15:20:36 -07009698static void tg3_set_msglevel(struct net_device *dev, u32 value)
9699{
9700 struct tg3 *tp = netdev_priv(dev);
9701 tp->msg_enable = value;
9702}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009703
Linus Torvalds1da177e2005-04-16 15:20:36 -07009704static int tg3_set_tso(struct net_device *dev, u32 value)
9705{
9706 struct tg3 *tp = netdev_priv(dev);
9707
9708 if (!(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
9709 if (value)
9710 return -EINVAL;
9711 return 0;
9712 }
Matt Carlson027455a2008-12-21 20:19:30 -08009713 if ((dev->features & NETIF_F_IPV6_CSUM) &&
Matt Carlsone849cdc2009-11-13 13:03:38 +00009714 ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_2) ||
9715 (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3))) {
Matt Carlson9936bcf2007-10-10 18:03:07 -07009716 if (value) {
Michael Chanb0026622006-07-03 19:42:14 -07009717 dev->features |= NETIF_F_TSO6;
Matt Carlsone849cdc2009-11-13 13:03:38 +00009718 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
9719 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -07009720 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
9721 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Matt Carlson321d32a2008-11-21 17:22:19 -08009722 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsone849cdc2009-11-13 13:03:38 +00009723 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson9936bcf2007-10-10 18:03:07 -07009724 dev->features |= NETIF_F_TSO_ECN;
9725 } else
9726 dev->features &= ~(NETIF_F_TSO6 | NETIF_F_TSO_ECN);
Michael Chanb0026622006-07-03 19:42:14 -07009727 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009728 return ethtool_op_set_tso(dev, value);
9729}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009730
Linus Torvalds1da177e2005-04-16 15:20:36 -07009731static int tg3_nway_reset(struct net_device *dev)
9732{
9733 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009734 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009735
Linus Torvalds1da177e2005-04-16 15:20:36 -07009736 if (!netif_running(dev))
9737 return -EAGAIN;
9738
Michael Chanc94e3942005-09-27 12:12:42 -07009739 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
9740 return -EINVAL;
9741
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009742 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
9743 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
9744 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009745 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009746 } else {
9747 u32 bmcr;
9748
9749 spin_lock_bh(&tp->lock);
9750 r = -EINVAL;
9751 tg3_readphy(tp, MII_BMCR, &bmcr);
9752 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
9753 ((bmcr & BMCR_ANENABLE) ||
9754 (tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT))) {
9755 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
9756 BMCR_ANENABLE);
9757 r = 0;
9758 }
9759 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009760 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009761
Linus Torvalds1da177e2005-04-16 15:20:36 -07009762 return r;
9763}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009764
Linus Torvalds1da177e2005-04-16 15:20:36 -07009765static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
9766{
9767 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009768
Linus Torvalds1da177e2005-04-16 15:20:36 -07009769 ering->rx_max_pending = TG3_RX_RING_SIZE - 1;
9770 ering->rx_mini_max_pending = 0;
Michael Chan4f81c322006-03-20 21:33:42 -08009771 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE)
9772 ering->rx_jumbo_max_pending = TG3_RX_JUMBO_RING_SIZE - 1;
9773 else
9774 ering->rx_jumbo_max_pending = 0;
9775
9776 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009777
9778 ering->rx_pending = tp->rx_pending;
9779 ering->rx_mini_pending = 0;
Michael Chan4f81c322006-03-20 21:33:42 -08009780 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE)
9781 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
9782 else
9783 ering->rx_jumbo_pending = 0;
9784
Matt Carlsonf3f3f272009-08-28 14:03:21 +00009785 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009786}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009787
Linus Torvalds1da177e2005-04-16 15:20:36 -07009788static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
9789{
9790 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +00009791 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009792
Linus Torvalds1da177e2005-04-16 15:20:36 -07009793 if ((ering->rx_pending > TG3_RX_RING_SIZE - 1) ||
9794 (ering->rx_jumbo_pending > TG3_RX_JUMBO_RING_SIZE - 1) ||
Michael Chanbc3a9252006-10-18 20:55:18 -07009795 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
9796 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Michael Chan7f62ad52007-02-20 23:25:40 -08009797 ((tp->tg3_flags2 & TG3_FLG2_TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -07009798 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009799 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009800
Michael Chanbbe832c2005-06-24 20:20:04 -07009801 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009802 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009803 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -07009804 irq_sync = 1;
9805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009806
Michael Chanbbe832c2005-06-24 20:20:04 -07009807 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009808
Linus Torvalds1da177e2005-04-16 15:20:36 -07009809 tp->rx_pending = ering->rx_pending;
9810
9811 if ((tp->tg3_flags2 & TG3_FLG2_MAX_RXPEND_64) &&
9812 tp->rx_pending > 63)
9813 tp->rx_pending = 63;
9814 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +00009815
9816 for (i = 0; i < TG3_IRQ_MAX_VECS; i++)
9817 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009818
9819 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -07009820 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -07009821 err = tg3_restart_hw(tp, 1);
9822 if (!err)
9823 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009824 }
9825
David S. Millerf47c11e2005-06-24 20:18:35 -07009826 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009827
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009828 if (irq_sync && !err)
9829 tg3_phy_start(tp);
9830
Michael Chanb9ec6c12006-07-25 16:37:27 -07009831 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009832}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009833
Linus Torvalds1da177e2005-04-16 15:20:36 -07009834static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
9835{
9836 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009837
Linus Torvalds1da177e2005-04-16 15:20:36 -07009838 epause->autoneg = (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG) != 0;
Matt Carlson8d018622007-12-20 20:05:44 -08009839
Steve Glendinninge18ce342008-12-16 02:00:00 -08009840 if (tp->link_config.active_flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -08009841 epause->rx_pause = 1;
9842 else
9843 epause->rx_pause = 0;
9844
Steve Glendinninge18ce342008-12-16 02:00:00 -08009845 if (tp->link_config.active_flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -08009846 epause->tx_pause = 1;
9847 else
9848 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009849}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009850
Linus Torvalds1da177e2005-04-16 15:20:36 -07009851static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
9852{
9853 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009854 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009855
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009856 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson27121682010-02-17 15:16:57 +00009857 u32 newadv;
9858 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009859
Matt Carlson27121682010-02-17 15:16:57 +00009860 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009861
Matt Carlson27121682010-02-17 15:16:57 +00009862 if (!(phydev->supported & SUPPORTED_Pause) ||
9863 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
9864 ((epause->rx_pause && !epause->tx_pause) ||
9865 (!epause->rx_pause && epause->tx_pause))))
9866 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009867
Matt Carlson27121682010-02-17 15:16:57 +00009868 tp->link_config.flowctrl = 0;
9869 if (epause->rx_pause) {
9870 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009871
Matt Carlson27121682010-02-17 15:16:57 +00009872 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -08009873 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +00009874 newadv = ADVERTISED_Pause;
9875 } else
9876 newadv = ADVERTISED_Pause |
9877 ADVERTISED_Asym_Pause;
9878 } else if (epause->tx_pause) {
9879 tp->link_config.flowctrl |= FLOW_CTRL_TX;
9880 newadv = ADVERTISED_Asym_Pause;
9881 } else
9882 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009883
Matt Carlson27121682010-02-17 15:16:57 +00009884 if (epause->autoneg)
9885 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
9886 else
9887 tp->tg3_flags &= ~TG3_FLAG_PAUSE_AUTONEG;
9888
9889 if (tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED) {
9890 u32 oldadv = phydev->advertising &
9891 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
9892 if (oldadv != newadv) {
9893 phydev->advertising &=
9894 ~(ADVERTISED_Pause |
9895 ADVERTISED_Asym_Pause);
9896 phydev->advertising |= newadv;
9897 if (phydev->autoneg) {
9898 /*
9899 * Always renegotiate the link to
9900 * inform our link partner of our
9901 * flow control settings, even if the
9902 * flow control is forced. Let
9903 * tg3_adjust_link() do the final
9904 * flow control setup.
9905 */
9906 return phy_start_aneg(phydev);
9907 }
9908 }
9909
9910 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009911 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +00009912 } else {
9913 tp->link_config.orig_advertising &=
9914 ~(ADVERTISED_Pause |
9915 ADVERTISED_Asym_Pause);
9916 tp->link_config.orig_advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009917 }
9918 } else {
9919 int irq_sync = 0;
9920
9921 if (netif_running(dev)) {
9922 tg3_netif_stop(tp);
9923 irq_sync = 1;
9924 }
9925
9926 tg3_full_lock(tp, irq_sync);
9927
9928 if (epause->autoneg)
9929 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
9930 else
9931 tp->tg3_flags &= ~TG3_FLAG_PAUSE_AUTONEG;
9932 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -08009933 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009934 else
Steve Glendinninge18ce342008-12-16 02:00:00 -08009935 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009936 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -08009937 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009938 else
Steve Glendinninge18ce342008-12-16 02:00:00 -08009939 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009940
9941 if (netif_running(dev)) {
9942 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
9943 err = tg3_restart_hw(tp, 1);
9944 if (!err)
9945 tg3_netif_start(tp);
9946 }
9947
9948 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -07009949 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009950
Michael Chanb9ec6c12006-07-25 16:37:27 -07009951 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009952}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009953
Linus Torvalds1da177e2005-04-16 15:20:36 -07009954static u32 tg3_get_rx_csum(struct net_device *dev)
9955{
9956 struct tg3 *tp = netdev_priv(dev);
9957 return (tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) != 0;
9958}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009959
Linus Torvalds1da177e2005-04-16 15:20:36 -07009960static int tg3_set_rx_csum(struct net_device *dev, u32 data)
9961{
9962 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009963
Linus Torvalds1da177e2005-04-16 15:20:36 -07009964 if (tp->tg3_flags & TG3_FLAG_BROKEN_CHECKSUMS) {
9965 if (data != 0)
9966 return -EINVAL;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00009967 return 0;
9968 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009969
David S. Millerf47c11e2005-06-24 20:18:35 -07009970 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009971 if (data)
9972 tp->tg3_flags |= TG3_FLAG_RX_CHECKSUMS;
9973 else
9974 tp->tg3_flags &= ~TG3_FLAG_RX_CHECKSUMS;
David S. Millerf47c11e2005-06-24 20:18:35 -07009975 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009976
Linus Torvalds1da177e2005-04-16 15:20:36 -07009977 return 0;
9978}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009979
Linus Torvalds1da177e2005-04-16 15:20:36 -07009980static int tg3_set_tx_csum(struct net_device *dev, u32 data)
9981{
9982 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009983
Linus Torvalds1da177e2005-04-16 15:20:36 -07009984 if (tp->tg3_flags & TG3_FLAG_BROKEN_CHECKSUMS) {
9985 if (data != 0)
9986 return -EINVAL;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00009987 return 0;
9988 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009989
Matt Carlson321d32a2008-11-21 17:22:19 -08009990 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Michael Chan6460d942007-07-14 19:07:52 -07009991 ethtool_op_set_tx_ipv6_csum(dev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009992 else
Michael Chan9c27dbd2006-03-20 22:28:27 -08009993 ethtool_op_set_tx_csum(dev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009994
9995 return 0;
9996}
9997
Jeff Garzikb9f2c042007-10-03 18:07:32 -07009998static int tg3_get_sset_count (struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009999{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010000 switch (sset) {
10001 case ETH_SS_TEST:
10002 return TG3_NUM_TEST;
10003 case ETH_SS_STATS:
10004 return TG3_NUM_STATS;
10005 default:
10006 return -EOPNOTSUPP;
10007 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070010008}
10009
Linus Torvalds1da177e2005-04-16 15:20:36 -070010010static void tg3_get_strings (struct net_device *dev, u32 stringset, u8 *buf)
10011{
10012 switch (stringset) {
10013 case ETH_SS_STATS:
10014 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
10015 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070010016 case ETH_SS_TEST:
10017 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
10018 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010019 default:
10020 WARN_ON(1); /* we need a WARN() */
10021 break;
10022 }
10023}
10024
Michael Chan4009a932005-09-05 17:52:54 -070010025static int tg3_phys_id(struct net_device *dev, u32 data)
10026{
10027 struct tg3 *tp = netdev_priv(dev);
10028 int i;
10029
10030 if (!netif_running(tp->dev))
10031 return -EAGAIN;
10032
10033 if (data == 0)
Stephen Hemminger759afc32008-02-23 19:51:59 -080010034 data = UINT_MAX / 2;
Michael Chan4009a932005-09-05 17:52:54 -070010035
10036 for (i = 0; i < (data * 2); i++) {
10037 if ((i % 2) == 0)
10038 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10039 LED_CTRL_1000MBPS_ON |
10040 LED_CTRL_100MBPS_ON |
10041 LED_CTRL_10MBPS_ON |
10042 LED_CTRL_TRAFFIC_OVERRIDE |
10043 LED_CTRL_TRAFFIC_BLINK |
10044 LED_CTRL_TRAFFIC_LED);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010045
Michael Chan4009a932005-09-05 17:52:54 -070010046 else
10047 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10048 LED_CTRL_TRAFFIC_OVERRIDE);
10049
10050 if (msleep_interruptible(500))
10051 break;
10052 }
10053 tw32(MAC_LED_CTRL, tp->led_ctrl);
10054 return 0;
10055}
10056
Linus Torvalds1da177e2005-04-16 15:20:36 -070010057static void tg3_get_ethtool_stats (struct net_device *dev,
10058 struct ethtool_stats *estats, u64 *tmp_stats)
10059{
10060 struct tg3 *tp = netdev_priv(dev);
10061 memcpy(tmp_stats, tg3_get_estats(tp), sizeof(tp->estats));
10062}
10063
Michael Chan566f86a2005-05-29 14:56:58 -070010064#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080010065#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
10066#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
10067#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Michael Chanb16250e2006-09-27 16:10:14 -070010068#define NVRAM_SELFBOOT_HW_SIZE 0x20
10069#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070010070
10071static int tg3_test_nvram(struct tg3 *tp)
10072{
Al Virob9fc7dc2007-12-17 22:59:57 -080010073 u32 csum, magic;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010074 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010075 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070010076
Matt Carlsondf259d82009-04-20 06:57:14 +000010077 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM)
10078 return 0;
10079
Matt Carlsone4f34112009-02-25 14:25:00 +000010080 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080010081 return -EIO;
10082
Michael Chan1b277772006-03-20 22:27:48 -080010083 if (magic == TG3_EEPROM_MAGIC)
10084 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070010085 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080010086 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
10087 TG3_EEPROM_SB_FORMAT_1) {
10088 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
10089 case TG3_EEPROM_SB_REVISION_0:
10090 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
10091 break;
10092 case TG3_EEPROM_SB_REVISION_2:
10093 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
10094 break;
10095 case TG3_EEPROM_SB_REVISION_3:
10096 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
10097 break;
10098 default:
10099 return 0;
10100 }
10101 } else
Michael Chan1b277772006-03-20 22:27:48 -080010102 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070010103 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
10104 size = NVRAM_SELFBOOT_HW_SIZE;
10105 else
Michael Chan1b277772006-03-20 22:27:48 -080010106 return -EIO;
10107
10108 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070010109 if (buf == NULL)
10110 return -ENOMEM;
10111
Michael Chan1b277772006-03-20 22:27:48 -080010112 err = -EIO;
10113 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010114 err = tg3_nvram_read_be32(tp, i, &buf[j]);
10115 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070010116 break;
Michael Chan566f86a2005-05-29 14:56:58 -070010117 }
Michael Chan1b277772006-03-20 22:27:48 -080010118 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070010119 goto out;
10120
Michael Chan1b277772006-03-20 22:27:48 -080010121 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010122 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080010123 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010124 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080010125 u8 *buf8 = (u8 *) buf, csum8 = 0;
10126
Al Virob9fc7dc2007-12-17 22:59:57 -080010127 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080010128 TG3_EEPROM_SB_REVISION_2) {
10129 /* For rev 2, the csum doesn't include the MBA. */
10130 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
10131 csum8 += buf8[i];
10132 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
10133 csum8 += buf8[i];
10134 } else {
10135 for (i = 0; i < size; i++)
10136 csum8 += buf8[i];
10137 }
Michael Chan1b277772006-03-20 22:27:48 -080010138
Adrian Bunkad96b482006-04-05 22:21:04 -070010139 if (csum8 == 0) {
10140 err = 0;
10141 goto out;
10142 }
10143
10144 err = -EIO;
10145 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080010146 }
Michael Chan566f86a2005-05-29 14:56:58 -070010147
Al Virob9fc7dc2007-12-17 22:59:57 -080010148 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010149 TG3_EEPROM_MAGIC_HW) {
10150 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000010151 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070010152 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070010153
10154 /* Separate the parity bits and the data bytes. */
10155 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
10156 if ((i == 0) || (i == 8)) {
10157 int l;
10158 u8 msk;
10159
10160 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
10161 parity[k++] = buf8[i] & msk;
10162 i++;
Matt Carlson859a588792010-04-05 10:19:28 +000010163 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070010164 int l;
10165 u8 msk;
10166
10167 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
10168 parity[k++] = buf8[i] & msk;
10169 i++;
10170
10171 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
10172 parity[k++] = buf8[i] & msk;
10173 i++;
10174 }
10175 data[j++] = buf8[i];
10176 }
10177
10178 err = -EIO;
10179 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
10180 u8 hw8 = hweight8(data[i]);
10181
10182 if ((hw8 & 0x1) && parity[i])
10183 goto out;
10184 else if (!(hw8 & 0x1) && !parity[i])
10185 goto out;
10186 }
10187 err = 0;
10188 goto out;
10189 }
10190
Michael Chan566f86a2005-05-29 14:56:58 -070010191 /* Bootstrap checksum at offset 0x10 */
10192 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlsona9dc5292009-02-25 14:25:30 +000010193 if (csum != be32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070010194 goto out;
10195
10196 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
10197 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlsona9dc5292009-02-25 14:25:30 +000010198 if (csum != be32_to_cpu(buf[0xfc/4]))
10199 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070010200
10201 err = 0;
10202
10203out:
10204 kfree(buf);
10205 return err;
10206}
10207
Michael Chanca430072005-05-29 14:57:23 -070010208#define TG3_SERDES_TIMEOUT_SEC 2
10209#define TG3_COPPER_TIMEOUT_SEC 6
10210
10211static int tg3_test_link(struct tg3 *tp)
10212{
10213 int i, max;
10214
10215 if (!netif_running(tp->dev))
10216 return -ENODEV;
10217
Michael Chan4c987482005-09-05 17:52:38 -070010218 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070010219 max = TG3_SERDES_TIMEOUT_SEC;
10220 else
10221 max = TG3_COPPER_TIMEOUT_SEC;
10222
10223 for (i = 0; i < max; i++) {
10224 if (netif_carrier_ok(tp->dev))
10225 return 0;
10226
10227 if (msleep_interruptible(1000))
10228 break;
10229 }
10230
10231 return -EIO;
10232}
10233
Michael Chana71116d2005-05-29 14:58:11 -070010234/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080010235static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070010236{
Michael Chanb16250e2006-09-27 16:10:14 -070010237 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070010238 u32 offset, read_mask, write_mask, val, save_val, read_val;
10239 static struct {
10240 u16 offset;
10241 u16 flags;
10242#define TG3_FL_5705 0x1
10243#define TG3_FL_NOT_5705 0x2
10244#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070010245#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070010246 u32 read_mask;
10247 u32 write_mask;
10248 } reg_tbl[] = {
10249 /* MAC Control Registers */
10250 { MAC_MODE, TG3_FL_NOT_5705,
10251 0x00000000, 0x00ef6f8c },
10252 { MAC_MODE, TG3_FL_5705,
10253 0x00000000, 0x01ef6b8c },
10254 { MAC_STATUS, TG3_FL_NOT_5705,
10255 0x03800107, 0x00000000 },
10256 { MAC_STATUS, TG3_FL_5705,
10257 0x03800100, 0x00000000 },
10258 { MAC_ADDR_0_HIGH, 0x0000,
10259 0x00000000, 0x0000ffff },
10260 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010261 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070010262 { MAC_RX_MTU_SIZE, 0x0000,
10263 0x00000000, 0x0000ffff },
10264 { MAC_TX_MODE, 0x0000,
10265 0x00000000, 0x00000070 },
10266 { MAC_TX_LENGTHS, 0x0000,
10267 0x00000000, 0x00003fff },
10268 { MAC_RX_MODE, TG3_FL_NOT_5705,
10269 0x00000000, 0x000007fc },
10270 { MAC_RX_MODE, TG3_FL_5705,
10271 0x00000000, 0x000007dc },
10272 { MAC_HASH_REG_0, 0x0000,
10273 0x00000000, 0xffffffff },
10274 { MAC_HASH_REG_1, 0x0000,
10275 0x00000000, 0xffffffff },
10276 { MAC_HASH_REG_2, 0x0000,
10277 0x00000000, 0xffffffff },
10278 { MAC_HASH_REG_3, 0x0000,
10279 0x00000000, 0xffffffff },
10280
10281 /* Receive Data and Receive BD Initiator Control Registers. */
10282 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
10283 0x00000000, 0xffffffff },
10284 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
10285 0x00000000, 0xffffffff },
10286 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
10287 0x00000000, 0x00000003 },
10288 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
10289 0x00000000, 0xffffffff },
10290 { RCVDBDI_STD_BD+0, 0x0000,
10291 0x00000000, 0xffffffff },
10292 { RCVDBDI_STD_BD+4, 0x0000,
10293 0x00000000, 0xffffffff },
10294 { RCVDBDI_STD_BD+8, 0x0000,
10295 0x00000000, 0xffff0002 },
10296 { RCVDBDI_STD_BD+0xc, 0x0000,
10297 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010298
Michael Chana71116d2005-05-29 14:58:11 -070010299 /* Receive BD Initiator Control Registers. */
10300 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
10301 0x00000000, 0xffffffff },
10302 { RCVBDI_STD_THRESH, TG3_FL_5705,
10303 0x00000000, 0x000003ff },
10304 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
10305 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010306
Michael Chana71116d2005-05-29 14:58:11 -070010307 /* Host Coalescing Control Registers. */
10308 { HOSTCC_MODE, TG3_FL_NOT_5705,
10309 0x00000000, 0x00000004 },
10310 { HOSTCC_MODE, TG3_FL_5705,
10311 0x00000000, 0x000000f6 },
10312 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
10313 0x00000000, 0xffffffff },
10314 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
10315 0x00000000, 0x000003ff },
10316 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
10317 0x00000000, 0xffffffff },
10318 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
10319 0x00000000, 0x000003ff },
10320 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
10321 0x00000000, 0xffffffff },
10322 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10323 0x00000000, 0x000000ff },
10324 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
10325 0x00000000, 0xffffffff },
10326 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10327 0x00000000, 0x000000ff },
10328 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
10329 0x00000000, 0xffffffff },
10330 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
10331 0x00000000, 0xffffffff },
10332 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10333 0x00000000, 0xffffffff },
10334 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10335 0x00000000, 0x000000ff },
10336 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10337 0x00000000, 0xffffffff },
10338 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10339 0x00000000, 0x000000ff },
10340 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
10341 0x00000000, 0xffffffff },
10342 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
10343 0x00000000, 0xffffffff },
10344 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
10345 0x00000000, 0xffffffff },
10346 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
10347 0x00000000, 0xffffffff },
10348 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
10349 0x00000000, 0xffffffff },
10350 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
10351 0xffffffff, 0x00000000 },
10352 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
10353 0xffffffff, 0x00000000 },
10354
10355 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070010356 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010357 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070010358 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010359 0x00000000, 0x007fffff },
10360 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
10361 0x00000000, 0x0000003f },
10362 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
10363 0x00000000, 0x000001ff },
10364 { BUFMGR_MB_HIGH_WATER, 0x0000,
10365 0x00000000, 0x000001ff },
10366 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
10367 0xffffffff, 0x00000000 },
10368 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
10369 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010370
Michael Chana71116d2005-05-29 14:58:11 -070010371 /* Mailbox Registers */
10372 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
10373 0x00000000, 0x000001ff },
10374 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
10375 0x00000000, 0x000001ff },
10376 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
10377 0x00000000, 0x000007ff },
10378 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
10379 0x00000000, 0x000001ff },
10380
10381 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
10382 };
10383
Michael Chanb16250e2006-09-27 16:10:14 -070010384 is_5705 = is_5750 = 0;
10385 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
Michael Chana71116d2005-05-29 14:58:11 -070010386 is_5705 = 1;
Michael Chanb16250e2006-09-27 16:10:14 -070010387 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
10388 is_5750 = 1;
10389 }
Michael Chana71116d2005-05-29 14:58:11 -070010390
10391 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
10392 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
10393 continue;
10394
10395 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
10396 continue;
10397
10398 if ((tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
10399 (reg_tbl[i].flags & TG3_FL_NOT_5788))
10400 continue;
10401
Michael Chanb16250e2006-09-27 16:10:14 -070010402 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
10403 continue;
10404
Michael Chana71116d2005-05-29 14:58:11 -070010405 offset = (u32) reg_tbl[i].offset;
10406 read_mask = reg_tbl[i].read_mask;
10407 write_mask = reg_tbl[i].write_mask;
10408
10409 /* Save the original register content */
10410 save_val = tr32(offset);
10411
10412 /* Determine the read-only value. */
10413 read_val = save_val & read_mask;
10414
10415 /* Write zero to the register, then make sure the read-only bits
10416 * are not changed and the read/write bits are all zeros.
10417 */
10418 tw32(offset, 0);
10419
10420 val = tr32(offset);
10421
10422 /* Test the read-only and read/write bits. */
10423 if (((val & read_mask) != read_val) || (val & write_mask))
10424 goto out;
10425
10426 /* Write ones to all the bits defined by RdMask and WrMask, then
10427 * make sure the read-only bits are not changed and the
10428 * read/write bits are all ones.
10429 */
10430 tw32(offset, read_mask | write_mask);
10431
10432 val = tr32(offset);
10433
10434 /* Test the read-only bits. */
10435 if ((val & read_mask) != read_val)
10436 goto out;
10437
10438 /* Test the read/write bits. */
10439 if ((val & write_mask) != write_mask)
10440 goto out;
10441
10442 tw32(offset, save_val);
10443 }
10444
10445 return 0;
10446
10447out:
Michael Chan9f88f292006-12-07 00:22:54 -080010448 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000010449 netdev_err(tp->dev,
10450 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070010451 tw32(offset, save_val);
10452 return -EIO;
10453}
10454
Michael Chan7942e1d2005-05-29 14:58:36 -070010455static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
10456{
Arjan van de Venf71e1302006-03-03 21:33:57 -050010457 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070010458 int i;
10459 u32 j;
10460
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020010461 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070010462 for (j = 0; j < len; j += 4) {
10463 u32 val;
10464
10465 tg3_write_mem(tp, offset + j, test_pattern[i]);
10466 tg3_read_mem(tp, offset + j, &val);
10467 if (val != test_pattern[i])
10468 return -EIO;
10469 }
10470 }
10471 return 0;
10472}
10473
10474static int tg3_test_memory(struct tg3 *tp)
10475{
10476 static struct mem_entry {
10477 u32 offset;
10478 u32 len;
10479 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080010480 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070010481 { 0x00002000, 0x1c000},
10482 { 0xffffffff, 0x00000}
10483 }, mem_tbl_5705[] = {
10484 { 0x00000100, 0x0000c},
10485 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070010486 { 0x00004000, 0x00800},
10487 { 0x00006000, 0x01000},
10488 { 0x00008000, 0x02000},
10489 { 0x00010000, 0x0e000},
10490 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080010491 }, mem_tbl_5755[] = {
10492 { 0x00000200, 0x00008},
10493 { 0x00004000, 0x00800},
10494 { 0x00006000, 0x00800},
10495 { 0x00008000, 0x02000},
10496 { 0x00010000, 0x0c000},
10497 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070010498 }, mem_tbl_5906[] = {
10499 { 0x00000200, 0x00008},
10500 { 0x00004000, 0x00400},
10501 { 0x00006000, 0x00400},
10502 { 0x00008000, 0x01000},
10503 { 0x00010000, 0x01000},
10504 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000010505 }, mem_tbl_5717[] = {
10506 { 0x00000200, 0x00008},
10507 { 0x00010000, 0x0a000},
10508 { 0x00020000, 0x13c00},
10509 { 0xffffffff, 0x00000}
10510 }, mem_tbl_57765[] = {
10511 { 0x00000200, 0x00008},
10512 { 0x00004000, 0x00800},
10513 { 0x00006000, 0x09800},
10514 { 0x00010000, 0x0a000},
10515 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070010516 };
10517 struct mem_entry *mem_tbl;
10518 int err = 0;
10519 int i;
10520
Matt Carlson8b5a6c42010-01-20 16:58:06 +000010521 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
10522 mem_tbl = mem_tbl_5717;
10523 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
10524 mem_tbl = mem_tbl_57765;
10525 else if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Matt Carlson321d32a2008-11-21 17:22:19 -080010526 mem_tbl = mem_tbl_5755;
10527 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
10528 mem_tbl = mem_tbl_5906;
10529 else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
10530 mem_tbl = mem_tbl_5705;
10531 else
Michael Chan7942e1d2005-05-29 14:58:36 -070010532 mem_tbl = mem_tbl_570x;
10533
10534 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
10535 if ((err = tg3_do_mem_test(tp, mem_tbl[i].offset,
10536 mem_tbl[i].len)) != 0)
10537 break;
10538 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010539
Michael Chan7942e1d2005-05-29 14:58:36 -070010540 return err;
10541}
10542
Michael Chan9f40dea2005-09-05 17:53:06 -070010543#define TG3_MAC_LOOPBACK 0
10544#define TG3_PHY_LOOPBACK 1
10545
10546static int tg3_run_loopback(struct tg3 *tp, int loopback_mode)
Michael Chanc76949a2005-05-29 14:58:59 -070010547{
Michael Chan9f40dea2005-09-05 17:53:06 -070010548 u32 mac_mode, rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010549 u32 desc_idx, coal_now;
Michael Chanc76949a2005-05-29 14:58:59 -070010550 struct sk_buff *skb, *rx_skb;
10551 u8 *tx_data;
10552 dma_addr_t map;
10553 int num_pkts, tx_len, rx_len, i, err;
10554 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000010555 struct tg3_napi *tnapi, *rnapi;
Matt Carlson21f581a2009-08-28 14:00:25 +000010556 struct tg3_rx_prodring_set *tpr = &tp->prodring[0];
Michael Chanc76949a2005-05-29 14:58:59 -070010557
Matt Carlsonc8873402010-02-12 14:47:11 +000010558 tnapi = &tp->napi[0];
10559 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000010560 if (tp->irq_cnt > 1) {
Matt Carlson0c1d0e22009-09-01 13:16:33 +000010561 rnapi = &tp->napi[1];
Matt Carlsonc8873402010-02-12 14:47:11 +000010562 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
10563 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000010564 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010565 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000010566
Michael Chan9f40dea2005-09-05 17:53:06 -070010567 if (loopback_mode == TG3_MAC_LOOPBACK) {
Michael Chanc94e3942005-09-27 12:12:42 -070010568 /* HW errata - mac loopback fails in some cases on 5780.
10569 * Normal traffic and PHY loopback are not affected by
10570 * errata.
10571 */
10572 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780)
10573 return 0;
10574
Michael Chan9f40dea2005-09-05 17:53:06 -070010575 mac_mode = (tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK) |
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010576 MAC_MODE_PORT_INT_LPBACK;
10577 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
10578 mac_mode |= MAC_MODE_LINK_POLARITY;
Michael Chan3f7045c2006-09-27 16:02:29 -070010579 if (tp->tg3_flags & TG3_FLAG_10_100_ONLY)
10580 mac_mode |= MAC_MODE_PORT_MODE_MII;
10581 else
10582 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chan9f40dea2005-09-05 17:53:06 -070010583 tw32(MAC_MODE, mac_mode);
10584 } else if (loopback_mode == TG3_PHY_LOOPBACK) {
Michael Chan3f7045c2006-09-27 16:02:29 -070010585 u32 val;
10586
Matt Carlson7f97a4b2009-08-25 10:10:03 +000010587 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
10588 tg3_phy_fet_toggle_apd(tp, false);
Michael Chan5d64ad32006-12-07 00:19:40 -080010589 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED100;
10590 } else
10591 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED1000;
Michael Chan3f7045c2006-09-27 16:02:29 -070010592
Matt Carlson9ef8ca92007-07-11 19:48:29 -070010593 tg3_phy_toggle_automdix(tp, 0);
10594
Michael Chan3f7045c2006-09-27 16:02:29 -070010595 tg3_writephy(tp, MII_BMCR, val);
Michael Chanc94e3942005-09-27 12:12:42 -070010596 udelay(40);
Michael Chan5d64ad32006-12-07 00:19:40 -080010597
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010598 mac_mode = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000010599 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
Matt Carlson1061b7c2010-02-12 14:47:12 +000010600 tg3_writephy(tp, MII_TG3_FET_PTEST,
10601 MII_TG3_FET_PTEST_FRC_TX_LINK |
10602 MII_TG3_FET_PTEST_FRC_TX_LOCK);
10603 /* The write needs to be flushed for the AC131 */
10604 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
10605 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
Michael Chan5d64ad32006-12-07 00:19:40 -080010606 mac_mode |= MAC_MODE_PORT_MODE_MII;
10607 } else
10608 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chanb16250e2006-09-27 16:10:14 -070010609
Michael Chanc94e3942005-09-27 12:12:42 -070010610 /* reset to prevent losing 1st rx packet intermittently */
10611 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES) {
10612 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
10613 udelay(10);
10614 tw32_f(MAC_RX_MODE, tp->rx_mode);
10615 }
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010616 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlson79eb6902010-02-17 15:17:03 +000010617 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
10618 if (masked_phy_id == TG3_PHY_ID_BCM5401)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010619 mac_mode &= ~MAC_MODE_LINK_POLARITY;
Matt Carlson79eb6902010-02-17 15:17:03 +000010620 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010621 mac_mode |= MAC_MODE_LINK_POLARITY;
Michael Chanff18ff02006-03-27 23:17:27 -080010622 tg3_writephy(tp, MII_TG3_EXT_CTRL,
10623 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
10624 }
Michael Chan9f40dea2005-09-05 17:53:06 -070010625 tw32(MAC_MODE, mac_mode);
Matt Carlson859a588792010-04-05 10:19:28 +000010626 } else {
Michael Chan9f40dea2005-09-05 17:53:06 -070010627 return -EINVAL;
Matt Carlson859a588792010-04-05 10:19:28 +000010628 }
Michael Chanc76949a2005-05-29 14:58:59 -070010629
10630 err = -EIO;
10631
Michael Chanc76949a2005-05-29 14:58:59 -070010632 tx_len = 1514;
David S. Millera20e9c62006-07-31 22:38:16 -070010633 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070010634 if (!skb)
10635 return -ENOMEM;
10636
Michael Chanc76949a2005-05-29 14:58:59 -070010637 tx_data = skb_put(skb, tx_len);
10638 memcpy(tx_data, tp->dev->dev_addr, 6);
10639 memset(tx_data + 6, 0x0, 8);
10640
10641 tw32(MAC_RX_MTU_SIZE, tx_len + 4);
10642
10643 for (i = 14; i < tx_len; i++)
10644 tx_data[i] = (u8) (i & 0xff);
10645
Alexander Duyckf4188d82009-12-02 16:48:38 +000010646 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
10647 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000010648 dev_kfree_skb(skb);
10649 return -EIO;
10650 }
Michael Chanc76949a2005-05-29 14:58:59 -070010651
10652 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010653 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070010654
10655 udelay(10);
10656
Matt Carlson898a56f2009-08-28 14:02:40 +000010657 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070010658
Michael Chanc76949a2005-05-29 14:58:59 -070010659 num_pkts = 0;
10660
Alexander Duyckf4188d82009-12-02 16:48:38 +000010661 tg3_set_txd(tnapi, tnapi->tx_prod, map, tx_len, 0, 1);
Michael Chanc76949a2005-05-29 14:58:59 -070010662
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010663 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070010664 num_pkts++;
10665
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010666 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
10667 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070010668
10669 udelay(10);
10670
Matt Carlson303fc922009-11-02 14:27:34 +000010671 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
10672 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070010673 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010674 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070010675
10676 udelay(10);
10677
Matt Carlson898a56f2009-08-28 14:02:40 +000010678 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
10679 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010680 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070010681 (rx_idx == (rx_start_idx + num_pkts)))
10682 break;
10683 }
10684
Alexander Duyckf4188d82009-12-02 16:48:38 +000010685 pci_unmap_single(tp->pdev, map, tx_len, PCI_DMA_TODEVICE);
Michael Chanc76949a2005-05-29 14:58:59 -070010686 dev_kfree_skb(skb);
10687
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010688 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070010689 goto out;
10690
10691 if (rx_idx != rx_start_idx + num_pkts)
10692 goto out;
10693
Matt Carlson72334482009-08-28 14:03:01 +000010694 desc = &rnapi->rx_rcb[rx_start_idx];
Michael Chanc76949a2005-05-29 14:58:59 -070010695 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
10696 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
10697 if (opaque_key != RXD_OPAQUE_RING_STD)
10698 goto out;
10699
10700 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
10701 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
10702 goto out;
10703
10704 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) - 4;
10705 if (rx_len != tx_len)
10706 goto out;
10707
Matt Carlson21f581a2009-08-28 14:00:25 +000010708 rx_skb = tpr->rx_std_buffers[desc_idx].skb;
Michael Chanc76949a2005-05-29 14:58:59 -070010709
Matt Carlson21f581a2009-08-28 14:00:25 +000010710 map = pci_unmap_addr(&tpr->rx_std_buffers[desc_idx], mapping);
Michael Chanc76949a2005-05-29 14:58:59 -070010711 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len, PCI_DMA_FROMDEVICE);
10712
10713 for (i = 14; i < tx_len; i++) {
10714 if (*(rx_skb->data + i) != (u8) (i & 0xff))
10715 goto out;
10716 }
10717 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010718
Michael Chanc76949a2005-05-29 14:58:59 -070010719 /* tg3_free_rings will unmap and free the rx_skb */
10720out:
10721 return err;
10722}
10723
Michael Chan9f40dea2005-09-05 17:53:06 -070010724#define TG3_MAC_LOOPBACK_FAILED 1
10725#define TG3_PHY_LOOPBACK_FAILED 2
10726#define TG3_LOOPBACK_FAILED (TG3_MAC_LOOPBACK_FAILED | \
10727 TG3_PHY_LOOPBACK_FAILED)
10728
10729static int tg3_test_loopback(struct tg3 *tp)
10730{
10731 int err = 0;
Matt Carlson9936bcf2007-10-10 18:03:07 -070010732 u32 cpmuctrl = 0;
Michael Chan9f40dea2005-09-05 17:53:06 -070010733
10734 if (!netif_running(tp->dev))
10735 return TG3_LOOPBACK_FAILED;
10736
Michael Chanb9ec6c12006-07-25 16:37:27 -070010737 err = tg3_reset_hw(tp, 1);
10738 if (err)
10739 return TG3_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070010740
Matt Carlson6833c042008-11-21 17:18:59 -080010741 /* Turn off gphy autopowerdown. */
10742 if (tp->tg3_flags3 & TG3_FLG3_PHY_ENABLE_APD)
10743 tg3_phy_toggle_apd(tp, false);
10744
Matt Carlson321d32a2008-11-21 17:22:19 -080010745 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070010746 int i;
10747 u32 status;
10748
10749 tw32(TG3_CPMU_MUTEX_REQ, CPMU_MUTEX_REQ_DRIVER);
10750
10751 /* Wait for up to 40 microseconds to acquire lock. */
10752 for (i = 0; i < 4; i++) {
10753 status = tr32(TG3_CPMU_MUTEX_GNT);
10754 if (status == CPMU_MUTEX_GNT_DRIVER)
10755 break;
10756 udelay(10);
10757 }
10758
10759 if (status != CPMU_MUTEX_GNT_DRIVER)
10760 return TG3_LOOPBACK_FAILED;
10761
Matt Carlsonb2a5c192008-04-03 21:44:44 -070010762 /* Turn off link-based power management. */
Matt Carlsone8750932007-11-12 21:11:51 -080010763 cpmuctrl = tr32(TG3_CPMU_CTRL);
Matt Carlson109115e2008-05-02 16:48:59 -070010764 tw32(TG3_CPMU_CTRL,
10765 cpmuctrl & ~(CPMU_CTRL_LINK_SPEED_MODE |
10766 CPMU_CTRL_LINK_AWARE_MODE));
Matt Carlson9936bcf2007-10-10 18:03:07 -070010767 }
10768
Michael Chan9f40dea2005-09-05 17:53:06 -070010769 if (tg3_run_loopback(tp, TG3_MAC_LOOPBACK))
10770 err |= TG3_MAC_LOOPBACK_FAILED;
Matt Carlson9936bcf2007-10-10 18:03:07 -070010771
Matt Carlson321d32a2008-11-21 17:22:19 -080010772 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070010773 tw32(TG3_CPMU_CTRL, cpmuctrl);
10774
10775 /* Release the mutex */
10776 tw32(TG3_CPMU_MUTEX_GNT, CPMU_MUTEX_GNT_DRIVER);
10777 }
10778
Matt Carlsondd477002008-05-25 23:45:58 -070010779 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
10780 !(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)) {
Michael Chan9f40dea2005-09-05 17:53:06 -070010781 if (tg3_run_loopback(tp, TG3_PHY_LOOPBACK))
10782 err |= TG3_PHY_LOOPBACK_FAILED;
10783 }
10784
Matt Carlson6833c042008-11-21 17:18:59 -080010785 /* Re-enable gphy autopowerdown. */
10786 if (tp->tg3_flags3 & TG3_FLG3_PHY_ENABLE_APD)
10787 tg3_phy_toggle_apd(tp, true);
10788
Michael Chan9f40dea2005-09-05 17:53:06 -070010789 return err;
10790}
10791
Michael Chan4cafd3f2005-05-29 14:56:34 -070010792static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
10793 u64 *data)
10794{
Michael Chan566f86a2005-05-29 14:56:58 -070010795 struct tg3 *tp = netdev_priv(dev);
10796
Michael Chanbc1c7562006-03-20 17:48:03 -080010797 if (tp->link_config.phy_is_low_power)
10798 tg3_set_power_state(tp, PCI_D0);
10799
Michael Chan566f86a2005-05-29 14:56:58 -070010800 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
10801
10802 if (tg3_test_nvram(tp) != 0) {
10803 etest->flags |= ETH_TEST_FL_FAILED;
10804 data[0] = 1;
10805 }
Michael Chanca430072005-05-29 14:57:23 -070010806 if (tg3_test_link(tp) != 0) {
10807 etest->flags |= ETH_TEST_FL_FAILED;
10808 data[1] = 1;
10809 }
Michael Chana71116d2005-05-29 14:58:11 -070010810 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010811 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070010812
Michael Chanbbe832c2005-06-24 20:20:04 -070010813 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010814 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010815 tg3_netif_stop(tp);
10816 irq_sync = 1;
10817 }
10818
10819 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070010820
10821 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080010822 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070010823 tg3_halt_cpu(tp, RX_CPU_BASE);
10824 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
10825 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080010826 if (!err)
10827 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070010828
Michael Chand9ab5ad2006-03-20 22:27:35 -080010829 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)
10830 tg3_phy_reset(tp);
10831
Michael Chana71116d2005-05-29 14:58:11 -070010832 if (tg3_test_registers(tp) != 0) {
10833 etest->flags |= ETH_TEST_FL_FAILED;
10834 data[2] = 1;
10835 }
Michael Chan7942e1d2005-05-29 14:58:36 -070010836 if (tg3_test_memory(tp) != 0) {
10837 etest->flags |= ETH_TEST_FL_FAILED;
10838 data[3] = 1;
10839 }
Michael Chan9f40dea2005-09-05 17:53:06 -070010840 if ((data[4] = tg3_test_loopback(tp)) != 0)
Michael Chanc76949a2005-05-29 14:58:59 -070010841 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070010842
David S. Millerf47c11e2005-06-24 20:18:35 -070010843 tg3_full_unlock(tp);
10844
Michael Chand4bc3922005-05-29 14:59:20 -070010845 if (tg3_test_interrupt(tp) != 0) {
10846 etest->flags |= ETH_TEST_FL_FAILED;
10847 data[5] = 1;
10848 }
David S. Millerf47c11e2005-06-24 20:18:35 -070010849
10850 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070010851
Michael Chana71116d2005-05-29 14:58:11 -070010852 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10853 if (netif_running(dev)) {
10854 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010855 err2 = tg3_restart_hw(tp, 1);
10856 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070010857 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070010858 }
David S. Millerf47c11e2005-06-24 20:18:35 -070010859
10860 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010861
10862 if (irq_sync && !err2)
10863 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070010864 }
Michael Chanbc1c7562006-03-20 17:48:03 -080010865 if (tp->link_config.phy_is_low_power)
10866 tg3_set_power_state(tp, PCI_D3hot);
10867
Michael Chan4cafd3f2005-05-29 14:56:34 -070010868}
10869
Linus Torvalds1da177e2005-04-16 15:20:36 -070010870static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
10871{
10872 struct mii_ioctl_data *data = if_mii(ifr);
10873 struct tg3 *tp = netdev_priv(dev);
10874 int err;
10875
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010876 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010877 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010878 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
10879 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010880 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10881 return phy_mii_ioctl(phydev, data, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010882 }
10883
Matt Carlson33f401a2010-04-05 10:19:27 +000010884 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010885 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000010886 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010887
10888 /* fallthru */
10889 case SIOCGMIIREG: {
10890 u32 mii_regval;
10891
10892 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
10893 break; /* We have no PHY */
10894
Michael Chanbc1c7562006-03-20 17:48:03 -080010895 if (tp->link_config.phy_is_low_power)
10896 return -EAGAIN;
10897
David S. Millerf47c11e2005-06-24 20:18:35 -070010898 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010899 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070010900 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010901
10902 data->val_out = mii_regval;
10903
10904 return err;
10905 }
10906
10907 case SIOCSMIIREG:
10908 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
10909 break; /* We have no PHY */
10910
Michael Chanbc1c7562006-03-20 17:48:03 -080010911 if (tp->link_config.phy_is_low_power)
10912 return -EAGAIN;
10913
David S. Millerf47c11e2005-06-24 20:18:35 -070010914 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010915 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070010916 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010917
10918 return err;
10919
10920 default:
10921 /* do nothing */
10922 break;
10923 }
10924 return -EOPNOTSUPP;
10925}
10926
10927#if TG3_VLAN_TAG_USED
10928static void tg3_vlan_rx_register(struct net_device *dev, struct vlan_group *grp)
10929{
10930 struct tg3 *tp = netdev_priv(dev);
10931
Matt Carlson844b3ee2009-02-25 14:23:56 +000010932 if (!netif_running(dev)) {
10933 tp->vlgrp = grp;
10934 return;
10935 }
10936
10937 tg3_netif_stop(tp);
Michael Chan29315e82006-06-29 20:12:30 -070010938
David S. Millerf47c11e2005-06-24 20:18:35 -070010939 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010940
10941 tp->vlgrp = grp;
10942
10943 /* Update RX_MODE_KEEP_VLAN_TAG bit in RX_MODE register. */
10944 __tg3_set_rx_mode(dev);
10945
Matt Carlson844b3ee2009-02-25 14:23:56 +000010946 tg3_netif_start(tp);
Michael Chan46966542007-07-11 19:47:19 -070010947
10948 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010949}
Linus Torvalds1da177e2005-04-16 15:20:36 -070010950#endif
10951
David S. Miller15f98502005-05-18 22:49:26 -070010952static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
10953{
10954 struct tg3 *tp = netdev_priv(dev);
10955
10956 memcpy(ec, &tp->coal, sizeof(*ec));
10957 return 0;
10958}
10959
Michael Chand244c892005-07-05 14:42:33 -070010960static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
10961{
10962 struct tg3 *tp = netdev_priv(dev);
10963 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
10964 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
10965
10966 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
10967 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
10968 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
10969 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
10970 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
10971 }
10972
10973 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
10974 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
10975 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
10976 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
10977 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
10978 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
10979 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
10980 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
10981 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
10982 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
10983 return -EINVAL;
10984
10985 /* No rx interrupts will be generated if both are zero */
10986 if ((ec->rx_coalesce_usecs == 0) &&
10987 (ec->rx_max_coalesced_frames == 0))
10988 return -EINVAL;
10989
10990 /* No tx interrupts will be generated if both are zero */
10991 if ((ec->tx_coalesce_usecs == 0) &&
10992 (ec->tx_max_coalesced_frames == 0))
10993 return -EINVAL;
10994
10995 /* Only copy relevant parameters, ignore all others. */
10996 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
10997 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
10998 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
10999 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
11000 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
11001 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
11002 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
11003 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
11004 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
11005
11006 if (netif_running(dev)) {
11007 tg3_full_lock(tp, 0);
11008 __tg3_set_coalesce(tp, &tp->coal);
11009 tg3_full_unlock(tp);
11010 }
11011 return 0;
11012}
11013
Jeff Garzik7282d492006-09-13 14:30:00 -040011014static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011015 .get_settings = tg3_get_settings,
11016 .set_settings = tg3_set_settings,
11017 .get_drvinfo = tg3_get_drvinfo,
11018 .get_regs_len = tg3_get_regs_len,
11019 .get_regs = tg3_get_regs,
11020 .get_wol = tg3_get_wol,
11021 .set_wol = tg3_set_wol,
11022 .get_msglevel = tg3_get_msglevel,
11023 .set_msglevel = tg3_set_msglevel,
11024 .nway_reset = tg3_nway_reset,
11025 .get_link = ethtool_op_get_link,
11026 .get_eeprom_len = tg3_get_eeprom_len,
11027 .get_eeprom = tg3_get_eeprom,
11028 .set_eeprom = tg3_set_eeprom,
11029 .get_ringparam = tg3_get_ringparam,
11030 .set_ringparam = tg3_set_ringparam,
11031 .get_pauseparam = tg3_get_pauseparam,
11032 .set_pauseparam = tg3_set_pauseparam,
11033 .get_rx_csum = tg3_get_rx_csum,
11034 .set_rx_csum = tg3_set_rx_csum,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011035 .set_tx_csum = tg3_set_tx_csum,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011036 .set_sg = ethtool_op_set_sg,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011037 .set_tso = tg3_set_tso,
Michael Chan4cafd3f2005-05-29 14:56:34 -070011038 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011039 .get_strings = tg3_get_strings,
Michael Chan4009a932005-09-05 17:52:54 -070011040 .phys_id = tg3_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011041 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070011042 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070011043 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070011044 .get_sset_count = tg3_get_sset_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011045};
11046
11047static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
11048{
Michael Chan1b277772006-03-20 22:27:48 -080011049 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011050
11051 tp->nvram_size = EEPROM_CHIP_SIZE;
11052
Matt Carlsone4f34112009-02-25 14:25:00 +000011053 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011054 return;
11055
Michael Chanb16250e2006-09-27 16:10:14 -070011056 if ((magic != TG3_EEPROM_MAGIC) &&
11057 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
11058 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011059 return;
11060
11061 /*
11062 * Size the chip by reading offsets at increasing powers of two.
11063 * When we encounter our validation signature, we know the addressing
11064 * has wrapped around, and thus have our chip size.
11065 */
Michael Chan1b277772006-03-20 22:27:48 -080011066 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011067
11068 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000011069 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011070 return;
11071
Michael Chan18201802006-03-20 22:29:15 -080011072 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011073 break;
11074
11075 cursize <<= 1;
11076 }
11077
11078 tp->nvram_size = cursize;
11079}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011080
Linus Torvalds1da177e2005-04-16 15:20:36 -070011081static void __devinit tg3_get_nvram_size(struct tg3 *tp)
11082{
11083 u32 val;
11084
Matt Carlsondf259d82009-04-20 06:57:14 +000011085 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
11086 tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080011087 return;
11088
11089 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080011090 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080011091 tg3_get_eeprom_size(tp);
11092 return;
11093 }
11094
Matt Carlson6d348f22009-02-25 14:25:52 +000011095 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011096 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000011097 /* This is confusing. We want to operate on the
11098 * 16-bit value at offset 0xf2. The tg3_nvram_read()
11099 * call will read from NVRAM and byteswap the data
11100 * according to the byteswapping settings for all
11101 * other register accesses. This ensures the data we
11102 * want will always reside in the lower 16-bits.
11103 * However, the data in NVRAM is in LE format, which
11104 * means the data from the NVRAM read will always be
11105 * opposite the endianness of the CPU. The 16-bit
11106 * byteswap then brings the data to CPU endianness.
11107 */
11108 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011109 return;
11110 }
11111 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070011112 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011113}
11114
11115static void __devinit tg3_get_nvram_info(struct tg3 *tp)
11116{
11117 u32 nvcfg1;
11118
11119 nvcfg1 = tr32(NVRAM_CFG1);
11120 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
11121 tp->tg3_flags2 |= TG3_FLG2_FLASH;
Matt Carlson8590a602009-08-28 12:29:16 +000011122 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011123 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11124 tw32(NVRAM_CFG1, nvcfg1);
11125 }
11126
Michael Chan4c987482005-09-05 17:52:38 -070011127 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750) ||
Michael Chana4e2b342005-10-26 15:46:52 -070011128 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011129 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011130 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
11131 tp->nvram_jedecnum = JEDEC_ATMEL;
11132 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
11133 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11134 break;
11135 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
11136 tp->nvram_jedecnum = JEDEC_ATMEL;
11137 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
11138 break;
11139 case FLASH_VENDOR_ATMEL_EEPROM:
11140 tp->nvram_jedecnum = JEDEC_ATMEL;
11141 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11142 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11143 break;
11144 case FLASH_VENDOR_ST:
11145 tp->nvram_jedecnum = JEDEC_ST;
11146 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
11147 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11148 break;
11149 case FLASH_VENDOR_SAIFUN:
11150 tp->nvram_jedecnum = JEDEC_SAIFUN;
11151 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
11152 break;
11153 case FLASH_VENDOR_SST_SMALL:
11154 case FLASH_VENDOR_SST_LARGE:
11155 tp->nvram_jedecnum = JEDEC_SST;
11156 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
11157 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011158 }
Matt Carlson8590a602009-08-28 12:29:16 +000011159 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011160 tp->nvram_jedecnum = JEDEC_ATMEL;
11161 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
11162 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11163 }
11164}
11165
Matt Carlsona1b950d2009-09-01 13:20:17 +000011166static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
11167{
11168 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
11169 case FLASH_5752PAGE_SIZE_256:
11170 tp->nvram_pagesize = 256;
11171 break;
11172 case FLASH_5752PAGE_SIZE_512:
11173 tp->nvram_pagesize = 512;
11174 break;
11175 case FLASH_5752PAGE_SIZE_1K:
11176 tp->nvram_pagesize = 1024;
11177 break;
11178 case FLASH_5752PAGE_SIZE_2K:
11179 tp->nvram_pagesize = 2048;
11180 break;
11181 case FLASH_5752PAGE_SIZE_4K:
11182 tp->nvram_pagesize = 4096;
11183 break;
11184 case FLASH_5752PAGE_SIZE_264:
11185 tp->nvram_pagesize = 264;
11186 break;
11187 case FLASH_5752PAGE_SIZE_528:
11188 tp->nvram_pagesize = 528;
11189 break;
11190 }
11191}
11192
Michael Chan361b4ac2005-04-21 17:11:21 -070011193static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
11194{
11195 u32 nvcfg1;
11196
11197 nvcfg1 = tr32(NVRAM_CFG1);
11198
Michael Chane6af3012005-04-21 17:12:05 -070011199 /* NVRAM protection for TPM */
11200 if (nvcfg1 & (1 << 27))
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011201 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Michael Chane6af3012005-04-21 17:12:05 -070011202
Michael Chan361b4ac2005-04-21 17:11:21 -070011203 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011204 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
11205 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
11206 tp->nvram_jedecnum = JEDEC_ATMEL;
11207 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11208 break;
11209 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11210 tp->nvram_jedecnum = JEDEC_ATMEL;
11211 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11212 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11213 break;
11214 case FLASH_5752VENDOR_ST_M45PE10:
11215 case FLASH_5752VENDOR_ST_M45PE20:
11216 case FLASH_5752VENDOR_ST_M45PE40:
11217 tp->nvram_jedecnum = JEDEC_ST;
11218 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11219 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11220 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070011221 }
11222
11223 if (tp->tg3_flags2 & TG3_FLG2_FLASH) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000011224 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000011225 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070011226 /* For eeprom, set pagesize to maximum eeprom size */
11227 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11228
11229 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11230 tw32(NVRAM_CFG1, nvcfg1);
11231 }
11232}
11233
Michael Chand3c7b882006-03-23 01:28:25 -080011234static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
11235{
Matt Carlson989a9d22007-05-05 11:51:05 -070011236 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080011237
11238 nvcfg1 = tr32(NVRAM_CFG1);
11239
11240 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070011241 if (nvcfg1 & (1 << 27)) {
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011242 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Matt Carlson989a9d22007-05-05 11:51:05 -070011243 protect = 1;
11244 }
Michael Chand3c7b882006-03-23 01:28:25 -080011245
Matt Carlson989a9d22007-05-05 11:51:05 -070011246 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11247 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011248 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11249 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11250 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11251 case FLASH_5755VENDOR_ATMEL_FLASH_5:
11252 tp->nvram_jedecnum = JEDEC_ATMEL;
11253 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11254 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11255 tp->nvram_pagesize = 264;
11256 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
11257 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
11258 tp->nvram_size = (protect ? 0x3e200 :
11259 TG3_NVRAM_SIZE_512KB);
11260 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
11261 tp->nvram_size = (protect ? 0x1f200 :
11262 TG3_NVRAM_SIZE_256KB);
11263 else
11264 tp->nvram_size = (protect ? 0x1f200 :
11265 TG3_NVRAM_SIZE_128KB);
11266 break;
11267 case FLASH_5752VENDOR_ST_M45PE10:
11268 case FLASH_5752VENDOR_ST_M45PE20:
11269 case FLASH_5752VENDOR_ST_M45PE40:
11270 tp->nvram_jedecnum = JEDEC_ST;
11271 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11272 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11273 tp->nvram_pagesize = 256;
11274 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
11275 tp->nvram_size = (protect ?
11276 TG3_NVRAM_SIZE_64KB :
11277 TG3_NVRAM_SIZE_128KB);
11278 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
11279 tp->nvram_size = (protect ?
11280 TG3_NVRAM_SIZE_64KB :
11281 TG3_NVRAM_SIZE_256KB);
11282 else
11283 tp->nvram_size = (protect ?
11284 TG3_NVRAM_SIZE_128KB :
11285 TG3_NVRAM_SIZE_512KB);
11286 break;
Michael Chand3c7b882006-03-23 01:28:25 -080011287 }
11288}
11289
Michael Chan1b277772006-03-20 22:27:48 -080011290static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
11291{
11292 u32 nvcfg1;
11293
11294 nvcfg1 = tr32(NVRAM_CFG1);
11295
11296 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011297 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
11298 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11299 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
11300 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11301 tp->nvram_jedecnum = JEDEC_ATMEL;
11302 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11303 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080011304
Matt Carlson8590a602009-08-28 12:29:16 +000011305 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11306 tw32(NVRAM_CFG1, nvcfg1);
11307 break;
11308 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11309 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11310 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11311 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11312 tp->nvram_jedecnum = JEDEC_ATMEL;
11313 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11314 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11315 tp->nvram_pagesize = 264;
11316 break;
11317 case FLASH_5752VENDOR_ST_M45PE10:
11318 case FLASH_5752VENDOR_ST_M45PE20:
11319 case FLASH_5752VENDOR_ST_M45PE40:
11320 tp->nvram_jedecnum = JEDEC_ST;
11321 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11322 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11323 tp->nvram_pagesize = 256;
11324 break;
Michael Chan1b277772006-03-20 22:27:48 -080011325 }
11326}
11327
Matt Carlson6b91fa02007-10-10 18:01:09 -070011328static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
11329{
11330 u32 nvcfg1, protect = 0;
11331
11332 nvcfg1 = tr32(NVRAM_CFG1);
11333
11334 /* NVRAM protection for TPM */
11335 if (nvcfg1 & (1 << 27)) {
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011336 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011337 protect = 1;
11338 }
11339
11340 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11341 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011342 case FLASH_5761VENDOR_ATMEL_ADB021D:
11343 case FLASH_5761VENDOR_ATMEL_ADB041D:
11344 case FLASH_5761VENDOR_ATMEL_ADB081D:
11345 case FLASH_5761VENDOR_ATMEL_ADB161D:
11346 case FLASH_5761VENDOR_ATMEL_MDB021D:
11347 case FLASH_5761VENDOR_ATMEL_MDB041D:
11348 case FLASH_5761VENDOR_ATMEL_MDB081D:
11349 case FLASH_5761VENDOR_ATMEL_MDB161D:
11350 tp->nvram_jedecnum = JEDEC_ATMEL;
11351 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11352 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11353 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
11354 tp->nvram_pagesize = 256;
11355 break;
11356 case FLASH_5761VENDOR_ST_A_M45PE20:
11357 case FLASH_5761VENDOR_ST_A_M45PE40:
11358 case FLASH_5761VENDOR_ST_A_M45PE80:
11359 case FLASH_5761VENDOR_ST_A_M45PE16:
11360 case FLASH_5761VENDOR_ST_M_M45PE20:
11361 case FLASH_5761VENDOR_ST_M_M45PE40:
11362 case FLASH_5761VENDOR_ST_M_M45PE80:
11363 case FLASH_5761VENDOR_ST_M_M45PE16:
11364 tp->nvram_jedecnum = JEDEC_ST;
11365 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11366 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11367 tp->nvram_pagesize = 256;
11368 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011369 }
11370
11371 if (protect) {
11372 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
11373 } else {
11374 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011375 case FLASH_5761VENDOR_ATMEL_ADB161D:
11376 case FLASH_5761VENDOR_ATMEL_MDB161D:
11377 case FLASH_5761VENDOR_ST_A_M45PE16:
11378 case FLASH_5761VENDOR_ST_M_M45PE16:
11379 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
11380 break;
11381 case FLASH_5761VENDOR_ATMEL_ADB081D:
11382 case FLASH_5761VENDOR_ATMEL_MDB081D:
11383 case FLASH_5761VENDOR_ST_A_M45PE80:
11384 case FLASH_5761VENDOR_ST_M_M45PE80:
11385 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
11386 break;
11387 case FLASH_5761VENDOR_ATMEL_ADB041D:
11388 case FLASH_5761VENDOR_ATMEL_MDB041D:
11389 case FLASH_5761VENDOR_ST_A_M45PE40:
11390 case FLASH_5761VENDOR_ST_M_M45PE40:
11391 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11392 break;
11393 case FLASH_5761VENDOR_ATMEL_ADB021D:
11394 case FLASH_5761VENDOR_ATMEL_MDB021D:
11395 case FLASH_5761VENDOR_ST_A_M45PE20:
11396 case FLASH_5761VENDOR_ST_M_M45PE20:
11397 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11398 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011399 }
11400 }
11401}
11402
Michael Chanb5d37722006-09-27 16:06:21 -070011403static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
11404{
11405 tp->nvram_jedecnum = JEDEC_ATMEL;
11406 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11407 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11408}
11409
Matt Carlson321d32a2008-11-21 17:22:19 -080011410static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
11411{
11412 u32 nvcfg1;
11413
11414 nvcfg1 = tr32(NVRAM_CFG1);
11415
11416 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11417 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11418 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11419 tp->nvram_jedecnum = JEDEC_ATMEL;
11420 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11421 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11422
11423 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11424 tw32(NVRAM_CFG1, nvcfg1);
11425 return;
11426 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11427 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11428 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11429 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11430 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11431 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11432 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
11433 tp->nvram_jedecnum = JEDEC_ATMEL;
11434 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11435 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11436
11437 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11438 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11439 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11440 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11441 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11442 break;
11443 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11444 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11445 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11446 break;
11447 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11448 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
11449 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11450 break;
11451 }
11452 break;
11453 case FLASH_5752VENDOR_ST_M45PE10:
11454 case FLASH_5752VENDOR_ST_M45PE20:
11455 case FLASH_5752VENDOR_ST_M45PE40:
11456 tp->nvram_jedecnum = JEDEC_ST;
11457 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11458 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11459
11460 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11461 case FLASH_5752VENDOR_ST_M45PE10:
11462 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11463 break;
11464 case FLASH_5752VENDOR_ST_M45PE20:
11465 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11466 break;
11467 case FLASH_5752VENDOR_ST_M45PE40:
11468 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11469 break;
11470 }
11471 break;
11472 default:
Matt Carlsondf259d82009-04-20 06:57:14 +000011473 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
Matt Carlson321d32a2008-11-21 17:22:19 -080011474 return;
11475 }
11476
Matt Carlsona1b950d2009-09-01 13:20:17 +000011477 tg3_nvram_get_pagesize(tp, nvcfg1);
11478 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Matt Carlson321d32a2008-11-21 17:22:19 -080011479 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011480}
11481
11482
11483static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
11484{
11485 u32 nvcfg1;
11486
11487 nvcfg1 = tr32(NVRAM_CFG1);
11488
11489 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11490 case FLASH_5717VENDOR_ATMEL_EEPROM:
11491 case FLASH_5717VENDOR_MICRO_EEPROM:
11492 tp->nvram_jedecnum = JEDEC_ATMEL;
11493 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11494 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11495
11496 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11497 tw32(NVRAM_CFG1, nvcfg1);
11498 return;
11499 case FLASH_5717VENDOR_ATMEL_MDB011D:
11500 case FLASH_5717VENDOR_ATMEL_ADB011B:
11501 case FLASH_5717VENDOR_ATMEL_ADB011D:
11502 case FLASH_5717VENDOR_ATMEL_MDB021D:
11503 case FLASH_5717VENDOR_ATMEL_ADB021B:
11504 case FLASH_5717VENDOR_ATMEL_ADB021D:
11505 case FLASH_5717VENDOR_ATMEL_45USPT:
11506 tp->nvram_jedecnum = JEDEC_ATMEL;
11507 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11508 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11509
11510 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11511 case FLASH_5717VENDOR_ATMEL_MDB021D:
11512 case FLASH_5717VENDOR_ATMEL_ADB021B:
11513 case FLASH_5717VENDOR_ATMEL_ADB021D:
11514 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11515 break;
11516 default:
11517 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11518 break;
11519 }
Matt Carlson321d32a2008-11-21 17:22:19 -080011520 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011521 case FLASH_5717VENDOR_ST_M_M25PE10:
11522 case FLASH_5717VENDOR_ST_A_M25PE10:
11523 case FLASH_5717VENDOR_ST_M_M45PE10:
11524 case FLASH_5717VENDOR_ST_A_M45PE10:
11525 case FLASH_5717VENDOR_ST_M_M25PE20:
11526 case FLASH_5717VENDOR_ST_A_M25PE20:
11527 case FLASH_5717VENDOR_ST_M_M45PE20:
11528 case FLASH_5717VENDOR_ST_A_M45PE20:
11529 case FLASH_5717VENDOR_ST_25USPT:
11530 case FLASH_5717VENDOR_ST_45USPT:
11531 tp->nvram_jedecnum = JEDEC_ST;
11532 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11533 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11534
11535 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11536 case FLASH_5717VENDOR_ST_M_M25PE20:
11537 case FLASH_5717VENDOR_ST_A_M25PE20:
11538 case FLASH_5717VENDOR_ST_M_M45PE20:
11539 case FLASH_5717VENDOR_ST_A_M45PE20:
11540 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11541 break;
11542 default:
11543 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11544 break;
11545 }
Matt Carlson321d32a2008-11-21 17:22:19 -080011546 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011547 default:
11548 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
11549 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080011550 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000011551
11552 tg3_nvram_get_pagesize(tp, nvcfg1);
11553 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
11554 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
Matt Carlson321d32a2008-11-21 17:22:19 -080011555}
11556
Linus Torvalds1da177e2005-04-16 15:20:36 -070011557/* Chips other than 5700/5701 use the NVRAM for fetching info. */
11558static void __devinit tg3_nvram_init(struct tg3 *tp)
11559{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011560 tw32_f(GRC_EEPROM_ADDR,
11561 (EEPROM_ADDR_FSM_RESET |
11562 (EEPROM_DEFAULT_CLOCK_PERIOD <<
11563 EEPROM_ADDR_CLKPERD_SHIFT)));
11564
Michael Chan9d57f012006-12-07 00:23:25 -080011565 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011566
11567 /* Enable seeprom accesses. */
11568 tw32_f(GRC_LOCAL_CTRL,
11569 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
11570 udelay(100);
11571
11572 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
11573 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
11574 tp->tg3_flags |= TG3_FLAG_NVRAM;
11575
Michael Chanec41c7d2006-01-17 02:40:55 -080011576 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000011577 netdev_warn(tp->dev,
11578 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000011579 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080011580 return;
11581 }
Michael Chane6af3012005-04-21 17:12:05 -070011582 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011583
Matt Carlson989a9d22007-05-05 11:51:05 -070011584 tp->nvram_size = 0;
11585
Michael Chan361b4ac2005-04-21 17:11:21 -070011586 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
11587 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080011588 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
11589 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070011590 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070011591 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
11592 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080011593 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070011594 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
11595 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070011596 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
11597 tg3_get_5906_nvram_info(tp);
Matt Carlsonb703df62009-12-03 08:36:21 +000011598 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
11599 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson321d32a2008-11-21 17:22:19 -080011600 tg3_get_57780_nvram_info(tp);
Matt Carlsona1b950d2009-09-01 13:20:17 +000011601 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
11602 tg3_get_5717_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070011603 else
11604 tg3_get_nvram_info(tp);
11605
Matt Carlson989a9d22007-05-05 11:51:05 -070011606 if (tp->nvram_size == 0)
11607 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011608
Michael Chane6af3012005-04-21 17:12:05 -070011609 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080011610 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011611
11612 } else {
11613 tp->tg3_flags &= ~(TG3_FLAG_NVRAM | TG3_FLAG_NVRAM_BUFFERED);
11614
11615 tg3_get_eeprom_size(tp);
11616 }
11617}
11618
Linus Torvalds1da177e2005-04-16 15:20:36 -070011619static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
11620 u32 offset, u32 len, u8 *buf)
11621{
11622 int i, j, rc = 0;
11623 u32 val;
11624
11625 for (i = 0; i < len; i += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080011626 u32 addr;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011627 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011628
11629 addr = offset + i;
11630
11631 memcpy(&data, buf + i, 4);
11632
Matt Carlson62cedd12009-04-20 14:52:29 -070011633 /*
11634 * The SEEPROM interface expects the data to always be opposite
11635 * the native endian format. We accomplish this by reversing
11636 * all the operations that would have been performed on the
11637 * data from a call to tg3_nvram_read_be32().
11638 */
11639 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011640
11641 val = tr32(GRC_EEPROM_ADDR);
11642 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
11643
11644 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
11645 EEPROM_ADDR_READ);
11646 tw32(GRC_EEPROM_ADDR, val |
11647 (0 << EEPROM_ADDR_DEVID_SHIFT) |
11648 (addr & EEPROM_ADDR_ADDR_MASK) |
11649 EEPROM_ADDR_START |
11650 EEPROM_ADDR_WRITE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011651
Michael Chan9d57f012006-12-07 00:23:25 -080011652 for (j = 0; j < 1000; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011653 val = tr32(GRC_EEPROM_ADDR);
11654
11655 if (val & EEPROM_ADDR_COMPLETE)
11656 break;
Michael Chan9d57f012006-12-07 00:23:25 -080011657 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011658 }
11659 if (!(val & EEPROM_ADDR_COMPLETE)) {
11660 rc = -EBUSY;
11661 break;
11662 }
11663 }
11664
11665 return rc;
11666}
11667
11668/* offset and length are dword aligned */
11669static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
11670 u8 *buf)
11671{
11672 int ret = 0;
11673 u32 pagesize = tp->nvram_pagesize;
11674 u32 pagemask = pagesize - 1;
11675 u32 nvram_cmd;
11676 u8 *tmp;
11677
11678 tmp = kmalloc(pagesize, GFP_KERNEL);
11679 if (tmp == NULL)
11680 return -ENOMEM;
11681
11682 while (len) {
11683 int j;
Michael Chane6af3012005-04-21 17:12:05 -070011684 u32 phy_addr, page_off, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011685
11686 phy_addr = offset & ~pagemask;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011687
Linus Torvalds1da177e2005-04-16 15:20:36 -070011688 for (j = 0; j < pagesize; j += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000011689 ret = tg3_nvram_read_be32(tp, phy_addr + j,
11690 (__be32 *) (tmp + j));
11691 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011692 break;
11693 }
11694 if (ret)
11695 break;
11696
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011697 page_off = offset & pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011698 size = pagesize;
11699 if (len < size)
11700 size = len;
11701
11702 len -= size;
11703
11704 memcpy(tmp + page_off, buf, size);
11705
11706 offset = offset + (pagesize - page_off);
11707
Michael Chane6af3012005-04-21 17:12:05 -070011708 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011709
11710 /*
11711 * Before we can erase the flash page, we need
11712 * to issue a special "write enable" command.
11713 */
11714 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
11715
11716 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
11717 break;
11718
11719 /* Erase the target page */
11720 tw32(NVRAM_ADDR, phy_addr);
11721
11722 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
11723 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
11724
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011725 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011726 break;
11727
11728 /* Issue another write enable to start the write. */
11729 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
11730
11731 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
11732 break;
11733
11734 for (j = 0; j < pagesize; j += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080011735 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011736
Al Virob9fc7dc2007-12-17 22:59:57 -080011737 data = *((__be32 *) (tmp + j));
Matt Carlsona9dc5292009-02-25 14:25:30 +000011738
Al Virob9fc7dc2007-12-17 22:59:57 -080011739 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011740
11741 tw32(NVRAM_ADDR, phy_addr + j);
11742
11743 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
11744 NVRAM_CMD_WR;
11745
11746 if (j == 0)
11747 nvram_cmd |= NVRAM_CMD_FIRST;
11748 else if (j == (pagesize - 4))
11749 nvram_cmd |= NVRAM_CMD_LAST;
11750
11751 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
11752 break;
11753 }
11754 if (ret)
11755 break;
11756 }
11757
11758 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
11759 tg3_nvram_exec_cmd(tp, nvram_cmd);
11760
11761 kfree(tmp);
11762
11763 return ret;
11764}
11765
11766/* offset and length are dword aligned */
11767static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
11768 u8 *buf)
11769{
11770 int i, ret = 0;
11771
11772 for (i = 0; i < len; i += 4, offset += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080011773 u32 page_off, phy_addr, nvram_cmd;
11774 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011775
11776 memcpy(&data, buf + i, 4);
Al Virob9fc7dc2007-12-17 22:59:57 -080011777 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011778
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011779 page_off = offset % tp->nvram_pagesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011780
Michael Chan18201802006-03-20 22:29:15 -080011781 phy_addr = tg3_nvram_phys_addr(tp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011782
11783 tw32(NVRAM_ADDR, phy_addr);
11784
11785 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
11786
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011787 if (page_off == 0 || i == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011788 nvram_cmd |= NVRAM_CMD_FIRST;
Michael Chanf6d9a252006-04-29 19:00:24 -070011789 if (page_off == (tp->nvram_pagesize - 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011790 nvram_cmd |= NVRAM_CMD_LAST;
11791
11792 if (i == (len - 4))
11793 nvram_cmd |= NVRAM_CMD_LAST;
11794
Matt Carlson321d32a2008-11-21 17:22:19 -080011795 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
11796 !(tp->tg3_flags3 & TG3_FLG3_5755_PLUS) &&
Michael Chan4c987482005-09-05 17:52:38 -070011797 (tp->nvram_jedecnum == JEDEC_ST) &&
11798 (nvram_cmd & NVRAM_CMD_FIRST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011799
11800 if ((ret = tg3_nvram_exec_cmd(tp,
11801 NVRAM_CMD_WREN | NVRAM_CMD_GO |
11802 NVRAM_CMD_DONE)))
11803
11804 break;
11805 }
11806 if (!(tp->tg3_flags2 & TG3_FLG2_FLASH)) {
11807 /* We always do complete word writes to eeprom. */
11808 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
11809 }
11810
11811 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
11812 break;
11813 }
11814 return ret;
11815}
11816
11817/* offset and length are dword aligned */
11818static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
11819{
11820 int ret;
11821
Linus Torvalds1da177e2005-04-16 15:20:36 -070011822 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT) {
Michael Chan314fba32005-04-21 17:07:04 -070011823 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
11824 ~GRC_LCLCTRL_GPIO_OUTPUT1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011825 udelay(40);
11826 }
11827
11828 if (!(tp->tg3_flags & TG3_FLAG_NVRAM)) {
11829 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
Matt Carlson859a588792010-04-05 10:19:28 +000011830 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011831 u32 grc_mode;
11832
Michael Chanec41c7d2006-01-17 02:40:55 -080011833 ret = tg3_nvram_lock(tp);
11834 if (ret)
11835 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011836
Michael Chane6af3012005-04-21 17:12:05 -070011837 tg3_enable_nvram_access(tp);
11838 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011839 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011840 tw32(NVRAM_WRITE1, 0x406);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011841
11842 grc_mode = tr32(GRC_MODE);
11843 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
11844
11845 if ((tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) ||
11846 !(tp->tg3_flags2 & TG3_FLG2_FLASH)) {
11847
11848 ret = tg3_nvram_write_block_buffered(tp, offset, len,
11849 buf);
Matt Carlson859a588792010-04-05 10:19:28 +000011850 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011851 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
11852 buf);
11853 }
11854
11855 grc_mode = tr32(GRC_MODE);
11856 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
11857
Michael Chane6af3012005-04-21 17:12:05 -070011858 tg3_disable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011859 tg3_nvram_unlock(tp);
11860 }
11861
11862 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT) {
Michael Chan314fba32005-04-21 17:07:04 -070011863 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011864 udelay(40);
11865 }
11866
11867 return ret;
11868}
11869
11870struct subsys_tbl_ent {
11871 u16 subsys_vendor, subsys_devid;
11872 u32 phy_id;
11873};
11874
Matt Carlson24daf2b2010-02-17 15:17:02 +000011875static struct subsys_tbl_ent subsys_id_to_phy_id[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011876 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000011877 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011878 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011879 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011880 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011881 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011882 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011883 { TG3PCI_SUBVENDOR_ID_BROADCOM,
11884 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
11885 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011886 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011887 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011888 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011889 { TG3PCI_SUBVENDOR_ID_BROADCOM,
11890 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
11891 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011892 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011893 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011894 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011895 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011896 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011897 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011898 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070011899
11900 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000011901 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011902 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011903 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011904 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011905 { TG3PCI_SUBVENDOR_ID_3COM,
11906 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
11907 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011908 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011909 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011910 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070011911
11912 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000011913 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000011914 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011915 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000011916 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011917 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000011918 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011919 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000011920 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070011921
11922 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000011923 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000011924 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011925 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000011926 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011927 { TG3PCI_SUBVENDOR_ID_COMPAQ,
11928 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
11929 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000011930 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011931 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000011932 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070011933
11934 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000011935 { TG3PCI_SUBVENDOR_ID_IBM,
11936 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011937};
11938
Matt Carlson24daf2b2010-02-17 15:17:02 +000011939static struct subsys_tbl_ent * __devinit tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011940{
11941 int i;
11942
11943 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
11944 if ((subsys_id_to_phy_id[i].subsys_vendor ==
11945 tp->pdev->subsystem_vendor) &&
11946 (subsys_id_to_phy_id[i].subsys_devid ==
11947 tp->pdev->subsystem_device))
11948 return &subsys_id_to_phy_id[i];
11949 }
11950 return NULL;
11951}
11952
Michael Chan7d0c41e2005-04-21 17:06:20 -070011953static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011954{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011955 u32 val;
Michael Chancaf636c72006-03-22 01:05:31 -080011956 u16 pmcsr;
11957
11958 /* On some early chips the SRAM cannot be accessed in D3hot state,
11959 * so need make sure we're in D0.
11960 */
11961 pci_read_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, &pmcsr);
11962 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
11963 pci_write_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, pmcsr);
11964 msleep(1);
Michael Chan7d0c41e2005-04-21 17:06:20 -070011965
11966 /* Make sure register accesses (indirect or otherwise)
11967 * will function correctly.
11968 */
11969 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
11970 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011971
David S. Millerf49639e2006-06-09 11:58:36 -070011972 /* The memory arbiter has to be enabled in order for SRAM accesses
11973 * to succeed. Normally on powerup the tg3 chip firmware will make
11974 * sure it is enabled, but other entities such as system netboot
11975 * code might disable it.
11976 */
11977 val = tr32(MEMARB_MODE);
11978 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
11979
Matt Carlson79eb6902010-02-17 15:17:03 +000011980 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070011981 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
11982
Gary Zambranoa85feb82007-05-05 11:52:19 -070011983 /* Assume an onboard device and WOL capable by default. */
11984 tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT | TG3_FLAG_WOL_CAP;
David S. Miller72b845e2006-03-14 14:11:48 -080011985
Michael Chanb5d37722006-09-27 16:06:21 -070011986 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080011987 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Michael Chanb5d37722006-09-27 16:06:21 -070011988 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080011989 tp->tg3_flags2 |= TG3_FLG2_IS_NIC;
11990 }
Matt Carlson0527ba32007-10-10 18:03:30 -070011991 val = tr32(VCPU_CFGSHDW);
11992 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Matt Carlson8ed5d972007-05-07 00:25:49 -070011993 tp->tg3_flags |= TG3_FLAG_ASPM_WORKAROUND;
Matt Carlson0527ba32007-10-10 18:03:30 -070011994 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Matt Carlson20232762008-12-21 20:18:56 -080011995 (val & VCPU_CFGSHDW_WOL_MAGPKT))
Matt Carlson0527ba32007-10-10 18:03:30 -070011996 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
Matt Carlson05ac4cb2008-11-03 16:53:46 -080011997 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070011998 }
11999
Linus Torvalds1da177e2005-04-16 15:20:36 -070012000 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
12001 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
12002 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070012003 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012004 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012005
12006 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
12007 tp->nic_sram_data_cfg = nic_cfg;
12008
12009 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
12010 ver >>= NIC_SRAM_DATA_VER_SHIFT;
12011 if ((GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700) &&
12012 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) &&
12013 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703) &&
12014 (ver > 0) && (ver < 0x100))
12015 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
12016
Matt Carlsona9daf362008-05-25 23:49:44 -070012017 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
12018 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
12019
Linus Torvalds1da177e2005-04-16 15:20:36 -070012020 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
12021 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
12022 eeprom_phy_serdes = 1;
12023
12024 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
12025 if (nic_phy_id != 0) {
12026 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
12027 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
12028
12029 eeprom_phy_id = (id1 >> 16) << 10;
12030 eeprom_phy_id |= (id2 & 0xfc00) << 16;
12031 eeprom_phy_id |= (id2 & 0x03ff) << 0;
12032 } else
12033 eeprom_phy_id = 0;
12034
Michael Chan7d0c41e2005-04-21 17:06:20 -070012035 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070012036 if (eeprom_phy_serdes) {
Matt Carlsond1ec96a2010-01-12 10:11:38 +000012037 if ((tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
12038 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Michael Chan747e8f82005-07-25 12:33:22 -070012039 tp->tg3_flags2 |= TG3_FLG2_MII_SERDES;
12040 else
12041 tp->tg3_flags2 |= TG3_FLG2_PHY_SERDES;
12042 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070012043
John W. Linvillecbf46852005-04-21 17:01:29 -070012044 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012045 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
12046 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070012047 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070012048 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
12049
12050 switch (led_cfg) {
12051 default:
12052 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
12053 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12054 break;
12055
12056 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
12057 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12058 break;
12059
12060 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
12061 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070012062
12063 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
12064 * read on some older 5700/5701 bootcode.
12065 */
12066 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
12067 ASIC_REV_5700 ||
12068 GET_ASIC_REV(tp->pci_chip_rev_id) ==
12069 ASIC_REV_5701)
12070 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12071
Linus Torvalds1da177e2005-04-16 15:20:36 -070012072 break;
12073
12074 case SHASTA_EXT_LED_SHARED:
12075 tp->led_ctrl = LED_CTRL_MODE_SHARED;
12076 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
12077 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
12078 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12079 LED_CTRL_MODE_PHY_2);
12080 break;
12081
12082 case SHASTA_EXT_LED_MAC:
12083 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
12084 break;
12085
12086 case SHASTA_EXT_LED_COMBO:
12087 tp->led_ctrl = LED_CTRL_MODE_COMBO;
12088 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
12089 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12090 LED_CTRL_MODE_PHY_2);
12091 break;
12092
Stephen Hemminger855e1112008-04-16 16:37:28 -070012093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012094
12095 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
12096 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
12097 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
12098 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12099
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012100 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
12101 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080012102
Michael Chan9d26e212006-12-07 00:21:14 -080012103 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012104 tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080012105 if ((tp->pdev->subsystem_vendor ==
12106 PCI_VENDOR_ID_ARIMA) &&
12107 (tp->pdev->subsystem_device == 0x205a ||
12108 tp->pdev->subsystem_device == 0x2063))
12109 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
12110 } else {
David S. Millerf49639e2006-06-09 11:58:36 -070012111 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080012112 tp->tg3_flags2 |= TG3_FLG2_IS_NIC;
12113 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012114
12115 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
12116 tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
John W. Linvillecbf46852005-04-21 17:01:29 -070012117 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012118 tp->tg3_flags2 |= TG3_FLG2_ASF_NEW_HANDSHAKE;
12119 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012120
12121 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
12122 (tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
Matt Carlson0d3031d2007-10-10 18:02:43 -070012123 tp->tg3_flags3 |= TG3_FLG3_ENABLE_APE;
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012124
Gary Zambranoa85feb82007-05-05 11:52:19 -070012125 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES &&
12126 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
12127 tp->tg3_flags &= ~TG3_FLAG_WOL_CAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012128
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070012129 if ((tp->tg3_flags & TG3_FLAG_WOL_CAP) &&
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012130 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE))
Matt Carlson0527ba32007-10-10 18:03:30 -070012131 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
12132
Linus Torvalds1da177e2005-04-16 15:20:36 -070012133 if (cfg2 & (1 << 17))
12134 tp->tg3_flags2 |= TG3_FLG2_CAPACITIVE_COUPLING;
12135
12136 /* serdes signal pre-emphasis in register 0x590 set by */
12137 /* bootcode if bit 18 is set */
12138 if (cfg2 & (1 << 18))
12139 tp->tg3_flags2 |= TG3_FLG2_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070012140
Matt Carlson321d32a2008-11-21 17:22:19 -080012141 if (((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
12142 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080012143 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
12144 tp->tg3_flags3 |= TG3_FLG3_PHY_ENABLE_APD;
12145
Matt Carlson8ed5d972007-05-07 00:25:49 -070012146 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
12147 u32 cfg3;
12148
12149 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
12150 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
12151 tp->tg3_flags |= TG3_FLAG_ASPM_WORKAROUND;
12152 }
Matt Carlsona9daf362008-05-25 23:49:44 -070012153
Matt Carlson14417062010-02-17 15:16:59 +000012154 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
12155 tp->tg3_flags3 |= TG3_FLG3_RGMII_INBAND_DISABLE;
Matt Carlsona9daf362008-05-25 23:49:44 -070012156 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
12157 tp->tg3_flags3 |= TG3_FLG3_RGMII_EXT_IBND_RX_EN;
12158 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
12159 tp->tg3_flags3 |= TG3_FLG3_RGMII_EXT_IBND_TX_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012160 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012161done:
12162 device_init_wakeup(&tp->pdev->dev, tp->tg3_flags & TG3_FLAG_WOL_CAP);
12163 device_set_wakeup_enable(&tp->pdev->dev,
12164 tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
Michael Chan7d0c41e2005-04-21 17:06:20 -070012165}
12166
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012167static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
12168{
12169 int i;
12170 u32 val;
12171
12172 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
12173 tw32(OTP_CTRL, cmd);
12174
12175 /* Wait for up to 1 ms for command to execute. */
12176 for (i = 0; i < 100; i++) {
12177 val = tr32(OTP_STATUS);
12178 if (val & OTP_STATUS_CMD_DONE)
12179 break;
12180 udelay(10);
12181 }
12182
12183 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
12184}
12185
12186/* Read the gphy configuration from the OTP region of the chip. The gphy
12187 * configuration is a 32-bit value that straddles the alignment boundary.
12188 * We do two 32-bit reads and then shift and merge the results.
12189 */
12190static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
12191{
12192 u32 bhalf_otp, thalf_otp;
12193
12194 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
12195
12196 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
12197 return 0;
12198
12199 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
12200
12201 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12202 return 0;
12203
12204 thalf_otp = tr32(OTP_READ_DATA);
12205
12206 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
12207
12208 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12209 return 0;
12210
12211 bhalf_otp = tr32(OTP_READ_DATA);
12212
12213 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
12214}
12215
Michael Chan7d0c41e2005-04-21 17:06:20 -070012216static int __devinit tg3_phy_probe(struct tg3 *tp)
12217{
12218 u32 hw_phy_id_1, hw_phy_id_2;
12219 u32 hw_phy_id, hw_phy_id_masked;
12220 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012221
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012222 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)
12223 return tg3_phy_init(tp);
12224
Linus Torvalds1da177e2005-04-16 15:20:36 -070012225 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010012226 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012227 */
12228 err = 0;
Matt Carlson0d3031d2007-10-10 18:02:43 -070012229 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
12230 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000012231 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012232 } else {
12233 /* Now read the physical PHY_ID from the chip and verify
12234 * that it is sane. If it doesn't look good, we fall back
12235 * to either the hard-coded table based PHY_ID and failing
12236 * that the value found in the eeprom area.
12237 */
12238 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
12239 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
12240
12241 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
12242 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
12243 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
12244
Matt Carlson79eb6902010-02-17 15:17:03 +000012245 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012246 }
12247
Matt Carlson79eb6902010-02-17 15:17:03 +000012248 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012249 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000012250 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012251 tp->tg3_flags2 |= TG3_FLG2_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070012252 else
12253 tp->tg3_flags2 &= ~TG3_FLG2_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012254 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000012255 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070012256 /* Do nothing, phy ID already set up in
12257 * tg3_get_eeprom_hw_cfg().
12258 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070012259 } else {
12260 struct subsys_tbl_ent *p;
12261
12262 /* No eeprom signature? Try the hardcoded
12263 * subsys device table.
12264 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012265 p = tg3_lookup_by_subsys(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012266 if (!p)
12267 return -ENODEV;
12268
12269 tp->phy_id = p->phy_id;
12270 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000012271 tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012272 tp->tg3_flags2 |= TG3_FLG2_PHY_SERDES;
12273 }
12274 }
12275
Michael Chan747e8f82005-07-25 12:33:22 -070012276 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES) &&
Matt Carlson0d3031d2007-10-10 18:02:43 -070012277 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070012278 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chan3600d912006-12-07 00:21:48 -080012279 u32 bmsr, adv_reg, tg3_ctrl, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012280
12281 tg3_readphy(tp, MII_BMSR, &bmsr);
12282 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
12283 (bmsr & BMSR_LSTATUS))
12284 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012285
Linus Torvalds1da177e2005-04-16 15:20:36 -070012286 err = tg3_phy_reset(tp);
12287 if (err)
12288 return err;
12289
12290 adv_reg = (ADVERTISE_10HALF | ADVERTISE_10FULL |
12291 ADVERTISE_100HALF | ADVERTISE_100FULL |
12292 ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
12293 tg3_ctrl = 0;
12294 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY)) {
12295 tg3_ctrl = (MII_TG3_CTRL_ADV_1000_HALF |
12296 MII_TG3_CTRL_ADV_1000_FULL);
12297 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
12298 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
12299 tg3_ctrl |= (MII_TG3_CTRL_AS_MASTER |
12300 MII_TG3_CTRL_ENABLE_AS_MASTER);
12301 }
12302
Michael Chan3600d912006-12-07 00:21:48 -080012303 mask = (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
12304 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
12305 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full);
12306 if (!tg3_copper_is_advertising_all(tp, mask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012307 tg3_writephy(tp, MII_ADVERTISE, adv_reg);
12308
12309 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY))
12310 tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
12311
12312 tg3_writephy(tp, MII_BMCR,
12313 BMCR_ANENABLE | BMCR_ANRESTART);
12314 }
12315 tg3_phy_set_wirespeed(tp);
12316
12317 tg3_writephy(tp, MII_ADVERTISE, adv_reg);
12318 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY))
12319 tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
12320 }
12321
12322skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000012323 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012324 err = tg3_init_5401phy_dsp(tp);
12325 if (err)
12326 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012327
Linus Torvalds1da177e2005-04-16 15:20:36 -070012328 err = tg3_init_5401phy_dsp(tp);
12329 }
12330
Michael Chan747e8f82005-07-25 12:33:22 -070012331 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012332 tp->link_config.advertising =
12333 (ADVERTISED_1000baseT_Half |
12334 ADVERTISED_1000baseT_Full |
12335 ADVERTISED_Autoneg |
12336 ADVERTISED_FIBRE);
12337 if (tp->tg3_flags & TG3_FLAG_10_100_ONLY)
12338 tp->link_config.advertising &=
12339 ~(ADVERTISED_1000baseT_Half |
12340 ADVERTISED_1000baseT_Full);
12341
12342 return err;
12343}
12344
Matt Carlson184b8902010-04-05 10:19:25 +000012345static void __devinit tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012346{
Matt Carlson184b8902010-04-05 10:19:25 +000012347 u8 vpd_data[TG3_NVM_VPD_LEN];
Matt Carlson4181b2c2010-02-26 14:04:45 +000012348 unsigned int block_end, rosize, len;
Matt Carlson184b8902010-04-05 10:19:25 +000012349 int j, i = 0;
Michael Chan1b277772006-03-20 22:27:48 -080012350 u32 magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012351
Matt Carlsondf259d82009-04-20 06:57:14 +000012352 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
12353 tg3_nvram_read(tp, 0x0, &magic))
David S. Millerf49639e2006-06-09 11:58:36 -070012354 goto out_not_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012355
Michael Chan18201802006-03-20 22:29:15 -080012356 if (magic == TG3_EEPROM_MAGIC) {
Matt Carlson141518c2009-12-03 08:36:22 +000012357 for (i = 0; i < TG3_NVM_VPD_LEN; i += 4) {
Michael Chan1b277772006-03-20 22:27:48 -080012358 u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012359
Matt Carlson6d348f22009-02-25 14:25:52 +000012360 /* The data is in little-endian format in NVRAM.
12361 * Use the big-endian read routines to preserve
12362 * the byte order as it exists in NVRAM.
12363 */
Matt Carlson141518c2009-12-03 08:36:22 +000012364 if (tg3_nvram_read_be32(tp, TG3_NVM_VPD_OFF + i, &tmp))
Michael Chan1b277772006-03-20 22:27:48 -080012365 goto out_not_found;
12366
Matt Carlson6d348f22009-02-25 14:25:52 +000012367 memcpy(&vpd_data[i], &tmp, sizeof(tmp));
Michael Chan1b277772006-03-20 22:27:48 -080012368 }
12369 } else {
Matt Carlson94c982b2009-12-03 08:36:23 +000012370 ssize_t cnt;
Matt Carlson4181b2c2010-02-26 14:04:45 +000012371 unsigned int pos = 0;
Michael Chan1b277772006-03-20 22:27:48 -080012372
Matt Carlson94c982b2009-12-03 08:36:23 +000012373 for (; pos < TG3_NVM_VPD_LEN && i < 3; i++, pos += cnt) {
12374 cnt = pci_read_vpd(tp->pdev, pos,
12375 TG3_NVM_VPD_LEN - pos,
12376 &vpd_data[pos]);
12377 if (cnt == -ETIMEDOUT || -EINTR)
12378 cnt = 0;
12379 else if (cnt < 0)
David S. Millerf49639e2006-06-09 11:58:36 -070012380 goto out_not_found;
Michael Chan1b277772006-03-20 22:27:48 -080012381 }
Matt Carlson94c982b2009-12-03 08:36:23 +000012382 if (pos != TG3_NVM_VPD_LEN)
12383 goto out_not_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012384 }
12385
Matt Carlson4181b2c2010-02-26 14:04:45 +000012386 i = pci_vpd_find_tag(vpd_data, 0, TG3_NVM_VPD_LEN,
12387 PCI_VPD_LRDT_RO_DATA);
12388 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012389 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000012390
12391 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
12392 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
12393 i += PCI_VPD_LRDT_TAG_SIZE;
12394
12395 if (block_end > TG3_NVM_VPD_LEN)
12396 goto out_not_found;
12397
Matt Carlson184b8902010-04-05 10:19:25 +000012398 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
12399 PCI_VPD_RO_KEYWORD_MFR_ID);
12400 if (j > 0) {
12401 len = pci_vpd_info_field_size(&vpd_data[j]);
12402
12403 j += PCI_VPD_INFO_FLD_HDR_SIZE;
12404 if (j + len > block_end || len != 4 ||
12405 memcmp(&vpd_data[j], "1028", 4))
12406 goto partno;
12407
12408 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
12409 PCI_VPD_RO_KEYWORD_VENDOR0);
12410 if (j < 0)
12411 goto partno;
12412
12413 len = pci_vpd_info_field_size(&vpd_data[j]);
12414
12415 j += PCI_VPD_INFO_FLD_HDR_SIZE;
12416 if (j + len > block_end)
12417 goto partno;
12418
12419 memcpy(tp->fw_ver, &vpd_data[j], len);
12420 strncat(tp->fw_ver, " bc ", TG3_NVM_VPD_LEN - len - 1);
12421 }
12422
12423partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000012424 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
12425 PCI_VPD_RO_KEYWORD_PARTNO);
12426 if (i < 0)
12427 goto out_not_found;
12428
12429 len = pci_vpd_info_field_size(&vpd_data[i]);
12430
12431 i += PCI_VPD_INFO_FLD_HDR_SIZE;
12432 if (len > TG3_BPN_SIZE ||
12433 (len + i) > TG3_NVM_VPD_LEN)
12434 goto out_not_found;
12435
12436 memcpy(tp->board_part_number, &vpd_data[i], len);
12437
12438 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012439
12440out_not_found:
Michael Chanb5d37722006-09-27 16:06:21 -070012441 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12442 strcpy(tp->board_part_number, "BCM95906");
Matt Carlsondf259d82009-04-20 06:57:14 +000012443 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12444 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
12445 strcpy(tp->board_part_number, "BCM57780");
12446 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12447 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
12448 strcpy(tp->board_part_number, "BCM57760");
12449 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12450 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
12451 strcpy(tp->board_part_number, "BCM57790");
Matt Carlson5e7ccf22009-08-25 10:08:42 +000012452 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12453 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
12454 strcpy(tp->board_part_number, "BCM57788");
Matt Carlsonb474eca2010-02-17 15:16:58 +000012455 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12456 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
12457 strcpy(tp->board_part_number, "BCM57761");
12458 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12459 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
Matt Carlsonb703df62009-12-03 08:36:21 +000012460 strcpy(tp->board_part_number, "BCM57765");
Matt Carlsonb474eca2010-02-17 15:16:58 +000012461 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12462 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
12463 strcpy(tp->board_part_number, "BCM57781");
12464 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12465 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
12466 strcpy(tp->board_part_number, "BCM57785");
12467 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12468 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
12469 strcpy(tp->board_part_number, "BCM57791");
12470 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12471 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
12472 strcpy(tp->board_part_number, "BCM57795");
Michael Chanb5d37722006-09-27 16:06:21 -070012473 else
12474 strcpy(tp->board_part_number, "none");
Linus Torvalds1da177e2005-04-16 15:20:36 -070012475}
12476
Matt Carlson9c8a6202007-10-21 16:16:08 -070012477static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
12478{
12479 u32 val;
12480
Matt Carlsone4f34112009-02-25 14:25:00 +000012481 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070012482 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000012483 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070012484 val != 0)
12485 return 0;
12486
12487 return 1;
12488}
12489
Matt Carlsonacd9c112009-02-25 14:26:33 +000012490static void __devinit tg3_read_bc_ver(struct tg3 *tp)
12491{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012492 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000012493 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012494 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000012495
12496 if (tg3_nvram_read(tp, 0xc, &offset) ||
12497 tg3_nvram_read(tp, 0x4, &start))
12498 return;
12499
12500 offset = tg3_nvram_logical_addr(tp, offset);
12501
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012502 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000012503 return;
12504
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012505 if ((val & 0xfc000000) == 0x0c000000) {
12506 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000012507 return;
12508
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012509 if (val == 0)
12510 newver = true;
12511 }
12512
Matt Carlson75f99362010-04-05 10:19:24 +000012513 dst_off = strlen(tp->fw_ver);
12514
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012515 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000012516 if (TG3_VER_SIZE - dst_off < 16 ||
12517 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012518 return;
12519
12520 offset = offset + ver_offset - start;
12521 for (i = 0; i < 16; i += 4) {
12522 __be32 v;
12523 if (tg3_nvram_read_be32(tp, offset + i, &v))
12524 return;
12525
Matt Carlson75f99362010-04-05 10:19:24 +000012526 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012527 }
12528 } else {
12529 u32 major, minor;
12530
12531 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
12532 return;
12533
12534 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
12535 TG3_NVM_BCVER_MAJSFT;
12536 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000012537 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
12538 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000012539 }
12540}
12541
Matt Carlsona6f6cb12009-02-25 14:27:43 +000012542static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
12543{
12544 u32 val, major, minor;
12545
12546 /* Use native endian representation */
12547 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
12548 return;
12549
12550 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
12551 TG3_NVM_HWSB_CFG1_MAJSFT;
12552 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
12553 TG3_NVM_HWSB_CFG1_MINSFT;
12554
12555 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
12556}
12557
Matt Carlsondfe00d72008-11-21 17:19:41 -080012558static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
12559{
12560 u32 offset, major, minor, build;
12561
Matt Carlson75f99362010-04-05 10:19:24 +000012562 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080012563
12564 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
12565 return;
12566
12567 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
12568 case TG3_EEPROM_SB_REVISION_0:
12569 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
12570 break;
12571 case TG3_EEPROM_SB_REVISION_2:
12572 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
12573 break;
12574 case TG3_EEPROM_SB_REVISION_3:
12575 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
12576 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000012577 case TG3_EEPROM_SB_REVISION_4:
12578 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
12579 break;
12580 case TG3_EEPROM_SB_REVISION_5:
12581 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
12582 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080012583 default:
12584 return;
12585 }
12586
Matt Carlsone4f34112009-02-25 14:25:00 +000012587 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080012588 return;
12589
12590 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
12591 TG3_EEPROM_SB_EDH_BLD_SHFT;
12592 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
12593 TG3_EEPROM_SB_EDH_MAJ_SHFT;
12594 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
12595
12596 if (minor > 99 || build > 26)
12597 return;
12598
Matt Carlson75f99362010-04-05 10:19:24 +000012599 offset = strlen(tp->fw_ver);
12600 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
12601 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080012602
12603 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000012604 offset = strlen(tp->fw_ver);
12605 if (offset < TG3_VER_SIZE - 1)
12606 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080012607 }
12608}
12609
Matt Carlsonacd9c112009-02-25 14:26:33 +000012610static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080012611{
12612 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000012613 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070012614
12615 for (offset = TG3_NVM_DIR_START;
12616 offset < TG3_NVM_DIR_END;
12617 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000012618 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012619 return;
12620
12621 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
12622 break;
12623 }
12624
12625 if (offset == TG3_NVM_DIR_END)
12626 return;
12627
12628 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
12629 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000012630 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012631 return;
12632
Matt Carlsone4f34112009-02-25 14:25:00 +000012633 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070012634 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000012635 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012636 return;
12637
12638 offset += val - start;
12639
Matt Carlsonacd9c112009-02-25 14:26:33 +000012640 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012641
Matt Carlsonacd9c112009-02-25 14:26:33 +000012642 tp->fw_ver[vlen++] = ',';
12643 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070012644
12645 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012646 __be32 v;
12647 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012648 return;
12649
Al Virob9fc7dc2007-12-17 22:59:57 -080012650 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012651
Matt Carlsonacd9c112009-02-25 14:26:33 +000012652 if (vlen > TG3_VER_SIZE - sizeof(v)) {
12653 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012654 break;
12655 }
12656
Matt Carlsonacd9c112009-02-25 14:26:33 +000012657 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
12658 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012659 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000012660}
12661
Matt Carlson7fd76442009-02-25 14:27:20 +000012662static void __devinit tg3_read_dash_ver(struct tg3 *tp)
12663{
12664 int vlen;
12665 u32 apedata;
12666
12667 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) ||
12668 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
12669 return;
12670
12671 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
12672 if (apedata != APE_SEG_SIG_MAGIC)
12673 return;
12674
12675 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
12676 if (!(apedata & APE_FW_STATUS_READY))
12677 return;
12678
12679 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
12680
12681 vlen = strlen(tp->fw_ver);
12682
12683 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " DASH v%d.%d.%d.%d",
12684 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
12685 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
12686 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
12687 (apedata & APE_FW_VERSION_BLDMSK));
12688}
12689
Matt Carlsonacd9c112009-02-25 14:26:33 +000012690static void __devinit tg3_read_fw_ver(struct tg3 *tp)
12691{
12692 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000012693 bool vpd_vers = false;
12694
12695 if (tp->fw_ver[0] != 0)
12696 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000012697
Matt Carlsondf259d82009-04-20 06:57:14 +000012698 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) {
Matt Carlson75f99362010-04-05 10:19:24 +000012699 strcat(tp->fw_ver, "sb");
Matt Carlsondf259d82009-04-20 06:57:14 +000012700 return;
12701 }
12702
Matt Carlsonacd9c112009-02-25 14:26:33 +000012703 if (tg3_nvram_read(tp, 0, &val))
12704 return;
12705
12706 if (val == TG3_EEPROM_MAGIC)
12707 tg3_read_bc_ver(tp);
12708 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
12709 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000012710 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
12711 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000012712 else
12713 return;
12714
12715 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
Matt Carlson75f99362010-04-05 10:19:24 +000012716 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) || vpd_vers)
12717 goto done;
Matt Carlsonacd9c112009-02-25 14:26:33 +000012718
12719 tg3_read_mgmtfw_ver(tp);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012720
Matt Carlson75f99362010-04-05 10:19:24 +000012721done:
Matt Carlson9c8a6202007-10-21 16:16:08 -070012722 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080012723}
12724
Michael Chan7544b092007-05-05 13:08:32 -070012725static struct pci_dev * __devinit tg3_find_peer(struct tg3 *);
12726
Linus Torvalds1da177e2005-04-16 15:20:36 -070012727static int __devinit tg3_get_invariants(struct tg3 *tp)
12728{
12729 static struct pci_device_id write_reorder_chipsets[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012730 { PCI_DEVICE(PCI_VENDOR_ID_AMD,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012731 PCI_DEVICE_ID_AMD_FE_GATE_700C) },
John W. Linvillec165b002006-07-08 13:28:53 -070012732 { PCI_DEVICE(PCI_VENDOR_ID_AMD,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012733 PCI_DEVICE_ID_AMD_8131_BRIDGE) },
Michael Chan399de502005-10-03 14:02:39 -070012734 { PCI_DEVICE(PCI_VENDOR_ID_VIA,
12735 PCI_DEVICE_ID_VIA_8385_0) },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012736 { },
12737 };
12738 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012739 u32 pci_state_reg, grc_misc_cfg;
12740 u32 val;
12741 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080012742 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012743
Linus Torvalds1da177e2005-04-16 15:20:36 -070012744 /* Force memory write invalidate off. If we leave it on,
12745 * then on 5700_BX chips we have to enable a workaround.
12746 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
12747 * to match the cacheline size. The Broadcom driver have this
12748 * workaround but turns MWI off all the times so never uses
12749 * it. This seems to suggest that the workaround is insufficient.
12750 */
12751 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
12752 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
12753 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
12754
12755 /* It is absolutely critical that TG3PCI_MISC_HOST_CTRL
12756 * has the register indirect write enable bit set before
12757 * we try to access any of the MMIO registers. It is also
12758 * critical that the PCI-X hw workaround situation is decided
12759 * before that as well.
12760 */
12761 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
12762 &misc_ctrl_reg);
12763
12764 tp->pci_chip_rev_id = (misc_ctrl_reg >>
12765 MISC_HOST_CTRL_CHIPREV_SHIFT);
Matt Carlson795d01c2007-10-07 23:28:17 -070012766 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
12767 u32 prod_id_asic_rev;
12768
Matt Carlson5001e2f2009-11-13 13:03:51 +000012769 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
12770 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
12771 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5724)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012772 pci_read_config_dword(tp->pdev,
12773 TG3PCI_GEN2_PRODID_ASICREV,
12774 &prod_id_asic_rev);
Matt Carlsonb703df62009-12-03 08:36:21 +000012775 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
12776 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
12777 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
12778 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
12779 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
12780 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
12781 pci_read_config_dword(tp->pdev,
12782 TG3PCI_GEN15_PRODID_ASICREV,
12783 &prod_id_asic_rev);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012784 else
12785 pci_read_config_dword(tp->pdev, TG3PCI_PRODID_ASICREV,
12786 &prod_id_asic_rev);
12787
Matt Carlson321d32a2008-11-21 17:22:19 -080012788 tp->pci_chip_rev_id = prod_id_asic_rev;
Matt Carlson795d01c2007-10-07 23:28:17 -070012789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012790
Michael Chanff645be2005-04-21 17:09:53 -070012791 /* Wrong chip ID in 5752 A0. This code can be removed later
12792 * as A0 is not in production.
12793 */
12794 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
12795 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
12796
Michael Chan68929142005-08-09 20:17:14 -070012797 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
12798 * we need to disable memory and use config. cycles
12799 * only to access all registers. The 5702/03 chips
12800 * can mistakenly decode the special cycles from the
12801 * ICH chipsets as memory write cycles, causing corruption
12802 * of register and memory space. Only certain ICH bridges
12803 * will drive special cycles with non-zero data during the
12804 * address phase which can fall within the 5703's address
12805 * range. This is not an ICH bug as the PCI spec allows
12806 * non-zero address during special cycles. However, only
12807 * these ICH bridges are known to drive non-zero addresses
12808 * during special cycles.
12809 *
12810 * Since special cycles do not cross PCI bridges, we only
12811 * enable this workaround if the 5703 is on the secondary
12812 * bus of these ICH bridges.
12813 */
12814 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
12815 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
12816 static struct tg3_dev_id {
12817 u32 vendor;
12818 u32 device;
12819 u32 rev;
12820 } ich_chipsets[] = {
12821 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
12822 PCI_ANY_ID },
12823 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
12824 PCI_ANY_ID },
12825 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
12826 0xa },
12827 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
12828 PCI_ANY_ID },
12829 { },
12830 };
12831 struct tg3_dev_id *pci_id = &ich_chipsets[0];
12832 struct pci_dev *bridge = NULL;
12833
12834 while (pci_id->vendor != 0) {
12835 bridge = pci_get_device(pci_id->vendor, pci_id->device,
12836 bridge);
12837 if (!bridge) {
12838 pci_id++;
12839 continue;
12840 }
12841 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070012842 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070012843 continue;
12844 }
12845 if (bridge->subordinate &&
12846 (bridge->subordinate->number ==
12847 tp->pdev->bus->number)) {
12848
12849 tp->tg3_flags2 |= TG3_FLG2_ICH_WORKAROUND;
12850 pci_dev_put(bridge);
12851 break;
12852 }
12853 }
12854 }
12855
Matt Carlson41588ba2008-04-19 18:12:33 -070012856 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
12857 static struct tg3_dev_id {
12858 u32 vendor;
12859 u32 device;
12860 } bridge_chipsets[] = {
12861 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
12862 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
12863 { },
12864 };
12865 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
12866 struct pci_dev *bridge = NULL;
12867
12868 while (pci_id->vendor != 0) {
12869 bridge = pci_get_device(pci_id->vendor,
12870 pci_id->device,
12871 bridge);
12872 if (!bridge) {
12873 pci_id++;
12874 continue;
12875 }
12876 if (bridge->subordinate &&
12877 (bridge->subordinate->number <=
12878 tp->pdev->bus->number) &&
12879 (bridge->subordinate->subordinate >=
12880 tp->pdev->bus->number)) {
12881 tp->tg3_flags3 |= TG3_FLG3_5701_DMA_BUG;
12882 pci_dev_put(bridge);
12883 break;
12884 }
12885 }
12886 }
12887
Michael Chan4a29cc22006-03-19 13:21:12 -080012888 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
12889 * DMA addresses > 40-bit. This bridge may have other additional
12890 * 57xx devices behind it in some 4-port NIC designs for example.
12891 * Any tg3 device found behind the bridge will also need the 40-bit
12892 * DMA workaround.
12893 */
Michael Chana4e2b342005-10-26 15:46:52 -070012894 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
12895 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
12896 tp->tg3_flags2 |= TG3_FLG2_5780_CLASS;
Michael Chan4a29cc22006-03-19 13:21:12 -080012897 tp->tg3_flags |= TG3_FLAG_40BIT_DMA_BUG;
Michael Chan4cf78e42005-07-25 12:29:19 -070012898 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a588792010-04-05 10:19:28 +000012899 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080012900 struct pci_dev *bridge = NULL;
12901
12902 do {
12903 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
12904 PCI_DEVICE_ID_SERVERWORKS_EPB,
12905 bridge);
12906 if (bridge && bridge->subordinate &&
12907 (bridge->subordinate->number <=
12908 tp->pdev->bus->number) &&
12909 (bridge->subordinate->subordinate >=
12910 tp->pdev->bus->number)) {
12911 tp->tg3_flags |= TG3_FLAG_40BIT_DMA_BUG;
12912 pci_dev_put(bridge);
12913 break;
12914 }
12915 } while (bridge);
12916 }
Michael Chan4cf78e42005-07-25 12:29:19 -070012917
Linus Torvalds1da177e2005-04-16 15:20:36 -070012918 /* Initialize misc host control in PCI block. */
12919 tp->misc_host_ctrl |= (misc_ctrl_reg &
12920 MISC_HOST_CTRL_CHIPREV);
12921 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
12922 tp->misc_host_ctrl);
12923
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012924 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
12925 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
12926 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Michael Chan7544b092007-05-05 13:08:32 -070012927 tp->pdev_peer = tg3_find_peer(tp);
12928
Matt Carlson321d32a2008-11-21 17:22:19 -080012929 /* Intentionally exclude ASIC_REV_5906 */
12930 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Michael Chand9ab5ad2006-03-20 22:27:35 -080012931 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070012932 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070012933 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070012934 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012935 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlsonb703df62009-12-03 08:36:21 +000012936 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
12937 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson321d32a2008-11-21 17:22:19 -080012938 tp->tg3_flags3 |= TG3_FLG3_5755_PLUS;
12939
12940 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
12941 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Michael Chanb5d37722006-09-27 16:06:21 -070012942 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080012943 (tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
Michael Chana4e2b342005-10-26 15:46:52 -070012944 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
John W. Linville6708e5c2005-04-21 17:00:52 -070012945 tp->tg3_flags2 |= TG3_FLG2_5750_PLUS;
12946
John W. Linville1b440c562005-04-21 17:03:18 -070012947 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) ||
12948 (tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
12949 tp->tg3_flags2 |= TG3_FLG2_5705_PLUS;
12950
Matt Carlson027455a2008-12-21 20:19:30 -080012951 /* 5700 B0 chips do not support checksumming correctly due
12952 * to hardware bugs.
12953 */
12954 if (tp->pci_chip_rev_id == CHIPREV_ID_5700_B0)
12955 tp->tg3_flags |= TG3_FLAG_BROKEN_CHECKSUMS;
12956 else {
12957 tp->tg3_flags |= TG3_FLAG_RX_CHECKSUMS;
12958 tp->dev->features |= NETIF_F_IP_CSUM | NETIF_F_SG;
12959 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
12960 tp->dev->features |= NETIF_F_IPV6_CSUM;
12961 }
12962
Matt Carlson507399f2009-11-13 13:03:37 +000012963 /* Determine TSO capabilities */
Matt Carlsonb703df62009-12-03 08:36:21 +000012964 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
12965 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlsone849cdc2009-11-13 13:03:38 +000012966 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_3;
12967 else if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
12968 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlson507399f2009-11-13 13:03:37 +000012969 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_2;
12970 else if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
12971 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_1 | TG3_FLG2_TSO_BUG;
12972 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
12973 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
12974 tp->tg3_flags2 &= ~TG3_FLG2_TSO_BUG;
12975 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
12976 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
12977 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
12978 tp->tg3_flags2 |= TG3_FLG2_TSO_BUG;
12979 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
12980 tp->fw_needed = FIRMWARE_TG3TSO5;
12981 else
12982 tp->fw_needed = FIRMWARE_TG3TSO;
12983 }
12984
12985 tp->irq_max = 1;
12986
Michael Chan5a6f3072006-03-20 22:28:05 -080012987 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
Michael Chan7544b092007-05-05 13:08:32 -070012988 tp->tg3_flags |= TG3_FLAG_SUPPORT_MSI;
12989 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
12990 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
12991 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
12992 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
12993 tp->pdev_peer == tp->pdev))
12994 tp->tg3_flags &= ~TG3_FLAG_SUPPORT_MSI;
12995
Matt Carlson321d32a2008-11-21 17:22:19 -080012996 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070012997 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanfcfa0a32006-03-20 22:28:41 -080012998 tp->tg3_flags2 |= TG3_FLG2_1SHOT_MSI;
Michael Chan52c0fd82006-06-29 20:15:54 -070012999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013000
Matt Carlsonb703df62009-12-03 08:36:21 +000013001 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13002 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
Matt Carlson507399f2009-11-13 13:03:37 +000013003 tp->tg3_flags |= TG3_FLAG_SUPPORT_MSIX;
13004 tp->irq_max = TG3_IRQ_MAX_VECS;
13005 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013006 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000013007
Matt Carlson615774f2009-11-13 13:03:39 +000013008 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13009 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
13010 tp->tg3_flags3 |= TG3_FLG3_SHORT_DMA_BUG;
13011 else if (!(tp->tg3_flags3 & TG3_FLG3_5755_PLUS)) {
13012 tp->tg3_flags3 |= TG3_FLG3_4G_DMA_BNDRY_BUG;
13013 tp->tg3_flags3 |= TG3_FLG3_40BIT_DMA_LIMIT_BUG;
Matt Carlson0e1406d2009-11-02 12:33:33 +000013014 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013015
Matt Carlsonb703df62009-12-03 08:36:21 +000013016 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13017 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
13018 tp->tg3_flags3 |= TG3_FLG3_USE_JUMBO_BDFLAG;
13019
Matt Carlsonf51f3562008-05-25 23:45:08 -070013020 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
Matt Carlsonc6cdf432010-04-05 10:19:26 +000013021 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
13022 (tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG))
Matt Carlson8f666b02009-08-28 13:58:24 +000013023 tp->tg3_flags |= TG3_FLAG_JUMBO_CAPABLE;
Michael Chan0f893dc2005-07-25 12:30:38 -070013024
Matt Carlson52f44902008-11-21 17:17:04 -080013025 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
13026 &pci_state_reg);
13027
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013028 tp->pcie_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_EXP);
13029 if (tp->pcie_cap != 0) {
13030 u16 lnkctl;
13031
Linus Torvalds1da177e2005-04-16 15:20:36 -070013032 tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS;
Matt Carlson5f5c51e2007-11-12 21:19:37 -080013033
13034 pcie_set_readrq(tp->pdev, 4096);
13035
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013036 pci_read_config_word(tp->pdev,
13037 tp->pcie_cap + PCI_EXP_LNKCTL,
13038 &lnkctl);
13039 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
13040 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanc7835a72006-11-15 21:14:42 -080013041 tp->tg3_flags2 &= ~TG3_FLG2_HW_TSO_2;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013042 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013043 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000013044 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
13045 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013046 tp->tg3_flags3 |= TG3_FLG3_CLKREQ_BUG;
Matt Carlson614b0592010-01-20 16:58:02 +000013047 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
13048 tp->tg3_flags3 |= TG3_FLG3_L1PLLPD_EN;
Michael Chanc7835a72006-11-15 21:14:42 -080013049 }
Matt Carlson52f44902008-11-21 17:17:04 -080013050 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Matt Carlsonfcb389d2008-11-03 16:55:44 -080013051 tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS;
Matt Carlson52f44902008-11-21 17:17:04 -080013052 } else if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
13053 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
13054 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
13055 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000013056 dev_err(&tp->pdev->dev,
13057 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080013058 return -EIO;
13059 }
13060
13061 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
13062 tp->tg3_flags |= TG3_FLAG_PCIX_MODE;
13063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013064
Michael Chan399de502005-10-03 14:02:39 -070013065 /* If we have an AMD 762 or VIA K8T800 chipset, write
13066 * reordering to the mailbox registers done by the host
13067 * controller can cause major troubles. We read back from
13068 * every mailbox register write to force the writes to be
13069 * posted to the chip in order.
13070 */
13071 if (pci_dev_present(write_reorder_chipsets) &&
13072 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
13073 tp->tg3_flags |= TG3_FLAG_MBOX_WRITE_REORDER;
13074
Matt Carlson69fc4052008-12-21 20:19:57 -080013075 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
13076 &tp->pci_cacheline_sz);
13077 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13078 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013079 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
13080 tp->pci_lat_timer < 64) {
13081 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080013082 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13083 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013084 }
13085
Matt Carlson52f44902008-11-21 17:17:04 -080013086 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
13087 /* 5700 BX chips need to have their TX producer index
13088 * mailboxes written twice to workaround a bug.
13089 */
13090 tp->tg3_flags |= TG3_FLAG_TXD_MBOX_HWBUG;
Matt Carlson9974a352007-10-07 23:27:28 -070013091
Matt Carlson52f44902008-11-21 17:17:04 -080013092 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013093 *
13094 * The workaround is to use indirect register accesses
13095 * for all chip writes not to mailbox registers.
13096 */
Matt Carlson52f44902008-11-21 17:17:04 -080013097 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013098 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013099
13100 tp->tg3_flags |= TG3_FLAG_PCIX_TARGET_HWBUG;
13101
13102 /* The chip can have it's power management PCI config
13103 * space registers clobbered due to this bug.
13104 * So explicitly force the chip into D0 here.
13105 */
Matt Carlson9974a352007-10-07 23:27:28 -070013106 pci_read_config_dword(tp->pdev,
13107 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013108 &pm_reg);
13109 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
13110 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070013111 pci_write_config_dword(tp->pdev,
13112 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013113 pm_reg);
13114
13115 /* Also, force SERR#/PERR# in PCI command. */
13116 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13117 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
13118 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13119 }
13120 }
13121
Linus Torvalds1da177e2005-04-16 15:20:36 -070013122 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
13123 tp->tg3_flags |= TG3_FLAG_PCI_HIGH_SPEED;
13124 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
13125 tp->tg3_flags |= TG3_FLAG_PCI_32BIT;
13126
13127 /* Chip-specific fixup from Broadcom driver */
13128 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
13129 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
13130 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
13131 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
13132 }
13133
Michael Chan1ee582d2005-08-09 20:16:46 -070013134 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070013135 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013136 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070013137 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070013138 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013139 tp->write32_tx_mbox = tg3_write32;
13140 tp->write32_rx_mbox = tg3_write32;
13141
13142 /* Various workaround register access methods */
13143 if (tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG)
13144 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013145 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
13146 ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
13147 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
13148 /*
13149 * Back to back register writes can cause problems on these
13150 * chips, the workaround is to read back all reg writes
13151 * except those to mailbox regs.
13152 *
13153 * See tg3_write_indirect_reg32().
13154 */
Michael Chan1ee582d2005-08-09 20:16:46 -070013155 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013156 }
13157
Michael Chan1ee582d2005-08-09 20:16:46 -070013158 if ((tp->tg3_flags & TG3_FLAG_TXD_MBOX_HWBUG) ||
13159 (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)) {
13160 tp->write32_tx_mbox = tg3_write32_tx_mbox;
13161 if (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)
13162 tp->write32_rx_mbox = tg3_write_flush_reg32;
13163 }
Michael Chan20094932005-08-09 20:16:32 -070013164
Michael Chan68929142005-08-09 20:17:14 -070013165 if (tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND) {
13166 tp->read32 = tg3_read_indirect_reg32;
13167 tp->write32 = tg3_write_indirect_reg32;
13168 tp->read32_mbox = tg3_read_indirect_mbox;
13169 tp->write32_mbox = tg3_write_indirect_mbox;
13170 tp->write32_tx_mbox = tg3_write_indirect_mbox;
13171 tp->write32_rx_mbox = tg3_write_indirect_mbox;
13172
13173 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070013174 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070013175
13176 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13177 pci_cmd &= ~PCI_COMMAND_MEMORY;
13178 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13179 }
Michael Chanb5d37722006-09-27 16:06:21 -070013180 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
13181 tp->read32_mbox = tg3_read32_mbox_5906;
13182 tp->write32_mbox = tg3_write32_mbox_5906;
13183 tp->write32_tx_mbox = tg3_write32_mbox_5906;
13184 tp->write32_rx_mbox = tg3_write32_mbox_5906;
13185 }
Michael Chan68929142005-08-09 20:17:14 -070013186
Michael Chanbbadf502006-04-06 21:46:34 -070013187 if (tp->write32 == tg3_write_indirect_reg32 ||
13188 ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) &&
13189 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070013190 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Michael Chanbbadf502006-04-06 21:46:34 -070013191 tp->tg3_flags |= TG3_FLAG_SRAM_USE_CONFIG;
13192
Michael Chan7d0c41e2005-04-21 17:06:20 -070013193 /* Get eeprom hw config before calling tg3_set_power_state().
Michael Chan9d26e212006-12-07 00:21:14 -080013194 * In particular, the TG3_FLG2_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070013195 * determined before calling tg3_set_power_state() so that
13196 * we know whether or not to switch out of Vaux power.
13197 * When the flag is set, it means that GPIO1 is used for eeprom
13198 * write protect and also implies that it is a LOM where GPIOs
13199 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013200 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070013201 tg3_get_eeprom_hw_cfg(tp);
13202
Matt Carlson0d3031d2007-10-10 18:02:43 -070013203 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
13204 /* Allow reads and writes to the
13205 * APE register and memory space.
13206 */
13207 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
13208 PCISTATE_ALLOW_APE_SHMEM_WR;
13209 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
13210 pci_state_reg);
13211 }
13212
Matt Carlson9936bcf2007-10-10 18:03:07 -070013213 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013214 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013215 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013216 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlsonb703df62009-12-03 08:36:21 +000013217 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13218 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlsond30cdd22007-10-07 23:28:35 -070013219 tp->tg3_flags |= TG3_FLAG_CPMU_PRESENT;
13220
Michael Chan314fba32005-04-21 17:07:04 -070013221 /* Set up tp->grc_local_ctrl before calling tg3_set_power_state().
13222 * GPIO1 driven high will bring 5700's external PHY out of reset.
13223 * It is also used as eeprom write protect on LOMs.
13224 */
13225 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
13226 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
13227 (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT))
13228 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
13229 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070013230 /* Unused GPIO3 must be driven as output on 5752 because there
13231 * are no pull-up resistors on unused GPIO pins.
13232 */
13233 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
13234 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070013235
Matt Carlson321d32a2008-11-21 17:22:19 -080013236 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsoncb4ed1f2010-01-20 16:58:09 +000013237 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
13238 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Michael Chanaf36e6b2006-03-23 01:28:06 -080013239 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
13240
Matt Carlson8d519ab2009-04-20 06:58:01 +000013241 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
13242 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070013243 /* Turn off the debug UART. */
13244 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
13245 if (tp->tg3_flags2 & TG3_FLG2_IS_NIC)
13246 /* Keep VMain power. */
13247 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
13248 GRC_LCLCTRL_GPIO_OUTPUT0;
13249 }
13250
Linus Torvalds1da177e2005-04-16 15:20:36 -070013251 /* Force the chip into D0. */
Michael Chanbc1c7562006-03-20 17:48:03 -080013252 err = tg3_set_power_state(tp, PCI_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013253 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000013254 dev_err(&tp->pdev->dev, "Transition to D0 failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070013255 return err;
13256 }
13257
Linus Torvalds1da177e2005-04-16 15:20:36 -070013258 /* Derive initial jumbo mode from MTU assigned in
13259 * ether_setup() via the alloc_etherdev() call
13260 */
Michael Chan0f893dc2005-07-25 12:30:38 -070013261 if (tp->dev->mtu > ETH_DATA_LEN &&
Michael Chana4e2b342005-10-26 15:46:52 -070013262 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Michael Chan0f893dc2005-07-25 12:30:38 -070013263 tp->tg3_flags |= TG3_FLAG_JUMBO_RING_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013264
13265 /* Determine WakeOnLan speed to use. */
13266 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13267 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
13268 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
13269 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
13270 tp->tg3_flags &= ~(TG3_FLAG_WOL_SPEED_100MB);
13271 } else {
13272 tp->tg3_flags |= TG3_FLAG_WOL_SPEED_100MB;
13273 }
13274
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013275 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
13276 tp->tg3_flags3 |= TG3_FLG3_PHY_IS_FET;
13277
Linus Torvalds1da177e2005-04-16 15:20:36 -070013278 /* A few boards don't want Ethernet@WireSpeed phy feature */
13279 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
13280 ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
13281 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070013282 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013283 (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) ||
Michael Chan747e8f82005-07-25 12:33:22 -070013284 (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013285 tp->tg3_flags2 |= TG3_FLG2_NO_ETH_WIRE_SPEED;
13286
13287 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
13288 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
13289 tp->tg3_flags2 |= TG3_FLG2_PHY_ADC_BUG;
13290 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
13291 tp->tg3_flags2 |= TG3_FLG2_PHY_5704_A0_BUG;
13292
Matt Carlson321d32a2008-11-21 17:22:19 -080013293 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013294 !(tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080013295 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013296 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
Matt Carlsonb703df62009-12-03 08:36:21 +000013297 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
13298 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57765) {
Michael Chanc424cb22006-04-29 18:56:34 -070013299 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013300 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013301 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
13302 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080013303 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
13304 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
13305 tp->tg3_flags2 |= TG3_FLG2_PHY_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080013306 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
13307 tp->tg3_flags2 |= TG3_FLG2_PHY_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080013308 } else
Michael Chanc424cb22006-04-29 18:56:34 -070013309 tp->tg3_flags2 |= TG3_FLG2_PHY_BER_BUG;
13310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013311
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013312 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
13313 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
13314 tp->phy_otp = tg3_read_otp_phycfg(tp);
13315 if (tp->phy_otp == 0)
13316 tp->phy_otp = TG3_OTP_DEFAULT;
13317 }
13318
Matt Carlsonf51f3562008-05-25 23:45:08 -070013319 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT)
Matt Carlson8ef21422008-05-02 16:47:53 -070013320 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
13321 else
13322 tp->mi_mode = MAC_MI_MODE_BASE;
13323
Linus Torvalds1da177e2005-04-16 15:20:36 -070013324 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013325 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
13326 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
13327 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
13328
Matt Carlson321d32a2008-11-21 17:22:19 -080013329 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
13330 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson57e69832008-05-25 23:48:31 -070013331 tp->tg3_flags3 |= TG3_FLG3_USE_PHYLIB;
13332
Matt Carlson158d7ab2008-05-29 01:37:54 -070013333 err = tg3_mdio_init(tp);
13334 if (err)
13335 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013336
Matt Carlson55dffe72010-01-12 10:11:39 +000013337 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 &&
13338 (tp->pci_chip_rev_id != CHIPREV_ID_5717_A0 ||
13339 (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)))
13340 return -ENOTSUPP;
13341
Linus Torvalds1da177e2005-04-16 15:20:36 -070013342 /* Initialize data/descriptor byte/word swapping. */
13343 val = tr32(GRC_MODE);
13344 val &= GRC_MODE_HOST_STACKUP;
13345 tw32(GRC_MODE, val | tp->grc_mode);
13346
13347 tg3_switch_clocks(tp);
13348
13349 /* Clear this out for sanity. */
13350 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
13351
13352 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
13353 &pci_state_reg);
13354 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
13355 (tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG) == 0) {
13356 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
13357
13358 if (chiprevid == CHIPREV_ID_5701_A0 ||
13359 chiprevid == CHIPREV_ID_5701_B0 ||
13360 chiprevid == CHIPREV_ID_5701_B2 ||
13361 chiprevid == CHIPREV_ID_5701_B5) {
13362 void __iomem *sram_base;
13363
13364 /* Write some dummy words into the SRAM status block
13365 * area, see if it reads back correctly. If the return
13366 * value is bad, force enable the PCIX workaround.
13367 */
13368 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
13369
13370 writel(0x00000000, sram_base);
13371 writel(0x00000000, sram_base + 4);
13372 writel(0xffffffff, sram_base + 4);
13373 if (readl(sram_base) != 0x00000000)
13374 tp->tg3_flags |= TG3_FLAG_PCIX_TARGET_HWBUG;
13375 }
13376 }
13377
13378 udelay(50);
13379 tg3_nvram_init(tp);
13380
13381 grc_misc_cfg = tr32(GRC_MISC_CFG);
13382 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
13383
Linus Torvalds1da177e2005-04-16 15:20:36 -070013384 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
13385 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
13386 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
13387 tp->tg3_flags2 |= TG3_FLG2_IS_5788;
13388
David S. Millerfac9b832005-05-18 22:46:34 -070013389 if (!(tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
13390 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700))
13391 tp->tg3_flags |= TG3_FLAG_TAGGED_STATUS;
13392 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) {
13393 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
13394 HOSTCC_MODE_CLRTICK_TXBD);
13395
13396 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
13397 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13398 tp->misc_host_ctrl);
13399 }
13400
Matt Carlson3bda1252008-08-15 14:08:22 -070013401 /* Preserve the APE MAC_MODE bits */
13402 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
13403 tp->mac_mode = tr32(MAC_MODE) |
13404 MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
13405 else
13406 tp->mac_mode = TG3_DEF_MAC_MODE;
13407
Linus Torvalds1da177e2005-04-16 15:20:36 -070013408 /* these are limited to 10/100 only */
13409 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
13410 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
13411 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
13412 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
13413 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
13414 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
13415 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
13416 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
13417 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
Michael Chan676917d2006-12-07 00:20:22 -080013418 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
13419 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013420 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
Matt Carlsond1101142010-02-17 15:16:55 +000013421 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
13422 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013423 (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013424 tp->tg3_flags |= TG3_FLAG_10_100_ONLY;
13425
13426 err = tg3_phy_probe(tp);
13427 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000013428 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013429 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013430 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013431 }
13432
Matt Carlson184b8902010-04-05 10:19:25 +000013433 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080013434 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013435
13436 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
13437 tp->tg3_flags &= ~TG3_FLAG_USE_MI_INTERRUPT;
13438 } else {
13439 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
13440 tp->tg3_flags |= TG3_FLAG_USE_MI_INTERRUPT;
13441 else
13442 tp->tg3_flags &= ~TG3_FLAG_USE_MI_INTERRUPT;
13443 }
13444
13445 /* 5700 {AX,BX} chips have a broken status block link
13446 * change bit implementation, so we must use the
13447 * status register in those cases.
13448 */
13449 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
13450 tp->tg3_flags |= TG3_FLAG_USE_LINKCHG_REG;
13451 else
13452 tp->tg3_flags &= ~TG3_FLAG_USE_LINKCHG_REG;
13453
13454 /* The led_ctrl is set during tg3_phy_probe, here we might
13455 * have to force the link status polling mechanism based
13456 * upon subsystem IDs.
13457 */
13458 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070013459 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070013460 !(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)) {
13461 tp->tg3_flags |= (TG3_FLAG_USE_MI_INTERRUPT |
13462 TG3_FLAG_USE_LINKCHG_REG);
13463 }
13464
13465 /* For all SERDES we poll the MAC status register. */
13466 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
13467 tp->tg3_flags |= TG3_FLAG_POLL_SERDES;
13468 else
13469 tp->tg3_flags &= ~TG3_FLAG_POLL_SERDES;
13470
Matt Carlsonad829262008-11-21 17:16:16 -080013471 tp->rx_offset = NET_IP_ALIGN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013472 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
13473 (tp->tg3_flags & TG3_FLAG_PCIX_MODE) != 0)
13474 tp->rx_offset = 0;
13475
Michael Chanf92905d2006-06-29 20:14:29 -070013476 tp->rx_std_max_post = TG3_RX_RING_SIZE;
13477
13478 /* Increment the rx prod index on the rx std ring by at most
13479 * 8 for these chips to workaround hw errata.
13480 */
13481 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13482 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
13483 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
13484 tp->rx_std_max_post = 8;
13485
Matt Carlson8ed5d972007-05-07 00:25:49 -070013486 if (tp->tg3_flags & TG3_FLAG_ASPM_WORKAROUND)
13487 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
13488 PCIE_PWR_MGMT_L1_THRESH_MSK;
13489
Linus Torvalds1da177e2005-04-16 15:20:36 -070013490 return err;
13491}
13492
David S. Miller49b6e95f2007-03-29 01:38:42 -070013493#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070013494static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
13495{
13496 struct net_device *dev = tp->dev;
13497 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070013498 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070013499 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070013500 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013501
David S. Miller49b6e95f2007-03-29 01:38:42 -070013502 addr = of_get_property(dp, "local-mac-address", &len);
13503 if (addr && len == 6) {
13504 memcpy(dev->dev_addr, addr, 6);
13505 memcpy(dev->perm_addr, dev->dev_addr, 6);
13506 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013507 }
13508 return -ENODEV;
13509}
13510
13511static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
13512{
13513 struct net_device *dev = tp->dev;
13514
13515 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070013516 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013517 return 0;
13518}
13519#endif
13520
13521static int __devinit tg3_get_device_address(struct tg3 *tp)
13522{
13523 struct net_device *dev = tp->dev;
13524 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080013525 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013526
David S. Miller49b6e95f2007-03-29 01:38:42 -070013527#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070013528 if (!tg3_get_macaddr_sparc(tp))
13529 return 0;
13530#endif
13531
13532 mac_offset = 0x7c;
David S. Millerf49639e2006-06-09 11:58:36 -070013533 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) ||
Michael Chana4e2b342005-10-26 15:46:52 -070013534 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013535 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
13536 mac_offset = 0xcc;
13537 if (tg3_nvram_lock(tp))
13538 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
13539 else
13540 tg3_nvram_unlock(tp);
Matt Carlsona1b950d2009-09-01 13:20:17 +000013541 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
13542 if (tr32(TG3_CPMU_STATUS) & TG3_CPMU_STATUS_PCIE_FUNC)
13543 mac_offset = 0xcc;
13544 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070013545 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013546
13547 /* First try to get it from MAC address mailbox. */
13548 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
13549 if ((hi >> 16) == 0x484b) {
13550 dev->dev_addr[0] = (hi >> 8) & 0xff;
13551 dev->dev_addr[1] = (hi >> 0) & 0xff;
13552
13553 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
13554 dev->dev_addr[2] = (lo >> 24) & 0xff;
13555 dev->dev_addr[3] = (lo >> 16) & 0xff;
13556 dev->dev_addr[4] = (lo >> 8) & 0xff;
13557 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013558
Michael Chan008652b2006-03-27 23:14:53 -080013559 /* Some old bootcode may report a 0 MAC address in SRAM */
13560 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
13561 }
13562 if (!addr_ok) {
13563 /* Next, try NVRAM. */
Matt Carlsondf259d82009-04-20 06:57:14 +000013564 if (!(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) &&
13565 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000013566 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070013567 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
13568 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080013569 }
13570 /* Finally just fetch it out of the MAC control regs. */
13571 else {
13572 hi = tr32(MAC_ADDR_0_HIGH);
13573 lo = tr32(MAC_ADDR_0_LOW);
13574
13575 dev->dev_addr[5] = lo & 0xff;
13576 dev->dev_addr[4] = (lo >> 8) & 0xff;
13577 dev->dev_addr[3] = (lo >> 16) & 0xff;
13578 dev->dev_addr[2] = (lo >> 24) & 0xff;
13579 dev->dev_addr[1] = hi & 0xff;
13580 dev->dev_addr[0] = (hi >> 8) & 0xff;
13581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013582 }
13583
13584 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070013585#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070013586 if (!tg3_get_default_macaddr_sparc(tp))
13587 return 0;
13588#endif
13589 return -EINVAL;
13590 }
John W. Linville2ff43692005-09-12 14:44:20 -070013591 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013592 return 0;
13593}
13594
David S. Miller59e6b432005-05-18 22:50:10 -070013595#define BOUNDARY_SINGLE_CACHELINE 1
13596#define BOUNDARY_MULTI_CACHELINE 2
13597
13598static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
13599{
13600 int cacheline_size;
13601 u8 byte;
13602 int goal;
13603
13604 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
13605 if (byte == 0)
13606 cacheline_size = 1024;
13607 else
13608 cacheline_size = (int) byte * 4;
13609
13610 /* On 5703 and later chips, the boundary bits have no
13611 * effect.
13612 */
13613 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13614 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13615 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
13616 goto out;
13617
13618#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
13619 goal = BOUNDARY_MULTI_CACHELINE;
13620#else
13621#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
13622 goal = BOUNDARY_SINGLE_CACHELINE;
13623#else
13624 goal = 0;
13625#endif
13626#endif
13627
Matt Carlsonb703df62009-12-03 08:36:21 +000013628 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13629 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000013630 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
13631 goto out;
13632 }
13633
David S. Miller59e6b432005-05-18 22:50:10 -070013634 if (!goal)
13635 goto out;
13636
13637 /* PCI controllers on most RISC systems tend to disconnect
13638 * when a device tries to burst across a cache-line boundary.
13639 * Therefore, letting tg3 do so just wastes PCI bandwidth.
13640 *
13641 * Unfortunately, for PCI-E there are only limited
13642 * write-side controls for this, and thus for reads
13643 * we will still get the disconnects. We'll also waste
13644 * these PCI cycles for both read and write for chips
13645 * other than 5700 and 5701 which do not implement the
13646 * boundary bits.
13647 */
13648 if ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) &&
13649 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)) {
13650 switch (cacheline_size) {
13651 case 16:
13652 case 32:
13653 case 64:
13654 case 128:
13655 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13656 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
13657 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
13658 } else {
13659 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
13660 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
13661 }
13662 break;
13663
13664 case 256:
13665 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
13666 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
13667 break;
13668
13669 default:
13670 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
13671 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
13672 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070013673 }
David S. Miller59e6b432005-05-18 22:50:10 -070013674 } else if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
13675 switch (cacheline_size) {
13676 case 16:
13677 case 32:
13678 case 64:
13679 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13680 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
13681 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
13682 break;
13683 }
13684 /* fallthrough */
13685 case 128:
13686 default:
13687 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
13688 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
13689 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070013690 }
David S. Miller59e6b432005-05-18 22:50:10 -070013691 } else {
13692 switch (cacheline_size) {
13693 case 16:
13694 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13695 val |= (DMA_RWCTRL_READ_BNDRY_16 |
13696 DMA_RWCTRL_WRITE_BNDRY_16);
13697 break;
13698 }
13699 /* fallthrough */
13700 case 32:
13701 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13702 val |= (DMA_RWCTRL_READ_BNDRY_32 |
13703 DMA_RWCTRL_WRITE_BNDRY_32);
13704 break;
13705 }
13706 /* fallthrough */
13707 case 64:
13708 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13709 val |= (DMA_RWCTRL_READ_BNDRY_64 |
13710 DMA_RWCTRL_WRITE_BNDRY_64);
13711 break;
13712 }
13713 /* fallthrough */
13714 case 128:
13715 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13716 val |= (DMA_RWCTRL_READ_BNDRY_128 |
13717 DMA_RWCTRL_WRITE_BNDRY_128);
13718 break;
13719 }
13720 /* fallthrough */
13721 case 256:
13722 val |= (DMA_RWCTRL_READ_BNDRY_256 |
13723 DMA_RWCTRL_WRITE_BNDRY_256);
13724 break;
13725 case 512:
13726 val |= (DMA_RWCTRL_READ_BNDRY_512 |
13727 DMA_RWCTRL_WRITE_BNDRY_512);
13728 break;
13729 case 1024:
13730 default:
13731 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
13732 DMA_RWCTRL_WRITE_BNDRY_1024);
13733 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070013734 }
David S. Miller59e6b432005-05-18 22:50:10 -070013735 }
13736
13737out:
13738 return val;
13739}
13740
Linus Torvalds1da177e2005-04-16 15:20:36 -070013741static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
13742{
13743 struct tg3_internal_buffer_desc test_desc;
13744 u32 sram_dma_descs;
13745 int i, ret;
13746
13747 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
13748
13749 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
13750 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
13751 tw32(RDMAC_STATUS, 0);
13752 tw32(WDMAC_STATUS, 0);
13753
13754 tw32(BUFMGR_MODE, 0);
13755 tw32(FTQ_RESET, 0);
13756
13757 test_desc.addr_hi = ((u64) buf_dma) >> 32;
13758 test_desc.addr_lo = buf_dma & 0xffffffff;
13759 test_desc.nic_mbuf = 0x00002100;
13760 test_desc.len = size;
13761
13762 /*
13763 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
13764 * the *second* time the tg3 driver was getting loaded after an
13765 * initial scan.
13766 *
13767 * Broadcom tells me:
13768 * ...the DMA engine is connected to the GRC block and a DMA
13769 * reset may affect the GRC block in some unpredictable way...
13770 * The behavior of resets to individual blocks has not been tested.
13771 *
13772 * Broadcom noted the GRC reset will also reset all sub-components.
13773 */
13774 if (to_device) {
13775 test_desc.cqid_sqid = (13 << 8) | 2;
13776
13777 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
13778 udelay(40);
13779 } else {
13780 test_desc.cqid_sqid = (16 << 8) | 7;
13781
13782 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
13783 udelay(40);
13784 }
13785 test_desc.flags = 0x00000005;
13786
13787 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
13788 u32 val;
13789
13790 val = *(((u32 *)&test_desc) + i);
13791 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
13792 sram_dma_descs + (i * sizeof(u32)));
13793 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
13794 }
13795 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
13796
Matt Carlson859a588792010-04-05 10:19:28 +000013797 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013798 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a588792010-04-05 10:19:28 +000013799 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070013800 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013801
13802 ret = -ENODEV;
13803 for (i = 0; i < 40; i++) {
13804 u32 val;
13805
13806 if (to_device)
13807 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
13808 else
13809 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
13810 if ((val & 0xffff) == sram_dma_descs) {
13811 ret = 0;
13812 break;
13813 }
13814
13815 udelay(100);
13816 }
13817
13818 return ret;
13819}
13820
David S. Millerded73402005-05-23 13:59:47 -070013821#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070013822
13823static int __devinit tg3_test_dma(struct tg3 *tp)
13824{
13825 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070013826 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000013827 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013828
13829 buf = pci_alloc_consistent(tp->pdev, TEST_BUFFER_SIZE, &buf_dma);
13830 if (!buf) {
13831 ret = -ENOMEM;
13832 goto out_nofree;
13833 }
13834
13835 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
13836 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
13837
David S. Miller59e6b432005-05-18 22:50:10 -070013838 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013839
Matt Carlsonb703df62009-12-03 08:36:21 +000013840 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13841 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000013842 goto out;
13843
Linus Torvalds1da177e2005-04-16 15:20:36 -070013844 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
13845 /* DMA read watermark not used on PCIE */
13846 tp->dma_rwctrl |= 0x00180000;
13847 } else if (!(tp->tg3_flags & TG3_FLAG_PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070013848 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
13849 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013850 tp->dma_rwctrl |= 0x003f0000;
13851 else
13852 tp->dma_rwctrl |= 0x003f000f;
13853 } else {
13854 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
13855 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
13856 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080013857 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013858
Michael Chan4a29cc22006-03-19 13:21:12 -080013859 /* If the 5704 is behind the EPB bridge, we can
13860 * do the less restrictive ONE_DMA workaround for
13861 * better performance.
13862 */
13863 if ((tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG) &&
13864 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
13865 tp->dma_rwctrl |= 0x8000;
13866 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013867 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
13868
Michael Chan49afdeb2007-02-13 12:17:03 -080013869 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
13870 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070013871 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080013872 tp->dma_rwctrl |=
13873 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
13874 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
13875 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070013876 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
13877 /* 5780 always in PCIX mode */
13878 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070013879 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
13880 /* 5714 always in PCIX mode */
13881 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013882 } else {
13883 tp->dma_rwctrl |= 0x001b000f;
13884 }
13885 }
13886
13887 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
13888 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
13889 tp->dma_rwctrl &= 0xfffffff0;
13890
13891 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13892 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
13893 /* Remove this if it causes problems for some boards. */
13894 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
13895
13896 /* On 5700/5701 chips, we need to set this bit.
13897 * Otherwise the chip will issue cacheline transactions
13898 * to streamable DMA memory with not all the byte
13899 * enables turned on. This is an error on several
13900 * RISC PCI controllers, in particular sparc64.
13901 *
13902 * On 5703/5704 chips, this bit has been reassigned
13903 * a different meaning. In particular, it is used
13904 * on those chips to enable a PCI-X workaround.
13905 */
13906 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
13907 }
13908
13909 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
13910
13911#if 0
13912 /* Unneeded, already done by tg3_get_invariants. */
13913 tg3_switch_clocks(tp);
13914#endif
13915
Linus Torvalds1da177e2005-04-16 15:20:36 -070013916 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13917 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
13918 goto out;
13919
David S. Miller59e6b432005-05-18 22:50:10 -070013920 /* It is best to perform DMA test with maximum write burst size
13921 * to expose the 5700/5701 write DMA bug.
13922 */
13923 saved_dma_rwctrl = tp->dma_rwctrl;
13924 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
13925 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
13926
Linus Torvalds1da177e2005-04-16 15:20:36 -070013927 while (1) {
13928 u32 *p = buf, i;
13929
13930 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
13931 p[i] = i;
13932
13933 /* Send the buffer to the chip. */
13934 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
13935 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000013936 dev_err(&tp->pdev->dev,
13937 "%s: Buffer write failed. err = %d\n",
13938 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013939 break;
13940 }
13941
13942#if 0
13943 /* validate data reached card RAM correctly. */
13944 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
13945 u32 val;
13946 tg3_read_mem(tp, 0x2100 + (i*4), &val);
13947 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000013948 dev_err(&tp->pdev->dev,
13949 "%s: Buffer corrupted on device! "
13950 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013951 /* ret = -ENODEV here? */
13952 }
13953 p[i] = 0;
13954 }
13955#endif
13956 /* Now read it back. */
13957 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
13958 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000013959 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
13960 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013961 break;
13962 }
13963
13964 /* Verify it. */
13965 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
13966 if (p[i] == i)
13967 continue;
13968
David S. Miller59e6b432005-05-18 22:50:10 -070013969 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
13970 DMA_RWCTRL_WRITE_BNDRY_16) {
13971 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013972 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
13973 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
13974 break;
13975 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000013976 dev_err(&tp->pdev->dev,
13977 "%s: Buffer corrupted on read back! "
13978 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013979 ret = -ENODEV;
13980 goto out;
13981 }
13982 }
13983
13984 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
13985 /* Success. */
13986 ret = 0;
13987 break;
13988 }
13989 }
David S. Miller59e6b432005-05-18 22:50:10 -070013990 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
13991 DMA_RWCTRL_WRITE_BNDRY_16) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070013992 static struct pci_device_id dma_wait_state_chipsets[] = {
13993 { PCI_DEVICE(PCI_VENDOR_ID_APPLE,
13994 PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
13995 { },
13996 };
13997
David S. Miller59e6b432005-05-18 22:50:10 -070013998 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070013999 * now look for chipsets that are known to expose the
14000 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070014001 */
Michael Chan6d1cfba2005-06-08 14:13:14 -070014002 if (pci_dev_present(dma_wait_state_chipsets)) {
14003 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14004 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a588792010-04-05 10:19:28 +000014005 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014006 /* Safe to use the calculated DMA boundary. */
14007 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a588792010-04-05 10:19:28 +000014008 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070014009
David S. Miller59e6b432005-05-18 22:50:10 -070014010 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14011 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014012
14013out:
14014 pci_free_consistent(tp->pdev, TEST_BUFFER_SIZE, buf, buf_dma);
14015out_nofree:
14016 return ret;
14017}
14018
14019static void __devinit tg3_init_link_config(struct tg3 *tp)
14020{
14021 tp->link_config.advertising =
14022 (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
14023 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
14024 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full |
14025 ADVERTISED_Autoneg | ADVERTISED_MII);
14026 tp->link_config.speed = SPEED_INVALID;
14027 tp->link_config.duplex = DUPLEX_INVALID;
14028 tp->link_config.autoneg = AUTONEG_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014029 tp->link_config.active_speed = SPEED_INVALID;
14030 tp->link_config.active_duplex = DUPLEX_INVALID;
14031 tp->link_config.phy_is_low_power = 0;
14032 tp->link_config.orig_speed = SPEED_INVALID;
14033 tp->link_config.orig_duplex = DUPLEX_INVALID;
14034 tp->link_config.orig_autoneg = AUTONEG_INVALID;
14035}
14036
14037static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
14038{
Matt Carlson666bc832010-01-20 16:58:03 +000014039 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14040 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
14041 tp->bufmgr_config.mbuf_read_dma_low_water =
14042 DEFAULT_MB_RDMA_LOW_WATER_5705;
14043 tp->bufmgr_config.mbuf_mac_rx_low_water =
14044 DEFAULT_MB_MACRX_LOW_WATER_57765;
14045 tp->bufmgr_config.mbuf_high_water =
14046 DEFAULT_MB_HIGH_WATER_57765;
14047
14048 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14049 DEFAULT_MB_RDMA_LOW_WATER_5705;
14050 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14051 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
14052 tp->bufmgr_config.mbuf_high_water_jumbo =
14053 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
14054 } else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
Michael Chanfdfec1722005-07-25 12:31:48 -070014055 tp->bufmgr_config.mbuf_read_dma_low_water =
14056 DEFAULT_MB_RDMA_LOW_WATER_5705;
14057 tp->bufmgr_config.mbuf_mac_rx_low_water =
14058 DEFAULT_MB_MACRX_LOW_WATER_5705;
14059 tp->bufmgr_config.mbuf_high_water =
14060 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070014061 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
14062 tp->bufmgr_config.mbuf_mac_rx_low_water =
14063 DEFAULT_MB_MACRX_LOW_WATER_5906;
14064 tp->bufmgr_config.mbuf_high_water =
14065 DEFAULT_MB_HIGH_WATER_5906;
14066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014067
Michael Chanfdfec1722005-07-25 12:31:48 -070014068 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14069 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
14070 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14071 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
14072 tp->bufmgr_config.mbuf_high_water_jumbo =
14073 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
14074 } else {
14075 tp->bufmgr_config.mbuf_read_dma_low_water =
14076 DEFAULT_MB_RDMA_LOW_WATER;
14077 tp->bufmgr_config.mbuf_mac_rx_low_water =
14078 DEFAULT_MB_MACRX_LOW_WATER;
14079 tp->bufmgr_config.mbuf_high_water =
14080 DEFAULT_MB_HIGH_WATER;
14081
14082 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14083 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
14084 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14085 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
14086 tp->bufmgr_config.mbuf_high_water_jumbo =
14087 DEFAULT_MB_HIGH_WATER_JUMBO;
14088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014089
14090 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
14091 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
14092}
14093
14094static char * __devinit tg3_phy_string(struct tg3 *tp)
14095{
Matt Carlson79eb6902010-02-17 15:17:03 +000014096 switch (tp->phy_id & TG3_PHY_ID_MASK) {
14097 case TG3_PHY_ID_BCM5400: return "5400";
14098 case TG3_PHY_ID_BCM5401: return "5401";
14099 case TG3_PHY_ID_BCM5411: return "5411";
14100 case TG3_PHY_ID_BCM5701: return "5701";
14101 case TG3_PHY_ID_BCM5703: return "5703";
14102 case TG3_PHY_ID_BCM5704: return "5704";
14103 case TG3_PHY_ID_BCM5705: return "5705";
14104 case TG3_PHY_ID_BCM5750: return "5750";
14105 case TG3_PHY_ID_BCM5752: return "5752";
14106 case TG3_PHY_ID_BCM5714: return "5714";
14107 case TG3_PHY_ID_BCM5780: return "5780";
14108 case TG3_PHY_ID_BCM5755: return "5755";
14109 case TG3_PHY_ID_BCM5787: return "5787";
14110 case TG3_PHY_ID_BCM5784: return "5784";
14111 case TG3_PHY_ID_BCM5756: return "5722/5756";
14112 case TG3_PHY_ID_BCM5906: return "5906";
14113 case TG3_PHY_ID_BCM5761: return "5761";
14114 case TG3_PHY_ID_BCM5718C: return "5718C";
14115 case TG3_PHY_ID_BCM5718S: return "5718S";
14116 case TG3_PHY_ID_BCM57765: return "57765";
14117 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070014118 case 0: return "serdes";
14119 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070014120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014121}
14122
Michael Chanf9804dd2005-09-27 12:13:10 -070014123static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
14124{
14125 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
14126 strcpy(str, "PCI Express");
14127 return str;
14128 } else if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
14129 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
14130
14131 strcpy(str, "PCIX:");
14132
14133 if ((clock_ctrl == 7) ||
14134 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
14135 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
14136 strcat(str, "133MHz");
14137 else if (clock_ctrl == 0)
14138 strcat(str, "33MHz");
14139 else if (clock_ctrl == 2)
14140 strcat(str, "50MHz");
14141 else if (clock_ctrl == 4)
14142 strcat(str, "66MHz");
14143 else if (clock_ctrl == 6)
14144 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070014145 } else {
14146 strcpy(str, "PCI:");
14147 if (tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED)
14148 strcat(str, "66MHz");
14149 else
14150 strcat(str, "33MHz");
14151 }
14152 if (tp->tg3_flags & TG3_FLAG_PCI_32BIT)
14153 strcat(str, ":32-bit");
14154 else
14155 strcat(str, ":64-bit");
14156 return str;
14157}
14158
Michael Chan8c2dc7e2005-12-19 16:26:02 -080014159static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014160{
14161 struct pci_dev *peer;
14162 unsigned int func, devnr = tp->pdev->devfn & ~7;
14163
14164 for (func = 0; func < 8; func++) {
14165 peer = pci_get_slot(tp->pdev->bus, devnr | func);
14166 if (peer && peer != tp->pdev)
14167 break;
14168 pci_dev_put(peer);
14169 }
Michael Chan16fe9d72005-12-13 21:09:54 -080014170 /* 5704 can be configured in single-port mode, set peer to
14171 * tp->pdev in that case.
14172 */
14173 if (!peer) {
14174 peer = tp->pdev;
14175 return peer;
14176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014177
14178 /*
14179 * We don't need to keep the refcount elevated; there's no way
14180 * to remove one half of this device without removing the other
14181 */
14182 pci_dev_put(peer);
14183
14184 return peer;
14185}
14186
David S. Miller15f98502005-05-18 22:49:26 -070014187static void __devinit tg3_init_coal(struct tg3 *tp)
14188{
14189 struct ethtool_coalesce *ec = &tp->coal;
14190
14191 memset(ec, 0, sizeof(*ec));
14192 ec->cmd = ETHTOOL_GCOALESCE;
14193 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
14194 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
14195 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
14196 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
14197 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
14198 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
14199 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
14200 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
14201 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
14202
14203 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
14204 HOSTCC_MODE_CLRTICK_TXBD)) {
14205 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
14206 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
14207 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
14208 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
14209 }
Michael Chand244c892005-07-05 14:42:33 -070014210
14211 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
14212 ec->rx_coalesce_usecs_irq = 0;
14213 ec->tx_coalesce_usecs_irq = 0;
14214 ec->stats_block_coalesce_usecs = 0;
14215 }
David S. Miller15f98502005-05-18 22:49:26 -070014216}
14217
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080014218static const struct net_device_ops tg3_netdev_ops = {
14219 .ndo_open = tg3_open,
14220 .ndo_stop = tg3_close,
Stephen Hemminger00829822008-11-20 20:14:53 -080014221 .ndo_start_xmit = tg3_start_xmit,
14222 .ndo_get_stats = tg3_get_stats,
14223 .ndo_validate_addr = eth_validate_addr,
14224 .ndo_set_multicast_list = tg3_set_rx_mode,
14225 .ndo_set_mac_address = tg3_set_mac_addr,
14226 .ndo_do_ioctl = tg3_ioctl,
14227 .ndo_tx_timeout = tg3_tx_timeout,
14228 .ndo_change_mtu = tg3_change_mtu,
14229#if TG3_VLAN_TAG_USED
14230 .ndo_vlan_rx_register = tg3_vlan_rx_register,
14231#endif
14232#ifdef CONFIG_NET_POLL_CONTROLLER
14233 .ndo_poll_controller = tg3_poll_controller,
14234#endif
14235};
14236
14237static const struct net_device_ops tg3_netdev_ops_dma_bug = {
14238 .ndo_open = tg3_open,
14239 .ndo_stop = tg3_close,
14240 .ndo_start_xmit = tg3_start_xmit_dma_bug,
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080014241 .ndo_get_stats = tg3_get_stats,
14242 .ndo_validate_addr = eth_validate_addr,
14243 .ndo_set_multicast_list = tg3_set_rx_mode,
14244 .ndo_set_mac_address = tg3_set_mac_addr,
14245 .ndo_do_ioctl = tg3_ioctl,
14246 .ndo_tx_timeout = tg3_tx_timeout,
14247 .ndo_change_mtu = tg3_change_mtu,
14248#if TG3_VLAN_TAG_USED
14249 .ndo_vlan_rx_register = tg3_vlan_rx_register,
14250#endif
14251#ifdef CONFIG_NET_POLL_CONTROLLER
14252 .ndo_poll_controller = tg3_poll_controller,
14253#endif
14254};
14255
Linus Torvalds1da177e2005-04-16 15:20:36 -070014256static int __devinit tg3_init_one(struct pci_dev *pdev,
14257 const struct pci_device_id *ent)
14258{
Linus Torvalds1da177e2005-04-16 15:20:36 -070014259 struct net_device *dev;
14260 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000014261 int i, err, pm_cap;
14262 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070014263 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080014264 u64 dma_mask, persist_dma_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014265
Joe Perches05dbe002010-02-17 19:44:19 +000014266 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014267
14268 err = pci_enable_device(pdev);
14269 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014270 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014271 return err;
14272 }
14273
Linus Torvalds1da177e2005-04-16 15:20:36 -070014274 err = pci_request_regions(pdev, DRV_MODULE_NAME);
14275 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014276 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014277 goto err_out_disable_pdev;
14278 }
14279
14280 pci_set_master(pdev);
14281
14282 /* Find power-management capability. */
14283 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
14284 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000014285 dev_err(&pdev->dev,
14286 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014287 err = -EIO;
14288 goto err_out_free_res;
14289 }
14290
Matt Carlsonfe5f5782009-09-01 13:09:39 +000014291 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014292 if (!dev) {
Matt Carlson2445e462010-04-05 10:19:21 +000014293 dev_err(&pdev->dev, "Etherdev alloc failed, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014294 err = -ENOMEM;
14295 goto err_out_free_res;
14296 }
14297
Linus Torvalds1da177e2005-04-16 15:20:36 -070014298 SET_NETDEV_DEV(dev, &pdev->dev);
14299
Linus Torvalds1da177e2005-04-16 15:20:36 -070014300#if TG3_VLAN_TAG_USED
14301 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014302#endif
14303
14304 tp = netdev_priv(dev);
14305 tp->pdev = pdev;
14306 tp->dev = dev;
14307 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014308 tp->rx_mode = TG3_DEF_RX_MODE;
14309 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070014310
Linus Torvalds1da177e2005-04-16 15:20:36 -070014311 if (tg3_debug > 0)
14312 tp->msg_enable = tg3_debug;
14313 else
14314 tp->msg_enable = TG3_DEF_MSG_ENABLE;
14315
14316 /* The word/byte swap controls here control register access byte
14317 * swapping. DMA data byte swapping is controlled in the GRC_MODE
14318 * setting below.
14319 */
14320 tp->misc_host_ctrl =
14321 MISC_HOST_CTRL_MASK_PCI_INT |
14322 MISC_HOST_CTRL_WORD_SWAP |
14323 MISC_HOST_CTRL_INDIR_ACCESS |
14324 MISC_HOST_CTRL_PCISTATE_RW;
14325
14326 /* The NONFRM (non-frame) byte/word swap controls take effect
14327 * on descriptor entries, anything which isn't packet data.
14328 *
14329 * The StrongARM chips on the board (one for tx, one for rx)
14330 * are running in big-endian mode.
14331 */
14332 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
14333 GRC_MODE_WSWAP_NONFRM_DATA);
14334#ifdef __BIG_ENDIAN
14335 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
14336#endif
14337 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014338 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000014339 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014340
Matt Carlsond5fe4882008-11-21 17:20:32 -080014341 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010014342 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014343 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014344 err = -ENOMEM;
14345 goto err_out_free_dev;
14346 }
14347
14348 tg3_init_link_config(tp);
14349
Linus Torvalds1da177e2005-04-16 15:20:36 -070014350 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
14351 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014352
Linus Torvalds1da177e2005-04-16 15:20:36 -070014353 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014354 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014355 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014356
14357 err = tg3_get_invariants(tp);
14358 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014359 dev_err(&pdev->dev,
14360 "Problem fetching invariants of chip, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014361 goto err_out_iounmap;
14362 }
14363
Matt Carlson615774f2009-11-13 13:03:39 +000014364 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) &&
14365 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0)
Stephen Hemminger00829822008-11-20 20:14:53 -080014366 dev->netdev_ops = &tg3_netdev_ops;
14367 else
14368 dev->netdev_ops = &tg3_netdev_ops_dma_bug;
14369
14370
Michael Chan4a29cc22006-03-19 13:21:12 -080014371 /* The EPB bridge inside 5714, 5715, and 5780 and any
14372 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080014373 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
14374 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
14375 * do DMA address check in tg3_start_xmit().
14376 */
Michael Chan4a29cc22006-03-19 13:21:12 -080014377 if (tp->tg3_flags2 & TG3_FLG2_IS_5788)
Yang Hongyang284901a2009-04-06 19:01:15 -070014378 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Michael Chan4a29cc22006-03-19 13:21:12 -080014379 else if (tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070014380 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080014381#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070014382 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080014383#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080014384 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070014385 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080014386
14387 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070014388 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080014389 err = pci_set_dma_mask(pdev, dma_mask);
14390 if (!err) {
14391 dev->features |= NETIF_F_HIGHDMA;
14392 err = pci_set_consistent_dma_mask(pdev,
14393 persist_dma_mask);
14394 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014395 dev_err(&pdev->dev, "Unable to obtain 64 bit "
14396 "DMA for consistent allocations\n");
Michael Chan72f2afb2006-03-06 19:28:35 -080014397 goto err_out_iounmap;
14398 }
14399 }
14400 }
Yang Hongyang284901a2009-04-06 19:01:15 -070014401 if (err || dma_mask == DMA_BIT_MASK(32)) {
14402 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080014403 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014404 dev_err(&pdev->dev,
14405 "No usable DMA configuration, aborting\n");
Michael Chan72f2afb2006-03-06 19:28:35 -080014406 goto err_out_iounmap;
14407 }
14408 }
14409
Michael Chanfdfec1722005-07-25 12:31:48 -070014410 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014411
Matt Carlson507399f2009-11-13 13:03:37 +000014412 /* Selectively allow TSO based on operating conditions */
14413 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO) ||
14414 (tp->fw_needed && !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)))
14415 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
14416 else {
14417 tp->tg3_flags2 &= ~(TG3_FLG2_TSO_CAPABLE | TG3_FLG2_TSO_BUG);
14418 tp->fw_needed = NULL;
14419 }
14420
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014421 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
Matt Carlson9e9fd122009-01-19 16:57:45 -080014422 tp->fw_needed = FIRMWARE_TG3;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014423
Michael Chan4e3a7aa2006-03-20 17:47:44 -080014424 /* TSO is on by default on chips that support hardware TSO.
14425 * Firmware TSO on older chips gives lower performance, so it
14426 * is off by default, but can be enabled using ethtool.
14427 */
Matt Carlsone849cdc2009-11-13 13:03:38 +000014428 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO) &&
14429 (dev->features & NETIF_F_IP_CSUM))
14430 dev->features |= NETIF_F_TSO;
14431
14432 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_2) ||
14433 (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3)) {
14434 if (dev->features & NETIF_F_IPV6_CSUM)
Michael Chanb0026622006-07-03 19:42:14 -070014435 dev->features |= NETIF_F_TSO6;
Matt Carlsone849cdc2009-11-13 13:03:38 +000014436 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
14437 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070014438 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
14439 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014440 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000014441 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson9936bcf2007-10-10 18:03:07 -070014442 dev->features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070014443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014444
Linus Torvalds1da177e2005-04-16 15:20:36 -070014445 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
14446 !(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) &&
14447 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
14448 tp->tg3_flags2 |= TG3_FLG2_MAX_RXPEND_64;
14449 tp->rx_pending = 63;
14450 }
14451
Linus Torvalds1da177e2005-04-16 15:20:36 -070014452 err = tg3_get_device_address(tp);
14453 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014454 dev_err(&pdev->dev,
14455 "Could not obtain valid ethernet address, aborting\n");
Matt Carlson026a6c22009-12-03 08:36:24 +000014456 goto err_out_iounmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014457 }
14458
Matt Carlson0d3031d2007-10-10 18:02:43 -070014459 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
Matt Carlson63532392008-11-03 16:49:57 -080014460 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
Al Viro79ea13c2008-01-24 02:06:46 -080014461 if (!tp->aperegs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014462 dev_err(&pdev->dev,
14463 "Cannot map APE registers, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070014464 err = -ENOMEM;
Matt Carlson026a6c22009-12-03 08:36:24 +000014465 goto err_out_iounmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070014466 }
14467
14468 tg3_ape_lock_init(tp);
Matt Carlson7fd76442009-02-25 14:27:20 +000014469
14470 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF)
14471 tg3_read_dash_ver(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070014472 }
14473
Matt Carlsonc88864d2007-11-12 21:07:01 -080014474 /*
14475 * Reset chip in case UNDI or EFI driver did not shutdown
14476 * DMA self test will enable WDMAC and we'll see (spurious)
14477 * pending DMA on the PCI bus at that point.
14478 */
14479 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
14480 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
14481 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
14482 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
14483 }
14484
14485 err = tg3_test_dma(tp);
14486 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014487 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080014488 goto err_out_apeunmap;
14489 }
14490
Matt Carlsonc88864d2007-11-12 21:07:01 -080014491 /* flow control autonegotiation is default behavior */
14492 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
Steve Glendinninge18ce342008-12-16 02:00:00 -080014493 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlsonc88864d2007-11-12 21:07:01 -080014494
Matt Carlson78f90dc2009-11-13 13:03:42 +000014495 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
14496 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
14497 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
14498 for (i = 0; i < TG3_IRQ_MAX_VECS; i++) {
14499 struct tg3_napi *tnapi = &tp->napi[i];
14500
14501 tnapi->tp = tp;
14502 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
14503
14504 tnapi->int_mbox = intmbx;
14505 if (i < 4)
14506 intmbx += 0x8;
14507 else
14508 intmbx += 0x4;
14509
14510 tnapi->consmbox = rcvmbx;
14511 tnapi->prodmbox = sndmbx;
14512
14513 if (i) {
14514 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
14515 netif_napi_add(dev, &tnapi->napi, tg3_poll_msix, 64);
14516 } else {
14517 tnapi->coal_now = HOSTCC_MODE_NOW;
14518 netif_napi_add(dev, &tnapi->napi, tg3_poll, 64);
14519 }
14520
14521 if (!(tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX))
14522 break;
14523
14524 /*
14525 * If we support MSIX, we'll be using RSS. If we're using
14526 * RSS, the first vector only handles link interrupts and the
14527 * remaining vectors handle rx and tx interrupts. Reuse the
14528 * mailbox values for the next iteration. The values we setup
14529 * above are still useful for the single vectored mode.
14530 */
14531 if (!i)
14532 continue;
14533
14534 rcvmbx += 0x8;
14535
14536 if (sndmbx & 0x4)
14537 sndmbx -= 0x4;
14538 else
14539 sndmbx += 0xc;
14540 }
14541
Matt Carlsonc88864d2007-11-12 21:07:01 -080014542 tg3_init_coal(tp);
14543
Michael Chanc49a1562006-12-17 17:07:29 -080014544 pci_set_drvdata(pdev, dev);
14545
Linus Torvalds1da177e2005-04-16 15:20:36 -070014546 err = register_netdev(dev);
14547 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014548 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070014549 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014550 }
14551
Joe Perches05dbe002010-02-17 19:44:19 +000014552 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
14553 tp->board_part_number,
14554 tp->pci_chip_rev_id,
14555 tg3_bus_string(tp, str),
14556 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014557
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000014558 if (tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED) {
14559 struct phy_device *phydev;
14560 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000014561 netdev_info(dev,
14562 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000014563 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000014564 } else
Matt Carlson5129c3a2010-04-05 10:19:23 +000014565 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
14566 "(WireSpeed[%d])\n", tg3_phy_string(tp),
Joe Perches05dbe002010-02-17 19:44:19 +000014567 ((tp->tg3_flags & TG3_FLAG_10_100_ONLY) ? "10/100Base-TX" :
14568 ((tp->tg3_flags2 & TG3_FLG2_ANY_SERDES) ? "1000Base-SX" :
14569 "10/100/1000Base-T")),
14570 (tp->tg3_flags2 & TG3_FLG2_NO_ETH_WIRE_SPEED) == 0);
Matt Carlsondf59c942008-11-03 16:52:56 -080014571
Joe Perches05dbe002010-02-17 19:44:19 +000014572 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
14573 (tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) != 0,
14574 (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) != 0,
14575 (tp->tg3_flags & TG3_FLAG_USE_MI_INTERRUPT) != 0,
14576 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0,
14577 (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) != 0);
14578 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
14579 tp->dma_rwctrl,
14580 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
14581 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014582
14583 return 0;
14584
Matt Carlson0d3031d2007-10-10 18:02:43 -070014585err_out_apeunmap:
14586 if (tp->aperegs) {
14587 iounmap(tp->aperegs);
14588 tp->aperegs = NULL;
14589 }
14590
Linus Torvalds1da177e2005-04-16 15:20:36 -070014591err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070014592 if (tp->regs) {
14593 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070014594 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070014595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014596
14597err_out_free_dev:
14598 free_netdev(dev);
14599
14600err_out_free_res:
14601 pci_release_regions(pdev);
14602
14603err_out_disable_pdev:
14604 pci_disable_device(pdev);
14605 pci_set_drvdata(pdev, NULL);
14606 return err;
14607}
14608
14609static void __devexit tg3_remove_one(struct pci_dev *pdev)
14610{
14611 struct net_device *dev = pci_get_drvdata(pdev);
14612
14613 if (dev) {
14614 struct tg3 *tp = netdev_priv(dev);
14615
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014616 if (tp->fw)
14617 release_firmware(tp->fw);
14618
Michael Chan7faa0062006-02-02 17:29:28 -080014619 flush_scheduled_work();
Matt Carlson158d7ab2008-05-29 01:37:54 -070014620
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014621 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
14622 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070014623 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014624 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070014625
Linus Torvalds1da177e2005-04-16 15:20:36 -070014626 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070014627 if (tp->aperegs) {
14628 iounmap(tp->aperegs);
14629 tp->aperegs = NULL;
14630 }
Michael Chan68929142005-08-09 20:17:14 -070014631 if (tp->regs) {
14632 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070014633 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070014634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014635 free_netdev(dev);
14636 pci_release_regions(pdev);
14637 pci_disable_device(pdev);
14638 pci_set_drvdata(pdev, NULL);
14639 }
14640}
14641
14642static int tg3_suspend(struct pci_dev *pdev, pm_message_t state)
14643{
14644 struct net_device *dev = pci_get_drvdata(pdev);
14645 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070014646 pci_power_t target_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014647 int err;
14648
Michael Chan3e0c95f2007-08-03 20:56:54 -070014649 /* PCI register 4 needs to be saved whether netif_running() or not.
14650 * MSI address and data need to be saved if using MSI and
14651 * netif_running().
14652 */
14653 pci_save_state(pdev);
14654
Linus Torvalds1da177e2005-04-16 15:20:36 -070014655 if (!netif_running(dev))
14656 return 0;
14657
Michael Chan7faa0062006-02-02 17:29:28 -080014658 flush_scheduled_work();
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014659 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014660 tg3_netif_stop(tp);
14661
14662 del_timer_sync(&tp->timer);
14663
David S. Millerf47c11e2005-06-24 20:18:35 -070014664 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014665 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070014666 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014667
14668 netif_device_detach(dev);
14669
David S. Millerf47c11e2005-06-24 20:18:35 -070014670 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070014671 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan6a9eba12005-12-13 21:08:58 -080014672 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
David S. Millerf47c11e2005-06-24 20:18:35 -070014673 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014674
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070014675 target_state = pdev->pm_cap ? pci_target_state(pdev) : PCI_D3hot;
14676
14677 err = tg3_set_power_state(tp, target_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014678 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014679 int err2;
14680
David S. Millerf47c11e2005-06-24 20:18:35 -070014681 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014682
Michael Chan6a9eba12005-12-13 21:08:58 -080014683 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014684 err2 = tg3_restart_hw(tp, 1);
14685 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070014686 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014687
14688 tp->timer.expires = jiffies + tp->timer_offset;
14689 add_timer(&tp->timer);
14690
14691 netif_device_attach(dev);
14692 tg3_netif_start(tp);
14693
Michael Chanb9ec6c12006-07-25 16:37:27 -070014694out:
David S. Millerf47c11e2005-06-24 20:18:35 -070014695 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014696
14697 if (!err2)
14698 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014699 }
14700
14701 return err;
14702}
14703
14704static int tg3_resume(struct pci_dev *pdev)
14705{
14706 struct net_device *dev = pci_get_drvdata(pdev);
14707 struct tg3 *tp = netdev_priv(dev);
14708 int err;
14709
Michael Chan3e0c95f2007-08-03 20:56:54 -070014710 pci_restore_state(tp->pdev);
14711
Linus Torvalds1da177e2005-04-16 15:20:36 -070014712 if (!netif_running(dev))
14713 return 0;
14714
Michael Chanbc1c7562006-03-20 17:48:03 -080014715 err = tg3_set_power_state(tp, PCI_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014716 if (err)
14717 return err;
14718
14719 netif_device_attach(dev);
14720
David S. Millerf47c11e2005-06-24 20:18:35 -070014721 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014722
Michael Chan6a9eba12005-12-13 21:08:58 -080014723 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Michael Chanb9ec6c12006-07-25 16:37:27 -070014724 err = tg3_restart_hw(tp, 1);
14725 if (err)
14726 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014727
14728 tp->timer.expires = jiffies + tp->timer_offset;
14729 add_timer(&tp->timer);
14730
Linus Torvalds1da177e2005-04-16 15:20:36 -070014731 tg3_netif_start(tp);
14732
Michael Chanb9ec6c12006-07-25 16:37:27 -070014733out:
David S. Millerf47c11e2005-06-24 20:18:35 -070014734 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014735
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014736 if (!err)
14737 tg3_phy_start(tp);
14738
Michael Chanb9ec6c12006-07-25 16:37:27 -070014739 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014740}
14741
14742static struct pci_driver tg3_driver = {
14743 .name = DRV_MODULE_NAME,
14744 .id_table = tg3_pci_tbl,
14745 .probe = tg3_init_one,
14746 .remove = __devexit_p(tg3_remove_one),
14747 .suspend = tg3_suspend,
14748 .resume = tg3_resume
14749};
14750
14751static int __init tg3_init(void)
14752{
Jeff Garzik29917622006-08-19 17:48:59 -040014753 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014754}
14755
14756static void __exit tg3_cleanup(void)
14757{
14758 pci_unregister_driver(&tg3_driver);
14759}
14760
14761module_init(tg3_init);
14762module_exit(tg3_cleanup);