blob: 820a7ddd7e091fd314897b551712337d9638d791 [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>
Matt Carlson6867c842010-07-11 09:31:44 +000021#include <linux/stringify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/kernel.h>
23#include <linux/types.h>
24#include <linux/compiler.h>
25#include <linux/slab.h>
26#include <linux/delay.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020027#include <linux/in.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/init.h>
29#include <linux/ioport.h>
30#include <linux/pci.h>
31#include <linux/netdevice.h>
32#include <linux/etherdevice.h>
33#include <linux/skbuff.h>
34#include <linux/ethtool.h>
35#include <linux/mii.h>
Matt Carlson158d7ab2008-05-29 01:37:54 -070036#include <linux/phy.h>
Matt Carlsona9daf362008-05-25 23:49:44 -070037#include <linux/brcmphy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/if_vlan.h>
39#include <linux/ip.h>
40#include <linux/tcp.h>
41#include <linux/workqueue.h>
Michael Chan61487482005-09-05 17:53:19 -070042#include <linux/prefetch.h>
Tobias Klauserf9a5f7d2005-10-29 15:09:26 +020043#include <linux/dma-mapping.h>
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080044#include <linux/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#include <net/checksum.h>
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030047#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49#include <asm/system.h>
50#include <asm/io.h>
51#include <asm/byteorder.h>
52#include <asm/uaccess.h>
53
David S. Miller49b6e95f2007-03-29 01:38:42 -070054#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <asm/idprom.h>
David S. Miller49b6e95f2007-03-29 01:38:42 -070056#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#endif
58
Matt Carlson63532392008-11-03 16:49:57 -080059#define BAR_0 0
60#define BAR_2 2
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
63#define TG3_VLAN_TAG_USED 1
64#else
65#define TG3_VLAN_TAG_USED 0
66#endif
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include "tg3.h"
69
70#define DRV_MODULE_NAME "tg3"
Matt Carlson6867c842010-07-11 09:31:44 +000071#define TG3_MAJ_NUM 3
Matt Carlson98e32a92010-07-11 09:31:47 +000072#define TG3_MIN_NUM 112
Matt Carlson6867c842010-07-11 09:31:44 +000073#define DRV_MODULE_VERSION \
74 __stringify(TG3_MAJ_NUM) "." __stringify(TG3_MIN_NUM)
Matt Carlson98e32a92010-07-11 09:31:47 +000075#define DRV_MODULE_RELDATE "July 11, 2010"
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77#define TG3_DEF_MAC_MODE 0
78#define TG3_DEF_RX_MODE 0
79#define TG3_DEF_TX_MODE 0
80#define TG3_DEF_MSG_ENABLE \
81 (NETIF_MSG_DRV | \
82 NETIF_MSG_PROBE | \
83 NETIF_MSG_LINK | \
84 NETIF_MSG_TIMER | \
85 NETIF_MSG_IFDOWN | \
86 NETIF_MSG_IFUP | \
87 NETIF_MSG_RX_ERR | \
88 NETIF_MSG_TX_ERR)
89
90/* length of time before we decide the hardware is borked,
91 * and dev->tx_timeout() should be called to fix the problem
92 */
93#define TG3_TX_TIMEOUT (5 * HZ)
94
95/* hardware minimum and maximum for a single frame's data payload */
96#define TG3_MIN_MTU 60
97#define TG3_MAX_MTU(tp) \
Matt Carlson8f666b02009-08-28 13:58:24 +000098 ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) ? 9000 : 1500)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100/* These numbers seem to be hard coded in the NIC firmware somehow.
101 * You can't change the ring sizes, but you can change where you place
102 * them in the NIC onboard memory.
103 */
104#define TG3_RX_RING_SIZE 512
105#define TG3_DEF_RX_RING_PENDING 200
106#define TG3_RX_JUMBO_RING_SIZE 256
107#define TG3_DEF_RX_JUMBO_RING_PENDING 100
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000108#define TG3_RSS_INDIR_TBL_SIZE 128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110/* Do not place this n-ring entries value into the tp struct itself,
111 * we really want to expose these constants to GCC so that modulo et
112 * al. operations are done with shifts and masks instead of with
113 * hw multiply/modulo instructions. Another solution would be to
114 * replace things like '% foo' with '& (foo - 1)'.
115 */
116#define TG3_RX_RCB_RING_SIZE(tp) \
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000117 (((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) && \
Matt Carlson5ea1c502009-09-11 16:50:16 -0700118 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) ? 1024 : 512)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120#define TG3_TX_RING_SIZE 512
121#define TG3_DEF_TX_RING_PENDING (TG3_TX_RING_SIZE - 1)
122
123#define TG3_RX_RING_BYTES (sizeof(struct tg3_rx_buffer_desc) * \
124 TG3_RX_RING_SIZE)
Matt Carlson79ed5ac2009-08-28 14:00:55 +0000125#define TG3_RX_JUMBO_RING_BYTES (sizeof(struct tg3_ext_rx_buffer_desc) * \
126 TG3_RX_JUMBO_RING_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127#define TG3_RX_RCB_RING_BYTES(tp) (sizeof(struct tg3_rx_buffer_desc) * \
Matt Carlson79ed5ac2009-08-28 14:00:55 +0000128 TG3_RX_RCB_RING_SIZE(tp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129#define TG3_TX_RING_BYTES (sizeof(struct tg3_tx_buffer_desc) * \
130 TG3_TX_RING_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131#define NEXT_TX(N) (((N) + 1) & (TG3_TX_RING_SIZE - 1))
132
Matt Carlson9dc7a112010-04-12 06:58:28 +0000133#define TG3_RX_DMA_ALIGN 16
134#define TG3_RX_HEADROOM ALIGN(VLAN_HLEN, TG3_RX_DMA_ALIGN)
135
Matt Carlson287be122009-08-28 13:58:46 +0000136#define TG3_DMA_BYTE_ENAB 64
137
138#define TG3_RX_STD_DMA_SZ 1536
139#define TG3_RX_JMB_DMA_SZ 9046
140
141#define TG3_RX_DMA_TO_MAP_SZ(x) ((x) + TG3_DMA_BYTE_ENAB)
142
143#define TG3_RX_STD_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_STD_DMA_SZ)
144#define TG3_RX_JMB_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_JMB_DMA_SZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000146#define TG3_RX_STD_BUFF_RING_SIZE \
147 (sizeof(struct ring_info) * TG3_RX_RING_SIZE)
148
149#define TG3_RX_JMB_BUFF_RING_SIZE \
150 (sizeof(struct ring_info) * TG3_RX_JUMBO_RING_SIZE)
151
Matt Carlsond2757fc2010-04-12 06:58:27 +0000152/* Due to a hardware bug, the 5701 can only DMA to memory addresses
153 * that are at least dword aligned when used in PCIX mode. The driver
154 * works around this bug by double copying the packet. This workaround
155 * is built into the normal double copy length check for efficiency.
156 *
157 * However, the double copy is only necessary on those architectures
158 * where unaligned memory accesses are inefficient. For those architectures
159 * where unaligned memory accesses incur little penalty, we can reintegrate
160 * the 5701 in the normal rx path. Doing so saves a device structure
161 * dereference by hardcoding the double copy threshold in place.
162 */
163#define TG3_RX_COPY_THRESHOLD 256
164#if NET_IP_ALIGN == 0 || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
165 #define TG3_RX_COPY_THRESH(tp) TG3_RX_COPY_THRESHOLD
166#else
167 #define TG3_RX_COPY_THRESH(tp) ((tp)->rx_copy_thresh)
168#endif
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170/* minimum number of free TX descriptors required to wake up TX process */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000171#define TG3_TX_WAKEUP_THRESH(tnapi) ((tnapi)->tx_pending / 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Matt Carlsonad829262008-11-21 17:16:16 -0800173#define TG3_RAW_IP_ALIGN 2
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175/* number of ETHTOOL_GSTATS u64's */
176#define TG3_NUM_STATS (sizeof(struct tg3_ethtool_stats)/sizeof(u64))
177
Michael Chan4cafd3f2005-05-29 14:56:34 -0700178#define TG3_NUM_TEST 6
179
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000180#define TG3_FW_UPDATE_TIMEOUT_SEC 5
181
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800182#define FIRMWARE_TG3 "tigon/tg3.bin"
183#define FIRMWARE_TG3TSO "tigon/tg3_tso.bin"
184#define FIRMWARE_TG3TSO5 "tigon/tg3_tso5.bin"
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186static char version[] __devinitdata =
Joe Perches05dbe002010-02-17 19:44:19 +0000187 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189MODULE_AUTHOR("David S. Miller (davem@redhat.com) and Jeff Garzik (jgarzik@pobox.com)");
190MODULE_DESCRIPTION("Broadcom Tigon3 ethernet driver");
191MODULE_LICENSE("GPL");
192MODULE_VERSION(DRV_MODULE_VERSION);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800193MODULE_FIRMWARE(FIRMWARE_TG3);
194MODULE_FIRMWARE(FIRMWARE_TG3TSO);
195MODULE_FIRMWARE(FIRMWARE_TG3TSO5);
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197static int tg3_debug = -1; /* -1 == use TG3_DEF_MSG_ENABLE as value */
198module_param(tg3_debug, int, 0);
199MODULE_PARM_DESC(tg3_debug, "Tigon3 bitmapped debugging message enable value");
200
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000201static DEFINE_PCI_DEVICE_TABLE(tg3_pci_tbl) = {
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700202 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5700)},
203 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5701)},
204 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702)},
205 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703)},
206 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704)},
207 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702FE)},
208 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705)},
209 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705_2)},
210 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M)},
211 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M_2)},
212 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702X)},
213 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703X)},
214 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S)},
215 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702A3)},
216 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703A3)},
217 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5782)},
218 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5788)},
219 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5789)},
220 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901)},
221 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901_2)},
222 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S_2)},
223 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705F)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700224 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5721)},
Michael Chan126a3362006-09-27 16:03:07 -0700225 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5722)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700226 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700227 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751M)},
228 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751F)},
229 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752)},
230 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752M)},
231 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753)},
232 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753M)},
233 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753F)},
234 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754)},
235 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754M)},
236 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755)},
237 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755M)},
Michael Chan126a3362006-09-27 16:03:07 -0700238 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5756)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700239 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5786)},
240 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787)},
241 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787M)},
Michael Chan676917d2006-12-07 00:20:22 -0800242 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787F)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700243 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714)},
244 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714S)},
245 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715)},
246 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715S)},
247 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780)},
248 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780S)},
249 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5781)},
Michael Chanb5d37722006-09-27 16:06:21 -0700250 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906)},
251 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906M)},
Matt Carlsond30cdd22007-10-07 23:28:35 -0700252 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5784)},
253 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5764)},
Matt Carlson6c7af272007-10-21 16:12:02 -0700254 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5723)},
Matt Carlson9936bcf2007-10-10 18:03:07 -0700255 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761)},
256 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761E)},
Matt Carlsonc88e6682008-11-03 16:49:18 -0800257 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761S)},
258 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761SE)},
Matt Carlson2befdce2009-08-28 12:28:45 +0000259 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_G)},
260 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_F)},
Matt Carlson321d32a2008-11-21 17:22:19 -0800261 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780)},
262 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57760)},
263 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57790)},
Matt Carlson5e7ccf22009-08-25 10:08:42 +0000264 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57788)},
Matt Carlson5001e2f2009-11-13 13:03:51 +0000265 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717)},
266 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5718)},
267 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5724)},
Matt Carlsonb0f75222010-01-20 16:58:11 +0000268 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57781)},
269 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57785)},
270 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57761)},
271 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57765)},
272 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57791)},
273 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57795)},
Matt Carlson302b5002010-06-05 17:24:38 +0000274 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5719)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700275 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9DXX)},
276 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9MXX)},
277 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1000)},
278 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1001)},
279 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1003)},
280 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC9100)},
281 {PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_TIGON3)},
282 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283};
284
285MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
286
Andreas Mohr50da8592006-08-14 23:54:30 -0700287static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 const char string[ETH_GSTRING_LEN];
289} ethtool_stats_keys[TG3_NUM_STATS] = {
290 { "rx_octets" },
291 { "rx_fragments" },
292 { "rx_ucast_packets" },
293 { "rx_mcast_packets" },
294 { "rx_bcast_packets" },
295 { "rx_fcs_errors" },
296 { "rx_align_errors" },
297 { "rx_xon_pause_rcvd" },
298 { "rx_xoff_pause_rcvd" },
299 { "rx_mac_ctrl_rcvd" },
300 { "rx_xoff_entered" },
301 { "rx_frame_too_long_errors" },
302 { "rx_jabbers" },
303 { "rx_undersize_packets" },
304 { "rx_in_length_errors" },
305 { "rx_out_length_errors" },
306 { "rx_64_or_less_octet_packets" },
307 { "rx_65_to_127_octet_packets" },
308 { "rx_128_to_255_octet_packets" },
309 { "rx_256_to_511_octet_packets" },
310 { "rx_512_to_1023_octet_packets" },
311 { "rx_1024_to_1522_octet_packets" },
312 { "rx_1523_to_2047_octet_packets" },
313 { "rx_2048_to_4095_octet_packets" },
314 { "rx_4096_to_8191_octet_packets" },
315 { "rx_8192_to_9022_octet_packets" },
316
317 { "tx_octets" },
318 { "tx_collisions" },
319
320 { "tx_xon_sent" },
321 { "tx_xoff_sent" },
322 { "tx_flow_control" },
323 { "tx_mac_errors" },
324 { "tx_single_collisions" },
325 { "tx_mult_collisions" },
326 { "tx_deferred" },
327 { "tx_excessive_collisions" },
328 { "tx_late_collisions" },
329 { "tx_collide_2times" },
330 { "tx_collide_3times" },
331 { "tx_collide_4times" },
332 { "tx_collide_5times" },
333 { "tx_collide_6times" },
334 { "tx_collide_7times" },
335 { "tx_collide_8times" },
336 { "tx_collide_9times" },
337 { "tx_collide_10times" },
338 { "tx_collide_11times" },
339 { "tx_collide_12times" },
340 { "tx_collide_13times" },
341 { "tx_collide_14times" },
342 { "tx_collide_15times" },
343 { "tx_ucast_packets" },
344 { "tx_mcast_packets" },
345 { "tx_bcast_packets" },
346 { "tx_carrier_sense_errors" },
347 { "tx_discards" },
348 { "tx_errors" },
349
350 { "dma_writeq_full" },
351 { "dma_write_prioq_full" },
352 { "rxbds_empty" },
353 { "rx_discards" },
354 { "rx_errors" },
355 { "rx_threshold_hit" },
356
357 { "dma_readq_full" },
358 { "dma_read_prioq_full" },
359 { "tx_comp_queue_full" },
360
361 { "ring_set_send_prod_index" },
362 { "ring_status_update" },
363 { "nic_irqs" },
364 { "nic_avoided_irqs" },
365 { "nic_tx_threshold_hit" }
366};
367
Andreas Mohr50da8592006-08-14 23:54:30 -0700368static const struct {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700369 const char string[ETH_GSTRING_LEN];
370} ethtool_test_keys[TG3_NUM_TEST] = {
371 { "nvram test (online) " },
372 { "link test (online) " },
373 { "register test (offline)" },
374 { "memory test (offline)" },
375 { "loopback test (offline)" },
376 { "interrupt test (offline)" },
377};
378
Michael Chanb401e9e2005-12-19 16:27:04 -0800379static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
380{
381 writel(val, tp->regs + off);
382}
383
384static u32 tg3_read32(struct tg3 *tp, u32 off)
385{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000386 return readl(tp->regs + off);
Michael Chanb401e9e2005-12-19 16:27:04 -0800387}
388
Matt Carlson0d3031d2007-10-10 18:02:43 -0700389static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
390{
391 writel(val, tp->aperegs + off);
392}
393
394static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
395{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000396 return readl(tp->aperegs + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700397}
398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
400{
Michael Chan68929142005-08-09 20:17:14 -0700401 unsigned long flags;
402
403 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700404 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
405 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
Michael Chan68929142005-08-09 20:17:14 -0700406 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700407}
408
409static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
410{
411 writel(val, tp->regs + off);
412 readl(tp->regs + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413}
414
Michael Chan68929142005-08-09 20:17:14 -0700415static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
416{
417 unsigned long flags;
418 u32 val;
419
420 spin_lock_irqsave(&tp->indirect_lock, flags);
421 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
422 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
423 spin_unlock_irqrestore(&tp->indirect_lock, flags);
424 return val;
425}
426
427static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
428{
429 unsigned long flags;
430
431 if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
432 pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
433 TG3_64BIT_REG_LOW, val);
434 return;
435 }
Matt Carlson66711e62009-11-13 13:03:49 +0000436 if (off == TG3_RX_STD_PROD_IDX_REG) {
Michael Chan68929142005-08-09 20:17:14 -0700437 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
438 TG3_64BIT_REG_LOW, val);
439 return;
440 }
441
442 spin_lock_irqsave(&tp->indirect_lock, flags);
443 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
444 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
445 spin_unlock_irqrestore(&tp->indirect_lock, flags);
446
447 /* In indirect mode when disabling interrupts, we also need
448 * to clear the interrupt bit in the GRC local ctrl register.
449 */
450 if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
451 (val == 0x1)) {
452 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
453 tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
454 }
455}
456
457static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
458{
459 unsigned long flags;
460 u32 val;
461
462 spin_lock_irqsave(&tp->indirect_lock, flags);
463 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
464 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
465 spin_unlock_irqrestore(&tp->indirect_lock, flags);
466 return val;
467}
468
Michael Chanb401e9e2005-12-19 16:27:04 -0800469/* usec_wait specifies the wait time in usec when writing to certain registers
470 * where it is unsafe to read back the register without some delay.
471 * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
472 * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
473 */
474static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Michael Chanb401e9e2005-12-19 16:27:04 -0800476 if ((tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG) ||
477 (tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND))
478 /* Non-posted methods */
479 tp->write32(tp, off, val);
480 else {
481 /* Posted method */
482 tg3_write32(tp, off, val);
483 if (usec_wait)
484 udelay(usec_wait);
485 tp->read32(tp, off);
486 }
487 /* Wait again after the read for the posted method to guarantee that
488 * the wait time is met.
489 */
490 if (usec_wait)
491 udelay(usec_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492}
493
Michael Chan09ee9292005-08-09 20:17:00 -0700494static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
495{
496 tp->write32_mbox(tp, off, val);
Michael Chan68929142005-08-09 20:17:14 -0700497 if (!(tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER) &&
498 !(tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND))
499 tp->read32_mbox(tp, off);
Michael Chan09ee9292005-08-09 20:17:00 -0700500}
501
Michael Chan20094932005-08-09 20:16:32 -0700502static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
504 void __iomem *mbox = tp->regs + off;
505 writel(val, mbox);
506 if (tp->tg3_flags & TG3_FLAG_TXD_MBOX_HWBUG)
507 writel(val, mbox);
508 if (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)
509 readl(mbox);
510}
511
Michael Chanb5d37722006-09-27 16:06:21 -0700512static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
513{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000514 return readl(tp->regs + off + GRCMBOX_BASE);
Michael Chanb5d37722006-09-27 16:06:21 -0700515}
516
517static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
518{
519 writel(val, tp->regs + off + GRCMBOX_BASE);
520}
521
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000522#define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700523#define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000524#define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
525#define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
526#define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
Michael Chan20094932005-08-09 20:16:32 -0700527
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000528#define tw32(reg, val) tp->write32(tp, reg, val)
529#define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
530#define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
531#define tr32(reg) tp->read32(tp, reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
534{
Michael Chan68929142005-08-09 20:17:14 -0700535 unsigned long flags;
536
Michael Chanb5d37722006-09-27 16:06:21 -0700537 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) &&
538 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
539 return;
540
Michael Chan68929142005-08-09 20:17:14 -0700541 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chanbbadf502006-04-06 21:46:34 -0700542 if (tp->tg3_flags & TG3_FLAG_SRAM_USE_CONFIG) {
543 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
544 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Michael Chanbbadf502006-04-06 21:46:34 -0700546 /* Always leave this as zero. */
547 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
548 } else {
549 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
550 tw32_f(TG3PCI_MEM_WIN_DATA, val);
551
552 /* Always leave this as zero. */
553 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
554 }
Michael Chan68929142005-08-09 20:17:14 -0700555 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556}
557
558static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
559{
Michael Chan68929142005-08-09 20:17:14 -0700560 unsigned long flags;
561
Michael Chanb5d37722006-09-27 16:06:21 -0700562 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) &&
563 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
564 *val = 0;
565 return;
566 }
567
Michael Chan68929142005-08-09 20:17:14 -0700568 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chanbbadf502006-04-06 21:46:34 -0700569 if (tp->tg3_flags & TG3_FLAG_SRAM_USE_CONFIG) {
570 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
571 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Michael Chanbbadf502006-04-06 21:46:34 -0700573 /* Always leave this as zero. */
574 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
575 } else {
576 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
577 *val = tr32(TG3PCI_MEM_WIN_DATA);
578
579 /* Always leave this as zero. */
580 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
581 }
Michael Chan68929142005-08-09 20:17:14 -0700582 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583}
584
Matt Carlson0d3031d2007-10-10 18:02:43 -0700585static void tg3_ape_lock_init(struct tg3 *tp)
586{
587 int i;
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000588 u32 regbase;
589
590 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
591 regbase = TG3_APE_LOCK_GRANT;
592 else
593 regbase = TG3_APE_PER_LOCK_GRANT;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700594
595 /* Make sure the driver hasn't any stale locks. */
596 for (i = 0; i < 8; i++)
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000597 tg3_ape_write32(tp, regbase + 4 * i, APE_LOCK_GRANT_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700598}
599
600static int tg3_ape_lock(struct tg3 *tp, int locknum)
601{
602 int i, off;
603 int ret = 0;
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000604 u32 status, req, gnt;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700605
606 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
607 return 0;
608
609 switch (locknum) {
Matt Carlson33f401a2010-04-05 10:19:27 +0000610 case TG3_APE_LOCK_GRC:
611 case TG3_APE_LOCK_MEM:
612 break;
613 default:
614 return -EINVAL;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700615 }
616
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000617 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
618 req = TG3_APE_LOCK_REQ;
619 gnt = TG3_APE_LOCK_GRANT;
620 } else {
621 req = TG3_APE_PER_LOCK_REQ;
622 gnt = TG3_APE_PER_LOCK_GRANT;
623 }
624
Matt Carlson0d3031d2007-10-10 18:02:43 -0700625 off = 4 * locknum;
626
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000627 tg3_ape_write32(tp, req + off, APE_LOCK_REQ_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700628
629 /* Wait for up to 1 millisecond to acquire lock. */
630 for (i = 0; i < 100; i++) {
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000631 status = tg3_ape_read32(tp, gnt + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700632 if (status == APE_LOCK_GRANT_DRIVER)
633 break;
634 udelay(10);
635 }
636
637 if (status != APE_LOCK_GRANT_DRIVER) {
638 /* Revoke the lock request. */
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000639 tg3_ape_write32(tp, gnt + off,
Matt Carlson0d3031d2007-10-10 18:02:43 -0700640 APE_LOCK_GRANT_DRIVER);
641
642 ret = -EBUSY;
643 }
644
645 return ret;
646}
647
648static void tg3_ape_unlock(struct tg3 *tp, int locknum)
649{
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000650 u32 gnt;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700651
652 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
653 return;
654
655 switch (locknum) {
Matt Carlson33f401a2010-04-05 10:19:27 +0000656 case TG3_APE_LOCK_GRC:
657 case TG3_APE_LOCK_MEM:
658 break;
659 default:
660 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700661 }
662
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000663 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
664 gnt = TG3_APE_LOCK_GRANT;
665 else
666 gnt = TG3_APE_PER_LOCK_GRANT;
667
668 tg3_ape_write32(tp, gnt + 4 * locknum, APE_LOCK_GRANT_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700669}
670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671static void tg3_disable_ints(struct tg3 *tp)
672{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000673 int i;
674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 tw32(TG3PCI_MISC_HOST_CTRL,
676 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000677 for (i = 0; i < tp->irq_max; i++)
678 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679}
680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681static void tg3_enable_ints(struct tg3 *tp)
682{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000683 int i;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000684
Michael Chanbbe832c2005-06-24 20:20:04 -0700685 tp->irq_sync = 0;
686 wmb();
687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 tw32(TG3PCI_MISC_HOST_CTRL,
689 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000690
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000691 tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000692 for (i = 0; i < tp->irq_cnt; i++) {
693 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000694
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000695 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
696 if (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)
697 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
698
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000699 tp->coal_now |= tnapi->coal_now;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000700 }
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000701
702 /* Force an initial interrupt */
703 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) &&
704 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
705 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
706 else
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000707 tw32(HOSTCC_MODE, tp->coal_now);
708
709 tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710}
711
Matt Carlson17375d22009-08-28 14:02:18 +0000712static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
Michael Chan04237dd2005-04-25 15:17:17 -0700713{
Matt Carlson17375d22009-08-28 14:02:18 +0000714 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +0000715 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan04237dd2005-04-25 15:17:17 -0700716 unsigned int work_exists = 0;
717
718 /* check for phy events */
719 if (!(tp->tg3_flags &
720 (TG3_FLAG_USE_LINKCHG_REG |
721 TG3_FLAG_POLL_SERDES))) {
722 if (sblk->status & SD_STATUS_LINK_CHG)
723 work_exists = 1;
724 }
725 /* check for RX/TX work to do */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000726 if (sblk->idx[0].tx_consumer != tnapi->tx_cons ||
Matt Carlson8d9d7cf2009-09-01 13:19:05 +0000727 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -0700728 work_exists = 1;
729
730 return work_exists;
731}
732
Matt Carlson17375d22009-08-28 14:02:18 +0000733/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -0700734 * similar to tg3_enable_ints, but it accurately determines whether there
735 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400736 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 */
Matt Carlson17375d22009-08-28 14:02:18 +0000738static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
Matt Carlson17375d22009-08-28 14:02:18 +0000740 struct tg3 *tp = tnapi->tp;
741
Matt Carlson898a56f2009-08-28 14:02:40 +0000742 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 mmiowb();
744
David S. Millerfac9b832005-05-18 22:46:34 -0700745 /* When doing tagged status, this work check is unnecessary.
746 * The last_tag we write above tells the chip which piece of
747 * work we've completed.
748 */
749 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) &&
Matt Carlson17375d22009-08-28 14:02:18 +0000750 tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -0700751 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +0000752 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753}
754
Matt Carlsonfed97812009-09-01 13:10:19 +0000755static void tg3_napi_disable(struct tg3 *tp)
756{
757 int i;
758
759 for (i = tp->irq_cnt - 1; i >= 0; i--)
760 napi_disable(&tp->napi[i].napi);
761}
762
763static void tg3_napi_enable(struct tg3 *tp)
764{
765 int i;
766
767 for (i = 0; i < tp->irq_cnt; i++)
768 napi_enable(&tp->napi[i].napi);
769}
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771static inline void tg3_netif_stop(struct tg3 *tp)
772{
Michael Chanbbe832c2005-06-24 20:20:04 -0700773 tp->dev->trans_start = jiffies; /* prevent tx timeout */
Matt Carlsonfed97812009-09-01 13:10:19 +0000774 tg3_napi_disable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 netif_tx_disable(tp->dev);
776}
777
778static inline void tg3_netif_start(struct tg3 *tp)
779{
Matt Carlsonfe5f5782009-09-01 13:09:39 +0000780 /* NOTE: unconditional netif_tx_wake_all_queues is only
781 * appropriate so long as all callers are assured to
782 * have free tx slots (such as after tg3_init_hw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 */
Matt Carlsonfe5f5782009-09-01 13:09:39 +0000784 netif_tx_wake_all_queues(tp->dev);
785
Matt Carlsonfed97812009-09-01 13:10:19 +0000786 tg3_napi_enable(tp);
787 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
David S. Millerf47c11e2005-06-24 20:18:35 -0700788 tg3_enable_ints(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789}
790
791static void tg3_switch_clocks(struct tg3 *tp)
792{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000793 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 u32 orig_clock_ctrl;
795
Matt Carlson795d01c2007-10-07 23:28:17 -0700796 if ((tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
797 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -0700798 return;
799
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000800 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 orig_clock_ctrl = clock_ctrl;
803 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
804 CLOCK_CTRL_CLKRUN_OENABLE |
805 0x1f);
806 tp->pci_clock_ctrl = clock_ctrl;
807
808 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
809 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800810 tw32_wait_f(TG3PCI_CLOCK_CTRL,
811 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 }
813 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800814 tw32_wait_f(TG3PCI_CLOCK_CTRL,
815 clock_ctrl |
816 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
817 40);
818 tw32_wait_f(TG3PCI_CLOCK_CTRL,
819 clock_ctrl | (CLOCK_CTRL_ALTCLK),
820 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 }
Michael Chanb401e9e2005-12-19 16:27:04 -0800822 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823}
824
825#define PHY_BUSY_LOOPS 5000
826
827static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
828{
829 u32 frame_val;
830 unsigned int loops;
831 int ret;
832
833 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
834 tw32_f(MAC_MI_MODE,
835 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
836 udelay(80);
837 }
838
839 *val = 0x0;
840
Matt Carlson882e9792009-09-01 13:21:36 +0000841 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 MI_COM_PHY_ADDR_MASK);
843 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
844 MI_COM_REG_ADDR_MASK);
845 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 tw32_f(MAC_MI_COM, frame_val);
848
849 loops = PHY_BUSY_LOOPS;
850 while (loops != 0) {
851 udelay(10);
852 frame_val = tr32(MAC_MI_COM);
853
854 if ((frame_val & MI_COM_BUSY) == 0) {
855 udelay(5);
856 frame_val = tr32(MAC_MI_COM);
857 break;
858 }
859 loops -= 1;
860 }
861
862 ret = -EBUSY;
863 if (loops != 0) {
864 *val = frame_val & MI_COM_DATA_MASK;
865 ret = 0;
866 }
867
868 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
869 tw32_f(MAC_MI_MODE, tp->mi_mode);
870 udelay(80);
871 }
872
873 return ret;
874}
875
876static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
877{
878 u32 frame_val;
879 unsigned int loops;
880 int ret;
881
Matt Carlson7f97a4b2009-08-25 10:10:03 +0000882 if ((tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) &&
Michael Chanb5d37722006-09-27 16:06:21 -0700883 (reg == MII_TG3_CTRL || reg == MII_TG3_AUX_CTRL))
884 return 0;
885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
887 tw32_f(MAC_MI_MODE,
888 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
889 udelay(80);
890 }
891
Matt Carlson882e9792009-09-01 13:21:36 +0000892 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 MI_COM_PHY_ADDR_MASK);
894 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
895 MI_COM_REG_ADDR_MASK);
896 frame_val |= (val & MI_COM_DATA_MASK);
897 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 tw32_f(MAC_MI_COM, frame_val);
900
901 loops = PHY_BUSY_LOOPS;
902 while (loops != 0) {
903 udelay(10);
904 frame_val = tr32(MAC_MI_COM);
905 if ((frame_val & MI_COM_BUSY) == 0) {
906 udelay(5);
907 frame_val = tr32(MAC_MI_COM);
908 break;
909 }
910 loops -= 1;
911 }
912
913 ret = -EBUSY;
914 if (loops != 0)
915 ret = 0;
916
917 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
918 tw32_f(MAC_MI_MODE, tp->mi_mode);
919 udelay(80);
920 }
921
922 return ret;
923}
924
Matt Carlson95e28692008-05-25 23:44:14 -0700925static int tg3_bmcr_reset(struct tg3 *tp)
926{
927 u32 phy_control;
928 int limit, err;
929
930 /* OK, reset it, and poll the BMCR_RESET bit until it
931 * clears or we time out.
932 */
933 phy_control = BMCR_RESET;
934 err = tg3_writephy(tp, MII_BMCR, phy_control);
935 if (err != 0)
936 return -EBUSY;
937
938 limit = 5000;
939 while (limit--) {
940 err = tg3_readphy(tp, MII_BMCR, &phy_control);
941 if (err != 0)
942 return -EBUSY;
943
944 if ((phy_control & BMCR_RESET) == 0) {
945 udelay(40);
946 break;
947 }
948 udelay(10);
949 }
Roel Kluind4675b52009-02-12 16:33:27 -0800950 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -0700951 return -EBUSY;
952
953 return 0;
954}
955
Matt Carlson158d7ab2008-05-29 01:37:54 -0700956static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
957{
Francois Romieu3d165432009-01-19 16:56:50 -0800958 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700959 u32 val;
960
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000961 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -0700962
963 if (tg3_readphy(tp, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000964 val = -EIO;
965
966 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -0700967
968 return val;
969}
970
971static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
972{
Francois Romieu3d165432009-01-19 16:56:50 -0800973 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000974 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700975
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000976 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -0700977
978 if (tg3_writephy(tp, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000979 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700980
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000981 spin_unlock_bh(&tp->lock);
982
983 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700984}
985
986static int tg3_mdio_reset(struct mii_bus *bp)
987{
988 return 0;
989}
990
Matt Carlson9c61d6b2008-11-03 16:54:56 -0800991static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -0700992{
993 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800994 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -0700995
Matt Carlson3f0e3ad2009-11-02 14:24:36 +0000996 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800997 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +0000998 case PHY_ID_BCM50610:
999 case PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001000 val = MAC_PHYCFG2_50610_LED_MODES;
1001 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001002 case PHY_ID_BCMAC131:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001003 val = MAC_PHYCFG2_AC131_LED_MODES;
1004 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001005 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001006 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
1007 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001008 case PHY_ID_RTL8201E:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001009 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
1010 break;
1011 default:
Matt Carlsona9daf362008-05-25 23:49:44 -07001012 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001013 }
1014
1015 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
1016 tw32(MAC_PHYCFG2, val);
1017
1018 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001019 val &= ~(MAC_PHYCFG1_RGMII_INT |
1020 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
1021 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001022 tw32(MAC_PHYCFG1, val);
1023
1024 return;
1025 }
1026
Matt Carlson14417062010-02-17 15:16:59 +00001027 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE))
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001028 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
1029 MAC_PHYCFG2_FMODE_MASK_MASK |
1030 MAC_PHYCFG2_GMODE_MASK_MASK |
1031 MAC_PHYCFG2_ACT_MASK_MASK |
1032 MAC_PHYCFG2_QUAL_MASK_MASK |
1033 MAC_PHYCFG2_INBAND_ENABLE;
1034
1035 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001036
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001037 val = tr32(MAC_PHYCFG1);
1038 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
1039 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
Matt Carlson14417062010-02-17 15:16:59 +00001040 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE)) {
Matt Carlsona9daf362008-05-25 23:49:44 -07001041 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
1042 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
1043 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
1044 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1045 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001046 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1047 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1048 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001049
Matt Carlsona9daf362008-05-25 23:49:44 -07001050 val = tr32(MAC_EXT_RGMII_MODE);
1051 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1052 MAC_RGMII_MODE_RX_QUALITY |
1053 MAC_RGMII_MODE_RX_ACTIVITY |
1054 MAC_RGMII_MODE_RX_ENG_DET |
1055 MAC_RGMII_MODE_TX_ENABLE |
1056 MAC_RGMII_MODE_TX_LOWPWR |
1057 MAC_RGMII_MODE_TX_RESET);
Matt Carlson14417062010-02-17 15:16:59 +00001058 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE)) {
Matt Carlsona9daf362008-05-25 23:49:44 -07001059 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
1060 val |= MAC_RGMII_MODE_RX_INT_B |
1061 MAC_RGMII_MODE_RX_QUALITY |
1062 MAC_RGMII_MODE_RX_ACTIVITY |
1063 MAC_RGMII_MODE_RX_ENG_DET;
1064 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
1065 val |= MAC_RGMII_MODE_TX_ENABLE |
1066 MAC_RGMII_MODE_TX_LOWPWR |
1067 MAC_RGMII_MODE_TX_RESET;
1068 }
1069 tw32(MAC_EXT_RGMII_MODE, val);
1070}
1071
Matt Carlson158d7ab2008-05-29 01:37:54 -07001072static void tg3_mdio_start(struct tg3 *tp)
1073{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001074 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1075 tw32_f(MAC_MI_MODE, tp->mi_mode);
1076 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001077
Matt Carlson9ea48182010-02-17 15:17:01 +00001078 if ((tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED) &&
1079 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1080 tg3_mdio_config_5785(tp);
1081}
1082
1083static int tg3_mdio_init(struct tg3 *tp)
1084{
1085 int i;
1086 u32 reg;
1087 struct phy_device *phydev;
1088
Matt Carlsona50d0792010-06-05 17:24:37 +00001089 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
1090 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) {
Matt Carlson9c7df912010-06-05 17:24:36 +00001091 u32 is_serdes;
Matt Carlson882e9792009-09-01 13:21:36 +00001092
Matt Carlson9c7df912010-06-05 17:24:36 +00001093 tp->phy_addr = PCI_FUNC(tp->pdev->devfn) + 1;
Matt Carlson882e9792009-09-01 13:21:36 +00001094
Matt Carlsond1ec96a2010-01-12 10:11:38 +00001095 if (tp->pci_chip_rev_id != CHIPREV_ID_5717_A0)
1096 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1097 else
1098 is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1099 TG3_CPMU_PHY_STRAP_IS_SERDES;
Matt Carlson882e9792009-09-01 13:21:36 +00001100 if (is_serdes)
1101 tp->phy_addr += 7;
1102 } else
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001103 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001104
Matt Carlson158d7ab2008-05-29 01:37:54 -07001105 tg3_mdio_start(tp);
1106
1107 if (!(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) ||
1108 (tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED))
1109 return 0;
1110
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001111 tp->mdio_bus = mdiobus_alloc();
1112 if (tp->mdio_bus == NULL)
1113 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001114
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001115 tp->mdio_bus->name = "tg3 mdio bus";
1116 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001117 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001118 tp->mdio_bus->priv = tp;
1119 tp->mdio_bus->parent = &tp->pdev->dev;
1120 tp->mdio_bus->read = &tg3_mdio_read;
1121 tp->mdio_bus->write = &tg3_mdio_write;
1122 tp->mdio_bus->reset = &tg3_mdio_reset;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001123 tp->mdio_bus->phy_mask = ~(1 << TG3_PHY_MII_ADDR);
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001124 tp->mdio_bus->irq = &tp->mdio_irq[0];
Matt Carlson158d7ab2008-05-29 01:37:54 -07001125
1126 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001127 tp->mdio_bus->irq[i] = PHY_POLL;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001128
1129 /* The bus registration will look for all the PHYs on the mdio bus.
1130 * Unfortunately, it does not ensure the PHY is powered up before
1131 * accessing the PHY ID registers. A chip reset is the
1132 * quickest way to bring the device back to an operational state..
1133 */
1134 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1135 tg3_bmcr_reset(tp);
1136
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001137 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001138 if (i) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001139 dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001140 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001141 return i;
1142 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001143
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001144 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsona9daf362008-05-25 23:49:44 -07001145
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001146 if (!phydev || !phydev->drv) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001147 dev_warn(&tp->pdev->dev, "No PHY devices\n");
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001148 mdiobus_unregister(tp->mdio_bus);
1149 mdiobus_free(tp->mdio_bus);
1150 return -ENODEV;
1151 }
1152
1153 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001154 case PHY_ID_BCM57780:
Matt Carlson321d32a2008-11-21 17:22:19 -08001155 phydev->interface = PHY_INTERFACE_MODE_GMII;
Matt Carlsonc704dc22009-11-02 14:32:12 +00001156 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08001157 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001158 case PHY_ID_BCM50610:
1159 case PHY_ID_BCM50610M:
Matt Carlson32e5a8d2009-11-02 14:31:39 +00001160 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001161 PHY_BRCM_RX_REFCLK_UNUSED |
Matt Carlson52fae082009-11-02 14:32:38 +00001162 PHY_BRCM_DIS_TXCRXC_NOENRGY |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001163 PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson14417062010-02-17 15:16:59 +00001164 if (tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE)
Matt Carlsona9daf362008-05-25 23:49:44 -07001165 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
1166 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
1167 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
1168 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
1169 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001170 /* fallthru */
Matt Carlson6a443a02010-02-17 15:17:04 +00001171 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001172 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001173 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001174 case PHY_ID_RTL8201E:
1175 case PHY_ID_BCMAC131:
Matt Carlsona9daf362008-05-25 23:49:44 -07001176 phydev->interface = PHY_INTERFACE_MODE_MII;
Matt Carlsoncdd4e09d2009-11-02 14:31:11 +00001177 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001178 tp->tg3_flags3 |= TG3_FLG3_PHY_IS_FET;
Matt Carlsona9daf362008-05-25 23:49:44 -07001179 break;
1180 }
1181
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001182 tp->tg3_flags3 |= TG3_FLG3_MDIOBUS_INITED;
1183
1184 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1185 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001186
1187 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001188}
1189
1190static void tg3_mdio_fini(struct tg3 *tp)
1191{
1192 if (tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED) {
1193 tp->tg3_flags3 &= ~TG3_FLG3_MDIOBUS_INITED;
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001194 mdiobus_unregister(tp->mdio_bus);
1195 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001196 }
1197}
1198
Matt Carlson95e28692008-05-25 23:44:14 -07001199/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001200static inline void tg3_generate_fw_event(struct tg3 *tp)
1201{
1202 u32 val;
1203
1204 val = tr32(GRC_RX_CPU_EVENT);
1205 val |= GRC_RX_CPU_DRIVER_EVENT;
1206 tw32_f(GRC_RX_CPU_EVENT, val);
1207
1208 tp->last_event_jiffies = jiffies;
1209}
1210
1211#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1212
1213/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001214static void tg3_wait_for_event_ack(struct tg3 *tp)
1215{
1216 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001217 unsigned int delay_cnt;
1218 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001219
Matt Carlson4ba526c2008-08-15 14:10:04 -07001220 /* If enough time has passed, no wait is necessary. */
1221 time_remain = (long)(tp->last_event_jiffies + 1 +
1222 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1223 (long)jiffies;
1224 if (time_remain < 0)
1225 return;
1226
1227 /* Check if we can shorten the wait time. */
1228 delay_cnt = jiffies_to_usecs(time_remain);
1229 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1230 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1231 delay_cnt = (delay_cnt >> 3) + 1;
1232
1233 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001234 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1235 break;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001236 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001237 }
1238}
1239
1240/* tp->lock is held. */
1241static void tg3_ump_link_report(struct tg3 *tp)
1242{
1243 u32 reg;
1244 u32 val;
1245
1246 if (!(tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
1247 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
1248 return;
1249
1250 tg3_wait_for_event_ack(tp);
1251
1252 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1253
1254 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1255
1256 val = 0;
1257 if (!tg3_readphy(tp, MII_BMCR, &reg))
1258 val = reg << 16;
1259 if (!tg3_readphy(tp, MII_BMSR, &reg))
1260 val |= (reg & 0xffff);
1261 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX, val);
1262
1263 val = 0;
1264 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1265 val = reg << 16;
1266 if (!tg3_readphy(tp, MII_LPA, &reg))
1267 val |= (reg & 0xffff);
1268 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 4, val);
1269
1270 val = 0;
1271 if (!(tp->tg3_flags2 & TG3_FLG2_MII_SERDES)) {
1272 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1273 val = reg << 16;
1274 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1275 val |= (reg & 0xffff);
1276 }
1277 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 8, val);
1278
1279 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1280 val = reg << 16;
1281 else
1282 val = 0;
1283 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 12, val);
1284
Matt Carlson4ba526c2008-08-15 14:10:04 -07001285 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001286}
1287
1288static void tg3_link_report(struct tg3 *tp)
1289{
1290 if (!netif_carrier_ok(tp->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001291 netif_info(tp, link, tp->dev, "Link is down\n");
Matt Carlson95e28692008-05-25 23:44:14 -07001292 tg3_ump_link_report(tp);
1293 } else if (netif_msg_link(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001294 netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
1295 (tp->link_config.active_speed == SPEED_1000 ?
1296 1000 :
1297 (tp->link_config.active_speed == SPEED_100 ?
1298 100 : 10)),
1299 (tp->link_config.active_duplex == DUPLEX_FULL ?
1300 "full" : "half"));
Matt Carlson95e28692008-05-25 23:44:14 -07001301
Joe Perches05dbe002010-02-17 19:44:19 +00001302 netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
1303 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
1304 "on" : "off",
1305 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
1306 "on" : "off");
Matt Carlson95e28692008-05-25 23:44:14 -07001307 tg3_ump_link_report(tp);
1308 }
1309}
1310
1311static u16 tg3_advert_flowctrl_1000T(u8 flow_ctrl)
1312{
1313 u16 miireg;
1314
Steve Glendinninge18ce342008-12-16 02:00:00 -08001315 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001316 miireg = ADVERTISE_PAUSE_CAP;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001317 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001318 miireg = ADVERTISE_PAUSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001319 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001320 miireg = ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1321 else
1322 miireg = 0;
1323
1324 return miireg;
1325}
1326
1327static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1328{
1329 u16 miireg;
1330
Steve Glendinninge18ce342008-12-16 02:00:00 -08001331 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001332 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001333 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001334 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001335 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001336 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1337 else
1338 miireg = 0;
1339
1340 return miireg;
1341}
1342
Matt Carlson95e28692008-05-25 23:44:14 -07001343static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1344{
1345 u8 cap = 0;
1346
1347 if (lcladv & ADVERTISE_1000XPAUSE) {
1348 if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1349 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001350 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001351 else if (rmtadv & LPA_1000XPAUSE_ASYM)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001352 cap = FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001353 } else {
1354 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001355 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001356 }
1357 } else if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1358 if ((rmtadv & LPA_1000XPAUSE) && (rmtadv & LPA_1000XPAUSE_ASYM))
Steve Glendinninge18ce342008-12-16 02:00:00 -08001359 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001360 }
1361
1362 return cap;
1363}
1364
Matt Carlsonf51f3562008-05-25 23:45:08 -07001365static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001366{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001367 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001368 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001369 u32 old_rx_mode = tp->rx_mode;
1370 u32 old_tx_mode = tp->tx_mode;
1371
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001372 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001373 autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001374 else
1375 autoneg = tp->link_config.autoneg;
1376
1377 if (autoneg == AUTONEG_ENABLE &&
Matt Carlson95e28692008-05-25 23:44:14 -07001378 (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG)) {
1379 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001380 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001381 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001382 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001383 } else
1384 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001385
Matt Carlsonf51f3562008-05-25 23:45:08 -07001386 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001387
Steve Glendinninge18ce342008-12-16 02:00:00 -08001388 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001389 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1390 else
1391 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1392
Matt Carlsonf51f3562008-05-25 23:45:08 -07001393 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001394 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001395
Steve Glendinninge18ce342008-12-16 02:00:00 -08001396 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001397 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1398 else
1399 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1400
Matt Carlsonf51f3562008-05-25 23:45:08 -07001401 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001402 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001403}
1404
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001405static void tg3_adjust_link(struct net_device *dev)
1406{
1407 u8 oldflowctrl, linkmesg = 0;
1408 u32 mac_mode, lcl_adv, rmt_adv;
1409 struct tg3 *tp = netdev_priv(dev);
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001410 struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001411
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001412 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001413
1414 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
1415 MAC_MODE_HALF_DUPLEX);
1416
1417 oldflowctrl = tp->link_config.active_flowctrl;
1418
1419 if (phydev->link) {
1420 lcl_adv = 0;
1421 rmt_adv = 0;
1422
1423 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
1424 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001425 else if (phydev->speed == SPEED_1000 ||
1426 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001427 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001428 else
1429 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001430
1431 if (phydev->duplex == DUPLEX_HALF)
1432 mac_mode |= MAC_MODE_HALF_DUPLEX;
1433 else {
1434 lcl_adv = tg3_advert_flowctrl_1000T(
1435 tp->link_config.flowctrl);
1436
1437 if (phydev->pause)
1438 rmt_adv = LPA_PAUSE_CAP;
1439 if (phydev->asym_pause)
1440 rmt_adv |= LPA_PAUSE_ASYM;
1441 }
1442
1443 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
1444 } else
1445 mac_mode |= MAC_MODE_PORT_MODE_GMII;
1446
1447 if (mac_mode != tp->mac_mode) {
1448 tp->mac_mode = mac_mode;
1449 tw32_f(MAC_MODE, tp->mac_mode);
1450 udelay(40);
1451 }
1452
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001453 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
1454 if (phydev->speed == SPEED_10)
1455 tw32(MAC_MI_STAT,
1456 MAC_MI_STAT_10MBPS_MODE |
1457 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1458 else
1459 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1460 }
1461
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001462 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
1463 tw32(MAC_TX_LENGTHS,
1464 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1465 (6 << TX_LENGTHS_IPG_SHIFT) |
1466 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
1467 else
1468 tw32(MAC_TX_LENGTHS,
1469 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1470 (6 << TX_LENGTHS_IPG_SHIFT) |
1471 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
1472
1473 if ((phydev->link && tp->link_config.active_speed == SPEED_INVALID) ||
1474 (!phydev->link && tp->link_config.active_speed != SPEED_INVALID) ||
1475 phydev->speed != tp->link_config.active_speed ||
1476 phydev->duplex != tp->link_config.active_duplex ||
1477 oldflowctrl != tp->link_config.active_flowctrl)
Matt Carlsonc6cdf432010-04-05 10:19:26 +00001478 linkmesg = 1;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001479
1480 tp->link_config.active_speed = phydev->speed;
1481 tp->link_config.active_duplex = phydev->duplex;
1482
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001483 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001484
1485 if (linkmesg)
1486 tg3_link_report(tp);
1487}
1488
1489static int tg3_phy_init(struct tg3 *tp)
1490{
1491 struct phy_device *phydev;
1492
1493 if (tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED)
1494 return 0;
1495
1496 /* Bring the PHY back to a known state. */
1497 tg3_bmcr_reset(tp);
1498
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001499 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001500
1501 /* Attach the MAC to the PHY. */
Kay Sieversfb28ad32008-11-10 13:55:14 -08001502 phydev = phy_connect(tp->dev, dev_name(&phydev->dev), tg3_adjust_link,
Matt Carlsona9daf362008-05-25 23:49:44 -07001503 phydev->dev_flags, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001504 if (IS_ERR(phydev)) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001505 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001506 return PTR_ERR(phydev);
1507 }
1508
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001509 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001510 switch (phydev->interface) {
1511 case PHY_INTERFACE_MODE_GMII:
1512 case PHY_INTERFACE_MODE_RGMII:
Matt Carlson321d32a2008-11-21 17:22:19 -08001513 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY)) {
1514 phydev->supported &= (PHY_GBIT_FEATURES |
1515 SUPPORTED_Pause |
1516 SUPPORTED_Asym_Pause);
1517 break;
1518 }
1519 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001520 case PHY_INTERFACE_MODE_MII:
1521 phydev->supported &= (PHY_BASIC_FEATURES |
1522 SUPPORTED_Pause |
1523 SUPPORTED_Asym_Pause);
1524 break;
1525 default:
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001526 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001527 return -EINVAL;
1528 }
1529
1530 tp->tg3_flags3 |= TG3_FLG3_PHY_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001531
1532 phydev->advertising = phydev->supported;
1533
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001534 return 0;
1535}
1536
1537static void tg3_phy_start(struct tg3 *tp)
1538{
1539 struct phy_device *phydev;
1540
1541 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
1542 return;
1543
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001544 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001545
1546 if (tp->link_config.phy_is_low_power) {
1547 tp->link_config.phy_is_low_power = 0;
1548 phydev->speed = tp->link_config.orig_speed;
1549 phydev->duplex = tp->link_config.orig_duplex;
1550 phydev->autoneg = tp->link_config.orig_autoneg;
1551 phydev->advertising = tp->link_config.orig_advertising;
1552 }
1553
1554 phy_start(phydev);
1555
1556 phy_start_aneg(phydev);
1557}
1558
1559static void tg3_phy_stop(struct tg3 *tp)
1560{
1561 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
1562 return;
1563
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001564 phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001565}
1566
1567static void tg3_phy_fini(struct tg3 *tp)
1568{
1569 if (tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001570 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001571 tp->tg3_flags3 &= ~TG3_FLG3_PHY_CONNECTED;
1572 }
1573}
1574
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001575static void tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
1576{
1577 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1578 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
1579}
1580
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001581static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
1582{
1583 u32 phytest;
1584
1585 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
1586 u32 phy;
1587
1588 tg3_writephy(tp, MII_TG3_FET_TEST,
1589 phytest | MII_TG3_FET_SHADOW_EN);
1590 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
1591 if (enable)
1592 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
1593 else
1594 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
1595 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
1596 }
1597 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
1598 }
1599}
1600
Matt Carlson6833c042008-11-21 17:18:59 -08001601static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
1602{
1603 u32 reg;
1604
Matt Carlsonecf14102010-01-20 16:58:05 +00001605 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
Matt Carlsona50d0792010-06-05 17:24:37 +00001606 ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
1607 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) &&
Matt Carlsonecf14102010-01-20 16:58:05 +00001608 (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08001609 return;
1610
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001611 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
1612 tg3_phy_fet_toggle_apd(tp, enable);
1613 return;
1614 }
1615
Matt Carlson6833c042008-11-21 17:18:59 -08001616 reg = MII_TG3_MISC_SHDW_WREN |
1617 MII_TG3_MISC_SHDW_SCR5_SEL |
1618 MII_TG3_MISC_SHDW_SCR5_LPED |
1619 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
1620 MII_TG3_MISC_SHDW_SCR5_SDTL |
1621 MII_TG3_MISC_SHDW_SCR5_C125OE;
1622 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 || !enable)
1623 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
1624
1625 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1626
1627
1628 reg = MII_TG3_MISC_SHDW_WREN |
1629 MII_TG3_MISC_SHDW_APD_SEL |
1630 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
1631 if (enable)
1632 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
1633
1634 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1635}
1636
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001637static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
1638{
1639 u32 phy;
1640
1641 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
1642 (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES))
1643 return;
1644
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001645 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001646 u32 ephy;
1647
Matt Carlson535ef6e2009-08-25 10:09:36 +00001648 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
1649 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
1650
1651 tg3_writephy(tp, MII_TG3_FET_TEST,
1652 ephy | MII_TG3_FET_SHADOW_EN);
1653 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001654 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00001655 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001656 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00001657 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
1658 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001659 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00001660 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001661 }
1662 } else {
1663 phy = MII_TG3_AUXCTL_MISC_RDSEL_MISC |
1664 MII_TG3_AUXCTL_SHDWSEL_MISC;
1665 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, phy) &&
1666 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &phy)) {
1667 if (enable)
1668 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1669 else
1670 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1671 phy |= MII_TG3_AUXCTL_MISC_WREN;
1672 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1673 }
1674 }
1675}
1676
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677static void tg3_phy_set_wirespeed(struct tg3 *tp)
1678{
1679 u32 val;
1680
1681 if (tp->tg3_flags2 & TG3_FLG2_NO_ETH_WIRE_SPEED)
1682 return;
1683
1684 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x7007) &&
1685 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &val))
1686 tg3_writephy(tp, MII_TG3_AUX_CTRL,
1687 (val | (1 << 15) | (1 << 4)));
1688}
1689
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001690static void tg3_phy_apply_otp(struct tg3 *tp)
1691{
1692 u32 otp, phy;
1693
1694 if (!tp->phy_otp)
1695 return;
1696
1697 otp = tp->phy_otp;
1698
1699 /* Enable SM_DSP clock and tx 6dB coding. */
1700 phy = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1701 MII_TG3_AUXCTL_ACTL_SMDSP_ENA |
1702 MII_TG3_AUXCTL_ACTL_TX_6DB;
1703 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1704
1705 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
1706 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
1707 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
1708
1709 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
1710 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
1711 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
1712
1713 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
1714 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
1715 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
1716
1717 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
1718 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
1719
1720 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
1721 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
1722
1723 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
1724 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
1725 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
1726
1727 /* Turn off SM_DSP clock. */
1728 phy = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1729 MII_TG3_AUXCTL_ACTL_TX_6DB;
1730 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1731}
1732
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733static int tg3_wait_macro_done(struct tg3 *tp)
1734{
1735 int limit = 100;
1736
1737 while (limit--) {
1738 u32 tmp32;
1739
1740 if (!tg3_readphy(tp, 0x16, &tmp32)) {
1741 if ((tmp32 & 0x1000) == 0)
1742 break;
1743 }
1744 }
Roel Kluind4675b52009-02-12 16:33:27 -08001745 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 return -EBUSY;
1747
1748 return 0;
1749}
1750
1751static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
1752{
1753 static const u32 test_pat[4][6] = {
1754 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
1755 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
1756 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
1757 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
1758 };
1759 int chan;
1760
1761 for (chan = 0; chan < 4; chan++) {
1762 int i;
1763
1764 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1765 (chan * 0x2000) | 0x0200);
1766 tg3_writephy(tp, 0x16, 0x0002);
1767
1768 for (i = 0; i < 6; i++)
1769 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
1770 test_pat[chan][i]);
1771
1772 tg3_writephy(tp, 0x16, 0x0202);
1773 if (tg3_wait_macro_done(tp)) {
1774 *resetp = 1;
1775 return -EBUSY;
1776 }
1777
1778 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1779 (chan * 0x2000) | 0x0200);
1780 tg3_writephy(tp, 0x16, 0x0082);
1781 if (tg3_wait_macro_done(tp)) {
1782 *resetp = 1;
1783 return -EBUSY;
1784 }
1785
1786 tg3_writephy(tp, 0x16, 0x0802);
1787 if (tg3_wait_macro_done(tp)) {
1788 *resetp = 1;
1789 return -EBUSY;
1790 }
1791
1792 for (i = 0; i < 6; i += 2) {
1793 u32 low, high;
1794
1795 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
1796 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
1797 tg3_wait_macro_done(tp)) {
1798 *resetp = 1;
1799 return -EBUSY;
1800 }
1801 low &= 0x7fff;
1802 high &= 0x000f;
1803 if (low != test_pat[chan][i] ||
1804 high != test_pat[chan][i+1]) {
1805 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
1806 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
1807 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
1808
1809 return -EBUSY;
1810 }
1811 }
1812 }
1813
1814 return 0;
1815}
1816
1817static int tg3_phy_reset_chanpat(struct tg3 *tp)
1818{
1819 int chan;
1820
1821 for (chan = 0; chan < 4; chan++) {
1822 int i;
1823
1824 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1825 (chan * 0x2000) | 0x0200);
1826 tg3_writephy(tp, 0x16, 0x0002);
1827 for (i = 0; i < 6; i++)
1828 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
1829 tg3_writephy(tp, 0x16, 0x0202);
1830 if (tg3_wait_macro_done(tp))
1831 return -EBUSY;
1832 }
1833
1834 return 0;
1835}
1836
1837static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
1838{
1839 u32 reg32, phy9_orig;
1840 int retries, do_phy_reset, err;
1841
1842 retries = 10;
1843 do_phy_reset = 1;
1844 do {
1845 if (do_phy_reset) {
1846 err = tg3_bmcr_reset(tp);
1847 if (err)
1848 return err;
1849 do_phy_reset = 0;
1850 }
1851
1852 /* Disable transmitter and interrupt. */
1853 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
1854 continue;
1855
1856 reg32 |= 0x3000;
1857 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
1858
1859 /* Set full-duplex, 1000 mbps. */
1860 tg3_writephy(tp, MII_BMCR,
1861 BMCR_FULLDPLX | TG3_BMCR_SPEED1000);
1862
1863 /* Set to master mode. */
1864 if (tg3_readphy(tp, MII_TG3_CTRL, &phy9_orig))
1865 continue;
1866
1867 tg3_writephy(tp, MII_TG3_CTRL,
1868 (MII_TG3_CTRL_AS_MASTER |
1869 MII_TG3_CTRL_ENABLE_AS_MASTER));
1870
1871 /* Enable SM_DSP_CLOCK and 6dB. */
1872 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
1873
1874 /* Block the PHY control access. */
1875 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8005);
1876 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0800);
1877
1878 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
1879 if (!err)
1880 break;
1881 } while (--retries);
1882
1883 err = tg3_phy_reset_chanpat(tp);
1884 if (err)
1885 return err;
1886
1887 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8005);
1888 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0000);
1889
1890 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
1891 tg3_writephy(tp, 0x16, 0x0000);
1892
1893 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
1894 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
1895 /* Set Extended packet length bit for jumbo frames */
1896 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4400);
Matt Carlson859a588792010-04-05 10:19:28 +00001897 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
1899 }
1900
1901 tg3_writephy(tp, MII_TG3_CTRL, phy9_orig);
1902
1903 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
1904 reg32 &= ~0x3000;
1905 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
1906 } else if (!err)
1907 err = -EBUSY;
1908
1909 return err;
1910}
1911
1912/* This will reset the tigon3 PHY if there is no valid
1913 * link unless the FORCE argument is non-zero.
1914 */
1915static int tg3_phy_reset(struct tg3 *tp)
1916{
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001917 u32 cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 u32 phy_status;
1919 int err;
1920
Michael Chan60189dd2006-12-17 17:08:07 -08001921 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
1922 u32 val;
1923
1924 val = tr32(GRC_MISC_CFG);
1925 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
1926 udelay(40);
1927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 err = tg3_readphy(tp, MII_BMSR, &phy_status);
1929 err |= tg3_readphy(tp, MII_BMSR, &phy_status);
1930 if (err != 0)
1931 return -EBUSY;
1932
Michael Chanc8e1e822006-04-29 18:55:17 -07001933 if (netif_running(tp->dev) && netif_carrier_ok(tp->dev)) {
1934 netif_carrier_off(tp->dev);
1935 tg3_link_report(tp);
1936 }
1937
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
1939 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
1940 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
1941 err = tg3_phy_reset_5703_4_5(tp);
1942 if (err)
1943 return err;
1944 goto out;
1945 }
1946
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001947 cpmuctrl = 0;
1948 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
1949 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
1950 cpmuctrl = tr32(TG3_CPMU_CTRL);
1951 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
1952 tw32(TG3_CPMU_CTRL,
1953 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
1954 }
1955
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 err = tg3_bmcr_reset(tp);
1957 if (err)
1958 return err;
1959
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001960 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
1961 u32 phy;
1962
1963 phy = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
1964 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, phy);
1965
1966 tw32(TG3_CPMU_CTRL, cpmuctrl);
1967 }
1968
Matt Carlsonbcb37f62008-11-03 16:52:09 -08001969 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
1970 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08001971 u32 val;
1972
1973 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
1974 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
1975 CPMU_LSPD_1000MB_MACCLK_12_5) {
1976 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
1977 udelay(40);
1978 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
1979 }
1980 }
1981
Matt Carlsona50d0792010-06-05 17:24:37 +00001982 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
1983 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) &&
Matt Carlsonecf14102010-01-20 16:58:05 +00001984 (tp->tg3_flags2 & TG3_FLG2_MII_SERDES))
1985 return 0;
1986
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001987 tg3_phy_apply_otp(tp);
1988
Matt Carlson6833c042008-11-21 17:18:59 -08001989 if (tp->tg3_flags3 & TG3_FLG3_PHY_ENABLE_APD)
1990 tg3_phy_toggle_apd(tp, true);
1991 else
1992 tg3_phy_toggle_apd(tp, false);
1993
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994out:
1995 if (tp->tg3_flags2 & TG3_FLG2_PHY_ADC_BUG) {
1996 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
1997 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x201f);
1998 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x2aaa);
1999 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
2000 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0323);
2001 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
2002 }
2003 if (tp->tg3_flags2 & TG3_FLG2_PHY_5704_A0_BUG) {
2004 tg3_writephy(tp, 0x1c, 0x8d68);
2005 tg3_writephy(tp, 0x1c, 0x8d68);
2006 }
2007 if (tp->tg3_flags2 & TG3_FLG2_PHY_BER_BUG) {
2008 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
2009 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
2010 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x310b);
2011 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x201f);
2012 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x9506);
2013 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x401f);
2014 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x14e2);
2015 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
Matt Carlson859a588792010-04-05 10:19:28 +00002016 } else if (tp->tg3_flags2 & TG3_FLG2_PHY_JITTER_BUG) {
Michael Chanc424cb22006-04-29 18:56:34 -07002017 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
2018 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
Michael Chanc1d2a192007-01-08 19:57:20 -08002019 if (tp->tg3_flags2 & TG3_FLG2_PHY_ADJUST_TRIM) {
2020 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2021 tg3_writephy(tp, MII_TG3_TEST1,
2022 MII_TG3_TEST1_TRIM_EN | 0x4);
2023 } else
2024 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
Michael Chanc424cb22006-04-29 18:56:34 -07002025 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
2026 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 /* Set Extended packet length bit (bit 14) on all chips that */
2028 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002029 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 /* Cannot do read-modify-write on 5401 */
2031 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4c20);
Matt Carlson8f666b02009-08-28 13:58:24 +00002032 } else if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 u32 phy_reg;
2034
2035 /* Set bit 14 with read-modify-write to preserve other bits */
2036 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0007) &&
2037 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &phy_reg))
2038 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy_reg | 0x4000);
2039 }
2040
2041 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2042 * jumbo frames transmission.
2043 */
Matt Carlson8f666b02009-08-28 13:58:24 +00002044 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 u32 phy_reg;
2046
2047 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &phy_reg))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002048 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2049 phy_reg | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 }
2051
Michael Chan715116a2006-09-27 16:09:25 -07002052 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002053 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002054 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002055 }
2056
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002057 tg3_phy_toggle_automdix(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 tg3_phy_set_wirespeed(tp);
2059 return 0;
2060}
2061
2062static void tg3_frob_aux_power(struct tg3 *tp)
2063{
2064 struct tg3 *tp_peer = tp;
2065
Matt Carlson334355a2010-01-20 16:58:10 +00002066 /* The GPIOs do something completely different on 57765. */
2067 if ((tp->tg3_flags2 & TG3_FLG2_IS_NIC) == 0 ||
Matt Carlsona50d0792010-06-05 17:24:37 +00002068 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlson334355a2010-01-20 16:58:10 +00002069 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 return;
2071
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00002072 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2073 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
2074 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002075 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002077 dev_peer = pci_get_drvdata(tp->pdev_peer);
Michael Chanbc1c7562006-03-20 17:48:03 -08002078 /* remove_one() may have been run on the peer. */
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002079 if (!dev_peer)
Michael Chanbc1c7562006-03-20 17:48:03 -08002080 tp_peer = tp;
2081 else
2082 tp_peer = netdev_priv(dev_peer);
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
2085 if ((tp->tg3_flags & TG3_FLAG_WOL_ENABLE) != 0 ||
Michael Chan6921d202005-12-13 21:15:53 -08002086 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0 ||
2087 (tp_peer->tg3_flags & TG3_FLAG_WOL_ENABLE) != 0 ||
2088 (tp_peer->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2090 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Michael Chanb401e9e2005-12-19 16:27:04 -08002091 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2092 (GRC_LCLCTRL_GPIO_OE0 |
2093 GRC_LCLCTRL_GPIO_OE1 |
2094 GRC_LCLCTRL_GPIO_OE2 |
2095 GRC_LCLCTRL_GPIO_OUTPUT0 |
2096 GRC_LCLCTRL_GPIO_OUTPUT1),
2097 100);
Matt Carlson8d519ab2009-04-20 06:58:01 +00002098 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2099 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -07002100 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2101 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2102 GRC_LCLCTRL_GPIO_OE1 |
2103 GRC_LCLCTRL_GPIO_OE2 |
2104 GRC_LCLCTRL_GPIO_OUTPUT0 |
2105 GRC_LCLCTRL_GPIO_OUTPUT1 |
2106 tp->grc_local_ctrl;
2107 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2108
2109 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2110 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2111
2112 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2113 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 } else {
2115 u32 no_gpio2;
Michael Chandc56b7d2005-12-19 16:26:28 -08002116 u32 grc_local_ctrl = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
2118 if (tp_peer != tp &&
2119 (tp_peer->tg3_flags & TG3_FLAG_INIT_COMPLETE) != 0)
2120 return;
2121
Michael Chandc56b7d2005-12-19 16:26:28 -08002122 /* Workaround to prevent overdrawing Amps. */
2123 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2124 ASIC_REV_5714) {
2125 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chanb401e9e2005-12-19 16:27:04 -08002126 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2127 grc_local_ctrl, 100);
Michael Chandc56b7d2005-12-19 16:26:28 -08002128 }
2129
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 /* On 5753 and variants, GPIO2 cannot be used. */
2131 no_gpio2 = tp->nic_sram_data_cfg &
2132 NIC_SRAM_DATA_CFG_NO_GPIO2;
2133
Michael Chandc56b7d2005-12-19 16:26:28 -08002134 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 GRC_LCLCTRL_GPIO_OE1 |
2136 GRC_LCLCTRL_GPIO_OE2 |
2137 GRC_LCLCTRL_GPIO_OUTPUT1 |
2138 GRC_LCLCTRL_GPIO_OUTPUT2;
2139 if (no_gpio2) {
2140 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2141 GRC_LCLCTRL_GPIO_OUTPUT2);
2142 }
Michael Chanb401e9e2005-12-19 16:27:04 -08002143 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2144 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
2146 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2147
Michael Chanb401e9e2005-12-19 16:27:04 -08002148 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2149 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
2151 if (!no_gpio2) {
2152 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chanb401e9e2005-12-19 16:27:04 -08002153 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2154 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 }
2156 }
2157 } else {
2158 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
2159 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
2160 if (tp_peer != tp &&
2161 (tp_peer->tg3_flags & TG3_FLAG_INIT_COMPLETE) != 0)
2162 return;
2163
Michael Chanb401e9e2005-12-19 16:27:04 -08002164 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2165 (GRC_LCLCTRL_GPIO_OE1 |
2166 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
Michael Chanb401e9e2005-12-19 16:27:04 -08002168 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2169 GRC_LCLCTRL_GPIO_OE1, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
Michael Chanb401e9e2005-12-19 16:27:04 -08002171 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2172 (GRC_LCLCTRL_GPIO_OE1 |
2173 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 }
2175 }
2176}
2177
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002178static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2179{
2180 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2181 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00002182 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002183 if (speed != SPEED_10)
2184 return 1;
2185 } else if (speed == SPEED_10)
2186 return 1;
2187
2188 return 0;
2189}
2190
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191static int tg3_setup_phy(struct tg3 *, int);
2192
2193#define RESET_KIND_SHUTDOWN 0
2194#define RESET_KIND_INIT 1
2195#define RESET_KIND_SUSPEND 2
2196
2197static void tg3_write_sig_post_reset(struct tg3 *, int);
2198static int tg3_halt_cpu(struct tg3 *, u32);
2199
Matt Carlson0a459aa2008-11-03 16:54:15 -08002200static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002201{
Matt Carlsonce057f02007-11-12 21:08:03 -08002202 u32 val;
2203
Michael Chan51297242007-02-13 12:17:57 -08002204 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
2205 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2206 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2207 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2208
2209 sg_dig_ctrl |=
2210 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2211 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2212 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2213 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002214 return;
Michael Chan51297242007-02-13 12:17:57 -08002215 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002216
Michael Chan60189dd2006-12-17 17:08:07 -08002217 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002218 tg3_bmcr_reset(tp);
2219 val = tr32(GRC_MISC_CFG);
2220 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2221 udelay(40);
2222 return;
Matt Carlson0e5f7842009-11-02 14:26:38 +00002223 } else if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
2224 u32 phytest;
2225 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2226 u32 phy;
2227
2228 tg3_writephy(tp, MII_ADVERTISE, 0);
2229 tg3_writephy(tp, MII_BMCR,
2230 BMCR_ANENABLE | BMCR_ANRESTART);
2231
2232 tg3_writephy(tp, MII_TG3_FET_TEST,
2233 phytest | MII_TG3_FET_SHADOW_EN);
2234 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
2235 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
2236 tg3_writephy(tp,
2237 MII_TG3_FET_SHDW_AUXMODE4,
2238 phy);
2239 }
2240 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2241 }
2242 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002243 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07002244 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2245 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002246
2247 tg3_writephy(tp, MII_TG3_AUX_CTRL,
2248 MII_TG3_AUXCTL_SHDWSEL_PWRCTL |
2249 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2250 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2251 MII_TG3_AUXCTL_PCTL_VREG_11V);
Michael Chan715116a2006-09-27 16:09:25 -07002252 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002253
Michael Chan15c3b692006-03-22 01:06:52 -08002254 /* The PHY should not be powered down on some chips because
2255 * of bugs.
2256 */
2257 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2258 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2259 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
2260 (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)))
2261 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08002262
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002263 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2264 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002265 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2266 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2267 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2268 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2269 }
2270
Michael Chan15c3b692006-03-22 01:06:52 -08002271 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2272}
2273
Matt Carlson3f007892008-11-03 16:51:36 -08002274/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002275static int tg3_nvram_lock(struct tg3 *tp)
2276{
2277 if (tp->tg3_flags & TG3_FLAG_NVRAM) {
2278 int i;
2279
2280 if (tp->nvram_lock_cnt == 0) {
2281 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2282 for (i = 0; i < 8000; i++) {
2283 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2284 break;
2285 udelay(20);
2286 }
2287 if (i == 8000) {
2288 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2289 return -ENODEV;
2290 }
2291 }
2292 tp->nvram_lock_cnt++;
2293 }
2294 return 0;
2295}
2296
2297/* tp->lock is held. */
2298static void tg3_nvram_unlock(struct tg3 *tp)
2299{
2300 if (tp->tg3_flags & TG3_FLAG_NVRAM) {
2301 if (tp->nvram_lock_cnt > 0)
2302 tp->nvram_lock_cnt--;
2303 if (tp->nvram_lock_cnt == 0)
2304 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
2305 }
2306}
2307
2308/* tp->lock is held. */
2309static void tg3_enable_nvram_access(struct tg3 *tp)
2310{
2311 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Matt Carlsonf66a29b2009-11-13 13:03:36 +00002312 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002313 u32 nvaccess = tr32(NVRAM_ACCESS);
2314
2315 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
2316 }
2317}
2318
2319/* tp->lock is held. */
2320static void tg3_disable_nvram_access(struct tg3 *tp)
2321{
2322 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Matt Carlsonf66a29b2009-11-13 13:03:36 +00002323 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002324 u32 nvaccess = tr32(NVRAM_ACCESS);
2325
2326 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
2327 }
2328}
2329
2330static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
2331 u32 offset, u32 *val)
2332{
2333 u32 tmp;
2334 int i;
2335
2336 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
2337 return -EINVAL;
2338
2339 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
2340 EEPROM_ADDR_DEVID_MASK |
2341 EEPROM_ADDR_READ);
2342 tw32(GRC_EEPROM_ADDR,
2343 tmp |
2344 (0 << EEPROM_ADDR_DEVID_SHIFT) |
2345 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
2346 EEPROM_ADDR_ADDR_MASK) |
2347 EEPROM_ADDR_READ | EEPROM_ADDR_START);
2348
2349 for (i = 0; i < 1000; i++) {
2350 tmp = tr32(GRC_EEPROM_ADDR);
2351
2352 if (tmp & EEPROM_ADDR_COMPLETE)
2353 break;
2354 msleep(1);
2355 }
2356 if (!(tmp & EEPROM_ADDR_COMPLETE))
2357 return -EBUSY;
2358
Matt Carlson62cedd12009-04-20 14:52:29 -07002359 tmp = tr32(GRC_EEPROM_DATA);
2360
2361 /*
2362 * The data will always be opposite the native endian
2363 * format. Perform a blind byteswap to compensate.
2364 */
2365 *val = swab32(tmp);
2366
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002367 return 0;
2368}
2369
2370#define NVRAM_CMD_TIMEOUT 10000
2371
2372static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
2373{
2374 int i;
2375
2376 tw32(NVRAM_CMD, nvram_cmd);
2377 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
2378 udelay(10);
2379 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
2380 udelay(10);
2381 break;
2382 }
2383 }
2384
2385 if (i == NVRAM_CMD_TIMEOUT)
2386 return -EBUSY;
2387
2388 return 0;
2389}
2390
2391static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
2392{
2393 if ((tp->tg3_flags & TG3_FLAG_NVRAM) &&
2394 (tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) &&
2395 (tp->tg3_flags2 & TG3_FLG2_FLASH) &&
2396 !(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM_ADDR_TRANS) &&
2397 (tp->nvram_jedecnum == JEDEC_ATMEL))
2398
2399 addr = ((addr / tp->nvram_pagesize) <<
2400 ATMEL_AT45DB0X1B_PAGE_POS) +
2401 (addr % tp->nvram_pagesize);
2402
2403 return addr;
2404}
2405
2406static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
2407{
2408 if ((tp->tg3_flags & TG3_FLAG_NVRAM) &&
2409 (tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) &&
2410 (tp->tg3_flags2 & TG3_FLG2_FLASH) &&
2411 !(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM_ADDR_TRANS) &&
2412 (tp->nvram_jedecnum == JEDEC_ATMEL))
2413
2414 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
2415 tp->nvram_pagesize) +
2416 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
2417
2418 return addr;
2419}
2420
Matt Carlsone4f34112009-02-25 14:25:00 +00002421/* NOTE: Data read in from NVRAM is byteswapped according to
2422 * the byteswapping settings for all other register accesses.
2423 * tg3 devices are BE devices, so on a BE machine, the data
2424 * returned will be exactly as it is seen in NVRAM. On a LE
2425 * machine, the 32-bit value will be byteswapped.
2426 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002427static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
2428{
2429 int ret;
2430
2431 if (!(tp->tg3_flags & TG3_FLAG_NVRAM))
2432 return tg3_nvram_read_using_eeprom(tp, offset, val);
2433
2434 offset = tg3_nvram_phys_addr(tp, offset);
2435
2436 if (offset > NVRAM_ADDR_MSK)
2437 return -EINVAL;
2438
2439 ret = tg3_nvram_lock(tp);
2440 if (ret)
2441 return ret;
2442
2443 tg3_enable_nvram_access(tp);
2444
2445 tw32(NVRAM_ADDR, offset);
2446 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
2447 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
2448
2449 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00002450 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002451
2452 tg3_disable_nvram_access(tp);
2453
2454 tg3_nvram_unlock(tp);
2455
2456 return ret;
2457}
2458
Matt Carlsona9dc5292009-02-25 14:25:30 +00002459/* Ensures NVRAM data is in bytestream format. */
2460static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002461{
2462 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00002463 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002464 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00002465 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002466 return res;
2467}
2468
2469/* tp->lock is held. */
Matt Carlson3f007892008-11-03 16:51:36 -08002470static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
2471{
2472 u32 addr_high, addr_low;
2473 int i;
2474
2475 addr_high = ((tp->dev->dev_addr[0] << 8) |
2476 tp->dev->dev_addr[1]);
2477 addr_low = ((tp->dev->dev_addr[2] << 24) |
2478 (tp->dev->dev_addr[3] << 16) |
2479 (tp->dev->dev_addr[4] << 8) |
2480 (tp->dev->dev_addr[5] << 0));
2481 for (i = 0; i < 4; i++) {
2482 if (i == 1 && skip_mac_1)
2483 continue;
2484 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
2485 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
2486 }
2487
2488 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2489 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2490 for (i = 0; i < 12; i++) {
2491 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
2492 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
2493 }
2494 }
2495
2496 addr_high = (tp->dev->dev_addr[0] +
2497 tp->dev->dev_addr[1] +
2498 tp->dev->dev_addr[2] +
2499 tp->dev->dev_addr[3] +
2500 tp->dev->dev_addr[4] +
2501 tp->dev->dev_addr[5]) &
2502 TX_BACKOFF_SEED_MASK;
2503 tw32(MAC_TX_BACKOFF_SEED, addr_high);
2504}
2505
Michael Chanbc1c7562006-03-20 17:48:03 -08002506static int tg3_set_power_state(struct tg3 *tp, pci_power_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507{
2508 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002509 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510
2511 /* Make sure register accesses (indirect or otherwise)
2512 * will function correctly.
2513 */
2514 pci_write_config_dword(tp->pdev,
2515 TG3PCI_MISC_HOST_CTRL,
2516 tp->misc_host_ctrl);
2517
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 switch (state) {
Michael Chanbc1c7562006-03-20 17:48:03 -08002519 case PCI_D0:
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07002520 pci_enable_wake(tp->pdev, state, false);
2521 pci_set_power_state(tp->pdev, PCI_D0);
Michael Chan8c6bda12005-04-21 17:09:08 -07002522
Michael Chan9d26e212006-12-07 00:21:14 -08002523 /* Switch out of Vaux if it is a NIC */
2524 if (tp->tg3_flags2 & TG3_FLG2_IS_NIC)
Michael Chanb401e9e2005-12-19 16:27:04 -08002525 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526
2527 return 0;
2528
Michael Chanbc1c7562006-03-20 17:48:03 -08002529 case PCI_D1:
Michael Chanbc1c7562006-03-20 17:48:03 -08002530 case PCI_D2:
Michael Chanbc1c7562006-03-20 17:48:03 -08002531 case PCI_D3hot:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 break;
2533
2534 default:
Joe Perches05dbe002010-02-17 19:44:19 +00002535 netdev_err(tp->dev, "Invalid power state (D%d) requested\n",
2536 state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002538 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002539
2540 /* Restore the CLKREQ setting. */
2541 if (tp->tg3_flags3 & TG3_FLG3_CLKREQ_BUG) {
2542 u16 lnkctl;
2543
2544 pci_read_config_word(tp->pdev,
2545 tp->pcie_cap + PCI_EXP_LNKCTL,
2546 &lnkctl);
2547 lnkctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
2548 pci_write_config_word(tp->pdev,
2549 tp->pcie_cap + PCI_EXP_LNKCTL,
2550 lnkctl);
2551 }
2552
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
2554 tw32(TG3PCI_MISC_HOST_CTRL,
2555 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
2556
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002557 device_should_wake = pci_pme_capable(tp->pdev, state) &&
2558 device_may_wakeup(&tp->pdev->dev) &&
2559 (tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
2560
Matt Carlsondd477002008-05-25 23:45:58 -07002561 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08002562 do_low_power = false;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002563 if ((tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED) &&
2564 !tp->link_config.phy_is_low_power) {
2565 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002566 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002567
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002568 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002569
2570 tp->link_config.phy_is_low_power = 1;
2571
2572 tp->link_config.orig_speed = phydev->speed;
2573 tp->link_config.orig_duplex = phydev->duplex;
2574 tp->link_config.orig_autoneg = phydev->autoneg;
2575 tp->link_config.orig_advertising = phydev->advertising;
2576
2577 advertising = ADVERTISED_TP |
2578 ADVERTISED_Pause |
2579 ADVERTISED_Autoneg |
2580 ADVERTISED_10baseT_Half;
2581
2582 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002583 device_should_wake) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002584 if (tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB)
2585 advertising |=
2586 ADVERTISED_100baseT_Half |
2587 ADVERTISED_100baseT_Full |
2588 ADVERTISED_10baseT_Full;
2589 else
2590 advertising |= ADVERTISED_10baseT_Full;
2591 }
2592
2593 phydev->advertising = advertising;
2594
2595 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002596
2597 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00002598 if (phyid != PHY_ID_BCMAC131) {
2599 phyid &= PHY_BCM_OUI_MASK;
2600 if (phyid == PHY_BCM_OUI_1 ||
2601 phyid == PHY_BCM_OUI_2 ||
2602 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08002603 do_low_power = true;
2604 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002605 }
Matt Carlsondd477002008-05-25 23:45:58 -07002606 } else {
Matt Carlson20232762008-12-21 20:18:56 -08002607 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002608
Matt Carlsondd477002008-05-25 23:45:58 -07002609 if (tp->link_config.phy_is_low_power == 0) {
2610 tp->link_config.phy_is_low_power = 1;
2611 tp->link_config.orig_speed = tp->link_config.speed;
2612 tp->link_config.orig_duplex = tp->link_config.duplex;
2613 tp->link_config.orig_autoneg = tp->link_config.autoneg;
2614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615
Matt Carlsondd477002008-05-25 23:45:58 -07002616 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)) {
2617 tp->link_config.speed = SPEED_10;
2618 tp->link_config.duplex = DUPLEX_HALF;
2619 tp->link_config.autoneg = AUTONEG_ENABLE;
2620 tg3_setup_phy(tp, 0);
2621 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 }
2623
Michael Chanb5d37722006-09-27 16:06:21 -07002624 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
2625 u32 val;
2626
2627 val = tr32(GRC_VCPU_EXT_CTRL);
2628 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
2629 } else if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08002630 int i;
2631 u32 val;
2632
2633 for (i = 0; i < 200; i++) {
2634 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
2635 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
2636 break;
2637 msleep(1);
2638 }
2639 }
Gary Zambranoa85feb82007-05-05 11:52:19 -07002640 if (tp->tg3_flags & TG3_FLAG_WOL_CAP)
2641 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
2642 WOL_DRV_STATE_SHUTDOWN |
2643 WOL_DRV_WOL |
2644 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08002645
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002646 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 u32 mac_mode;
2648
2649 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08002650 if (do_low_power) {
Matt Carlsondd477002008-05-25 23:45:58 -07002651 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x5a);
2652 udelay(40);
2653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654
Michael Chan3f7045c2006-09-27 16:02:29 -07002655 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)
2656 mac_mode = MAC_MODE_PORT_MODE_GMII;
2657 else
2658 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002660 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
2661 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2662 ASIC_REV_5700) {
2663 u32 speed = (tp->tg3_flags &
2664 TG3_FLAG_WOL_SPEED_100MB) ?
2665 SPEED_100 : SPEED_10;
2666 if (tg3_5700_link_polarity(tp, speed))
2667 mac_mode |= MAC_MODE_LINK_POLARITY;
2668 else
2669 mac_mode &= ~MAC_MODE_LINK_POLARITY;
2670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 } else {
2672 mac_mode = MAC_MODE_PORT_MODE_TBI;
2673 }
2674
John W. Linvillecbf46852005-04-21 17:01:29 -07002675 if (!(tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 tw32(MAC_LED_CTRL, tp->led_ctrl);
2677
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002678 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
2679 if (((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
2680 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) &&
2681 ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
2682 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)))
2683 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684
Matt Carlson3bda1252008-08-15 14:08:22 -07002685 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
2686 mac_mode |= tp->mac_mode &
2687 (MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN);
2688 if (mac_mode & MAC_MODE_APE_TX_EN)
2689 mac_mode |= MAC_MODE_TDE_ENABLE;
2690 }
2691
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 tw32_f(MAC_MODE, mac_mode);
2693 udelay(100);
2694
2695 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
2696 udelay(10);
2697 }
2698
2699 if (!(tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB) &&
2700 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2701 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
2702 u32 base_val;
2703
2704 base_val = tp->pci_clock_ctrl;
2705 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
2706 CLOCK_CTRL_TXCLK_DISABLE);
2707
Michael Chanb401e9e2005-12-19 16:27:04 -08002708 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
2709 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Michael Chand7b0a852007-02-13 12:17:38 -08002710 } else if ((tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
Matt Carlson795d01c2007-10-07 23:28:17 -07002711 (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
Michael Chand7b0a852007-02-13 12:17:38 -08002712 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)) {
Michael Chan4cf78e42005-07-25 12:29:19 -07002713 /* do nothing */
Michael Chan85e94ce2005-04-21 17:05:28 -07002714 } else if (!((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF))) {
2716 u32 newbits1, newbits2;
2717
2718 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2719 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2720 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
2721 CLOCK_CTRL_TXCLK_DISABLE |
2722 CLOCK_CTRL_ALTCLK);
2723 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2724 } else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
2725 newbits1 = CLOCK_CTRL_625_CORE;
2726 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
2727 } else {
2728 newbits1 = CLOCK_CTRL_ALTCLK;
2729 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2730 }
2731
Michael Chanb401e9e2005-12-19 16:27:04 -08002732 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
2733 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734
Michael Chanb401e9e2005-12-19 16:27:04 -08002735 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
2736 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737
2738 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
2739 u32 newbits3;
2740
2741 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2742 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2743 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
2744 CLOCK_CTRL_TXCLK_DISABLE |
2745 CLOCK_CTRL_44MHZ_CORE);
2746 } else {
2747 newbits3 = CLOCK_CTRL_44MHZ_CORE;
2748 }
2749
Michael Chanb401e9e2005-12-19 16:27:04 -08002750 tw32_wait_f(TG3PCI_CLOCK_CTRL,
2751 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 }
2753 }
2754
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002755 if (!(device_should_wake) &&
Matt Carlson22435842008-11-21 17:21:13 -08002756 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08002757 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08002758
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 tg3_frob_aux_power(tp);
2760
2761 /* Workaround for unstable PLL clock */
2762 if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
2763 (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
2764 u32 val = tr32(0x7d00);
2765
2766 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
2767 tw32(0x7d00, val);
Michael Chan6921d202005-12-13 21:15:53 -08002768 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08002769 int err;
2770
2771 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08002773 if (!err)
2774 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08002775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 }
2777
Michael Chanbbadf502006-04-06 21:46:34 -07002778 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
2779
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002780 if (device_should_wake)
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07002781 pci_enable_wake(tp->pdev, state, true);
2782
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 /* Finally, set the new power state. */
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07002784 pci_set_power_state(tp->pdev, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 return 0;
2787}
2788
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
2790{
2791 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
2792 case MII_TG3_AUX_STAT_10HALF:
2793 *speed = SPEED_10;
2794 *duplex = DUPLEX_HALF;
2795 break;
2796
2797 case MII_TG3_AUX_STAT_10FULL:
2798 *speed = SPEED_10;
2799 *duplex = DUPLEX_FULL;
2800 break;
2801
2802 case MII_TG3_AUX_STAT_100HALF:
2803 *speed = SPEED_100;
2804 *duplex = DUPLEX_HALF;
2805 break;
2806
2807 case MII_TG3_AUX_STAT_100FULL:
2808 *speed = SPEED_100;
2809 *duplex = DUPLEX_FULL;
2810 break;
2811
2812 case MII_TG3_AUX_STAT_1000HALF:
2813 *speed = SPEED_1000;
2814 *duplex = DUPLEX_HALF;
2815 break;
2816
2817 case MII_TG3_AUX_STAT_1000FULL:
2818 *speed = SPEED_1000;
2819 *duplex = DUPLEX_FULL;
2820 break;
2821
2822 default:
Matt Carlson7f97a4b2009-08-25 10:10:03 +00002823 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07002824 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
2825 SPEED_10;
2826 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
2827 DUPLEX_HALF;
2828 break;
2829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 *speed = SPEED_INVALID;
2831 *duplex = DUPLEX_INVALID;
2832 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834}
2835
2836static void tg3_phy_copper_begin(struct tg3 *tp)
2837{
2838 u32 new_adv;
2839 int i;
2840
2841 if (tp->link_config.phy_is_low_power) {
2842 /* Entering low power mode. Disable gigabit and
2843 * 100baseT advertisements.
2844 */
2845 tg3_writephy(tp, MII_TG3_CTRL, 0);
2846
2847 new_adv = (ADVERTISE_10HALF | ADVERTISE_10FULL |
2848 ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
2849 if (tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB)
2850 new_adv |= (ADVERTISE_100HALF | ADVERTISE_100FULL);
2851
2852 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2853 } else if (tp->link_config.speed == SPEED_INVALID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 if (tp->tg3_flags & TG3_FLAG_10_100_ONLY)
2855 tp->link_config.advertising &=
2856 ~(ADVERTISED_1000baseT_Half |
2857 ADVERTISED_1000baseT_Full);
2858
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002859 new_adv = ADVERTISE_CSMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 if (tp->link_config.advertising & ADVERTISED_10baseT_Half)
2861 new_adv |= ADVERTISE_10HALF;
2862 if (tp->link_config.advertising & ADVERTISED_10baseT_Full)
2863 new_adv |= ADVERTISE_10FULL;
2864 if (tp->link_config.advertising & ADVERTISED_100baseT_Half)
2865 new_adv |= ADVERTISE_100HALF;
2866 if (tp->link_config.advertising & ADVERTISED_100baseT_Full)
2867 new_adv |= ADVERTISE_100FULL;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002868
2869 new_adv |= tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2870
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2872
2873 if (tp->link_config.advertising &
2874 (ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full)) {
2875 new_adv = 0;
2876 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
2877 new_adv |= MII_TG3_CTRL_ADV_1000_HALF;
2878 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
2879 new_adv |= MII_TG3_CTRL_ADV_1000_FULL;
2880 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY) &&
2881 (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2882 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0))
2883 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2884 MII_TG3_CTRL_ENABLE_AS_MASTER);
2885 tg3_writephy(tp, MII_TG3_CTRL, new_adv);
2886 } else {
2887 tg3_writephy(tp, MII_TG3_CTRL, 0);
2888 }
2889 } else {
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002890 new_adv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2891 new_adv |= ADVERTISE_CSMA;
2892
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 /* Asking for a specific link mode. */
2894 if (tp->link_config.speed == SPEED_1000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2896
2897 if (tp->link_config.duplex == DUPLEX_FULL)
2898 new_adv = MII_TG3_CTRL_ADV_1000_FULL;
2899 else
2900 new_adv = MII_TG3_CTRL_ADV_1000_HALF;
2901 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2902 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
2903 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2904 MII_TG3_CTRL_ENABLE_AS_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 if (tp->link_config.speed == SPEED_100) {
2907 if (tp->link_config.duplex == DUPLEX_FULL)
2908 new_adv |= ADVERTISE_100FULL;
2909 else
2910 new_adv |= ADVERTISE_100HALF;
2911 } else {
2912 if (tp->link_config.duplex == DUPLEX_FULL)
2913 new_adv |= ADVERTISE_10FULL;
2914 else
2915 new_adv |= ADVERTISE_10HALF;
2916 }
2917 tg3_writephy(tp, MII_ADVERTISE, new_adv);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002918
2919 new_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002921
2922 tg3_writephy(tp, MII_TG3_CTRL, new_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 }
2924
2925 if (tp->link_config.autoneg == AUTONEG_DISABLE &&
2926 tp->link_config.speed != SPEED_INVALID) {
2927 u32 bmcr, orig_bmcr;
2928
2929 tp->link_config.active_speed = tp->link_config.speed;
2930 tp->link_config.active_duplex = tp->link_config.duplex;
2931
2932 bmcr = 0;
2933 switch (tp->link_config.speed) {
2934 default:
2935 case SPEED_10:
2936 break;
2937
2938 case SPEED_100:
2939 bmcr |= BMCR_SPEED100;
2940 break;
2941
2942 case SPEED_1000:
2943 bmcr |= TG3_BMCR_SPEED1000;
2944 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946
2947 if (tp->link_config.duplex == DUPLEX_FULL)
2948 bmcr |= BMCR_FULLDPLX;
2949
2950 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
2951 (bmcr != orig_bmcr)) {
2952 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
2953 for (i = 0; i < 1500; i++) {
2954 u32 tmp;
2955
2956 udelay(10);
2957 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
2958 tg3_readphy(tp, MII_BMSR, &tmp))
2959 continue;
2960 if (!(tmp & BMSR_LSTATUS)) {
2961 udelay(40);
2962 break;
2963 }
2964 }
2965 tg3_writephy(tp, MII_BMCR, bmcr);
2966 udelay(40);
2967 }
2968 } else {
2969 tg3_writephy(tp, MII_BMCR,
2970 BMCR_ANENABLE | BMCR_ANRESTART);
2971 }
2972}
2973
2974static int tg3_init_5401phy_dsp(struct tg3 *tp)
2975{
2976 int err;
2977
2978 /* Turn off tap power management. */
2979 /* Set Extended packet length bit */
2980 err = tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4c20);
2981
2982 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x0012);
2983 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x1804);
2984
2985 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x0013);
2986 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x1204);
2987
2988 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8006);
2989 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0132);
2990
2991 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8006);
2992 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0232);
2993
2994 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x201f);
2995 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0a20);
2996
2997 udelay(40);
2998
2999 return err;
3000}
3001
Michael Chan3600d912006-12-07 00:21:48 -08003002static int tg3_copper_is_advertising_all(struct tg3 *tp, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003{
Michael Chan3600d912006-12-07 00:21:48 -08003004 u32 adv_reg, all_mask = 0;
3005
3006 if (mask & ADVERTISED_10baseT_Half)
3007 all_mask |= ADVERTISE_10HALF;
3008 if (mask & ADVERTISED_10baseT_Full)
3009 all_mask |= ADVERTISE_10FULL;
3010 if (mask & ADVERTISED_100baseT_Half)
3011 all_mask |= ADVERTISE_100HALF;
3012 if (mask & ADVERTISED_100baseT_Full)
3013 all_mask |= ADVERTISE_100FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014
3015 if (tg3_readphy(tp, MII_ADVERTISE, &adv_reg))
3016 return 0;
3017
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 if ((adv_reg & all_mask) != all_mask)
3019 return 0;
3020 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY)) {
3021 u32 tg3_ctrl;
3022
Michael Chan3600d912006-12-07 00:21:48 -08003023 all_mask = 0;
3024 if (mask & ADVERTISED_1000baseT_Half)
3025 all_mask |= ADVERTISE_1000HALF;
3026 if (mask & ADVERTISED_1000baseT_Full)
3027 all_mask |= ADVERTISE_1000FULL;
3028
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 if (tg3_readphy(tp, MII_TG3_CTRL, &tg3_ctrl))
3030 return 0;
3031
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 if ((tg3_ctrl & all_mask) != all_mask)
3033 return 0;
3034 }
3035 return 1;
3036}
3037
Matt Carlsonef167e22007-12-20 20:10:01 -08003038static int tg3_adv_1000T_flowctrl_ok(struct tg3 *tp, u32 *lcladv, u32 *rmtadv)
3039{
3040 u32 curadv, reqadv;
3041
3042 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
3043 return 1;
3044
3045 curadv = *lcladv & (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
3046 reqadv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
3047
3048 if (tp->link_config.active_duplex == DUPLEX_FULL) {
3049 if (curadv != reqadv)
3050 return 0;
3051
3052 if (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG)
3053 tg3_readphy(tp, MII_LPA, rmtadv);
3054 } else {
3055 /* Reprogram the advertisement register, even if it
3056 * does not affect the current link. If the link
3057 * gets renegotiated in the future, we can save an
3058 * additional renegotiation cycle by advertising
3059 * it correctly in the first place.
3060 */
3061 if (curadv != reqadv) {
3062 *lcladv &= ~(ADVERTISE_PAUSE_CAP |
3063 ADVERTISE_PAUSE_ASYM);
3064 tg3_writephy(tp, MII_ADVERTISE, *lcladv | reqadv);
3065 }
3066 }
3067
3068 return 1;
3069}
3070
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
3072{
3073 int current_link_up;
3074 u32 bmsr, dummy;
Matt Carlsonef167e22007-12-20 20:10:01 -08003075 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 u16 current_speed;
3077 u8 current_duplex;
3078 int i, err;
3079
3080 tw32(MAC_EVENT, 0);
3081
3082 tw32_f(MAC_STATUS,
3083 (MAC_STATUS_SYNC_CHANGED |
3084 MAC_STATUS_CFG_CHANGED |
3085 MAC_STATUS_MI_COMPLETION |
3086 MAC_STATUS_LNKSTATE_CHANGED));
3087 udelay(40);
3088
Matt Carlson8ef21422008-05-02 16:47:53 -07003089 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
3090 tw32_f(MAC_MI_MODE,
3091 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
3092 udelay(80);
3093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094
3095 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x02);
3096
3097 /* Some third-party PHYs need to be reset on link going
3098 * down.
3099 */
3100 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3101 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
3102 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
3103 netif_carrier_ok(tp->dev)) {
3104 tg3_readphy(tp, MII_BMSR, &bmsr);
3105 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3106 !(bmsr & BMSR_LSTATUS))
3107 force_reset = 1;
3108 }
3109 if (force_reset)
3110 tg3_phy_reset(tp);
3111
Matt Carlson79eb6902010-02-17 15:17:03 +00003112 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 tg3_readphy(tp, MII_BMSR, &bmsr);
3114 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
3115 !(tp->tg3_flags & TG3_FLAG_INIT_COMPLETE))
3116 bmsr = 0;
3117
3118 if (!(bmsr & BMSR_LSTATUS)) {
3119 err = tg3_init_5401phy_dsp(tp);
3120 if (err)
3121 return err;
3122
3123 tg3_readphy(tp, MII_BMSR, &bmsr);
3124 for (i = 0; i < 1000; i++) {
3125 udelay(10);
3126 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3127 (bmsr & BMSR_LSTATUS)) {
3128 udelay(40);
3129 break;
3130 }
3131 }
3132
Matt Carlson79eb6902010-02-17 15:17:03 +00003133 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
3134 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 !(bmsr & BMSR_LSTATUS) &&
3136 tp->link_config.active_speed == SPEED_1000) {
3137 err = tg3_phy_reset(tp);
3138 if (!err)
3139 err = tg3_init_5401phy_dsp(tp);
3140 if (err)
3141 return err;
3142 }
3143 }
3144 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3145 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
3146 /* 5701 {A0,B0} CRC bug workaround */
3147 tg3_writephy(tp, 0x15, 0x0a75);
3148 tg3_writephy(tp, 0x1c, 0x8c68);
3149 tg3_writephy(tp, 0x1c, 0x8d68);
3150 tg3_writephy(tp, 0x1c, 0x8c68);
3151 }
3152
3153 /* Clear pending interrupts... */
3154 tg3_readphy(tp, MII_TG3_ISTAT, &dummy);
3155 tg3_readphy(tp, MII_TG3_ISTAT, &dummy);
3156
3157 if (tp->tg3_flags & TG3_FLAG_USE_MI_INTERRUPT)
3158 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlson7f97a4b2009-08-25 10:10:03 +00003159 else if (!(tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 tg3_writephy(tp, MII_TG3_IMASK, ~0);
3161
3162 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3163 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3164 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
3165 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3166 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
3167 else
3168 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
3169 }
3170
3171 current_link_up = 0;
3172 current_speed = SPEED_INVALID;
3173 current_duplex = DUPLEX_INVALID;
3174
3175 if (tp->tg3_flags2 & TG3_FLG2_CAPACITIVE_COUPLING) {
3176 u32 val;
3177
3178 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4007);
3179 tg3_readphy(tp, MII_TG3_AUX_CTRL, &val);
3180 if (!(val & (1 << 10))) {
3181 val |= (1 << 10);
3182 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
3183 goto relink;
3184 }
3185 }
3186
3187 bmsr = 0;
3188 for (i = 0; i < 100; i++) {
3189 tg3_readphy(tp, MII_BMSR, &bmsr);
3190 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3191 (bmsr & BMSR_LSTATUS))
3192 break;
3193 udelay(40);
3194 }
3195
3196 if (bmsr & BMSR_LSTATUS) {
3197 u32 aux_stat, bmcr;
3198
3199 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
3200 for (i = 0; i < 2000; i++) {
3201 udelay(10);
3202 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
3203 aux_stat)
3204 break;
3205 }
3206
3207 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
3208 &current_speed,
3209 &current_duplex);
3210
3211 bmcr = 0;
3212 for (i = 0; i < 200; i++) {
3213 tg3_readphy(tp, MII_BMCR, &bmcr);
3214 if (tg3_readphy(tp, MII_BMCR, &bmcr))
3215 continue;
3216 if (bmcr && bmcr != 0x7fff)
3217 break;
3218 udelay(10);
3219 }
3220
Matt Carlsonef167e22007-12-20 20:10:01 -08003221 lcl_adv = 0;
3222 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223
Matt Carlsonef167e22007-12-20 20:10:01 -08003224 tp->link_config.active_speed = current_speed;
3225 tp->link_config.active_duplex = current_duplex;
3226
3227 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3228 if ((bmcr & BMCR_ANENABLE) &&
3229 tg3_copper_is_advertising_all(tp,
3230 tp->link_config.advertising)) {
3231 if (tg3_adv_1000T_flowctrl_ok(tp, &lcl_adv,
3232 &rmt_adv))
3233 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 }
3235 } else {
3236 if (!(bmcr & BMCR_ANENABLE) &&
3237 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08003238 tp->link_config.duplex == current_duplex &&
3239 tp->link_config.flowctrl ==
3240 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 }
3243 }
3244
Matt Carlsonef167e22007-12-20 20:10:01 -08003245 if (current_link_up == 1 &&
3246 tp->link_config.active_duplex == DUPLEX_FULL)
3247 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 }
3249
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250relink:
Michael Chan6921d202005-12-13 21:15:53 -08003251 if (current_link_up == 0 || tp->link_config.phy_is_low_power) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 u32 tmp;
3253
3254 tg3_phy_copper_begin(tp);
3255
3256 tg3_readphy(tp, MII_BMSR, &tmp);
3257 if (!tg3_readphy(tp, MII_BMSR, &tmp) &&
3258 (tmp & BMSR_LSTATUS))
3259 current_link_up = 1;
3260 }
3261
3262 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
3263 if (current_link_up == 1) {
3264 if (tp->link_config.active_speed == SPEED_100 ||
3265 tp->link_config.active_speed == SPEED_10)
3266 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3267 else
3268 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlson7f97a4b2009-08-25 10:10:03 +00003269 } else if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET)
3270 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3271 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
3273
3274 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
3275 if (tp->link_config.active_duplex == DUPLEX_HALF)
3276 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
3277
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003279 if (current_link_up == 1 &&
3280 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003282 else
3283 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 }
3285
3286 /* ??? Without this setting Netgear GA302T PHY does not
3287 * ??? send/receive packets...
3288 */
Matt Carlson79eb6902010-02-17 15:17:03 +00003289 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
3291 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
3292 tw32_f(MAC_MI_MODE, tp->mi_mode);
3293 udelay(80);
3294 }
3295
3296 tw32_f(MAC_MODE, tp->mac_mode);
3297 udelay(40);
3298
3299 if (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) {
3300 /* Polled via timer. */
3301 tw32_f(MAC_EVENT, 0);
3302 } else {
3303 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
3304 }
3305 udelay(40);
3306
3307 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
3308 current_link_up == 1 &&
3309 tp->link_config.active_speed == SPEED_1000 &&
3310 ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) ||
3311 (tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED))) {
3312 udelay(120);
3313 tw32_f(MAC_STATUS,
3314 (MAC_STATUS_SYNC_CHANGED |
3315 MAC_STATUS_CFG_CHANGED));
3316 udelay(40);
3317 tg3_write_mem(tp,
3318 NIC_SRAM_FIRMWARE_MBOX,
3319 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
3320 }
3321
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003322 /* Prevent send BD corruption. */
3323 if (tp->tg3_flags3 & TG3_FLG3_CLKREQ_BUG) {
3324 u16 oldlnkctl, newlnkctl;
3325
3326 pci_read_config_word(tp->pdev,
3327 tp->pcie_cap + PCI_EXP_LNKCTL,
3328 &oldlnkctl);
3329 if (tp->link_config.active_speed == SPEED_100 ||
3330 tp->link_config.active_speed == SPEED_10)
3331 newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
3332 else
3333 newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
3334 if (newlnkctl != oldlnkctl)
3335 pci_write_config_word(tp->pdev,
3336 tp->pcie_cap + PCI_EXP_LNKCTL,
3337 newlnkctl);
3338 }
3339
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 if (current_link_up != netif_carrier_ok(tp->dev)) {
3341 if (current_link_up)
3342 netif_carrier_on(tp->dev);
3343 else
3344 netif_carrier_off(tp->dev);
3345 tg3_link_report(tp);
3346 }
3347
3348 return 0;
3349}
3350
3351struct tg3_fiber_aneginfo {
3352 int state;
3353#define ANEG_STATE_UNKNOWN 0
3354#define ANEG_STATE_AN_ENABLE 1
3355#define ANEG_STATE_RESTART_INIT 2
3356#define ANEG_STATE_RESTART 3
3357#define ANEG_STATE_DISABLE_LINK_OK 4
3358#define ANEG_STATE_ABILITY_DETECT_INIT 5
3359#define ANEG_STATE_ABILITY_DETECT 6
3360#define ANEG_STATE_ACK_DETECT_INIT 7
3361#define ANEG_STATE_ACK_DETECT 8
3362#define ANEG_STATE_COMPLETE_ACK_INIT 9
3363#define ANEG_STATE_COMPLETE_ACK 10
3364#define ANEG_STATE_IDLE_DETECT_INIT 11
3365#define ANEG_STATE_IDLE_DETECT 12
3366#define ANEG_STATE_LINK_OK 13
3367#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
3368#define ANEG_STATE_NEXT_PAGE_WAIT 15
3369
3370 u32 flags;
3371#define MR_AN_ENABLE 0x00000001
3372#define MR_RESTART_AN 0x00000002
3373#define MR_AN_COMPLETE 0x00000004
3374#define MR_PAGE_RX 0x00000008
3375#define MR_NP_LOADED 0x00000010
3376#define MR_TOGGLE_TX 0x00000020
3377#define MR_LP_ADV_FULL_DUPLEX 0x00000040
3378#define MR_LP_ADV_HALF_DUPLEX 0x00000080
3379#define MR_LP_ADV_SYM_PAUSE 0x00000100
3380#define MR_LP_ADV_ASYM_PAUSE 0x00000200
3381#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
3382#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
3383#define MR_LP_ADV_NEXT_PAGE 0x00001000
3384#define MR_TOGGLE_RX 0x00002000
3385#define MR_NP_RX 0x00004000
3386
3387#define MR_LINK_OK 0x80000000
3388
3389 unsigned long link_time, cur_time;
3390
3391 u32 ability_match_cfg;
3392 int ability_match_count;
3393
3394 char ability_match, idle_match, ack_match;
3395
3396 u32 txconfig, rxconfig;
3397#define ANEG_CFG_NP 0x00000080
3398#define ANEG_CFG_ACK 0x00000040
3399#define ANEG_CFG_RF2 0x00000020
3400#define ANEG_CFG_RF1 0x00000010
3401#define ANEG_CFG_PS2 0x00000001
3402#define ANEG_CFG_PS1 0x00008000
3403#define ANEG_CFG_HD 0x00004000
3404#define ANEG_CFG_FD 0x00002000
3405#define ANEG_CFG_INVAL 0x00001f06
3406
3407};
3408#define ANEG_OK 0
3409#define ANEG_DONE 1
3410#define ANEG_TIMER_ENAB 2
3411#define ANEG_FAILED -1
3412
3413#define ANEG_STATE_SETTLE_TIME 10000
3414
3415static int tg3_fiber_aneg_smachine(struct tg3 *tp,
3416 struct tg3_fiber_aneginfo *ap)
3417{
Matt Carlson5be73b42007-12-20 20:09:29 -08003418 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 unsigned long delta;
3420 u32 rx_cfg_reg;
3421 int ret;
3422
3423 if (ap->state == ANEG_STATE_UNKNOWN) {
3424 ap->rxconfig = 0;
3425 ap->link_time = 0;
3426 ap->cur_time = 0;
3427 ap->ability_match_cfg = 0;
3428 ap->ability_match_count = 0;
3429 ap->ability_match = 0;
3430 ap->idle_match = 0;
3431 ap->ack_match = 0;
3432 }
3433 ap->cur_time++;
3434
3435 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
3436 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
3437
3438 if (rx_cfg_reg != ap->ability_match_cfg) {
3439 ap->ability_match_cfg = rx_cfg_reg;
3440 ap->ability_match = 0;
3441 ap->ability_match_count = 0;
3442 } else {
3443 if (++ap->ability_match_count > 1) {
3444 ap->ability_match = 1;
3445 ap->ability_match_cfg = rx_cfg_reg;
3446 }
3447 }
3448 if (rx_cfg_reg & ANEG_CFG_ACK)
3449 ap->ack_match = 1;
3450 else
3451 ap->ack_match = 0;
3452
3453 ap->idle_match = 0;
3454 } else {
3455 ap->idle_match = 1;
3456 ap->ability_match_cfg = 0;
3457 ap->ability_match_count = 0;
3458 ap->ability_match = 0;
3459 ap->ack_match = 0;
3460
3461 rx_cfg_reg = 0;
3462 }
3463
3464 ap->rxconfig = rx_cfg_reg;
3465 ret = ANEG_OK;
3466
Matt Carlson33f401a2010-04-05 10:19:27 +00003467 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 case ANEG_STATE_UNKNOWN:
3469 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
3470 ap->state = ANEG_STATE_AN_ENABLE;
3471
3472 /* fallthru */
3473 case ANEG_STATE_AN_ENABLE:
3474 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
3475 if (ap->flags & MR_AN_ENABLE) {
3476 ap->link_time = 0;
3477 ap->cur_time = 0;
3478 ap->ability_match_cfg = 0;
3479 ap->ability_match_count = 0;
3480 ap->ability_match = 0;
3481 ap->idle_match = 0;
3482 ap->ack_match = 0;
3483
3484 ap->state = ANEG_STATE_RESTART_INIT;
3485 } else {
3486 ap->state = ANEG_STATE_DISABLE_LINK_OK;
3487 }
3488 break;
3489
3490 case ANEG_STATE_RESTART_INIT:
3491 ap->link_time = ap->cur_time;
3492 ap->flags &= ~(MR_NP_LOADED);
3493 ap->txconfig = 0;
3494 tw32(MAC_TX_AUTO_NEG, 0);
3495 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3496 tw32_f(MAC_MODE, tp->mac_mode);
3497 udelay(40);
3498
3499 ret = ANEG_TIMER_ENAB;
3500 ap->state = ANEG_STATE_RESTART;
3501
3502 /* fallthru */
3503 case ANEG_STATE_RESTART:
3504 delta = ap->cur_time - ap->link_time;
Matt Carlson859a588792010-04-05 10:19:28 +00003505 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a588792010-04-05 10:19:28 +00003507 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 break;
3510
3511 case ANEG_STATE_DISABLE_LINK_OK:
3512 ret = ANEG_DONE;
3513 break;
3514
3515 case ANEG_STATE_ABILITY_DETECT_INIT:
3516 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08003517 ap->txconfig = ANEG_CFG_FD;
3518 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3519 if (flowctrl & ADVERTISE_1000XPAUSE)
3520 ap->txconfig |= ANEG_CFG_PS1;
3521 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3522 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3524 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3525 tw32_f(MAC_MODE, tp->mac_mode);
3526 udelay(40);
3527
3528 ap->state = ANEG_STATE_ABILITY_DETECT;
3529 break;
3530
3531 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a588792010-04-05 10:19:28 +00003532 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 break;
3535
3536 case ANEG_STATE_ACK_DETECT_INIT:
3537 ap->txconfig |= ANEG_CFG_ACK;
3538 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3539 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3540 tw32_f(MAC_MODE, tp->mac_mode);
3541 udelay(40);
3542
3543 ap->state = ANEG_STATE_ACK_DETECT;
3544
3545 /* fallthru */
3546 case ANEG_STATE_ACK_DETECT:
3547 if (ap->ack_match != 0) {
3548 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
3549 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
3550 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
3551 } else {
3552 ap->state = ANEG_STATE_AN_ENABLE;
3553 }
3554 } else if (ap->ability_match != 0 &&
3555 ap->rxconfig == 0) {
3556 ap->state = ANEG_STATE_AN_ENABLE;
3557 }
3558 break;
3559
3560 case ANEG_STATE_COMPLETE_ACK_INIT:
3561 if (ap->rxconfig & ANEG_CFG_INVAL) {
3562 ret = ANEG_FAILED;
3563 break;
3564 }
3565 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
3566 MR_LP_ADV_HALF_DUPLEX |
3567 MR_LP_ADV_SYM_PAUSE |
3568 MR_LP_ADV_ASYM_PAUSE |
3569 MR_LP_ADV_REMOTE_FAULT1 |
3570 MR_LP_ADV_REMOTE_FAULT2 |
3571 MR_LP_ADV_NEXT_PAGE |
3572 MR_TOGGLE_RX |
3573 MR_NP_RX);
3574 if (ap->rxconfig & ANEG_CFG_FD)
3575 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
3576 if (ap->rxconfig & ANEG_CFG_HD)
3577 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
3578 if (ap->rxconfig & ANEG_CFG_PS1)
3579 ap->flags |= MR_LP_ADV_SYM_PAUSE;
3580 if (ap->rxconfig & ANEG_CFG_PS2)
3581 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
3582 if (ap->rxconfig & ANEG_CFG_RF1)
3583 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
3584 if (ap->rxconfig & ANEG_CFG_RF2)
3585 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
3586 if (ap->rxconfig & ANEG_CFG_NP)
3587 ap->flags |= MR_LP_ADV_NEXT_PAGE;
3588
3589 ap->link_time = ap->cur_time;
3590
3591 ap->flags ^= (MR_TOGGLE_TX);
3592 if (ap->rxconfig & 0x0008)
3593 ap->flags |= MR_TOGGLE_RX;
3594 if (ap->rxconfig & ANEG_CFG_NP)
3595 ap->flags |= MR_NP_RX;
3596 ap->flags |= MR_PAGE_RX;
3597
3598 ap->state = ANEG_STATE_COMPLETE_ACK;
3599 ret = ANEG_TIMER_ENAB;
3600 break;
3601
3602 case ANEG_STATE_COMPLETE_ACK:
3603 if (ap->ability_match != 0 &&
3604 ap->rxconfig == 0) {
3605 ap->state = ANEG_STATE_AN_ENABLE;
3606 break;
3607 }
3608 delta = ap->cur_time - ap->link_time;
3609 if (delta > ANEG_STATE_SETTLE_TIME) {
3610 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
3611 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3612 } else {
3613 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
3614 !(ap->flags & MR_NP_RX)) {
3615 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3616 } else {
3617 ret = ANEG_FAILED;
3618 }
3619 }
3620 }
3621 break;
3622
3623 case ANEG_STATE_IDLE_DETECT_INIT:
3624 ap->link_time = ap->cur_time;
3625 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3626 tw32_f(MAC_MODE, tp->mac_mode);
3627 udelay(40);
3628
3629 ap->state = ANEG_STATE_IDLE_DETECT;
3630 ret = ANEG_TIMER_ENAB;
3631 break;
3632
3633 case ANEG_STATE_IDLE_DETECT:
3634 if (ap->ability_match != 0 &&
3635 ap->rxconfig == 0) {
3636 ap->state = ANEG_STATE_AN_ENABLE;
3637 break;
3638 }
3639 delta = ap->cur_time - ap->link_time;
3640 if (delta > ANEG_STATE_SETTLE_TIME) {
3641 /* XXX another gem from the Broadcom driver :( */
3642 ap->state = ANEG_STATE_LINK_OK;
3643 }
3644 break;
3645
3646 case ANEG_STATE_LINK_OK:
3647 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
3648 ret = ANEG_DONE;
3649 break;
3650
3651 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
3652 /* ??? unimplemented */
3653 break;
3654
3655 case ANEG_STATE_NEXT_PAGE_WAIT:
3656 /* ??? unimplemented */
3657 break;
3658
3659 default:
3660 ret = ANEG_FAILED;
3661 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663
3664 return ret;
3665}
3666
Matt Carlson5be73b42007-12-20 20:09:29 -08003667static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668{
3669 int res = 0;
3670 struct tg3_fiber_aneginfo aninfo;
3671 int status = ANEG_FAILED;
3672 unsigned int tick;
3673 u32 tmp;
3674
3675 tw32_f(MAC_TX_AUTO_NEG, 0);
3676
3677 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
3678 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
3679 udelay(40);
3680
3681 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
3682 udelay(40);
3683
3684 memset(&aninfo, 0, sizeof(aninfo));
3685 aninfo.flags |= MR_AN_ENABLE;
3686 aninfo.state = ANEG_STATE_UNKNOWN;
3687 aninfo.cur_time = 0;
3688 tick = 0;
3689 while (++tick < 195000) {
3690 status = tg3_fiber_aneg_smachine(tp, &aninfo);
3691 if (status == ANEG_DONE || status == ANEG_FAILED)
3692 break;
3693
3694 udelay(1);
3695 }
3696
3697 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3698 tw32_f(MAC_MODE, tp->mac_mode);
3699 udelay(40);
3700
Matt Carlson5be73b42007-12-20 20:09:29 -08003701 *txflags = aninfo.txconfig;
3702 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703
3704 if (status == ANEG_DONE &&
3705 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
3706 MR_LP_ADV_FULL_DUPLEX)))
3707 res = 1;
3708
3709 return res;
3710}
3711
3712static void tg3_init_bcm8002(struct tg3 *tp)
3713{
3714 u32 mac_status = tr32(MAC_STATUS);
3715 int i;
3716
3717 /* Reset when initting first time or we have a link. */
3718 if ((tp->tg3_flags & TG3_FLAG_INIT_COMPLETE) &&
3719 !(mac_status & MAC_STATUS_PCS_SYNCED))
3720 return;
3721
3722 /* Set PLL lock range. */
3723 tg3_writephy(tp, 0x16, 0x8007);
3724
3725 /* SW reset */
3726 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
3727
3728 /* Wait for reset to complete. */
3729 /* XXX schedule_timeout() ... */
3730 for (i = 0; i < 500; i++)
3731 udelay(10);
3732
3733 /* Config mode; select PMA/Ch 1 regs. */
3734 tg3_writephy(tp, 0x10, 0x8411);
3735
3736 /* Enable auto-lock and comdet, select txclk for tx. */
3737 tg3_writephy(tp, 0x11, 0x0a10);
3738
3739 tg3_writephy(tp, 0x18, 0x00a0);
3740 tg3_writephy(tp, 0x16, 0x41ff);
3741
3742 /* Assert and deassert POR. */
3743 tg3_writephy(tp, 0x13, 0x0400);
3744 udelay(40);
3745 tg3_writephy(tp, 0x13, 0x0000);
3746
3747 tg3_writephy(tp, 0x11, 0x0a50);
3748 udelay(40);
3749 tg3_writephy(tp, 0x11, 0x0a10);
3750
3751 /* Wait for signal to stabilize */
3752 /* XXX schedule_timeout() ... */
3753 for (i = 0; i < 15000; i++)
3754 udelay(10);
3755
3756 /* Deselect the channel register so we can read the PHYID
3757 * later.
3758 */
3759 tg3_writephy(tp, 0x10, 0x8011);
3760}
3761
3762static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
3763{
Matt Carlson82cd3d12007-12-20 20:09:00 -08003764 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 u32 sg_dig_ctrl, sg_dig_status;
3766 u32 serdes_cfg, expected_sg_dig_ctrl;
3767 int workaround, port_a;
3768 int current_link_up;
3769
3770 serdes_cfg = 0;
3771 expected_sg_dig_ctrl = 0;
3772 workaround = 0;
3773 port_a = 1;
3774 current_link_up = 0;
3775
3776 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
3777 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
3778 workaround = 1;
3779 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
3780 port_a = 0;
3781
3782 /* preserve bits 0-11,13,14 for signal pre-emphasis */
3783 /* preserve bits 20-23 for voltage regulator */
3784 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
3785 }
3786
3787 sg_dig_ctrl = tr32(SG_DIG_CTRL);
3788
3789 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003790 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 if (workaround) {
3792 u32 val = serdes_cfg;
3793
3794 if (port_a)
3795 val |= 0xc010000;
3796 else
3797 val |= 0x4010000;
3798 tw32_f(MAC_SERDES_CFG, val);
3799 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003800
3801 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 }
3803 if (mac_status & MAC_STATUS_PCS_SYNCED) {
3804 tg3_setup_flow_control(tp, 0, 0);
3805 current_link_up = 1;
3806 }
3807 goto out;
3808 }
3809
3810 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003811 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812
Matt Carlson82cd3d12007-12-20 20:09:00 -08003813 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3814 if (flowctrl & ADVERTISE_1000XPAUSE)
3815 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
3816 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3817 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818
3819 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003820 if ((tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT) &&
3821 tp->serdes_counter &&
3822 ((mac_status & (MAC_STATUS_PCS_SYNCED |
3823 MAC_STATUS_RCVD_CFG)) ==
3824 MAC_STATUS_PCS_SYNCED)) {
3825 tp->serdes_counter--;
3826 current_link_up = 1;
3827 goto out;
3828 }
3829restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830 if (workaround)
3831 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003832 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 udelay(5);
3834 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
3835
Michael Chan3d3ebe72006-09-27 15:59:15 -07003836 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
3837 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
3839 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003840 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841 mac_status = tr32(MAC_STATUS);
3842
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003843 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08003845 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846
Matt Carlson82cd3d12007-12-20 20:09:00 -08003847 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
3848 local_adv |= ADVERTISE_1000XPAUSE;
3849 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
3850 local_adv |= ADVERTISE_1000XPSE_ASYM;
3851
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003852 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003853 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003854 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003855 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856
3857 tg3_setup_flow_control(tp, local_adv, remote_adv);
3858 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003859 tp->serdes_counter = 0;
3860 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003861 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003862 if (tp->serdes_counter)
3863 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864 else {
3865 if (workaround) {
3866 u32 val = serdes_cfg;
3867
3868 if (port_a)
3869 val |= 0xc010000;
3870 else
3871 val |= 0x4010000;
3872
3873 tw32_f(MAC_SERDES_CFG, val);
3874 }
3875
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003876 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877 udelay(40);
3878
3879 /* Link parallel detection - link is up */
3880 /* only if we have PCS_SYNC and not */
3881 /* receiving config code words */
3882 mac_status = tr32(MAC_STATUS);
3883 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
3884 !(mac_status & MAC_STATUS_RCVD_CFG)) {
3885 tg3_setup_flow_control(tp, 0, 0);
3886 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003887 tp->tg3_flags2 |=
3888 TG3_FLG2_PARALLEL_DETECT;
3889 tp->serdes_counter =
3890 SERDES_PARALLEL_DET_TIMEOUT;
3891 } else
3892 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 }
3894 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07003895 } else {
3896 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
3897 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898 }
3899
3900out:
3901 return current_link_up;
3902}
3903
3904static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
3905{
3906 int current_link_up = 0;
3907
Michael Chan5cf64b8a2007-05-05 12:11:21 -07003908 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910
3911 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08003912 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003914
Matt Carlson5be73b42007-12-20 20:09:29 -08003915 if (fiber_autoneg(tp, &txflags, &rxflags)) {
3916 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917
Matt Carlson5be73b42007-12-20 20:09:29 -08003918 if (txflags & ANEG_CFG_PS1)
3919 local_adv |= ADVERTISE_1000XPAUSE;
3920 if (txflags & ANEG_CFG_PS2)
3921 local_adv |= ADVERTISE_1000XPSE_ASYM;
3922
3923 if (rxflags & MR_LP_ADV_SYM_PAUSE)
3924 remote_adv |= LPA_1000XPAUSE;
3925 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
3926 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927
3928 tg3_setup_flow_control(tp, local_adv, remote_adv);
3929
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 current_link_up = 1;
3931 }
3932 for (i = 0; i < 30; i++) {
3933 udelay(20);
3934 tw32_f(MAC_STATUS,
3935 (MAC_STATUS_SYNC_CHANGED |
3936 MAC_STATUS_CFG_CHANGED));
3937 udelay(40);
3938 if ((tr32(MAC_STATUS) &
3939 (MAC_STATUS_SYNC_CHANGED |
3940 MAC_STATUS_CFG_CHANGED)) == 0)
3941 break;
3942 }
3943
3944 mac_status = tr32(MAC_STATUS);
3945 if (current_link_up == 0 &&
3946 (mac_status & MAC_STATUS_PCS_SYNCED) &&
3947 !(mac_status & MAC_STATUS_RCVD_CFG))
3948 current_link_up = 1;
3949 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08003950 tg3_setup_flow_control(tp, 0, 0);
3951
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 /* Forcing 1000FD link up. */
3953 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954
3955 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
3956 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003957
3958 tw32_f(MAC_MODE, tp->mac_mode);
3959 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960 }
3961
3962out:
3963 return current_link_up;
3964}
3965
3966static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
3967{
3968 u32 orig_pause_cfg;
3969 u16 orig_active_speed;
3970 u8 orig_active_duplex;
3971 u32 mac_status;
3972 int current_link_up;
3973 int i;
3974
Matt Carlson8d018622007-12-20 20:05:44 -08003975 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 orig_active_speed = tp->link_config.active_speed;
3977 orig_active_duplex = tp->link_config.active_duplex;
3978
3979 if (!(tp->tg3_flags2 & TG3_FLG2_HW_AUTONEG) &&
3980 netif_carrier_ok(tp->dev) &&
3981 (tp->tg3_flags & TG3_FLAG_INIT_COMPLETE)) {
3982 mac_status = tr32(MAC_STATUS);
3983 mac_status &= (MAC_STATUS_PCS_SYNCED |
3984 MAC_STATUS_SIGNAL_DET |
3985 MAC_STATUS_CFG_CHANGED |
3986 MAC_STATUS_RCVD_CFG);
3987 if (mac_status == (MAC_STATUS_PCS_SYNCED |
3988 MAC_STATUS_SIGNAL_DET)) {
3989 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
3990 MAC_STATUS_CFG_CHANGED));
3991 return 0;
3992 }
3993 }
3994
3995 tw32_f(MAC_TX_AUTO_NEG, 0);
3996
3997 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
3998 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
3999 tw32_f(MAC_MODE, tp->mac_mode);
4000 udelay(40);
4001
Matt Carlson79eb6902010-02-17 15:17:03 +00004002 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 tg3_init_bcm8002(tp);
4004
4005 /* Enable link change event even when serdes polling. */
4006 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4007 udelay(40);
4008
4009 current_link_up = 0;
4010 mac_status = tr32(MAC_STATUS);
4011
4012 if (tp->tg3_flags2 & TG3_FLG2_HW_AUTONEG)
4013 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
4014 else
4015 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
4016
Matt Carlson898a56f2009-08-28 14:02:40 +00004017 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00004019 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020
4021 for (i = 0; i < 100; i++) {
4022 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4023 MAC_STATUS_CFG_CHANGED));
4024 udelay(5);
4025 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07004026 MAC_STATUS_CFG_CHANGED |
4027 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 break;
4029 }
4030
4031 mac_status = tr32(MAC_STATUS);
4032 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
4033 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004034 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
4035 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 tw32_f(MAC_MODE, (tp->mac_mode |
4037 MAC_MODE_SEND_CONFIGS));
4038 udelay(1);
4039 tw32_f(MAC_MODE, tp->mac_mode);
4040 }
4041 }
4042
4043 if (current_link_up == 1) {
4044 tp->link_config.active_speed = SPEED_1000;
4045 tp->link_config.active_duplex = DUPLEX_FULL;
4046 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4047 LED_CTRL_LNKLED_OVERRIDE |
4048 LED_CTRL_1000MBPS_ON));
4049 } else {
4050 tp->link_config.active_speed = SPEED_INVALID;
4051 tp->link_config.active_duplex = DUPLEX_INVALID;
4052 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4053 LED_CTRL_LNKLED_OVERRIDE |
4054 LED_CTRL_TRAFFIC_OVERRIDE));
4055 }
4056
4057 if (current_link_up != netif_carrier_ok(tp->dev)) {
4058 if (current_link_up)
4059 netif_carrier_on(tp->dev);
4060 else
4061 netif_carrier_off(tp->dev);
4062 tg3_link_report(tp);
4063 } else {
Matt Carlson8d018622007-12-20 20:05:44 -08004064 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 if (orig_pause_cfg != now_pause_cfg ||
4066 orig_active_speed != tp->link_config.active_speed ||
4067 orig_active_duplex != tp->link_config.active_duplex)
4068 tg3_link_report(tp);
4069 }
4070
4071 return 0;
4072}
4073
Michael Chan747e8f82005-07-25 12:33:22 -07004074static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
4075{
4076 int current_link_up, err = 0;
4077 u32 bmsr, bmcr;
4078 u16 current_speed;
4079 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08004080 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07004081
4082 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4083 tw32_f(MAC_MODE, tp->mac_mode);
4084 udelay(40);
4085
4086 tw32(MAC_EVENT, 0);
4087
4088 tw32_f(MAC_STATUS,
4089 (MAC_STATUS_SYNC_CHANGED |
4090 MAC_STATUS_CFG_CHANGED |
4091 MAC_STATUS_MI_COMPLETION |
4092 MAC_STATUS_LNKSTATE_CHANGED));
4093 udelay(40);
4094
4095 if (force_reset)
4096 tg3_phy_reset(tp);
4097
4098 current_link_up = 0;
4099 current_speed = SPEED_INVALID;
4100 current_duplex = DUPLEX_INVALID;
4101
4102 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4103 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004104 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
4105 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4106 bmsr |= BMSR_LSTATUS;
4107 else
4108 bmsr &= ~BMSR_LSTATUS;
4109 }
Michael Chan747e8f82005-07-25 12:33:22 -07004110
4111 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
4112
4113 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlson2bd3ed02008-06-09 15:39:55 -07004114 (tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004115 /* do nothing, just check for link up at the end */
4116 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4117 u32 adv, new_adv;
4118
4119 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4120 new_adv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
4121 ADVERTISE_1000XPAUSE |
4122 ADVERTISE_1000XPSE_ASYM |
4123 ADVERTISE_SLCT);
4124
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004125 new_adv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Michael Chan747e8f82005-07-25 12:33:22 -07004126
4127 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
4128 new_adv |= ADVERTISE_1000XHALF;
4129 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
4130 new_adv |= ADVERTISE_1000XFULL;
4131
4132 if ((new_adv != adv) || !(bmcr & BMCR_ANENABLE)) {
4133 tg3_writephy(tp, MII_ADVERTISE, new_adv);
4134 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
4135 tg3_writephy(tp, MII_BMCR, bmcr);
4136
4137 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07004138 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Michael Chan747e8f82005-07-25 12:33:22 -07004139 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
4140
4141 return err;
4142 }
4143 } else {
4144 u32 new_bmcr;
4145
4146 bmcr &= ~BMCR_SPEED1000;
4147 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
4148
4149 if (tp->link_config.duplex == DUPLEX_FULL)
4150 new_bmcr |= BMCR_FULLDPLX;
4151
4152 if (new_bmcr != bmcr) {
4153 /* BMCR_SPEED1000 is a reserved bit that needs
4154 * to be set on write.
4155 */
4156 new_bmcr |= BMCR_SPEED1000;
4157
4158 /* Force a linkdown */
4159 if (netif_carrier_ok(tp->dev)) {
4160 u32 adv;
4161
4162 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4163 adv &= ~(ADVERTISE_1000XFULL |
4164 ADVERTISE_1000XHALF |
4165 ADVERTISE_SLCT);
4166 tg3_writephy(tp, MII_ADVERTISE, adv);
4167 tg3_writephy(tp, MII_BMCR, bmcr |
4168 BMCR_ANRESTART |
4169 BMCR_ANENABLE);
4170 udelay(10);
4171 netif_carrier_off(tp->dev);
4172 }
4173 tg3_writephy(tp, MII_BMCR, new_bmcr);
4174 bmcr = new_bmcr;
4175 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4176 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004177 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
4178 ASIC_REV_5714) {
4179 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4180 bmsr |= BMSR_LSTATUS;
4181 else
4182 bmsr &= ~BMSR_LSTATUS;
4183 }
Michael Chan747e8f82005-07-25 12:33:22 -07004184 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
4185 }
4186 }
4187
4188 if (bmsr & BMSR_LSTATUS) {
4189 current_speed = SPEED_1000;
4190 current_link_up = 1;
4191 if (bmcr & BMCR_FULLDPLX)
4192 current_duplex = DUPLEX_FULL;
4193 else
4194 current_duplex = DUPLEX_HALF;
4195
Matt Carlsonef167e22007-12-20 20:10:01 -08004196 local_adv = 0;
4197 remote_adv = 0;
4198
Michael Chan747e8f82005-07-25 12:33:22 -07004199 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08004200 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07004201
4202 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
4203 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
4204 common = local_adv & remote_adv;
4205 if (common & (ADVERTISE_1000XHALF |
4206 ADVERTISE_1000XFULL)) {
4207 if (common & ADVERTISE_1000XFULL)
4208 current_duplex = DUPLEX_FULL;
4209 else
4210 current_duplex = DUPLEX_HALF;
Matt Carlson57d8b882010-06-05 17:24:35 +00004211 } else if (!(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
4212 /* Link is up via parallel detect */
Matt Carlson859a588792010-04-05 10:19:28 +00004213 } else {
Michael Chan747e8f82005-07-25 12:33:22 -07004214 current_link_up = 0;
Matt Carlson859a588792010-04-05 10:19:28 +00004215 }
Michael Chan747e8f82005-07-25 12:33:22 -07004216 }
4217 }
4218
Matt Carlsonef167e22007-12-20 20:10:01 -08004219 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
4220 tg3_setup_flow_control(tp, local_adv, remote_adv);
4221
Michael Chan747e8f82005-07-25 12:33:22 -07004222 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4223 if (tp->link_config.active_duplex == DUPLEX_HALF)
4224 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4225
4226 tw32_f(MAC_MODE, tp->mac_mode);
4227 udelay(40);
4228
4229 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4230
4231 tp->link_config.active_speed = current_speed;
4232 tp->link_config.active_duplex = current_duplex;
4233
4234 if (current_link_up != netif_carrier_ok(tp->dev)) {
4235 if (current_link_up)
4236 netif_carrier_on(tp->dev);
4237 else {
4238 netif_carrier_off(tp->dev);
4239 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
4240 }
4241 tg3_link_report(tp);
4242 }
4243 return err;
4244}
4245
4246static void tg3_serdes_parallel_detect(struct tg3 *tp)
4247{
Michael Chan3d3ebe72006-09-27 15:59:15 -07004248 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07004249 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07004250 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07004251 return;
4252 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00004253
Michael Chan747e8f82005-07-25 12:33:22 -07004254 if (!netif_carrier_ok(tp->dev) &&
4255 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
4256 u32 bmcr;
4257
4258 tg3_readphy(tp, MII_BMCR, &bmcr);
4259 if (bmcr & BMCR_ANENABLE) {
4260 u32 phy1, phy2;
4261
4262 /* Select shadow register 0x1f */
4263 tg3_writephy(tp, 0x1c, 0x7c00);
4264 tg3_readphy(tp, 0x1c, &phy1);
4265
4266 /* Select expansion interrupt status register */
4267 tg3_writephy(tp, 0x17, 0x0f01);
4268 tg3_readphy(tp, 0x15, &phy2);
4269 tg3_readphy(tp, 0x15, &phy2);
4270
4271 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
4272 /* We have signal detect and not receiving
4273 * config code words, link is up by parallel
4274 * detection.
4275 */
4276
4277 bmcr &= ~BMCR_ANENABLE;
4278 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
4279 tg3_writephy(tp, MII_BMCR, bmcr);
4280 tp->tg3_flags2 |= TG3_FLG2_PARALLEL_DETECT;
4281 }
4282 }
Matt Carlson859a588792010-04-05 10:19:28 +00004283 } else if (netif_carrier_ok(tp->dev) &&
4284 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
4285 (tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004286 u32 phy2;
4287
4288 /* Select expansion interrupt status register */
4289 tg3_writephy(tp, 0x17, 0x0f01);
4290 tg3_readphy(tp, 0x15, &phy2);
4291 if (phy2 & 0x20) {
4292 u32 bmcr;
4293
4294 /* Config code words received, turn on autoneg. */
4295 tg3_readphy(tp, MII_BMCR, &bmcr);
4296 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
4297
4298 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
4299
4300 }
4301 }
4302}
4303
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304static int tg3_setup_phy(struct tg3 *tp, int force_reset)
4305{
4306 int err;
4307
Matt Carlson859a588792010-04-05 10:19:28 +00004308 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlson859a588792010-04-05 10:19:28 +00004310 else if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07004311 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a588792010-04-05 10:19:28 +00004312 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314
Matt Carlsonbcb37f62008-11-03 16:52:09 -08004315 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08004316 u32 val, scale;
4317
4318 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
4319 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
4320 scale = 65;
4321 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
4322 scale = 6;
4323 else
4324 scale = 12;
4325
4326 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
4327 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
4328 tw32(GRC_MISC_CFG, val);
4329 }
4330
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 if (tp->link_config.active_speed == SPEED_1000 &&
4332 tp->link_config.active_duplex == DUPLEX_HALF)
4333 tw32(MAC_TX_LENGTHS,
4334 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4335 (6 << TX_LENGTHS_IPG_SHIFT) |
4336 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
4337 else
4338 tw32(MAC_TX_LENGTHS,
4339 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4340 (6 << TX_LENGTHS_IPG_SHIFT) |
4341 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
4342
4343 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
4344 if (netif_carrier_ok(tp->dev)) {
4345 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07004346 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 } else {
4348 tw32(HOSTCC_STAT_COAL_TICKS, 0);
4349 }
4350 }
4351
Matt Carlson8ed5d972007-05-07 00:25:49 -07004352 if (tp->tg3_flags & TG3_FLAG_ASPM_WORKAROUND) {
4353 u32 val = tr32(PCIE_PWR_MGMT_THRESH);
4354 if (!netif_carrier_ok(tp->dev))
4355 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
4356 tp->pwrmgmt_thresh;
4357 else
4358 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
4359 tw32(PCIE_PWR_MGMT_THRESH, val);
4360 }
4361
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 return err;
4363}
4364
Michael Chandf3e6542006-05-26 17:48:07 -07004365/* This is called whenever we suspect that the system chipset is re-
4366 * ordering the sequence of MMIO to the tx send mailbox. The symptom
4367 * is bogus tx completions. We try to recover by setting the
4368 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
4369 * in the workqueue.
4370 */
4371static void tg3_tx_recover(struct tg3 *tp)
4372{
4373 BUG_ON((tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER) ||
4374 tp->write32_tx_mbox == tg3_write_indirect_mbox);
4375
Matt Carlson5129c3a2010-04-05 10:19:23 +00004376 netdev_warn(tp->dev,
4377 "The system may be re-ordering memory-mapped I/O "
4378 "cycles to the network device, attempting to recover. "
4379 "Please report the problem to the driver maintainer "
4380 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07004381
4382 spin_lock(&tp->lock);
Michael Chandf3e6542006-05-26 17:48:07 -07004383 tp->tg3_flags |= TG3_FLAG_TX_RECOVERY_PENDING;
Michael Chandf3e6542006-05-26 17:48:07 -07004384 spin_unlock(&tp->lock);
4385}
4386
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004387static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07004388{
Matt Carlsonf65aac12010-08-02 11:26:03 +00004389 /* Tell compiler to fetch tx indices from memory. */
4390 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004391 return tnapi->tx_pending -
4392 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07004393}
4394
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395/* Tigon3 never reports partial packet sends. So we do not
4396 * need special logic to handle SKBs that have not had all
4397 * of their frags sent yet, like SunGEM does.
4398 */
Matt Carlson17375d22009-08-28 14:02:18 +00004399static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400{
Matt Carlson17375d22009-08-28 14:02:18 +00004401 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00004402 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004403 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004404 struct netdev_queue *txq;
4405 int index = tnapi - tp->napi;
4406
Matt Carlson19cfaec2009-12-03 08:36:20 +00004407 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004408 index--;
4409
4410 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411
4412 while (sw_idx != hw_idx) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00004413 struct ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07004415 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416
Michael Chandf3e6542006-05-26 17:48:07 -07004417 if (unlikely(skb == NULL)) {
4418 tg3_tx_recover(tp);
4419 return;
4420 }
4421
Alexander Duyckf4188d82009-12-02 16:48:38 +00004422 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004423 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00004424 skb_headlen(skb),
4425 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426
4427 ri->skb = NULL;
4428
4429 sw_idx = NEXT_TX(sw_idx);
4430
4431 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004432 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07004433 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
4434 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00004435
4436 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004437 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00004438 skb_shinfo(skb)->frags[i].size,
4439 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 sw_idx = NEXT_TX(sw_idx);
4441 }
4442
David S. Millerf47c11e2005-06-24 20:18:35 -07004443 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07004444
4445 if (unlikely(tx_bug)) {
4446 tg3_tx_recover(tp);
4447 return;
4448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449 }
4450
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004451 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452
Michael Chan1b2a7202006-08-07 21:46:02 -07004453 /* Need to make the tx_cons update visible to tg3_start_xmit()
4454 * before checking for netif_queue_stopped(). Without the
4455 * memory barrier, there is a small possibility that tg3_start_xmit()
4456 * will miss it and cause the queue to be stopped forever.
4457 */
4458 smp_mb();
4459
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004460 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004461 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004462 __netif_tx_lock(txq, smp_processor_id());
4463 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004464 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004465 netif_tx_wake_queue(txq);
4466 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07004467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468}
4469
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004470static void tg3_rx_skb_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
4471{
4472 if (!ri->skb)
4473 return;
4474
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004475 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004476 map_sz, PCI_DMA_FROMDEVICE);
4477 dev_kfree_skb_any(ri->skb);
4478 ri->skb = NULL;
4479}
4480
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481/* Returns size of skb allocated or < 0 on error.
4482 *
4483 * We only need to fill in the address because the other members
4484 * of the RX descriptor are invariant, see tg3_init_rings.
4485 *
4486 * Note the purposeful assymetry of cpu vs. chip accesses. For
4487 * posting buffers we only dirty the first cache line of the RX
4488 * descriptor (containing the address). Whereas for the RX status
4489 * buffers the cpu only reads the last cacheline of the RX descriptor
4490 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
4491 */
Matt Carlson86b21e52009-11-13 13:03:45 +00004492static int tg3_alloc_rx_skb(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Matt Carlsona3896162009-11-13 13:03:44 +00004493 u32 opaque_key, u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494{
4495 struct tg3_rx_buffer_desc *desc;
4496 struct ring_info *map, *src_map;
4497 struct sk_buff *skb;
4498 dma_addr_t mapping;
4499 int skb_size, dest_idx;
4500
4501 src_map = NULL;
4502 switch (opaque_key) {
4503 case RXD_OPAQUE_RING_STD:
4504 dest_idx = dest_idx_unmasked % TG3_RX_RING_SIZE;
Matt Carlson21f581a2009-08-28 14:00:25 +00004505 desc = &tpr->rx_std[dest_idx];
4506 map = &tpr->rx_std_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004507 skb_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 break;
4509
4510 case RXD_OPAQUE_RING_JUMBO:
4511 dest_idx = dest_idx_unmasked % TG3_RX_JUMBO_RING_SIZE;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00004512 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00004513 map = &tpr->rx_jmb_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004514 skb_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 break;
4516
4517 default:
4518 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520
4521 /* Do not overwrite any of the map or rp information
4522 * until we are sure we can commit to a new buffer.
4523 *
4524 * Callers depend upon this behavior and assume that
4525 * we leave everything unchanged if we fail.
4526 */
Matt Carlson287be122009-08-28 13:58:46 +00004527 skb = netdev_alloc_skb(tp->dev, skb_size + tp->rx_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 if (skb == NULL)
4529 return -ENOMEM;
4530
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 skb_reserve(skb, tp->rx_offset);
4532
Matt Carlson287be122009-08-28 13:58:46 +00004533 mapping = pci_map_single(tp->pdev, skb->data, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 PCI_DMA_FROMDEVICE);
Matt Carlsona21771d2009-11-02 14:25:31 +00004535 if (pci_dma_mapping_error(tp->pdev, mapping)) {
4536 dev_kfree_skb(skb);
4537 return -EIO;
4538 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539
4540 map->skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004541 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543 desc->addr_hi = ((u64)mapping >> 32);
4544 desc->addr_lo = ((u64)mapping & 0xffffffff);
4545
4546 return skb_size;
4547}
4548
4549/* We only need to move over in the address because the other
4550 * members of the RX descriptor are invariant. See notes above
4551 * tg3_alloc_rx_skb for full details.
4552 */
Matt Carlsona3896162009-11-13 13:03:44 +00004553static void tg3_recycle_rx(struct tg3_napi *tnapi,
4554 struct tg3_rx_prodring_set *dpr,
4555 u32 opaque_key, int src_idx,
4556 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557{
Matt Carlson17375d22009-08-28 14:02:18 +00004558 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
4560 struct ring_info *src_map, *dest_map;
Matt Carlsona3896162009-11-13 13:03:44 +00004561 struct tg3_rx_prodring_set *spr = &tp->prodring[0];
Matt Carlsonc6cdf432010-04-05 10:19:26 +00004562 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563
4564 switch (opaque_key) {
4565 case RXD_OPAQUE_RING_STD:
4566 dest_idx = dest_idx_unmasked % TG3_RX_RING_SIZE;
Matt Carlsona3896162009-11-13 13:03:44 +00004567 dest_desc = &dpr->rx_std[dest_idx];
4568 dest_map = &dpr->rx_std_buffers[dest_idx];
4569 src_desc = &spr->rx_std[src_idx];
4570 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571 break;
4572
4573 case RXD_OPAQUE_RING_JUMBO:
4574 dest_idx = dest_idx_unmasked % TG3_RX_JUMBO_RING_SIZE;
Matt Carlsona3896162009-11-13 13:03:44 +00004575 dest_desc = &dpr->rx_jmb[dest_idx].std;
4576 dest_map = &dpr->rx_jmb_buffers[dest_idx];
4577 src_desc = &spr->rx_jmb[src_idx].std;
4578 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 break;
4580
4581 default:
4582 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584
4585 dest_map->skb = src_map->skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004586 dma_unmap_addr_set(dest_map, mapping,
4587 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588 dest_desc->addr_hi = src_desc->addr_hi;
4589 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00004590
4591 /* Ensure that the update to the skb happens after the physical
4592 * addresses have been transferred to the new BD location.
4593 */
4594 smp_wmb();
4595
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 src_map->skb = NULL;
4597}
4598
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599/* The RX ring scheme is composed of multiple rings which post fresh
4600 * buffers to the chip, and one special ring the chip uses to report
4601 * status back to the host.
4602 *
4603 * The special ring reports the status of received packets to the
4604 * host. The chip does not write into the original descriptor the
4605 * RX buffer was obtained from. The chip simply takes the original
4606 * descriptor as provided by the host, updates the status and length
4607 * field, then writes this into the next status ring entry.
4608 *
4609 * Each ring the host uses to post buffers to the chip is described
4610 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
4611 * it is first placed into the on-chip ram. When the packet's length
4612 * is known, it walks down the TG3_BDINFO entries to select the ring.
4613 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
4614 * which is within the range of the new packet's length is chosen.
4615 *
4616 * The "separate ring for rx status" scheme may sound queer, but it makes
4617 * sense from a cache coherency perspective. If only the host writes
4618 * to the buffer post rings, and only the chip writes to the rx status
4619 * rings, then cache lines never move beyond shared-modified state.
4620 * If both the host and chip were to write into the same ring, cache line
4621 * eviction could occur since both entities want it in an exclusive state.
4622 */
Matt Carlson17375d22009-08-28 14:02:18 +00004623static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624{
Matt Carlson17375d22009-08-28 14:02:18 +00004625 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07004626 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00004627 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00004628 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07004629 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 int received;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004631 struct tg3_rx_prodring_set *tpr = tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004633 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 /*
4635 * We need to order the read of hw_idx and the read of
4636 * the opaque cookie.
4637 */
4638 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 work_mask = 0;
4640 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00004641 std_prod_idx = tpr->rx_std_prod_idx;
4642 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00004644 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00004645 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 unsigned int len;
4647 struct sk_buff *skb;
4648 dma_addr_t dma_addr;
4649 u32 opaque_key, desc_idx, *post_ptr;
Matt Carlson9dc7a112010-04-12 06:58:28 +00004650 bool hw_vlan __maybe_unused = false;
4651 u16 vtag __maybe_unused = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652
4653 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
4654 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
4655 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004656 ri = &tp->prodring[0].rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004657 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00004658 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00004659 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07004660 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004662 ri = &tp->prodring[0].rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004663 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00004664 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00004665 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00004666 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668
4669 work_mask |= opaque_key;
4670
4671 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
4672 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
4673 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00004674 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675 desc_idx, *post_ptr);
4676 drop_it_no_recycle:
4677 /* Other statistics kept track of by card. */
4678 tp->net_stats.rx_dropped++;
4679 goto next_pkt;
4680 }
4681
Matt Carlsonad829262008-11-21 17:16:16 -08004682 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
4683 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684
Matt Carlsond2757fc2010-04-12 06:58:27 +00004685 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686 int skb_size;
4687
Matt Carlson86b21e52009-11-13 13:03:45 +00004688 skb_size = tg3_alloc_rx_skb(tp, tpr, opaque_key,
Matt Carlsonafc081f2009-11-13 13:03:43 +00004689 *post_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690 if (skb_size < 0)
4691 goto drop_it;
4692
Matt Carlson287be122009-08-28 13:58:46 +00004693 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694 PCI_DMA_FROMDEVICE);
4695
Matt Carlson61e800c2010-02-17 15:16:54 +00004696 /* Ensure that the update to the skb happens
4697 * after the usage of the old DMA mapping.
4698 */
4699 smp_wmb();
4700
4701 ri->skb = NULL;
4702
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 skb_put(skb, len);
4704 } else {
4705 struct sk_buff *copy_skb;
4706
Matt Carlsona3896162009-11-13 13:03:44 +00004707 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708 desc_idx, *post_ptr);
4709
Matt Carlson9dc7a112010-04-12 06:58:28 +00004710 copy_skb = netdev_alloc_skb(tp->dev, len + VLAN_HLEN +
4711 TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 if (copy_skb == NULL)
4713 goto drop_it_no_recycle;
4714
Matt Carlson9dc7a112010-04-12 06:58:28 +00004715 skb_reserve(copy_skb, TG3_RAW_IP_ALIGN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716 skb_put(copy_skb, len);
4717 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03004718 skb_copy_from_linear_data(skb, copy_skb->data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
4720
4721 /* We'll reuse the original ring buffer. */
4722 skb = copy_skb;
4723 }
4724
4725 if ((tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) &&
4726 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
4727 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
4728 >> RXD_TCPCSUM_SHIFT) == 0xffff))
4729 skb->ip_summed = CHECKSUM_UNNECESSARY;
4730 else
4731 skb->ip_summed = CHECKSUM_NONE;
4732
4733 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00004734
4735 if (len > (tp->dev->mtu + ETH_HLEN) &&
4736 skb->protocol != htons(ETH_P_8021Q)) {
4737 dev_kfree_skb(skb);
4738 goto next_pkt;
4739 }
4740
Matt Carlson9dc7a112010-04-12 06:58:28 +00004741 if (desc->type_flags & RXD_FLAG_VLAN &&
4742 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG)) {
4743 vtag = desc->err_vlan & RXD_VLAN_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744#if TG3_VLAN_TAG_USED
Matt Carlson9dc7a112010-04-12 06:58:28 +00004745 if (tp->vlgrp)
4746 hw_vlan = true;
4747 else
4748#endif
4749 {
4750 struct vlan_ethhdr *ve = (struct vlan_ethhdr *)
4751 __skb_push(skb, VLAN_HLEN);
4752
4753 memmove(ve, skb->data + VLAN_HLEN,
4754 ETH_ALEN * 2);
4755 ve->h_vlan_proto = htons(ETH_P_8021Q);
4756 ve->h_vlan_TCI = htons(vtag);
4757 }
4758 }
4759
4760#if TG3_VLAN_TAG_USED
4761 if (hw_vlan)
4762 vlan_gro_receive(&tnapi->napi, tp->vlgrp, vtag, skb);
4763 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764#endif
Matt Carlson17375d22009-08-28 14:02:18 +00004765 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767 received++;
4768 budget--;
4769
4770next_pkt:
4771 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07004772
4773 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson86cfe4f2010-01-12 10:11:37 +00004774 tpr->rx_std_prod_idx = std_prod_idx % TG3_RX_RING_SIZE;
4775 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
4776 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07004777 work_mask &= ~RXD_OPAQUE_RING_STD;
4778 rx_std_posted = 0;
4779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07004781 sw_idx++;
Eric Dumazet6b31a512007-02-06 13:29:21 -08004782 sw_idx &= (TG3_RX_RCB_RING_SIZE(tp) - 1);
Michael Chan52f6d692005-04-25 15:14:32 -07004783
4784 /* Refresh hw_idx to see if there is new work */
4785 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004786 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07004787 rmb();
4788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789 }
4790
4791 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00004792 tnapi->rx_rcb_ptr = sw_idx;
4793 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794
4795 /* Refill RX ring(s). */
Matt Carlsone4af1af2010-02-12 14:47:05 +00004796 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004797 if (work_mask & RXD_OPAQUE_RING_STD) {
4798 tpr->rx_std_prod_idx = std_prod_idx % TG3_RX_RING_SIZE;
4799 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
4800 tpr->rx_std_prod_idx);
4801 }
4802 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
4803 tpr->rx_jmb_prod_idx = jmb_prod_idx %
4804 TG3_RX_JUMBO_RING_SIZE;
4805 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
4806 tpr->rx_jmb_prod_idx);
4807 }
4808 mmiowb();
4809 } else if (work_mask) {
4810 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
4811 * updated before the producer indices can be updated.
4812 */
4813 smp_wmb();
4814
Matt Carlson43619352009-11-13 13:03:47 +00004815 tpr->rx_std_prod_idx = std_prod_idx % TG3_RX_RING_SIZE;
Matt Carlson43619352009-11-13 13:03:47 +00004816 tpr->rx_jmb_prod_idx = jmb_prod_idx % TG3_RX_JUMBO_RING_SIZE;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004817
Matt Carlsone4af1af2010-02-12 14:47:05 +00004818 if (tnapi != &tp->napi[1])
4819 napi_schedule(&tp->napi[1].napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821
4822 return received;
4823}
4824
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004825static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827 /* handle link change and other phy events */
4828 if (!(tp->tg3_flags &
4829 (TG3_FLAG_USE_LINKCHG_REG |
4830 TG3_FLAG_POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004831 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
4832
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833 if (sblk->status & SD_STATUS_LINK_CHG) {
4834 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004835 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07004836 spin_lock(&tp->lock);
Matt Carlsondd477002008-05-25 23:45:58 -07004837 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
4838 tw32_f(MAC_STATUS,
4839 (MAC_STATUS_SYNC_CHANGED |
4840 MAC_STATUS_CFG_CHANGED |
4841 MAC_STATUS_MI_COMPLETION |
4842 MAC_STATUS_LNKSTATE_CHANGED));
4843 udelay(40);
4844 } else
4845 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07004846 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 }
4848 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004849}
4850
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004851static int tg3_rx_prodring_xfer(struct tg3 *tp,
4852 struct tg3_rx_prodring_set *dpr,
4853 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004854{
4855 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004856 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004857
4858 while (1) {
4859 src_prod_idx = spr->rx_std_prod_idx;
4860
4861 /* Make sure updates to the rx_std_buffers[] entries and the
4862 * standard producer index are seen in the correct order.
4863 */
4864 smp_rmb();
4865
4866 if (spr->rx_std_cons_idx == src_prod_idx)
4867 break;
4868
4869 if (spr->rx_std_cons_idx < src_prod_idx)
4870 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
4871 else
4872 cpycnt = TG3_RX_RING_SIZE - spr->rx_std_cons_idx;
4873
4874 cpycnt = min(cpycnt, TG3_RX_RING_SIZE - dpr->rx_std_prod_idx);
4875
4876 si = spr->rx_std_cons_idx;
4877 di = dpr->rx_std_prod_idx;
4878
Matt Carlsone92967b2010-02-12 14:47:06 +00004879 for (i = di; i < di + cpycnt; i++) {
4880 if (dpr->rx_std_buffers[i].skb) {
4881 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004882 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00004883 break;
4884 }
4885 }
4886
4887 if (!cpycnt)
4888 break;
4889
4890 /* Ensure that updates to the rx_std_buffers ring and the
4891 * shadowed hardware producer ring from tg3_recycle_skb() are
4892 * ordered correctly WRT the skb check above.
4893 */
4894 smp_rmb();
4895
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004896 memcpy(&dpr->rx_std_buffers[di],
4897 &spr->rx_std_buffers[si],
4898 cpycnt * sizeof(struct ring_info));
4899
4900 for (i = 0; i < cpycnt; i++, di++, si++) {
4901 struct tg3_rx_buffer_desc *sbd, *dbd;
4902 sbd = &spr->rx_std[si];
4903 dbd = &dpr->rx_std[di];
4904 dbd->addr_hi = sbd->addr_hi;
4905 dbd->addr_lo = sbd->addr_lo;
4906 }
4907
4908 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) %
4909 TG3_RX_RING_SIZE;
4910 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) %
4911 TG3_RX_RING_SIZE;
4912 }
4913
4914 while (1) {
4915 src_prod_idx = spr->rx_jmb_prod_idx;
4916
4917 /* Make sure updates to the rx_jmb_buffers[] entries and
4918 * the jumbo producer index are seen in the correct order.
4919 */
4920 smp_rmb();
4921
4922 if (spr->rx_jmb_cons_idx == src_prod_idx)
4923 break;
4924
4925 if (spr->rx_jmb_cons_idx < src_prod_idx)
4926 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
4927 else
4928 cpycnt = TG3_RX_JUMBO_RING_SIZE - spr->rx_jmb_cons_idx;
4929
4930 cpycnt = min(cpycnt,
4931 TG3_RX_JUMBO_RING_SIZE - dpr->rx_jmb_prod_idx);
4932
4933 si = spr->rx_jmb_cons_idx;
4934 di = dpr->rx_jmb_prod_idx;
4935
Matt Carlsone92967b2010-02-12 14:47:06 +00004936 for (i = di; i < di + cpycnt; i++) {
4937 if (dpr->rx_jmb_buffers[i].skb) {
4938 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004939 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00004940 break;
4941 }
4942 }
4943
4944 if (!cpycnt)
4945 break;
4946
4947 /* Ensure that updates to the rx_jmb_buffers ring and the
4948 * shadowed hardware producer ring from tg3_recycle_skb() are
4949 * ordered correctly WRT the skb check above.
4950 */
4951 smp_rmb();
4952
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004953 memcpy(&dpr->rx_jmb_buffers[di],
4954 &spr->rx_jmb_buffers[si],
4955 cpycnt * sizeof(struct ring_info));
4956
4957 for (i = 0; i < cpycnt; i++, di++, si++) {
4958 struct tg3_rx_buffer_desc *sbd, *dbd;
4959 sbd = &spr->rx_jmb[si].std;
4960 dbd = &dpr->rx_jmb[di].std;
4961 dbd->addr_hi = sbd->addr_hi;
4962 dbd->addr_lo = sbd->addr_lo;
4963 }
4964
4965 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) %
4966 TG3_RX_JUMBO_RING_SIZE;
4967 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) %
4968 TG3_RX_JUMBO_RING_SIZE;
4969 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004970
4971 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004972}
4973
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004974static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
4975{
4976 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977
4978 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004979 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00004980 tg3_tx(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07004981 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
Michael Chan4fd7ab52007-10-12 01:39:50 -07004982 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983 }
4984
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985 /* run RX thread, within the bounds set by NAPI.
4986 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004987 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004989 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00004990 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004992 if ((tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00004993 struct tg3_rx_prodring_set *dpr = &tp->prodring[0];
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004994 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00004995 u32 std_prod_idx = dpr->rx_std_prod_idx;
4996 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004997
Matt Carlsone4af1af2010-02-12 14:47:05 +00004998 for (i = 1; i < tp->irq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004999 err |= tg3_rx_prodring_xfer(tp, dpr,
5000 tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005001
5002 wmb();
5003
Matt Carlsone4af1af2010-02-12 14:47:05 +00005004 if (std_prod_idx != dpr->rx_std_prod_idx)
5005 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5006 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005007
Matt Carlsone4af1af2010-02-12 14:47:05 +00005008 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
5009 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5010 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005011
5012 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005013
5014 if (err)
5015 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005016 }
5017
David S. Miller6f535762007-10-11 18:08:29 -07005018 return work_done;
5019}
David S. Millerf7383c22005-05-18 22:50:53 -07005020
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005021static int tg3_poll_msix(struct napi_struct *napi, int budget)
5022{
5023 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5024 struct tg3 *tp = tnapi->tp;
5025 int work_done = 0;
5026 struct tg3_hw_status *sblk = tnapi->hw_status;
5027
5028 while (1) {
5029 work_done = tg3_poll_work(tnapi, work_done, budget);
5030
5031 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
5032 goto tx_recovery;
5033
5034 if (unlikely(work_done >= budget))
5035 break;
5036
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005037 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005038 * to tell the hw how much work has been processed,
5039 * so we must read it before checking for more work.
5040 */
5041 tnapi->last_tag = sblk->status_tag;
5042 tnapi->last_irq_tag = tnapi->last_tag;
5043 rmb();
5044
5045 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00005046 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
5047 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005048 napi_complete(napi);
5049 /* Reenable interrupts. */
5050 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
5051 mmiowb();
5052 break;
5053 }
5054 }
5055
5056 return work_done;
5057
5058tx_recovery:
5059 /* work_done is guaranteed to be less than budget. */
5060 napi_complete(napi);
5061 schedule_work(&tp->reset_task);
5062 return work_done;
5063}
5064
David S. Miller6f535762007-10-11 18:08:29 -07005065static int tg3_poll(struct napi_struct *napi, int budget)
5066{
Matt Carlson8ef04422009-08-28 14:01:37 +00005067 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5068 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07005069 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00005070 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07005071
5072 while (1) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005073 tg3_poll_link(tp);
5074
Matt Carlson17375d22009-08-28 14:02:18 +00005075 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07005076
5077 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
5078 goto tx_recovery;
5079
5080 if (unlikely(work_done >= budget))
5081 break;
5082
Michael Chan4fd7ab52007-10-12 01:39:50 -07005083 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) {
Matt Carlson17375d22009-08-28 14:02:18 +00005084 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07005085 * to tell the hw how much work has been processed,
5086 * so we must read it before checking for more work.
5087 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005088 tnapi->last_tag = sblk->status_tag;
5089 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07005090 rmb();
5091 } else
5092 sblk->status &= ~SD_STATUS_UPDATED;
5093
Matt Carlson17375d22009-08-28 14:02:18 +00005094 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005095 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00005096 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07005097 break;
5098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099 }
5100
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005101 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07005102
5103tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07005104 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08005105 napi_complete(napi);
David S. Miller6f535762007-10-11 18:08:29 -07005106 schedule_work(&tp->reset_task);
Michael Chan4fd7ab52007-10-12 01:39:50 -07005107 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108}
5109
David S. Millerf47c11e2005-06-24 20:18:35 -07005110static void tg3_irq_quiesce(struct tg3 *tp)
5111{
Matt Carlson4f125f42009-09-01 12:55:02 +00005112 int i;
5113
David S. Millerf47c11e2005-06-24 20:18:35 -07005114 BUG_ON(tp->irq_sync);
5115
5116 tp->irq_sync = 1;
5117 smp_mb();
5118
Matt Carlson4f125f42009-09-01 12:55:02 +00005119 for (i = 0; i < tp->irq_cnt; i++)
5120 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07005121}
5122
5123static inline int tg3_irq_sync(struct tg3 *tp)
5124{
5125 return tp->irq_sync;
5126}
5127
5128/* Fully shutdown all tg3 driver activity elsewhere in the system.
5129 * If irq_sync is non-zero, then the IRQ handler must be synchronized
5130 * with as well. Most of the time, this is not necessary except when
5131 * shutting down the device.
5132 */
5133static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
5134{
Michael Chan46966542007-07-11 19:47:19 -07005135 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07005136 if (irq_sync)
5137 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07005138}
5139
5140static inline void tg3_full_unlock(struct tg3 *tp)
5141{
David S. Millerf47c11e2005-06-24 20:18:35 -07005142 spin_unlock_bh(&tp->lock);
5143}
5144
Michael Chanfcfa0a32006-03-20 22:28:41 -08005145/* One-shot MSI handler - Chip automatically disables interrupt
5146 * after sending MSI so driver doesn't have to do it.
5147 */
David Howells7d12e782006-10-05 14:55:46 +01005148static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08005149{
Matt Carlson09943a12009-08-28 14:01:57 +00005150 struct tg3_napi *tnapi = dev_id;
5151 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08005152
Matt Carlson898a56f2009-08-28 14:02:40 +00005153 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005154 if (tnapi->rx_rcb)
5155 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005156
5157 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005158 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005159
5160 return IRQ_HANDLED;
5161}
5162
Michael Chan88b06bc22005-04-21 17:13:25 -07005163/* MSI ISR - No need to check for interrupt sharing and no need to
5164 * flush status block and interrupt mailbox. PCI ordering rules
5165 * guarantee that MSI will arrive after the status block.
5166 */
David Howells7d12e782006-10-05 14:55:46 +01005167static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07005168{
Matt Carlson09943a12009-08-28 14:01:57 +00005169 struct tg3_napi *tnapi = dev_id;
5170 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07005171
Matt Carlson898a56f2009-08-28 14:02:40 +00005172 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005173 if (tnapi->rx_rcb)
5174 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07005175 /*
David S. Millerfac9b832005-05-18 22:46:34 -07005176 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07005177 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07005178 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07005179 * NIC to stop sending us irqs, engaging "in-intr-handler"
5180 * event coalescing.
5181 */
5182 tw32_mailbox(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07005183 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005184 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07005185
Michael Chan88b06bc22005-04-21 17:13:25 -07005186 return IRQ_RETVAL(1);
5187}
5188
David Howells7d12e782006-10-05 14:55:46 +01005189static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190{
Matt Carlson09943a12009-08-28 14:01:57 +00005191 struct tg3_napi *tnapi = dev_id;
5192 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005193 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194 unsigned int handled = 1;
5195
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196 /* In INTx mode, it is possible for the interrupt to arrive at
5197 * the CPU before the status block posted prior to the interrupt.
5198 * Reading the PCI State register will confirm whether the
5199 * interrupt is ours and will flush the status block.
5200 */
Michael Chand18edcb2007-03-24 20:57:11 -07005201 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
5202 if ((tp->tg3_flags & TG3_FLAG_CHIP_RESETTING) ||
5203 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5204 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005205 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07005206 }
Michael Chand18edcb2007-03-24 20:57:11 -07005207 }
5208
5209 /*
5210 * Writing any value to intr-mbox-0 clears PCI INTA# and
5211 * chip-internal interrupt pending events.
5212 * Writing non-zero to intr-mbox-0 additional tells the
5213 * NIC to stop sending us irqs, engaging "in-intr-handler"
5214 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005215 *
5216 * Flush the mailbox to de-assert the IRQ immediately to prevent
5217 * spurious interrupts. The flush impacts performance but
5218 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005219 */
Michael Chanc04cb342007-05-07 00:26:15 -07005220 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07005221 if (tg3_irq_sync(tp))
5222 goto out;
5223 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00005224 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00005225 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00005226 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07005227 } else {
5228 /* No work, shared interrupt perhaps? re-enable
5229 * interrupts, and flush that PCI write
5230 */
5231 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
5232 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07005233 }
David S. Millerf47c11e2005-06-24 20:18:35 -07005234out:
David S. Millerfac9b832005-05-18 22:46:34 -07005235 return IRQ_RETVAL(handled);
5236}
5237
David Howells7d12e782006-10-05 14:55:46 +01005238static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07005239{
Matt Carlson09943a12009-08-28 14:01:57 +00005240 struct tg3_napi *tnapi = dev_id;
5241 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005242 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07005243 unsigned int handled = 1;
5244
David S. Millerfac9b832005-05-18 22:46:34 -07005245 /* In INTx mode, it is possible for the interrupt to arrive at
5246 * the CPU before the status block posted prior to the interrupt.
5247 * Reading the PCI State register will confirm whether the
5248 * interrupt is ours and will flush the status block.
5249 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005250 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Michael Chand18edcb2007-03-24 20:57:11 -07005251 if ((tp->tg3_flags & TG3_FLAG_CHIP_RESETTING) ||
5252 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5253 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005254 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255 }
Michael Chand18edcb2007-03-24 20:57:11 -07005256 }
5257
5258 /*
5259 * writing any value to intr-mbox-0 clears PCI INTA# and
5260 * chip-internal interrupt pending events.
5261 * writing non-zero to intr-mbox-0 additional tells the
5262 * NIC to stop sending us irqs, engaging "in-intr-handler"
5263 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005264 *
5265 * Flush the mailbox to de-assert the IRQ immediately to prevent
5266 * spurious interrupts. The flush impacts performance but
5267 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005268 */
Michael Chanc04cb342007-05-07 00:26:15 -07005269 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00005270
5271 /*
5272 * In a shared interrupt configuration, sometimes other devices'
5273 * interrupts will scream. We record the current status tag here
5274 * so that the above check can report that the screaming interrupts
5275 * are unhandled. Eventually they will be silenced.
5276 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005277 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00005278
Michael Chand18edcb2007-03-24 20:57:11 -07005279 if (tg3_irq_sync(tp))
5280 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00005281
Matt Carlson72334482009-08-28 14:03:01 +00005282 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00005283
Matt Carlson09943a12009-08-28 14:01:57 +00005284 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00005285
David S. Millerf47c11e2005-06-24 20:18:35 -07005286out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287 return IRQ_RETVAL(handled);
5288}
5289
Michael Chan79381092005-04-21 17:13:59 -07005290/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01005291static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07005292{
Matt Carlson09943a12009-08-28 14:01:57 +00005293 struct tg3_napi *tnapi = dev_id;
5294 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005295 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07005296
Michael Chanf9804dd2005-09-27 12:13:10 -07005297 if ((sblk->status & SD_STATUS_UPDATED) ||
5298 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07005299 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07005300 return IRQ_RETVAL(1);
5301 }
5302 return IRQ_RETVAL(0);
5303}
5304
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07005305static int tg3_init_hw(struct tg3 *, int);
Michael Chan944d9802005-05-29 14:57:48 -07005306static int tg3_halt(struct tg3 *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307
Michael Chanb9ec6c12006-07-25 16:37:27 -07005308/* Restart hardware after configuration changes, self-test, etc.
5309 * Invoked with tp->lock held.
5310 */
5311static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
Eric Dumazet78c61462008-04-24 23:33:06 -07005312 __releases(tp->lock)
5313 __acquires(tp->lock)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005314{
5315 int err;
5316
5317 err = tg3_init_hw(tp, reset_phy);
5318 if (err) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00005319 netdev_err(tp->dev,
5320 "Failed to re-initialize device, aborting\n");
Michael Chanb9ec6c12006-07-25 16:37:27 -07005321 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
5322 tg3_full_unlock(tp);
5323 del_timer_sync(&tp->timer);
5324 tp->irq_sync = 0;
Matt Carlsonfed97812009-09-01 13:10:19 +00005325 tg3_napi_enable(tp);
Michael Chanb9ec6c12006-07-25 16:37:27 -07005326 dev_close(tp->dev);
5327 tg3_full_lock(tp, 0);
5328 }
5329 return err;
5330}
5331
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332#ifdef CONFIG_NET_POLL_CONTROLLER
5333static void tg3_poll_controller(struct net_device *dev)
5334{
Matt Carlson4f125f42009-09-01 12:55:02 +00005335 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07005336 struct tg3 *tp = netdev_priv(dev);
5337
Matt Carlson4f125f42009-09-01 12:55:02 +00005338 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00005339 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340}
5341#endif
5342
David Howellsc4028952006-11-22 14:57:56 +00005343static void tg3_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344{
David Howellsc4028952006-11-22 14:57:56 +00005345 struct tg3 *tp = container_of(work, struct tg3, reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005346 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347 unsigned int restart_timer;
5348
Michael Chan7faa0062006-02-02 17:29:28 -08005349 tg3_full_lock(tp, 0);
Michael Chan7faa0062006-02-02 17:29:28 -08005350
5351 if (!netif_running(tp->dev)) {
Michael Chan7faa0062006-02-02 17:29:28 -08005352 tg3_full_unlock(tp);
5353 return;
5354 }
5355
5356 tg3_full_unlock(tp);
5357
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005358 tg3_phy_stop(tp);
5359
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360 tg3_netif_stop(tp);
5361
David S. Millerf47c11e2005-06-24 20:18:35 -07005362 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363
5364 restart_timer = tp->tg3_flags2 & TG3_FLG2_RESTART_TIMER;
5365 tp->tg3_flags2 &= ~TG3_FLG2_RESTART_TIMER;
5366
Michael Chandf3e6542006-05-26 17:48:07 -07005367 if (tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING) {
5368 tp->write32_tx_mbox = tg3_write32_tx_mbox;
5369 tp->write32_rx_mbox = tg3_write_flush_reg32;
5370 tp->tg3_flags |= TG3_FLAG_MBOX_WRITE_REORDER;
5371 tp->tg3_flags &= ~TG3_FLAG_TX_RECOVERY_PENDING;
5372 }
5373
Michael Chan944d9802005-05-29 14:57:48 -07005374 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005375 err = tg3_init_hw(tp, 1);
5376 if (err)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005377 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378
5379 tg3_netif_start(tp);
5380
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381 if (restart_timer)
5382 mod_timer(&tp->timer, jiffies + 1);
Michael Chan7faa0062006-02-02 17:29:28 -08005383
Michael Chanb9ec6c12006-07-25 16:37:27 -07005384out:
Michael Chan7faa0062006-02-02 17:29:28 -08005385 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005386
5387 if (!err)
5388 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389}
5390
Michael Chanb0408752007-02-13 12:18:30 -08005391static void tg3_dump_short_state(struct tg3 *tp)
5392{
Joe Perches05dbe002010-02-17 19:44:19 +00005393 netdev_err(tp->dev, "DEBUG: MAC_TX_STATUS[%08x] MAC_RX_STATUS[%08x]\n",
5394 tr32(MAC_TX_STATUS), tr32(MAC_RX_STATUS));
5395 netdev_err(tp->dev, "DEBUG: RDMAC_STATUS[%08x] WDMAC_STATUS[%08x]\n",
5396 tr32(RDMAC_STATUS), tr32(WDMAC_STATUS));
Michael Chanb0408752007-02-13 12:18:30 -08005397}
5398
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399static void tg3_tx_timeout(struct net_device *dev)
5400{
5401 struct tg3 *tp = netdev_priv(dev);
5402
Michael Chanb0408752007-02-13 12:18:30 -08005403 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00005404 netdev_err(dev, "transmit timed out, resetting\n");
Michael Chanb0408752007-02-13 12:18:30 -08005405 tg3_dump_short_state(tp);
5406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407
5408 schedule_work(&tp->reset_task);
5409}
5410
Michael Chanc58ec932005-09-17 00:46:27 -07005411/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
5412static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
5413{
5414 u32 base = (u32) mapping & 0xffffffff;
5415
5416 return ((base > 0xffffdcc0) &&
5417 (base + len + 8 < base));
5418}
5419
Michael Chan72f2afb2006-03-06 19:28:35 -08005420/* Test for DMA addresses > 40-bit */
5421static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
5422 int len)
5423{
5424#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Michael Chan6728a8e2006-03-27 23:16:49 -08005425 if (tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG)
Yang Hongyang50cf1562009-04-06 19:01:14 -07005426 return (((u64) mapping + len) > DMA_BIT_MASK(40));
Michael Chan72f2afb2006-03-06 19:28:35 -08005427 return 0;
5428#else
5429 return 0;
5430#endif
5431}
5432
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005433static void tg3_set_txd(struct tg3_napi *, int, dma_addr_t, int, u32, u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434
Michael Chan72f2afb2006-03-06 19:28:35 -08005435/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00005436static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
5437 struct sk_buff *skb, u32 last_plus_one,
5438 u32 *start, u32 base_flags, u32 mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439{
Matt Carlson24f4efd2009-11-13 13:03:35 +00005440 struct tg3 *tp = tnapi->tp;
Matt Carlson41588ba2008-04-19 18:12:33 -07005441 struct sk_buff *new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07005442 dma_addr_t new_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443 u32 entry = *start;
Michael Chanc58ec932005-09-17 00:46:27 -07005444 int i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445
Matt Carlson41588ba2008-04-19 18:12:33 -07005446 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
5447 new_skb = skb_copy(skb, GFP_ATOMIC);
5448 else {
5449 int more_headroom = 4 - ((unsigned long)skb->data & 3);
5450
5451 new_skb = skb_copy_expand(skb,
5452 skb_headroom(skb) + more_headroom,
5453 skb_tailroom(skb), GFP_ATOMIC);
5454 }
5455
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07005457 ret = -1;
5458 } else {
5459 /* New SKB is guaranteed to be linear. */
5460 entry = *start;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005461 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
5462 PCI_DMA_TODEVICE);
5463 /* Make sure the mapping succeeded */
5464 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
5465 ret = -1;
5466 dev_kfree_skb(new_skb);
5467 new_skb = NULL;
David S. Miller90079ce2008-09-11 04:52:51 -07005468
Michael Chanc58ec932005-09-17 00:46:27 -07005469 /* Make sure new skb does not cross any 4G boundaries.
5470 * Drop the packet if it does.
5471 */
Alexander Duyckf4188d82009-12-02 16:48:38 +00005472 } else if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
5473 tg3_4g_overflow_test(new_addr, new_skb->len)) {
5474 pci_unmap_single(tp->pdev, new_addr, new_skb->len,
5475 PCI_DMA_TODEVICE);
Michael Chanc58ec932005-09-17 00:46:27 -07005476 ret = -1;
5477 dev_kfree_skb(new_skb);
5478 new_skb = NULL;
5479 } else {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005480 tg3_set_txd(tnapi, entry, new_addr, new_skb->len,
Michael Chanc58ec932005-09-17 00:46:27 -07005481 base_flags, 1 | (mss << 1));
5482 *start = NEXT_TX(entry);
5483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484 }
5485
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486 /* Now clean up the sw ring entries. */
5487 i = 0;
5488 while (entry != last_plus_one) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00005489 int len;
5490
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005491 if (i == 0)
Alexander Duyckf4188d82009-12-02 16:48:38 +00005492 len = skb_headlen(skb);
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005493 else
Alexander Duyckf4188d82009-12-02 16:48:38 +00005494 len = skb_shinfo(skb)->frags[i-1].size;
5495
5496 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005497 dma_unmap_addr(&tnapi->tx_buffers[entry],
Alexander Duyckf4188d82009-12-02 16:48:38 +00005498 mapping),
5499 len, PCI_DMA_TODEVICE);
5500 if (i == 0) {
5501 tnapi->tx_buffers[entry].skb = new_skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005502 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00005503 new_addr);
5504 } else {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005505 tnapi->tx_buffers[entry].skb = NULL;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507 entry = NEXT_TX(entry);
5508 i++;
5509 }
5510
5511 dev_kfree_skb(skb);
5512
Michael Chanc58ec932005-09-17 00:46:27 -07005513 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514}
5515
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005516static void tg3_set_txd(struct tg3_napi *tnapi, int entry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517 dma_addr_t mapping, int len, u32 flags,
5518 u32 mss_and_is_end)
5519{
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005520 struct tg3_tx_buffer_desc *txd = &tnapi->tx_ring[entry];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521 int is_end = (mss_and_is_end & 0x1);
5522 u32 mss = (mss_and_is_end >> 1);
5523 u32 vlan_tag = 0;
5524
5525 if (is_end)
5526 flags |= TXD_FLAG_END;
5527 if (flags & TXD_FLAG_VLAN) {
5528 vlan_tag = flags >> 16;
5529 flags &= 0xffff;
5530 }
5531 vlan_tag |= (mss << TXD_MSS_SHIFT);
5532
5533 txd->addr_hi = ((u64) mapping >> 32);
5534 txd->addr_lo = ((u64) mapping & 0xffffffff);
5535 txd->len_flags = (len << TXD_LEN_SHIFT) | flags;
5536 txd->vlan_tag = vlan_tag << TXD_VLAN_TAG_SHIFT;
5537}
5538
Michael Chan5a6f3072006-03-20 22:28:05 -08005539/* hard_start_xmit for devices that don't have any bugs and
Matt Carlsone849cdc2009-11-13 13:03:38 +00005540 * support TG3_FLG2_HW_TSO_2 and TG3_FLG2_HW_TSO_3 only.
Michael Chan5a6f3072006-03-20 22:28:05 -08005541 */
Stephen Hemminger613573252009-08-31 19:50:58 +00005542static netdev_tx_t tg3_start_xmit(struct sk_buff *skb,
5543 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544{
5545 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546 u32 len, entry, base_flags, mss;
David S. Miller90079ce2008-09-11 04:52:51 -07005547 dma_addr_t mapping;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005548 struct tg3_napi *tnapi;
5549 struct netdev_queue *txq;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005550 unsigned int i, last;
5551
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005552 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
5553 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Matt Carlson19cfaec2009-12-03 08:36:20 +00005554 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005555 tnapi++;
Michael Chan5a6f3072006-03-20 22:28:05 -08005556
Michael Chan00b70502006-06-17 21:58:45 -07005557 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005558 * and TX reclaim runs via tp->napi.poll inside of a software
Michael Chan5a6f3072006-03-20 22:28:05 -08005559 * interrupt. Furthermore, IRQ processing runs lockless so we have
5560 * no IRQ context deadlocks to worry about either. Rejoice!
5561 */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005562 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005563 if (!netif_tx_queue_stopped(txq)) {
5564 netif_tx_stop_queue(txq);
Michael Chan5a6f3072006-03-20 22:28:05 -08005565
5566 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00005567 netdev_err(dev,
5568 "BUG! Tx Ring full when queue awake!\n");
Michael Chan5a6f3072006-03-20 22:28:05 -08005569 }
Michael Chan5a6f3072006-03-20 22:28:05 -08005570 return NETDEV_TX_BUSY;
5571 }
5572
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005573 entry = tnapi->tx_prod;
Michael Chan5a6f3072006-03-20 22:28:05 -08005574 base_flags = 0;
Matt Carlsonbe98da62010-07-11 09:31:46 +00005575 mss = skb_shinfo(skb)->gso_size;
5576 if (mss) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005577 int tcp_opt_len, ip_tcp_len;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005578 u32 hdrlen;
Michael Chan5a6f3072006-03-20 22:28:05 -08005579
5580 if (skb_header_cloned(skb) &&
5581 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
5582 dev_kfree_skb(skb);
5583 goto out_unlock;
5584 }
5585
Michael Chanb0026622006-07-03 19:42:14 -07005586 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005587 hdrlen = skb_headlen(skb) - ETH_HLEN;
Michael Chanb0026622006-07-03 19:42:14 -07005588 else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005589 struct iphdr *iph = ip_hdr(skb);
5590
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07005591 tcp_opt_len = tcp_optlen(skb);
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -03005592 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
Michael Chanb0026622006-07-03 19:42:14 -07005593
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005594 iph->check = 0;
5595 iph->tot_len = htons(mss + ip_tcp_len + tcp_opt_len);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005596 hdrlen = ip_tcp_len + tcp_opt_len;
Michael Chanb0026622006-07-03 19:42:14 -07005597 }
Michael Chan5a6f3072006-03-20 22:28:05 -08005598
Matt Carlsone849cdc2009-11-13 13:03:38 +00005599 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005600 mss |= (hdrlen & 0xc) << 12;
5601 if (hdrlen & 0x10)
5602 base_flags |= 0x00000010;
5603 base_flags |= (hdrlen & 0x3e0) << 5;
5604 } else
5605 mss |= hdrlen << 9;
5606
Michael Chan5a6f3072006-03-20 22:28:05 -08005607 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
5608 TXD_FLAG_CPU_POST_DMA);
5609
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005610 tcp_hdr(skb)->check = 0;
Michael Chan5a6f3072006-03-20 22:28:05 -08005611
Matt Carlson859a588792010-04-05 10:19:28 +00005612 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005613 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson859a588792010-04-05 10:19:28 +00005614 }
5615
Michael Chan5a6f3072006-03-20 22:28:05 -08005616#if TG3_VLAN_TAG_USED
5617 if (tp->vlgrp != NULL && vlan_tx_tag_present(skb))
5618 base_flags |= (TXD_FLAG_VLAN |
5619 (vlan_tx_tag_get(skb) << 16));
5620#endif
5621
Alexander Duyckf4188d82009-12-02 16:48:38 +00005622 len = skb_headlen(skb);
5623
5624 /* Queue skb data, a.k.a. the main skb fragment. */
5625 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
5626 if (pci_dma_mapping_error(tp->pdev, mapping)) {
David S. Miller90079ce2008-09-11 04:52:51 -07005627 dev_kfree_skb(skb);
5628 goto out_unlock;
5629 }
5630
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005631 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005632 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005633
Matt Carlsonb703df62009-12-03 08:36:21 +00005634 if ((tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005635 !mss && skb->len > ETH_DATA_LEN)
5636 base_flags |= TXD_FLAG_JMB_PKT;
5637
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005638 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
Michael Chan5a6f3072006-03-20 22:28:05 -08005639 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
5640
5641 entry = NEXT_TX(entry);
5642
5643 /* Now loop through additional data fragments, and queue them. */
5644 if (skb_shinfo(skb)->nr_frags > 0) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005645 last = skb_shinfo(skb)->nr_frags - 1;
5646 for (i = 0; i <= last; i++) {
5647 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5648
5649 len = frag->size;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005650 mapping = pci_map_page(tp->pdev,
5651 frag->page,
5652 frag->page_offset,
5653 len, PCI_DMA_TODEVICE);
5654 if (pci_dma_mapping_error(tp->pdev, mapping))
5655 goto dma_error;
5656
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005657 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005658 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00005659 mapping);
Michael Chan5a6f3072006-03-20 22:28:05 -08005660
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005661 tg3_set_txd(tnapi, entry, mapping, len,
Michael Chan5a6f3072006-03-20 22:28:05 -08005662 base_flags, (i == last) | (mss << 1));
5663
5664 entry = NEXT_TX(entry);
5665 }
5666 }
5667
5668 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005669 tw32_tx_mbox(tnapi->prodmbox, entry);
Michael Chan5a6f3072006-03-20 22:28:05 -08005670
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005671 tnapi->tx_prod = entry;
5672 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005673 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00005674
5675 /* netif_tx_stop_queue() must be done before checking
5676 * checking tx index in tg3_tx_avail() below, because in
5677 * tg3_tx(), we update tx index before checking for
5678 * netif_tx_queue_stopped().
5679 */
5680 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005681 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005682 netif_tx_wake_queue(txq);
Michael Chan5a6f3072006-03-20 22:28:05 -08005683 }
5684
5685out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00005686 mmiowb();
Michael Chan5a6f3072006-03-20 22:28:05 -08005687
5688 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005689
5690dma_error:
5691 last = i;
5692 entry = tnapi->tx_prod;
5693 tnapi->tx_buffers[entry].skb = NULL;
5694 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005695 dma_unmap_addr(&tnapi->tx_buffers[entry], mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005696 skb_headlen(skb),
5697 PCI_DMA_TODEVICE);
5698 for (i = 0; i <= last; i++) {
5699 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5700 entry = NEXT_TX(entry);
5701
5702 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005703 dma_unmap_addr(&tnapi->tx_buffers[entry],
Alexander Duyckf4188d82009-12-02 16:48:38 +00005704 mapping),
5705 frag->size, PCI_DMA_TODEVICE);
5706 }
5707
5708 dev_kfree_skb(skb);
5709 return NETDEV_TX_OK;
Michael Chan5a6f3072006-03-20 22:28:05 -08005710}
5711
Stephen Hemminger613573252009-08-31 19:50:58 +00005712static netdev_tx_t tg3_start_xmit_dma_bug(struct sk_buff *,
5713 struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07005714
5715/* Use GSO to workaround a rare TSO bug that may be triggered when the
5716 * TSO header is greater than 80 bytes.
5717 */
5718static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
5719{
5720 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005721 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07005722
5723 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005724 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07005725 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00005726
5727 /* netif_tx_stop_queue() must be done before checking
5728 * checking tx index in tg3_tx_avail() below, because in
5729 * tg3_tx(), we update tx index before checking for
5730 * netif_tx_queue_stopped().
5731 */
5732 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005733 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08005734 return NETDEV_TX_BUSY;
5735
5736 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07005737 }
5738
5739 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07005740 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07005741 goto tg3_tso_bug_end;
5742
5743 do {
5744 nskb = segs;
5745 segs = segs->next;
5746 nskb->next = NULL;
5747 tg3_start_xmit_dma_bug(nskb, tp->dev);
5748 } while (segs);
5749
5750tg3_tso_bug_end:
5751 dev_kfree_skb(skb);
5752
5753 return NETDEV_TX_OK;
5754}
Michael Chan52c0fd82006-06-29 20:15:54 -07005755
Michael Chan5a6f3072006-03-20 22:28:05 -08005756/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
5757 * support TG3_FLG2_HW_TSO_1 or firmware TSO only.
5758 */
Stephen Hemminger613573252009-08-31 19:50:58 +00005759static netdev_tx_t tg3_start_xmit_dma_bug(struct sk_buff *skb,
5760 struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08005761{
5762 struct tg3 *tp = netdev_priv(dev);
Michael Chan5a6f3072006-03-20 22:28:05 -08005763 u32 len, entry, base_flags, mss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764 int would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07005765 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00005766 struct tg3_napi *tnapi;
5767 struct netdev_queue *txq;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005768 unsigned int i, last;
5769
Matt Carlson24f4efd2009-11-13 13:03:35 +00005770 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
5771 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Matt Carlson19cfaec2009-12-03 08:36:20 +00005772 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlson24f4efd2009-11-13 13:03:35 +00005773 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774
Michael Chan00b70502006-06-17 21:58:45 -07005775 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005776 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07005777 * interrupt. Furthermore, IRQ processing runs lockless so we have
5778 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779 */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005780 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00005781 if (!netif_tx_queue_stopped(txq)) {
5782 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08005783
5784 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00005785 netdev_err(dev,
5786 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08005787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788 return NETDEV_TX_BUSY;
5789 }
5790
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005791 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07005793 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005794 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00005795
Matt Carlsonbe98da62010-07-11 09:31:46 +00005796 mss = skb_shinfo(skb)->gso_size;
5797 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005798 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00005799 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800
5801 if (skb_header_cloned(skb) &&
5802 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
5803 dev_kfree_skb(skb);
5804 goto out_unlock;
5805 }
5806
Matt Carlson34195c32010-07-11 09:31:42 +00005807 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07005808 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809
Matt Carlson34195c32010-07-11 09:31:42 +00005810 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) {
5811 hdr_len = skb_headlen(skb) - ETH_HLEN;
5812 } else {
5813 u32 ip_tcp_len;
5814
5815 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
5816 hdr_len = ip_tcp_len + tcp_opt_len;
5817
5818 iph->check = 0;
5819 iph->tot_len = htons(mss + hdr_len);
5820 }
5821
Michael Chan52c0fd82006-06-29 20:15:54 -07005822 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Michael Chan7f62ad52007-02-20 23:25:40 -08005823 (tp->tg3_flags2 & TG3_FLG2_TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00005824 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07005825
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
5827 TXD_FLAG_CPU_POST_DMA);
5828
Linus Torvalds1da177e2005-04-16 15:20:36 -07005829 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005830 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005832 } else
5833 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
5834 iph->daddr, 0,
5835 IPPROTO_TCP,
5836 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005837
Matt Carlson615774f2009-11-13 13:03:39 +00005838 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) {
5839 mss |= (hdr_len & 0xc) << 12;
5840 if (hdr_len & 0x10)
5841 base_flags |= 0x00000010;
5842 base_flags |= (hdr_len & 0x3e0) << 5;
5843 } else if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_2)
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005844 mss |= hdr_len << 9;
5845 else if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_1) ||
5846 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005847 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848 int tsflags;
5849
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005850 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851 mss |= (tsflags << 11);
5852 }
5853 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005854 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855 int tsflags;
5856
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005857 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005858 base_flags |= tsflags << 12;
5859 }
5860 }
5861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005862#if TG3_VLAN_TAG_USED
5863 if (tp->vlgrp != NULL && vlan_tx_tag_present(skb))
5864 base_flags |= (TXD_FLAG_VLAN |
5865 (vlan_tx_tag_get(skb) << 16));
5866#endif
5867
Matt Carlsonb703df62009-12-03 08:36:21 +00005868 if ((tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) &&
Matt Carlson615774f2009-11-13 13:03:39 +00005869 !mss && skb->len > ETH_DATA_LEN)
5870 base_flags |= TXD_FLAG_JMB_PKT;
5871
Alexander Duyckf4188d82009-12-02 16:48:38 +00005872 len = skb_headlen(skb);
5873
5874 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
5875 if (pci_dma_mapping_error(tp->pdev, mapping)) {
David S. Miller90079ce2008-09-11 04:52:51 -07005876 dev_kfree_skb(skb);
5877 goto out_unlock;
5878 }
5879
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005880 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005881 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882
5883 would_hit_hwbug = 0;
5884
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005885 if ((tp->tg3_flags3 & TG3_FLG3_SHORT_DMA_BUG) && len <= 8)
5886 would_hit_hwbug = 1;
5887
Matt Carlson0e1406d2009-11-02 12:33:33 +00005888 if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
5889 tg3_4g_overflow_test(mapping, len))
Matt Carlson41588ba2008-04-19 18:12:33 -07005890 would_hit_hwbug = 1;
Matt Carlson0e1406d2009-11-02 12:33:33 +00005891
5892 if ((tp->tg3_flags3 & TG3_FLG3_40BIT_DMA_LIMIT_BUG) &&
5893 tg3_40bit_overflow_test(tp, mapping, len))
5894 would_hit_hwbug = 1;
5895
5896 if (tp->tg3_flags3 & TG3_FLG3_5701_DMA_BUG)
Michael Chanc58ec932005-09-17 00:46:27 -07005897 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005899 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
5901
5902 entry = NEXT_TX(entry);
5903
5904 /* Now loop through additional data fragments, and queue them. */
5905 if (skb_shinfo(skb)->nr_frags > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005906 last = skb_shinfo(skb)->nr_frags - 1;
5907 for (i = 0; i <= last; i++) {
5908 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5909
5910 len = frag->size;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005911 mapping = pci_map_page(tp->pdev,
5912 frag->page,
5913 frag->page_offset,
5914 len, PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005915
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005916 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005917 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00005918 mapping);
5919 if (pci_dma_mapping_error(tp->pdev, mapping))
5920 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005922 if ((tp->tg3_flags3 & TG3_FLG3_SHORT_DMA_BUG) &&
5923 len <= 8)
5924 would_hit_hwbug = 1;
5925
Matt Carlson0e1406d2009-11-02 12:33:33 +00005926 if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
5927 tg3_4g_overflow_test(mapping, len))
Michael Chanc58ec932005-09-17 00:46:27 -07005928 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005929
Matt Carlson0e1406d2009-11-02 12:33:33 +00005930 if ((tp->tg3_flags3 & TG3_FLG3_40BIT_DMA_LIMIT_BUG) &&
5931 tg3_40bit_overflow_test(tp, mapping, len))
Michael Chan72f2afb2006-03-06 19:28:35 -08005932 would_hit_hwbug = 1;
5933
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005935 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005936 base_flags, (i == last)|(mss << 1));
5937 else
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005938 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939 base_flags, (i == last));
5940
5941 entry = NEXT_TX(entry);
5942 }
5943 }
5944
5945 if (would_hit_hwbug) {
5946 u32 last_plus_one = entry;
5947 u32 start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948
Michael Chanc58ec932005-09-17 00:46:27 -07005949 start = entry - 1 - skb_shinfo(skb)->nr_frags;
5950 start &= (TG3_TX_RING_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005951
5952 /* If the workaround fails due to memory/mapping
5953 * failure, silently drop this packet.
5954 */
Matt Carlson24f4efd2009-11-13 13:03:35 +00005955 if (tigon3_dma_hwbug_workaround(tnapi, skb, last_plus_one,
Michael Chanc58ec932005-09-17 00:46:27 -07005956 &start, base_flags, mss))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005957 goto out_unlock;
5958
5959 entry = start;
5960 }
5961
5962 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00005963 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005964
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005965 tnapi->tx_prod = entry;
5966 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00005967 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00005968
5969 /* netif_tx_stop_queue() must be done before checking
5970 * checking tx index in tg3_tx_avail() below, because in
5971 * tg3_tx(), we update tx index before checking for
5972 * netif_tx_queue_stopped().
5973 */
5974 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005975 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00005976 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07005977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005978
5979out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00005980 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005981
5982 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005983
5984dma_error:
5985 last = i;
5986 entry = tnapi->tx_prod;
5987 tnapi->tx_buffers[entry].skb = NULL;
5988 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005989 dma_unmap_addr(&tnapi->tx_buffers[entry], mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005990 skb_headlen(skb),
5991 PCI_DMA_TODEVICE);
5992 for (i = 0; i <= last; i++) {
5993 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5994 entry = NEXT_TX(entry);
5995
5996 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005997 dma_unmap_addr(&tnapi->tx_buffers[entry],
Alexander Duyckf4188d82009-12-02 16:48:38 +00005998 mapping),
5999 frag->size, PCI_DMA_TODEVICE);
6000 }
6001
6002 dev_kfree_skb(skb);
6003 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006004}
6005
6006static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
6007 int new_mtu)
6008{
6009 dev->mtu = new_mtu;
6010
Michael Chanef7f5ec2005-07-25 12:32:25 -07006011 if (new_mtu > ETH_DATA_LEN) {
Michael Chana4e2b342005-10-26 15:46:52 -07006012 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
Michael Chanef7f5ec2005-07-25 12:32:25 -07006013 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
6014 ethtool_op_set_tso(dev, 0);
Matt Carlson859a588792010-04-05 10:19:28 +00006015 } else {
Michael Chanef7f5ec2005-07-25 12:32:25 -07006016 tp->tg3_flags |= TG3_FLAG_JUMBO_RING_ENABLE;
Matt Carlson859a588792010-04-05 10:19:28 +00006017 }
Michael Chanef7f5ec2005-07-25 12:32:25 -07006018 } else {
Michael Chana4e2b342005-10-26 15:46:52 -07006019 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
Michael Chanef7f5ec2005-07-25 12:32:25 -07006020 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
Michael Chan0f893dc2005-07-25 12:30:38 -07006021 tp->tg3_flags &= ~TG3_FLAG_JUMBO_RING_ENABLE;
Michael Chanef7f5ec2005-07-25 12:32:25 -07006022 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006023}
6024
6025static int tg3_change_mtu(struct net_device *dev, int new_mtu)
6026{
6027 struct tg3 *tp = netdev_priv(dev);
Michael Chanb9ec6c12006-07-25 16:37:27 -07006028 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006029
6030 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
6031 return -EINVAL;
6032
6033 if (!netif_running(dev)) {
6034 /* We'll just catch it later when the
6035 * device is up'd.
6036 */
6037 tg3_set_mtu(dev, tp, new_mtu);
6038 return 0;
6039 }
6040
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006041 tg3_phy_stop(tp);
6042
Linus Torvalds1da177e2005-04-16 15:20:36 -07006043 tg3_netif_stop(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07006044
6045 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006046
Michael Chan944d9802005-05-29 14:57:48 -07006047 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006048
6049 tg3_set_mtu(dev, tp, new_mtu);
6050
Michael Chanb9ec6c12006-07-25 16:37:27 -07006051 err = tg3_restart_hw(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006052
Michael Chanb9ec6c12006-07-25 16:37:27 -07006053 if (!err)
6054 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006055
David S. Millerf47c11e2005-06-24 20:18:35 -07006056 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006057
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006058 if (!err)
6059 tg3_phy_start(tp);
6060
Michael Chanb9ec6c12006-07-25 16:37:27 -07006061 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062}
6063
Matt Carlson21f581a2009-08-28 14:00:25 +00006064static void tg3_rx_prodring_free(struct tg3 *tp,
6065 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006066{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067 int i;
6068
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006069 if (tpr != &tp->prodring[0]) {
6070 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
6071 i = (i + 1) % TG3_RX_RING_SIZE)
6072 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6073 tp->rx_pkt_map_sz);
6074
6075 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
6076 for (i = tpr->rx_jmb_cons_idx;
6077 i != tpr->rx_jmb_prod_idx;
6078 i = (i + 1) % TG3_RX_JUMBO_RING_SIZE) {
6079 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6080 TG3_RX_JMB_MAP_SZ);
6081 }
6082 }
6083
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006084 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006087 for (i = 0; i < TG3_RX_RING_SIZE; i++)
6088 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6089 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006090
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006091 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006092 for (i = 0; i < TG3_RX_JUMBO_RING_SIZE; i++)
6093 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6094 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095 }
6096}
6097
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006098/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006099 *
6100 * The chip has been shut down and the driver detached from
6101 * the networking, so no interrupts or new tx packets will
6102 * end up in the driver. tp->{tx,}lock are held and thus
6103 * we may not sleep.
6104 */
Matt Carlson21f581a2009-08-28 14:00:25 +00006105static int tg3_rx_prodring_alloc(struct tg3 *tp,
6106 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006107{
Matt Carlson287be122009-08-28 13:58:46 +00006108 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006109
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006110 tpr->rx_std_cons_idx = 0;
6111 tpr->rx_std_prod_idx = 0;
6112 tpr->rx_jmb_cons_idx = 0;
6113 tpr->rx_jmb_prod_idx = 0;
6114
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006115 if (tpr != &tp->prodring[0]) {
6116 memset(&tpr->rx_std_buffers[0], 0, TG3_RX_STD_BUFF_RING_SIZE);
6117 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE)
6118 memset(&tpr->rx_jmb_buffers[0], 0,
6119 TG3_RX_JMB_BUFF_RING_SIZE);
6120 goto done;
6121 }
6122
Linus Torvalds1da177e2005-04-16 15:20:36 -07006123 /* Zero out all descriptors. */
Matt Carlson21f581a2009-08-28 14:00:25 +00006124 memset(tpr->rx_std, 0, TG3_RX_RING_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006125
Matt Carlson287be122009-08-28 13:58:46 +00006126 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Michael Chana4e2b342005-10-26 15:46:52 -07006127 if ((tp->tg3_flags2 & TG3_FLG2_5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00006128 tp->dev->mtu > ETH_DATA_LEN)
6129 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
6130 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07006131
Linus Torvalds1da177e2005-04-16 15:20:36 -07006132 /* Initialize invariants of the rings, we only set this
6133 * stuff once. This works because the card does not
6134 * write into the rx buffer posting rings.
6135 */
6136 for (i = 0; i < TG3_RX_RING_SIZE; i++) {
6137 struct tg3_rx_buffer_desc *rxd;
6138
Matt Carlson21f581a2009-08-28 14:00:25 +00006139 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00006140 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006141 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
6142 rxd->opaque = (RXD_OPAQUE_RING_STD |
6143 (i << RXD_OPAQUE_INDEX_SHIFT));
6144 }
6145
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006146 /* Now allocate fresh SKBs for each rx ring. */
6147 for (i = 0; i < tp->rx_pending; i++) {
Matt Carlson86b21e52009-11-13 13:03:45 +00006148 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_STD, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006149 netdev_warn(tp->dev,
6150 "Using a smaller RX standard ring. Only "
6151 "%d out of %d buffers were allocated "
6152 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006153 if (i == 0)
6154 goto initfail;
6155 tp->rx_pending = i;
6156 break;
6157 }
6158 }
6159
6160 if (!(tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE))
6161 goto done;
6162
Matt Carlson21f581a2009-08-28 14:00:25 +00006163 memset(tpr->rx_jmb, 0, TG3_RX_JUMBO_RING_BYTES);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006164
Matt Carlson0d86df82010-02-17 15:17:00 +00006165 if (!(tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE))
6166 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006167
Matt Carlson0d86df82010-02-17 15:17:00 +00006168 for (i = 0; i < TG3_RX_JUMBO_RING_SIZE; i++) {
6169 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006170
Matt Carlson0d86df82010-02-17 15:17:00 +00006171 rxd = &tpr->rx_jmb[i].std;
6172 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
6173 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
6174 RXD_FLAG_JUMBO;
6175 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
6176 (i << RXD_OPAQUE_INDEX_SHIFT));
6177 }
6178
6179 for (i = 0; i < tp->rx_jumbo_pending; i++) {
6180 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_JUMBO, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006181 netdev_warn(tp->dev,
6182 "Using a smaller RX jumbo ring. Only %d "
6183 "out of %d buffers were allocated "
6184 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00006185 if (i == 0)
6186 goto initfail;
6187 tp->rx_jumbo_pending = i;
6188 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006189 }
6190 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006191
6192done:
Michael Chan32d8c572006-07-25 16:38:29 -07006193 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006194
6195initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00006196 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006197 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006198}
6199
Matt Carlson21f581a2009-08-28 14:00:25 +00006200static void tg3_rx_prodring_fini(struct tg3 *tp,
6201 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006202{
Matt Carlson21f581a2009-08-28 14:00:25 +00006203 kfree(tpr->rx_std_buffers);
6204 tpr->rx_std_buffers = NULL;
6205 kfree(tpr->rx_jmb_buffers);
6206 tpr->rx_jmb_buffers = NULL;
6207 if (tpr->rx_std) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006208 pci_free_consistent(tp->pdev, TG3_RX_RING_BYTES,
Matt Carlson21f581a2009-08-28 14:00:25 +00006209 tpr->rx_std, tpr->rx_std_mapping);
6210 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211 }
Matt Carlson21f581a2009-08-28 14:00:25 +00006212 if (tpr->rx_jmb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006213 pci_free_consistent(tp->pdev, TG3_RX_JUMBO_RING_BYTES,
Matt Carlson21f581a2009-08-28 14:00:25 +00006214 tpr->rx_jmb, tpr->rx_jmb_mapping);
6215 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006216 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006217}
6218
Matt Carlson21f581a2009-08-28 14:00:25 +00006219static int tg3_rx_prodring_init(struct tg3 *tp,
6220 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006221{
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006222 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE, GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006223 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006224 return -ENOMEM;
6225
Matt Carlson21f581a2009-08-28 14:00:25 +00006226 tpr->rx_std = pci_alloc_consistent(tp->pdev, TG3_RX_RING_BYTES,
6227 &tpr->rx_std_mapping);
6228 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006229 goto err_out;
6230
6231 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006232 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE,
Matt Carlson21f581a2009-08-28 14:00:25 +00006233 GFP_KERNEL);
6234 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006235 goto err_out;
6236
Matt Carlson21f581a2009-08-28 14:00:25 +00006237 tpr->rx_jmb = pci_alloc_consistent(tp->pdev,
6238 TG3_RX_JUMBO_RING_BYTES,
6239 &tpr->rx_jmb_mapping);
6240 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006241 goto err_out;
6242 }
6243
6244 return 0;
6245
6246err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00006247 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006248 return -ENOMEM;
6249}
6250
6251/* Free up pending packets in all rx/tx rings.
6252 *
6253 * The chip has been shut down and the driver detached from
6254 * the networking, so no interrupts or new tx packets will
6255 * end up in the driver. tp->{tx,}lock is not held and we are not
6256 * in an interrupt context and thus may sleep.
6257 */
6258static void tg3_free_rings(struct tg3 *tp)
6259{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006260 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006261
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006262 for (j = 0; j < tp->irq_cnt; j++) {
6263 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006264
Matt Carlsonb28f6422010-06-05 17:24:32 +00006265 tg3_rx_prodring_free(tp, &tp->prodring[j]);
6266
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006267 if (!tnapi->tx_buffers)
6268 continue;
6269
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006270 for (i = 0; i < TG3_TX_RING_SIZE; ) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00006271 struct ring_info *txp;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006272 struct sk_buff *skb;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006273 unsigned int k;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006274
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006275 txp = &tnapi->tx_buffers[i];
6276 skb = txp->skb;
6277
6278 if (skb == NULL) {
6279 i++;
6280 continue;
6281 }
6282
Alexander Duyckf4188d82009-12-02 16:48:38 +00006283 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006284 dma_unmap_addr(txp, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006285 skb_headlen(skb),
6286 PCI_DMA_TODEVICE);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006287 txp->skb = NULL;
6288
Alexander Duyckf4188d82009-12-02 16:48:38 +00006289 i++;
6290
6291 for (k = 0; k < skb_shinfo(skb)->nr_frags; k++) {
6292 txp = &tnapi->tx_buffers[i & (TG3_TX_RING_SIZE - 1)];
6293 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006294 dma_unmap_addr(txp, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006295 skb_shinfo(skb)->frags[k].size,
6296 PCI_DMA_TODEVICE);
6297 i++;
6298 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006299
6300 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006301 }
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006302 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006303}
6304
6305/* Initialize tx/rx rings for packet processing.
6306 *
6307 * The chip has been shut down and the driver detached from
6308 * the networking, so no interrupts or new tx packets will
6309 * end up in the driver. tp->{tx,}lock are held and thus
6310 * we may not sleep.
6311 */
6312static int tg3_init_rings(struct tg3 *tp)
6313{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006314 int i;
Matt Carlson72334482009-08-28 14:03:01 +00006315
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006316 /* Free up all the SKBs. */
6317 tg3_free_rings(tp);
6318
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006319 for (i = 0; i < tp->irq_cnt; i++) {
6320 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006321
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006322 tnapi->last_tag = 0;
6323 tnapi->last_irq_tag = 0;
6324 tnapi->hw_status->status = 0;
6325 tnapi->hw_status->status_tag = 0;
6326 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6327
6328 tnapi->tx_prod = 0;
6329 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006330 if (tnapi->tx_ring)
6331 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006332
6333 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006334 if (tnapi->rx_rcb)
6335 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006336
Matt Carlsone4af1af2010-02-12 14:47:05 +00006337 if (tg3_rx_prodring_alloc(tp, &tp->prodring[i])) {
6338 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006339 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006340 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006341 }
Matt Carlson72334482009-08-28 14:03:01 +00006342
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006343 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006344}
6345
6346/*
6347 * Must not be invoked with interrupt sources disabled and
6348 * the hardware shutdown down.
6349 */
6350static void tg3_free_consistent(struct tg3 *tp)
6351{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006352 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006353
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006354 for (i = 0; i < tp->irq_cnt; i++) {
6355 struct tg3_napi *tnapi = &tp->napi[i];
6356
6357 if (tnapi->tx_ring) {
6358 pci_free_consistent(tp->pdev, TG3_TX_RING_BYTES,
6359 tnapi->tx_ring, tnapi->tx_desc_mapping);
6360 tnapi->tx_ring = NULL;
6361 }
6362
6363 kfree(tnapi->tx_buffers);
6364 tnapi->tx_buffers = NULL;
6365
6366 if (tnapi->rx_rcb) {
6367 pci_free_consistent(tp->pdev, TG3_RX_RCB_RING_BYTES(tp),
6368 tnapi->rx_rcb,
6369 tnapi->rx_rcb_mapping);
6370 tnapi->rx_rcb = NULL;
6371 }
6372
6373 if (tnapi->hw_status) {
6374 pci_free_consistent(tp->pdev, TG3_HW_STATUS_SIZE,
6375 tnapi->hw_status,
6376 tnapi->status_mapping);
6377 tnapi->hw_status = NULL;
6378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006379 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006380
Linus Torvalds1da177e2005-04-16 15:20:36 -07006381 if (tp->hw_stats) {
6382 pci_free_consistent(tp->pdev, sizeof(struct tg3_hw_stats),
6383 tp->hw_stats, tp->stats_mapping);
6384 tp->hw_stats = NULL;
6385 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006386
Matt Carlsone4af1af2010-02-12 14:47:05 +00006387 for (i = 0; i < tp->irq_cnt; i++)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006388 tg3_rx_prodring_fini(tp, &tp->prodring[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006389}
6390
6391/*
6392 * Must not be invoked with interrupt sources disabled and
6393 * the hardware shutdown down. Can sleep.
6394 */
6395static int tg3_alloc_consistent(struct tg3 *tp)
6396{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006397 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006398
Matt Carlsone4af1af2010-02-12 14:47:05 +00006399 for (i = 0; i < tp->irq_cnt; i++) {
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006400 if (tg3_rx_prodring_init(tp, &tp->prodring[i]))
6401 goto err_out;
6402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006403
Linus Torvalds1da177e2005-04-16 15:20:36 -07006404 tp->hw_stats = pci_alloc_consistent(tp->pdev,
6405 sizeof(struct tg3_hw_stats),
6406 &tp->stats_mapping);
6407 if (!tp->hw_stats)
6408 goto err_out;
6409
Linus Torvalds1da177e2005-04-16 15:20:36 -07006410 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6411
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006412 for (i = 0; i < tp->irq_cnt; i++) {
6413 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006414 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006415
6416 tnapi->hw_status = pci_alloc_consistent(tp->pdev,
6417 TG3_HW_STATUS_SIZE,
6418 &tnapi->status_mapping);
6419 if (!tnapi->hw_status)
6420 goto err_out;
6421
6422 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006423 sblk = tnapi->hw_status;
6424
Matt Carlson19cfaec2009-12-03 08:36:20 +00006425 /* If multivector TSS is enabled, vector 0 does not handle
6426 * tx interrupts. Don't allocate any resources for it.
6427 */
6428 if ((!i && !(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)) ||
6429 (i && (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS))) {
6430 tnapi->tx_buffers = kzalloc(sizeof(struct ring_info) *
6431 TG3_TX_RING_SIZE,
6432 GFP_KERNEL);
6433 if (!tnapi->tx_buffers)
6434 goto err_out;
6435
6436 tnapi->tx_ring = pci_alloc_consistent(tp->pdev,
6437 TG3_TX_RING_BYTES,
6438 &tnapi->tx_desc_mapping);
6439 if (!tnapi->tx_ring)
6440 goto err_out;
6441 }
6442
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006443 /*
6444 * When RSS is enabled, the status block format changes
6445 * slightly. The "rx_jumbo_consumer", "reserved",
6446 * and "rx_mini_consumer" members get mapped to the
6447 * other three rx return ring producer indexes.
6448 */
6449 switch (i) {
6450 default:
6451 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
6452 break;
6453 case 2:
6454 tnapi->rx_rcb_prod_idx = &sblk->rx_jumbo_consumer;
6455 break;
6456 case 3:
6457 tnapi->rx_rcb_prod_idx = &sblk->reserved;
6458 break;
6459 case 4:
6460 tnapi->rx_rcb_prod_idx = &sblk->rx_mini_consumer;
6461 break;
6462 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006463
Matt Carlsone4af1af2010-02-12 14:47:05 +00006464 tnapi->prodring = &tp->prodring[i];
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006465
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006466 /*
6467 * If multivector RSS is enabled, vector 0 does not handle
6468 * rx or tx interrupts. Don't allocate any resources for it.
6469 */
6470 if (!i && (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS))
6471 continue;
6472
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006473 tnapi->rx_rcb = pci_alloc_consistent(tp->pdev,
6474 TG3_RX_RCB_RING_BYTES(tp),
6475 &tnapi->rx_rcb_mapping);
6476 if (!tnapi->rx_rcb)
6477 goto err_out;
6478
6479 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006480 }
6481
Linus Torvalds1da177e2005-04-16 15:20:36 -07006482 return 0;
6483
6484err_out:
6485 tg3_free_consistent(tp);
6486 return -ENOMEM;
6487}
6488
6489#define MAX_WAIT_CNT 1000
6490
6491/* To stop a block, clear the enable bit and poll till it
6492 * clears. tp->lock is held.
6493 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006494static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006495{
6496 unsigned int i;
6497 u32 val;
6498
6499 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
6500 switch (ofs) {
6501 case RCVLSC_MODE:
6502 case DMAC_MODE:
6503 case MBFREE_MODE:
6504 case BUFMGR_MODE:
6505 case MEMARB_MODE:
6506 /* We can't enable/disable these bits of the
6507 * 5705/5750, just say success.
6508 */
6509 return 0;
6510
6511 default:
6512 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006514 }
6515
6516 val = tr32(ofs);
6517 val &= ~enable_bit;
6518 tw32_f(ofs, val);
6519
6520 for (i = 0; i < MAX_WAIT_CNT; i++) {
6521 udelay(100);
6522 val = tr32(ofs);
6523 if ((val & enable_bit) == 0)
6524 break;
6525 }
6526
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006527 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00006528 dev_err(&tp->pdev->dev,
6529 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
6530 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006531 return -ENODEV;
6532 }
6533
6534 return 0;
6535}
6536
6537/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006538static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006539{
6540 int i, err;
6541
6542 tg3_disable_ints(tp);
6543
6544 tp->rx_mode &= ~RX_MODE_ENABLE;
6545 tw32_f(MAC_RX_MODE, tp->rx_mode);
6546 udelay(10);
6547
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006548 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
6549 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
6550 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
6551 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
6552 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
6553 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006554
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006555 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
6556 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
6557 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
6558 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
6559 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
6560 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
6561 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006562
6563 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
6564 tw32_f(MAC_MODE, tp->mac_mode);
6565 udelay(40);
6566
6567 tp->tx_mode &= ~TX_MODE_ENABLE;
6568 tw32_f(MAC_TX_MODE, tp->tx_mode);
6569
6570 for (i = 0; i < MAX_WAIT_CNT; i++) {
6571 udelay(100);
6572 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
6573 break;
6574 }
6575 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00006576 dev_err(&tp->pdev->dev,
6577 "%s timed out, TX_MODE_ENABLE will not clear "
6578 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07006579 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006580 }
6581
Michael Chane6de8ad2005-05-05 14:42:41 -07006582 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006583 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
6584 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006585
6586 tw32(FTQ_RESET, 0xffffffff);
6587 tw32(FTQ_RESET, 0x00000000);
6588
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006589 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
6590 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006591
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006592 for (i = 0; i < tp->irq_cnt; i++) {
6593 struct tg3_napi *tnapi = &tp->napi[i];
6594 if (tnapi->hw_status)
6595 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006597 if (tp->hw_stats)
6598 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6599
Linus Torvalds1da177e2005-04-16 15:20:36 -07006600 return err;
6601}
6602
Matt Carlson0d3031d2007-10-10 18:02:43 -07006603static void tg3_ape_send_event(struct tg3 *tp, u32 event)
6604{
6605 int i;
6606 u32 apedata;
6607
6608 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
6609 if (apedata != APE_SEG_SIG_MAGIC)
6610 return;
6611
6612 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
Matt Carlson731fd792008-08-15 14:07:51 -07006613 if (!(apedata & APE_FW_STATUS_READY))
Matt Carlson0d3031d2007-10-10 18:02:43 -07006614 return;
6615
6616 /* Wait for up to 1 millisecond for APE to service previous event. */
6617 for (i = 0; i < 10; i++) {
6618 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
6619 return;
6620
6621 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
6622
6623 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6624 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
6625 event | APE_EVENT_STATUS_EVENT_PENDING);
6626
6627 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
6628
6629 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6630 break;
6631
6632 udelay(100);
6633 }
6634
6635 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6636 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
6637}
6638
6639static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
6640{
6641 u32 event;
6642 u32 apedata;
6643
6644 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
6645 return;
6646
6647 switch (kind) {
Matt Carlson33f401a2010-04-05 10:19:27 +00006648 case RESET_KIND_INIT:
6649 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
6650 APE_HOST_SEG_SIG_MAGIC);
6651 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
6652 APE_HOST_SEG_LEN_MAGIC);
6653 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
6654 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
6655 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
Matt Carlson6867c842010-07-11 09:31:44 +00006656 APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
Matt Carlson33f401a2010-04-05 10:19:27 +00006657 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
6658 APE_HOST_BEHAV_NO_PHYLOCK);
Matt Carlson0d3031d2007-10-10 18:02:43 -07006659
Matt Carlson33f401a2010-04-05 10:19:27 +00006660 event = APE_EVENT_STATUS_STATE_START;
6661 break;
6662 case RESET_KIND_SHUTDOWN:
6663 /* With the interface we are currently using,
6664 * APE does not track driver state. Wiping
6665 * out the HOST SEGMENT SIGNATURE forces
6666 * the APE to assume OS absent status.
6667 */
6668 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
Matt Carlsonb2aee152008-11-03 16:51:11 -08006669
Matt Carlson33f401a2010-04-05 10:19:27 +00006670 event = APE_EVENT_STATUS_STATE_UNLOAD;
6671 break;
6672 case RESET_KIND_SUSPEND:
6673 event = APE_EVENT_STATUS_STATE_SUSPEND;
6674 break;
6675 default:
6676 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -07006677 }
6678
6679 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
6680
6681 tg3_ape_send_event(tp, event);
6682}
6683
Michael Chane6af3012005-04-21 17:12:05 -07006684/* tp->lock is held. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006685static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
6686{
David S. Millerf49639e2006-06-09 11:58:36 -07006687 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
6688 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006689
6690 if (tp->tg3_flags2 & TG3_FLG2_ASF_NEW_HANDSHAKE) {
6691 switch (kind) {
6692 case RESET_KIND_INIT:
6693 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6694 DRV_STATE_START);
6695 break;
6696
6697 case RESET_KIND_SHUTDOWN:
6698 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6699 DRV_STATE_UNLOAD);
6700 break;
6701
6702 case RESET_KIND_SUSPEND:
6703 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6704 DRV_STATE_SUSPEND);
6705 break;
6706
6707 default:
6708 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006709 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006710 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07006711
6712 if (kind == RESET_KIND_INIT ||
6713 kind == RESET_KIND_SUSPEND)
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_post_reset(struct tg3 *tp, int kind)
6719{
6720 if (tp->tg3_flags2 & TG3_FLG2_ASF_NEW_HANDSHAKE) {
6721 switch (kind) {
6722 case RESET_KIND_INIT:
6723 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6724 DRV_STATE_START_DONE);
6725 break;
6726
6727 case RESET_KIND_SHUTDOWN:
6728 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6729 DRV_STATE_UNLOAD_DONE);
6730 break;
6731
6732 default:
6733 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006735 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07006736
6737 if (kind == RESET_KIND_SHUTDOWN)
6738 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006739}
6740
6741/* tp->lock is held. */
6742static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
6743{
6744 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) {
6745 switch (kind) {
6746 case RESET_KIND_INIT:
6747 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6748 DRV_STATE_START);
6749 break;
6750
6751 case RESET_KIND_SHUTDOWN:
6752 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6753 DRV_STATE_UNLOAD);
6754 break;
6755
6756 case RESET_KIND_SUSPEND:
6757 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6758 DRV_STATE_SUSPEND);
6759 break;
6760
6761 default:
6762 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006764 }
6765}
6766
Michael Chan7a6f4362006-09-27 16:03:31 -07006767static int tg3_poll_fw(struct tg3 *tp)
6768{
6769 int i;
6770 u32 val;
6771
Michael Chanb5d37722006-09-27 16:06:21 -07006772 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Gary Zambrano0ccead12006-11-14 16:34:00 -08006773 /* Wait up to 20ms for init done. */
6774 for (i = 0; i < 200; i++) {
Michael Chanb5d37722006-09-27 16:06:21 -07006775 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
6776 return 0;
Gary Zambrano0ccead12006-11-14 16:34:00 -08006777 udelay(100);
Michael Chanb5d37722006-09-27 16:06:21 -07006778 }
6779 return -ENODEV;
6780 }
6781
Michael Chan7a6f4362006-09-27 16:03:31 -07006782 /* Wait for firmware initialization to complete. */
6783 for (i = 0; i < 100000; i++) {
6784 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
6785 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
6786 break;
6787 udelay(10);
6788 }
6789
6790 /* Chip might not be fitted with firmware. Some Sun onboard
6791 * parts are configured like that. So don't signal the timeout
6792 * of the above loop as an error, but do report the lack of
6793 * running firmware once.
6794 */
6795 if (i >= 100000 &&
6796 !(tp->tg3_flags2 & TG3_FLG2_NO_FWARE_REPORTED)) {
6797 tp->tg3_flags2 |= TG3_FLG2_NO_FWARE_REPORTED;
6798
Joe Perches05dbe002010-02-17 19:44:19 +00006799 netdev_info(tp->dev, "No firmware running\n");
Michael Chan7a6f4362006-09-27 16:03:31 -07006800 }
6801
Matt Carlson6b10c162010-02-12 14:47:08 +00006802 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
6803 /* The 57765 A0 needs a little more
6804 * time to do some important work.
6805 */
6806 mdelay(10);
6807 }
6808
Michael Chan7a6f4362006-09-27 16:03:31 -07006809 return 0;
6810}
6811
Michael Chanee6a99b2007-07-18 21:49:10 -07006812/* Save PCI command register before chip reset */
6813static void tg3_save_pci_state(struct tg3 *tp)
6814{
Matt Carlson8a6eac92007-10-21 16:17:55 -07006815 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07006816}
6817
6818/* Restore PCI state after chip reset */
6819static void tg3_restore_pci_state(struct tg3 *tp)
6820{
6821 u32 val;
6822
6823 /* Re-enable indirect register accesses. */
6824 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
6825 tp->misc_host_ctrl);
6826
6827 /* Set MAX PCI retry to zero. */
6828 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
6829 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
6830 (tp->tg3_flags & TG3_FLAG_PCIX_MODE))
6831 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07006832 /* Allow reads and writes to the APE register and memory space. */
6833 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
6834 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00006835 PCISTATE_ALLOW_APE_SHMEM_WR |
6836 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07006837 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
6838
Matt Carlson8a6eac92007-10-21 16:17:55 -07006839 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07006840
Matt Carlsonfcb389d2008-11-03 16:55:44 -08006841 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785) {
6842 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)
6843 pcie_set_readrq(tp->pdev, 4096);
6844 else {
6845 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
6846 tp->pci_cacheline_sz);
6847 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
6848 tp->pci_lat_timer);
6849 }
Michael Chan114342f2007-10-15 02:12:26 -07006850 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08006851
Michael Chanee6a99b2007-07-18 21:49:10 -07006852 /* Make sure PCI-X relaxed ordering bit is clear. */
Matt Carlson52f44902008-11-21 17:17:04 -08006853 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
Matt Carlson9974a352007-10-07 23:27:28 -07006854 u16 pcix_cmd;
6855
6856 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
6857 &pcix_cmd);
6858 pcix_cmd &= ~PCI_X_CMD_ERO;
6859 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
6860 pcix_cmd);
6861 }
Michael Chanee6a99b2007-07-18 21:49:10 -07006862
6863 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
Michael Chanee6a99b2007-07-18 21:49:10 -07006864
6865 /* Chip reset on 5780 will reset MSI enable bit,
6866 * so need to restore it.
6867 */
6868 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
6869 u16 ctrl;
6870
6871 pci_read_config_word(tp->pdev,
6872 tp->msi_cap + PCI_MSI_FLAGS,
6873 &ctrl);
6874 pci_write_config_word(tp->pdev,
6875 tp->msi_cap + PCI_MSI_FLAGS,
6876 ctrl | PCI_MSI_FLAGS_ENABLE);
6877 val = tr32(MSGINT_MODE);
6878 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
6879 }
6880 }
6881}
6882
Linus Torvalds1da177e2005-04-16 15:20:36 -07006883static void tg3_stop_fw(struct tg3 *);
6884
6885/* tp->lock is held. */
6886static int tg3_chip_reset(struct tg3 *tp)
6887{
6888 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07006889 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00006890 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006891
David S. Millerf49639e2006-06-09 11:58:36 -07006892 tg3_nvram_lock(tp);
6893
Matt Carlson77b483f2008-08-15 14:07:24 -07006894 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
6895
David S. Millerf49639e2006-06-09 11:58:36 -07006896 /* No matching tg3_nvram_unlock() after this because
6897 * chip reset below will undo the nvram lock.
6898 */
6899 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006900
Michael Chanee6a99b2007-07-18 21:49:10 -07006901 /* GRC_MISC_CFG core clock reset will clear the memory
6902 * enable bit in PCI register 4 and the MSI enable bit
6903 * on some chips, so we save relevant registers here.
6904 */
6905 tg3_save_pci_state(tp);
6906
Michael Chand9ab5ad2006-03-20 22:27:35 -08006907 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08006908 (tp->tg3_flags3 & TG3_FLG3_5755_PLUS))
Michael Chand9ab5ad2006-03-20 22:27:35 -08006909 tw32(GRC_FASTBOOT_PC, 0);
6910
Linus Torvalds1da177e2005-04-16 15:20:36 -07006911 /*
6912 * We must avoid the readl() that normally takes place.
6913 * It locks machines, causes machine checks, and other
6914 * fun things. So, temporarily disable the 5701
6915 * hardware workaround, while we do the reset.
6916 */
Michael Chan1ee582d2005-08-09 20:16:46 -07006917 write_op = tp->write32;
6918 if (write_op == tg3_write_flush_reg32)
6919 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006920
Michael Chand18edcb2007-03-24 20:57:11 -07006921 /* Prevent the irq handler from reading or writing PCI registers
6922 * during chip reset when the memory enable bit in the PCI command
6923 * register may be cleared. The chip does not generate interrupt
6924 * at this time, but the irq handler may still be called due to irq
6925 * sharing or irqpoll.
6926 */
6927 tp->tg3_flags |= TG3_FLAG_CHIP_RESETTING;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006928 for (i = 0; i < tp->irq_cnt; i++) {
6929 struct tg3_napi *tnapi = &tp->napi[i];
6930 if (tnapi->hw_status) {
6931 tnapi->hw_status->status = 0;
6932 tnapi->hw_status->status_tag = 0;
6933 }
6934 tnapi->last_tag = 0;
6935 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07006936 }
Michael Chand18edcb2007-03-24 20:57:11 -07006937 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00006938
6939 for (i = 0; i < tp->irq_cnt; i++)
6940 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07006941
Matt Carlson255ca312009-08-25 10:07:27 +00006942 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
6943 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
6944 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
6945 }
6946
Linus Torvalds1da177e2005-04-16 15:20:36 -07006947 /* do the reset */
6948 val = GRC_MISC_CFG_CORECLK_RESET;
6949
6950 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
Matt Carlson88075d92010-08-02 11:25:58 +00006951 /* Force PCIe 1.0a mode */
6952 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
6953 !(tp->tg3_flags3 & TG3_FLG3_5717_PLUS) &&
6954 tr32(TG3_PCIE_PHY_TSTCTL) ==
6955 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
6956 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
6957
Linus Torvalds1da177e2005-04-16 15:20:36 -07006958 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
6959 tw32(GRC_MISC_CFG, (1 << 29));
6960 val |= (1 << 29);
6961 }
6962 }
6963
Michael Chanb5d37722006-09-27 16:06:21 -07006964 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
6965 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
6966 tw32(GRC_VCPU_EXT_CTRL,
6967 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
6968 }
6969
Matt Carlsonf37500d2010-08-02 11:25:59 +00006970 /* Manage gphy power for all CPMU absent PCIe devices. */
6971 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
6972 !(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006973 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00006974
Linus Torvalds1da177e2005-04-16 15:20:36 -07006975 tw32(GRC_MISC_CFG, val);
6976
Michael Chan1ee582d2005-08-09 20:16:46 -07006977 /* restore 5701 hardware bug workaround write method */
6978 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006979
6980 /* Unfortunately, we have to delay before the PCI read back.
6981 * Some 575X chips even will not respond to a PCI cfg access
6982 * when the reset command is given to the chip.
6983 *
6984 * How do these hardware designers expect things to work
6985 * properly if the PCI write is posted for a long period
6986 * of time? It is always necessary to have some method by
6987 * which a register read back can occur to push the write
6988 * out which does the reset.
6989 *
6990 * For most tg3 variants the trick below was working.
6991 * Ho hum...
6992 */
6993 udelay(120);
6994
6995 /* Flush PCI posted writes. The normal MMIO registers
6996 * are inaccessible at this time so this is the only
6997 * way to make this reliably (actually, this is no longer
6998 * the case, see above). I tried to use indirect
6999 * register read/write but this upset some 5701 variants.
7000 */
7001 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
7002
7003 udelay(120);
7004
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007005 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) && tp->pcie_cap) {
Matt Carlsone7126992009-08-25 10:08:16 +00007006 u16 val16;
7007
Linus Torvalds1da177e2005-04-16 15:20:36 -07007008 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
7009 int i;
7010 u32 cfg_val;
7011
7012 /* Wait for link training to complete. */
7013 for (i = 0; i < 5000; i++)
7014 udelay(100);
7015
7016 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
7017 pci_write_config_dword(tp->pdev, 0xc4,
7018 cfg_val | (1 << 15));
7019 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007020
Matt Carlsone7126992009-08-25 10:08:16 +00007021 /* Clear the "no snoop" and "relaxed ordering" bits. */
7022 pci_read_config_word(tp->pdev,
7023 tp->pcie_cap + PCI_EXP_DEVCTL,
7024 &val16);
7025 val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
7026 PCI_EXP_DEVCTL_NOSNOOP_EN);
7027 /*
7028 * Older PCIe devices only support the 128 byte
7029 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007030 */
Matt Carlson6de34cb2010-08-02 11:25:55 +00007031 if (!(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT))
Matt Carlsone7126992009-08-25 10:08:16 +00007032 val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007033 pci_write_config_word(tp->pdev,
7034 tp->pcie_cap + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007035 val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007036
7037 pcie_set_readrq(tp->pdev, 4096);
7038
7039 /* Clear error status */
7040 pci_write_config_word(tp->pdev,
7041 tp->pcie_cap + PCI_EXP_DEVSTA,
7042 PCI_EXP_DEVSTA_CED |
7043 PCI_EXP_DEVSTA_NFED |
7044 PCI_EXP_DEVSTA_FED |
7045 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007046 }
7047
Michael Chanee6a99b2007-07-18 21:49:10 -07007048 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007049
Michael Chand18edcb2007-03-24 20:57:11 -07007050 tp->tg3_flags &= ~TG3_FLAG_CHIP_RESETTING;
7051
Michael Chanee6a99b2007-07-18 21:49:10 -07007052 val = 0;
7053 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
Michael Chan4cf78e42005-07-25 12:29:19 -07007054 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07007055 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007056
7057 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
7058 tg3_stop_fw(tp);
7059 tw32(0x5000, 0x400);
7060 }
7061
7062 tw32(GRC_MODE, tp->grc_mode);
7063
7064 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007065 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007066
7067 tw32(0xc4, val | (1 << 15));
7068 }
7069
7070 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
7071 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
7072 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
7073 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
7074 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
7075 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7076 }
7077
7078 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
7079 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
7080 tw32_f(MAC_MODE, tp->mac_mode);
Michael Chan747e8f82005-07-25 12:33:22 -07007081 } else if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES) {
7082 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
7083 tw32_f(MAC_MODE, tp->mac_mode);
Matt Carlson3bda1252008-08-15 14:08:22 -07007084 } else if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
7085 tp->mac_mode &= (MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN);
7086 if (tp->mac_mode & MAC_MODE_APE_TX_EN)
7087 tp->mac_mode |= MAC_MODE_TDE_ENABLE;
7088 tw32_f(MAC_MODE, tp->mac_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007089 } else
7090 tw32_f(MAC_MODE, 0);
7091 udelay(40);
7092
Matt Carlson77b483f2008-08-15 14:07:24 -07007093 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
7094
Michael Chan7a6f4362006-09-27 16:03:31 -07007095 err = tg3_poll_fw(tp);
7096 if (err)
7097 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007098
Matt Carlson0a9140c2009-08-28 12:27:50 +00007099 tg3_mdio_start(tp);
7100
Linus Torvalds1da177e2005-04-16 15:20:36 -07007101 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007102 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
7103 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonc885e822010-08-02 11:25:57 +00007104 !(tp->tg3_flags3 & TG3_FLG3_5717_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007105 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007106
7107 tw32(0x7c00, val | (1 << 25));
7108 }
7109
7110 /* Reprobe ASF enable state. */
7111 tp->tg3_flags &= ~TG3_FLAG_ENABLE_ASF;
7112 tp->tg3_flags2 &= ~TG3_FLG2_ASF_NEW_HANDSHAKE;
7113 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
7114 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
7115 u32 nic_cfg;
7116
7117 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
7118 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
7119 tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
Matt Carlson4ba526c2008-08-15 14:10:04 -07007120 tp->last_event_jiffies = jiffies;
John W. Linvillecbf46852005-04-21 17:01:29 -07007121 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007122 tp->tg3_flags2 |= TG3_FLG2_ASF_NEW_HANDSHAKE;
7123 }
7124 }
7125
7126 return 0;
7127}
7128
7129/* tp->lock is held. */
7130static void tg3_stop_fw(struct tg3 *tp)
7131{
Matt Carlson0d3031d2007-10-10 18:02:43 -07007132 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
7133 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07007134 /* Wait for RX cpu to ACK the previous event. */
7135 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007136
7137 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007138
7139 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007140
Matt Carlson7c5026a2008-05-02 16:49:29 -07007141 /* Wait for RX cpu to ACK this event. */
7142 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007143 }
7144}
7145
7146/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07007147static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007148{
7149 int err;
7150
7151 tg3_stop_fw(tp);
7152
Michael Chan944d9802005-05-29 14:57:48 -07007153 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007154
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007155 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007156 err = tg3_chip_reset(tp);
7157
Matt Carlsondaba2a62009-04-20 06:58:52 +00007158 __tg3_set_mac_addr(tp, 0);
7159
Michael Chan944d9802005-05-29 14:57:48 -07007160 tg3_write_sig_legacy(tp, kind);
7161 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007162
7163 if (err)
7164 return err;
7165
7166 return 0;
7167}
7168
Linus Torvalds1da177e2005-04-16 15:20:36 -07007169#define RX_CPU_SCRATCH_BASE 0x30000
7170#define RX_CPU_SCRATCH_SIZE 0x04000
7171#define TX_CPU_SCRATCH_BASE 0x34000
7172#define TX_CPU_SCRATCH_SIZE 0x04000
7173
7174/* tp->lock is held. */
7175static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
7176{
7177 int i;
7178
Eric Sesterhenn5d9428d2006-04-02 13:52:48 +02007179 BUG_ON(offset == TX_CPU_BASE &&
7180 (tp->tg3_flags2 & TG3_FLG2_5705_PLUS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007181
Michael Chanb5d37722006-09-27 16:06:21 -07007182 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7183 u32 val = tr32(GRC_VCPU_EXT_CTRL);
7184
7185 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
7186 return 0;
7187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007188 if (offset == RX_CPU_BASE) {
7189 for (i = 0; i < 10000; i++) {
7190 tw32(offset + CPU_STATE, 0xffffffff);
7191 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7192 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7193 break;
7194 }
7195
7196 tw32(offset + CPU_STATE, 0xffffffff);
7197 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
7198 udelay(10);
7199 } else {
7200 for (i = 0; i < 10000; i++) {
7201 tw32(offset + CPU_STATE, 0xffffffff);
7202 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7203 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7204 break;
7205 }
7206 }
7207
7208 if (i >= 10000) {
Joe Perches05dbe002010-02-17 19:44:19 +00007209 netdev_err(tp->dev, "%s timed out, %s CPU\n",
7210 __func__, offset == RX_CPU_BASE ? "RX" : "TX");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007211 return -ENODEV;
7212 }
Michael Chanec41c7d2006-01-17 02:40:55 -08007213
7214 /* Clear firmware's nvram arbitration. */
7215 if (tp->tg3_flags & TG3_FLAG_NVRAM)
7216 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007217 return 0;
7218}
7219
7220struct fw_info {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007221 unsigned int fw_base;
7222 unsigned int fw_len;
7223 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007224};
7225
7226/* tp->lock is held. */
7227static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base, u32 cpu_scratch_base,
7228 int cpu_scratch_size, struct fw_info *info)
7229{
Michael Chanec41c7d2006-01-17 02:40:55 -08007230 int err, lock_err, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007231 void (*write_op)(struct tg3 *, u32, u32);
7232
7233 if (cpu_base == TX_CPU_BASE &&
7234 (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007235 netdev_err(tp->dev,
7236 "%s: Trying to load TX cpu firmware which is 5705\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007237 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007238 return -EINVAL;
7239 }
7240
7241 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
7242 write_op = tg3_write_mem;
7243 else
7244 write_op = tg3_write_indirect_reg32;
7245
Michael Chan1b628152005-05-29 14:59:49 -07007246 /* It is possible that bootcode is still loading at this point.
7247 * Get the nvram lock first before halting the cpu.
7248 */
Michael Chanec41c7d2006-01-17 02:40:55 -08007249 lock_err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007250 err = tg3_halt_cpu(tp, cpu_base);
Michael Chanec41c7d2006-01-17 02:40:55 -08007251 if (!lock_err)
7252 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007253 if (err)
7254 goto out;
7255
7256 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
7257 write_op(tp, cpu_scratch_base + i, 0);
7258 tw32(cpu_base + CPU_STATE, 0xffffffff);
7259 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007260 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007261 write_op(tp, (cpu_scratch_base +
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007262 (info->fw_base & 0xffff) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07007263 (i * sizeof(u32))),
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007264 be32_to_cpu(info->fw_data[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007265
7266 err = 0;
7267
7268out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007269 return err;
7270}
7271
7272/* tp->lock is held. */
7273static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
7274{
7275 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007276 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007277 int err, i;
7278
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007279 fw_data = (void *)tp->fw->data;
7280
7281 /* Firmware blob starts with version numbers, followed by
7282 start address and length. We are setting complete length.
7283 length = end_address_of_bss - start_address_of_text.
7284 Remainder is the blob to be loaded contiguously
7285 from start address. */
7286
7287 info.fw_base = be32_to_cpu(fw_data[1]);
7288 info.fw_len = tp->fw->size - 12;
7289 info.fw_data = &fw_data[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007290
7291 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
7292 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
7293 &info);
7294 if (err)
7295 return err;
7296
7297 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
7298 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
7299 &info);
7300 if (err)
7301 return err;
7302
7303 /* Now startup only the RX cpu. */
7304 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007305 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007306
7307 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007308 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007309 break;
7310 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7311 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007312 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007313 udelay(1000);
7314 }
7315 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007316 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
7317 "should be %08x\n", __func__,
Joe Perches05dbe002010-02-17 19:44:19 +00007318 tr32(RX_CPU_BASE + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007319 return -ENODEV;
7320 }
7321 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7322 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
7323
7324 return 0;
7325}
7326
Linus Torvalds1da177e2005-04-16 15:20:36 -07007327/* 5705 needs a special version of the TSO firmware. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007328
7329/* tp->lock is held. */
7330static int tg3_load_tso_firmware(struct tg3 *tp)
7331{
7332 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007333 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007334 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
7335 int err, i;
7336
7337 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
7338 return 0;
7339
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007340 fw_data = (void *)tp->fw->data;
7341
7342 /* Firmware blob starts with version numbers, followed by
7343 start address and length. We are setting complete length.
7344 length = end_address_of_bss - start_address_of_text.
7345 Remainder is the blob to be loaded contiguously
7346 from start address. */
7347
7348 info.fw_base = be32_to_cpu(fw_data[1]);
7349 cpu_scratch_size = tp->fw_len;
7350 info.fw_len = tp->fw->size - 12;
7351 info.fw_data = &fw_data[3];
7352
Linus Torvalds1da177e2005-04-16 15:20:36 -07007353 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007354 cpu_base = RX_CPU_BASE;
7355 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007356 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007357 cpu_base = TX_CPU_BASE;
7358 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
7359 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
7360 }
7361
7362 err = tg3_load_firmware_cpu(tp, cpu_base,
7363 cpu_scratch_base, cpu_scratch_size,
7364 &info);
7365 if (err)
7366 return err;
7367
7368 /* Now startup the cpu. */
7369 tw32(cpu_base + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007370 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007371
7372 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007373 if (tr32(cpu_base + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007374 break;
7375 tw32(cpu_base + CPU_STATE, 0xffffffff);
7376 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007377 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007378 udelay(1000);
7379 }
7380 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007381 netdev_err(tp->dev,
7382 "%s fails to set CPU PC, is %08x should be %08x\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007383 __func__, tr32(cpu_base + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007384 return -ENODEV;
7385 }
7386 tw32(cpu_base + CPU_STATE, 0xffffffff);
7387 tw32_f(cpu_base + CPU_MODE, 0x00000000);
7388 return 0;
7389}
7390
Linus Torvalds1da177e2005-04-16 15:20:36 -07007391
Linus Torvalds1da177e2005-04-16 15:20:36 -07007392static int tg3_set_mac_addr(struct net_device *dev, void *p)
7393{
7394 struct tg3 *tp = netdev_priv(dev);
7395 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07007396 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007397
Michael Chanf9804dd2005-09-27 12:13:10 -07007398 if (!is_valid_ether_addr(addr->sa_data))
7399 return -EINVAL;
7400
Linus Torvalds1da177e2005-04-16 15:20:36 -07007401 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7402
Michael Chane75f7c92006-03-20 21:33:26 -08007403 if (!netif_running(dev))
7404 return 0;
7405
Michael Chan58712ef2006-04-29 18:58:01 -07007406 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) {
Michael Chan986e0ae2007-05-05 12:10:20 -07007407 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07007408
Michael Chan986e0ae2007-05-05 12:10:20 -07007409 addr0_high = tr32(MAC_ADDR_0_HIGH);
7410 addr0_low = tr32(MAC_ADDR_0_LOW);
7411 addr1_high = tr32(MAC_ADDR_1_HIGH);
7412 addr1_low = tr32(MAC_ADDR_1_LOW);
7413
7414 /* Skip MAC addr 1 if ASF is using it. */
7415 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
7416 !(addr1_high == 0 && addr1_low == 0))
7417 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07007418 }
Michael Chan986e0ae2007-05-05 12:10:20 -07007419 spin_lock_bh(&tp->lock);
7420 __tg3_set_mac_addr(tp, skip_mac_1);
7421 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007422
Michael Chanb9ec6c12006-07-25 16:37:27 -07007423 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007424}
7425
7426/* tp->lock is held. */
7427static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
7428 dma_addr_t mapping, u32 maxlen_flags,
7429 u32 nic_addr)
7430{
7431 tg3_write_mem(tp,
7432 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
7433 ((u64) mapping >> 32));
7434 tg3_write_mem(tp,
7435 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
7436 ((u64) mapping & 0xffffffff));
7437 tg3_write_mem(tp,
7438 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
7439 maxlen_flags);
7440
7441 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7442 tg3_write_mem(tp,
7443 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
7444 nic_addr);
7445}
7446
7447static void __tg3_set_rx_mode(struct net_device *);
Michael Chand244c892005-07-05 14:42:33 -07007448static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07007449{
Matt Carlsonb6080e12009-09-01 13:12:00 +00007450 int i;
7451
Matt Carlson19cfaec2009-12-03 08:36:20 +00007452 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007453 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
7454 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
7455 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007456 } else {
7457 tw32(HOSTCC_TXCOL_TICKS, 0);
7458 tw32(HOSTCC_TXMAX_FRAMES, 0);
7459 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007460 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007461
Matt Carlson20d73752010-07-11 09:31:41 +00007462 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00007463 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
7464 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
7465 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
7466 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007467 tw32(HOSTCC_RXCOL_TICKS, 0);
7468 tw32(HOSTCC_RXMAX_FRAMES, 0);
7469 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07007470 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007471
David S. Miller15f98502005-05-18 22:49:26 -07007472 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7473 u32 val = ec->stats_block_coalesce_usecs;
7474
Matt Carlsonb6080e12009-09-01 13:12:00 +00007475 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
7476 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
7477
David S. Miller15f98502005-05-18 22:49:26 -07007478 if (!netif_carrier_ok(tp->dev))
7479 val = 0;
7480
7481 tw32(HOSTCC_STAT_COAL_TICKS, val);
7482 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007483
7484 for (i = 0; i < tp->irq_cnt - 1; i++) {
7485 u32 reg;
7486
7487 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
7488 tw32(reg, ec->rx_coalesce_usecs);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007489 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
7490 tw32(reg, ec->rx_max_coalesced_frames);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007491 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
7492 tw32(reg, ec->rx_max_coalesced_frames_irq);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007493
7494 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS) {
7495 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
7496 tw32(reg, ec->tx_coalesce_usecs);
7497 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
7498 tw32(reg, ec->tx_max_coalesced_frames);
7499 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
7500 tw32(reg, ec->tx_max_coalesced_frames_irq);
7501 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007502 }
7503
7504 for (; i < tp->irq_max - 1; i++) {
7505 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007506 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007507 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007508
7509 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS) {
7510 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
7511 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
7512 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
7513 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007514 }
David S. Miller15f98502005-05-18 22:49:26 -07007515}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007516
7517/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00007518static void tg3_rings_reset(struct tg3 *tp)
7519{
7520 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007521 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007522 struct tg3_napi *tnapi = &tp->napi[0];
7523
7524 /* Disable all transmit rings but the first. */
7525 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7526 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00007527 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
7528 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007529 else
7530 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7531
7532 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7533 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
7534 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
7535 BDINFO_FLAGS_DISABLED);
7536
7537
7538 /* Disable all receive return rings but the first. */
Matt Carlsona50d0792010-06-05 17:24:37 +00007539 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
7540 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007541 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
7542 else if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00007543 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00007544 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
7545 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson2d31eca2009-09-01 12:53:31 +00007546 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
7547 else
7548 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7549
7550 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7551 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
7552 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
7553 BDINFO_FLAGS_DISABLED);
7554
7555 /* Disable interrupts */
7556 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
7557
7558 /* Zero mailbox registers. */
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007559 if (tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX) {
7560 for (i = 1; i < TG3_IRQ_MAX_VECS; i++) {
7561 tp->napi[i].tx_prod = 0;
7562 tp->napi[i].tx_cons = 0;
Matt Carlsonc2353a32010-01-20 16:58:08 +00007563 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
7564 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007565 tw32_rx_mbox(tp->napi[i].consmbox, 0);
7566 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
7567 }
Matt Carlsonc2353a32010-01-20 16:58:08 +00007568 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS))
7569 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007570 } else {
7571 tp->napi[0].tx_prod = 0;
7572 tp->napi[0].tx_cons = 0;
7573 tw32_mailbox(tp->napi[0].prodmbox, 0);
7574 tw32_rx_mbox(tp->napi[0].consmbox, 0);
7575 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007576
7577 /* Make sure the NIC-based send BD rings are disabled. */
7578 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7579 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
7580 for (i = 0; i < 16; i++)
7581 tw32_tx_mbox(mbox + i * 8, 0);
7582 }
7583
7584 txrcb = NIC_SRAM_SEND_RCB;
7585 rxrcb = NIC_SRAM_RCV_RET_RCB;
7586
7587 /* Clear status block in ram. */
7588 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7589
7590 /* Set status block DMA address */
7591 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
7592 ((u64) tnapi->status_mapping >> 32));
7593 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
7594 ((u64) tnapi->status_mapping & 0xffffffff));
7595
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007596 if (tnapi->tx_ring) {
7597 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7598 (TG3_TX_RING_SIZE <<
7599 BDINFO_FLAGS_MAXLEN_SHIFT),
7600 NIC_SRAM_TX_BUFFER_DESC);
7601 txrcb += TG3_BDINFO_SIZE;
7602 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007603
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007604 if (tnapi->rx_rcb) {
7605 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
7606 (TG3_RX_RCB_RING_SIZE(tp) <<
7607 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
7608 rxrcb += TG3_BDINFO_SIZE;
7609 }
7610
7611 stblk = HOSTCC_STATBLCK_RING1;
7612
7613 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
7614 u64 mapping = (u64)tnapi->status_mapping;
7615 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
7616 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
7617
7618 /* Clear status block in ram. */
7619 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7620
Matt Carlson19cfaec2009-12-03 08:36:20 +00007621 if (tnapi->tx_ring) {
7622 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7623 (TG3_TX_RING_SIZE <<
7624 BDINFO_FLAGS_MAXLEN_SHIFT),
7625 NIC_SRAM_TX_BUFFER_DESC);
7626 txrcb += TG3_BDINFO_SIZE;
7627 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007628
7629 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
7630 (TG3_RX_RCB_RING_SIZE(tp) <<
7631 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
7632
7633 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007634 rxrcb += TG3_BDINFO_SIZE;
7635 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007636}
7637
7638/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07007639static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007640{
7641 u32 val, rdmac_mode;
7642 int i, err, limit;
Matt Carlson21f581a2009-08-28 14:00:25 +00007643 struct tg3_rx_prodring_set *tpr = &tp->prodring[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007644
7645 tg3_disable_ints(tp);
7646
7647 tg3_stop_fw(tp);
7648
7649 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
7650
Matt Carlson859a588792010-04-05 10:19:28 +00007651 if (tp->tg3_flags & TG3_FLAG_INIT_COMPLETE)
Michael Chane6de8ad2005-05-05 14:42:41 -07007652 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007653
Matt Carlson603f1172010-02-12 14:47:10 +00007654 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08007655 tg3_phy_reset(tp);
7656
Linus Torvalds1da177e2005-04-16 15:20:36 -07007657 err = tg3_chip_reset(tp);
7658 if (err)
7659 return err;
7660
7661 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
7662
Matt Carlsonbcb37f62008-11-03 16:52:09 -08007663 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07007664 val = tr32(TG3_CPMU_CTRL);
7665 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
7666 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08007667
7668 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
7669 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
7670 val |= CPMU_LSPD_10MB_MACCLK_6_25;
7671 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
7672
7673 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
7674 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
7675 val |= CPMU_LNK_AWARE_MACCLK_6_25;
7676 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
7677
7678 val = tr32(TG3_CPMU_HST_ACC);
7679 val &= ~CPMU_HST_ACC_MACCLK_MASK;
7680 val |= CPMU_HST_ACC_MACCLK_6_25;
7681 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07007682 }
7683
Matt Carlson33466d92009-04-20 06:57:41 +00007684 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7685 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
7686 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
7687 PCIE_PWR_MGMT_L1_THRESH_4MS;
7688 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00007689
7690 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
7691 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
7692
7693 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00007694
Matt Carlsonf40386c2009-11-02 14:24:02 +00007695 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7696 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00007697 }
7698
Matt Carlson614b0592010-01-20 16:58:02 +00007699 if (tp->tg3_flags3 & TG3_FLG3_L1PLLPD_EN) {
7700 u32 grc_mode = tr32(GRC_MODE);
7701
7702 /* Access the lower 1K of PL PCIE block registers. */
7703 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
7704 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
7705
7706 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
7707 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
7708 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
7709
7710 tw32(GRC_MODE, grc_mode);
7711 }
7712
Matt Carlsoncea46462010-04-12 06:58:24 +00007713 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
7714 u32 grc_mode = tr32(GRC_MODE);
7715
7716 /* Access the lower 1K of PL PCIE block registers. */
7717 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
7718 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
7719
7720 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5);
7721 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
7722 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
7723
7724 tw32(GRC_MODE, grc_mode);
Matt Carlsona977dbe2010-04-12 06:58:26 +00007725
7726 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
7727 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
7728 val |= CPMU_LSPD_10MB_MACCLK_6_25;
7729 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00007730 }
7731
Linus Torvalds1da177e2005-04-16 15:20:36 -07007732 /* This works around an issue with Athlon chipsets on
7733 * B3 tigon3 silicon. This bit has no effect on any
7734 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07007735 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007736 */
Matt Carlson795d01c2007-10-07 23:28:17 -07007737 if (!(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT)) {
7738 if (!(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
7739 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
7740 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007742
7743 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
7744 (tp->tg3_flags & TG3_FLAG_PCIX_MODE)) {
7745 val = tr32(TG3PCI_PCISTATE);
7746 val |= PCISTATE_RETRY_SAME_DMA;
7747 tw32(TG3PCI_PCISTATE, val);
7748 }
7749
Matt Carlson0d3031d2007-10-10 18:02:43 -07007750 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
7751 /* Allow reads and writes to the
7752 * APE register and memory space.
7753 */
7754 val = tr32(TG3PCI_PCISTATE);
7755 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00007756 PCISTATE_ALLOW_APE_SHMEM_WR |
7757 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07007758 tw32(TG3PCI_PCISTATE, val);
7759 }
7760
Linus Torvalds1da177e2005-04-16 15:20:36 -07007761 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
7762 /* Enable some hw fixes. */
7763 val = tr32(TG3PCI_MSI_DATA);
7764 val |= (1 << 26) | (1 << 28) | (1 << 29);
7765 tw32(TG3PCI_MSI_DATA, val);
7766 }
7767
7768 /* Descriptor ring init may make accesses to the
7769 * NIC SRAM area to setup the TX descriptors, so we
7770 * can only do this after the hardware has been
7771 * successfully reset.
7772 */
Michael Chan32d8c572006-07-25 16:38:29 -07007773 err = tg3_init_rings(tp);
7774 if (err)
7775 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007776
Matt Carlsonc885e822010-08-02 11:25:57 +00007777 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00007778 val = tr32(TG3PCI_DMA_RW_CTRL) &
7779 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Matt Carlson1a319022010-04-12 06:58:25 +00007780 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
7781 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00007782 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
7783 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
7784 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07007785 /* This value is determined during the probe time DMA
7786 * engine test, tg3_test_dma.
7787 */
7788 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
7789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007790
7791 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
7792 GRC_MODE_4X_NIC_SEND_RINGS |
7793 GRC_MODE_NO_TX_PHDR_CSUM |
7794 GRC_MODE_NO_RX_PHDR_CSUM);
7795 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07007796
7797 /* Pseudo-header checksum is done by hardware logic and not
7798 * the offload processers, so make the chip do the pseudo-
7799 * header checksums on receive. For transmit it is more
7800 * convenient to do the pseudo-header checksum in software
7801 * as Linux does that on transmit for us in all cases.
7802 */
7803 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007804
7805 tw32(GRC_MODE,
7806 tp->grc_mode |
7807 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
7808
7809 /* Setup the timer prescalar register. Clock is always 66Mhz. */
7810 val = tr32(GRC_MISC_CFG);
7811 val &= ~0xff;
7812 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
7813 tw32(GRC_MISC_CFG, val);
7814
7815 /* Initialize MBUF/DESC pool. */
John W. Linvillecbf46852005-04-21 17:01:29 -07007816 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007817 /* Do nothing. */
7818 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
7819 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
7820 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
7821 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
7822 else
7823 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
7824 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
7825 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Matt Carlson859a588792010-04-05 10:19:28 +00007826 } else if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007827 int fw_len;
7828
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007829 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007830 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
7831 tw32(BUFMGR_MB_POOL_ADDR,
7832 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
7833 tw32(BUFMGR_MB_POOL_SIZE,
7834 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
7835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007836
Michael Chan0f893dc2005-07-25 12:30:38 -07007837 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007838 tw32(BUFMGR_MB_RDMA_LOW_WATER,
7839 tp->bufmgr_config.mbuf_read_dma_low_water);
7840 tw32(BUFMGR_MB_MACRX_LOW_WATER,
7841 tp->bufmgr_config.mbuf_mac_rx_low_water);
7842 tw32(BUFMGR_MB_HIGH_WATER,
7843 tp->bufmgr_config.mbuf_high_water);
7844 } else {
7845 tw32(BUFMGR_MB_RDMA_LOW_WATER,
7846 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
7847 tw32(BUFMGR_MB_MACRX_LOW_WATER,
7848 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
7849 tw32(BUFMGR_MB_HIGH_WATER,
7850 tp->bufmgr_config.mbuf_high_water_jumbo);
7851 }
7852 tw32(BUFMGR_DMA_LOW_WATER,
7853 tp->bufmgr_config.dma_low_water);
7854 tw32(BUFMGR_DMA_HIGH_WATER,
7855 tp->bufmgr_config.dma_high_water);
7856
7857 tw32(BUFMGR_MODE, BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE);
7858 for (i = 0; i < 2000; i++) {
7859 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
7860 break;
7861 udelay(10);
7862 }
7863 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00007864 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007865 return -ENODEV;
7866 }
7867
7868 /* Setup replenish threshold. */
Michael Chanf92905d2006-06-29 20:14:29 -07007869 val = tp->rx_pending / 8;
7870 if (val == 0)
7871 val = 1;
7872 else if (val > tp->rx_std_max_post)
7873 val = tp->rx_std_max_post;
Michael Chanb5d37722006-09-27 16:06:21 -07007874 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7875 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
7876 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
7877
7878 if (val > (TG3_RX_INTERNAL_RING_SZ_5906 / 2))
7879 val = TG3_RX_INTERNAL_RING_SZ_5906 / 2;
7880 }
Michael Chanf92905d2006-06-29 20:14:29 -07007881
7882 tw32(RCVBDI_STD_THRESH, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007883
7884 /* Initialize TG3_BDINFO's at:
7885 * RCVDBDI_STD_BD: standard eth size rx ring
7886 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
7887 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
7888 *
7889 * like so:
7890 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
7891 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
7892 * ring attribute flags
7893 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
7894 *
7895 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
7896 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
7897 *
7898 * The size of each ring is fixed in the firmware, but the location is
7899 * configurable.
7900 */
7901 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00007902 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007903 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00007904 ((u64) tpr->rx_std_mapping & 0xffffffff));
Matt Carlsona50d0792010-06-05 17:24:37 +00007905 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
7906 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5719)
Matt Carlson87668d32009-11-13 13:03:34 +00007907 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
7908 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007909
Matt Carlsonfdb72b32009-08-28 13:57:12 +00007910 /* Disable the mini ring */
7911 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007912 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
7913 BDINFO_FLAGS_DISABLED);
7914
Matt Carlsonfdb72b32009-08-28 13:57:12 +00007915 /* Program the jumbo buffer descriptor ring control
7916 * blocks on those devices that have them.
7917 */
Matt Carlson8f666b02009-08-28 13:58:24 +00007918 if ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) &&
Matt Carlsonfdb72b32009-08-28 13:57:12 +00007919 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007920 /* Setup replenish threshold. */
7921 tw32(RCVBDI_JUMBO_THRESH, tp->rx_jumbo_pending / 8);
7922
Michael Chan0f893dc2005-07-25 12:30:38 -07007923 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007924 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00007925 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007926 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00007927 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007928 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlson79ed5ac2009-08-28 14:00:55 +00007929 (RX_JUMBO_MAX_SIZE << BDINFO_FLAGS_MAXLEN_SHIFT) |
7930 BDINFO_FLAGS_USE_EXT_RECV);
Matt Carlsona50d0792010-06-05 17:24:37 +00007931 if (!(tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) ||
7932 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson87668d32009-11-13 13:03:34 +00007933 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
7934 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007935 } else {
7936 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
7937 BDINFO_FLAGS_DISABLED);
7938 }
7939
Matt Carlsonc885e822010-08-02 11:25:57 +00007940 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)
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 Carlsonc885e822010-08-02 11:25:57 +00007957 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007958 tw32(STD_REPLENISH_LWM, 32);
7959 tw32(JMB_REPLENISH_LWM, 16);
7960 }
7961
Matt Carlson2d31eca2009-09-01 12:53:31 +00007962 tg3_rings_reset(tp);
7963
Linus Torvalds1da177e2005-04-16 15:20:36 -07007964 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07007965 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007966
7967 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00007968 tw32(MAC_RX_MTU_SIZE,
7969 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007970
7971 /* The slot time is changed by tg3_setup_phy if we
7972 * run at gigabit with half duplex.
7973 */
7974 tw32(MAC_TX_LENGTHS,
7975 (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
7976 (6 << TX_LENGTHS_IPG_SHIFT) |
7977 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
7978
7979 /* Receive rules. */
7980 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
7981 tw32(RCVLPC_CONFIG, 0x0181);
7982
7983 /* Calculate RDMAC_MODE setting early, we need it to determine
7984 * the RCVLPC_STATE_ENABLE mask.
7985 */
7986 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
7987 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
7988 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
7989 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
7990 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07007991
Matt Carlsona50d0792010-06-05 17:24:37 +00007992 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
7993 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlson0339e4e2010-02-12 14:47:09 +00007994 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
7995
Matt Carlson57e69832008-05-25 23:48:31 -07007996 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08007997 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
7998 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07007999 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
8000 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
8001 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
8002
Michael Chan85e94ce2005-04-21 17:05:28 -07008003 /* If statement applies to 5705 and 5750 PCI devices only */
8004 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8005 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) ||
8006 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008007 if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07008008 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008009 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
8010 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
8011 !(tp->tg3_flags2 & TG3_FLG2_IS_5788)) {
8012 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8013 }
8014 }
8015
Michael Chan85e94ce2005-04-21 17:05:28 -07008016 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)
8017 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8018
Linus Torvalds1da177e2005-04-16 15:20:36 -07008019 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
Matt Carlson027455a2008-12-21 20:19:30 -08008020 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
8021
Matt Carlsone849cdc2009-11-13 13:03:38 +00008022 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
8023 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson027455a2008-12-21 20:19:30 -08008024 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
8025 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008026
8027 /* Receive/send statistics. */
Michael Chan16613942006-06-29 20:15:13 -07008028 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
8029 val = tr32(RCVLPC_STATS_ENABLE);
8030 val &= ~RCVLPC_STATSENAB_DACK_FIX;
8031 tw32(RCVLPC_STATS_ENABLE, val);
8032 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
8033 (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008034 val = tr32(RCVLPC_STATS_ENABLE);
8035 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
8036 tw32(RCVLPC_STATS_ENABLE, val);
8037 } else {
8038 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
8039 }
8040 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
8041 tw32(SNDDATAI_STATSENAB, 0xffffff);
8042 tw32(SNDDATAI_STATSCTRL,
8043 (SNDDATAI_SCTRL_ENABLE |
8044 SNDDATAI_SCTRL_FASTUPD));
8045
8046 /* Setup host coalescing engine. */
8047 tw32(HOSTCC_MODE, 0);
8048 for (i = 0; i < 2000; i++) {
8049 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
8050 break;
8051 udelay(10);
8052 }
8053
Michael Chand244c892005-07-05 14:42:33 -07008054 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008055
Linus Torvalds1da177e2005-04-16 15:20:36 -07008056 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
8057 /* Status/statistics block address. See tg3_timer,
8058 * the tg3_periodic_fetch_stats call there, and
8059 * tg3_get_stats to see how this works for 5705/5750 chips.
8060 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008061 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8062 ((u64) tp->stats_mapping >> 32));
8063 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8064 ((u64) tp->stats_mapping & 0xffffffff));
8065 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008066
Linus Torvalds1da177e2005-04-16 15:20:36 -07008067 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008068
8069 /* Clear statistics and status block memory areas */
8070 for (i = NIC_SRAM_STATS_BLK;
8071 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
8072 i += sizeof(u32)) {
8073 tg3_write_mem(tp, i, 0);
8074 udelay(40);
8075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008076 }
8077
8078 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
8079
8080 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
8081 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
8082 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
8083 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
8084
Michael Chanc94e3942005-09-27 12:12:42 -07008085 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES) {
8086 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
8087 /* reset to prevent losing 1st rx packet intermittently */
8088 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8089 udelay(10);
8090 }
8091
Matt Carlson3bda1252008-08-15 14:08:22 -07008092 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
8093 tp->mac_mode &= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
8094 else
8095 tp->mac_mode = 0;
8096 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Linus Torvalds1da177e2005-04-16 15:20:36 -07008097 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE | MAC_MODE_FHDE_ENABLE;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07008098 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
8099 !(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
8100 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
8101 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008102 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
8103 udelay(40);
8104
Michael Chan314fba32005-04-21 17:07:04 -07008105 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Michael Chan9d26e212006-12-07 00:21:14 -08008106 * If TG3_FLG2_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07008107 * register to preserve the GPIO settings for LOMs. The GPIOs,
8108 * whether used as inputs or outputs, are set by boot code after
8109 * reset.
8110 */
Michael Chan9d26e212006-12-07 00:21:14 -08008111 if (!(tp->tg3_flags2 & TG3_FLG2_IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07008112 u32 gpio_mask;
8113
Michael Chan9d26e212006-12-07 00:21:14 -08008114 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
8115 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
8116 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07008117
8118 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
8119 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
8120 GRC_LCLCTRL_GPIO_OUTPUT3;
8121
Michael Chanaf36e6b2006-03-23 01:28:06 -08008122 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
8123 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
8124
Gary Zambranoaaf84462007-05-05 11:51:45 -07008125 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07008126 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
8127
8128 /* GPIO1 must be driven high for eeprom write protect */
Michael Chan9d26e212006-12-07 00:21:14 -08008129 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT)
8130 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
8131 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07008132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008133 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8134 udelay(100);
8135
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008136 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX) {
8137 val = tr32(MSGINT_MODE);
8138 val |= MSGINT_MODE_MULTIVEC_EN | MSGINT_MODE_ENABLE;
8139 tw32(MSGINT_MODE, val);
8140 }
8141
Linus Torvalds1da177e2005-04-16 15:20:36 -07008142 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
8143 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
8144 udelay(40);
8145 }
8146
8147 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
8148 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
8149 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
8150 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
8151 WDMAC_MODE_LNGREAD_ENAB);
8152
Michael Chan85e94ce2005-04-21 17:05:28 -07008153 /* If statement applies to 5705 and 5750 PCI devices only */
8154 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8155 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) ||
8156 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750) {
Matt Carlson29ea0952009-08-25 10:07:54 +00008157 if ((tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008158 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
8159 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
8160 /* nothing */
8161 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
8162 !(tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
8163 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)) {
8164 val |= WDMAC_MODE_RX_ACCEL;
8165 }
8166 }
8167
Michael Chand9ab5ad2006-03-20 22:27:35 -08008168 /* Enable host coalescing bug fix */
Matt Carlson321d32a2008-11-21 17:22:19 -08008169 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Matt Carlsonf51f3562008-05-25 23:45:08 -07008170 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad2006-03-20 22:27:35 -08008171
Matt Carlson788a0352009-11-02 14:26:03 +00008172 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
8173 val |= WDMAC_MODE_BURST_ALL_DATA;
8174
Linus Torvalds1da177e2005-04-16 15:20:36 -07008175 tw32_f(WDMAC_MODE, val);
8176 udelay(40);
8177
Matt Carlson9974a352007-10-07 23:27:28 -07008178 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
8179 u16 pcix_cmd;
8180
8181 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8182 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008183 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07008184 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
8185 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008186 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07008187 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
8188 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008189 }
Matt Carlson9974a352007-10-07 23:27:28 -07008190 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8191 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008192 }
8193
8194 tw32_f(RDMAC_MODE, rdmac_mode);
8195 udelay(40);
8196
8197 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
8198 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
8199 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07008200
8201 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
8202 tw32(SNDDATAC_MODE,
8203 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
8204 else
8205 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
8206
Linus Torvalds1da177e2005-04-16 15:20:36 -07008207 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
8208 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
8209 tw32(RCVDBDI_MODE, RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ);
8210 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008211 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
8212 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008213 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Matt Carlson19cfaec2009-12-03 08:36:20 +00008214 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008215 val |= SNDBDI_MODE_MULTI_TXQ_EN;
8216 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008217 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
8218
8219 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
8220 err = tg3_load_5701_a0_firmware_fix(tp);
8221 if (err)
8222 return err;
8223 }
8224
Linus Torvalds1da177e2005-04-16 15:20:36 -07008225 if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) {
8226 err = tg3_load_tso_firmware(tp);
8227 if (err)
8228 return err;
8229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008230
8231 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonb1d05212010-06-05 17:24:31 +00008232 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
8233 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
8234 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008235 tw32_f(MAC_TX_MODE, tp->tx_mode);
8236 udelay(100);
8237
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008238 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS) {
8239 u32 reg = MAC_RSS_INDIR_TBL_0;
8240 u8 *ent = (u8 *)&val;
8241
8242 /* Setup the indirection table */
8243 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
8244 int idx = i % sizeof(val);
8245
Matt Carlson5efeeea2010-07-11 09:31:40 +00008246 ent[idx] = i % (tp->irq_cnt - 1);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008247 if (idx == sizeof(val) - 1) {
8248 tw32(reg, val);
8249 reg += 4;
8250 }
8251 }
8252
8253 /* Setup the "secret" hash key. */
8254 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
8255 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
8256 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
8257 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
8258 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
8259 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
8260 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
8261 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
8262 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
8263 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
8264 }
8265
Linus Torvalds1da177e2005-04-16 15:20:36 -07008266 tp->rx_mode = RX_MODE_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08008267 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Michael Chanaf36e6b2006-03-23 01:28:06 -08008268 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
8269
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008270 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)
8271 tp->rx_mode |= RX_MODE_RSS_ENABLE |
8272 RX_MODE_RSS_ITBL_HASH_BITS_7 |
8273 RX_MODE_RSS_IPV6_HASH_EN |
8274 RX_MODE_RSS_TCP_IPV6_HASH_EN |
8275 RX_MODE_RSS_IPV4_HASH_EN |
8276 RX_MODE_RSS_TCP_IPV4_HASH_EN;
8277
Linus Torvalds1da177e2005-04-16 15:20:36 -07008278 tw32_f(MAC_RX_MODE, tp->rx_mode);
8279 udelay(10);
8280
Linus Torvalds1da177e2005-04-16 15:20:36 -07008281 tw32(MAC_LED_CTRL, tp->led_ctrl);
8282
8283 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Michael Chanc94e3942005-09-27 12:12:42 -07008284 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008285 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8286 udelay(10);
8287 }
8288 tw32_f(MAC_RX_MODE, tp->rx_mode);
8289 udelay(10);
8290
8291 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
8292 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
8293 !(tp->tg3_flags2 & TG3_FLG2_SERDES_PREEMPHASIS)) {
8294 /* Set drive transmission level to 1.2V */
8295 /* only if the signal pre-emphasis bit is not set */
8296 val = tr32(MAC_SERDES_CFG);
8297 val &= 0xfffff000;
8298 val |= 0x880;
8299 tw32(MAC_SERDES_CFG, val);
8300 }
8301 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
8302 tw32(MAC_SERDES_CFG, 0x616000);
8303 }
8304
8305 /* Prevent chip from dropping frames when flow control
8306 * is enabled.
8307 */
Matt Carlson666bc832010-01-20 16:58:03 +00008308 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8309 val = 1;
8310 else
8311 val = 2;
8312 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008313
8314 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
8315 (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)) {
8316 /* Use hardware link auto-negotiation */
8317 tp->tg3_flags2 |= TG3_FLG2_HW_AUTONEG;
8318 }
8319
Michael Chand4d2c552006-03-20 17:47:20 -08008320 if ((tp->tg3_flags2 & TG3_FLG2_MII_SERDES) &&
8321 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)) {
8322 u32 tmp;
8323
8324 tmp = tr32(SERDES_RX_CTRL);
8325 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
8326 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
8327 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
8328 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8329 }
8330
Matt Carlsondd477002008-05-25 23:45:58 -07008331 if (!(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)) {
8332 if (tp->link_config.phy_is_low_power) {
8333 tp->link_config.phy_is_low_power = 0;
8334 tp->link_config.speed = tp->link_config.orig_speed;
8335 tp->link_config.duplex = tp->link_config.orig_duplex;
8336 tp->link_config.autoneg = tp->link_config.orig_autoneg;
8337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008338
Matt Carlsondd477002008-05-25 23:45:58 -07008339 err = tg3_setup_phy(tp, 0);
8340 if (err)
8341 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008342
Matt Carlsondd477002008-05-25 23:45:58 -07008343 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
Matt Carlson7f97a4b2009-08-25 10:10:03 +00008344 !(tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07008345 u32 tmp;
8346
8347 /* Clear CRC stats. */
8348 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
8349 tg3_writephy(tp, MII_TG3_TEST1,
8350 tmp | MII_TG3_TEST1_CRC_EN);
8351 tg3_readphy(tp, 0x14, &tmp);
8352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008353 }
8354 }
8355
8356 __tg3_set_rx_mode(tp->dev);
8357
8358 /* Initialize receive rules. */
8359 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
8360 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
8361 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
8362 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
8363
Michael Chan4cf78e42005-07-25 12:29:19 -07008364 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Michael Chana4e2b342005-10-26 15:46:52 -07008365 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008366 limit = 8;
8367 else
8368 limit = 16;
8369 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF)
8370 limit -= 4;
8371 switch (limit) {
8372 case 16:
8373 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
8374 case 15:
8375 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
8376 case 14:
8377 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
8378 case 13:
8379 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
8380 case 12:
8381 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
8382 case 11:
8383 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
8384 case 10:
8385 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
8386 case 9:
8387 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
8388 case 8:
8389 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
8390 case 7:
8391 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
8392 case 6:
8393 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
8394 case 5:
8395 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
8396 case 4:
8397 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
8398 case 3:
8399 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
8400 case 2:
8401 case 1:
8402
8403 default:
8404 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07008405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008406
Matt Carlson9ce768e2007-10-11 19:49:11 -07008407 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
8408 /* Write our heartbeat update interval to APE. */
8409 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
8410 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07008411
Linus Torvalds1da177e2005-04-16 15:20:36 -07008412 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
8413
Linus Torvalds1da177e2005-04-16 15:20:36 -07008414 return 0;
8415}
8416
8417/* Called at device open time to get the chip ready for
8418 * packet processing. Invoked with tp->lock held.
8419 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008420static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008421{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008422 tg3_switch_clocks(tp);
8423
8424 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
8425
Matt Carlson2f751b62008-08-04 23:17:34 -07008426 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008427}
8428
8429#define TG3_STAT_ADD32(PSTAT, REG) \
8430do { u32 __val = tr32(REG); \
8431 (PSTAT)->low += __val; \
8432 if ((PSTAT)->low < __val) \
8433 (PSTAT)->high += 1; \
8434} while (0)
8435
8436static void tg3_periodic_fetch_stats(struct tg3 *tp)
8437{
8438 struct tg3_hw_stats *sp = tp->hw_stats;
8439
8440 if (!netif_carrier_ok(tp->dev))
8441 return;
8442
8443 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
8444 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
8445 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
8446 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
8447 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
8448 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
8449 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
8450 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
8451 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
8452 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
8453 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
8454 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
8455 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
8456
8457 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
8458 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
8459 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
8460 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
8461 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
8462 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
8463 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
8464 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
8465 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
8466 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
8467 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
8468 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
8469 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
8470 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07008471
8472 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
8473 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
8474 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008475}
8476
8477static void tg3_timer(unsigned long __opaque)
8478{
8479 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008480
Michael Chanf475f162006-03-27 23:20:14 -08008481 if (tp->irq_sync)
8482 goto restart_timer;
8483
David S. Millerf47c11e2005-06-24 20:18:35 -07008484 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008485
David S. Millerfac9b832005-05-18 22:46:34 -07008486 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)) {
8487 /* All of this garbage is because when using non-tagged
8488 * IRQ status the mailbox/status_block protocol the chip
8489 * uses with the cpu is race prone.
8490 */
Matt Carlson898a56f2009-08-28 14:02:40 +00008491 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -07008492 tw32(GRC_LOCAL_CTRL,
8493 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
8494 } else {
8495 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00008496 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -07008497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008498
David S. Millerfac9b832005-05-18 22:46:34 -07008499 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
8500 tp->tg3_flags2 |= TG3_FLG2_RESTART_TIMER;
David S. Millerf47c11e2005-06-24 20:18:35 -07008501 spin_unlock(&tp->lock);
David S. Millerfac9b832005-05-18 22:46:34 -07008502 schedule_work(&tp->reset_task);
8503 return;
8504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008505 }
8506
Linus Torvalds1da177e2005-04-16 15:20:36 -07008507 /* This part only runs once per second. */
8508 if (!--tp->timer_counter) {
David S. Millerfac9b832005-05-18 22:46:34 -07008509 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
8510 tg3_periodic_fetch_stats(tp);
8511
Linus Torvalds1da177e2005-04-16 15:20:36 -07008512 if (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) {
8513 u32 mac_stat;
8514 int phy_event;
8515
8516 mac_stat = tr32(MAC_STATUS);
8517
8518 phy_event = 0;
8519 if (tp->tg3_flags & TG3_FLAG_USE_MI_INTERRUPT) {
8520 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
8521 phy_event = 1;
8522 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
8523 phy_event = 1;
8524
8525 if (phy_event)
8526 tg3_setup_phy(tp, 0);
8527 } else if (tp->tg3_flags & TG3_FLAG_POLL_SERDES) {
8528 u32 mac_stat = tr32(MAC_STATUS);
8529 int need_setup = 0;
8530
8531 if (netif_carrier_ok(tp->dev) &&
8532 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
8533 need_setup = 1;
8534 }
Matt Carlsonbe98da62010-07-11 09:31:46 +00008535 if (!netif_carrier_ok(tp->dev) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008536 (mac_stat & (MAC_STATUS_PCS_SYNCED |
8537 MAC_STATUS_SIGNAL_DET))) {
8538 need_setup = 1;
8539 }
8540 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07008541 if (!tp->serdes_counter) {
8542 tw32_f(MAC_MODE,
8543 (tp->mac_mode &
8544 ~MAC_MODE_PORT_MODE_MASK));
8545 udelay(40);
8546 tw32_f(MAC_MODE, tp->mac_mode);
8547 udelay(40);
8548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008549 tg3_setup_phy(tp, 0);
8550 }
Matt Carlson57d8b882010-06-05 17:24:35 +00008551 } else if ((tp->tg3_flags2 & TG3_FLG2_MII_SERDES) &&
Matt Carlson2138c002010-07-11 09:31:43 +00008552 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -07008553 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +00008554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008555
8556 tp->timer_counter = tp->timer_multiplier;
8557 }
8558
Michael Chan130b8e42006-09-27 16:00:40 -07008559 /* Heartbeat is only sent once every 2 seconds.
8560 *
8561 * The heartbeat is to tell the ASF firmware that the host
8562 * driver is still alive. In the event that the OS crashes,
8563 * ASF needs to reset the hardware to free up the FIFO space
8564 * that may be filled with rx packets destined for the host.
8565 * If the FIFO is full, ASF will no longer function properly.
8566 *
8567 * Unintended resets have been reported on real time kernels
8568 * where the timer doesn't run on time. Netpoll will also have
8569 * same problem.
8570 *
8571 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
8572 * to check the ring condition when the heartbeat is expiring
8573 * before doing the reset. This will prevent most unintended
8574 * resets.
8575 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008576 if (!--tp->asf_counter) {
Matt Carlsonbc7959b2008-08-15 14:08:55 -07008577 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
8578 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07008579 tg3_wait_for_event_ack(tp);
8580
Michael Chanbbadf502006-04-06 21:46:34 -07008581 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07008582 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07008583 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +00008584 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
8585 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -07008586
8587 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008588 }
8589 tp->asf_counter = tp->asf_multiplier;
8590 }
8591
David S. Millerf47c11e2005-06-24 20:18:35 -07008592 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008593
Michael Chanf475f162006-03-27 23:20:14 -08008594restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07008595 tp->timer.expires = jiffies + tp->timer_offset;
8596 add_timer(&tp->timer);
8597}
8598
Matt Carlson4f125f42009-09-01 12:55:02 +00008599static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -08008600{
David Howells7d12e782006-10-05 14:55:46 +01008601 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008602 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +00008603 char *name;
8604 struct tg3_napi *tnapi = &tp->napi[irq_num];
8605
8606 if (tp->irq_cnt == 1)
8607 name = tp->dev->name;
8608 else {
8609 name = &tnapi->irq_lbl[0];
8610 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
8611 name[IFNAMSIZ-1] = 0;
8612 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08008613
Matt Carlson679563f2009-09-01 12:55:46 +00008614 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI_OR_MSIX) {
Michael Chanfcfa0a32006-03-20 22:28:41 -08008615 fn = tg3_msi;
8616 if (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)
8617 fn = tg3_msi_1shot;
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07008618 flags = IRQF_SAMPLE_RANDOM;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008619 } else {
8620 fn = tg3_interrupt;
8621 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)
8622 fn = tg3_interrupt_tagged;
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07008623 flags = IRQF_SHARED | IRQF_SAMPLE_RANDOM;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008624 }
Matt Carlson4f125f42009-09-01 12:55:02 +00008625
8626 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08008627}
8628
Michael Chan79381092005-04-21 17:13:59 -07008629static int tg3_test_interrupt(struct tg3 *tp)
8630{
Matt Carlson09943a12009-08-28 14:01:57 +00008631 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -07008632 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -07008633 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008634 u32 val;
Michael Chan79381092005-04-21 17:13:59 -07008635
Michael Chand4bc3922005-05-29 14:59:20 -07008636 if (!netif_running(dev))
8637 return -ENODEV;
8638
Michael Chan79381092005-04-21 17:13:59 -07008639 tg3_disable_ints(tp);
8640
Matt Carlson4f125f42009-09-01 12:55:02 +00008641 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07008642
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008643 /*
8644 * Turn off MSI one shot mode. Otherwise this test has no
8645 * observable way to know whether the interrupt was delivered.
8646 */
Matt Carlsonc885e822010-08-02 11:25:57 +00008647 if ((tp->tg3_flags3 & TG3_FLG3_5717_PLUS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008648 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
8649 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
8650 tw32(MSGINT_MODE, val);
8651 }
8652
Matt Carlson4f125f42009-09-01 12:55:02 +00008653 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Matt Carlson09943a12009-08-28 14:01:57 +00008654 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07008655 if (err)
8656 return err;
8657
Matt Carlson898a56f2009-08-28 14:02:40 +00008658 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -07008659 tg3_enable_ints(tp);
8660
8661 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00008662 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -07008663
8664 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -07008665 u32 int_mbox, misc_host_ctrl;
8666
Matt Carlson898a56f2009-08-28 14:02:40 +00008667 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -07008668 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
8669
8670 if ((int_mbox != 0) ||
8671 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
8672 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -07008673 break;
Michael Chanb16250e2006-09-27 16:10:14 -07008674 }
8675
Michael Chan79381092005-04-21 17:13:59 -07008676 msleep(10);
8677 }
8678
8679 tg3_disable_ints(tp);
8680
Matt Carlson4f125f42009-09-01 12:55:02 +00008681 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008682
Matt Carlson4f125f42009-09-01 12:55:02 +00008683 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07008684
8685 if (err)
8686 return err;
8687
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008688 if (intr_ok) {
8689 /* Reenable MSI one shot mode. */
Matt Carlsonc885e822010-08-02 11:25:57 +00008690 if ((tp->tg3_flags3 & TG3_FLG3_5717_PLUS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008691 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
8692 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
8693 tw32(MSGINT_MODE, val);
8694 }
Michael Chan79381092005-04-21 17:13:59 -07008695 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008696 }
Michael Chan79381092005-04-21 17:13:59 -07008697
8698 return -EIO;
8699}
8700
8701/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
8702 * successfully restored
8703 */
8704static int tg3_test_msi(struct tg3 *tp)
8705{
Michael Chan79381092005-04-21 17:13:59 -07008706 int err;
8707 u16 pci_cmd;
8708
8709 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSI))
8710 return 0;
8711
8712 /* Turn off SERR reporting in case MSI terminates with Master
8713 * Abort.
8714 */
8715 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
8716 pci_write_config_word(tp->pdev, PCI_COMMAND,
8717 pci_cmd & ~PCI_COMMAND_SERR);
8718
8719 err = tg3_test_interrupt(tp);
8720
8721 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
8722
8723 if (!err)
8724 return 0;
8725
8726 /* other failures */
8727 if (err != -EIO)
8728 return err;
8729
8730 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +00008731 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
8732 "to INTx mode. Please report this failure to the PCI "
8733 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -07008734
Matt Carlson4f125f42009-09-01 12:55:02 +00008735 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +00008736
Michael Chan79381092005-04-21 17:13:59 -07008737 pci_disable_msi(tp->pdev);
8738
8739 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI;
Andre Detschdc8bf1b2010-04-26 07:27:07 +00008740 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -07008741
Matt Carlson4f125f42009-09-01 12:55:02 +00008742 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07008743 if (err)
8744 return err;
8745
8746 /* Need to reset the chip because the MSI cycle may have terminated
8747 * with Master Abort.
8748 */
David S. Millerf47c11e2005-06-24 20:18:35 -07008749 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07008750
Michael Chan944d9802005-05-29 14:57:48 -07008751 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008752 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07008753
David S. Millerf47c11e2005-06-24 20:18:35 -07008754 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07008755
8756 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +00008757 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -07008758
8759 return err;
8760}
8761
Matt Carlson9e9fd122009-01-19 16:57:45 -08008762static int tg3_request_firmware(struct tg3 *tp)
8763{
8764 const __be32 *fw_data;
8765
8766 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00008767 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
8768 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08008769 return -ENOENT;
8770 }
8771
8772 fw_data = (void *)tp->fw->data;
8773
8774 /* Firmware blob starts with version numbers, followed by
8775 * start address and _full_ length including BSS sections
8776 * (which must be longer than the actual data, of course
8777 */
8778
8779 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
8780 if (tp->fw_len < (tp->fw->size - 12)) {
Joe Perches05dbe002010-02-17 19:44:19 +00008781 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
8782 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08008783 release_firmware(tp->fw);
8784 tp->fw = NULL;
8785 return -EINVAL;
8786 }
8787
8788 /* We no longer need firmware; we have it. */
8789 tp->fw_needed = NULL;
8790 return 0;
8791}
8792
Matt Carlson679563f2009-09-01 12:55:46 +00008793static bool tg3_enable_msix(struct tg3 *tp)
8794{
8795 int i, rc, cpus = num_online_cpus();
8796 struct msix_entry msix_ent[tp->irq_max];
8797
8798 if (cpus == 1)
8799 /* Just fallback to the simpler MSI mode. */
8800 return false;
8801
8802 /*
8803 * We want as many rx rings enabled as there are cpus.
8804 * The first MSIX vector only deals with link interrupts, etc,
8805 * so we add one to the number of vectors we are requesting.
8806 */
8807 tp->irq_cnt = min_t(unsigned, cpus + 1, tp->irq_max);
8808
8809 for (i = 0; i < tp->irq_max; i++) {
8810 msix_ent[i].entry = i;
8811 msix_ent[i].vector = 0;
8812 }
8813
8814 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +00008815 if (rc < 0) {
8816 return false;
8817 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +00008818 if (pci_enable_msix(tp->pdev, msix_ent, rc))
8819 return false;
Joe Perches05dbe002010-02-17 19:44:19 +00008820 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
8821 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +00008822 tp->irq_cnt = rc;
8823 }
8824
8825 for (i = 0; i < tp->irq_max; i++)
8826 tp->napi[i].irq_vec = msix_ent[i].vector;
8827
Matt Carlson2430b032010-06-05 17:24:34 +00008828 tp->dev->real_num_tx_queues = 1;
8829 if (tp->irq_cnt > 1) {
8830 tp->tg3_flags3 |= TG3_FLG3_ENABLE_RSS;
8831
Matt Carlsona50d0792010-06-05 17:24:37 +00008832 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
8833 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) {
Matt Carlson2430b032010-06-05 17:24:34 +00008834 tp->tg3_flags3 |= TG3_FLG3_ENABLE_TSS;
8835 tp->dev->real_num_tx_queues = tp->irq_cnt - 1;
8836 }
8837 }
Matt Carlsonfe5f5782009-09-01 13:09:39 +00008838
Matt Carlson679563f2009-09-01 12:55:46 +00008839 return true;
8840}
8841
Matt Carlson07b01732009-08-28 14:01:15 +00008842static void tg3_ints_init(struct tg3 *tp)
8843{
Matt Carlson679563f2009-09-01 12:55:46 +00008844 if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSI_OR_MSIX) &&
8845 !(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +00008846 /* All MSI supporting chips should support tagged
8847 * status. Assert that this is the case.
8848 */
Matt Carlson5129c3a2010-04-05 10:19:23 +00008849 netdev_warn(tp->dev,
8850 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +00008851 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +00008852 }
Matt Carlson4f125f42009-09-01 12:55:02 +00008853
Matt Carlson679563f2009-09-01 12:55:46 +00008854 if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX) && tg3_enable_msix(tp))
8855 tp->tg3_flags2 |= TG3_FLG2_USING_MSIX;
8856 else if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSI) &&
8857 pci_enable_msi(tp->pdev) == 0)
8858 tp->tg3_flags2 |= TG3_FLG2_USING_MSI;
8859
8860 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI_OR_MSIX) {
8861 u32 msi_mode = tr32(MSGINT_MODE);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008862 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
8863 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson679563f2009-09-01 12:55:46 +00008864 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
8865 }
8866defcfg:
8867 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSIX)) {
8868 tp->irq_cnt = 1;
8869 tp->napi[0].irq_vec = tp->pdev->irq;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00008870 tp->dev->real_num_tx_queues = 1;
Matt Carlson679563f2009-09-01 12:55:46 +00008871 }
Matt Carlson07b01732009-08-28 14:01:15 +00008872}
8873
8874static void tg3_ints_fini(struct tg3 *tp)
8875{
Matt Carlson679563f2009-09-01 12:55:46 +00008876 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
8877 pci_disable_msix(tp->pdev);
8878 else if (tp->tg3_flags2 & TG3_FLG2_USING_MSI)
8879 pci_disable_msi(tp->pdev);
8880 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI_OR_MSIX;
Matt Carlson774ee752010-08-02 11:25:56 +00008881 tp->tg3_flags3 &= ~(TG3_FLG3_ENABLE_RSS | TG3_FLG3_ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +00008882}
8883
Linus Torvalds1da177e2005-04-16 15:20:36 -07008884static int tg3_open(struct net_device *dev)
8885{
8886 struct tg3 *tp = netdev_priv(dev);
Matt Carlson4f125f42009-09-01 12:55:02 +00008887 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008888
Matt Carlson9e9fd122009-01-19 16:57:45 -08008889 if (tp->fw_needed) {
8890 err = tg3_request_firmware(tp);
8891 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
8892 if (err)
8893 return err;
8894 } else if (err) {
Joe Perches05dbe002010-02-17 19:44:19 +00008895 netdev_warn(tp->dev, "TSO capability disabled\n");
Matt Carlson9e9fd122009-01-19 16:57:45 -08008896 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
8897 } else if (!(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
Joe Perches05dbe002010-02-17 19:44:19 +00008898 netdev_notice(tp->dev, "TSO capability restored\n");
Matt Carlson9e9fd122009-01-19 16:57:45 -08008899 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
8900 }
8901 }
8902
Michael Chanc49a1562006-12-17 17:07:29 -08008903 netif_carrier_off(tp->dev);
8904
Michael Chanbc1c7562006-03-20 17:48:03 -08008905 err = tg3_set_power_state(tp, PCI_D0);
Matt Carlson2f751b62008-08-04 23:17:34 -07008906 if (err)
Michael Chanbc1c7562006-03-20 17:48:03 -08008907 return err;
Matt Carlson2f751b62008-08-04 23:17:34 -07008908
8909 tg3_full_lock(tp, 0);
Michael Chanbc1c7562006-03-20 17:48:03 -08008910
Linus Torvalds1da177e2005-04-16 15:20:36 -07008911 tg3_disable_ints(tp);
8912 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
8913
David S. Millerf47c11e2005-06-24 20:18:35 -07008914 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008915
Matt Carlson679563f2009-09-01 12:55:46 +00008916 /*
8917 * Setup interrupts first so we know how
8918 * many NAPI resources to allocate
8919 */
8920 tg3_ints_init(tp);
8921
Linus Torvalds1da177e2005-04-16 15:20:36 -07008922 /* The placement of this call is tied
8923 * to the setup and use of Host TX descriptors.
8924 */
8925 err = tg3_alloc_consistent(tp);
8926 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00008927 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008928
Matt Carlsonfed97812009-09-01 13:10:19 +00008929 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07008930
Matt Carlson4f125f42009-09-01 12:55:02 +00008931 for (i = 0; i < tp->irq_cnt; i++) {
8932 struct tg3_napi *tnapi = &tp->napi[i];
8933 err = tg3_request_irq(tp, i);
8934 if (err) {
8935 for (i--; i >= 0; i--)
8936 free_irq(tnapi->irq_vec, tnapi);
8937 break;
8938 }
8939 }
Matt Carlson07b01732009-08-28 14:01:15 +00008940
8941 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00008942 goto err_out2;
Matt Carlson07b01732009-08-28 14:01:15 +00008943
David S. Millerf47c11e2005-06-24 20:18:35 -07008944 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008945
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008946 err = tg3_init_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008947 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -07008948 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008949 tg3_free_rings(tp);
8950 } else {
David S. Millerfac9b832005-05-18 22:46:34 -07008951 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)
8952 tp->timer_offset = HZ;
8953 else
8954 tp->timer_offset = HZ / 10;
8955
8956 BUG_ON(tp->timer_offset > HZ);
8957 tp->timer_counter = tp->timer_multiplier =
8958 (HZ / tp->timer_offset);
8959 tp->asf_counter = tp->asf_multiplier =
Michael Chan28fbef72005-10-26 15:48:35 -07008960 ((HZ / tp->timer_offset) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008961
8962 init_timer(&tp->timer);
8963 tp->timer.expires = jiffies + tp->timer_offset;
8964 tp->timer.data = (unsigned long) tp;
8965 tp->timer.function = tg3_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008966 }
8967
David S. Millerf47c11e2005-06-24 20:18:35 -07008968 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008969
Matt Carlson07b01732009-08-28 14:01:15 +00008970 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00008971 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008972
Michael Chan79381092005-04-21 17:13:59 -07008973 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
8974 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -07008975
Michael Chan79381092005-04-21 17:13:59 -07008976 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -07008977 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -07008978 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -07008979 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07008980 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07008981
Matt Carlson679563f2009-09-01 12:55:46 +00008982 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -07008983 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08008984
Matt Carlsonc885e822010-08-02 11:25:57 +00008985 if (!(tp->tg3_flags3 & TG3_FLG3_5717_PLUS) &&
8986 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008987 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -08008988
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008989 tw32(PCIE_TRANSACTION_CFG,
8990 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -08008991 }
Michael Chan79381092005-04-21 17:13:59 -07008992 }
8993
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07008994 tg3_phy_start(tp);
8995
David S. Millerf47c11e2005-06-24 20:18:35 -07008996 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008997
Michael Chan79381092005-04-21 17:13:59 -07008998 add_timer(&tp->timer);
8999 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009000 tg3_enable_ints(tp);
9001
David S. Millerf47c11e2005-06-24 20:18:35 -07009002 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009003
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009004 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009005
9006 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +00009007
Matt Carlson679563f2009-09-01 12:55:46 +00009008err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +00009009 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9010 struct tg3_napi *tnapi = &tp->napi[i];
9011 free_irq(tnapi->irq_vec, tnapi);
9012 }
Matt Carlson07b01732009-08-28 14:01:15 +00009013
Matt Carlson679563f2009-09-01 12:55:46 +00009014err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +00009015 tg3_napi_disable(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00009016 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +00009017
9018err_out1:
9019 tg3_ints_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00009020 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009021}
9022
Eric Dumazet511d2222010-07-07 20:44:24 +00009023static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *,
9024 struct rtnl_link_stats64 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009025static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *);
9026
9027static int tg3_close(struct net_device *dev)
9028{
Matt Carlson4f125f42009-09-01 12:55:02 +00009029 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009030 struct tg3 *tp = netdev_priv(dev);
9031
Matt Carlsonfed97812009-09-01 13:10:19 +00009032 tg3_napi_disable(tp);
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07009033 cancel_work_sync(&tp->reset_task);
Michael Chan7faa0062006-02-02 17:29:28 -08009034
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009035 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009036
9037 del_timer_sync(&tp->timer);
9038
Matt Carlson24bb4fb2009-10-05 17:55:29 +00009039 tg3_phy_stop(tp);
9040
David S. Millerf47c11e2005-06-24 20:18:35 -07009041 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009042
9043 tg3_disable_ints(tp);
9044
Michael Chan944d9802005-05-29 14:57:48 -07009045 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009046 tg3_free_rings(tp);
Michael Chan5cf64b8a2007-05-05 12:11:21 -07009047 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009048
David S. Millerf47c11e2005-06-24 20:18:35 -07009049 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009050
Matt Carlson4f125f42009-09-01 12:55:02 +00009051 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9052 struct tg3_napi *tnapi = &tp->napi[i];
9053 free_irq(tnapi->irq_vec, tnapi);
9054 }
Matt Carlson07b01732009-08-28 14:01:15 +00009055
9056 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009057
Eric Dumazet511d2222010-07-07 20:44:24 +00009058 tg3_get_stats64(tp->dev, &tp->net_stats_prev);
9059
Linus Torvalds1da177e2005-04-16 15:20:36 -07009060 memcpy(&tp->estats_prev, tg3_get_estats(tp),
9061 sizeof(tp->estats_prev));
9062
9063 tg3_free_consistent(tp);
9064
Michael Chanbc1c7562006-03-20 17:48:03 -08009065 tg3_set_power_state(tp, PCI_D3hot);
9066
9067 netif_carrier_off(tp->dev);
9068
Linus Torvalds1da177e2005-04-16 15:20:36 -07009069 return 0;
9070}
9071
Eric Dumazet511d2222010-07-07 20:44:24 +00009072static inline u64 get_stat64(tg3_stat64_t *val)
Stefan Buehler816f8b82008-08-15 14:10:54 -07009073{
9074 return ((u64)val->high << 32) | ((u64)val->low);
9075}
9076
Eric Dumazet511d2222010-07-07 20:44:24 +00009077static u64 calc_crc_errors(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009078{
9079 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9080
9081 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
9082 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
9083 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009084 u32 val;
9085
David S. Millerf47c11e2005-06-24 20:18:35 -07009086 spin_lock_bh(&tp->lock);
Michael Chan569a5df2007-02-13 12:18:15 -08009087 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
9088 tg3_writephy(tp, MII_TG3_TEST1,
9089 val | MII_TG3_TEST1_CRC_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009090 tg3_readphy(tp, 0x14, &val);
9091 } else
9092 val = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07009093 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009094
9095 tp->phy_crc_errors += val;
9096
9097 return tp->phy_crc_errors;
9098 }
9099
9100 return get_stat64(&hw_stats->rx_fcs_errors);
9101}
9102
9103#define ESTAT_ADD(member) \
9104 estats->member = old_estats->member + \
Eric Dumazet511d2222010-07-07 20:44:24 +00009105 get_stat64(&hw_stats->member)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009106
9107static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *tp)
9108{
9109 struct tg3_ethtool_stats *estats = &tp->estats;
9110 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
9111 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9112
9113 if (!hw_stats)
9114 return old_estats;
9115
9116 ESTAT_ADD(rx_octets);
9117 ESTAT_ADD(rx_fragments);
9118 ESTAT_ADD(rx_ucast_packets);
9119 ESTAT_ADD(rx_mcast_packets);
9120 ESTAT_ADD(rx_bcast_packets);
9121 ESTAT_ADD(rx_fcs_errors);
9122 ESTAT_ADD(rx_align_errors);
9123 ESTAT_ADD(rx_xon_pause_rcvd);
9124 ESTAT_ADD(rx_xoff_pause_rcvd);
9125 ESTAT_ADD(rx_mac_ctrl_rcvd);
9126 ESTAT_ADD(rx_xoff_entered);
9127 ESTAT_ADD(rx_frame_too_long_errors);
9128 ESTAT_ADD(rx_jabbers);
9129 ESTAT_ADD(rx_undersize_packets);
9130 ESTAT_ADD(rx_in_length_errors);
9131 ESTAT_ADD(rx_out_length_errors);
9132 ESTAT_ADD(rx_64_or_less_octet_packets);
9133 ESTAT_ADD(rx_65_to_127_octet_packets);
9134 ESTAT_ADD(rx_128_to_255_octet_packets);
9135 ESTAT_ADD(rx_256_to_511_octet_packets);
9136 ESTAT_ADD(rx_512_to_1023_octet_packets);
9137 ESTAT_ADD(rx_1024_to_1522_octet_packets);
9138 ESTAT_ADD(rx_1523_to_2047_octet_packets);
9139 ESTAT_ADD(rx_2048_to_4095_octet_packets);
9140 ESTAT_ADD(rx_4096_to_8191_octet_packets);
9141 ESTAT_ADD(rx_8192_to_9022_octet_packets);
9142
9143 ESTAT_ADD(tx_octets);
9144 ESTAT_ADD(tx_collisions);
9145 ESTAT_ADD(tx_xon_sent);
9146 ESTAT_ADD(tx_xoff_sent);
9147 ESTAT_ADD(tx_flow_control);
9148 ESTAT_ADD(tx_mac_errors);
9149 ESTAT_ADD(tx_single_collisions);
9150 ESTAT_ADD(tx_mult_collisions);
9151 ESTAT_ADD(tx_deferred);
9152 ESTAT_ADD(tx_excessive_collisions);
9153 ESTAT_ADD(tx_late_collisions);
9154 ESTAT_ADD(tx_collide_2times);
9155 ESTAT_ADD(tx_collide_3times);
9156 ESTAT_ADD(tx_collide_4times);
9157 ESTAT_ADD(tx_collide_5times);
9158 ESTAT_ADD(tx_collide_6times);
9159 ESTAT_ADD(tx_collide_7times);
9160 ESTAT_ADD(tx_collide_8times);
9161 ESTAT_ADD(tx_collide_9times);
9162 ESTAT_ADD(tx_collide_10times);
9163 ESTAT_ADD(tx_collide_11times);
9164 ESTAT_ADD(tx_collide_12times);
9165 ESTAT_ADD(tx_collide_13times);
9166 ESTAT_ADD(tx_collide_14times);
9167 ESTAT_ADD(tx_collide_15times);
9168 ESTAT_ADD(tx_ucast_packets);
9169 ESTAT_ADD(tx_mcast_packets);
9170 ESTAT_ADD(tx_bcast_packets);
9171 ESTAT_ADD(tx_carrier_sense_errors);
9172 ESTAT_ADD(tx_discards);
9173 ESTAT_ADD(tx_errors);
9174
9175 ESTAT_ADD(dma_writeq_full);
9176 ESTAT_ADD(dma_write_prioq_full);
9177 ESTAT_ADD(rxbds_empty);
9178 ESTAT_ADD(rx_discards);
9179 ESTAT_ADD(rx_errors);
9180 ESTAT_ADD(rx_threshold_hit);
9181
9182 ESTAT_ADD(dma_readq_full);
9183 ESTAT_ADD(dma_read_prioq_full);
9184 ESTAT_ADD(tx_comp_queue_full);
9185
9186 ESTAT_ADD(ring_set_send_prod_index);
9187 ESTAT_ADD(ring_status_update);
9188 ESTAT_ADD(nic_irqs);
9189 ESTAT_ADD(nic_avoided_irqs);
9190 ESTAT_ADD(nic_tx_threshold_hit);
9191
9192 return estats;
9193}
9194
Eric Dumazet511d2222010-07-07 20:44:24 +00009195static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
9196 struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009197{
9198 struct tg3 *tp = netdev_priv(dev);
Eric Dumazet511d2222010-07-07 20:44:24 +00009199 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009200 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9201
9202 if (!hw_stats)
9203 return old_stats;
9204
9205 stats->rx_packets = old_stats->rx_packets +
9206 get_stat64(&hw_stats->rx_ucast_packets) +
9207 get_stat64(&hw_stats->rx_mcast_packets) +
9208 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009209
Linus Torvalds1da177e2005-04-16 15:20:36 -07009210 stats->tx_packets = old_stats->tx_packets +
9211 get_stat64(&hw_stats->tx_ucast_packets) +
9212 get_stat64(&hw_stats->tx_mcast_packets) +
9213 get_stat64(&hw_stats->tx_bcast_packets);
9214
9215 stats->rx_bytes = old_stats->rx_bytes +
9216 get_stat64(&hw_stats->rx_octets);
9217 stats->tx_bytes = old_stats->tx_bytes +
9218 get_stat64(&hw_stats->tx_octets);
9219
9220 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -07009221 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009222 stats->tx_errors = old_stats->tx_errors +
9223 get_stat64(&hw_stats->tx_errors) +
9224 get_stat64(&hw_stats->tx_mac_errors) +
9225 get_stat64(&hw_stats->tx_carrier_sense_errors) +
9226 get_stat64(&hw_stats->tx_discards);
9227
9228 stats->multicast = old_stats->multicast +
9229 get_stat64(&hw_stats->rx_mcast_packets);
9230 stats->collisions = old_stats->collisions +
9231 get_stat64(&hw_stats->tx_collisions);
9232
9233 stats->rx_length_errors = old_stats->rx_length_errors +
9234 get_stat64(&hw_stats->rx_frame_too_long_errors) +
9235 get_stat64(&hw_stats->rx_undersize_packets);
9236
9237 stats->rx_over_errors = old_stats->rx_over_errors +
9238 get_stat64(&hw_stats->rxbds_empty);
9239 stats->rx_frame_errors = old_stats->rx_frame_errors +
9240 get_stat64(&hw_stats->rx_align_errors);
9241 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
9242 get_stat64(&hw_stats->tx_discards);
9243 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
9244 get_stat64(&hw_stats->tx_carrier_sense_errors);
9245
9246 stats->rx_crc_errors = old_stats->rx_crc_errors +
9247 calc_crc_errors(tp);
9248
John W. Linville4f63b872005-09-12 14:43:18 -07009249 stats->rx_missed_errors = old_stats->rx_missed_errors +
9250 get_stat64(&hw_stats->rx_discards);
9251
Linus Torvalds1da177e2005-04-16 15:20:36 -07009252 return stats;
9253}
9254
9255static inline u32 calc_crc(unsigned char *buf, int len)
9256{
9257 u32 reg;
9258 u32 tmp;
9259 int j, k;
9260
9261 reg = 0xffffffff;
9262
9263 for (j = 0; j < len; j++) {
9264 reg ^= buf[j];
9265
9266 for (k = 0; k < 8; k++) {
9267 tmp = reg & 0x01;
9268
9269 reg >>= 1;
9270
Matt Carlson859a588792010-04-05 10:19:28 +00009271 if (tmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009272 reg ^= 0xedb88320;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009273 }
9274 }
9275
9276 return ~reg;
9277}
9278
9279static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
9280{
9281 /* accept or reject all multicast frames */
9282 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
9283 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
9284 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
9285 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
9286}
9287
9288static void __tg3_set_rx_mode(struct net_device *dev)
9289{
9290 struct tg3 *tp = netdev_priv(dev);
9291 u32 rx_mode;
9292
9293 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
9294 RX_MODE_KEEP_VLAN_TAG);
9295
9296 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
9297 * flag clear.
9298 */
9299#if TG3_VLAN_TAG_USED
9300 if (!tp->vlgrp &&
9301 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
9302 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9303#else
9304 /* By definition, VLAN is disabled always in this
9305 * case.
9306 */
9307 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
9308 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9309#endif
9310
9311 if (dev->flags & IFF_PROMISC) {
9312 /* Promiscuous mode. */
9313 rx_mode |= RX_MODE_PROMISC;
9314 } else if (dev->flags & IFF_ALLMULTI) {
9315 /* Accept all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +00009316 tg3_set_multi(tp, 1);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00009317 } else if (netdev_mc_empty(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009318 /* Reject all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +00009319 tg3_set_multi(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009320 } else {
9321 /* Accept one or more multicast(s). */
Jiri Pirko22bedad32010-04-01 21:22:57 +00009322 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009323 u32 mc_filter[4] = { 0, };
9324 u32 regidx;
9325 u32 bit;
9326 u32 crc;
9327
Jiri Pirko22bedad32010-04-01 21:22:57 +00009328 netdev_for_each_mc_addr(ha, dev) {
9329 crc = calc_crc(ha->addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009330 bit = ~crc & 0x7f;
9331 regidx = (bit & 0x60) >> 5;
9332 bit &= 0x1f;
9333 mc_filter[regidx] |= (1 << bit);
9334 }
9335
9336 tw32(MAC_HASH_REG_0, mc_filter[0]);
9337 tw32(MAC_HASH_REG_1, mc_filter[1]);
9338 tw32(MAC_HASH_REG_2, mc_filter[2]);
9339 tw32(MAC_HASH_REG_3, mc_filter[3]);
9340 }
9341
9342 if (rx_mode != tp->rx_mode) {
9343 tp->rx_mode = rx_mode;
9344 tw32_f(MAC_RX_MODE, rx_mode);
9345 udelay(10);
9346 }
9347}
9348
9349static void tg3_set_rx_mode(struct net_device *dev)
9350{
9351 struct tg3 *tp = netdev_priv(dev);
9352
Michael Chane75f7c92006-03-20 21:33:26 -08009353 if (!netif_running(dev))
9354 return;
9355
David S. Millerf47c11e2005-06-24 20:18:35 -07009356 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009357 __tg3_set_rx_mode(dev);
David S. Millerf47c11e2005-06-24 20:18:35 -07009358 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009359}
9360
9361#define TG3_REGDUMP_LEN (32 * 1024)
9362
9363static int tg3_get_regs_len(struct net_device *dev)
9364{
9365 return TG3_REGDUMP_LEN;
9366}
9367
9368static void tg3_get_regs(struct net_device *dev,
9369 struct ethtool_regs *regs, void *_p)
9370{
9371 u32 *p = _p;
9372 struct tg3 *tp = netdev_priv(dev);
9373 u8 *orig_p = _p;
9374 int i;
9375
9376 regs->version = 0;
9377
9378 memset(p, 0, TG3_REGDUMP_LEN);
9379
Michael Chanbc1c7562006-03-20 17:48:03 -08009380 if (tp->link_config.phy_is_low_power)
9381 return;
9382
David S. Millerf47c11e2005-06-24 20:18:35 -07009383 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009384
9385#define __GET_REG32(reg) (*(p)++ = tr32(reg))
Matt Carlsonbe98da62010-07-11 09:31:46 +00009386#define GET_REG32_LOOP(base, len) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07009387do { p = (u32 *)(orig_p + (base)); \
9388 for (i = 0; i < len; i += 4) \
9389 __GET_REG32((base) + i); \
9390} while (0)
9391#define GET_REG32_1(reg) \
9392do { p = (u32 *)(orig_p + (reg)); \
9393 __GET_REG32((reg)); \
9394} while (0)
9395
9396 GET_REG32_LOOP(TG3PCI_VENDOR, 0xb0);
9397 GET_REG32_LOOP(MAILBOX_INTERRUPT_0, 0x200);
9398 GET_REG32_LOOP(MAC_MODE, 0x4f0);
9399 GET_REG32_LOOP(SNDDATAI_MODE, 0xe0);
9400 GET_REG32_1(SNDDATAC_MODE);
9401 GET_REG32_LOOP(SNDBDS_MODE, 0x80);
9402 GET_REG32_LOOP(SNDBDI_MODE, 0x48);
9403 GET_REG32_1(SNDBDC_MODE);
9404 GET_REG32_LOOP(RCVLPC_MODE, 0x20);
9405 GET_REG32_LOOP(RCVLPC_SELLST_BASE, 0x15c);
9406 GET_REG32_LOOP(RCVDBDI_MODE, 0x0c);
9407 GET_REG32_LOOP(RCVDBDI_JUMBO_BD, 0x3c);
9408 GET_REG32_LOOP(RCVDBDI_BD_PROD_IDX_0, 0x44);
9409 GET_REG32_1(RCVDCC_MODE);
9410 GET_REG32_LOOP(RCVBDI_MODE, 0x20);
9411 GET_REG32_LOOP(RCVCC_MODE, 0x14);
9412 GET_REG32_LOOP(RCVLSC_MODE, 0x08);
9413 GET_REG32_1(MBFREE_MODE);
9414 GET_REG32_LOOP(HOSTCC_MODE, 0x100);
9415 GET_REG32_LOOP(MEMARB_MODE, 0x10);
9416 GET_REG32_LOOP(BUFMGR_MODE, 0x58);
9417 GET_REG32_LOOP(RDMAC_MODE, 0x08);
9418 GET_REG32_LOOP(WDMAC_MODE, 0x08);
Chris Elmquist091465d2005-12-20 13:25:19 -08009419 GET_REG32_1(RX_CPU_MODE);
9420 GET_REG32_1(RX_CPU_STATE);
9421 GET_REG32_1(RX_CPU_PGMCTR);
9422 GET_REG32_1(RX_CPU_HWBKPT);
9423 GET_REG32_1(TX_CPU_MODE);
9424 GET_REG32_1(TX_CPU_STATE);
9425 GET_REG32_1(TX_CPU_PGMCTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009426 GET_REG32_LOOP(GRCMBOX_INTERRUPT_0, 0x110);
9427 GET_REG32_LOOP(FTQ_RESET, 0x120);
9428 GET_REG32_LOOP(MSGINT_MODE, 0x0c);
9429 GET_REG32_1(DMAC_MODE);
9430 GET_REG32_LOOP(GRC_MODE, 0x4c);
9431 if (tp->tg3_flags & TG3_FLAG_NVRAM)
9432 GET_REG32_LOOP(NVRAM_CMD, 0x24);
9433
9434#undef __GET_REG32
9435#undef GET_REG32_LOOP
9436#undef GET_REG32_1
9437
David S. Millerf47c11e2005-06-24 20:18:35 -07009438 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009439}
9440
9441static int tg3_get_eeprom_len(struct net_device *dev)
9442{
9443 struct tg3 *tp = netdev_priv(dev);
9444
9445 return tp->nvram_size;
9446}
9447
Linus Torvalds1da177e2005-04-16 15:20:36 -07009448static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9449{
9450 struct tg3 *tp = netdev_priv(dev);
9451 int ret;
9452 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -08009453 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009454 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009455
Matt Carlsondf259d82009-04-20 06:57:14 +00009456 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM)
9457 return -EINVAL;
9458
Michael Chanbc1c7562006-03-20 17:48:03 -08009459 if (tp->link_config.phy_is_low_power)
9460 return -EAGAIN;
9461
Linus Torvalds1da177e2005-04-16 15:20:36 -07009462 offset = eeprom->offset;
9463 len = eeprom->len;
9464 eeprom->len = 0;
9465
9466 eeprom->magic = TG3_EEPROM_MAGIC;
9467
9468 if (offset & 3) {
9469 /* adjustments to start on required 4 byte boundary */
9470 b_offset = offset & 3;
9471 b_count = 4 - b_offset;
9472 if (b_count > len) {
9473 /* i.e. offset=1 len=2 */
9474 b_count = len;
9475 }
Matt Carlsona9dc5292009-02-25 14:25:30 +00009476 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009477 if (ret)
9478 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +00009479 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009480 len -= b_count;
9481 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00009482 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009483 }
9484
9485 /* read bytes upto the last 4 byte boundary */
9486 pd = &data[eeprom->len];
9487 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +00009488 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009489 if (ret) {
9490 eeprom->len += i;
9491 return ret;
9492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009493 memcpy(pd + i, &val, 4);
9494 }
9495 eeprom->len += i;
9496
9497 if (len & 3) {
9498 /* read last bytes not ending on 4 byte boundary */
9499 pd = &data[eeprom->len];
9500 b_count = len & 3;
9501 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009502 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009503 if (ret)
9504 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009505 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009506 eeprom->len += b_count;
9507 }
9508 return 0;
9509}
9510
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009511static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009512
9513static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9514{
9515 struct tg3 *tp = netdev_priv(dev);
9516 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009517 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009518 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009519 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009520
Michael Chanbc1c7562006-03-20 17:48:03 -08009521 if (tp->link_config.phy_is_low_power)
9522 return -EAGAIN;
9523
Matt Carlsondf259d82009-04-20 06:57:14 +00009524 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
9525 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009526 return -EINVAL;
9527
9528 offset = eeprom->offset;
9529 len = eeprom->len;
9530
9531 if ((b_offset = (offset & 3))) {
9532 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +00009533 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009534 if (ret)
9535 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009536 len += b_offset;
9537 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -07009538 if (len < 4)
9539 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009540 }
9541
9542 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -07009543 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009544 /* adjustments to end on required 4 byte boundary */
9545 odd_len = 1;
9546 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009547 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009548 if (ret)
9549 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009550 }
9551
9552 buf = data;
9553 if (b_offset || odd_len) {
9554 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009555 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009556 return -ENOMEM;
9557 if (b_offset)
9558 memcpy(buf, &start, 4);
9559 if (odd_len)
9560 memcpy(buf+len-4, &end, 4);
9561 memcpy(buf + b_offset, data, eeprom->len);
9562 }
9563
9564 ret = tg3_nvram_write_block(tp, offset, len, buf);
9565
9566 if (buf != data)
9567 kfree(buf);
9568
9569 return ret;
9570}
9571
9572static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9573{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009574 struct tg3 *tp = netdev_priv(dev);
9575
9576 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009577 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009578 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
9579 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009580 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
9581 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009582 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009583
Linus Torvalds1da177e2005-04-16 15:20:36 -07009584 cmd->supported = (SUPPORTED_Autoneg);
9585
9586 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY))
9587 cmd->supported |= (SUPPORTED_1000baseT_Half |
9588 SUPPORTED_1000baseT_Full);
9589
Karsten Keilef348142006-05-12 12:49:08 -07009590 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009591 cmd->supported |= (SUPPORTED_100baseT_Half |
9592 SUPPORTED_100baseT_Full |
9593 SUPPORTED_10baseT_Half |
9594 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -08009595 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -07009596 cmd->port = PORT_TP;
9597 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009598 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -07009599 cmd->port = PORT_FIBRE;
9600 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009601
Linus Torvalds1da177e2005-04-16 15:20:36 -07009602 cmd->advertising = tp->link_config.advertising;
9603 if (netif_running(dev)) {
9604 cmd->speed = tp->link_config.active_speed;
9605 cmd->duplex = tp->link_config.active_duplex;
9606 }
Matt Carlson882e9792009-09-01 13:21:36 +00009607 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +00009608 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009609 cmd->autoneg = tp->link_config.autoneg;
9610 cmd->maxtxpkt = 0;
9611 cmd->maxrxpkt = 0;
9612 return 0;
9613}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009614
Linus Torvalds1da177e2005-04-16 15:20:36 -07009615static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9616{
9617 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009618
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009619 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009620 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009621 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
9622 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009623 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
9624 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009625 }
9626
Matt Carlson7e5856b2009-02-25 14:23:01 +00009627 if (cmd->autoneg != AUTONEG_ENABLE &&
9628 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -07009629 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +00009630
9631 if (cmd->autoneg == AUTONEG_DISABLE &&
9632 cmd->duplex != DUPLEX_FULL &&
9633 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -07009634 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009635
Matt Carlson7e5856b2009-02-25 14:23:01 +00009636 if (cmd->autoneg == AUTONEG_ENABLE) {
9637 u32 mask = ADVERTISED_Autoneg |
9638 ADVERTISED_Pause |
9639 ADVERTISED_Asym_Pause;
9640
Julia Lawall3f07d122010-03-13 12:22:16 -08009641 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +00009642 mask |= ADVERTISED_1000baseT_Half |
9643 ADVERTISED_1000baseT_Full;
9644
9645 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES))
9646 mask |= ADVERTISED_100baseT_Half |
9647 ADVERTISED_100baseT_Full |
9648 ADVERTISED_10baseT_Half |
9649 ADVERTISED_10baseT_Full |
9650 ADVERTISED_TP;
9651 else
9652 mask |= ADVERTISED_FIBRE;
9653
9654 if (cmd->advertising & ~mask)
9655 return -EINVAL;
9656
9657 mask &= (ADVERTISED_1000baseT_Half |
9658 ADVERTISED_1000baseT_Full |
9659 ADVERTISED_100baseT_Half |
9660 ADVERTISED_100baseT_Full |
9661 ADVERTISED_10baseT_Half |
9662 ADVERTISED_10baseT_Full);
9663
9664 cmd->advertising &= mask;
9665 } else {
9666 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES) {
9667 if (cmd->speed != SPEED_1000)
9668 return -EINVAL;
9669
9670 if (cmd->duplex != DUPLEX_FULL)
9671 return -EINVAL;
9672 } else {
9673 if (cmd->speed != SPEED_100 &&
9674 cmd->speed != SPEED_10)
9675 return -EINVAL;
9676 }
9677 }
9678
David S. Millerf47c11e2005-06-24 20:18:35 -07009679 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009680
9681 tp->link_config.autoneg = cmd->autoneg;
9682 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -07009683 tp->link_config.advertising = (cmd->advertising |
9684 ADVERTISED_Autoneg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009685 tp->link_config.speed = SPEED_INVALID;
9686 tp->link_config.duplex = DUPLEX_INVALID;
9687 } else {
9688 tp->link_config.advertising = 0;
9689 tp->link_config.speed = cmd->speed;
9690 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009691 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009692
Michael Chan24fcad62006-12-17 17:06:46 -08009693 tp->link_config.orig_speed = tp->link_config.speed;
9694 tp->link_config.orig_duplex = tp->link_config.duplex;
9695 tp->link_config.orig_autoneg = tp->link_config.autoneg;
9696
Linus Torvalds1da177e2005-04-16 15:20:36 -07009697 if (netif_running(dev))
9698 tg3_setup_phy(tp, 1);
9699
David S. Millerf47c11e2005-06-24 20:18:35 -07009700 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009701
Linus Torvalds1da177e2005-04-16 15:20:36 -07009702 return 0;
9703}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009704
Linus Torvalds1da177e2005-04-16 15:20:36 -07009705static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
9706{
9707 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009708
Linus Torvalds1da177e2005-04-16 15:20:36 -07009709 strcpy(info->driver, DRV_MODULE_NAME);
9710 strcpy(info->version, DRV_MODULE_VERSION);
Michael Chanc4e65752006-03-20 22:29:32 -08009711 strcpy(info->fw_version, tp->fw_ver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009712 strcpy(info->bus_info, pci_name(tp->pdev));
9713}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009714
Linus Torvalds1da177e2005-04-16 15:20:36 -07009715static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
9716{
9717 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009718
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009719 if ((tp->tg3_flags & TG3_FLAG_WOL_CAP) &&
9720 device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -07009721 wol->supported = WAKE_MAGIC;
9722 else
9723 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009724 wol->wolopts = 0;
Matt Carlson05ac4cb2008-11-03 16:53:46 -08009725 if ((tp->tg3_flags & TG3_FLAG_WOL_ENABLE) &&
9726 device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009727 wol->wolopts = WAKE_MAGIC;
9728 memset(&wol->sopass, 0, sizeof(wol->sopass));
9729}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009730
Linus Torvalds1da177e2005-04-16 15:20:36 -07009731static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
9732{
9733 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009734 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009735
Linus Torvalds1da177e2005-04-16 15:20:36 -07009736 if (wol->wolopts & ~WAKE_MAGIC)
9737 return -EINVAL;
9738 if ((wol->wolopts & WAKE_MAGIC) &&
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009739 !((tp->tg3_flags & TG3_FLAG_WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009740 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009741
David S. Millerf47c11e2005-06-24 20:18:35 -07009742 spin_lock_bh(&tp->lock);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009743 if (wol->wolopts & WAKE_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009744 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009745 device_set_wakeup_enable(dp, true);
9746 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009747 tp->tg3_flags &= ~TG3_FLAG_WOL_ENABLE;
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009748 device_set_wakeup_enable(dp, false);
9749 }
David S. Millerf47c11e2005-06-24 20:18:35 -07009750 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009751
Linus Torvalds1da177e2005-04-16 15:20:36 -07009752 return 0;
9753}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009754
Linus Torvalds1da177e2005-04-16 15:20:36 -07009755static u32 tg3_get_msglevel(struct net_device *dev)
9756{
9757 struct tg3 *tp = netdev_priv(dev);
9758 return tp->msg_enable;
9759}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009760
Linus Torvalds1da177e2005-04-16 15:20:36 -07009761static void tg3_set_msglevel(struct net_device *dev, u32 value)
9762{
9763 struct tg3 *tp = netdev_priv(dev);
9764 tp->msg_enable = value;
9765}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009766
Linus Torvalds1da177e2005-04-16 15:20:36 -07009767static int tg3_set_tso(struct net_device *dev, u32 value)
9768{
9769 struct tg3 *tp = netdev_priv(dev);
9770
9771 if (!(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
9772 if (value)
9773 return -EINVAL;
9774 return 0;
9775 }
Matt Carlson027455a2008-12-21 20:19:30 -08009776 if ((dev->features & NETIF_F_IPV6_CSUM) &&
Matt Carlsone849cdc2009-11-13 13:03:38 +00009777 ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_2) ||
9778 (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3))) {
Matt Carlson9936bcf2007-10-10 18:03:07 -07009779 if (value) {
Michael Chanb0026622006-07-03 19:42:14 -07009780 dev->features |= NETIF_F_TSO6;
Matt Carlsone849cdc2009-11-13 13:03:38 +00009781 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
9782 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -07009783 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
9784 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Matt Carlson321d32a2008-11-21 17:22:19 -08009785 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsone849cdc2009-11-13 13:03:38 +00009786 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson9936bcf2007-10-10 18:03:07 -07009787 dev->features |= NETIF_F_TSO_ECN;
9788 } else
9789 dev->features &= ~(NETIF_F_TSO6 | NETIF_F_TSO_ECN);
Michael Chanb0026622006-07-03 19:42:14 -07009790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009791 return ethtool_op_set_tso(dev, value);
9792}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009793
Linus Torvalds1da177e2005-04-16 15:20:36 -07009794static int tg3_nway_reset(struct net_device *dev)
9795{
9796 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009797 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009798
Linus Torvalds1da177e2005-04-16 15:20:36 -07009799 if (!netif_running(dev))
9800 return -EAGAIN;
9801
Michael Chanc94e3942005-09-27 12:12:42 -07009802 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
9803 return -EINVAL;
9804
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009805 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
9806 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
9807 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009808 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009809 } else {
9810 u32 bmcr;
9811
9812 spin_lock_bh(&tp->lock);
9813 r = -EINVAL;
9814 tg3_readphy(tp, MII_BMCR, &bmcr);
9815 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
9816 ((bmcr & BMCR_ANENABLE) ||
9817 (tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT))) {
9818 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
9819 BMCR_ANENABLE);
9820 r = 0;
9821 }
9822 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009823 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009824
Linus Torvalds1da177e2005-04-16 15:20:36 -07009825 return r;
9826}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009827
Linus Torvalds1da177e2005-04-16 15:20:36 -07009828static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
9829{
9830 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009831
Linus Torvalds1da177e2005-04-16 15:20:36 -07009832 ering->rx_max_pending = TG3_RX_RING_SIZE - 1;
9833 ering->rx_mini_max_pending = 0;
Michael Chan4f81c322006-03-20 21:33:42 -08009834 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE)
9835 ering->rx_jumbo_max_pending = TG3_RX_JUMBO_RING_SIZE - 1;
9836 else
9837 ering->rx_jumbo_max_pending = 0;
9838
9839 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009840
9841 ering->rx_pending = tp->rx_pending;
9842 ering->rx_mini_pending = 0;
Michael Chan4f81c322006-03-20 21:33:42 -08009843 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE)
9844 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
9845 else
9846 ering->rx_jumbo_pending = 0;
9847
Matt Carlsonf3f3f272009-08-28 14:03:21 +00009848 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009849}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009850
Linus Torvalds1da177e2005-04-16 15:20:36 -07009851static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
9852{
9853 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +00009854 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009855
Linus Torvalds1da177e2005-04-16 15:20:36 -07009856 if ((ering->rx_pending > TG3_RX_RING_SIZE - 1) ||
9857 (ering->rx_jumbo_pending > TG3_RX_JUMBO_RING_SIZE - 1) ||
Michael Chanbc3a9252006-10-18 20:55:18 -07009858 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
9859 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Michael Chan7f62ad52007-02-20 23:25:40 -08009860 ((tp->tg3_flags2 & TG3_FLG2_TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -07009861 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009862 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009863
Michael Chanbbe832c2005-06-24 20:20:04 -07009864 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009865 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009866 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -07009867 irq_sync = 1;
9868 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009869
Michael Chanbbe832c2005-06-24 20:20:04 -07009870 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009871
Linus Torvalds1da177e2005-04-16 15:20:36 -07009872 tp->rx_pending = ering->rx_pending;
9873
9874 if ((tp->tg3_flags2 & TG3_FLG2_MAX_RXPEND_64) &&
9875 tp->rx_pending > 63)
9876 tp->rx_pending = 63;
9877 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +00009878
9879 for (i = 0; i < TG3_IRQ_MAX_VECS; i++)
9880 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009881
9882 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -07009883 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -07009884 err = tg3_restart_hw(tp, 1);
9885 if (!err)
9886 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009887 }
9888
David S. Millerf47c11e2005-06-24 20:18:35 -07009889 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009890
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009891 if (irq_sync && !err)
9892 tg3_phy_start(tp);
9893
Michael Chanb9ec6c12006-07-25 16:37:27 -07009894 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009895}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009896
Linus Torvalds1da177e2005-04-16 15:20:36 -07009897static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
9898{
9899 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009900
Linus Torvalds1da177e2005-04-16 15:20:36 -07009901 epause->autoneg = (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG) != 0;
Matt Carlson8d018622007-12-20 20:05:44 -08009902
Steve Glendinninge18ce342008-12-16 02:00:00 -08009903 if (tp->link_config.active_flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -08009904 epause->rx_pause = 1;
9905 else
9906 epause->rx_pause = 0;
9907
Steve Glendinninge18ce342008-12-16 02:00:00 -08009908 if (tp->link_config.active_flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -08009909 epause->tx_pause = 1;
9910 else
9911 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009912}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009913
Linus Torvalds1da177e2005-04-16 15:20:36 -07009914static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
9915{
9916 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009917 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009918
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009919 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson27121682010-02-17 15:16:57 +00009920 u32 newadv;
9921 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009922
Matt Carlson27121682010-02-17 15:16:57 +00009923 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009924
Matt Carlson27121682010-02-17 15:16:57 +00009925 if (!(phydev->supported & SUPPORTED_Pause) ||
9926 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
9927 ((epause->rx_pause && !epause->tx_pause) ||
9928 (!epause->rx_pause && epause->tx_pause))))
9929 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009930
Matt Carlson27121682010-02-17 15:16:57 +00009931 tp->link_config.flowctrl = 0;
9932 if (epause->rx_pause) {
9933 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009934
Matt Carlson27121682010-02-17 15:16:57 +00009935 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -08009936 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +00009937 newadv = ADVERTISED_Pause;
9938 } else
9939 newadv = ADVERTISED_Pause |
9940 ADVERTISED_Asym_Pause;
9941 } else if (epause->tx_pause) {
9942 tp->link_config.flowctrl |= FLOW_CTRL_TX;
9943 newadv = ADVERTISED_Asym_Pause;
9944 } else
9945 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009946
Matt Carlson27121682010-02-17 15:16:57 +00009947 if (epause->autoneg)
9948 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
9949 else
9950 tp->tg3_flags &= ~TG3_FLAG_PAUSE_AUTONEG;
9951
9952 if (tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED) {
9953 u32 oldadv = phydev->advertising &
9954 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
9955 if (oldadv != newadv) {
9956 phydev->advertising &=
9957 ~(ADVERTISED_Pause |
9958 ADVERTISED_Asym_Pause);
9959 phydev->advertising |= newadv;
9960 if (phydev->autoneg) {
9961 /*
9962 * Always renegotiate the link to
9963 * inform our link partner of our
9964 * flow control settings, even if the
9965 * flow control is forced. Let
9966 * tg3_adjust_link() do the final
9967 * flow control setup.
9968 */
9969 return phy_start_aneg(phydev);
9970 }
9971 }
9972
9973 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009974 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +00009975 } else {
9976 tp->link_config.orig_advertising &=
9977 ~(ADVERTISED_Pause |
9978 ADVERTISED_Asym_Pause);
9979 tp->link_config.orig_advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009980 }
9981 } else {
9982 int irq_sync = 0;
9983
9984 if (netif_running(dev)) {
9985 tg3_netif_stop(tp);
9986 irq_sync = 1;
9987 }
9988
9989 tg3_full_lock(tp, irq_sync);
9990
9991 if (epause->autoneg)
9992 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
9993 else
9994 tp->tg3_flags &= ~TG3_FLAG_PAUSE_AUTONEG;
9995 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -08009996 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009997 else
Steve Glendinninge18ce342008-12-16 02:00:00 -08009998 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009999 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010000 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010001 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010002 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010003
10004 if (netif_running(dev)) {
10005 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10006 err = tg3_restart_hw(tp, 1);
10007 if (!err)
10008 tg3_netif_start(tp);
10009 }
10010
10011 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010012 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010013
Michael Chanb9ec6c12006-07-25 16:37:27 -070010014 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010015}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010016
Linus Torvalds1da177e2005-04-16 15:20:36 -070010017static u32 tg3_get_rx_csum(struct net_device *dev)
10018{
10019 struct tg3 *tp = netdev_priv(dev);
10020 return (tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) != 0;
10021}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010022
Linus Torvalds1da177e2005-04-16 15:20:36 -070010023static int tg3_set_rx_csum(struct net_device *dev, u32 data)
10024{
10025 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010026
Linus Torvalds1da177e2005-04-16 15:20:36 -070010027 if (tp->tg3_flags & TG3_FLAG_BROKEN_CHECKSUMS) {
10028 if (data != 0)
10029 return -EINVAL;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010030 return 0;
10031 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010032
David S. Millerf47c11e2005-06-24 20:18:35 -070010033 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010034 if (data)
10035 tp->tg3_flags |= TG3_FLAG_RX_CHECKSUMS;
10036 else
10037 tp->tg3_flags &= ~TG3_FLAG_RX_CHECKSUMS;
David S. Millerf47c11e2005-06-24 20:18:35 -070010038 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010039
Linus Torvalds1da177e2005-04-16 15:20:36 -070010040 return 0;
10041}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010042
Linus Torvalds1da177e2005-04-16 15:20:36 -070010043static int tg3_set_tx_csum(struct net_device *dev, u32 data)
10044{
10045 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010046
Linus Torvalds1da177e2005-04-16 15:20:36 -070010047 if (tp->tg3_flags & TG3_FLAG_BROKEN_CHECKSUMS) {
10048 if (data != 0)
10049 return -EINVAL;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010050 return 0;
10051 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010052
Matt Carlson321d32a2008-11-21 17:22:19 -080010053 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Michael Chan6460d942007-07-14 19:07:52 -070010054 ethtool_op_set_tx_ipv6_csum(dev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010055 else
Michael Chan9c27dbd2006-03-20 22:28:27 -080010056 ethtool_op_set_tx_csum(dev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010057
10058 return 0;
10059}
10060
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010061static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010062{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010063 switch (sset) {
10064 case ETH_SS_TEST:
10065 return TG3_NUM_TEST;
10066 case ETH_SS_STATS:
10067 return TG3_NUM_STATS;
10068 default:
10069 return -EOPNOTSUPP;
10070 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070010071}
10072
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010073static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010074{
10075 switch (stringset) {
10076 case ETH_SS_STATS:
10077 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
10078 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070010079 case ETH_SS_TEST:
10080 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
10081 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010082 default:
10083 WARN_ON(1); /* we need a WARN() */
10084 break;
10085 }
10086}
10087
Michael Chan4009a932005-09-05 17:52:54 -070010088static int tg3_phys_id(struct net_device *dev, u32 data)
10089{
10090 struct tg3 *tp = netdev_priv(dev);
10091 int i;
10092
10093 if (!netif_running(tp->dev))
10094 return -EAGAIN;
10095
10096 if (data == 0)
Stephen Hemminger759afc32008-02-23 19:51:59 -080010097 data = UINT_MAX / 2;
Michael Chan4009a932005-09-05 17:52:54 -070010098
10099 for (i = 0; i < (data * 2); i++) {
10100 if ((i % 2) == 0)
10101 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10102 LED_CTRL_1000MBPS_ON |
10103 LED_CTRL_100MBPS_ON |
10104 LED_CTRL_10MBPS_ON |
10105 LED_CTRL_TRAFFIC_OVERRIDE |
10106 LED_CTRL_TRAFFIC_BLINK |
10107 LED_CTRL_TRAFFIC_LED);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010108
Michael Chan4009a932005-09-05 17:52:54 -070010109 else
10110 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10111 LED_CTRL_TRAFFIC_OVERRIDE);
10112
10113 if (msleep_interruptible(500))
10114 break;
10115 }
10116 tw32(MAC_LED_CTRL, tp->led_ctrl);
10117 return 0;
10118}
10119
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010120static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010121 struct ethtool_stats *estats, u64 *tmp_stats)
10122{
10123 struct tg3 *tp = netdev_priv(dev);
10124 memcpy(tmp_stats, tg3_get_estats(tp), sizeof(tp->estats));
10125}
10126
Michael Chan566f86a2005-05-29 14:56:58 -070010127#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080010128#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
10129#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
10130#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Michael Chanb16250e2006-09-27 16:10:14 -070010131#define NVRAM_SELFBOOT_HW_SIZE 0x20
10132#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070010133
10134static int tg3_test_nvram(struct tg3 *tp)
10135{
Al Virob9fc7dc2007-12-17 22:59:57 -080010136 u32 csum, magic;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010137 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010138 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070010139
Matt Carlsondf259d82009-04-20 06:57:14 +000010140 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM)
10141 return 0;
10142
Matt Carlsone4f34112009-02-25 14:25:00 +000010143 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080010144 return -EIO;
10145
Michael Chan1b277772006-03-20 22:27:48 -080010146 if (magic == TG3_EEPROM_MAGIC)
10147 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070010148 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080010149 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
10150 TG3_EEPROM_SB_FORMAT_1) {
10151 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
10152 case TG3_EEPROM_SB_REVISION_0:
10153 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
10154 break;
10155 case TG3_EEPROM_SB_REVISION_2:
10156 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
10157 break;
10158 case TG3_EEPROM_SB_REVISION_3:
10159 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
10160 break;
10161 default:
10162 return 0;
10163 }
10164 } else
Michael Chan1b277772006-03-20 22:27:48 -080010165 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070010166 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
10167 size = NVRAM_SELFBOOT_HW_SIZE;
10168 else
Michael Chan1b277772006-03-20 22:27:48 -080010169 return -EIO;
10170
10171 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070010172 if (buf == NULL)
10173 return -ENOMEM;
10174
Michael Chan1b277772006-03-20 22:27:48 -080010175 err = -EIO;
10176 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010177 err = tg3_nvram_read_be32(tp, i, &buf[j]);
10178 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070010179 break;
Michael Chan566f86a2005-05-29 14:56:58 -070010180 }
Michael Chan1b277772006-03-20 22:27:48 -080010181 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070010182 goto out;
10183
Michael Chan1b277772006-03-20 22:27:48 -080010184 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010185 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080010186 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010187 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080010188 u8 *buf8 = (u8 *) buf, csum8 = 0;
10189
Al Virob9fc7dc2007-12-17 22:59:57 -080010190 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080010191 TG3_EEPROM_SB_REVISION_2) {
10192 /* For rev 2, the csum doesn't include the MBA. */
10193 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
10194 csum8 += buf8[i];
10195 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
10196 csum8 += buf8[i];
10197 } else {
10198 for (i = 0; i < size; i++)
10199 csum8 += buf8[i];
10200 }
Michael Chan1b277772006-03-20 22:27:48 -080010201
Adrian Bunkad96b482006-04-05 22:21:04 -070010202 if (csum8 == 0) {
10203 err = 0;
10204 goto out;
10205 }
10206
10207 err = -EIO;
10208 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080010209 }
Michael Chan566f86a2005-05-29 14:56:58 -070010210
Al Virob9fc7dc2007-12-17 22:59:57 -080010211 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010212 TG3_EEPROM_MAGIC_HW) {
10213 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000010214 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070010215 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070010216
10217 /* Separate the parity bits and the data bytes. */
10218 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
10219 if ((i == 0) || (i == 8)) {
10220 int l;
10221 u8 msk;
10222
10223 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
10224 parity[k++] = buf8[i] & msk;
10225 i++;
Matt Carlson859a588792010-04-05 10:19:28 +000010226 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070010227 int l;
10228 u8 msk;
10229
10230 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
10231 parity[k++] = buf8[i] & msk;
10232 i++;
10233
10234 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
10235 parity[k++] = buf8[i] & msk;
10236 i++;
10237 }
10238 data[j++] = buf8[i];
10239 }
10240
10241 err = -EIO;
10242 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
10243 u8 hw8 = hweight8(data[i]);
10244
10245 if ((hw8 & 0x1) && parity[i])
10246 goto out;
10247 else if (!(hw8 & 0x1) && !parity[i])
10248 goto out;
10249 }
10250 err = 0;
10251 goto out;
10252 }
10253
Michael Chan566f86a2005-05-29 14:56:58 -070010254 /* Bootstrap checksum at offset 0x10 */
10255 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlsona9dc5292009-02-25 14:25:30 +000010256 if (csum != be32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070010257 goto out;
10258
10259 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
10260 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlsona9dc5292009-02-25 14:25:30 +000010261 if (csum != be32_to_cpu(buf[0xfc/4]))
10262 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070010263
10264 err = 0;
10265
10266out:
10267 kfree(buf);
10268 return err;
10269}
10270
Michael Chanca430072005-05-29 14:57:23 -070010271#define TG3_SERDES_TIMEOUT_SEC 2
10272#define TG3_COPPER_TIMEOUT_SEC 6
10273
10274static int tg3_test_link(struct tg3 *tp)
10275{
10276 int i, max;
10277
10278 if (!netif_running(tp->dev))
10279 return -ENODEV;
10280
Michael Chan4c987482005-09-05 17:52:38 -070010281 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070010282 max = TG3_SERDES_TIMEOUT_SEC;
10283 else
10284 max = TG3_COPPER_TIMEOUT_SEC;
10285
10286 for (i = 0; i < max; i++) {
10287 if (netif_carrier_ok(tp->dev))
10288 return 0;
10289
10290 if (msleep_interruptible(1000))
10291 break;
10292 }
10293
10294 return -EIO;
10295}
10296
Michael Chana71116d2005-05-29 14:58:11 -070010297/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080010298static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070010299{
Michael Chanb16250e2006-09-27 16:10:14 -070010300 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070010301 u32 offset, read_mask, write_mask, val, save_val, read_val;
10302 static struct {
10303 u16 offset;
10304 u16 flags;
10305#define TG3_FL_5705 0x1
10306#define TG3_FL_NOT_5705 0x2
10307#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070010308#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070010309 u32 read_mask;
10310 u32 write_mask;
10311 } reg_tbl[] = {
10312 /* MAC Control Registers */
10313 { MAC_MODE, TG3_FL_NOT_5705,
10314 0x00000000, 0x00ef6f8c },
10315 { MAC_MODE, TG3_FL_5705,
10316 0x00000000, 0x01ef6b8c },
10317 { MAC_STATUS, TG3_FL_NOT_5705,
10318 0x03800107, 0x00000000 },
10319 { MAC_STATUS, TG3_FL_5705,
10320 0x03800100, 0x00000000 },
10321 { MAC_ADDR_0_HIGH, 0x0000,
10322 0x00000000, 0x0000ffff },
10323 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010324 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070010325 { MAC_RX_MTU_SIZE, 0x0000,
10326 0x00000000, 0x0000ffff },
10327 { MAC_TX_MODE, 0x0000,
10328 0x00000000, 0x00000070 },
10329 { MAC_TX_LENGTHS, 0x0000,
10330 0x00000000, 0x00003fff },
10331 { MAC_RX_MODE, TG3_FL_NOT_5705,
10332 0x00000000, 0x000007fc },
10333 { MAC_RX_MODE, TG3_FL_5705,
10334 0x00000000, 0x000007dc },
10335 { MAC_HASH_REG_0, 0x0000,
10336 0x00000000, 0xffffffff },
10337 { MAC_HASH_REG_1, 0x0000,
10338 0x00000000, 0xffffffff },
10339 { MAC_HASH_REG_2, 0x0000,
10340 0x00000000, 0xffffffff },
10341 { MAC_HASH_REG_3, 0x0000,
10342 0x00000000, 0xffffffff },
10343
10344 /* Receive Data and Receive BD Initiator Control Registers. */
10345 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
10346 0x00000000, 0xffffffff },
10347 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
10348 0x00000000, 0xffffffff },
10349 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
10350 0x00000000, 0x00000003 },
10351 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
10352 0x00000000, 0xffffffff },
10353 { RCVDBDI_STD_BD+0, 0x0000,
10354 0x00000000, 0xffffffff },
10355 { RCVDBDI_STD_BD+4, 0x0000,
10356 0x00000000, 0xffffffff },
10357 { RCVDBDI_STD_BD+8, 0x0000,
10358 0x00000000, 0xffff0002 },
10359 { RCVDBDI_STD_BD+0xc, 0x0000,
10360 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010361
Michael Chana71116d2005-05-29 14:58:11 -070010362 /* Receive BD Initiator Control Registers. */
10363 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
10364 0x00000000, 0xffffffff },
10365 { RCVBDI_STD_THRESH, TG3_FL_5705,
10366 0x00000000, 0x000003ff },
10367 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
10368 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010369
Michael Chana71116d2005-05-29 14:58:11 -070010370 /* Host Coalescing Control Registers. */
10371 { HOSTCC_MODE, TG3_FL_NOT_5705,
10372 0x00000000, 0x00000004 },
10373 { HOSTCC_MODE, TG3_FL_5705,
10374 0x00000000, 0x000000f6 },
10375 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
10376 0x00000000, 0xffffffff },
10377 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
10378 0x00000000, 0x000003ff },
10379 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
10380 0x00000000, 0xffffffff },
10381 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
10382 0x00000000, 0x000003ff },
10383 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
10384 0x00000000, 0xffffffff },
10385 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10386 0x00000000, 0x000000ff },
10387 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
10388 0x00000000, 0xffffffff },
10389 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10390 0x00000000, 0x000000ff },
10391 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
10392 0x00000000, 0xffffffff },
10393 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
10394 0x00000000, 0xffffffff },
10395 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10396 0x00000000, 0xffffffff },
10397 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10398 0x00000000, 0x000000ff },
10399 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10400 0x00000000, 0xffffffff },
10401 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10402 0x00000000, 0x000000ff },
10403 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
10404 0x00000000, 0xffffffff },
10405 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
10406 0x00000000, 0xffffffff },
10407 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
10408 0x00000000, 0xffffffff },
10409 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
10410 0x00000000, 0xffffffff },
10411 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
10412 0x00000000, 0xffffffff },
10413 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
10414 0xffffffff, 0x00000000 },
10415 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
10416 0xffffffff, 0x00000000 },
10417
10418 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070010419 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010420 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070010421 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010422 0x00000000, 0x007fffff },
10423 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
10424 0x00000000, 0x0000003f },
10425 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
10426 0x00000000, 0x000001ff },
10427 { BUFMGR_MB_HIGH_WATER, 0x0000,
10428 0x00000000, 0x000001ff },
10429 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
10430 0xffffffff, 0x00000000 },
10431 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
10432 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010433
Michael Chana71116d2005-05-29 14:58:11 -070010434 /* Mailbox Registers */
10435 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
10436 0x00000000, 0x000001ff },
10437 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
10438 0x00000000, 0x000001ff },
10439 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
10440 0x00000000, 0x000007ff },
10441 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
10442 0x00000000, 0x000001ff },
10443
10444 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
10445 };
10446
Michael Chanb16250e2006-09-27 16:10:14 -070010447 is_5705 = is_5750 = 0;
10448 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
Michael Chana71116d2005-05-29 14:58:11 -070010449 is_5705 = 1;
Michael Chanb16250e2006-09-27 16:10:14 -070010450 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
10451 is_5750 = 1;
10452 }
Michael Chana71116d2005-05-29 14:58:11 -070010453
10454 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
10455 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
10456 continue;
10457
10458 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
10459 continue;
10460
10461 if ((tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
10462 (reg_tbl[i].flags & TG3_FL_NOT_5788))
10463 continue;
10464
Michael Chanb16250e2006-09-27 16:10:14 -070010465 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
10466 continue;
10467
Michael Chana71116d2005-05-29 14:58:11 -070010468 offset = (u32) reg_tbl[i].offset;
10469 read_mask = reg_tbl[i].read_mask;
10470 write_mask = reg_tbl[i].write_mask;
10471
10472 /* Save the original register content */
10473 save_val = tr32(offset);
10474
10475 /* Determine the read-only value. */
10476 read_val = save_val & read_mask;
10477
10478 /* Write zero to the register, then make sure the read-only bits
10479 * are not changed and the read/write bits are all zeros.
10480 */
10481 tw32(offset, 0);
10482
10483 val = tr32(offset);
10484
10485 /* Test the read-only and read/write bits. */
10486 if (((val & read_mask) != read_val) || (val & write_mask))
10487 goto out;
10488
10489 /* Write ones to all the bits defined by RdMask and WrMask, then
10490 * make sure the read-only bits are not changed and the
10491 * read/write bits are all ones.
10492 */
10493 tw32(offset, read_mask | write_mask);
10494
10495 val = tr32(offset);
10496
10497 /* Test the read-only bits. */
10498 if ((val & read_mask) != read_val)
10499 goto out;
10500
10501 /* Test the read/write bits. */
10502 if ((val & write_mask) != write_mask)
10503 goto out;
10504
10505 tw32(offset, save_val);
10506 }
10507
10508 return 0;
10509
10510out:
Michael Chan9f88f292006-12-07 00:22:54 -080010511 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000010512 netdev_err(tp->dev,
10513 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070010514 tw32(offset, save_val);
10515 return -EIO;
10516}
10517
Michael Chan7942e1d2005-05-29 14:58:36 -070010518static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
10519{
Arjan van de Venf71e1302006-03-03 21:33:57 -050010520 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070010521 int i;
10522 u32 j;
10523
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020010524 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070010525 for (j = 0; j < len; j += 4) {
10526 u32 val;
10527
10528 tg3_write_mem(tp, offset + j, test_pattern[i]);
10529 tg3_read_mem(tp, offset + j, &val);
10530 if (val != test_pattern[i])
10531 return -EIO;
10532 }
10533 }
10534 return 0;
10535}
10536
10537static int tg3_test_memory(struct tg3 *tp)
10538{
10539 static struct mem_entry {
10540 u32 offset;
10541 u32 len;
10542 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080010543 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070010544 { 0x00002000, 0x1c000},
10545 { 0xffffffff, 0x00000}
10546 }, mem_tbl_5705[] = {
10547 { 0x00000100, 0x0000c},
10548 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070010549 { 0x00004000, 0x00800},
10550 { 0x00006000, 0x01000},
10551 { 0x00008000, 0x02000},
10552 { 0x00010000, 0x0e000},
10553 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080010554 }, mem_tbl_5755[] = {
10555 { 0x00000200, 0x00008},
10556 { 0x00004000, 0x00800},
10557 { 0x00006000, 0x00800},
10558 { 0x00008000, 0x02000},
10559 { 0x00010000, 0x0c000},
10560 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070010561 }, mem_tbl_5906[] = {
10562 { 0x00000200, 0x00008},
10563 { 0x00004000, 0x00400},
10564 { 0x00006000, 0x00400},
10565 { 0x00008000, 0x01000},
10566 { 0x00010000, 0x01000},
10567 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000010568 }, mem_tbl_5717[] = {
10569 { 0x00000200, 0x00008},
10570 { 0x00010000, 0x0a000},
10571 { 0x00020000, 0x13c00},
10572 { 0xffffffff, 0x00000}
10573 }, mem_tbl_57765[] = {
10574 { 0x00000200, 0x00008},
10575 { 0x00004000, 0x00800},
10576 { 0x00006000, 0x09800},
10577 { 0x00010000, 0x0a000},
10578 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070010579 };
10580 struct mem_entry *mem_tbl;
10581 int err = 0;
10582 int i;
10583
Matt Carlsona50d0792010-06-05 17:24:37 +000010584 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
10585 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlson8b5a6c42010-01-20 16:58:06 +000010586 mem_tbl = mem_tbl_5717;
10587 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
10588 mem_tbl = mem_tbl_57765;
10589 else if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Matt Carlson321d32a2008-11-21 17:22:19 -080010590 mem_tbl = mem_tbl_5755;
10591 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
10592 mem_tbl = mem_tbl_5906;
10593 else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
10594 mem_tbl = mem_tbl_5705;
10595 else
Michael Chan7942e1d2005-05-29 14:58:36 -070010596 mem_tbl = mem_tbl_570x;
10597
10598 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000010599 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
10600 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070010601 break;
10602 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010603
Michael Chan7942e1d2005-05-29 14:58:36 -070010604 return err;
10605}
10606
Michael Chan9f40dea2005-09-05 17:53:06 -070010607#define TG3_MAC_LOOPBACK 0
10608#define TG3_PHY_LOOPBACK 1
10609
10610static int tg3_run_loopback(struct tg3 *tp, int loopback_mode)
Michael Chanc76949a2005-05-29 14:58:59 -070010611{
Michael Chan9f40dea2005-09-05 17:53:06 -070010612 u32 mac_mode, rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010613 u32 desc_idx, coal_now;
Michael Chanc76949a2005-05-29 14:58:59 -070010614 struct sk_buff *skb, *rx_skb;
10615 u8 *tx_data;
10616 dma_addr_t map;
10617 int num_pkts, tx_len, rx_len, i, err;
10618 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000010619 struct tg3_napi *tnapi, *rnapi;
Matt Carlson21f581a2009-08-28 14:00:25 +000010620 struct tg3_rx_prodring_set *tpr = &tp->prodring[0];
Michael Chanc76949a2005-05-29 14:58:59 -070010621
Matt Carlsonc8873402010-02-12 14:47:11 +000010622 tnapi = &tp->napi[0];
10623 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000010624 if (tp->irq_cnt > 1) {
Matt Carlson0c1d0e22009-09-01 13:16:33 +000010625 rnapi = &tp->napi[1];
Matt Carlsonc8873402010-02-12 14:47:11 +000010626 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
10627 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000010628 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010629 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000010630
Michael Chan9f40dea2005-09-05 17:53:06 -070010631 if (loopback_mode == TG3_MAC_LOOPBACK) {
Michael Chanc94e3942005-09-27 12:12:42 -070010632 /* HW errata - mac loopback fails in some cases on 5780.
10633 * Normal traffic and PHY loopback are not affected by
10634 * errata.
10635 */
10636 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780)
10637 return 0;
10638
Michael Chan9f40dea2005-09-05 17:53:06 -070010639 mac_mode = (tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK) |
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010640 MAC_MODE_PORT_INT_LPBACK;
10641 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
10642 mac_mode |= MAC_MODE_LINK_POLARITY;
Michael Chan3f7045c2006-09-27 16:02:29 -070010643 if (tp->tg3_flags & TG3_FLAG_10_100_ONLY)
10644 mac_mode |= MAC_MODE_PORT_MODE_MII;
10645 else
10646 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chan9f40dea2005-09-05 17:53:06 -070010647 tw32(MAC_MODE, mac_mode);
10648 } else if (loopback_mode == TG3_PHY_LOOPBACK) {
Michael Chan3f7045c2006-09-27 16:02:29 -070010649 u32 val;
10650
Matt Carlson7f97a4b2009-08-25 10:10:03 +000010651 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
10652 tg3_phy_fet_toggle_apd(tp, false);
Michael Chan5d64ad32006-12-07 00:19:40 -080010653 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED100;
10654 } else
10655 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED1000;
Michael Chan3f7045c2006-09-27 16:02:29 -070010656
Matt Carlson9ef8ca92007-07-11 19:48:29 -070010657 tg3_phy_toggle_automdix(tp, 0);
10658
Michael Chan3f7045c2006-09-27 16:02:29 -070010659 tg3_writephy(tp, MII_BMCR, val);
Michael Chanc94e3942005-09-27 12:12:42 -070010660 udelay(40);
Michael Chan5d64ad32006-12-07 00:19:40 -080010661
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010662 mac_mode = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000010663 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
Matt Carlson1061b7c2010-02-12 14:47:12 +000010664 tg3_writephy(tp, MII_TG3_FET_PTEST,
10665 MII_TG3_FET_PTEST_FRC_TX_LINK |
10666 MII_TG3_FET_PTEST_FRC_TX_LOCK);
10667 /* The write needs to be flushed for the AC131 */
10668 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
10669 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
Michael Chan5d64ad32006-12-07 00:19:40 -080010670 mac_mode |= MAC_MODE_PORT_MODE_MII;
10671 } else
10672 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chanb16250e2006-09-27 16:10:14 -070010673
Michael Chanc94e3942005-09-27 12:12:42 -070010674 /* reset to prevent losing 1st rx packet intermittently */
10675 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES) {
10676 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
10677 udelay(10);
10678 tw32_f(MAC_RX_MODE, tp->rx_mode);
10679 }
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010680 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlson79eb6902010-02-17 15:17:03 +000010681 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
10682 if (masked_phy_id == TG3_PHY_ID_BCM5401)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010683 mac_mode &= ~MAC_MODE_LINK_POLARITY;
Matt Carlson79eb6902010-02-17 15:17:03 +000010684 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010685 mac_mode |= MAC_MODE_LINK_POLARITY;
Michael Chanff18ff02006-03-27 23:17:27 -080010686 tg3_writephy(tp, MII_TG3_EXT_CTRL,
10687 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
10688 }
Michael Chan9f40dea2005-09-05 17:53:06 -070010689 tw32(MAC_MODE, mac_mode);
Matt Carlson859a588792010-04-05 10:19:28 +000010690 } else {
Michael Chan9f40dea2005-09-05 17:53:06 -070010691 return -EINVAL;
Matt Carlson859a588792010-04-05 10:19:28 +000010692 }
Michael Chanc76949a2005-05-29 14:58:59 -070010693
10694 err = -EIO;
10695
Michael Chanc76949a2005-05-29 14:58:59 -070010696 tx_len = 1514;
David S. Millera20e9c62006-07-31 22:38:16 -070010697 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070010698 if (!skb)
10699 return -ENOMEM;
10700
Michael Chanc76949a2005-05-29 14:58:59 -070010701 tx_data = skb_put(skb, tx_len);
10702 memcpy(tx_data, tp->dev->dev_addr, 6);
10703 memset(tx_data + 6, 0x0, 8);
10704
10705 tw32(MAC_RX_MTU_SIZE, tx_len + 4);
10706
10707 for (i = 14; i < tx_len; i++)
10708 tx_data[i] = (u8) (i & 0xff);
10709
Alexander Duyckf4188d82009-12-02 16:48:38 +000010710 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
10711 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000010712 dev_kfree_skb(skb);
10713 return -EIO;
10714 }
Michael Chanc76949a2005-05-29 14:58:59 -070010715
10716 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010717 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070010718
10719 udelay(10);
10720
Matt Carlson898a56f2009-08-28 14:02:40 +000010721 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070010722
Michael Chanc76949a2005-05-29 14:58:59 -070010723 num_pkts = 0;
10724
Alexander Duyckf4188d82009-12-02 16:48:38 +000010725 tg3_set_txd(tnapi, tnapi->tx_prod, map, tx_len, 0, 1);
Michael Chanc76949a2005-05-29 14:58:59 -070010726
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010727 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070010728 num_pkts++;
10729
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010730 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
10731 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070010732
10733 udelay(10);
10734
Matt Carlson303fc922009-11-02 14:27:34 +000010735 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
10736 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070010737 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010738 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070010739
10740 udelay(10);
10741
Matt Carlson898a56f2009-08-28 14:02:40 +000010742 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
10743 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010744 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070010745 (rx_idx == (rx_start_idx + num_pkts)))
10746 break;
10747 }
10748
Alexander Duyckf4188d82009-12-02 16:48:38 +000010749 pci_unmap_single(tp->pdev, map, tx_len, PCI_DMA_TODEVICE);
Michael Chanc76949a2005-05-29 14:58:59 -070010750 dev_kfree_skb(skb);
10751
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010752 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070010753 goto out;
10754
10755 if (rx_idx != rx_start_idx + num_pkts)
10756 goto out;
10757
Matt Carlson72334482009-08-28 14:03:01 +000010758 desc = &rnapi->rx_rcb[rx_start_idx];
Michael Chanc76949a2005-05-29 14:58:59 -070010759 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
10760 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
10761 if (opaque_key != RXD_OPAQUE_RING_STD)
10762 goto out;
10763
10764 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
10765 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
10766 goto out;
10767
10768 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) - 4;
10769 if (rx_len != tx_len)
10770 goto out;
10771
Matt Carlson21f581a2009-08-28 14:00:25 +000010772 rx_skb = tpr->rx_std_buffers[desc_idx].skb;
Michael Chanc76949a2005-05-29 14:58:59 -070010773
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +000010774 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx], mapping);
Michael Chanc76949a2005-05-29 14:58:59 -070010775 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len, PCI_DMA_FROMDEVICE);
10776
10777 for (i = 14; i < tx_len; i++) {
10778 if (*(rx_skb->data + i) != (u8) (i & 0xff))
10779 goto out;
10780 }
10781 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010782
Michael Chanc76949a2005-05-29 14:58:59 -070010783 /* tg3_free_rings will unmap and free the rx_skb */
10784out:
10785 return err;
10786}
10787
Michael Chan9f40dea2005-09-05 17:53:06 -070010788#define TG3_MAC_LOOPBACK_FAILED 1
10789#define TG3_PHY_LOOPBACK_FAILED 2
10790#define TG3_LOOPBACK_FAILED (TG3_MAC_LOOPBACK_FAILED | \
10791 TG3_PHY_LOOPBACK_FAILED)
10792
10793static int tg3_test_loopback(struct tg3 *tp)
10794{
10795 int err = 0;
Matt Carlson9936bcf2007-10-10 18:03:07 -070010796 u32 cpmuctrl = 0;
Michael Chan9f40dea2005-09-05 17:53:06 -070010797
10798 if (!netif_running(tp->dev))
10799 return TG3_LOOPBACK_FAILED;
10800
Michael Chanb9ec6c12006-07-25 16:37:27 -070010801 err = tg3_reset_hw(tp, 1);
10802 if (err)
10803 return TG3_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070010804
Matt Carlson6833c042008-11-21 17:18:59 -080010805 /* Turn off gphy autopowerdown. */
10806 if (tp->tg3_flags3 & TG3_FLG3_PHY_ENABLE_APD)
10807 tg3_phy_toggle_apd(tp, false);
10808
Matt Carlson321d32a2008-11-21 17:22:19 -080010809 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070010810 int i;
10811 u32 status;
10812
10813 tw32(TG3_CPMU_MUTEX_REQ, CPMU_MUTEX_REQ_DRIVER);
10814
10815 /* Wait for up to 40 microseconds to acquire lock. */
10816 for (i = 0; i < 4; i++) {
10817 status = tr32(TG3_CPMU_MUTEX_GNT);
10818 if (status == CPMU_MUTEX_GNT_DRIVER)
10819 break;
10820 udelay(10);
10821 }
10822
10823 if (status != CPMU_MUTEX_GNT_DRIVER)
10824 return TG3_LOOPBACK_FAILED;
10825
Matt Carlsonb2a5c192008-04-03 21:44:44 -070010826 /* Turn off link-based power management. */
Matt Carlsone8750932007-11-12 21:11:51 -080010827 cpmuctrl = tr32(TG3_CPMU_CTRL);
Matt Carlson109115e2008-05-02 16:48:59 -070010828 tw32(TG3_CPMU_CTRL,
10829 cpmuctrl & ~(CPMU_CTRL_LINK_SPEED_MODE |
10830 CPMU_CTRL_LINK_AWARE_MODE));
Matt Carlson9936bcf2007-10-10 18:03:07 -070010831 }
10832
Michael Chan9f40dea2005-09-05 17:53:06 -070010833 if (tg3_run_loopback(tp, TG3_MAC_LOOPBACK))
10834 err |= TG3_MAC_LOOPBACK_FAILED;
Matt Carlson9936bcf2007-10-10 18:03:07 -070010835
Matt Carlson321d32a2008-11-21 17:22:19 -080010836 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070010837 tw32(TG3_CPMU_CTRL, cpmuctrl);
10838
10839 /* Release the mutex */
10840 tw32(TG3_CPMU_MUTEX_GNT, CPMU_MUTEX_GNT_DRIVER);
10841 }
10842
Matt Carlsondd477002008-05-25 23:45:58 -070010843 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
10844 !(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)) {
Michael Chan9f40dea2005-09-05 17:53:06 -070010845 if (tg3_run_loopback(tp, TG3_PHY_LOOPBACK))
10846 err |= TG3_PHY_LOOPBACK_FAILED;
10847 }
10848
Matt Carlson6833c042008-11-21 17:18:59 -080010849 /* Re-enable gphy autopowerdown. */
10850 if (tp->tg3_flags3 & TG3_FLG3_PHY_ENABLE_APD)
10851 tg3_phy_toggle_apd(tp, true);
10852
Michael Chan9f40dea2005-09-05 17:53:06 -070010853 return err;
10854}
10855
Michael Chan4cafd3f2005-05-29 14:56:34 -070010856static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
10857 u64 *data)
10858{
Michael Chan566f86a2005-05-29 14:56:58 -070010859 struct tg3 *tp = netdev_priv(dev);
10860
Michael Chanbc1c7562006-03-20 17:48:03 -080010861 if (tp->link_config.phy_is_low_power)
10862 tg3_set_power_state(tp, PCI_D0);
10863
Michael Chan566f86a2005-05-29 14:56:58 -070010864 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
10865
10866 if (tg3_test_nvram(tp) != 0) {
10867 etest->flags |= ETH_TEST_FL_FAILED;
10868 data[0] = 1;
10869 }
Michael Chanca430072005-05-29 14:57:23 -070010870 if (tg3_test_link(tp) != 0) {
10871 etest->flags |= ETH_TEST_FL_FAILED;
10872 data[1] = 1;
10873 }
Michael Chana71116d2005-05-29 14:58:11 -070010874 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010875 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070010876
Michael Chanbbe832c2005-06-24 20:20:04 -070010877 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010878 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010879 tg3_netif_stop(tp);
10880 irq_sync = 1;
10881 }
10882
10883 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070010884
10885 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080010886 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070010887 tg3_halt_cpu(tp, RX_CPU_BASE);
10888 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
10889 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080010890 if (!err)
10891 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070010892
Michael Chand9ab5ad2006-03-20 22:27:35 -080010893 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)
10894 tg3_phy_reset(tp);
10895
Michael Chana71116d2005-05-29 14:58:11 -070010896 if (tg3_test_registers(tp) != 0) {
10897 etest->flags |= ETH_TEST_FL_FAILED;
10898 data[2] = 1;
10899 }
Michael Chan7942e1d2005-05-29 14:58:36 -070010900 if (tg3_test_memory(tp) != 0) {
10901 etest->flags |= ETH_TEST_FL_FAILED;
10902 data[3] = 1;
10903 }
Michael Chan9f40dea2005-09-05 17:53:06 -070010904 if ((data[4] = tg3_test_loopback(tp)) != 0)
Michael Chanc76949a2005-05-29 14:58:59 -070010905 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070010906
David S. Millerf47c11e2005-06-24 20:18:35 -070010907 tg3_full_unlock(tp);
10908
Michael Chand4bc3922005-05-29 14:59:20 -070010909 if (tg3_test_interrupt(tp) != 0) {
10910 etest->flags |= ETH_TEST_FL_FAILED;
10911 data[5] = 1;
10912 }
David S. Millerf47c11e2005-06-24 20:18:35 -070010913
10914 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070010915
Michael Chana71116d2005-05-29 14:58:11 -070010916 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10917 if (netif_running(dev)) {
10918 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010919 err2 = tg3_restart_hw(tp, 1);
10920 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070010921 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070010922 }
David S. Millerf47c11e2005-06-24 20:18:35 -070010923
10924 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010925
10926 if (irq_sync && !err2)
10927 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070010928 }
Michael Chanbc1c7562006-03-20 17:48:03 -080010929 if (tp->link_config.phy_is_low_power)
10930 tg3_set_power_state(tp, PCI_D3hot);
10931
Michael Chan4cafd3f2005-05-29 14:56:34 -070010932}
10933
Linus Torvalds1da177e2005-04-16 15:20:36 -070010934static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
10935{
10936 struct mii_ioctl_data *data = if_mii(ifr);
10937 struct tg3 *tp = netdev_priv(dev);
10938 int err;
10939
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010940 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010941 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010942 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
10943 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010944 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000010945 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010946 }
10947
Matt Carlson33f401a2010-04-05 10:19:27 +000010948 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010949 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000010950 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010951
10952 /* fallthru */
10953 case SIOCGMIIREG: {
10954 u32 mii_regval;
10955
10956 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
10957 break; /* We have no PHY */
10958
Michael Chanbc1c7562006-03-20 17:48:03 -080010959 if (tp->link_config.phy_is_low_power)
10960 return -EAGAIN;
10961
David S. Millerf47c11e2005-06-24 20:18:35 -070010962 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010963 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070010964 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010965
10966 data->val_out = mii_regval;
10967
10968 return err;
10969 }
10970
10971 case SIOCSMIIREG:
10972 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
10973 break; /* We have no PHY */
10974
Michael Chanbc1c7562006-03-20 17:48:03 -080010975 if (tp->link_config.phy_is_low_power)
10976 return -EAGAIN;
10977
David S. Millerf47c11e2005-06-24 20:18:35 -070010978 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010979 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070010980 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010981
10982 return err;
10983
10984 default:
10985 /* do nothing */
10986 break;
10987 }
10988 return -EOPNOTSUPP;
10989}
10990
10991#if TG3_VLAN_TAG_USED
10992static void tg3_vlan_rx_register(struct net_device *dev, struct vlan_group *grp)
10993{
10994 struct tg3 *tp = netdev_priv(dev);
10995
Matt Carlson844b3ee2009-02-25 14:23:56 +000010996 if (!netif_running(dev)) {
10997 tp->vlgrp = grp;
10998 return;
10999 }
11000
11001 tg3_netif_stop(tp);
Michael Chan29315e82006-06-29 20:12:30 -070011002
David S. Millerf47c11e2005-06-24 20:18:35 -070011003 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011004
11005 tp->vlgrp = grp;
11006
11007 /* Update RX_MODE_KEEP_VLAN_TAG bit in RX_MODE register. */
11008 __tg3_set_rx_mode(dev);
11009
Matt Carlson844b3ee2009-02-25 14:23:56 +000011010 tg3_netif_start(tp);
Michael Chan46966542007-07-11 19:47:19 -070011011
11012 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011013}
Linus Torvalds1da177e2005-04-16 15:20:36 -070011014#endif
11015
David S. Miller15f98502005-05-18 22:49:26 -070011016static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11017{
11018 struct tg3 *tp = netdev_priv(dev);
11019
11020 memcpy(ec, &tp->coal, sizeof(*ec));
11021 return 0;
11022}
11023
Michael Chand244c892005-07-05 14:42:33 -070011024static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11025{
11026 struct tg3 *tp = netdev_priv(dev);
11027 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
11028 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
11029
11030 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
11031 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
11032 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
11033 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
11034 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
11035 }
11036
11037 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
11038 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
11039 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
11040 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
11041 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
11042 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
11043 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
11044 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
11045 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
11046 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
11047 return -EINVAL;
11048
11049 /* No rx interrupts will be generated if both are zero */
11050 if ((ec->rx_coalesce_usecs == 0) &&
11051 (ec->rx_max_coalesced_frames == 0))
11052 return -EINVAL;
11053
11054 /* No tx interrupts will be generated if both are zero */
11055 if ((ec->tx_coalesce_usecs == 0) &&
11056 (ec->tx_max_coalesced_frames == 0))
11057 return -EINVAL;
11058
11059 /* Only copy relevant parameters, ignore all others. */
11060 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
11061 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
11062 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
11063 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
11064 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
11065 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
11066 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
11067 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
11068 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
11069
11070 if (netif_running(dev)) {
11071 tg3_full_lock(tp, 0);
11072 __tg3_set_coalesce(tp, &tp->coal);
11073 tg3_full_unlock(tp);
11074 }
11075 return 0;
11076}
11077
Jeff Garzik7282d492006-09-13 14:30:00 -040011078static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011079 .get_settings = tg3_get_settings,
11080 .set_settings = tg3_set_settings,
11081 .get_drvinfo = tg3_get_drvinfo,
11082 .get_regs_len = tg3_get_regs_len,
11083 .get_regs = tg3_get_regs,
11084 .get_wol = tg3_get_wol,
11085 .set_wol = tg3_set_wol,
11086 .get_msglevel = tg3_get_msglevel,
11087 .set_msglevel = tg3_set_msglevel,
11088 .nway_reset = tg3_nway_reset,
11089 .get_link = ethtool_op_get_link,
11090 .get_eeprom_len = tg3_get_eeprom_len,
11091 .get_eeprom = tg3_get_eeprom,
11092 .set_eeprom = tg3_set_eeprom,
11093 .get_ringparam = tg3_get_ringparam,
11094 .set_ringparam = tg3_set_ringparam,
11095 .get_pauseparam = tg3_get_pauseparam,
11096 .set_pauseparam = tg3_set_pauseparam,
11097 .get_rx_csum = tg3_get_rx_csum,
11098 .set_rx_csum = tg3_set_rx_csum,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011099 .set_tx_csum = tg3_set_tx_csum,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011100 .set_sg = ethtool_op_set_sg,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011101 .set_tso = tg3_set_tso,
Michael Chan4cafd3f2005-05-29 14:56:34 -070011102 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011103 .get_strings = tg3_get_strings,
Michael Chan4009a932005-09-05 17:52:54 -070011104 .phys_id = tg3_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011105 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070011106 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070011107 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070011108 .get_sset_count = tg3_get_sset_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011109};
11110
11111static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
11112{
Michael Chan1b277772006-03-20 22:27:48 -080011113 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011114
11115 tp->nvram_size = EEPROM_CHIP_SIZE;
11116
Matt Carlsone4f34112009-02-25 14:25:00 +000011117 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011118 return;
11119
Michael Chanb16250e2006-09-27 16:10:14 -070011120 if ((magic != TG3_EEPROM_MAGIC) &&
11121 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
11122 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011123 return;
11124
11125 /*
11126 * Size the chip by reading offsets at increasing powers of two.
11127 * When we encounter our validation signature, we know the addressing
11128 * has wrapped around, and thus have our chip size.
11129 */
Michael Chan1b277772006-03-20 22:27:48 -080011130 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011131
11132 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000011133 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011134 return;
11135
Michael Chan18201802006-03-20 22:29:15 -080011136 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011137 break;
11138
11139 cursize <<= 1;
11140 }
11141
11142 tp->nvram_size = cursize;
11143}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011144
Linus Torvalds1da177e2005-04-16 15:20:36 -070011145static void __devinit tg3_get_nvram_size(struct tg3 *tp)
11146{
11147 u32 val;
11148
Matt Carlsondf259d82009-04-20 06:57:14 +000011149 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
11150 tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080011151 return;
11152
11153 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080011154 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080011155 tg3_get_eeprom_size(tp);
11156 return;
11157 }
11158
Matt Carlson6d348f22009-02-25 14:25:52 +000011159 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011160 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000011161 /* This is confusing. We want to operate on the
11162 * 16-bit value at offset 0xf2. The tg3_nvram_read()
11163 * call will read from NVRAM and byteswap the data
11164 * according to the byteswapping settings for all
11165 * other register accesses. This ensures the data we
11166 * want will always reside in the lower 16-bits.
11167 * However, the data in NVRAM is in LE format, which
11168 * means the data from the NVRAM read will always be
11169 * opposite the endianness of the CPU. The 16-bit
11170 * byteswap then brings the data to CPU endianness.
11171 */
11172 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011173 return;
11174 }
11175 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070011176 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011177}
11178
11179static void __devinit tg3_get_nvram_info(struct tg3 *tp)
11180{
11181 u32 nvcfg1;
11182
11183 nvcfg1 = tr32(NVRAM_CFG1);
11184 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
11185 tp->tg3_flags2 |= TG3_FLG2_FLASH;
Matt Carlson8590a602009-08-28 12:29:16 +000011186 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011187 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11188 tw32(NVRAM_CFG1, nvcfg1);
11189 }
11190
Michael Chan4c987482005-09-05 17:52:38 -070011191 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750) ||
Michael Chana4e2b342005-10-26 15:46:52 -070011192 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011193 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011194 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
11195 tp->nvram_jedecnum = JEDEC_ATMEL;
11196 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
11197 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11198 break;
11199 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
11200 tp->nvram_jedecnum = JEDEC_ATMEL;
11201 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
11202 break;
11203 case FLASH_VENDOR_ATMEL_EEPROM:
11204 tp->nvram_jedecnum = JEDEC_ATMEL;
11205 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11206 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11207 break;
11208 case FLASH_VENDOR_ST:
11209 tp->nvram_jedecnum = JEDEC_ST;
11210 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
11211 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11212 break;
11213 case FLASH_VENDOR_SAIFUN:
11214 tp->nvram_jedecnum = JEDEC_SAIFUN;
11215 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
11216 break;
11217 case FLASH_VENDOR_SST_SMALL:
11218 case FLASH_VENDOR_SST_LARGE:
11219 tp->nvram_jedecnum = JEDEC_SST;
11220 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
11221 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011222 }
Matt Carlson8590a602009-08-28 12:29:16 +000011223 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011224 tp->nvram_jedecnum = JEDEC_ATMEL;
11225 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
11226 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11227 }
11228}
11229
Matt Carlsona1b950d2009-09-01 13:20:17 +000011230static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
11231{
11232 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
11233 case FLASH_5752PAGE_SIZE_256:
11234 tp->nvram_pagesize = 256;
11235 break;
11236 case FLASH_5752PAGE_SIZE_512:
11237 tp->nvram_pagesize = 512;
11238 break;
11239 case FLASH_5752PAGE_SIZE_1K:
11240 tp->nvram_pagesize = 1024;
11241 break;
11242 case FLASH_5752PAGE_SIZE_2K:
11243 tp->nvram_pagesize = 2048;
11244 break;
11245 case FLASH_5752PAGE_SIZE_4K:
11246 tp->nvram_pagesize = 4096;
11247 break;
11248 case FLASH_5752PAGE_SIZE_264:
11249 tp->nvram_pagesize = 264;
11250 break;
11251 case FLASH_5752PAGE_SIZE_528:
11252 tp->nvram_pagesize = 528;
11253 break;
11254 }
11255}
11256
Michael Chan361b4ac2005-04-21 17:11:21 -070011257static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
11258{
11259 u32 nvcfg1;
11260
11261 nvcfg1 = tr32(NVRAM_CFG1);
11262
Michael Chane6af3012005-04-21 17:12:05 -070011263 /* NVRAM protection for TPM */
11264 if (nvcfg1 & (1 << 27))
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011265 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Michael Chane6af3012005-04-21 17:12:05 -070011266
Michael Chan361b4ac2005-04-21 17:11:21 -070011267 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011268 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
11269 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
11270 tp->nvram_jedecnum = JEDEC_ATMEL;
11271 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11272 break;
11273 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11274 tp->nvram_jedecnum = JEDEC_ATMEL;
11275 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11276 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11277 break;
11278 case FLASH_5752VENDOR_ST_M45PE10:
11279 case FLASH_5752VENDOR_ST_M45PE20:
11280 case FLASH_5752VENDOR_ST_M45PE40:
11281 tp->nvram_jedecnum = JEDEC_ST;
11282 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11283 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11284 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070011285 }
11286
11287 if (tp->tg3_flags2 & TG3_FLG2_FLASH) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000011288 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000011289 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070011290 /* For eeprom, set pagesize to maximum eeprom size */
11291 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11292
11293 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11294 tw32(NVRAM_CFG1, nvcfg1);
11295 }
11296}
11297
Michael Chand3c7b882006-03-23 01:28:25 -080011298static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
11299{
Matt Carlson989a9d22007-05-05 11:51:05 -070011300 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080011301
11302 nvcfg1 = tr32(NVRAM_CFG1);
11303
11304 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070011305 if (nvcfg1 & (1 << 27)) {
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011306 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Matt Carlson989a9d22007-05-05 11:51:05 -070011307 protect = 1;
11308 }
Michael Chand3c7b882006-03-23 01:28:25 -080011309
Matt Carlson989a9d22007-05-05 11:51:05 -070011310 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11311 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011312 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11313 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11314 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11315 case FLASH_5755VENDOR_ATMEL_FLASH_5:
11316 tp->nvram_jedecnum = JEDEC_ATMEL;
11317 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11318 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11319 tp->nvram_pagesize = 264;
11320 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
11321 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
11322 tp->nvram_size = (protect ? 0x3e200 :
11323 TG3_NVRAM_SIZE_512KB);
11324 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
11325 tp->nvram_size = (protect ? 0x1f200 :
11326 TG3_NVRAM_SIZE_256KB);
11327 else
11328 tp->nvram_size = (protect ? 0x1f200 :
11329 TG3_NVRAM_SIZE_128KB);
11330 break;
11331 case FLASH_5752VENDOR_ST_M45PE10:
11332 case FLASH_5752VENDOR_ST_M45PE20:
11333 case FLASH_5752VENDOR_ST_M45PE40:
11334 tp->nvram_jedecnum = JEDEC_ST;
11335 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11336 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11337 tp->nvram_pagesize = 256;
11338 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
11339 tp->nvram_size = (protect ?
11340 TG3_NVRAM_SIZE_64KB :
11341 TG3_NVRAM_SIZE_128KB);
11342 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
11343 tp->nvram_size = (protect ?
11344 TG3_NVRAM_SIZE_64KB :
11345 TG3_NVRAM_SIZE_256KB);
11346 else
11347 tp->nvram_size = (protect ?
11348 TG3_NVRAM_SIZE_128KB :
11349 TG3_NVRAM_SIZE_512KB);
11350 break;
Michael Chand3c7b882006-03-23 01:28:25 -080011351 }
11352}
11353
Michael Chan1b277772006-03-20 22:27:48 -080011354static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
11355{
11356 u32 nvcfg1;
11357
11358 nvcfg1 = tr32(NVRAM_CFG1);
11359
11360 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011361 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
11362 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11363 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
11364 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11365 tp->nvram_jedecnum = JEDEC_ATMEL;
11366 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11367 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080011368
Matt Carlson8590a602009-08-28 12:29:16 +000011369 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11370 tw32(NVRAM_CFG1, nvcfg1);
11371 break;
11372 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11373 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11374 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11375 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11376 tp->nvram_jedecnum = JEDEC_ATMEL;
11377 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11378 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11379 tp->nvram_pagesize = 264;
11380 break;
11381 case FLASH_5752VENDOR_ST_M45PE10:
11382 case FLASH_5752VENDOR_ST_M45PE20:
11383 case FLASH_5752VENDOR_ST_M45PE40:
11384 tp->nvram_jedecnum = JEDEC_ST;
11385 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11386 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11387 tp->nvram_pagesize = 256;
11388 break;
Michael Chan1b277772006-03-20 22:27:48 -080011389 }
11390}
11391
Matt Carlson6b91fa02007-10-10 18:01:09 -070011392static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
11393{
11394 u32 nvcfg1, protect = 0;
11395
11396 nvcfg1 = tr32(NVRAM_CFG1);
11397
11398 /* NVRAM protection for TPM */
11399 if (nvcfg1 & (1 << 27)) {
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011400 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011401 protect = 1;
11402 }
11403
11404 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11405 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011406 case FLASH_5761VENDOR_ATMEL_ADB021D:
11407 case FLASH_5761VENDOR_ATMEL_ADB041D:
11408 case FLASH_5761VENDOR_ATMEL_ADB081D:
11409 case FLASH_5761VENDOR_ATMEL_ADB161D:
11410 case FLASH_5761VENDOR_ATMEL_MDB021D:
11411 case FLASH_5761VENDOR_ATMEL_MDB041D:
11412 case FLASH_5761VENDOR_ATMEL_MDB081D:
11413 case FLASH_5761VENDOR_ATMEL_MDB161D:
11414 tp->nvram_jedecnum = JEDEC_ATMEL;
11415 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11416 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11417 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
11418 tp->nvram_pagesize = 256;
11419 break;
11420 case FLASH_5761VENDOR_ST_A_M45PE20:
11421 case FLASH_5761VENDOR_ST_A_M45PE40:
11422 case FLASH_5761VENDOR_ST_A_M45PE80:
11423 case FLASH_5761VENDOR_ST_A_M45PE16:
11424 case FLASH_5761VENDOR_ST_M_M45PE20:
11425 case FLASH_5761VENDOR_ST_M_M45PE40:
11426 case FLASH_5761VENDOR_ST_M_M45PE80:
11427 case FLASH_5761VENDOR_ST_M_M45PE16:
11428 tp->nvram_jedecnum = JEDEC_ST;
11429 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11430 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11431 tp->nvram_pagesize = 256;
11432 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011433 }
11434
11435 if (protect) {
11436 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
11437 } else {
11438 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011439 case FLASH_5761VENDOR_ATMEL_ADB161D:
11440 case FLASH_5761VENDOR_ATMEL_MDB161D:
11441 case FLASH_5761VENDOR_ST_A_M45PE16:
11442 case FLASH_5761VENDOR_ST_M_M45PE16:
11443 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
11444 break;
11445 case FLASH_5761VENDOR_ATMEL_ADB081D:
11446 case FLASH_5761VENDOR_ATMEL_MDB081D:
11447 case FLASH_5761VENDOR_ST_A_M45PE80:
11448 case FLASH_5761VENDOR_ST_M_M45PE80:
11449 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
11450 break;
11451 case FLASH_5761VENDOR_ATMEL_ADB041D:
11452 case FLASH_5761VENDOR_ATMEL_MDB041D:
11453 case FLASH_5761VENDOR_ST_A_M45PE40:
11454 case FLASH_5761VENDOR_ST_M_M45PE40:
11455 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11456 break;
11457 case FLASH_5761VENDOR_ATMEL_ADB021D:
11458 case FLASH_5761VENDOR_ATMEL_MDB021D:
11459 case FLASH_5761VENDOR_ST_A_M45PE20:
11460 case FLASH_5761VENDOR_ST_M_M45PE20:
11461 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11462 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011463 }
11464 }
11465}
11466
Michael Chanb5d37722006-09-27 16:06:21 -070011467static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
11468{
11469 tp->nvram_jedecnum = JEDEC_ATMEL;
11470 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11471 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11472}
11473
Matt Carlson321d32a2008-11-21 17:22:19 -080011474static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
11475{
11476 u32 nvcfg1;
11477
11478 nvcfg1 = tr32(NVRAM_CFG1);
11479
11480 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11481 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11482 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11483 tp->nvram_jedecnum = JEDEC_ATMEL;
11484 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11485 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11486
11487 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11488 tw32(NVRAM_CFG1, nvcfg1);
11489 return;
11490 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11491 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11492 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11493 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11494 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11495 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11496 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
11497 tp->nvram_jedecnum = JEDEC_ATMEL;
11498 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11499 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11500
11501 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11502 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11503 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11504 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11505 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11506 break;
11507 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11508 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11509 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11510 break;
11511 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11512 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
11513 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11514 break;
11515 }
11516 break;
11517 case FLASH_5752VENDOR_ST_M45PE10:
11518 case FLASH_5752VENDOR_ST_M45PE20:
11519 case FLASH_5752VENDOR_ST_M45PE40:
11520 tp->nvram_jedecnum = JEDEC_ST;
11521 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11522 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11523
11524 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11525 case FLASH_5752VENDOR_ST_M45PE10:
11526 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11527 break;
11528 case FLASH_5752VENDOR_ST_M45PE20:
11529 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11530 break;
11531 case FLASH_5752VENDOR_ST_M45PE40:
11532 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11533 break;
11534 }
11535 break;
11536 default:
Matt Carlsondf259d82009-04-20 06:57:14 +000011537 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
Matt Carlson321d32a2008-11-21 17:22:19 -080011538 return;
11539 }
11540
Matt Carlsona1b950d2009-09-01 13:20:17 +000011541 tg3_nvram_get_pagesize(tp, nvcfg1);
11542 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Matt Carlson321d32a2008-11-21 17:22:19 -080011543 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011544}
11545
11546
11547static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
11548{
11549 u32 nvcfg1;
11550
11551 nvcfg1 = tr32(NVRAM_CFG1);
11552
11553 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11554 case FLASH_5717VENDOR_ATMEL_EEPROM:
11555 case FLASH_5717VENDOR_MICRO_EEPROM:
11556 tp->nvram_jedecnum = JEDEC_ATMEL;
11557 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11558 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11559
11560 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11561 tw32(NVRAM_CFG1, nvcfg1);
11562 return;
11563 case FLASH_5717VENDOR_ATMEL_MDB011D:
11564 case FLASH_5717VENDOR_ATMEL_ADB011B:
11565 case FLASH_5717VENDOR_ATMEL_ADB011D:
11566 case FLASH_5717VENDOR_ATMEL_MDB021D:
11567 case FLASH_5717VENDOR_ATMEL_ADB021B:
11568 case FLASH_5717VENDOR_ATMEL_ADB021D:
11569 case FLASH_5717VENDOR_ATMEL_45USPT:
11570 tp->nvram_jedecnum = JEDEC_ATMEL;
11571 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11572 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11573
11574 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11575 case FLASH_5717VENDOR_ATMEL_MDB021D:
11576 case FLASH_5717VENDOR_ATMEL_ADB021B:
11577 case FLASH_5717VENDOR_ATMEL_ADB021D:
11578 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11579 break;
11580 default:
11581 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11582 break;
11583 }
Matt Carlson321d32a2008-11-21 17:22:19 -080011584 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011585 case FLASH_5717VENDOR_ST_M_M25PE10:
11586 case FLASH_5717VENDOR_ST_A_M25PE10:
11587 case FLASH_5717VENDOR_ST_M_M45PE10:
11588 case FLASH_5717VENDOR_ST_A_M45PE10:
11589 case FLASH_5717VENDOR_ST_M_M25PE20:
11590 case FLASH_5717VENDOR_ST_A_M25PE20:
11591 case FLASH_5717VENDOR_ST_M_M45PE20:
11592 case FLASH_5717VENDOR_ST_A_M45PE20:
11593 case FLASH_5717VENDOR_ST_25USPT:
11594 case FLASH_5717VENDOR_ST_45USPT:
11595 tp->nvram_jedecnum = JEDEC_ST;
11596 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11597 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11598
11599 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11600 case FLASH_5717VENDOR_ST_M_M25PE20:
11601 case FLASH_5717VENDOR_ST_A_M25PE20:
11602 case FLASH_5717VENDOR_ST_M_M45PE20:
11603 case FLASH_5717VENDOR_ST_A_M45PE20:
11604 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11605 break;
11606 default:
11607 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11608 break;
11609 }
Matt Carlson321d32a2008-11-21 17:22:19 -080011610 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011611 default:
11612 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
11613 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080011614 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000011615
11616 tg3_nvram_get_pagesize(tp, nvcfg1);
11617 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
11618 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
Matt Carlson321d32a2008-11-21 17:22:19 -080011619}
11620
Linus Torvalds1da177e2005-04-16 15:20:36 -070011621/* Chips other than 5700/5701 use the NVRAM for fetching info. */
11622static void __devinit tg3_nvram_init(struct tg3 *tp)
11623{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011624 tw32_f(GRC_EEPROM_ADDR,
11625 (EEPROM_ADDR_FSM_RESET |
11626 (EEPROM_DEFAULT_CLOCK_PERIOD <<
11627 EEPROM_ADDR_CLKPERD_SHIFT)));
11628
Michael Chan9d57f012006-12-07 00:23:25 -080011629 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011630
11631 /* Enable seeprom accesses. */
11632 tw32_f(GRC_LOCAL_CTRL,
11633 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
11634 udelay(100);
11635
11636 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
11637 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
11638 tp->tg3_flags |= TG3_FLAG_NVRAM;
11639
Michael Chanec41c7d2006-01-17 02:40:55 -080011640 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000011641 netdev_warn(tp->dev,
11642 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000011643 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080011644 return;
11645 }
Michael Chane6af3012005-04-21 17:12:05 -070011646 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011647
Matt Carlson989a9d22007-05-05 11:51:05 -070011648 tp->nvram_size = 0;
11649
Michael Chan361b4ac2005-04-21 17:11:21 -070011650 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
11651 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080011652 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
11653 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070011654 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070011655 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
11656 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080011657 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070011658 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
11659 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070011660 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
11661 tg3_get_5906_nvram_info(tp);
Matt Carlsonb703df62009-12-03 08:36:21 +000011662 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
11663 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson321d32a2008-11-21 17:22:19 -080011664 tg3_get_57780_nvram_info(tp);
Matt Carlsona50d0792010-06-05 17:24:37 +000011665 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
11666 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000011667 tg3_get_5717_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070011668 else
11669 tg3_get_nvram_info(tp);
11670
Matt Carlson989a9d22007-05-05 11:51:05 -070011671 if (tp->nvram_size == 0)
11672 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011673
Michael Chane6af3012005-04-21 17:12:05 -070011674 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080011675 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011676
11677 } else {
11678 tp->tg3_flags &= ~(TG3_FLAG_NVRAM | TG3_FLAG_NVRAM_BUFFERED);
11679
11680 tg3_get_eeprom_size(tp);
11681 }
11682}
11683
Linus Torvalds1da177e2005-04-16 15:20:36 -070011684static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
11685 u32 offset, u32 len, u8 *buf)
11686{
11687 int i, j, rc = 0;
11688 u32 val;
11689
11690 for (i = 0; i < len; i += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080011691 u32 addr;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011692 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011693
11694 addr = offset + i;
11695
11696 memcpy(&data, buf + i, 4);
11697
Matt Carlson62cedd12009-04-20 14:52:29 -070011698 /*
11699 * The SEEPROM interface expects the data to always be opposite
11700 * the native endian format. We accomplish this by reversing
11701 * all the operations that would have been performed on the
11702 * data from a call to tg3_nvram_read_be32().
11703 */
11704 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011705
11706 val = tr32(GRC_EEPROM_ADDR);
11707 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
11708
11709 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
11710 EEPROM_ADDR_READ);
11711 tw32(GRC_EEPROM_ADDR, val |
11712 (0 << EEPROM_ADDR_DEVID_SHIFT) |
11713 (addr & EEPROM_ADDR_ADDR_MASK) |
11714 EEPROM_ADDR_START |
11715 EEPROM_ADDR_WRITE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011716
Michael Chan9d57f012006-12-07 00:23:25 -080011717 for (j = 0; j < 1000; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011718 val = tr32(GRC_EEPROM_ADDR);
11719
11720 if (val & EEPROM_ADDR_COMPLETE)
11721 break;
Michael Chan9d57f012006-12-07 00:23:25 -080011722 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011723 }
11724 if (!(val & EEPROM_ADDR_COMPLETE)) {
11725 rc = -EBUSY;
11726 break;
11727 }
11728 }
11729
11730 return rc;
11731}
11732
11733/* offset and length are dword aligned */
11734static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
11735 u8 *buf)
11736{
11737 int ret = 0;
11738 u32 pagesize = tp->nvram_pagesize;
11739 u32 pagemask = pagesize - 1;
11740 u32 nvram_cmd;
11741 u8 *tmp;
11742
11743 tmp = kmalloc(pagesize, GFP_KERNEL);
11744 if (tmp == NULL)
11745 return -ENOMEM;
11746
11747 while (len) {
11748 int j;
Michael Chane6af3012005-04-21 17:12:05 -070011749 u32 phy_addr, page_off, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011750
11751 phy_addr = offset & ~pagemask;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011752
Linus Torvalds1da177e2005-04-16 15:20:36 -070011753 for (j = 0; j < pagesize; j += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000011754 ret = tg3_nvram_read_be32(tp, phy_addr + j,
11755 (__be32 *) (tmp + j));
11756 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011757 break;
11758 }
11759 if (ret)
11760 break;
11761
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011762 page_off = offset & pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011763 size = pagesize;
11764 if (len < size)
11765 size = len;
11766
11767 len -= size;
11768
11769 memcpy(tmp + page_off, buf, size);
11770
11771 offset = offset + (pagesize - page_off);
11772
Michael Chane6af3012005-04-21 17:12:05 -070011773 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011774
11775 /*
11776 * Before we can erase the flash page, we need
11777 * to issue a special "write enable" command.
11778 */
11779 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
11780
11781 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
11782 break;
11783
11784 /* Erase the target page */
11785 tw32(NVRAM_ADDR, phy_addr);
11786
11787 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
11788 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
11789
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011790 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011791 break;
11792
11793 /* Issue another write enable to start the write. */
11794 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
11795
11796 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
11797 break;
11798
11799 for (j = 0; j < pagesize; j += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080011800 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011801
Al Virob9fc7dc2007-12-17 22:59:57 -080011802 data = *((__be32 *) (tmp + j));
Matt Carlsona9dc5292009-02-25 14:25:30 +000011803
Al Virob9fc7dc2007-12-17 22:59:57 -080011804 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011805
11806 tw32(NVRAM_ADDR, phy_addr + j);
11807
11808 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
11809 NVRAM_CMD_WR;
11810
11811 if (j == 0)
11812 nvram_cmd |= NVRAM_CMD_FIRST;
11813 else if (j == (pagesize - 4))
11814 nvram_cmd |= NVRAM_CMD_LAST;
11815
11816 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
11817 break;
11818 }
11819 if (ret)
11820 break;
11821 }
11822
11823 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
11824 tg3_nvram_exec_cmd(tp, nvram_cmd);
11825
11826 kfree(tmp);
11827
11828 return ret;
11829}
11830
11831/* offset and length are dword aligned */
11832static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
11833 u8 *buf)
11834{
11835 int i, ret = 0;
11836
11837 for (i = 0; i < len; i += 4, offset += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080011838 u32 page_off, phy_addr, nvram_cmd;
11839 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011840
11841 memcpy(&data, buf + i, 4);
Al Virob9fc7dc2007-12-17 22:59:57 -080011842 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011843
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011844 page_off = offset % tp->nvram_pagesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011845
Michael Chan18201802006-03-20 22:29:15 -080011846 phy_addr = tg3_nvram_phys_addr(tp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011847
11848 tw32(NVRAM_ADDR, phy_addr);
11849
11850 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
11851
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011852 if (page_off == 0 || i == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011853 nvram_cmd |= NVRAM_CMD_FIRST;
Michael Chanf6d9a252006-04-29 19:00:24 -070011854 if (page_off == (tp->nvram_pagesize - 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011855 nvram_cmd |= NVRAM_CMD_LAST;
11856
11857 if (i == (len - 4))
11858 nvram_cmd |= NVRAM_CMD_LAST;
11859
Matt Carlson321d32a2008-11-21 17:22:19 -080011860 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
11861 !(tp->tg3_flags3 & TG3_FLG3_5755_PLUS) &&
Michael Chan4c987482005-09-05 17:52:38 -070011862 (tp->nvram_jedecnum == JEDEC_ST) &&
11863 (nvram_cmd & NVRAM_CMD_FIRST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011864
11865 if ((ret = tg3_nvram_exec_cmd(tp,
11866 NVRAM_CMD_WREN | NVRAM_CMD_GO |
11867 NVRAM_CMD_DONE)))
11868
11869 break;
11870 }
11871 if (!(tp->tg3_flags2 & TG3_FLG2_FLASH)) {
11872 /* We always do complete word writes to eeprom. */
11873 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
11874 }
11875
11876 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
11877 break;
11878 }
11879 return ret;
11880}
11881
11882/* offset and length are dword aligned */
11883static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
11884{
11885 int ret;
11886
Linus Torvalds1da177e2005-04-16 15:20:36 -070011887 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT) {
Michael Chan314fba32005-04-21 17:07:04 -070011888 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
11889 ~GRC_LCLCTRL_GPIO_OUTPUT1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011890 udelay(40);
11891 }
11892
11893 if (!(tp->tg3_flags & TG3_FLAG_NVRAM)) {
11894 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
Matt Carlson859a588792010-04-05 10:19:28 +000011895 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011896 u32 grc_mode;
11897
Michael Chanec41c7d2006-01-17 02:40:55 -080011898 ret = tg3_nvram_lock(tp);
11899 if (ret)
11900 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011901
Michael Chane6af3012005-04-21 17:12:05 -070011902 tg3_enable_nvram_access(tp);
11903 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011904 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011905 tw32(NVRAM_WRITE1, 0x406);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011906
11907 grc_mode = tr32(GRC_MODE);
11908 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
11909
11910 if ((tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) ||
11911 !(tp->tg3_flags2 & TG3_FLG2_FLASH)) {
11912
11913 ret = tg3_nvram_write_block_buffered(tp, offset, len,
11914 buf);
Matt Carlson859a588792010-04-05 10:19:28 +000011915 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011916 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
11917 buf);
11918 }
11919
11920 grc_mode = tr32(GRC_MODE);
11921 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
11922
Michael Chane6af3012005-04-21 17:12:05 -070011923 tg3_disable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011924 tg3_nvram_unlock(tp);
11925 }
11926
11927 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT) {
Michael Chan314fba32005-04-21 17:07:04 -070011928 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011929 udelay(40);
11930 }
11931
11932 return ret;
11933}
11934
11935struct subsys_tbl_ent {
11936 u16 subsys_vendor, subsys_devid;
11937 u32 phy_id;
11938};
11939
Matt Carlson24daf2b2010-02-17 15:17:02 +000011940static struct subsys_tbl_ent subsys_id_to_phy_id[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011941 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000011942 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011943 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011944 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011945 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011946 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011947 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011948 { TG3PCI_SUBVENDOR_ID_BROADCOM,
11949 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
11950 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011951 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011952 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011953 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011954 { TG3PCI_SUBVENDOR_ID_BROADCOM,
11955 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
11956 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011957 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011958 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011959 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011960 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011961 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011962 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011963 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070011964
11965 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000011966 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011967 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011968 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011969 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011970 { TG3PCI_SUBVENDOR_ID_3COM,
11971 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
11972 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011973 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011974 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011975 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070011976
11977 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000011978 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000011979 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011980 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000011981 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011982 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000011983 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011984 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000011985 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070011986
11987 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000011988 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000011989 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011990 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000011991 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011992 { TG3PCI_SUBVENDOR_ID_COMPAQ,
11993 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
11994 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000011995 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011996 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000011997 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070011998
11999 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012000 { TG3PCI_SUBVENDOR_ID_IBM,
12001 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012002};
12003
Matt Carlson24daf2b2010-02-17 15:17:02 +000012004static struct subsys_tbl_ent * __devinit tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012005{
12006 int i;
12007
12008 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
12009 if ((subsys_id_to_phy_id[i].subsys_vendor ==
12010 tp->pdev->subsystem_vendor) &&
12011 (subsys_id_to_phy_id[i].subsys_devid ==
12012 tp->pdev->subsystem_device))
12013 return &subsys_id_to_phy_id[i];
12014 }
12015 return NULL;
12016}
12017
Michael Chan7d0c41e2005-04-21 17:06:20 -070012018static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012019{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012020 u32 val;
Michael Chancaf636c72006-03-22 01:05:31 -080012021 u16 pmcsr;
12022
12023 /* On some early chips the SRAM cannot be accessed in D3hot state,
12024 * so need make sure we're in D0.
12025 */
12026 pci_read_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, &pmcsr);
12027 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
12028 pci_write_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, pmcsr);
12029 msleep(1);
Michael Chan7d0c41e2005-04-21 17:06:20 -070012030
12031 /* Make sure register accesses (indirect or otherwise)
12032 * will function correctly.
12033 */
12034 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
12035 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012036
David S. Millerf49639e2006-06-09 11:58:36 -070012037 /* The memory arbiter has to be enabled in order for SRAM accesses
12038 * to succeed. Normally on powerup the tg3 chip firmware will make
12039 * sure it is enabled, but other entities such as system netboot
12040 * code might disable it.
12041 */
12042 val = tr32(MEMARB_MODE);
12043 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
12044
Matt Carlson79eb6902010-02-17 15:17:03 +000012045 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012046 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12047
Gary Zambranoa85feb82007-05-05 11:52:19 -070012048 /* Assume an onboard device and WOL capable by default. */
12049 tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT | TG3_FLAG_WOL_CAP;
David S. Miller72b845e2006-03-14 14:11:48 -080012050
Michael Chanb5d37722006-09-27 16:06:21 -070012051 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080012052 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Michael Chanb5d37722006-09-27 16:06:21 -070012053 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080012054 tp->tg3_flags2 |= TG3_FLG2_IS_NIC;
12055 }
Matt Carlson0527ba32007-10-10 18:03:30 -070012056 val = tr32(VCPU_CFGSHDW);
12057 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Matt Carlson8ed5d972007-05-07 00:25:49 -070012058 tp->tg3_flags |= TG3_FLAG_ASPM_WORKAROUND;
Matt Carlson0527ba32007-10-10 18:03:30 -070012059 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Matt Carlson20232762008-12-21 20:18:56 -080012060 (val & VCPU_CFGSHDW_WOL_MAGPKT))
Matt Carlson0527ba32007-10-10 18:03:30 -070012061 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012062 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070012063 }
12064
Linus Torvalds1da177e2005-04-16 15:20:36 -070012065 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
12066 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
12067 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070012068 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012069 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012070
12071 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
12072 tp->nic_sram_data_cfg = nic_cfg;
12073
12074 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
12075 ver >>= NIC_SRAM_DATA_VER_SHIFT;
12076 if ((GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700) &&
12077 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) &&
12078 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703) &&
12079 (ver > 0) && (ver < 0x100))
12080 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
12081
Matt Carlsona9daf362008-05-25 23:49:44 -070012082 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
12083 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
12084
Linus Torvalds1da177e2005-04-16 15:20:36 -070012085 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
12086 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
12087 eeprom_phy_serdes = 1;
12088
12089 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
12090 if (nic_phy_id != 0) {
12091 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
12092 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
12093
12094 eeprom_phy_id = (id1 >> 16) << 10;
12095 eeprom_phy_id |= (id2 & 0xfc00) << 16;
12096 eeprom_phy_id |= (id2 & 0x03ff) << 0;
12097 } else
12098 eeprom_phy_id = 0;
12099
Michael Chan7d0c41e2005-04-21 17:06:20 -070012100 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070012101 if (eeprom_phy_serdes) {
Matt Carlsona50d0792010-06-05 17:24:37 +000012102 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Michael Chan747e8f82005-07-25 12:33:22 -070012103 tp->tg3_flags2 |= TG3_FLG2_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000012104 else
12105 tp->tg3_flags2 |= TG3_FLG2_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070012106 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070012107
John W. Linvillecbf46852005-04-21 17:01:29 -070012108 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012109 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
12110 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070012111 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070012112 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
12113
12114 switch (led_cfg) {
12115 default:
12116 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
12117 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12118 break;
12119
12120 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
12121 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12122 break;
12123
12124 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
12125 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070012126
12127 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
12128 * read on some older 5700/5701 bootcode.
12129 */
12130 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
12131 ASIC_REV_5700 ||
12132 GET_ASIC_REV(tp->pci_chip_rev_id) ==
12133 ASIC_REV_5701)
12134 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12135
Linus Torvalds1da177e2005-04-16 15:20:36 -070012136 break;
12137
12138 case SHASTA_EXT_LED_SHARED:
12139 tp->led_ctrl = LED_CTRL_MODE_SHARED;
12140 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
12141 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
12142 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12143 LED_CTRL_MODE_PHY_2);
12144 break;
12145
12146 case SHASTA_EXT_LED_MAC:
12147 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
12148 break;
12149
12150 case SHASTA_EXT_LED_COMBO:
12151 tp->led_ctrl = LED_CTRL_MODE_COMBO;
12152 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
12153 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12154 LED_CTRL_MODE_PHY_2);
12155 break;
12156
Stephen Hemminger855e1112008-04-16 16:37:28 -070012157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012158
12159 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
12160 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
12161 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
12162 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12163
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012164 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
12165 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080012166
Michael Chan9d26e212006-12-07 00:21:14 -080012167 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012168 tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080012169 if ((tp->pdev->subsystem_vendor ==
12170 PCI_VENDOR_ID_ARIMA) &&
12171 (tp->pdev->subsystem_device == 0x205a ||
12172 tp->pdev->subsystem_device == 0x2063))
12173 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
12174 } else {
David S. Millerf49639e2006-06-09 11:58:36 -070012175 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080012176 tp->tg3_flags2 |= TG3_FLG2_IS_NIC;
12177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012178
12179 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
12180 tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
John W. Linvillecbf46852005-04-21 17:01:29 -070012181 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012182 tp->tg3_flags2 |= TG3_FLG2_ASF_NEW_HANDSHAKE;
12183 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012184
12185 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
12186 (tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
Matt Carlson0d3031d2007-10-10 18:02:43 -070012187 tp->tg3_flags3 |= TG3_FLG3_ENABLE_APE;
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012188
Gary Zambranoa85feb82007-05-05 11:52:19 -070012189 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES &&
12190 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
12191 tp->tg3_flags &= ~TG3_FLAG_WOL_CAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012192
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070012193 if ((tp->tg3_flags & TG3_FLAG_WOL_CAP) &&
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012194 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE))
Matt Carlson0527ba32007-10-10 18:03:30 -070012195 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
12196
Linus Torvalds1da177e2005-04-16 15:20:36 -070012197 if (cfg2 & (1 << 17))
12198 tp->tg3_flags2 |= TG3_FLG2_CAPACITIVE_COUPLING;
12199
12200 /* serdes signal pre-emphasis in register 0x590 set by */
12201 /* bootcode if bit 18 is set */
12202 if (cfg2 & (1 << 18))
12203 tp->tg3_flags2 |= TG3_FLG2_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070012204
Matt Carlson321d32a2008-11-21 17:22:19 -080012205 if (((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
12206 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080012207 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
12208 tp->tg3_flags3 |= TG3_FLG3_PHY_ENABLE_APD;
12209
Matt Carlson8c69b1e2010-08-02 11:26:00 +000012210 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
12211 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
12212 !(tp->tg3_flags3 & TG3_FLG3_5717_PLUS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070012213 u32 cfg3;
12214
12215 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
12216 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
12217 tp->tg3_flags |= TG3_FLAG_ASPM_WORKAROUND;
12218 }
Matt Carlsona9daf362008-05-25 23:49:44 -070012219
Matt Carlson14417062010-02-17 15:16:59 +000012220 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
12221 tp->tg3_flags3 |= TG3_FLG3_RGMII_INBAND_DISABLE;
Matt Carlsona9daf362008-05-25 23:49:44 -070012222 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
12223 tp->tg3_flags3 |= TG3_FLG3_RGMII_EXT_IBND_RX_EN;
12224 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
12225 tp->tg3_flags3 |= TG3_FLG3_RGMII_EXT_IBND_TX_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012226 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012227done:
12228 device_init_wakeup(&tp->pdev->dev, tp->tg3_flags & TG3_FLAG_WOL_CAP);
12229 device_set_wakeup_enable(&tp->pdev->dev,
12230 tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
Michael Chan7d0c41e2005-04-21 17:06:20 -070012231}
12232
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012233static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
12234{
12235 int i;
12236 u32 val;
12237
12238 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
12239 tw32(OTP_CTRL, cmd);
12240
12241 /* Wait for up to 1 ms for command to execute. */
12242 for (i = 0; i < 100; i++) {
12243 val = tr32(OTP_STATUS);
12244 if (val & OTP_STATUS_CMD_DONE)
12245 break;
12246 udelay(10);
12247 }
12248
12249 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
12250}
12251
12252/* Read the gphy configuration from the OTP region of the chip. The gphy
12253 * configuration is a 32-bit value that straddles the alignment boundary.
12254 * We do two 32-bit reads and then shift and merge the results.
12255 */
12256static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
12257{
12258 u32 bhalf_otp, thalf_otp;
12259
12260 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
12261
12262 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
12263 return 0;
12264
12265 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
12266
12267 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12268 return 0;
12269
12270 thalf_otp = tr32(OTP_READ_DATA);
12271
12272 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
12273
12274 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12275 return 0;
12276
12277 bhalf_otp = tr32(OTP_READ_DATA);
12278
12279 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
12280}
12281
Michael Chan7d0c41e2005-04-21 17:06:20 -070012282static int __devinit tg3_phy_probe(struct tg3 *tp)
12283{
12284 u32 hw_phy_id_1, hw_phy_id_2;
12285 u32 hw_phy_id, hw_phy_id_masked;
12286 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012287
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012288 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)
12289 return tg3_phy_init(tp);
12290
Linus Torvalds1da177e2005-04-16 15:20:36 -070012291 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010012292 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012293 */
12294 err = 0;
Matt Carlson0d3031d2007-10-10 18:02:43 -070012295 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
12296 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000012297 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012298 } else {
12299 /* Now read the physical PHY_ID from the chip and verify
12300 * that it is sane. If it doesn't look good, we fall back
12301 * to either the hard-coded table based PHY_ID and failing
12302 * that the value found in the eeprom area.
12303 */
12304 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
12305 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
12306
12307 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
12308 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
12309 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
12310
Matt Carlson79eb6902010-02-17 15:17:03 +000012311 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012312 }
12313
Matt Carlson79eb6902010-02-17 15:17:03 +000012314 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012315 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000012316 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012317 tp->tg3_flags2 |= TG3_FLG2_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070012318 else
12319 tp->tg3_flags2 &= ~TG3_FLG2_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012320 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000012321 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070012322 /* Do nothing, phy ID already set up in
12323 * tg3_get_eeprom_hw_cfg().
12324 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070012325 } else {
12326 struct subsys_tbl_ent *p;
12327
12328 /* No eeprom signature? Try the hardcoded
12329 * subsys device table.
12330 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012331 p = tg3_lookup_by_subsys(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012332 if (!p)
12333 return -ENODEV;
12334
12335 tp->phy_id = p->phy_id;
12336 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000012337 tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012338 tp->tg3_flags2 |= TG3_FLG2_PHY_SERDES;
12339 }
12340 }
12341
Michael Chan747e8f82005-07-25 12:33:22 -070012342 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES) &&
Matt Carlson0d3031d2007-10-10 18:02:43 -070012343 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070012344 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chan3600d912006-12-07 00:21:48 -080012345 u32 bmsr, adv_reg, tg3_ctrl, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012346
12347 tg3_readphy(tp, MII_BMSR, &bmsr);
12348 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
12349 (bmsr & BMSR_LSTATUS))
12350 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012351
Linus Torvalds1da177e2005-04-16 15:20:36 -070012352 err = tg3_phy_reset(tp);
12353 if (err)
12354 return err;
12355
12356 adv_reg = (ADVERTISE_10HALF | ADVERTISE_10FULL |
12357 ADVERTISE_100HALF | ADVERTISE_100FULL |
12358 ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
12359 tg3_ctrl = 0;
12360 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY)) {
12361 tg3_ctrl = (MII_TG3_CTRL_ADV_1000_HALF |
12362 MII_TG3_CTRL_ADV_1000_FULL);
12363 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
12364 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
12365 tg3_ctrl |= (MII_TG3_CTRL_AS_MASTER |
12366 MII_TG3_CTRL_ENABLE_AS_MASTER);
12367 }
12368
Michael Chan3600d912006-12-07 00:21:48 -080012369 mask = (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
12370 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
12371 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full);
12372 if (!tg3_copper_is_advertising_all(tp, mask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012373 tg3_writephy(tp, MII_ADVERTISE, adv_reg);
12374
12375 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY))
12376 tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
12377
12378 tg3_writephy(tp, MII_BMCR,
12379 BMCR_ANENABLE | BMCR_ANRESTART);
12380 }
12381 tg3_phy_set_wirespeed(tp);
12382
12383 tg3_writephy(tp, MII_ADVERTISE, adv_reg);
12384 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY))
12385 tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
12386 }
12387
12388skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000012389 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012390 err = tg3_init_5401phy_dsp(tp);
12391 if (err)
12392 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012393
Linus Torvalds1da177e2005-04-16 15:20:36 -070012394 err = tg3_init_5401phy_dsp(tp);
12395 }
12396
Michael Chan747e8f82005-07-25 12:33:22 -070012397 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012398 tp->link_config.advertising =
12399 (ADVERTISED_1000baseT_Half |
12400 ADVERTISED_1000baseT_Full |
12401 ADVERTISED_Autoneg |
12402 ADVERTISED_FIBRE);
12403 if (tp->tg3_flags & TG3_FLAG_10_100_ONLY)
12404 tp->link_config.advertising &=
12405 ~(ADVERTISED_1000baseT_Half |
12406 ADVERTISED_1000baseT_Full);
12407
12408 return err;
12409}
12410
Matt Carlson184b8902010-04-05 10:19:25 +000012411static void __devinit tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012412{
Matt Carlson184b8902010-04-05 10:19:25 +000012413 u8 vpd_data[TG3_NVM_VPD_LEN];
Matt Carlson4181b2c2010-02-26 14:04:45 +000012414 unsigned int block_end, rosize, len;
Matt Carlson184b8902010-04-05 10:19:25 +000012415 int j, i = 0;
Michael Chan1b277772006-03-20 22:27:48 -080012416 u32 magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012417
Matt Carlsondf259d82009-04-20 06:57:14 +000012418 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
12419 tg3_nvram_read(tp, 0x0, &magic))
David S. Millerf49639e2006-06-09 11:58:36 -070012420 goto out_not_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012421
Michael Chan18201802006-03-20 22:29:15 -080012422 if (magic == TG3_EEPROM_MAGIC) {
Matt Carlson141518c2009-12-03 08:36:22 +000012423 for (i = 0; i < TG3_NVM_VPD_LEN; i += 4) {
Michael Chan1b277772006-03-20 22:27:48 -080012424 u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012425
Matt Carlson6d348f22009-02-25 14:25:52 +000012426 /* The data is in little-endian format in NVRAM.
12427 * Use the big-endian read routines to preserve
12428 * the byte order as it exists in NVRAM.
12429 */
Matt Carlson141518c2009-12-03 08:36:22 +000012430 if (tg3_nvram_read_be32(tp, TG3_NVM_VPD_OFF + i, &tmp))
Michael Chan1b277772006-03-20 22:27:48 -080012431 goto out_not_found;
12432
Matt Carlson6d348f22009-02-25 14:25:52 +000012433 memcpy(&vpd_data[i], &tmp, sizeof(tmp));
Michael Chan1b277772006-03-20 22:27:48 -080012434 }
12435 } else {
Matt Carlson94c982b2009-12-03 08:36:23 +000012436 ssize_t cnt;
Matt Carlson4181b2c2010-02-26 14:04:45 +000012437 unsigned int pos = 0;
Michael Chan1b277772006-03-20 22:27:48 -080012438
Matt Carlson94c982b2009-12-03 08:36:23 +000012439 for (; pos < TG3_NVM_VPD_LEN && i < 3; i++, pos += cnt) {
12440 cnt = pci_read_vpd(tp->pdev, pos,
12441 TG3_NVM_VPD_LEN - pos,
12442 &vpd_data[pos]);
12443 if (cnt == -ETIMEDOUT || -EINTR)
12444 cnt = 0;
12445 else if (cnt < 0)
David S. Millerf49639e2006-06-09 11:58:36 -070012446 goto out_not_found;
Michael Chan1b277772006-03-20 22:27:48 -080012447 }
Matt Carlson94c982b2009-12-03 08:36:23 +000012448 if (pos != TG3_NVM_VPD_LEN)
12449 goto out_not_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012450 }
12451
Matt Carlson4181b2c2010-02-26 14:04:45 +000012452 i = pci_vpd_find_tag(vpd_data, 0, TG3_NVM_VPD_LEN,
12453 PCI_VPD_LRDT_RO_DATA);
12454 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012455 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000012456
12457 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
12458 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
12459 i += PCI_VPD_LRDT_TAG_SIZE;
12460
12461 if (block_end > TG3_NVM_VPD_LEN)
12462 goto out_not_found;
12463
Matt Carlson184b8902010-04-05 10:19:25 +000012464 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
12465 PCI_VPD_RO_KEYWORD_MFR_ID);
12466 if (j > 0) {
12467 len = pci_vpd_info_field_size(&vpd_data[j]);
12468
12469 j += PCI_VPD_INFO_FLD_HDR_SIZE;
12470 if (j + len > block_end || len != 4 ||
12471 memcmp(&vpd_data[j], "1028", 4))
12472 goto partno;
12473
12474 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
12475 PCI_VPD_RO_KEYWORD_VENDOR0);
12476 if (j < 0)
12477 goto partno;
12478
12479 len = pci_vpd_info_field_size(&vpd_data[j]);
12480
12481 j += PCI_VPD_INFO_FLD_HDR_SIZE;
12482 if (j + len > block_end)
12483 goto partno;
12484
12485 memcpy(tp->fw_ver, &vpd_data[j], len);
12486 strncat(tp->fw_ver, " bc ", TG3_NVM_VPD_LEN - len - 1);
12487 }
12488
12489partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000012490 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
12491 PCI_VPD_RO_KEYWORD_PARTNO);
12492 if (i < 0)
12493 goto out_not_found;
12494
12495 len = pci_vpd_info_field_size(&vpd_data[i]);
12496
12497 i += PCI_VPD_INFO_FLD_HDR_SIZE;
12498 if (len > TG3_BPN_SIZE ||
12499 (len + i) > TG3_NVM_VPD_LEN)
12500 goto out_not_found;
12501
12502 memcpy(tp->board_part_number, &vpd_data[i], len);
12503
12504 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012505
12506out_not_found:
Michael Chanb5d37722006-09-27 16:06:21 -070012507 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12508 strcpy(tp->board_part_number, "BCM95906");
Matt Carlsondf259d82009-04-20 06:57:14 +000012509 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12510 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
12511 strcpy(tp->board_part_number, "BCM57780");
12512 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12513 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
12514 strcpy(tp->board_part_number, "BCM57760");
12515 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12516 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
12517 strcpy(tp->board_part_number, "BCM57790");
Matt Carlson5e7ccf22009-08-25 10:08:42 +000012518 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12519 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
12520 strcpy(tp->board_part_number, "BCM57788");
Matt Carlsonb474eca2010-02-17 15:16:58 +000012521 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12522 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
12523 strcpy(tp->board_part_number, "BCM57761");
12524 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12525 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
Matt Carlsonb703df62009-12-03 08:36:21 +000012526 strcpy(tp->board_part_number, "BCM57765");
Matt Carlsonb474eca2010-02-17 15:16:58 +000012527 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12528 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
12529 strcpy(tp->board_part_number, "BCM57781");
12530 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12531 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
12532 strcpy(tp->board_part_number, "BCM57785");
12533 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12534 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
12535 strcpy(tp->board_part_number, "BCM57791");
12536 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12537 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
12538 strcpy(tp->board_part_number, "BCM57795");
Michael Chanb5d37722006-09-27 16:06:21 -070012539 else
12540 strcpy(tp->board_part_number, "none");
Linus Torvalds1da177e2005-04-16 15:20:36 -070012541}
12542
Matt Carlson9c8a6202007-10-21 16:16:08 -070012543static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
12544{
12545 u32 val;
12546
Matt Carlsone4f34112009-02-25 14:25:00 +000012547 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070012548 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000012549 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070012550 val != 0)
12551 return 0;
12552
12553 return 1;
12554}
12555
Matt Carlsonacd9c112009-02-25 14:26:33 +000012556static void __devinit tg3_read_bc_ver(struct tg3 *tp)
12557{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012558 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000012559 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012560 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000012561
12562 if (tg3_nvram_read(tp, 0xc, &offset) ||
12563 tg3_nvram_read(tp, 0x4, &start))
12564 return;
12565
12566 offset = tg3_nvram_logical_addr(tp, offset);
12567
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012568 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000012569 return;
12570
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012571 if ((val & 0xfc000000) == 0x0c000000) {
12572 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000012573 return;
12574
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012575 if (val == 0)
12576 newver = true;
12577 }
12578
Matt Carlson75f99362010-04-05 10:19:24 +000012579 dst_off = strlen(tp->fw_ver);
12580
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012581 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000012582 if (TG3_VER_SIZE - dst_off < 16 ||
12583 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012584 return;
12585
12586 offset = offset + ver_offset - start;
12587 for (i = 0; i < 16; i += 4) {
12588 __be32 v;
12589 if (tg3_nvram_read_be32(tp, offset + i, &v))
12590 return;
12591
Matt Carlson75f99362010-04-05 10:19:24 +000012592 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012593 }
12594 } else {
12595 u32 major, minor;
12596
12597 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
12598 return;
12599
12600 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
12601 TG3_NVM_BCVER_MAJSFT;
12602 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000012603 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
12604 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000012605 }
12606}
12607
Matt Carlsona6f6cb12009-02-25 14:27:43 +000012608static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
12609{
12610 u32 val, major, minor;
12611
12612 /* Use native endian representation */
12613 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
12614 return;
12615
12616 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
12617 TG3_NVM_HWSB_CFG1_MAJSFT;
12618 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
12619 TG3_NVM_HWSB_CFG1_MINSFT;
12620
12621 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
12622}
12623
Matt Carlsondfe00d72008-11-21 17:19:41 -080012624static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
12625{
12626 u32 offset, major, minor, build;
12627
Matt Carlson75f99362010-04-05 10:19:24 +000012628 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080012629
12630 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
12631 return;
12632
12633 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
12634 case TG3_EEPROM_SB_REVISION_0:
12635 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
12636 break;
12637 case TG3_EEPROM_SB_REVISION_2:
12638 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
12639 break;
12640 case TG3_EEPROM_SB_REVISION_3:
12641 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
12642 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000012643 case TG3_EEPROM_SB_REVISION_4:
12644 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
12645 break;
12646 case TG3_EEPROM_SB_REVISION_5:
12647 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
12648 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080012649 default:
12650 return;
12651 }
12652
Matt Carlsone4f34112009-02-25 14:25:00 +000012653 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080012654 return;
12655
12656 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
12657 TG3_EEPROM_SB_EDH_BLD_SHFT;
12658 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
12659 TG3_EEPROM_SB_EDH_MAJ_SHFT;
12660 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
12661
12662 if (minor > 99 || build > 26)
12663 return;
12664
Matt Carlson75f99362010-04-05 10:19:24 +000012665 offset = strlen(tp->fw_ver);
12666 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
12667 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080012668
12669 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000012670 offset = strlen(tp->fw_ver);
12671 if (offset < TG3_VER_SIZE - 1)
12672 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080012673 }
12674}
12675
Matt Carlsonacd9c112009-02-25 14:26:33 +000012676static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080012677{
12678 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000012679 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070012680
12681 for (offset = TG3_NVM_DIR_START;
12682 offset < TG3_NVM_DIR_END;
12683 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000012684 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012685 return;
12686
12687 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
12688 break;
12689 }
12690
12691 if (offset == TG3_NVM_DIR_END)
12692 return;
12693
12694 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
12695 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000012696 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012697 return;
12698
Matt Carlsone4f34112009-02-25 14:25:00 +000012699 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070012700 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000012701 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012702 return;
12703
12704 offset += val - start;
12705
Matt Carlsonacd9c112009-02-25 14:26:33 +000012706 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012707
Matt Carlsonacd9c112009-02-25 14:26:33 +000012708 tp->fw_ver[vlen++] = ',';
12709 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070012710
12711 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012712 __be32 v;
12713 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012714 return;
12715
Al Virob9fc7dc2007-12-17 22:59:57 -080012716 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012717
Matt Carlsonacd9c112009-02-25 14:26:33 +000012718 if (vlen > TG3_VER_SIZE - sizeof(v)) {
12719 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012720 break;
12721 }
12722
Matt Carlsonacd9c112009-02-25 14:26:33 +000012723 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
12724 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012725 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000012726}
12727
Matt Carlson7fd76442009-02-25 14:27:20 +000012728static void __devinit tg3_read_dash_ver(struct tg3 *tp)
12729{
12730 int vlen;
12731 u32 apedata;
Matt Carlsonecc79642010-08-02 11:26:01 +000012732 char *fwtype;
Matt Carlson7fd76442009-02-25 14:27:20 +000012733
12734 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) ||
12735 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
12736 return;
12737
12738 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
12739 if (apedata != APE_SEG_SIG_MAGIC)
12740 return;
12741
12742 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
12743 if (!(apedata & APE_FW_STATUS_READY))
12744 return;
12745
12746 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
12747
Matt Carlsonecc79642010-08-02 11:26:01 +000012748 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI)
12749 fwtype = "NCSI";
12750 else
12751 fwtype = "DASH";
12752
Matt Carlson7fd76442009-02-25 14:27:20 +000012753 vlen = strlen(tp->fw_ver);
12754
Matt Carlsonecc79642010-08-02 11:26:01 +000012755 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
12756 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000012757 (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 Carlsonc885e822010-08-02 11:25:57 +000013010 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13011 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13012 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
13013 tp->tg3_flags3 |= TG3_FLG3_5717_PLUS;
13014
Matt Carlson321d32a2008-11-21 17:22:19 -080013015 /* Intentionally exclude ASIC_REV_5906 */
13016 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Michael Chand9ab5ad2006-03-20 22:27:35 -080013017 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013018 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013019 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013020 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013021 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlsonc885e822010-08-02 11:25:57 +000013022 (tp->tg3_flags3 & TG3_FLG3_5717_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080013023 tp->tg3_flags3 |= TG3_FLG3_5755_PLUS;
13024
13025 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13026 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Michael Chanb5d37722006-09-27 16:06:21 -070013027 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013028 (tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
Michael Chana4e2b342005-10-26 15:46:52 -070013029 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
John W. Linville6708e5c2005-04-21 17:00:52 -070013030 tp->tg3_flags2 |= TG3_FLG2_5750_PLUS;
13031
John W. Linville1b440c562005-04-21 17:03:18 -070013032 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) ||
13033 (tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
13034 tp->tg3_flags2 |= TG3_FLG2_5705_PLUS;
13035
Matt Carlson027455a2008-12-21 20:19:30 -080013036 /* 5700 B0 chips do not support checksumming correctly due
13037 * to hardware bugs.
13038 */
13039 if (tp->pci_chip_rev_id == CHIPREV_ID_5700_B0)
13040 tp->tg3_flags |= TG3_FLAG_BROKEN_CHECKSUMS;
13041 else {
Eric Dumazet7fe876a2010-07-08 06:14:55 +000013042 unsigned long features = NETIF_F_IP_CSUM | NETIF_F_SG | NETIF_F_GRO;
13043
Matt Carlson027455a2008-12-21 20:19:30 -080013044 tp->tg3_flags |= TG3_FLAG_RX_CHECKSUMS;
Matt Carlson027455a2008-12-21 20:19:30 -080013045 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Eric Dumazet7fe876a2010-07-08 06:14:55 +000013046 features |= NETIF_F_IPV6_CSUM;
13047 tp->dev->features |= features;
13048 vlan_features_add(tp->dev, features);
Matt Carlson027455a2008-12-21 20:19:30 -080013049 }
13050
Matt Carlson507399f2009-11-13 13:03:37 +000013051 /* Determine TSO capabilities */
Matt Carlsonc885e822010-08-02 11:25:57 +000013052 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)
Matt Carlsone849cdc2009-11-13 13:03:38 +000013053 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_3;
13054 else if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
13055 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlson507399f2009-11-13 13:03:37 +000013056 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_2;
13057 else if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
13058 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_1 | TG3_FLG2_TSO_BUG;
13059 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
13060 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
13061 tp->tg3_flags2 &= ~TG3_FLG2_TSO_BUG;
13062 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13063 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13064 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
13065 tp->tg3_flags2 |= TG3_FLG2_TSO_BUG;
13066 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
13067 tp->fw_needed = FIRMWARE_TG3TSO5;
13068 else
13069 tp->fw_needed = FIRMWARE_TG3TSO;
13070 }
13071
13072 tp->irq_max = 1;
13073
Michael Chan5a6f3072006-03-20 22:28:05 -080013074 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
Michael Chan7544b092007-05-05 13:08:32 -070013075 tp->tg3_flags |= TG3_FLAG_SUPPORT_MSI;
13076 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
13077 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
13078 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
13079 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
13080 tp->pdev_peer == tp->pdev))
13081 tp->tg3_flags &= ~TG3_FLAG_SUPPORT_MSI;
13082
Matt Carlson321d32a2008-11-21 17:22:19 -080013083 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070013084 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanfcfa0a32006-03-20 22:28:41 -080013085 tp->tg3_flags2 |= TG3_FLG2_1SHOT_MSI;
Michael Chan52c0fd82006-06-29 20:15:54 -070013086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013087
Matt Carlsonc885e822010-08-02 11:25:57 +000013088 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
Matt Carlson507399f2009-11-13 13:03:37 +000013089 tp->tg3_flags |= TG3_FLAG_SUPPORT_MSIX;
13090 tp->irq_max = TG3_IRQ_MAX_VECS;
13091 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013092 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000013093
Matt Carlson615774f2009-11-13 13:03:39 +000013094 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsona50d0792010-06-05 17:24:37 +000013095 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlson615774f2009-11-13 13:03:39 +000013096 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
13097 tp->tg3_flags3 |= TG3_FLG3_SHORT_DMA_BUG;
13098 else if (!(tp->tg3_flags3 & TG3_FLG3_5755_PLUS)) {
13099 tp->tg3_flags3 |= TG3_FLG3_4G_DMA_BNDRY_BUG;
13100 tp->tg3_flags3 |= TG3_FLG3_40BIT_DMA_LIMIT_BUG;
Matt Carlson0e1406d2009-11-02 12:33:33 +000013101 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013102
Matt Carlsonc885e822010-08-02 11:25:57 +000013103 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)
Matt Carlsonb703df62009-12-03 08:36:21 +000013104 tp->tg3_flags3 |= TG3_FLG3_USE_JUMBO_BDFLAG;
13105
Matt Carlsonf51f3562008-05-25 23:45:08 -070013106 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
Matt Carlsonc6cdf432010-04-05 10:19:26 +000013107 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
13108 (tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG))
Matt Carlson8f666b02009-08-28 13:58:24 +000013109 tp->tg3_flags |= TG3_FLAG_JUMBO_CAPABLE;
Michael Chan0f893dc2005-07-25 12:30:38 -070013110
Matt Carlson52f44902008-11-21 17:17:04 -080013111 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
13112 &pci_state_reg);
13113
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013114 tp->pcie_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_EXP);
13115 if (tp->pcie_cap != 0) {
13116 u16 lnkctl;
13117
Linus Torvalds1da177e2005-04-16 15:20:36 -070013118 tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS;
Matt Carlson5f5c51e2007-11-12 21:19:37 -080013119
13120 pcie_set_readrq(tp->pdev, 4096);
13121
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013122 pci_read_config_word(tp->pdev,
13123 tp->pcie_cap + PCI_EXP_LNKCTL,
13124 &lnkctl);
13125 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
13126 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanc7835a72006-11-15 21:14:42 -080013127 tp->tg3_flags2 &= ~TG3_FLG2_HW_TSO_2;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013128 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013129 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000013130 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
13131 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013132 tp->tg3_flags3 |= TG3_FLG3_CLKREQ_BUG;
Matt Carlson614b0592010-01-20 16:58:02 +000013133 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
13134 tp->tg3_flags3 |= TG3_FLG3_L1PLLPD_EN;
Michael Chanc7835a72006-11-15 21:14:42 -080013135 }
Matt Carlson52f44902008-11-21 17:17:04 -080013136 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Matt Carlsonfcb389d2008-11-03 16:55:44 -080013137 tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS;
Matt Carlson52f44902008-11-21 17:17:04 -080013138 } else if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
13139 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
13140 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
13141 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000013142 dev_err(&tp->pdev->dev,
13143 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080013144 return -EIO;
13145 }
13146
13147 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
13148 tp->tg3_flags |= TG3_FLAG_PCIX_MODE;
13149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013150
Michael Chan399de502005-10-03 14:02:39 -070013151 /* If we have an AMD 762 or VIA K8T800 chipset, write
13152 * reordering to the mailbox registers done by the host
13153 * controller can cause major troubles. We read back from
13154 * every mailbox register write to force the writes to be
13155 * posted to the chip in order.
13156 */
13157 if (pci_dev_present(write_reorder_chipsets) &&
13158 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
13159 tp->tg3_flags |= TG3_FLAG_MBOX_WRITE_REORDER;
13160
Matt Carlson69fc4052008-12-21 20:19:57 -080013161 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
13162 &tp->pci_cacheline_sz);
13163 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13164 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013165 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
13166 tp->pci_lat_timer < 64) {
13167 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080013168 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13169 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013170 }
13171
Matt Carlson52f44902008-11-21 17:17:04 -080013172 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
13173 /* 5700 BX chips need to have their TX producer index
13174 * mailboxes written twice to workaround a bug.
13175 */
13176 tp->tg3_flags |= TG3_FLAG_TXD_MBOX_HWBUG;
Matt Carlson9974a352007-10-07 23:27:28 -070013177
Matt Carlson52f44902008-11-21 17:17:04 -080013178 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013179 *
13180 * The workaround is to use indirect register accesses
13181 * for all chip writes not to mailbox registers.
13182 */
Matt Carlson52f44902008-11-21 17:17:04 -080013183 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013184 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013185
13186 tp->tg3_flags |= TG3_FLAG_PCIX_TARGET_HWBUG;
13187
13188 /* The chip can have it's power management PCI config
13189 * space registers clobbered due to this bug.
13190 * So explicitly force the chip into D0 here.
13191 */
Matt Carlson9974a352007-10-07 23:27:28 -070013192 pci_read_config_dword(tp->pdev,
13193 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013194 &pm_reg);
13195 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
13196 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070013197 pci_write_config_dword(tp->pdev,
13198 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013199 pm_reg);
13200
13201 /* Also, force SERR#/PERR# in PCI command. */
13202 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13203 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
13204 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13205 }
13206 }
13207
Linus Torvalds1da177e2005-04-16 15:20:36 -070013208 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
13209 tp->tg3_flags |= TG3_FLAG_PCI_HIGH_SPEED;
13210 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
13211 tp->tg3_flags |= TG3_FLAG_PCI_32BIT;
13212
13213 /* Chip-specific fixup from Broadcom driver */
13214 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
13215 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
13216 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
13217 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
13218 }
13219
Michael Chan1ee582d2005-08-09 20:16:46 -070013220 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070013221 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013222 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070013223 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070013224 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013225 tp->write32_tx_mbox = tg3_write32;
13226 tp->write32_rx_mbox = tg3_write32;
13227
13228 /* Various workaround register access methods */
13229 if (tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG)
13230 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013231 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
13232 ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
13233 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
13234 /*
13235 * Back to back register writes can cause problems on these
13236 * chips, the workaround is to read back all reg writes
13237 * except those to mailbox regs.
13238 *
13239 * See tg3_write_indirect_reg32().
13240 */
Michael Chan1ee582d2005-08-09 20:16:46 -070013241 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013242 }
13243
Michael Chan1ee582d2005-08-09 20:16:46 -070013244 if ((tp->tg3_flags & TG3_FLAG_TXD_MBOX_HWBUG) ||
13245 (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)) {
13246 tp->write32_tx_mbox = tg3_write32_tx_mbox;
13247 if (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)
13248 tp->write32_rx_mbox = tg3_write_flush_reg32;
13249 }
Michael Chan20094932005-08-09 20:16:32 -070013250
Michael Chan68929142005-08-09 20:17:14 -070013251 if (tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND) {
13252 tp->read32 = tg3_read_indirect_reg32;
13253 tp->write32 = tg3_write_indirect_reg32;
13254 tp->read32_mbox = tg3_read_indirect_mbox;
13255 tp->write32_mbox = tg3_write_indirect_mbox;
13256 tp->write32_tx_mbox = tg3_write_indirect_mbox;
13257 tp->write32_rx_mbox = tg3_write_indirect_mbox;
13258
13259 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070013260 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070013261
13262 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13263 pci_cmd &= ~PCI_COMMAND_MEMORY;
13264 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13265 }
Michael Chanb5d37722006-09-27 16:06:21 -070013266 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
13267 tp->read32_mbox = tg3_read32_mbox_5906;
13268 tp->write32_mbox = tg3_write32_mbox_5906;
13269 tp->write32_tx_mbox = tg3_write32_mbox_5906;
13270 tp->write32_rx_mbox = tg3_write32_mbox_5906;
13271 }
Michael Chan68929142005-08-09 20:17:14 -070013272
Michael Chanbbadf502006-04-06 21:46:34 -070013273 if (tp->write32 == tg3_write_indirect_reg32 ||
13274 ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) &&
13275 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070013276 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Michael Chanbbadf502006-04-06 21:46:34 -070013277 tp->tg3_flags |= TG3_FLAG_SRAM_USE_CONFIG;
13278
Michael Chan7d0c41e2005-04-21 17:06:20 -070013279 /* Get eeprom hw config before calling tg3_set_power_state().
Michael Chan9d26e212006-12-07 00:21:14 -080013280 * In particular, the TG3_FLG2_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070013281 * determined before calling tg3_set_power_state() so that
13282 * we know whether or not to switch out of Vaux power.
13283 * When the flag is set, it means that GPIO1 is used for eeprom
13284 * write protect and also implies that it is a LOM where GPIOs
13285 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013286 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070013287 tg3_get_eeprom_hw_cfg(tp);
13288
Matt Carlson0d3031d2007-10-10 18:02:43 -070013289 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
13290 /* Allow reads and writes to the
13291 * APE register and memory space.
13292 */
13293 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +000013294 PCISTATE_ALLOW_APE_SHMEM_WR |
13295 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070013296 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
13297 pci_state_reg);
13298 }
13299
Matt Carlson9936bcf2007-10-10 18:03:07 -070013300 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013301 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013302 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013303 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlsonc885e822010-08-02 11:25:57 +000013304 (tp->tg3_flags3 & TG3_FLG3_5717_PLUS))
Matt Carlsond30cdd22007-10-07 23:28:35 -070013305 tp->tg3_flags |= TG3_FLAG_CPMU_PRESENT;
13306
Michael Chan314fba32005-04-21 17:07:04 -070013307 /* Set up tp->grc_local_ctrl before calling tg3_set_power_state().
13308 * GPIO1 driven high will bring 5700's external PHY out of reset.
13309 * It is also used as eeprom write protect on LOMs.
13310 */
13311 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
13312 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
13313 (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT))
13314 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
13315 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070013316 /* Unused GPIO3 must be driven as output on 5752 because there
13317 * are no pull-up resistors on unused GPIO pins.
13318 */
13319 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
13320 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070013321
Matt Carlson321d32a2008-11-21 17:22:19 -080013322 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsoncb4ed1f2010-01-20 16:58:09 +000013323 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
13324 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Michael Chanaf36e6b2006-03-23 01:28:06 -080013325 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
13326
Matt Carlson8d519ab2009-04-20 06:58:01 +000013327 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
13328 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070013329 /* Turn off the debug UART. */
13330 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
13331 if (tp->tg3_flags2 & TG3_FLG2_IS_NIC)
13332 /* Keep VMain power. */
13333 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
13334 GRC_LCLCTRL_GPIO_OUTPUT0;
13335 }
13336
Linus Torvalds1da177e2005-04-16 15:20:36 -070013337 /* Force the chip into D0. */
Michael Chanbc1c7562006-03-20 17:48:03 -080013338 err = tg3_set_power_state(tp, PCI_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013339 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000013340 dev_err(&tp->pdev->dev, "Transition to D0 failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070013341 return err;
13342 }
13343
Linus Torvalds1da177e2005-04-16 15:20:36 -070013344 /* Derive initial jumbo mode from MTU assigned in
13345 * ether_setup() via the alloc_etherdev() call
13346 */
Michael Chan0f893dc2005-07-25 12:30:38 -070013347 if (tp->dev->mtu > ETH_DATA_LEN &&
Michael Chana4e2b342005-10-26 15:46:52 -070013348 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Michael Chan0f893dc2005-07-25 12:30:38 -070013349 tp->tg3_flags |= TG3_FLAG_JUMBO_RING_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013350
13351 /* Determine WakeOnLan speed to use. */
13352 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13353 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
13354 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
13355 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
13356 tp->tg3_flags &= ~(TG3_FLAG_WOL_SPEED_100MB);
13357 } else {
13358 tp->tg3_flags |= TG3_FLAG_WOL_SPEED_100MB;
13359 }
13360
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013361 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
13362 tp->tg3_flags3 |= TG3_FLG3_PHY_IS_FET;
13363
Linus Torvalds1da177e2005-04-16 15:20:36 -070013364 /* A few boards don't want Ethernet@WireSpeed phy feature */
13365 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
13366 ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
13367 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070013368 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013369 (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) ||
Michael Chan747e8f82005-07-25 12:33:22 -070013370 (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013371 tp->tg3_flags2 |= TG3_FLG2_NO_ETH_WIRE_SPEED;
13372
13373 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
13374 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
13375 tp->tg3_flags2 |= TG3_FLG2_PHY_ADC_BUG;
13376 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
13377 tp->tg3_flags2 |= TG3_FLG2_PHY_5704_A0_BUG;
13378
Matt Carlson321d32a2008-11-21 17:22:19 -080013379 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013380 !(tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080013381 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013382 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
Matt Carlsonc885e822010-08-02 11:25:57 +000013383 !(tp->tg3_flags3 & TG3_FLG3_5717_PLUS)) {
Michael Chanc424cb22006-04-29 18:56:34 -070013384 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013385 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013386 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
13387 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080013388 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
13389 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
13390 tp->tg3_flags2 |= TG3_FLG2_PHY_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080013391 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
13392 tp->tg3_flags2 |= TG3_FLG2_PHY_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080013393 } else
Michael Chanc424cb22006-04-29 18:56:34 -070013394 tp->tg3_flags2 |= TG3_FLG2_PHY_BER_BUG;
13395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013396
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013397 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
13398 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
13399 tp->phy_otp = tg3_read_otp_phycfg(tp);
13400 if (tp->phy_otp == 0)
13401 tp->phy_otp = TG3_OTP_DEFAULT;
13402 }
13403
Matt Carlsonf51f3562008-05-25 23:45:08 -070013404 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT)
Matt Carlson8ef21422008-05-02 16:47:53 -070013405 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
13406 else
13407 tp->mi_mode = MAC_MI_MODE_BASE;
13408
Linus Torvalds1da177e2005-04-16 15:20:36 -070013409 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013410 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
13411 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
13412 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
13413
Matt Carlson321d32a2008-11-21 17:22:19 -080013414 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
13415 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson57e69832008-05-25 23:48:31 -070013416 tp->tg3_flags3 |= TG3_FLG3_USE_PHYLIB;
13417
Matt Carlson158d7ab2008-05-29 01:37:54 -070013418 err = tg3_mdio_init(tp);
13419 if (err)
13420 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013421
Matt Carlson55dffe72010-01-12 10:11:39 +000013422 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 &&
Matt Carlson2138c002010-07-11 09:31:43 +000013423 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0)
Matt Carlson55dffe72010-01-12 10:11:39 +000013424 return -ENOTSUPP;
13425
Linus Torvalds1da177e2005-04-16 15:20:36 -070013426 /* Initialize data/descriptor byte/word swapping. */
13427 val = tr32(GRC_MODE);
13428 val &= GRC_MODE_HOST_STACKUP;
13429 tw32(GRC_MODE, val | tp->grc_mode);
13430
13431 tg3_switch_clocks(tp);
13432
13433 /* Clear this out for sanity. */
13434 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
13435
13436 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
13437 &pci_state_reg);
13438 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
13439 (tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG) == 0) {
13440 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
13441
13442 if (chiprevid == CHIPREV_ID_5701_A0 ||
13443 chiprevid == CHIPREV_ID_5701_B0 ||
13444 chiprevid == CHIPREV_ID_5701_B2 ||
13445 chiprevid == CHIPREV_ID_5701_B5) {
13446 void __iomem *sram_base;
13447
13448 /* Write some dummy words into the SRAM status block
13449 * area, see if it reads back correctly. If the return
13450 * value is bad, force enable the PCIX workaround.
13451 */
13452 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
13453
13454 writel(0x00000000, sram_base);
13455 writel(0x00000000, sram_base + 4);
13456 writel(0xffffffff, sram_base + 4);
13457 if (readl(sram_base) != 0x00000000)
13458 tp->tg3_flags |= TG3_FLAG_PCIX_TARGET_HWBUG;
13459 }
13460 }
13461
13462 udelay(50);
13463 tg3_nvram_init(tp);
13464
13465 grc_misc_cfg = tr32(GRC_MISC_CFG);
13466 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
13467
Linus Torvalds1da177e2005-04-16 15:20:36 -070013468 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
13469 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
13470 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
13471 tp->tg3_flags2 |= TG3_FLG2_IS_5788;
13472
David S. Millerfac9b832005-05-18 22:46:34 -070013473 if (!(tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
13474 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700))
13475 tp->tg3_flags |= TG3_FLAG_TAGGED_STATUS;
13476 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) {
13477 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
13478 HOSTCC_MODE_CLRTICK_TXBD);
13479
13480 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
13481 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13482 tp->misc_host_ctrl);
13483 }
13484
Matt Carlson3bda1252008-08-15 14:08:22 -070013485 /* Preserve the APE MAC_MODE bits */
13486 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
13487 tp->mac_mode = tr32(MAC_MODE) |
13488 MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
13489 else
13490 tp->mac_mode = TG3_DEF_MAC_MODE;
13491
Linus Torvalds1da177e2005-04-16 15:20:36 -070013492 /* these are limited to 10/100 only */
13493 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
13494 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
13495 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
13496 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
13497 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
13498 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
13499 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
13500 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
13501 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
Michael Chan676917d2006-12-07 00:20:22 -080013502 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
13503 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013504 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
Matt Carlsond1101142010-02-17 15:16:55 +000013505 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
13506 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013507 (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013508 tp->tg3_flags |= TG3_FLAG_10_100_ONLY;
13509
13510 err = tg3_phy_probe(tp);
13511 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000013512 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013513 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013514 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013515 }
13516
Matt Carlson184b8902010-04-05 10:19:25 +000013517 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080013518 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013519
13520 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
13521 tp->tg3_flags &= ~TG3_FLAG_USE_MI_INTERRUPT;
13522 } else {
13523 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
13524 tp->tg3_flags |= TG3_FLAG_USE_MI_INTERRUPT;
13525 else
13526 tp->tg3_flags &= ~TG3_FLAG_USE_MI_INTERRUPT;
13527 }
13528
13529 /* 5700 {AX,BX} chips have a broken status block link
13530 * change bit implementation, so we must use the
13531 * status register in those cases.
13532 */
13533 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
13534 tp->tg3_flags |= TG3_FLAG_USE_LINKCHG_REG;
13535 else
13536 tp->tg3_flags &= ~TG3_FLAG_USE_LINKCHG_REG;
13537
13538 /* The led_ctrl is set during tg3_phy_probe, here we might
13539 * have to force the link status polling mechanism based
13540 * upon subsystem IDs.
13541 */
13542 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070013543 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070013544 !(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)) {
13545 tp->tg3_flags |= (TG3_FLAG_USE_MI_INTERRUPT |
13546 TG3_FLAG_USE_LINKCHG_REG);
13547 }
13548
13549 /* For all SERDES we poll the MAC status register. */
13550 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
13551 tp->tg3_flags |= TG3_FLAG_POLL_SERDES;
13552 else
13553 tp->tg3_flags &= ~TG3_FLAG_POLL_SERDES;
13554
Matt Carlson9dc7a112010-04-12 06:58:28 +000013555 tp->rx_offset = NET_IP_ALIGN + TG3_RX_HEADROOM;
Matt Carlsond2757fc2010-04-12 06:58:27 +000013556 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013557 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsond2757fc2010-04-12 06:58:27 +000013558 (tp->tg3_flags & TG3_FLAG_PCIX_MODE) != 0) {
Matt Carlson9dc7a112010-04-12 06:58:28 +000013559 tp->rx_offset -= NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000013560#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000013561 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000013562#endif
13563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013564
Michael Chanf92905d2006-06-29 20:14:29 -070013565 tp->rx_std_max_post = TG3_RX_RING_SIZE;
13566
13567 /* Increment the rx prod index on the rx std ring by at most
13568 * 8 for these chips to workaround hw errata.
13569 */
13570 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13571 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
13572 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
13573 tp->rx_std_max_post = 8;
13574
Matt Carlson8ed5d972007-05-07 00:25:49 -070013575 if (tp->tg3_flags & TG3_FLAG_ASPM_WORKAROUND)
13576 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
13577 PCIE_PWR_MGMT_L1_THRESH_MSK;
13578
Linus Torvalds1da177e2005-04-16 15:20:36 -070013579 return err;
13580}
13581
David S. Miller49b6e95f2007-03-29 01:38:42 -070013582#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070013583static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
13584{
13585 struct net_device *dev = tp->dev;
13586 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070013587 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070013588 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070013589 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013590
David S. Miller49b6e95f2007-03-29 01:38:42 -070013591 addr = of_get_property(dp, "local-mac-address", &len);
13592 if (addr && len == 6) {
13593 memcpy(dev->dev_addr, addr, 6);
13594 memcpy(dev->perm_addr, dev->dev_addr, 6);
13595 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013596 }
13597 return -ENODEV;
13598}
13599
13600static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
13601{
13602 struct net_device *dev = tp->dev;
13603
13604 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070013605 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013606 return 0;
13607}
13608#endif
13609
13610static int __devinit tg3_get_device_address(struct tg3 *tp)
13611{
13612 struct net_device *dev = tp->dev;
13613 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080013614 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013615
David S. Miller49b6e95f2007-03-29 01:38:42 -070013616#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070013617 if (!tg3_get_macaddr_sparc(tp))
13618 return 0;
13619#endif
13620
13621 mac_offset = 0x7c;
David S. Millerf49639e2006-06-09 11:58:36 -070013622 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) ||
Michael Chana4e2b342005-10-26 15:46:52 -070013623 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013624 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
13625 mac_offset = 0xcc;
13626 if (tg3_nvram_lock(tp))
13627 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
13628 else
13629 tg3_nvram_unlock(tp);
Matt Carlsona50d0792010-06-05 17:24:37 +000013630 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13631 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) {
13632 if (PCI_FUNC(tp->pdev->devfn) & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000013633 mac_offset = 0xcc;
Matt Carlsona50d0792010-06-05 17:24:37 +000013634 if (PCI_FUNC(tp->pdev->devfn) > 1)
13635 mac_offset += 0x18c;
Matt Carlsona1b950d2009-09-01 13:20:17 +000013636 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070013637 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013638
13639 /* First try to get it from MAC address mailbox. */
13640 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
13641 if ((hi >> 16) == 0x484b) {
13642 dev->dev_addr[0] = (hi >> 8) & 0xff;
13643 dev->dev_addr[1] = (hi >> 0) & 0xff;
13644
13645 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
13646 dev->dev_addr[2] = (lo >> 24) & 0xff;
13647 dev->dev_addr[3] = (lo >> 16) & 0xff;
13648 dev->dev_addr[4] = (lo >> 8) & 0xff;
13649 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013650
Michael Chan008652b2006-03-27 23:14:53 -080013651 /* Some old bootcode may report a 0 MAC address in SRAM */
13652 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
13653 }
13654 if (!addr_ok) {
13655 /* Next, try NVRAM. */
Matt Carlsondf259d82009-04-20 06:57:14 +000013656 if (!(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) &&
13657 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000013658 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070013659 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
13660 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080013661 }
13662 /* Finally just fetch it out of the MAC control regs. */
13663 else {
13664 hi = tr32(MAC_ADDR_0_HIGH);
13665 lo = tr32(MAC_ADDR_0_LOW);
13666
13667 dev->dev_addr[5] = lo & 0xff;
13668 dev->dev_addr[4] = (lo >> 8) & 0xff;
13669 dev->dev_addr[3] = (lo >> 16) & 0xff;
13670 dev->dev_addr[2] = (lo >> 24) & 0xff;
13671 dev->dev_addr[1] = hi & 0xff;
13672 dev->dev_addr[0] = (hi >> 8) & 0xff;
13673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013674 }
13675
13676 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070013677#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070013678 if (!tg3_get_default_macaddr_sparc(tp))
13679 return 0;
13680#endif
13681 return -EINVAL;
13682 }
John W. Linville2ff43692005-09-12 14:44:20 -070013683 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013684 return 0;
13685}
13686
David S. Miller59e6b432005-05-18 22:50:10 -070013687#define BOUNDARY_SINGLE_CACHELINE 1
13688#define BOUNDARY_MULTI_CACHELINE 2
13689
13690static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
13691{
13692 int cacheline_size;
13693 u8 byte;
13694 int goal;
13695
13696 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
13697 if (byte == 0)
13698 cacheline_size = 1024;
13699 else
13700 cacheline_size = (int) byte * 4;
13701
13702 /* On 5703 and later chips, the boundary bits have no
13703 * effect.
13704 */
13705 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13706 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13707 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
13708 goto out;
13709
13710#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
13711 goal = BOUNDARY_MULTI_CACHELINE;
13712#else
13713#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
13714 goal = BOUNDARY_SINGLE_CACHELINE;
13715#else
13716 goal = 0;
13717#endif
13718#endif
13719
Matt Carlsonc885e822010-08-02 11:25:57 +000013720 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000013721 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
13722 goto out;
13723 }
13724
David S. Miller59e6b432005-05-18 22:50:10 -070013725 if (!goal)
13726 goto out;
13727
13728 /* PCI controllers on most RISC systems tend to disconnect
13729 * when a device tries to burst across a cache-line boundary.
13730 * Therefore, letting tg3 do so just wastes PCI bandwidth.
13731 *
13732 * Unfortunately, for PCI-E there are only limited
13733 * write-side controls for this, and thus for reads
13734 * we will still get the disconnects. We'll also waste
13735 * these PCI cycles for both read and write for chips
13736 * other than 5700 and 5701 which do not implement the
13737 * boundary bits.
13738 */
13739 if ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) &&
13740 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)) {
13741 switch (cacheline_size) {
13742 case 16:
13743 case 32:
13744 case 64:
13745 case 128:
13746 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13747 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
13748 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
13749 } else {
13750 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
13751 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
13752 }
13753 break;
13754
13755 case 256:
13756 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
13757 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
13758 break;
13759
13760 default:
13761 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
13762 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
13763 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070013764 }
David S. Miller59e6b432005-05-18 22:50:10 -070013765 } else if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
13766 switch (cacheline_size) {
13767 case 16:
13768 case 32:
13769 case 64:
13770 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13771 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
13772 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
13773 break;
13774 }
13775 /* fallthrough */
13776 case 128:
13777 default:
13778 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
13779 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
13780 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070013781 }
David S. Miller59e6b432005-05-18 22:50:10 -070013782 } else {
13783 switch (cacheline_size) {
13784 case 16:
13785 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13786 val |= (DMA_RWCTRL_READ_BNDRY_16 |
13787 DMA_RWCTRL_WRITE_BNDRY_16);
13788 break;
13789 }
13790 /* fallthrough */
13791 case 32:
13792 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13793 val |= (DMA_RWCTRL_READ_BNDRY_32 |
13794 DMA_RWCTRL_WRITE_BNDRY_32);
13795 break;
13796 }
13797 /* fallthrough */
13798 case 64:
13799 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13800 val |= (DMA_RWCTRL_READ_BNDRY_64 |
13801 DMA_RWCTRL_WRITE_BNDRY_64);
13802 break;
13803 }
13804 /* fallthrough */
13805 case 128:
13806 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13807 val |= (DMA_RWCTRL_READ_BNDRY_128 |
13808 DMA_RWCTRL_WRITE_BNDRY_128);
13809 break;
13810 }
13811 /* fallthrough */
13812 case 256:
13813 val |= (DMA_RWCTRL_READ_BNDRY_256 |
13814 DMA_RWCTRL_WRITE_BNDRY_256);
13815 break;
13816 case 512:
13817 val |= (DMA_RWCTRL_READ_BNDRY_512 |
13818 DMA_RWCTRL_WRITE_BNDRY_512);
13819 break;
13820 case 1024:
13821 default:
13822 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
13823 DMA_RWCTRL_WRITE_BNDRY_1024);
13824 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070013825 }
David S. Miller59e6b432005-05-18 22:50:10 -070013826 }
13827
13828out:
13829 return val;
13830}
13831
Linus Torvalds1da177e2005-04-16 15:20:36 -070013832static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
13833{
13834 struct tg3_internal_buffer_desc test_desc;
13835 u32 sram_dma_descs;
13836 int i, ret;
13837
13838 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
13839
13840 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
13841 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
13842 tw32(RDMAC_STATUS, 0);
13843 tw32(WDMAC_STATUS, 0);
13844
13845 tw32(BUFMGR_MODE, 0);
13846 tw32(FTQ_RESET, 0);
13847
13848 test_desc.addr_hi = ((u64) buf_dma) >> 32;
13849 test_desc.addr_lo = buf_dma & 0xffffffff;
13850 test_desc.nic_mbuf = 0x00002100;
13851 test_desc.len = size;
13852
13853 /*
13854 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
13855 * the *second* time the tg3 driver was getting loaded after an
13856 * initial scan.
13857 *
13858 * Broadcom tells me:
13859 * ...the DMA engine is connected to the GRC block and a DMA
13860 * reset may affect the GRC block in some unpredictable way...
13861 * The behavior of resets to individual blocks has not been tested.
13862 *
13863 * Broadcom noted the GRC reset will also reset all sub-components.
13864 */
13865 if (to_device) {
13866 test_desc.cqid_sqid = (13 << 8) | 2;
13867
13868 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
13869 udelay(40);
13870 } else {
13871 test_desc.cqid_sqid = (16 << 8) | 7;
13872
13873 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
13874 udelay(40);
13875 }
13876 test_desc.flags = 0x00000005;
13877
13878 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
13879 u32 val;
13880
13881 val = *(((u32 *)&test_desc) + i);
13882 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
13883 sram_dma_descs + (i * sizeof(u32)));
13884 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
13885 }
13886 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
13887
Matt Carlson859a588792010-04-05 10:19:28 +000013888 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013889 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a588792010-04-05 10:19:28 +000013890 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070013891 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013892
13893 ret = -ENODEV;
13894 for (i = 0; i < 40; i++) {
13895 u32 val;
13896
13897 if (to_device)
13898 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
13899 else
13900 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
13901 if ((val & 0xffff) == sram_dma_descs) {
13902 ret = 0;
13903 break;
13904 }
13905
13906 udelay(100);
13907 }
13908
13909 return ret;
13910}
13911
David S. Millerded73402005-05-23 13:59:47 -070013912#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070013913
13914static int __devinit tg3_test_dma(struct tg3 *tp)
13915{
13916 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070013917 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000013918 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013919
13920 buf = pci_alloc_consistent(tp->pdev, TEST_BUFFER_SIZE, &buf_dma);
13921 if (!buf) {
13922 ret = -ENOMEM;
13923 goto out_nofree;
13924 }
13925
13926 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
13927 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
13928
David S. Miller59e6b432005-05-18 22:50:10 -070013929 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013930
Matt Carlsonc885e822010-08-02 11:25:57 +000013931 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000013932 goto out;
13933
Linus Torvalds1da177e2005-04-16 15:20:36 -070013934 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
13935 /* DMA read watermark not used on PCIE */
13936 tp->dma_rwctrl |= 0x00180000;
13937 } else if (!(tp->tg3_flags & TG3_FLAG_PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070013938 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
13939 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013940 tp->dma_rwctrl |= 0x003f0000;
13941 else
13942 tp->dma_rwctrl |= 0x003f000f;
13943 } else {
13944 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
13945 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
13946 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080013947 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013948
Michael Chan4a29cc22006-03-19 13:21:12 -080013949 /* If the 5704 is behind the EPB bridge, we can
13950 * do the less restrictive ONE_DMA workaround for
13951 * better performance.
13952 */
13953 if ((tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG) &&
13954 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
13955 tp->dma_rwctrl |= 0x8000;
13956 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013957 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
13958
Michael Chan49afdeb2007-02-13 12:17:03 -080013959 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
13960 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070013961 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080013962 tp->dma_rwctrl |=
13963 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
13964 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
13965 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070013966 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
13967 /* 5780 always in PCIX mode */
13968 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070013969 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
13970 /* 5714 always in PCIX mode */
13971 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013972 } else {
13973 tp->dma_rwctrl |= 0x001b000f;
13974 }
13975 }
13976
13977 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
13978 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
13979 tp->dma_rwctrl &= 0xfffffff0;
13980
13981 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13982 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
13983 /* Remove this if it causes problems for some boards. */
13984 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
13985
13986 /* On 5700/5701 chips, we need to set this bit.
13987 * Otherwise the chip will issue cacheline transactions
13988 * to streamable DMA memory with not all the byte
13989 * enables turned on. This is an error on several
13990 * RISC PCI controllers, in particular sparc64.
13991 *
13992 * On 5703/5704 chips, this bit has been reassigned
13993 * a different meaning. In particular, it is used
13994 * on those chips to enable a PCI-X workaround.
13995 */
13996 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
13997 }
13998
13999 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14000
14001#if 0
14002 /* Unneeded, already done by tg3_get_invariants. */
14003 tg3_switch_clocks(tp);
14004#endif
14005
Linus Torvalds1da177e2005-04-16 15:20:36 -070014006 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14007 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
14008 goto out;
14009
David S. Miller59e6b432005-05-18 22:50:10 -070014010 /* It is best to perform DMA test with maximum write burst size
14011 * to expose the 5700/5701 write DMA bug.
14012 */
14013 saved_dma_rwctrl = tp->dma_rwctrl;
14014 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14015 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14016
Linus Torvalds1da177e2005-04-16 15:20:36 -070014017 while (1) {
14018 u32 *p = buf, i;
14019
14020 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
14021 p[i] = i;
14022
14023 /* Send the buffer to the chip. */
14024 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
14025 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000014026 dev_err(&tp->pdev->dev,
14027 "%s: Buffer write failed. err = %d\n",
14028 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014029 break;
14030 }
14031
14032#if 0
14033 /* validate data reached card RAM correctly. */
14034 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14035 u32 val;
14036 tg3_read_mem(tp, 0x2100 + (i*4), &val);
14037 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000014038 dev_err(&tp->pdev->dev,
14039 "%s: Buffer corrupted on device! "
14040 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014041 /* ret = -ENODEV here? */
14042 }
14043 p[i] = 0;
14044 }
14045#endif
14046 /* Now read it back. */
14047 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
14048 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000014049 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
14050 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014051 break;
14052 }
14053
14054 /* Verify it. */
14055 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14056 if (p[i] == i)
14057 continue;
14058
David S. Miller59e6b432005-05-18 22:50:10 -070014059 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14060 DMA_RWCTRL_WRITE_BNDRY_16) {
14061 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014062 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
14063 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14064 break;
14065 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000014066 dev_err(&tp->pdev->dev,
14067 "%s: Buffer corrupted on read back! "
14068 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014069 ret = -ENODEV;
14070 goto out;
14071 }
14072 }
14073
14074 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
14075 /* Success. */
14076 ret = 0;
14077 break;
14078 }
14079 }
David S. Miller59e6b432005-05-18 22:50:10 -070014080 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14081 DMA_RWCTRL_WRITE_BNDRY_16) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014082 static struct pci_device_id dma_wait_state_chipsets[] = {
14083 { PCI_DEVICE(PCI_VENDOR_ID_APPLE,
14084 PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
14085 { },
14086 };
14087
David S. Miller59e6b432005-05-18 22:50:10 -070014088 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070014089 * now look for chipsets that are known to expose the
14090 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070014091 */
Michael Chan6d1cfba2005-06-08 14:13:14 -070014092 if (pci_dev_present(dma_wait_state_chipsets)) {
14093 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14094 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a588792010-04-05 10:19:28 +000014095 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014096 /* Safe to use the calculated DMA boundary. */
14097 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a588792010-04-05 10:19:28 +000014098 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070014099
David S. Miller59e6b432005-05-18 22:50:10 -070014100 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014102
14103out:
14104 pci_free_consistent(tp->pdev, TEST_BUFFER_SIZE, buf, buf_dma);
14105out_nofree:
14106 return ret;
14107}
14108
14109static void __devinit tg3_init_link_config(struct tg3 *tp)
14110{
14111 tp->link_config.advertising =
14112 (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
14113 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
14114 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full |
14115 ADVERTISED_Autoneg | ADVERTISED_MII);
14116 tp->link_config.speed = SPEED_INVALID;
14117 tp->link_config.duplex = DUPLEX_INVALID;
14118 tp->link_config.autoneg = AUTONEG_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014119 tp->link_config.active_speed = SPEED_INVALID;
14120 tp->link_config.active_duplex = DUPLEX_INVALID;
14121 tp->link_config.phy_is_low_power = 0;
14122 tp->link_config.orig_speed = SPEED_INVALID;
14123 tp->link_config.orig_duplex = DUPLEX_INVALID;
14124 tp->link_config.orig_autoneg = AUTONEG_INVALID;
14125}
14126
14127static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
14128{
Matt Carlsonc885e822010-08-02 11:25:57 +000014129 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
Matt Carlson666bc832010-01-20 16:58:03 +000014130 tp->bufmgr_config.mbuf_read_dma_low_water =
14131 DEFAULT_MB_RDMA_LOW_WATER_5705;
14132 tp->bufmgr_config.mbuf_mac_rx_low_water =
14133 DEFAULT_MB_MACRX_LOW_WATER_57765;
14134 tp->bufmgr_config.mbuf_high_water =
14135 DEFAULT_MB_HIGH_WATER_57765;
14136
14137 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14138 DEFAULT_MB_RDMA_LOW_WATER_5705;
14139 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14140 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
14141 tp->bufmgr_config.mbuf_high_water_jumbo =
14142 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
14143 } else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
Michael Chanfdfec1722005-07-25 12:31:48 -070014144 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_5705;
14148 tp->bufmgr_config.mbuf_high_water =
14149 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070014150 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
14151 tp->bufmgr_config.mbuf_mac_rx_low_water =
14152 DEFAULT_MB_MACRX_LOW_WATER_5906;
14153 tp->bufmgr_config.mbuf_high_water =
14154 DEFAULT_MB_HIGH_WATER_5906;
14155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014156
Michael Chanfdfec1722005-07-25 12:31:48 -070014157 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14158 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
14159 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14160 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
14161 tp->bufmgr_config.mbuf_high_water_jumbo =
14162 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
14163 } else {
14164 tp->bufmgr_config.mbuf_read_dma_low_water =
14165 DEFAULT_MB_RDMA_LOW_WATER;
14166 tp->bufmgr_config.mbuf_mac_rx_low_water =
14167 DEFAULT_MB_MACRX_LOW_WATER;
14168 tp->bufmgr_config.mbuf_high_water =
14169 DEFAULT_MB_HIGH_WATER;
14170
14171 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14172 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
14173 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14174 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
14175 tp->bufmgr_config.mbuf_high_water_jumbo =
14176 DEFAULT_MB_HIGH_WATER_JUMBO;
14177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014178
14179 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
14180 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
14181}
14182
14183static char * __devinit tg3_phy_string(struct tg3 *tp)
14184{
Matt Carlson79eb6902010-02-17 15:17:03 +000014185 switch (tp->phy_id & TG3_PHY_ID_MASK) {
14186 case TG3_PHY_ID_BCM5400: return "5400";
14187 case TG3_PHY_ID_BCM5401: return "5401";
14188 case TG3_PHY_ID_BCM5411: return "5411";
14189 case TG3_PHY_ID_BCM5701: return "5701";
14190 case TG3_PHY_ID_BCM5703: return "5703";
14191 case TG3_PHY_ID_BCM5704: return "5704";
14192 case TG3_PHY_ID_BCM5705: return "5705";
14193 case TG3_PHY_ID_BCM5750: return "5750";
14194 case TG3_PHY_ID_BCM5752: return "5752";
14195 case TG3_PHY_ID_BCM5714: return "5714";
14196 case TG3_PHY_ID_BCM5780: return "5780";
14197 case TG3_PHY_ID_BCM5755: return "5755";
14198 case TG3_PHY_ID_BCM5787: return "5787";
14199 case TG3_PHY_ID_BCM5784: return "5784";
14200 case TG3_PHY_ID_BCM5756: return "5722/5756";
14201 case TG3_PHY_ID_BCM5906: return "5906";
14202 case TG3_PHY_ID_BCM5761: return "5761";
14203 case TG3_PHY_ID_BCM5718C: return "5718C";
14204 case TG3_PHY_ID_BCM5718S: return "5718S";
14205 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000014206 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson79eb6902010-02-17 15:17:03 +000014207 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070014208 case 0: return "serdes";
14209 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070014210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014211}
14212
Michael Chanf9804dd2005-09-27 12:13:10 -070014213static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
14214{
14215 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
14216 strcpy(str, "PCI Express");
14217 return str;
14218 } else if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
14219 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
14220
14221 strcpy(str, "PCIX:");
14222
14223 if ((clock_ctrl == 7) ||
14224 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
14225 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
14226 strcat(str, "133MHz");
14227 else if (clock_ctrl == 0)
14228 strcat(str, "33MHz");
14229 else if (clock_ctrl == 2)
14230 strcat(str, "50MHz");
14231 else if (clock_ctrl == 4)
14232 strcat(str, "66MHz");
14233 else if (clock_ctrl == 6)
14234 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070014235 } else {
14236 strcpy(str, "PCI:");
14237 if (tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED)
14238 strcat(str, "66MHz");
14239 else
14240 strcat(str, "33MHz");
14241 }
14242 if (tp->tg3_flags & TG3_FLAG_PCI_32BIT)
14243 strcat(str, ":32-bit");
14244 else
14245 strcat(str, ":64-bit");
14246 return str;
14247}
14248
Michael Chan8c2dc7e2005-12-19 16:26:02 -080014249static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014250{
14251 struct pci_dev *peer;
14252 unsigned int func, devnr = tp->pdev->devfn & ~7;
14253
14254 for (func = 0; func < 8; func++) {
14255 peer = pci_get_slot(tp->pdev->bus, devnr | func);
14256 if (peer && peer != tp->pdev)
14257 break;
14258 pci_dev_put(peer);
14259 }
Michael Chan16fe9d72005-12-13 21:09:54 -080014260 /* 5704 can be configured in single-port mode, set peer to
14261 * tp->pdev in that case.
14262 */
14263 if (!peer) {
14264 peer = tp->pdev;
14265 return peer;
14266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014267
14268 /*
14269 * We don't need to keep the refcount elevated; there's no way
14270 * to remove one half of this device without removing the other
14271 */
14272 pci_dev_put(peer);
14273
14274 return peer;
14275}
14276
David S. Miller15f98502005-05-18 22:49:26 -070014277static void __devinit tg3_init_coal(struct tg3 *tp)
14278{
14279 struct ethtool_coalesce *ec = &tp->coal;
14280
14281 memset(ec, 0, sizeof(*ec));
14282 ec->cmd = ETHTOOL_GCOALESCE;
14283 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
14284 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
14285 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
14286 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
14287 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
14288 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
14289 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
14290 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
14291 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
14292
14293 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
14294 HOSTCC_MODE_CLRTICK_TXBD)) {
14295 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
14296 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
14297 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
14298 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
14299 }
Michael Chand244c892005-07-05 14:42:33 -070014300
14301 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
14302 ec->rx_coalesce_usecs_irq = 0;
14303 ec->tx_coalesce_usecs_irq = 0;
14304 ec->stats_block_coalesce_usecs = 0;
14305 }
David S. Miller15f98502005-05-18 22:49:26 -070014306}
14307
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080014308static const struct net_device_ops tg3_netdev_ops = {
14309 .ndo_open = tg3_open,
14310 .ndo_stop = tg3_close,
Stephen Hemminger00829822008-11-20 20:14:53 -080014311 .ndo_start_xmit = tg3_start_xmit,
Eric Dumazet511d2222010-07-07 20:44:24 +000014312 .ndo_get_stats64 = tg3_get_stats64,
Stephen Hemminger00829822008-11-20 20:14:53 -080014313 .ndo_validate_addr = eth_validate_addr,
14314 .ndo_set_multicast_list = tg3_set_rx_mode,
14315 .ndo_set_mac_address = tg3_set_mac_addr,
14316 .ndo_do_ioctl = tg3_ioctl,
14317 .ndo_tx_timeout = tg3_tx_timeout,
14318 .ndo_change_mtu = tg3_change_mtu,
14319#if TG3_VLAN_TAG_USED
14320 .ndo_vlan_rx_register = tg3_vlan_rx_register,
14321#endif
14322#ifdef CONFIG_NET_POLL_CONTROLLER
14323 .ndo_poll_controller = tg3_poll_controller,
14324#endif
14325};
14326
14327static const struct net_device_ops tg3_netdev_ops_dma_bug = {
14328 .ndo_open = tg3_open,
14329 .ndo_stop = tg3_close,
14330 .ndo_start_xmit = tg3_start_xmit_dma_bug,
Eric Dumazet511d2222010-07-07 20:44:24 +000014331 .ndo_get_stats64 = tg3_get_stats64,
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080014332 .ndo_validate_addr = eth_validate_addr,
14333 .ndo_set_multicast_list = tg3_set_rx_mode,
14334 .ndo_set_mac_address = tg3_set_mac_addr,
14335 .ndo_do_ioctl = tg3_ioctl,
14336 .ndo_tx_timeout = tg3_tx_timeout,
14337 .ndo_change_mtu = tg3_change_mtu,
14338#if TG3_VLAN_TAG_USED
14339 .ndo_vlan_rx_register = tg3_vlan_rx_register,
14340#endif
14341#ifdef CONFIG_NET_POLL_CONTROLLER
14342 .ndo_poll_controller = tg3_poll_controller,
14343#endif
14344};
14345
Linus Torvalds1da177e2005-04-16 15:20:36 -070014346static int __devinit tg3_init_one(struct pci_dev *pdev,
14347 const struct pci_device_id *ent)
14348{
Linus Torvalds1da177e2005-04-16 15:20:36 -070014349 struct net_device *dev;
14350 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000014351 int i, err, pm_cap;
14352 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070014353 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080014354 u64 dma_mask, persist_dma_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014355
Joe Perches05dbe002010-02-17 19:44:19 +000014356 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014357
14358 err = pci_enable_device(pdev);
14359 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014360 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014361 return err;
14362 }
14363
Linus Torvalds1da177e2005-04-16 15:20:36 -070014364 err = pci_request_regions(pdev, DRV_MODULE_NAME);
14365 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014366 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014367 goto err_out_disable_pdev;
14368 }
14369
14370 pci_set_master(pdev);
14371
14372 /* Find power-management capability. */
14373 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
14374 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000014375 dev_err(&pdev->dev,
14376 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014377 err = -EIO;
14378 goto err_out_free_res;
14379 }
14380
Matt Carlsonfe5f5782009-09-01 13:09:39 +000014381 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014382 if (!dev) {
Matt Carlson2445e462010-04-05 10:19:21 +000014383 dev_err(&pdev->dev, "Etherdev alloc failed, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014384 err = -ENOMEM;
14385 goto err_out_free_res;
14386 }
14387
Linus Torvalds1da177e2005-04-16 15:20:36 -070014388 SET_NETDEV_DEV(dev, &pdev->dev);
14389
Linus Torvalds1da177e2005-04-16 15:20:36 -070014390#if TG3_VLAN_TAG_USED
14391 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014392#endif
14393
14394 tp = netdev_priv(dev);
14395 tp->pdev = pdev;
14396 tp->dev = dev;
14397 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014398 tp->rx_mode = TG3_DEF_RX_MODE;
14399 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070014400
Linus Torvalds1da177e2005-04-16 15:20:36 -070014401 if (tg3_debug > 0)
14402 tp->msg_enable = tg3_debug;
14403 else
14404 tp->msg_enable = TG3_DEF_MSG_ENABLE;
14405
14406 /* The word/byte swap controls here control register access byte
14407 * swapping. DMA data byte swapping is controlled in the GRC_MODE
14408 * setting below.
14409 */
14410 tp->misc_host_ctrl =
14411 MISC_HOST_CTRL_MASK_PCI_INT |
14412 MISC_HOST_CTRL_WORD_SWAP |
14413 MISC_HOST_CTRL_INDIR_ACCESS |
14414 MISC_HOST_CTRL_PCISTATE_RW;
14415
14416 /* The NONFRM (non-frame) byte/word swap controls take effect
14417 * on descriptor entries, anything which isn't packet data.
14418 *
14419 * The StrongARM chips on the board (one for tx, one for rx)
14420 * are running in big-endian mode.
14421 */
14422 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
14423 GRC_MODE_WSWAP_NONFRM_DATA);
14424#ifdef __BIG_ENDIAN
14425 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
14426#endif
14427 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014428 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000014429 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014430
Matt Carlsond5fe4882008-11-21 17:20:32 -080014431 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010014432 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014433 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014434 err = -ENOMEM;
14435 goto err_out_free_dev;
14436 }
14437
14438 tg3_init_link_config(tp);
14439
Linus Torvalds1da177e2005-04-16 15:20:36 -070014440 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
14441 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014442
Linus Torvalds1da177e2005-04-16 15:20:36 -070014443 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014444 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014445 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014446
14447 err = tg3_get_invariants(tp);
14448 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014449 dev_err(&pdev->dev,
14450 "Problem fetching invariants of chip, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014451 goto err_out_iounmap;
14452 }
14453
Matt Carlson615774f2009-11-13 13:03:39 +000014454 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) &&
Matt Carlsona50d0792010-06-05 17:24:37 +000014455 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0 &&
14456 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5719)
Stephen Hemminger00829822008-11-20 20:14:53 -080014457 dev->netdev_ops = &tg3_netdev_ops;
14458 else
14459 dev->netdev_ops = &tg3_netdev_ops_dma_bug;
14460
14461
Michael Chan4a29cc22006-03-19 13:21:12 -080014462 /* The EPB bridge inside 5714, 5715, and 5780 and any
14463 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080014464 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
14465 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
14466 * do DMA address check in tg3_start_xmit().
14467 */
Michael Chan4a29cc22006-03-19 13:21:12 -080014468 if (tp->tg3_flags2 & TG3_FLG2_IS_5788)
Yang Hongyang284901a2009-04-06 19:01:15 -070014469 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Michael Chan4a29cc22006-03-19 13:21:12 -080014470 else if (tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070014471 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080014472#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070014473 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080014474#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080014475 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070014476 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080014477
14478 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070014479 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080014480 err = pci_set_dma_mask(pdev, dma_mask);
14481 if (!err) {
14482 dev->features |= NETIF_F_HIGHDMA;
14483 err = pci_set_consistent_dma_mask(pdev,
14484 persist_dma_mask);
14485 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014486 dev_err(&pdev->dev, "Unable to obtain 64 bit "
14487 "DMA for consistent allocations\n");
Michael Chan72f2afb2006-03-06 19:28:35 -080014488 goto err_out_iounmap;
14489 }
14490 }
14491 }
Yang Hongyang284901a2009-04-06 19:01:15 -070014492 if (err || dma_mask == DMA_BIT_MASK(32)) {
14493 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080014494 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014495 dev_err(&pdev->dev,
14496 "No usable DMA configuration, aborting\n");
Michael Chan72f2afb2006-03-06 19:28:35 -080014497 goto err_out_iounmap;
14498 }
14499 }
14500
Michael Chanfdfec1722005-07-25 12:31:48 -070014501 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014502
Matt Carlson507399f2009-11-13 13:03:37 +000014503 /* Selectively allow TSO based on operating conditions */
14504 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO) ||
14505 (tp->fw_needed && !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)))
14506 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
14507 else {
14508 tp->tg3_flags2 &= ~(TG3_FLG2_TSO_CAPABLE | TG3_FLG2_TSO_BUG);
14509 tp->fw_needed = NULL;
14510 }
14511
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014512 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
Matt Carlson9e9fd122009-01-19 16:57:45 -080014513 tp->fw_needed = FIRMWARE_TG3;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014514
Michael Chan4e3a7aa2006-03-20 17:47:44 -080014515 /* TSO is on by default on chips that support hardware TSO.
14516 * Firmware TSO on older chips gives lower performance, so it
14517 * is off by default, but can be enabled using ethtool.
14518 */
Matt Carlsone849cdc2009-11-13 13:03:38 +000014519 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO) &&
Eric Dumazet7fe876a2010-07-08 06:14:55 +000014520 (dev->features & NETIF_F_IP_CSUM)) {
Matt Carlsone849cdc2009-11-13 13:03:38 +000014521 dev->features |= NETIF_F_TSO;
Eric Dumazet7fe876a2010-07-08 06:14:55 +000014522 vlan_features_add(dev, NETIF_F_TSO);
14523 }
Matt Carlsone849cdc2009-11-13 13:03:38 +000014524 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_2) ||
14525 (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3)) {
Eric Dumazet7fe876a2010-07-08 06:14:55 +000014526 if (dev->features & NETIF_F_IPV6_CSUM) {
Michael Chanb0026622006-07-03 19:42:14 -070014527 dev->features |= NETIF_F_TSO6;
Eric Dumazet7fe876a2010-07-08 06:14:55 +000014528 vlan_features_add(dev, NETIF_F_TSO6);
14529 }
Matt Carlsone849cdc2009-11-13 13:03:38 +000014530 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
14531 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070014532 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
14533 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014534 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Eric Dumazet7fe876a2010-07-08 06:14:55 +000014535 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070014536 dev->features |= NETIF_F_TSO_ECN;
Eric Dumazet7fe876a2010-07-08 06:14:55 +000014537 vlan_features_add(dev, NETIF_F_TSO_ECN);
14538 }
Michael Chanb0026622006-07-03 19:42:14 -070014539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014540
Linus Torvalds1da177e2005-04-16 15:20:36 -070014541 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
14542 !(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) &&
14543 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
14544 tp->tg3_flags2 |= TG3_FLG2_MAX_RXPEND_64;
14545 tp->rx_pending = 63;
14546 }
14547
Linus Torvalds1da177e2005-04-16 15:20:36 -070014548 err = tg3_get_device_address(tp);
14549 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014550 dev_err(&pdev->dev,
14551 "Could not obtain valid ethernet address, aborting\n");
Matt Carlson026a6c22009-12-03 08:36:24 +000014552 goto err_out_iounmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014553 }
14554
Matt Carlson0d3031d2007-10-10 18:02:43 -070014555 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
Matt Carlson63532392008-11-03 16:49:57 -080014556 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
Al Viro79ea13c2008-01-24 02:06:46 -080014557 if (!tp->aperegs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014558 dev_err(&pdev->dev,
14559 "Cannot map APE registers, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070014560 err = -ENOMEM;
Matt Carlson026a6c22009-12-03 08:36:24 +000014561 goto err_out_iounmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070014562 }
14563
14564 tg3_ape_lock_init(tp);
Matt Carlson7fd76442009-02-25 14:27:20 +000014565
14566 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF)
14567 tg3_read_dash_ver(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070014568 }
14569
Matt Carlsonc88864d2007-11-12 21:07:01 -080014570 /*
14571 * Reset chip in case UNDI or EFI driver did not shutdown
14572 * DMA self test will enable WDMAC and we'll see (spurious)
14573 * pending DMA on the PCI bus at that point.
14574 */
14575 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
14576 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
14577 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
14578 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
14579 }
14580
14581 err = tg3_test_dma(tp);
14582 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014583 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080014584 goto err_out_apeunmap;
14585 }
14586
Matt Carlsonc88864d2007-11-12 21:07:01 -080014587 /* flow control autonegotiation is default behavior */
14588 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
Steve Glendinninge18ce342008-12-16 02:00:00 -080014589 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlsonc88864d2007-11-12 21:07:01 -080014590
Matt Carlson78f90dc2009-11-13 13:03:42 +000014591 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
14592 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
14593 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
14594 for (i = 0; i < TG3_IRQ_MAX_VECS; i++) {
14595 struct tg3_napi *tnapi = &tp->napi[i];
14596
14597 tnapi->tp = tp;
14598 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
14599
14600 tnapi->int_mbox = intmbx;
14601 if (i < 4)
14602 intmbx += 0x8;
14603 else
14604 intmbx += 0x4;
14605
14606 tnapi->consmbox = rcvmbx;
14607 tnapi->prodmbox = sndmbx;
14608
14609 if (i) {
14610 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
14611 netif_napi_add(dev, &tnapi->napi, tg3_poll_msix, 64);
14612 } else {
14613 tnapi->coal_now = HOSTCC_MODE_NOW;
14614 netif_napi_add(dev, &tnapi->napi, tg3_poll, 64);
14615 }
14616
14617 if (!(tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX))
14618 break;
14619
14620 /*
14621 * If we support MSIX, we'll be using RSS. If we're using
14622 * RSS, the first vector only handles link interrupts and the
14623 * remaining vectors handle rx and tx interrupts. Reuse the
14624 * mailbox values for the next iteration. The values we setup
14625 * above are still useful for the single vectored mode.
14626 */
14627 if (!i)
14628 continue;
14629
14630 rcvmbx += 0x8;
14631
14632 if (sndmbx & 0x4)
14633 sndmbx -= 0x4;
14634 else
14635 sndmbx += 0xc;
14636 }
14637
Matt Carlsonc88864d2007-11-12 21:07:01 -080014638 tg3_init_coal(tp);
14639
Michael Chanc49a1562006-12-17 17:07:29 -080014640 pci_set_drvdata(pdev, dev);
14641
Linus Torvalds1da177e2005-04-16 15:20:36 -070014642 err = register_netdev(dev);
14643 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014644 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070014645 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014646 }
14647
Joe Perches05dbe002010-02-17 19:44:19 +000014648 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
14649 tp->board_part_number,
14650 tp->pci_chip_rev_id,
14651 tg3_bus_string(tp, str),
14652 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014653
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000014654 if (tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED) {
14655 struct phy_device *phydev;
14656 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000014657 netdev_info(dev,
14658 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000014659 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000014660 } else
Matt Carlson5129c3a2010-04-05 10:19:23 +000014661 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
14662 "(WireSpeed[%d])\n", tg3_phy_string(tp),
Joe Perches05dbe002010-02-17 19:44:19 +000014663 ((tp->tg3_flags & TG3_FLAG_10_100_ONLY) ? "10/100Base-TX" :
14664 ((tp->tg3_flags2 & TG3_FLG2_ANY_SERDES) ? "1000Base-SX" :
14665 "10/100/1000Base-T")),
14666 (tp->tg3_flags2 & TG3_FLG2_NO_ETH_WIRE_SPEED) == 0);
Matt Carlsondf59c942008-11-03 16:52:56 -080014667
Joe Perches05dbe002010-02-17 19:44:19 +000014668 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
14669 (tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) != 0,
14670 (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) != 0,
14671 (tp->tg3_flags & TG3_FLAG_USE_MI_INTERRUPT) != 0,
14672 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0,
14673 (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) != 0);
14674 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
14675 tp->dma_rwctrl,
14676 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
14677 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014678
14679 return 0;
14680
Matt Carlson0d3031d2007-10-10 18:02:43 -070014681err_out_apeunmap:
14682 if (tp->aperegs) {
14683 iounmap(tp->aperegs);
14684 tp->aperegs = NULL;
14685 }
14686
Linus Torvalds1da177e2005-04-16 15:20:36 -070014687err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070014688 if (tp->regs) {
14689 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070014690 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070014691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014692
14693err_out_free_dev:
14694 free_netdev(dev);
14695
14696err_out_free_res:
14697 pci_release_regions(pdev);
14698
14699err_out_disable_pdev:
14700 pci_disable_device(pdev);
14701 pci_set_drvdata(pdev, NULL);
14702 return err;
14703}
14704
14705static void __devexit tg3_remove_one(struct pci_dev *pdev)
14706{
14707 struct net_device *dev = pci_get_drvdata(pdev);
14708
14709 if (dev) {
14710 struct tg3 *tp = netdev_priv(dev);
14711
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014712 if (tp->fw)
14713 release_firmware(tp->fw);
14714
Michael Chan7faa0062006-02-02 17:29:28 -080014715 flush_scheduled_work();
Matt Carlson158d7ab2008-05-29 01:37:54 -070014716
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014717 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
14718 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070014719 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014720 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070014721
Linus Torvalds1da177e2005-04-16 15:20:36 -070014722 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070014723 if (tp->aperegs) {
14724 iounmap(tp->aperegs);
14725 tp->aperegs = NULL;
14726 }
Michael Chan68929142005-08-09 20:17:14 -070014727 if (tp->regs) {
14728 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070014729 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070014730 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014731 free_netdev(dev);
14732 pci_release_regions(pdev);
14733 pci_disable_device(pdev);
14734 pci_set_drvdata(pdev, NULL);
14735 }
14736}
14737
14738static int tg3_suspend(struct pci_dev *pdev, pm_message_t state)
14739{
14740 struct net_device *dev = pci_get_drvdata(pdev);
14741 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070014742 pci_power_t target_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014743 int err;
14744
Michael Chan3e0c95f2007-08-03 20:56:54 -070014745 /* PCI register 4 needs to be saved whether netif_running() or not.
14746 * MSI address and data need to be saved if using MSI and
14747 * netif_running().
14748 */
14749 pci_save_state(pdev);
14750
Linus Torvalds1da177e2005-04-16 15:20:36 -070014751 if (!netif_running(dev))
14752 return 0;
14753
Michael Chan7faa0062006-02-02 17:29:28 -080014754 flush_scheduled_work();
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014755 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014756 tg3_netif_stop(tp);
14757
14758 del_timer_sync(&tp->timer);
14759
David S. Millerf47c11e2005-06-24 20:18:35 -070014760 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014761 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070014762 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014763
14764 netif_device_detach(dev);
14765
David S. Millerf47c11e2005-06-24 20:18:35 -070014766 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070014767 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan6a9eba12005-12-13 21:08:58 -080014768 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
David S. Millerf47c11e2005-06-24 20:18:35 -070014769 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014770
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070014771 target_state = pdev->pm_cap ? pci_target_state(pdev) : PCI_D3hot;
14772
14773 err = tg3_set_power_state(tp, target_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014774 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014775 int err2;
14776
David S. Millerf47c11e2005-06-24 20:18:35 -070014777 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014778
Michael Chan6a9eba12005-12-13 21:08:58 -080014779 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014780 err2 = tg3_restart_hw(tp, 1);
14781 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070014782 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014783
14784 tp->timer.expires = jiffies + tp->timer_offset;
14785 add_timer(&tp->timer);
14786
14787 netif_device_attach(dev);
14788 tg3_netif_start(tp);
14789
Michael Chanb9ec6c12006-07-25 16:37:27 -070014790out:
David S. Millerf47c11e2005-06-24 20:18:35 -070014791 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014792
14793 if (!err2)
14794 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014795 }
14796
14797 return err;
14798}
14799
14800static int tg3_resume(struct pci_dev *pdev)
14801{
14802 struct net_device *dev = pci_get_drvdata(pdev);
14803 struct tg3 *tp = netdev_priv(dev);
14804 int err;
14805
Michael Chan3e0c95f2007-08-03 20:56:54 -070014806 pci_restore_state(tp->pdev);
14807
Linus Torvalds1da177e2005-04-16 15:20:36 -070014808 if (!netif_running(dev))
14809 return 0;
14810
Michael Chanbc1c7562006-03-20 17:48:03 -080014811 err = tg3_set_power_state(tp, PCI_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014812 if (err)
14813 return err;
14814
14815 netif_device_attach(dev);
14816
David S. Millerf47c11e2005-06-24 20:18:35 -070014817 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014818
Michael Chan6a9eba12005-12-13 21:08:58 -080014819 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Michael Chanb9ec6c12006-07-25 16:37:27 -070014820 err = tg3_restart_hw(tp, 1);
14821 if (err)
14822 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014823
14824 tp->timer.expires = jiffies + tp->timer_offset;
14825 add_timer(&tp->timer);
14826
Linus Torvalds1da177e2005-04-16 15:20:36 -070014827 tg3_netif_start(tp);
14828
Michael Chanb9ec6c12006-07-25 16:37:27 -070014829out:
David S. Millerf47c11e2005-06-24 20:18:35 -070014830 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014831
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014832 if (!err)
14833 tg3_phy_start(tp);
14834
Michael Chanb9ec6c12006-07-25 16:37:27 -070014835 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014836}
14837
14838static struct pci_driver tg3_driver = {
14839 .name = DRV_MODULE_NAME,
14840 .id_table = tg3_pci_tbl,
14841 .probe = tg3_init_one,
14842 .remove = __devexit_p(tg3_remove_one),
14843 .suspend = tg3_suspend,
14844 .resume = tg3_resume
14845};
14846
14847static int __init tg3_init(void)
14848{
Jeff Garzik29917622006-08-19 17:48:59 -040014849 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014850}
14851
14852static void __exit tg3_cleanup(void)
14853{
14854 pci_unregister_driver(&tg3_driver);
14855}
14856
14857module_init(tg3_init);
14858module_exit(tg3_cleanup);