blob: efac448ded15d718aeb81bfad5e220a94fdeb6ff [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 Carlson83038a22010-06-05 17:24:39 +000070#define DRV_MODULE_VERSION "3.111"
71#define DRV_MODULE_RELDATE "June 5, 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 Carlson9dc7a112010-04-12 06:58:28 +0000129#define TG3_RX_DMA_ALIGN 16
130#define TG3_RX_HEADROOM ALIGN(VLAN_HLEN, TG3_RX_DMA_ALIGN)
131
Matt Carlson287be122009-08-28 13:58:46 +0000132#define TG3_DMA_BYTE_ENAB 64
133
134#define TG3_RX_STD_DMA_SZ 1536
135#define TG3_RX_JMB_DMA_SZ 9046
136
137#define TG3_RX_DMA_TO_MAP_SZ(x) ((x) + TG3_DMA_BYTE_ENAB)
138
139#define TG3_RX_STD_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_STD_DMA_SZ)
140#define TG3_RX_JMB_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_JMB_DMA_SZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000142#define TG3_RX_STD_BUFF_RING_SIZE \
143 (sizeof(struct ring_info) * TG3_RX_RING_SIZE)
144
145#define TG3_RX_JMB_BUFF_RING_SIZE \
146 (sizeof(struct ring_info) * TG3_RX_JUMBO_RING_SIZE)
147
Matt Carlsond2757fc2010-04-12 06:58:27 +0000148/* Due to a hardware bug, the 5701 can only DMA to memory addresses
149 * that are at least dword aligned when used in PCIX mode. The driver
150 * works around this bug by double copying the packet. This workaround
151 * is built into the normal double copy length check for efficiency.
152 *
153 * However, the double copy is only necessary on those architectures
154 * where unaligned memory accesses are inefficient. For those architectures
155 * where unaligned memory accesses incur little penalty, we can reintegrate
156 * the 5701 in the normal rx path. Doing so saves a device structure
157 * dereference by hardcoding the double copy threshold in place.
158 */
159#define TG3_RX_COPY_THRESHOLD 256
160#if NET_IP_ALIGN == 0 || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
161 #define TG3_RX_COPY_THRESH(tp) TG3_RX_COPY_THRESHOLD
162#else
163 #define TG3_RX_COPY_THRESH(tp) ((tp)->rx_copy_thresh)
164#endif
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166/* minimum number of free TX descriptors required to wake up TX process */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000167#define TG3_TX_WAKEUP_THRESH(tnapi) ((tnapi)->tx_pending / 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Matt Carlsonad829262008-11-21 17:16:16 -0800169#define TG3_RAW_IP_ALIGN 2
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171/* number of ETHTOOL_GSTATS u64's */
172#define TG3_NUM_STATS (sizeof(struct tg3_ethtool_stats)/sizeof(u64))
173
Michael Chan4cafd3f2005-05-29 14:56:34 -0700174#define TG3_NUM_TEST 6
175
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000176#define TG3_FW_UPDATE_TIMEOUT_SEC 5
177
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800178#define FIRMWARE_TG3 "tigon/tg3.bin"
179#define FIRMWARE_TG3TSO "tigon/tg3_tso.bin"
180#define FIRMWARE_TG3TSO5 "tigon/tg3_tso5.bin"
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182static char version[] __devinitdata =
Joe Perches05dbe002010-02-17 19:44:19 +0000183 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185MODULE_AUTHOR("David S. Miller (davem@redhat.com) and Jeff Garzik (jgarzik@pobox.com)");
186MODULE_DESCRIPTION("Broadcom Tigon3 ethernet driver");
187MODULE_LICENSE("GPL");
188MODULE_VERSION(DRV_MODULE_VERSION);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800189MODULE_FIRMWARE(FIRMWARE_TG3);
190MODULE_FIRMWARE(FIRMWARE_TG3TSO);
191MODULE_FIRMWARE(FIRMWARE_TG3TSO5);
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193static int tg3_debug = -1; /* -1 == use TG3_DEF_MSG_ENABLE as value */
194module_param(tg3_debug, int, 0);
195MODULE_PARM_DESC(tg3_debug, "Tigon3 bitmapped debugging message enable value");
196
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000197static DEFINE_PCI_DEVICE_TABLE(tg3_pci_tbl) = {
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700198 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5700)},
199 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5701)},
200 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702)},
201 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703)},
202 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704)},
203 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702FE)},
204 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705)},
205 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705_2)},
206 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M)},
207 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M_2)},
208 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702X)},
209 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703X)},
210 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S)},
211 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702A3)},
212 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703A3)},
213 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5782)},
214 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5788)},
215 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5789)},
216 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901)},
217 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901_2)},
218 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S_2)},
219 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705F)},
220 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5720)},
221 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5721)},
Michael Chan126a3362006-09-27 16:03:07 -0700222 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5722)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700223 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5750)},
224 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751)},
225 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5750M)},
226 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751M)},
227 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751F)},
228 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752)},
229 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752M)},
230 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753)},
231 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753M)},
232 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753F)},
233 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754)},
234 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754M)},
235 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755)},
236 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755M)},
Michael Chan126a3362006-09-27 16:03:07 -0700237 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5756)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700238 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5786)},
239 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787)},
240 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787M)},
Michael Chan676917d2006-12-07 00:20:22 -0800241 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787F)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700242 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714)},
243 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714S)},
244 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715)},
245 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715S)},
246 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780)},
247 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780S)},
248 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5781)},
Michael Chanb5d37722006-09-27 16:06:21 -0700249 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906)},
250 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906M)},
Matt Carlsond30cdd22007-10-07 23:28:35 -0700251 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5784)},
252 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5764)},
Matt Carlson6c7af272007-10-21 16:12:02 -0700253 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5723)},
Matt Carlson9936bcf2007-10-10 18:03:07 -0700254 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761)},
255 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761E)},
Matt Carlsonc88e6682008-11-03 16:49:18 -0800256 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761S)},
257 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761SE)},
Matt Carlson2befdce2009-08-28 12:28:45 +0000258 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_G)},
259 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_F)},
Matt Carlson321d32a2008-11-21 17:22:19 -0800260 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780)},
261 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57760)},
262 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57790)},
Matt Carlson5e7ccf22009-08-25 10:08:42 +0000263 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57788)},
Matt Carlson5001e2f2009-11-13 13:03:51 +0000264 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717)},
265 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5718)},
266 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5724)},
Matt Carlsonb0f75222010-01-20 16:58:11 +0000267 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57781)},
268 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57785)},
269 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57761)},
270 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57765)},
271 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57791)},
272 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57795)},
Matt Carlson302b5002010-06-05 17:24:38 +0000273 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5719)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700274 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9DXX)},
275 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9MXX)},
276 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1000)},
277 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1001)},
278 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1003)},
279 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC9100)},
280 {PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_TIGON3)},
281 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282};
283
284MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
285
Andreas Mohr50da8592006-08-14 23:54:30 -0700286static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 const char string[ETH_GSTRING_LEN];
288} ethtool_stats_keys[TG3_NUM_STATS] = {
289 { "rx_octets" },
290 { "rx_fragments" },
291 { "rx_ucast_packets" },
292 { "rx_mcast_packets" },
293 { "rx_bcast_packets" },
294 { "rx_fcs_errors" },
295 { "rx_align_errors" },
296 { "rx_xon_pause_rcvd" },
297 { "rx_xoff_pause_rcvd" },
298 { "rx_mac_ctrl_rcvd" },
299 { "rx_xoff_entered" },
300 { "rx_frame_too_long_errors" },
301 { "rx_jabbers" },
302 { "rx_undersize_packets" },
303 { "rx_in_length_errors" },
304 { "rx_out_length_errors" },
305 { "rx_64_or_less_octet_packets" },
306 { "rx_65_to_127_octet_packets" },
307 { "rx_128_to_255_octet_packets" },
308 { "rx_256_to_511_octet_packets" },
309 { "rx_512_to_1023_octet_packets" },
310 { "rx_1024_to_1522_octet_packets" },
311 { "rx_1523_to_2047_octet_packets" },
312 { "rx_2048_to_4095_octet_packets" },
313 { "rx_4096_to_8191_octet_packets" },
314 { "rx_8192_to_9022_octet_packets" },
315
316 { "tx_octets" },
317 { "tx_collisions" },
318
319 { "tx_xon_sent" },
320 { "tx_xoff_sent" },
321 { "tx_flow_control" },
322 { "tx_mac_errors" },
323 { "tx_single_collisions" },
324 { "tx_mult_collisions" },
325 { "tx_deferred" },
326 { "tx_excessive_collisions" },
327 { "tx_late_collisions" },
328 { "tx_collide_2times" },
329 { "tx_collide_3times" },
330 { "tx_collide_4times" },
331 { "tx_collide_5times" },
332 { "tx_collide_6times" },
333 { "tx_collide_7times" },
334 { "tx_collide_8times" },
335 { "tx_collide_9times" },
336 { "tx_collide_10times" },
337 { "tx_collide_11times" },
338 { "tx_collide_12times" },
339 { "tx_collide_13times" },
340 { "tx_collide_14times" },
341 { "tx_collide_15times" },
342 { "tx_ucast_packets" },
343 { "tx_mcast_packets" },
344 { "tx_bcast_packets" },
345 { "tx_carrier_sense_errors" },
346 { "tx_discards" },
347 { "tx_errors" },
348
349 { "dma_writeq_full" },
350 { "dma_write_prioq_full" },
351 { "rxbds_empty" },
352 { "rx_discards" },
353 { "rx_errors" },
354 { "rx_threshold_hit" },
355
356 { "dma_readq_full" },
357 { "dma_read_prioq_full" },
358 { "tx_comp_queue_full" },
359
360 { "ring_set_send_prod_index" },
361 { "ring_status_update" },
362 { "nic_irqs" },
363 { "nic_avoided_irqs" },
364 { "nic_tx_threshold_hit" }
365};
366
Andreas Mohr50da8592006-08-14 23:54:30 -0700367static const struct {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700368 const char string[ETH_GSTRING_LEN];
369} ethtool_test_keys[TG3_NUM_TEST] = {
370 { "nvram test (online) " },
371 { "link test (online) " },
372 { "register test (offline)" },
373 { "memory test (offline)" },
374 { "loopback test (offline)" },
375 { "interrupt test (offline)" },
376};
377
Michael Chanb401e9e2005-12-19 16:27:04 -0800378static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
379{
380 writel(val, tp->regs + off);
381}
382
383static u32 tg3_read32(struct tg3 *tp, u32 off)
384{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000385 return readl(tp->regs + off);
Michael Chanb401e9e2005-12-19 16:27:04 -0800386}
387
Matt Carlson0d3031d2007-10-10 18:02:43 -0700388static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
389{
390 writel(val, tp->aperegs + off);
391}
392
393static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
394{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000395 return readl(tp->aperegs + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700396}
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
399{
Michael Chan68929142005-08-09 20:17:14 -0700400 unsigned long flags;
401
402 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700403 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
404 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
Michael Chan68929142005-08-09 20:17:14 -0700405 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700406}
407
408static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
409{
410 writel(val, tp->regs + off);
411 readl(tp->regs + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}
413
Michael Chan68929142005-08-09 20:17:14 -0700414static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
415{
416 unsigned long flags;
417 u32 val;
418
419 spin_lock_irqsave(&tp->indirect_lock, flags);
420 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
421 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
422 spin_unlock_irqrestore(&tp->indirect_lock, flags);
423 return val;
424}
425
426static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
427{
428 unsigned long flags;
429
430 if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
431 pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
432 TG3_64BIT_REG_LOW, val);
433 return;
434 }
Matt Carlson66711e62009-11-13 13:03:49 +0000435 if (off == TG3_RX_STD_PROD_IDX_REG) {
Michael Chan68929142005-08-09 20:17:14 -0700436 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
437 TG3_64BIT_REG_LOW, val);
438 return;
439 }
440
441 spin_lock_irqsave(&tp->indirect_lock, flags);
442 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
443 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
444 spin_unlock_irqrestore(&tp->indirect_lock, flags);
445
446 /* In indirect mode when disabling interrupts, we also need
447 * to clear the interrupt bit in the GRC local ctrl register.
448 */
449 if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
450 (val == 0x1)) {
451 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
452 tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
453 }
454}
455
456static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
457{
458 unsigned long flags;
459 u32 val;
460
461 spin_lock_irqsave(&tp->indirect_lock, flags);
462 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
463 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
464 spin_unlock_irqrestore(&tp->indirect_lock, flags);
465 return val;
466}
467
Michael Chanb401e9e2005-12-19 16:27:04 -0800468/* usec_wait specifies the wait time in usec when writing to certain registers
469 * where it is unsafe to read back the register without some delay.
470 * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
471 * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
472 */
473static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
Michael Chanb401e9e2005-12-19 16:27:04 -0800475 if ((tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG) ||
476 (tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND))
477 /* Non-posted methods */
478 tp->write32(tp, off, val);
479 else {
480 /* Posted method */
481 tg3_write32(tp, off, val);
482 if (usec_wait)
483 udelay(usec_wait);
484 tp->read32(tp, off);
485 }
486 /* Wait again after the read for the posted method to guarantee that
487 * the wait time is met.
488 */
489 if (usec_wait)
490 udelay(usec_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491}
492
Michael Chan09ee9292005-08-09 20:17:00 -0700493static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
494{
495 tp->write32_mbox(tp, off, val);
Michael Chan68929142005-08-09 20:17:14 -0700496 if (!(tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER) &&
497 !(tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND))
498 tp->read32_mbox(tp, off);
Michael Chan09ee9292005-08-09 20:17:00 -0700499}
500
Michael Chan20094932005-08-09 20:16:32 -0700501static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
503 void __iomem *mbox = tp->regs + off;
504 writel(val, mbox);
505 if (tp->tg3_flags & TG3_FLAG_TXD_MBOX_HWBUG)
506 writel(val, mbox);
507 if (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)
508 readl(mbox);
509}
510
Michael Chanb5d37722006-09-27 16:06:21 -0700511static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
512{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000513 return readl(tp->regs + off + GRCMBOX_BASE);
Michael Chanb5d37722006-09-27 16:06:21 -0700514}
515
516static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
517{
518 writel(val, tp->regs + off + GRCMBOX_BASE);
519}
520
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000521#define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700522#define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000523#define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
524#define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
525#define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
Michael Chan20094932005-08-09 20:16:32 -0700526
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000527#define tw32(reg, val) tp->write32(tp, reg, val)
528#define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
529#define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
530#define tr32(reg) tp->read32(tp, reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
533{
Michael Chan68929142005-08-09 20:17:14 -0700534 unsigned long flags;
535
Michael Chanb5d37722006-09-27 16:06:21 -0700536 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) &&
537 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
538 return;
539
Michael Chan68929142005-08-09 20:17:14 -0700540 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chanbbadf502006-04-06 21:46:34 -0700541 if (tp->tg3_flags & TG3_FLAG_SRAM_USE_CONFIG) {
542 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
543 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Michael Chanbbadf502006-04-06 21:46:34 -0700545 /* Always leave this as zero. */
546 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
547 } else {
548 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
549 tw32_f(TG3PCI_MEM_WIN_DATA, val);
550
551 /* Always leave this as zero. */
552 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
553 }
Michael Chan68929142005-08-09 20:17:14 -0700554 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555}
556
557static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
558{
Michael Chan68929142005-08-09 20:17:14 -0700559 unsigned long flags;
560
Michael Chanb5d37722006-09-27 16:06:21 -0700561 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) &&
562 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
563 *val = 0;
564 return;
565 }
566
Michael Chan68929142005-08-09 20:17:14 -0700567 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chanbbadf502006-04-06 21:46:34 -0700568 if (tp->tg3_flags & TG3_FLAG_SRAM_USE_CONFIG) {
569 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
570 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Michael Chanbbadf502006-04-06 21:46:34 -0700572 /* Always leave this as zero. */
573 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
574 } else {
575 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
576 *val = tr32(TG3PCI_MEM_WIN_DATA);
577
578 /* Always leave this as zero. */
579 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
580 }
Michael Chan68929142005-08-09 20:17:14 -0700581 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582}
583
Matt Carlson0d3031d2007-10-10 18:02:43 -0700584static void tg3_ape_lock_init(struct tg3 *tp)
585{
586 int i;
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000587 u32 regbase;
588
589 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
590 regbase = TG3_APE_LOCK_GRANT;
591 else
592 regbase = TG3_APE_PER_LOCK_GRANT;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700593
594 /* Make sure the driver hasn't any stale locks. */
595 for (i = 0; i < 8; i++)
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000596 tg3_ape_write32(tp, regbase + 4 * i, APE_LOCK_GRANT_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700597}
598
599static int tg3_ape_lock(struct tg3 *tp, int locknum)
600{
601 int i, off;
602 int ret = 0;
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000603 u32 status, req, gnt;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700604
605 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
606 return 0;
607
608 switch (locknum) {
Matt Carlson33f401a2010-04-05 10:19:27 +0000609 case TG3_APE_LOCK_GRC:
610 case TG3_APE_LOCK_MEM:
611 break;
612 default:
613 return -EINVAL;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700614 }
615
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000616 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
617 req = TG3_APE_LOCK_REQ;
618 gnt = TG3_APE_LOCK_GRANT;
619 } else {
620 req = TG3_APE_PER_LOCK_REQ;
621 gnt = TG3_APE_PER_LOCK_GRANT;
622 }
623
Matt Carlson0d3031d2007-10-10 18:02:43 -0700624 off = 4 * locknum;
625
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000626 tg3_ape_write32(tp, req + off, APE_LOCK_REQ_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700627
628 /* Wait for up to 1 millisecond to acquire lock. */
629 for (i = 0; i < 100; i++) {
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000630 status = tg3_ape_read32(tp, gnt + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700631 if (status == APE_LOCK_GRANT_DRIVER)
632 break;
633 udelay(10);
634 }
635
636 if (status != APE_LOCK_GRANT_DRIVER) {
637 /* Revoke the lock request. */
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000638 tg3_ape_write32(tp, gnt + off,
Matt Carlson0d3031d2007-10-10 18:02:43 -0700639 APE_LOCK_GRANT_DRIVER);
640
641 ret = -EBUSY;
642 }
643
644 return ret;
645}
646
647static void tg3_ape_unlock(struct tg3 *tp, int locknum)
648{
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000649 u32 gnt;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700650
651 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
652 return;
653
654 switch (locknum) {
Matt Carlson33f401a2010-04-05 10:19:27 +0000655 case TG3_APE_LOCK_GRC:
656 case TG3_APE_LOCK_MEM:
657 break;
658 default:
659 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700660 }
661
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000662 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
663 gnt = TG3_APE_LOCK_GRANT;
664 else
665 gnt = TG3_APE_PER_LOCK_GRANT;
666
667 tg3_ape_write32(tp, gnt + 4 * locknum, APE_LOCK_GRANT_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700668}
669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670static void tg3_disable_ints(struct tg3 *tp)
671{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000672 int i;
673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 tw32(TG3PCI_MISC_HOST_CTRL,
675 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000676 for (i = 0; i < tp->irq_max; i++)
677 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678}
679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680static void tg3_enable_ints(struct tg3 *tp)
681{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000682 int i;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000683
Michael Chanbbe832c2005-06-24 20:20:04 -0700684 tp->irq_sync = 0;
685 wmb();
686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 tw32(TG3PCI_MISC_HOST_CTRL,
688 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000689
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000690 tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000691 for (i = 0; i < tp->irq_cnt; i++) {
692 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000693
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000694 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
695 if (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)
696 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
697
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000698 tp->coal_now |= tnapi->coal_now;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000699 }
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000700
701 /* Force an initial interrupt */
702 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) &&
703 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
704 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
705 else
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000706 tw32(HOSTCC_MODE, tp->coal_now);
707
708 tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709}
710
Matt Carlson17375d22009-08-28 14:02:18 +0000711static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
Michael Chan04237dd2005-04-25 15:17:17 -0700712{
Matt Carlson17375d22009-08-28 14:02:18 +0000713 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +0000714 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan04237dd2005-04-25 15:17:17 -0700715 unsigned int work_exists = 0;
716
717 /* check for phy events */
718 if (!(tp->tg3_flags &
719 (TG3_FLAG_USE_LINKCHG_REG |
720 TG3_FLAG_POLL_SERDES))) {
721 if (sblk->status & SD_STATUS_LINK_CHG)
722 work_exists = 1;
723 }
724 /* check for RX/TX work to do */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000725 if (sblk->idx[0].tx_consumer != tnapi->tx_cons ||
Matt Carlson8d9d7cf2009-09-01 13:19:05 +0000726 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -0700727 work_exists = 1;
728
729 return work_exists;
730}
731
Matt Carlson17375d22009-08-28 14:02:18 +0000732/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -0700733 * similar to tg3_enable_ints, but it accurately determines whether there
734 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400735 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 */
Matt Carlson17375d22009-08-28 14:02:18 +0000737static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
Matt Carlson17375d22009-08-28 14:02:18 +0000739 struct tg3 *tp = tnapi->tp;
740
Matt Carlson898a56f2009-08-28 14:02:40 +0000741 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 mmiowb();
743
David S. Millerfac9b832005-05-18 22:46:34 -0700744 /* When doing tagged status, this work check is unnecessary.
745 * The last_tag we write above tells the chip which piece of
746 * work we've completed.
747 */
748 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) &&
Matt Carlson17375d22009-08-28 14:02:18 +0000749 tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -0700750 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +0000751 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752}
753
Matt Carlsonfed97812009-09-01 13:10:19 +0000754static void tg3_napi_disable(struct tg3 *tp)
755{
756 int i;
757
758 for (i = tp->irq_cnt - 1; i >= 0; i--)
759 napi_disable(&tp->napi[i].napi);
760}
761
762static void tg3_napi_enable(struct tg3 *tp)
763{
764 int i;
765
766 for (i = 0; i < tp->irq_cnt; i++)
767 napi_enable(&tp->napi[i].napi);
768}
769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770static inline void tg3_netif_stop(struct tg3 *tp)
771{
Michael Chanbbe832c2005-06-24 20:20:04 -0700772 tp->dev->trans_start = jiffies; /* prevent tx timeout */
Matt Carlsonfed97812009-09-01 13:10:19 +0000773 tg3_napi_disable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 netif_tx_disable(tp->dev);
775}
776
777static inline void tg3_netif_start(struct tg3 *tp)
778{
Matt Carlsonfe5f5782009-09-01 13:09:39 +0000779 /* NOTE: unconditional netif_tx_wake_all_queues is only
780 * appropriate so long as all callers are assured to
781 * have free tx slots (such as after tg3_init_hw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 */
Matt Carlsonfe5f5782009-09-01 13:09:39 +0000783 netif_tx_wake_all_queues(tp->dev);
784
Matt Carlsonfed97812009-09-01 13:10:19 +0000785 tg3_napi_enable(tp);
786 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
David S. Millerf47c11e2005-06-24 20:18:35 -0700787 tg3_enable_ints(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788}
789
790static void tg3_switch_clocks(struct tg3 *tp)
791{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000792 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 u32 orig_clock_ctrl;
794
Matt Carlson795d01c2007-10-07 23:28:17 -0700795 if ((tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
796 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -0700797 return;
798
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000799 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 orig_clock_ctrl = clock_ctrl;
802 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
803 CLOCK_CTRL_CLKRUN_OENABLE |
804 0x1f);
805 tp->pci_clock_ctrl = clock_ctrl;
806
807 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
808 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800809 tw32_wait_f(TG3PCI_CLOCK_CTRL,
810 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 }
812 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800813 tw32_wait_f(TG3PCI_CLOCK_CTRL,
814 clock_ctrl |
815 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
816 40);
817 tw32_wait_f(TG3PCI_CLOCK_CTRL,
818 clock_ctrl | (CLOCK_CTRL_ALTCLK),
819 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 }
Michael Chanb401e9e2005-12-19 16:27:04 -0800821 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822}
823
824#define PHY_BUSY_LOOPS 5000
825
826static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
827{
828 u32 frame_val;
829 unsigned int loops;
830 int ret;
831
832 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
833 tw32_f(MAC_MI_MODE,
834 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
835 udelay(80);
836 }
837
838 *val = 0x0;
839
Matt Carlson882e9792009-09-01 13:21:36 +0000840 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 MI_COM_PHY_ADDR_MASK);
842 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
843 MI_COM_REG_ADDR_MASK);
844 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 tw32_f(MAC_MI_COM, frame_val);
847
848 loops = PHY_BUSY_LOOPS;
849 while (loops != 0) {
850 udelay(10);
851 frame_val = tr32(MAC_MI_COM);
852
853 if ((frame_val & MI_COM_BUSY) == 0) {
854 udelay(5);
855 frame_val = tr32(MAC_MI_COM);
856 break;
857 }
858 loops -= 1;
859 }
860
861 ret = -EBUSY;
862 if (loops != 0) {
863 *val = frame_val & MI_COM_DATA_MASK;
864 ret = 0;
865 }
866
867 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
868 tw32_f(MAC_MI_MODE, tp->mi_mode);
869 udelay(80);
870 }
871
872 return ret;
873}
874
875static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
876{
877 u32 frame_val;
878 unsigned int loops;
879 int ret;
880
Matt Carlson7f97a4b2009-08-25 10:10:03 +0000881 if ((tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) &&
Michael Chanb5d37722006-09-27 16:06:21 -0700882 (reg == MII_TG3_CTRL || reg == MII_TG3_AUX_CTRL))
883 return 0;
884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
886 tw32_f(MAC_MI_MODE,
887 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
888 udelay(80);
889 }
890
Matt Carlson882e9792009-09-01 13:21:36 +0000891 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 MI_COM_PHY_ADDR_MASK);
893 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
894 MI_COM_REG_ADDR_MASK);
895 frame_val |= (val & MI_COM_DATA_MASK);
896 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400897
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 tw32_f(MAC_MI_COM, frame_val);
899
900 loops = PHY_BUSY_LOOPS;
901 while (loops != 0) {
902 udelay(10);
903 frame_val = tr32(MAC_MI_COM);
904 if ((frame_val & MI_COM_BUSY) == 0) {
905 udelay(5);
906 frame_val = tr32(MAC_MI_COM);
907 break;
908 }
909 loops -= 1;
910 }
911
912 ret = -EBUSY;
913 if (loops != 0)
914 ret = 0;
915
916 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
917 tw32_f(MAC_MI_MODE, tp->mi_mode);
918 udelay(80);
919 }
920
921 return ret;
922}
923
Matt Carlson95e28692008-05-25 23:44:14 -0700924static int tg3_bmcr_reset(struct tg3 *tp)
925{
926 u32 phy_control;
927 int limit, err;
928
929 /* OK, reset it, and poll the BMCR_RESET bit until it
930 * clears or we time out.
931 */
932 phy_control = BMCR_RESET;
933 err = tg3_writephy(tp, MII_BMCR, phy_control);
934 if (err != 0)
935 return -EBUSY;
936
937 limit = 5000;
938 while (limit--) {
939 err = tg3_readphy(tp, MII_BMCR, &phy_control);
940 if (err != 0)
941 return -EBUSY;
942
943 if ((phy_control & BMCR_RESET) == 0) {
944 udelay(40);
945 break;
946 }
947 udelay(10);
948 }
Roel Kluind4675b52009-02-12 16:33:27 -0800949 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -0700950 return -EBUSY;
951
952 return 0;
953}
954
Matt Carlson158d7ab2008-05-29 01:37:54 -0700955static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
956{
Francois Romieu3d165432009-01-19 16:56:50 -0800957 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700958 u32 val;
959
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000960 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -0700961
962 if (tg3_readphy(tp, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000963 val = -EIO;
964
965 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -0700966
967 return val;
968}
969
970static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
971{
Francois Romieu3d165432009-01-19 16:56:50 -0800972 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000973 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700974
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000975 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -0700976
977 if (tg3_writephy(tp, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000978 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700979
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000980 spin_unlock_bh(&tp->lock);
981
982 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700983}
984
985static int tg3_mdio_reset(struct mii_bus *bp)
986{
987 return 0;
988}
989
Matt Carlson9c61d6b2008-11-03 16:54:56 -0800990static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -0700991{
992 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800993 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -0700994
Matt Carlson3f0e3ad2009-11-02 14:24:36 +0000995 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800996 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +0000997 case PHY_ID_BCM50610:
998 case PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800999 val = MAC_PHYCFG2_50610_LED_MODES;
1000 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001001 case PHY_ID_BCMAC131:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001002 val = MAC_PHYCFG2_AC131_LED_MODES;
1003 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001004 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001005 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
1006 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001007 case PHY_ID_RTL8201E:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001008 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
1009 break;
1010 default:
Matt Carlsona9daf362008-05-25 23:49:44 -07001011 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001012 }
1013
1014 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
1015 tw32(MAC_PHYCFG2, val);
1016
1017 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001018 val &= ~(MAC_PHYCFG1_RGMII_INT |
1019 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
1020 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001021 tw32(MAC_PHYCFG1, val);
1022
1023 return;
1024 }
1025
Matt Carlson14417062010-02-17 15:16:59 +00001026 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE))
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001027 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
1028 MAC_PHYCFG2_FMODE_MASK_MASK |
1029 MAC_PHYCFG2_GMODE_MASK_MASK |
1030 MAC_PHYCFG2_ACT_MASK_MASK |
1031 MAC_PHYCFG2_QUAL_MASK_MASK |
1032 MAC_PHYCFG2_INBAND_ENABLE;
1033
1034 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001035
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001036 val = tr32(MAC_PHYCFG1);
1037 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
1038 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
Matt Carlson14417062010-02-17 15:16:59 +00001039 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE)) {
Matt Carlsona9daf362008-05-25 23:49:44 -07001040 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
1041 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
1042 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
1043 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1044 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001045 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1046 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1047 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001048
Matt Carlsona9daf362008-05-25 23:49:44 -07001049 val = tr32(MAC_EXT_RGMII_MODE);
1050 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1051 MAC_RGMII_MODE_RX_QUALITY |
1052 MAC_RGMII_MODE_RX_ACTIVITY |
1053 MAC_RGMII_MODE_RX_ENG_DET |
1054 MAC_RGMII_MODE_TX_ENABLE |
1055 MAC_RGMII_MODE_TX_LOWPWR |
1056 MAC_RGMII_MODE_TX_RESET);
Matt Carlson14417062010-02-17 15:16:59 +00001057 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE)) {
Matt Carlsona9daf362008-05-25 23:49:44 -07001058 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
1059 val |= MAC_RGMII_MODE_RX_INT_B |
1060 MAC_RGMII_MODE_RX_QUALITY |
1061 MAC_RGMII_MODE_RX_ACTIVITY |
1062 MAC_RGMII_MODE_RX_ENG_DET;
1063 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
1064 val |= MAC_RGMII_MODE_TX_ENABLE |
1065 MAC_RGMII_MODE_TX_LOWPWR |
1066 MAC_RGMII_MODE_TX_RESET;
1067 }
1068 tw32(MAC_EXT_RGMII_MODE, val);
1069}
1070
Matt Carlson158d7ab2008-05-29 01:37:54 -07001071static void tg3_mdio_start(struct tg3 *tp)
1072{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001073 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1074 tw32_f(MAC_MI_MODE, tp->mi_mode);
1075 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001076
Matt Carlson9ea48182010-02-17 15:17:01 +00001077 if ((tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED) &&
1078 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1079 tg3_mdio_config_5785(tp);
1080}
1081
1082static int tg3_mdio_init(struct tg3 *tp)
1083{
1084 int i;
1085 u32 reg;
1086 struct phy_device *phydev;
1087
Matt Carlsona50d0792010-06-05 17:24:37 +00001088 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
1089 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) {
Matt Carlson9c7df912010-06-05 17:24:36 +00001090 u32 is_serdes;
Matt Carlson882e9792009-09-01 13:21:36 +00001091
Matt Carlson9c7df912010-06-05 17:24:36 +00001092 tp->phy_addr = PCI_FUNC(tp->pdev->devfn) + 1;
Matt Carlson882e9792009-09-01 13:21:36 +00001093
Matt Carlsond1ec96a2010-01-12 10:11:38 +00001094 if (tp->pci_chip_rev_id != CHIPREV_ID_5717_A0)
1095 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1096 else
1097 is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1098 TG3_CPMU_PHY_STRAP_IS_SERDES;
Matt Carlson882e9792009-09-01 13:21:36 +00001099 if (is_serdes)
1100 tp->phy_addr += 7;
1101 } else
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001102 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001103
Matt Carlson158d7ab2008-05-29 01:37:54 -07001104 tg3_mdio_start(tp);
1105
1106 if (!(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) ||
1107 (tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED))
1108 return 0;
1109
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001110 tp->mdio_bus = mdiobus_alloc();
1111 if (tp->mdio_bus == NULL)
1112 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001113
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001114 tp->mdio_bus->name = "tg3 mdio bus";
1115 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001116 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001117 tp->mdio_bus->priv = tp;
1118 tp->mdio_bus->parent = &tp->pdev->dev;
1119 tp->mdio_bus->read = &tg3_mdio_read;
1120 tp->mdio_bus->write = &tg3_mdio_write;
1121 tp->mdio_bus->reset = &tg3_mdio_reset;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001122 tp->mdio_bus->phy_mask = ~(1 << TG3_PHY_MII_ADDR);
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001123 tp->mdio_bus->irq = &tp->mdio_irq[0];
Matt Carlson158d7ab2008-05-29 01:37:54 -07001124
1125 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001126 tp->mdio_bus->irq[i] = PHY_POLL;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001127
1128 /* The bus registration will look for all the PHYs on the mdio bus.
1129 * Unfortunately, it does not ensure the PHY is powered up before
1130 * accessing the PHY ID registers. A chip reset is the
1131 * quickest way to bring the device back to an operational state..
1132 */
1133 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1134 tg3_bmcr_reset(tp);
1135
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001136 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001137 if (i) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001138 dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001139 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001140 return i;
1141 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001142
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001143 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsona9daf362008-05-25 23:49:44 -07001144
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001145 if (!phydev || !phydev->drv) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001146 dev_warn(&tp->pdev->dev, "No PHY devices\n");
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001147 mdiobus_unregister(tp->mdio_bus);
1148 mdiobus_free(tp->mdio_bus);
1149 return -ENODEV;
1150 }
1151
1152 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001153 case PHY_ID_BCM57780:
Matt Carlson321d32a2008-11-21 17:22:19 -08001154 phydev->interface = PHY_INTERFACE_MODE_GMII;
Matt Carlsonc704dc22009-11-02 14:32:12 +00001155 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08001156 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001157 case PHY_ID_BCM50610:
1158 case PHY_ID_BCM50610M:
Matt Carlson32e5a8d2009-11-02 14:31:39 +00001159 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001160 PHY_BRCM_RX_REFCLK_UNUSED |
Matt Carlson52fae082009-11-02 14:32:38 +00001161 PHY_BRCM_DIS_TXCRXC_NOENRGY |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001162 PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson14417062010-02-17 15:16:59 +00001163 if (tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE)
Matt Carlsona9daf362008-05-25 23:49:44 -07001164 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
1165 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
1166 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
1167 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
1168 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001169 /* fallthru */
Matt Carlson6a443a02010-02-17 15:17:04 +00001170 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001171 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001172 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001173 case PHY_ID_RTL8201E:
1174 case PHY_ID_BCMAC131:
Matt Carlsona9daf362008-05-25 23:49:44 -07001175 phydev->interface = PHY_INTERFACE_MODE_MII;
Matt Carlsoncdd4e09d2009-11-02 14:31:11 +00001176 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001177 tp->tg3_flags3 |= TG3_FLG3_PHY_IS_FET;
Matt Carlsona9daf362008-05-25 23:49:44 -07001178 break;
1179 }
1180
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001181 tp->tg3_flags3 |= TG3_FLG3_MDIOBUS_INITED;
1182
1183 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1184 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001185
1186 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001187}
1188
1189static void tg3_mdio_fini(struct tg3 *tp)
1190{
1191 if (tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED) {
1192 tp->tg3_flags3 &= ~TG3_FLG3_MDIOBUS_INITED;
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001193 mdiobus_unregister(tp->mdio_bus);
1194 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001195 }
1196}
1197
Matt Carlson95e28692008-05-25 23:44:14 -07001198/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001199static inline void tg3_generate_fw_event(struct tg3 *tp)
1200{
1201 u32 val;
1202
1203 val = tr32(GRC_RX_CPU_EVENT);
1204 val |= GRC_RX_CPU_DRIVER_EVENT;
1205 tw32_f(GRC_RX_CPU_EVENT, val);
1206
1207 tp->last_event_jiffies = jiffies;
1208}
1209
1210#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1211
1212/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001213static void tg3_wait_for_event_ack(struct tg3 *tp)
1214{
1215 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001216 unsigned int delay_cnt;
1217 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001218
Matt Carlson4ba526c2008-08-15 14:10:04 -07001219 /* If enough time has passed, no wait is necessary. */
1220 time_remain = (long)(tp->last_event_jiffies + 1 +
1221 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1222 (long)jiffies;
1223 if (time_remain < 0)
1224 return;
1225
1226 /* Check if we can shorten the wait time. */
1227 delay_cnt = jiffies_to_usecs(time_remain);
1228 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1229 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1230 delay_cnt = (delay_cnt >> 3) + 1;
1231
1232 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001233 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1234 break;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001235 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001236 }
1237}
1238
1239/* tp->lock is held. */
1240static void tg3_ump_link_report(struct tg3 *tp)
1241{
1242 u32 reg;
1243 u32 val;
1244
1245 if (!(tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
1246 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
1247 return;
1248
1249 tg3_wait_for_event_ack(tp);
1250
1251 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1252
1253 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1254
1255 val = 0;
1256 if (!tg3_readphy(tp, MII_BMCR, &reg))
1257 val = reg << 16;
1258 if (!tg3_readphy(tp, MII_BMSR, &reg))
1259 val |= (reg & 0xffff);
1260 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX, val);
1261
1262 val = 0;
1263 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1264 val = reg << 16;
1265 if (!tg3_readphy(tp, MII_LPA, &reg))
1266 val |= (reg & 0xffff);
1267 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 4, val);
1268
1269 val = 0;
1270 if (!(tp->tg3_flags2 & TG3_FLG2_MII_SERDES)) {
1271 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1272 val = reg << 16;
1273 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1274 val |= (reg & 0xffff);
1275 }
1276 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 8, val);
1277
1278 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1279 val = reg << 16;
1280 else
1281 val = 0;
1282 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 12, val);
1283
Matt Carlson4ba526c2008-08-15 14:10:04 -07001284 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001285}
1286
1287static void tg3_link_report(struct tg3 *tp)
1288{
1289 if (!netif_carrier_ok(tp->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001290 netif_info(tp, link, tp->dev, "Link is down\n");
Matt Carlson95e28692008-05-25 23:44:14 -07001291 tg3_ump_link_report(tp);
1292 } else if (netif_msg_link(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001293 netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
1294 (tp->link_config.active_speed == SPEED_1000 ?
1295 1000 :
1296 (tp->link_config.active_speed == SPEED_100 ?
1297 100 : 10)),
1298 (tp->link_config.active_duplex == DUPLEX_FULL ?
1299 "full" : "half"));
Matt Carlson95e28692008-05-25 23:44:14 -07001300
Joe Perches05dbe002010-02-17 19:44:19 +00001301 netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
1302 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
1303 "on" : "off",
1304 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
1305 "on" : "off");
Matt Carlson95e28692008-05-25 23:44:14 -07001306 tg3_ump_link_report(tp);
1307 }
1308}
1309
1310static u16 tg3_advert_flowctrl_1000T(u8 flow_ctrl)
1311{
1312 u16 miireg;
1313
Steve Glendinninge18ce342008-12-16 02:00:00 -08001314 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001315 miireg = ADVERTISE_PAUSE_CAP;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001316 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001317 miireg = ADVERTISE_PAUSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001318 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001319 miireg = ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1320 else
1321 miireg = 0;
1322
1323 return miireg;
1324}
1325
1326static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1327{
1328 u16 miireg;
1329
Steve Glendinninge18ce342008-12-16 02:00:00 -08001330 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001331 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001332 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001333 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001334 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001335 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1336 else
1337 miireg = 0;
1338
1339 return miireg;
1340}
1341
Matt Carlson95e28692008-05-25 23:44:14 -07001342static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1343{
1344 u8 cap = 0;
1345
1346 if (lcladv & ADVERTISE_1000XPAUSE) {
1347 if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1348 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001349 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001350 else if (rmtadv & LPA_1000XPAUSE_ASYM)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001351 cap = FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001352 } else {
1353 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001354 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001355 }
1356 } else if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1357 if ((rmtadv & LPA_1000XPAUSE) && (rmtadv & LPA_1000XPAUSE_ASYM))
Steve Glendinninge18ce342008-12-16 02:00:00 -08001358 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001359 }
1360
1361 return cap;
1362}
1363
Matt Carlsonf51f3562008-05-25 23:45:08 -07001364static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001365{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001366 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001367 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001368 u32 old_rx_mode = tp->rx_mode;
1369 u32 old_tx_mode = tp->tx_mode;
1370
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001371 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001372 autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001373 else
1374 autoneg = tp->link_config.autoneg;
1375
1376 if (autoneg == AUTONEG_ENABLE &&
Matt Carlson95e28692008-05-25 23:44:14 -07001377 (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG)) {
1378 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001379 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001380 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001381 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001382 } else
1383 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001384
Matt Carlsonf51f3562008-05-25 23:45:08 -07001385 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001386
Steve Glendinninge18ce342008-12-16 02:00:00 -08001387 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001388 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1389 else
1390 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1391
Matt Carlsonf51f3562008-05-25 23:45:08 -07001392 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001393 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001394
Steve Glendinninge18ce342008-12-16 02:00:00 -08001395 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001396 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1397 else
1398 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1399
Matt Carlsonf51f3562008-05-25 23:45:08 -07001400 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001401 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001402}
1403
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001404static void tg3_adjust_link(struct net_device *dev)
1405{
1406 u8 oldflowctrl, linkmesg = 0;
1407 u32 mac_mode, lcl_adv, rmt_adv;
1408 struct tg3 *tp = netdev_priv(dev);
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001409 struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001410
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001411 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001412
1413 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
1414 MAC_MODE_HALF_DUPLEX);
1415
1416 oldflowctrl = tp->link_config.active_flowctrl;
1417
1418 if (phydev->link) {
1419 lcl_adv = 0;
1420 rmt_adv = 0;
1421
1422 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
1423 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001424 else if (phydev->speed == SPEED_1000 ||
1425 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001426 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001427 else
1428 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001429
1430 if (phydev->duplex == DUPLEX_HALF)
1431 mac_mode |= MAC_MODE_HALF_DUPLEX;
1432 else {
1433 lcl_adv = tg3_advert_flowctrl_1000T(
1434 tp->link_config.flowctrl);
1435
1436 if (phydev->pause)
1437 rmt_adv = LPA_PAUSE_CAP;
1438 if (phydev->asym_pause)
1439 rmt_adv |= LPA_PAUSE_ASYM;
1440 }
1441
1442 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
1443 } else
1444 mac_mode |= MAC_MODE_PORT_MODE_GMII;
1445
1446 if (mac_mode != tp->mac_mode) {
1447 tp->mac_mode = mac_mode;
1448 tw32_f(MAC_MODE, tp->mac_mode);
1449 udelay(40);
1450 }
1451
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001452 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
1453 if (phydev->speed == SPEED_10)
1454 tw32(MAC_MI_STAT,
1455 MAC_MI_STAT_10MBPS_MODE |
1456 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1457 else
1458 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1459 }
1460
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001461 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
1462 tw32(MAC_TX_LENGTHS,
1463 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1464 (6 << TX_LENGTHS_IPG_SHIFT) |
1465 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
1466 else
1467 tw32(MAC_TX_LENGTHS,
1468 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1469 (6 << TX_LENGTHS_IPG_SHIFT) |
1470 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
1471
1472 if ((phydev->link && tp->link_config.active_speed == SPEED_INVALID) ||
1473 (!phydev->link && tp->link_config.active_speed != SPEED_INVALID) ||
1474 phydev->speed != tp->link_config.active_speed ||
1475 phydev->duplex != tp->link_config.active_duplex ||
1476 oldflowctrl != tp->link_config.active_flowctrl)
Matt Carlsonc6cdf432010-04-05 10:19:26 +00001477 linkmesg = 1;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001478
1479 tp->link_config.active_speed = phydev->speed;
1480 tp->link_config.active_duplex = phydev->duplex;
1481
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001482 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001483
1484 if (linkmesg)
1485 tg3_link_report(tp);
1486}
1487
1488static int tg3_phy_init(struct tg3 *tp)
1489{
1490 struct phy_device *phydev;
1491
1492 if (tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED)
1493 return 0;
1494
1495 /* Bring the PHY back to a known state. */
1496 tg3_bmcr_reset(tp);
1497
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001498 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001499
1500 /* Attach the MAC to the PHY. */
Kay Sieversfb28ad32008-11-10 13:55:14 -08001501 phydev = phy_connect(tp->dev, dev_name(&phydev->dev), tg3_adjust_link,
Matt Carlsona9daf362008-05-25 23:49:44 -07001502 phydev->dev_flags, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001503 if (IS_ERR(phydev)) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001504 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001505 return PTR_ERR(phydev);
1506 }
1507
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001508 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001509 switch (phydev->interface) {
1510 case PHY_INTERFACE_MODE_GMII:
1511 case PHY_INTERFACE_MODE_RGMII:
Matt Carlson321d32a2008-11-21 17:22:19 -08001512 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY)) {
1513 phydev->supported &= (PHY_GBIT_FEATURES |
1514 SUPPORTED_Pause |
1515 SUPPORTED_Asym_Pause);
1516 break;
1517 }
1518 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001519 case PHY_INTERFACE_MODE_MII:
1520 phydev->supported &= (PHY_BASIC_FEATURES |
1521 SUPPORTED_Pause |
1522 SUPPORTED_Asym_Pause);
1523 break;
1524 default:
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001525 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001526 return -EINVAL;
1527 }
1528
1529 tp->tg3_flags3 |= TG3_FLG3_PHY_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001530
1531 phydev->advertising = phydev->supported;
1532
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001533 return 0;
1534}
1535
1536static void tg3_phy_start(struct tg3 *tp)
1537{
1538 struct phy_device *phydev;
1539
1540 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
1541 return;
1542
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001543 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001544
1545 if (tp->link_config.phy_is_low_power) {
1546 tp->link_config.phy_is_low_power = 0;
1547 phydev->speed = tp->link_config.orig_speed;
1548 phydev->duplex = tp->link_config.orig_duplex;
1549 phydev->autoneg = tp->link_config.orig_autoneg;
1550 phydev->advertising = tp->link_config.orig_advertising;
1551 }
1552
1553 phy_start(phydev);
1554
1555 phy_start_aneg(phydev);
1556}
1557
1558static void tg3_phy_stop(struct tg3 *tp)
1559{
1560 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
1561 return;
1562
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001563 phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001564}
1565
1566static void tg3_phy_fini(struct tg3 *tp)
1567{
1568 if (tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001569 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001570 tp->tg3_flags3 &= ~TG3_FLG3_PHY_CONNECTED;
1571 }
1572}
1573
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001574static void tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
1575{
1576 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1577 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
1578}
1579
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001580static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
1581{
1582 u32 phytest;
1583
1584 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
1585 u32 phy;
1586
1587 tg3_writephy(tp, MII_TG3_FET_TEST,
1588 phytest | MII_TG3_FET_SHADOW_EN);
1589 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
1590 if (enable)
1591 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
1592 else
1593 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
1594 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
1595 }
1596 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
1597 }
1598}
1599
Matt Carlson6833c042008-11-21 17:18:59 -08001600static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
1601{
1602 u32 reg;
1603
Matt Carlsonecf14102010-01-20 16:58:05 +00001604 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
Matt Carlsona50d0792010-06-05 17:24:37 +00001605 ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
1606 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) &&
Matt Carlsonecf14102010-01-20 16:58:05 +00001607 (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08001608 return;
1609
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001610 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
1611 tg3_phy_fet_toggle_apd(tp, enable);
1612 return;
1613 }
1614
Matt Carlson6833c042008-11-21 17:18:59 -08001615 reg = MII_TG3_MISC_SHDW_WREN |
1616 MII_TG3_MISC_SHDW_SCR5_SEL |
1617 MII_TG3_MISC_SHDW_SCR5_LPED |
1618 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
1619 MII_TG3_MISC_SHDW_SCR5_SDTL |
1620 MII_TG3_MISC_SHDW_SCR5_C125OE;
1621 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 || !enable)
1622 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
1623
1624 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1625
1626
1627 reg = MII_TG3_MISC_SHDW_WREN |
1628 MII_TG3_MISC_SHDW_APD_SEL |
1629 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
1630 if (enable)
1631 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
1632
1633 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1634}
1635
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001636static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
1637{
1638 u32 phy;
1639
1640 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
1641 (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES))
1642 return;
1643
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001644 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001645 u32 ephy;
1646
Matt Carlson535ef6e2009-08-25 10:09:36 +00001647 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
1648 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
1649
1650 tg3_writephy(tp, MII_TG3_FET_TEST,
1651 ephy | MII_TG3_FET_SHADOW_EN);
1652 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001653 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00001654 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001655 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00001656 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
1657 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001658 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00001659 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001660 }
1661 } else {
1662 phy = MII_TG3_AUXCTL_MISC_RDSEL_MISC |
1663 MII_TG3_AUXCTL_SHDWSEL_MISC;
1664 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, phy) &&
1665 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &phy)) {
1666 if (enable)
1667 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1668 else
1669 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1670 phy |= MII_TG3_AUXCTL_MISC_WREN;
1671 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1672 }
1673 }
1674}
1675
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676static void tg3_phy_set_wirespeed(struct tg3 *tp)
1677{
1678 u32 val;
1679
1680 if (tp->tg3_flags2 & TG3_FLG2_NO_ETH_WIRE_SPEED)
1681 return;
1682
1683 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x7007) &&
1684 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &val))
1685 tg3_writephy(tp, MII_TG3_AUX_CTRL,
1686 (val | (1 << 15) | (1 << 4)));
1687}
1688
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001689static void tg3_phy_apply_otp(struct tg3 *tp)
1690{
1691 u32 otp, phy;
1692
1693 if (!tp->phy_otp)
1694 return;
1695
1696 otp = tp->phy_otp;
1697
1698 /* Enable SM_DSP clock and tx 6dB coding. */
1699 phy = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1700 MII_TG3_AUXCTL_ACTL_SMDSP_ENA |
1701 MII_TG3_AUXCTL_ACTL_TX_6DB;
1702 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1703
1704 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
1705 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
1706 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
1707
1708 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
1709 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
1710 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
1711
1712 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
1713 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
1714 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
1715
1716 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
1717 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
1718
1719 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
1720 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
1721
1722 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
1723 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
1724 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
1725
1726 /* Turn off SM_DSP clock. */
1727 phy = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1728 MII_TG3_AUXCTL_ACTL_TX_6DB;
1729 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1730}
1731
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732static int tg3_wait_macro_done(struct tg3 *tp)
1733{
1734 int limit = 100;
1735
1736 while (limit--) {
1737 u32 tmp32;
1738
1739 if (!tg3_readphy(tp, 0x16, &tmp32)) {
1740 if ((tmp32 & 0x1000) == 0)
1741 break;
1742 }
1743 }
Roel Kluind4675b52009-02-12 16:33:27 -08001744 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 return -EBUSY;
1746
1747 return 0;
1748}
1749
1750static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
1751{
1752 static const u32 test_pat[4][6] = {
1753 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
1754 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
1755 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
1756 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
1757 };
1758 int chan;
1759
1760 for (chan = 0; chan < 4; chan++) {
1761 int i;
1762
1763 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1764 (chan * 0x2000) | 0x0200);
1765 tg3_writephy(tp, 0x16, 0x0002);
1766
1767 for (i = 0; i < 6; i++)
1768 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
1769 test_pat[chan][i]);
1770
1771 tg3_writephy(tp, 0x16, 0x0202);
1772 if (tg3_wait_macro_done(tp)) {
1773 *resetp = 1;
1774 return -EBUSY;
1775 }
1776
1777 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1778 (chan * 0x2000) | 0x0200);
1779 tg3_writephy(tp, 0x16, 0x0082);
1780 if (tg3_wait_macro_done(tp)) {
1781 *resetp = 1;
1782 return -EBUSY;
1783 }
1784
1785 tg3_writephy(tp, 0x16, 0x0802);
1786 if (tg3_wait_macro_done(tp)) {
1787 *resetp = 1;
1788 return -EBUSY;
1789 }
1790
1791 for (i = 0; i < 6; i += 2) {
1792 u32 low, high;
1793
1794 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
1795 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
1796 tg3_wait_macro_done(tp)) {
1797 *resetp = 1;
1798 return -EBUSY;
1799 }
1800 low &= 0x7fff;
1801 high &= 0x000f;
1802 if (low != test_pat[chan][i] ||
1803 high != test_pat[chan][i+1]) {
1804 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
1805 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
1806 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
1807
1808 return -EBUSY;
1809 }
1810 }
1811 }
1812
1813 return 0;
1814}
1815
1816static int tg3_phy_reset_chanpat(struct tg3 *tp)
1817{
1818 int chan;
1819
1820 for (chan = 0; chan < 4; chan++) {
1821 int i;
1822
1823 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1824 (chan * 0x2000) | 0x0200);
1825 tg3_writephy(tp, 0x16, 0x0002);
1826 for (i = 0; i < 6; i++)
1827 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
1828 tg3_writephy(tp, 0x16, 0x0202);
1829 if (tg3_wait_macro_done(tp))
1830 return -EBUSY;
1831 }
1832
1833 return 0;
1834}
1835
1836static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
1837{
1838 u32 reg32, phy9_orig;
1839 int retries, do_phy_reset, err;
1840
1841 retries = 10;
1842 do_phy_reset = 1;
1843 do {
1844 if (do_phy_reset) {
1845 err = tg3_bmcr_reset(tp);
1846 if (err)
1847 return err;
1848 do_phy_reset = 0;
1849 }
1850
1851 /* Disable transmitter and interrupt. */
1852 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
1853 continue;
1854
1855 reg32 |= 0x3000;
1856 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
1857
1858 /* Set full-duplex, 1000 mbps. */
1859 tg3_writephy(tp, MII_BMCR,
1860 BMCR_FULLDPLX | TG3_BMCR_SPEED1000);
1861
1862 /* Set to master mode. */
1863 if (tg3_readphy(tp, MII_TG3_CTRL, &phy9_orig))
1864 continue;
1865
1866 tg3_writephy(tp, MII_TG3_CTRL,
1867 (MII_TG3_CTRL_AS_MASTER |
1868 MII_TG3_CTRL_ENABLE_AS_MASTER));
1869
1870 /* Enable SM_DSP_CLOCK and 6dB. */
1871 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
1872
1873 /* Block the PHY control access. */
1874 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8005);
1875 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0800);
1876
1877 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
1878 if (!err)
1879 break;
1880 } while (--retries);
1881
1882 err = tg3_phy_reset_chanpat(tp);
1883 if (err)
1884 return err;
1885
1886 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8005);
1887 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0000);
1888
1889 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
1890 tg3_writephy(tp, 0x16, 0x0000);
1891
1892 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
1893 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
1894 /* Set Extended packet length bit for jumbo frames */
1895 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4400);
Matt Carlson859a588792010-04-05 10:19:28 +00001896 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
1898 }
1899
1900 tg3_writephy(tp, MII_TG3_CTRL, phy9_orig);
1901
1902 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
1903 reg32 &= ~0x3000;
1904 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
1905 } else if (!err)
1906 err = -EBUSY;
1907
1908 return err;
1909}
1910
1911/* This will reset the tigon3 PHY if there is no valid
1912 * link unless the FORCE argument is non-zero.
1913 */
1914static int tg3_phy_reset(struct tg3 *tp)
1915{
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001916 u32 cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 u32 phy_status;
1918 int err;
1919
Michael Chan60189dd2006-12-17 17:08:07 -08001920 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
1921 u32 val;
1922
1923 val = tr32(GRC_MISC_CFG);
1924 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
1925 udelay(40);
1926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 err = tg3_readphy(tp, MII_BMSR, &phy_status);
1928 err |= tg3_readphy(tp, MII_BMSR, &phy_status);
1929 if (err != 0)
1930 return -EBUSY;
1931
Michael Chanc8e1e822006-04-29 18:55:17 -07001932 if (netif_running(tp->dev) && netif_carrier_ok(tp->dev)) {
1933 netif_carrier_off(tp->dev);
1934 tg3_link_report(tp);
1935 }
1936
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
1938 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
1939 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
1940 err = tg3_phy_reset_5703_4_5(tp);
1941 if (err)
1942 return err;
1943 goto out;
1944 }
1945
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001946 cpmuctrl = 0;
1947 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
1948 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
1949 cpmuctrl = tr32(TG3_CPMU_CTRL);
1950 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
1951 tw32(TG3_CPMU_CTRL,
1952 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
1953 }
1954
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 err = tg3_bmcr_reset(tp);
1956 if (err)
1957 return err;
1958
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001959 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
1960 u32 phy;
1961
1962 phy = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
1963 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, phy);
1964
1965 tw32(TG3_CPMU_CTRL, cpmuctrl);
1966 }
1967
Matt Carlsonbcb37f62008-11-03 16:52:09 -08001968 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
1969 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08001970 u32 val;
1971
1972 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
1973 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
1974 CPMU_LSPD_1000MB_MACCLK_12_5) {
1975 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
1976 udelay(40);
1977 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
1978 }
1979 }
1980
Matt Carlsona50d0792010-06-05 17:24:37 +00001981 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
1982 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) &&
Matt Carlsonecf14102010-01-20 16:58:05 +00001983 (tp->tg3_flags2 & TG3_FLG2_MII_SERDES))
1984 return 0;
1985
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001986 tg3_phy_apply_otp(tp);
1987
Matt Carlson6833c042008-11-21 17:18:59 -08001988 if (tp->tg3_flags3 & TG3_FLG3_PHY_ENABLE_APD)
1989 tg3_phy_toggle_apd(tp, true);
1990 else
1991 tg3_phy_toggle_apd(tp, false);
1992
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993out:
1994 if (tp->tg3_flags2 & TG3_FLG2_PHY_ADC_BUG) {
1995 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
1996 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x201f);
1997 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x2aaa);
1998 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
1999 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0323);
2000 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
2001 }
2002 if (tp->tg3_flags2 & TG3_FLG2_PHY_5704_A0_BUG) {
2003 tg3_writephy(tp, 0x1c, 0x8d68);
2004 tg3_writephy(tp, 0x1c, 0x8d68);
2005 }
2006 if (tp->tg3_flags2 & TG3_FLG2_PHY_BER_BUG) {
2007 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
2008 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
2009 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x310b);
2010 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x201f);
2011 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x9506);
2012 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x401f);
2013 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x14e2);
2014 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
Matt Carlson859a588792010-04-05 10:19:28 +00002015 } else if (tp->tg3_flags2 & TG3_FLG2_PHY_JITTER_BUG) {
Michael Chanc424cb22006-04-29 18:56:34 -07002016 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
2017 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
Michael Chanc1d2a192007-01-08 19:57:20 -08002018 if (tp->tg3_flags2 & TG3_FLG2_PHY_ADJUST_TRIM) {
2019 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2020 tg3_writephy(tp, MII_TG3_TEST1,
2021 MII_TG3_TEST1_TRIM_EN | 0x4);
2022 } else
2023 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
Michael Chanc424cb22006-04-29 18:56:34 -07002024 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
2025 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 /* Set Extended packet length bit (bit 14) on all chips that */
2027 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002028 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 /* Cannot do read-modify-write on 5401 */
2030 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4c20);
Matt Carlson8f666b02009-08-28 13:58:24 +00002031 } else if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 u32 phy_reg;
2033
2034 /* Set bit 14 with read-modify-write to preserve other bits */
2035 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0007) &&
2036 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &phy_reg))
2037 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy_reg | 0x4000);
2038 }
2039
2040 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2041 * jumbo frames transmission.
2042 */
Matt Carlson8f666b02009-08-28 13:58:24 +00002043 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 u32 phy_reg;
2045
2046 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &phy_reg))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002047 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2048 phy_reg | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 }
2050
Michael Chan715116a2006-09-27 16:09:25 -07002051 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002052 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002053 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002054 }
2055
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002056 tg3_phy_toggle_automdix(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 tg3_phy_set_wirespeed(tp);
2058 return 0;
2059}
2060
2061static void tg3_frob_aux_power(struct tg3 *tp)
2062{
2063 struct tg3 *tp_peer = tp;
2064
Matt Carlson334355a2010-01-20 16:58:10 +00002065 /* The GPIOs do something completely different on 57765. */
2066 if ((tp->tg3_flags2 & TG3_FLG2_IS_NIC) == 0 ||
Matt Carlsona50d0792010-06-05 17:24:37 +00002067 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlson334355a2010-01-20 16:58:10 +00002068 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 return;
2070
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00002071 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2072 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
2073 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002074 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002076 dev_peer = pci_get_drvdata(tp->pdev_peer);
Michael Chanbc1c7562006-03-20 17:48:03 -08002077 /* remove_one() may have been run on the peer. */
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002078 if (!dev_peer)
Michael Chanbc1c7562006-03-20 17:48:03 -08002079 tp_peer = tp;
2080 else
2081 tp_peer = netdev_priv(dev_peer);
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
2084 if ((tp->tg3_flags & TG3_FLAG_WOL_ENABLE) != 0 ||
Michael Chan6921d202005-12-13 21:15:53 -08002085 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0 ||
2086 (tp_peer->tg3_flags & TG3_FLAG_WOL_ENABLE) != 0 ||
2087 (tp_peer->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2089 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Michael Chanb401e9e2005-12-19 16:27:04 -08002090 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2091 (GRC_LCLCTRL_GPIO_OE0 |
2092 GRC_LCLCTRL_GPIO_OE1 |
2093 GRC_LCLCTRL_GPIO_OE2 |
2094 GRC_LCLCTRL_GPIO_OUTPUT0 |
2095 GRC_LCLCTRL_GPIO_OUTPUT1),
2096 100);
Matt Carlson8d519ab2009-04-20 06:58:01 +00002097 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2098 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -07002099 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2100 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2101 GRC_LCLCTRL_GPIO_OE1 |
2102 GRC_LCLCTRL_GPIO_OE2 |
2103 GRC_LCLCTRL_GPIO_OUTPUT0 |
2104 GRC_LCLCTRL_GPIO_OUTPUT1 |
2105 tp->grc_local_ctrl;
2106 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2107
2108 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2109 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2110
2111 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2112 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 } else {
2114 u32 no_gpio2;
Michael Chandc56b7d2005-12-19 16:26:28 -08002115 u32 grc_local_ctrl = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
2117 if (tp_peer != tp &&
2118 (tp_peer->tg3_flags & TG3_FLAG_INIT_COMPLETE) != 0)
2119 return;
2120
Michael Chandc56b7d2005-12-19 16:26:28 -08002121 /* Workaround to prevent overdrawing Amps. */
2122 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2123 ASIC_REV_5714) {
2124 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chanb401e9e2005-12-19 16:27:04 -08002125 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2126 grc_local_ctrl, 100);
Michael Chandc56b7d2005-12-19 16:26:28 -08002127 }
2128
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 /* On 5753 and variants, GPIO2 cannot be used. */
2130 no_gpio2 = tp->nic_sram_data_cfg &
2131 NIC_SRAM_DATA_CFG_NO_GPIO2;
2132
Michael Chandc56b7d2005-12-19 16:26:28 -08002133 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 GRC_LCLCTRL_GPIO_OE1 |
2135 GRC_LCLCTRL_GPIO_OE2 |
2136 GRC_LCLCTRL_GPIO_OUTPUT1 |
2137 GRC_LCLCTRL_GPIO_OUTPUT2;
2138 if (no_gpio2) {
2139 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2140 GRC_LCLCTRL_GPIO_OUTPUT2);
2141 }
Michael Chanb401e9e2005-12-19 16:27:04 -08002142 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2143 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
2145 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2146
Michael Chanb401e9e2005-12-19 16:27:04 -08002147 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2148 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
2150 if (!no_gpio2) {
2151 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chanb401e9e2005-12-19 16:27:04 -08002152 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2153 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 }
2155 }
2156 } else {
2157 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
2158 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
2159 if (tp_peer != tp &&
2160 (tp_peer->tg3_flags & TG3_FLAG_INIT_COMPLETE) != 0)
2161 return;
2162
Michael Chanb401e9e2005-12-19 16:27:04 -08002163 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2164 (GRC_LCLCTRL_GPIO_OE1 |
2165 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
Michael Chanb401e9e2005-12-19 16:27:04 -08002167 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2168 GRC_LCLCTRL_GPIO_OE1, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
Michael Chanb401e9e2005-12-19 16:27:04 -08002170 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2171 (GRC_LCLCTRL_GPIO_OE1 |
2172 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 }
2174 }
2175}
2176
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002177static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2178{
2179 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2180 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00002181 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002182 if (speed != SPEED_10)
2183 return 1;
2184 } else if (speed == SPEED_10)
2185 return 1;
2186
2187 return 0;
2188}
2189
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190static int tg3_setup_phy(struct tg3 *, int);
2191
2192#define RESET_KIND_SHUTDOWN 0
2193#define RESET_KIND_INIT 1
2194#define RESET_KIND_SUSPEND 2
2195
2196static void tg3_write_sig_post_reset(struct tg3 *, int);
2197static int tg3_halt_cpu(struct tg3 *, u32);
2198
Matt Carlson0a459aa2008-11-03 16:54:15 -08002199static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002200{
Matt Carlsonce057f02007-11-12 21:08:03 -08002201 u32 val;
2202
Michael Chan51297242007-02-13 12:17:57 -08002203 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
2204 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2205 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2206 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2207
2208 sg_dig_ctrl |=
2209 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2210 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2211 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2212 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002213 return;
Michael Chan51297242007-02-13 12:17:57 -08002214 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002215
Michael Chan60189dd2006-12-17 17:08:07 -08002216 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002217 tg3_bmcr_reset(tp);
2218 val = tr32(GRC_MISC_CFG);
2219 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2220 udelay(40);
2221 return;
Matt Carlson0e5f7842009-11-02 14:26:38 +00002222 } else if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
2223 u32 phytest;
2224 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2225 u32 phy;
2226
2227 tg3_writephy(tp, MII_ADVERTISE, 0);
2228 tg3_writephy(tp, MII_BMCR,
2229 BMCR_ANENABLE | BMCR_ANRESTART);
2230
2231 tg3_writephy(tp, MII_TG3_FET_TEST,
2232 phytest | MII_TG3_FET_SHADOW_EN);
2233 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
2234 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
2235 tg3_writephy(tp,
2236 MII_TG3_FET_SHDW_AUXMODE4,
2237 phy);
2238 }
2239 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2240 }
2241 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002242 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07002243 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2244 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002245
2246 tg3_writephy(tp, MII_TG3_AUX_CTRL,
2247 MII_TG3_AUXCTL_SHDWSEL_PWRCTL |
2248 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2249 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2250 MII_TG3_AUXCTL_PCTL_VREG_11V);
Michael Chan715116a2006-09-27 16:09:25 -07002251 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002252
Michael Chan15c3b692006-03-22 01:06:52 -08002253 /* The PHY should not be powered down on some chips because
2254 * of bugs.
2255 */
2256 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2257 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2258 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
2259 (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)))
2260 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08002261
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002262 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2263 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002264 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2265 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2266 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2267 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2268 }
2269
Michael Chan15c3b692006-03-22 01:06:52 -08002270 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2271}
2272
Matt Carlson3f007892008-11-03 16:51:36 -08002273/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002274static int tg3_nvram_lock(struct tg3 *tp)
2275{
2276 if (tp->tg3_flags & TG3_FLAG_NVRAM) {
2277 int i;
2278
2279 if (tp->nvram_lock_cnt == 0) {
2280 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2281 for (i = 0; i < 8000; i++) {
2282 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2283 break;
2284 udelay(20);
2285 }
2286 if (i == 8000) {
2287 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2288 return -ENODEV;
2289 }
2290 }
2291 tp->nvram_lock_cnt++;
2292 }
2293 return 0;
2294}
2295
2296/* tp->lock is held. */
2297static void tg3_nvram_unlock(struct tg3 *tp)
2298{
2299 if (tp->tg3_flags & TG3_FLAG_NVRAM) {
2300 if (tp->nvram_lock_cnt > 0)
2301 tp->nvram_lock_cnt--;
2302 if (tp->nvram_lock_cnt == 0)
2303 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
2304 }
2305}
2306
2307/* tp->lock is held. */
2308static void tg3_enable_nvram_access(struct tg3 *tp)
2309{
2310 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Matt Carlsonf66a29b2009-11-13 13:03:36 +00002311 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002312 u32 nvaccess = tr32(NVRAM_ACCESS);
2313
2314 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
2315 }
2316}
2317
2318/* tp->lock is held. */
2319static void tg3_disable_nvram_access(struct tg3 *tp)
2320{
2321 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Matt Carlsonf66a29b2009-11-13 13:03:36 +00002322 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002323 u32 nvaccess = tr32(NVRAM_ACCESS);
2324
2325 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
2326 }
2327}
2328
2329static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
2330 u32 offset, u32 *val)
2331{
2332 u32 tmp;
2333 int i;
2334
2335 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
2336 return -EINVAL;
2337
2338 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
2339 EEPROM_ADDR_DEVID_MASK |
2340 EEPROM_ADDR_READ);
2341 tw32(GRC_EEPROM_ADDR,
2342 tmp |
2343 (0 << EEPROM_ADDR_DEVID_SHIFT) |
2344 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
2345 EEPROM_ADDR_ADDR_MASK) |
2346 EEPROM_ADDR_READ | EEPROM_ADDR_START);
2347
2348 for (i = 0; i < 1000; i++) {
2349 tmp = tr32(GRC_EEPROM_ADDR);
2350
2351 if (tmp & EEPROM_ADDR_COMPLETE)
2352 break;
2353 msleep(1);
2354 }
2355 if (!(tmp & EEPROM_ADDR_COMPLETE))
2356 return -EBUSY;
2357
Matt Carlson62cedd12009-04-20 14:52:29 -07002358 tmp = tr32(GRC_EEPROM_DATA);
2359
2360 /*
2361 * The data will always be opposite the native endian
2362 * format. Perform a blind byteswap to compensate.
2363 */
2364 *val = swab32(tmp);
2365
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002366 return 0;
2367}
2368
2369#define NVRAM_CMD_TIMEOUT 10000
2370
2371static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
2372{
2373 int i;
2374
2375 tw32(NVRAM_CMD, nvram_cmd);
2376 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
2377 udelay(10);
2378 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
2379 udelay(10);
2380 break;
2381 }
2382 }
2383
2384 if (i == NVRAM_CMD_TIMEOUT)
2385 return -EBUSY;
2386
2387 return 0;
2388}
2389
2390static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
2391{
2392 if ((tp->tg3_flags & TG3_FLAG_NVRAM) &&
2393 (tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) &&
2394 (tp->tg3_flags2 & TG3_FLG2_FLASH) &&
2395 !(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM_ADDR_TRANS) &&
2396 (tp->nvram_jedecnum == JEDEC_ATMEL))
2397
2398 addr = ((addr / tp->nvram_pagesize) <<
2399 ATMEL_AT45DB0X1B_PAGE_POS) +
2400 (addr % tp->nvram_pagesize);
2401
2402 return addr;
2403}
2404
2405static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
2406{
2407 if ((tp->tg3_flags & TG3_FLAG_NVRAM) &&
2408 (tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) &&
2409 (tp->tg3_flags2 & TG3_FLG2_FLASH) &&
2410 !(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM_ADDR_TRANS) &&
2411 (tp->nvram_jedecnum == JEDEC_ATMEL))
2412
2413 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
2414 tp->nvram_pagesize) +
2415 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
2416
2417 return addr;
2418}
2419
Matt Carlsone4f34112009-02-25 14:25:00 +00002420/* NOTE: Data read in from NVRAM is byteswapped according to
2421 * the byteswapping settings for all other register accesses.
2422 * tg3 devices are BE devices, so on a BE machine, the data
2423 * returned will be exactly as it is seen in NVRAM. On a LE
2424 * machine, the 32-bit value will be byteswapped.
2425 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002426static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
2427{
2428 int ret;
2429
2430 if (!(tp->tg3_flags & TG3_FLAG_NVRAM))
2431 return tg3_nvram_read_using_eeprom(tp, offset, val);
2432
2433 offset = tg3_nvram_phys_addr(tp, offset);
2434
2435 if (offset > NVRAM_ADDR_MSK)
2436 return -EINVAL;
2437
2438 ret = tg3_nvram_lock(tp);
2439 if (ret)
2440 return ret;
2441
2442 tg3_enable_nvram_access(tp);
2443
2444 tw32(NVRAM_ADDR, offset);
2445 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
2446 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
2447
2448 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00002449 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002450
2451 tg3_disable_nvram_access(tp);
2452
2453 tg3_nvram_unlock(tp);
2454
2455 return ret;
2456}
2457
Matt Carlsona9dc5292009-02-25 14:25:30 +00002458/* Ensures NVRAM data is in bytestream format. */
2459static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002460{
2461 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00002462 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002463 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00002464 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002465 return res;
2466}
2467
2468/* tp->lock is held. */
Matt Carlson3f007892008-11-03 16:51:36 -08002469static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
2470{
2471 u32 addr_high, addr_low;
2472 int i;
2473
2474 addr_high = ((tp->dev->dev_addr[0] << 8) |
2475 tp->dev->dev_addr[1]);
2476 addr_low = ((tp->dev->dev_addr[2] << 24) |
2477 (tp->dev->dev_addr[3] << 16) |
2478 (tp->dev->dev_addr[4] << 8) |
2479 (tp->dev->dev_addr[5] << 0));
2480 for (i = 0; i < 4; i++) {
2481 if (i == 1 && skip_mac_1)
2482 continue;
2483 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
2484 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
2485 }
2486
2487 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2488 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2489 for (i = 0; i < 12; i++) {
2490 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
2491 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
2492 }
2493 }
2494
2495 addr_high = (tp->dev->dev_addr[0] +
2496 tp->dev->dev_addr[1] +
2497 tp->dev->dev_addr[2] +
2498 tp->dev->dev_addr[3] +
2499 tp->dev->dev_addr[4] +
2500 tp->dev->dev_addr[5]) &
2501 TX_BACKOFF_SEED_MASK;
2502 tw32(MAC_TX_BACKOFF_SEED, addr_high);
2503}
2504
Michael Chanbc1c7562006-03-20 17:48:03 -08002505static int tg3_set_power_state(struct tg3 *tp, pci_power_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506{
2507 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002508 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509
2510 /* Make sure register accesses (indirect or otherwise)
2511 * will function correctly.
2512 */
2513 pci_write_config_dword(tp->pdev,
2514 TG3PCI_MISC_HOST_CTRL,
2515 tp->misc_host_ctrl);
2516
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 switch (state) {
Michael Chanbc1c7562006-03-20 17:48:03 -08002518 case PCI_D0:
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07002519 pci_enable_wake(tp->pdev, state, false);
2520 pci_set_power_state(tp->pdev, PCI_D0);
Michael Chan8c6bda12005-04-21 17:09:08 -07002521
Michael Chan9d26e212006-12-07 00:21:14 -08002522 /* Switch out of Vaux if it is a NIC */
2523 if (tp->tg3_flags2 & TG3_FLG2_IS_NIC)
Michael Chanb401e9e2005-12-19 16:27:04 -08002524 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525
2526 return 0;
2527
Michael Chanbc1c7562006-03-20 17:48:03 -08002528 case PCI_D1:
Michael Chanbc1c7562006-03-20 17:48:03 -08002529 case PCI_D2:
Michael Chanbc1c7562006-03-20 17:48:03 -08002530 case PCI_D3hot:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 break;
2532
2533 default:
Joe Perches05dbe002010-02-17 19:44:19 +00002534 netdev_err(tp->dev, "Invalid power state (D%d) requested\n",
2535 state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002537 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002538
2539 /* Restore the CLKREQ setting. */
2540 if (tp->tg3_flags3 & TG3_FLG3_CLKREQ_BUG) {
2541 u16 lnkctl;
2542
2543 pci_read_config_word(tp->pdev,
2544 tp->pcie_cap + PCI_EXP_LNKCTL,
2545 &lnkctl);
2546 lnkctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
2547 pci_write_config_word(tp->pdev,
2548 tp->pcie_cap + PCI_EXP_LNKCTL,
2549 lnkctl);
2550 }
2551
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
2553 tw32(TG3PCI_MISC_HOST_CTRL,
2554 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
2555
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002556 device_should_wake = pci_pme_capable(tp->pdev, state) &&
2557 device_may_wakeup(&tp->pdev->dev) &&
2558 (tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
2559
Matt Carlsondd477002008-05-25 23:45:58 -07002560 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08002561 do_low_power = false;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002562 if ((tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED) &&
2563 !tp->link_config.phy_is_low_power) {
2564 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002565 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002566
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002567 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002568
2569 tp->link_config.phy_is_low_power = 1;
2570
2571 tp->link_config.orig_speed = phydev->speed;
2572 tp->link_config.orig_duplex = phydev->duplex;
2573 tp->link_config.orig_autoneg = phydev->autoneg;
2574 tp->link_config.orig_advertising = phydev->advertising;
2575
2576 advertising = ADVERTISED_TP |
2577 ADVERTISED_Pause |
2578 ADVERTISED_Autoneg |
2579 ADVERTISED_10baseT_Half;
2580
2581 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002582 device_should_wake) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002583 if (tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB)
2584 advertising |=
2585 ADVERTISED_100baseT_Half |
2586 ADVERTISED_100baseT_Full |
2587 ADVERTISED_10baseT_Full;
2588 else
2589 advertising |= ADVERTISED_10baseT_Full;
2590 }
2591
2592 phydev->advertising = advertising;
2593
2594 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002595
2596 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00002597 if (phyid != PHY_ID_BCMAC131) {
2598 phyid &= PHY_BCM_OUI_MASK;
2599 if (phyid == PHY_BCM_OUI_1 ||
2600 phyid == PHY_BCM_OUI_2 ||
2601 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08002602 do_low_power = true;
2603 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002604 }
Matt Carlsondd477002008-05-25 23:45:58 -07002605 } else {
Matt Carlson20232762008-12-21 20:18:56 -08002606 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002607
Matt Carlsondd477002008-05-25 23:45:58 -07002608 if (tp->link_config.phy_is_low_power == 0) {
2609 tp->link_config.phy_is_low_power = 1;
2610 tp->link_config.orig_speed = tp->link_config.speed;
2611 tp->link_config.orig_duplex = tp->link_config.duplex;
2612 tp->link_config.orig_autoneg = tp->link_config.autoneg;
2613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614
Matt Carlsondd477002008-05-25 23:45:58 -07002615 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)) {
2616 tp->link_config.speed = SPEED_10;
2617 tp->link_config.duplex = DUPLEX_HALF;
2618 tp->link_config.autoneg = AUTONEG_ENABLE;
2619 tg3_setup_phy(tp, 0);
2620 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 }
2622
Michael Chanb5d37722006-09-27 16:06:21 -07002623 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
2624 u32 val;
2625
2626 val = tr32(GRC_VCPU_EXT_CTRL);
2627 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
2628 } else if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08002629 int i;
2630 u32 val;
2631
2632 for (i = 0; i < 200; i++) {
2633 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
2634 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
2635 break;
2636 msleep(1);
2637 }
2638 }
Gary Zambranoa85feb82007-05-05 11:52:19 -07002639 if (tp->tg3_flags & TG3_FLAG_WOL_CAP)
2640 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
2641 WOL_DRV_STATE_SHUTDOWN |
2642 WOL_DRV_WOL |
2643 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08002644
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002645 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 u32 mac_mode;
2647
2648 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08002649 if (do_low_power) {
Matt Carlsondd477002008-05-25 23:45:58 -07002650 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x5a);
2651 udelay(40);
2652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653
Michael Chan3f7045c2006-09-27 16:02:29 -07002654 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)
2655 mac_mode = MAC_MODE_PORT_MODE_GMII;
2656 else
2657 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002659 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
2660 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2661 ASIC_REV_5700) {
2662 u32 speed = (tp->tg3_flags &
2663 TG3_FLAG_WOL_SPEED_100MB) ?
2664 SPEED_100 : SPEED_10;
2665 if (tg3_5700_link_polarity(tp, speed))
2666 mac_mode |= MAC_MODE_LINK_POLARITY;
2667 else
2668 mac_mode &= ~MAC_MODE_LINK_POLARITY;
2669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 } else {
2671 mac_mode = MAC_MODE_PORT_MODE_TBI;
2672 }
2673
John W. Linvillecbf46852005-04-21 17:01:29 -07002674 if (!(tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 tw32(MAC_LED_CTRL, tp->led_ctrl);
2676
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002677 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
2678 if (((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
2679 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) &&
2680 ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
2681 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)))
2682 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683
Matt Carlson3bda1252008-08-15 14:08:22 -07002684 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
2685 mac_mode |= tp->mac_mode &
2686 (MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN);
2687 if (mac_mode & MAC_MODE_APE_TX_EN)
2688 mac_mode |= MAC_MODE_TDE_ENABLE;
2689 }
2690
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 tw32_f(MAC_MODE, mac_mode);
2692 udelay(100);
2693
2694 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
2695 udelay(10);
2696 }
2697
2698 if (!(tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB) &&
2699 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2700 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
2701 u32 base_val;
2702
2703 base_val = tp->pci_clock_ctrl;
2704 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
2705 CLOCK_CTRL_TXCLK_DISABLE);
2706
Michael Chanb401e9e2005-12-19 16:27:04 -08002707 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
2708 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Michael Chand7b0a852007-02-13 12:17:38 -08002709 } else if ((tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
Matt Carlson795d01c2007-10-07 23:28:17 -07002710 (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
Michael Chand7b0a852007-02-13 12:17:38 -08002711 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)) {
Michael Chan4cf78e42005-07-25 12:29:19 -07002712 /* do nothing */
Michael Chan85e94ce2005-04-21 17:05:28 -07002713 } else if (!((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF))) {
2715 u32 newbits1, newbits2;
2716
2717 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2718 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2719 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
2720 CLOCK_CTRL_TXCLK_DISABLE |
2721 CLOCK_CTRL_ALTCLK);
2722 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2723 } else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
2724 newbits1 = CLOCK_CTRL_625_CORE;
2725 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
2726 } else {
2727 newbits1 = CLOCK_CTRL_ALTCLK;
2728 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2729 }
2730
Michael Chanb401e9e2005-12-19 16:27:04 -08002731 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
2732 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733
Michael Chanb401e9e2005-12-19 16:27:04 -08002734 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
2735 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736
2737 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
2738 u32 newbits3;
2739
2740 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2741 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2742 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
2743 CLOCK_CTRL_TXCLK_DISABLE |
2744 CLOCK_CTRL_44MHZ_CORE);
2745 } else {
2746 newbits3 = CLOCK_CTRL_44MHZ_CORE;
2747 }
2748
Michael Chanb401e9e2005-12-19 16:27:04 -08002749 tw32_wait_f(TG3PCI_CLOCK_CTRL,
2750 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 }
2752 }
2753
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002754 if (!(device_should_wake) &&
Matt Carlson22435842008-11-21 17:21:13 -08002755 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08002756 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08002757
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 tg3_frob_aux_power(tp);
2759
2760 /* Workaround for unstable PLL clock */
2761 if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
2762 (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
2763 u32 val = tr32(0x7d00);
2764
2765 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
2766 tw32(0x7d00, val);
Michael Chan6921d202005-12-13 21:15:53 -08002767 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08002768 int err;
2769
2770 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08002772 if (!err)
2773 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08002774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 }
2776
Michael Chanbbadf502006-04-06 21:46:34 -07002777 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
2778
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002779 if (device_should_wake)
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07002780 pci_enable_wake(tp->pdev, state, true);
2781
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 /* Finally, set the new power state. */
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07002783 pci_set_power_state(tp->pdev, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 return 0;
2786}
2787
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
2789{
2790 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
2791 case MII_TG3_AUX_STAT_10HALF:
2792 *speed = SPEED_10;
2793 *duplex = DUPLEX_HALF;
2794 break;
2795
2796 case MII_TG3_AUX_STAT_10FULL:
2797 *speed = SPEED_10;
2798 *duplex = DUPLEX_FULL;
2799 break;
2800
2801 case MII_TG3_AUX_STAT_100HALF:
2802 *speed = SPEED_100;
2803 *duplex = DUPLEX_HALF;
2804 break;
2805
2806 case MII_TG3_AUX_STAT_100FULL:
2807 *speed = SPEED_100;
2808 *duplex = DUPLEX_FULL;
2809 break;
2810
2811 case MII_TG3_AUX_STAT_1000HALF:
2812 *speed = SPEED_1000;
2813 *duplex = DUPLEX_HALF;
2814 break;
2815
2816 case MII_TG3_AUX_STAT_1000FULL:
2817 *speed = SPEED_1000;
2818 *duplex = DUPLEX_FULL;
2819 break;
2820
2821 default:
Matt Carlson7f97a4b2009-08-25 10:10:03 +00002822 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07002823 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
2824 SPEED_10;
2825 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
2826 DUPLEX_HALF;
2827 break;
2828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 *speed = SPEED_INVALID;
2830 *duplex = DUPLEX_INVALID;
2831 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833}
2834
2835static void tg3_phy_copper_begin(struct tg3 *tp)
2836{
2837 u32 new_adv;
2838 int i;
2839
2840 if (tp->link_config.phy_is_low_power) {
2841 /* Entering low power mode. Disable gigabit and
2842 * 100baseT advertisements.
2843 */
2844 tg3_writephy(tp, MII_TG3_CTRL, 0);
2845
2846 new_adv = (ADVERTISE_10HALF | ADVERTISE_10FULL |
2847 ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
2848 if (tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB)
2849 new_adv |= (ADVERTISE_100HALF | ADVERTISE_100FULL);
2850
2851 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2852 } else if (tp->link_config.speed == SPEED_INVALID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 if (tp->tg3_flags & TG3_FLAG_10_100_ONLY)
2854 tp->link_config.advertising &=
2855 ~(ADVERTISED_1000baseT_Half |
2856 ADVERTISED_1000baseT_Full);
2857
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002858 new_adv = ADVERTISE_CSMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 if (tp->link_config.advertising & ADVERTISED_10baseT_Half)
2860 new_adv |= ADVERTISE_10HALF;
2861 if (tp->link_config.advertising & ADVERTISED_10baseT_Full)
2862 new_adv |= ADVERTISE_10FULL;
2863 if (tp->link_config.advertising & ADVERTISED_100baseT_Half)
2864 new_adv |= ADVERTISE_100HALF;
2865 if (tp->link_config.advertising & ADVERTISED_100baseT_Full)
2866 new_adv |= ADVERTISE_100FULL;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002867
2868 new_adv |= tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2869
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2871
2872 if (tp->link_config.advertising &
2873 (ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full)) {
2874 new_adv = 0;
2875 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
2876 new_adv |= MII_TG3_CTRL_ADV_1000_HALF;
2877 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
2878 new_adv |= MII_TG3_CTRL_ADV_1000_FULL;
2879 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY) &&
2880 (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2881 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0))
2882 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2883 MII_TG3_CTRL_ENABLE_AS_MASTER);
2884 tg3_writephy(tp, MII_TG3_CTRL, new_adv);
2885 } else {
2886 tg3_writephy(tp, MII_TG3_CTRL, 0);
2887 }
2888 } else {
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002889 new_adv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2890 new_adv |= ADVERTISE_CSMA;
2891
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 /* Asking for a specific link mode. */
2893 if (tp->link_config.speed == SPEED_1000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2895
2896 if (tp->link_config.duplex == DUPLEX_FULL)
2897 new_adv = MII_TG3_CTRL_ADV_1000_FULL;
2898 else
2899 new_adv = MII_TG3_CTRL_ADV_1000_HALF;
2900 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2901 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
2902 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2903 MII_TG3_CTRL_ENABLE_AS_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 if (tp->link_config.speed == SPEED_100) {
2906 if (tp->link_config.duplex == DUPLEX_FULL)
2907 new_adv |= ADVERTISE_100FULL;
2908 else
2909 new_adv |= ADVERTISE_100HALF;
2910 } else {
2911 if (tp->link_config.duplex == DUPLEX_FULL)
2912 new_adv |= ADVERTISE_10FULL;
2913 else
2914 new_adv |= ADVERTISE_10HALF;
2915 }
2916 tg3_writephy(tp, MII_ADVERTISE, new_adv);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002917
2918 new_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002920
2921 tg3_writephy(tp, MII_TG3_CTRL, new_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 }
2923
2924 if (tp->link_config.autoneg == AUTONEG_DISABLE &&
2925 tp->link_config.speed != SPEED_INVALID) {
2926 u32 bmcr, orig_bmcr;
2927
2928 tp->link_config.active_speed = tp->link_config.speed;
2929 tp->link_config.active_duplex = tp->link_config.duplex;
2930
2931 bmcr = 0;
2932 switch (tp->link_config.speed) {
2933 default:
2934 case SPEED_10:
2935 break;
2936
2937 case SPEED_100:
2938 bmcr |= BMCR_SPEED100;
2939 break;
2940
2941 case SPEED_1000:
2942 bmcr |= TG3_BMCR_SPEED1000;
2943 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945
2946 if (tp->link_config.duplex == DUPLEX_FULL)
2947 bmcr |= BMCR_FULLDPLX;
2948
2949 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
2950 (bmcr != orig_bmcr)) {
2951 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
2952 for (i = 0; i < 1500; i++) {
2953 u32 tmp;
2954
2955 udelay(10);
2956 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
2957 tg3_readphy(tp, MII_BMSR, &tmp))
2958 continue;
2959 if (!(tmp & BMSR_LSTATUS)) {
2960 udelay(40);
2961 break;
2962 }
2963 }
2964 tg3_writephy(tp, MII_BMCR, bmcr);
2965 udelay(40);
2966 }
2967 } else {
2968 tg3_writephy(tp, MII_BMCR,
2969 BMCR_ANENABLE | BMCR_ANRESTART);
2970 }
2971}
2972
2973static int tg3_init_5401phy_dsp(struct tg3 *tp)
2974{
2975 int err;
2976
2977 /* Turn off tap power management. */
2978 /* Set Extended packet length bit */
2979 err = tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4c20);
2980
2981 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x0012);
2982 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x1804);
2983
2984 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x0013);
2985 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x1204);
2986
2987 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8006);
2988 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0132);
2989
2990 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8006);
2991 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0232);
2992
2993 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x201f);
2994 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0a20);
2995
2996 udelay(40);
2997
2998 return err;
2999}
3000
Michael Chan3600d912006-12-07 00:21:48 -08003001static int tg3_copper_is_advertising_all(struct tg3 *tp, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002{
Michael Chan3600d912006-12-07 00:21:48 -08003003 u32 adv_reg, all_mask = 0;
3004
3005 if (mask & ADVERTISED_10baseT_Half)
3006 all_mask |= ADVERTISE_10HALF;
3007 if (mask & ADVERTISED_10baseT_Full)
3008 all_mask |= ADVERTISE_10FULL;
3009 if (mask & ADVERTISED_100baseT_Half)
3010 all_mask |= ADVERTISE_100HALF;
3011 if (mask & ADVERTISED_100baseT_Full)
3012 all_mask |= ADVERTISE_100FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013
3014 if (tg3_readphy(tp, MII_ADVERTISE, &adv_reg))
3015 return 0;
3016
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 if ((adv_reg & all_mask) != all_mask)
3018 return 0;
3019 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY)) {
3020 u32 tg3_ctrl;
3021
Michael Chan3600d912006-12-07 00:21:48 -08003022 all_mask = 0;
3023 if (mask & ADVERTISED_1000baseT_Half)
3024 all_mask |= ADVERTISE_1000HALF;
3025 if (mask & ADVERTISED_1000baseT_Full)
3026 all_mask |= ADVERTISE_1000FULL;
3027
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 if (tg3_readphy(tp, MII_TG3_CTRL, &tg3_ctrl))
3029 return 0;
3030
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 if ((tg3_ctrl & all_mask) != all_mask)
3032 return 0;
3033 }
3034 return 1;
3035}
3036
Matt Carlsonef167e22007-12-20 20:10:01 -08003037static int tg3_adv_1000T_flowctrl_ok(struct tg3 *tp, u32 *lcladv, u32 *rmtadv)
3038{
3039 u32 curadv, reqadv;
3040
3041 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
3042 return 1;
3043
3044 curadv = *lcladv & (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
3045 reqadv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
3046
3047 if (tp->link_config.active_duplex == DUPLEX_FULL) {
3048 if (curadv != reqadv)
3049 return 0;
3050
3051 if (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG)
3052 tg3_readphy(tp, MII_LPA, rmtadv);
3053 } else {
3054 /* Reprogram the advertisement register, even if it
3055 * does not affect the current link. If the link
3056 * gets renegotiated in the future, we can save an
3057 * additional renegotiation cycle by advertising
3058 * it correctly in the first place.
3059 */
3060 if (curadv != reqadv) {
3061 *lcladv &= ~(ADVERTISE_PAUSE_CAP |
3062 ADVERTISE_PAUSE_ASYM);
3063 tg3_writephy(tp, MII_ADVERTISE, *lcladv | reqadv);
3064 }
3065 }
3066
3067 return 1;
3068}
3069
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
3071{
3072 int current_link_up;
3073 u32 bmsr, dummy;
Matt Carlsonef167e22007-12-20 20:10:01 -08003074 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 u16 current_speed;
3076 u8 current_duplex;
3077 int i, err;
3078
3079 tw32(MAC_EVENT, 0);
3080
3081 tw32_f(MAC_STATUS,
3082 (MAC_STATUS_SYNC_CHANGED |
3083 MAC_STATUS_CFG_CHANGED |
3084 MAC_STATUS_MI_COMPLETION |
3085 MAC_STATUS_LNKSTATE_CHANGED));
3086 udelay(40);
3087
Matt Carlson8ef21422008-05-02 16:47:53 -07003088 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
3089 tw32_f(MAC_MI_MODE,
3090 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
3091 udelay(80);
3092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093
3094 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x02);
3095
3096 /* Some third-party PHYs need to be reset on link going
3097 * down.
3098 */
3099 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3100 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
3101 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
3102 netif_carrier_ok(tp->dev)) {
3103 tg3_readphy(tp, MII_BMSR, &bmsr);
3104 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3105 !(bmsr & BMSR_LSTATUS))
3106 force_reset = 1;
3107 }
3108 if (force_reset)
3109 tg3_phy_reset(tp);
3110
Matt Carlson79eb6902010-02-17 15:17:03 +00003111 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 tg3_readphy(tp, MII_BMSR, &bmsr);
3113 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
3114 !(tp->tg3_flags & TG3_FLAG_INIT_COMPLETE))
3115 bmsr = 0;
3116
3117 if (!(bmsr & BMSR_LSTATUS)) {
3118 err = tg3_init_5401phy_dsp(tp);
3119 if (err)
3120 return err;
3121
3122 tg3_readphy(tp, MII_BMSR, &bmsr);
3123 for (i = 0; i < 1000; i++) {
3124 udelay(10);
3125 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3126 (bmsr & BMSR_LSTATUS)) {
3127 udelay(40);
3128 break;
3129 }
3130 }
3131
Matt Carlson79eb6902010-02-17 15:17:03 +00003132 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
3133 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 !(bmsr & BMSR_LSTATUS) &&
3135 tp->link_config.active_speed == SPEED_1000) {
3136 err = tg3_phy_reset(tp);
3137 if (!err)
3138 err = tg3_init_5401phy_dsp(tp);
3139 if (err)
3140 return err;
3141 }
3142 }
3143 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3144 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
3145 /* 5701 {A0,B0} CRC bug workaround */
3146 tg3_writephy(tp, 0x15, 0x0a75);
3147 tg3_writephy(tp, 0x1c, 0x8c68);
3148 tg3_writephy(tp, 0x1c, 0x8d68);
3149 tg3_writephy(tp, 0x1c, 0x8c68);
3150 }
3151
3152 /* Clear pending interrupts... */
3153 tg3_readphy(tp, MII_TG3_ISTAT, &dummy);
3154 tg3_readphy(tp, MII_TG3_ISTAT, &dummy);
3155
3156 if (tp->tg3_flags & TG3_FLAG_USE_MI_INTERRUPT)
3157 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlson7f97a4b2009-08-25 10:10:03 +00003158 else if (!(tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 tg3_writephy(tp, MII_TG3_IMASK, ~0);
3160
3161 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3162 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3163 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
3164 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3165 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
3166 else
3167 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
3168 }
3169
3170 current_link_up = 0;
3171 current_speed = SPEED_INVALID;
3172 current_duplex = DUPLEX_INVALID;
3173
3174 if (tp->tg3_flags2 & TG3_FLG2_CAPACITIVE_COUPLING) {
3175 u32 val;
3176
3177 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4007);
3178 tg3_readphy(tp, MII_TG3_AUX_CTRL, &val);
3179 if (!(val & (1 << 10))) {
3180 val |= (1 << 10);
3181 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
3182 goto relink;
3183 }
3184 }
3185
3186 bmsr = 0;
3187 for (i = 0; i < 100; i++) {
3188 tg3_readphy(tp, MII_BMSR, &bmsr);
3189 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3190 (bmsr & BMSR_LSTATUS))
3191 break;
3192 udelay(40);
3193 }
3194
3195 if (bmsr & BMSR_LSTATUS) {
3196 u32 aux_stat, bmcr;
3197
3198 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
3199 for (i = 0; i < 2000; i++) {
3200 udelay(10);
3201 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
3202 aux_stat)
3203 break;
3204 }
3205
3206 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
3207 &current_speed,
3208 &current_duplex);
3209
3210 bmcr = 0;
3211 for (i = 0; i < 200; i++) {
3212 tg3_readphy(tp, MII_BMCR, &bmcr);
3213 if (tg3_readphy(tp, MII_BMCR, &bmcr))
3214 continue;
3215 if (bmcr && bmcr != 0x7fff)
3216 break;
3217 udelay(10);
3218 }
3219
Matt Carlsonef167e22007-12-20 20:10:01 -08003220 lcl_adv = 0;
3221 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222
Matt Carlsonef167e22007-12-20 20:10:01 -08003223 tp->link_config.active_speed = current_speed;
3224 tp->link_config.active_duplex = current_duplex;
3225
3226 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3227 if ((bmcr & BMCR_ANENABLE) &&
3228 tg3_copper_is_advertising_all(tp,
3229 tp->link_config.advertising)) {
3230 if (tg3_adv_1000T_flowctrl_ok(tp, &lcl_adv,
3231 &rmt_adv))
3232 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 }
3234 } else {
3235 if (!(bmcr & BMCR_ANENABLE) &&
3236 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08003237 tp->link_config.duplex == current_duplex &&
3238 tp->link_config.flowctrl ==
3239 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 }
3242 }
3243
Matt Carlsonef167e22007-12-20 20:10:01 -08003244 if (current_link_up == 1 &&
3245 tp->link_config.active_duplex == DUPLEX_FULL)
3246 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 }
3248
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249relink:
Michael Chan6921d202005-12-13 21:15:53 -08003250 if (current_link_up == 0 || tp->link_config.phy_is_low_power) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 u32 tmp;
3252
3253 tg3_phy_copper_begin(tp);
3254
3255 tg3_readphy(tp, MII_BMSR, &tmp);
3256 if (!tg3_readphy(tp, MII_BMSR, &tmp) &&
3257 (tmp & BMSR_LSTATUS))
3258 current_link_up = 1;
3259 }
3260
3261 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
3262 if (current_link_up == 1) {
3263 if (tp->link_config.active_speed == SPEED_100 ||
3264 tp->link_config.active_speed == SPEED_10)
3265 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3266 else
3267 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlson7f97a4b2009-08-25 10:10:03 +00003268 } else if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET)
3269 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3270 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
3272
3273 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
3274 if (tp->link_config.active_duplex == DUPLEX_HALF)
3275 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
3276
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003278 if (current_link_up == 1 &&
3279 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003281 else
3282 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 }
3284
3285 /* ??? Without this setting Netgear GA302T PHY does not
3286 * ??? send/receive packets...
3287 */
Matt Carlson79eb6902010-02-17 15:17:03 +00003288 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
3290 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
3291 tw32_f(MAC_MI_MODE, tp->mi_mode);
3292 udelay(80);
3293 }
3294
3295 tw32_f(MAC_MODE, tp->mac_mode);
3296 udelay(40);
3297
3298 if (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) {
3299 /* Polled via timer. */
3300 tw32_f(MAC_EVENT, 0);
3301 } else {
3302 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
3303 }
3304 udelay(40);
3305
3306 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
3307 current_link_up == 1 &&
3308 tp->link_config.active_speed == SPEED_1000 &&
3309 ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) ||
3310 (tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED))) {
3311 udelay(120);
3312 tw32_f(MAC_STATUS,
3313 (MAC_STATUS_SYNC_CHANGED |
3314 MAC_STATUS_CFG_CHANGED));
3315 udelay(40);
3316 tg3_write_mem(tp,
3317 NIC_SRAM_FIRMWARE_MBOX,
3318 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
3319 }
3320
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003321 /* Prevent send BD corruption. */
3322 if (tp->tg3_flags3 & TG3_FLG3_CLKREQ_BUG) {
3323 u16 oldlnkctl, newlnkctl;
3324
3325 pci_read_config_word(tp->pdev,
3326 tp->pcie_cap + PCI_EXP_LNKCTL,
3327 &oldlnkctl);
3328 if (tp->link_config.active_speed == SPEED_100 ||
3329 tp->link_config.active_speed == SPEED_10)
3330 newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
3331 else
3332 newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
3333 if (newlnkctl != oldlnkctl)
3334 pci_write_config_word(tp->pdev,
3335 tp->pcie_cap + PCI_EXP_LNKCTL,
3336 newlnkctl);
3337 }
3338
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 if (current_link_up != netif_carrier_ok(tp->dev)) {
3340 if (current_link_up)
3341 netif_carrier_on(tp->dev);
3342 else
3343 netif_carrier_off(tp->dev);
3344 tg3_link_report(tp);
3345 }
3346
3347 return 0;
3348}
3349
3350struct tg3_fiber_aneginfo {
3351 int state;
3352#define ANEG_STATE_UNKNOWN 0
3353#define ANEG_STATE_AN_ENABLE 1
3354#define ANEG_STATE_RESTART_INIT 2
3355#define ANEG_STATE_RESTART 3
3356#define ANEG_STATE_DISABLE_LINK_OK 4
3357#define ANEG_STATE_ABILITY_DETECT_INIT 5
3358#define ANEG_STATE_ABILITY_DETECT 6
3359#define ANEG_STATE_ACK_DETECT_INIT 7
3360#define ANEG_STATE_ACK_DETECT 8
3361#define ANEG_STATE_COMPLETE_ACK_INIT 9
3362#define ANEG_STATE_COMPLETE_ACK 10
3363#define ANEG_STATE_IDLE_DETECT_INIT 11
3364#define ANEG_STATE_IDLE_DETECT 12
3365#define ANEG_STATE_LINK_OK 13
3366#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
3367#define ANEG_STATE_NEXT_PAGE_WAIT 15
3368
3369 u32 flags;
3370#define MR_AN_ENABLE 0x00000001
3371#define MR_RESTART_AN 0x00000002
3372#define MR_AN_COMPLETE 0x00000004
3373#define MR_PAGE_RX 0x00000008
3374#define MR_NP_LOADED 0x00000010
3375#define MR_TOGGLE_TX 0x00000020
3376#define MR_LP_ADV_FULL_DUPLEX 0x00000040
3377#define MR_LP_ADV_HALF_DUPLEX 0x00000080
3378#define MR_LP_ADV_SYM_PAUSE 0x00000100
3379#define MR_LP_ADV_ASYM_PAUSE 0x00000200
3380#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
3381#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
3382#define MR_LP_ADV_NEXT_PAGE 0x00001000
3383#define MR_TOGGLE_RX 0x00002000
3384#define MR_NP_RX 0x00004000
3385
3386#define MR_LINK_OK 0x80000000
3387
3388 unsigned long link_time, cur_time;
3389
3390 u32 ability_match_cfg;
3391 int ability_match_count;
3392
3393 char ability_match, idle_match, ack_match;
3394
3395 u32 txconfig, rxconfig;
3396#define ANEG_CFG_NP 0x00000080
3397#define ANEG_CFG_ACK 0x00000040
3398#define ANEG_CFG_RF2 0x00000020
3399#define ANEG_CFG_RF1 0x00000010
3400#define ANEG_CFG_PS2 0x00000001
3401#define ANEG_CFG_PS1 0x00008000
3402#define ANEG_CFG_HD 0x00004000
3403#define ANEG_CFG_FD 0x00002000
3404#define ANEG_CFG_INVAL 0x00001f06
3405
3406};
3407#define ANEG_OK 0
3408#define ANEG_DONE 1
3409#define ANEG_TIMER_ENAB 2
3410#define ANEG_FAILED -1
3411
3412#define ANEG_STATE_SETTLE_TIME 10000
3413
3414static int tg3_fiber_aneg_smachine(struct tg3 *tp,
3415 struct tg3_fiber_aneginfo *ap)
3416{
Matt Carlson5be73b42007-12-20 20:09:29 -08003417 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 unsigned long delta;
3419 u32 rx_cfg_reg;
3420 int ret;
3421
3422 if (ap->state == ANEG_STATE_UNKNOWN) {
3423 ap->rxconfig = 0;
3424 ap->link_time = 0;
3425 ap->cur_time = 0;
3426 ap->ability_match_cfg = 0;
3427 ap->ability_match_count = 0;
3428 ap->ability_match = 0;
3429 ap->idle_match = 0;
3430 ap->ack_match = 0;
3431 }
3432 ap->cur_time++;
3433
3434 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
3435 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
3436
3437 if (rx_cfg_reg != ap->ability_match_cfg) {
3438 ap->ability_match_cfg = rx_cfg_reg;
3439 ap->ability_match = 0;
3440 ap->ability_match_count = 0;
3441 } else {
3442 if (++ap->ability_match_count > 1) {
3443 ap->ability_match = 1;
3444 ap->ability_match_cfg = rx_cfg_reg;
3445 }
3446 }
3447 if (rx_cfg_reg & ANEG_CFG_ACK)
3448 ap->ack_match = 1;
3449 else
3450 ap->ack_match = 0;
3451
3452 ap->idle_match = 0;
3453 } else {
3454 ap->idle_match = 1;
3455 ap->ability_match_cfg = 0;
3456 ap->ability_match_count = 0;
3457 ap->ability_match = 0;
3458 ap->ack_match = 0;
3459
3460 rx_cfg_reg = 0;
3461 }
3462
3463 ap->rxconfig = rx_cfg_reg;
3464 ret = ANEG_OK;
3465
Matt Carlson33f401a2010-04-05 10:19:27 +00003466 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 case ANEG_STATE_UNKNOWN:
3468 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
3469 ap->state = ANEG_STATE_AN_ENABLE;
3470
3471 /* fallthru */
3472 case ANEG_STATE_AN_ENABLE:
3473 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
3474 if (ap->flags & MR_AN_ENABLE) {
3475 ap->link_time = 0;
3476 ap->cur_time = 0;
3477 ap->ability_match_cfg = 0;
3478 ap->ability_match_count = 0;
3479 ap->ability_match = 0;
3480 ap->idle_match = 0;
3481 ap->ack_match = 0;
3482
3483 ap->state = ANEG_STATE_RESTART_INIT;
3484 } else {
3485 ap->state = ANEG_STATE_DISABLE_LINK_OK;
3486 }
3487 break;
3488
3489 case ANEG_STATE_RESTART_INIT:
3490 ap->link_time = ap->cur_time;
3491 ap->flags &= ~(MR_NP_LOADED);
3492 ap->txconfig = 0;
3493 tw32(MAC_TX_AUTO_NEG, 0);
3494 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3495 tw32_f(MAC_MODE, tp->mac_mode);
3496 udelay(40);
3497
3498 ret = ANEG_TIMER_ENAB;
3499 ap->state = ANEG_STATE_RESTART;
3500
3501 /* fallthru */
3502 case ANEG_STATE_RESTART:
3503 delta = ap->cur_time - ap->link_time;
Matt Carlson859a588792010-04-05 10:19:28 +00003504 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a588792010-04-05 10:19:28 +00003506 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 break;
3509
3510 case ANEG_STATE_DISABLE_LINK_OK:
3511 ret = ANEG_DONE;
3512 break;
3513
3514 case ANEG_STATE_ABILITY_DETECT_INIT:
3515 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08003516 ap->txconfig = ANEG_CFG_FD;
3517 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3518 if (flowctrl & ADVERTISE_1000XPAUSE)
3519 ap->txconfig |= ANEG_CFG_PS1;
3520 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3521 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3523 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3524 tw32_f(MAC_MODE, tp->mac_mode);
3525 udelay(40);
3526
3527 ap->state = ANEG_STATE_ABILITY_DETECT;
3528 break;
3529
3530 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a588792010-04-05 10:19:28 +00003531 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 break;
3534
3535 case ANEG_STATE_ACK_DETECT_INIT:
3536 ap->txconfig |= ANEG_CFG_ACK;
3537 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3538 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3539 tw32_f(MAC_MODE, tp->mac_mode);
3540 udelay(40);
3541
3542 ap->state = ANEG_STATE_ACK_DETECT;
3543
3544 /* fallthru */
3545 case ANEG_STATE_ACK_DETECT:
3546 if (ap->ack_match != 0) {
3547 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
3548 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
3549 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
3550 } else {
3551 ap->state = ANEG_STATE_AN_ENABLE;
3552 }
3553 } else if (ap->ability_match != 0 &&
3554 ap->rxconfig == 0) {
3555 ap->state = ANEG_STATE_AN_ENABLE;
3556 }
3557 break;
3558
3559 case ANEG_STATE_COMPLETE_ACK_INIT:
3560 if (ap->rxconfig & ANEG_CFG_INVAL) {
3561 ret = ANEG_FAILED;
3562 break;
3563 }
3564 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
3565 MR_LP_ADV_HALF_DUPLEX |
3566 MR_LP_ADV_SYM_PAUSE |
3567 MR_LP_ADV_ASYM_PAUSE |
3568 MR_LP_ADV_REMOTE_FAULT1 |
3569 MR_LP_ADV_REMOTE_FAULT2 |
3570 MR_LP_ADV_NEXT_PAGE |
3571 MR_TOGGLE_RX |
3572 MR_NP_RX);
3573 if (ap->rxconfig & ANEG_CFG_FD)
3574 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
3575 if (ap->rxconfig & ANEG_CFG_HD)
3576 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
3577 if (ap->rxconfig & ANEG_CFG_PS1)
3578 ap->flags |= MR_LP_ADV_SYM_PAUSE;
3579 if (ap->rxconfig & ANEG_CFG_PS2)
3580 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
3581 if (ap->rxconfig & ANEG_CFG_RF1)
3582 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
3583 if (ap->rxconfig & ANEG_CFG_RF2)
3584 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
3585 if (ap->rxconfig & ANEG_CFG_NP)
3586 ap->flags |= MR_LP_ADV_NEXT_PAGE;
3587
3588 ap->link_time = ap->cur_time;
3589
3590 ap->flags ^= (MR_TOGGLE_TX);
3591 if (ap->rxconfig & 0x0008)
3592 ap->flags |= MR_TOGGLE_RX;
3593 if (ap->rxconfig & ANEG_CFG_NP)
3594 ap->flags |= MR_NP_RX;
3595 ap->flags |= MR_PAGE_RX;
3596
3597 ap->state = ANEG_STATE_COMPLETE_ACK;
3598 ret = ANEG_TIMER_ENAB;
3599 break;
3600
3601 case ANEG_STATE_COMPLETE_ACK:
3602 if (ap->ability_match != 0 &&
3603 ap->rxconfig == 0) {
3604 ap->state = ANEG_STATE_AN_ENABLE;
3605 break;
3606 }
3607 delta = ap->cur_time - ap->link_time;
3608 if (delta > ANEG_STATE_SETTLE_TIME) {
3609 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
3610 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3611 } else {
3612 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
3613 !(ap->flags & MR_NP_RX)) {
3614 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3615 } else {
3616 ret = ANEG_FAILED;
3617 }
3618 }
3619 }
3620 break;
3621
3622 case ANEG_STATE_IDLE_DETECT_INIT:
3623 ap->link_time = ap->cur_time;
3624 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3625 tw32_f(MAC_MODE, tp->mac_mode);
3626 udelay(40);
3627
3628 ap->state = ANEG_STATE_IDLE_DETECT;
3629 ret = ANEG_TIMER_ENAB;
3630 break;
3631
3632 case ANEG_STATE_IDLE_DETECT:
3633 if (ap->ability_match != 0 &&
3634 ap->rxconfig == 0) {
3635 ap->state = ANEG_STATE_AN_ENABLE;
3636 break;
3637 }
3638 delta = ap->cur_time - ap->link_time;
3639 if (delta > ANEG_STATE_SETTLE_TIME) {
3640 /* XXX another gem from the Broadcom driver :( */
3641 ap->state = ANEG_STATE_LINK_OK;
3642 }
3643 break;
3644
3645 case ANEG_STATE_LINK_OK:
3646 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
3647 ret = ANEG_DONE;
3648 break;
3649
3650 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
3651 /* ??? unimplemented */
3652 break;
3653
3654 case ANEG_STATE_NEXT_PAGE_WAIT:
3655 /* ??? unimplemented */
3656 break;
3657
3658 default:
3659 ret = ANEG_FAILED;
3660 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662
3663 return ret;
3664}
3665
Matt Carlson5be73b42007-12-20 20:09:29 -08003666static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667{
3668 int res = 0;
3669 struct tg3_fiber_aneginfo aninfo;
3670 int status = ANEG_FAILED;
3671 unsigned int tick;
3672 u32 tmp;
3673
3674 tw32_f(MAC_TX_AUTO_NEG, 0);
3675
3676 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
3677 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
3678 udelay(40);
3679
3680 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
3681 udelay(40);
3682
3683 memset(&aninfo, 0, sizeof(aninfo));
3684 aninfo.flags |= MR_AN_ENABLE;
3685 aninfo.state = ANEG_STATE_UNKNOWN;
3686 aninfo.cur_time = 0;
3687 tick = 0;
3688 while (++tick < 195000) {
3689 status = tg3_fiber_aneg_smachine(tp, &aninfo);
3690 if (status == ANEG_DONE || status == ANEG_FAILED)
3691 break;
3692
3693 udelay(1);
3694 }
3695
3696 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3697 tw32_f(MAC_MODE, tp->mac_mode);
3698 udelay(40);
3699
Matt Carlson5be73b42007-12-20 20:09:29 -08003700 *txflags = aninfo.txconfig;
3701 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702
3703 if (status == ANEG_DONE &&
3704 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
3705 MR_LP_ADV_FULL_DUPLEX)))
3706 res = 1;
3707
3708 return res;
3709}
3710
3711static void tg3_init_bcm8002(struct tg3 *tp)
3712{
3713 u32 mac_status = tr32(MAC_STATUS);
3714 int i;
3715
3716 /* Reset when initting first time or we have a link. */
3717 if ((tp->tg3_flags & TG3_FLAG_INIT_COMPLETE) &&
3718 !(mac_status & MAC_STATUS_PCS_SYNCED))
3719 return;
3720
3721 /* Set PLL lock range. */
3722 tg3_writephy(tp, 0x16, 0x8007);
3723
3724 /* SW reset */
3725 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
3726
3727 /* Wait for reset to complete. */
3728 /* XXX schedule_timeout() ... */
3729 for (i = 0; i < 500; i++)
3730 udelay(10);
3731
3732 /* Config mode; select PMA/Ch 1 regs. */
3733 tg3_writephy(tp, 0x10, 0x8411);
3734
3735 /* Enable auto-lock and comdet, select txclk for tx. */
3736 tg3_writephy(tp, 0x11, 0x0a10);
3737
3738 tg3_writephy(tp, 0x18, 0x00a0);
3739 tg3_writephy(tp, 0x16, 0x41ff);
3740
3741 /* Assert and deassert POR. */
3742 tg3_writephy(tp, 0x13, 0x0400);
3743 udelay(40);
3744 tg3_writephy(tp, 0x13, 0x0000);
3745
3746 tg3_writephy(tp, 0x11, 0x0a50);
3747 udelay(40);
3748 tg3_writephy(tp, 0x11, 0x0a10);
3749
3750 /* Wait for signal to stabilize */
3751 /* XXX schedule_timeout() ... */
3752 for (i = 0; i < 15000; i++)
3753 udelay(10);
3754
3755 /* Deselect the channel register so we can read the PHYID
3756 * later.
3757 */
3758 tg3_writephy(tp, 0x10, 0x8011);
3759}
3760
3761static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
3762{
Matt Carlson82cd3d12007-12-20 20:09:00 -08003763 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 u32 sg_dig_ctrl, sg_dig_status;
3765 u32 serdes_cfg, expected_sg_dig_ctrl;
3766 int workaround, port_a;
3767 int current_link_up;
3768
3769 serdes_cfg = 0;
3770 expected_sg_dig_ctrl = 0;
3771 workaround = 0;
3772 port_a = 1;
3773 current_link_up = 0;
3774
3775 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
3776 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
3777 workaround = 1;
3778 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
3779 port_a = 0;
3780
3781 /* preserve bits 0-11,13,14 for signal pre-emphasis */
3782 /* preserve bits 20-23 for voltage regulator */
3783 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
3784 }
3785
3786 sg_dig_ctrl = tr32(SG_DIG_CTRL);
3787
3788 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003789 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 if (workaround) {
3791 u32 val = serdes_cfg;
3792
3793 if (port_a)
3794 val |= 0xc010000;
3795 else
3796 val |= 0x4010000;
3797 tw32_f(MAC_SERDES_CFG, val);
3798 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003799
3800 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801 }
3802 if (mac_status & MAC_STATUS_PCS_SYNCED) {
3803 tg3_setup_flow_control(tp, 0, 0);
3804 current_link_up = 1;
3805 }
3806 goto out;
3807 }
3808
3809 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003810 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811
Matt Carlson82cd3d12007-12-20 20:09:00 -08003812 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3813 if (flowctrl & ADVERTISE_1000XPAUSE)
3814 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
3815 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3816 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817
3818 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003819 if ((tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT) &&
3820 tp->serdes_counter &&
3821 ((mac_status & (MAC_STATUS_PCS_SYNCED |
3822 MAC_STATUS_RCVD_CFG)) ==
3823 MAC_STATUS_PCS_SYNCED)) {
3824 tp->serdes_counter--;
3825 current_link_up = 1;
3826 goto out;
3827 }
3828restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 if (workaround)
3830 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003831 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 udelay(5);
3833 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
3834
Michael Chan3d3ebe72006-09-27 15:59:15 -07003835 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
3836 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
3838 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003839 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840 mac_status = tr32(MAC_STATUS);
3841
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003842 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08003844 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845
Matt Carlson82cd3d12007-12-20 20:09:00 -08003846 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
3847 local_adv |= ADVERTISE_1000XPAUSE;
3848 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
3849 local_adv |= ADVERTISE_1000XPSE_ASYM;
3850
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003851 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003852 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003853 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003854 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855
3856 tg3_setup_flow_control(tp, local_adv, remote_adv);
3857 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003858 tp->serdes_counter = 0;
3859 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003860 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003861 if (tp->serdes_counter)
3862 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 else {
3864 if (workaround) {
3865 u32 val = serdes_cfg;
3866
3867 if (port_a)
3868 val |= 0xc010000;
3869 else
3870 val |= 0x4010000;
3871
3872 tw32_f(MAC_SERDES_CFG, val);
3873 }
3874
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003875 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 udelay(40);
3877
3878 /* Link parallel detection - link is up */
3879 /* only if we have PCS_SYNC and not */
3880 /* receiving config code words */
3881 mac_status = tr32(MAC_STATUS);
3882 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
3883 !(mac_status & MAC_STATUS_RCVD_CFG)) {
3884 tg3_setup_flow_control(tp, 0, 0);
3885 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003886 tp->tg3_flags2 |=
3887 TG3_FLG2_PARALLEL_DETECT;
3888 tp->serdes_counter =
3889 SERDES_PARALLEL_DET_TIMEOUT;
3890 } else
3891 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 }
3893 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07003894 } else {
3895 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
3896 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897 }
3898
3899out:
3900 return current_link_up;
3901}
3902
3903static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
3904{
3905 int current_link_up = 0;
3906
Michael Chan5cf64b8a2007-05-05 12:11:21 -07003907 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909
3910 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08003911 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003913
Matt Carlson5be73b42007-12-20 20:09:29 -08003914 if (fiber_autoneg(tp, &txflags, &rxflags)) {
3915 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916
Matt Carlson5be73b42007-12-20 20:09:29 -08003917 if (txflags & ANEG_CFG_PS1)
3918 local_adv |= ADVERTISE_1000XPAUSE;
3919 if (txflags & ANEG_CFG_PS2)
3920 local_adv |= ADVERTISE_1000XPSE_ASYM;
3921
3922 if (rxflags & MR_LP_ADV_SYM_PAUSE)
3923 remote_adv |= LPA_1000XPAUSE;
3924 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
3925 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926
3927 tg3_setup_flow_control(tp, local_adv, remote_adv);
3928
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 current_link_up = 1;
3930 }
3931 for (i = 0; i < 30; i++) {
3932 udelay(20);
3933 tw32_f(MAC_STATUS,
3934 (MAC_STATUS_SYNC_CHANGED |
3935 MAC_STATUS_CFG_CHANGED));
3936 udelay(40);
3937 if ((tr32(MAC_STATUS) &
3938 (MAC_STATUS_SYNC_CHANGED |
3939 MAC_STATUS_CFG_CHANGED)) == 0)
3940 break;
3941 }
3942
3943 mac_status = tr32(MAC_STATUS);
3944 if (current_link_up == 0 &&
3945 (mac_status & MAC_STATUS_PCS_SYNCED) &&
3946 !(mac_status & MAC_STATUS_RCVD_CFG))
3947 current_link_up = 1;
3948 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08003949 tg3_setup_flow_control(tp, 0, 0);
3950
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 /* Forcing 1000FD link up. */
3952 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953
3954 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
3955 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003956
3957 tw32_f(MAC_MODE, tp->mac_mode);
3958 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 }
3960
3961out:
3962 return current_link_up;
3963}
3964
3965static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
3966{
3967 u32 orig_pause_cfg;
3968 u16 orig_active_speed;
3969 u8 orig_active_duplex;
3970 u32 mac_status;
3971 int current_link_up;
3972 int i;
3973
Matt Carlson8d018622007-12-20 20:05:44 -08003974 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 orig_active_speed = tp->link_config.active_speed;
3976 orig_active_duplex = tp->link_config.active_duplex;
3977
3978 if (!(tp->tg3_flags2 & TG3_FLG2_HW_AUTONEG) &&
3979 netif_carrier_ok(tp->dev) &&
3980 (tp->tg3_flags & TG3_FLAG_INIT_COMPLETE)) {
3981 mac_status = tr32(MAC_STATUS);
3982 mac_status &= (MAC_STATUS_PCS_SYNCED |
3983 MAC_STATUS_SIGNAL_DET |
3984 MAC_STATUS_CFG_CHANGED |
3985 MAC_STATUS_RCVD_CFG);
3986 if (mac_status == (MAC_STATUS_PCS_SYNCED |
3987 MAC_STATUS_SIGNAL_DET)) {
3988 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
3989 MAC_STATUS_CFG_CHANGED));
3990 return 0;
3991 }
3992 }
3993
3994 tw32_f(MAC_TX_AUTO_NEG, 0);
3995
3996 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
3997 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
3998 tw32_f(MAC_MODE, tp->mac_mode);
3999 udelay(40);
4000
Matt Carlson79eb6902010-02-17 15:17:03 +00004001 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 tg3_init_bcm8002(tp);
4003
4004 /* Enable link change event even when serdes polling. */
4005 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4006 udelay(40);
4007
4008 current_link_up = 0;
4009 mac_status = tr32(MAC_STATUS);
4010
4011 if (tp->tg3_flags2 & TG3_FLG2_HW_AUTONEG)
4012 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
4013 else
4014 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
4015
Matt Carlson898a56f2009-08-28 14:02:40 +00004016 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00004018 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019
4020 for (i = 0; i < 100; i++) {
4021 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4022 MAC_STATUS_CFG_CHANGED));
4023 udelay(5);
4024 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07004025 MAC_STATUS_CFG_CHANGED |
4026 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 break;
4028 }
4029
4030 mac_status = tr32(MAC_STATUS);
4031 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
4032 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004033 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
4034 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 tw32_f(MAC_MODE, (tp->mac_mode |
4036 MAC_MODE_SEND_CONFIGS));
4037 udelay(1);
4038 tw32_f(MAC_MODE, tp->mac_mode);
4039 }
4040 }
4041
4042 if (current_link_up == 1) {
4043 tp->link_config.active_speed = SPEED_1000;
4044 tp->link_config.active_duplex = DUPLEX_FULL;
4045 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4046 LED_CTRL_LNKLED_OVERRIDE |
4047 LED_CTRL_1000MBPS_ON));
4048 } else {
4049 tp->link_config.active_speed = SPEED_INVALID;
4050 tp->link_config.active_duplex = DUPLEX_INVALID;
4051 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4052 LED_CTRL_LNKLED_OVERRIDE |
4053 LED_CTRL_TRAFFIC_OVERRIDE));
4054 }
4055
4056 if (current_link_up != netif_carrier_ok(tp->dev)) {
4057 if (current_link_up)
4058 netif_carrier_on(tp->dev);
4059 else
4060 netif_carrier_off(tp->dev);
4061 tg3_link_report(tp);
4062 } else {
Matt Carlson8d018622007-12-20 20:05:44 -08004063 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 if (orig_pause_cfg != now_pause_cfg ||
4065 orig_active_speed != tp->link_config.active_speed ||
4066 orig_active_duplex != tp->link_config.active_duplex)
4067 tg3_link_report(tp);
4068 }
4069
4070 return 0;
4071}
4072
Michael Chan747e8f82005-07-25 12:33:22 -07004073static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
4074{
4075 int current_link_up, err = 0;
4076 u32 bmsr, bmcr;
4077 u16 current_speed;
4078 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08004079 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07004080
4081 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4082 tw32_f(MAC_MODE, tp->mac_mode);
4083 udelay(40);
4084
4085 tw32(MAC_EVENT, 0);
4086
4087 tw32_f(MAC_STATUS,
4088 (MAC_STATUS_SYNC_CHANGED |
4089 MAC_STATUS_CFG_CHANGED |
4090 MAC_STATUS_MI_COMPLETION |
4091 MAC_STATUS_LNKSTATE_CHANGED));
4092 udelay(40);
4093
4094 if (force_reset)
4095 tg3_phy_reset(tp);
4096
4097 current_link_up = 0;
4098 current_speed = SPEED_INVALID;
4099 current_duplex = DUPLEX_INVALID;
4100
4101 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4102 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004103 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
4104 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4105 bmsr |= BMSR_LSTATUS;
4106 else
4107 bmsr &= ~BMSR_LSTATUS;
4108 }
Michael Chan747e8f82005-07-25 12:33:22 -07004109
4110 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
4111
4112 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlson2bd3ed02008-06-09 15:39:55 -07004113 (tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004114 /* do nothing, just check for link up at the end */
4115 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4116 u32 adv, new_adv;
4117
4118 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4119 new_adv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
4120 ADVERTISE_1000XPAUSE |
4121 ADVERTISE_1000XPSE_ASYM |
4122 ADVERTISE_SLCT);
4123
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004124 new_adv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Michael Chan747e8f82005-07-25 12:33:22 -07004125
4126 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
4127 new_adv |= ADVERTISE_1000XHALF;
4128 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
4129 new_adv |= ADVERTISE_1000XFULL;
4130
4131 if ((new_adv != adv) || !(bmcr & BMCR_ANENABLE)) {
4132 tg3_writephy(tp, MII_ADVERTISE, new_adv);
4133 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
4134 tg3_writephy(tp, MII_BMCR, bmcr);
4135
4136 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07004137 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Michael Chan747e8f82005-07-25 12:33:22 -07004138 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
4139
4140 return err;
4141 }
4142 } else {
4143 u32 new_bmcr;
4144
4145 bmcr &= ~BMCR_SPEED1000;
4146 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
4147
4148 if (tp->link_config.duplex == DUPLEX_FULL)
4149 new_bmcr |= BMCR_FULLDPLX;
4150
4151 if (new_bmcr != bmcr) {
4152 /* BMCR_SPEED1000 is a reserved bit that needs
4153 * to be set on write.
4154 */
4155 new_bmcr |= BMCR_SPEED1000;
4156
4157 /* Force a linkdown */
4158 if (netif_carrier_ok(tp->dev)) {
4159 u32 adv;
4160
4161 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4162 adv &= ~(ADVERTISE_1000XFULL |
4163 ADVERTISE_1000XHALF |
4164 ADVERTISE_SLCT);
4165 tg3_writephy(tp, MII_ADVERTISE, adv);
4166 tg3_writephy(tp, MII_BMCR, bmcr |
4167 BMCR_ANRESTART |
4168 BMCR_ANENABLE);
4169 udelay(10);
4170 netif_carrier_off(tp->dev);
4171 }
4172 tg3_writephy(tp, MII_BMCR, new_bmcr);
4173 bmcr = new_bmcr;
4174 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4175 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004176 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
4177 ASIC_REV_5714) {
4178 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4179 bmsr |= BMSR_LSTATUS;
4180 else
4181 bmsr &= ~BMSR_LSTATUS;
4182 }
Michael Chan747e8f82005-07-25 12:33:22 -07004183 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
4184 }
4185 }
4186
4187 if (bmsr & BMSR_LSTATUS) {
4188 current_speed = SPEED_1000;
4189 current_link_up = 1;
4190 if (bmcr & BMCR_FULLDPLX)
4191 current_duplex = DUPLEX_FULL;
4192 else
4193 current_duplex = DUPLEX_HALF;
4194
Matt Carlsonef167e22007-12-20 20:10:01 -08004195 local_adv = 0;
4196 remote_adv = 0;
4197
Michael Chan747e8f82005-07-25 12:33:22 -07004198 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08004199 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07004200
4201 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
4202 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
4203 common = local_adv & remote_adv;
4204 if (common & (ADVERTISE_1000XHALF |
4205 ADVERTISE_1000XFULL)) {
4206 if (common & ADVERTISE_1000XFULL)
4207 current_duplex = DUPLEX_FULL;
4208 else
4209 current_duplex = DUPLEX_HALF;
Matt Carlson57d8b882010-06-05 17:24:35 +00004210 } else if (!(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
4211 /* Link is up via parallel detect */
Matt Carlson859a588792010-04-05 10:19:28 +00004212 } else {
Michael Chan747e8f82005-07-25 12:33:22 -07004213 current_link_up = 0;
Matt Carlson859a588792010-04-05 10:19:28 +00004214 }
Michael Chan747e8f82005-07-25 12:33:22 -07004215 }
4216 }
4217
Matt Carlsonef167e22007-12-20 20:10:01 -08004218 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
4219 tg3_setup_flow_control(tp, local_adv, remote_adv);
4220
Michael Chan747e8f82005-07-25 12:33:22 -07004221 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4222 if (tp->link_config.active_duplex == DUPLEX_HALF)
4223 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4224
4225 tw32_f(MAC_MODE, tp->mac_mode);
4226 udelay(40);
4227
4228 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4229
4230 tp->link_config.active_speed = current_speed;
4231 tp->link_config.active_duplex = current_duplex;
4232
4233 if (current_link_up != netif_carrier_ok(tp->dev)) {
4234 if (current_link_up)
4235 netif_carrier_on(tp->dev);
4236 else {
4237 netif_carrier_off(tp->dev);
4238 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
4239 }
4240 tg3_link_report(tp);
4241 }
4242 return err;
4243}
4244
4245static void tg3_serdes_parallel_detect(struct tg3 *tp)
4246{
Michael Chan3d3ebe72006-09-27 15:59:15 -07004247 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07004248 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07004249 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07004250 return;
4251 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00004252
Michael Chan747e8f82005-07-25 12:33:22 -07004253 if (!netif_carrier_ok(tp->dev) &&
4254 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
4255 u32 bmcr;
4256
4257 tg3_readphy(tp, MII_BMCR, &bmcr);
4258 if (bmcr & BMCR_ANENABLE) {
4259 u32 phy1, phy2;
4260
4261 /* Select shadow register 0x1f */
4262 tg3_writephy(tp, 0x1c, 0x7c00);
4263 tg3_readphy(tp, 0x1c, &phy1);
4264
4265 /* Select expansion interrupt status register */
4266 tg3_writephy(tp, 0x17, 0x0f01);
4267 tg3_readphy(tp, 0x15, &phy2);
4268 tg3_readphy(tp, 0x15, &phy2);
4269
4270 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
4271 /* We have signal detect and not receiving
4272 * config code words, link is up by parallel
4273 * detection.
4274 */
4275
4276 bmcr &= ~BMCR_ANENABLE;
4277 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
4278 tg3_writephy(tp, MII_BMCR, bmcr);
4279 tp->tg3_flags2 |= TG3_FLG2_PARALLEL_DETECT;
4280 }
4281 }
Matt Carlson859a588792010-04-05 10:19:28 +00004282 } else if (netif_carrier_ok(tp->dev) &&
4283 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
4284 (tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004285 u32 phy2;
4286
4287 /* Select expansion interrupt status register */
4288 tg3_writephy(tp, 0x17, 0x0f01);
4289 tg3_readphy(tp, 0x15, &phy2);
4290 if (phy2 & 0x20) {
4291 u32 bmcr;
4292
4293 /* Config code words received, turn on autoneg. */
4294 tg3_readphy(tp, MII_BMCR, &bmcr);
4295 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
4296
4297 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
4298
4299 }
4300 }
4301}
4302
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303static int tg3_setup_phy(struct tg3 *tp, int force_reset)
4304{
4305 int err;
4306
Matt Carlson859a588792010-04-05 10:19:28 +00004307 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlson859a588792010-04-05 10:19:28 +00004309 else if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07004310 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a588792010-04-05 10:19:28 +00004311 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313
Matt Carlsonbcb37f62008-11-03 16:52:09 -08004314 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08004315 u32 val, scale;
4316
4317 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
4318 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
4319 scale = 65;
4320 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
4321 scale = 6;
4322 else
4323 scale = 12;
4324
4325 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
4326 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
4327 tw32(GRC_MISC_CFG, val);
4328 }
4329
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 if (tp->link_config.active_speed == SPEED_1000 &&
4331 tp->link_config.active_duplex == DUPLEX_HALF)
4332 tw32(MAC_TX_LENGTHS,
4333 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4334 (6 << TX_LENGTHS_IPG_SHIFT) |
4335 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
4336 else
4337 tw32(MAC_TX_LENGTHS,
4338 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4339 (6 << TX_LENGTHS_IPG_SHIFT) |
4340 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
4341
4342 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
4343 if (netif_carrier_ok(tp->dev)) {
4344 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07004345 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346 } else {
4347 tw32(HOSTCC_STAT_COAL_TICKS, 0);
4348 }
4349 }
4350
Matt Carlson8ed5d972007-05-07 00:25:49 -07004351 if (tp->tg3_flags & TG3_FLAG_ASPM_WORKAROUND) {
4352 u32 val = tr32(PCIE_PWR_MGMT_THRESH);
4353 if (!netif_carrier_ok(tp->dev))
4354 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
4355 tp->pwrmgmt_thresh;
4356 else
4357 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
4358 tw32(PCIE_PWR_MGMT_THRESH, val);
4359 }
4360
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 return err;
4362}
4363
Michael Chandf3e6542006-05-26 17:48:07 -07004364/* This is called whenever we suspect that the system chipset is re-
4365 * ordering the sequence of MMIO to the tx send mailbox. The symptom
4366 * is bogus tx completions. We try to recover by setting the
4367 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
4368 * in the workqueue.
4369 */
4370static void tg3_tx_recover(struct tg3 *tp)
4371{
4372 BUG_ON((tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER) ||
4373 tp->write32_tx_mbox == tg3_write_indirect_mbox);
4374
Matt Carlson5129c3a2010-04-05 10:19:23 +00004375 netdev_warn(tp->dev,
4376 "The system may be re-ordering memory-mapped I/O "
4377 "cycles to the network device, attempting to recover. "
4378 "Please report the problem to the driver maintainer "
4379 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07004380
4381 spin_lock(&tp->lock);
Michael Chandf3e6542006-05-26 17:48:07 -07004382 tp->tg3_flags |= TG3_FLAG_TX_RECOVERY_PENDING;
Michael Chandf3e6542006-05-26 17:48:07 -07004383 spin_unlock(&tp->lock);
4384}
4385
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004386static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07004387{
4388 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004389 return tnapi->tx_pending -
4390 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07004391}
4392
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393/* Tigon3 never reports partial packet sends. So we do not
4394 * need special logic to handle SKBs that have not had all
4395 * of their frags sent yet, like SunGEM does.
4396 */
Matt Carlson17375d22009-08-28 14:02:18 +00004397static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398{
Matt Carlson17375d22009-08-28 14:02:18 +00004399 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00004400 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004401 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004402 struct netdev_queue *txq;
4403 int index = tnapi - tp->napi;
4404
Matt Carlson19cfaec2009-12-03 08:36:20 +00004405 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004406 index--;
4407
4408 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409
4410 while (sw_idx != hw_idx) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00004411 struct ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07004413 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414
Michael Chandf3e6542006-05-26 17:48:07 -07004415 if (unlikely(skb == NULL)) {
4416 tg3_tx_recover(tp);
4417 return;
4418 }
4419
Alexander Duyckf4188d82009-12-02 16:48:38 +00004420 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004421 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00004422 skb_headlen(skb),
4423 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424
4425 ri->skb = NULL;
4426
4427 sw_idx = NEXT_TX(sw_idx);
4428
4429 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004430 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07004431 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
4432 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00004433
4434 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004435 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00004436 skb_shinfo(skb)->frags[i].size,
4437 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438 sw_idx = NEXT_TX(sw_idx);
4439 }
4440
David S. Millerf47c11e2005-06-24 20:18:35 -07004441 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07004442
4443 if (unlikely(tx_bug)) {
4444 tg3_tx_recover(tp);
4445 return;
4446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 }
4448
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004449 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450
Michael Chan1b2a7202006-08-07 21:46:02 -07004451 /* Need to make the tx_cons update visible to tg3_start_xmit()
4452 * before checking for netif_queue_stopped(). Without the
4453 * memory barrier, there is a small possibility that tg3_start_xmit()
4454 * will miss it and cause the queue to be stopped forever.
4455 */
4456 smp_mb();
4457
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004458 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004459 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004460 __netif_tx_lock(txq, smp_processor_id());
4461 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004462 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004463 netif_tx_wake_queue(txq);
4464 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07004465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466}
4467
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004468static void tg3_rx_skb_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
4469{
4470 if (!ri->skb)
4471 return;
4472
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004473 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004474 map_sz, PCI_DMA_FROMDEVICE);
4475 dev_kfree_skb_any(ri->skb);
4476 ri->skb = NULL;
4477}
4478
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479/* Returns size of skb allocated or < 0 on error.
4480 *
4481 * We only need to fill in the address because the other members
4482 * of the RX descriptor are invariant, see tg3_init_rings.
4483 *
4484 * Note the purposeful assymetry of cpu vs. chip accesses. For
4485 * posting buffers we only dirty the first cache line of the RX
4486 * descriptor (containing the address). Whereas for the RX status
4487 * buffers the cpu only reads the last cacheline of the RX descriptor
4488 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
4489 */
Matt Carlson86b21e52009-11-13 13:03:45 +00004490static int tg3_alloc_rx_skb(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Matt Carlsona3896162009-11-13 13:03:44 +00004491 u32 opaque_key, u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492{
4493 struct tg3_rx_buffer_desc *desc;
4494 struct ring_info *map, *src_map;
4495 struct sk_buff *skb;
4496 dma_addr_t mapping;
4497 int skb_size, dest_idx;
4498
4499 src_map = NULL;
4500 switch (opaque_key) {
4501 case RXD_OPAQUE_RING_STD:
4502 dest_idx = dest_idx_unmasked % TG3_RX_RING_SIZE;
Matt Carlson21f581a2009-08-28 14:00:25 +00004503 desc = &tpr->rx_std[dest_idx];
4504 map = &tpr->rx_std_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004505 skb_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 break;
4507
4508 case RXD_OPAQUE_RING_JUMBO:
4509 dest_idx = dest_idx_unmasked % TG3_RX_JUMBO_RING_SIZE;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00004510 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00004511 map = &tpr->rx_jmb_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004512 skb_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 break;
4514
4515 default:
4516 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518
4519 /* Do not overwrite any of the map or rp information
4520 * until we are sure we can commit to a new buffer.
4521 *
4522 * Callers depend upon this behavior and assume that
4523 * we leave everything unchanged if we fail.
4524 */
Matt Carlson287be122009-08-28 13:58:46 +00004525 skb = netdev_alloc_skb(tp->dev, skb_size + tp->rx_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 if (skb == NULL)
4527 return -ENOMEM;
4528
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 skb_reserve(skb, tp->rx_offset);
4530
Matt Carlson287be122009-08-28 13:58:46 +00004531 mapping = pci_map_single(tp->pdev, skb->data, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 PCI_DMA_FROMDEVICE);
Matt Carlsona21771d2009-11-02 14:25:31 +00004533 if (pci_dma_mapping_error(tp->pdev, mapping)) {
4534 dev_kfree_skb(skb);
4535 return -EIO;
4536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537
4538 map->skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004539 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541 desc->addr_hi = ((u64)mapping >> 32);
4542 desc->addr_lo = ((u64)mapping & 0xffffffff);
4543
4544 return skb_size;
4545}
4546
4547/* We only need to move over in the address because the other
4548 * members of the RX descriptor are invariant. See notes above
4549 * tg3_alloc_rx_skb for full details.
4550 */
Matt Carlsona3896162009-11-13 13:03:44 +00004551static void tg3_recycle_rx(struct tg3_napi *tnapi,
4552 struct tg3_rx_prodring_set *dpr,
4553 u32 opaque_key, int src_idx,
4554 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555{
Matt Carlson17375d22009-08-28 14:02:18 +00004556 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
4558 struct ring_info *src_map, *dest_map;
Matt Carlsona3896162009-11-13 13:03:44 +00004559 struct tg3_rx_prodring_set *spr = &tp->prodring[0];
Matt Carlsonc6cdf432010-04-05 10:19:26 +00004560 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561
4562 switch (opaque_key) {
4563 case RXD_OPAQUE_RING_STD:
4564 dest_idx = dest_idx_unmasked % TG3_RX_RING_SIZE;
Matt Carlsona3896162009-11-13 13:03:44 +00004565 dest_desc = &dpr->rx_std[dest_idx];
4566 dest_map = &dpr->rx_std_buffers[dest_idx];
4567 src_desc = &spr->rx_std[src_idx];
4568 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569 break;
4570
4571 case RXD_OPAQUE_RING_JUMBO:
4572 dest_idx = dest_idx_unmasked % TG3_RX_JUMBO_RING_SIZE;
Matt Carlsona3896162009-11-13 13:03:44 +00004573 dest_desc = &dpr->rx_jmb[dest_idx].std;
4574 dest_map = &dpr->rx_jmb_buffers[dest_idx];
4575 src_desc = &spr->rx_jmb[src_idx].std;
4576 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 break;
4578
4579 default:
4580 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582
4583 dest_map->skb = src_map->skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004584 dma_unmap_addr_set(dest_map, mapping,
4585 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 dest_desc->addr_hi = src_desc->addr_hi;
4587 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00004588
4589 /* Ensure that the update to the skb happens after the physical
4590 * addresses have been transferred to the new BD location.
4591 */
4592 smp_wmb();
4593
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594 src_map->skb = NULL;
4595}
4596
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597/* The RX ring scheme is composed of multiple rings which post fresh
4598 * buffers to the chip, and one special ring the chip uses to report
4599 * status back to the host.
4600 *
4601 * The special ring reports the status of received packets to the
4602 * host. The chip does not write into the original descriptor the
4603 * RX buffer was obtained from. The chip simply takes the original
4604 * descriptor as provided by the host, updates the status and length
4605 * field, then writes this into the next status ring entry.
4606 *
4607 * Each ring the host uses to post buffers to the chip is described
4608 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
4609 * it is first placed into the on-chip ram. When the packet's length
4610 * is known, it walks down the TG3_BDINFO entries to select the ring.
4611 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
4612 * which is within the range of the new packet's length is chosen.
4613 *
4614 * The "separate ring for rx status" scheme may sound queer, but it makes
4615 * sense from a cache coherency perspective. If only the host writes
4616 * to the buffer post rings, and only the chip writes to the rx status
4617 * rings, then cache lines never move beyond shared-modified state.
4618 * If both the host and chip were to write into the same ring, cache line
4619 * eviction could occur since both entities want it in an exclusive state.
4620 */
Matt Carlson17375d22009-08-28 14:02:18 +00004621static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622{
Matt Carlson17375d22009-08-28 14:02:18 +00004623 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07004624 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00004625 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00004626 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07004627 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628 int received;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004629 struct tg3_rx_prodring_set *tpr = tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004631 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632 /*
4633 * We need to order the read of hw_idx and the read of
4634 * the opaque cookie.
4635 */
4636 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637 work_mask = 0;
4638 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00004639 std_prod_idx = tpr->rx_std_prod_idx;
4640 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00004642 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00004643 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644 unsigned int len;
4645 struct sk_buff *skb;
4646 dma_addr_t dma_addr;
4647 u32 opaque_key, desc_idx, *post_ptr;
Matt Carlson9dc7a112010-04-12 06:58:28 +00004648 bool hw_vlan __maybe_unused = false;
4649 u16 vtag __maybe_unused = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650
4651 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
4652 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
4653 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004654 ri = &tp->prodring[0].rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004655 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00004656 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00004657 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07004658 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004660 ri = &tp->prodring[0].rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004661 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00004662 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00004663 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00004664 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666
4667 work_mask |= opaque_key;
4668
4669 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
4670 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
4671 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00004672 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 desc_idx, *post_ptr);
4674 drop_it_no_recycle:
4675 /* Other statistics kept track of by card. */
4676 tp->net_stats.rx_dropped++;
4677 goto next_pkt;
4678 }
4679
Matt Carlsonad829262008-11-21 17:16:16 -08004680 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
4681 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682
Matt Carlsond2757fc2010-04-12 06:58:27 +00004683 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 int skb_size;
4685
Matt Carlson86b21e52009-11-13 13:03:45 +00004686 skb_size = tg3_alloc_rx_skb(tp, tpr, opaque_key,
Matt Carlsonafc081f2009-11-13 13:03:43 +00004687 *post_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 if (skb_size < 0)
4689 goto drop_it;
4690
Matt Carlson287be122009-08-28 13:58:46 +00004691 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 PCI_DMA_FROMDEVICE);
4693
Matt Carlson61e800c2010-02-17 15:16:54 +00004694 /* Ensure that the update to the skb happens
4695 * after the usage of the old DMA mapping.
4696 */
4697 smp_wmb();
4698
4699 ri->skb = NULL;
4700
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701 skb_put(skb, len);
4702 } else {
4703 struct sk_buff *copy_skb;
4704
Matt Carlsona3896162009-11-13 13:03:44 +00004705 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 desc_idx, *post_ptr);
4707
Matt Carlson9dc7a112010-04-12 06:58:28 +00004708 copy_skb = netdev_alloc_skb(tp->dev, len + VLAN_HLEN +
4709 TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710 if (copy_skb == NULL)
4711 goto drop_it_no_recycle;
4712
Matt Carlson9dc7a112010-04-12 06:58:28 +00004713 skb_reserve(copy_skb, TG3_RAW_IP_ALIGN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714 skb_put(copy_skb, len);
4715 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03004716 skb_copy_from_linear_data(skb, copy_skb->data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
4718
4719 /* We'll reuse the original ring buffer. */
4720 skb = copy_skb;
4721 }
4722
4723 if ((tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) &&
4724 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
4725 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
4726 >> RXD_TCPCSUM_SHIFT) == 0xffff))
4727 skb->ip_summed = CHECKSUM_UNNECESSARY;
4728 else
4729 skb->ip_summed = CHECKSUM_NONE;
4730
4731 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00004732
4733 if (len > (tp->dev->mtu + ETH_HLEN) &&
4734 skb->protocol != htons(ETH_P_8021Q)) {
4735 dev_kfree_skb(skb);
4736 goto next_pkt;
4737 }
4738
Matt Carlson9dc7a112010-04-12 06:58:28 +00004739 if (desc->type_flags & RXD_FLAG_VLAN &&
4740 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG)) {
4741 vtag = desc->err_vlan & RXD_VLAN_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742#if TG3_VLAN_TAG_USED
Matt Carlson9dc7a112010-04-12 06:58:28 +00004743 if (tp->vlgrp)
4744 hw_vlan = true;
4745 else
4746#endif
4747 {
4748 struct vlan_ethhdr *ve = (struct vlan_ethhdr *)
4749 __skb_push(skb, VLAN_HLEN);
4750
4751 memmove(ve, skb->data + VLAN_HLEN,
4752 ETH_ALEN * 2);
4753 ve->h_vlan_proto = htons(ETH_P_8021Q);
4754 ve->h_vlan_TCI = htons(vtag);
4755 }
4756 }
4757
4758#if TG3_VLAN_TAG_USED
4759 if (hw_vlan)
4760 vlan_gro_receive(&tnapi->napi, tp->vlgrp, vtag, skb);
4761 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762#endif
Matt Carlson17375d22009-08-28 14:02:18 +00004763 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765 received++;
4766 budget--;
4767
4768next_pkt:
4769 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07004770
4771 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson86cfe4f2010-01-12 10:11:37 +00004772 tpr->rx_std_prod_idx = std_prod_idx % TG3_RX_RING_SIZE;
4773 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
4774 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07004775 work_mask &= ~RXD_OPAQUE_RING_STD;
4776 rx_std_posted = 0;
4777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07004779 sw_idx++;
Eric Dumazet6b31a512007-02-06 13:29:21 -08004780 sw_idx &= (TG3_RX_RCB_RING_SIZE(tp) - 1);
Michael Chan52f6d692005-04-25 15:14:32 -07004781
4782 /* Refresh hw_idx to see if there is new work */
4783 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004784 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07004785 rmb();
4786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787 }
4788
4789 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00004790 tnapi->rx_rcb_ptr = sw_idx;
4791 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792
4793 /* Refill RX ring(s). */
Matt Carlsone4af1af2010-02-12 14:47:05 +00004794 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004795 if (work_mask & RXD_OPAQUE_RING_STD) {
4796 tpr->rx_std_prod_idx = std_prod_idx % TG3_RX_RING_SIZE;
4797 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
4798 tpr->rx_std_prod_idx);
4799 }
4800 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
4801 tpr->rx_jmb_prod_idx = jmb_prod_idx %
4802 TG3_RX_JUMBO_RING_SIZE;
4803 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
4804 tpr->rx_jmb_prod_idx);
4805 }
4806 mmiowb();
4807 } else if (work_mask) {
4808 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
4809 * updated before the producer indices can be updated.
4810 */
4811 smp_wmb();
4812
Matt Carlson43619352009-11-13 13:03:47 +00004813 tpr->rx_std_prod_idx = std_prod_idx % TG3_RX_RING_SIZE;
Matt Carlson43619352009-11-13 13:03:47 +00004814 tpr->rx_jmb_prod_idx = jmb_prod_idx % TG3_RX_JUMBO_RING_SIZE;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004815
Matt Carlsone4af1af2010-02-12 14:47:05 +00004816 if (tnapi != &tp->napi[1])
4817 napi_schedule(&tp->napi[1].napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819
4820 return received;
4821}
4822
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004823static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825 /* handle link change and other phy events */
4826 if (!(tp->tg3_flags &
4827 (TG3_FLAG_USE_LINKCHG_REG |
4828 TG3_FLAG_POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004829 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
4830
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 if (sblk->status & SD_STATUS_LINK_CHG) {
4832 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004833 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07004834 spin_lock(&tp->lock);
Matt Carlsondd477002008-05-25 23:45:58 -07004835 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
4836 tw32_f(MAC_STATUS,
4837 (MAC_STATUS_SYNC_CHANGED |
4838 MAC_STATUS_CFG_CHANGED |
4839 MAC_STATUS_MI_COMPLETION |
4840 MAC_STATUS_LNKSTATE_CHANGED));
4841 udelay(40);
4842 } else
4843 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07004844 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845 }
4846 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004847}
4848
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004849static int tg3_rx_prodring_xfer(struct tg3 *tp,
4850 struct tg3_rx_prodring_set *dpr,
4851 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004852{
4853 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004854 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004855
4856 while (1) {
4857 src_prod_idx = spr->rx_std_prod_idx;
4858
4859 /* Make sure updates to the rx_std_buffers[] entries and the
4860 * standard producer index are seen in the correct order.
4861 */
4862 smp_rmb();
4863
4864 if (spr->rx_std_cons_idx == src_prod_idx)
4865 break;
4866
4867 if (spr->rx_std_cons_idx < src_prod_idx)
4868 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
4869 else
4870 cpycnt = TG3_RX_RING_SIZE - spr->rx_std_cons_idx;
4871
4872 cpycnt = min(cpycnt, TG3_RX_RING_SIZE - dpr->rx_std_prod_idx);
4873
4874 si = spr->rx_std_cons_idx;
4875 di = dpr->rx_std_prod_idx;
4876
Matt Carlsone92967b2010-02-12 14:47:06 +00004877 for (i = di; i < di + cpycnt; i++) {
4878 if (dpr->rx_std_buffers[i].skb) {
4879 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004880 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00004881 break;
4882 }
4883 }
4884
4885 if (!cpycnt)
4886 break;
4887
4888 /* Ensure that updates to the rx_std_buffers ring and the
4889 * shadowed hardware producer ring from tg3_recycle_skb() are
4890 * ordered correctly WRT the skb check above.
4891 */
4892 smp_rmb();
4893
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004894 memcpy(&dpr->rx_std_buffers[di],
4895 &spr->rx_std_buffers[si],
4896 cpycnt * sizeof(struct ring_info));
4897
4898 for (i = 0; i < cpycnt; i++, di++, si++) {
4899 struct tg3_rx_buffer_desc *sbd, *dbd;
4900 sbd = &spr->rx_std[si];
4901 dbd = &dpr->rx_std[di];
4902 dbd->addr_hi = sbd->addr_hi;
4903 dbd->addr_lo = sbd->addr_lo;
4904 }
4905
4906 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) %
4907 TG3_RX_RING_SIZE;
4908 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) %
4909 TG3_RX_RING_SIZE;
4910 }
4911
4912 while (1) {
4913 src_prod_idx = spr->rx_jmb_prod_idx;
4914
4915 /* Make sure updates to the rx_jmb_buffers[] entries and
4916 * the jumbo producer index are seen in the correct order.
4917 */
4918 smp_rmb();
4919
4920 if (spr->rx_jmb_cons_idx == src_prod_idx)
4921 break;
4922
4923 if (spr->rx_jmb_cons_idx < src_prod_idx)
4924 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
4925 else
4926 cpycnt = TG3_RX_JUMBO_RING_SIZE - spr->rx_jmb_cons_idx;
4927
4928 cpycnt = min(cpycnt,
4929 TG3_RX_JUMBO_RING_SIZE - dpr->rx_jmb_prod_idx);
4930
4931 si = spr->rx_jmb_cons_idx;
4932 di = dpr->rx_jmb_prod_idx;
4933
Matt Carlsone92967b2010-02-12 14:47:06 +00004934 for (i = di; i < di + cpycnt; i++) {
4935 if (dpr->rx_jmb_buffers[i].skb) {
4936 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004937 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00004938 break;
4939 }
4940 }
4941
4942 if (!cpycnt)
4943 break;
4944
4945 /* Ensure that updates to the rx_jmb_buffers ring and the
4946 * shadowed hardware producer ring from tg3_recycle_skb() are
4947 * ordered correctly WRT the skb check above.
4948 */
4949 smp_rmb();
4950
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004951 memcpy(&dpr->rx_jmb_buffers[di],
4952 &spr->rx_jmb_buffers[si],
4953 cpycnt * sizeof(struct ring_info));
4954
4955 for (i = 0; i < cpycnt; i++, di++, si++) {
4956 struct tg3_rx_buffer_desc *sbd, *dbd;
4957 sbd = &spr->rx_jmb[si].std;
4958 dbd = &dpr->rx_jmb[di].std;
4959 dbd->addr_hi = sbd->addr_hi;
4960 dbd->addr_lo = sbd->addr_lo;
4961 }
4962
4963 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) %
4964 TG3_RX_JUMBO_RING_SIZE;
4965 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) %
4966 TG3_RX_JUMBO_RING_SIZE;
4967 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004968
4969 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004970}
4971
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004972static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
4973{
4974 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975
4976 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004977 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00004978 tg3_tx(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07004979 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
Michael Chan4fd7ab52007-10-12 01:39:50 -07004980 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 }
4982
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983 /* run RX thread, within the bounds set by NAPI.
4984 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004985 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004987 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00004988 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004990 if ((tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00004991 struct tg3_rx_prodring_set *dpr = &tp->prodring[0];
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004992 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00004993 u32 std_prod_idx = dpr->rx_std_prod_idx;
4994 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004995
Matt Carlsone4af1af2010-02-12 14:47:05 +00004996 for (i = 1; i < tp->irq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004997 err |= tg3_rx_prodring_xfer(tp, dpr,
4998 tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004999
5000 wmb();
5001
Matt Carlsone4af1af2010-02-12 14:47:05 +00005002 if (std_prod_idx != dpr->rx_std_prod_idx)
5003 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5004 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005005
Matt Carlsone4af1af2010-02-12 14:47:05 +00005006 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
5007 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5008 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005009
5010 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005011
5012 if (err)
5013 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005014 }
5015
David S. Miller6f535762007-10-11 18:08:29 -07005016 return work_done;
5017}
David S. Millerf7383c22005-05-18 22:50:53 -07005018
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005019static int tg3_poll_msix(struct napi_struct *napi, int budget)
5020{
5021 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5022 struct tg3 *tp = tnapi->tp;
5023 int work_done = 0;
5024 struct tg3_hw_status *sblk = tnapi->hw_status;
5025
5026 while (1) {
5027 work_done = tg3_poll_work(tnapi, work_done, budget);
5028
5029 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
5030 goto tx_recovery;
5031
5032 if (unlikely(work_done >= budget))
5033 break;
5034
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005035 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005036 * to tell the hw how much work has been processed,
5037 * so we must read it before checking for more work.
5038 */
5039 tnapi->last_tag = sblk->status_tag;
5040 tnapi->last_irq_tag = tnapi->last_tag;
5041 rmb();
5042
5043 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00005044 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
5045 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005046 napi_complete(napi);
5047 /* Reenable interrupts. */
5048 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
5049 mmiowb();
5050 break;
5051 }
5052 }
5053
5054 return work_done;
5055
5056tx_recovery:
5057 /* work_done is guaranteed to be less than budget. */
5058 napi_complete(napi);
5059 schedule_work(&tp->reset_task);
5060 return work_done;
5061}
5062
David S. Miller6f535762007-10-11 18:08:29 -07005063static int tg3_poll(struct napi_struct *napi, int budget)
5064{
Matt Carlson8ef04422009-08-28 14:01:37 +00005065 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5066 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07005067 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00005068 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07005069
5070 while (1) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005071 tg3_poll_link(tp);
5072
Matt Carlson17375d22009-08-28 14:02:18 +00005073 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07005074
5075 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
5076 goto tx_recovery;
5077
5078 if (unlikely(work_done >= budget))
5079 break;
5080
Michael Chan4fd7ab52007-10-12 01:39:50 -07005081 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) {
Matt Carlson17375d22009-08-28 14:02:18 +00005082 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07005083 * to tell the hw how much work has been processed,
5084 * so we must read it before checking for more work.
5085 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005086 tnapi->last_tag = sblk->status_tag;
5087 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07005088 rmb();
5089 } else
5090 sblk->status &= ~SD_STATUS_UPDATED;
5091
Matt Carlson17375d22009-08-28 14:02:18 +00005092 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005093 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00005094 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07005095 break;
5096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097 }
5098
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005099 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07005100
5101tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07005102 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08005103 napi_complete(napi);
David S. Miller6f535762007-10-11 18:08:29 -07005104 schedule_work(&tp->reset_task);
Michael Chan4fd7ab52007-10-12 01:39:50 -07005105 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106}
5107
David S. Millerf47c11e2005-06-24 20:18:35 -07005108static void tg3_irq_quiesce(struct tg3 *tp)
5109{
Matt Carlson4f125f42009-09-01 12:55:02 +00005110 int i;
5111
David S. Millerf47c11e2005-06-24 20:18:35 -07005112 BUG_ON(tp->irq_sync);
5113
5114 tp->irq_sync = 1;
5115 smp_mb();
5116
Matt Carlson4f125f42009-09-01 12:55:02 +00005117 for (i = 0; i < tp->irq_cnt; i++)
5118 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07005119}
5120
5121static inline int tg3_irq_sync(struct tg3 *tp)
5122{
5123 return tp->irq_sync;
5124}
5125
5126/* Fully shutdown all tg3 driver activity elsewhere in the system.
5127 * If irq_sync is non-zero, then the IRQ handler must be synchronized
5128 * with as well. Most of the time, this is not necessary except when
5129 * shutting down the device.
5130 */
5131static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
5132{
Michael Chan46966542007-07-11 19:47:19 -07005133 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07005134 if (irq_sync)
5135 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07005136}
5137
5138static inline void tg3_full_unlock(struct tg3 *tp)
5139{
David S. Millerf47c11e2005-06-24 20:18:35 -07005140 spin_unlock_bh(&tp->lock);
5141}
5142
Michael Chanfcfa0a32006-03-20 22:28:41 -08005143/* One-shot MSI handler - Chip automatically disables interrupt
5144 * after sending MSI so driver doesn't have to do it.
5145 */
David Howells7d12e782006-10-05 14:55:46 +01005146static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08005147{
Matt Carlson09943a12009-08-28 14:01:57 +00005148 struct tg3_napi *tnapi = dev_id;
5149 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08005150
Matt Carlson898a56f2009-08-28 14:02:40 +00005151 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005152 if (tnapi->rx_rcb)
5153 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005154
5155 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005156 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005157
5158 return IRQ_HANDLED;
5159}
5160
Michael Chan88b06bc22005-04-21 17:13:25 -07005161/* MSI ISR - No need to check for interrupt sharing and no need to
5162 * flush status block and interrupt mailbox. PCI ordering rules
5163 * guarantee that MSI will arrive after the status block.
5164 */
David Howells7d12e782006-10-05 14:55:46 +01005165static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07005166{
Matt Carlson09943a12009-08-28 14:01:57 +00005167 struct tg3_napi *tnapi = dev_id;
5168 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07005169
Matt Carlson898a56f2009-08-28 14:02:40 +00005170 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005171 if (tnapi->rx_rcb)
5172 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07005173 /*
David S. Millerfac9b832005-05-18 22:46:34 -07005174 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07005175 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07005176 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07005177 * NIC to stop sending us irqs, engaging "in-intr-handler"
5178 * event coalescing.
5179 */
5180 tw32_mailbox(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07005181 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005182 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07005183
Michael Chan88b06bc22005-04-21 17:13:25 -07005184 return IRQ_RETVAL(1);
5185}
5186
David Howells7d12e782006-10-05 14:55:46 +01005187static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188{
Matt Carlson09943a12009-08-28 14:01:57 +00005189 struct tg3_napi *tnapi = dev_id;
5190 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005191 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192 unsigned int handled = 1;
5193
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194 /* In INTx mode, it is possible for the interrupt to arrive at
5195 * the CPU before the status block posted prior to the interrupt.
5196 * Reading the PCI State register will confirm whether the
5197 * interrupt is ours and will flush the status block.
5198 */
Michael Chand18edcb2007-03-24 20:57:11 -07005199 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
5200 if ((tp->tg3_flags & TG3_FLAG_CHIP_RESETTING) ||
5201 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5202 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005203 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07005204 }
Michael Chand18edcb2007-03-24 20:57:11 -07005205 }
5206
5207 /*
5208 * Writing any value to intr-mbox-0 clears PCI INTA# and
5209 * chip-internal interrupt pending events.
5210 * Writing non-zero to intr-mbox-0 additional tells the
5211 * NIC to stop sending us irqs, engaging "in-intr-handler"
5212 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005213 *
5214 * Flush the mailbox to de-assert the IRQ immediately to prevent
5215 * spurious interrupts. The flush impacts performance but
5216 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005217 */
Michael Chanc04cb342007-05-07 00:26:15 -07005218 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07005219 if (tg3_irq_sync(tp))
5220 goto out;
5221 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00005222 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00005223 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00005224 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07005225 } else {
5226 /* No work, shared interrupt perhaps? re-enable
5227 * interrupts, and flush that PCI write
5228 */
5229 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
5230 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07005231 }
David S. Millerf47c11e2005-06-24 20:18:35 -07005232out:
David S. Millerfac9b832005-05-18 22:46:34 -07005233 return IRQ_RETVAL(handled);
5234}
5235
David Howells7d12e782006-10-05 14:55:46 +01005236static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -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;
David S. Millerfac9b832005-05-18 22:46:34 -07005241 unsigned int handled = 1;
5242
David S. Millerfac9b832005-05-18 22:46:34 -07005243 /* In INTx mode, it is possible for the interrupt to arrive at
5244 * the CPU before the status block posted prior to the interrupt.
5245 * Reading the PCI State register will confirm whether the
5246 * interrupt is ours and will flush the status block.
5247 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005248 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Michael Chand18edcb2007-03-24 20:57:11 -07005249 if ((tp->tg3_flags & TG3_FLAG_CHIP_RESETTING) ||
5250 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5251 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005252 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253 }
Michael Chand18edcb2007-03-24 20:57:11 -07005254 }
5255
5256 /*
5257 * writing any value to intr-mbox-0 clears PCI INTA# and
5258 * chip-internal interrupt pending events.
5259 * writing non-zero to intr-mbox-0 additional tells the
5260 * NIC to stop sending us irqs, engaging "in-intr-handler"
5261 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005262 *
5263 * Flush the mailbox to de-assert the IRQ immediately to prevent
5264 * spurious interrupts. The flush impacts performance but
5265 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005266 */
Michael Chanc04cb342007-05-07 00:26:15 -07005267 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00005268
5269 /*
5270 * In a shared interrupt configuration, sometimes other devices'
5271 * interrupts will scream. We record the current status tag here
5272 * so that the above check can report that the screaming interrupts
5273 * are unhandled. Eventually they will be silenced.
5274 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005275 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00005276
Michael Chand18edcb2007-03-24 20:57:11 -07005277 if (tg3_irq_sync(tp))
5278 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00005279
Matt Carlson72334482009-08-28 14:03:01 +00005280 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00005281
Matt Carlson09943a12009-08-28 14:01:57 +00005282 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00005283
David S. Millerf47c11e2005-06-24 20:18:35 -07005284out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285 return IRQ_RETVAL(handled);
5286}
5287
Michael Chan79381092005-04-21 17:13:59 -07005288/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01005289static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07005290{
Matt Carlson09943a12009-08-28 14:01:57 +00005291 struct tg3_napi *tnapi = dev_id;
5292 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005293 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07005294
Michael Chanf9804dd2005-09-27 12:13:10 -07005295 if ((sblk->status & SD_STATUS_UPDATED) ||
5296 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07005297 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07005298 return IRQ_RETVAL(1);
5299 }
5300 return IRQ_RETVAL(0);
5301}
5302
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07005303static int tg3_init_hw(struct tg3 *, int);
Michael Chan944d9802005-05-29 14:57:48 -07005304static int tg3_halt(struct tg3 *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305
Michael Chanb9ec6c12006-07-25 16:37:27 -07005306/* Restart hardware after configuration changes, self-test, etc.
5307 * Invoked with tp->lock held.
5308 */
5309static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
Eric Dumazet78c61462008-04-24 23:33:06 -07005310 __releases(tp->lock)
5311 __acquires(tp->lock)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005312{
5313 int err;
5314
5315 err = tg3_init_hw(tp, reset_phy);
5316 if (err) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00005317 netdev_err(tp->dev,
5318 "Failed to re-initialize device, aborting\n");
Michael Chanb9ec6c12006-07-25 16:37:27 -07005319 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
5320 tg3_full_unlock(tp);
5321 del_timer_sync(&tp->timer);
5322 tp->irq_sync = 0;
Matt Carlsonfed97812009-09-01 13:10:19 +00005323 tg3_napi_enable(tp);
Michael Chanb9ec6c12006-07-25 16:37:27 -07005324 dev_close(tp->dev);
5325 tg3_full_lock(tp, 0);
5326 }
5327 return err;
5328}
5329
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330#ifdef CONFIG_NET_POLL_CONTROLLER
5331static void tg3_poll_controller(struct net_device *dev)
5332{
Matt Carlson4f125f42009-09-01 12:55:02 +00005333 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07005334 struct tg3 *tp = netdev_priv(dev);
5335
Matt Carlson4f125f42009-09-01 12:55:02 +00005336 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00005337 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338}
5339#endif
5340
David Howellsc4028952006-11-22 14:57:56 +00005341static void tg3_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342{
David Howellsc4028952006-11-22 14:57:56 +00005343 struct tg3 *tp = container_of(work, struct tg3, reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005344 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345 unsigned int restart_timer;
5346
Michael Chan7faa0062006-02-02 17:29:28 -08005347 tg3_full_lock(tp, 0);
Michael Chan7faa0062006-02-02 17:29:28 -08005348
5349 if (!netif_running(tp->dev)) {
Michael Chan7faa0062006-02-02 17:29:28 -08005350 tg3_full_unlock(tp);
5351 return;
5352 }
5353
5354 tg3_full_unlock(tp);
5355
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005356 tg3_phy_stop(tp);
5357
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358 tg3_netif_stop(tp);
5359
David S. Millerf47c11e2005-06-24 20:18:35 -07005360 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361
5362 restart_timer = tp->tg3_flags2 & TG3_FLG2_RESTART_TIMER;
5363 tp->tg3_flags2 &= ~TG3_FLG2_RESTART_TIMER;
5364
Michael Chandf3e6542006-05-26 17:48:07 -07005365 if (tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING) {
5366 tp->write32_tx_mbox = tg3_write32_tx_mbox;
5367 tp->write32_rx_mbox = tg3_write_flush_reg32;
5368 tp->tg3_flags |= TG3_FLAG_MBOX_WRITE_REORDER;
5369 tp->tg3_flags &= ~TG3_FLAG_TX_RECOVERY_PENDING;
5370 }
5371
Michael Chan944d9802005-05-29 14:57:48 -07005372 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005373 err = tg3_init_hw(tp, 1);
5374 if (err)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005375 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376
5377 tg3_netif_start(tp);
5378
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379 if (restart_timer)
5380 mod_timer(&tp->timer, jiffies + 1);
Michael Chan7faa0062006-02-02 17:29:28 -08005381
Michael Chanb9ec6c12006-07-25 16:37:27 -07005382out:
Michael Chan7faa0062006-02-02 17:29:28 -08005383 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005384
5385 if (!err)
5386 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387}
5388
Michael Chanb0408752007-02-13 12:18:30 -08005389static void tg3_dump_short_state(struct tg3 *tp)
5390{
Joe Perches05dbe002010-02-17 19:44:19 +00005391 netdev_err(tp->dev, "DEBUG: MAC_TX_STATUS[%08x] MAC_RX_STATUS[%08x]\n",
5392 tr32(MAC_TX_STATUS), tr32(MAC_RX_STATUS));
5393 netdev_err(tp->dev, "DEBUG: RDMAC_STATUS[%08x] WDMAC_STATUS[%08x]\n",
5394 tr32(RDMAC_STATUS), tr32(WDMAC_STATUS));
Michael Chanb0408752007-02-13 12:18:30 -08005395}
5396
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397static void tg3_tx_timeout(struct net_device *dev)
5398{
5399 struct tg3 *tp = netdev_priv(dev);
5400
Michael Chanb0408752007-02-13 12:18:30 -08005401 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00005402 netdev_err(dev, "transmit timed out, resetting\n");
Michael Chanb0408752007-02-13 12:18:30 -08005403 tg3_dump_short_state(tp);
5404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405
5406 schedule_work(&tp->reset_task);
5407}
5408
Michael Chanc58ec932005-09-17 00:46:27 -07005409/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
5410static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
5411{
5412 u32 base = (u32) mapping & 0xffffffff;
5413
5414 return ((base > 0xffffdcc0) &&
5415 (base + len + 8 < base));
5416}
5417
Michael Chan72f2afb2006-03-06 19:28:35 -08005418/* Test for DMA addresses > 40-bit */
5419static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
5420 int len)
5421{
5422#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Michael Chan6728a8e2006-03-27 23:16:49 -08005423 if (tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG)
Yang Hongyang50cf1562009-04-06 19:01:14 -07005424 return (((u64) mapping + len) > DMA_BIT_MASK(40));
Michael Chan72f2afb2006-03-06 19:28:35 -08005425 return 0;
5426#else
5427 return 0;
5428#endif
5429}
5430
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005431static void tg3_set_txd(struct tg3_napi *, int, dma_addr_t, int, u32, u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432
Michael Chan72f2afb2006-03-06 19:28:35 -08005433/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00005434static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
5435 struct sk_buff *skb, u32 last_plus_one,
5436 u32 *start, u32 base_flags, u32 mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437{
Matt Carlson24f4efd2009-11-13 13:03:35 +00005438 struct tg3 *tp = tnapi->tp;
Matt Carlson41588ba2008-04-19 18:12:33 -07005439 struct sk_buff *new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07005440 dma_addr_t new_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441 u32 entry = *start;
Michael Chanc58ec932005-09-17 00:46:27 -07005442 int i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443
Matt Carlson41588ba2008-04-19 18:12:33 -07005444 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
5445 new_skb = skb_copy(skb, GFP_ATOMIC);
5446 else {
5447 int more_headroom = 4 - ((unsigned long)skb->data & 3);
5448
5449 new_skb = skb_copy_expand(skb,
5450 skb_headroom(skb) + more_headroom,
5451 skb_tailroom(skb), GFP_ATOMIC);
5452 }
5453
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07005455 ret = -1;
5456 } else {
5457 /* New SKB is guaranteed to be linear. */
5458 entry = *start;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005459 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
5460 PCI_DMA_TODEVICE);
5461 /* Make sure the mapping succeeded */
5462 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
5463 ret = -1;
5464 dev_kfree_skb(new_skb);
5465 new_skb = NULL;
David S. Miller90079ce2008-09-11 04:52:51 -07005466
Michael Chanc58ec932005-09-17 00:46:27 -07005467 /* Make sure new skb does not cross any 4G boundaries.
5468 * Drop the packet if it does.
5469 */
Alexander Duyckf4188d82009-12-02 16:48:38 +00005470 } else if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
5471 tg3_4g_overflow_test(new_addr, new_skb->len)) {
5472 pci_unmap_single(tp->pdev, new_addr, new_skb->len,
5473 PCI_DMA_TODEVICE);
Michael Chanc58ec932005-09-17 00:46:27 -07005474 ret = -1;
5475 dev_kfree_skb(new_skb);
5476 new_skb = NULL;
5477 } else {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005478 tg3_set_txd(tnapi, entry, new_addr, new_skb->len,
Michael Chanc58ec932005-09-17 00:46:27 -07005479 base_flags, 1 | (mss << 1));
5480 *start = NEXT_TX(entry);
5481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482 }
5483
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484 /* Now clean up the sw ring entries. */
5485 i = 0;
5486 while (entry != last_plus_one) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00005487 int len;
5488
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005489 if (i == 0)
Alexander Duyckf4188d82009-12-02 16:48:38 +00005490 len = skb_headlen(skb);
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005491 else
Alexander Duyckf4188d82009-12-02 16:48:38 +00005492 len = skb_shinfo(skb)->frags[i-1].size;
5493
5494 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005495 dma_unmap_addr(&tnapi->tx_buffers[entry],
Alexander Duyckf4188d82009-12-02 16:48:38 +00005496 mapping),
5497 len, PCI_DMA_TODEVICE);
5498 if (i == 0) {
5499 tnapi->tx_buffers[entry].skb = new_skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005500 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00005501 new_addr);
5502 } else {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005503 tnapi->tx_buffers[entry].skb = NULL;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505 entry = NEXT_TX(entry);
5506 i++;
5507 }
5508
5509 dev_kfree_skb(skb);
5510
Michael Chanc58ec932005-09-17 00:46:27 -07005511 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512}
5513
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005514static void tg3_set_txd(struct tg3_napi *tnapi, int entry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515 dma_addr_t mapping, int len, u32 flags,
5516 u32 mss_and_is_end)
5517{
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005518 struct tg3_tx_buffer_desc *txd = &tnapi->tx_ring[entry];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519 int is_end = (mss_and_is_end & 0x1);
5520 u32 mss = (mss_and_is_end >> 1);
5521 u32 vlan_tag = 0;
5522
5523 if (is_end)
5524 flags |= TXD_FLAG_END;
5525 if (flags & TXD_FLAG_VLAN) {
5526 vlan_tag = flags >> 16;
5527 flags &= 0xffff;
5528 }
5529 vlan_tag |= (mss << TXD_MSS_SHIFT);
5530
5531 txd->addr_hi = ((u64) mapping >> 32);
5532 txd->addr_lo = ((u64) mapping & 0xffffffff);
5533 txd->len_flags = (len << TXD_LEN_SHIFT) | flags;
5534 txd->vlan_tag = vlan_tag << TXD_VLAN_TAG_SHIFT;
5535}
5536
Michael Chan5a6f3072006-03-20 22:28:05 -08005537/* hard_start_xmit for devices that don't have any bugs and
Matt Carlsone849cdc2009-11-13 13:03:38 +00005538 * support TG3_FLG2_HW_TSO_2 and TG3_FLG2_HW_TSO_3 only.
Michael Chan5a6f3072006-03-20 22:28:05 -08005539 */
Stephen Hemminger613573252009-08-31 19:50:58 +00005540static netdev_tx_t tg3_start_xmit(struct sk_buff *skb,
5541 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542{
5543 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544 u32 len, entry, base_flags, mss;
David S. Miller90079ce2008-09-11 04:52:51 -07005545 dma_addr_t mapping;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005546 struct tg3_napi *tnapi;
5547 struct netdev_queue *txq;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005548 unsigned int i, last;
5549
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005550 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
5551 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Matt Carlson19cfaec2009-12-03 08:36:20 +00005552 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005553 tnapi++;
Michael Chan5a6f3072006-03-20 22:28:05 -08005554
Michael Chan00b70502006-06-17 21:58:45 -07005555 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005556 * and TX reclaim runs via tp->napi.poll inside of a software
Michael Chan5a6f3072006-03-20 22:28:05 -08005557 * interrupt. Furthermore, IRQ processing runs lockless so we have
5558 * no IRQ context deadlocks to worry about either. Rejoice!
5559 */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005560 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005561 if (!netif_tx_queue_stopped(txq)) {
5562 netif_tx_stop_queue(txq);
Michael Chan5a6f3072006-03-20 22:28:05 -08005563
5564 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00005565 netdev_err(dev,
5566 "BUG! Tx Ring full when queue awake!\n");
Michael Chan5a6f3072006-03-20 22:28:05 -08005567 }
Michael Chan5a6f3072006-03-20 22:28:05 -08005568 return NETDEV_TX_BUSY;
5569 }
5570
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005571 entry = tnapi->tx_prod;
Michael Chan5a6f3072006-03-20 22:28:05 -08005572 base_flags = 0;
Michael Chan5a6f3072006-03-20 22:28:05 -08005573 mss = 0;
Matt Carlsonc13e3712007-05-05 11:50:04 -07005574 if ((mss = skb_shinfo(skb)->gso_size) != 0) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005575 int tcp_opt_len, ip_tcp_len;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005576 u32 hdrlen;
Michael Chan5a6f3072006-03-20 22:28:05 -08005577
5578 if (skb_header_cloned(skb) &&
5579 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
5580 dev_kfree_skb(skb);
5581 goto out_unlock;
5582 }
5583
Michael Chanb0026622006-07-03 19:42:14 -07005584 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005585 hdrlen = skb_headlen(skb) - ETH_HLEN;
Michael Chanb0026622006-07-03 19:42:14 -07005586 else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005587 struct iphdr *iph = ip_hdr(skb);
5588
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07005589 tcp_opt_len = tcp_optlen(skb);
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -03005590 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
Michael Chanb0026622006-07-03 19:42:14 -07005591
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005592 iph->check = 0;
5593 iph->tot_len = htons(mss + ip_tcp_len + tcp_opt_len);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005594 hdrlen = ip_tcp_len + tcp_opt_len;
Michael Chanb0026622006-07-03 19:42:14 -07005595 }
Michael Chan5a6f3072006-03-20 22:28:05 -08005596
Matt Carlsone849cdc2009-11-13 13:03:38 +00005597 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005598 mss |= (hdrlen & 0xc) << 12;
5599 if (hdrlen & 0x10)
5600 base_flags |= 0x00000010;
5601 base_flags |= (hdrlen & 0x3e0) << 5;
5602 } else
5603 mss |= hdrlen << 9;
5604
Michael Chan5a6f3072006-03-20 22:28:05 -08005605 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
5606 TXD_FLAG_CPU_POST_DMA);
5607
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005608 tcp_hdr(skb)->check = 0;
Michael Chan5a6f3072006-03-20 22:28:05 -08005609
Matt Carlson859a588792010-04-05 10:19:28 +00005610 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005611 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson859a588792010-04-05 10:19:28 +00005612 }
5613
Michael Chan5a6f3072006-03-20 22:28:05 -08005614#if TG3_VLAN_TAG_USED
5615 if (tp->vlgrp != NULL && vlan_tx_tag_present(skb))
5616 base_flags |= (TXD_FLAG_VLAN |
5617 (vlan_tx_tag_get(skb) << 16));
5618#endif
5619
Alexander Duyckf4188d82009-12-02 16:48:38 +00005620 len = skb_headlen(skb);
5621
5622 /* Queue skb data, a.k.a. the main skb fragment. */
5623 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
5624 if (pci_dma_mapping_error(tp->pdev, mapping)) {
David S. Miller90079ce2008-09-11 04:52:51 -07005625 dev_kfree_skb(skb);
5626 goto out_unlock;
5627 }
5628
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005629 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005630 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005631
Matt Carlsonb703df62009-12-03 08:36:21 +00005632 if ((tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005633 !mss && skb->len > ETH_DATA_LEN)
5634 base_flags |= TXD_FLAG_JMB_PKT;
5635
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005636 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
Michael Chan5a6f3072006-03-20 22:28:05 -08005637 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
5638
5639 entry = NEXT_TX(entry);
5640
5641 /* Now loop through additional data fragments, and queue them. */
5642 if (skb_shinfo(skb)->nr_frags > 0) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005643 last = skb_shinfo(skb)->nr_frags - 1;
5644 for (i = 0; i <= last; i++) {
5645 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5646
5647 len = frag->size;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005648 mapping = pci_map_page(tp->pdev,
5649 frag->page,
5650 frag->page_offset,
5651 len, PCI_DMA_TODEVICE);
5652 if (pci_dma_mapping_error(tp->pdev, mapping))
5653 goto dma_error;
5654
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005655 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005656 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00005657 mapping);
Michael Chan5a6f3072006-03-20 22:28:05 -08005658
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005659 tg3_set_txd(tnapi, entry, mapping, len,
Michael Chan5a6f3072006-03-20 22:28:05 -08005660 base_flags, (i == last) | (mss << 1));
5661
5662 entry = NEXT_TX(entry);
5663 }
5664 }
5665
5666 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005667 tw32_tx_mbox(tnapi->prodmbox, entry);
Michael Chan5a6f3072006-03-20 22:28:05 -08005668
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005669 tnapi->tx_prod = entry;
5670 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005671 netif_tx_stop_queue(txq);
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005672 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005673 netif_tx_wake_queue(txq);
Michael Chan5a6f3072006-03-20 22:28:05 -08005674 }
5675
5676out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00005677 mmiowb();
Michael Chan5a6f3072006-03-20 22:28:05 -08005678
5679 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005680
5681dma_error:
5682 last = i;
5683 entry = tnapi->tx_prod;
5684 tnapi->tx_buffers[entry].skb = NULL;
5685 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005686 dma_unmap_addr(&tnapi->tx_buffers[entry], mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005687 skb_headlen(skb),
5688 PCI_DMA_TODEVICE);
5689 for (i = 0; i <= last; i++) {
5690 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5691 entry = NEXT_TX(entry);
5692
5693 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005694 dma_unmap_addr(&tnapi->tx_buffers[entry],
Alexander Duyckf4188d82009-12-02 16:48:38 +00005695 mapping),
5696 frag->size, PCI_DMA_TODEVICE);
5697 }
5698
5699 dev_kfree_skb(skb);
5700 return NETDEV_TX_OK;
Michael Chan5a6f3072006-03-20 22:28:05 -08005701}
5702
Stephen Hemminger613573252009-08-31 19:50:58 +00005703static netdev_tx_t tg3_start_xmit_dma_bug(struct sk_buff *,
5704 struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07005705
5706/* Use GSO to workaround a rare TSO bug that may be triggered when the
5707 * TSO header is greater than 80 bytes.
5708 */
5709static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
5710{
5711 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005712 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07005713
5714 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005715 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07005716 netif_stop_queue(tp->dev);
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005717 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08005718 return NETDEV_TX_BUSY;
5719
5720 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07005721 }
5722
5723 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07005724 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07005725 goto tg3_tso_bug_end;
5726
5727 do {
5728 nskb = segs;
5729 segs = segs->next;
5730 nskb->next = NULL;
5731 tg3_start_xmit_dma_bug(nskb, tp->dev);
5732 } while (segs);
5733
5734tg3_tso_bug_end:
5735 dev_kfree_skb(skb);
5736
5737 return NETDEV_TX_OK;
5738}
Michael Chan52c0fd82006-06-29 20:15:54 -07005739
Michael Chan5a6f3072006-03-20 22:28:05 -08005740/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
5741 * support TG3_FLG2_HW_TSO_1 or firmware TSO only.
5742 */
Stephen Hemminger613573252009-08-31 19:50:58 +00005743static netdev_tx_t tg3_start_xmit_dma_bug(struct sk_buff *skb,
5744 struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08005745{
5746 struct tg3 *tp = netdev_priv(dev);
Michael Chan5a6f3072006-03-20 22:28:05 -08005747 u32 len, entry, base_flags, mss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748 int would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07005749 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00005750 struct tg3_napi *tnapi;
5751 struct netdev_queue *txq;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005752 unsigned int i, last;
5753
Matt Carlson24f4efd2009-11-13 13:03:35 +00005754 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
5755 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Matt Carlson19cfaec2009-12-03 08:36:20 +00005756 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlson24f4efd2009-11-13 13:03:35 +00005757 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758
Michael Chan00b70502006-06-17 21:58:45 -07005759 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005760 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07005761 * interrupt. Furthermore, IRQ processing runs lockless so we have
5762 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763 */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005764 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00005765 if (!netif_tx_queue_stopped(txq)) {
5766 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08005767
5768 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00005769 netdev_err(dev,
5770 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08005771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005772 return NETDEV_TX_BUSY;
5773 }
5774
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005775 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005776 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07005777 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00005779
Matt Carlsonc13e3712007-05-05 11:50:04 -07005780 if ((mss = skb_shinfo(skb)->gso_size) != 0) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005781 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00005782 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005783
5784 if (skb_header_cloned(skb) &&
5785 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
5786 dev_kfree_skb(skb);
5787 goto out_unlock;
5788 }
5789
Matt Carlson34195c32010-07-11 09:31:42 +00005790 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07005791 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792
Matt Carlson34195c32010-07-11 09:31:42 +00005793 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) {
5794 hdr_len = skb_headlen(skb) - ETH_HLEN;
5795 } else {
5796 u32 ip_tcp_len;
5797
5798 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
5799 hdr_len = ip_tcp_len + tcp_opt_len;
5800
5801 iph->check = 0;
5802 iph->tot_len = htons(mss + hdr_len);
5803 }
5804
Michael Chan52c0fd82006-06-29 20:15:54 -07005805 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Michael Chan7f62ad52007-02-20 23:25:40 -08005806 (tp->tg3_flags2 & TG3_FLG2_TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00005807 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07005808
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
5810 TXD_FLAG_CPU_POST_DMA);
5811
Linus Torvalds1da177e2005-04-16 15:20:36 -07005812 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005813 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005814 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005815 } else
5816 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
5817 iph->daddr, 0,
5818 IPPROTO_TCP,
5819 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820
Matt Carlson615774f2009-11-13 13:03:39 +00005821 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) {
5822 mss |= (hdr_len & 0xc) << 12;
5823 if (hdr_len & 0x10)
5824 base_flags |= 0x00000010;
5825 base_flags |= (hdr_len & 0x3e0) << 5;
5826 } else if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_2)
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005827 mss |= hdr_len << 9;
5828 else if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_1) ||
5829 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005830 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831 int tsflags;
5832
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005833 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005834 mss |= (tsflags << 11);
5835 }
5836 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005837 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005838 int tsflags;
5839
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005840 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005841 base_flags |= tsflags << 12;
5842 }
5843 }
5844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005845#if TG3_VLAN_TAG_USED
5846 if (tp->vlgrp != NULL && vlan_tx_tag_present(skb))
5847 base_flags |= (TXD_FLAG_VLAN |
5848 (vlan_tx_tag_get(skb) << 16));
5849#endif
5850
Matt Carlsonb703df62009-12-03 08:36:21 +00005851 if ((tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) &&
Matt Carlson615774f2009-11-13 13:03:39 +00005852 !mss && skb->len > ETH_DATA_LEN)
5853 base_flags |= TXD_FLAG_JMB_PKT;
5854
Alexander Duyckf4188d82009-12-02 16:48:38 +00005855 len = skb_headlen(skb);
5856
5857 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
5858 if (pci_dma_mapping_error(tp->pdev, mapping)) {
David S. Miller90079ce2008-09-11 04:52:51 -07005859 dev_kfree_skb(skb);
5860 goto out_unlock;
5861 }
5862
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005863 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005864 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005865
5866 would_hit_hwbug = 0;
5867
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005868 if ((tp->tg3_flags3 & TG3_FLG3_SHORT_DMA_BUG) && len <= 8)
5869 would_hit_hwbug = 1;
5870
Matt Carlson0e1406d2009-11-02 12:33:33 +00005871 if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
5872 tg3_4g_overflow_test(mapping, len))
Matt Carlson41588ba2008-04-19 18:12:33 -07005873 would_hit_hwbug = 1;
Matt Carlson0e1406d2009-11-02 12:33:33 +00005874
5875 if ((tp->tg3_flags3 & TG3_FLG3_40BIT_DMA_LIMIT_BUG) &&
5876 tg3_40bit_overflow_test(tp, mapping, len))
5877 would_hit_hwbug = 1;
5878
5879 if (tp->tg3_flags3 & TG3_FLG3_5701_DMA_BUG)
Michael Chanc58ec932005-09-17 00:46:27 -07005880 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005881
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005882 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005883 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
5884
5885 entry = NEXT_TX(entry);
5886
5887 /* Now loop through additional data fragments, and queue them. */
5888 if (skb_shinfo(skb)->nr_frags > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005889 last = skb_shinfo(skb)->nr_frags - 1;
5890 for (i = 0; i <= last; i++) {
5891 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5892
5893 len = frag->size;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005894 mapping = pci_map_page(tp->pdev,
5895 frag->page,
5896 frag->page_offset,
5897 len, PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005899 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005900 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00005901 mapping);
5902 if (pci_dma_mapping_error(tp->pdev, mapping))
5903 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005905 if ((tp->tg3_flags3 & TG3_FLG3_SHORT_DMA_BUG) &&
5906 len <= 8)
5907 would_hit_hwbug = 1;
5908
Matt Carlson0e1406d2009-11-02 12:33:33 +00005909 if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
5910 tg3_4g_overflow_test(mapping, len))
Michael Chanc58ec932005-09-17 00:46:27 -07005911 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005912
Matt Carlson0e1406d2009-11-02 12:33:33 +00005913 if ((tp->tg3_flags3 & TG3_FLG3_40BIT_DMA_LIMIT_BUG) &&
5914 tg3_40bit_overflow_test(tp, mapping, len))
Michael Chan72f2afb2006-03-06 19:28:35 -08005915 would_hit_hwbug = 1;
5916
Linus Torvalds1da177e2005-04-16 15:20:36 -07005917 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005918 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005919 base_flags, (i == last)|(mss << 1));
5920 else
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005921 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922 base_flags, (i == last));
5923
5924 entry = NEXT_TX(entry);
5925 }
5926 }
5927
5928 if (would_hit_hwbug) {
5929 u32 last_plus_one = entry;
5930 u32 start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005931
Michael Chanc58ec932005-09-17 00:46:27 -07005932 start = entry - 1 - skb_shinfo(skb)->nr_frags;
5933 start &= (TG3_TX_RING_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934
5935 /* If the workaround fails due to memory/mapping
5936 * failure, silently drop this packet.
5937 */
Matt Carlson24f4efd2009-11-13 13:03:35 +00005938 if (tigon3_dma_hwbug_workaround(tnapi, skb, last_plus_one,
Michael Chanc58ec932005-09-17 00:46:27 -07005939 &start, base_flags, mss))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005940 goto out_unlock;
5941
5942 entry = start;
5943 }
5944
5945 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00005946 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005947
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005948 tnapi->tx_prod = entry;
5949 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00005950 netif_tx_stop_queue(txq);
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005951 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00005952 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07005953 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005954
5955out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00005956 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005957
5958 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005959
5960dma_error:
5961 last = i;
5962 entry = tnapi->tx_prod;
5963 tnapi->tx_buffers[entry].skb = NULL;
5964 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005965 dma_unmap_addr(&tnapi->tx_buffers[entry], mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005966 skb_headlen(skb),
5967 PCI_DMA_TODEVICE);
5968 for (i = 0; i <= last; i++) {
5969 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5970 entry = NEXT_TX(entry);
5971
5972 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005973 dma_unmap_addr(&tnapi->tx_buffers[entry],
Alexander Duyckf4188d82009-12-02 16:48:38 +00005974 mapping),
5975 frag->size, PCI_DMA_TODEVICE);
5976 }
5977
5978 dev_kfree_skb(skb);
5979 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005980}
5981
5982static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
5983 int new_mtu)
5984{
5985 dev->mtu = new_mtu;
5986
Michael Chanef7f5ec2005-07-25 12:32:25 -07005987 if (new_mtu > ETH_DATA_LEN) {
Michael Chana4e2b342005-10-26 15:46:52 -07005988 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
Michael Chanef7f5ec2005-07-25 12:32:25 -07005989 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
5990 ethtool_op_set_tso(dev, 0);
Matt Carlson859a588792010-04-05 10:19:28 +00005991 } else {
Michael Chanef7f5ec2005-07-25 12:32:25 -07005992 tp->tg3_flags |= TG3_FLAG_JUMBO_RING_ENABLE;
Matt Carlson859a588792010-04-05 10:19:28 +00005993 }
Michael Chanef7f5ec2005-07-25 12:32:25 -07005994 } else {
Michael Chana4e2b342005-10-26 15:46:52 -07005995 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
Michael Chanef7f5ec2005-07-25 12:32:25 -07005996 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
Michael Chan0f893dc2005-07-25 12:30:38 -07005997 tp->tg3_flags &= ~TG3_FLAG_JUMBO_RING_ENABLE;
Michael Chanef7f5ec2005-07-25 12:32:25 -07005998 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999}
6000
6001static int tg3_change_mtu(struct net_device *dev, int new_mtu)
6002{
6003 struct tg3 *tp = netdev_priv(dev);
Michael Chanb9ec6c12006-07-25 16:37:27 -07006004 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006005
6006 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
6007 return -EINVAL;
6008
6009 if (!netif_running(dev)) {
6010 /* We'll just catch it later when the
6011 * device is up'd.
6012 */
6013 tg3_set_mtu(dev, tp, new_mtu);
6014 return 0;
6015 }
6016
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006017 tg3_phy_stop(tp);
6018
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019 tg3_netif_stop(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07006020
6021 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022
Michael Chan944d9802005-05-29 14:57:48 -07006023 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006024
6025 tg3_set_mtu(dev, tp, new_mtu);
6026
Michael Chanb9ec6c12006-07-25 16:37:27 -07006027 err = tg3_restart_hw(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028
Michael Chanb9ec6c12006-07-25 16:37:27 -07006029 if (!err)
6030 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006031
David S. Millerf47c11e2005-06-24 20:18:35 -07006032 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006034 if (!err)
6035 tg3_phy_start(tp);
6036
Michael Chanb9ec6c12006-07-25 16:37:27 -07006037 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006038}
6039
Matt Carlson21f581a2009-08-28 14:00:25 +00006040static void tg3_rx_prodring_free(struct tg3 *tp,
6041 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006042{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006043 int i;
6044
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006045 if (tpr != &tp->prodring[0]) {
6046 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
6047 i = (i + 1) % TG3_RX_RING_SIZE)
6048 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6049 tp->rx_pkt_map_sz);
6050
6051 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
6052 for (i = tpr->rx_jmb_cons_idx;
6053 i != tpr->rx_jmb_prod_idx;
6054 i = (i + 1) % TG3_RX_JUMBO_RING_SIZE) {
6055 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6056 TG3_RX_JMB_MAP_SZ);
6057 }
6058 }
6059
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006060 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006063 for (i = 0; i < TG3_RX_RING_SIZE; i++)
6064 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6065 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006066
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006067 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006068 for (i = 0; i < TG3_RX_JUMBO_RING_SIZE; i++)
6069 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6070 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006071 }
6072}
6073
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006074/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075 *
6076 * The chip has been shut down and the driver detached from
6077 * the networking, so no interrupts or new tx packets will
6078 * end up in the driver. tp->{tx,}lock are held and thus
6079 * we may not sleep.
6080 */
Matt Carlson21f581a2009-08-28 14:00:25 +00006081static int tg3_rx_prodring_alloc(struct tg3 *tp,
6082 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006083{
Matt Carlson287be122009-08-28 13:58:46 +00006084 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006085
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006086 tpr->rx_std_cons_idx = 0;
6087 tpr->rx_std_prod_idx = 0;
6088 tpr->rx_jmb_cons_idx = 0;
6089 tpr->rx_jmb_prod_idx = 0;
6090
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006091 if (tpr != &tp->prodring[0]) {
6092 memset(&tpr->rx_std_buffers[0], 0, TG3_RX_STD_BUFF_RING_SIZE);
6093 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE)
6094 memset(&tpr->rx_jmb_buffers[0], 0,
6095 TG3_RX_JMB_BUFF_RING_SIZE);
6096 goto done;
6097 }
6098
Linus Torvalds1da177e2005-04-16 15:20:36 -07006099 /* Zero out all descriptors. */
Matt Carlson21f581a2009-08-28 14:00:25 +00006100 memset(tpr->rx_std, 0, TG3_RX_RING_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006101
Matt Carlson287be122009-08-28 13:58:46 +00006102 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Michael Chana4e2b342005-10-26 15:46:52 -07006103 if ((tp->tg3_flags2 & TG3_FLG2_5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00006104 tp->dev->mtu > ETH_DATA_LEN)
6105 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
6106 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07006107
Linus Torvalds1da177e2005-04-16 15:20:36 -07006108 /* Initialize invariants of the rings, we only set this
6109 * stuff once. This works because the card does not
6110 * write into the rx buffer posting rings.
6111 */
6112 for (i = 0; i < TG3_RX_RING_SIZE; i++) {
6113 struct tg3_rx_buffer_desc *rxd;
6114
Matt Carlson21f581a2009-08-28 14:00:25 +00006115 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00006116 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006117 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
6118 rxd->opaque = (RXD_OPAQUE_RING_STD |
6119 (i << RXD_OPAQUE_INDEX_SHIFT));
6120 }
6121
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006122 /* Now allocate fresh SKBs for each rx ring. */
6123 for (i = 0; i < tp->rx_pending; i++) {
Matt Carlson86b21e52009-11-13 13:03:45 +00006124 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_STD, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006125 netdev_warn(tp->dev,
6126 "Using a smaller RX standard ring. Only "
6127 "%d out of %d buffers were allocated "
6128 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006129 if (i == 0)
6130 goto initfail;
6131 tp->rx_pending = i;
6132 break;
6133 }
6134 }
6135
6136 if (!(tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE))
6137 goto done;
6138
Matt Carlson21f581a2009-08-28 14:00:25 +00006139 memset(tpr->rx_jmb, 0, TG3_RX_JUMBO_RING_BYTES);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006140
Matt Carlson0d86df82010-02-17 15:17:00 +00006141 if (!(tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE))
6142 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006143
Matt Carlson0d86df82010-02-17 15:17:00 +00006144 for (i = 0; i < TG3_RX_JUMBO_RING_SIZE; i++) {
6145 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006146
Matt Carlson0d86df82010-02-17 15:17:00 +00006147 rxd = &tpr->rx_jmb[i].std;
6148 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
6149 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
6150 RXD_FLAG_JUMBO;
6151 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
6152 (i << RXD_OPAQUE_INDEX_SHIFT));
6153 }
6154
6155 for (i = 0; i < tp->rx_jumbo_pending; i++) {
6156 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_JUMBO, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006157 netdev_warn(tp->dev,
6158 "Using a smaller RX jumbo ring. Only %d "
6159 "out of %d buffers were allocated "
6160 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00006161 if (i == 0)
6162 goto initfail;
6163 tp->rx_jumbo_pending = i;
6164 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006165 }
6166 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006167
6168done:
Michael Chan32d8c572006-07-25 16:38:29 -07006169 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006170
6171initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00006172 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006173 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006174}
6175
Matt Carlson21f581a2009-08-28 14:00:25 +00006176static void tg3_rx_prodring_fini(struct tg3 *tp,
6177 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006178{
Matt Carlson21f581a2009-08-28 14:00:25 +00006179 kfree(tpr->rx_std_buffers);
6180 tpr->rx_std_buffers = NULL;
6181 kfree(tpr->rx_jmb_buffers);
6182 tpr->rx_jmb_buffers = NULL;
6183 if (tpr->rx_std) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006184 pci_free_consistent(tp->pdev, TG3_RX_RING_BYTES,
Matt Carlson21f581a2009-08-28 14:00:25 +00006185 tpr->rx_std, tpr->rx_std_mapping);
6186 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006187 }
Matt Carlson21f581a2009-08-28 14:00:25 +00006188 if (tpr->rx_jmb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006189 pci_free_consistent(tp->pdev, TG3_RX_JUMBO_RING_BYTES,
Matt Carlson21f581a2009-08-28 14:00:25 +00006190 tpr->rx_jmb, tpr->rx_jmb_mapping);
6191 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006192 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006193}
6194
Matt Carlson21f581a2009-08-28 14:00:25 +00006195static int tg3_rx_prodring_init(struct tg3 *tp,
6196 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006197{
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006198 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE, GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006199 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006200 return -ENOMEM;
6201
Matt Carlson21f581a2009-08-28 14:00:25 +00006202 tpr->rx_std = pci_alloc_consistent(tp->pdev, TG3_RX_RING_BYTES,
6203 &tpr->rx_std_mapping);
6204 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006205 goto err_out;
6206
6207 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006208 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE,
Matt Carlson21f581a2009-08-28 14:00:25 +00006209 GFP_KERNEL);
6210 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006211 goto err_out;
6212
Matt Carlson21f581a2009-08-28 14:00:25 +00006213 tpr->rx_jmb = pci_alloc_consistent(tp->pdev,
6214 TG3_RX_JUMBO_RING_BYTES,
6215 &tpr->rx_jmb_mapping);
6216 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006217 goto err_out;
6218 }
6219
6220 return 0;
6221
6222err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00006223 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006224 return -ENOMEM;
6225}
6226
6227/* Free up pending packets in all rx/tx rings.
6228 *
6229 * The chip has been shut down and the driver detached from
6230 * the networking, so no interrupts or new tx packets will
6231 * end up in the driver. tp->{tx,}lock is not held and we are not
6232 * in an interrupt context and thus may sleep.
6233 */
6234static void tg3_free_rings(struct tg3 *tp)
6235{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006236 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006237
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006238 for (j = 0; j < tp->irq_cnt; j++) {
6239 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006240
Matt Carlsonb28f6422010-06-05 17:24:32 +00006241 tg3_rx_prodring_free(tp, &tp->prodring[j]);
6242
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006243 if (!tnapi->tx_buffers)
6244 continue;
6245
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006246 for (i = 0; i < TG3_TX_RING_SIZE; ) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00006247 struct ring_info *txp;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006248 struct sk_buff *skb;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006249 unsigned int k;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006250
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006251 txp = &tnapi->tx_buffers[i];
6252 skb = txp->skb;
6253
6254 if (skb == NULL) {
6255 i++;
6256 continue;
6257 }
6258
Alexander Duyckf4188d82009-12-02 16:48:38 +00006259 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006260 dma_unmap_addr(txp, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006261 skb_headlen(skb),
6262 PCI_DMA_TODEVICE);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006263 txp->skb = NULL;
6264
Alexander Duyckf4188d82009-12-02 16:48:38 +00006265 i++;
6266
6267 for (k = 0; k < skb_shinfo(skb)->nr_frags; k++) {
6268 txp = &tnapi->tx_buffers[i & (TG3_TX_RING_SIZE - 1)];
6269 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006270 dma_unmap_addr(txp, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006271 skb_shinfo(skb)->frags[k].size,
6272 PCI_DMA_TODEVICE);
6273 i++;
6274 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006275
6276 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006277 }
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006278 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006279}
6280
6281/* Initialize tx/rx rings for packet processing.
6282 *
6283 * The chip has been shut down and the driver detached from
6284 * the networking, so no interrupts or new tx packets will
6285 * end up in the driver. tp->{tx,}lock are held and thus
6286 * we may not sleep.
6287 */
6288static int tg3_init_rings(struct tg3 *tp)
6289{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006290 int i;
Matt Carlson72334482009-08-28 14:03:01 +00006291
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006292 /* Free up all the SKBs. */
6293 tg3_free_rings(tp);
6294
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006295 for (i = 0; i < tp->irq_cnt; i++) {
6296 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006297
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006298 tnapi->last_tag = 0;
6299 tnapi->last_irq_tag = 0;
6300 tnapi->hw_status->status = 0;
6301 tnapi->hw_status->status_tag = 0;
6302 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6303
6304 tnapi->tx_prod = 0;
6305 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006306 if (tnapi->tx_ring)
6307 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006308
6309 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006310 if (tnapi->rx_rcb)
6311 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006312
Matt Carlsone4af1af2010-02-12 14:47:05 +00006313 if (tg3_rx_prodring_alloc(tp, &tp->prodring[i])) {
6314 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006315 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006316 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006317 }
Matt Carlson72334482009-08-28 14:03:01 +00006318
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006319 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006320}
6321
6322/*
6323 * Must not be invoked with interrupt sources disabled and
6324 * the hardware shutdown down.
6325 */
6326static void tg3_free_consistent(struct tg3 *tp)
6327{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006328 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006329
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006330 for (i = 0; i < tp->irq_cnt; i++) {
6331 struct tg3_napi *tnapi = &tp->napi[i];
6332
6333 if (tnapi->tx_ring) {
6334 pci_free_consistent(tp->pdev, TG3_TX_RING_BYTES,
6335 tnapi->tx_ring, tnapi->tx_desc_mapping);
6336 tnapi->tx_ring = NULL;
6337 }
6338
6339 kfree(tnapi->tx_buffers);
6340 tnapi->tx_buffers = NULL;
6341
6342 if (tnapi->rx_rcb) {
6343 pci_free_consistent(tp->pdev, TG3_RX_RCB_RING_BYTES(tp),
6344 tnapi->rx_rcb,
6345 tnapi->rx_rcb_mapping);
6346 tnapi->rx_rcb = NULL;
6347 }
6348
6349 if (tnapi->hw_status) {
6350 pci_free_consistent(tp->pdev, TG3_HW_STATUS_SIZE,
6351 tnapi->hw_status,
6352 tnapi->status_mapping);
6353 tnapi->hw_status = NULL;
6354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006355 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006356
Linus Torvalds1da177e2005-04-16 15:20:36 -07006357 if (tp->hw_stats) {
6358 pci_free_consistent(tp->pdev, sizeof(struct tg3_hw_stats),
6359 tp->hw_stats, tp->stats_mapping);
6360 tp->hw_stats = NULL;
6361 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006362
Matt Carlsone4af1af2010-02-12 14:47:05 +00006363 for (i = 0; i < tp->irq_cnt; i++)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006364 tg3_rx_prodring_fini(tp, &tp->prodring[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006365}
6366
6367/*
6368 * Must not be invoked with interrupt sources disabled and
6369 * the hardware shutdown down. Can sleep.
6370 */
6371static int tg3_alloc_consistent(struct tg3 *tp)
6372{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006373 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006374
Matt Carlsone4af1af2010-02-12 14:47:05 +00006375 for (i = 0; i < tp->irq_cnt; i++) {
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006376 if (tg3_rx_prodring_init(tp, &tp->prodring[i]))
6377 goto err_out;
6378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006379
Linus Torvalds1da177e2005-04-16 15:20:36 -07006380 tp->hw_stats = pci_alloc_consistent(tp->pdev,
6381 sizeof(struct tg3_hw_stats),
6382 &tp->stats_mapping);
6383 if (!tp->hw_stats)
6384 goto err_out;
6385
Linus Torvalds1da177e2005-04-16 15:20:36 -07006386 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6387
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006388 for (i = 0; i < tp->irq_cnt; i++) {
6389 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006390 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006391
6392 tnapi->hw_status = pci_alloc_consistent(tp->pdev,
6393 TG3_HW_STATUS_SIZE,
6394 &tnapi->status_mapping);
6395 if (!tnapi->hw_status)
6396 goto err_out;
6397
6398 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006399 sblk = tnapi->hw_status;
6400
Matt Carlson19cfaec2009-12-03 08:36:20 +00006401 /* If multivector TSS is enabled, vector 0 does not handle
6402 * tx interrupts. Don't allocate any resources for it.
6403 */
6404 if ((!i && !(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)) ||
6405 (i && (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS))) {
6406 tnapi->tx_buffers = kzalloc(sizeof(struct ring_info) *
6407 TG3_TX_RING_SIZE,
6408 GFP_KERNEL);
6409 if (!tnapi->tx_buffers)
6410 goto err_out;
6411
6412 tnapi->tx_ring = pci_alloc_consistent(tp->pdev,
6413 TG3_TX_RING_BYTES,
6414 &tnapi->tx_desc_mapping);
6415 if (!tnapi->tx_ring)
6416 goto err_out;
6417 }
6418
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006419 /*
6420 * When RSS is enabled, the status block format changes
6421 * slightly. The "rx_jumbo_consumer", "reserved",
6422 * and "rx_mini_consumer" members get mapped to the
6423 * other three rx return ring producer indexes.
6424 */
6425 switch (i) {
6426 default:
6427 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
6428 break;
6429 case 2:
6430 tnapi->rx_rcb_prod_idx = &sblk->rx_jumbo_consumer;
6431 break;
6432 case 3:
6433 tnapi->rx_rcb_prod_idx = &sblk->reserved;
6434 break;
6435 case 4:
6436 tnapi->rx_rcb_prod_idx = &sblk->rx_mini_consumer;
6437 break;
6438 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006439
Matt Carlsone4af1af2010-02-12 14:47:05 +00006440 tnapi->prodring = &tp->prodring[i];
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006441
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006442 /*
6443 * If multivector RSS is enabled, vector 0 does not handle
6444 * rx or tx interrupts. Don't allocate any resources for it.
6445 */
6446 if (!i && (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS))
6447 continue;
6448
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006449 tnapi->rx_rcb = pci_alloc_consistent(tp->pdev,
6450 TG3_RX_RCB_RING_BYTES(tp),
6451 &tnapi->rx_rcb_mapping);
6452 if (!tnapi->rx_rcb)
6453 goto err_out;
6454
6455 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006456 }
6457
Linus Torvalds1da177e2005-04-16 15:20:36 -07006458 return 0;
6459
6460err_out:
6461 tg3_free_consistent(tp);
6462 return -ENOMEM;
6463}
6464
6465#define MAX_WAIT_CNT 1000
6466
6467/* To stop a block, clear the enable bit and poll till it
6468 * clears. tp->lock is held.
6469 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006470static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006471{
6472 unsigned int i;
6473 u32 val;
6474
6475 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
6476 switch (ofs) {
6477 case RCVLSC_MODE:
6478 case DMAC_MODE:
6479 case MBFREE_MODE:
6480 case BUFMGR_MODE:
6481 case MEMARB_MODE:
6482 /* We can't enable/disable these bits of the
6483 * 5705/5750, just say success.
6484 */
6485 return 0;
6486
6487 default:
6488 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006490 }
6491
6492 val = tr32(ofs);
6493 val &= ~enable_bit;
6494 tw32_f(ofs, val);
6495
6496 for (i = 0; i < MAX_WAIT_CNT; i++) {
6497 udelay(100);
6498 val = tr32(ofs);
6499 if ((val & enable_bit) == 0)
6500 break;
6501 }
6502
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006503 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00006504 dev_err(&tp->pdev->dev,
6505 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
6506 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006507 return -ENODEV;
6508 }
6509
6510 return 0;
6511}
6512
6513/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006514static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006515{
6516 int i, err;
6517
6518 tg3_disable_ints(tp);
6519
6520 tp->rx_mode &= ~RX_MODE_ENABLE;
6521 tw32_f(MAC_RX_MODE, tp->rx_mode);
6522 udelay(10);
6523
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006524 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
6525 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
6526 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
6527 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
6528 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
6529 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006530
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006531 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
6532 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
6533 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
6534 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
6535 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
6536 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
6537 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006538
6539 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
6540 tw32_f(MAC_MODE, tp->mac_mode);
6541 udelay(40);
6542
6543 tp->tx_mode &= ~TX_MODE_ENABLE;
6544 tw32_f(MAC_TX_MODE, tp->tx_mode);
6545
6546 for (i = 0; i < MAX_WAIT_CNT; i++) {
6547 udelay(100);
6548 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
6549 break;
6550 }
6551 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00006552 dev_err(&tp->pdev->dev,
6553 "%s timed out, TX_MODE_ENABLE will not clear "
6554 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07006555 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006556 }
6557
Michael Chane6de8ad2005-05-05 14:42:41 -07006558 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006559 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
6560 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006561
6562 tw32(FTQ_RESET, 0xffffffff);
6563 tw32(FTQ_RESET, 0x00000000);
6564
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006565 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
6566 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006567
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006568 for (i = 0; i < tp->irq_cnt; i++) {
6569 struct tg3_napi *tnapi = &tp->napi[i];
6570 if (tnapi->hw_status)
6571 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006573 if (tp->hw_stats)
6574 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6575
Linus Torvalds1da177e2005-04-16 15:20:36 -07006576 return err;
6577}
6578
Matt Carlson0d3031d2007-10-10 18:02:43 -07006579static void tg3_ape_send_event(struct tg3 *tp, u32 event)
6580{
6581 int i;
6582 u32 apedata;
6583
6584 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
6585 if (apedata != APE_SEG_SIG_MAGIC)
6586 return;
6587
6588 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
Matt Carlson731fd792008-08-15 14:07:51 -07006589 if (!(apedata & APE_FW_STATUS_READY))
Matt Carlson0d3031d2007-10-10 18:02:43 -07006590 return;
6591
6592 /* Wait for up to 1 millisecond for APE to service previous event. */
6593 for (i = 0; i < 10; i++) {
6594 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
6595 return;
6596
6597 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
6598
6599 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6600 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
6601 event | APE_EVENT_STATUS_EVENT_PENDING);
6602
6603 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
6604
6605 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6606 break;
6607
6608 udelay(100);
6609 }
6610
6611 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6612 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
6613}
6614
6615static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
6616{
6617 u32 event;
6618 u32 apedata;
6619
6620 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
6621 return;
6622
6623 switch (kind) {
Matt Carlson33f401a2010-04-05 10:19:27 +00006624 case RESET_KIND_INIT:
6625 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
6626 APE_HOST_SEG_SIG_MAGIC);
6627 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
6628 APE_HOST_SEG_LEN_MAGIC);
6629 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
6630 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
6631 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
6632 APE_HOST_DRIVER_ID_MAGIC);
6633 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
6634 APE_HOST_BEHAV_NO_PHYLOCK);
Matt Carlson0d3031d2007-10-10 18:02:43 -07006635
Matt Carlson33f401a2010-04-05 10:19:27 +00006636 event = APE_EVENT_STATUS_STATE_START;
6637 break;
6638 case RESET_KIND_SHUTDOWN:
6639 /* With the interface we are currently using,
6640 * APE does not track driver state. Wiping
6641 * out the HOST SEGMENT SIGNATURE forces
6642 * the APE to assume OS absent status.
6643 */
6644 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
Matt Carlsonb2aee152008-11-03 16:51:11 -08006645
Matt Carlson33f401a2010-04-05 10:19:27 +00006646 event = APE_EVENT_STATUS_STATE_UNLOAD;
6647 break;
6648 case RESET_KIND_SUSPEND:
6649 event = APE_EVENT_STATUS_STATE_SUSPEND;
6650 break;
6651 default:
6652 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -07006653 }
6654
6655 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
6656
6657 tg3_ape_send_event(tp, event);
6658}
6659
Michael Chane6af3012005-04-21 17:12:05 -07006660/* tp->lock is held. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006661static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
6662{
David S. Millerf49639e2006-06-09 11:58:36 -07006663 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
6664 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006665
6666 if (tp->tg3_flags2 & TG3_FLG2_ASF_NEW_HANDSHAKE) {
6667 switch (kind) {
6668 case RESET_KIND_INIT:
6669 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6670 DRV_STATE_START);
6671 break;
6672
6673 case RESET_KIND_SHUTDOWN:
6674 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6675 DRV_STATE_UNLOAD);
6676 break;
6677
6678 case RESET_KIND_SUSPEND:
6679 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6680 DRV_STATE_SUSPEND);
6681 break;
6682
6683 default:
6684 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006686 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07006687
6688 if (kind == RESET_KIND_INIT ||
6689 kind == RESET_KIND_SUSPEND)
6690 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006691}
6692
6693/* tp->lock is held. */
6694static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
6695{
6696 if (tp->tg3_flags2 & TG3_FLG2_ASF_NEW_HANDSHAKE) {
6697 switch (kind) {
6698 case RESET_KIND_INIT:
6699 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6700 DRV_STATE_START_DONE);
6701 break;
6702
6703 case RESET_KIND_SHUTDOWN:
6704 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6705 DRV_STATE_UNLOAD_DONE);
6706 break;
6707
6708 default:
6709 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006711 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07006712
6713 if (kind == RESET_KIND_SHUTDOWN)
6714 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006715}
6716
6717/* tp->lock is held. */
6718static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
6719{
6720 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) {
6721 switch (kind) {
6722 case RESET_KIND_INIT:
6723 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6724 DRV_STATE_START);
6725 break;
6726
6727 case RESET_KIND_SHUTDOWN:
6728 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6729 DRV_STATE_UNLOAD);
6730 break;
6731
6732 case RESET_KIND_SUSPEND:
6733 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6734 DRV_STATE_SUSPEND);
6735 break;
6736
6737 default:
6738 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006739 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006740 }
6741}
6742
Michael Chan7a6f4362006-09-27 16:03:31 -07006743static int tg3_poll_fw(struct tg3 *tp)
6744{
6745 int i;
6746 u32 val;
6747
Michael Chanb5d37722006-09-27 16:06:21 -07006748 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Gary Zambrano0ccead12006-11-14 16:34:00 -08006749 /* Wait up to 20ms for init done. */
6750 for (i = 0; i < 200; i++) {
Michael Chanb5d37722006-09-27 16:06:21 -07006751 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
6752 return 0;
Gary Zambrano0ccead12006-11-14 16:34:00 -08006753 udelay(100);
Michael Chanb5d37722006-09-27 16:06:21 -07006754 }
6755 return -ENODEV;
6756 }
6757
Michael Chan7a6f4362006-09-27 16:03:31 -07006758 /* Wait for firmware initialization to complete. */
6759 for (i = 0; i < 100000; i++) {
6760 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
6761 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
6762 break;
6763 udelay(10);
6764 }
6765
6766 /* Chip might not be fitted with firmware. Some Sun onboard
6767 * parts are configured like that. So don't signal the timeout
6768 * of the above loop as an error, but do report the lack of
6769 * running firmware once.
6770 */
6771 if (i >= 100000 &&
6772 !(tp->tg3_flags2 & TG3_FLG2_NO_FWARE_REPORTED)) {
6773 tp->tg3_flags2 |= TG3_FLG2_NO_FWARE_REPORTED;
6774
Joe Perches05dbe002010-02-17 19:44:19 +00006775 netdev_info(tp->dev, "No firmware running\n");
Michael Chan7a6f4362006-09-27 16:03:31 -07006776 }
6777
Matt Carlson6b10c162010-02-12 14:47:08 +00006778 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
6779 /* The 57765 A0 needs a little more
6780 * time to do some important work.
6781 */
6782 mdelay(10);
6783 }
6784
Michael Chan7a6f4362006-09-27 16:03:31 -07006785 return 0;
6786}
6787
Michael Chanee6a99b2007-07-18 21:49:10 -07006788/* Save PCI command register before chip reset */
6789static void tg3_save_pci_state(struct tg3 *tp)
6790{
Matt Carlson8a6eac92007-10-21 16:17:55 -07006791 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07006792}
6793
6794/* Restore PCI state after chip reset */
6795static void tg3_restore_pci_state(struct tg3 *tp)
6796{
6797 u32 val;
6798
6799 /* Re-enable indirect register accesses. */
6800 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
6801 tp->misc_host_ctrl);
6802
6803 /* Set MAX PCI retry to zero. */
6804 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
6805 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
6806 (tp->tg3_flags & TG3_FLAG_PCIX_MODE))
6807 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07006808 /* Allow reads and writes to the APE register and memory space. */
6809 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
6810 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00006811 PCISTATE_ALLOW_APE_SHMEM_WR |
6812 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07006813 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
6814
Matt Carlson8a6eac92007-10-21 16:17:55 -07006815 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07006816
Matt Carlsonfcb389d2008-11-03 16:55:44 -08006817 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785) {
6818 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)
6819 pcie_set_readrq(tp->pdev, 4096);
6820 else {
6821 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
6822 tp->pci_cacheline_sz);
6823 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
6824 tp->pci_lat_timer);
6825 }
Michael Chan114342f2007-10-15 02:12:26 -07006826 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08006827
Michael Chanee6a99b2007-07-18 21:49:10 -07006828 /* Make sure PCI-X relaxed ordering bit is clear. */
Matt Carlson52f44902008-11-21 17:17:04 -08006829 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
Matt Carlson9974a352007-10-07 23:27:28 -07006830 u16 pcix_cmd;
6831
6832 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
6833 &pcix_cmd);
6834 pcix_cmd &= ~PCI_X_CMD_ERO;
6835 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
6836 pcix_cmd);
6837 }
Michael Chanee6a99b2007-07-18 21:49:10 -07006838
6839 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
Michael Chanee6a99b2007-07-18 21:49:10 -07006840
6841 /* Chip reset on 5780 will reset MSI enable bit,
6842 * so need to restore it.
6843 */
6844 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
6845 u16 ctrl;
6846
6847 pci_read_config_word(tp->pdev,
6848 tp->msi_cap + PCI_MSI_FLAGS,
6849 &ctrl);
6850 pci_write_config_word(tp->pdev,
6851 tp->msi_cap + PCI_MSI_FLAGS,
6852 ctrl | PCI_MSI_FLAGS_ENABLE);
6853 val = tr32(MSGINT_MODE);
6854 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
6855 }
6856 }
6857}
6858
Linus Torvalds1da177e2005-04-16 15:20:36 -07006859static void tg3_stop_fw(struct tg3 *);
6860
6861/* tp->lock is held. */
6862static int tg3_chip_reset(struct tg3 *tp)
6863{
6864 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07006865 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00006866 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006867
David S. Millerf49639e2006-06-09 11:58:36 -07006868 tg3_nvram_lock(tp);
6869
Matt Carlson77b483f2008-08-15 14:07:24 -07006870 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
6871
David S. Millerf49639e2006-06-09 11:58:36 -07006872 /* No matching tg3_nvram_unlock() after this because
6873 * chip reset below will undo the nvram lock.
6874 */
6875 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006876
Michael Chanee6a99b2007-07-18 21:49:10 -07006877 /* GRC_MISC_CFG core clock reset will clear the memory
6878 * enable bit in PCI register 4 and the MSI enable bit
6879 * on some chips, so we save relevant registers here.
6880 */
6881 tg3_save_pci_state(tp);
6882
Michael Chand9ab5ad2006-03-20 22:27:35 -08006883 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08006884 (tp->tg3_flags3 & TG3_FLG3_5755_PLUS))
Michael Chand9ab5ad2006-03-20 22:27:35 -08006885 tw32(GRC_FASTBOOT_PC, 0);
6886
Linus Torvalds1da177e2005-04-16 15:20:36 -07006887 /*
6888 * We must avoid the readl() that normally takes place.
6889 * It locks machines, causes machine checks, and other
6890 * fun things. So, temporarily disable the 5701
6891 * hardware workaround, while we do the reset.
6892 */
Michael Chan1ee582d2005-08-09 20:16:46 -07006893 write_op = tp->write32;
6894 if (write_op == tg3_write_flush_reg32)
6895 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006896
Michael Chand18edcb2007-03-24 20:57:11 -07006897 /* Prevent the irq handler from reading or writing PCI registers
6898 * during chip reset when the memory enable bit in the PCI command
6899 * register may be cleared. The chip does not generate interrupt
6900 * at this time, but the irq handler may still be called due to irq
6901 * sharing or irqpoll.
6902 */
6903 tp->tg3_flags |= TG3_FLAG_CHIP_RESETTING;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006904 for (i = 0; i < tp->irq_cnt; i++) {
6905 struct tg3_napi *tnapi = &tp->napi[i];
6906 if (tnapi->hw_status) {
6907 tnapi->hw_status->status = 0;
6908 tnapi->hw_status->status_tag = 0;
6909 }
6910 tnapi->last_tag = 0;
6911 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07006912 }
Michael Chand18edcb2007-03-24 20:57:11 -07006913 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00006914
6915 for (i = 0; i < tp->irq_cnt; i++)
6916 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07006917
Matt Carlson255ca312009-08-25 10:07:27 +00006918 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
6919 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
6920 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
6921 }
6922
Linus Torvalds1da177e2005-04-16 15:20:36 -07006923 /* do the reset */
6924 val = GRC_MISC_CFG_CORECLK_RESET;
6925
6926 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
6927 if (tr32(0x7e2c) == 0x60) {
6928 tw32(0x7e2c, 0x20);
6929 }
6930 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
6931 tw32(GRC_MISC_CFG, (1 << 29));
6932 val |= (1 << 29);
6933 }
6934 }
6935
Michael Chanb5d37722006-09-27 16:06:21 -07006936 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
6937 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
6938 tw32(GRC_VCPU_EXT_CTRL,
6939 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
6940 }
6941
Linus Torvalds1da177e2005-04-16 15:20:36 -07006942 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
6943 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
6944 tw32(GRC_MISC_CFG, val);
6945
Michael Chan1ee582d2005-08-09 20:16:46 -07006946 /* restore 5701 hardware bug workaround write method */
6947 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006948
6949 /* Unfortunately, we have to delay before the PCI read back.
6950 * Some 575X chips even will not respond to a PCI cfg access
6951 * when the reset command is given to the chip.
6952 *
6953 * How do these hardware designers expect things to work
6954 * properly if the PCI write is posted for a long period
6955 * of time? It is always necessary to have some method by
6956 * which a register read back can occur to push the write
6957 * out which does the reset.
6958 *
6959 * For most tg3 variants the trick below was working.
6960 * Ho hum...
6961 */
6962 udelay(120);
6963
6964 /* Flush PCI posted writes. The normal MMIO registers
6965 * are inaccessible at this time so this is the only
6966 * way to make this reliably (actually, this is no longer
6967 * the case, see above). I tried to use indirect
6968 * register read/write but this upset some 5701 variants.
6969 */
6970 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
6971
6972 udelay(120);
6973
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006974 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) && tp->pcie_cap) {
Matt Carlsone7126992009-08-25 10:08:16 +00006975 u16 val16;
6976
Linus Torvalds1da177e2005-04-16 15:20:36 -07006977 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
6978 int i;
6979 u32 cfg_val;
6980
6981 /* Wait for link training to complete. */
6982 for (i = 0; i < 5000; i++)
6983 udelay(100);
6984
6985 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
6986 pci_write_config_dword(tp->pdev, 0xc4,
6987 cfg_val | (1 << 15));
6988 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006989
Matt Carlsone7126992009-08-25 10:08:16 +00006990 /* Clear the "no snoop" and "relaxed ordering" bits. */
6991 pci_read_config_word(tp->pdev,
6992 tp->pcie_cap + PCI_EXP_DEVCTL,
6993 &val16);
6994 val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
6995 PCI_EXP_DEVCTL_NOSNOOP_EN);
6996 /*
6997 * Older PCIe devices only support the 128 byte
6998 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006999 */
Matt Carlsone7126992009-08-25 10:08:16 +00007000 if (!(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
7001 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784))
7002 val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007003 pci_write_config_word(tp->pdev,
7004 tp->pcie_cap + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007005 val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007006
7007 pcie_set_readrq(tp->pdev, 4096);
7008
7009 /* Clear error status */
7010 pci_write_config_word(tp->pdev,
7011 tp->pcie_cap + PCI_EXP_DEVSTA,
7012 PCI_EXP_DEVSTA_CED |
7013 PCI_EXP_DEVSTA_NFED |
7014 PCI_EXP_DEVSTA_FED |
7015 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007016 }
7017
Michael Chanee6a99b2007-07-18 21:49:10 -07007018 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007019
Michael Chand18edcb2007-03-24 20:57:11 -07007020 tp->tg3_flags &= ~TG3_FLAG_CHIP_RESETTING;
7021
Michael Chanee6a99b2007-07-18 21:49:10 -07007022 val = 0;
7023 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
Michael Chan4cf78e42005-07-25 12:29:19 -07007024 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07007025 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007026
7027 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
7028 tg3_stop_fw(tp);
7029 tw32(0x5000, 0x400);
7030 }
7031
7032 tw32(GRC_MODE, tp->grc_mode);
7033
7034 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007035 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007036
7037 tw32(0xc4, val | (1 << 15));
7038 }
7039
7040 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
7041 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
7042 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
7043 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
7044 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
7045 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7046 }
7047
7048 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
7049 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
7050 tw32_f(MAC_MODE, tp->mac_mode);
Michael Chan747e8f82005-07-25 12:33:22 -07007051 } else if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES) {
7052 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
7053 tw32_f(MAC_MODE, tp->mac_mode);
Matt Carlson3bda1252008-08-15 14:08:22 -07007054 } else if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
7055 tp->mac_mode &= (MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN);
7056 if (tp->mac_mode & MAC_MODE_APE_TX_EN)
7057 tp->mac_mode |= MAC_MODE_TDE_ENABLE;
7058 tw32_f(MAC_MODE, tp->mac_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007059 } else
7060 tw32_f(MAC_MODE, 0);
7061 udelay(40);
7062
Matt Carlson77b483f2008-08-15 14:07:24 -07007063 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
7064
Michael Chan7a6f4362006-09-27 16:03:31 -07007065 err = tg3_poll_fw(tp);
7066 if (err)
7067 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007068
Matt Carlson0a9140c2009-08-28 12:27:50 +00007069 tg3_mdio_start(tp);
7070
Matt Carlson52cdf852009-11-02 14:25:06 +00007071 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7072 u8 phy_addr;
7073
7074 phy_addr = tp->phy_addr;
7075 tp->phy_addr = TG3_PHY_PCIE_ADDR;
7076
7077 tg3_writephy(tp, TG3_PCIEPHY_BLOCK_ADDR,
7078 TG3_PCIEPHY_TXB_BLK << TG3_PCIEPHY_BLOCK_SHIFT);
7079 val = TG3_PCIEPHY_TX0CTRL1_TXOCM | TG3_PCIEPHY_TX0CTRL1_RDCTL |
7080 TG3_PCIEPHY_TX0CTRL1_TXCMV | TG3_PCIEPHY_TX0CTRL1_TKSEL |
7081 TG3_PCIEPHY_TX0CTRL1_NB_EN;
7082 tg3_writephy(tp, TG3_PCIEPHY_TX0CTRL1, val);
7083 udelay(10);
7084
7085 tg3_writephy(tp, TG3_PCIEPHY_BLOCK_ADDR,
7086 TG3_PCIEPHY_XGXS_BLK1 << TG3_PCIEPHY_BLOCK_SHIFT);
7087 val = TG3_PCIEPHY_PWRMGMT4_LOWPWR_EN |
7088 TG3_PCIEPHY_PWRMGMT4_L1PLLPD_EN;
7089 tg3_writephy(tp, TG3_PCIEPHY_PWRMGMT4, val);
7090 udelay(10);
7091
7092 tp->phy_addr = phy_addr;
7093 }
7094
Linus Torvalds1da177e2005-04-16 15:20:36 -07007095 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007096 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
7097 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonb703df62009-12-03 08:36:21 +00007098 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
Matt Carlsona50d0792010-06-05 17:24:37 +00007099 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5719 &&
Matt Carlsonb703df62009-12-03 08:36:21 +00007100 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57765) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007101 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007102
7103 tw32(0x7c00, val | (1 << 25));
7104 }
7105
7106 /* Reprobe ASF enable state. */
7107 tp->tg3_flags &= ~TG3_FLAG_ENABLE_ASF;
7108 tp->tg3_flags2 &= ~TG3_FLG2_ASF_NEW_HANDSHAKE;
7109 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
7110 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
7111 u32 nic_cfg;
7112
7113 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
7114 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
7115 tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
Matt Carlson4ba526c2008-08-15 14:10:04 -07007116 tp->last_event_jiffies = jiffies;
John W. Linvillecbf46852005-04-21 17:01:29 -07007117 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007118 tp->tg3_flags2 |= TG3_FLG2_ASF_NEW_HANDSHAKE;
7119 }
7120 }
7121
7122 return 0;
7123}
7124
7125/* tp->lock is held. */
7126static void tg3_stop_fw(struct tg3 *tp)
7127{
Matt Carlson0d3031d2007-10-10 18:02:43 -07007128 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
7129 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07007130 /* Wait for RX cpu to ACK the previous event. */
7131 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007132
7133 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007134
7135 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007136
Matt Carlson7c5026a2008-05-02 16:49:29 -07007137 /* Wait for RX cpu to ACK this event. */
7138 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007139 }
7140}
7141
7142/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07007143static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007144{
7145 int err;
7146
7147 tg3_stop_fw(tp);
7148
Michael Chan944d9802005-05-29 14:57:48 -07007149 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007150
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007151 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007152 err = tg3_chip_reset(tp);
7153
Matt Carlsondaba2a62009-04-20 06:58:52 +00007154 __tg3_set_mac_addr(tp, 0);
7155
Michael Chan944d9802005-05-29 14:57:48 -07007156 tg3_write_sig_legacy(tp, kind);
7157 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007158
7159 if (err)
7160 return err;
7161
7162 return 0;
7163}
7164
Linus Torvalds1da177e2005-04-16 15:20:36 -07007165#define RX_CPU_SCRATCH_BASE 0x30000
7166#define RX_CPU_SCRATCH_SIZE 0x04000
7167#define TX_CPU_SCRATCH_BASE 0x34000
7168#define TX_CPU_SCRATCH_SIZE 0x04000
7169
7170/* tp->lock is held. */
7171static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
7172{
7173 int i;
7174
Eric Sesterhenn5d9428d2006-04-02 13:52:48 +02007175 BUG_ON(offset == TX_CPU_BASE &&
7176 (tp->tg3_flags2 & TG3_FLG2_5705_PLUS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007177
Michael Chanb5d37722006-09-27 16:06:21 -07007178 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7179 u32 val = tr32(GRC_VCPU_EXT_CTRL);
7180
7181 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
7182 return 0;
7183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007184 if (offset == RX_CPU_BASE) {
7185 for (i = 0; i < 10000; i++) {
7186 tw32(offset + CPU_STATE, 0xffffffff);
7187 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7188 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7189 break;
7190 }
7191
7192 tw32(offset + CPU_STATE, 0xffffffff);
7193 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
7194 udelay(10);
7195 } else {
7196 for (i = 0; i < 10000; i++) {
7197 tw32(offset + CPU_STATE, 0xffffffff);
7198 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7199 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7200 break;
7201 }
7202 }
7203
7204 if (i >= 10000) {
Joe Perches05dbe002010-02-17 19:44:19 +00007205 netdev_err(tp->dev, "%s timed out, %s CPU\n",
7206 __func__, offset == RX_CPU_BASE ? "RX" : "TX");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007207 return -ENODEV;
7208 }
Michael Chanec41c7d2006-01-17 02:40:55 -08007209
7210 /* Clear firmware's nvram arbitration. */
7211 if (tp->tg3_flags & TG3_FLAG_NVRAM)
7212 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007213 return 0;
7214}
7215
7216struct fw_info {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007217 unsigned int fw_base;
7218 unsigned int fw_len;
7219 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007220};
7221
7222/* tp->lock is held. */
7223static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base, u32 cpu_scratch_base,
7224 int cpu_scratch_size, struct fw_info *info)
7225{
Michael Chanec41c7d2006-01-17 02:40:55 -08007226 int err, lock_err, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007227 void (*write_op)(struct tg3 *, u32, u32);
7228
7229 if (cpu_base == TX_CPU_BASE &&
7230 (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007231 netdev_err(tp->dev,
7232 "%s: Trying to load TX cpu firmware which is 5705\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007233 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007234 return -EINVAL;
7235 }
7236
7237 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
7238 write_op = tg3_write_mem;
7239 else
7240 write_op = tg3_write_indirect_reg32;
7241
Michael Chan1b628152005-05-29 14:59:49 -07007242 /* It is possible that bootcode is still loading at this point.
7243 * Get the nvram lock first before halting the cpu.
7244 */
Michael Chanec41c7d2006-01-17 02:40:55 -08007245 lock_err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007246 err = tg3_halt_cpu(tp, cpu_base);
Michael Chanec41c7d2006-01-17 02:40:55 -08007247 if (!lock_err)
7248 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007249 if (err)
7250 goto out;
7251
7252 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
7253 write_op(tp, cpu_scratch_base + i, 0);
7254 tw32(cpu_base + CPU_STATE, 0xffffffff);
7255 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007256 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007257 write_op(tp, (cpu_scratch_base +
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007258 (info->fw_base & 0xffff) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07007259 (i * sizeof(u32))),
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007260 be32_to_cpu(info->fw_data[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007261
7262 err = 0;
7263
7264out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007265 return err;
7266}
7267
7268/* tp->lock is held. */
7269static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
7270{
7271 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007272 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007273 int err, i;
7274
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007275 fw_data = (void *)tp->fw->data;
7276
7277 /* Firmware blob starts with version numbers, followed by
7278 start address and length. We are setting complete length.
7279 length = end_address_of_bss - start_address_of_text.
7280 Remainder is the blob to be loaded contiguously
7281 from start address. */
7282
7283 info.fw_base = be32_to_cpu(fw_data[1]);
7284 info.fw_len = tp->fw->size - 12;
7285 info.fw_data = &fw_data[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007286
7287 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
7288 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
7289 &info);
7290 if (err)
7291 return err;
7292
7293 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
7294 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
7295 &info);
7296 if (err)
7297 return err;
7298
7299 /* Now startup only the RX cpu. */
7300 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007301 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007302
7303 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007304 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007305 break;
7306 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7307 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007308 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007309 udelay(1000);
7310 }
7311 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007312 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
7313 "should be %08x\n", __func__,
Joe Perches05dbe002010-02-17 19:44:19 +00007314 tr32(RX_CPU_BASE + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007315 return -ENODEV;
7316 }
7317 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7318 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
7319
7320 return 0;
7321}
7322
Linus Torvalds1da177e2005-04-16 15:20:36 -07007323/* 5705 needs a special version of the TSO firmware. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007324
7325/* tp->lock is held. */
7326static int tg3_load_tso_firmware(struct tg3 *tp)
7327{
7328 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007329 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007330 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
7331 int err, i;
7332
7333 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
7334 return 0;
7335
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007336 fw_data = (void *)tp->fw->data;
7337
7338 /* Firmware blob starts with version numbers, followed by
7339 start address and length. We are setting complete length.
7340 length = end_address_of_bss - start_address_of_text.
7341 Remainder is the blob to be loaded contiguously
7342 from start address. */
7343
7344 info.fw_base = be32_to_cpu(fw_data[1]);
7345 cpu_scratch_size = tp->fw_len;
7346 info.fw_len = tp->fw->size - 12;
7347 info.fw_data = &fw_data[3];
7348
Linus Torvalds1da177e2005-04-16 15:20:36 -07007349 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007350 cpu_base = RX_CPU_BASE;
7351 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007352 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007353 cpu_base = TX_CPU_BASE;
7354 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
7355 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
7356 }
7357
7358 err = tg3_load_firmware_cpu(tp, cpu_base,
7359 cpu_scratch_base, cpu_scratch_size,
7360 &info);
7361 if (err)
7362 return err;
7363
7364 /* Now startup the cpu. */
7365 tw32(cpu_base + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007366 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007367
7368 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007369 if (tr32(cpu_base + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007370 break;
7371 tw32(cpu_base + CPU_STATE, 0xffffffff);
7372 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007373 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007374 udelay(1000);
7375 }
7376 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007377 netdev_err(tp->dev,
7378 "%s fails to set CPU PC, is %08x should be %08x\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007379 __func__, tr32(cpu_base + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007380 return -ENODEV;
7381 }
7382 tw32(cpu_base + CPU_STATE, 0xffffffff);
7383 tw32_f(cpu_base + CPU_MODE, 0x00000000);
7384 return 0;
7385}
7386
Linus Torvalds1da177e2005-04-16 15:20:36 -07007387
Linus Torvalds1da177e2005-04-16 15:20:36 -07007388static int tg3_set_mac_addr(struct net_device *dev, void *p)
7389{
7390 struct tg3 *tp = netdev_priv(dev);
7391 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07007392 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007393
Michael Chanf9804dd2005-09-27 12:13:10 -07007394 if (!is_valid_ether_addr(addr->sa_data))
7395 return -EINVAL;
7396
Linus Torvalds1da177e2005-04-16 15:20:36 -07007397 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7398
Michael Chane75f7c92006-03-20 21:33:26 -08007399 if (!netif_running(dev))
7400 return 0;
7401
Michael Chan58712ef2006-04-29 18:58:01 -07007402 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) {
Michael Chan986e0ae2007-05-05 12:10:20 -07007403 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07007404
Michael Chan986e0ae2007-05-05 12:10:20 -07007405 addr0_high = tr32(MAC_ADDR_0_HIGH);
7406 addr0_low = tr32(MAC_ADDR_0_LOW);
7407 addr1_high = tr32(MAC_ADDR_1_HIGH);
7408 addr1_low = tr32(MAC_ADDR_1_LOW);
7409
7410 /* Skip MAC addr 1 if ASF is using it. */
7411 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
7412 !(addr1_high == 0 && addr1_low == 0))
7413 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07007414 }
Michael Chan986e0ae2007-05-05 12:10:20 -07007415 spin_lock_bh(&tp->lock);
7416 __tg3_set_mac_addr(tp, skip_mac_1);
7417 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007418
Michael Chanb9ec6c12006-07-25 16:37:27 -07007419 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007420}
7421
7422/* tp->lock is held. */
7423static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
7424 dma_addr_t mapping, u32 maxlen_flags,
7425 u32 nic_addr)
7426{
7427 tg3_write_mem(tp,
7428 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
7429 ((u64) mapping >> 32));
7430 tg3_write_mem(tp,
7431 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
7432 ((u64) mapping & 0xffffffff));
7433 tg3_write_mem(tp,
7434 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
7435 maxlen_flags);
7436
7437 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7438 tg3_write_mem(tp,
7439 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
7440 nic_addr);
7441}
7442
7443static void __tg3_set_rx_mode(struct net_device *);
Michael Chand244c892005-07-05 14:42:33 -07007444static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07007445{
Matt Carlsonb6080e12009-09-01 13:12:00 +00007446 int i;
7447
Matt Carlson19cfaec2009-12-03 08:36:20 +00007448 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007449 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
7450 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
7451 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007452 } else {
7453 tw32(HOSTCC_TXCOL_TICKS, 0);
7454 tw32(HOSTCC_TXMAX_FRAMES, 0);
7455 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007456 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007457
Matt Carlson20d73752010-07-11 09:31:41 +00007458 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00007459 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
7460 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
7461 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
7462 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007463 tw32(HOSTCC_RXCOL_TICKS, 0);
7464 tw32(HOSTCC_RXMAX_FRAMES, 0);
7465 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07007466 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007467
David S. Miller15f98502005-05-18 22:49:26 -07007468 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7469 u32 val = ec->stats_block_coalesce_usecs;
7470
Matt Carlsonb6080e12009-09-01 13:12:00 +00007471 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
7472 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
7473
David S. Miller15f98502005-05-18 22:49:26 -07007474 if (!netif_carrier_ok(tp->dev))
7475 val = 0;
7476
7477 tw32(HOSTCC_STAT_COAL_TICKS, val);
7478 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007479
7480 for (i = 0; i < tp->irq_cnt - 1; i++) {
7481 u32 reg;
7482
7483 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
7484 tw32(reg, ec->rx_coalesce_usecs);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007485 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
7486 tw32(reg, ec->rx_max_coalesced_frames);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007487 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
7488 tw32(reg, ec->rx_max_coalesced_frames_irq);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007489
7490 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS) {
7491 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
7492 tw32(reg, ec->tx_coalesce_usecs);
7493 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
7494 tw32(reg, ec->tx_max_coalesced_frames);
7495 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
7496 tw32(reg, ec->tx_max_coalesced_frames_irq);
7497 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007498 }
7499
7500 for (; i < tp->irq_max - 1; i++) {
7501 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007502 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007503 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007504
7505 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS) {
7506 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
7507 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
7508 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
7509 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007510 }
David S. Miller15f98502005-05-18 22:49:26 -07007511}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007512
7513/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00007514static void tg3_rings_reset(struct tg3 *tp)
7515{
7516 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007517 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007518 struct tg3_napi *tnapi = &tp->napi[0];
7519
7520 /* Disable all transmit rings but the first. */
7521 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7522 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00007523 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
7524 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007525 else
7526 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7527
7528 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7529 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
7530 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
7531 BDINFO_FLAGS_DISABLED);
7532
7533
7534 /* Disable all receive return rings but the first. */
Matt Carlsona50d0792010-06-05 17:24:37 +00007535 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
7536 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007537 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
7538 else if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00007539 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00007540 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
7541 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson2d31eca2009-09-01 12:53:31 +00007542 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
7543 else
7544 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7545
7546 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7547 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
7548 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
7549 BDINFO_FLAGS_DISABLED);
7550
7551 /* Disable interrupts */
7552 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
7553
7554 /* Zero mailbox registers. */
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007555 if (tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX) {
7556 for (i = 1; i < TG3_IRQ_MAX_VECS; i++) {
7557 tp->napi[i].tx_prod = 0;
7558 tp->napi[i].tx_cons = 0;
Matt Carlsonc2353a32010-01-20 16:58:08 +00007559 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
7560 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007561 tw32_rx_mbox(tp->napi[i].consmbox, 0);
7562 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
7563 }
Matt Carlsonc2353a32010-01-20 16:58:08 +00007564 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS))
7565 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007566 } else {
7567 tp->napi[0].tx_prod = 0;
7568 tp->napi[0].tx_cons = 0;
7569 tw32_mailbox(tp->napi[0].prodmbox, 0);
7570 tw32_rx_mbox(tp->napi[0].consmbox, 0);
7571 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007572
7573 /* Make sure the NIC-based send BD rings are disabled. */
7574 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7575 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
7576 for (i = 0; i < 16; i++)
7577 tw32_tx_mbox(mbox + i * 8, 0);
7578 }
7579
7580 txrcb = NIC_SRAM_SEND_RCB;
7581 rxrcb = NIC_SRAM_RCV_RET_RCB;
7582
7583 /* Clear status block in ram. */
7584 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7585
7586 /* Set status block DMA address */
7587 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
7588 ((u64) tnapi->status_mapping >> 32));
7589 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
7590 ((u64) tnapi->status_mapping & 0xffffffff));
7591
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007592 if (tnapi->tx_ring) {
7593 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7594 (TG3_TX_RING_SIZE <<
7595 BDINFO_FLAGS_MAXLEN_SHIFT),
7596 NIC_SRAM_TX_BUFFER_DESC);
7597 txrcb += TG3_BDINFO_SIZE;
7598 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007599
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007600 if (tnapi->rx_rcb) {
7601 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
7602 (TG3_RX_RCB_RING_SIZE(tp) <<
7603 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
7604 rxrcb += TG3_BDINFO_SIZE;
7605 }
7606
7607 stblk = HOSTCC_STATBLCK_RING1;
7608
7609 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
7610 u64 mapping = (u64)tnapi->status_mapping;
7611 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
7612 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
7613
7614 /* Clear status block in ram. */
7615 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7616
Matt Carlson19cfaec2009-12-03 08:36:20 +00007617 if (tnapi->tx_ring) {
7618 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7619 (TG3_TX_RING_SIZE <<
7620 BDINFO_FLAGS_MAXLEN_SHIFT),
7621 NIC_SRAM_TX_BUFFER_DESC);
7622 txrcb += TG3_BDINFO_SIZE;
7623 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007624
7625 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
7626 (TG3_RX_RCB_RING_SIZE(tp) <<
7627 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
7628
7629 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007630 rxrcb += TG3_BDINFO_SIZE;
7631 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007632}
7633
7634/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07007635static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007636{
7637 u32 val, rdmac_mode;
7638 int i, err, limit;
Matt Carlson21f581a2009-08-28 14:00:25 +00007639 struct tg3_rx_prodring_set *tpr = &tp->prodring[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007640
7641 tg3_disable_ints(tp);
7642
7643 tg3_stop_fw(tp);
7644
7645 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
7646
Matt Carlson859a588792010-04-05 10:19:28 +00007647 if (tp->tg3_flags & TG3_FLAG_INIT_COMPLETE)
Michael Chane6de8ad2005-05-05 14:42:41 -07007648 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007649
Matt Carlson603f1172010-02-12 14:47:10 +00007650 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08007651 tg3_phy_reset(tp);
7652
Linus Torvalds1da177e2005-04-16 15:20:36 -07007653 err = tg3_chip_reset(tp);
7654 if (err)
7655 return err;
7656
7657 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
7658
Matt Carlsonbcb37f62008-11-03 16:52:09 -08007659 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07007660 val = tr32(TG3_CPMU_CTRL);
7661 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
7662 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08007663
7664 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
7665 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
7666 val |= CPMU_LSPD_10MB_MACCLK_6_25;
7667 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
7668
7669 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
7670 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
7671 val |= CPMU_LNK_AWARE_MACCLK_6_25;
7672 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
7673
7674 val = tr32(TG3_CPMU_HST_ACC);
7675 val &= ~CPMU_HST_ACC_MACCLK_MASK;
7676 val |= CPMU_HST_ACC_MACCLK_6_25;
7677 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07007678 }
7679
Matt Carlson33466d92009-04-20 06:57:41 +00007680 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7681 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
7682 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
7683 PCIE_PWR_MGMT_L1_THRESH_4MS;
7684 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00007685
7686 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
7687 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
7688
7689 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00007690
Matt Carlsonf40386c2009-11-02 14:24:02 +00007691 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7692 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00007693 }
7694
Matt Carlson614b0592010-01-20 16:58:02 +00007695 if (tp->tg3_flags3 & TG3_FLG3_L1PLLPD_EN) {
7696 u32 grc_mode = tr32(GRC_MODE);
7697
7698 /* Access the lower 1K of PL PCIE block registers. */
7699 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
7700 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
7701
7702 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
7703 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
7704 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
7705
7706 tw32(GRC_MODE, grc_mode);
7707 }
7708
Matt Carlsoncea46462010-04-12 06:58:24 +00007709 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
7710 u32 grc_mode = tr32(GRC_MODE);
7711
7712 /* Access the lower 1K of PL PCIE block registers. */
7713 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
7714 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
7715
7716 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5);
7717 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
7718 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
7719
7720 tw32(GRC_MODE, grc_mode);
Matt Carlsona977dbe2010-04-12 06:58:26 +00007721
7722 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
7723 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
7724 val |= CPMU_LSPD_10MB_MACCLK_6_25;
7725 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00007726 }
7727
Linus Torvalds1da177e2005-04-16 15:20:36 -07007728 /* This works around an issue with Athlon chipsets on
7729 * B3 tigon3 silicon. This bit has no effect on any
7730 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07007731 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007732 */
Matt Carlson795d01c2007-10-07 23:28:17 -07007733 if (!(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT)) {
7734 if (!(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
7735 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
7736 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007738
7739 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
7740 (tp->tg3_flags & TG3_FLAG_PCIX_MODE)) {
7741 val = tr32(TG3PCI_PCISTATE);
7742 val |= PCISTATE_RETRY_SAME_DMA;
7743 tw32(TG3PCI_PCISTATE, val);
7744 }
7745
Matt Carlson0d3031d2007-10-10 18:02:43 -07007746 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
7747 /* Allow reads and writes to the
7748 * APE register and memory space.
7749 */
7750 val = tr32(TG3PCI_PCISTATE);
7751 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00007752 PCISTATE_ALLOW_APE_SHMEM_WR |
7753 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07007754 tw32(TG3PCI_PCISTATE, val);
7755 }
7756
Linus Torvalds1da177e2005-04-16 15:20:36 -07007757 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
7758 /* Enable some hw fixes. */
7759 val = tr32(TG3PCI_MSI_DATA);
7760 val |= (1 << 26) | (1 << 28) | (1 << 29);
7761 tw32(TG3PCI_MSI_DATA, val);
7762 }
7763
7764 /* Descriptor ring init may make accesses to the
7765 * NIC SRAM area to setup the TX descriptors, so we
7766 * can only do this after the hardware has been
7767 * successfully reset.
7768 */
Michael Chan32d8c572006-07-25 16:38:29 -07007769 err = tg3_init_rings(tp);
7770 if (err)
7771 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007772
Matt Carlsonb703df62009-12-03 08:36:21 +00007773 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsona50d0792010-06-05 17:24:37 +00007774 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlsonb703df62009-12-03 08:36:21 +00007775 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00007776 val = tr32(TG3PCI_DMA_RW_CTRL) &
7777 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Matt Carlson1a319022010-04-12 06:58:25 +00007778 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
7779 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00007780 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
7781 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
7782 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07007783 /* This value is determined during the probe time DMA
7784 * engine test, tg3_test_dma.
7785 */
7786 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
7787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007788
7789 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
7790 GRC_MODE_4X_NIC_SEND_RINGS |
7791 GRC_MODE_NO_TX_PHDR_CSUM |
7792 GRC_MODE_NO_RX_PHDR_CSUM);
7793 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07007794
7795 /* Pseudo-header checksum is done by hardware logic and not
7796 * the offload processers, so make the chip do the pseudo-
7797 * header checksums on receive. For transmit it is more
7798 * convenient to do the pseudo-header checksum in software
7799 * as Linux does that on transmit for us in all cases.
7800 */
7801 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007802
7803 tw32(GRC_MODE,
7804 tp->grc_mode |
7805 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
7806
7807 /* Setup the timer prescalar register. Clock is always 66Mhz. */
7808 val = tr32(GRC_MISC_CFG);
7809 val &= ~0xff;
7810 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
7811 tw32(GRC_MISC_CFG, val);
7812
7813 /* Initialize MBUF/DESC pool. */
John W. Linvillecbf46852005-04-21 17:01:29 -07007814 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007815 /* Do nothing. */
7816 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
7817 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
7818 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
7819 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
7820 else
7821 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
7822 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
7823 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Matt Carlson859a588792010-04-05 10:19:28 +00007824 } else if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007825 int fw_len;
7826
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007827 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007828 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
7829 tw32(BUFMGR_MB_POOL_ADDR,
7830 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
7831 tw32(BUFMGR_MB_POOL_SIZE,
7832 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
7833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007834
Michael Chan0f893dc2005-07-25 12:30:38 -07007835 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007836 tw32(BUFMGR_MB_RDMA_LOW_WATER,
7837 tp->bufmgr_config.mbuf_read_dma_low_water);
7838 tw32(BUFMGR_MB_MACRX_LOW_WATER,
7839 tp->bufmgr_config.mbuf_mac_rx_low_water);
7840 tw32(BUFMGR_MB_HIGH_WATER,
7841 tp->bufmgr_config.mbuf_high_water);
7842 } else {
7843 tw32(BUFMGR_MB_RDMA_LOW_WATER,
7844 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
7845 tw32(BUFMGR_MB_MACRX_LOW_WATER,
7846 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
7847 tw32(BUFMGR_MB_HIGH_WATER,
7848 tp->bufmgr_config.mbuf_high_water_jumbo);
7849 }
7850 tw32(BUFMGR_DMA_LOW_WATER,
7851 tp->bufmgr_config.dma_low_water);
7852 tw32(BUFMGR_DMA_HIGH_WATER,
7853 tp->bufmgr_config.dma_high_water);
7854
7855 tw32(BUFMGR_MODE, BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE);
7856 for (i = 0; i < 2000; i++) {
7857 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
7858 break;
7859 udelay(10);
7860 }
7861 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00007862 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007863 return -ENODEV;
7864 }
7865
7866 /* Setup replenish threshold. */
Michael Chanf92905d2006-06-29 20:14:29 -07007867 val = tp->rx_pending / 8;
7868 if (val == 0)
7869 val = 1;
7870 else if (val > tp->rx_std_max_post)
7871 val = tp->rx_std_max_post;
Michael Chanb5d37722006-09-27 16:06:21 -07007872 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7873 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
7874 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
7875
7876 if (val > (TG3_RX_INTERNAL_RING_SZ_5906 / 2))
7877 val = TG3_RX_INTERNAL_RING_SZ_5906 / 2;
7878 }
Michael Chanf92905d2006-06-29 20:14:29 -07007879
7880 tw32(RCVBDI_STD_THRESH, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007881
7882 /* Initialize TG3_BDINFO's at:
7883 * RCVDBDI_STD_BD: standard eth size rx ring
7884 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
7885 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
7886 *
7887 * like so:
7888 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
7889 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
7890 * ring attribute flags
7891 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
7892 *
7893 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
7894 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
7895 *
7896 * The size of each ring is fixed in the firmware, but the location is
7897 * configurable.
7898 */
7899 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00007900 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007901 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00007902 ((u64) tpr->rx_std_mapping & 0xffffffff));
Matt Carlsona50d0792010-06-05 17:24:37 +00007903 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
7904 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5719)
Matt Carlson87668d32009-11-13 13:03:34 +00007905 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
7906 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007907
Matt Carlsonfdb72b32009-08-28 13:57:12 +00007908 /* Disable the mini ring */
7909 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007910 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
7911 BDINFO_FLAGS_DISABLED);
7912
Matt Carlsonfdb72b32009-08-28 13:57:12 +00007913 /* Program the jumbo buffer descriptor ring control
7914 * blocks on those devices that have them.
7915 */
Matt Carlson8f666b02009-08-28 13:58:24 +00007916 if ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) &&
Matt Carlsonfdb72b32009-08-28 13:57:12 +00007917 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007918 /* Setup replenish threshold. */
7919 tw32(RCVBDI_JUMBO_THRESH, tp->rx_jumbo_pending / 8);
7920
Michael Chan0f893dc2005-07-25 12:30:38 -07007921 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007922 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00007923 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007924 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00007925 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007926 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlson79ed5ac2009-08-28 14:00:55 +00007927 (RX_JUMBO_MAX_SIZE << BDINFO_FLAGS_MAXLEN_SHIFT) |
7928 BDINFO_FLAGS_USE_EXT_RECV);
Matt Carlsona50d0792010-06-05 17:24:37 +00007929 if (!(tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) ||
7930 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson87668d32009-11-13 13:03:34 +00007931 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
7932 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007933 } else {
7934 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
7935 BDINFO_FLAGS_DISABLED);
7936 }
7937
Matt Carlsonb703df62009-12-03 08:36:21 +00007938 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsona50d0792010-06-05 17:24:37 +00007939 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlsonb703df62009-12-03 08:36:21 +00007940 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007941 val = (RX_STD_MAX_SIZE_5705 << BDINFO_FLAGS_MAXLEN_SHIFT) |
Matt Carlson04380d42010-04-12 06:58:29 +00007942 (TG3_RX_STD_DMA_SZ << 2);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007943 else
Matt Carlson04380d42010-04-12 06:58:29 +00007944 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00007945 } else
7946 val = RX_STD_MAX_SIZE_5705 << BDINFO_FLAGS_MAXLEN_SHIFT;
7947
7948 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007949
Matt Carlson411da642009-11-13 13:03:46 +00007950 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e62009-11-13 13:03:49 +00007951 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007952
Matt Carlson411da642009-11-13 13:03:46 +00007953 tpr->rx_jmb_prod_idx = (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) ?
Matt Carlson21f581a2009-08-28 14:00:25 +00007954 tp->rx_jumbo_pending : 0;
Matt Carlson66711e62009-11-13 13:03:49 +00007955 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007956
Matt Carlsonb703df62009-12-03 08:36:21 +00007957 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsona50d0792010-06-05 17:24:37 +00007958 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlsonb703df62009-12-03 08:36:21 +00007959 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007960 tw32(STD_REPLENISH_LWM, 32);
7961 tw32(JMB_REPLENISH_LWM, 16);
7962 }
7963
Matt Carlson2d31eca2009-09-01 12:53:31 +00007964 tg3_rings_reset(tp);
7965
Linus Torvalds1da177e2005-04-16 15:20:36 -07007966 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07007967 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007968
7969 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00007970 tw32(MAC_RX_MTU_SIZE,
7971 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007972
7973 /* The slot time is changed by tg3_setup_phy if we
7974 * run at gigabit with half duplex.
7975 */
7976 tw32(MAC_TX_LENGTHS,
7977 (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
7978 (6 << TX_LENGTHS_IPG_SHIFT) |
7979 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
7980
7981 /* Receive rules. */
7982 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
7983 tw32(RCVLPC_CONFIG, 0x0181);
7984
7985 /* Calculate RDMAC_MODE setting early, we need it to determine
7986 * the RCVLPC_STATE_ENABLE mask.
7987 */
7988 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
7989 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
7990 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
7991 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
7992 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07007993
Matt Carlsona50d0792010-06-05 17:24:37 +00007994 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
7995 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlson0339e4e2010-02-12 14:47:09 +00007996 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
7997
Matt Carlson57e69832008-05-25 23:48:31 -07007998 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08007999 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8000 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07008001 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
8002 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
8003 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
8004
Michael Chan85e94ce2005-04-21 17:05:28 -07008005 /* If statement applies to 5705 and 5750 PCI devices only */
8006 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8007 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) ||
8008 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008009 if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07008010 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008011 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
8012 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
8013 !(tp->tg3_flags2 & TG3_FLG2_IS_5788)) {
8014 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8015 }
8016 }
8017
Michael Chan85e94ce2005-04-21 17:05:28 -07008018 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)
8019 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8020
Linus Torvalds1da177e2005-04-16 15:20:36 -07008021 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
Matt Carlson027455a2008-12-21 20:19:30 -08008022 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
8023
Matt Carlsone849cdc2009-11-13 13:03:38 +00008024 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
8025 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson027455a2008-12-21 20:19:30 -08008026 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
8027 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008028
8029 /* Receive/send statistics. */
Michael Chan16613942006-06-29 20:15:13 -07008030 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
8031 val = tr32(RCVLPC_STATS_ENABLE);
8032 val &= ~RCVLPC_STATSENAB_DACK_FIX;
8033 tw32(RCVLPC_STATS_ENABLE, val);
8034 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
8035 (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008036 val = tr32(RCVLPC_STATS_ENABLE);
8037 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
8038 tw32(RCVLPC_STATS_ENABLE, val);
8039 } else {
8040 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
8041 }
8042 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
8043 tw32(SNDDATAI_STATSENAB, 0xffffff);
8044 tw32(SNDDATAI_STATSCTRL,
8045 (SNDDATAI_SCTRL_ENABLE |
8046 SNDDATAI_SCTRL_FASTUPD));
8047
8048 /* Setup host coalescing engine. */
8049 tw32(HOSTCC_MODE, 0);
8050 for (i = 0; i < 2000; i++) {
8051 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
8052 break;
8053 udelay(10);
8054 }
8055
Michael Chand244c892005-07-05 14:42:33 -07008056 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008057
Linus Torvalds1da177e2005-04-16 15:20:36 -07008058 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
8059 /* Status/statistics block address. See tg3_timer,
8060 * the tg3_periodic_fetch_stats call there, and
8061 * tg3_get_stats to see how this works for 5705/5750 chips.
8062 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008063 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8064 ((u64) tp->stats_mapping >> 32));
8065 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8066 ((u64) tp->stats_mapping & 0xffffffff));
8067 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008068
Linus Torvalds1da177e2005-04-16 15:20:36 -07008069 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008070
8071 /* Clear statistics and status block memory areas */
8072 for (i = NIC_SRAM_STATS_BLK;
8073 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
8074 i += sizeof(u32)) {
8075 tg3_write_mem(tp, i, 0);
8076 udelay(40);
8077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008078 }
8079
8080 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
8081
8082 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
8083 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
8084 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
8085 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
8086
Michael Chanc94e3942005-09-27 12:12:42 -07008087 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES) {
8088 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
8089 /* reset to prevent losing 1st rx packet intermittently */
8090 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8091 udelay(10);
8092 }
8093
Matt Carlson3bda1252008-08-15 14:08:22 -07008094 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
8095 tp->mac_mode &= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
8096 else
8097 tp->mac_mode = 0;
8098 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Linus Torvalds1da177e2005-04-16 15:20:36 -07008099 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE | MAC_MODE_FHDE_ENABLE;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07008100 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
8101 !(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
8102 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
8103 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008104 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
8105 udelay(40);
8106
Michael Chan314fba32005-04-21 17:07:04 -07008107 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Michael Chan9d26e212006-12-07 00:21:14 -08008108 * If TG3_FLG2_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07008109 * register to preserve the GPIO settings for LOMs. The GPIOs,
8110 * whether used as inputs or outputs, are set by boot code after
8111 * reset.
8112 */
Michael Chan9d26e212006-12-07 00:21:14 -08008113 if (!(tp->tg3_flags2 & TG3_FLG2_IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07008114 u32 gpio_mask;
8115
Michael Chan9d26e212006-12-07 00:21:14 -08008116 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
8117 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
8118 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07008119
8120 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
8121 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
8122 GRC_LCLCTRL_GPIO_OUTPUT3;
8123
Michael Chanaf36e6b2006-03-23 01:28:06 -08008124 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
8125 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
8126
Gary Zambranoaaf84462007-05-05 11:51:45 -07008127 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07008128 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
8129
8130 /* GPIO1 must be driven high for eeprom write protect */
Michael Chan9d26e212006-12-07 00:21:14 -08008131 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT)
8132 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
8133 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07008134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008135 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8136 udelay(100);
8137
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008138 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX) {
8139 val = tr32(MSGINT_MODE);
8140 val |= MSGINT_MODE_MULTIVEC_EN | MSGINT_MODE_ENABLE;
8141 tw32(MSGINT_MODE, val);
8142 }
8143
Linus Torvalds1da177e2005-04-16 15:20:36 -07008144 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
8145 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
8146 udelay(40);
8147 }
8148
8149 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
8150 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
8151 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
8152 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
8153 WDMAC_MODE_LNGREAD_ENAB);
8154
Michael Chan85e94ce2005-04-21 17:05:28 -07008155 /* If statement applies to 5705 and 5750 PCI devices only */
8156 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8157 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) ||
8158 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750) {
Matt Carlson29ea0952009-08-25 10:07:54 +00008159 if ((tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008160 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
8161 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
8162 /* nothing */
8163 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
8164 !(tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
8165 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)) {
8166 val |= WDMAC_MODE_RX_ACCEL;
8167 }
8168 }
8169
Michael Chand9ab5ad2006-03-20 22:27:35 -08008170 /* Enable host coalescing bug fix */
Matt Carlson321d32a2008-11-21 17:22:19 -08008171 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Matt Carlsonf51f3562008-05-25 23:45:08 -07008172 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad2006-03-20 22:27:35 -08008173
Matt Carlson788a0352009-11-02 14:26:03 +00008174 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
8175 val |= WDMAC_MODE_BURST_ALL_DATA;
8176
Linus Torvalds1da177e2005-04-16 15:20:36 -07008177 tw32_f(WDMAC_MODE, val);
8178 udelay(40);
8179
Matt Carlson9974a352007-10-07 23:27:28 -07008180 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
8181 u16 pcix_cmd;
8182
8183 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8184 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008185 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07008186 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
8187 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008188 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07008189 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
8190 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008191 }
Matt Carlson9974a352007-10-07 23:27:28 -07008192 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8193 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008194 }
8195
8196 tw32_f(RDMAC_MODE, rdmac_mode);
8197 udelay(40);
8198
8199 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
8200 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
8201 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07008202
8203 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
8204 tw32(SNDDATAC_MODE,
8205 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
8206 else
8207 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
8208
Linus Torvalds1da177e2005-04-16 15:20:36 -07008209 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
8210 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
8211 tw32(RCVDBDI_MODE, RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ);
8212 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008213 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
8214 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008215 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Matt Carlson19cfaec2009-12-03 08:36:20 +00008216 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008217 val |= SNDBDI_MODE_MULTI_TXQ_EN;
8218 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008219 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
8220
8221 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
8222 err = tg3_load_5701_a0_firmware_fix(tp);
8223 if (err)
8224 return err;
8225 }
8226
Linus Torvalds1da177e2005-04-16 15:20:36 -07008227 if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) {
8228 err = tg3_load_tso_firmware(tp);
8229 if (err)
8230 return err;
8231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008232
8233 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonb1d05212010-06-05 17:24:31 +00008234 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
8235 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
8236 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008237 tw32_f(MAC_TX_MODE, tp->tx_mode);
8238 udelay(100);
8239
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008240 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS) {
8241 u32 reg = MAC_RSS_INDIR_TBL_0;
8242 u8 *ent = (u8 *)&val;
8243
8244 /* Setup the indirection table */
8245 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
8246 int idx = i % sizeof(val);
8247
Matt Carlson5efeeea2010-07-11 09:31:40 +00008248 ent[idx] = i % (tp->irq_cnt - 1);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008249 if (idx == sizeof(val) - 1) {
8250 tw32(reg, val);
8251 reg += 4;
8252 }
8253 }
8254
8255 /* Setup the "secret" hash key. */
8256 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
8257 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
8258 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
8259 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
8260 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
8261 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
8262 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
8263 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
8264 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
8265 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
8266 }
8267
Linus Torvalds1da177e2005-04-16 15:20:36 -07008268 tp->rx_mode = RX_MODE_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08008269 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Michael Chanaf36e6b2006-03-23 01:28:06 -08008270 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
8271
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008272 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)
8273 tp->rx_mode |= RX_MODE_RSS_ENABLE |
8274 RX_MODE_RSS_ITBL_HASH_BITS_7 |
8275 RX_MODE_RSS_IPV6_HASH_EN |
8276 RX_MODE_RSS_TCP_IPV6_HASH_EN |
8277 RX_MODE_RSS_IPV4_HASH_EN |
8278 RX_MODE_RSS_TCP_IPV4_HASH_EN;
8279
Linus Torvalds1da177e2005-04-16 15:20:36 -07008280 tw32_f(MAC_RX_MODE, tp->rx_mode);
8281 udelay(10);
8282
Linus Torvalds1da177e2005-04-16 15:20:36 -07008283 tw32(MAC_LED_CTRL, tp->led_ctrl);
8284
8285 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Michael Chanc94e3942005-09-27 12:12:42 -07008286 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008287 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8288 udelay(10);
8289 }
8290 tw32_f(MAC_RX_MODE, tp->rx_mode);
8291 udelay(10);
8292
8293 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
8294 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
8295 !(tp->tg3_flags2 & TG3_FLG2_SERDES_PREEMPHASIS)) {
8296 /* Set drive transmission level to 1.2V */
8297 /* only if the signal pre-emphasis bit is not set */
8298 val = tr32(MAC_SERDES_CFG);
8299 val &= 0xfffff000;
8300 val |= 0x880;
8301 tw32(MAC_SERDES_CFG, val);
8302 }
8303 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
8304 tw32(MAC_SERDES_CFG, 0x616000);
8305 }
8306
8307 /* Prevent chip from dropping frames when flow control
8308 * is enabled.
8309 */
Matt Carlson666bc832010-01-20 16:58:03 +00008310 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8311 val = 1;
8312 else
8313 val = 2;
8314 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008315
8316 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
8317 (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)) {
8318 /* Use hardware link auto-negotiation */
8319 tp->tg3_flags2 |= TG3_FLG2_HW_AUTONEG;
8320 }
8321
Michael Chand4d2c552006-03-20 17:47:20 -08008322 if ((tp->tg3_flags2 & TG3_FLG2_MII_SERDES) &&
8323 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)) {
8324 u32 tmp;
8325
8326 tmp = tr32(SERDES_RX_CTRL);
8327 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
8328 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
8329 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
8330 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8331 }
8332
Matt Carlsondd477002008-05-25 23:45:58 -07008333 if (!(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)) {
8334 if (tp->link_config.phy_is_low_power) {
8335 tp->link_config.phy_is_low_power = 0;
8336 tp->link_config.speed = tp->link_config.orig_speed;
8337 tp->link_config.duplex = tp->link_config.orig_duplex;
8338 tp->link_config.autoneg = tp->link_config.orig_autoneg;
8339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008340
Matt Carlsondd477002008-05-25 23:45:58 -07008341 err = tg3_setup_phy(tp, 0);
8342 if (err)
8343 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008344
Matt Carlsondd477002008-05-25 23:45:58 -07008345 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
Matt Carlson7f97a4b2009-08-25 10:10:03 +00008346 !(tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07008347 u32 tmp;
8348
8349 /* Clear CRC stats. */
8350 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
8351 tg3_writephy(tp, MII_TG3_TEST1,
8352 tmp | MII_TG3_TEST1_CRC_EN);
8353 tg3_readphy(tp, 0x14, &tmp);
8354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008355 }
8356 }
8357
8358 __tg3_set_rx_mode(tp->dev);
8359
8360 /* Initialize receive rules. */
8361 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
8362 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
8363 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
8364 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
8365
Michael Chan4cf78e42005-07-25 12:29:19 -07008366 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Michael Chana4e2b342005-10-26 15:46:52 -07008367 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008368 limit = 8;
8369 else
8370 limit = 16;
8371 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF)
8372 limit -= 4;
8373 switch (limit) {
8374 case 16:
8375 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
8376 case 15:
8377 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
8378 case 14:
8379 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
8380 case 13:
8381 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
8382 case 12:
8383 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
8384 case 11:
8385 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
8386 case 10:
8387 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
8388 case 9:
8389 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
8390 case 8:
8391 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
8392 case 7:
8393 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
8394 case 6:
8395 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
8396 case 5:
8397 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
8398 case 4:
8399 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
8400 case 3:
8401 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
8402 case 2:
8403 case 1:
8404
8405 default:
8406 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07008407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008408
Matt Carlson9ce768e2007-10-11 19:49:11 -07008409 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
8410 /* Write our heartbeat update interval to APE. */
8411 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
8412 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07008413
Linus Torvalds1da177e2005-04-16 15:20:36 -07008414 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
8415
Linus Torvalds1da177e2005-04-16 15:20:36 -07008416 return 0;
8417}
8418
8419/* Called at device open time to get the chip ready for
8420 * packet processing. Invoked with tp->lock held.
8421 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008422static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008423{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008424 tg3_switch_clocks(tp);
8425
8426 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
8427
Matt Carlson2f751b62008-08-04 23:17:34 -07008428 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008429}
8430
8431#define TG3_STAT_ADD32(PSTAT, REG) \
8432do { u32 __val = tr32(REG); \
8433 (PSTAT)->low += __val; \
8434 if ((PSTAT)->low < __val) \
8435 (PSTAT)->high += 1; \
8436} while (0)
8437
8438static void tg3_periodic_fetch_stats(struct tg3 *tp)
8439{
8440 struct tg3_hw_stats *sp = tp->hw_stats;
8441
8442 if (!netif_carrier_ok(tp->dev))
8443 return;
8444
8445 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
8446 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
8447 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
8448 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
8449 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
8450 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
8451 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
8452 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
8453 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
8454 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
8455 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
8456 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
8457 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
8458
8459 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
8460 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
8461 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
8462 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
8463 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
8464 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
8465 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
8466 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
8467 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
8468 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
8469 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
8470 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
8471 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
8472 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07008473
8474 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
8475 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
8476 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008477}
8478
8479static void tg3_timer(unsigned long __opaque)
8480{
8481 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008482
Michael Chanf475f162006-03-27 23:20:14 -08008483 if (tp->irq_sync)
8484 goto restart_timer;
8485
David S. Millerf47c11e2005-06-24 20:18:35 -07008486 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008487
David S. Millerfac9b832005-05-18 22:46:34 -07008488 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)) {
8489 /* All of this garbage is because when using non-tagged
8490 * IRQ status the mailbox/status_block protocol the chip
8491 * uses with the cpu is race prone.
8492 */
Matt Carlson898a56f2009-08-28 14:02:40 +00008493 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -07008494 tw32(GRC_LOCAL_CTRL,
8495 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
8496 } else {
8497 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00008498 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -07008499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008500
David S. Millerfac9b832005-05-18 22:46:34 -07008501 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
8502 tp->tg3_flags2 |= TG3_FLG2_RESTART_TIMER;
David S. Millerf47c11e2005-06-24 20:18:35 -07008503 spin_unlock(&tp->lock);
David S. Millerfac9b832005-05-18 22:46:34 -07008504 schedule_work(&tp->reset_task);
8505 return;
8506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008507 }
8508
Linus Torvalds1da177e2005-04-16 15:20:36 -07008509 /* This part only runs once per second. */
8510 if (!--tp->timer_counter) {
David S. Millerfac9b832005-05-18 22:46:34 -07008511 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
8512 tg3_periodic_fetch_stats(tp);
8513
Linus Torvalds1da177e2005-04-16 15:20:36 -07008514 if (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) {
8515 u32 mac_stat;
8516 int phy_event;
8517
8518 mac_stat = tr32(MAC_STATUS);
8519
8520 phy_event = 0;
8521 if (tp->tg3_flags & TG3_FLAG_USE_MI_INTERRUPT) {
8522 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
8523 phy_event = 1;
8524 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
8525 phy_event = 1;
8526
8527 if (phy_event)
8528 tg3_setup_phy(tp, 0);
8529 } else if (tp->tg3_flags & TG3_FLAG_POLL_SERDES) {
8530 u32 mac_stat = tr32(MAC_STATUS);
8531 int need_setup = 0;
8532
8533 if (netif_carrier_ok(tp->dev) &&
8534 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
8535 need_setup = 1;
8536 }
8537 if (! netif_carrier_ok(tp->dev) &&
8538 (mac_stat & (MAC_STATUS_PCS_SYNCED |
8539 MAC_STATUS_SIGNAL_DET))) {
8540 need_setup = 1;
8541 }
8542 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07008543 if (!tp->serdes_counter) {
8544 tw32_f(MAC_MODE,
8545 (tp->mac_mode &
8546 ~MAC_MODE_PORT_MODE_MASK));
8547 udelay(40);
8548 tw32_f(MAC_MODE, tp->mac_mode);
8549 udelay(40);
8550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008551 tg3_setup_phy(tp, 0);
8552 }
Matt Carlson57d8b882010-06-05 17:24:35 +00008553 } else if ((tp->tg3_flags2 & TG3_FLG2_MII_SERDES) &&
8554 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -07008555 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +00008556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008557
8558 tp->timer_counter = tp->timer_multiplier;
8559 }
8560
Michael Chan130b8e42006-09-27 16:00:40 -07008561 /* Heartbeat is only sent once every 2 seconds.
8562 *
8563 * The heartbeat is to tell the ASF firmware that the host
8564 * driver is still alive. In the event that the OS crashes,
8565 * ASF needs to reset the hardware to free up the FIFO space
8566 * that may be filled with rx packets destined for the host.
8567 * If the FIFO is full, ASF will no longer function properly.
8568 *
8569 * Unintended resets have been reported on real time kernels
8570 * where the timer doesn't run on time. Netpoll will also have
8571 * same problem.
8572 *
8573 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
8574 * to check the ring condition when the heartbeat is expiring
8575 * before doing the reset. This will prevent most unintended
8576 * resets.
8577 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008578 if (!--tp->asf_counter) {
Matt Carlsonbc7959b2008-08-15 14:08:55 -07008579 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
8580 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07008581 tg3_wait_for_event_ack(tp);
8582
Michael Chanbbadf502006-04-06 21:46:34 -07008583 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07008584 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07008585 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +00008586 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
8587 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -07008588
8589 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008590 }
8591 tp->asf_counter = tp->asf_multiplier;
8592 }
8593
David S. Millerf47c11e2005-06-24 20:18:35 -07008594 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008595
Michael Chanf475f162006-03-27 23:20:14 -08008596restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07008597 tp->timer.expires = jiffies + tp->timer_offset;
8598 add_timer(&tp->timer);
8599}
8600
Matt Carlson4f125f42009-09-01 12:55:02 +00008601static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -08008602{
David Howells7d12e782006-10-05 14:55:46 +01008603 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008604 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +00008605 char *name;
8606 struct tg3_napi *tnapi = &tp->napi[irq_num];
8607
8608 if (tp->irq_cnt == 1)
8609 name = tp->dev->name;
8610 else {
8611 name = &tnapi->irq_lbl[0];
8612 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
8613 name[IFNAMSIZ-1] = 0;
8614 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08008615
Matt Carlson679563f2009-09-01 12:55:46 +00008616 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI_OR_MSIX) {
Michael Chanfcfa0a32006-03-20 22:28:41 -08008617 fn = tg3_msi;
8618 if (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)
8619 fn = tg3_msi_1shot;
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07008620 flags = IRQF_SAMPLE_RANDOM;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008621 } else {
8622 fn = tg3_interrupt;
8623 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)
8624 fn = tg3_interrupt_tagged;
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07008625 flags = IRQF_SHARED | IRQF_SAMPLE_RANDOM;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008626 }
Matt Carlson4f125f42009-09-01 12:55:02 +00008627
8628 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08008629}
8630
Michael Chan79381092005-04-21 17:13:59 -07008631static int tg3_test_interrupt(struct tg3 *tp)
8632{
Matt Carlson09943a12009-08-28 14:01:57 +00008633 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -07008634 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -07008635 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008636 u32 val;
Michael Chan79381092005-04-21 17:13:59 -07008637
Michael Chand4bc3922005-05-29 14:59:20 -07008638 if (!netif_running(dev))
8639 return -ENODEV;
8640
Michael Chan79381092005-04-21 17:13:59 -07008641 tg3_disable_ints(tp);
8642
Matt Carlson4f125f42009-09-01 12:55:02 +00008643 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07008644
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008645 /*
8646 * Turn off MSI one shot mode. Otherwise this test has no
8647 * observable way to know whether the interrupt was delivered.
8648 */
Matt Carlsonb703df62009-12-03 08:36:21 +00008649 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsona50d0792010-06-05 17:24:37 +00008650 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlsonb703df62009-12-03 08:36:21 +00008651 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008652 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
8653 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
8654 tw32(MSGINT_MODE, val);
8655 }
8656
Matt Carlson4f125f42009-09-01 12:55:02 +00008657 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Matt Carlson09943a12009-08-28 14:01:57 +00008658 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07008659 if (err)
8660 return err;
8661
Matt Carlson898a56f2009-08-28 14:02:40 +00008662 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -07008663 tg3_enable_ints(tp);
8664
8665 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00008666 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -07008667
8668 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -07008669 u32 int_mbox, misc_host_ctrl;
8670
Matt Carlson898a56f2009-08-28 14:02:40 +00008671 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -07008672 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
8673
8674 if ((int_mbox != 0) ||
8675 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
8676 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -07008677 break;
Michael Chanb16250e2006-09-27 16:10:14 -07008678 }
8679
Michael Chan79381092005-04-21 17:13:59 -07008680 msleep(10);
8681 }
8682
8683 tg3_disable_ints(tp);
8684
Matt Carlson4f125f42009-09-01 12:55:02 +00008685 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008686
Matt Carlson4f125f42009-09-01 12:55:02 +00008687 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07008688
8689 if (err)
8690 return err;
8691
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008692 if (intr_ok) {
8693 /* Reenable MSI one shot mode. */
Matt Carlsonb703df62009-12-03 08:36:21 +00008694 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsona50d0792010-06-05 17:24:37 +00008695 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlsonb703df62009-12-03 08:36:21 +00008696 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008697 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
8698 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
8699 tw32(MSGINT_MODE, val);
8700 }
Michael Chan79381092005-04-21 17:13:59 -07008701 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008702 }
Michael Chan79381092005-04-21 17:13:59 -07008703
8704 return -EIO;
8705}
8706
8707/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
8708 * successfully restored
8709 */
8710static int tg3_test_msi(struct tg3 *tp)
8711{
Michael Chan79381092005-04-21 17:13:59 -07008712 int err;
8713 u16 pci_cmd;
8714
8715 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSI))
8716 return 0;
8717
8718 /* Turn off SERR reporting in case MSI terminates with Master
8719 * Abort.
8720 */
8721 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
8722 pci_write_config_word(tp->pdev, PCI_COMMAND,
8723 pci_cmd & ~PCI_COMMAND_SERR);
8724
8725 err = tg3_test_interrupt(tp);
8726
8727 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
8728
8729 if (!err)
8730 return 0;
8731
8732 /* other failures */
8733 if (err != -EIO)
8734 return err;
8735
8736 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +00008737 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
8738 "to INTx mode. Please report this failure to the PCI "
8739 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -07008740
Matt Carlson4f125f42009-09-01 12:55:02 +00008741 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +00008742
Michael Chan79381092005-04-21 17:13:59 -07008743 pci_disable_msi(tp->pdev);
8744
8745 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI;
Andre Detschdc8bf1b2010-04-26 07:27:07 +00008746 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -07008747
Matt Carlson4f125f42009-09-01 12:55:02 +00008748 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07008749 if (err)
8750 return err;
8751
8752 /* Need to reset the chip because the MSI cycle may have terminated
8753 * with Master Abort.
8754 */
David S. Millerf47c11e2005-06-24 20:18:35 -07008755 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07008756
Michael Chan944d9802005-05-29 14:57:48 -07008757 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008758 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07008759
David S. Millerf47c11e2005-06-24 20:18:35 -07008760 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07008761
8762 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +00008763 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -07008764
8765 return err;
8766}
8767
Matt Carlson9e9fd122009-01-19 16:57:45 -08008768static int tg3_request_firmware(struct tg3 *tp)
8769{
8770 const __be32 *fw_data;
8771
8772 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00008773 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
8774 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08008775 return -ENOENT;
8776 }
8777
8778 fw_data = (void *)tp->fw->data;
8779
8780 /* Firmware blob starts with version numbers, followed by
8781 * start address and _full_ length including BSS sections
8782 * (which must be longer than the actual data, of course
8783 */
8784
8785 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
8786 if (tp->fw_len < (tp->fw->size - 12)) {
Joe Perches05dbe002010-02-17 19:44:19 +00008787 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
8788 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08008789 release_firmware(tp->fw);
8790 tp->fw = NULL;
8791 return -EINVAL;
8792 }
8793
8794 /* We no longer need firmware; we have it. */
8795 tp->fw_needed = NULL;
8796 return 0;
8797}
8798
Matt Carlson679563f2009-09-01 12:55:46 +00008799static bool tg3_enable_msix(struct tg3 *tp)
8800{
8801 int i, rc, cpus = num_online_cpus();
8802 struct msix_entry msix_ent[tp->irq_max];
8803
8804 if (cpus == 1)
8805 /* Just fallback to the simpler MSI mode. */
8806 return false;
8807
8808 /*
8809 * We want as many rx rings enabled as there are cpus.
8810 * The first MSIX vector only deals with link interrupts, etc,
8811 * so we add one to the number of vectors we are requesting.
8812 */
8813 tp->irq_cnt = min_t(unsigned, cpus + 1, tp->irq_max);
8814
8815 for (i = 0; i < tp->irq_max; i++) {
8816 msix_ent[i].entry = i;
8817 msix_ent[i].vector = 0;
8818 }
8819
8820 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +00008821 if (rc < 0) {
8822 return false;
8823 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +00008824 if (pci_enable_msix(tp->pdev, msix_ent, rc))
8825 return false;
Joe Perches05dbe002010-02-17 19:44:19 +00008826 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
8827 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +00008828 tp->irq_cnt = rc;
8829 }
8830
8831 for (i = 0; i < tp->irq_max; i++)
8832 tp->napi[i].irq_vec = msix_ent[i].vector;
8833
Matt Carlson2430b032010-06-05 17:24:34 +00008834 tp->dev->real_num_tx_queues = 1;
8835 if (tp->irq_cnt > 1) {
8836 tp->tg3_flags3 |= TG3_FLG3_ENABLE_RSS;
8837
Matt Carlsona50d0792010-06-05 17:24:37 +00008838 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
8839 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) {
Matt Carlson2430b032010-06-05 17:24:34 +00008840 tp->tg3_flags3 |= TG3_FLG3_ENABLE_TSS;
8841 tp->dev->real_num_tx_queues = tp->irq_cnt - 1;
8842 }
8843 }
Matt Carlsonfe5f5782009-09-01 13:09:39 +00008844
Matt Carlson679563f2009-09-01 12:55:46 +00008845 return true;
8846}
8847
Matt Carlson07b01732009-08-28 14:01:15 +00008848static void tg3_ints_init(struct tg3 *tp)
8849{
Matt Carlson679563f2009-09-01 12:55:46 +00008850 if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSI_OR_MSIX) &&
8851 !(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +00008852 /* All MSI supporting chips should support tagged
8853 * status. Assert that this is the case.
8854 */
Matt Carlson5129c3a2010-04-05 10:19:23 +00008855 netdev_warn(tp->dev,
8856 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +00008857 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +00008858 }
Matt Carlson4f125f42009-09-01 12:55:02 +00008859
Matt Carlson679563f2009-09-01 12:55:46 +00008860 if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX) && tg3_enable_msix(tp))
8861 tp->tg3_flags2 |= TG3_FLG2_USING_MSIX;
8862 else if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSI) &&
8863 pci_enable_msi(tp->pdev) == 0)
8864 tp->tg3_flags2 |= TG3_FLG2_USING_MSI;
8865
8866 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI_OR_MSIX) {
8867 u32 msi_mode = tr32(MSGINT_MODE);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008868 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
8869 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson679563f2009-09-01 12:55:46 +00008870 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
8871 }
8872defcfg:
8873 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSIX)) {
8874 tp->irq_cnt = 1;
8875 tp->napi[0].irq_vec = tp->pdev->irq;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00008876 tp->dev->real_num_tx_queues = 1;
Matt Carlson679563f2009-09-01 12:55:46 +00008877 }
Matt Carlson07b01732009-08-28 14:01:15 +00008878}
8879
8880static void tg3_ints_fini(struct tg3 *tp)
8881{
Matt Carlson679563f2009-09-01 12:55:46 +00008882 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
8883 pci_disable_msix(tp->pdev);
8884 else if (tp->tg3_flags2 & TG3_FLG2_USING_MSI)
8885 pci_disable_msi(tp->pdev);
8886 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI_OR_MSIX;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008887 tp->tg3_flags3 &= ~TG3_FLG3_ENABLE_RSS;
Matt Carlson07b01732009-08-28 14:01:15 +00008888}
8889
Linus Torvalds1da177e2005-04-16 15:20:36 -07008890static int tg3_open(struct net_device *dev)
8891{
8892 struct tg3 *tp = netdev_priv(dev);
Matt Carlson4f125f42009-09-01 12:55:02 +00008893 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008894
Matt Carlson9e9fd122009-01-19 16:57:45 -08008895 if (tp->fw_needed) {
8896 err = tg3_request_firmware(tp);
8897 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
8898 if (err)
8899 return err;
8900 } else if (err) {
Joe Perches05dbe002010-02-17 19:44:19 +00008901 netdev_warn(tp->dev, "TSO capability disabled\n");
Matt Carlson9e9fd122009-01-19 16:57:45 -08008902 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
8903 } else if (!(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
Joe Perches05dbe002010-02-17 19:44:19 +00008904 netdev_notice(tp->dev, "TSO capability restored\n");
Matt Carlson9e9fd122009-01-19 16:57:45 -08008905 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
8906 }
8907 }
8908
Michael Chanc49a1562006-12-17 17:07:29 -08008909 netif_carrier_off(tp->dev);
8910
Michael Chanbc1c7562006-03-20 17:48:03 -08008911 err = tg3_set_power_state(tp, PCI_D0);
Matt Carlson2f751b62008-08-04 23:17:34 -07008912 if (err)
Michael Chanbc1c7562006-03-20 17:48:03 -08008913 return err;
Matt Carlson2f751b62008-08-04 23:17:34 -07008914
8915 tg3_full_lock(tp, 0);
Michael Chanbc1c7562006-03-20 17:48:03 -08008916
Linus Torvalds1da177e2005-04-16 15:20:36 -07008917 tg3_disable_ints(tp);
8918 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
8919
David S. Millerf47c11e2005-06-24 20:18:35 -07008920 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008921
Matt Carlson679563f2009-09-01 12:55:46 +00008922 /*
8923 * Setup interrupts first so we know how
8924 * many NAPI resources to allocate
8925 */
8926 tg3_ints_init(tp);
8927
Linus Torvalds1da177e2005-04-16 15:20:36 -07008928 /* The placement of this call is tied
8929 * to the setup and use of Host TX descriptors.
8930 */
8931 err = tg3_alloc_consistent(tp);
8932 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00008933 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008934
Matt Carlsonfed97812009-09-01 13:10:19 +00008935 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07008936
Matt Carlson4f125f42009-09-01 12:55:02 +00008937 for (i = 0; i < tp->irq_cnt; i++) {
8938 struct tg3_napi *tnapi = &tp->napi[i];
8939 err = tg3_request_irq(tp, i);
8940 if (err) {
8941 for (i--; i >= 0; i--)
8942 free_irq(tnapi->irq_vec, tnapi);
8943 break;
8944 }
8945 }
Matt Carlson07b01732009-08-28 14:01:15 +00008946
8947 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00008948 goto err_out2;
Matt Carlson07b01732009-08-28 14:01:15 +00008949
David S. Millerf47c11e2005-06-24 20:18:35 -07008950 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008951
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008952 err = tg3_init_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008953 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -07008954 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008955 tg3_free_rings(tp);
8956 } else {
David S. Millerfac9b832005-05-18 22:46:34 -07008957 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)
8958 tp->timer_offset = HZ;
8959 else
8960 tp->timer_offset = HZ / 10;
8961
8962 BUG_ON(tp->timer_offset > HZ);
8963 tp->timer_counter = tp->timer_multiplier =
8964 (HZ / tp->timer_offset);
8965 tp->asf_counter = tp->asf_multiplier =
Michael Chan28fbef72005-10-26 15:48:35 -07008966 ((HZ / tp->timer_offset) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008967
8968 init_timer(&tp->timer);
8969 tp->timer.expires = jiffies + tp->timer_offset;
8970 tp->timer.data = (unsigned long) tp;
8971 tp->timer.function = tg3_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008972 }
8973
David S. Millerf47c11e2005-06-24 20:18:35 -07008974 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008975
Matt Carlson07b01732009-08-28 14:01:15 +00008976 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00008977 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008978
Michael Chan79381092005-04-21 17:13:59 -07008979 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
8980 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -07008981
Michael Chan79381092005-04-21 17:13:59 -07008982 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -07008983 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -07008984 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -07008985 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07008986 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07008987
Matt Carlson679563f2009-09-01 12:55:46 +00008988 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -07008989 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08008990
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008991 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
Matt Carlsona50d0792010-06-05 17:24:37 +00008992 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5719 &&
Matt Carlsonb703df62009-12-03 08:36:21 +00008993 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57765 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008994 (tp->tg3_flags2 & TG3_FLG2_USING_MSI) &&
8995 (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)) {
8996 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -08008997
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008998 tw32(PCIE_TRANSACTION_CFG,
8999 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009000 }
Michael Chan79381092005-04-21 17:13:59 -07009001 }
9002
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009003 tg3_phy_start(tp);
9004
David S. Millerf47c11e2005-06-24 20:18:35 -07009005 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009006
Michael Chan79381092005-04-21 17:13:59 -07009007 add_timer(&tp->timer);
9008 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009009 tg3_enable_ints(tp);
9010
David S. Millerf47c11e2005-06-24 20:18:35 -07009011 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009012
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009013 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009014
9015 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +00009016
Matt Carlson679563f2009-09-01 12:55:46 +00009017err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +00009018 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9019 struct tg3_napi *tnapi = &tp->napi[i];
9020 free_irq(tnapi->irq_vec, tnapi);
9021 }
Matt Carlson07b01732009-08-28 14:01:15 +00009022
Matt Carlson679563f2009-09-01 12:55:46 +00009023err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +00009024 tg3_napi_disable(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00009025 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +00009026
9027err_out1:
9028 tg3_ints_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00009029 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009030}
9031
Eric Dumazet511d2222010-07-07 20:44:24 +00009032static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *,
9033 struct rtnl_link_stats64 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009034static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *);
9035
9036static int tg3_close(struct net_device *dev)
9037{
Matt Carlson4f125f42009-09-01 12:55:02 +00009038 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009039 struct tg3 *tp = netdev_priv(dev);
9040
Matt Carlsonfed97812009-09-01 13:10:19 +00009041 tg3_napi_disable(tp);
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07009042 cancel_work_sync(&tp->reset_task);
Michael Chan7faa0062006-02-02 17:29:28 -08009043
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009044 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009045
9046 del_timer_sync(&tp->timer);
9047
Matt Carlson24bb4fb2009-10-05 17:55:29 +00009048 tg3_phy_stop(tp);
9049
David S. Millerf47c11e2005-06-24 20:18:35 -07009050 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009051
9052 tg3_disable_ints(tp);
9053
Michael Chan944d9802005-05-29 14:57:48 -07009054 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009055 tg3_free_rings(tp);
Michael Chan5cf64b8a2007-05-05 12:11:21 -07009056 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009057
David S. Millerf47c11e2005-06-24 20:18:35 -07009058 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009059
Matt Carlson4f125f42009-09-01 12:55:02 +00009060 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9061 struct tg3_napi *tnapi = &tp->napi[i];
9062 free_irq(tnapi->irq_vec, tnapi);
9063 }
Matt Carlson07b01732009-08-28 14:01:15 +00009064
9065 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009066
Eric Dumazet511d2222010-07-07 20:44:24 +00009067 tg3_get_stats64(tp->dev, &tp->net_stats_prev);
9068
Linus Torvalds1da177e2005-04-16 15:20:36 -07009069 memcpy(&tp->estats_prev, tg3_get_estats(tp),
9070 sizeof(tp->estats_prev));
9071
9072 tg3_free_consistent(tp);
9073
Michael Chanbc1c7562006-03-20 17:48:03 -08009074 tg3_set_power_state(tp, PCI_D3hot);
9075
9076 netif_carrier_off(tp->dev);
9077
Linus Torvalds1da177e2005-04-16 15:20:36 -07009078 return 0;
9079}
9080
Eric Dumazet511d2222010-07-07 20:44:24 +00009081static inline u64 get_stat64(tg3_stat64_t *val)
Stefan Buehler816f8b82008-08-15 14:10:54 -07009082{
9083 return ((u64)val->high << 32) | ((u64)val->low);
9084}
9085
Eric Dumazet511d2222010-07-07 20:44:24 +00009086static u64 calc_crc_errors(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009087{
9088 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9089
9090 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
9091 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
9092 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009093 u32 val;
9094
David S. Millerf47c11e2005-06-24 20:18:35 -07009095 spin_lock_bh(&tp->lock);
Michael Chan569a5df2007-02-13 12:18:15 -08009096 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
9097 tg3_writephy(tp, MII_TG3_TEST1,
9098 val | MII_TG3_TEST1_CRC_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009099 tg3_readphy(tp, 0x14, &val);
9100 } else
9101 val = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07009102 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009103
9104 tp->phy_crc_errors += val;
9105
9106 return tp->phy_crc_errors;
9107 }
9108
9109 return get_stat64(&hw_stats->rx_fcs_errors);
9110}
9111
9112#define ESTAT_ADD(member) \
9113 estats->member = old_estats->member + \
Eric Dumazet511d2222010-07-07 20:44:24 +00009114 get_stat64(&hw_stats->member)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009115
9116static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *tp)
9117{
9118 struct tg3_ethtool_stats *estats = &tp->estats;
9119 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
9120 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9121
9122 if (!hw_stats)
9123 return old_estats;
9124
9125 ESTAT_ADD(rx_octets);
9126 ESTAT_ADD(rx_fragments);
9127 ESTAT_ADD(rx_ucast_packets);
9128 ESTAT_ADD(rx_mcast_packets);
9129 ESTAT_ADD(rx_bcast_packets);
9130 ESTAT_ADD(rx_fcs_errors);
9131 ESTAT_ADD(rx_align_errors);
9132 ESTAT_ADD(rx_xon_pause_rcvd);
9133 ESTAT_ADD(rx_xoff_pause_rcvd);
9134 ESTAT_ADD(rx_mac_ctrl_rcvd);
9135 ESTAT_ADD(rx_xoff_entered);
9136 ESTAT_ADD(rx_frame_too_long_errors);
9137 ESTAT_ADD(rx_jabbers);
9138 ESTAT_ADD(rx_undersize_packets);
9139 ESTAT_ADD(rx_in_length_errors);
9140 ESTAT_ADD(rx_out_length_errors);
9141 ESTAT_ADD(rx_64_or_less_octet_packets);
9142 ESTAT_ADD(rx_65_to_127_octet_packets);
9143 ESTAT_ADD(rx_128_to_255_octet_packets);
9144 ESTAT_ADD(rx_256_to_511_octet_packets);
9145 ESTAT_ADD(rx_512_to_1023_octet_packets);
9146 ESTAT_ADD(rx_1024_to_1522_octet_packets);
9147 ESTAT_ADD(rx_1523_to_2047_octet_packets);
9148 ESTAT_ADD(rx_2048_to_4095_octet_packets);
9149 ESTAT_ADD(rx_4096_to_8191_octet_packets);
9150 ESTAT_ADD(rx_8192_to_9022_octet_packets);
9151
9152 ESTAT_ADD(tx_octets);
9153 ESTAT_ADD(tx_collisions);
9154 ESTAT_ADD(tx_xon_sent);
9155 ESTAT_ADD(tx_xoff_sent);
9156 ESTAT_ADD(tx_flow_control);
9157 ESTAT_ADD(tx_mac_errors);
9158 ESTAT_ADD(tx_single_collisions);
9159 ESTAT_ADD(tx_mult_collisions);
9160 ESTAT_ADD(tx_deferred);
9161 ESTAT_ADD(tx_excessive_collisions);
9162 ESTAT_ADD(tx_late_collisions);
9163 ESTAT_ADD(tx_collide_2times);
9164 ESTAT_ADD(tx_collide_3times);
9165 ESTAT_ADD(tx_collide_4times);
9166 ESTAT_ADD(tx_collide_5times);
9167 ESTAT_ADD(tx_collide_6times);
9168 ESTAT_ADD(tx_collide_7times);
9169 ESTAT_ADD(tx_collide_8times);
9170 ESTAT_ADD(tx_collide_9times);
9171 ESTAT_ADD(tx_collide_10times);
9172 ESTAT_ADD(tx_collide_11times);
9173 ESTAT_ADD(tx_collide_12times);
9174 ESTAT_ADD(tx_collide_13times);
9175 ESTAT_ADD(tx_collide_14times);
9176 ESTAT_ADD(tx_collide_15times);
9177 ESTAT_ADD(tx_ucast_packets);
9178 ESTAT_ADD(tx_mcast_packets);
9179 ESTAT_ADD(tx_bcast_packets);
9180 ESTAT_ADD(tx_carrier_sense_errors);
9181 ESTAT_ADD(tx_discards);
9182 ESTAT_ADD(tx_errors);
9183
9184 ESTAT_ADD(dma_writeq_full);
9185 ESTAT_ADD(dma_write_prioq_full);
9186 ESTAT_ADD(rxbds_empty);
9187 ESTAT_ADD(rx_discards);
9188 ESTAT_ADD(rx_errors);
9189 ESTAT_ADD(rx_threshold_hit);
9190
9191 ESTAT_ADD(dma_readq_full);
9192 ESTAT_ADD(dma_read_prioq_full);
9193 ESTAT_ADD(tx_comp_queue_full);
9194
9195 ESTAT_ADD(ring_set_send_prod_index);
9196 ESTAT_ADD(ring_status_update);
9197 ESTAT_ADD(nic_irqs);
9198 ESTAT_ADD(nic_avoided_irqs);
9199 ESTAT_ADD(nic_tx_threshold_hit);
9200
9201 return estats;
9202}
9203
Eric Dumazet511d2222010-07-07 20:44:24 +00009204static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
9205 struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009206{
9207 struct tg3 *tp = netdev_priv(dev);
Eric Dumazet511d2222010-07-07 20:44:24 +00009208 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009209 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9210
9211 if (!hw_stats)
9212 return old_stats;
9213
9214 stats->rx_packets = old_stats->rx_packets +
9215 get_stat64(&hw_stats->rx_ucast_packets) +
9216 get_stat64(&hw_stats->rx_mcast_packets) +
9217 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009218
Linus Torvalds1da177e2005-04-16 15:20:36 -07009219 stats->tx_packets = old_stats->tx_packets +
9220 get_stat64(&hw_stats->tx_ucast_packets) +
9221 get_stat64(&hw_stats->tx_mcast_packets) +
9222 get_stat64(&hw_stats->tx_bcast_packets);
9223
9224 stats->rx_bytes = old_stats->rx_bytes +
9225 get_stat64(&hw_stats->rx_octets);
9226 stats->tx_bytes = old_stats->tx_bytes +
9227 get_stat64(&hw_stats->tx_octets);
9228
9229 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -07009230 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009231 stats->tx_errors = old_stats->tx_errors +
9232 get_stat64(&hw_stats->tx_errors) +
9233 get_stat64(&hw_stats->tx_mac_errors) +
9234 get_stat64(&hw_stats->tx_carrier_sense_errors) +
9235 get_stat64(&hw_stats->tx_discards);
9236
9237 stats->multicast = old_stats->multicast +
9238 get_stat64(&hw_stats->rx_mcast_packets);
9239 stats->collisions = old_stats->collisions +
9240 get_stat64(&hw_stats->tx_collisions);
9241
9242 stats->rx_length_errors = old_stats->rx_length_errors +
9243 get_stat64(&hw_stats->rx_frame_too_long_errors) +
9244 get_stat64(&hw_stats->rx_undersize_packets);
9245
9246 stats->rx_over_errors = old_stats->rx_over_errors +
9247 get_stat64(&hw_stats->rxbds_empty);
9248 stats->rx_frame_errors = old_stats->rx_frame_errors +
9249 get_stat64(&hw_stats->rx_align_errors);
9250 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
9251 get_stat64(&hw_stats->tx_discards);
9252 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
9253 get_stat64(&hw_stats->tx_carrier_sense_errors);
9254
9255 stats->rx_crc_errors = old_stats->rx_crc_errors +
9256 calc_crc_errors(tp);
9257
John W. Linville4f63b872005-09-12 14:43:18 -07009258 stats->rx_missed_errors = old_stats->rx_missed_errors +
9259 get_stat64(&hw_stats->rx_discards);
9260
Linus Torvalds1da177e2005-04-16 15:20:36 -07009261 return stats;
9262}
9263
9264static inline u32 calc_crc(unsigned char *buf, int len)
9265{
9266 u32 reg;
9267 u32 tmp;
9268 int j, k;
9269
9270 reg = 0xffffffff;
9271
9272 for (j = 0; j < len; j++) {
9273 reg ^= buf[j];
9274
9275 for (k = 0; k < 8; k++) {
9276 tmp = reg & 0x01;
9277
9278 reg >>= 1;
9279
Matt Carlson859a588792010-04-05 10:19:28 +00009280 if (tmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009281 reg ^= 0xedb88320;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009282 }
9283 }
9284
9285 return ~reg;
9286}
9287
9288static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
9289{
9290 /* accept or reject all multicast frames */
9291 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
9292 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
9293 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
9294 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
9295}
9296
9297static void __tg3_set_rx_mode(struct net_device *dev)
9298{
9299 struct tg3 *tp = netdev_priv(dev);
9300 u32 rx_mode;
9301
9302 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
9303 RX_MODE_KEEP_VLAN_TAG);
9304
9305 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
9306 * flag clear.
9307 */
9308#if TG3_VLAN_TAG_USED
9309 if (!tp->vlgrp &&
9310 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
9311 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9312#else
9313 /* By definition, VLAN is disabled always in this
9314 * case.
9315 */
9316 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
9317 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9318#endif
9319
9320 if (dev->flags & IFF_PROMISC) {
9321 /* Promiscuous mode. */
9322 rx_mode |= RX_MODE_PROMISC;
9323 } else if (dev->flags & IFF_ALLMULTI) {
9324 /* Accept all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +00009325 tg3_set_multi(tp, 1);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00009326 } else if (netdev_mc_empty(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009327 /* Reject all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +00009328 tg3_set_multi(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009329 } else {
9330 /* Accept one or more multicast(s). */
Jiri Pirko22bedad32010-04-01 21:22:57 +00009331 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009332 u32 mc_filter[4] = { 0, };
9333 u32 regidx;
9334 u32 bit;
9335 u32 crc;
9336
Jiri Pirko22bedad32010-04-01 21:22:57 +00009337 netdev_for_each_mc_addr(ha, dev) {
9338 crc = calc_crc(ha->addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009339 bit = ~crc & 0x7f;
9340 regidx = (bit & 0x60) >> 5;
9341 bit &= 0x1f;
9342 mc_filter[regidx] |= (1 << bit);
9343 }
9344
9345 tw32(MAC_HASH_REG_0, mc_filter[0]);
9346 tw32(MAC_HASH_REG_1, mc_filter[1]);
9347 tw32(MAC_HASH_REG_2, mc_filter[2]);
9348 tw32(MAC_HASH_REG_3, mc_filter[3]);
9349 }
9350
9351 if (rx_mode != tp->rx_mode) {
9352 tp->rx_mode = rx_mode;
9353 tw32_f(MAC_RX_MODE, rx_mode);
9354 udelay(10);
9355 }
9356}
9357
9358static void tg3_set_rx_mode(struct net_device *dev)
9359{
9360 struct tg3 *tp = netdev_priv(dev);
9361
Michael Chane75f7c92006-03-20 21:33:26 -08009362 if (!netif_running(dev))
9363 return;
9364
David S. Millerf47c11e2005-06-24 20:18:35 -07009365 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009366 __tg3_set_rx_mode(dev);
David S. Millerf47c11e2005-06-24 20:18:35 -07009367 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009368}
9369
9370#define TG3_REGDUMP_LEN (32 * 1024)
9371
9372static int tg3_get_regs_len(struct net_device *dev)
9373{
9374 return TG3_REGDUMP_LEN;
9375}
9376
9377static void tg3_get_regs(struct net_device *dev,
9378 struct ethtool_regs *regs, void *_p)
9379{
9380 u32 *p = _p;
9381 struct tg3 *tp = netdev_priv(dev);
9382 u8 *orig_p = _p;
9383 int i;
9384
9385 regs->version = 0;
9386
9387 memset(p, 0, TG3_REGDUMP_LEN);
9388
Michael Chanbc1c7562006-03-20 17:48:03 -08009389 if (tp->link_config.phy_is_low_power)
9390 return;
9391
David S. Millerf47c11e2005-06-24 20:18:35 -07009392 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009393
9394#define __GET_REG32(reg) (*(p)++ = tr32(reg))
9395#define GET_REG32_LOOP(base,len) \
9396do { p = (u32 *)(orig_p + (base)); \
9397 for (i = 0; i < len; i += 4) \
9398 __GET_REG32((base) + i); \
9399} while (0)
9400#define GET_REG32_1(reg) \
9401do { p = (u32 *)(orig_p + (reg)); \
9402 __GET_REG32((reg)); \
9403} while (0)
9404
9405 GET_REG32_LOOP(TG3PCI_VENDOR, 0xb0);
9406 GET_REG32_LOOP(MAILBOX_INTERRUPT_0, 0x200);
9407 GET_REG32_LOOP(MAC_MODE, 0x4f0);
9408 GET_REG32_LOOP(SNDDATAI_MODE, 0xe0);
9409 GET_REG32_1(SNDDATAC_MODE);
9410 GET_REG32_LOOP(SNDBDS_MODE, 0x80);
9411 GET_REG32_LOOP(SNDBDI_MODE, 0x48);
9412 GET_REG32_1(SNDBDC_MODE);
9413 GET_REG32_LOOP(RCVLPC_MODE, 0x20);
9414 GET_REG32_LOOP(RCVLPC_SELLST_BASE, 0x15c);
9415 GET_REG32_LOOP(RCVDBDI_MODE, 0x0c);
9416 GET_REG32_LOOP(RCVDBDI_JUMBO_BD, 0x3c);
9417 GET_REG32_LOOP(RCVDBDI_BD_PROD_IDX_0, 0x44);
9418 GET_REG32_1(RCVDCC_MODE);
9419 GET_REG32_LOOP(RCVBDI_MODE, 0x20);
9420 GET_REG32_LOOP(RCVCC_MODE, 0x14);
9421 GET_REG32_LOOP(RCVLSC_MODE, 0x08);
9422 GET_REG32_1(MBFREE_MODE);
9423 GET_REG32_LOOP(HOSTCC_MODE, 0x100);
9424 GET_REG32_LOOP(MEMARB_MODE, 0x10);
9425 GET_REG32_LOOP(BUFMGR_MODE, 0x58);
9426 GET_REG32_LOOP(RDMAC_MODE, 0x08);
9427 GET_REG32_LOOP(WDMAC_MODE, 0x08);
Chris Elmquist091465d2005-12-20 13:25:19 -08009428 GET_REG32_1(RX_CPU_MODE);
9429 GET_REG32_1(RX_CPU_STATE);
9430 GET_REG32_1(RX_CPU_PGMCTR);
9431 GET_REG32_1(RX_CPU_HWBKPT);
9432 GET_REG32_1(TX_CPU_MODE);
9433 GET_REG32_1(TX_CPU_STATE);
9434 GET_REG32_1(TX_CPU_PGMCTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009435 GET_REG32_LOOP(GRCMBOX_INTERRUPT_0, 0x110);
9436 GET_REG32_LOOP(FTQ_RESET, 0x120);
9437 GET_REG32_LOOP(MSGINT_MODE, 0x0c);
9438 GET_REG32_1(DMAC_MODE);
9439 GET_REG32_LOOP(GRC_MODE, 0x4c);
9440 if (tp->tg3_flags & TG3_FLAG_NVRAM)
9441 GET_REG32_LOOP(NVRAM_CMD, 0x24);
9442
9443#undef __GET_REG32
9444#undef GET_REG32_LOOP
9445#undef GET_REG32_1
9446
David S. Millerf47c11e2005-06-24 20:18:35 -07009447 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009448}
9449
9450static int tg3_get_eeprom_len(struct net_device *dev)
9451{
9452 struct tg3 *tp = netdev_priv(dev);
9453
9454 return tp->nvram_size;
9455}
9456
Linus Torvalds1da177e2005-04-16 15:20:36 -07009457static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9458{
9459 struct tg3 *tp = netdev_priv(dev);
9460 int ret;
9461 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -08009462 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009463 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009464
Matt Carlsondf259d82009-04-20 06:57:14 +00009465 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM)
9466 return -EINVAL;
9467
Michael Chanbc1c7562006-03-20 17:48:03 -08009468 if (tp->link_config.phy_is_low_power)
9469 return -EAGAIN;
9470
Linus Torvalds1da177e2005-04-16 15:20:36 -07009471 offset = eeprom->offset;
9472 len = eeprom->len;
9473 eeprom->len = 0;
9474
9475 eeprom->magic = TG3_EEPROM_MAGIC;
9476
9477 if (offset & 3) {
9478 /* adjustments to start on required 4 byte boundary */
9479 b_offset = offset & 3;
9480 b_count = 4 - b_offset;
9481 if (b_count > len) {
9482 /* i.e. offset=1 len=2 */
9483 b_count = len;
9484 }
Matt Carlsona9dc5292009-02-25 14:25:30 +00009485 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009486 if (ret)
9487 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009488 memcpy(data, ((char*)&val) + b_offset, b_count);
9489 len -= b_count;
9490 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00009491 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009492 }
9493
9494 /* read bytes upto the last 4 byte boundary */
9495 pd = &data[eeprom->len];
9496 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +00009497 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009498 if (ret) {
9499 eeprom->len += i;
9500 return ret;
9501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009502 memcpy(pd + i, &val, 4);
9503 }
9504 eeprom->len += i;
9505
9506 if (len & 3) {
9507 /* read last bytes not ending on 4 byte boundary */
9508 pd = &data[eeprom->len];
9509 b_count = len & 3;
9510 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009511 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009512 if (ret)
9513 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009514 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009515 eeprom->len += b_count;
9516 }
9517 return 0;
9518}
9519
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009520static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009521
9522static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9523{
9524 struct tg3 *tp = netdev_priv(dev);
9525 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009526 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009527 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009528 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009529
Michael Chanbc1c7562006-03-20 17:48:03 -08009530 if (tp->link_config.phy_is_low_power)
9531 return -EAGAIN;
9532
Matt Carlsondf259d82009-04-20 06:57:14 +00009533 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
9534 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009535 return -EINVAL;
9536
9537 offset = eeprom->offset;
9538 len = eeprom->len;
9539
9540 if ((b_offset = (offset & 3))) {
9541 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +00009542 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009543 if (ret)
9544 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009545 len += b_offset;
9546 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -07009547 if (len < 4)
9548 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009549 }
9550
9551 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -07009552 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009553 /* adjustments to end on required 4 byte boundary */
9554 odd_len = 1;
9555 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009556 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009557 if (ret)
9558 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009559 }
9560
9561 buf = data;
9562 if (b_offset || odd_len) {
9563 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009564 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009565 return -ENOMEM;
9566 if (b_offset)
9567 memcpy(buf, &start, 4);
9568 if (odd_len)
9569 memcpy(buf+len-4, &end, 4);
9570 memcpy(buf + b_offset, data, eeprom->len);
9571 }
9572
9573 ret = tg3_nvram_write_block(tp, offset, len, buf);
9574
9575 if (buf != data)
9576 kfree(buf);
9577
9578 return ret;
9579}
9580
9581static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9582{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009583 struct tg3 *tp = netdev_priv(dev);
9584
9585 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009586 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009587 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
9588 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009589 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
9590 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009591 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009592
Linus Torvalds1da177e2005-04-16 15:20:36 -07009593 cmd->supported = (SUPPORTED_Autoneg);
9594
9595 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY))
9596 cmd->supported |= (SUPPORTED_1000baseT_Half |
9597 SUPPORTED_1000baseT_Full);
9598
Karsten Keilef348142006-05-12 12:49:08 -07009599 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009600 cmd->supported |= (SUPPORTED_100baseT_Half |
9601 SUPPORTED_100baseT_Full |
9602 SUPPORTED_10baseT_Half |
9603 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -08009604 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -07009605 cmd->port = PORT_TP;
9606 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009607 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -07009608 cmd->port = PORT_FIBRE;
9609 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009610
Linus Torvalds1da177e2005-04-16 15:20:36 -07009611 cmd->advertising = tp->link_config.advertising;
9612 if (netif_running(dev)) {
9613 cmd->speed = tp->link_config.active_speed;
9614 cmd->duplex = tp->link_config.active_duplex;
9615 }
Matt Carlson882e9792009-09-01 13:21:36 +00009616 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +00009617 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009618 cmd->autoneg = tp->link_config.autoneg;
9619 cmd->maxtxpkt = 0;
9620 cmd->maxrxpkt = 0;
9621 return 0;
9622}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009623
Linus Torvalds1da177e2005-04-16 15:20:36 -07009624static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9625{
9626 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009627
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009628 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009629 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009630 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
9631 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009632 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
9633 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009634 }
9635
Matt Carlson7e5856b2009-02-25 14:23:01 +00009636 if (cmd->autoneg != AUTONEG_ENABLE &&
9637 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -07009638 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +00009639
9640 if (cmd->autoneg == AUTONEG_DISABLE &&
9641 cmd->duplex != DUPLEX_FULL &&
9642 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -07009643 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009644
Matt Carlson7e5856b2009-02-25 14:23:01 +00009645 if (cmd->autoneg == AUTONEG_ENABLE) {
9646 u32 mask = ADVERTISED_Autoneg |
9647 ADVERTISED_Pause |
9648 ADVERTISED_Asym_Pause;
9649
Julia Lawall3f07d122010-03-13 12:22:16 -08009650 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +00009651 mask |= ADVERTISED_1000baseT_Half |
9652 ADVERTISED_1000baseT_Full;
9653
9654 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES))
9655 mask |= ADVERTISED_100baseT_Half |
9656 ADVERTISED_100baseT_Full |
9657 ADVERTISED_10baseT_Half |
9658 ADVERTISED_10baseT_Full |
9659 ADVERTISED_TP;
9660 else
9661 mask |= ADVERTISED_FIBRE;
9662
9663 if (cmd->advertising & ~mask)
9664 return -EINVAL;
9665
9666 mask &= (ADVERTISED_1000baseT_Half |
9667 ADVERTISED_1000baseT_Full |
9668 ADVERTISED_100baseT_Half |
9669 ADVERTISED_100baseT_Full |
9670 ADVERTISED_10baseT_Half |
9671 ADVERTISED_10baseT_Full);
9672
9673 cmd->advertising &= mask;
9674 } else {
9675 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES) {
9676 if (cmd->speed != SPEED_1000)
9677 return -EINVAL;
9678
9679 if (cmd->duplex != DUPLEX_FULL)
9680 return -EINVAL;
9681 } else {
9682 if (cmd->speed != SPEED_100 &&
9683 cmd->speed != SPEED_10)
9684 return -EINVAL;
9685 }
9686 }
9687
David S. Millerf47c11e2005-06-24 20:18:35 -07009688 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009689
9690 tp->link_config.autoneg = cmd->autoneg;
9691 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -07009692 tp->link_config.advertising = (cmd->advertising |
9693 ADVERTISED_Autoneg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009694 tp->link_config.speed = SPEED_INVALID;
9695 tp->link_config.duplex = DUPLEX_INVALID;
9696 } else {
9697 tp->link_config.advertising = 0;
9698 tp->link_config.speed = cmd->speed;
9699 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009700 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009701
Michael Chan24fcad62006-12-17 17:06:46 -08009702 tp->link_config.orig_speed = tp->link_config.speed;
9703 tp->link_config.orig_duplex = tp->link_config.duplex;
9704 tp->link_config.orig_autoneg = tp->link_config.autoneg;
9705
Linus Torvalds1da177e2005-04-16 15:20:36 -07009706 if (netif_running(dev))
9707 tg3_setup_phy(tp, 1);
9708
David S. Millerf47c11e2005-06-24 20:18:35 -07009709 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009710
Linus Torvalds1da177e2005-04-16 15:20:36 -07009711 return 0;
9712}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009713
Linus Torvalds1da177e2005-04-16 15:20:36 -07009714static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
9715{
9716 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009717
Linus Torvalds1da177e2005-04-16 15:20:36 -07009718 strcpy(info->driver, DRV_MODULE_NAME);
9719 strcpy(info->version, DRV_MODULE_VERSION);
Michael Chanc4e65752006-03-20 22:29:32 -08009720 strcpy(info->fw_version, tp->fw_ver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009721 strcpy(info->bus_info, pci_name(tp->pdev));
9722}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009723
Linus Torvalds1da177e2005-04-16 15:20:36 -07009724static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
9725{
9726 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009727
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009728 if ((tp->tg3_flags & TG3_FLAG_WOL_CAP) &&
9729 device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -07009730 wol->supported = WAKE_MAGIC;
9731 else
9732 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009733 wol->wolopts = 0;
Matt Carlson05ac4cb2008-11-03 16:53:46 -08009734 if ((tp->tg3_flags & TG3_FLAG_WOL_ENABLE) &&
9735 device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009736 wol->wolopts = WAKE_MAGIC;
9737 memset(&wol->sopass, 0, sizeof(wol->sopass));
9738}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009739
Linus Torvalds1da177e2005-04-16 15:20:36 -07009740static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
9741{
9742 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009743 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009744
Linus Torvalds1da177e2005-04-16 15:20:36 -07009745 if (wol->wolopts & ~WAKE_MAGIC)
9746 return -EINVAL;
9747 if ((wol->wolopts & WAKE_MAGIC) &&
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009748 !((tp->tg3_flags & TG3_FLAG_WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009749 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009750
David S. Millerf47c11e2005-06-24 20:18:35 -07009751 spin_lock_bh(&tp->lock);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009752 if (wol->wolopts & WAKE_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009753 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009754 device_set_wakeup_enable(dp, true);
9755 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009756 tp->tg3_flags &= ~TG3_FLAG_WOL_ENABLE;
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009757 device_set_wakeup_enable(dp, false);
9758 }
David S. Millerf47c11e2005-06-24 20:18:35 -07009759 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009760
Linus Torvalds1da177e2005-04-16 15:20:36 -07009761 return 0;
9762}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009763
Linus Torvalds1da177e2005-04-16 15:20:36 -07009764static u32 tg3_get_msglevel(struct net_device *dev)
9765{
9766 struct tg3 *tp = netdev_priv(dev);
9767 return tp->msg_enable;
9768}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009769
Linus Torvalds1da177e2005-04-16 15:20:36 -07009770static void tg3_set_msglevel(struct net_device *dev, u32 value)
9771{
9772 struct tg3 *tp = netdev_priv(dev);
9773 tp->msg_enable = value;
9774}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009775
Linus Torvalds1da177e2005-04-16 15:20:36 -07009776static int tg3_set_tso(struct net_device *dev, u32 value)
9777{
9778 struct tg3 *tp = netdev_priv(dev);
9779
9780 if (!(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
9781 if (value)
9782 return -EINVAL;
9783 return 0;
9784 }
Matt Carlson027455a2008-12-21 20:19:30 -08009785 if ((dev->features & NETIF_F_IPV6_CSUM) &&
Matt Carlsone849cdc2009-11-13 13:03:38 +00009786 ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_2) ||
9787 (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3))) {
Matt Carlson9936bcf2007-10-10 18:03:07 -07009788 if (value) {
Michael Chanb0026622006-07-03 19:42:14 -07009789 dev->features |= NETIF_F_TSO6;
Matt Carlsone849cdc2009-11-13 13:03:38 +00009790 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
9791 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -07009792 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
9793 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Matt Carlson321d32a2008-11-21 17:22:19 -08009794 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsone849cdc2009-11-13 13:03:38 +00009795 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson9936bcf2007-10-10 18:03:07 -07009796 dev->features |= NETIF_F_TSO_ECN;
9797 } else
9798 dev->features &= ~(NETIF_F_TSO6 | NETIF_F_TSO_ECN);
Michael Chanb0026622006-07-03 19:42:14 -07009799 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009800 return ethtool_op_set_tso(dev, value);
9801}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009802
Linus Torvalds1da177e2005-04-16 15:20:36 -07009803static int tg3_nway_reset(struct net_device *dev)
9804{
9805 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009806 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009807
Linus Torvalds1da177e2005-04-16 15:20:36 -07009808 if (!netif_running(dev))
9809 return -EAGAIN;
9810
Michael Chanc94e3942005-09-27 12:12:42 -07009811 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
9812 return -EINVAL;
9813
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009814 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
9815 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
9816 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009817 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009818 } else {
9819 u32 bmcr;
9820
9821 spin_lock_bh(&tp->lock);
9822 r = -EINVAL;
9823 tg3_readphy(tp, MII_BMCR, &bmcr);
9824 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
9825 ((bmcr & BMCR_ANENABLE) ||
9826 (tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT))) {
9827 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
9828 BMCR_ANENABLE);
9829 r = 0;
9830 }
9831 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009832 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009833
Linus Torvalds1da177e2005-04-16 15:20:36 -07009834 return r;
9835}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009836
Linus Torvalds1da177e2005-04-16 15:20:36 -07009837static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
9838{
9839 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009840
Linus Torvalds1da177e2005-04-16 15:20:36 -07009841 ering->rx_max_pending = TG3_RX_RING_SIZE - 1;
9842 ering->rx_mini_max_pending = 0;
Michael Chan4f81c322006-03-20 21:33:42 -08009843 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE)
9844 ering->rx_jumbo_max_pending = TG3_RX_JUMBO_RING_SIZE - 1;
9845 else
9846 ering->rx_jumbo_max_pending = 0;
9847
9848 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009849
9850 ering->rx_pending = tp->rx_pending;
9851 ering->rx_mini_pending = 0;
Michael Chan4f81c322006-03-20 21:33:42 -08009852 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE)
9853 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
9854 else
9855 ering->rx_jumbo_pending = 0;
9856
Matt Carlsonf3f3f272009-08-28 14:03:21 +00009857 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009858}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009859
Linus Torvalds1da177e2005-04-16 15:20:36 -07009860static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
9861{
9862 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +00009863 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009864
Linus Torvalds1da177e2005-04-16 15:20:36 -07009865 if ((ering->rx_pending > TG3_RX_RING_SIZE - 1) ||
9866 (ering->rx_jumbo_pending > TG3_RX_JUMBO_RING_SIZE - 1) ||
Michael Chanbc3a9252006-10-18 20:55:18 -07009867 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
9868 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Michael Chan7f62ad52007-02-20 23:25:40 -08009869 ((tp->tg3_flags2 & TG3_FLG2_TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -07009870 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009871 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009872
Michael Chanbbe832c2005-06-24 20:20:04 -07009873 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009874 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009875 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -07009876 irq_sync = 1;
9877 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009878
Michael Chanbbe832c2005-06-24 20:20:04 -07009879 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009880
Linus Torvalds1da177e2005-04-16 15:20:36 -07009881 tp->rx_pending = ering->rx_pending;
9882
9883 if ((tp->tg3_flags2 & TG3_FLG2_MAX_RXPEND_64) &&
9884 tp->rx_pending > 63)
9885 tp->rx_pending = 63;
9886 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +00009887
9888 for (i = 0; i < TG3_IRQ_MAX_VECS; i++)
9889 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009890
9891 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -07009892 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -07009893 err = tg3_restart_hw(tp, 1);
9894 if (!err)
9895 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009896 }
9897
David S. Millerf47c11e2005-06-24 20:18:35 -07009898 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009899
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009900 if (irq_sync && !err)
9901 tg3_phy_start(tp);
9902
Michael Chanb9ec6c12006-07-25 16:37:27 -07009903 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009904}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009905
Linus Torvalds1da177e2005-04-16 15:20:36 -07009906static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
9907{
9908 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009909
Linus Torvalds1da177e2005-04-16 15:20:36 -07009910 epause->autoneg = (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG) != 0;
Matt Carlson8d018622007-12-20 20:05:44 -08009911
Steve Glendinninge18ce342008-12-16 02:00:00 -08009912 if (tp->link_config.active_flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -08009913 epause->rx_pause = 1;
9914 else
9915 epause->rx_pause = 0;
9916
Steve Glendinninge18ce342008-12-16 02:00:00 -08009917 if (tp->link_config.active_flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -08009918 epause->tx_pause = 1;
9919 else
9920 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009921}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009922
Linus Torvalds1da177e2005-04-16 15:20:36 -07009923static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
9924{
9925 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009926 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009927
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009928 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson27121682010-02-17 15:16:57 +00009929 u32 newadv;
9930 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009931
Matt Carlson27121682010-02-17 15:16:57 +00009932 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009933
Matt Carlson27121682010-02-17 15:16:57 +00009934 if (!(phydev->supported & SUPPORTED_Pause) ||
9935 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
9936 ((epause->rx_pause && !epause->tx_pause) ||
9937 (!epause->rx_pause && epause->tx_pause))))
9938 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009939
Matt Carlson27121682010-02-17 15:16:57 +00009940 tp->link_config.flowctrl = 0;
9941 if (epause->rx_pause) {
9942 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009943
Matt Carlson27121682010-02-17 15:16:57 +00009944 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -08009945 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +00009946 newadv = ADVERTISED_Pause;
9947 } else
9948 newadv = ADVERTISED_Pause |
9949 ADVERTISED_Asym_Pause;
9950 } else if (epause->tx_pause) {
9951 tp->link_config.flowctrl |= FLOW_CTRL_TX;
9952 newadv = ADVERTISED_Asym_Pause;
9953 } else
9954 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009955
Matt Carlson27121682010-02-17 15:16:57 +00009956 if (epause->autoneg)
9957 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
9958 else
9959 tp->tg3_flags &= ~TG3_FLAG_PAUSE_AUTONEG;
9960
9961 if (tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED) {
9962 u32 oldadv = phydev->advertising &
9963 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
9964 if (oldadv != newadv) {
9965 phydev->advertising &=
9966 ~(ADVERTISED_Pause |
9967 ADVERTISED_Asym_Pause);
9968 phydev->advertising |= newadv;
9969 if (phydev->autoneg) {
9970 /*
9971 * Always renegotiate the link to
9972 * inform our link partner of our
9973 * flow control settings, even if the
9974 * flow control is forced. Let
9975 * tg3_adjust_link() do the final
9976 * flow control setup.
9977 */
9978 return phy_start_aneg(phydev);
9979 }
9980 }
9981
9982 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009983 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +00009984 } else {
9985 tp->link_config.orig_advertising &=
9986 ~(ADVERTISED_Pause |
9987 ADVERTISED_Asym_Pause);
9988 tp->link_config.orig_advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009989 }
9990 } else {
9991 int irq_sync = 0;
9992
9993 if (netif_running(dev)) {
9994 tg3_netif_stop(tp);
9995 irq_sync = 1;
9996 }
9997
9998 tg3_full_lock(tp, irq_sync);
9999
10000 if (epause->autoneg)
10001 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
10002 else
10003 tp->tg3_flags &= ~TG3_FLAG_PAUSE_AUTONEG;
10004 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010005 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010006 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010007 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010008 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010009 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010010 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010011 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010012
10013 if (netif_running(dev)) {
10014 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10015 err = tg3_restart_hw(tp, 1);
10016 if (!err)
10017 tg3_netif_start(tp);
10018 }
10019
10020 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010021 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010022
Michael Chanb9ec6c12006-07-25 16:37:27 -070010023 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010024}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010025
Linus Torvalds1da177e2005-04-16 15:20:36 -070010026static u32 tg3_get_rx_csum(struct net_device *dev)
10027{
10028 struct tg3 *tp = netdev_priv(dev);
10029 return (tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) != 0;
10030}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010031
Linus Torvalds1da177e2005-04-16 15:20:36 -070010032static int tg3_set_rx_csum(struct net_device *dev, u32 data)
10033{
10034 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010035
Linus Torvalds1da177e2005-04-16 15:20:36 -070010036 if (tp->tg3_flags & TG3_FLAG_BROKEN_CHECKSUMS) {
10037 if (data != 0)
10038 return -EINVAL;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010039 return 0;
10040 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010041
David S. Millerf47c11e2005-06-24 20:18:35 -070010042 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010043 if (data)
10044 tp->tg3_flags |= TG3_FLAG_RX_CHECKSUMS;
10045 else
10046 tp->tg3_flags &= ~TG3_FLAG_RX_CHECKSUMS;
David S. Millerf47c11e2005-06-24 20:18:35 -070010047 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010048
Linus Torvalds1da177e2005-04-16 15:20:36 -070010049 return 0;
10050}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010051
Linus Torvalds1da177e2005-04-16 15:20:36 -070010052static int tg3_set_tx_csum(struct net_device *dev, u32 data)
10053{
10054 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010055
Linus Torvalds1da177e2005-04-16 15:20:36 -070010056 if (tp->tg3_flags & TG3_FLAG_BROKEN_CHECKSUMS) {
10057 if (data != 0)
10058 return -EINVAL;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010059 return 0;
10060 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010061
Matt Carlson321d32a2008-11-21 17:22:19 -080010062 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Michael Chan6460d942007-07-14 19:07:52 -070010063 ethtool_op_set_tx_ipv6_csum(dev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010064 else
Michael Chan9c27dbd2006-03-20 22:28:27 -080010065 ethtool_op_set_tx_csum(dev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010066
10067 return 0;
10068}
10069
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010070static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010071{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010072 switch (sset) {
10073 case ETH_SS_TEST:
10074 return TG3_NUM_TEST;
10075 case ETH_SS_STATS:
10076 return TG3_NUM_STATS;
10077 default:
10078 return -EOPNOTSUPP;
10079 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070010080}
10081
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010082static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010083{
10084 switch (stringset) {
10085 case ETH_SS_STATS:
10086 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
10087 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070010088 case ETH_SS_TEST:
10089 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
10090 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010091 default:
10092 WARN_ON(1); /* we need a WARN() */
10093 break;
10094 }
10095}
10096
Michael Chan4009a932005-09-05 17:52:54 -070010097static int tg3_phys_id(struct net_device *dev, u32 data)
10098{
10099 struct tg3 *tp = netdev_priv(dev);
10100 int i;
10101
10102 if (!netif_running(tp->dev))
10103 return -EAGAIN;
10104
10105 if (data == 0)
Stephen Hemminger759afc32008-02-23 19:51:59 -080010106 data = UINT_MAX / 2;
Michael Chan4009a932005-09-05 17:52:54 -070010107
10108 for (i = 0; i < (data * 2); i++) {
10109 if ((i % 2) == 0)
10110 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10111 LED_CTRL_1000MBPS_ON |
10112 LED_CTRL_100MBPS_ON |
10113 LED_CTRL_10MBPS_ON |
10114 LED_CTRL_TRAFFIC_OVERRIDE |
10115 LED_CTRL_TRAFFIC_BLINK |
10116 LED_CTRL_TRAFFIC_LED);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010117
Michael Chan4009a932005-09-05 17:52:54 -070010118 else
10119 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10120 LED_CTRL_TRAFFIC_OVERRIDE);
10121
10122 if (msleep_interruptible(500))
10123 break;
10124 }
10125 tw32(MAC_LED_CTRL, tp->led_ctrl);
10126 return 0;
10127}
10128
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010129static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010130 struct ethtool_stats *estats, u64 *tmp_stats)
10131{
10132 struct tg3 *tp = netdev_priv(dev);
10133 memcpy(tmp_stats, tg3_get_estats(tp), sizeof(tp->estats));
10134}
10135
Michael Chan566f86a2005-05-29 14:56:58 -070010136#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080010137#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
10138#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
10139#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Michael Chanb16250e2006-09-27 16:10:14 -070010140#define NVRAM_SELFBOOT_HW_SIZE 0x20
10141#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070010142
10143static int tg3_test_nvram(struct tg3 *tp)
10144{
Al Virob9fc7dc2007-12-17 22:59:57 -080010145 u32 csum, magic;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010146 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010147 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070010148
Matt Carlsondf259d82009-04-20 06:57:14 +000010149 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM)
10150 return 0;
10151
Matt Carlsone4f34112009-02-25 14:25:00 +000010152 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080010153 return -EIO;
10154
Michael Chan1b277772006-03-20 22:27:48 -080010155 if (magic == TG3_EEPROM_MAGIC)
10156 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070010157 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080010158 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
10159 TG3_EEPROM_SB_FORMAT_1) {
10160 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
10161 case TG3_EEPROM_SB_REVISION_0:
10162 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
10163 break;
10164 case TG3_EEPROM_SB_REVISION_2:
10165 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
10166 break;
10167 case TG3_EEPROM_SB_REVISION_3:
10168 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
10169 break;
10170 default:
10171 return 0;
10172 }
10173 } else
Michael Chan1b277772006-03-20 22:27:48 -080010174 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070010175 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
10176 size = NVRAM_SELFBOOT_HW_SIZE;
10177 else
Michael Chan1b277772006-03-20 22:27:48 -080010178 return -EIO;
10179
10180 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070010181 if (buf == NULL)
10182 return -ENOMEM;
10183
Michael Chan1b277772006-03-20 22:27:48 -080010184 err = -EIO;
10185 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010186 err = tg3_nvram_read_be32(tp, i, &buf[j]);
10187 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070010188 break;
Michael Chan566f86a2005-05-29 14:56:58 -070010189 }
Michael Chan1b277772006-03-20 22:27:48 -080010190 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070010191 goto out;
10192
Michael Chan1b277772006-03-20 22:27:48 -080010193 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010194 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080010195 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010196 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080010197 u8 *buf8 = (u8 *) buf, csum8 = 0;
10198
Al Virob9fc7dc2007-12-17 22:59:57 -080010199 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080010200 TG3_EEPROM_SB_REVISION_2) {
10201 /* For rev 2, the csum doesn't include the MBA. */
10202 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
10203 csum8 += buf8[i];
10204 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
10205 csum8 += buf8[i];
10206 } else {
10207 for (i = 0; i < size; i++)
10208 csum8 += buf8[i];
10209 }
Michael Chan1b277772006-03-20 22:27:48 -080010210
Adrian Bunkad96b482006-04-05 22:21:04 -070010211 if (csum8 == 0) {
10212 err = 0;
10213 goto out;
10214 }
10215
10216 err = -EIO;
10217 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080010218 }
Michael Chan566f86a2005-05-29 14:56:58 -070010219
Al Virob9fc7dc2007-12-17 22:59:57 -080010220 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010221 TG3_EEPROM_MAGIC_HW) {
10222 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000010223 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070010224 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070010225
10226 /* Separate the parity bits and the data bytes. */
10227 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
10228 if ((i == 0) || (i == 8)) {
10229 int l;
10230 u8 msk;
10231
10232 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
10233 parity[k++] = buf8[i] & msk;
10234 i++;
Matt Carlson859a588792010-04-05 10:19:28 +000010235 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070010236 int l;
10237 u8 msk;
10238
10239 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
10240 parity[k++] = buf8[i] & msk;
10241 i++;
10242
10243 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
10244 parity[k++] = buf8[i] & msk;
10245 i++;
10246 }
10247 data[j++] = buf8[i];
10248 }
10249
10250 err = -EIO;
10251 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
10252 u8 hw8 = hweight8(data[i]);
10253
10254 if ((hw8 & 0x1) && parity[i])
10255 goto out;
10256 else if (!(hw8 & 0x1) && !parity[i])
10257 goto out;
10258 }
10259 err = 0;
10260 goto out;
10261 }
10262
Michael Chan566f86a2005-05-29 14:56:58 -070010263 /* Bootstrap checksum at offset 0x10 */
10264 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlsona9dc5292009-02-25 14:25:30 +000010265 if (csum != be32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070010266 goto out;
10267
10268 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
10269 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlsona9dc5292009-02-25 14:25:30 +000010270 if (csum != be32_to_cpu(buf[0xfc/4]))
10271 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070010272
10273 err = 0;
10274
10275out:
10276 kfree(buf);
10277 return err;
10278}
10279
Michael Chanca430072005-05-29 14:57:23 -070010280#define TG3_SERDES_TIMEOUT_SEC 2
10281#define TG3_COPPER_TIMEOUT_SEC 6
10282
10283static int tg3_test_link(struct tg3 *tp)
10284{
10285 int i, max;
10286
10287 if (!netif_running(tp->dev))
10288 return -ENODEV;
10289
Michael Chan4c987482005-09-05 17:52:38 -070010290 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070010291 max = TG3_SERDES_TIMEOUT_SEC;
10292 else
10293 max = TG3_COPPER_TIMEOUT_SEC;
10294
10295 for (i = 0; i < max; i++) {
10296 if (netif_carrier_ok(tp->dev))
10297 return 0;
10298
10299 if (msleep_interruptible(1000))
10300 break;
10301 }
10302
10303 return -EIO;
10304}
10305
Michael Chana71116d2005-05-29 14:58:11 -070010306/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080010307static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070010308{
Michael Chanb16250e2006-09-27 16:10:14 -070010309 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070010310 u32 offset, read_mask, write_mask, val, save_val, read_val;
10311 static struct {
10312 u16 offset;
10313 u16 flags;
10314#define TG3_FL_5705 0x1
10315#define TG3_FL_NOT_5705 0x2
10316#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070010317#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070010318 u32 read_mask;
10319 u32 write_mask;
10320 } reg_tbl[] = {
10321 /* MAC Control Registers */
10322 { MAC_MODE, TG3_FL_NOT_5705,
10323 0x00000000, 0x00ef6f8c },
10324 { MAC_MODE, TG3_FL_5705,
10325 0x00000000, 0x01ef6b8c },
10326 { MAC_STATUS, TG3_FL_NOT_5705,
10327 0x03800107, 0x00000000 },
10328 { MAC_STATUS, TG3_FL_5705,
10329 0x03800100, 0x00000000 },
10330 { MAC_ADDR_0_HIGH, 0x0000,
10331 0x00000000, 0x0000ffff },
10332 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010333 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070010334 { MAC_RX_MTU_SIZE, 0x0000,
10335 0x00000000, 0x0000ffff },
10336 { MAC_TX_MODE, 0x0000,
10337 0x00000000, 0x00000070 },
10338 { MAC_TX_LENGTHS, 0x0000,
10339 0x00000000, 0x00003fff },
10340 { MAC_RX_MODE, TG3_FL_NOT_5705,
10341 0x00000000, 0x000007fc },
10342 { MAC_RX_MODE, TG3_FL_5705,
10343 0x00000000, 0x000007dc },
10344 { MAC_HASH_REG_0, 0x0000,
10345 0x00000000, 0xffffffff },
10346 { MAC_HASH_REG_1, 0x0000,
10347 0x00000000, 0xffffffff },
10348 { MAC_HASH_REG_2, 0x0000,
10349 0x00000000, 0xffffffff },
10350 { MAC_HASH_REG_3, 0x0000,
10351 0x00000000, 0xffffffff },
10352
10353 /* Receive Data and Receive BD Initiator Control Registers. */
10354 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
10355 0x00000000, 0xffffffff },
10356 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
10357 0x00000000, 0xffffffff },
10358 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
10359 0x00000000, 0x00000003 },
10360 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
10361 0x00000000, 0xffffffff },
10362 { RCVDBDI_STD_BD+0, 0x0000,
10363 0x00000000, 0xffffffff },
10364 { RCVDBDI_STD_BD+4, 0x0000,
10365 0x00000000, 0xffffffff },
10366 { RCVDBDI_STD_BD+8, 0x0000,
10367 0x00000000, 0xffff0002 },
10368 { RCVDBDI_STD_BD+0xc, 0x0000,
10369 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010370
Michael Chana71116d2005-05-29 14:58:11 -070010371 /* Receive BD Initiator Control Registers. */
10372 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
10373 0x00000000, 0xffffffff },
10374 { RCVBDI_STD_THRESH, TG3_FL_5705,
10375 0x00000000, 0x000003ff },
10376 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
10377 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010378
Michael Chana71116d2005-05-29 14:58:11 -070010379 /* Host Coalescing Control Registers. */
10380 { HOSTCC_MODE, TG3_FL_NOT_5705,
10381 0x00000000, 0x00000004 },
10382 { HOSTCC_MODE, TG3_FL_5705,
10383 0x00000000, 0x000000f6 },
10384 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
10385 0x00000000, 0xffffffff },
10386 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
10387 0x00000000, 0x000003ff },
10388 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
10389 0x00000000, 0xffffffff },
10390 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
10391 0x00000000, 0x000003ff },
10392 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
10393 0x00000000, 0xffffffff },
10394 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10395 0x00000000, 0x000000ff },
10396 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
10397 0x00000000, 0xffffffff },
10398 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10399 0x00000000, 0x000000ff },
10400 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
10401 0x00000000, 0xffffffff },
10402 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
10403 0x00000000, 0xffffffff },
10404 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10405 0x00000000, 0xffffffff },
10406 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10407 0x00000000, 0x000000ff },
10408 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10409 0x00000000, 0xffffffff },
10410 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10411 0x00000000, 0x000000ff },
10412 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
10413 0x00000000, 0xffffffff },
10414 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
10415 0x00000000, 0xffffffff },
10416 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
10417 0x00000000, 0xffffffff },
10418 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
10419 0x00000000, 0xffffffff },
10420 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
10421 0x00000000, 0xffffffff },
10422 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
10423 0xffffffff, 0x00000000 },
10424 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
10425 0xffffffff, 0x00000000 },
10426
10427 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070010428 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010429 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070010430 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010431 0x00000000, 0x007fffff },
10432 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
10433 0x00000000, 0x0000003f },
10434 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
10435 0x00000000, 0x000001ff },
10436 { BUFMGR_MB_HIGH_WATER, 0x0000,
10437 0x00000000, 0x000001ff },
10438 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
10439 0xffffffff, 0x00000000 },
10440 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
10441 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010442
Michael Chana71116d2005-05-29 14:58:11 -070010443 /* Mailbox Registers */
10444 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
10445 0x00000000, 0x000001ff },
10446 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
10447 0x00000000, 0x000001ff },
10448 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
10449 0x00000000, 0x000007ff },
10450 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
10451 0x00000000, 0x000001ff },
10452
10453 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
10454 };
10455
Michael Chanb16250e2006-09-27 16:10:14 -070010456 is_5705 = is_5750 = 0;
10457 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
Michael Chana71116d2005-05-29 14:58:11 -070010458 is_5705 = 1;
Michael Chanb16250e2006-09-27 16:10:14 -070010459 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
10460 is_5750 = 1;
10461 }
Michael Chana71116d2005-05-29 14:58:11 -070010462
10463 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
10464 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
10465 continue;
10466
10467 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
10468 continue;
10469
10470 if ((tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
10471 (reg_tbl[i].flags & TG3_FL_NOT_5788))
10472 continue;
10473
Michael Chanb16250e2006-09-27 16:10:14 -070010474 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
10475 continue;
10476
Michael Chana71116d2005-05-29 14:58:11 -070010477 offset = (u32) reg_tbl[i].offset;
10478 read_mask = reg_tbl[i].read_mask;
10479 write_mask = reg_tbl[i].write_mask;
10480
10481 /* Save the original register content */
10482 save_val = tr32(offset);
10483
10484 /* Determine the read-only value. */
10485 read_val = save_val & read_mask;
10486
10487 /* Write zero to the register, then make sure the read-only bits
10488 * are not changed and the read/write bits are all zeros.
10489 */
10490 tw32(offset, 0);
10491
10492 val = tr32(offset);
10493
10494 /* Test the read-only and read/write bits. */
10495 if (((val & read_mask) != read_val) || (val & write_mask))
10496 goto out;
10497
10498 /* Write ones to all the bits defined by RdMask and WrMask, then
10499 * make sure the read-only bits are not changed and the
10500 * read/write bits are all ones.
10501 */
10502 tw32(offset, read_mask | write_mask);
10503
10504 val = tr32(offset);
10505
10506 /* Test the read-only bits. */
10507 if ((val & read_mask) != read_val)
10508 goto out;
10509
10510 /* Test the read/write bits. */
10511 if ((val & write_mask) != write_mask)
10512 goto out;
10513
10514 tw32(offset, save_val);
10515 }
10516
10517 return 0;
10518
10519out:
Michael Chan9f88f292006-12-07 00:22:54 -080010520 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000010521 netdev_err(tp->dev,
10522 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070010523 tw32(offset, save_val);
10524 return -EIO;
10525}
10526
Michael Chan7942e1d2005-05-29 14:58:36 -070010527static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
10528{
Arjan van de Venf71e1302006-03-03 21:33:57 -050010529 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070010530 int i;
10531 u32 j;
10532
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020010533 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070010534 for (j = 0; j < len; j += 4) {
10535 u32 val;
10536
10537 tg3_write_mem(tp, offset + j, test_pattern[i]);
10538 tg3_read_mem(tp, offset + j, &val);
10539 if (val != test_pattern[i])
10540 return -EIO;
10541 }
10542 }
10543 return 0;
10544}
10545
10546static int tg3_test_memory(struct tg3 *tp)
10547{
10548 static struct mem_entry {
10549 u32 offset;
10550 u32 len;
10551 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080010552 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070010553 { 0x00002000, 0x1c000},
10554 { 0xffffffff, 0x00000}
10555 }, mem_tbl_5705[] = {
10556 { 0x00000100, 0x0000c},
10557 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070010558 { 0x00004000, 0x00800},
10559 { 0x00006000, 0x01000},
10560 { 0x00008000, 0x02000},
10561 { 0x00010000, 0x0e000},
10562 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080010563 }, mem_tbl_5755[] = {
10564 { 0x00000200, 0x00008},
10565 { 0x00004000, 0x00800},
10566 { 0x00006000, 0x00800},
10567 { 0x00008000, 0x02000},
10568 { 0x00010000, 0x0c000},
10569 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070010570 }, mem_tbl_5906[] = {
10571 { 0x00000200, 0x00008},
10572 { 0x00004000, 0x00400},
10573 { 0x00006000, 0x00400},
10574 { 0x00008000, 0x01000},
10575 { 0x00010000, 0x01000},
10576 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000010577 }, mem_tbl_5717[] = {
10578 { 0x00000200, 0x00008},
10579 { 0x00010000, 0x0a000},
10580 { 0x00020000, 0x13c00},
10581 { 0xffffffff, 0x00000}
10582 }, mem_tbl_57765[] = {
10583 { 0x00000200, 0x00008},
10584 { 0x00004000, 0x00800},
10585 { 0x00006000, 0x09800},
10586 { 0x00010000, 0x0a000},
10587 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070010588 };
10589 struct mem_entry *mem_tbl;
10590 int err = 0;
10591 int i;
10592
Matt Carlsona50d0792010-06-05 17:24:37 +000010593 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
10594 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlson8b5a6c42010-01-20 16:58:06 +000010595 mem_tbl = mem_tbl_5717;
10596 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
10597 mem_tbl = mem_tbl_57765;
10598 else if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Matt Carlson321d32a2008-11-21 17:22:19 -080010599 mem_tbl = mem_tbl_5755;
10600 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
10601 mem_tbl = mem_tbl_5906;
10602 else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
10603 mem_tbl = mem_tbl_5705;
10604 else
Michael Chan7942e1d2005-05-29 14:58:36 -070010605 mem_tbl = mem_tbl_570x;
10606
10607 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
10608 if ((err = tg3_do_mem_test(tp, mem_tbl[i].offset,
10609 mem_tbl[i].len)) != 0)
10610 break;
10611 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010612
Michael Chan7942e1d2005-05-29 14:58:36 -070010613 return err;
10614}
10615
Michael Chan9f40dea2005-09-05 17:53:06 -070010616#define TG3_MAC_LOOPBACK 0
10617#define TG3_PHY_LOOPBACK 1
10618
10619static int tg3_run_loopback(struct tg3 *tp, int loopback_mode)
Michael Chanc76949a2005-05-29 14:58:59 -070010620{
Michael Chan9f40dea2005-09-05 17:53:06 -070010621 u32 mac_mode, rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010622 u32 desc_idx, coal_now;
Michael Chanc76949a2005-05-29 14:58:59 -070010623 struct sk_buff *skb, *rx_skb;
10624 u8 *tx_data;
10625 dma_addr_t map;
10626 int num_pkts, tx_len, rx_len, i, err;
10627 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000010628 struct tg3_napi *tnapi, *rnapi;
Matt Carlson21f581a2009-08-28 14:00:25 +000010629 struct tg3_rx_prodring_set *tpr = &tp->prodring[0];
Michael Chanc76949a2005-05-29 14:58:59 -070010630
Matt Carlsonc8873402010-02-12 14:47:11 +000010631 tnapi = &tp->napi[0];
10632 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000010633 if (tp->irq_cnt > 1) {
Matt Carlson0c1d0e22009-09-01 13:16:33 +000010634 rnapi = &tp->napi[1];
Matt Carlsonc8873402010-02-12 14:47:11 +000010635 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
10636 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000010637 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010638 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000010639
Michael Chan9f40dea2005-09-05 17:53:06 -070010640 if (loopback_mode == TG3_MAC_LOOPBACK) {
Michael Chanc94e3942005-09-27 12:12:42 -070010641 /* HW errata - mac loopback fails in some cases on 5780.
10642 * Normal traffic and PHY loopback are not affected by
10643 * errata.
10644 */
10645 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780)
10646 return 0;
10647
Michael Chan9f40dea2005-09-05 17:53:06 -070010648 mac_mode = (tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK) |
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010649 MAC_MODE_PORT_INT_LPBACK;
10650 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
10651 mac_mode |= MAC_MODE_LINK_POLARITY;
Michael Chan3f7045c2006-09-27 16:02:29 -070010652 if (tp->tg3_flags & TG3_FLAG_10_100_ONLY)
10653 mac_mode |= MAC_MODE_PORT_MODE_MII;
10654 else
10655 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chan9f40dea2005-09-05 17:53:06 -070010656 tw32(MAC_MODE, mac_mode);
10657 } else if (loopback_mode == TG3_PHY_LOOPBACK) {
Michael Chan3f7045c2006-09-27 16:02:29 -070010658 u32 val;
10659
Matt Carlson7f97a4b2009-08-25 10:10:03 +000010660 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
10661 tg3_phy_fet_toggle_apd(tp, false);
Michael Chan5d64ad32006-12-07 00:19:40 -080010662 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED100;
10663 } else
10664 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED1000;
Michael Chan3f7045c2006-09-27 16:02:29 -070010665
Matt Carlson9ef8ca92007-07-11 19:48:29 -070010666 tg3_phy_toggle_automdix(tp, 0);
10667
Michael Chan3f7045c2006-09-27 16:02:29 -070010668 tg3_writephy(tp, MII_BMCR, val);
Michael Chanc94e3942005-09-27 12:12:42 -070010669 udelay(40);
Michael Chan5d64ad32006-12-07 00:19:40 -080010670
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010671 mac_mode = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000010672 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
Matt Carlson1061b7c2010-02-12 14:47:12 +000010673 tg3_writephy(tp, MII_TG3_FET_PTEST,
10674 MII_TG3_FET_PTEST_FRC_TX_LINK |
10675 MII_TG3_FET_PTEST_FRC_TX_LOCK);
10676 /* The write needs to be flushed for the AC131 */
10677 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
10678 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
Michael Chan5d64ad32006-12-07 00:19:40 -080010679 mac_mode |= MAC_MODE_PORT_MODE_MII;
10680 } else
10681 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chanb16250e2006-09-27 16:10:14 -070010682
Michael Chanc94e3942005-09-27 12:12:42 -070010683 /* reset to prevent losing 1st rx packet intermittently */
10684 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES) {
10685 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
10686 udelay(10);
10687 tw32_f(MAC_RX_MODE, tp->rx_mode);
10688 }
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010689 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlson79eb6902010-02-17 15:17:03 +000010690 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
10691 if (masked_phy_id == TG3_PHY_ID_BCM5401)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010692 mac_mode &= ~MAC_MODE_LINK_POLARITY;
Matt Carlson79eb6902010-02-17 15:17:03 +000010693 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010694 mac_mode |= MAC_MODE_LINK_POLARITY;
Michael Chanff18ff02006-03-27 23:17:27 -080010695 tg3_writephy(tp, MII_TG3_EXT_CTRL,
10696 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
10697 }
Michael Chan9f40dea2005-09-05 17:53:06 -070010698 tw32(MAC_MODE, mac_mode);
Matt Carlson859a588792010-04-05 10:19:28 +000010699 } else {
Michael Chan9f40dea2005-09-05 17:53:06 -070010700 return -EINVAL;
Matt Carlson859a588792010-04-05 10:19:28 +000010701 }
Michael Chanc76949a2005-05-29 14:58:59 -070010702
10703 err = -EIO;
10704
Michael Chanc76949a2005-05-29 14:58:59 -070010705 tx_len = 1514;
David S. Millera20e9c62006-07-31 22:38:16 -070010706 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070010707 if (!skb)
10708 return -ENOMEM;
10709
Michael Chanc76949a2005-05-29 14:58:59 -070010710 tx_data = skb_put(skb, tx_len);
10711 memcpy(tx_data, tp->dev->dev_addr, 6);
10712 memset(tx_data + 6, 0x0, 8);
10713
10714 tw32(MAC_RX_MTU_SIZE, tx_len + 4);
10715
10716 for (i = 14; i < tx_len; i++)
10717 tx_data[i] = (u8) (i & 0xff);
10718
Alexander Duyckf4188d82009-12-02 16:48:38 +000010719 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
10720 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000010721 dev_kfree_skb(skb);
10722 return -EIO;
10723 }
Michael Chanc76949a2005-05-29 14:58:59 -070010724
10725 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010726 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070010727
10728 udelay(10);
10729
Matt Carlson898a56f2009-08-28 14:02:40 +000010730 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070010731
Michael Chanc76949a2005-05-29 14:58:59 -070010732 num_pkts = 0;
10733
Alexander Duyckf4188d82009-12-02 16:48:38 +000010734 tg3_set_txd(tnapi, tnapi->tx_prod, map, tx_len, 0, 1);
Michael Chanc76949a2005-05-29 14:58:59 -070010735
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010736 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070010737 num_pkts++;
10738
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010739 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
10740 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070010741
10742 udelay(10);
10743
Matt Carlson303fc922009-11-02 14:27:34 +000010744 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
10745 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070010746 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010747 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070010748
10749 udelay(10);
10750
Matt Carlson898a56f2009-08-28 14:02:40 +000010751 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
10752 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010753 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070010754 (rx_idx == (rx_start_idx + num_pkts)))
10755 break;
10756 }
10757
Alexander Duyckf4188d82009-12-02 16:48:38 +000010758 pci_unmap_single(tp->pdev, map, tx_len, PCI_DMA_TODEVICE);
Michael Chanc76949a2005-05-29 14:58:59 -070010759 dev_kfree_skb(skb);
10760
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010761 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070010762 goto out;
10763
10764 if (rx_idx != rx_start_idx + num_pkts)
10765 goto out;
10766
Matt Carlson72334482009-08-28 14:03:01 +000010767 desc = &rnapi->rx_rcb[rx_start_idx];
Michael Chanc76949a2005-05-29 14:58:59 -070010768 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
10769 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
10770 if (opaque_key != RXD_OPAQUE_RING_STD)
10771 goto out;
10772
10773 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
10774 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
10775 goto out;
10776
10777 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) - 4;
10778 if (rx_len != tx_len)
10779 goto out;
10780
Matt Carlson21f581a2009-08-28 14:00:25 +000010781 rx_skb = tpr->rx_std_buffers[desc_idx].skb;
Michael Chanc76949a2005-05-29 14:58:59 -070010782
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +000010783 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx], mapping);
Michael Chanc76949a2005-05-29 14:58:59 -070010784 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len, PCI_DMA_FROMDEVICE);
10785
10786 for (i = 14; i < tx_len; i++) {
10787 if (*(rx_skb->data + i) != (u8) (i & 0xff))
10788 goto out;
10789 }
10790 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010791
Michael Chanc76949a2005-05-29 14:58:59 -070010792 /* tg3_free_rings will unmap and free the rx_skb */
10793out:
10794 return err;
10795}
10796
Michael Chan9f40dea2005-09-05 17:53:06 -070010797#define TG3_MAC_LOOPBACK_FAILED 1
10798#define TG3_PHY_LOOPBACK_FAILED 2
10799#define TG3_LOOPBACK_FAILED (TG3_MAC_LOOPBACK_FAILED | \
10800 TG3_PHY_LOOPBACK_FAILED)
10801
10802static int tg3_test_loopback(struct tg3 *tp)
10803{
10804 int err = 0;
Matt Carlson9936bcf2007-10-10 18:03:07 -070010805 u32 cpmuctrl = 0;
Michael Chan9f40dea2005-09-05 17:53:06 -070010806
10807 if (!netif_running(tp->dev))
10808 return TG3_LOOPBACK_FAILED;
10809
Michael Chanb9ec6c12006-07-25 16:37:27 -070010810 err = tg3_reset_hw(tp, 1);
10811 if (err)
10812 return TG3_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070010813
Matt Carlson6833c042008-11-21 17:18:59 -080010814 /* Turn off gphy autopowerdown. */
10815 if (tp->tg3_flags3 & TG3_FLG3_PHY_ENABLE_APD)
10816 tg3_phy_toggle_apd(tp, false);
10817
Matt Carlson321d32a2008-11-21 17:22:19 -080010818 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070010819 int i;
10820 u32 status;
10821
10822 tw32(TG3_CPMU_MUTEX_REQ, CPMU_MUTEX_REQ_DRIVER);
10823
10824 /* Wait for up to 40 microseconds to acquire lock. */
10825 for (i = 0; i < 4; i++) {
10826 status = tr32(TG3_CPMU_MUTEX_GNT);
10827 if (status == CPMU_MUTEX_GNT_DRIVER)
10828 break;
10829 udelay(10);
10830 }
10831
10832 if (status != CPMU_MUTEX_GNT_DRIVER)
10833 return TG3_LOOPBACK_FAILED;
10834
Matt Carlsonb2a5c192008-04-03 21:44:44 -070010835 /* Turn off link-based power management. */
Matt Carlsone8750932007-11-12 21:11:51 -080010836 cpmuctrl = tr32(TG3_CPMU_CTRL);
Matt Carlson109115e2008-05-02 16:48:59 -070010837 tw32(TG3_CPMU_CTRL,
10838 cpmuctrl & ~(CPMU_CTRL_LINK_SPEED_MODE |
10839 CPMU_CTRL_LINK_AWARE_MODE));
Matt Carlson9936bcf2007-10-10 18:03:07 -070010840 }
10841
Michael Chan9f40dea2005-09-05 17:53:06 -070010842 if (tg3_run_loopback(tp, TG3_MAC_LOOPBACK))
10843 err |= TG3_MAC_LOOPBACK_FAILED;
Matt Carlson9936bcf2007-10-10 18:03:07 -070010844
Matt Carlson321d32a2008-11-21 17:22:19 -080010845 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070010846 tw32(TG3_CPMU_CTRL, cpmuctrl);
10847
10848 /* Release the mutex */
10849 tw32(TG3_CPMU_MUTEX_GNT, CPMU_MUTEX_GNT_DRIVER);
10850 }
10851
Matt Carlsondd477002008-05-25 23:45:58 -070010852 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
10853 !(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)) {
Michael Chan9f40dea2005-09-05 17:53:06 -070010854 if (tg3_run_loopback(tp, TG3_PHY_LOOPBACK))
10855 err |= TG3_PHY_LOOPBACK_FAILED;
10856 }
10857
Matt Carlson6833c042008-11-21 17:18:59 -080010858 /* Re-enable gphy autopowerdown. */
10859 if (tp->tg3_flags3 & TG3_FLG3_PHY_ENABLE_APD)
10860 tg3_phy_toggle_apd(tp, true);
10861
Michael Chan9f40dea2005-09-05 17:53:06 -070010862 return err;
10863}
10864
Michael Chan4cafd3f2005-05-29 14:56:34 -070010865static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
10866 u64 *data)
10867{
Michael Chan566f86a2005-05-29 14:56:58 -070010868 struct tg3 *tp = netdev_priv(dev);
10869
Michael Chanbc1c7562006-03-20 17:48:03 -080010870 if (tp->link_config.phy_is_low_power)
10871 tg3_set_power_state(tp, PCI_D0);
10872
Michael Chan566f86a2005-05-29 14:56:58 -070010873 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
10874
10875 if (tg3_test_nvram(tp) != 0) {
10876 etest->flags |= ETH_TEST_FL_FAILED;
10877 data[0] = 1;
10878 }
Michael Chanca430072005-05-29 14:57:23 -070010879 if (tg3_test_link(tp) != 0) {
10880 etest->flags |= ETH_TEST_FL_FAILED;
10881 data[1] = 1;
10882 }
Michael Chana71116d2005-05-29 14:58:11 -070010883 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010884 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070010885
Michael Chanbbe832c2005-06-24 20:20:04 -070010886 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010887 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010888 tg3_netif_stop(tp);
10889 irq_sync = 1;
10890 }
10891
10892 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070010893
10894 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080010895 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070010896 tg3_halt_cpu(tp, RX_CPU_BASE);
10897 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
10898 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080010899 if (!err)
10900 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070010901
Michael Chand9ab5ad2006-03-20 22:27:35 -080010902 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)
10903 tg3_phy_reset(tp);
10904
Michael Chana71116d2005-05-29 14:58:11 -070010905 if (tg3_test_registers(tp) != 0) {
10906 etest->flags |= ETH_TEST_FL_FAILED;
10907 data[2] = 1;
10908 }
Michael Chan7942e1d2005-05-29 14:58:36 -070010909 if (tg3_test_memory(tp) != 0) {
10910 etest->flags |= ETH_TEST_FL_FAILED;
10911 data[3] = 1;
10912 }
Michael Chan9f40dea2005-09-05 17:53:06 -070010913 if ((data[4] = tg3_test_loopback(tp)) != 0)
Michael Chanc76949a2005-05-29 14:58:59 -070010914 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070010915
David S. Millerf47c11e2005-06-24 20:18:35 -070010916 tg3_full_unlock(tp);
10917
Michael Chand4bc3922005-05-29 14:59:20 -070010918 if (tg3_test_interrupt(tp) != 0) {
10919 etest->flags |= ETH_TEST_FL_FAILED;
10920 data[5] = 1;
10921 }
David S. Millerf47c11e2005-06-24 20:18:35 -070010922
10923 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070010924
Michael Chana71116d2005-05-29 14:58:11 -070010925 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10926 if (netif_running(dev)) {
10927 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010928 err2 = tg3_restart_hw(tp, 1);
10929 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070010930 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070010931 }
David S. Millerf47c11e2005-06-24 20:18:35 -070010932
10933 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010934
10935 if (irq_sync && !err2)
10936 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070010937 }
Michael Chanbc1c7562006-03-20 17:48:03 -080010938 if (tp->link_config.phy_is_low_power)
10939 tg3_set_power_state(tp, PCI_D3hot);
10940
Michael Chan4cafd3f2005-05-29 14:56:34 -070010941}
10942
Linus Torvalds1da177e2005-04-16 15:20:36 -070010943static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
10944{
10945 struct mii_ioctl_data *data = if_mii(ifr);
10946 struct tg3 *tp = netdev_priv(dev);
10947 int err;
10948
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010949 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010950 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010951 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
10952 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010953 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10954 return phy_mii_ioctl(phydev, data, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010955 }
10956
Matt Carlson33f401a2010-04-05 10:19:27 +000010957 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010958 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000010959 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010960
10961 /* fallthru */
10962 case SIOCGMIIREG: {
10963 u32 mii_regval;
10964
10965 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
10966 break; /* We have no PHY */
10967
Michael Chanbc1c7562006-03-20 17:48:03 -080010968 if (tp->link_config.phy_is_low_power)
10969 return -EAGAIN;
10970
David S. Millerf47c11e2005-06-24 20:18:35 -070010971 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010972 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070010973 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010974
10975 data->val_out = mii_regval;
10976
10977 return err;
10978 }
10979
10980 case SIOCSMIIREG:
10981 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
10982 break; /* We have no PHY */
10983
Michael Chanbc1c7562006-03-20 17:48:03 -080010984 if (tp->link_config.phy_is_low_power)
10985 return -EAGAIN;
10986
David S. Millerf47c11e2005-06-24 20:18:35 -070010987 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010988 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070010989 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010990
10991 return err;
10992
10993 default:
10994 /* do nothing */
10995 break;
10996 }
10997 return -EOPNOTSUPP;
10998}
10999
11000#if TG3_VLAN_TAG_USED
11001static void tg3_vlan_rx_register(struct net_device *dev, struct vlan_group *grp)
11002{
11003 struct tg3 *tp = netdev_priv(dev);
11004
Matt Carlson844b3ee2009-02-25 14:23:56 +000011005 if (!netif_running(dev)) {
11006 tp->vlgrp = grp;
11007 return;
11008 }
11009
11010 tg3_netif_stop(tp);
Michael Chan29315e82006-06-29 20:12:30 -070011011
David S. Millerf47c11e2005-06-24 20:18:35 -070011012 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011013
11014 tp->vlgrp = grp;
11015
11016 /* Update RX_MODE_KEEP_VLAN_TAG bit in RX_MODE register. */
11017 __tg3_set_rx_mode(dev);
11018
Matt Carlson844b3ee2009-02-25 14:23:56 +000011019 tg3_netif_start(tp);
Michael Chan46966542007-07-11 19:47:19 -070011020
11021 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011022}
Linus Torvalds1da177e2005-04-16 15:20:36 -070011023#endif
11024
David S. Miller15f98502005-05-18 22:49:26 -070011025static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11026{
11027 struct tg3 *tp = netdev_priv(dev);
11028
11029 memcpy(ec, &tp->coal, sizeof(*ec));
11030 return 0;
11031}
11032
Michael Chand244c892005-07-05 14:42:33 -070011033static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11034{
11035 struct tg3 *tp = netdev_priv(dev);
11036 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
11037 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
11038
11039 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
11040 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
11041 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
11042 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
11043 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
11044 }
11045
11046 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
11047 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
11048 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
11049 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
11050 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
11051 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
11052 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
11053 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
11054 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
11055 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
11056 return -EINVAL;
11057
11058 /* No rx interrupts will be generated if both are zero */
11059 if ((ec->rx_coalesce_usecs == 0) &&
11060 (ec->rx_max_coalesced_frames == 0))
11061 return -EINVAL;
11062
11063 /* No tx interrupts will be generated if both are zero */
11064 if ((ec->tx_coalesce_usecs == 0) &&
11065 (ec->tx_max_coalesced_frames == 0))
11066 return -EINVAL;
11067
11068 /* Only copy relevant parameters, ignore all others. */
11069 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
11070 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
11071 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
11072 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
11073 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
11074 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
11075 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
11076 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
11077 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
11078
11079 if (netif_running(dev)) {
11080 tg3_full_lock(tp, 0);
11081 __tg3_set_coalesce(tp, &tp->coal);
11082 tg3_full_unlock(tp);
11083 }
11084 return 0;
11085}
11086
Jeff Garzik7282d492006-09-13 14:30:00 -040011087static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011088 .get_settings = tg3_get_settings,
11089 .set_settings = tg3_set_settings,
11090 .get_drvinfo = tg3_get_drvinfo,
11091 .get_regs_len = tg3_get_regs_len,
11092 .get_regs = tg3_get_regs,
11093 .get_wol = tg3_get_wol,
11094 .set_wol = tg3_set_wol,
11095 .get_msglevel = tg3_get_msglevel,
11096 .set_msglevel = tg3_set_msglevel,
11097 .nway_reset = tg3_nway_reset,
11098 .get_link = ethtool_op_get_link,
11099 .get_eeprom_len = tg3_get_eeprom_len,
11100 .get_eeprom = tg3_get_eeprom,
11101 .set_eeprom = tg3_set_eeprom,
11102 .get_ringparam = tg3_get_ringparam,
11103 .set_ringparam = tg3_set_ringparam,
11104 .get_pauseparam = tg3_get_pauseparam,
11105 .set_pauseparam = tg3_set_pauseparam,
11106 .get_rx_csum = tg3_get_rx_csum,
11107 .set_rx_csum = tg3_set_rx_csum,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011108 .set_tx_csum = tg3_set_tx_csum,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011109 .set_sg = ethtool_op_set_sg,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011110 .set_tso = tg3_set_tso,
Michael Chan4cafd3f2005-05-29 14:56:34 -070011111 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011112 .get_strings = tg3_get_strings,
Michael Chan4009a932005-09-05 17:52:54 -070011113 .phys_id = tg3_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011114 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070011115 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070011116 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070011117 .get_sset_count = tg3_get_sset_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011118};
11119
11120static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
11121{
Michael Chan1b277772006-03-20 22:27:48 -080011122 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011123
11124 tp->nvram_size = EEPROM_CHIP_SIZE;
11125
Matt Carlsone4f34112009-02-25 14:25:00 +000011126 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011127 return;
11128
Michael Chanb16250e2006-09-27 16:10:14 -070011129 if ((magic != TG3_EEPROM_MAGIC) &&
11130 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
11131 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011132 return;
11133
11134 /*
11135 * Size the chip by reading offsets at increasing powers of two.
11136 * When we encounter our validation signature, we know the addressing
11137 * has wrapped around, and thus have our chip size.
11138 */
Michael Chan1b277772006-03-20 22:27:48 -080011139 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011140
11141 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000011142 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011143 return;
11144
Michael Chan18201802006-03-20 22:29:15 -080011145 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011146 break;
11147
11148 cursize <<= 1;
11149 }
11150
11151 tp->nvram_size = cursize;
11152}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011153
Linus Torvalds1da177e2005-04-16 15:20:36 -070011154static void __devinit tg3_get_nvram_size(struct tg3 *tp)
11155{
11156 u32 val;
11157
Matt Carlsondf259d82009-04-20 06:57:14 +000011158 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
11159 tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080011160 return;
11161
11162 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080011163 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080011164 tg3_get_eeprom_size(tp);
11165 return;
11166 }
11167
Matt Carlson6d348f22009-02-25 14:25:52 +000011168 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011169 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000011170 /* This is confusing. We want to operate on the
11171 * 16-bit value at offset 0xf2. The tg3_nvram_read()
11172 * call will read from NVRAM and byteswap the data
11173 * according to the byteswapping settings for all
11174 * other register accesses. This ensures the data we
11175 * want will always reside in the lower 16-bits.
11176 * However, the data in NVRAM is in LE format, which
11177 * means the data from the NVRAM read will always be
11178 * opposite the endianness of the CPU. The 16-bit
11179 * byteswap then brings the data to CPU endianness.
11180 */
11181 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011182 return;
11183 }
11184 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070011185 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011186}
11187
11188static void __devinit tg3_get_nvram_info(struct tg3 *tp)
11189{
11190 u32 nvcfg1;
11191
11192 nvcfg1 = tr32(NVRAM_CFG1);
11193 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
11194 tp->tg3_flags2 |= TG3_FLG2_FLASH;
Matt Carlson8590a602009-08-28 12:29:16 +000011195 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011196 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11197 tw32(NVRAM_CFG1, nvcfg1);
11198 }
11199
Michael Chan4c987482005-09-05 17:52:38 -070011200 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750) ||
Michael Chana4e2b342005-10-26 15:46:52 -070011201 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011202 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011203 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
11204 tp->nvram_jedecnum = JEDEC_ATMEL;
11205 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
11206 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11207 break;
11208 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
11209 tp->nvram_jedecnum = JEDEC_ATMEL;
11210 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
11211 break;
11212 case FLASH_VENDOR_ATMEL_EEPROM:
11213 tp->nvram_jedecnum = JEDEC_ATMEL;
11214 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11215 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11216 break;
11217 case FLASH_VENDOR_ST:
11218 tp->nvram_jedecnum = JEDEC_ST;
11219 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
11220 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11221 break;
11222 case FLASH_VENDOR_SAIFUN:
11223 tp->nvram_jedecnum = JEDEC_SAIFUN;
11224 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
11225 break;
11226 case FLASH_VENDOR_SST_SMALL:
11227 case FLASH_VENDOR_SST_LARGE:
11228 tp->nvram_jedecnum = JEDEC_SST;
11229 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
11230 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011231 }
Matt Carlson8590a602009-08-28 12:29:16 +000011232 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011233 tp->nvram_jedecnum = JEDEC_ATMEL;
11234 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
11235 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11236 }
11237}
11238
Matt Carlsona1b950d2009-09-01 13:20:17 +000011239static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
11240{
11241 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
11242 case FLASH_5752PAGE_SIZE_256:
11243 tp->nvram_pagesize = 256;
11244 break;
11245 case FLASH_5752PAGE_SIZE_512:
11246 tp->nvram_pagesize = 512;
11247 break;
11248 case FLASH_5752PAGE_SIZE_1K:
11249 tp->nvram_pagesize = 1024;
11250 break;
11251 case FLASH_5752PAGE_SIZE_2K:
11252 tp->nvram_pagesize = 2048;
11253 break;
11254 case FLASH_5752PAGE_SIZE_4K:
11255 tp->nvram_pagesize = 4096;
11256 break;
11257 case FLASH_5752PAGE_SIZE_264:
11258 tp->nvram_pagesize = 264;
11259 break;
11260 case FLASH_5752PAGE_SIZE_528:
11261 tp->nvram_pagesize = 528;
11262 break;
11263 }
11264}
11265
Michael Chan361b4ac2005-04-21 17:11:21 -070011266static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
11267{
11268 u32 nvcfg1;
11269
11270 nvcfg1 = tr32(NVRAM_CFG1);
11271
Michael Chane6af3012005-04-21 17:12:05 -070011272 /* NVRAM protection for TPM */
11273 if (nvcfg1 & (1 << 27))
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011274 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Michael Chane6af3012005-04-21 17:12:05 -070011275
Michael Chan361b4ac2005-04-21 17:11:21 -070011276 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011277 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
11278 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
11279 tp->nvram_jedecnum = JEDEC_ATMEL;
11280 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11281 break;
11282 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11283 tp->nvram_jedecnum = JEDEC_ATMEL;
11284 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11285 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11286 break;
11287 case FLASH_5752VENDOR_ST_M45PE10:
11288 case FLASH_5752VENDOR_ST_M45PE20:
11289 case FLASH_5752VENDOR_ST_M45PE40:
11290 tp->nvram_jedecnum = JEDEC_ST;
11291 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11292 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11293 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070011294 }
11295
11296 if (tp->tg3_flags2 & TG3_FLG2_FLASH) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000011297 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000011298 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070011299 /* For eeprom, set pagesize to maximum eeprom size */
11300 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11301
11302 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11303 tw32(NVRAM_CFG1, nvcfg1);
11304 }
11305}
11306
Michael Chand3c7b882006-03-23 01:28:25 -080011307static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
11308{
Matt Carlson989a9d22007-05-05 11:51:05 -070011309 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080011310
11311 nvcfg1 = tr32(NVRAM_CFG1);
11312
11313 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070011314 if (nvcfg1 & (1 << 27)) {
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011315 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Matt Carlson989a9d22007-05-05 11:51:05 -070011316 protect = 1;
11317 }
Michael Chand3c7b882006-03-23 01:28:25 -080011318
Matt Carlson989a9d22007-05-05 11:51:05 -070011319 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11320 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011321 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11322 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11323 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11324 case FLASH_5755VENDOR_ATMEL_FLASH_5:
11325 tp->nvram_jedecnum = JEDEC_ATMEL;
11326 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11327 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11328 tp->nvram_pagesize = 264;
11329 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
11330 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
11331 tp->nvram_size = (protect ? 0x3e200 :
11332 TG3_NVRAM_SIZE_512KB);
11333 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
11334 tp->nvram_size = (protect ? 0x1f200 :
11335 TG3_NVRAM_SIZE_256KB);
11336 else
11337 tp->nvram_size = (protect ? 0x1f200 :
11338 TG3_NVRAM_SIZE_128KB);
11339 break;
11340 case FLASH_5752VENDOR_ST_M45PE10:
11341 case FLASH_5752VENDOR_ST_M45PE20:
11342 case FLASH_5752VENDOR_ST_M45PE40:
11343 tp->nvram_jedecnum = JEDEC_ST;
11344 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11345 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11346 tp->nvram_pagesize = 256;
11347 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
11348 tp->nvram_size = (protect ?
11349 TG3_NVRAM_SIZE_64KB :
11350 TG3_NVRAM_SIZE_128KB);
11351 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
11352 tp->nvram_size = (protect ?
11353 TG3_NVRAM_SIZE_64KB :
11354 TG3_NVRAM_SIZE_256KB);
11355 else
11356 tp->nvram_size = (protect ?
11357 TG3_NVRAM_SIZE_128KB :
11358 TG3_NVRAM_SIZE_512KB);
11359 break;
Michael Chand3c7b882006-03-23 01:28:25 -080011360 }
11361}
11362
Michael Chan1b277772006-03-20 22:27:48 -080011363static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
11364{
11365 u32 nvcfg1;
11366
11367 nvcfg1 = tr32(NVRAM_CFG1);
11368
11369 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011370 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
11371 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11372 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
11373 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11374 tp->nvram_jedecnum = JEDEC_ATMEL;
11375 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11376 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080011377
Matt Carlson8590a602009-08-28 12:29:16 +000011378 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11379 tw32(NVRAM_CFG1, nvcfg1);
11380 break;
11381 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11382 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11383 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11384 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11385 tp->nvram_jedecnum = JEDEC_ATMEL;
11386 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11387 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11388 tp->nvram_pagesize = 264;
11389 break;
11390 case FLASH_5752VENDOR_ST_M45PE10:
11391 case FLASH_5752VENDOR_ST_M45PE20:
11392 case FLASH_5752VENDOR_ST_M45PE40:
11393 tp->nvram_jedecnum = JEDEC_ST;
11394 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11395 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11396 tp->nvram_pagesize = 256;
11397 break;
Michael Chan1b277772006-03-20 22:27:48 -080011398 }
11399}
11400
Matt Carlson6b91fa02007-10-10 18:01:09 -070011401static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
11402{
11403 u32 nvcfg1, protect = 0;
11404
11405 nvcfg1 = tr32(NVRAM_CFG1);
11406
11407 /* NVRAM protection for TPM */
11408 if (nvcfg1 & (1 << 27)) {
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011409 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011410 protect = 1;
11411 }
11412
11413 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11414 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011415 case FLASH_5761VENDOR_ATMEL_ADB021D:
11416 case FLASH_5761VENDOR_ATMEL_ADB041D:
11417 case FLASH_5761VENDOR_ATMEL_ADB081D:
11418 case FLASH_5761VENDOR_ATMEL_ADB161D:
11419 case FLASH_5761VENDOR_ATMEL_MDB021D:
11420 case FLASH_5761VENDOR_ATMEL_MDB041D:
11421 case FLASH_5761VENDOR_ATMEL_MDB081D:
11422 case FLASH_5761VENDOR_ATMEL_MDB161D:
11423 tp->nvram_jedecnum = JEDEC_ATMEL;
11424 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11425 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11426 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
11427 tp->nvram_pagesize = 256;
11428 break;
11429 case FLASH_5761VENDOR_ST_A_M45PE20:
11430 case FLASH_5761VENDOR_ST_A_M45PE40:
11431 case FLASH_5761VENDOR_ST_A_M45PE80:
11432 case FLASH_5761VENDOR_ST_A_M45PE16:
11433 case FLASH_5761VENDOR_ST_M_M45PE20:
11434 case FLASH_5761VENDOR_ST_M_M45PE40:
11435 case FLASH_5761VENDOR_ST_M_M45PE80:
11436 case FLASH_5761VENDOR_ST_M_M45PE16:
11437 tp->nvram_jedecnum = JEDEC_ST;
11438 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11439 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11440 tp->nvram_pagesize = 256;
11441 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011442 }
11443
11444 if (protect) {
11445 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
11446 } else {
11447 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011448 case FLASH_5761VENDOR_ATMEL_ADB161D:
11449 case FLASH_5761VENDOR_ATMEL_MDB161D:
11450 case FLASH_5761VENDOR_ST_A_M45PE16:
11451 case FLASH_5761VENDOR_ST_M_M45PE16:
11452 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
11453 break;
11454 case FLASH_5761VENDOR_ATMEL_ADB081D:
11455 case FLASH_5761VENDOR_ATMEL_MDB081D:
11456 case FLASH_5761VENDOR_ST_A_M45PE80:
11457 case FLASH_5761VENDOR_ST_M_M45PE80:
11458 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
11459 break;
11460 case FLASH_5761VENDOR_ATMEL_ADB041D:
11461 case FLASH_5761VENDOR_ATMEL_MDB041D:
11462 case FLASH_5761VENDOR_ST_A_M45PE40:
11463 case FLASH_5761VENDOR_ST_M_M45PE40:
11464 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11465 break;
11466 case FLASH_5761VENDOR_ATMEL_ADB021D:
11467 case FLASH_5761VENDOR_ATMEL_MDB021D:
11468 case FLASH_5761VENDOR_ST_A_M45PE20:
11469 case FLASH_5761VENDOR_ST_M_M45PE20:
11470 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11471 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011472 }
11473 }
11474}
11475
Michael Chanb5d37722006-09-27 16:06:21 -070011476static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
11477{
11478 tp->nvram_jedecnum = JEDEC_ATMEL;
11479 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11480 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11481}
11482
Matt Carlson321d32a2008-11-21 17:22:19 -080011483static void __devinit tg3_get_57780_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_5787VENDOR_ATMEL_EEPROM_376KHZ:
11491 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
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_5752VENDOR_ATMEL_FLASH_BUFFERED:
11500 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11501 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11502 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11503 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11504 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11505 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
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_5752VENDOR_ATMEL_FLASH_BUFFERED:
11512 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11513 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11514 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11515 break;
11516 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11517 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11518 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11519 break;
11520 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11521 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
11522 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11523 break;
11524 }
11525 break;
11526 case FLASH_5752VENDOR_ST_M45PE10:
11527 case FLASH_5752VENDOR_ST_M45PE20:
11528 case FLASH_5752VENDOR_ST_M45PE40:
11529 tp->nvram_jedecnum = JEDEC_ST;
11530 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11531 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11532
11533 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11534 case FLASH_5752VENDOR_ST_M45PE10:
11535 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11536 break;
11537 case FLASH_5752VENDOR_ST_M45PE20:
11538 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11539 break;
11540 case FLASH_5752VENDOR_ST_M45PE40:
11541 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11542 break;
11543 }
11544 break;
11545 default:
Matt Carlsondf259d82009-04-20 06:57:14 +000011546 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
Matt Carlson321d32a2008-11-21 17:22:19 -080011547 return;
11548 }
11549
Matt Carlsona1b950d2009-09-01 13:20:17 +000011550 tg3_nvram_get_pagesize(tp, nvcfg1);
11551 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Matt Carlson321d32a2008-11-21 17:22:19 -080011552 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011553}
11554
11555
11556static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
11557{
11558 u32 nvcfg1;
11559
11560 nvcfg1 = tr32(NVRAM_CFG1);
11561
11562 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11563 case FLASH_5717VENDOR_ATMEL_EEPROM:
11564 case FLASH_5717VENDOR_MICRO_EEPROM:
11565 tp->nvram_jedecnum = JEDEC_ATMEL;
11566 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11567 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11568
11569 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11570 tw32(NVRAM_CFG1, nvcfg1);
11571 return;
11572 case FLASH_5717VENDOR_ATMEL_MDB011D:
11573 case FLASH_5717VENDOR_ATMEL_ADB011B:
11574 case FLASH_5717VENDOR_ATMEL_ADB011D:
11575 case FLASH_5717VENDOR_ATMEL_MDB021D:
11576 case FLASH_5717VENDOR_ATMEL_ADB021B:
11577 case FLASH_5717VENDOR_ATMEL_ADB021D:
11578 case FLASH_5717VENDOR_ATMEL_45USPT:
11579 tp->nvram_jedecnum = JEDEC_ATMEL;
11580 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11581 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11582
11583 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11584 case FLASH_5717VENDOR_ATMEL_MDB021D:
11585 case FLASH_5717VENDOR_ATMEL_ADB021B:
11586 case FLASH_5717VENDOR_ATMEL_ADB021D:
11587 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11588 break;
11589 default:
11590 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11591 break;
11592 }
Matt Carlson321d32a2008-11-21 17:22:19 -080011593 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011594 case FLASH_5717VENDOR_ST_M_M25PE10:
11595 case FLASH_5717VENDOR_ST_A_M25PE10:
11596 case FLASH_5717VENDOR_ST_M_M45PE10:
11597 case FLASH_5717VENDOR_ST_A_M45PE10:
11598 case FLASH_5717VENDOR_ST_M_M25PE20:
11599 case FLASH_5717VENDOR_ST_A_M25PE20:
11600 case FLASH_5717VENDOR_ST_M_M45PE20:
11601 case FLASH_5717VENDOR_ST_A_M45PE20:
11602 case FLASH_5717VENDOR_ST_25USPT:
11603 case FLASH_5717VENDOR_ST_45USPT:
11604 tp->nvram_jedecnum = JEDEC_ST;
11605 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11606 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11607
11608 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11609 case FLASH_5717VENDOR_ST_M_M25PE20:
11610 case FLASH_5717VENDOR_ST_A_M25PE20:
11611 case FLASH_5717VENDOR_ST_M_M45PE20:
11612 case FLASH_5717VENDOR_ST_A_M45PE20:
11613 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11614 break;
11615 default:
11616 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11617 break;
11618 }
Matt Carlson321d32a2008-11-21 17:22:19 -080011619 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011620 default:
11621 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
11622 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080011623 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000011624
11625 tg3_nvram_get_pagesize(tp, nvcfg1);
11626 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
11627 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
Matt Carlson321d32a2008-11-21 17:22:19 -080011628}
11629
Linus Torvalds1da177e2005-04-16 15:20:36 -070011630/* Chips other than 5700/5701 use the NVRAM for fetching info. */
11631static void __devinit tg3_nvram_init(struct tg3 *tp)
11632{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011633 tw32_f(GRC_EEPROM_ADDR,
11634 (EEPROM_ADDR_FSM_RESET |
11635 (EEPROM_DEFAULT_CLOCK_PERIOD <<
11636 EEPROM_ADDR_CLKPERD_SHIFT)));
11637
Michael Chan9d57f012006-12-07 00:23:25 -080011638 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011639
11640 /* Enable seeprom accesses. */
11641 tw32_f(GRC_LOCAL_CTRL,
11642 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
11643 udelay(100);
11644
11645 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
11646 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
11647 tp->tg3_flags |= TG3_FLAG_NVRAM;
11648
Michael Chanec41c7d2006-01-17 02:40:55 -080011649 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000011650 netdev_warn(tp->dev,
11651 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000011652 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080011653 return;
11654 }
Michael Chane6af3012005-04-21 17:12:05 -070011655 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011656
Matt Carlson989a9d22007-05-05 11:51:05 -070011657 tp->nvram_size = 0;
11658
Michael Chan361b4ac2005-04-21 17:11:21 -070011659 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
11660 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080011661 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
11662 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070011663 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070011664 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
11665 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080011666 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070011667 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
11668 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070011669 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
11670 tg3_get_5906_nvram_info(tp);
Matt Carlsonb703df62009-12-03 08:36:21 +000011671 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
11672 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson321d32a2008-11-21 17:22:19 -080011673 tg3_get_57780_nvram_info(tp);
Matt Carlsona50d0792010-06-05 17:24:37 +000011674 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
11675 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000011676 tg3_get_5717_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070011677 else
11678 tg3_get_nvram_info(tp);
11679
Matt Carlson989a9d22007-05-05 11:51:05 -070011680 if (tp->nvram_size == 0)
11681 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011682
Michael Chane6af3012005-04-21 17:12:05 -070011683 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080011684 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011685
11686 } else {
11687 tp->tg3_flags &= ~(TG3_FLAG_NVRAM | TG3_FLAG_NVRAM_BUFFERED);
11688
11689 tg3_get_eeprom_size(tp);
11690 }
11691}
11692
Linus Torvalds1da177e2005-04-16 15:20:36 -070011693static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
11694 u32 offset, u32 len, u8 *buf)
11695{
11696 int i, j, rc = 0;
11697 u32 val;
11698
11699 for (i = 0; i < len; i += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080011700 u32 addr;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011701 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011702
11703 addr = offset + i;
11704
11705 memcpy(&data, buf + i, 4);
11706
Matt Carlson62cedd12009-04-20 14:52:29 -070011707 /*
11708 * The SEEPROM interface expects the data to always be opposite
11709 * the native endian format. We accomplish this by reversing
11710 * all the operations that would have been performed on the
11711 * data from a call to tg3_nvram_read_be32().
11712 */
11713 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011714
11715 val = tr32(GRC_EEPROM_ADDR);
11716 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
11717
11718 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
11719 EEPROM_ADDR_READ);
11720 tw32(GRC_EEPROM_ADDR, val |
11721 (0 << EEPROM_ADDR_DEVID_SHIFT) |
11722 (addr & EEPROM_ADDR_ADDR_MASK) |
11723 EEPROM_ADDR_START |
11724 EEPROM_ADDR_WRITE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011725
Michael Chan9d57f012006-12-07 00:23:25 -080011726 for (j = 0; j < 1000; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011727 val = tr32(GRC_EEPROM_ADDR);
11728
11729 if (val & EEPROM_ADDR_COMPLETE)
11730 break;
Michael Chan9d57f012006-12-07 00:23:25 -080011731 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011732 }
11733 if (!(val & EEPROM_ADDR_COMPLETE)) {
11734 rc = -EBUSY;
11735 break;
11736 }
11737 }
11738
11739 return rc;
11740}
11741
11742/* offset and length are dword aligned */
11743static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
11744 u8 *buf)
11745{
11746 int ret = 0;
11747 u32 pagesize = tp->nvram_pagesize;
11748 u32 pagemask = pagesize - 1;
11749 u32 nvram_cmd;
11750 u8 *tmp;
11751
11752 tmp = kmalloc(pagesize, GFP_KERNEL);
11753 if (tmp == NULL)
11754 return -ENOMEM;
11755
11756 while (len) {
11757 int j;
Michael Chane6af3012005-04-21 17:12:05 -070011758 u32 phy_addr, page_off, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011759
11760 phy_addr = offset & ~pagemask;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011761
Linus Torvalds1da177e2005-04-16 15:20:36 -070011762 for (j = 0; j < pagesize; j += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000011763 ret = tg3_nvram_read_be32(tp, phy_addr + j,
11764 (__be32 *) (tmp + j));
11765 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011766 break;
11767 }
11768 if (ret)
11769 break;
11770
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011771 page_off = offset & pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011772 size = pagesize;
11773 if (len < size)
11774 size = len;
11775
11776 len -= size;
11777
11778 memcpy(tmp + page_off, buf, size);
11779
11780 offset = offset + (pagesize - page_off);
11781
Michael Chane6af3012005-04-21 17:12:05 -070011782 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011783
11784 /*
11785 * Before we can erase the flash page, we need
11786 * to issue a special "write enable" command.
11787 */
11788 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
11789
11790 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
11791 break;
11792
11793 /* Erase the target page */
11794 tw32(NVRAM_ADDR, phy_addr);
11795
11796 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
11797 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
11798
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011799 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011800 break;
11801
11802 /* Issue another write enable to start the write. */
11803 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
11804
11805 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
11806 break;
11807
11808 for (j = 0; j < pagesize; j += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080011809 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011810
Al Virob9fc7dc2007-12-17 22:59:57 -080011811 data = *((__be32 *) (tmp + j));
Matt Carlsona9dc5292009-02-25 14:25:30 +000011812
Al Virob9fc7dc2007-12-17 22:59:57 -080011813 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011814
11815 tw32(NVRAM_ADDR, phy_addr + j);
11816
11817 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
11818 NVRAM_CMD_WR;
11819
11820 if (j == 0)
11821 nvram_cmd |= NVRAM_CMD_FIRST;
11822 else if (j == (pagesize - 4))
11823 nvram_cmd |= NVRAM_CMD_LAST;
11824
11825 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
11826 break;
11827 }
11828 if (ret)
11829 break;
11830 }
11831
11832 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
11833 tg3_nvram_exec_cmd(tp, nvram_cmd);
11834
11835 kfree(tmp);
11836
11837 return ret;
11838}
11839
11840/* offset and length are dword aligned */
11841static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
11842 u8 *buf)
11843{
11844 int i, ret = 0;
11845
11846 for (i = 0; i < len; i += 4, offset += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080011847 u32 page_off, phy_addr, nvram_cmd;
11848 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011849
11850 memcpy(&data, buf + i, 4);
Al Virob9fc7dc2007-12-17 22:59:57 -080011851 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011852
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011853 page_off = offset % tp->nvram_pagesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011854
Michael Chan18201802006-03-20 22:29:15 -080011855 phy_addr = tg3_nvram_phys_addr(tp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011856
11857 tw32(NVRAM_ADDR, phy_addr);
11858
11859 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
11860
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011861 if (page_off == 0 || i == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011862 nvram_cmd |= NVRAM_CMD_FIRST;
Michael Chanf6d9a252006-04-29 19:00:24 -070011863 if (page_off == (tp->nvram_pagesize - 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011864 nvram_cmd |= NVRAM_CMD_LAST;
11865
11866 if (i == (len - 4))
11867 nvram_cmd |= NVRAM_CMD_LAST;
11868
Matt Carlson321d32a2008-11-21 17:22:19 -080011869 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
11870 !(tp->tg3_flags3 & TG3_FLG3_5755_PLUS) &&
Michael Chan4c987482005-09-05 17:52:38 -070011871 (tp->nvram_jedecnum == JEDEC_ST) &&
11872 (nvram_cmd & NVRAM_CMD_FIRST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011873
11874 if ((ret = tg3_nvram_exec_cmd(tp,
11875 NVRAM_CMD_WREN | NVRAM_CMD_GO |
11876 NVRAM_CMD_DONE)))
11877
11878 break;
11879 }
11880 if (!(tp->tg3_flags2 & TG3_FLG2_FLASH)) {
11881 /* We always do complete word writes to eeprom. */
11882 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
11883 }
11884
11885 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
11886 break;
11887 }
11888 return ret;
11889}
11890
11891/* offset and length are dword aligned */
11892static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
11893{
11894 int ret;
11895
Linus Torvalds1da177e2005-04-16 15:20:36 -070011896 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT) {
Michael Chan314fba32005-04-21 17:07:04 -070011897 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
11898 ~GRC_LCLCTRL_GPIO_OUTPUT1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011899 udelay(40);
11900 }
11901
11902 if (!(tp->tg3_flags & TG3_FLAG_NVRAM)) {
11903 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
Matt Carlson859a588792010-04-05 10:19:28 +000011904 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011905 u32 grc_mode;
11906
Michael Chanec41c7d2006-01-17 02:40:55 -080011907 ret = tg3_nvram_lock(tp);
11908 if (ret)
11909 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011910
Michael Chane6af3012005-04-21 17:12:05 -070011911 tg3_enable_nvram_access(tp);
11912 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011913 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011914 tw32(NVRAM_WRITE1, 0x406);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011915
11916 grc_mode = tr32(GRC_MODE);
11917 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
11918
11919 if ((tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) ||
11920 !(tp->tg3_flags2 & TG3_FLG2_FLASH)) {
11921
11922 ret = tg3_nvram_write_block_buffered(tp, offset, len,
11923 buf);
Matt Carlson859a588792010-04-05 10:19:28 +000011924 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011925 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
11926 buf);
11927 }
11928
11929 grc_mode = tr32(GRC_MODE);
11930 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
11931
Michael Chane6af3012005-04-21 17:12:05 -070011932 tg3_disable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011933 tg3_nvram_unlock(tp);
11934 }
11935
11936 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT) {
Michael Chan314fba32005-04-21 17:07:04 -070011937 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011938 udelay(40);
11939 }
11940
11941 return ret;
11942}
11943
11944struct subsys_tbl_ent {
11945 u16 subsys_vendor, subsys_devid;
11946 u32 phy_id;
11947};
11948
Matt Carlson24daf2b2010-02-17 15:17:02 +000011949static struct subsys_tbl_ent subsys_id_to_phy_id[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011950 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000011951 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011952 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011953 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011954 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011955 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011956 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011957 { TG3PCI_SUBVENDOR_ID_BROADCOM,
11958 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
11959 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011960 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011961 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011962 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011963 { TG3PCI_SUBVENDOR_ID_BROADCOM,
11964 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
11965 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011966 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011967 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011968 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011969 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011970 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011971 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011972 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070011973
11974 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000011975 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011976 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011977 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011978 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011979 { TG3PCI_SUBVENDOR_ID_3COM,
11980 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
11981 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011982 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011983 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011984 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070011985
11986 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000011987 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000011988 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011989 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000011990 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011991 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000011992 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011993 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000011994 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070011995
11996 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000011997 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000011998 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011999 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012000 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012001 { TG3PCI_SUBVENDOR_ID_COMPAQ,
12002 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
12003 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012004 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012005 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012006 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012007
12008 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012009 { TG3PCI_SUBVENDOR_ID_IBM,
12010 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012011};
12012
Matt Carlson24daf2b2010-02-17 15:17:02 +000012013static struct subsys_tbl_ent * __devinit tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012014{
12015 int i;
12016
12017 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
12018 if ((subsys_id_to_phy_id[i].subsys_vendor ==
12019 tp->pdev->subsystem_vendor) &&
12020 (subsys_id_to_phy_id[i].subsys_devid ==
12021 tp->pdev->subsystem_device))
12022 return &subsys_id_to_phy_id[i];
12023 }
12024 return NULL;
12025}
12026
Michael Chan7d0c41e2005-04-21 17:06:20 -070012027static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012028{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012029 u32 val;
Michael Chancaf636c72006-03-22 01:05:31 -080012030 u16 pmcsr;
12031
12032 /* On some early chips the SRAM cannot be accessed in D3hot state,
12033 * so need make sure we're in D0.
12034 */
12035 pci_read_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, &pmcsr);
12036 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
12037 pci_write_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, pmcsr);
12038 msleep(1);
Michael Chan7d0c41e2005-04-21 17:06:20 -070012039
12040 /* Make sure register accesses (indirect or otherwise)
12041 * will function correctly.
12042 */
12043 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
12044 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012045
David S. Millerf49639e2006-06-09 11:58:36 -070012046 /* The memory arbiter has to be enabled in order for SRAM accesses
12047 * to succeed. Normally on powerup the tg3 chip firmware will make
12048 * sure it is enabled, but other entities such as system netboot
12049 * code might disable it.
12050 */
12051 val = tr32(MEMARB_MODE);
12052 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
12053
Matt Carlson79eb6902010-02-17 15:17:03 +000012054 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012055 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12056
Gary Zambranoa85feb82007-05-05 11:52:19 -070012057 /* Assume an onboard device and WOL capable by default. */
12058 tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT | TG3_FLAG_WOL_CAP;
David S. Miller72b845e2006-03-14 14:11:48 -080012059
Michael Chanb5d37722006-09-27 16:06:21 -070012060 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080012061 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Michael Chanb5d37722006-09-27 16:06:21 -070012062 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080012063 tp->tg3_flags2 |= TG3_FLG2_IS_NIC;
12064 }
Matt Carlson0527ba32007-10-10 18:03:30 -070012065 val = tr32(VCPU_CFGSHDW);
12066 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Matt Carlson8ed5d972007-05-07 00:25:49 -070012067 tp->tg3_flags |= TG3_FLAG_ASPM_WORKAROUND;
Matt Carlson0527ba32007-10-10 18:03:30 -070012068 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Matt Carlson20232762008-12-21 20:18:56 -080012069 (val & VCPU_CFGSHDW_WOL_MAGPKT))
Matt Carlson0527ba32007-10-10 18:03:30 -070012070 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012071 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070012072 }
12073
Linus Torvalds1da177e2005-04-16 15:20:36 -070012074 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
12075 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
12076 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070012077 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012078 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012079
12080 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
12081 tp->nic_sram_data_cfg = nic_cfg;
12082
12083 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
12084 ver >>= NIC_SRAM_DATA_VER_SHIFT;
12085 if ((GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700) &&
12086 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) &&
12087 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703) &&
12088 (ver > 0) && (ver < 0x100))
12089 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
12090
Matt Carlsona9daf362008-05-25 23:49:44 -070012091 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
12092 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
12093
Linus Torvalds1da177e2005-04-16 15:20:36 -070012094 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
12095 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
12096 eeprom_phy_serdes = 1;
12097
12098 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
12099 if (nic_phy_id != 0) {
12100 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
12101 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
12102
12103 eeprom_phy_id = (id1 >> 16) << 10;
12104 eeprom_phy_id |= (id2 & 0xfc00) << 16;
12105 eeprom_phy_id |= (id2 & 0x03ff) << 0;
12106 } else
12107 eeprom_phy_id = 0;
12108
Michael Chan7d0c41e2005-04-21 17:06:20 -070012109 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070012110 if (eeprom_phy_serdes) {
Matt Carlsona50d0792010-06-05 17:24:37 +000012111 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Michael Chan747e8f82005-07-25 12:33:22 -070012112 tp->tg3_flags2 |= TG3_FLG2_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000012113 else
12114 tp->tg3_flags2 |= TG3_FLG2_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070012115 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070012116
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 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
12119 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070012120 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070012121 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
12122
12123 switch (led_cfg) {
12124 default:
12125 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
12126 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12127 break;
12128
12129 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
12130 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12131 break;
12132
12133 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
12134 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070012135
12136 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
12137 * read on some older 5700/5701 bootcode.
12138 */
12139 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
12140 ASIC_REV_5700 ||
12141 GET_ASIC_REV(tp->pci_chip_rev_id) ==
12142 ASIC_REV_5701)
12143 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12144
Linus Torvalds1da177e2005-04-16 15:20:36 -070012145 break;
12146
12147 case SHASTA_EXT_LED_SHARED:
12148 tp->led_ctrl = LED_CTRL_MODE_SHARED;
12149 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
12150 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
12151 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12152 LED_CTRL_MODE_PHY_2);
12153 break;
12154
12155 case SHASTA_EXT_LED_MAC:
12156 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
12157 break;
12158
12159 case SHASTA_EXT_LED_COMBO:
12160 tp->led_ctrl = LED_CTRL_MODE_COMBO;
12161 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
12162 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12163 LED_CTRL_MODE_PHY_2);
12164 break;
12165
Stephen Hemminger855e1112008-04-16 16:37:28 -070012166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012167
12168 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
12169 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
12170 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
12171 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12172
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012173 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
12174 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080012175
Michael Chan9d26e212006-12-07 00:21:14 -080012176 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012177 tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080012178 if ((tp->pdev->subsystem_vendor ==
12179 PCI_VENDOR_ID_ARIMA) &&
12180 (tp->pdev->subsystem_device == 0x205a ||
12181 tp->pdev->subsystem_device == 0x2063))
12182 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
12183 } else {
David S. Millerf49639e2006-06-09 11:58:36 -070012184 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080012185 tp->tg3_flags2 |= TG3_FLG2_IS_NIC;
12186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012187
12188 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
12189 tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
John W. Linvillecbf46852005-04-21 17:01:29 -070012190 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012191 tp->tg3_flags2 |= TG3_FLG2_ASF_NEW_HANDSHAKE;
12192 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012193
12194 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
12195 (tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
Matt Carlson0d3031d2007-10-10 18:02:43 -070012196 tp->tg3_flags3 |= TG3_FLG3_ENABLE_APE;
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012197
Gary Zambranoa85feb82007-05-05 11:52:19 -070012198 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES &&
12199 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
12200 tp->tg3_flags &= ~TG3_FLAG_WOL_CAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012201
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070012202 if ((tp->tg3_flags & TG3_FLAG_WOL_CAP) &&
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012203 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE))
Matt Carlson0527ba32007-10-10 18:03:30 -070012204 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
12205
Linus Torvalds1da177e2005-04-16 15:20:36 -070012206 if (cfg2 & (1 << 17))
12207 tp->tg3_flags2 |= TG3_FLG2_CAPACITIVE_COUPLING;
12208
12209 /* serdes signal pre-emphasis in register 0x590 set by */
12210 /* bootcode if bit 18 is set */
12211 if (cfg2 & (1 << 18))
12212 tp->tg3_flags2 |= TG3_FLG2_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070012213
Matt Carlson321d32a2008-11-21 17:22:19 -080012214 if (((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
12215 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080012216 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
12217 tp->tg3_flags3 |= TG3_FLG3_PHY_ENABLE_APD;
12218
Matt Carlson8ed5d972007-05-07 00:25:49 -070012219 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
12220 u32 cfg3;
12221
12222 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
12223 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
12224 tp->tg3_flags |= TG3_FLAG_ASPM_WORKAROUND;
12225 }
Matt Carlsona9daf362008-05-25 23:49:44 -070012226
Matt Carlson14417062010-02-17 15:16:59 +000012227 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
12228 tp->tg3_flags3 |= TG3_FLG3_RGMII_INBAND_DISABLE;
Matt Carlsona9daf362008-05-25 23:49:44 -070012229 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
12230 tp->tg3_flags3 |= TG3_FLG3_RGMII_EXT_IBND_RX_EN;
12231 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
12232 tp->tg3_flags3 |= TG3_FLG3_RGMII_EXT_IBND_TX_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012233 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012234done:
12235 device_init_wakeup(&tp->pdev->dev, tp->tg3_flags & TG3_FLAG_WOL_CAP);
12236 device_set_wakeup_enable(&tp->pdev->dev,
12237 tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
Michael Chan7d0c41e2005-04-21 17:06:20 -070012238}
12239
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012240static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
12241{
12242 int i;
12243 u32 val;
12244
12245 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
12246 tw32(OTP_CTRL, cmd);
12247
12248 /* Wait for up to 1 ms for command to execute. */
12249 for (i = 0; i < 100; i++) {
12250 val = tr32(OTP_STATUS);
12251 if (val & OTP_STATUS_CMD_DONE)
12252 break;
12253 udelay(10);
12254 }
12255
12256 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
12257}
12258
12259/* Read the gphy configuration from the OTP region of the chip. The gphy
12260 * configuration is a 32-bit value that straddles the alignment boundary.
12261 * We do two 32-bit reads and then shift and merge the results.
12262 */
12263static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
12264{
12265 u32 bhalf_otp, thalf_otp;
12266
12267 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
12268
12269 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
12270 return 0;
12271
12272 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
12273
12274 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12275 return 0;
12276
12277 thalf_otp = tr32(OTP_READ_DATA);
12278
12279 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
12280
12281 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12282 return 0;
12283
12284 bhalf_otp = tr32(OTP_READ_DATA);
12285
12286 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
12287}
12288
Michael Chan7d0c41e2005-04-21 17:06:20 -070012289static int __devinit tg3_phy_probe(struct tg3 *tp)
12290{
12291 u32 hw_phy_id_1, hw_phy_id_2;
12292 u32 hw_phy_id, hw_phy_id_masked;
12293 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012294
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012295 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)
12296 return tg3_phy_init(tp);
12297
Linus Torvalds1da177e2005-04-16 15:20:36 -070012298 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010012299 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012300 */
12301 err = 0;
Matt Carlson0d3031d2007-10-10 18:02:43 -070012302 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
12303 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000012304 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012305 } else {
12306 /* Now read the physical PHY_ID from the chip and verify
12307 * that it is sane. If it doesn't look good, we fall back
12308 * to either the hard-coded table based PHY_ID and failing
12309 * that the value found in the eeprom area.
12310 */
12311 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
12312 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
12313
12314 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
12315 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
12316 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
12317
Matt Carlson79eb6902010-02-17 15:17:03 +000012318 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012319 }
12320
Matt Carlson79eb6902010-02-17 15:17:03 +000012321 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012322 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000012323 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012324 tp->tg3_flags2 |= TG3_FLG2_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070012325 else
12326 tp->tg3_flags2 &= ~TG3_FLG2_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012327 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000012328 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070012329 /* Do nothing, phy ID already set up in
12330 * tg3_get_eeprom_hw_cfg().
12331 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070012332 } else {
12333 struct subsys_tbl_ent *p;
12334
12335 /* No eeprom signature? Try the hardcoded
12336 * subsys device table.
12337 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012338 p = tg3_lookup_by_subsys(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012339 if (!p)
12340 return -ENODEV;
12341
12342 tp->phy_id = p->phy_id;
12343 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000012344 tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012345 tp->tg3_flags2 |= TG3_FLG2_PHY_SERDES;
12346 }
12347 }
12348
Michael Chan747e8f82005-07-25 12:33:22 -070012349 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES) &&
Matt Carlson0d3031d2007-10-10 18:02:43 -070012350 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070012351 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chan3600d912006-12-07 00:21:48 -080012352 u32 bmsr, adv_reg, tg3_ctrl, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012353
12354 tg3_readphy(tp, MII_BMSR, &bmsr);
12355 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
12356 (bmsr & BMSR_LSTATUS))
12357 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012358
Linus Torvalds1da177e2005-04-16 15:20:36 -070012359 err = tg3_phy_reset(tp);
12360 if (err)
12361 return err;
12362
12363 adv_reg = (ADVERTISE_10HALF | ADVERTISE_10FULL |
12364 ADVERTISE_100HALF | ADVERTISE_100FULL |
12365 ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
12366 tg3_ctrl = 0;
12367 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY)) {
12368 tg3_ctrl = (MII_TG3_CTRL_ADV_1000_HALF |
12369 MII_TG3_CTRL_ADV_1000_FULL);
12370 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
12371 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
12372 tg3_ctrl |= (MII_TG3_CTRL_AS_MASTER |
12373 MII_TG3_CTRL_ENABLE_AS_MASTER);
12374 }
12375
Michael Chan3600d912006-12-07 00:21:48 -080012376 mask = (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
12377 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
12378 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full);
12379 if (!tg3_copper_is_advertising_all(tp, mask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012380 tg3_writephy(tp, MII_ADVERTISE, adv_reg);
12381
12382 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY))
12383 tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
12384
12385 tg3_writephy(tp, MII_BMCR,
12386 BMCR_ANENABLE | BMCR_ANRESTART);
12387 }
12388 tg3_phy_set_wirespeed(tp);
12389
12390 tg3_writephy(tp, MII_ADVERTISE, adv_reg);
12391 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY))
12392 tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
12393 }
12394
12395skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000012396 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012397 err = tg3_init_5401phy_dsp(tp);
12398 if (err)
12399 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012400
Linus Torvalds1da177e2005-04-16 15:20:36 -070012401 err = tg3_init_5401phy_dsp(tp);
12402 }
12403
Michael Chan747e8f82005-07-25 12:33:22 -070012404 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012405 tp->link_config.advertising =
12406 (ADVERTISED_1000baseT_Half |
12407 ADVERTISED_1000baseT_Full |
12408 ADVERTISED_Autoneg |
12409 ADVERTISED_FIBRE);
12410 if (tp->tg3_flags & TG3_FLAG_10_100_ONLY)
12411 tp->link_config.advertising &=
12412 ~(ADVERTISED_1000baseT_Half |
12413 ADVERTISED_1000baseT_Full);
12414
12415 return err;
12416}
12417
Matt Carlson184b8902010-04-05 10:19:25 +000012418static void __devinit tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012419{
Matt Carlson184b8902010-04-05 10:19:25 +000012420 u8 vpd_data[TG3_NVM_VPD_LEN];
Matt Carlson4181b2c2010-02-26 14:04:45 +000012421 unsigned int block_end, rosize, len;
Matt Carlson184b8902010-04-05 10:19:25 +000012422 int j, i = 0;
Michael Chan1b277772006-03-20 22:27:48 -080012423 u32 magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012424
Matt Carlsondf259d82009-04-20 06:57:14 +000012425 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
12426 tg3_nvram_read(tp, 0x0, &magic))
David S. Millerf49639e2006-06-09 11:58:36 -070012427 goto out_not_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012428
Michael Chan18201802006-03-20 22:29:15 -080012429 if (magic == TG3_EEPROM_MAGIC) {
Matt Carlson141518c2009-12-03 08:36:22 +000012430 for (i = 0; i < TG3_NVM_VPD_LEN; i += 4) {
Michael Chan1b277772006-03-20 22:27:48 -080012431 u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012432
Matt Carlson6d348f22009-02-25 14:25:52 +000012433 /* The data is in little-endian format in NVRAM.
12434 * Use the big-endian read routines to preserve
12435 * the byte order as it exists in NVRAM.
12436 */
Matt Carlson141518c2009-12-03 08:36:22 +000012437 if (tg3_nvram_read_be32(tp, TG3_NVM_VPD_OFF + i, &tmp))
Michael Chan1b277772006-03-20 22:27:48 -080012438 goto out_not_found;
12439
Matt Carlson6d348f22009-02-25 14:25:52 +000012440 memcpy(&vpd_data[i], &tmp, sizeof(tmp));
Michael Chan1b277772006-03-20 22:27:48 -080012441 }
12442 } else {
Matt Carlson94c982b2009-12-03 08:36:23 +000012443 ssize_t cnt;
Matt Carlson4181b2c2010-02-26 14:04:45 +000012444 unsigned int pos = 0;
Michael Chan1b277772006-03-20 22:27:48 -080012445
Matt Carlson94c982b2009-12-03 08:36:23 +000012446 for (; pos < TG3_NVM_VPD_LEN && i < 3; i++, pos += cnt) {
12447 cnt = pci_read_vpd(tp->pdev, pos,
12448 TG3_NVM_VPD_LEN - pos,
12449 &vpd_data[pos]);
12450 if (cnt == -ETIMEDOUT || -EINTR)
12451 cnt = 0;
12452 else if (cnt < 0)
David S. Millerf49639e2006-06-09 11:58:36 -070012453 goto out_not_found;
Michael Chan1b277772006-03-20 22:27:48 -080012454 }
Matt Carlson94c982b2009-12-03 08:36:23 +000012455 if (pos != TG3_NVM_VPD_LEN)
12456 goto out_not_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012457 }
12458
Matt Carlson4181b2c2010-02-26 14:04:45 +000012459 i = pci_vpd_find_tag(vpd_data, 0, TG3_NVM_VPD_LEN,
12460 PCI_VPD_LRDT_RO_DATA);
12461 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012462 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000012463
12464 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
12465 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
12466 i += PCI_VPD_LRDT_TAG_SIZE;
12467
12468 if (block_end > TG3_NVM_VPD_LEN)
12469 goto out_not_found;
12470
Matt Carlson184b8902010-04-05 10:19:25 +000012471 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
12472 PCI_VPD_RO_KEYWORD_MFR_ID);
12473 if (j > 0) {
12474 len = pci_vpd_info_field_size(&vpd_data[j]);
12475
12476 j += PCI_VPD_INFO_FLD_HDR_SIZE;
12477 if (j + len > block_end || len != 4 ||
12478 memcmp(&vpd_data[j], "1028", 4))
12479 goto partno;
12480
12481 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
12482 PCI_VPD_RO_KEYWORD_VENDOR0);
12483 if (j < 0)
12484 goto partno;
12485
12486 len = pci_vpd_info_field_size(&vpd_data[j]);
12487
12488 j += PCI_VPD_INFO_FLD_HDR_SIZE;
12489 if (j + len > block_end)
12490 goto partno;
12491
12492 memcpy(tp->fw_ver, &vpd_data[j], len);
12493 strncat(tp->fw_ver, " bc ", TG3_NVM_VPD_LEN - len - 1);
12494 }
12495
12496partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000012497 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
12498 PCI_VPD_RO_KEYWORD_PARTNO);
12499 if (i < 0)
12500 goto out_not_found;
12501
12502 len = pci_vpd_info_field_size(&vpd_data[i]);
12503
12504 i += PCI_VPD_INFO_FLD_HDR_SIZE;
12505 if (len > TG3_BPN_SIZE ||
12506 (len + i) > TG3_NVM_VPD_LEN)
12507 goto out_not_found;
12508
12509 memcpy(tp->board_part_number, &vpd_data[i], len);
12510
12511 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012512
12513out_not_found:
Michael Chanb5d37722006-09-27 16:06:21 -070012514 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12515 strcpy(tp->board_part_number, "BCM95906");
Matt Carlsondf259d82009-04-20 06:57:14 +000012516 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12517 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
12518 strcpy(tp->board_part_number, "BCM57780");
12519 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12520 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
12521 strcpy(tp->board_part_number, "BCM57760");
12522 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12523 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
12524 strcpy(tp->board_part_number, "BCM57790");
Matt Carlson5e7ccf22009-08-25 10:08:42 +000012525 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12526 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
12527 strcpy(tp->board_part_number, "BCM57788");
Matt Carlsonb474eca2010-02-17 15:16:58 +000012528 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12529 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
12530 strcpy(tp->board_part_number, "BCM57761");
12531 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12532 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
Matt Carlsonb703df62009-12-03 08:36:21 +000012533 strcpy(tp->board_part_number, "BCM57765");
Matt Carlsonb474eca2010-02-17 15:16:58 +000012534 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12535 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
12536 strcpy(tp->board_part_number, "BCM57781");
12537 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12538 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
12539 strcpy(tp->board_part_number, "BCM57785");
12540 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12541 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
12542 strcpy(tp->board_part_number, "BCM57791");
12543 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12544 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
12545 strcpy(tp->board_part_number, "BCM57795");
Michael Chanb5d37722006-09-27 16:06:21 -070012546 else
12547 strcpy(tp->board_part_number, "none");
Linus Torvalds1da177e2005-04-16 15:20:36 -070012548}
12549
Matt Carlson9c8a6202007-10-21 16:16:08 -070012550static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
12551{
12552 u32 val;
12553
Matt Carlsone4f34112009-02-25 14:25:00 +000012554 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070012555 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000012556 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070012557 val != 0)
12558 return 0;
12559
12560 return 1;
12561}
12562
Matt Carlsonacd9c112009-02-25 14:26:33 +000012563static void __devinit tg3_read_bc_ver(struct tg3 *tp)
12564{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012565 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000012566 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012567 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000012568
12569 if (tg3_nvram_read(tp, 0xc, &offset) ||
12570 tg3_nvram_read(tp, 0x4, &start))
12571 return;
12572
12573 offset = tg3_nvram_logical_addr(tp, offset);
12574
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012575 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000012576 return;
12577
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012578 if ((val & 0xfc000000) == 0x0c000000) {
12579 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000012580 return;
12581
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012582 if (val == 0)
12583 newver = true;
12584 }
12585
Matt Carlson75f99362010-04-05 10:19:24 +000012586 dst_off = strlen(tp->fw_ver);
12587
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012588 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000012589 if (TG3_VER_SIZE - dst_off < 16 ||
12590 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012591 return;
12592
12593 offset = offset + ver_offset - start;
12594 for (i = 0; i < 16; i += 4) {
12595 __be32 v;
12596 if (tg3_nvram_read_be32(tp, offset + i, &v))
12597 return;
12598
Matt Carlson75f99362010-04-05 10:19:24 +000012599 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012600 }
12601 } else {
12602 u32 major, minor;
12603
12604 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
12605 return;
12606
12607 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
12608 TG3_NVM_BCVER_MAJSFT;
12609 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000012610 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
12611 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000012612 }
12613}
12614
Matt Carlsona6f6cb12009-02-25 14:27:43 +000012615static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
12616{
12617 u32 val, major, minor;
12618
12619 /* Use native endian representation */
12620 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
12621 return;
12622
12623 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
12624 TG3_NVM_HWSB_CFG1_MAJSFT;
12625 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
12626 TG3_NVM_HWSB_CFG1_MINSFT;
12627
12628 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
12629}
12630
Matt Carlsondfe00d72008-11-21 17:19:41 -080012631static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
12632{
12633 u32 offset, major, minor, build;
12634
Matt Carlson75f99362010-04-05 10:19:24 +000012635 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080012636
12637 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
12638 return;
12639
12640 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
12641 case TG3_EEPROM_SB_REVISION_0:
12642 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
12643 break;
12644 case TG3_EEPROM_SB_REVISION_2:
12645 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
12646 break;
12647 case TG3_EEPROM_SB_REVISION_3:
12648 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
12649 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000012650 case TG3_EEPROM_SB_REVISION_4:
12651 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
12652 break;
12653 case TG3_EEPROM_SB_REVISION_5:
12654 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
12655 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080012656 default:
12657 return;
12658 }
12659
Matt Carlsone4f34112009-02-25 14:25:00 +000012660 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080012661 return;
12662
12663 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
12664 TG3_EEPROM_SB_EDH_BLD_SHFT;
12665 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
12666 TG3_EEPROM_SB_EDH_MAJ_SHFT;
12667 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
12668
12669 if (minor > 99 || build > 26)
12670 return;
12671
Matt Carlson75f99362010-04-05 10:19:24 +000012672 offset = strlen(tp->fw_ver);
12673 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
12674 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080012675
12676 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000012677 offset = strlen(tp->fw_ver);
12678 if (offset < TG3_VER_SIZE - 1)
12679 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080012680 }
12681}
12682
Matt Carlsonacd9c112009-02-25 14:26:33 +000012683static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080012684{
12685 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000012686 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070012687
12688 for (offset = TG3_NVM_DIR_START;
12689 offset < TG3_NVM_DIR_END;
12690 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000012691 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012692 return;
12693
12694 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
12695 break;
12696 }
12697
12698 if (offset == TG3_NVM_DIR_END)
12699 return;
12700
12701 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
12702 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000012703 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012704 return;
12705
Matt Carlsone4f34112009-02-25 14:25:00 +000012706 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070012707 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000012708 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012709 return;
12710
12711 offset += val - start;
12712
Matt Carlsonacd9c112009-02-25 14:26:33 +000012713 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012714
Matt Carlsonacd9c112009-02-25 14:26:33 +000012715 tp->fw_ver[vlen++] = ',';
12716 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070012717
12718 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012719 __be32 v;
12720 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012721 return;
12722
Al Virob9fc7dc2007-12-17 22:59:57 -080012723 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012724
Matt Carlsonacd9c112009-02-25 14:26:33 +000012725 if (vlen > TG3_VER_SIZE - sizeof(v)) {
12726 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012727 break;
12728 }
12729
Matt Carlsonacd9c112009-02-25 14:26:33 +000012730 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
12731 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012732 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000012733}
12734
Matt Carlson7fd76442009-02-25 14:27:20 +000012735static void __devinit tg3_read_dash_ver(struct tg3 *tp)
12736{
12737 int vlen;
12738 u32 apedata;
12739
12740 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) ||
12741 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
12742 return;
12743
12744 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
12745 if (apedata != APE_SEG_SIG_MAGIC)
12746 return;
12747
12748 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
12749 if (!(apedata & APE_FW_STATUS_READY))
12750 return;
12751
12752 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
12753
12754 vlen = strlen(tp->fw_ver);
12755
12756 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " DASH v%d.%d.%d.%d",
12757 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
12758 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
12759 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
12760 (apedata & APE_FW_VERSION_BLDMSK));
12761}
12762
Matt Carlsonacd9c112009-02-25 14:26:33 +000012763static void __devinit tg3_read_fw_ver(struct tg3 *tp)
12764{
12765 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000012766 bool vpd_vers = false;
12767
12768 if (tp->fw_ver[0] != 0)
12769 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000012770
Matt Carlsondf259d82009-04-20 06:57:14 +000012771 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) {
Matt Carlson75f99362010-04-05 10:19:24 +000012772 strcat(tp->fw_ver, "sb");
Matt Carlsondf259d82009-04-20 06:57:14 +000012773 return;
12774 }
12775
Matt Carlsonacd9c112009-02-25 14:26:33 +000012776 if (tg3_nvram_read(tp, 0, &val))
12777 return;
12778
12779 if (val == TG3_EEPROM_MAGIC)
12780 tg3_read_bc_ver(tp);
12781 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
12782 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000012783 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
12784 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000012785 else
12786 return;
12787
12788 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
Matt Carlson75f99362010-04-05 10:19:24 +000012789 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) || vpd_vers)
12790 goto done;
Matt Carlsonacd9c112009-02-25 14:26:33 +000012791
12792 tg3_read_mgmtfw_ver(tp);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012793
Matt Carlson75f99362010-04-05 10:19:24 +000012794done:
Matt Carlson9c8a6202007-10-21 16:16:08 -070012795 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080012796}
12797
Michael Chan7544b092007-05-05 13:08:32 -070012798static struct pci_dev * __devinit tg3_find_peer(struct tg3 *);
12799
Eric Dumazet7fe876a2010-07-08 06:14:55 +000012800static void inline vlan_features_add(struct net_device *dev, unsigned long flags)
12801{
12802#if TG3_VLAN_TAG_USED
12803 dev->vlan_features |= flags;
12804#endif
12805}
12806
Linus Torvalds1da177e2005-04-16 15:20:36 -070012807static int __devinit tg3_get_invariants(struct tg3 *tp)
12808{
12809 static struct pci_device_id write_reorder_chipsets[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012810 { PCI_DEVICE(PCI_VENDOR_ID_AMD,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012811 PCI_DEVICE_ID_AMD_FE_GATE_700C) },
John W. Linvillec165b002006-07-08 13:28:53 -070012812 { PCI_DEVICE(PCI_VENDOR_ID_AMD,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012813 PCI_DEVICE_ID_AMD_8131_BRIDGE) },
Michael Chan399de502005-10-03 14:02:39 -070012814 { PCI_DEVICE(PCI_VENDOR_ID_VIA,
12815 PCI_DEVICE_ID_VIA_8385_0) },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012816 { },
12817 };
12818 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012819 u32 pci_state_reg, grc_misc_cfg;
12820 u32 val;
12821 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080012822 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012823
Linus Torvalds1da177e2005-04-16 15:20:36 -070012824 /* Force memory write invalidate off. If we leave it on,
12825 * then on 5700_BX chips we have to enable a workaround.
12826 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
12827 * to match the cacheline size. The Broadcom driver have this
12828 * workaround but turns MWI off all the times so never uses
12829 * it. This seems to suggest that the workaround is insufficient.
12830 */
12831 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
12832 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
12833 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
12834
12835 /* It is absolutely critical that TG3PCI_MISC_HOST_CTRL
12836 * has the register indirect write enable bit set before
12837 * we try to access any of the MMIO registers. It is also
12838 * critical that the PCI-X hw workaround situation is decided
12839 * before that as well.
12840 */
12841 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
12842 &misc_ctrl_reg);
12843
12844 tp->pci_chip_rev_id = (misc_ctrl_reg >>
12845 MISC_HOST_CTRL_CHIPREV_SHIFT);
Matt Carlson795d01c2007-10-07 23:28:17 -070012846 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
12847 u32 prod_id_asic_rev;
12848
Matt Carlson5001e2f2009-11-13 13:03:51 +000012849 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
12850 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
Matt Carlsona50d0792010-06-05 17:24:37 +000012851 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5724 ||
12852 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012853 pci_read_config_dword(tp->pdev,
12854 TG3PCI_GEN2_PRODID_ASICREV,
12855 &prod_id_asic_rev);
Matt Carlsonb703df62009-12-03 08:36:21 +000012856 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
12857 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
12858 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
12859 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
12860 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
12861 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
12862 pci_read_config_dword(tp->pdev,
12863 TG3PCI_GEN15_PRODID_ASICREV,
12864 &prod_id_asic_rev);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012865 else
12866 pci_read_config_dword(tp->pdev, TG3PCI_PRODID_ASICREV,
12867 &prod_id_asic_rev);
12868
Matt Carlson321d32a2008-11-21 17:22:19 -080012869 tp->pci_chip_rev_id = prod_id_asic_rev;
Matt Carlson795d01c2007-10-07 23:28:17 -070012870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012871
Michael Chanff645be2005-04-21 17:09:53 -070012872 /* Wrong chip ID in 5752 A0. This code can be removed later
12873 * as A0 is not in production.
12874 */
12875 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
12876 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
12877
Michael Chan68929142005-08-09 20:17:14 -070012878 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
12879 * we need to disable memory and use config. cycles
12880 * only to access all registers. The 5702/03 chips
12881 * can mistakenly decode the special cycles from the
12882 * ICH chipsets as memory write cycles, causing corruption
12883 * of register and memory space. Only certain ICH bridges
12884 * will drive special cycles with non-zero data during the
12885 * address phase which can fall within the 5703's address
12886 * range. This is not an ICH bug as the PCI spec allows
12887 * non-zero address during special cycles. However, only
12888 * these ICH bridges are known to drive non-zero addresses
12889 * during special cycles.
12890 *
12891 * Since special cycles do not cross PCI bridges, we only
12892 * enable this workaround if the 5703 is on the secondary
12893 * bus of these ICH bridges.
12894 */
12895 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
12896 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
12897 static struct tg3_dev_id {
12898 u32 vendor;
12899 u32 device;
12900 u32 rev;
12901 } ich_chipsets[] = {
12902 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
12903 PCI_ANY_ID },
12904 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
12905 PCI_ANY_ID },
12906 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
12907 0xa },
12908 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
12909 PCI_ANY_ID },
12910 { },
12911 };
12912 struct tg3_dev_id *pci_id = &ich_chipsets[0];
12913 struct pci_dev *bridge = NULL;
12914
12915 while (pci_id->vendor != 0) {
12916 bridge = pci_get_device(pci_id->vendor, pci_id->device,
12917 bridge);
12918 if (!bridge) {
12919 pci_id++;
12920 continue;
12921 }
12922 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070012923 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070012924 continue;
12925 }
12926 if (bridge->subordinate &&
12927 (bridge->subordinate->number ==
12928 tp->pdev->bus->number)) {
12929
12930 tp->tg3_flags2 |= TG3_FLG2_ICH_WORKAROUND;
12931 pci_dev_put(bridge);
12932 break;
12933 }
12934 }
12935 }
12936
Matt Carlson41588ba2008-04-19 18:12:33 -070012937 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
12938 static struct tg3_dev_id {
12939 u32 vendor;
12940 u32 device;
12941 } bridge_chipsets[] = {
12942 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
12943 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
12944 { },
12945 };
12946 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
12947 struct pci_dev *bridge = NULL;
12948
12949 while (pci_id->vendor != 0) {
12950 bridge = pci_get_device(pci_id->vendor,
12951 pci_id->device,
12952 bridge);
12953 if (!bridge) {
12954 pci_id++;
12955 continue;
12956 }
12957 if (bridge->subordinate &&
12958 (bridge->subordinate->number <=
12959 tp->pdev->bus->number) &&
12960 (bridge->subordinate->subordinate >=
12961 tp->pdev->bus->number)) {
12962 tp->tg3_flags3 |= TG3_FLG3_5701_DMA_BUG;
12963 pci_dev_put(bridge);
12964 break;
12965 }
12966 }
12967 }
12968
Michael Chan4a29cc22006-03-19 13:21:12 -080012969 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
12970 * DMA addresses > 40-bit. This bridge may have other additional
12971 * 57xx devices behind it in some 4-port NIC designs for example.
12972 * Any tg3 device found behind the bridge will also need the 40-bit
12973 * DMA workaround.
12974 */
Michael Chana4e2b342005-10-26 15:46:52 -070012975 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
12976 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
12977 tp->tg3_flags2 |= TG3_FLG2_5780_CLASS;
Michael Chan4a29cc22006-03-19 13:21:12 -080012978 tp->tg3_flags |= TG3_FLAG_40BIT_DMA_BUG;
Michael Chan4cf78e42005-07-25 12:29:19 -070012979 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a588792010-04-05 10:19:28 +000012980 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080012981 struct pci_dev *bridge = NULL;
12982
12983 do {
12984 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
12985 PCI_DEVICE_ID_SERVERWORKS_EPB,
12986 bridge);
12987 if (bridge && bridge->subordinate &&
12988 (bridge->subordinate->number <=
12989 tp->pdev->bus->number) &&
12990 (bridge->subordinate->subordinate >=
12991 tp->pdev->bus->number)) {
12992 tp->tg3_flags |= TG3_FLAG_40BIT_DMA_BUG;
12993 pci_dev_put(bridge);
12994 break;
12995 }
12996 } while (bridge);
12997 }
Michael Chan4cf78e42005-07-25 12:29:19 -070012998
Linus Torvalds1da177e2005-04-16 15:20:36 -070012999 /* Initialize misc host control in PCI block. */
13000 tp->misc_host_ctrl |= (misc_ctrl_reg &
13001 MISC_HOST_CTRL_CHIPREV);
13002 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13003 tp->misc_host_ctrl);
13004
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013005 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
13006 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
13007 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Michael Chan7544b092007-05-05 13:08:32 -070013008 tp->pdev_peer = tg3_find_peer(tp);
13009
Matt Carlson321d32a2008-11-21 17:22:19 -080013010 /* Intentionally exclude ASIC_REV_5906 */
13011 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Michael Chand9ab5ad2006-03-20 22:27:35 -080013012 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013013 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013014 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013015 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013016 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlsonb703df62009-12-03 08:36:21 +000013017 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsona50d0792010-06-05 17:24:37 +000013018 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlsonb703df62009-12-03 08:36:21 +000013019 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson321d32a2008-11-21 17:22:19 -080013020 tp->tg3_flags3 |= TG3_FLG3_5755_PLUS;
13021
13022 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13023 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Michael Chanb5d37722006-09-27 16:06:21 -070013024 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013025 (tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
Michael Chana4e2b342005-10-26 15:46:52 -070013026 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
John W. Linville6708e5c2005-04-21 17:00:52 -070013027 tp->tg3_flags2 |= TG3_FLG2_5750_PLUS;
13028
John W. Linville1b440c562005-04-21 17:03:18 -070013029 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) ||
13030 (tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
13031 tp->tg3_flags2 |= TG3_FLG2_5705_PLUS;
13032
Matt Carlson027455a2008-12-21 20:19:30 -080013033 /* 5700 B0 chips do not support checksumming correctly due
13034 * to hardware bugs.
13035 */
13036 if (tp->pci_chip_rev_id == CHIPREV_ID_5700_B0)
13037 tp->tg3_flags |= TG3_FLAG_BROKEN_CHECKSUMS;
13038 else {
Eric Dumazet7fe876a2010-07-08 06:14:55 +000013039 unsigned long features = NETIF_F_IP_CSUM | NETIF_F_SG | NETIF_F_GRO;
13040
Matt Carlson027455a2008-12-21 20:19:30 -080013041 tp->tg3_flags |= TG3_FLAG_RX_CHECKSUMS;
Matt Carlson027455a2008-12-21 20:19:30 -080013042 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Eric Dumazet7fe876a2010-07-08 06:14:55 +000013043 features |= NETIF_F_IPV6_CSUM;
13044 tp->dev->features |= features;
13045 vlan_features_add(tp->dev, features);
Matt Carlson027455a2008-12-21 20:19:30 -080013046 }
13047
Matt Carlson507399f2009-11-13 13:03:37 +000013048 /* Determine TSO capabilities */
Matt Carlsonb703df62009-12-03 08:36:21 +000013049 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsona50d0792010-06-05 17:24:37 +000013050 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlsonb703df62009-12-03 08:36:21 +000013051 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlsone849cdc2009-11-13 13:03:38 +000013052 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_3;
13053 else if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
13054 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlson507399f2009-11-13 13:03:37 +000013055 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_2;
13056 else if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
13057 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_1 | TG3_FLG2_TSO_BUG;
13058 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
13059 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
13060 tp->tg3_flags2 &= ~TG3_FLG2_TSO_BUG;
13061 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13062 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13063 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
13064 tp->tg3_flags2 |= TG3_FLG2_TSO_BUG;
13065 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
13066 tp->fw_needed = FIRMWARE_TG3TSO5;
13067 else
13068 tp->fw_needed = FIRMWARE_TG3TSO;
13069 }
13070
13071 tp->irq_max = 1;
13072
Michael Chan5a6f3072006-03-20 22:28:05 -080013073 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
Michael Chan7544b092007-05-05 13:08:32 -070013074 tp->tg3_flags |= TG3_FLAG_SUPPORT_MSI;
13075 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
13076 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
13077 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
13078 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
13079 tp->pdev_peer == tp->pdev))
13080 tp->tg3_flags &= ~TG3_FLAG_SUPPORT_MSI;
13081
Matt Carlson321d32a2008-11-21 17:22:19 -080013082 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070013083 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanfcfa0a32006-03-20 22:28:41 -080013084 tp->tg3_flags2 |= TG3_FLG2_1SHOT_MSI;
Michael Chan52c0fd82006-06-29 20:15:54 -070013085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013086
Matt Carlsonb703df62009-12-03 08:36:21 +000013087 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsona50d0792010-06-05 17:24:37 +000013088 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlsonb703df62009-12-03 08:36:21 +000013089 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
Matt Carlson507399f2009-11-13 13:03:37 +000013090 tp->tg3_flags |= TG3_FLAG_SUPPORT_MSIX;
13091 tp->irq_max = TG3_IRQ_MAX_VECS;
13092 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013093 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000013094
Matt Carlson615774f2009-11-13 13:03:39 +000013095 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsona50d0792010-06-05 17:24:37 +000013096 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlson615774f2009-11-13 13:03:39 +000013097 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
13098 tp->tg3_flags3 |= TG3_FLG3_SHORT_DMA_BUG;
13099 else if (!(tp->tg3_flags3 & TG3_FLG3_5755_PLUS)) {
13100 tp->tg3_flags3 |= TG3_FLG3_4G_DMA_BNDRY_BUG;
13101 tp->tg3_flags3 |= TG3_FLG3_40BIT_DMA_LIMIT_BUG;
Matt Carlson0e1406d2009-11-02 12:33:33 +000013102 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013103
Matt Carlsonb703df62009-12-03 08:36:21 +000013104 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsona50d0792010-06-05 17:24:37 +000013105 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlsonb703df62009-12-03 08:36:21 +000013106 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
13107 tp->tg3_flags3 |= TG3_FLG3_USE_JUMBO_BDFLAG;
13108
Matt Carlsonf51f3562008-05-25 23:45:08 -070013109 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
Matt Carlsonc6cdf432010-04-05 10:19:26 +000013110 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
13111 (tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG))
Matt Carlson8f666b02009-08-28 13:58:24 +000013112 tp->tg3_flags |= TG3_FLAG_JUMBO_CAPABLE;
Michael Chan0f893dc2005-07-25 12:30:38 -070013113
Matt Carlson52f44902008-11-21 17:17:04 -080013114 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
13115 &pci_state_reg);
13116
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013117 tp->pcie_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_EXP);
13118 if (tp->pcie_cap != 0) {
13119 u16 lnkctl;
13120
Linus Torvalds1da177e2005-04-16 15:20:36 -070013121 tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS;
Matt Carlson5f5c51e2007-11-12 21:19:37 -080013122
13123 pcie_set_readrq(tp->pdev, 4096);
13124
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013125 pci_read_config_word(tp->pdev,
13126 tp->pcie_cap + PCI_EXP_LNKCTL,
13127 &lnkctl);
13128 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
13129 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanc7835a72006-11-15 21:14:42 -080013130 tp->tg3_flags2 &= ~TG3_FLG2_HW_TSO_2;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013131 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013132 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000013133 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
13134 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013135 tp->tg3_flags3 |= TG3_FLG3_CLKREQ_BUG;
Matt Carlson614b0592010-01-20 16:58:02 +000013136 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
13137 tp->tg3_flags3 |= TG3_FLG3_L1PLLPD_EN;
Michael Chanc7835a72006-11-15 21:14:42 -080013138 }
Matt Carlson52f44902008-11-21 17:17:04 -080013139 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Matt Carlsonfcb389d2008-11-03 16:55:44 -080013140 tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS;
Matt Carlson52f44902008-11-21 17:17:04 -080013141 } else if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
13142 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
13143 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
13144 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000013145 dev_err(&tp->pdev->dev,
13146 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080013147 return -EIO;
13148 }
13149
13150 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
13151 tp->tg3_flags |= TG3_FLAG_PCIX_MODE;
13152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013153
Michael Chan399de502005-10-03 14:02:39 -070013154 /* If we have an AMD 762 or VIA K8T800 chipset, write
13155 * reordering to the mailbox registers done by the host
13156 * controller can cause major troubles. We read back from
13157 * every mailbox register write to force the writes to be
13158 * posted to the chip in order.
13159 */
13160 if (pci_dev_present(write_reorder_chipsets) &&
13161 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
13162 tp->tg3_flags |= TG3_FLAG_MBOX_WRITE_REORDER;
13163
Matt Carlson69fc4052008-12-21 20:19:57 -080013164 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
13165 &tp->pci_cacheline_sz);
13166 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13167 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013168 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
13169 tp->pci_lat_timer < 64) {
13170 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080013171 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13172 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013173 }
13174
Matt Carlson52f44902008-11-21 17:17:04 -080013175 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
13176 /* 5700 BX chips need to have their TX producer index
13177 * mailboxes written twice to workaround a bug.
13178 */
13179 tp->tg3_flags |= TG3_FLAG_TXD_MBOX_HWBUG;
Matt Carlson9974a352007-10-07 23:27:28 -070013180
Matt Carlson52f44902008-11-21 17:17:04 -080013181 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013182 *
13183 * The workaround is to use indirect register accesses
13184 * for all chip writes not to mailbox registers.
13185 */
Matt Carlson52f44902008-11-21 17:17:04 -080013186 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013187 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013188
13189 tp->tg3_flags |= TG3_FLAG_PCIX_TARGET_HWBUG;
13190
13191 /* The chip can have it's power management PCI config
13192 * space registers clobbered due to this bug.
13193 * So explicitly force the chip into D0 here.
13194 */
Matt Carlson9974a352007-10-07 23:27:28 -070013195 pci_read_config_dword(tp->pdev,
13196 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013197 &pm_reg);
13198 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
13199 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070013200 pci_write_config_dword(tp->pdev,
13201 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013202 pm_reg);
13203
13204 /* Also, force SERR#/PERR# in PCI command. */
13205 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13206 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
13207 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13208 }
13209 }
13210
Linus Torvalds1da177e2005-04-16 15:20:36 -070013211 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
13212 tp->tg3_flags |= TG3_FLAG_PCI_HIGH_SPEED;
13213 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
13214 tp->tg3_flags |= TG3_FLAG_PCI_32BIT;
13215
13216 /* Chip-specific fixup from Broadcom driver */
13217 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
13218 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
13219 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
13220 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
13221 }
13222
Michael Chan1ee582d2005-08-09 20:16:46 -070013223 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070013224 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013225 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070013226 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070013227 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013228 tp->write32_tx_mbox = tg3_write32;
13229 tp->write32_rx_mbox = tg3_write32;
13230
13231 /* Various workaround register access methods */
13232 if (tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG)
13233 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013234 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
13235 ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
13236 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
13237 /*
13238 * Back to back register writes can cause problems on these
13239 * chips, the workaround is to read back all reg writes
13240 * except those to mailbox regs.
13241 *
13242 * See tg3_write_indirect_reg32().
13243 */
Michael Chan1ee582d2005-08-09 20:16:46 -070013244 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013245 }
13246
Michael Chan1ee582d2005-08-09 20:16:46 -070013247 if ((tp->tg3_flags & TG3_FLAG_TXD_MBOX_HWBUG) ||
13248 (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)) {
13249 tp->write32_tx_mbox = tg3_write32_tx_mbox;
13250 if (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)
13251 tp->write32_rx_mbox = tg3_write_flush_reg32;
13252 }
Michael Chan20094932005-08-09 20:16:32 -070013253
Michael Chan68929142005-08-09 20:17:14 -070013254 if (tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND) {
13255 tp->read32 = tg3_read_indirect_reg32;
13256 tp->write32 = tg3_write_indirect_reg32;
13257 tp->read32_mbox = tg3_read_indirect_mbox;
13258 tp->write32_mbox = tg3_write_indirect_mbox;
13259 tp->write32_tx_mbox = tg3_write_indirect_mbox;
13260 tp->write32_rx_mbox = tg3_write_indirect_mbox;
13261
13262 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070013263 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070013264
13265 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13266 pci_cmd &= ~PCI_COMMAND_MEMORY;
13267 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13268 }
Michael Chanb5d37722006-09-27 16:06:21 -070013269 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
13270 tp->read32_mbox = tg3_read32_mbox_5906;
13271 tp->write32_mbox = tg3_write32_mbox_5906;
13272 tp->write32_tx_mbox = tg3_write32_mbox_5906;
13273 tp->write32_rx_mbox = tg3_write32_mbox_5906;
13274 }
Michael Chan68929142005-08-09 20:17:14 -070013275
Michael Chanbbadf502006-04-06 21:46:34 -070013276 if (tp->write32 == tg3_write_indirect_reg32 ||
13277 ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) &&
13278 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070013279 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Michael Chanbbadf502006-04-06 21:46:34 -070013280 tp->tg3_flags |= TG3_FLAG_SRAM_USE_CONFIG;
13281
Michael Chan7d0c41e2005-04-21 17:06:20 -070013282 /* Get eeprom hw config before calling tg3_set_power_state().
Michael Chan9d26e212006-12-07 00:21:14 -080013283 * In particular, the TG3_FLG2_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070013284 * determined before calling tg3_set_power_state() so that
13285 * we know whether or not to switch out of Vaux power.
13286 * When the flag is set, it means that GPIO1 is used for eeprom
13287 * write protect and also implies that it is a LOM where GPIOs
13288 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013289 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070013290 tg3_get_eeprom_hw_cfg(tp);
13291
Matt Carlson0d3031d2007-10-10 18:02:43 -070013292 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
13293 /* Allow reads and writes to the
13294 * APE register and memory space.
13295 */
13296 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +000013297 PCISTATE_ALLOW_APE_SHMEM_WR |
13298 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070013299 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
13300 pci_state_reg);
13301 }
13302
Matt Carlson9936bcf2007-10-10 18:03:07 -070013303 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013304 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013305 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013306 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlsonb703df62009-12-03 08:36:21 +000013307 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsona50d0792010-06-05 17:24:37 +000013308 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlsonb703df62009-12-03 08:36:21 +000013309 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlsond30cdd22007-10-07 23:28:35 -070013310 tp->tg3_flags |= TG3_FLAG_CPMU_PRESENT;
13311
Michael Chan314fba32005-04-21 17:07:04 -070013312 /* Set up tp->grc_local_ctrl before calling tg3_set_power_state().
13313 * GPIO1 driven high will bring 5700's external PHY out of reset.
13314 * It is also used as eeprom write protect on LOMs.
13315 */
13316 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
13317 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
13318 (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT))
13319 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
13320 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070013321 /* Unused GPIO3 must be driven as output on 5752 because there
13322 * are no pull-up resistors on unused GPIO pins.
13323 */
13324 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
13325 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070013326
Matt Carlson321d32a2008-11-21 17:22:19 -080013327 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsoncb4ed1f2010-01-20 16:58:09 +000013328 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
13329 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Michael Chanaf36e6b2006-03-23 01:28:06 -080013330 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
13331
Matt Carlson8d519ab2009-04-20 06:58:01 +000013332 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
13333 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070013334 /* Turn off the debug UART. */
13335 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
13336 if (tp->tg3_flags2 & TG3_FLG2_IS_NIC)
13337 /* Keep VMain power. */
13338 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
13339 GRC_LCLCTRL_GPIO_OUTPUT0;
13340 }
13341
Linus Torvalds1da177e2005-04-16 15:20:36 -070013342 /* Force the chip into D0. */
Michael Chanbc1c7562006-03-20 17:48:03 -080013343 err = tg3_set_power_state(tp, PCI_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013344 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000013345 dev_err(&tp->pdev->dev, "Transition to D0 failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070013346 return err;
13347 }
13348
Linus Torvalds1da177e2005-04-16 15:20:36 -070013349 /* Derive initial jumbo mode from MTU assigned in
13350 * ether_setup() via the alloc_etherdev() call
13351 */
Michael Chan0f893dc2005-07-25 12:30:38 -070013352 if (tp->dev->mtu > ETH_DATA_LEN &&
Michael Chana4e2b342005-10-26 15:46:52 -070013353 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Michael Chan0f893dc2005-07-25 12:30:38 -070013354 tp->tg3_flags |= TG3_FLAG_JUMBO_RING_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013355
13356 /* Determine WakeOnLan speed to use. */
13357 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13358 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
13359 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
13360 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
13361 tp->tg3_flags &= ~(TG3_FLAG_WOL_SPEED_100MB);
13362 } else {
13363 tp->tg3_flags |= TG3_FLAG_WOL_SPEED_100MB;
13364 }
13365
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013366 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
13367 tp->tg3_flags3 |= TG3_FLG3_PHY_IS_FET;
13368
Linus Torvalds1da177e2005-04-16 15:20:36 -070013369 /* A few boards don't want Ethernet@WireSpeed phy feature */
13370 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
13371 ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
13372 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070013373 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013374 (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) ||
Michael Chan747e8f82005-07-25 12:33:22 -070013375 (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013376 tp->tg3_flags2 |= TG3_FLG2_NO_ETH_WIRE_SPEED;
13377
13378 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
13379 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
13380 tp->tg3_flags2 |= TG3_FLG2_PHY_ADC_BUG;
13381 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
13382 tp->tg3_flags2 |= TG3_FLG2_PHY_5704_A0_BUG;
13383
Matt Carlson321d32a2008-11-21 17:22:19 -080013384 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013385 !(tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080013386 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013387 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
Matt Carlsonb703df62009-12-03 08:36:21 +000013388 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
Matt Carlsona50d0792010-06-05 17:24:37 +000013389 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5719 &&
Matt Carlsonb703df62009-12-03 08:36:21 +000013390 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57765) {
Michael Chanc424cb22006-04-29 18:56:34 -070013391 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013392 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013393 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
13394 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080013395 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
13396 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
13397 tp->tg3_flags2 |= TG3_FLG2_PHY_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080013398 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
13399 tp->tg3_flags2 |= TG3_FLG2_PHY_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080013400 } else
Michael Chanc424cb22006-04-29 18:56:34 -070013401 tp->tg3_flags2 |= TG3_FLG2_PHY_BER_BUG;
13402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013403
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013404 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
13405 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
13406 tp->phy_otp = tg3_read_otp_phycfg(tp);
13407 if (tp->phy_otp == 0)
13408 tp->phy_otp = TG3_OTP_DEFAULT;
13409 }
13410
Matt Carlsonf51f3562008-05-25 23:45:08 -070013411 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT)
Matt Carlson8ef21422008-05-02 16:47:53 -070013412 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
13413 else
13414 tp->mi_mode = MAC_MI_MODE_BASE;
13415
Linus Torvalds1da177e2005-04-16 15:20:36 -070013416 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013417 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
13418 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
13419 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
13420
Matt Carlson321d32a2008-11-21 17:22:19 -080013421 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
13422 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson57e69832008-05-25 23:48:31 -070013423 tp->tg3_flags3 |= TG3_FLG3_USE_PHYLIB;
13424
Matt Carlson158d7ab2008-05-29 01:37:54 -070013425 err = tg3_mdio_init(tp);
13426 if (err)
13427 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013428
Matt Carlson55dffe72010-01-12 10:11:39 +000013429 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 &&
13430 (tp->pci_chip_rev_id != CHIPREV_ID_5717_A0 ||
13431 (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)))
13432 return -ENOTSUPP;
13433
Linus Torvalds1da177e2005-04-16 15:20:36 -070013434 /* Initialize data/descriptor byte/word swapping. */
13435 val = tr32(GRC_MODE);
13436 val &= GRC_MODE_HOST_STACKUP;
13437 tw32(GRC_MODE, val | tp->grc_mode);
13438
13439 tg3_switch_clocks(tp);
13440
13441 /* Clear this out for sanity. */
13442 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
13443
13444 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
13445 &pci_state_reg);
13446 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
13447 (tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG) == 0) {
13448 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
13449
13450 if (chiprevid == CHIPREV_ID_5701_A0 ||
13451 chiprevid == CHIPREV_ID_5701_B0 ||
13452 chiprevid == CHIPREV_ID_5701_B2 ||
13453 chiprevid == CHIPREV_ID_5701_B5) {
13454 void __iomem *sram_base;
13455
13456 /* Write some dummy words into the SRAM status block
13457 * area, see if it reads back correctly. If the return
13458 * value is bad, force enable the PCIX workaround.
13459 */
13460 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
13461
13462 writel(0x00000000, sram_base);
13463 writel(0x00000000, sram_base + 4);
13464 writel(0xffffffff, sram_base + 4);
13465 if (readl(sram_base) != 0x00000000)
13466 tp->tg3_flags |= TG3_FLAG_PCIX_TARGET_HWBUG;
13467 }
13468 }
13469
13470 udelay(50);
13471 tg3_nvram_init(tp);
13472
13473 grc_misc_cfg = tr32(GRC_MISC_CFG);
13474 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
13475
Linus Torvalds1da177e2005-04-16 15:20:36 -070013476 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
13477 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
13478 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
13479 tp->tg3_flags2 |= TG3_FLG2_IS_5788;
13480
David S. Millerfac9b832005-05-18 22:46:34 -070013481 if (!(tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
13482 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700))
13483 tp->tg3_flags |= TG3_FLAG_TAGGED_STATUS;
13484 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) {
13485 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
13486 HOSTCC_MODE_CLRTICK_TXBD);
13487
13488 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
13489 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13490 tp->misc_host_ctrl);
13491 }
13492
Matt Carlson3bda1252008-08-15 14:08:22 -070013493 /* Preserve the APE MAC_MODE bits */
13494 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
13495 tp->mac_mode = tr32(MAC_MODE) |
13496 MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
13497 else
13498 tp->mac_mode = TG3_DEF_MAC_MODE;
13499
Linus Torvalds1da177e2005-04-16 15:20:36 -070013500 /* these are limited to 10/100 only */
13501 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
13502 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
13503 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
13504 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
13505 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
13506 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
13507 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
13508 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
13509 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
Michael Chan676917d2006-12-07 00:20:22 -080013510 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
13511 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013512 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
Matt Carlsond1101142010-02-17 15:16:55 +000013513 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
13514 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013515 (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013516 tp->tg3_flags |= TG3_FLAG_10_100_ONLY;
13517
13518 err = tg3_phy_probe(tp);
13519 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000013520 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013521 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013522 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013523 }
13524
Matt Carlson184b8902010-04-05 10:19:25 +000013525 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080013526 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013527
13528 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
13529 tp->tg3_flags &= ~TG3_FLAG_USE_MI_INTERRUPT;
13530 } else {
13531 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
13532 tp->tg3_flags |= TG3_FLAG_USE_MI_INTERRUPT;
13533 else
13534 tp->tg3_flags &= ~TG3_FLAG_USE_MI_INTERRUPT;
13535 }
13536
13537 /* 5700 {AX,BX} chips have a broken status block link
13538 * change bit implementation, so we must use the
13539 * status register in those cases.
13540 */
13541 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
13542 tp->tg3_flags |= TG3_FLAG_USE_LINKCHG_REG;
13543 else
13544 tp->tg3_flags &= ~TG3_FLAG_USE_LINKCHG_REG;
13545
13546 /* The led_ctrl is set during tg3_phy_probe, here we might
13547 * have to force the link status polling mechanism based
13548 * upon subsystem IDs.
13549 */
13550 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070013551 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070013552 !(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)) {
13553 tp->tg3_flags |= (TG3_FLAG_USE_MI_INTERRUPT |
13554 TG3_FLAG_USE_LINKCHG_REG);
13555 }
13556
13557 /* For all SERDES we poll the MAC status register. */
13558 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
13559 tp->tg3_flags |= TG3_FLAG_POLL_SERDES;
13560 else
13561 tp->tg3_flags &= ~TG3_FLAG_POLL_SERDES;
13562
Matt Carlson9dc7a112010-04-12 06:58:28 +000013563 tp->rx_offset = NET_IP_ALIGN + TG3_RX_HEADROOM;
Matt Carlsond2757fc2010-04-12 06:58:27 +000013564 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013565 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsond2757fc2010-04-12 06:58:27 +000013566 (tp->tg3_flags & TG3_FLAG_PCIX_MODE) != 0) {
Matt Carlson9dc7a112010-04-12 06:58:28 +000013567 tp->rx_offset -= NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000013568#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000013569 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000013570#endif
13571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013572
Michael Chanf92905d2006-06-29 20:14:29 -070013573 tp->rx_std_max_post = TG3_RX_RING_SIZE;
13574
13575 /* Increment the rx prod index on the rx std ring by at most
13576 * 8 for these chips to workaround hw errata.
13577 */
13578 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13579 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
13580 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
13581 tp->rx_std_max_post = 8;
13582
Matt Carlson8ed5d972007-05-07 00:25:49 -070013583 if (tp->tg3_flags & TG3_FLAG_ASPM_WORKAROUND)
13584 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
13585 PCIE_PWR_MGMT_L1_THRESH_MSK;
13586
Linus Torvalds1da177e2005-04-16 15:20:36 -070013587 return err;
13588}
13589
David S. Miller49b6e95f2007-03-29 01:38:42 -070013590#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070013591static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
13592{
13593 struct net_device *dev = tp->dev;
13594 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070013595 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070013596 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070013597 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013598
David S. Miller49b6e95f2007-03-29 01:38:42 -070013599 addr = of_get_property(dp, "local-mac-address", &len);
13600 if (addr && len == 6) {
13601 memcpy(dev->dev_addr, addr, 6);
13602 memcpy(dev->perm_addr, dev->dev_addr, 6);
13603 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013604 }
13605 return -ENODEV;
13606}
13607
13608static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
13609{
13610 struct net_device *dev = tp->dev;
13611
13612 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070013613 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013614 return 0;
13615}
13616#endif
13617
13618static int __devinit tg3_get_device_address(struct tg3 *tp)
13619{
13620 struct net_device *dev = tp->dev;
13621 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080013622 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013623
David S. Miller49b6e95f2007-03-29 01:38:42 -070013624#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070013625 if (!tg3_get_macaddr_sparc(tp))
13626 return 0;
13627#endif
13628
13629 mac_offset = 0x7c;
David S. Millerf49639e2006-06-09 11:58:36 -070013630 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) ||
Michael Chana4e2b342005-10-26 15:46:52 -070013631 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013632 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
13633 mac_offset = 0xcc;
13634 if (tg3_nvram_lock(tp))
13635 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
13636 else
13637 tg3_nvram_unlock(tp);
Matt Carlsona50d0792010-06-05 17:24:37 +000013638 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13639 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) {
13640 if (PCI_FUNC(tp->pdev->devfn) & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000013641 mac_offset = 0xcc;
Matt Carlsona50d0792010-06-05 17:24:37 +000013642 if (PCI_FUNC(tp->pdev->devfn) > 1)
13643 mac_offset += 0x18c;
Matt Carlsona1b950d2009-09-01 13:20:17 +000013644 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070013645 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013646
13647 /* First try to get it from MAC address mailbox. */
13648 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
13649 if ((hi >> 16) == 0x484b) {
13650 dev->dev_addr[0] = (hi >> 8) & 0xff;
13651 dev->dev_addr[1] = (hi >> 0) & 0xff;
13652
13653 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
13654 dev->dev_addr[2] = (lo >> 24) & 0xff;
13655 dev->dev_addr[3] = (lo >> 16) & 0xff;
13656 dev->dev_addr[4] = (lo >> 8) & 0xff;
13657 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013658
Michael Chan008652b2006-03-27 23:14:53 -080013659 /* Some old bootcode may report a 0 MAC address in SRAM */
13660 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
13661 }
13662 if (!addr_ok) {
13663 /* Next, try NVRAM. */
Matt Carlsondf259d82009-04-20 06:57:14 +000013664 if (!(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) &&
13665 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000013666 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070013667 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
13668 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080013669 }
13670 /* Finally just fetch it out of the MAC control regs. */
13671 else {
13672 hi = tr32(MAC_ADDR_0_HIGH);
13673 lo = tr32(MAC_ADDR_0_LOW);
13674
13675 dev->dev_addr[5] = lo & 0xff;
13676 dev->dev_addr[4] = (lo >> 8) & 0xff;
13677 dev->dev_addr[3] = (lo >> 16) & 0xff;
13678 dev->dev_addr[2] = (lo >> 24) & 0xff;
13679 dev->dev_addr[1] = hi & 0xff;
13680 dev->dev_addr[0] = (hi >> 8) & 0xff;
13681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013682 }
13683
13684 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070013685#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070013686 if (!tg3_get_default_macaddr_sparc(tp))
13687 return 0;
13688#endif
13689 return -EINVAL;
13690 }
John W. Linville2ff43692005-09-12 14:44:20 -070013691 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013692 return 0;
13693}
13694
David S. Miller59e6b432005-05-18 22:50:10 -070013695#define BOUNDARY_SINGLE_CACHELINE 1
13696#define BOUNDARY_MULTI_CACHELINE 2
13697
13698static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
13699{
13700 int cacheline_size;
13701 u8 byte;
13702 int goal;
13703
13704 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
13705 if (byte == 0)
13706 cacheline_size = 1024;
13707 else
13708 cacheline_size = (int) byte * 4;
13709
13710 /* On 5703 and later chips, the boundary bits have no
13711 * effect.
13712 */
13713 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13714 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13715 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
13716 goto out;
13717
13718#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
13719 goal = BOUNDARY_MULTI_CACHELINE;
13720#else
13721#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
13722 goal = BOUNDARY_SINGLE_CACHELINE;
13723#else
13724 goal = 0;
13725#endif
13726#endif
13727
Matt Carlsonb703df62009-12-03 08:36:21 +000013728 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsona50d0792010-06-05 17:24:37 +000013729 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlsonb703df62009-12-03 08:36:21 +000013730 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000013731 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
13732 goto out;
13733 }
13734
David S. Miller59e6b432005-05-18 22:50:10 -070013735 if (!goal)
13736 goto out;
13737
13738 /* PCI controllers on most RISC systems tend to disconnect
13739 * when a device tries to burst across a cache-line boundary.
13740 * Therefore, letting tg3 do so just wastes PCI bandwidth.
13741 *
13742 * Unfortunately, for PCI-E there are only limited
13743 * write-side controls for this, and thus for reads
13744 * we will still get the disconnects. We'll also waste
13745 * these PCI cycles for both read and write for chips
13746 * other than 5700 and 5701 which do not implement the
13747 * boundary bits.
13748 */
13749 if ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) &&
13750 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)) {
13751 switch (cacheline_size) {
13752 case 16:
13753 case 32:
13754 case 64:
13755 case 128:
13756 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13757 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
13758 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
13759 } else {
13760 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
13761 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
13762 }
13763 break;
13764
13765 case 256:
13766 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
13767 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
13768 break;
13769
13770 default:
13771 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
13772 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
13773 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070013774 }
David S. Miller59e6b432005-05-18 22:50:10 -070013775 } else if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
13776 switch (cacheline_size) {
13777 case 16:
13778 case 32:
13779 case 64:
13780 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13781 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
13782 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
13783 break;
13784 }
13785 /* fallthrough */
13786 case 128:
13787 default:
13788 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
13789 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
13790 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070013791 }
David S. Miller59e6b432005-05-18 22:50:10 -070013792 } else {
13793 switch (cacheline_size) {
13794 case 16:
13795 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13796 val |= (DMA_RWCTRL_READ_BNDRY_16 |
13797 DMA_RWCTRL_WRITE_BNDRY_16);
13798 break;
13799 }
13800 /* fallthrough */
13801 case 32:
13802 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13803 val |= (DMA_RWCTRL_READ_BNDRY_32 |
13804 DMA_RWCTRL_WRITE_BNDRY_32);
13805 break;
13806 }
13807 /* fallthrough */
13808 case 64:
13809 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13810 val |= (DMA_RWCTRL_READ_BNDRY_64 |
13811 DMA_RWCTRL_WRITE_BNDRY_64);
13812 break;
13813 }
13814 /* fallthrough */
13815 case 128:
13816 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13817 val |= (DMA_RWCTRL_READ_BNDRY_128 |
13818 DMA_RWCTRL_WRITE_BNDRY_128);
13819 break;
13820 }
13821 /* fallthrough */
13822 case 256:
13823 val |= (DMA_RWCTRL_READ_BNDRY_256 |
13824 DMA_RWCTRL_WRITE_BNDRY_256);
13825 break;
13826 case 512:
13827 val |= (DMA_RWCTRL_READ_BNDRY_512 |
13828 DMA_RWCTRL_WRITE_BNDRY_512);
13829 break;
13830 case 1024:
13831 default:
13832 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
13833 DMA_RWCTRL_WRITE_BNDRY_1024);
13834 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070013835 }
David S. Miller59e6b432005-05-18 22:50:10 -070013836 }
13837
13838out:
13839 return val;
13840}
13841
Linus Torvalds1da177e2005-04-16 15:20:36 -070013842static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
13843{
13844 struct tg3_internal_buffer_desc test_desc;
13845 u32 sram_dma_descs;
13846 int i, ret;
13847
13848 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
13849
13850 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
13851 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
13852 tw32(RDMAC_STATUS, 0);
13853 tw32(WDMAC_STATUS, 0);
13854
13855 tw32(BUFMGR_MODE, 0);
13856 tw32(FTQ_RESET, 0);
13857
13858 test_desc.addr_hi = ((u64) buf_dma) >> 32;
13859 test_desc.addr_lo = buf_dma & 0xffffffff;
13860 test_desc.nic_mbuf = 0x00002100;
13861 test_desc.len = size;
13862
13863 /*
13864 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
13865 * the *second* time the tg3 driver was getting loaded after an
13866 * initial scan.
13867 *
13868 * Broadcom tells me:
13869 * ...the DMA engine is connected to the GRC block and a DMA
13870 * reset may affect the GRC block in some unpredictable way...
13871 * The behavior of resets to individual blocks has not been tested.
13872 *
13873 * Broadcom noted the GRC reset will also reset all sub-components.
13874 */
13875 if (to_device) {
13876 test_desc.cqid_sqid = (13 << 8) | 2;
13877
13878 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
13879 udelay(40);
13880 } else {
13881 test_desc.cqid_sqid = (16 << 8) | 7;
13882
13883 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
13884 udelay(40);
13885 }
13886 test_desc.flags = 0x00000005;
13887
13888 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
13889 u32 val;
13890
13891 val = *(((u32 *)&test_desc) + i);
13892 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
13893 sram_dma_descs + (i * sizeof(u32)));
13894 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
13895 }
13896 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
13897
Matt Carlson859a588792010-04-05 10:19:28 +000013898 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013899 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a588792010-04-05 10:19:28 +000013900 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070013901 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013902
13903 ret = -ENODEV;
13904 for (i = 0; i < 40; i++) {
13905 u32 val;
13906
13907 if (to_device)
13908 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
13909 else
13910 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
13911 if ((val & 0xffff) == sram_dma_descs) {
13912 ret = 0;
13913 break;
13914 }
13915
13916 udelay(100);
13917 }
13918
13919 return ret;
13920}
13921
David S. Millerded73402005-05-23 13:59:47 -070013922#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070013923
13924static int __devinit tg3_test_dma(struct tg3 *tp)
13925{
13926 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070013927 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000013928 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013929
13930 buf = pci_alloc_consistent(tp->pdev, TEST_BUFFER_SIZE, &buf_dma);
13931 if (!buf) {
13932 ret = -ENOMEM;
13933 goto out_nofree;
13934 }
13935
13936 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
13937 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
13938
David S. Miller59e6b432005-05-18 22:50:10 -070013939 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013940
Matt Carlsonb703df62009-12-03 08:36:21 +000013941 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsona50d0792010-06-05 17:24:37 +000013942 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlsonb703df62009-12-03 08:36:21 +000013943 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000013944 goto out;
13945
Linus Torvalds1da177e2005-04-16 15:20:36 -070013946 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
13947 /* DMA read watermark not used on PCIE */
13948 tp->dma_rwctrl |= 0x00180000;
13949 } else if (!(tp->tg3_flags & TG3_FLAG_PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070013950 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
13951 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013952 tp->dma_rwctrl |= 0x003f0000;
13953 else
13954 tp->dma_rwctrl |= 0x003f000f;
13955 } else {
13956 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
13957 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
13958 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080013959 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013960
Michael Chan4a29cc22006-03-19 13:21:12 -080013961 /* If the 5704 is behind the EPB bridge, we can
13962 * do the less restrictive ONE_DMA workaround for
13963 * better performance.
13964 */
13965 if ((tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG) &&
13966 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
13967 tp->dma_rwctrl |= 0x8000;
13968 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013969 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
13970
Michael Chan49afdeb2007-02-13 12:17:03 -080013971 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
13972 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070013973 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080013974 tp->dma_rwctrl |=
13975 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
13976 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
13977 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070013978 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
13979 /* 5780 always in PCIX mode */
13980 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070013981 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
13982 /* 5714 always in PCIX mode */
13983 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013984 } else {
13985 tp->dma_rwctrl |= 0x001b000f;
13986 }
13987 }
13988
13989 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
13990 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
13991 tp->dma_rwctrl &= 0xfffffff0;
13992
13993 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13994 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
13995 /* Remove this if it causes problems for some boards. */
13996 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
13997
13998 /* On 5700/5701 chips, we need to set this bit.
13999 * Otherwise the chip will issue cacheline transactions
14000 * to streamable DMA memory with not all the byte
14001 * enables turned on. This is an error on several
14002 * RISC PCI controllers, in particular sparc64.
14003 *
14004 * On 5703/5704 chips, this bit has been reassigned
14005 * a different meaning. In particular, it is used
14006 * on those chips to enable a PCI-X workaround.
14007 */
14008 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
14009 }
14010
14011 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14012
14013#if 0
14014 /* Unneeded, already done by tg3_get_invariants. */
14015 tg3_switch_clocks(tp);
14016#endif
14017
Linus Torvalds1da177e2005-04-16 15:20:36 -070014018 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14019 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
14020 goto out;
14021
David S. Miller59e6b432005-05-18 22:50:10 -070014022 /* It is best to perform DMA test with maximum write burst size
14023 * to expose the 5700/5701 write DMA bug.
14024 */
14025 saved_dma_rwctrl = tp->dma_rwctrl;
14026 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14027 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14028
Linus Torvalds1da177e2005-04-16 15:20:36 -070014029 while (1) {
14030 u32 *p = buf, i;
14031
14032 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
14033 p[i] = i;
14034
14035 /* Send the buffer to the chip. */
14036 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
14037 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000014038 dev_err(&tp->pdev->dev,
14039 "%s: Buffer write failed. err = %d\n",
14040 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014041 break;
14042 }
14043
14044#if 0
14045 /* validate data reached card RAM correctly. */
14046 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14047 u32 val;
14048 tg3_read_mem(tp, 0x2100 + (i*4), &val);
14049 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000014050 dev_err(&tp->pdev->dev,
14051 "%s: Buffer corrupted on device! "
14052 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014053 /* ret = -ENODEV here? */
14054 }
14055 p[i] = 0;
14056 }
14057#endif
14058 /* Now read it back. */
14059 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
14060 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000014061 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
14062 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014063 break;
14064 }
14065
14066 /* Verify it. */
14067 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14068 if (p[i] == i)
14069 continue;
14070
David S. Miller59e6b432005-05-18 22:50:10 -070014071 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14072 DMA_RWCTRL_WRITE_BNDRY_16) {
14073 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014074 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
14075 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14076 break;
14077 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000014078 dev_err(&tp->pdev->dev,
14079 "%s: Buffer corrupted on read back! "
14080 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014081 ret = -ENODEV;
14082 goto out;
14083 }
14084 }
14085
14086 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
14087 /* Success. */
14088 ret = 0;
14089 break;
14090 }
14091 }
David S. Miller59e6b432005-05-18 22:50:10 -070014092 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14093 DMA_RWCTRL_WRITE_BNDRY_16) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014094 static struct pci_device_id dma_wait_state_chipsets[] = {
14095 { PCI_DEVICE(PCI_VENDOR_ID_APPLE,
14096 PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
14097 { },
14098 };
14099
David S. Miller59e6b432005-05-18 22:50:10 -070014100 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070014101 * now look for chipsets that are known to expose the
14102 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070014103 */
Michael Chan6d1cfba2005-06-08 14:13:14 -070014104 if (pci_dev_present(dma_wait_state_chipsets)) {
14105 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14106 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a588792010-04-05 10:19:28 +000014107 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014108 /* Safe to use the calculated DMA boundary. */
14109 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a588792010-04-05 10:19:28 +000014110 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070014111
David S. Miller59e6b432005-05-18 22:50:10 -070014112 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14113 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014114
14115out:
14116 pci_free_consistent(tp->pdev, TEST_BUFFER_SIZE, buf, buf_dma);
14117out_nofree:
14118 return ret;
14119}
14120
14121static void __devinit tg3_init_link_config(struct tg3 *tp)
14122{
14123 tp->link_config.advertising =
14124 (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
14125 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
14126 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full |
14127 ADVERTISED_Autoneg | ADVERTISED_MII);
14128 tp->link_config.speed = SPEED_INVALID;
14129 tp->link_config.duplex = DUPLEX_INVALID;
14130 tp->link_config.autoneg = AUTONEG_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014131 tp->link_config.active_speed = SPEED_INVALID;
14132 tp->link_config.active_duplex = DUPLEX_INVALID;
14133 tp->link_config.phy_is_low_power = 0;
14134 tp->link_config.orig_speed = SPEED_INVALID;
14135 tp->link_config.orig_duplex = DUPLEX_INVALID;
14136 tp->link_config.orig_autoneg = AUTONEG_INVALID;
14137}
14138
14139static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
14140{
Matt Carlson666bc832010-01-20 16:58:03 +000014141 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsona50d0792010-06-05 17:24:37 +000014142 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlson666bc832010-01-20 16:58:03 +000014143 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
14144 tp->bufmgr_config.mbuf_read_dma_low_water =
14145 DEFAULT_MB_RDMA_LOW_WATER_5705;
14146 tp->bufmgr_config.mbuf_mac_rx_low_water =
14147 DEFAULT_MB_MACRX_LOW_WATER_57765;
14148 tp->bufmgr_config.mbuf_high_water =
14149 DEFAULT_MB_HIGH_WATER_57765;
14150
14151 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14152 DEFAULT_MB_RDMA_LOW_WATER_5705;
14153 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14154 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
14155 tp->bufmgr_config.mbuf_high_water_jumbo =
14156 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
14157 } else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
Michael Chanfdfec1722005-07-25 12:31:48 -070014158 tp->bufmgr_config.mbuf_read_dma_low_water =
14159 DEFAULT_MB_RDMA_LOW_WATER_5705;
14160 tp->bufmgr_config.mbuf_mac_rx_low_water =
14161 DEFAULT_MB_MACRX_LOW_WATER_5705;
14162 tp->bufmgr_config.mbuf_high_water =
14163 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070014164 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
14165 tp->bufmgr_config.mbuf_mac_rx_low_water =
14166 DEFAULT_MB_MACRX_LOW_WATER_5906;
14167 tp->bufmgr_config.mbuf_high_water =
14168 DEFAULT_MB_HIGH_WATER_5906;
14169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014170
Michael Chanfdfec1722005-07-25 12:31:48 -070014171 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14172 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
14173 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14174 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
14175 tp->bufmgr_config.mbuf_high_water_jumbo =
14176 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
14177 } else {
14178 tp->bufmgr_config.mbuf_read_dma_low_water =
14179 DEFAULT_MB_RDMA_LOW_WATER;
14180 tp->bufmgr_config.mbuf_mac_rx_low_water =
14181 DEFAULT_MB_MACRX_LOW_WATER;
14182 tp->bufmgr_config.mbuf_high_water =
14183 DEFAULT_MB_HIGH_WATER;
14184
14185 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14186 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
14187 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14188 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
14189 tp->bufmgr_config.mbuf_high_water_jumbo =
14190 DEFAULT_MB_HIGH_WATER_JUMBO;
14191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014192
14193 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
14194 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
14195}
14196
14197static char * __devinit tg3_phy_string(struct tg3 *tp)
14198{
Matt Carlson79eb6902010-02-17 15:17:03 +000014199 switch (tp->phy_id & TG3_PHY_ID_MASK) {
14200 case TG3_PHY_ID_BCM5400: return "5400";
14201 case TG3_PHY_ID_BCM5401: return "5401";
14202 case TG3_PHY_ID_BCM5411: return "5411";
14203 case TG3_PHY_ID_BCM5701: return "5701";
14204 case TG3_PHY_ID_BCM5703: return "5703";
14205 case TG3_PHY_ID_BCM5704: return "5704";
14206 case TG3_PHY_ID_BCM5705: return "5705";
14207 case TG3_PHY_ID_BCM5750: return "5750";
14208 case TG3_PHY_ID_BCM5752: return "5752";
14209 case TG3_PHY_ID_BCM5714: return "5714";
14210 case TG3_PHY_ID_BCM5780: return "5780";
14211 case TG3_PHY_ID_BCM5755: return "5755";
14212 case TG3_PHY_ID_BCM5787: return "5787";
14213 case TG3_PHY_ID_BCM5784: return "5784";
14214 case TG3_PHY_ID_BCM5756: return "5722/5756";
14215 case TG3_PHY_ID_BCM5906: return "5906";
14216 case TG3_PHY_ID_BCM5761: return "5761";
14217 case TG3_PHY_ID_BCM5718C: return "5718C";
14218 case TG3_PHY_ID_BCM5718S: return "5718S";
14219 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000014220 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson79eb6902010-02-17 15:17:03 +000014221 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070014222 case 0: return "serdes";
14223 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070014224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014225}
14226
Michael Chanf9804dd2005-09-27 12:13:10 -070014227static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
14228{
14229 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
14230 strcpy(str, "PCI Express");
14231 return str;
14232 } else if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
14233 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
14234
14235 strcpy(str, "PCIX:");
14236
14237 if ((clock_ctrl == 7) ||
14238 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
14239 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
14240 strcat(str, "133MHz");
14241 else if (clock_ctrl == 0)
14242 strcat(str, "33MHz");
14243 else if (clock_ctrl == 2)
14244 strcat(str, "50MHz");
14245 else if (clock_ctrl == 4)
14246 strcat(str, "66MHz");
14247 else if (clock_ctrl == 6)
14248 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070014249 } else {
14250 strcpy(str, "PCI:");
14251 if (tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED)
14252 strcat(str, "66MHz");
14253 else
14254 strcat(str, "33MHz");
14255 }
14256 if (tp->tg3_flags & TG3_FLAG_PCI_32BIT)
14257 strcat(str, ":32-bit");
14258 else
14259 strcat(str, ":64-bit");
14260 return str;
14261}
14262
Michael Chan8c2dc7e2005-12-19 16:26:02 -080014263static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014264{
14265 struct pci_dev *peer;
14266 unsigned int func, devnr = tp->pdev->devfn & ~7;
14267
14268 for (func = 0; func < 8; func++) {
14269 peer = pci_get_slot(tp->pdev->bus, devnr | func);
14270 if (peer && peer != tp->pdev)
14271 break;
14272 pci_dev_put(peer);
14273 }
Michael Chan16fe9d72005-12-13 21:09:54 -080014274 /* 5704 can be configured in single-port mode, set peer to
14275 * tp->pdev in that case.
14276 */
14277 if (!peer) {
14278 peer = tp->pdev;
14279 return peer;
14280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014281
14282 /*
14283 * We don't need to keep the refcount elevated; there's no way
14284 * to remove one half of this device without removing the other
14285 */
14286 pci_dev_put(peer);
14287
14288 return peer;
14289}
14290
David S. Miller15f98502005-05-18 22:49:26 -070014291static void __devinit tg3_init_coal(struct tg3 *tp)
14292{
14293 struct ethtool_coalesce *ec = &tp->coal;
14294
14295 memset(ec, 0, sizeof(*ec));
14296 ec->cmd = ETHTOOL_GCOALESCE;
14297 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
14298 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
14299 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
14300 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
14301 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
14302 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
14303 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
14304 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
14305 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
14306
14307 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
14308 HOSTCC_MODE_CLRTICK_TXBD)) {
14309 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
14310 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
14311 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
14312 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
14313 }
Michael Chand244c892005-07-05 14:42:33 -070014314
14315 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
14316 ec->rx_coalesce_usecs_irq = 0;
14317 ec->tx_coalesce_usecs_irq = 0;
14318 ec->stats_block_coalesce_usecs = 0;
14319 }
David S. Miller15f98502005-05-18 22:49:26 -070014320}
14321
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080014322static const struct net_device_ops tg3_netdev_ops = {
14323 .ndo_open = tg3_open,
14324 .ndo_stop = tg3_close,
Stephen Hemminger00829822008-11-20 20:14:53 -080014325 .ndo_start_xmit = tg3_start_xmit,
Eric Dumazet511d2222010-07-07 20:44:24 +000014326 .ndo_get_stats64 = tg3_get_stats64,
Stephen Hemminger00829822008-11-20 20:14:53 -080014327 .ndo_validate_addr = eth_validate_addr,
14328 .ndo_set_multicast_list = tg3_set_rx_mode,
14329 .ndo_set_mac_address = tg3_set_mac_addr,
14330 .ndo_do_ioctl = tg3_ioctl,
14331 .ndo_tx_timeout = tg3_tx_timeout,
14332 .ndo_change_mtu = tg3_change_mtu,
14333#if TG3_VLAN_TAG_USED
14334 .ndo_vlan_rx_register = tg3_vlan_rx_register,
14335#endif
14336#ifdef CONFIG_NET_POLL_CONTROLLER
14337 .ndo_poll_controller = tg3_poll_controller,
14338#endif
14339};
14340
14341static const struct net_device_ops tg3_netdev_ops_dma_bug = {
14342 .ndo_open = tg3_open,
14343 .ndo_stop = tg3_close,
14344 .ndo_start_xmit = tg3_start_xmit_dma_bug,
Eric Dumazet511d2222010-07-07 20:44:24 +000014345 .ndo_get_stats64 = tg3_get_stats64,
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080014346 .ndo_validate_addr = eth_validate_addr,
14347 .ndo_set_multicast_list = tg3_set_rx_mode,
14348 .ndo_set_mac_address = tg3_set_mac_addr,
14349 .ndo_do_ioctl = tg3_ioctl,
14350 .ndo_tx_timeout = tg3_tx_timeout,
14351 .ndo_change_mtu = tg3_change_mtu,
14352#if TG3_VLAN_TAG_USED
14353 .ndo_vlan_rx_register = tg3_vlan_rx_register,
14354#endif
14355#ifdef CONFIG_NET_POLL_CONTROLLER
14356 .ndo_poll_controller = tg3_poll_controller,
14357#endif
14358};
14359
Linus Torvalds1da177e2005-04-16 15:20:36 -070014360static int __devinit tg3_init_one(struct pci_dev *pdev,
14361 const struct pci_device_id *ent)
14362{
Linus Torvalds1da177e2005-04-16 15:20:36 -070014363 struct net_device *dev;
14364 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000014365 int i, err, pm_cap;
14366 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070014367 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080014368 u64 dma_mask, persist_dma_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014369
Joe Perches05dbe002010-02-17 19:44:19 +000014370 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014371
14372 err = pci_enable_device(pdev);
14373 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014374 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014375 return err;
14376 }
14377
Linus Torvalds1da177e2005-04-16 15:20:36 -070014378 err = pci_request_regions(pdev, DRV_MODULE_NAME);
14379 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014380 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014381 goto err_out_disable_pdev;
14382 }
14383
14384 pci_set_master(pdev);
14385
14386 /* Find power-management capability. */
14387 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
14388 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000014389 dev_err(&pdev->dev,
14390 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014391 err = -EIO;
14392 goto err_out_free_res;
14393 }
14394
Matt Carlsonfe5f5782009-09-01 13:09:39 +000014395 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014396 if (!dev) {
Matt Carlson2445e462010-04-05 10:19:21 +000014397 dev_err(&pdev->dev, "Etherdev alloc failed, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014398 err = -ENOMEM;
14399 goto err_out_free_res;
14400 }
14401
Linus Torvalds1da177e2005-04-16 15:20:36 -070014402 SET_NETDEV_DEV(dev, &pdev->dev);
14403
Linus Torvalds1da177e2005-04-16 15:20:36 -070014404#if TG3_VLAN_TAG_USED
14405 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014406#endif
14407
14408 tp = netdev_priv(dev);
14409 tp->pdev = pdev;
14410 tp->dev = dev;
14411 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014412 tp->rx_mode = TG3_DEF_RX_MODE;
14413 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070014414
Linus Torvalds1da177e2005-04-16 15:20:36 -070014415 if (tg3_debug > 0)
14416 tp->msg_enable = tg3_debug;
14417 else
14418 tp->msg_enable = TG3_DEF_MSG_ENABLE;
14419
14420 /* The word/byte swap controls here control register access byte
14421 * swapping. DMA data byte swapping is controlled in the GRC_MODE
14422 * setting below.
14423 */
14424 tp->misc_host_ctrl =
14425 MISC_HOST_CTRL_MASK_PCI_INT |
14426 MISC_HOST_CTRL_WORD_SWAP |
14427 MISC_HOST_CTRL_INDIR_ACCESS |
14428 MISC_HOST_CTRL_PCISTATE_RW;
14429
14430 /* The NONFRM (non-frame) byte/word swap controls take effect
14431 * on descriptor entries, anything which isn't packet data.
14432 *
14433 * The StrongARM chips on the board (one for tx, one for rx)
14434 * are running in big-endian mode.
14435 */
14436 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
14437 GRC_MODE_WSWAP_NONFRM_DATA);
14438#ifdef __BIG_ENDIAN
14439 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
14440#endif
14441 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014442 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000014443 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014444
Matt Carlsond5fe4882008-11-21 17:20:32 -080014445 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010014446 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014447 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014448 err = -ENOMEM;
14449 goto err_out_free_dev;
14450 }
14451
14452 tg3_init_link_config(tp);
14453
Linus Torvalds1da177e2005-04-16 15:20:36 -070014454 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
14455 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014456
Linus Torvalds1da177e2005-04-16 15:20:36 -070014457 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014458 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014459 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014460
14461 err = tg3_get_invariants(tp);
14462 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014463 dev_err(&pdev->dev,
14464 "Problem fetching invariants of chip, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014465 goto err_out_iounmap;
14466 }
14467
Matt Carlson615774f2009-11-13 13:03:39 +000014468 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) &&
Matt Carlsona50d0792010-06-05 17:24:37 +000014469 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0 &&
14470 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5719)
Stephen Hemminger00829822008-11-20 20:14:53 -080014471 dev->netdev_ops = &tg3_netdev_ops;
14472 else
14473 dev->netdev_ops = &tg3_netdev_ops_dma_bug;
14474
14475
Michael Chan4a29cc22006-03-19 13:21:12 -080014476 /* The EPB bridge inside 5714, 5715, and 5780 and any
14477 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080014478 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
14479 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
14480 * do DMA address check in tg3_start_xmit().
14481 */
Michael Chan4a29cc22006-03-19 13:21:12 -080014482 if (tp->tg3_flags2 & TG3_FLG2_IS_5788)
Yang Hongyang284901a2009-04-06 19:01:15 -070014483 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Michael Chan4a29cc22006-03-19 13:21:12 -080014484 else if (tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070014485 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080014486#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070014487 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080014488#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080014489 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070014490 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080014491
14492 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070014493 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080014494 err = pci_set_dma_mask(pdev, dma_mask);
14495 if (!err) {
14496 dev->features |= NETIF_F_HIGHDMA;
14497 err = pci_set_consistent_dma_mask(pdev,
14498 persist_dma_mask);
14499 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014500 dev_err(&pdev->dev, "Unable to obtain 64 bit "
14501 "DMA for consistent allocations\n");
Michael Chan72f2afb2006-03-06 19:28:35 -080014502 goto err_out_iounmap;
14503 }
14504 }
14505 }
Yang Hongyang284901a2009-04-06 19:01:15 -070014506 if (err || dma_mask == DMA_BIT_MASK(32)) {
14507 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080014508 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014509 dev_err(&pdev->dev,
14510 "No usable DMA configuration, aborting\n");
Michael Chan72f2afb2006-03-06 19:28:35 -080014511 goto err_out_iounmap;
14512 }
14513 }
14514
Michael Chanfdfec1722005-07-25 12:31:48 -070014515 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014516
Matt Carlson507399f2009-11-13 13:03:37 +000014517 /* Selectively allow TSO based on operating conditions */
14518 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO) ||
14519 (tp->fw_needed && !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)))
14520 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
14521 else {
14522 tp->tg3_flags2 &= ~(TG3_FLG2_TSO_CAPABLE | TG3_FLG2_TSO_BUG);
14523 tp->fw_needed = NULL;
14524 }
14525
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014526 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
Matt Carlson9e9fd122009-01-19 16:57:45 -080014527 tp->fw_needed = FIRMWARE_TG3;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014528
Michael Chan4e3a7aa2006-03-20 17:47:44 -080014529 /* TSO is on by default on chips that support hardware TSO.
14530 * Firmware TSO on older chips gives lower performance, so it
14531 * is off by default, but can be enabled using ethtool.
14532 */
Matt Carlsone849cdc2009-11-13 13:03:38 +000014533 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO) &&
Eric Dumazet7fe876a2010-07-08 06:14:55 +000014534 (dev->features & NETIF_F_IP_CSUM)) {
Matt Carlsone849cdc2009-11-13 13:03:38 +000014535 dev->features |= NETIF_F_TSO;
Eric Dumazet7fe876a2010-07-08 06:14:55 +000014536 vlan_features_add(dev, NETIF_F_TSO);
14537 }
Matt Carlsone849cdc2009-11-13 13:03:38 +000014538 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_2) ||
14539 (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3)) {
Eric Dumazet7fe876a2010-07-08 06:14:55 +000014540 if (dev->features & NETIF_F_IPV6_CSUM) {
Michael Chanb0026622006-07-03 19:42:14 -070014541 dev->features |= NETIF_F_TSO6;
Eric Dumazet7fe876a2010-07-08 06:14:55 +000014542 vlan_features_add(dev, NETIF_F_TSO6);
14543 }
Matt Carlsone849cdc2009-11-13 13:03:38 +000014544 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
14545 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070014546 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
14547 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014548 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Eric Dumazet7fe876a2010-07-08 06:14:55 +000014549 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070014550 dev->features |= NETIF_F_TSO_ECN;
Eric Dumazet7fe876a2010-07-08 06:14:55 +000014551 vlan_features_add(dev, NETIF_F_TSO_ECN);
14552 }
Michael Chanb0026622006-07-03 19:42:14 -070014553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014554
Linus Torvalds1da177e2005-04-16 15:20:36 -070014555 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
14556 !(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) &&
14557 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
14558 tp->tg3_flags2 |= TG3_FLG2_MAX_RXPEND_64;
14559 tp->rx_pending = 63;
14560 }
14561
Linus Torvalds1da177e2005-04-16 15:20:36 -070014562 err = tg3_get_device_address(tp);
14563 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014564 dev_err(&pdev->dev,
14565 "Could not obtain valid ethernet address, aborting\n");
Matt Carlson026a6c22009-12-03 08:36:24 +000014566 goto err_out_iounmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014567 }
14568
Matt Carlson0d3031d2007-10-10 18:02:43 -070014569 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
Matt Carlson63532392008-11-03 16:49:57 -080014570 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
Al Viro79ea13c2008-01-24 02:06:46 -080014571 if (!tp->aperegs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014572 dev_err(&pdev->dev,
14573 "Cannot map APE registers, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070014574 err = -ENOMEM;
Matt Carlson026a6c22009-12-03 08:36:24 +000014575 goto err_out_iounmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070014576 }
14577
14578 tg3_ape_lock_init(tp);
Matt Carlson7fd76442009-02-25 14:27:20 +000014579
14580 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF)
14581 tg3_read_dash_ver(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070014582 }
14583
Matt Carlsonc88864d2007-11-12 21:07:01 -080014584 /*
14585 * Reset chip in case UNDI or EFI driver did not shutdown
14586 * DMA self test will enable WDMAC and we'll see (spurious)
14587 * pending DMA on the PCI bus at that point.
14588 */
14589 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
14590 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
14591 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
14592 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
14593 }
14594
14595 err = tg3_test_dma(tp);
14596 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014597 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080014598 goto err_out_apeunmap;
14599 }
14600
Matt Carlsonc88864d2007-11-12 21:07:01 -080014601 /* flow control autonegotiation is default behavior */
14602 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
Steve Glendinninge18ce342008-12-16 02:00:00 -080014603 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlsonc88864d2007-11-12 21:07:01 -080014604
Matt Carlson78f90dc2009-11-13 13:03:42 +000014605 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
14606 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
14607 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
14608 for (i = 0; i < TG3_IRQ_MAX_VECS; i++) {
14609 struct tg3_napi *tnapi = &tp->napi[i];
14610
14611 tnapi->tp = tp;
14612 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
14613
14614 tnapi->int_mbox = intmbx;
14615 if (i < 4)
14616 intmbx += 0x8;
14617 else
14618 intmbx += 0x4;
14619
14620 tnapi->consmbox = rcvmbx;
14621 tnapi->prodmbox = sndmbx;
14622
14623 if (i) {
14624 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
14625 netif_napi_add(dev, &tnapi->napi, tg3_poll_msix, 64);
14626 } else {
14627 tnapi->coal_now = HOSTCC_MODE_NOW;
14628 netif_napi_add(dev, &tnapi->napi, tg3_poll, 64);
14629 }
14630
14631 if (!(tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX))
14632 break;
14633
14634 /*
14635 * If we support MSIX, we'll be using RSS. If we're using
14636 * RSS, the first vector only handles link interrupts and the
14637 * remaining vectors handle rx and tx interrupts. Reuse the
14638 * mailbox values for the next iteration. The values we setup
14639 * above are still useful for the single vectored mode.
14640 */
14641 if (!i)
14642 continue;
14643
14644 rcvmbx += 0x8;
14645
14646 if (sndmbx & 0x4)
14647 sndmbx -= 0x4;
14648 else
14649 sndmbx += 0xc;
14650 }
14651
Matt Carlsonc88864d2007-11-12 21:07:01 -080014652 tg3_init_coal(tp);
14653
Michael Chanc49a1562006-12-17 17:07:29 -080014654 pci_set_drvdata(pdev, dev);
14655
Linus Torvalds1da177e2005-04-16 15:20:36 -070014656 err = register_netdev(dev);
14657 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014658 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070014659 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014660 }
14661
Joe Perches05dbe002010-02-17 19:44:19 +000014662 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
14663 tp->board_part_number,
14664 tp->pci_chip_rev_id,
14665 tg3_bus_string(tp, str),
14666 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014667
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000014668 if (tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED) {
14669 struct phy_device *phydev;
14670 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000014671 netdev_info(dev,
14672 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000014673 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000014674 } else
Matt Carlson5129c3a2010-04-05 10:19:23 +000014675 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
14676 "(WireSpeed[%d])\n", tg3_phy_string(tp),
Joe Perches05dbe002010-02-17 19:44:19 +000014677 ((tp->tg3_flags & TG3_FLAG_10_100_ONLY) ? "10/100Base-TX" :
14678 ((tp->tg3_flags2 & TG3_FLG2_ANY_SERDES) ? "1000Base-SX" :
14679 "10/100/1000Base-T")),
14680 (tp->tg3_flags2 & TG3_FLG2_NO_ETH_WIRE_SPEED) == 0);
Matt Carlsondf59c942008-11-03 16:52:56 -080014681
Joe Perches05dbe002010-02-17 19:44:19 +000014682 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
14683 (tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) != 0,
14684 (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) != 0,
14685 (tp->tg3_flags & TG3_FLAG_USE_MI_INTERRUPT) != 0,
14686 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0,
14687 (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) != 0);
14688 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
14689 tp->dma_rwctrl,
14690 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
14691 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014692
14693 return 0;
14694
Matt Carlson0d3031d2007-10-10 18:02:43 -070014695err_out_apeunmap:
14696 if (tp->aperegs) {
14697 iounmap(tp->aperegs);
14698 tp->aperegs = NULL;
14699 }
14700
Linus Torvalds1da177e2005-04-16 15:20:36 -070014701err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070014702 if (tp->regs) {
14703 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070014704 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070014705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014706
14707err_out_free_dev:
14708 free_netdev(dev);
14709
14710err_out_free_res:
14711 pci_release_regions(pdev);
14712
14713err_out_disable_pdev:
14714 pci_disable_device(pdev);
14715 pci_set_drvdata(pdev, NULL);
14716 return err;
14717}
14718
14719static void __devexit tg3_remove_one(struct pci_dev *pdev)
14720{
14721 struct net_device *dev = pci_get_drvdata(pdev);
14722
14723 if (dev) {
14724 struct tg3 *tp = netdev_priv(dev);
14725
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014726 if (tp->fw)
14727 release_firmware(tp->fw);
14728
Michael Chan7faa0062006-02-02 17:29:28 -080014729 flush_scheduled_work();
Matt Carlson158d7ab2008-05-29 01:37:54 -070014730
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014731 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
14732 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070014733 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014734 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070014735
Linus Torvalds1da177e2005-04-16 15:20:36 -070014736 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070014737 if (tp->aperegs) {
14738 iounmap(tp->aperegs);
14739 tp->aperegs = NULL;
14740 }
Michael Chan68929142005-08-09 20:17:14 -070014741 if (tp->regs) {
14742 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070014743 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070014744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014745 free_netdev(dev);
14746 pci_release_regions(pdev);
14747 pci_disable_device(pdev);
14748 pci_set_drvdata(pdev, NULL);
14749 }
14750}
14751
14752static int tg3_suspend(struct pci_dev *pdev, pm_message_t state)
14753{
14754 struct net_device *dev = pci_get_drvdata(pdev);
14755 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070014756 pci_power_t target_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014757 int err;
14758
Michael Chan3e0c95f2007-08-03 20:56:54 -070014759 /* PCI register 4 needs to be saved whether netif_running() or not.
14760 * MSI address and data need to be saved if using MSI and
14761 * netif_running().
14762 */
14763 pci_save_state(pdev);
14764
Linus Torvalds1da177e2005-04-16 15:20:36 -070014765 if (!netif_running(dev))
14766 return 0;
14767
Michael Chan7faa0062006-02-02 17:29:28 -080014768 flush_scheduled_work();
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014769 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014770 tg3_netif_stop(tp);
14771
14772 del_timer_sync(&tp->timer);
14773
David S. Millerf47c11e2005-06-24 20:18:35 -070014774 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014775 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070014776 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014777
14778 netif_device_detach(dev);
14779
David S. Millerf47c11e2005-06-24 20:18:35 -070014780 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070014781 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan6a9eba12005-12-13 21:08:58 -080014782 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
David S. Millerf47c11e2005-06-24 20:18:35 -070014783 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014784
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070014785 target_state = pdev->pm_cap ? pci_target_state(pdev) : PCI_D3hot;
14786
14787 err = tg3_set_power_state(tp, target_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014788 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014789 int err2;
14790
David S. Millerf47c11e2005-06-24 20:18:35 -070014791 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014792
Michael Chan6a9eba12005-12-13 21:08:58 -080014793 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014794 err2 = tg3_restart_hw(tp, 1);
14795 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070014796 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014797
14798 tp->timer.expires = jiffies + tp->timer_offset;
14799 add_timer(&tp->timer);
14800
14801 netif_device_attach(dev);
14802 tg3_netif_start(tp);
14803
Michael Chanb9ec6c12006-07-25 16:37:27 -070014804out:
David S. Millerf47c11e2005-06-24 20:18:35 -070014805 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014806
14807 if (!err2)
14808 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014809 }
14810
14811 return err;
14812}
14813
14814static int tg3_resume(struct pci_dev *pdev)
14815{
14816 struct net_device *dev = pci_get_drvdata(pdev);
14817 struct tg3 *tp = netdev_priv(dev);
14818 int err;
14819
Michael Chan3e0c95f2007-08-03 20:56:54 -070014820 pci_restore_state(tp->pdev);
14821
Linus Torvalds1da177e2005-04-16 15:20:36 -070014822 if (!netif_running(dev))
14823 return 0;
14824
Michael Chanbc1c7562006-03-20 17:48:03 -080014825 err = tg3_set_power_state(tp, PCI_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014826 if (err)
14827 return err;
14828
14829 netif_device_attach(dev);
14830
David S. Millerf47c11e2005-06-24 20:18:35 -070014831 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014832
Michael Chan6a9eba12005-12-13 21:08:58 -080014833 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Michael Chanb9ec6c12006-07-25 16:37:27 -070014834 err = tg3_restart_hw(tp, 1);
14835 if (err)
14836 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014837
14838 tp->timer.expires = jiffies + tp->timer_offset;
14839 add_timer(&tp->timer);
14840
Linus Torvalds1da177e2005-04-16 15:20:36 -070014841 tg3_netif_start(tp);
14842
Michael Chanb9ec6c12006-07-25 16:37:27 -070014843out:
David S. Millerf47c11e2005-06-24 20:18:35 -070014844 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014845
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014846 if (!err)
14847 tg3_phy_start(tp);
14848
Michael Chanb9ec6c12006-07-25 16:37:27 -070014849 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014850}
14851
14852static struct pci_driver tg3_driver = {
14853 .name = DRV_MODULE_NAME,
14854 .id_table = tg3_pci_tbl,
14855 .probe = tg3_init_one,
14856 .remove = __devexit_p(tg3_remove_one),
14857 .suspend = tg3_suspend,
14858 .resume = tg3_resume
14859};
14860
14861static int __init tg3_init(void)
14862{
Jeff Garzik29917622006-08-19 17:48:59 -040014863 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014864}
14865
14866static void __exit tg3_cleanup(void)
14867{
14868 pci_unregister_driver(&tg3_driver);
14869}
14870
14871module_init(tg3_init);
14872module_exit(tg3_cleanup);