blob: fdb438dca9b3a96e5376492fec61bb88f3061555 [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 Carlson9ed6eda2010-08-02 11:26:08 +000072#define TG3_MIN_NUM 113
Matt Carlson6867c842010-07-11 09:31:44 +000073#define DRV_MODULE_VERSION \
74 __stringify(TG3_MAJ_NUM) "." __stringify(TG3_MIN_NUM)
Matt Carlson9ed6eda2010-08-02 11:26:08 +000075#define DRV_MODULE_RELDATE "August 2, 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 Carlsonf07e9af2010-08-02 11:26:07 +0000882 if ((tp->phy_flags & TG3_PHYFLG_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 Carlsonf07e9af2010-08-02 11:26:07 +00001178 tp->phy_flags |= TG3_PHYFLG_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;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001271 if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
Matt Carlson95e28692008-05-25 23:44:14 -07001272 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)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001379 if (tp->phy_flags & TG3_PHYFLG_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
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001493 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001494 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 Carlsonf07e9af2010-08-02 11:26:07 +00001513 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Matt Carlson321d32a2008-11-21 17:22:19 -08001514 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
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001530 tp->phy_flags |= TG3_PHYFLG_IS_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
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001541 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001542 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
Matt Carlson800960682010-08-02 11:26:06 +00001546 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
1547 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001548 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{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001561 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001562 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{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001569 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001570 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001571 tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001572 }
1573}
1574
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00001575static int tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001576{
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00001577 int err;
1578
1579 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1580 if (!err)
1581 err = tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
1582
1583 return err;
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001584}
1585
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001586static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
1587{
1588 u32 phytest;
1589
1590 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
1591 u32 phy;
1592
1593 tg3_writephy(tp, MII_TG3_FET_TEST,
1594 phytest | MII_TG3_FET_SHADOW_EN);
1595 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
1596 if (enable)
1597 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
1598 else
1599 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
1600 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
1601 }
1602 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
1603 }
1604}
1605
Matt Carlson6833c042008-11-21 17:18:59 -08001606static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
1607{
1608 u32 reg;
1609
Matt Carlsonecf14102010-01-20 16:58:05 +00001610 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
Matt Carlsona50d0792010-06-05 17:24:37 +00001611 ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
1612 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001613 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08001614 return;
1615
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001616 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001617 tg3_phy_fet_toggle_apd(tp, enable);
1618 return;
1619 }
1620
Matt Carlson6833c042008-11-21 17:18:59 -08001621 reg = MII_TG3_MISC_SHDW_WREN |
1622 MII_TG3_MISC_SHDW_SCR5_SEL |
1623 MII_TG3_MISC_SHDW_SCR5_LPED |
1624 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
1625 MII_TG3_MISC_SHDW_SCR5_SDTL |
1626 MII_TG3_MISC_SHDW_SCR5_C125OE;
1627 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 || !enable)
1628 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
1629
1630 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1631
1632
1633 reg = MII_TG3_MISC_SHDW_WREN |
1634 MII_TG3_MISC_SHDW_APD_SEL |
1635 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
1636 if (enable)
1637 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
1638
1639 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1640}
1641
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001642static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
1643{
1644 u32 phy;
1645
1646 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001647 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001648 return;
1649
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001650 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001651 u32 ephy;
1652
Matt Carlson535ef6e2009-08-25 10:09:36 +00001653 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
1654 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
1655
1656 tg3_writephy(tp, MII_TG3_FET_TEST,
1657 ephy | MII_TG3_FET_SHADOW_EN);
1658 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001659 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00001660 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001661 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00001662 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
1663 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001664 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00001665 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001666 }
1667 } else {
1668 phy = MII_TG3_AUXCTL_MISC_RDSEL_MISC |
1669 MII_TG3_AUXCTL_SHDWSEL_MISC;
1670 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, phy) &&
1671 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &phy)) {
1672 if (enable)
1673 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1674 else
1675 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1676 phy |= MII_TG3_AUXCTL_MISC_WREN;
1677 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1678 }
1679 }
1680}
1681
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682static void tg3_phy_set_wirespeed(struct tg3 *tp)
1683{
1684 u32 val;
1685
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001686 if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 return;
1688
1689 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x7007) &&
1690 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &val))
1691 tg3_writephy(tp, MII_TG3_AUX_CTRL,
1692 (val | (1 << 15) | (1 << 4)));
1693}
1694
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001695static void tg3_phy_apply_otp(struct tg3 *tp)
1696{
1697 u32 otp, phy;
1698
1699 if (!tp->phy_otp)
1700 return;
1701
1702 otp = tp->phy_otp;
1703
1704 /* Enable SM_DSP clock and tx 6dB coding. */
1705 phy = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1706 MII_TG3_AUXCTL_ACTL_SMDSP_ENA |
1707 MII_TG3_AUXCTL_ACTL_TX_6DB;
1708 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1709
1710 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
1711 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
1712 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
1713
1714 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
1715 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
1716 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
1717
1718 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
1719 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
1720 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
1721
1722 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
1723 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
1724
1725 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
1726 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
1727
1728 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
1729 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
1730 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
1731
1732 /* Turn off SM_DSP clock. */
1733 phy = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1734 MII_TG3_AUXCTL_ACTL_TX_6DB;
1735 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1736}
1737
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738static int tg3_wait_macro_done(struct tg3 *tp)
1739{
1740 int limit = 100;
1741
1742 while (limit--) {
1743 u32 tmp32;
1744
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001745 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 if ((tmp32 & 0x1000) == 0)
1747 break;
1748 }
1749 }
Roel Kluind4675b52009-02-12 16:33:27 -08001750 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 return -EBUSY;
1752
1753 return 0;
1754}
1755
1756static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
1757{
1758 static const u32 test_pat[4][6] = {
1759 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
1760 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
1761 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
1762 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
1763 };
1764 int chan;
1765
1766 for (chan = 0; chan < 4; chan++) {
1767 int i;
1768
1769 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1770 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001771 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
1773 for (i = 0; i < 6; i++)
1774 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
1775 test_pat[chan][i]);
1776
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001777 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 if (tg3_wait_macro_done(tp)) {
1779 *resetp = 1;
1780 return -EBUSY;
1781 }
1782
1783 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1784 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001785 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 if (tg3_wait_macro_done(tp)) {
1787 *resetp = 1;
1788 return -EBUSY;
1789 }
1790
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001791 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 if (tg3_wait_macro_done(tp)) {
1793 *resetp = 1;
1794 return -EBUSY;
1795 }
1796
1797 for (i = 0; i < 6; i += 2) {
1798 u32 low, high;
1799
1800 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
1801 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
1802 tg3_wait_macro_done(tp)) {
1803 *resetp = 1;
1804 return -EBUSY;
1805 }
1806 low &= 0x7fff;
1807 high &= 0x000f;
1808 if (low != test_pat[chan][i] ||
1809 high != test_pat[chan][i+1]) {
1810 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
1811 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
1812 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
1813
1814 return -EBUSY;
1815 }
1816 }
1817 }
1818
1819 return 0;
1820}
1821
1822static int tg3_phy_reset_chanpat(struct tg3 *tp)
1823{
1824 int chan;
1825
1826 for (chan = 0; chan < 4; chan++) {
1827 int i;
1828
1829 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1830 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001831 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 for (i = 0; i < 6; i++)
1833 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001834 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 if (tg3_wait_macro_done(tp))
1836 return -EBUSY;
1837 }
1838
1839 return 0;
1840}
1841
1842static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
1843{
1844 u32 reg32, phy9_orig;
1845 int retries, do_phy_reset, err;
1846
1847 retries = 10;
1848 do_phy_reset = 1;
1849 do {
1850 if (do_phy_reset) {
1851 err = tg3_bmcr_reset(tp);
1852 if (err)
1853 return err;
1854 do_phy_reset = 0;
1855 }
1856
1857 /* Disable transmitter and interrupt. */
1858 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
1859 continue;
1860
1861 reg32 |= 0x3000;
1862 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
1863
1864 /* Set full-duplex, 1000 mbps. */
1865 tg3_writephy(tp, MII_BMCR,
1866 BMCR_FULLDPLX | TG3_BMCR_SPEED1000);
1867
1868 /* Set to master mode. */
1869 if (tg3_readphy(tp, MII_TG3_CTRL, &phy9_orig))
1870 continue;
1871
1872 tg3_writephy(tp, MII_TG3_CTRL,
1873 (MII_TG3_CTRL_AS_MASTER |
1874 MII_TG3_CTRL_ENABLE_AS_MASTER));
1875
1876 /* Enable SM_DSP_CLOCK and 6dB. */
1877 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
1878
1879 /* Block the PHY control access. */
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00001880 tg3_phydsp_write(tp, 0x8005, 0x0800);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
1882 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
1883 if (!err)
1884 break;
1885 } while (--retries);
1886
1887 err = tg3_phy_reset_chanpat(tp);
1888 if (err)
1889 return err;
1890
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00001891 tg3_phydsp_write(tp, 0x8005, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
1893 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001894 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
1896 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
1897 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
1898 /* Set Extended packet length bit for jumbo frames */
1899 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4400);
Matt Carlson859a588792010-04-05 10:19:28 +00001900 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
1902 }
1903
1904 tg3_writephy(tp, MII_TG3_CTRL, phy9_orig);
1905
1906 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
1907 reg32 &= ~0x3000;
1908 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
1909 } else if (!err)
1910 err = -EBUSY;
1911
1912 return err;
1913}
1914
1915/* This will reset the tigon3 PHY if there is no valid
1916 * link unless the FORCE argument is non-zero.
1917 */
1918static int tg3_phy_reset(struct tg3 *tp)
1919{
Matt Carlsonf833c4c2010-09-15 09:00:01 +00001920 u32 val, cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 int err;
1922
Michael Chan60189dd2006-12-17 17:08:07 -08001923 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08001924 val = tr32(GRC_MISC_CFG);
1925 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
1926 udelay(40);
1927 }
Matt Carlsonf833c4c2010-09-15 09:00:01 +00001928 err = tg3_readphy(tp, MII_BMSR, &val);
1929 err |= tg3_readphy(tp, MII_BMSR, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 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) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00001961 val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
1962 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001963
1964 tw32(TG3_CPMU_CTRL, cpmuctrl);
1965 }
1966
Matt Carlsonbcb37f62008-11-03 16:52:09 -08001967 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
1968 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08001969 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
1970 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
1971 CPMU_LSPD_1000MB_MACCLK_12_5) {
1972 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
1973 udelay(40);
1974 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
1975 }
1976 }
1977
Matt Carlsona50d0792010-06-05 17:24:37 +00001978 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
1979 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001980 (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
Matt Carlsonecf14102010-01-20 16:58:05 +00001981 return 0;
1982
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001983 tg3_phy_apply_otp(tp);
1984
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001985 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -08001986 tg3_phy_toggle_apd(tp, true);
1987 else
1988 tg3_phy_toggle_apd(tp, false);
1989
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990out:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001991 if (tp->phy_flags & TG3_PHYFLG_ADC_BUG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00001993 tg3_phydsp_write(tp, 0x201f, 0x2aaa);
1994 tg3_phydsp_write(tp, 0x000a, 0x0323);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
1996 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001997 if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001998 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
1999 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002001 if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002003 tg3_phydsp_write(tp, 0x000a, 0x310b);
2004 tg3_phydsp_write(tp, 0x201f, 0x9506);
2005 tg3_phydsp_write(tp, 0x401f, 0x14e2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002007 } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
Michael Chanc424cb22006-04-29 18:56:34 -07002008 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
2009 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002010 if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
Michael Chanc1d2a192007-01-08 19:57:20 -08002011 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2012 tg3_writephy(tp, MII_TG3_TEST1,
2013 MII_TG3_TEST1_TRIM_EN | 0x4);
2014 } else
2015 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
Michael Chanc424cb22006-04-29 18:56:34 -07002016 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
2017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 /* Set Extended packet length bit (bit 14) on all chips that */
2019 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002020 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 /* Cannot do read-modify-write on 5401 */
2022 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4c20);
Matt Carlson8f666b02009-08-28 13:58:24 +00002023 } else if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 /* Set bit 14 with read-modify-write to preserve other bits */
2025 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0007) &&
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002026 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &val))
2027 tg3_writephy(tp, MII_TG3_AUX_CTRL, val | 0x4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 }
2029
2030 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2031 * jumbo frames transmission.
2032 */
Matt Carlson8f666b02009-08-28 13:58:24 +00002033 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002034 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002035 tg3_writephy(tp, MII_TG3_EXT_CTRL,
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002036 val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 }
2038
Michael Chan715116a2006-09-27 16:09:25 -07002039 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002040 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002041 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002042 }
2043
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002044 tg3_phy_toggle_automdix(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 tg3_phy_set_wirespeed(tp);
2046 return 0;
2047}
2048
2049static void tg3_frob_aux_power(struct tg3 *tp)
2050{
2051 struct tg3 *tp_peer = tp;
2052
Matt Carlson334355a2010-01-20 16:58:10 +00002053 /* The GPIOs do something completely different on 57765. */
2054 if ((tp->tg3_flags2 & TG3_FLG2_IS_NIC) == 0 ||
Matt Carlsona50d0792010-06-05 17:24:37 +00002055 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlson334355a2010-01-20 16:58:10 +00002056 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 return;
2058
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00002059 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2060 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
2061 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002062 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002064 dev_peer = pci_get_drvdata(tp->pdev_peer);
Michael Chanbc1c7562006-03-20 17:48:03 -08002065 /* remove_one() may have been run on the peer. */
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002066 if (!dev_peer)
Michael Chanbc1c7562006-03-20 17:48:03 -08002067 tp_peer = tp;
2068 else
2069 tp_peer = netdev_priv(dev_peer);
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
2072 if ((tp->tg3_flags & TG3_FLAG_WOL_ENABLE) != 0 ||
Michael Chan6921d202005-12-13 21:15:53 -08002073 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0 ||
2074 (tp_peer->tg3_flags & TG3_FLAG_WOL_ENABLE) != 0 ||
2075 (tp_peer->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2077 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Michael Chanb401e9e2005-12-19 16:27:04 -08002078 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2079 (GRC_LCLCTRL_GPIO_OE0 |
2080 GRC_LCLCTRL_GPIO_OE1 |
2081 GRC_LCLCTRL_GPIO_OE2 |
2082 GRC_LCLCTRL_GPIO_OUTPUT0 |
2083 GRC_LCLCTRL_GPIO_OUTPUT1),
2084 100);
Matt Carlson8d519ab2009-04-20 06:58:01 +00002085 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2086 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -07002087 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2088 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2089 GRC_LCLCTRL_GPIO_OE1 |
2090 GRC_LCLCTRL_GPIO_OE2 |
2091 GRC_LCLCTRL_GPIO_OUTPUT0 |
2092 GRC_LCLCTRL_GPIO_OUTPUT1 |
2093 tp->grc_local_ctrl;
2094 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2095
2096 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2097 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2098
2099 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2100 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 } else {
2102 u32 no_gpio2;
Michael Chandc56b7d2005-12-19 16:26:28 -08002103 u32 grc_local_ctrl = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
2105 if (tp_peer != tp &&
2106 (tp_peer->tg3_flags & TG3_FLAG_INIT_COMPLETE) != 0)
2107 return;
2108
Michael Chandc56b7d2005-12-19 16:26:28 -08002109 /* Workaround to prevent overdrawing Amps. */
2110 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2111 ASIC_REV_5714) {
2112 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chanb401e9e2005-12-19 16:27:04 -08002113 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2114 grc_local_ctrl, 100);
Michael Chandc56b7d2005-12-19 16:26:28 -08002115 }
2116
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 /* On 5753 and variants, GPIO2 cannot be used. */
2118 no_gpio2 = tp->nic_sram_data_cfg &
2119 NIC_SRAM_DATA_CFG_NO_GPIO2;
2120
Michael Chandc56b7d2005-12-19 16:26:28 -08002121 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 GRC_LCLCTRL_GPIO_OE1 |
2123 GRC_LCLCTRL_GPIO_OE2 |
2124 GRC_LCLCTRL_GPIO_OUTPUT1 |
2125 GRC_LCLCTRL_GPIO_OUTPUT2;
2126 if (no_gpio2) {
2127 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2128 GRC_LCLCTRL_GPIO_OUTPUT2);
2129 }
Michael Chanb401e9e2005-12-19 16:27:04 -08002130 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2131 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
2133 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2134
Michael Chanb401e9e2005-12-19 16:27:04 -08002135 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2136 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
2138 if (!no_gpio2) {
2139 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chanb401e9e2005-12-19 16:27:04 -08002140 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2141 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 }
2143 }
2144 } else {
2145 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
2146 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
2147 if (tp_peer != tp &&
2148 (tp_peer->tg3_flags & TG3_FLAG_INIT_COMPLETE) != 0)
2149 return;
2150
Michael Chanb401e9e2005-12-19 16:27:04 -08002151 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2152 (GRC_LCLCTRL_GPIO_OE1 |
2153 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
Michael Chanb401e9e2005-12-19 16:27:04 -08002155 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2156 GRC_LCLCTRL_GPIO_OE1, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
Michael Chanb401e9e2005-12-19 16:27:04 -08002158 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2159 (GRC_LCLCTRL_GPIO_OE1 |
2160 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 }
2162 }
2163}
2164
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002165static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2166{
2167 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2168 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00002169 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002170 if (speed != SPEED_10)
2171 return 1;
2172 } else if (speed == SPEED_10)
2173 return 1;
2174
2175 return 0;
2176}
2177
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178static int tg3_setup_phy(struct tg3 *, int);
2179
2180#define RESET_KIND_SHUTDOWN 0
2181#define RESET_KIND_INIT 1
2182#define RESET_KIND_SUSPEND 2
2183
2184static void tg3_write_sig_post_reset(struct tg3 *, int);
2185static int tg3_halt_cpu(struct tg3 *, u32);
2186
Matt Carlson0a459aa2008-11-03 16:54:15 -08002187static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002188{
Matt Carlsonce057f02007-11-12 21:08:03 -08002189 u32 val;
2190
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002191 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Michael Chan51297242007-02-13 12:17:57 -08002192 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2193 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2194 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2195
2196 sg_dig_ctrl |=
2197 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2198 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2199 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2200 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002201 return;
Michael Chan51297242007-02-13 12:17:57 -08002202 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002203
Michael Chan60189dd2006-12-17 17:08:07 -08002204 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002205 tg3_bmcr_reset(tp);
2206 val = tr32(GRC_MISC_CFG);
2207 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2208 udelay(40);
2209 return;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002210 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson0e5f7842009-11-02 14:26:38 +00002211 u32 phytest;
2212 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2213 u32 phy;
2214
2215 tg3_writephy(tp, MII_ADVERTISE, 0);
2216 tg3_writephy(tp, MII_BMCR,
2217 BMCR_ANENABLE | BMCR_ANRESTART);
2218
2219 tg3_writephy(tp, MII_TG3_FET_TEST,
2220 phytest | MII_TG3_FET_SHADOW_EN);
2221 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
2222 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
2223 tg3_writephy(tp,
2224 MII_TG3_FET_SHDW_AUXMODE4,
2225 phy);
2226 }
2227 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2228 }
2229 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002230 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07002231 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2232 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002233
2234 tg3_writephy(tp, MII_TG3_AUX_CTRL,
2235 MII_TG3_AUXCTL_SHDWSEL_PWRCTL |
2236 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2237 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2238 MII_TG3_AUXCTL_PCTL_VREG_11V);
Michael Chan715116a2006-09-27 16:09:25 -07002239 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002240
Michael Chan15c3b692006-03-22 01:06:52 -08002241 /* The PHY should not be powered down on some chips because
2242 * of bugs.
2243 */
2244 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2245 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2246 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002247 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Michael Chan15c3b692006-03-22 01:06:52 -08002248 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08002249
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002250 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2251 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002252 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2253 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2254 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2255 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2256 }
2257
Michael Chan15c3b692006-03-22 01:06:52 -08002258 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2259}
2260
Matt Carlson3f007892008-11-03 16:51:36 -08002261/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002262static int tg3_nvram_lock(struct tg3 *tp)
2263{
2264 if (tp->tg3_flags & TG3_FLAG_NVRAM) {
2265 int i;
2266
2267 if (tp->nvram_lock_cnt == 0) {
2268 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2269 for (i = 0; i < 8000; i++) {
2270 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2271 break;
2272 udelay(20);
2273 }
2274 if (i == 8000) {
2275 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2276 return -ENODEV;
2277 }
2278 }
2279 tp->nvram_lock_cnt++;
2280 }
2281 return 0;
2282}
2283
2284/* tp->lock is held. */
2285static void tg3_nvram_unlock(struct tg3 *tp)
2286{
2287 if (tp->tg3_flags & TG3_FLAG_NVRAM) {
2288 if (tp->nvram_lock_cnt > 0)
2289 tp->nvram_lock_cnt--;
2290 if (tp->nvram_lock_cnt == 0)
2291 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
2292 }
2293}
2294
2295/* tp->lock is held. */
2296static void tg3_enable_nvram_access(struct tg3 *tp)
2297{
2298 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Matt Carlsonf66a29b2009-11-13 13:03:36 +00002299 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002300 u32 nvaccess = tr32(NVRAM_ACCESS);
2301
2302 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
2303 }
2304}
2305
2306/* tp->lock is held. */
2307static void tg3_disable_nvram_access(struct tg3 *tp)
2308{
2309 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Matt Carlsonf66a29b2009-11-13 13:03:36 +00002310 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002311 u32 nvaccess = tr32(NVRAM_ACCESS);
2312
2313 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
2314 }
2315}
2316
2317static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
2318 u32 offset, u32 *val)
2319{
2320 u32 tmp;
2321 int i;
2322
2323 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
2324 return -EINVAL;
2325
2326 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
2327 EEPROM_ADDR_DEVID_MASK |
2328 EEPROM_ADDR_READ);
2329 tw32(GRC_EEPROM_ADDR,
2330 tmp |
2331 (0 << EEPROM_ADDR_DEVID_SHIFT) |
2332 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
2333 EEPROM_ADDR_ADDR_MASK) |
2334 EEPROM_ADDR_READ | EEPROM_ADDR_START);
2335
2336 for (i = 0; i < 1000; i++) {
2337 tmp = tr32(GRC_EEPROM_ADDR);
2338
2339 if (tmp & EEPROM_ADDR_COMPLETE)
2340 break;
2341 msleep(1);
2342 }
2343 if (!(tmp & EEPROM_ADDR_COMPLETE))
2344 return -EBUSY;
2345
Matt Carlson62cedd12009-04-20 14:52:29 -07002346 tmp = tr32(GRC_EEPROM_DATA);
2347
2348 /*
2349 * The data will always be opposite the native endian
2350 * format. Perform a blind byteswap to compensate.
2351 */
2352 *val = swab32(tmp);
2353
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002354 return 0;
2355}
2356
2357#define NVRAM_CMD_TIMEOUT 10000
2358
2359static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
2360{
2361 int i;
2362
2363 tw32(NVRAM_CMD, nvram_cmd);
2364 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
2365 udelay(10);
2366 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
2367 udelay(10);
2368 break;
2369 }
2370 }
2371
2372 if (i == NVRAM_CMD_TIMEOUT)
2373 return -EBUSY;
2374
2375 return 0;
2376}
2377
2378static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
2379{
2380 if ((tp->tg3_flags & TG3_FLAG_NVRAM) &&
2381 (tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) &&
2382 (tp->tg3_flags2 & TG3_FLG2_FLASH) &&
2383 !(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM_ADDR_TRANS) &&
2384 (tp->nvram_jedecnum == JEDEC_ATMEL))
2385
2386 addr = ((addr / tp->nvram_pagesize) <<
2387 ATMEL_AT45DB0X1B_PAGE_POS) +
2388 (addr % tp->nvram_pagesize);
2389
2390 return addr;
2391}
2392
2393static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
2394{
2395 if ((tp->tg3_flags & TG3_FLAG_NVRAM) &&
2396 (tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) &&
2397 (tp->tg3_flags2 & TG3_FLG2_FLASH) &&
2398 !(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM_ADDR_TRANS) &&
2399 (tp->nvram_jedecnum == JEDEC_ATMEL))
2400
2401 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
2402 tp->nvram_pagesize) +
2403 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
2404
2405 return addr;
2406}
2407
Matt Carlsone4f34112009-02-25 14:25:00 +00002408/* NOTE: Data read in from NVRAM is byteswapped according to
2409 * the byteswapping settings for all other register accesses.
2410 * tg3 devices are BE devices, so on a BE machine, the data
2411 * returned will be exactly as it is seen in NVRAM. On a LE
2412 * machine, the 32-bit value will be byteswapped.
2413 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002414static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
2415{
2416 int ret;
2417
2418 if (!(tp->tg3_flags & TG3_FLAG_NVRAM))
2419 return tg3_nvram_read_using_eeprom(tp, offset, val);
2420
2421 offset = tg3_nvram_phys_addr(tp, offset);
2422
2423 if (offset > NVRAM_ADDR_MSK)
2424 return -EINVAL;
2425
2426 ret = tg3_nvram_lock(tp);
2427 if (ret)
2428 return ret;
2429
2430 tg3_enable_nvram_access(tp);
2431
2432 tw32(NVRAM_ADDR, offset);
2433 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
2434 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
2435
2436 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00002437 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002438
2439 tg3_disable_nvram_access(tp);
2440
2441 tg3_nvram_unlock(tp);
2442
2443 return ret;
2444}
2445
Matt Carlsona9dc5292009-02-25 14:25:30 +00002446/* Ensures NVRAM data is in bytestream format. */
2447static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002448{
2449 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00002450 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002451 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00002452 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002453 return res;
2454}
2455
2456/* tp->lock is held. */
Matt Carlson3f007892008-11-03 16:51:36 -08002457static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
2458{
2459 u32 addr_high, addr_low;
2460 int i;
2461
2462 addr_high = ((tp->dev->dev_addr[0] << 8) |
2463 tp->dev->dev_addr[1]);
2464 addr_low = ((tp->dev->dev_addr[2] << 24) |
2465 (tp->dev->dev_addr[3] << 16) |
2466 (tp->dev->dev_addr[4] << 8) |
2467 (tp->dev->dev_addr[5] << 0));
2468 for (i = 0; i < 4; i++) {
2469 if (i == 1 && skip_mac_1)
2470 continue;
2471 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
2472 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
2473 }
2474
2475 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2476 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2477 for (i = 0; i < 12; i++) {
2478 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
2479 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
2480 }
2481 }
2482
2483 addr_high = (tp->dev->dev_addr[0] +
2484 tp->dev->dev_addr[1] +
2485 tp->dev->dev_addr[2] +
2486 tp->dev->dev_addr[3] +
2487 tp->dev->dev_addr[4] +
2488 tp->dev->dev_addr[5]) &
2489 TX_BACKOFF_SEED_MASK;
2490 tw32(MAC_TX_BACKOFF_SEED, addr_high);
2491}
2492
Michael Chanbc1c7562006-03-20 17:48:03 -08002493static int tg3_set_power_state(struct tg3 *tp, pci_power_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494{
2495 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002496 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
2498 /* Make sure register accesses (indirect or otherwise)
2499 * will function correctly.
2500 */
2501 pci_write_config_dword(tp->pdev,
2502 TG3PCI_MISC_HOST_CTRL,
2503 tp->misc_host_ctrl);
2504
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 switch (state) {
Michael Chanbc1c7562006-03-20 17:48:03 -08002506 case PCI_D0:
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07002507 pci_enable_wake(tp->pdev, state, false);
2508 pci_set_power_state(tp->pdev, PCI_D0);
Michael Chan8c6bda12005-04-21 17:09:08 -07002509
Michael Chan9d26e212006-12-07 00:21:14 -08002510 /* Switch out of Vaux if it is a NIC */
2511 if (tp->tg3_flags2 & TG3_FLG2_IS_NIC)
Michael Chanb401e9e2005-12-19 16:27:04 -08002512 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513
2514 return 0;
2515
Michael Chanbc1c7562006-03-20 17:48:03 -08002516 case PCI_D1:
Michael Chanbc1c7562006-03-20 17:48:03 -08002517 case PCI_D2:
Michael Chanbc1c7562006-03-20 17:48:03 -08002518 case PCI_D3hot:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 break;
2520
2521 default:
Joe Perches05dbe002010-02-17 19:44:19 +00002522 netdev_err(tp->dev, "Invalid power state (D%d) requested\n",
2523 state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002525 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002526
2527 /* Restore the CLKREQ setting. */
2528 if (tp->tg3_flags3 & TG3_FLG3_CLKREQ_BUG) {
2529 u16 lnkctl;
2530
2531 pci_read_config_word(tp->pdev,
2532 tp->pcie_cap + PCI_EXP_LNKCTL,
2533 &lnkctl);
2534 lnkctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
2535 pci_write_config_word(tp->pdev,
2536 tp->pcie_cap + PCI_EXP_LNKCTL,
2537 lnkctl);
2538 }
2539
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
2541 tw32(TG3PCI_MISC_HOST_CTRL,
2542 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
2543
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002544 device_should_wake = pci_pme_capable(tp->pdev, state) &&
2545 device_may_wakeup(&tp->pdev->dev) &&
2546 (tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
2547
Matt Carlsondd477002008-05-25 23:45:58 -07002548 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08002549 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002550 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson800960682010-08-02 11:26:06 +00002551 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002552 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002553 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002554
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002555 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002556
Matt Carlson800960682010-08-02 11:26:06 +00002557 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002558
2559 tp->link_config.orig_speed = phydev->speed;
2560 tp->link_config.orig_duplex = phydev->duplex;
2561 tp->link_config.orig_autoneg = phydev->autoneg;
2562 tp->link_config.orig_advertising = phydev->advertising;
2563
2564 advertising = ADVERTISED_TP |
2565 ADVERTISED_Pause |
2566 ADVERTISED_Autoneg |
2567 ADVERTISED_10baseT_Half;
2568
2569 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002570 device_should_wake) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002571 if (tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB)
2572 advertising |=
2573 ADVERTISED_100baseT_Half |
2574 ADVERTISED_100baseT_Full |
2575 ADVERTISED_10baseT_Full;
2576 else
2577 advertising |= ADVERTISED_10baseT_Full;
2578 }
2579
2580 phydev->advertising = advertising;
2581
2582 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002583
2584 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00002585 if (phyid != PHY_ID_BCMAC131) {
2586 phyid &= PHY_BCM_OUI_MASK;
2587 if (phyid == PHY_BCM_OUI_1 ||
2588 phyid == PHY_BCM_OUI_2 ||
2589 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08002590 do_low_power = true;
2591 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002592 }
Matt Carlsondd477002008-05-25 23:45:58 -07002593 } else {
Matt Carlson20232762008-12-21 20:18:56 -08002594 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002595
Matt Carlson800960682010-08-02 11:26:06 +00002596 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
2597 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07002598 tp->link_config.orig_speed = tp->link_config.speed;
2599 tp->link_config.orig_duplex = tp->link_config.duplex;
2600 tp->link_config.orig_autoneg = tp->link_config.autoneg;
2601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002603 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Matt Carlsondd477002008-05-25 23:45:58 -07002604 tp->link_config.speed = SPEED_10;
2605 tp->link_config.duplex = DUPLEX_HALF;
2606 tp->link_config.autoneg = AUTONEG_ENABLE;
2607 tg3_setup_phy(tp, 0);
2608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 }
2610
Michael Chanb5d37722006-09-27 16:06:21 -07002611 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
2612 u32 val;
2613
2614 val = tr32(GRC_VCPU_EXT_CTRL);
2615 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
2616 } else if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08002617 int i;
2618 u32 val;
2619
2620 for (i = 0; i < 200; i++) {
2621 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
2622 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
2623 break;
2624 msleep(1);
2625 }
2626 }
Gary Zambranoa85feb82007-05-05 11:52:19 -07002627 if (tp->tg3_flags & TG3_FLAG_WOL_CAP)
2628 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
2629 WOL_DRV_STATE_SHUTDOWN |
2630 WOL_DRV_WOL |
2631 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08002632
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002633 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 u32 mac_mode;
2635
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002636 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08002637 if (do_low_power) {
Matt Carlsondd477002008-05-25 23:45:58 -07002638 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x5a);
2639 udelay(40);
2640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002642 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07002643 mac_mode = MAC_MODE_PORT_MODE_GMII;
2644 else
2645 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002647 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
2648 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2649 ASIC_REV_5700) {
2650 u32 speed = (tp->tg3_flags &
2651 TG3_FLAG_WOL_SPEED_100MB) ?
2652 SPEED_100 : SPEED_10;
2653 if (tg3_5700_link_polarity(tp, speed))
2654 mac_mode |= MAC_MODE_LINK_POLARITY;
2655 else
2656 mac_mode &= ~MAC_MODE_LINK_POLARITY;
2657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 } else {
2659 mac_mode = MAC_MODE_PORT_MODE_TBI;
2660 }
2661
John W. Linvillecbf46852005-04-21 17:01:29 -07002662 if (!(tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 tw32(MAC_LED_CTRL, tp->led_ctrl);
2664
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002665 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
2666 if (((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
2667 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) &&
2668 ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
2669 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)))
2670 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671
Matt Carlson3bda1252008-08-15 14:08:22 -07002672 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
2673 mac_mode |= tp->mac_mode &
2674 (MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN);
2675 if (mac_mode & MAC_MODE_APE_TX_EN)
2676 mac_mode |= MAC_MODE_TDE_ENABLE;
2677 }
2678
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 tw32_f(MAC_MODE, mac_mode);
2680 udelay(100);
2681
2682 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
2683 udelay(10);
2684 }
2685
2686 if (!(tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB) &&
2687 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2688 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
2689 u32 base_val;
2690
2691 base_val = tp->pci_clock_ctrl;
2692 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
2693 CLOCK_CTRL_TXCLK_DISABLE);
2694
Michael Chanb401e9e2005-12-19 16:27:04 -08002695 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
2696 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Michael Chand7b0a852007-02-13 12:17:38 -08002697 } else if ((tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
Matt Carlson795d01c2007-10-07 23:28:17 -07002698 (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
Michael Chand7b0a852007-02-13 12:17:38 -08002699 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)) {
Michael Chan4cf78e42005-07-25 12:29:19 -07002700 /* do nothing */
Michael Chan85e94ce2005-04-21 17:05:28 -07002701 } else if (!((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF))) {
2703 u32 newbits1, newbits2;
2704
2705 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2706 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2707 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
2708 CLOCK_CTRL_TXCLK_DISABLE |
2709 CLOCK_CTRL_ALTCLK);
2710 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2711 } else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
2712 newbits1 = CLOCK_CTRL_625_CORE;
2713 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
2714 } else {
2715 newbits1 = CLOCK_CTRL_ALTCLK;
2716 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2717 }
2718
Michael Chanb401e9e2005-12-19 16:27:04 -08002719 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
2720 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721
Michael Chanb401e9e2005-12-19 16:27:04 -08002722 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
2723 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724
2725 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
2726 u32 newbits3;
2727
2728 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2729 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2730 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
2731 CLOCK_CTRL_TXCLK_DISABLE |
2732 CLOCK_CTRL_44MHZ_CORE);
2733 } else {
2734 newbits3 = CLOCK_CTRL_44MHZ_CORE;
2735 }
2736
Michael Chanb401e9e2005-12-19 16:27:04 -08002737 tw32_wait_f(TG3PCI_CLOCK_CTRL,
2738 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 }
2740 }
2741
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002742 if (!(device_should_wake) &&
Matt Carlson22435842008-11-21 17:21:13 -08002743 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08002744 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08002745
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 tg3_frob_aux_power(tp);
2747
2748 /* Workaround for unstable PLL clock */
2749 if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
2750 (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
2751 u32 val = tr32(0x7d00);
2752
2753 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
2754 tw32(0x7d00, val);
Michael Chan6921d202005-12-13 21:15:53 -08002755 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08002756 int err;
2757
2758 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08002760 if (!err)
2761 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08002762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 }
2764
Michael Chanbbadf502006-04-06 21:46:34 -07002765 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
2766
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002767 if (device_should_wake)
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07002768 pci_enable_wake(tp->pdev, state, true);
2769
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 /* Finally, set the new power state. */
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07002771 pci_set_power_state(tp->pdev, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 return 0;
2774}
2775
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
2777{
2778 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
2779 case MII_TG3_AUX_STAT_10HALF:
2780 *speed = SPEED_10;
2781 *duplex = DUPLEX_HALF;
2782 break;
2783
2784 case MII_TG3_AUX_STAT_10FULL:
2785 *speed = SPEED_10;
2786 *duplex = DUPLEX_FULL;
2787 break;
2788
2789 case MII_TG3_AUX_STAT_100HALF:
2790 *speed = SPEED_100;
2791 *duplex = DUPLEX_HALF;
2792 break;
2793
2794 case MII_TG3_AUX_STAT_100FULL:
2795 *speed = SPEED_100;
2796 *duplex = DUPLEX_FULL;
2797 break;
2798
2799 case MII_TG3_AUX_STAT_1000HALF:
2800 *speed = SPEED_1000;
2801 *duplex = DUPLEX_HALF;
2802 break;
2803
2804 case MII_TG3_AUX_STAT_1000FULL:
2805 *speed = SPEED_1000;
2806 *duplex = DUPLEX_FULL;
2807 break;
2808
2809 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002810 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07002811 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
2812 SPEED_10;
2813 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
2814 DUPLEX_HALF;
2815 break;
2816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 *speed = SPEED_INVALID;
2818 *duplex = DUPLEX_INVALID;
2819 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821}
2822
2823static void tg3_phy_copper_begin(struct tg3 *tp)
2824{
2825 u32 new_adv;
2826 int i;
2827
Matt Carlson800960682010-08-02 11:26:06 +00002828 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 /* Entering low power mode. Disable gigabit and
2830 * 100baseT advertisements.
2831 */
2832 tg3_writephy(tp, MII_TG3_CTRL, 0);
2833
2834 new_adv = (ADVERTISE_10HALF | ADVERTISE_10FULL |
2835 ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
2836 if (tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB)
2837 new_adv |= (ADVERTISE_100HALF | ADVERTISE_100FULL);
2838
2839 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2840 } else if (tp->link_config.speed == SPEED_INVALID) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002841 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 tp->link_config.advertising &=
2843 ~(ADVERTISED_1000baseT_Half |
2844 ADVERTISED_1000baseT_Full);
2845
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002846 new_adv = ADVERTISE_CSMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 if (tp->link_config.advertising & ADVERTISED_10baseT_Half)
2848 new_adv |= ADVERTISE_10HALF;
2849 if (tp->link_config.advertising & ADVERTISED_10baseT_Full)
2850 new_adv |= ADVERTISE_10FULL;
2851 if (tp->link_config.advertising & ADVERTISED_100baseT_Half)
2852 new_adv |= ADVERTISE_100HALF;
2853 if (tp->link_config.advertising & ADVERTISED_100baseT_Full)
2854 new_adv |= ADVERTISE_100FULL;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002855
2856 new_adv |= tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2857
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2859
2860 if (tp->link_config.advertising &
2861 (ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full)) {
2862 new_adv = 0;
2863 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
2864 new_adv |= MII_TG3_CTRL_ADV_1000_HALF;
2865 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
2866 new_adv |= MII_TG3_CTRL_ADV_1000_FULL;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002867 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2869 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0))
2870 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2871 MII_TG3_CTRL_ENABLE_AS_MASTER);
2872 tg3_writephy(tp, MII_TG3_CTRL, new_adv);
2873 } else {
2874 tg3_writephy(tp, MII_TG3_CTRL, 0);
2875 }
2876 } else {
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002877 new_adv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2878 new_adv |= ADVERTISE_CSMA;
2879
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 /* Asking for a specific link mode. */
2881 if (tp->link_config.speed == SPEED_1000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2883
2884 if (tp->link_config.duplex == DUPLEX_FULL)
2885 new_adv = MII_TG3_CTRL_ADV_1000_FULL;
2886 else
2887 new_adv = MII_TG3_CTRL_ADV_1000_HALF;
2888 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2889 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
2890 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2891 MII_TG3_CTRL_ENABLE_AS_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 if (tp->link_config.speed == SPEED_100) {
2894 if (tp->link_config.duplex == DUPLEX_FULL)
2895 new_adv |= ADVERTISE_100FULL;
2896 else
2897 new_adv |= ADVERTISE_100HALF;
2898 } else {
2899 if (tp->link_config.duplex == DUPLEX_FULL)
2900 new_adv |= ADVERTISE_10FULL;
2901 else
2902 new_adv |= ADVERTISE_10HALF;
2903 }
2904 tg3_writephy(tp, MII_ADVERTISE, new_adv);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002905
2906 new_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002908
2909 tg3_writephy(tp, MII_TG3_CTRL, new_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 }
2911
2912 if (tp->link_config.autoneg == AUTONEG_DISABLE &&
2913 tp->link_config.speed != SPEED_INVALID) {
2914 u32 bmcr, orig_bmcr;
2915
2916 tp->link_config.active_speed = tp->link_config.speed;
2917 tp->link_config.active_duplex = tp->link_config.duplex;
2918
2919 bmcr = 0;
2920 switch (tp->link_config.speed) {
2921 default:
2922 case SPEED_10:
2923 break;
2924
2925 case SPEED_100:
2926 bmcr |= BMCR_SPEED100;
2927 break;
2928
2929 case SPEED_1000:
2930 bmcr |= TG3_BMCR_SPEED1000;
2931 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933
2934 if (tp->link_config.duplex == DUPLEX_FULL)
2935 bmcr |= BMCR_FULLDPLX;
2936
2937 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
2938 (bmcr != orig_bmcr)) {
2939 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
2940 for (i = 0; i < 1500; i++) {
2941 u32 tmp;
2942
2943 udelay(10);
2944 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
2945 tg3_readphy(tp, MII_BMSR, &tmp))
2946 continue;
2947 if (!(tmp & BMSR_LSTATUS)) {
2948 udelay(40);
2949 break;
2950 }
2951 }
2952 tg3_writephy(tp, MII_BMCR, bmcr);
2953 udelay(40);
2954 }
2955 } else {
2956 tg3_writephy(tp, MII_BMCR,
2957 BMCR_ANENABLE | BMCR_ANRESTART);
2958 }
2959}
2960
2961static int tg3_init_5401phy_dsp(struct tg3 *tp)
2962{
2963 int err;
2964
2965 /* Turn off tap power management. */
2966 /* Set Extended packet length bit */
2967 err = tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4c20);
2968
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002969 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
2970 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
2971 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
2972 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
2973 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974
2975 udelay(40);
2976
2977 return err;
2978}
2979
Michael Chan3600d912006-12-07 00:21:48 -08002980static int tg3_copper_is_advertising_all(struct tg3 *tp, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981{
Michael Chan3600d912006-12-07 00:21:48 -08002982 u32 adv_reg, all_mask = 0;
2983
2984 if (mask & ADVERTISED_10baseT_Half)
2985 all_mask |= ADVERTISE_10HALF;
2986 if (mask & ADVERTISED_10baseT_Full)
2987 all_mask |= ADVERTISE_10FULL;
2988 if (mask & ADVERTISED_100baseT_Half)
2989 all_mask |= ADVERTISE_100HALF;
2990 if (mask & ADVERTISED_100baseT_Full)
2991 all_mask |= ADVERTISE_100FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992
2993 if (tg3_readphy(tp, MII_ADVERTISE, &adv_reg))
2994 return 0;
2995
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 if ((adv_reg & all_mask) != all_mask)
2997 return 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002998 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 u32 tg3_ctrl;
3000
Michael Chan3600d912006-12-07 00:21:48 -08003001 all_mask = 0;
3002 if (mask & ADVERTISED_1000baseT_Half)
3003 all_mask |= ADVERTISE_1000HALF;
3004 if (mask & ADVERTISED_1000baseT_Full)
3005 all_mask |= ADVERTISE_1000FULL;
3006
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 if (tg3_readphy(tp, MII_TG3_CTRL, &tg3_ctrl))
3008 return 0;
3009
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 if ((tg3_ctrl & all_mask) != all_mask)
3011 return 0;
3012 }
3013 return 1;
3014}
3015
Matt Carlsonef167e22007-12-20 20:10:01 -08003016static int tg3_adv_1000T_flowctrl_ok(struct tg3 *tp, u32 *lcladv, u32 *rmtadv)
3017{
3018 u32 curadv, reqadv;
3019
3020 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
3021 return 1;
3022
3023 curadv = *lcladv & (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
3024 reqadv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
3025
3026 if (tp->link_config.active_duplex == DUPLEX_FULL) {
3027 if (curadv != reqadv)
3028 return 0;
3029
3030 if (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG)
3031 tg3_readphy(tp, MII_LPA, rmtadv);
3032 } else {
3033 /* Reprogram the advertisement register, even if it
3034 * does not affect the current link. If the link
3035 * gets renegotiated in the future, we can save an
3036 * additional renegotiation cycle by advertising
3037 * it correctly in the first place.
3038 */
3039 if (curadv != reqadv) {
3040 *lcladv &= ~(ADVERTISE_PAUSE_CAP |
3041 ADVERTISE_PAUSE_ASYM);
3042 tg3_writephy(tp, MII_ADVERTISE, *lcladv | reqadv);
3043 }
3044 }
3045
3046 return 1;
3047}
3048
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
3050{
3051 int current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003052 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08003053 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 u16 current_speed;
3055 u8 current_duplex;
3056 int i, err;
3057
3058 tw32(MAC_EVENT, 0);
3059
3060 tw32_f(MAC_STATUS,
3061 (MAC_STATUS_SYNC_CHANGED |
3062 MAC_STATUS_CFG_CHANGED |
3063 MAC_STATUS_MI_COMPLETION |
3064 MAC_STATUS_LNKSTATE_CHANGED));
3065 udelay(40);
3066
Matt Carlson8ef21422008-05-02 16:47:53 -07003067 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
3068 tw32_f(MAC_MI_MODE,
3069 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
3070 udelay(80);
3071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072
3073 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x02);
3074
3075 /* Some third-party PHYs need to be reset on link going
3076 * down.
3077 */
3078 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3079 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
3080 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
3081 netif_carrier_ok(tp->dev)) {
3082 tg3_readphy(tp, MII_BMSR, &bmsr);
3083 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3084 !(bmsr & BMSR_LSTATUS))
3085 force_reset = 1;
3086 }
3087 if (force_reset)
3088 tg3_phy_reset(tp);
3089
Matt Carlson79eb6902010-02-17 15:17:03 +00003090 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 tg3_readphy(tp, MII_BMSR, &bmsr);
3092 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
3093 !(tp->tg3_flags & TG3_FLAG_INIT_COMPLETE))
3094 bmsr = 0;
3095
3096 if (!(bmsr & BMSR_LSTATUS)) {
3097 err = tg3_init_5401phy_dsp(tp);
3098 if (err)
3099 return err;
3100
3101 tg3_readphy(tp, MII_BMSR, &bmsr);
3102 for (i = 0; i < 1000; i++) {
3103 udelay(10);
3104 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3105 (bmsr & BMSR_LSTATUS)) {
3106 udelay(40);
3107 break;
3108 }
3109 }
3110
Matt Carlson79eb6902010-02-17 15:17:03 +00003111 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
3112 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 !(bmsr & BMSR_LSTATUS) &&
3114 tp->link_config.active_speed == SPEED_1000) {
3115 err = tg3_phy_reset(tp);
3116 if (!err)
3117 err = tg3_init_5401phy_dsp(tp);
3118 if (err)
3119 return err;
3120 }
3121 }
3122 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3123 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
3124 /* 5701 {A0,B0} CRC bug workaround */
3125 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00003126 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
3127 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
3128 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 }
3130
3131 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003132 tg3_readphy(tp, MII_TG3_ISTAT, &val);
3133 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003135 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003137 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 tg3_writephy(tp, MII_TG3_IMASK, ~0);
3139
3140 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3141 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3142 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
3143 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3144 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
3145 else
3146 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
3147 }
3148
3149 current_link_up = 0;
3150 current_speed = SPEED_INVALID;
3151 current_duplex = DUPLEX_INVALID;
3152
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003153 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4007);
3155 tg3_readphy(tp, MII_TG3_AUX_CTRL, &val);
3156 if (!(val & (1 << 10))) {
3157 val |= (1 << 10);
3158 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
3159 goto relink;
3160 }
3161 }
3162
3163 bmsr = 0;
3164 for (i = 0; i < 100; i++) {
3165 tg3_readphy(tp, MII_BMSR, &bmsr);
3166 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3167 (bmsr & BMSR_LSTATUS))
3168 break;
3169 udelay(40);
3170 }
3171
3172 if (bmsr & BMSR_LSTATUS) {
3173 u32 aux_stat, bmcr;
3174
3175 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
3176 for (i = 0; i < 2000; i++) {
3177 udelay(10);
3178 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
3179 aux_stat)
3180 break;
3181 }
3182
3183 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
3184 &current_speed,
3185 &current_duplex);
3186
3187 bmcr = 0;
3188 for (i = 0; i < 200; i++) {
3189 tg3_readphy(tp, MII_BMCR, &bmcr);
3190 if (tg3_readphy(tp, MII_BMCR, &bmcr))
3191 continue;
3192 if (bmcr && bmcr != 0x7fff)
3193 break;
3194 udelay(10);
3195 }
3196
Matt Carlsonef167e22007-12-20 20:10:01 -08003197 lcl_adv = 0;
3198 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199
Matt Carlsonef167e22007-12-20 20:10:01 -08003200 tp->link_config.active_speed = current_speed;
3201 tp->link_config.active_duplex = current_duplex;
3202
3203 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3204 if ((bmcr & BMCR_ANENABLE) &&
3205 tg3_copper_is_advertising_all(tp,
3206 tp->link_config.advertising)) {
3207 if (tg3_adv_1000T_flowctrl_ok(tp, &lcl_adv,
3208 &rmt_adv))
3209 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 }
3211 } else {
3212 if (!(bmcr & BMCR_ANENABLE) &&
3213 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08003214 tp->link_config.duplex == current_duplex &&
3215 tp->link_config.flowctrl ==
3216 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 }
3219 }
3220
Matt Carlsonef167e22007-12-20 20:10:01 -08003221 if (current_link_up == 1 &&
3222 tp->link_config.active_duplex == DUPLEX_FULL)
3223 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 }
3225
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226relink:
Matt Carlson800960682010-08-02 11:26:06 +00003227 if (current_link_up == 0 || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 tg3_phy_copper_begin(tp);
3229
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003230 tg3_readphy(tp, MII_BMSR, &bmsr);
3231 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3232 (bmsr & BMSR_LSTATUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 current_link_up = 1;
3234 }
3235
3236 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
3237 if (current_link_up == 1) {
3238 if (tp->link_config.active_speed == SPEED_100 ||
3239 tp->link_config.active_speed == SPEED_10)
3240 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3241 else
3242 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003243 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00003244 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3245 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
3247
3248 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
3249 if (tp->link_config.active_duplex == DUPLEX_HALF)
3250 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
3251
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003253 if (current_link_up == 1 &&
3254 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003256 else
3257 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 }
3259
3260 /* ??? Without this setting Netgear GA302T PHY does not
3261 * ??? send/receive packets...
3262 */
Matt Carlson79eb6902010-02-17 15:17:03 +00003263 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
3265 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
3266 tw32_f(MAC_MI_MODE, tp->mi_mode);
3267 udelay(80);
3268 }
3269
3270 tw32_f(MAC_MODE, tp->mac_mode);
3271 udelay(40);
3272
3273 if (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) {
3274 /* Polled via timer. */
3275 tw32_f(MAC_EVENT, 0);
3276 } else {
3277 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
3278 }
3279 udelay(40);
3280
3281 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
3282 current_link_up == 1 &&
3283 tp->link_config.active_speed == SPEED_1000 &&
3284 ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) ||
3285 (tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED))) {
3286 udelay(120);
3287 tw32_f(MAC_STATUS,
3288 (MAC_STATUS_SYNC_CHANGED |
3289 MAC_STATUS_CFG_CHANGED));
3290 udelay(40);
3291 tg3_write_mem(tp,
3292 NIC_SRAM_FIRMWARE_MBOX,
3293 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
3294 }
3295
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003296 /* Prevent send BD corruption. */
3297 if (tp->tg3_flags3 & TG3_FLG3_CLKREQ_BUG) {
3298 u16 oldlnkctl, newlnkctl;
3299
3300 pci_read_config_word(tp->pdev,
3301 tp->pcie_cap + PCI_EXP_LNKCTL,
3302 &oldlnkctl);
3303 if (tp->link_config.active_speed == SPEED_100 ||
3304 tp->link_config.active_speed == SPEED_10)
3305 newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
3306 else
3307 newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
3308 if (newlnkctl != oldlnkctl)
3309 pci_write_config_word(tp->pdev,
3310 tp->pcie_cap + PCI_EXP_LNKCTL,
3311 newlnkctl);
3312 }
3313
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 if (current_link_up != netif_carrier_ok(tp->dev)) {
3315 if (current_link_up)
3316 netif_carrier_on(tp->dev);
3317 else
3318 netif_carrier_off(tp->dev);
3319 tg3_link_report(tp);
3320 }
3321
3322 return 0;
3323}
3324
3325struct tg3_fiber_aneginfo {
3326 int state;
3327#define ANEG_STATE_UNKNOWN 0
3328#define ANEG_STATE_AN_ENABLE 1
3329#define ANEG_STATE_RESTART_INIT 2
3330#define ANEG_STATE_RESTART 3
3331#define ANEG_STATE_DISABLE_LINK_OK 4
3332#define ANEG_STATE_ABILITY_DETECT_INIT 5
3333#define ANEG_STATE_ABILITY_DETECT 6
3334#define ANEG_STATE_ACK_DETECT_INIT 7
3335#define ANEG_STATE_ACK_DETECT 8
3336#define ANEG_STATE_COMPLETE_ACK_INIT 9
3337#define ANEG_STATE_COMPLETE_ACK 10
3338#define ANEG_STATE_IDLE_DETECT_INIT 11
3339#define ANEG_STATE_IDLE_DETECT 12
3340#define ANEG_STATE_LINK_OK 13
3341#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
3342#define ANEG_STATE_NEXT_PAGE_WAIT 15
3343
3344 u32 flags;
3345#define MR_AN_ENABLE 0x00000001
3346#define MR_RESTART_AN 0x00000002
3347#define MR_AN_COMPLETE 0x00000004
3348#define MR_PAGE_RX 0x00000008
3349#define MR_NP_LOADED 0x00000010
3350#define MR_TOGGLE_TX 0x00000020
3351#define MR_LP_ADV_FULL_DUPLEX 0x00000040
3352#define MR_LP_ADV_HALF_DUPLEX 0x00000080
3353#define MR_LP_ADV_SYM_PAUSE 0x00000100
3354#define MR_LP_ADV_ASYM_PAUSE 0x00000200
3355#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
3356#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
3357#define MR_LP_ADV_NEXT_PAGE 0x00001000
3358#define MR_TOGGLE_RX 0x00002000
3359#define MR_NP_RX 0x00004000
3360
3361#define MR_LINK_OK 0x80000000
3362
3363 unsigned long link_time, cur_time;
3364
3365 u32 ability_match_cfg;
3366 int ability_match_count;
3367
3368 char ability_match, idle_match, ack_match;
3369
3370 u32 txconfig, rxconfig;
3371#define ANEG_CFG_NP 0x00000080
3372#define ANEG_CFG_ACK 0x00000040
3373#define ANEG_CFG_RF2 0x00000020
3374#define ANEG_CFG_RF1 0x00000010
3375#define ANEG_CFG_PS2 0x00000001
3376#define ANEG_CFG_PS1 0x00008000
3377#define ANEG_CFG_HD 0x00004000
3378#define ANEG_CFG_FD 0x00002000
3379#define ANEG_CFG_INVAL 0x00001f06
3380
3381};
3382#define ANEG_OK 0
3383#define ANEG_DONE 1
3384#define ANEG_TIMER_ENAB 2
3385#define ANEG_FAILED -1
3386
3387#define ANEG_STATE_SETTLE_TIME 10000
3388
3389static int tg3_fiber_aneg_smachine(struct tg3 *tp,
3390 struct tg3_fiber_aneginfo *ap)
3391{
Matt Carlson5be73b42007-12-20 20:09:29 -08003392 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 unsigned long delta;
3394 u32 rx_cfg_reg;
3395 int ret;
3396
3397 if (ap->state == ANEG_STATE_UNKNOWN) {
3398 ap->rxconfig = 0;
3399 ap->link_time = 0;
3400 ap->cur_time = 0;
3401 ap->ability_match_cfg = 0;
3402 ap->ability_match_count = 0;
3403 ap->ability_match = 0;
3404 ap->idle_match = 0;
3405 ap->ack_match = 0;
3406 }
3407 ap->cur_time++;
3408
3409 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
3410 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
3411
3412 if (rx_cfg_reg != ap->ability_match_cfg) {
3413 ap->ability_match_cfg = rx_cfg_reg;
3414 ap->ability_match = 0;
3415 ap->ability_match_count = 0;
3416 } else {
3417 if (++ap->ability_match_count > 1) {
3418 ap->ability_match = 1;
3419 ap->ability_match_cfg = rx_cfg_reg;
3420 }
3421 }
3422 if (rx_cfg_reg & ANEG_CFG_ACK)
3423 ap->ack_match = 1;
3424 else
3425 ap->ack_match = 0;
3426
3427 ap->idle_match = 0;
3428 } else {
3429 ap->idle_match = 1;
3430 ap->ability_match_cfg = 0;
3431 ap->ability_match_count = 0;
3432 ap->ability_match = 0;
3433 ap->ack_match = 0;
3434
3435 rx_cfg_reg = 0;
3436 }
3437
3438 ap->rxconfig = rx_cfg_reg;
3439 ret = ANEG_OK;
3440
Matt Carlson33f401a2010-04-05 10:19:27 +00003441 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 case ANEG_STATE_UNKNOWN:
3443 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
3444 ap->state = ANEG_STATE_AN_ENABLE;
3445
3446 /* fallthru */
3447 case ANEG_STATE_AN_ENABLE:
3448 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
3449 if (ap->flags & MR_AN_ENABLE) {
3450 ap->link_time = 0;
3451 ap->cur_time = 0;
3452 ap->ability_match_cfg = 0;
3453 ap->ability_match_count = 0;
3454 ap->ability_match = 0;
3455 ap->idle_match = 0;
3456 ap->ack_match = 0;
3457
3458 ap->state = ANEG_STATE_RESTART_INIT;
3459 } else {
3460 ap->state = ANEG_STATE_DISABLE_LINK_OK;
3461 }
3462 break;
3463
3464 case ANEG_STATE_RESTART_INIT:
3465 ap->link_time = ap->cur_time;
3466 ap->flags &= ~(MR_NP_LOADED);
3467 ap->txconfig = 0;
3468 tw32(MAC_TX_AUTO_NEG, 0);
3469 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3470 tw32_f(MAC_MODE, tp->mac_mode);
3471 udelay(40);
3472
3473 ret = ANEG_TIMER_ENAB;
3474 ap->state = ANEG_STATE_RESTART;
3475
3476 /* fallthru */
3477 case ANEG_STATE_RESTART:
3478 delta = ap->cur_time - ap->link_time;
Matt Carlson859a588792010-04-05 10:19:28 +00003479 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a588792010-04-05 10:19:28 +00003481 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 break;
3484
3485 case ANEG_STATE_DISABLE_LINK_OK:
3486 ret = ANEG_DONE;
3487 break;
3488
3489 case ANEG_STATE_ABILITY_DETECT_INIT:
3490 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08003491 ap->txconfig = ANEG_CFG_FD;
3492 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3493 if (flowctrl & ADVERTISE_1000XPAUSE)
3494 ap->txconfig |= ANEG_CFG_PS1;
3495 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3496 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3498 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3499 tw32_f(MAC_MODE, tp->mac_mode);
3500 udelay(40);
3501
3502 ap->state = ANEG_STATE_ABILITY_DETECT;
3503 break;
3504
3505 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a588792010-04-05 10:19:28 +00003506 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 break;
3509
3510 case ANEG_STATE_ACK_DETECT_INIT:
3511 ap->txconfig |= ANEG_CFG_ACK;
3512 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3513 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3514 tw32_f(MAC_MODE, tp->mac_mode);
3515 udelay(40);
3516
3517 ap->state = ANEG_STATE_ACK_DETECT;
3518
3519 /* fallthru */
3520 case ANEG_STATE_ACK_DETECT:
3521 if (ap->ack_match != 0) {
3522 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
3523 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
3524 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
3525 } else {
3526 ap->state = ANEG_STATE_AN_ENABLE;
3527 }
3528 } else if (ap->ability_match != 0 &&
3529 ap->rxconfig == 0) {
3530 ap->state = ANEG_STATE_AN_ENABLE;
3531 }
3532 break;
3533
3534 case ANEG_STATE_COMPLETE_ACK_INIT:
3535 if (ap->rxconfig & ANEG_CFG_INVAL) {
3536 ret = ANEG_FAILED;
3537 break;
3538 }
3539 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
3540 MR_LP_ADV_HALF_DUPLEX |
3541 MR_LP_ADV_SYM_PAUSE |
3542 MR_LP_ADV_ASYM_PAUSE |
3543 MR_LP_ADV_REMOTE_FAULT1 |
3544 MR_LP_ADV_REMOTE_FAULT2 |
3545 MR_LP_ADV_NEXT_PAGE |
3546 MR_TOGGLE_RX |
3547 MR_NP_RX);
3548 if (ap->rxconfig & ANEG_CFG_FD)
3549 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
3550 if (ap->rxconfig & ANEG_CFG_HD)
3551 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
3552 if (ap->rxconfig & ANEG_CFG_PS1)
3553 ap->flags |= MR_LP_ADV_SYM_PAUSE;
3554 if (ap->rxconfig & ANEG_CFG_PS2)
3555 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
3556 if (ap->rxconfig & ANEG_CFG_RF1)
3557 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
3558 if (ap->rxconfig & ANEG_CFG_RF2)
3559 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
3560 if (ap->rxconfig & ANEG_CFG_NP)
3561 ap->flags |= MR_LP_ADV_NEXT_PAGE;
3562
3563 ap->link_time = ap->cur_time;
3564
3565 ap->flags ^= (MR_TOGGLE_TX);
3566 if (ap->rxconfig & 0x0008)
3567 ap->flags |= MR_TOGGLE_RX;
3568 if (ap->rxconfig & ANEG_CFG_NP)
3569 ap->flags |= MR_NP_RX;
3570 ap->flags |= MR_PAGE_RX;
3571
3572 ap->state = ANEG_STATE_COMPLETE_ACK;
3573 ret = ANEG_TIMER_ENAB;
3574 break;
3575
3576 case ANEG_STATE_COMPLETE_ACK:
3577 if (ap->ability_match != 0 &&
3578 ap->rxconfig == 0) {
3579 ap->state = ANEG_STATE_AN_ENABLE;
3580 break;
3581 }
3582 delta = ap->cur_time - ap->link_time;
3583 if (delta > ANEG_STATE_SETTLE_TIME) {
3584 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
3585 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3586 } else {
3587 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
3588 !(ap->flags & MR_NP_RX)) {
3589 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3590 } else {
3591 ret = ANEG_FAILED;
3592 }
3593 }
3594 }
3595 break;
3596
3597 case ANEG_STATE_IDLE_DETECT_INIT:
3598 ap->link_time = ap->cur_time;
3599 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3600 tw32_f(MAC_MODE, tp->mac_mode);
3601 udelay(40);
3602
3603 ap->state = ANEG_STATE_IDLE_DETECT;
3604 ret = ANEG_TIMER_ENAB;
3605 break;
3606
3607 case ANEG_STATE_IDLE_DETECT:
3608 if (ap->ability_match != 0 &&
3609 ap->rxconfig == 0) {
3610 ap->state = ANEG_STATE_AN_ENABLE;
3611 break;
3612 }
3613 delta = ap->cur_time - ap->link_time;
3614 if (delta > ANEG_STATE_SETTLE_TIME) {
3615 /* XXX another gem from the Broadcom driver :( */
3616 ap->state = ANEG_STATE_LINK_OK;
3617 }
3618 break;
3619
3620 case ANEG_STATE_LINK_OK:
3621 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
3622 ret = ANEG_DONE;
3623 break;
3624
3625 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
3626 /* ??? unimplemented */
3627 break;
3628
3629 case ANEG_STATE_NEXT_PAGE_WAIT:
3630 /* ??? unimplemented */
3631 break;
3632
3633 default:
3634 ret = ANEG_FAILED;
3635 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637
3638 return ret;
3639}
3640
Matt Carlson5be73b42007-12-20 20:09:29 -08003641static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642{
3643 int res = 0;
3644 struct tg3_fiber_aneginfo aninfo;
3645 int status = ANEG_FAILED;
3646 unsigned int tick;
3647 u32 tmp;
3648
3649 tw32_f(MAC_TX_AUTO_NEG, 0);
3650
3651 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
3652 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
3653 udelay(40);
3654
3655 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
3656 udelay(40);
3657
3658 memset(&aninfo, 0, sizeof(aninfo));
3659 aninfo.flags |= MR_AN_ENABLE;
3660 aninfo.state = ANEG_STATE_UNKNOWN;
3661 aninfo.cur_time = 0;
3662 tick = 0;
3663 while (++tick < 195000) {
3664 status = tg3_fiber_aneg_smachine(tp, &aninfo);
3665 if (status == ANEG_DONE || status == ANEG_FAILED)
3666 break;
3667
3668 udelay(1);
3669 }
3670
3671 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3672 tw32_f(MAC_MODE, tp->mac_mode);
3673 udelay(40);
3674
Matt Carlson5be73b42007-12-20 20:09:29 -08003675 *txflags = aninfo.txconfig;
3676 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677
3678 if (status == ANEG_DONE &&
3679 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
3680 MR_LP_ADV_FULL_DUPLEX)))
3681 res = 1;
3682
3683 return res;
3684}
3685
3686static void tg3_init_bcm8002(struct tg3 *tp)
3687{
3688 u32 mac_status = tr32(MAC_STATUS);
3689 int i;
3690
3691 /* Reset when initting first time or we have a link. */
3692 if ((tp->tg3_flags & TG3_FLAG_INIT_COMPLETE) &&
3693 !(mac_status & MAC_STATUS_PCS_SYNCED))
3694 return;
3695
3696 /* Set PLL lock range. */
3697 tg3_writephy(tp, 0x16, 0x8007);
3698
3699 /* SW reset */
3700 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
3701
3702 /* Wait for reset to complete. */
3703 /* XXX schedule_timeout() ... */
3704 for (i = 0; i < 500; i++)
3705 udelay(10);
3706
3707 /* Config mode; select PMA/Ch 1 regs. */
3708 tg3_writephy(tp, 0x10, 0x8411);
3709
3710 /* Enable auto-lock and comdet, select txclk for tx. */
3711 tg3_writephy(tp, 0x11, 0x0a10);
3712
3713 tg3_writephy(tp, 0x18, 0x00a0);
3714 tg3_writephy(tp, 0x16, 0x41ff);
3715
3716 /* Assert and deassert POR. */
3717 tg3_writephy(tp, 0x13, 0x0400);
3718 udelay(40);
3719 tg3_writephy(tp, 0x13, 0x0000);
3720
3721 tg3_writephy(tp, 0x11, 0x0a50);
3722 udelay(40);
3723 tg3_writephy(tp, 0x11, 0x0a10);
3724
3725 /* Wait for signal to stabilize */
3726 /* XXX schedule_timeout() ... */
3727 for (i = 0; i < 15000; i++)
3728 udelay(10);
3729
3730 /* Deselect the channel register so we can read the PHYID
3731 * later.
3732 */
3733 tg3_writephy(tp, 0x10, 0x8011);
3734}
3735
3736static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
3737{
Matt Carlson82cd3d12007-12-20 20:09:00 -08003738 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 u32 sg_dig_ctrl, sg_dig_status;
3740 u32 serdes_cfg, expected_sg_dig_ctrl;
3741 int workaround, port_a;
3742 int current_link_up;
3743
3744 serdes_cfg = 0;
3745 expected_sg_dig_ctrl = 0;
3746 workaround = 0;
3747 port_a = 1;
3748 current_link_up = 0;
3749
3750 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
3751 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
3752 workaround = 1;
3753 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
3754 port_a = 0;
3755
3756 /* preserve bits 0-11,13,14 for signal pre-emphasis */
3757 /* preserve bits 20-23 for voltage regulator */
3758 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
3759 }
3760
3761 sg_dig_ctrl = tr32(SG_DIG_CTRL);
3762
3763 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003764 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 if (workaround) {
3766 u32 val = serdes_cfg;
3767
3768 if (port_a)
3769 val |= 0xc010000;
3770 else
3771 val |= 0x4010000;
3772 tw32_f(MAC_SERDES_CFG, val);
3773 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003774
3775 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 }
3777 if (mac_status & MAC_STATUS_PCS_SYNCED) {
3778 tg3_setup_flow_control(tp, 0, 0);
3779 current_link_up = 1;
3780 }
3781 goto out;
3782 }
3783
3784 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003785 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786
Matt Carlson82cd3d12007-12-20 20:09:00 -08003787 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3788 if (flowctrl & ADVERTISE_1000XPAUSE)
3789 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
3790 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3791 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792
3793 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003794 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07003795 tp->serdes_counter &&
3796 ((mac_status & (MAC_STATUS_PCS_SYNCED |
3797 MAC_STATUS_RCVD_CFG)) ==
3798 MAC_STATUS_PCS_SYNCED)) {
3799 tp->serdes_counter--;
3800 current_link_up = 1;
3801 goto out;
3802 }
3803restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804 if (workaround)
3805 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003806 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 udelay(5);
3808 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
3809
Michael Chan3d3ebe72006-09-27 15:59:15 -07003810 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003811 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
3813 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003814 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 mac_status = tr32(MAC_STATUS);
3816
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003817 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08003819 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820
Matt Carlson82cd3d12007-12-20 20:09:00 -08003821 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
3822 local_adv |= ADVERTISE_1000XPAUSE;
3823 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
3824 local_adv |= ADVERTISE_1000XPSE_ASYM;
3825
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003826 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003827 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003828 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003829 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830
3831 tg3_setup_flow_control(tp, local_adv, remote_adv);
3832 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003833 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003834 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003835 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003836 if (tp->serdes_counter)
3837 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 else {
3839 if (workaround) {
3840 u32 val = serdes_cfg;
3841
3842 if (port_a)
3843 val |= 0xc010000;
3844 else
3845 val |= 0x4010000;
3846
3847 tw32_f(MAC_SERDES_CFG, val);
3848 }
3849
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003850 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 udelay(40);
3852
3853 /* Link parallel detection - link is up */
3854 /* only if we have PCS_SYNC and not */
3855 /* receiving config code words */
3856 mac_status = tr32(MAC_STATUS);
3857 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
3858 !(mac_status & MAC_STATUS_RCVD_CFG)) {
3859 tg3_setup_flow_control(tp, 0, 0);
3860 current_link_up = 1;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003861 tp->phy_flags |=
3862 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003863 tp->serdes_counter =
3864 SERDES_PARALLEL_DET_TIMEOUT;
3865 } else
3866 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 }
3868 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07003869 } else {
3870 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003871 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 }
3873
3874out:
3875 return current_link_up;
3876}
3877
3878static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
3879{
3880 int current_link_up = 0;
3881
Michael Chan5cf64b8a2007-05-05 12:11:21 -07003882 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884
3885 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08003886 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003888
Matt Carlson5be73b42007-12-20 20:09:29 -08003889 if (fiber_autoneg(tp, &txflags, &rxflags)) {
3890 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891
Matt Carlson5be73b42007-12-20 20:09:29 -08003892 if (txflags & ANEG_CFG_PS1)
3893 local_adv |= ADVERTISE_1000XPAUSE;
3894 if (txflags & ANEG_CFG_PS2)
3895 local_adv |= ADVERTISE_1000XPSE_ASYM;
3896
3897 if (rxflags & MR_LP_ADV_SYM_PAUSE)
3898 remote_adv |= LPA_1000XPAUSE;
3899 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
3900 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901
3902 tg3_setup_flow_control(tp, local_adv, remote_adv);
3903
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 current_link_up = 1;
3905 }
3906 for (i = 0; i < 30; i++) {
3907 udelay(20);
3908 tw32_f(MAC_STATUS,
3909 (MAC_STATUS_SYNC_CHANGED |
3910 MAC_STATUS_CFG_CHANGED));
3911 udelay(40);
3912 if ((tr32(MAC_STATUS) &
3913 (MAC_STATUS_SYNC_CHANGED |
3914 MAC_STATUS_CFG_CHANGED)) == 0)
3915 break;
3916 }
3917
3918 mac_status = tr32(MAC_STATUS);
3919 if (current_link_up == 0 &&
3920 (mac_status & MAC_STATUS_PCS_SYNCED) &&
3921 !(mac_status & MAC_STATUS_RCVD_CFG))
3922 current_link_up = 1;
3923 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08003924 tg3_setup_flow_control(tp, 0, 0);
3925
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 /* Forcing 1000FD link up. */
3927 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928
3929 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
3930 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003931
3932 tw32_f(MAC_MODE, tp->mac_mode);
3933 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 }
3935
3936out:
3937 return current_link_up;
3938}
3939
3940static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
3941{
3942 u32 orig_pause_cfg;
3943 u16 orig_active_speed;
3944 u8 orig_active_duplex;
3945 u32 mac_status;
3946 int current_link_up;
3947 int i;
3948
Matt Carlson8d018622007-12-20 20:05:44 -08003949 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 orig_active_speed = tp->link_config.active_speed;
3951 orig_active_duplex = tp->link_config.active_duplex;
3952
3953 if (!(tp->tg3_flags2 & TG3_FLG2_HW_AUTONEG) &&
3954 netif_carrier_ok(tp->dev) &&
3955 (tp->tg3_flags & TG3_FLAG_INIT_COMPLETE)) {
3956 mac_status = tr32(MAC_STATUS);
3957 mac_status &= (MAC_STATUS_PCS_SYNCED |
3958 MAC_STATUS_SIGNAL_DET |
3959 MAC_STATUS_CFG_CHANGED |
3960 MAC_STATUS_RCVD_CFG);
3961 if (mac_status == (MAC_STATUS_PCS_SYNCED |
3962 MAC_STATUS_SIGNAL_DET)) {
3963 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
3964 MAC_STATUS_CFG_CHANGED));
3965 return 0;
3966 }
3967 }
3968
3969 tw32_f(MAC_TX_AUTO_NEG, 0);
3970
3971 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
3972 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
3973 tw32_f(MAC_MODE, tp->mac_mode);
3974 udelay(40);
3975
Matt Carlson79eb6902010-02-17 15:17:03 +00003976 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 tg3_init_bcm8002(tp);
3978
3979 /* Enable link change event even when serdes polling. */
3980 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
3981 udelay(40);
3982
3983 current_link_up = 0;
3984 mac_status = tr32(MAC_STATUS);
3985
3986 if (tp->tg3_flags2 & TG3_FLG2_HW_AUTONEG)
3987 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
3988 else
3989 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
3990
Matt Carlson898a56f2009-08-28 14:02:40 +00003991 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00003993 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994
3995 for (i = 0; i < 100; i++) {
3996 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
3997 MAC_STATUS_CFG_CHANGED));
3998 udelay(5);
3999 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07004000 MAC_STATUS_CFG_CHANGED |
4001 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 break;
4003 }
4004
4005 mac_status = tr32(MAC_STATUS);
4006 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
4007 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004008 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
4009 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 tw32_f(MAC_MODE, (tp->mac_mode |
4011 MAC_MODE_SEND_CONFIGS));
4012 udelay(1);
4013 tw32_f(MAC_MODE, tp->mac_mode);
4014 }
4015 }
4016
4017 if (current_link_up == 1) {
4018 tp->link_config.active_speed = SPEED_1000;
4019 tp->link_config.active_duplex = DUPLEX_FULL;
4020 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4021 LED_CTRL_LNKLED_OVERRIDE |
4022 LED_CTRL_1000MBPS_ON));
4023 } else {
4024 tp->link_config.active_speed = SPEED_INVALID;
4025 tp->link_config.active_duplex = DUPLEX_INVALID;
4026 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4027 LED_CTRL_LNKLED_OVERRIDE |
4028 LED_CTRL_TRAFFIC_OVERRIDE));
4029 }
4030
4031 if (current_link_up != netif_carrier_ok(tp->dev)) {
4032 if (current_link_up)
4033 netif_carrier_on(tp->dev);
4034 else
4035 netif_carrier_off(tp->dev);
4036 tg3_link_report(tp);
4037 } else {
Matt Carlson8d018622007-12-20 20:05:44 -08004038 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 if (orig_pause_cfg != now_pause_cfg ||
4040 orig_active_speed != tp->link_config.active_speed ||
4041 orig_active_duplex != tp->link_config.active_duplex)
4042 tg3_link_report(tp);
4043 }
4044
4045 return 0;
4046}
4047
Michael Chan747e8f82005-07-25 12:33:22 -07004048static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
4049{
4050 int current_link_up, err = 0;
4051 u32 bmsr, bmcr;
4052 u16 current_speed;
4053 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08004054 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07004055
4056 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4057 tw32_f(MAC_MODE, tp->mac_mode);
4058 udelay(40);
4059
4060 tw32(MAC_EVENT, 0);
4061
4062 tw32_f(MAC_STATUS,
4063 (MAC_STATUS_SYNC_CHANGED |
4064 MAC_STATUS_CFG_CHANGED |
4065 MAC_STATUS_MI_COMPLETION |
4066 MAC_STATUS_LNKSTATE_CHANGED));
4067 udelay(40);
4068
4069 if (force_reset)
4070 tg3_phy_reset(tp);
4071
4072 current_link_up = 0;
4073 current_speed = SPEED_INVALID;
4074 current_duplex = DUPLEX_INVALID;
4075
4076 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4077 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004078 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
4079 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4080 bmsr |= BMSR_LSTATUS;
4081 else
4082 bmsr &= ~BMSR_LSTATUS;
4083 }
Michael Chan747e8f82005-07-25 12:33:22 -07004084
4085 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
4086
4087 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004088 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004089 /* do nothing, just check for link up at the end */
4090 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4091 u32 adv, new_adv;
4092
4093 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4094 new_adv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
4095 ADVERTISE_1000XPAUSE |
4096 ADVERTISE_1000XPSE_ASYM |
4097 ADVERTISE_SLCT);
4098
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004099 new_adv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Michael Chan747e8f82005-07-25 12:33:22 -07004100
4101 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
4102 new_adv |= ADVERTISE_1000XHALF;
4103 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
4104 new_adv |= ADVERTISE_1000XFULL;
4105
4106 if ((new_adv != adv) || !(bmcr & BMCR_ANENABLE)) {
4107 tg3_writephy(tp, MII_ADVERTISE, new_adv);
4108 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
4109 tg3_writephy(tp, MII_BMCR, bmcr);
4110
4111 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07004112 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004113 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004114
4115 return err;
4116 }
4117 } else {
4118 u32 new_bmcr;
4119
4120 bmcr &= ~BMCR_SPEED1000;
4121 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
4122
4123 if (tp->link_config.duplex == DUPLEX_FULL)
4124 new_bmcr |= BMCR_FULLDPLX;
4125
4126 if (new_bmcr != bmcr) {
4127 /* BMCR_SPEED1000 is a reserved bit that needs
4128 * to be set on write.
4129 */
4130 new_bmcr |= BMCR_SPEED1000;
4131
4132 /* Force a linkdown */
4133 if (netif_carrier_ok(tp->dev)) {
4134 u32 adv;
4135
4136 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4137 adv &= ~(ADVERTISE_1000XFULL |
4138 ADVERTISE_1000XHALF |
4139 ADVERTISE_SLCT);
4140 tg3_writephy(tp, MII_ADVERTISE, adv);
4141 tg3_writephy(tp, MII_BMCR, bmcr |
4142 BMCR_ANRESTART |
4143 BMCR_ANENABLE);
4144 udelay(10);
4145 netif_carrier_off(tp->dev);
4146 }
4147 tg3_writephy(tp, MII_BMCR, new_bmcr);
4148 bmcr = new_bmcr;
4149 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4150 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004151 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
4152 ASIC_REV_5714) {
4153 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4154 bmsr |= BMSR_LSTATUS;
4155 else
4156 bmsr &= ~BMSR_LSTATUS;
4157 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004158 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004159 }
4160 }
4161
4162 if (bmsr & BMSR_LSTATUS) {
4163 current_speed = SPEED_1000;
4164 current_link_up = 1;
4165 if (bmcr & BMCR_FULLDPLX)
4166 current_duplex = DUPLEX_FULL;
4167 else
4168 current_duplex = DUPLEX_HALF;
4169
Matt Carlsonef167e22007-12-20 20:10:01 -08004170 local_adv = 0;
4171 remote_adv = 0;
4172
Michael Chan747e8f82005-07-25 12:33:22 -07004173 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08004174 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07004175
4176 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
4177 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
4178 common = local_adv & remote_adv;
4179 if (common & (ADVERTISE_1000XHALF |
4180 ADVERTISE_1000XFULL)) {
4181 if (common & ADVERTISE_1000XFULL)
4182 current_duplex = DUPLEX_FULL;
4183 else
4184 current_duplex = DUPLEX_HALF;
Matt Carlson57d8b882010-06-05 17:24:35 +00004185 } else if (!(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
4186 /* Link is up via parallel detect */
Matt Carlson859a588792010-04-05 10:19:28 +00004187 } else {
Michael Chan747e8f82005-07-25 12:33:22 -07004188 current_link_up = 0;
Matt Carlson859a588792010-04-05 10:19:28 +00004189 }
Michael Chan747e8f82005-07-25 12:33:22 -07004190 }
4191 }
4192
Matt Carlsonef167e22007-12-20 20:10:01 -08004193 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
4194 tg3_setup_flow_control(tp, local_adv, remote_adv);
4195
Michael Chan747e8f82005-07-25 12:33:22 -07004196 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4197 if (tp->link_config.active_duplex == DUPLEX_HALF)
4198 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4199
4200 tw32_f(MAC_MODE, tp->mac_mode);
4201 udelay(40);
4202
4203 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4204
4205 tp->link_config.active_speed = current_speed;
4206 tp->link_config.active_duplex = current_duplex;
4207
4208 if (current_link_up != netif_carrier_ok(tp->dev)) {
4209 if (current_link_up)
4210 netif_carrier_on(tp->dev);
4211 else {
4212 netif_carrier_off(tp->dev);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004213 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004214 }
4215 tg3_link_report(tp);
4216 }
4217 return err;
4218}
4219
4220static void tg3_serdes_parallel_detect(struct tg3 *tp)
4221{
Michael Chan3d3ebe72006-09-27 15:59:15 -07004222 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07004223 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07004224 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07004225 return;
4226 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00004227
Michael Chan747e8f82005-07-25 12:33:22 -07004228 if (!netif_carrier_ok(tp->dev) &&
4229 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
4230 u32 bmcr;
4231
4232 tg3_readphy(tp, MII_BMCR, &bmcr);
4233 if (bmcr & BMCR_ANENABLE) {
4234 u32 phy1, phy2;
4235
4236 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004237 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
4238 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07004239
4240 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004241 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
4242 MII_TG3_DSP_EXP1_INT_STAT);
4243 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
4244 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07004245
4246 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
4247 /* We have signal detect and not receiving
4248 * config code words, link is up by parallel
4249 * detection.
4250 */
4251
4252 bmcr &= ~BMCR_ANENABLE;
4253 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
4254 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004255 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004256 }
4257 }
Matt Carlson859a588792010-04-05 10:19:28 +00004258 } else if (netif_carrier_ok(tp->dev) &&
4259 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004260 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004261 u32 phy2;
4262
4263 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004264 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
4265 MII_TG3_DSP_EXP1_INT_STAT);
4266 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07004267 if (phy2 & 0x20) {
4268 u32 bmcr;
4269
4270 /* Config code words received, turn on autoneg. */
4271 tg3_readphy(tp, MII_BMCR, &bmcr);
4272 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
4273
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004274 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004275
4276 }
4277 }
4278}
4279
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280static int tg3_setup_phy(struct tg3 *tp, int force_reset)
4281{
4282 int err;
4283
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004284 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004286 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07004287 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a588792010-04-05 10:19:28 +00004288 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290
Matt Carlsonbcb37f62008-11-03 16:52:09 -08004291 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08004292 u32 val, scale;
4293
4294 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
4295 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
4296 scale = 65;
4297 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
4298 scale = 6;
4299 else
4300 scale = 12;
4301
4302 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
4303 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
4304 tw32(GRC_MISC_CFG, val);
4305 }
4306
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 if (tp->link_config.active_speed == SPEED_1000 &&
4308 tp->link_config.active_duplex == DUPLEX_HALF)
4309 tw32(MAC_TX_LENGTHS,
4310 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4311 (6 << TX_LENGTHS_IPG_SHIFT) |
4312 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
4313 else
4314 tw32(MAC_TX_LENGTHS,
4315 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4316 (6 << TX_LENGTHS_IPG_SHIFT) |
4317 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
4318
4319 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
4320 if (netif_carrier_ok(tp->dev)) {
4321 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07004322 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 } else {
4324 tw32(HOSTCC_STAT_COAL_TICKS, 0);
4325 }
4326 }
4327
Matt Carlson8ed5d972007-05-07 00:25:49 -07004328 if (tp->tg3_flags & TG3_FLAG_ASPM_WORKAROUND) {
4329 u32 val = tr32(PCIE_PWR_MGMT_THRESH);
4330 if (!netif_carrier_ok(tp->dev))
4331 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
4332 tp->pwrmgmt_thresh;
4333 else
4334 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
4335 tw32(PCIE_PWR_MGMT_THRESH, val);
4336 }
4337
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 return err;
4339}
4340
Michael Chandf3e6542006-05-26 17:48:07 -07004341/* This is called whenever we suspect that the system chipset is re-
4342 * ordering the sequence of MMIO to the tx send mailbox. The symptom
4343 * is bogus tx completions. We try to recover by setting the
4344 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
4345 * in the workqueue.
4346 */
4347static void tg3_tx_recover(struct tg3 *tp)
4348{
4349 BUG_ON((tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER) ||
4350 tp->write32_tx_mbox == tg3_write_indirect_mbox);
4351
Matt Carlson5129c3a2010-04-05 10:19:23 +00004352 netdev_warn(tp->dev,
4353 "The system may be re-ordering memory-mapped I/O "
4354 "cycles to the network device, attempting to recover. "
4355 "Please report the problem to the driver maintainer "
4356 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07004357
4358 spin_lock(&tp->lock);
Michael Chandf3e6542006-05-26 17:48:07 -07004359 tp->tg3_flags |= TG3_FLAG_TX_RECOVERY_PENDING;
Michael Chandf3e6542006-05-26 17:48:07 -07004360 spin_unlock(&tp->lock);
4361}
4362
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004363static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07004364{
Matt Carlsonf65aac12010-08-02 11:26:03 +00004365 /* Tell compiler to fetch tx indices from memory. */
4366 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004367 return tnapi->tx_pending -
4368 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07004369}
4370
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371/* Tigon3 never reports partial packet sends. So we do not
4372 * need special logic to handle SKBs that have not had all
4373 * of their frags sent yet, like SunGEM does.
4374 */
Matt Carlson17375d22009-08-28 14:02:18 +00004375static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376{
Matt Carlson17375d22009-08-28 14:02:18 +00004377 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00004378 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004379 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004380 struct netdev_queue *txq;
4381 int index = tnapi - tp->napi;
4382
Matt Carlson19cfaec2009-12-03 08:36:20 +00004383 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004384 index--;
4385
4386 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387
4388 while (sw_idx != hw_idx) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00004389 struct ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07004391 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392
Michael Chandf3e6542006-05-26 17:48:07 -07004393 if (unlikely(skb == NULL)) {
4394 tg3_tx_recover(tp);
4395 return;
4396 }
4397
Alexander Duyckf4188d82009-12-02 16:48:38 +00004398 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004399 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00004400 skb_headlen(skb),
4401 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402
4403 ri->skb = NULL;
4404
4405 sw_idx = NEXT_TX(sw_idx);
4406
4407 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004408 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07004409 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
4410 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00004411
4412 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004413 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00004414 skb_shinfo(skb)->frags[i].size,
4415 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 sw_idx = NEXT_TX(sw_idx);
4417 }
4418
David S. Millerf47c11e2005-06-24 20:18:35 -07004419 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07004420
4421 if (unlikely(tx_bug)) {
4422 tg3_tx_recover(tp);
4423 return;
4424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 }
4426
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004427 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428
Michael Chan1b2a7202006-08-07 21:46:02 -07004429 /* Need to make the tx_cons update visible to tg3_start_xmit()
4430 * before checking for netif_queue_stopped(). Without the
4431 * memory barrier, there is a small possibility that tg3_start_xmit()
4432 * will miss it and cause the queue to be stopped forever.
4433 */
4434 smp_mb();
4435
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004436 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004437 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004438 __netif_tx_lock(txq, smp_processor_id());
4439 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004440 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004441 netif_tx_wake_queue(txq);
4442 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07004443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444}
4445
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004446static void tg3_rx_skb_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
4447{
4448 if (!ri->skb)
4449 return;
4450
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004451 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004452 map_sz, PCI_DMA_FROMDEVICE);
4453 dev_kfree_skb_any(ri->skb);
4454 ri->skb = NULL;
4455}
4456
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457/* Returns size of skb allocated or < 0 on error.
4458 *
4459 * We only need to fill in the address because the other members
4460 * of the RX descriptor are invariant, see tg3_init_rings.
4461 *
4462 * Note the purposeful assymetry of cpu vs. chip accesses. For
4463 * posting buffers we only dirty the first cache line of the RX
4464 * descriptor (containing the address). Whereas for the RX status
4465 * buffers the cpu only reads the last cacheline of the RX descriptor
4466 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
4467 */
Matt Carlson86b21e52009-11-13 13:03:45 +00004468static int tg3_alloc_rx_skb(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Matt Carlsona3896162009-11-13 13:03:44 +00004469 u32 opaque_key, u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470{
4471 struct tg3_rx_buffer_desc *desc;
4472 struct ring_info *map, *src_map;
4473 struct sk_buff *skb;
4474 dma_addr_t mapping;
4475 int skb_size, dest_idx;
4476
4477 src_map = NULL;
4478 switch (opaque_key) {
4479 case RXD_OPAQUE_RING_STD:
4480 dest_idx = dest_idx_unmasked % TG3_RX_RING_SIZE;
Matt Carlson21f581a2009-08-28 14:00:25 +00004481 desc = &tpr->rx_std[dest_idx];
4482 map = &tpr->rx_std_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004483 skb_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 break;
4485
4486 case RXD_OPAQUE_RING_JUMBO:
4487 dest_idx = dest_idx_unmasked % TG3_RX_JUMBO_RING_SIZE;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00004488 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00004489 map = &tpr->rx_jmb_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004490 skb_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 break;
4492
4493 default:
4494 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496
4497 /* Do not overwrite any of the map or rp information
4498 * until we are sure we can commit to a new buffer.
4499 *
4500 * Callers depend upon this behavior and assume that
4501 * we leave everything unchanged if we fail.
4502 */
Matt Carlson287be122009-08-28 13:58:46 +00004503 skb = netdev_alloc_skb(tp->dev, skb_size + tp->rx_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504 if (skb == NULL)
4505 return -ENOMEM;
4506
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 skb_reserve(skb, tp->rx_offset);
4508
Matt Carlson287be122009-08-28 13:58:46 +00004509 mapping = pci_map_single(tp->pdev, skb->data, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 PCI_DMA_FROMDEVICE);
Matt Carlsona21771d2009-11-02 14:25:31 +00004511 if (pci_dma_mapping_error(tp->pdev, mapping)) {
4512 dev_kfree_skb(skb);
4513 return -EIO;
4514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515
4516 map->skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004517 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 desc->addr_hi = ((u64)mapping >> 32);
4520 desc->addr_lo = ((u64)mapping & 0xffffffff);
4521
4522 return skb_size;
4523}
4524
4525/* We only need to move over in the address because the other
4526 * members of the RX descriptor are invariant. See notes above
4527 * tg3_alloc_rx_skb for full details.
4528 */
Matt Carlsona3896162009-11-13 13:03:44 +00004529static void tg3_recycle_rx(struct tg3_napi *tnapi,
4530 struct tg3_rx_prodring_set *dpr,
4531 u32 opaque_key, int src_idx,
4532 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533{
Matt Carlson17375d22009-08-28 14:02:18 +00004534 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
4536 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00004537 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00004538 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539
4540 switch (opaque_key) {
4541 case RXD_OPAQUE_RING_STD:
4542 dest_idx = dest_idx_unmasked % TG3_RX_RING_SIZE;
Matt Carlsona3896162009-11-13 13:03:44 +00004543 dest_desc = &dpr->rx_std[dest_idx];
4544 dest_map = &dpr->rx_std_buffers[dest_idx];
4545 src_desc = &spr->rx_std[src_idx];
4546 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 break;
4548
4549 case RXD_OPAQUE_RING_JUMBO:
4550 dest_idx = dest_idx_unmasked % TG3_RX_JUMBO_RING_SIZE;
Matt Carlsona3896162009-11-13 13:03:44 +00004551 dest_desc = &dpr->rx_jmb[dest_idx].std;
4552 dest_map = &dpr->rx_jmb_buffers[dest_idx];
4553 src_desc = &spr->rx_jmb[src_idx].std;
4554 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555 break;
4556
4557 default:
4558 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004559 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560
4561 dest_map->skb = src_map->skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004562 dma_unmap_addr_set(dest_map, mapping,
4563 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 dest_desc->addr_hi = src_desc->addr_hi;
4565 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00004566
4567 /* Ensure that the update to the skb happens after the physical
4568 * addresses have been transferred to the new BD location.
4569 */
4570 smp_wmb();
4571
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 src_map->skb = NULL;
4573}
4574
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575/* The RX ring scheme is composed of multiple rings which post fresh
4576 * buffers to the chip, and one special ring the chip uses to report
4577 * status back to the host.
4578 *
4579 * The special ring reports the status of received packets to the
4580 * host. The chip does not write into the original descriptor the
4581 * RX buffer was obtained from. The chip simply takes the original
4582 * descriptor as provided by the host, updates the status and length
4583 * field, then writes this into the next status ring entry.
4584 *
4585 * Each ring the host uses to post buffers to the chip is described
4586 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
4587 * it is first placed into the on-chip ram. When the packet's length
4588 * is known, it walks down the TG3_BDINFO entries to select the ring.
4589 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
4590 * which is within the range of the new packet's length is chosen.
4591 *
4592 * The "separate ring for rx status" scheme may sound queer, but it makes
4593 * sense from a cache coherency perspective. If only the host writes
4594 * to the buffer post rings, and only the chip writes to the rx status
4595 * rings, then cache lines never move beyond shared-modified state.
4596 * If both the host and chip were to write into the same ring, cache line
4597 * eviction could occur since both entities want it in an exclusive state.
4598 */
Matt Carlson17375d22009-08-28 14:02:18 +00004599static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600{
Matt Carlson17375d22009-08-28 14:02:18 +00004601 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07004602 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00004603 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00004604 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07004605 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00004607 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004609 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610 /*
4611 * We need to order the read of hw_idx and the read of
4612 * the opaque cookie.
4613 */
4614 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 work_mask = 0;
4616 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00004617 std_prod_idx = tpr->rx_std_prod_idx;
4618 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00004620 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00004621 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 unsigned int len;
4623 struct sk_buff *skb;
4624 dma_addr_t dma_addr;
4625 u32 opaque_key, desc_idx, *post_ptr;
Matt Carlson9dc7a112010-04-12 06:58:28 +00004626 bool hw_vlan __maybe_unused = false;
4627 u16 vtag __maybe_unused = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628
4629 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
4630 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
4631 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00004632 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004633 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00004634 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00004635 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07004636 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00004638 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004639 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00004640 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00004641 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00004642 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644
4645 work_mask |= opaque_key;
4646
4647 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
4648 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
4649 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00004650 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 desc_idx, *post_ptr);
4652 drop_it_no_recycle:
4653 /* Other statistics kept track of by card. */
4654 tp->net_stats.rx_dropped++;
4655 goto next_pkt;
4656 }
4657
Matt Carlsonad829262008-11-21 17:16:16 -08004658 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
4659 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660
Matt Carlsond2757fc2010-04-12 06:58:27 +00004661 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662 int skb_size;
4663
Matt Carlson86b21e52009-11-13 13:03:45 +00004664 skb_size = tg3_alloc_rx_skb(tp, tpr, opaque_key,
Matt Carlsonafc081f2009-11-13 13:03:43 +00004665 *post_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 if (skb_size < 0)
4667 goto drop_it;
4668
Matt Carlson287be122009-08-28 13:58:46 +00004669 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670 PCI_DMA_FROMDEVICE);
4671
Matt Carlson61e800c2010-02-17 15:16:54 +00004672 /* Ensure that the update to the skb happens
4673 * after the usage of the old DMA mapping.
4674 */
4675 smp_wmb();
4676
4677 ri->skb = NULL;
4678
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 skb_put(skb, len);
4680 } else {
4681 struct sk_buff *copy_skb;
4682
Matt Carlsona3896162009-11-13 13:03:44 +00004683 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 desc_idx, *post_ptr);
4685
Matt Carlson9dc7a112010-04-12 06:58:28 +00004686 copy_skb = netdev_alloc_skb(tp->dev, len + VLAN_HLEN +
4687 TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 if (copy_skb == NULL)
4689 goto drop_it_no_recycle;
4690
Matt Carlson9dc7a112010-04-12 06:58:28 +00004691 skb_reserve(copy_skb, TG3_RAW_IP_ALIGN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 skb_put(copy_skb, len);
4693 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03004694 skb_copy_from_linear_data(skb, copy_skb->data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
4696
4697 /* We'll reuse the original ring buffer. */
4698 skb = copy_skb;
4699 }
4700
4701 if ((tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) &&
4702 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
4703 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
4704 >> RXD_TCPCSUM_SHIFT) == 0xffff))
4705 skb->ip_summed = CHECKSUM_UNNECESSARY;
4706 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07004707 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708
4709 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00004710
4711 if (len > (tp->dev->mtu + ETH_HLEN) &&
4712 skb->protocol != htons(ETH_P_8021Q)) {
4713 dev_kfree_skb(skb);
4714 goto next_pkt;
4715 }
4716
Matt Carlson9dc7a112010-04-12 06:58:28 +00004717 if (desc->type_flags & RXD_FLAG_VLAN &&
4718 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG)) {
4719 vtag = desc->err_vlan & RXD_VLAN_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720#if TG3_VLAN_TAG_USED
Matt Carlson9dc7a112010-04-12 06:58:28 +00004721 if (tp->vlgrp)
4722 hw_vlan = true;
4723 else
4724#endif
4725 {
4726 struct vlan_ethhdr *ve = (struct vlan_ethhdr *)
4727 __skb_push(skb, VLAN_HLEN);
4728
4729 memmove(ve, skb->data + VLAN_HLEN,
4730 ETH_ALEN * 2);
4731 ve->h_vlan_proto = htons(ETH_P_8021Q);
4732 ve->h_vlan_TCI = htons(vtag);
4733 }
4734 }
4735
4736#if TG3_VLAN_TAG_USED
4737 if (hw_vlan)
4738 vlan_gro_receive(&tnapi->napi, tp->vlgrp, vtag, skb);
4739 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740#endif
Matt Carlson17375d22009-08-28 14:02:18 +00004741 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743 received++;
4744 budget--;
4745
4746next_pkt:
4747 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07004748
4749 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson86cfe4f2010-01-12 10:11:37 +00004750 tpr->rx_std_prod_idx = std_prod_idx % TG3_RX_RING_SIZE;
4751 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
4752 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07004753 work_mask &= ~RXD_OPAQUE_RING_STD;
4754 rx_std_posted = 0;
4755 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07004757 sw_idx++;
Eric Dumazet6b31a512007-02-06 13:29:21 -08004758 sw_idx &= (TG3_RX_RCB_RING_SIZE(tp) - 1);
Michael Chan52f6d692005-04-25 15:14:32 -07004759
4760 /* Refresh hw_idx to see if there is new work */
4761 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004762 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07004763 rmb();
4764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765 }
4766
4767 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00004768 tnapi->rx_rcb_ptr = sw_idx;
4769 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770
4771 /* Refill RX ring(s). */
Matt Carlsone4af1af2010-02-12 14:47:05 +00004772 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004773 if (work_mask & RXD_OPAQUE_RING_STD) {
4774 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);
4777 }
4778 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
4779 tpr->rx_jmb_prod_idx = jmb_prod_idx %
4780 TG3_RX_JUMBO_RING_SIZE;
4781 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
4782 tpr->rx_jmb_prod_idx);
4783 }
4784 mmiowb();
4785 } else if (work_mask) {
4786 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
4787 * updated before the producer indices can be updated.
4788 */
4789 smp_wmb();
4790
Matt Carlson43619352009-11-13 13:03:47 +00004791 tpr->rx_std_prod_idx = std_prod_idx % TG3_RX_RING_SIZE;
Matt Carlson43619352009-11-13 13:03:47 +00004792 tpr->rx_jmb_prod_idx = jmb_prod_idx % TG3_RX_JUMBO_RING_SIZE;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004793
Matt Carlsone4af1af2010-02-12 14:47:05 +00004794 if (tnapi != &tp->napi[1])
4795 napi_schedule(&tp->napi[1].napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797
4798 return received;
4799}
4800
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004801static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803 /* handle link change and other phy events */
4804 if (!(tp->tg3_flags &
4805 (TG3_FLAG_USE_LINKCHG_REG |
4806 TG3_FLAG_POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004807 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
4808
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809 if (sblk->status & SD_STATUS_LINK_CHG) {
4810 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004811 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07004812 spin_lock(&tp->lock);
Matt Carlsondd477002008-05-25 23:45:58 -07004813 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
4814 tw32_f(MAC_STATUS,
4815 (MAC_STATUS_SYNC_CHANGED |
4816 MAC_STATUS_CFG_CHANGED |
4817 MAC_STATUS_MI_COMPLETION |
4818 MAC_STATUS_LNKSTATE_CHANGED));
4819 udelay(40);
4820 } else
4821 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07004822 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823 }
4824 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004825}
4826
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004827static int tg3_rx_prodring_xfer(struct tg3 *tp,
4828 struct tg3_rx_prodring_set *dpr,
4829 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004830{
4831 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004832 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004833
4834 while (1) {
4835 src_prod_idx = spr->rx_std_prod_idx;
4836
4837 /* Make sure updates to the rx_std_buffers[] entries and the
4838 * standard producer index are seen in the correct order.
4839 */
4840 smp_rmb();
4841
4842 if (spr->rx_std_cons_idx == src_prod_idx)
4843 break;
4844
4845 if (spr->rx_std_cons_idx < src_prod_idx)
4846 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
4847 else
4848 cpycnt = TG3_RX_RING_SIZE - spr->rx_std_cons_idx;
4849
4850 cpycnt = min(cpycnt, TG3_RX_RING_SIZE - dpr->rx_std_prod_idx);
4851
4852 si = spr->rx_std_cons_idx;
4853 di = dpr->rx_std_prod_idx;
4854
Matt Carlsone92967b2010-02-12 14:47:06 +00004855 for (i = di; i < di + cpycnt; i++) {
4856 if (dpr->rx_std_buffers[i].skb) {
4857 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004858 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00004859 break;
4860 }
4861 }
4862
4863 if (!cpycnt)
4864 break;
4865
4866 /* Ensure that updates to the rx_std_buffers ring and the
4867 * shadowed hardware producer ring from tg3_recycle_skb() are
4868 * ordered correctly WRT the skb check above.
4869 */
4870 smp_rmb();
4871
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004872 memcpy(&dpr->rx_std_buffers[di],
4873 &spr->rx_std_buffers[si],
4874 cpycnt * sizeof(struct ring_info));
4875
4876 for (i = 0; i < cpycnt; i++, di++, si++) {
4877 struct tg3_rx_buffer_desc *sbd, *dbd;
4878 sbd = &spr->rx_std[si];
4879 dbd = &dpr->rx_std[di];
4880 dbd->addr_hi = sbd->addr_hi;
4881 dbd->addr_lo = sbd->addr_lo;
4882 }
4883
4884 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) %
4885 TG3_RX_RING_SIZE;
4886 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) %
4887 TG3_RX_RING_SIZE;
4888 }
4889
4890 while (1) {
4891 src_prod_idx = spr->rx_jmb_prod_idx;
4892
4893 /* Make sure updates to the rx_jmb_buffers[] entries and
4894 * the jumbo producer index are seen in the correct order.
4895 */
4896 smp_rmb();
4897
4898 if (spr->rx_jmb_cons_idx == src_prod_idx)
4899 break;
4900
4901 if (spr->rx_jmb_cons_idx < src_prod_idx)
4902 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
4903 else
4904 cpycnt = TG3_RX_JUMBO_RING_SIZE - spr->rx_jmb_cons_idx;
4905
4906 cpycnt = min(cpycnt,
4907 TG3_RX_JUMBO_RING_SIZE - dpr->rx_jmb_prod_idx);
4908
4909 si = spr->rx_jmb_cons_idx;
4910 di = dpr->rx_jmb_prod_idx;
4911
Matt Carlsone92967b2010-02-12 14:47:06 +00004912 for (i = di; i < di + cpycnt; i++) {
4913 if (dpr->rx_jmb_buffers[i].skb) {
4914 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004915 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00004916 break;
4917 }
4918 }
4919
4920 if (!cpycnt)
4921 break;
4922
4923 /* Ensure that updates to the rx_jmb_buffers ring and the
4924 * shadowed hardware producer ring from tg3_recycle_skb() are
4925 * ordered correctly WRT the skb check above.
4926 */
4927 smp_rmb();
4928
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004929 memcpy(&dpr->rx_jmb_buffers[di],
4930 &spr->rx_jmb_buffers[si],
4931 cpycnt * sizeof(struct ring_info));
4932
4933 for (i = 0; i < cpycnt; i++, di++, si++) {
4934 struct tg3_rx_buffer_desc *sbd, *dbd;
4935 sbd = &spr->rx_jmb[si].std;
4936 dbd = &dpr->rx_jmb[di].std;
4937 dbd->addr_hi = sbd->addr_hi;
4938 dbd->addr_lo = sbd->addr_lo;
4939 }
4940
4941 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) %
4942 TG3_RX_JUMBO_RING_SIZE;
4943 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) %
4944 TG3_RX_JUMBO_RING_SIZE;
4945 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004946
4947 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004948}
4949
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004950static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
4951{
4952 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953
4954 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004955 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00004956 tg3_tx(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07004957 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
Michael Chan4fd7ab52007-10-12 01:39:50 -07004958 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959 }
4960
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 /* run RX thread, within the bounds set by NAPI.
4962 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004963 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004965 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00004966 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004968 if ((tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00004969 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004970 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00004971 u32 std_prod_idx = dpr->rx_std_prod_idx;
4972 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004973
Matt Carlsone4af1af2010-02-12 14:47:05 +00004974 for (i = 1; i < tp->irq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004975 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00004976 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004977
4978 wmb();
4979
Matt Carlsone4af1af2010-02-12 14:47:05 +00004980 if (std_prod_idx != dpr->rx_std_prod_idx)
4981 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
4982 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004983
Matt Carlsone4af1af2010-02-12 14:47:05 +00004984 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
4985 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
4986 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004987
4988 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004989
4990 if (err)
4991 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004992 }
4993
David S. Miller6f535762007-10-11 18:08:29 -07004994 return work_done;
4995}
David S. Millerf7383c22005-05-18 22:50:53 -07004996
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004997static int tg3_poll_msix(struct napi_struct *napi, int budget)
4998{
4999 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5000 struct tg3 *tp = tnapi->tp;
5001 int work_done = 0;
5002 struct tg3_hw_status *sblk = tnapi->hw_status;
5003
5004 while (1) {
5005 work_done = tg3_poll_work(tnapi, work_done, budget);
5006
5007 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
5008 goto tx_recovery;
5009
5010 if (unlikely(work_done >= budget))
5011 break;
5012
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005013 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005014 * to tell the hw how much work has been processed,
5015 * so we must read it before checking for more work.
5016 */
5017 tnapi->last_tag = sblk->status_tag;
5018 tnapi->last_irq_tag = tnapi->last_tag;
5019 rmb();
5020
5021 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00005022 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
5023 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005024 napi_complete(napi);
5025 /* Reenable interrupts. */
5026 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
5027 mmiowb();
5028 break;
5029 }
5030 }
5031
5032 return work_done;
5033
5034tx_recovery:
5035 /* work_done is guaranteed to be less than budget. */
5036 napi_complete(napi);
5037 schedule_work(&tp->reset_task);
5038 return work_done;
5039}
5040
David S. Miller6f535762007-10-11 18:08:29 -07005041static int tg3_poll(struct napi_struct *napi, int budget)
5042{
Matt Carlson8ef04422009-08-28 14:01:37 +00005043 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5044 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07005045 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00005046 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07005047
5048 while (1) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005049 tg3_poll_link(tp);
5050
Matt Carlson17375d22009-08-28 14:02:18 +00005051 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07005052
5053 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
5054 goto tx_recovery;
5055
5056 if (unlikely(work_done >= budget))
5057 break;
5058
Michael Chan4fd7ab52007-10-12 01:39:50 -07005059 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) {
Matt Carlson17375d22009-08-28 14:02:18 +00005060 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07005061 * to tell the hw how much work has been processed,
5062 * so we must read it before checking for more work.
5063 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005064 tnapi->last_tag = sblk->status_tag;
5065 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07005066 rmb();
5067 } else
5068 sblk->status &= ~SD_STATUS_UPDATED;
5069
Matt Carlson17375d22009-08-28 14:02:18 +00005070 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005071 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00005072 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07005073 break;
5074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075 }
5076
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005077 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07005078
5079tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07005080 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08005081 napi_complete(napi);
David S. Miller6f535762007-10-11 18:08:29 -07005082 schedule_work(&tp->reset_task);
Michael Chan4fd7ab52007-10-12 01:39:50 -07005083 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084}
5085
David S. Millerf47c11e2005-06-24 20:18:35 -07005086static void tg3_irq_quiesce(struct tg3 *tp)
5087{
Matt Carlson4f125f42009-09-01 12:55:02 +00005088 int i;
5089
David S. Millerf47c11e2005-06-24 20:18:35 -07005090 BUG_ON(tp->irq_sync);
5091
5092 tp->irq_sync = 1;
5093 smp_mb();
5094
Matt Carlson4f125f42009-09-01 12:55:02 +00005095 for (i = 0; i < tp->irq_cnt; i++)
5096 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07005097}
5098
5099static inline int tg3_irq_sync(struct tg3 *tp)
5100{
5101 return tp->irq_sync;
5102}
5103
5104/* Fully shutdown all tg3 driver activity elsewhere in the system.
5105 * If irq_sync is non-zero, then the IRQ handler must be synchronized
5106 * with as well. Most of the time, this is not necessary except when
5107 * shutting down the device.
5108 */
5109static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
5110{
Michael Chan46966542007-07-11 19:47:19 -07005111 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07005112 if (irq_sync)
5113 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07005114}
5115
5116static inline void tg3_full_unlock(struct tg3 *tp)
5117{
David S. Millerf47c11e2005-06-24 20:18:35 -07005118 spin_unlock_bh(&tp->lock);
5119}
5120
Michael Chanfcfa0a32006-03-20 22:28:41 -08005121/* One-shot MSI handler - Chip automatically disables interrupt
5122 * after sending MSI so driver doesn't have to do it.
5123 */
David Howells7d12e782006-10-05 14:55:46 +01005124static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08005125{
Matt Carlson09943a12009-08-28 14:01:57 +00005126 struct tg3_napi *tnapi = dev_id;
5127 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08005128
Matt Carlson898a56f2009-08-28 14:02:40 +00005129 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005130 if (tnapi->rx_rcb)
5131 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005132
5133 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005134 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005135
5136 return IRQ_HANDLED;
5137}
5138
Michael Chan88b06bc22005-04-21 17:13:25 -07005139/* MSI ISR - No need to check for interrupt sharing and no need to
5140 * flush status block and interrupt mailbox. PCI ordering rules
5141 * guarantee that MSI will arrive after the status block.
5142 */
David Howells7d12e782006-10-05 14:55:46 +01005143static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07005144{
Matt Carlson09943a12009-08-28 14:01:57 +00005145 struct tg3_napi *tnapi = dev_id;
5146 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07005147
Matt Carlson898a56f2009-08-28 14:02:40 +00005148 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005149 if (tnapi->rx_rcb)
5150 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07005151 /*
David S. Millerfac9b832005-05-18 22:46:34 -07005152 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07005153 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07005154 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07005155 * NIC to stop sending us irqs, engaging "in-intr-handler"
5156 * event coalescing.
5157 */
5158 tw32_mailbox(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07005159 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005160 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07005161
Michael Chan88b06bc22005-04-21 17:13:25 -07005162 return IRQ_RETVAL(1);
5163}
5164
David Howells7d12e782006-10-05 14:55:46 +01005165static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166{
Matt Carlson09943a12009-08-28 14:01:57 +00005167 struct tg3_napi *tnapi = dev_id;
5168 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005169 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170 unsigned int handled = 1;
5171
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172 /* In INTx mode, it is possible for the interrupt to arrive at
5173 * the CPU before the status block posted prior to the interrupt.
5174 * Reading the PCI State register will confirm whether the
5175 * interrupt is ours and will flush the status block.
5176 */
Michael Chand18edcb2007-03-24 20:57:11 -07005177 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
5178 if ((tp->tg3_flags & TG3_FLAG_CHIP_RESETTING) ||
5179 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5180 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005181 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07005182 }
Michael Chand18edcb2007-03-24 20:57:11 -07005183 }
5184
5185 /*
5186 * Writing any value to intr-mbox-0 clears PCI INTA# and
5187 * chip-internal interrupt pending events.
5188 * Writing non-zero to intr-mbox-0 additional tells the
5189 * NIC to stop sending us irqs, engaging "in-intr-handler"
5190 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005191 *
5192 * Flush the mailbox to de-assert the IRQ immediately to prevent
5193 * spurious interrupts. The flush impacts performance but
5194 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005195 */
Michael Chanc04cb342007-05-07 00:26:15 -07005196 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07005197 if (tg3_irq_sync(tp))
5198 goto out;
5199 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00005200 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00005201 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00005202 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07005203 } else {
5204 /* No work, shared interrupt perhaps? re-enable
5205 * interrupts, and flush that PCI write
5206 */
5207 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
5208 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07005209 }
David S. Millerf47c11e2005-06-24 20:18:35 -07005210out:
David S. Millerfac9b832005-05-18 22:46:34 -07005211 return IRQ_RETVAL(handled);
5212}
5213
David Howells7d12e782006-10-05 14:55:46 +01005214static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07005215{
Matt Carlson09943a12009-08-28 14:01:57 +00005216 struct tg3_napi *tnapi = dev_id;
5217 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005218 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07005219 unsigned int handled = 1;
5220
David S. Millerfac9b832005-05-18 22:46:34 -07005221 /* In INTx mode, it is possible for the interrupt to arrive at
5222 * the CPU before the status block posted prior to the interrupt.
5223 * Reading the PCI State register will confirm whether the
5224 * interrupt is ours and will flush the status block.
5225 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005226 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Michael Chand18edcb2007-03-24 20:57:11 -07005227 if ((tp->tg3_flags & TG3_FLAG_CHIP_RESETTING) ||
5228 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5229 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005230 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231 }
Michael Chand18edcb2007-03-24 20:57:11 -07005232 }
5233
5234 /*
5235 * writing any value to intr-mbox-0 clears PCI INTA# and
5236 * chip-internal interrupt pending events.
5237 * writing non-zero to intr-mbox-0 additional tells the
5238 * NIC to stop sending us irqs, engaging "in-intr-handler"
5239 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005240 *
5241 * Flush the mailbox to de-assert the IRQ immediately to prevent
5242 * spurious interrupts. The flush impacts performance but
5243 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005244 */
Michael Chanc04cb342007-05-07 00:26:15 -07005245 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00005246
5247 /*
5248 * In a shared interrupt configuration, sometimes other devices'
5249 * interrupts will scream. We record the current status tag here
5250 * so that the above check can report that the screaming interrupts
5251 * are unhandled. Eventually they will be silenced.
5252 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005253 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00005254
Michael Chand18edcb2007-03-24 20:57:11 -07005255 if (tg3_irq_sync(tp))
5256 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00005257
Matt Carlson72334482009-08-28 14:03:01 +00005258 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00005259
Matt Carlson09943a12009-08-28 14:01:57 +00005260 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00005261
David S. Millerf47c11e2005-06-24 20:18:35 -07005262out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263 return IRQ_RETVAL(handled);
5264}
5265
Michael Chan79381092005-04-21 17:13:59 -07005266/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01005267static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07005268{
Matt Carlson09943a12009-08-28 14:01:57 +00005269 struct tg3_napi *tnapi = dev_id;
5270 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005271 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07005272
Michael Chanf9804dd2005-09-27 12:13:10 -07005273 if ((sblk->status & SD_STATUS_UPDATED) ||
5274 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07005275 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07005276 return IRQ_RETVAL(1);
5277 }
5278 return IRQ_RETVAL(0);
5279}
5280
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07005281static int tg3_init_hw(struct tg3 *, int);
Michael Chan944d9802005-05-29 14:57:48 -07005282static int tg3_halt(struct tg3 *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283
Michael Chanb9ec6c12006-07-25 16:37:27 -07005284/* Restart hardware after configuration changes, self-test, etc.
5285 * Invoked with tp->lock held.
5286 */
5287static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
Eric Dumazet78c61462008-04-24 23:33:06 -07005288 __releases(tp->lock)
5289 __acquires(tp->lock)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005290{
5291 int err;
5292
5293 err = tg3_init_hw(tp, reset_phy);
5294 if (err) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00005295 netdev_err(tp->dev,
5296 "Failed to re-initialize device, aborting\n");
Michael Chanb9ec6c12006-07-25 16:37:27 -07005297 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
5298 tg3_full_unlock(tp);
5299 del_timer_sync(&tp->timer);
5300 tp->irq_sync = 0;
Matt Carlsonfed97812009-09-01 13:10:19 +00005301 tg3_napi_enable(tp);
Michael Chanb9ec6c12006-07-25 16:37:27 -07005302 dev_close(tp->dev);
5303 tg3_full_lock(tp, 0);
5304 }
5305 return err;
5306}
5307
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308#ifdef CONFIG_NET_POLL_CONTROLLER
5309static void tg3_poll_controller(struct net_device *dev)
5310{
Matt Carlson4f125f42009-09-01 12:55:02 +00005311 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07005312 struct tg3 *tp = netdev_priv(dev);
5313
Matt Carlson4f125f42009-09-01 12:55:02 +00005314 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00005315 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316}
5317#endif
5318
David Howellsc4028952006-11-22 14:57:56 +00005319static void tg3_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320{
David Howellsc4028952006-11-22 14:57:56 +00005321 struct tg3 *tp = container_of(work, struct tg3, reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005322 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323 unsigned int restart_timer;
5324
Michael Chan7faa0062006-02-02 17:29:28 -08005325 tg3_full_lock(tp, 0);
Michael Chan7faa0062006-02-02 17:29:28 -08005326
5327 if (!netif_running(tp->dev)) {
Michael Chan7faa0062006-02-02 17:29:28 -08005328 tg3_full_unlock(tp);
5329 return;
5330 }
5331
5332 tg3_full_unlock(tp);
5333
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005334 tg3_phy_stop(tp);
5335
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336 tg3_netif_stop(tp);
5337
David S. Millerf47c11e2005-06-24 20:18:35 -07005338 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339
5340 restart_timer = tp->tg3_flags2 & TG3_FLG2_RESTART_TIMER;
5341 tp->tg3_flags2 &= ~TG3_FLG2_RESTART_TIMER;
5342
Michael Chandf3e6542006-05-26 17:48:07 -07005343 if (tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING) {
5344 tp->write32_tx_mbox = tg3_write32_tx_mbox;
5345 tp->write32_rx_mbox = tg3_write_flush_reg32;
5346 tp->tg3_flags |= TG3_FLAG_MBOX_WRITE_REORDER;
5347 tp->tg3_flags &= ~TG3_FLAG_TX_RECOVERY_PENDING;
5348 }
5349
Michael Chan944d9802005-05-29 14:57:48 -07005350 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005351 err = tg3_init_hw(tp, 1);
5352 if (err)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005353 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354
5355 tg3_netif_start(tp);
5356
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357 if (restart_timer)
5358 mod_timer(&tp->timer, jiffies + 1);
Michael Chan7faa0062006-02-02 17:29:28 -08005359
Michael Chanb9ec6c12006-07-25 16:37:27 -07005360out:
Michael Chan7faa0062006-02-02 17:29:28 -08005361 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005362
5363 if (!err)
5364 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365}
5366
Michael Chanb0408752007-02-13 12:18:30 -08005367static void tg3_dump_short_state(struct tg3 *tp)
5368{
Joe Perches05dbe002010-02-17 19:44:19 +00005369 netdev_err(tp->dev, "DEBUG: MAC_TX_STATUS[%08x] MAC_RX_STATUS[%08x]\n",
5370 tr32(MAC_TX_STATUS), tr32(MAC_RX_STATUS));
5371 netdev_err(tp->dev, "DEBUG: RDMAC_STATUS[%08x] WDMAC_STATUS[%08x]\n",
5372 tr32(RDMAC_STATUS), tr32(WDMAC_STATUS));
Michael Chanb0408752007-02-13 12:18:30 -08005373}
5374
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375static void tg3_tx_timeout(struct net_device *dev)
5376{
5377 struct tg3 *tp = netdev_priv(dev);
5378
Michael Chanb0408752007-02-13 12:18:30 -08005379 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00005380 netdev_err(dev, "transmit timed out, resetting\n");
Michael Chanb0408752007-02-13 12:18:30 -08005381 tg3_dump_short_state(tp);
5382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383
5384 schedule_work(&tp->reset_task);
5385}
5386
Michael Chanc58ec932005-09-17 00:46:27 -07005387/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
5388static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
5389{
5390 u32 base = (u32) mapping & 0xffffffff;
5391
Eric Dumazet807540b2010-09-23 05:40:09 +00005392 return (base > 0xffffdcc0) && (base + len + 8 < base);
Michael Chanc58ec932005-09-17 00:46:27 -07005393}
5394
Michael Chan72f2afb2006-03-06 19:28:35 -08005395/* Test for DMA addresses > 40-bit */
5396static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
5397 int len)
5398{
5399#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Michael Chan6728a8e2006-03-27 23:16:49 -08005400 if (tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG)
Eric Dumazet807540b2010-09-23 05:40:09 +00005401 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08005402 return 0;
5403#else
5404 return 0;
5405#endif
5406}
5407
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005408static void tg3_set_txd(struct tg3_napi *, int, dma_addr_t, int, u32, u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409
Michael Chan72f2afb2006-03-06 19:28:35 -08005410/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00005411static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
5412 struct sk_buff *skb, u32 last_plus_one,
5413 u32 *start, u32 base_flags, u32 mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414{
Matt Carlson24f4efd2009-11-13 13:03:35 +00005415 struct tg3 *tp = tnapi->tp;
Matt Carlson41588ba2008-04-19 18:12:33 -07005416 struct sk_buff *new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07005417 dma_addr_t new_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418 u32 entry = *start;
Michael Chanc58ec932005-09-17 00:46:27 -07005419 int i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420
Matt Carlson41588ba2008-04-19 18:12:33 -07005421 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
5422 new_skb = skb_copy(skb, GFP_ATOMIC);
5423 else {
5424 int more_headroom = 4 - ((unsigned long)skb->data & 3);
5425
5426 new_skb = skb_copy_expand(skb,
5427 skb_headroom(skb) + more_headroom,
5428 skb_tailroom(skb), GFP_ATOMIC);
5429 }
5430
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07005432 ret = -1;
5433 } else {
5434 /* New SKB is guaranteed to be linear. */
5435 entry = *start;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005436 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
5437 PCI_DMA_TODEVICE);
5438 /* Make sure the mapping succeeded */
5439 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
5440 ret = -1;
5441 dev_kfree_skb(new_skb);
5442 new_skb = NULL;
David S. Miller90079ce2008-09-11 04:52:51 -07005443
Michael Chanc58ec932005-09-17 00:46:27 -07005444 /* Make sure new skb does not cross any 4G boundaries.
5445 * Drop the packet if it does.
5446 */
Alexander Duyckf4188d82009-12-02 16:48:38 +00005447 } else if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
5448 tg3_4g_overflow_test(new_addr, new_skb->len)) {
5449 pci_unmap_single(tp->pdev, new_addr, new_skb->len,
5450 PCI_DMA_TODEVICE);
Michael Chanc58ec932005-09-17 00:46:27 -07005451 ret = -1;
5452 dev_kfree_skb(new_skb);
5453 new_skb = NULL;
5454 } else {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005455 tg3_set_txd(tnapi, entry, new_addr, new_skb->len,
Michael Chanc58ec932005-09-17 00:46:27 -07005456 base_flags, 1 | (mss << 1));
5457 *start = NEXT_TX(entry);
5458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459 }
5460
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461 /* Now clean up the sw ring entries. */
5462 i = 0;
5463 while (entry != last_plus_one) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00005464 int len;
5465
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005466 if (i == 0)
Alexander Duyckf4188d82009-12-02 16:48:38 +00005467 len = skb_headlen(skb);
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005468 else
Alexander Duyckf4188d82009-12-02 16:48:38 +00005469 len = skb_shinfo(skb)->frags[i-1].size;
5470
5471 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005472 dma_unmap_addr(&tnapi->tx_buffers[entry],
Alexander Duyckf4188d82009-12-02 16:48:38 +00005473 mapping),
5474 len, PCI_DMA_TODEVICE);
5475 if (i == 0) {
5476 tnapi->tx_buffers[entry].skb = new_skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005477 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00005478 new_addr);
5479 } else {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005480 tnapi->tx_buffers[entry].skb = NULL;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482 entry = NEXT_TX(entry);
5483 i++;
5484 }
5485
5486 dev_kfree_skb(skb);
5487
Michael Chanc58ec932005-09-17 00:46:27 -07005488 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489}
5490
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005491static void tg3_set_txd(struct tg3_napi *tnapi, int entry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492 dma_addr_t mapping, int len, u32 flags,
5493 u32 mss_and_is_end)
5494{
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005495 struct tg3_tx_buffer_desc *txd = &tnapi->tx_ring[entry];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496 int is_end = (mss_and_is_end & 0x1);
5497 u32 mss = (mss_and_is_end >> 1);
5498 u32 vlan_tag = 0;
5499
5500 if (is_end)
5501 flags |= TXD_FLAG_END;
5502 if (flags & TXD_FLAG_VLAN) {
5503 vlan_tag = flags >> 16;
5504 flags &= 0xffff;
5505 }
5506 vlan_tag |= (mss << TXD_MSS_SHIFT);
5507
5508 txd->addr_hi = ((u64) mapping >> 32);
5509 txd->addr_lo = ((u64) mapping & 0xffffffff);
5510 txd->len_flags = (len << TXD_LEN_SHIFT) | flags;
5511 txd->vlan_tag = vlan_tag << TXD_VLAN_TAG_SHIFT;
5512}
5513
Michael Chan5a6f3072006-03-20 22:28:05 -08005514/* hard_start_xmit for devices that don't have any bugs and
Matt Carlsone849cdc2009-11-13 13:03:38 +00005515 * support TG3_FLG2_HW_TSO_2 and TG3_FLG2_HW_TSO_3 only.
Michael Chan5a6f3072006-03-20 22:28:05 -08005516 */
Stephen Hemminger613573252009-08-31 19:50:58 +00005517static netdev_tx_t tg3_start_xmit(struct sk_buff *skb,
5518 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519{
5520 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521 u32 len, entry, base_flags, mss;
David S. Miller90079ce2008-09-11 04:52:51 -07005522 dma_addr_t mapping;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005523 struct tg3_napi *tnapi;
5524 struct netdev_queue *txq;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005525 unsigned int i, last;
5526
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005527 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
5528 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Matt Carlson19cfaec2009-12-03 08:36:20 +00005529 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005530 tnapi++;
Michael Chan5a6f3072006-03-20 22:28:05 -08005531
Michael Chan00b70502006-06-17 21:58:45 -07005532 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005533 * and TX reclaim runs via tp->napi.poll inside of a software
Michael Chan5a6f3072006-03-20 22:28:05 -08005534 * interrupt. Furthermore, IRQ processing runs lockless so we have
5535 * no IRQ context deadlocks to worry about either. Rejoice!
5536 */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005537 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005538 if (!netif_tx_queue_stopped(txq)) {
5539 netif_tx_stop_queue(txq);
Michael Chan5a6f3072006-03-20 22:28:05 -08005540
5541 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00005542 netdev_err(dev,
5543 "BUG! Tx Ring full when queue awake!\n");
Michael Chan5a6f3072006-03-20 22:28:05 -08005544 }
Michael Chan5a6f3072006-03-20 22:28:05 -08005545 return NETDEV_TX_BUSY;
5546 }
5547
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005548 entry = tnapi->tx_prod;
Michael Chan5a6f3072006-03-20 22:28:05 -08005549 base_flags = 0;
Matt Carlsonbe98da62010-07-11 09:31:46 +00005550 mss = skb_shinfo(skb)->gso_size;
5551 if (mss) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005552 int tcp_opt_len, ip_tcp_len;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005553 u32 hdrlen;
Michael Chan5a6f3072006-03-20 22:28:05 -08005554
5555 if (skb_header_cloned(skb) &&
5556 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
5557 dev_kfree_skb(skb);
5558 goto out_unlock;
5559 }
5560
Matt Carlson02e96082010-09-15 08:59:59 +00005561 if (skb_is_gso_v6(skb)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005562 hdrlen = skb_headlen(skb) - ETH_HLEN;
Matt Carlson02e96082010-09-15 08:59:59 +00005563 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005564 struct iphdr *iph = ip_hdr(skb);
5565
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07005566 tcp_opt_len = tcp_optlen(skb);
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -03005567 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
Michael Chanb0026622006-07-03 19:42:14 -07005568
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005569 iph->check = 0;
5570 iph->tot_len = htons(mss + ip_tcp_len + tcp_opt_len);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005571 hdrlen = ip_tcp_len + tcp_opt_len;
Michael Chanb0026622006-07-03 19:42:14 -07005572 }
Michael Chan5a6f3072006-03-20 22:28:05 -08005573
Matt Carlsone849cdc2009-11-13 13:03:38 +00005574 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005575 mss |= (hdrlen & 0xc) << 12;
5576 if (hdrlen & 0x10)
5577 base_flags |= 0x00000010;
5578 base_flags |= (hdrlen & 0x3e0) << 5;
5579 } else
5580 mss |= hdrlen << 9;
5581
Michael Chan5a6f3072006-03-20 22:28:05 -08005582 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
5583 TXD_FLAG_CPU_POST_DMA);
5584
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005585 tcp_hdr(skb)->check = 0;
Michael Chan5a6f3072006-03-20 22:28:05 -08005586
Matt Carlson859a588792010-04-05 10:19:28 +00005587 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005588 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson859a588792010-04-05 10:19:28 +00005589 }
5590
Michael Chan5a6f3072006-03-20 22:28:05 -08005591#if TG3_VLAN_TAG_USED
5592 if (tp->vlgrp != NULL && vlan_tx_tag_present(skb))
5593 base_flags |= (TXD_FLAG_VLAN |
5594 (vlan_tx_tag_get(skb) << 16));
5595#endif
5596
Alexander Duyckf4188d82009-12-02 16:48:38 +00005597 len = skb_headlen(skb);
5598
5599 /* Queue skb data, a.k.a. the main skb fragment. */
5600 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
5601 if (pci_dma_mapping_error(tp->pdev, mapping)) {
David S. Miller90079ce2008-09-11 04:52:51 -07005602 dev_kfree_skb(skb);
5603 goto out_unlock;
5604 }
5605
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005606 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005607 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005608
Matt Carlsonb703df62009-12-03 08:36:21 +00005609 if ((tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005610 !mss && skb->len > ETH_DATA_LEN)
5611 base_flags |= TXD_FLAG_JMB_PKT;
5612
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005613 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
Michael Chan5a6f3072006-03-20 22:28:05 -08005614 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
5615
5616 entry = NEXT_TX(entry);
5617
5618 /* Now loop through additional data fragments, and queue them. */
5619 if (skb_shinfo(skb)->nr_frags > 0) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005620 last = skb_shinfo(skb)->nr_frags - 1;
5621 for (i = 0; i <= last; i++) {
5622 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5623
5624 len = frag->size;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005625 mapping = pci_map_page(tp->pdev,
5626 frag->page,
5627 frag->page_offset,
5628 len, PCI_DMA_TODEVICE);
5629 if (pci_dma_mapping_error(tp->pdev, mapping))
5630 goto dma_error;
5631
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005632 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005633 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00005634 mapping);
Michael Chan5a6f3072006-03-20 22:28:05 -08005635
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005636 tg3_set_txd(tnapi, entry, mapping, len,
Michael Chan5a6f3072006-03-20 22:28:05 -08005637 base_flags, (i == last) | (mss << 1));
5638
5639 entry = NEXT_TX(entry);
5640 }
5641 }
5642
5643 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005644 tw32_tx_mbox(tnapi->prodmbox, entry);
Michael Chan5a6f3072006-03-20 22:28:05 -08005645
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005646 tnapi->tx_prod = entry;
5647 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005648 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00005649
5650 /* netif_tx_stop_queue() must be done before checking
5651 * checking tx index in tg3_tx_avail() below, because in
5652 * tg3_tx(), we update tx index before checking for
5653 * netif_tx_queue_stopped().
5654 */
5655 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005656 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005657 netif_tx_wake_queue(txq);
Michael Chan5a6f3072006-03-20 22:28:05 -08005658 }
5659
5660out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00005661 mmiowb();
Michael Chan5a6f3072006-03-20 22:28:05 -08005662
5663 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005664
5665dma_error:
5666 last = i;
5667 entry = tnapi->tx_prod;
5668 tnapi->tx_buffers[entry].skb = NULL;
5669 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005670 dma_unmap_addr(&tnapi->tx_buffers[entry], mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005671 skb_headlen(skb),
5672 PCI_DMA_TODEVICE);
5673 for (i = 0; i <= last; i++) {
5674 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5675 entry = NEXT_TX(entry);
5676
5677 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005678 dma_unmap_addr(&tnapi->tx_buffers[entry],
Alexander Duyckf4188d82009-12-02 16:48:38 +00005679 mapping),
5680 frag->size, PCI_DMA_TODEVICE);
5681 }
5682
5683 dev_kfree_skb(skb);
5684 return NETDEV_TX_OK;
Michael Chan5a6f3072006-03-20 22:28:05 -08005685}
5686
Stephen Hemminger613573252009-08-31 19:50:58 +00005687static netdev_tx_t tg3_start_xmit_dma_bug(struct sk_buff *,
5688 struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07005689
5690/* Use GSO to workaround a rare TSO bug that may be triggered when the
5691 * TSO header is greater than 80 bytes.
5692 */
5693static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
5694{
5695 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005696 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07005697
5698 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005699 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07005700 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00005701
5702 /* netif_tx_stop_queue() must be done before checking
5703 * checking tx index in tg3_tx_avail() below, because in
5704 * tg3_tx(), we update tx index before checking for
5705 * netif_tx_queue_stopped().
5706 */
5707 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005708 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08005709 return NETDEV_TX_BUSY;
5710
5711 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07005712 }
5713
5714 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07005715 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07005716 goto tg3_tso_bug_end;
5717
5718 do {
5719 nskb = segs;
5720 segs = segs->next;
5721 nskb->next = NULL;
5722 tg3_start_xmit_dma_bug(nskb, tp->dev);
5723 } while (segs);
5724
5725tg3_tso_bug_end:
5726 dev_kfree_skb(skb);
5727
5728 return NETDEV_TX_OK;
5729}
Michael Chan52c0fd82006-06-29 20:15:54 -07005730
Michael Chan5a6f3072006-03-20 22:28:05 -08005731/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
5732 * support TG3_FLG2_HW_TSO_1 or firmware TSO only.
5733 */
Stephen Hemminger613573252009-08-31 19:50:58 +00005734static netdev_tx_t tg3_start_xmit_dma_bug(struct sk_buff *skb,
5735 struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08005736{
5737 struct tg3 *tp = netdev_priv(dev);
Michael Chan5a6f3072006-03-20 22:28:05 -08005738 u32 len, entry, base_flags, mss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739 int would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07005740 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00005741 struct tg3_napi *tnapi;
5742 struct netdev_queue *txq;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005743 unsigned int i, last;
5744
Matt Carlson24f4efd2009-11-13 13:03:35 +00005745 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
5746 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Matt Carlson19cfaec2009-12-03 08:36:20 +00005747 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlson24f4efd2009-11-13 13:03:35 +00005748 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749
Michael Chan00b70502006-06-17 21:58:45 -07005750 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005751 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07005752 * interrupt. Furthermore, IRQ processing runs lockless so we have
5753 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07005754 */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005755 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00005756 if (!netif_tx_queue_stopped(txq)) {
5757 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08005758
5759 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00005760 netdev_err(dev,
5761 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08005762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763 return NETDEV_TX_BUSY;
5764 }
5765
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005766 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07005768 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00005770
Matt Carlsonbe98da62010-07-11 09:31:46 +00005771 mss = skb_shinfo(skb)->gso_size;
5772 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005773 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00005774 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775
5776 if (skb_header_cloned(skb) &&
5777 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
5778 dev_kfree_skb(skb);
5779 goto out_unlock;
5780 }
5781
Matt Carlson34195c32010-07-11 09:31:42 +00005782 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07005783 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784
Matt Carlson02e96082010-09-15 08:59:59 +00005785 if (skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00005786 hdr_len = skb_headlen(skb) - ETH_HLEN;
5787 } else {
5788 u32 ip_tcp_len;
5789
5790 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
5791 hdr_len = ip_tcp_len + tcp_opt_len;
5792
5793 iph->check = 0;
5794 iph->tot_len = htons(mss + hdr_len);
5795 }
5796
Michael Chan52c0fd82006-06-29 20:15:54 -07005797 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Michael Chan7f62ad52007-02-20 23:25:40 -08005798 (tp->tg3_flags2 & TG3_FLG2_TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00005799 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07005800
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
5802 TXD_FLAG_CPU_POST_DMA);
5803
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005805 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005807 } else
5808 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
5809 iph->daddr, 0,
5810 IPPROTO_TCP,
5811 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005812
Matt Carlson615774f2009-11-13 13:03:39 +00005813 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) {
5814 mss |= (hdr_len & 0xc) << 12;
5815 if (hdr_len & 0x10)
5816 base_flags |= 0x00000010;
5817 base_flags |= (hdr_len & 0x3e0) << 5;
5818 } else if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_2)
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005819 mss |= hdr_len << 9;
5820 else if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_1) ||
5821 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005822 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005823 int tsflags;
5824
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005825 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826 mss |= (tsflags << 11);
5827 }
5828 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005829 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005830 int tsflags;
5831
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005832 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005833 base_flags |= tsflags << 12;
5834 }
5835 }
5836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005837#if TG3_VLAN_TAG_USED
5838 if (tp->vlgrp != NULL && vlan_tx_tag_present(skb))
5839 base_flags |= (TXD_FLAG_VLAN |
5840 (vlan_tx_tag_get(skb) << 16));
5841#endif
5842
Matt Carlsonb703df62009-12-03 08:36:21 +00005843 if ((tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) &&
Matt Carlson615774f2009-11-13 13:03:39 +00005844 !mss && skb->len > ETH_DATA_LEN)
5845 base_flags |= TXD_FLAG_JMB_PKT;
5846
Alexander Duyckf4188d82009-12-02 16:48:38 +00005847 len = skb_headlen(skb);
5848
5849 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
5850 if (pci_dma_mapping_error(tp->pdev, mapping)) {
David S. Miller90079ce2008-09-11 04:52:51 -07005851 dev_kfree_skb(skb);
5852 goto out_unlock;
5853 }
5854
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005855 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005856 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857
5858 would_hit_hwbug = 0;
5859
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005860 if ((tp->tg3_flags3 & TG3_FLG3_SHORT_DMA_BUG) && len <= 8)
5861 would_hit_hwbug = 1;
5862
Matt Carlson0e1406d2009-11-02 12:33:33 +00005863 if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
5864 tg3_4g_overflow_test(mapping, len))
Matt Carlson41588ba2008-04-19 18:12:33 -07005865 would_hit_hwbug = 1;
Matt Carlson0e1406d2009-11-02 12:33:33 +00005866
5867 if ((tp->tg3_flags3 & TG3_FLG3_40BIT_DMA_LIMIT_BUG) &&
5868 tg3_40bit_overflow_test(tp, mapping, len))
5869 would_hit_hwbug = 1;
5870
5871 if (tp->tg3_flags3 & TG3_FLG3_5701_DMA_BUG)
Michael Chanc58ec932005-09-17 00:46:27 -07005872 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005874 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005875 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
5876
5877 entry = NEXT_TX(entry);
5878
5879 /* Now loop through additional data fragments, and queue them. */
5880 if (skb_shinfo(skb)->nr_frags > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005881 last = skb_shinfo(skb)->nr_frags - 1;
5882 for (i = 0; i <= last; i++) {
5883 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5884
5885 len = frag->size;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005886 mapping = pci_map_page(tp->pdev,
5887 frag->page,
5888 frag->page_offset,
5889 len, PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005890
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005891 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005892 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00005893 mapping);
5894 if (pci_dma_mapping_error(tp->pdev, mapping))
5895 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005896
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005897 if ((tp->tg3_flags3 & TG3_FLG3_SHORT_DMA_BUG) &&
5898 len <= 8)
5899 would_hit_hwbug = 1;
5900
Matt Carlson0e1406d2009-11-02 12:33:33 +00005901 if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
5902 tg3_4g_overflow_test(mapping, len))
Michael Chanc58ec932005-09-17 00:46:27 -07005903 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904
Matt Carlson0e1406d2009-11-02 12:33:33 +00005905 if ((tp->tg3_flags3 & TG3_FLG3_40BIT_DMA_LIMIT_BUG) &&
5906 tg3_40bit_overflow_test(tp, mapping, len))
Michael Chan72f2afb2006-03-06 19:28:35 -08005907 would_hit_hwbug = 1;
5908
Linus Torvalds1da177e2005-04-16 15:20:36 -07005909 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005910 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005911 base_flags, (i == last)|(mss << 1));
5912 else
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005913 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914 base_flags, (i == last));
5915
5916 entry = NEXT_TX(entry);
5917 }
5918 }
5919
5920 if (would_hit_hwbug) {
5921 u32 last_plus_one = entry;
5922 u32 start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923
Michael Chanc58ec932005-09-17 00:46:27 -07005924 start = entry - 1 - skb_shinfo(skb)->nr_frags;
5925 start &= (TG3_TX_RING_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005926
5927 /* If the workaround fails due to memory/mapping
5928 * failure, silently drop this packet.
5929 */
Matt Carlson24f4efd2009-11-13 13:03:35 +00005930 if (tigon3_dma_hwbug_workaround(tnapi, skb, last_plus_one,
Michael Chanc58ec932005-09-17 00:46:27 -07005931 &start, base_flags, mss))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005932 goto out_unlock;
5933
5934 entry = start;
5935 }
5936
5937 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00005938 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005940 tnapi->tx_prod = entry;
5941 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00005942 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00005943
5944 /* netif_tx_stop_queue() must be done before checking
5945 * checking tx index in tg3_tx_avail() below, because in
5946 * tg3_tx(), we update tx index before checking for
5947 * netif_tx_queue_stopped().
5948 */
5949 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005950 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00005951 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07005952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005953
5954out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00005955 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005956
5957 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005958
5959dma_error:
5960 last = i;
5961 entry = tnapi->tx_prod;
5962 tnapi->tx_buffers[entry].skb = NULL;
5963 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005964 dma_unmap_addr(&tnapi->tx_buffers[entry], mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005965 skb_headlen(skb),
5966 PCI_DMA_TODEVICE);
5967 for (i = 0; i <= last; i++) {
5968 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5969 entry = NEXT_TX(entry);
5970
5971 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005972 dma_unmap_addr(&tnapi->tx_buffers[entry],
Alexander Duyckf4188d82009-12-02 16:48:38 +00005973 mapping),
5974 frag->size, PCI_DMA_TODEVICE);
5975 }
5976
5977 dev_kfree_skb(skb);
5978 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005979}
5980
5981static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
5982 int new_mtu)
5983{
5984 dev->mtu = new_mtu;
5985
Michael Chanef7f5ec2005-07-25 12:32:25 -07005986 if (new_mtu > ETH_DATA_LEN) {
Michael Chana4e2b342005-10-26 15:46:52 -07005987 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
Michael Chanef7f5ec2005-07-25 12:32:25 -07005988 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
5989 ethtool_op_set_tso(dev, 0);
Matt Carlson859a588792010-04-05 10:19:28 +00005990 } else {
Michael Chanef7f5ec2005-07-25 12:32:25 -07005991 tp->tg3_flags |= TG3_FLAG_JUMBO_RING_ENABLE;
Matt Carlson859a588792010-04-05 10:19:28 +00005992 }
Michael Chanef7f5ec2005-07-25 12:32:25 -07005993 } else {
Michael Chana4e2b342005-10-26 15:46:52 -07005994 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
Michael Chanef7f5ec2005-07-25 12:32:25 -07005995 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
Michael Chan0f893dc2005-07-25 12:30:38 -07005996 tp->tg3_flags &= ~TG3_FLAG_JUMBO_RING_ENABLE;
Michael Chanef7f5ec2005-07-25 12:32:25 -07005997 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998}
5999
6000static int tg3_change_mtu(struct net_device *dev, int new_mtu)
6001{
6002 struct tg3 *tp = netdev_priv(dev);
Michael Chanb9ec6c12006-07-25 16:37:27 -07006003 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006004
6005 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
6006 return -EINVAL;
6007
6008 if (!netif_running(dev)) {
6009 /* We'll just catch it later when the
6010 * device is up'd.
6011 */
6012 tg3_set_mtu(dev, tp, new_mtu);
6013 return 0;
6014 }
6015
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006016 tg3_phy_stop(tp);
6017
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018 tg3_netif_stop(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07006019
6020 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006021
Michael Chan944d9802005-05-29 14:57:48 -07006022 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006023
6024 tg3_set_mtu(dev, tp, new_mtu);
6025
Michael Chanb9ec6c12006-07-25 16:37:27 -07006026 err = tg3_restart_hw(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027
Michael Chanb9ec6c12006-07-25 16:37:27 -07006028 if (!err)
6029 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030
David S. Millerf47c11e2005-06-24 20:18:35 -07006031 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006032
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006033 if (!err)
6034 tg3_phy_start(tp);
6035
Michael Chanb9ec6c12006-07-25 16:37:27 -07006036 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006037}
6038
Matt Carlson21f581a2009-08-28 14:00:25 +00006039static void tg3_rx_prodring_free(struct tg3 *tp,
6040 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006041{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006042 int i;
6043
Matt Carlson8fea32b2010-09-15 08:59:58 +00006044 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006045 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
6046 i = (i + 1) % TG3_RX_RING_SIZE)
6047 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6048 tp->rx_pkt_map_sz);
6049
6050 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
6051 for (i = tpr->rx_jmb_cons_idx;
6052 i != tpr->rx_jmb_prod_idx;
6053 i = (i + 1) % TG3_RX_JUMBO_RING_SIZE) {
6054 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6055 TG3_RX_JMB_MAP_SZ);
6056 }
6057 }
6058
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006059 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006061
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006062 for (i = 0; i < TG3_RX_RING_SIZE; i++)
6063 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6064 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006065
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006066 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006067 for (i = 0; i < TG3_RX_JUMBO_RING_SIZE; i++)
6068 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6069 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070 }
6071}
6072
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006073/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006074 *
6075 * The chip has been shut down and the driver detached from
6076 * the networking, so no interrupts or new tx packets will
6077 * end up in the driver. tp->{tx,}lock are held and thus
6078 * we may not sleep.
6079 */
Matt Carlson21f581a2009-08-28 14:00:25 +00006080static int tg3_rx_prodring_alloc(struct tg3 *tp,
6081 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082{
Matt Carlson287be122009-08-28 13:58:46 +00006083 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006085 tpr->rx_std_cons_idx = 0;
6086 tpr->rx_std_prod_idx = 0;
6087 tpr->rx_jmb_cons_idx = 0;
6088 tpr->rx_jmb_prod_idx = 0;
6089
Matt Carlson8fea32b2010-09-15 08:59:58 +00006090 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006091 memset(&tpr->rx_std_buffers[0], 0, TG3_RX_STD_BUFF_RING_SIZE);
6092 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE)
6093 memset(&tpr->rx_jmb_buffers[0], 0,
6094 TG3_RX_JMB_BUFF_RING_SIZE);
6095 goto done;
6096 }
6097
Linus Torvalds1da177e2005-04-16 15:20:36 -07006098 /* Zero out all descriptors. */
Matt Carlson21f581a2009-08-28 14:00:25 +00006099 memset(tpr->rx_std, 0, TG3_RX_RING_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006100
Matt Carlson287be122009-08-28 13:58:46 +00006101 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Michael Chana4e2b342005-10-26 15:46:52 -07006102 if ((tp->tg3_flags2 & TG3_FLG2_5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00006103 tp->dev->mtu > ETH_DATA_LEN)
6104 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
6105 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07006106
Linus Torvalds1da177e2005-04-16 15:20:36 -07006107 /* Initialize invariants of the rings, we only set this
6108 * stuff once. This works because the card does not
6109 * write into the rx buffer posting rings.
6110 */
6111 for (i = 0; i < TG3_RX_RING_SIZE; i++) {
6112 struct tg3_rx_buffer_desc *rxd;
6113
Matt Carlson21f581a2009-08-28 14:00:25 +00006114 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00006115 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006116 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
6117 rxd->opaque = (RXD_OPAQUE_RING_STD |
6118 (i << RXD_OPAQUE_INDEX_SHIFT));
6119 }
6120
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006121 /* Now allocate fresh SKBs for each rx ring. */
6122 for (i = 0; i < tp->rx_pending; i++) {
Matt Carlson86b21e52009-11-13 13:03:45 +00006123 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_STD, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006124 netdev_warn(tp->dev,
6125 "Using a smaller RX standard ring. Only "
6126 "%d out of %d buffers were allocated "
6127 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006128 if (i == 0)
6129 goto initfail;
6130 tp->rx_pending = i;
6131 break;
6132 }
6133 }
6134
6135 if (!(tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE))
6136 goto done;
6137
Matt Carlson21f581a2009-08-28 14:00:25 +00006138 memset(tpr->rx_jmb, 0, TG3_RX_JUMBO_RING_BYTES);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006139
Matt Carlson0d86df82010-02-17 15:17:00 +00006140 if (!(tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE))
6141 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142
Matt Carlson0d86df82010-02-17 15:17:00 +00006143 for (i = 0; i < TG3_RX_JUMBO_RING_SIZE; i++) {
6144 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006145
Matt Carlson0d86df82010-02-17 15:17:00 +00006146 rxd = &tpr->rx_jmb[i].std;
6147 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
6148 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
6149 RXD_FLAG_JUMBO;
6150 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
6151 (i << RXD_OPAQUE_INDEX_SHIFT));
6152 }
6153
6154 for (i = 0; i < tp->rx_jumbo_pending; i++) {
6155 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_JUMBO, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006156 netdev_warn(tp->dev,
6157 "Using a smaller RX jumbo ring. Only %d "
6158 "out of %d buffers were allocated "
6159 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00006160 if (i == 0)
6161 goto initfail;
6162 tp->rx_jumbo_pending = i;
6163 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006164 }
6165 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006166
6167done:
Michael Chan32d8c572006-07-25 16:38:29 -07006168 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006169
6170initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00006171 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006172 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006173}
6174
Matt Carlson21f581a2009-08-28 14:00:25 +00006175static void tg3_rx_prodring_fini(struct tg3 *tp,
6176 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006177{
Matt Carlson21f581a2009-08-28 14:00:25 +00006178 kfree(tpr->rx_std_buffers);
6179 tpr->rx_std_buffers = NULL;
6180 kfree(tpr->rx_jmb_buffers);
6181 tpr->rx_jmb_buffers = NULL;
6182 if (tpr->rx_std) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006183 pci_free_consistent(tp->pdev, TG3_RX_RING_BYTES,
Matt Carlson21f581a2009-08-28 14:00:25 +00006184 tpr->rx_std, tpr->rx_std_mapping);
6185 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006186 }
Matt Carlson21f581a2009-08-28 14:00:25 +00006187 if (tpr->rx_jmb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006188 pci_free_consistent(tp->pdev, TG3_RX_JUMBO_RING_BYTES,
Matt Carlson21f581a2009-08-28 14:00:25 +00006189 tpr->rx_jmb, tpr->rx_jmb_mapping);
6190 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006191 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006192}
6193
Matt Carlson21f581a2009-08-28 14:00:25 +00006194static int tg3_rx_prodring_init(struct tg3 *tp,
6195 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006196{
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006197 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE, GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006198 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006199 return -ENOMEM;
6200
Matt Carlson21f581a2009-08-28 14:00:25 +00006201 tpr->rx_std = pci_alloc_consistent(tp->pdev, TG3_RX_RING_BYTES,
6202 &tpr->rx_std_mapping);
6203 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006204 goto err_out;
6205
6206 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006207 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE,
Matt Carlson21f581a2009-08-28 14:00:25 +00006208 GFP_KERNEL);
6209 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006210 goto err_out;
6211
Matt Carlson21f581a2009-08-28 14:00:25 +00006212 tpr->rx_jmb = pci_alloc_consistent(tp->pdev,
6213 TG3_RX_JUMBO_RING_BYTES,
6214 &tpr->rx_jmb_mapping);
6215 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006216 goto err_out;
6217 }
6218
6219 return 0;
6220
6221err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00006222 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006223 return -ENOMEM;
6224}
6225
6226/* Free up pending packets in all rx/tx rings.
6227 *
6228 * The chip has been shut down and the driver detached from
6229 * the networking, so no interrupts or new tx packets will
6230 * end up in the driver. tp->{tx,}lock is not held and we are not
6231 * in an interrupt context and thus may sleep.
6232 */
6233static void tg3_free_rings(struct tg3 *tp)
6234{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006235 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006236
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006237 for (j = 0; j < tp->irq_cnt; j++) {
6238 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006239
Matt Carlson8fea32b2010-09-15 08:59:58 +00006240 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00006241
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006242 if (!tnapi->tx_buffers)
6243 continue;
6244
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006245 for (i = 0; i < TG3_TX_RING_SIZE; ) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00006246 struct ring_info *txp;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006247 struct sk_buff *skb;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006248 unsigned int k;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006249
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006250 txp = &tnapi->tx_buffers[i];
6251 skb = txp->skb;
6252
6253 if (skb == NULL) {
6254 i++;
6255 continue;
6256 }
6257
Alexander Duyckf4188d82009-12-02 16:48:38 +00006258 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006259 dma_unmap_addr(txp, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006260 skb_headlen(skb),
6261 PCI_DMA_TODEVICE);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006262 txp->skb = NULL;
6263
Alexander Duyckf4188d82009-12-02 16:48:38 +00006264 i++;
6265
6266 for (k = 0; k < skb_shinfo(skb)->nr_frags; k++) {
6267 txp = &tnapi->tx_buffers[i & (TG3_TX_RING_SIZE - 1)];
6268 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006269 dma_unmap_addr(txp, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006270 skb_shinfo(skb)->frags[k].size,
6271 PCI_DMA_TODEVICE);
6272 i++;
6273 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006274
6275 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006276 }
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006277 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006278}
6279
6280/* Initialize tx/rx rings for packet processing.
6281 *
6282 * The chip has been shut down and the driver detached from
6283 * the networking, so no interrupts or new tx packets will
6284 * end up in the driver. tp->{tx,}lock are held and thus
6285 * we may not sleep.
6286 */
6287static int tg3_init_rings(struct tg3 *tp)
6288{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006289 int i;
Matt Carlson72334482009-08-28 14:03:01 +00006290
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006291 /* Free up all the SKBs. */
6292 tg3_free_rings(tp);
6293
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006294 for (i = 0; i < tp->irq_cnt; i++) {
6295 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006296
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006297 tnapi->last_tag = 0;
6298 tnapi->last_irq_tag = 0;
6299 tnapi->hw_status->status = 0;
6300 tnapi->hw_status->status_tag = 0;
6301 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6302
6303 tnapi->tx_prod = 0;
6304 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006305 if (tnapi->tx_ring)
6306 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006307
6308 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006309 if (tnapi->rx_rcb)
6310 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006311
Matt Carlson8fea32b2010-09-15 08:59:58 +00006312 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00006313 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006314 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006315 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006316 }
Matt Carlson72334482009-08-28 14:03:01 +00006317
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006318 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006319}
6320
6321/*
6322 * Must not be invoked with interrupt sources disabled and
6323 * the hardware shutdown down.
6324 */
6325static void tg3_free_consistent(struct tg3 *tp)
6326{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006327 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006328
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006329 for (i = 0; i < tp->irq_cnt; i++) {
6330 struct tg3_napi *tnapi = &tp->napi[i];
6331
6332 if (tnapi->tx_ring) {
6333 pci_free_consistent(tp->pdev, TG3_TX_RING_BYTES,
6334 tnapi->tx_ring, tnapi->tx_desc_mapping);
6335 tnapi->tx_ring = NULL;
6336 }
6337
6338 kfree(tnapi->tx_buffers);
6339 tnapi->tx_buffers = NULL;
6340
6341 if (tnapi->rx_rcb) {
6342 pci_free_consistent(tp->pdev, TG3_RX_RCB_RING_BYTES(tp),
6343 tnapi->rx_rcb,
6344 tnapi->rx_rcb_mapping);
6345 tnapi->rx_rcb = NULL;
6346 }
6347
Matt Carlson8fea32b2010-09-15 08:59:58 +00006348 tg3_rx_prodring_fini(tp, &tnapi->prodring);
6349
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006350 if (tnapi->hw_status) {
6351 pci_free_consistent(tp->pdev, TG3_HW_STATUS_SIZE,
6352 tnapi->hw_status,
6353 tnapi->status_mapping);
6354 tnapi->hw_status = NULL;
6355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006356 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006357
Linus Torvalds1da177e2005-04-16 15:20:36 -07006358 if (tp->hw_stats) {
6359 pci_free_consistent(tp->pdev, sizeof(struct tg3_hw_stats),
6360 tp->hw_stats, tp->stats_mapping);
6361 tp->hw_stats = NULL;
6362 }
6363}
6364
6365/*
6366 * Must not be invoked with interrupt sources disabled and
6367 * the hardware shutdown down. Can sleep.
6368 */
6369static int tg3_alloc_consistent(struct tg3 *tp)
6370{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006371 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006372
Linus Torvalds1da177e2005-04-16 15:20:36 -07006373 tp->hw_stats = pci_alloc_consistent(tp->pdev,
6374 sizeof(struct tg3_hw_stats),
6375 &tp->stats_mapping);
6376 if (!tp->hw_stats)
6377 goto err_out;
6378
Linus Torvalds1da177e2005-04-16 15:20:36 -07006379 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6380
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006381 for (i = 0; i < tp->irq_cnt; i++) {
6382 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006383 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006384
6385 tnapi->hw_status = pci_alloc_consistent(tp->pdev,
6386 TG3_HW_STATUS_SIZE,
6387 &tnapi->status_mapping);
6388 if (!tnapi->hw_status)
6389 goto err_out;
6390
6391 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006392 sblk = tnapi->hw_status;
6393
Matt Carlson8fea32b2010-09-15 08:59:58 +00006394 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
6395 goto err_out;
6396
Matt Carlson19cfaec2009-12-03 08:36:20 +00006397 /* If multivector TSS is enabled, vector 0 does not handle
6398 * tx interrupts. Don't allocate any resources for it.
6399 */
6400 if ((!i && !(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)) ||
6401 (i && (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS))) {
6402 tnapi->tx_buffers = kzalloc(sizeof(struct ring_info) *
6403 TG3_TX_RING_SIZE,
6404 GFP_KERNEL);
6405 if (!tnapi->tx_buffers)
6406 goto err_out;
6407
6408 tnapi->tx_ring = pci_alloc_consistent(tp->pdev,
6409 TG3_TX_RING_BYTES,
6410 &tnapi->tx_desc_mapping);
6411 if (!tnapi->tx_ring)
6412 goto err_out;
6413 }
6414
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006415 /*
6416 * When RSS is enabled, the status block format changes
6417 * slightly. The "rx_jumbo_consumer", "reserved",
6418 * and "rx_mini_consumer" members get mapped to the
6419 * other three rx return ring producer indexes.
6420 */
6421 switch (i) {
6422 default:
6423 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
6424 break;
6425 case 2:
6426 tnapi->rx_rcb_prod_idx = &sblk->rx_jumbo_consumer;
6427 break;
6428 case 3:
6429 tnapi->rx_rcb_prod_idx = &sblk->reserved;
6430 break;
6431 case 4:
6432 tnapi->rx_rcb_prod_idx = &sblk->rx_mini_consumer;
6433 break;
6434 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006435
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006436 /*
6437 * If multivector RSS is enabled, vector 0 does not handle
6438 * rx or tx interrupts. Don't allocate any resources for it.
6439 */
6440 if (!i && (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS))
6441 continue;
6442
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006443 tnapi->rx_rcb = pci_alloc_consistent(tp->pdev,
6444 TG3_RX_RCB_RING_BYTES(tp),
6445 &tnapi->rx_rcb_mapping);
6446 if (!tnapi->rx_rcb)
6447 goto err_out;
6448
6449 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006450 }
6451
Linus Torvalds1da177e2005-04-16 15:20:36 -07006452 return 0;
6453
6454err_out:
6455 tg3_free_consistent(tp);
6456 return -ENOMEM;
6457}
6458
6459#define MAX_WAIT_CNT 1000
6460
6461/* To stop a block, clear the enable bit and poll till it
6462 * clears. tp->lock is held.
6463 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006464static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006465{
6466 unsigned int i;
6467 u32 val;
6468
6469 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
6470 switch (ofs) {
6471 case RCVLSC_MODE:
6472 case DMAC_MODE:
6473 case MBFREE_MODE:
6474 case BUFMGR_MODE:
6475 case MEMARB_MODE:
6476 /* We can't enable/disable these bits of the
6477 * 5705/5750, just say success.
6478 */
6479 return 0;
6480
6481 default:
6482 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006484 }
6485
6486 val = tr32(ofs);
6487 val &= ~enable_bit;
6488 tw32_f(ofs, val);
6489
6490 for (i = 0; i < MAX_WAIT_CNT; i++) {
6491 udelay(100);
6492 val = tr32(ofs);
6493 if ((val & enable_bit) == 0)
6494 break;
6495 }
6496
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006497 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00006498 dev_err(&tp->pdev->dev,
6499 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
6500 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006501 return -ENODEV;
6502 }
6503
6504 return 0;
6505}
6506
6507/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006508static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006509{
6510 int i, err;
6511
6512 tg3_disable_ints(tp);
6513
6514 tp->rx_mode &= ~RX_MODE_ENABLE;
6515 tw32_f(MAC_RX_MODE, tp->rx_mode);
6516 udelay(10);
6517
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006518 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
6519 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
6520 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
6521 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
6522 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
6523 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006524
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006525 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
6526 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
6527 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
6528 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
6529 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
6530 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
6531 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006532
6533 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
6534 tw32_f(MAC_MODE, tp->mac_mode);
6535 udelay(40);
6536
6537 tp->tx_mode &= ~TX_MODE_ENABLE;
6538 tw32_f(MAC_TX_MODE, tp->tx_mode);
6539
6540 for (i = 0; i < MAX_WAIT_CNT; i++) {
6541 udelay(100);
6542 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
6543 break;
6544 }
6545 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00006546 dev_err(&tp->pdev->dev,
6547 "%s timed out, TX_MODE_ENABLE will not clear "
6548 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07006549 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006550 }
6551
Michael Chane6de8ad2005-05-05 14:42:41 -07006552 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006553 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
6554 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006555
6556 tw32(FTQ_RESET, 0xffffffff);
6557 tw32(FTQ_RESET, 0x00000000);
6558
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006559 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
6560 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006561
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006562 for (i = 0; i < tp->irq_cnt; i++) {
6563 struct tg3_napi *tnapi = &tp->napi[i];
6564 if (tnapi->hw_status)
6565 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6566 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006567 if (tp->hw_stats)
6568 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6569
Linus Torvalds1da177e2005-04-16 15:20:36 -07006570 return err;
6571}
6572
Matt Carlson0d3031d2007-10-10 18:02:43 -07006573static void tg3_ape_send_event(struct tg3 *tp, u32 event)
6574{
6575 int i;
6576 u32 apedata;
6577
Matt Carlsondc6d0742010-09-15 08:59:55 +00006578 /* NCSI does not support APE events */
6579 if (tp->tg3_flags3 & TG3_FLG3_APE_HAS_NCSI)
6580 return;
6581
Matt Carlson0d3031d2007-10-10 18:02:43 -07006582 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
6583 if (apedata != APE_SEG_SIG_MAGIC)
6584 return;
6585
6586 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
Matt Carlson731fd792008-08-15 14:07:51 -07006587 if (!(apedata & APE_FW_STATUS_READY))
Matt Carlson0d3031d2007-10-10 18:02:43 -07006588 return;
6589
6590 /* Wait for up to 1 millisecond for APE to service previous event. */
6591 for (i = 0; i < 10; i++) {
6592 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
6593 return;
6594
6595 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
6596
6597 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6598 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
6599 event | APE_EVENT_STATUS_EVENT_PENDING);
6600
6601 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
6602
6603 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6604 break;
6605
6606 udelay(100);
6607 }
6608
6609 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6610 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
6611}
6612
6613static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
6614{
6615 u32 event;
6616 u32 apedata;
6617
6618 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
6619 return;
6620
6621 switch (kind) {
Matt Carlson33f401a2010-04-05 10:19:27 +00006622 case RESET_KIND_INIT:
6623 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
6624 APE_HOST_SEG_SIG_MAGIC);
6625 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
6626 APE_HOST_SEG_LEN_MAGIC);
6627 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
6628 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
6629 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
Matt Carlson6867c842010-07-11 09:31:44 +00006630 APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
Matt Carlson33f401a2010-04-05 10:19:27 +00006631 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
6632 APE_HOST_BEHAV_NO_PHYLOCK);
Matt Carlsondc6d0742010-09-15 08:59:55 +00006633 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
6634 TG3_APE_HOST_DRVR_STATE_START);
Matt Carlson0d3031d2007-10-10 18:02:43 -07006635
Matt Carlson33f401a2010-04-05 10:19:27 +00006636 event = APE_EVENT_STATUS_STATE_START;
6637 break;
6638 case RESET_KIND_SHUTDOWN:
6639 /* With the interface we are currently using,
6640 * APE does not track driver state. Wiping
6641 * out the HOST SEGMENT SIGNATURE forces
6642 * the APE to assume OS absent status.
6643 */
6644 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
Matt Carlsonb2aee152008-11-03 16:51:11 -08006645
Matt Carlsondc6d0742010-09-15 08:59:55 +00006646 if (device_may_wakeup(&tp->pdev->dev) &&
6647 (tp->tg3_flags & TG3_FLAG_WOL_ENABLE)) {
6648 tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
6649 TG3_APE_HOST_WOL_SPEED_AUTO);
6650 apedata = TG3_APE_HOST_DRVR_STATE_WOL;
6651 } else
6652 apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
6653
6654 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
6655
Matt Carlson33f401a2010-04-05 10:19:27 +00006656 event = APE_EVENT_STATUS_STATE_UNLOAD;
6657 break;
6658 case RESET_KIND_SUSPEND:
6659 event = APE_EVENT_STATUS_STATE_SUSPEND;
6660 break;
6661 default:
6662 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -07006663 }
6664
6665 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
6666
6667 tg3_ape_send_event(tp, event);
6668}
6669
Michael Chane6af3012005-04-21 17:12:05 -07006670/* tp->lock is held. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006671static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
6672{
David S. Millerf49639e2006-06-09 11:58:36 -07006673 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
6674 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006675
6676 if (tp->tg3_flags2 & TG3_FLG2_ASF_NEW_HANDSHAKE) {
6677 switch (kind) {
6678 case RESET_KIND_INIT:
6679 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6680 DRV_STATE_START);
6681 break;
6682
6683 case RESET_KIND_SHUTDOWN:
6684 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6685 DRV_STATE_UNLOAD);
6686 break;
6687
6688 case RESET_KIND_SUSPEND:
6689 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6690 DRV_STATE_SUSPEND);
6691 break;
6692
6693 default:
6694 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006696 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07006697
6698 if (kind == RESET_KIND_INIT ||
6699 kind == RESET_KIND_SUSPEND)
6700 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006701}
6702
6703/* tp->lock is held. */
6704static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
6705{
6706 if (tp->tg3_flags2 & TG3_FLG2_ASF_NEW_HANDSHAKE) {
6707 switch (kind) {
6708 case RESET_KIND_INIT:
6709 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6710 DRV_STATE_START_DONE);
6711 break;
6712
6713 case RESET_KIND_SHUTDOWN:
6714 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6715 DRV_STATE_UNLOAD_DONE);
6716 break;
6717
6718 default:
6719 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006721 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07006722
6723 if (kind == RESET_KIND_SHUTDOWN)
6724 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006725}
6726
6727/* tp->lock is held. */
6728static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
6729{
6730 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) {
6731 switch (kind) {
6732 case RESET_KIND_INIT:
6733 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6734 DRV_STATE_START);
6735 break;
6736
6737 case RESET_KIND_SHUTDOWN:
6738 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6739 DRV_STATE_UNLOAD);
6740 break;
6741
6742 case RESET_KIND_SUSPEND:
6743 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6744 DRV_STATE_SUSPEND);
6745 break;
6746
6747 default:
6748 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006750 }
6751}
6752
Michael Chan7a6f4362006-09-27 16:03:31 -07006753static int tg3_poll_fw(struct tg3 *tp)
6754{
6755 int i;
6756 u32 val;
6757
Michael Chanb5d37722006-09-27 16:06:21 -07006758 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Gary Zambrano0ccead12006-11-14 16:34:00 -08006759 /* Wait up to 20ms for init done. */
6760 for (i = 0; i < 200; i++) {
Michael Chanb5d37722006-09-27 16:06:21 -07006761 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
6762 return 0;
Gary Zambrano0ccead12006-11-14 16:34:00 -08006763 udelay(100);
Michael Chanb5d37722006-09-27 16:06:21 -07006764 }
6765 return -ENODEV;
6766 }
6767
Michael Chan7a6f4362006-09-27 16:03:31 -07006768 /* Wait for firmware initialization to complete. */
6769 for (i = 0; i < 100000; i++) {
6770 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
6771 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
6772 break;
6773 udelay(10);
6774 }
6775
6776 /* Chip might not be fitted with firmware. Some Sun onboard
6777 * parts are configured like that. So don't signal the timeout
6778 * of the above loop as an error, but do report the lack of
6779 * running firmware once.
6780 */
6781 if (i >= 100000 &&
6782 !(tp->tg3_flags2 & TG3_FLG2_NO_FWARE_REPORTED)) {
6783 tp->tg3_flags2 |= TG3_FLG2_NO_FWARE_REPORTED;
6784
Joe Perches05dbe002010-02-17 19:44:19 +00006785 netdev_info(tp->dev, "No firmware running\n");
Michael Chan7a6f4362006-09-27 16:03:31 -07006786 }
6787
Matt Carlson6b10c162010-02-12 14:47:08 +00006788 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
6789 /* The 57765 A0 needs a little more
6790 * time to do some important work.
6791 */
6792 mdelay(10);
6793 }
6794
Michael Chan7a6f4362006-09-27 16:03:31 -07006795 return 0;
6796}
6797
Michael Chanee6a99b2007-07-18 21:49:10 -07006798/* Save PCI command register before chip reset */
6799static void tg3_save_pci_state(struct tg3 *tp)
6800{
Matt Carlson8a6eac92007-10-21 16:17:55 -07006801 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07006802}
6803
6804/* Restore PCI state after chip reset */
6805static void tg3_restore_pci_state(struct tg3 *tp)
6806{
6807 u32 val;
6808
6809 /* Re-enable indirect register accesses. */
6810 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
6811 tp->misc_host_ctrl);
6812
6813 /* Set MAX PCI retry to zero. */
6814 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
6815 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
6816 (tp->tg3_flags & TG3_FLAG_PCIX_MODE))
6817 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07006818 /* Allow reads and writes to the APE register and memory space. */
6819 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
6820 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00006821 PCISTATE_ALLOW_APE_SHMEM_WR |
6822 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07006823 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
6824
Matt Carlson8a6eac92007-10-21 16:17:55 -07006825 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07006826
Matt Carlsonfcb389d2008-11-03 16:55:44 -08006827 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785) {
6828 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)
6829 pcie_set_readrq(tp->pdev, 4096);
6830 else {
6831 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
6832 tp->pci_cacheline_sz);
6833 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
6834 tp->pci_lat_timer);
6835 }
Michael Chan114342f2007-10-15 02:12:26 -07006836 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08006837
Michael Chanee6a99b2007-07-18 21:49:10 -07006838 /* Make sure PCI-X relaxed ordering bit is clear. */
Matt Carlson52f44902008-11-21 17:17:04 -08006839 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
Matt Carlson9974a352007-10-07 23:27:28 -07006840 u16 pcix_cmd;
6841
6842 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
6843 &pcix_cmd);
6844 pcix_cmd &= ~PCI_X_CMD_ERO;
6845 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
6846 pcix_cmd);
6847 }
Michael Chanee6a99b2007-07-18 21:49:10 -07006848
6849 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
Michael Chanee6a99b2007-07-18 21:49:10 -07006850
6851 /* Chip reset on 5780 will reset MSI enable bit,
6852 * so need to restore it.
6853 */
6854 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
6855 u16 ctrl;
6856
6857 pci_read_config_word(tp->pdev,
6858 tp->msi_cap + PCI_MSI_FLAGS,
6859 &ctrl);
6860 pci_write_config_word(tp->pdev,
6861 tp->msi_cap + PCI_MSI_FLAGS,
6862 ctrl | PCI_MSI_FLAGS_ENABLE);
6863 val = tr32(MSGINT_MODE);
6864 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
6865 }
6866 }
6867}
6868
Linus Torvalds1da177e2005-04-16 15:20:36 -07006869static void tg3_stop_fw(struct tg3 *);
6870
6871/* tp->lock is held. */
6872static int tg3_chip_reset(struct tg3 *tp)
6873{
6874 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07006875 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00006876 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006877
David S. Millerf49639e2006-06-09 11:58:36 -07006878 tg3_nvram_lock(tp);
6879
Matt Carlson77b483f2008-08-15 14:07:24 -07006880 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
6881
David S. Millerf49639e2006-06-09 11:58:36 -07006882 /* No matching tg3_nvram_unlock() after this because
6883 * chip reset below will undo the nvram lock.
6884 */
6885 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006886
Michael Chanee6a99b2007-07-18 21:49:10 -07006887 /* GRC_MISC_CFG core clock reset will clear the memory
6888 * enable bit in PCI register 4 and the MSI enable bit
6889 * on some chips, so we save relevant registers here.
6890 */
6891 tg3_save_pci_state(tp);
6892
Michael Chand9ab5ad2006-03-20 22:27:35 -08006893 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08006894 (tp->tg3_flags3 & TG3_FLG3_5755_PLUS))
Michael Chand9ab5ad2006-03-20 22:27:35 -08006895 tw32(GRC_FASTBOOT_PC, 0);
6896
Linus Torvalds1da177e2005-04-16 15:20:36 -07006897 /*
6898 * We must avoid the readl() that normally takes place.
6899 * It locks machines, causes machine checks, and other
6900 * fun things. So, temporarily disable the 5701
6901 * hardware workaround, while we do the reset.
6902 */
Michael Chan1ee582d2005-08-09 20:16:46 -07006903 write_op = tp->write32;
6904 if (write_op == tg3_write_flush_reg32)
6905 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006906
Michael Chand18edcb2007-03-24 20:57:11 -07006907 /* Prevent the irq handler from reading or writing PCI registers
6908 * during chip reset when the memory enable bit in the PCI command
6909 * register may be cleared. The chip does not generate interrupt
6910 * at this time, but the irq handler may still be called due to irq
6911 * sharing or irqpoll.
6912 */
6913 tp->tg3_flags |= TG3_FLAG_CHIP_RESETTING;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006914 for (i = 0; i < tp->irq_cnt; i++) {
6915 struct tg3_napi *tnapi = &tp->napi[i];
6916 if (tnapi->hw_status) {
6917 tnapi->hw_status->status = 0;
6918 tnapi->hw_status->status_tag = 0;
6919 }
6920 tnapi->last_tag = 0;
6921 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07006922 }
Michael Chand18edcb2007-03-24 20:57:11 -07006923 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00006924
6925 for (i = 0; i < tp->irq_cnt; i++)
6926 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07006927
Matt Carlson255ca312009-08-25 10:07:27 +00006928 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
6929 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
6930 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
6931 }
6932
Linus Torvalds1da177e2005-04-16 15:20:36 -07006933 /* do the reset */
6934 val = GRC_MISC_CFG_CORECLK_RESET;
6935
6936 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
Matt Carlson88075d92010-08-02 11:25:58 +00006937 /* Force PCIe 1.0a mode */
6938 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
6939 !(tp->tg3_flags3 & TG3_FLG3_5717_PLUS) &&
6940 tr32(TG3_PCIE_PHY_TSTCTL) ==
6941 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
6942 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
6943
Linus Torvalds1da177e2005-04-16 15:20:36 -07006944 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
6945 tw32(GRC_MISC_CFG, (1 << 29));
6946 val |= (1 << 29);
6947 }
6948 }
6949
Michael Chanb5d37722006-09-27 16:06:21 -07006950 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
6951 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
6952 tw32(GRC_VCPU_EXT_CTRL,
6953 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
6954 }
6955
Matt Carlsonf37500d2010-08-02 11:25:59 +00006956 /* Manage gphy power for all CPMU absent PCIe devices. */
6957 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
6958 !(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006959 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00006960
Linus Torvalds1da177e2005-04-16 15:20:36 -07006961 tw32(GRC_MISC_CFG, val);
6962
Michael Chan1ee582d2005-08-09 20:16:46 -07006963 /* restore 5701 hardware bug workaround write method */
6964 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006965
6966 /* Unfortunately, we have to delay before the PCI read back.
6967 * Some 575X chips even will not respond to a PCI cfg access
6968 * when the reset command is given to the chip.
6969 *
6970 * How do these hardware designers expect things to work
6971 * properly if the PCI write is posted for a long period
6972 * of time? It is always necessary to have some method by
6973 * which a register read back can occur to push the write
6974 * out which does the reset.
6975 *
6976 * For most tg3 variants the trick below was working.
6977 * Ho hum...
6978 */
6979 udelay(120);
6980
6981 /* Flush PCI posted writes. The normal MMIO registers
6982 * are inaccessible at this time so this is the only
6983 * way to make this reliably (actually, this is no longer
6984 * the case, see above). I tried to use indirect
6985 * register read/write but this upset some 5701 variants.
6986 */
6987 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
6988
6989 udelay(120);
6990
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006991 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) && tp->pcie_cap) {
Matt Carlsone7126992009-08-25 10:08:16 +00006992 u16 val16;
6993
Linus Torvalds1da177e2005-04-16 15:20:36 -07006994 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
6995 int i;
6996 u32 cfg_val;
6997
6998 /* Wait for link training to complete. */
6999 for (i = 0; i < 5000; i++)
7000 udelay(100);
7001
7002 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
7003 pci_write_config_dword(tp->pdev, 0xc4,
7004 cfg_val | (1 << 15));
7005 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007006
Matt Carlsone7126992009-08-25 10:08:16 +00007007 /* Clear the "no snoop" and "relaxed ordering" bits. */
7008 pci_read_config_word(tp->pdev,
7009 tp->pcie_cap + PCI_EXP_DEVCTL,
7010 &val16);
7011 val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
7012 PCI_EXP_DEVCTL_NOSNOOP_EN);
7013 /*
7014 * Older PCIe devices only support the 128 byte
7015 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007016 */
Matt Carlson6de34cb2010-08-02 11:25:55 +00007017 if (!(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT))
Matt Carlsone7126992009-08-25 10:08:16 +00007018 val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007019 pci_write_config_word(tp->pdev,
7020 tp->pcie_cap + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007021 val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007022
7023 pcie_set_readrq(tp->pdev, 4096);
7024
7025 /* Clear error status */
7026 pci_write_config_word(tp->pdev,
7027 tp->pcie_cap + PCI_EXP_DEVSTA,
7028 PCI_EXP_DEVSTA_CED |
7029 PCI_EXP_DEVSTA_NFED |
7030 PCI_EXP_DEVSTA_FED |
7031 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007032 }
7033
Michael Chanee6a99b2007-07-18 21:49:10 -07007034 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007035
Michael Chand18edcb2007-03-24 20:57:11 -07007036 tp->tg3_flags &= ~TG3_FLAG_CHIP_RESETTING;
7037
Michael Chanee6a99b2007-07-18 21:49:10 -07007038 val = 0;
7039 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
Michael Chan4cf78e42005-07-25 12:29:19 -07007040 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07007041 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007042
7043 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
7044 tg3_stop_fw(tp);
7045 tw32(0x5000, 0x400);
7046 }
7047
7048 tw32(GRC_MODE, tp->grc_mode);
7049
7050 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007051 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007052
7053 tw32(0xc4, val | (1 << 15));
7054 }
7055
7056 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
7057 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
7058 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
7059 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
7060 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
7061 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7062 }
7063
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007064 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007065 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
7066 tw32_f(MAC_MODE, tp->mac_mode);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007067 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Michael Chan747e8f82005-07-25 12:33:22 -07007068 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
7069 tw32_f(MAC_MODE, tp->mac_mode);
Matt Carlson3bda1252008-08-15 14:08:22 -07007070 } else if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
7071 tp->mac_mode &= (MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN);
7072 if (tp->mac_mode & MAC_MODE_APE_TX_EN)
7073 tp->mac_mode |= MAC_MODE_TDE_ENABLE;
7074 tw32_f(MAC_MODE, tp->mac_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007075 } else
7076 tw32_f(MAC_MODE, 0);
7077 udelay(40);
7078
Matt Carlson77b483f2008-08-15 14:07:24 -07007079 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
7080
Michael Chan7a6f4362006-09-27 16:03:31 -07007081 err = tg3_poll_fw(tp);
7082 if (err)
7083 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007084
Matt Carlson0a9140c2009-08-28 12:27:50 +00007085 tg3_mdio_start(tp);
7086
Linus Torvalds1da177e2005-04-16 15:20:36 -07007087 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007088 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
7089 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonc885e822010-08-02 11:25:57 +00007090 !(tp->tg3_flags3 & TG3_FLG3_5717_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007091 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007092
7093 tw32(0x7c00, val | (1 << 25));
7094 }
7095
7096 /* Reprobe ASF enable state. */
7097 tp->tg3_flags &= ~TG3_FLAG_ENABLE_ASF;
7098 tp->tg3_flags2 &= ~TG3_FLG2_ASF_NEW_HANDSHAKE;
7099 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
7100 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
7101 u32 nic_cfg;
7102
7103 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
7104 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
7105 tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
Matt Carlson4ba526c2008-08-15 14:10:04 -07007106 tp->last_event_jiffies = jiffies;
John W. Linvillecbf46852005-04-21 17:01:29 -07007107 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007108 tp->tg3_flags2 |= TG3_FLG2_ASF_NEW_HANDSHAKE;
7109 }
7110 }
7111
7112 return 0;
7113}
7114
7115/* tp->lock is held. */
7116static void tg3_stop_fw(struct tg3 *tp)
7117{
Matt Carlson0d3031d2007-10-10 18:02:43 -07007118 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
7119 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07007120 /* Wait for RX cpu to ACK the previous event. */
7121 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007122
7123 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007124
7125 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007126
Matt Carlson7c5026a2008-05-02 16:49:29 -07007127 /* Wait for RX cpu to ACK this event. */
7128 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007129 }
7130}
7131
7132/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07007133static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007134{
7135 int err;
7136
7137 tg3_stop_fw(tp);
7138
Michael Chan944d9802005-05-29 14:57:48 -07007139 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007140
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007141 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007142 err = tg3_chip_reset(tp);
7143
Matt Carlsondaba2a62009-04-20 06:58:52 +00007144 __tg3_set_mac_addr(tp, 0);
7145
Michael Chan944d9802005-05-29 14:57:48 -07007146 tg3_write_sig_legacy(tp, kind);
7147 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007148
7149 if (err)
7150 return err;
7151
7152 return 0;
7153}
7154
Linus Torvalds1da177e2005-04-16 15:20:36 -07007155#define RX_CPU_SCRATCH_BASE 0x30000
7156#define RX_CPU_SCRATCH_SIZE 0x04000
7157#define TX_CPU_SCRATCH_BASE 0x34000
7158#define TX_CPU_SCRATCH_SIZE 0x04000
7159
7160/* tp->lock is held. */
7161static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
7162{
7163 int i;
7164
Eric Sesterhenn5d9428d2006-04-02 13:52:48 +02007165 BUG_ON(offset == TX_CPU_BASE &&
7166 (tp->tg3_flags2 & TG3_FLG2_5705_PLUS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007167
Michael Chanb5d37722006-09-27 16:06:21 -07007168 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7169 u32 val = tr32(GRC_VCPU_EXT_CTRL);
7170
7171 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
7172 return 0;
7173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007174 if (offset == RX_CPU_BASE) {
7175 for (i = 0; i < 10000; i++) {
7176 tw32(offset + CPU_STATE, 0xffffffff);
7177 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7178 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7179 break;
7180 }
7181
7182 tw32(offset + CPU_STATE, 0xffffffff);
7183 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
7184 udelay(10);
7185 } else {
7186 for (i = 0; i < 10000; i++) {
7187 tw32(offset + CPU_STATE, 0xffffffff);
7188 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7189 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7190 break;
7191 }
7192 }
7193
7194 if (i >= 10000) {
Joe Perches05dbe002010-02-17 19:44:19 +00007195 netdev_err(tp->dev, "%s timed out, %s CPU\n",
7196 __func__, offset == RX_CPU_BASE ? "RX" : "TX");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007197 return -ENODEV;
7198 }
Michael Chanec41c7d2006-01-17 02:40:55 -08007199
7200 /* Clear firmware's nvram arbitration. */
7201 if (tp->tg3_flags & TG3_FLAG_NVRAM)
7202 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007203 return 0;
7204}
7205
7206struct fw_info {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007207 unsigned int fw_base;
7208 unsigned int fw_len;
7209 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007210};
7211
7212/* tp->lock is held. */
7213static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base, u32 cpu_scratch_base,
7214 int cpu_scratch_size, struct fw_info *info)
7215{
Michael Chanec41c7d2006-01-17 02:40:55 -08007216 int err, lock_err, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007217 void (*write_op)(struct tg3 *, u32, u32);
7218
7219 if (cpu_base == TX_CPU_BASE &&
7220 (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007221 netdev_err(tp->dev,
7222 "%s: Trying to load TX cpu firmware which is 5705\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007223 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007224 return -EINVAL;
7225 }
7226
7227 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
7228 write_op = tg3_write_mem;
7229 else
7230 write_op = tg3_write_indirect_reg32;
7231
Michael Chan1b628152005-05-29 14:59:49 -07007232 /* It is possible that bootcode is still loading at this point.
7233 * Get the nvram lock first before halting the cpu.
7234 */
Michael Chanec41c7d2006-01-17 02:40:55 -08007235 lock_err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007236 err = tg3_halt_cpu(tp, cpu_base);
Michael Chanec41c7d2006-01-17 02:40:55 -08007237 if (!lock_err)
7238 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007239 if (err)
7240 goto out;
7241
7242 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
7243 write_op(tp, cpu_scratch_base + i, 0);
7244 tw32(cpu_base + CPU_STATE, 0xffffffff);
7245 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007246 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007247 write_op(tp, (cpu_scratch_base +
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007248 (info->fw_base & 0xffff) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07007249 (i * sizeof(u32))),
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007250 be32_to_cpu(info->fw_data[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007251
7252 err = 0;
7253
7254out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007255 return err;
7256}
7257
7258/* tp->lock is held. */
7259static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
7260{
7261 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007262 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007263 int err, i;
7264
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007265 fw_data = (void *)tp->fw->data;
7266
7267 /* Firmware blob starts with version numbers, followed by
7268 start address and length. We are setting complete length.
7269 length = end_address_of_bss - start_address_of_text.
7270 Remainder is the blob to be loaded contiguously
7271 from start address. */
7272
7273 info.fw_base = be32_to_cpu(fw_data[1]);
7274 info.fw_len = tp->fw->size - 12;
7275 info.fw_data = &fw_data[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007276
7277 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
7278 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
7279 &info);
7280 if (err)
7281 return err;
7282
7283 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
7284 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
7285 &info);
7286 if (err)
7287 return err;
7288
7289 /* Now startup only the RX cpu. */
7290 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007291 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007292
7293 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007294 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007295 break;
7296 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7297 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007298 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007299 udelay(1000);
7300 }
7301 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007302 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
7303 "should be %08x\n", __func__,
Joe Perches05dbe002010-02-17 19:44:19 +00007304 tr32(RX_CPU_BASE + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007305 return -ENODEV;
7306 }
7307 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7308 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
7309
7310 return 0;
7311}
7312
Linus Torvalds1da177e2005-04-16 15:20:36 -07007313/* 5705 needs a special version of the TSO firmware. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007314
7315/* tp->lock is held. */
7316static int tg3_load_tso_firmware(struct tg3 *tp)
7317{
7318 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007319 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007320 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
7321 int err, i;
7322
7323 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
7324 return 0;
7325
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007326 fw_data = (void *)tp->fw->data;
7327
7328 /* Firmware blob starts with version numbers, followed by
7329 start address and length. We are setting complete length.
7330 length = end_address_of_bss - start_address_of_text.
7331 Remainder is the blob to be loaded contiguously
7332 from start address. */
7333
7334 info.fw_base = be32_to_cpu(fw_data[1]);
7335 cpu_scratch_size = tp->fw_len;
7336 info.fw_len = tp->fw->size - 12;
7337 info.fw_data = &fw_data[3];
7338
Linus Torvalds1da177e2005-04-16 15:20:36 -07007339 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007340 cpu_base = RX_CPU_BASE;
7341 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007342 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007343 cpu_base = TX_CPU_BASE;
7344 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
7345 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
7346 }
7347
7348 err = tg3_load_firmware_cpu(tp, cpu_base,
7349 cpu_scratch_base, cpu_scratch_size,
7350 &info);
7351 if (err)
7352 return err;
7353
7354 /* Now startup the cpu. */
7355 tw32(cpu_base + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007356 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007357
7358 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007359 if (tr32(cpu_base + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007360 break;
7361 tw32(cpu_base + CPU_STATE, 0xffffffff);
7362 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007363 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007364 udelay(1000);
7365 }
7366 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007367 netdev_err(tp->dev,
7368 "%s fails to set CPU PC, is %08x should be %08x\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007369 __func__, tr32(cpu_base + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007370 return -ENODEV;
7371 }
7372 tw32(cpu_base + CPU_STATE, 0xffffffff);
7373 tw32_f(cpu_base + CPU_MODE, 0x00000000);
7374 return 0;
7375}
7376
Linus Torvalds1da177e2005-04-16 15:20:36 -07007377
Linus Torvalds1da177e2005-04-16 15:20:36 -07007378static int tg3_set_mac_addr(struct net_device *dev, void *p)
7379{
7380 struct tg3 *tp = netdev_priv(dev);
7381 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07007382 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007383
Michael Chanf9804dd2005-09-27 12:13:10 -07007384 if (!is_valid_ether_addr(addr->sa_data))
7385 return -EINVAL;
7386
Linus Torvalds1da177e2005-04-16 15:20:36 -07007387 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7388
Michael Chane75f7c92006-03-20 21:33:26 -08007389 if (!netif_running(dev))
7390 return 0;
7391
Michael Chan58712ef2006-04-29 18:58:01 -07007392 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) {
Michael Chan986e0ae2007-05-05 12:10:20 -07007393 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07007394
Michael Chan986e0ae2007-05-05 12:10:20 -07007395 addr0_high = tr32(MAC_ADDR_0_HIGH);
7396 addr0_low = tr32(MAC_ADDR_0_LOW);
7397 addr1_high = tr32(MAC_ADDR_1_HIGH);
7398 addr1_low = tr32(MAC_ADDR_1_LOW);
7399
7400 /* Skip MAC addr 1 if ASF is using it. */
7401 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
7402 !(addr1_high == 0 && addr1_low == 0))
7403 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07007404 }
Michael Chan986e0ae2007-05-05 12:10:20 -07007405 spin_lock_bh(&tp->lock);
7406 __tg3_set_mac_addr(tp, skip_mac_1);
7407 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007408
Michael Chanb9ec6c12006-07-25 16:37:27 -07007409 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007410}
7411
7412/* tp->lock is held. */
7413static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
7414 dma_addr_t mapping, u32 maxlen_flags,
7415 u32 nic_addr)
7416{
7417 tg3_write_mem(tp,
7418 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
7419 ((u64) mapping >> 32));
7420 tg3_write_mem(tp,
7421 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
7422 ((u64) mapping & 0xffffffff));
7423 tg3_write_mem(tp,
7424 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
7425 maxlen_flags);
7426
7427 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7428 tg3_write_mem(tp,
7429 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
7430 nic_addr);
7431}
7432
7433static void __tg3_set_rx_mode(struct net_device *);
Michael Chand244c892005-07-05 14:42:33 -07007434static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07007435{
Matt Carlsonb6080e12009-09-01 13:12:00 +00007436 int i;
7437
Matt Carlson19cfaec2009-12-03 08:36:20 +00007438 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007439 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
7440 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
7441 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007442 } else {
7443 tw32(HOSTCC_TXCOL_TICKS, 0);
7444 tw32(HOSTCC_TXMAX_FRAMES, 0);
7445 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007446 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007447
Matt Carlson20d73752010-07-11 09:31:41 +00007448 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00007449 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
7450 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
7451 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
7452 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007453 tw32(HOSTCC_RXCOL_TICKS, 0);
7454 tw32(HOSTCC_RXMAX_FRAMES, 0);
7455 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07007456 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007457
David S. Miller15f98502005-05-18 22:49:26 -07007458 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7459 u32 val = ec->stats_block_coalesce_usecs;
7460
Matt Carlsonb6080e12009-09-01 13:12:00 +00007461 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
7462 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
7463
David S. Miller15f98502005-05-18 22:49:26 -07007464 if (!netif_carrier_ok(tp->dev))
7465 val = 0;
7466
7467 tw32(HOSTCC_STAT_COAL_TICKS, val);
7468 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007469
7470 for (i = 0; i < tp->irq_cnt - 1; i++) {
7471 u32 reg;
7472
7473 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
7474 tw32(reg, ec->rx_coalesce_usecs);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007475 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
7476 tw32(reg, ec->rx_max_coalesced_frames);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007477 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
7478 tw32(reg, ec->rx_max_coalesced_frames_irq);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007479
7480 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS) {
7481 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
7482 tw32(reg, ec->tx_coalesce_usecs);
7483 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
7484 tw32(reg, ec->tx_max_coalesced_frames);
7485 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
7486 tw32(reg, ec->tx_max_coalesced_frames_irq);
7487 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007488 }
7489
7490 for (; i < tp->irq_max - 1; i++) {
7491 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007492 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007493 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007494
7495 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS) {
7496 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
7497 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
7498 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
7499 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007500 }
David S. Miller15f98502005-05-18 22:49:26 -07007501}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007502
7503/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00007504static void tg3_rings_reset(struct tg3 *tp)
7505{
7506 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007507 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007508 struct tg3_napi *tnapi = &tp->napi[0];
7509
7510 /* Disable all transmit rings but the first. */
7511 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7512 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00007513 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
7514 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007515 else
7516 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7517
7518 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7519 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
7520 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
7521 BDINFO_FLAGS_DISABLED);
7522
7523
7524 /* Disable all receive return rings but the first. */
Matt Carlsona50d0792010-06-05 17:24:37 +00007525 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
7526 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007527 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
7528 else if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00007529 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00007530 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
7531 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson2d31eca2009-09-01 12:53:31 +00007532 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
7533 else
7534 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7535
7536 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7537 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
7538 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
7539 BDINFO_FLAGS_DISABLED);
7540
7541 /* Disable interrupts */
7542 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
7543
7544 /* Zero mailbox registers. */
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007545 if (tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00007546 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007547 tp->napi[i].tx_prod = 0;
7548 tp->napi[i].tx_cons = 0;
Matt Carlsonc2353a32010-01-20 16:58:08 +00007549 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
7550 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007551 tw32_rx_mbox(tp->napi[i].consmbox, 0);
7552 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
7553 }
Matt Carlsonc2353a32010-01-20 16:58:08 +00007554 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS))
7555 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007556 } else {
7557 tp->napi[0].tx_prod = 0;
7558 tp->napi[0].tx_cons = 0;
7559 tw32_mailbox(tp->napi[0].prodmbox, 0);
7560 tw32_rx_mbox(tp->napi[0].consmbox, 0);
7561 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007562
7563 /* Make sure the NIC-based send BD rings are disabled. */
7564 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7565 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
7566 for (i = 0; i < 16; i++)
7567 tw32_tx_mbox(mbox + i * 8, 0);
7568 }
7569
7570 txrcb = NIC_SRAM_SEND_RCB;
7571 rxrcb = NIC_SRAM_RCV_RET_RCB;
7572
7573 /* Clear status block in ram. */
7574 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7575
7576 /* Set status block DMA address */
7577 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
7578 ((u64) tnapi->status_mapping >> 32));
7579 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
7580 ((u64) tnapi->status_mapping & 0xffffffff));
7581
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007582 if (tnapi->tx_ring) {
7583 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7584 (TG3_TX_RING_SIZE <<
7585 BDINFO_FLAGS_MAXLEN_SHIFT),
7586 NIC_SRAM_TX_BUFFER_DESC);
7587 txrcb += TG3_BDINFO_SIZE;
7588 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007589
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007590 if (tnapi->rx_rcb) {
7591 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
7592 (TG3_RX_RCB_RING_SIZE(tp) <<
7593 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
7594 rxrcb += TG3_BDINFO_SIZE;
7595 }
7596
7597 stblk = HOSTCC_STATBLCK_RING1;
7598
7599 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
7600 u64 mapping = (u64)tnapi->status_mapping;
7601 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
7602 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
7603
7604 /* Clear status block in ram. */
7605 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7606
Matt Carlson19cfaec2009-12-03 08:36:20 +00007607 if (tnapi->tx_ring) {
7608 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7609 (TG3_TX_RING_SIZE <<
7610 BDINFO_FLAGS_MAXLEN_SHIFT),
7611 NIC_SRAM_TX_BUFFER_DESC);
7612 txrcb += TG3_BDINFO_SIZE;
7613 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007614
7615 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
7616 (TG3_RX_RCB_RING_SIZE(tp) <<
7617 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
7618
7619 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007620 rxrcb += TG3_BDINFO_SIZE;
7621 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007622}
7623
7624/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07007625static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007626{
7627 u32 val, rdmac_mode;
7628 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00007629 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007630
7631 tg3_disable_ints(tp);
7632
7633 tg3_stop_fw(tp);
7634
7635 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
7636
Matt Carlson859a588792010-04-05 10:19:28 +00007637 if (tp->tg3_flags & TG3_FLAG_INIT_COMPLETE)
Michael Chane6de8ad2005-05-05 14:42:41 -07007638 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007639
Matt Carlson603f1172010-02-12 14:47:10 +00007640 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08007641 tg3_phy_reset(tp);
7642
Linus Torvalds1da177e2005-04-16 15:20:36 -07007643 err = tg3_chip_reset(tp);
7644 if (err)
7645 return err;
7646
7647 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
7648
Matt Carlsonbcb37f62008-11-03 16:52:09 -08007649 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07007650 val = tr32(TG3_CPMU_CTRL);
7651 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
7652 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08007653
7654 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
7655 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
7656 val |= CPMU_LSPD_10MB_MACCLK_6_25;
7657 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
7658
7659 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
7660 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
7661 val |= CPMU_LNK_AWARE_MACCLK_6_25;
7662 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
7663
7664 val = tr32(TG3_CPMU_HST_ACC);
7665 val &= ~CPMU_HST_ACC_MACCLK_MASK;
7666 val |= CPMU_HST_ACC_MACCLK_6_25;
7667 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07007668 }
7669
Matt Carlson33466d92009-04-20 06:57:41 +00007670 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7671 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
7672 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
7673 PCIE_PWR_MGMT_L1_THRESH_4MS;
7674 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00007675
7676 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
7677 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
7678
7679 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00007680
Matt Carlsonf40386c2009-11-02 14:24:02 +00007681 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7682 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00007683 }
7684
Matt Carlson614b0592010-01-20 16:58:02 +00007685 if (tp->tg3_flags3 & TG3_FLG3_L1PLLPD_EN) {
7686 u32 grc_mode = tr32(GRC_MODE);
7687
7688 /* Access the lower 1K of PL PCIE block registers. */
7689 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
7690 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
7691
7692 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
7693 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
7694 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
7695
7696 tw32(GRC_MODE, grc_mode);
7697 }
7698
Matt Carlsoncea46462010-04-12 06:58:24 +00007699 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
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_PHYCTL5);
7707 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
7708 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
7709
7710 tw32(GRC_MODE, grc_mode);
Matt Carlsona977dbe2010-04-12 06:58:26 +00007711
7712 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
7713 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
7714 val |= CPMU_LSPD_10MB_MACCLK_6_25;
7715 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00007716 }
7717
Linus Torvalds1da177e2005-04-16 15:20:36 -07007718 /* This works around an issue with Athlon chipsets on
7719 * B3 tigon3 silicon. This bit has no effect on any
7720 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07007721 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007722 */
Matt Carlson795d01c2007-10-07 23:28:17 -07007723 if (!(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT)) {
7724 if (!(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
7725 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
7726 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7727 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007728
7729 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
7730 (tp->tg3_flags & TG3_FLAG_PCIX_MODE)) {
7731 val = tr32(TG3PCI_PCISTATE);
7732 val |= PCISTATE_RETRY_SAME_DMA;
7733 tw32(TG3PCI_PCISTATE, val);
7734 }
7735
Matt Carlson0d3031d2007-10-10 18:02:43 -07007736 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
7737 /* Allow reads and writes to the
7738 * APE register and memory space.
7739 */
7740 val = tr32(TG3PCI_PCISTATE);
7741 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00007742 PCISTATE_ALLOW_APE_SHMEM_WR |
7743 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07007744 tw32(TG3PCI_PCISTATE, val);
7745 }
7746
Linus Torvalds1da177e2005-04-16 15:20:36 -07007747 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
7748 /* Enable some hw fixes. */
7749 val = tr32(TG3PCI_MSI_DATA);
7750 val |= (1 << 26) | (1 << 28) | (1 << 29);
7751 tw32(TG3PCI_MSI_DATA, val);
7752 }
7753
7754 /* Descriptor ring init may make accesses to the
7755 * NIC SRAM area to setup the TX descriptors, so we
7756 * can only do this after the hardware has been
7757 * successfully reset.
7758 */
Michael Chan32d8c572006-07-25 16:38:29 -07007759 err = tg3_init_rings(tp);
7760 if (err)
7761 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007762
Matt Carlsonc885e822010-08-02 11:25:57 +00007763 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00007764 val = tr32(TG3PCI_DMA_RW_CTRL) &
7765 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Matt Carlson1a319022010-04-12 06:58:25 +00007766 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
7767 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00007768 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
7769 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
7770 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07007771 /* This value is determined during the probe time DMA
7772 * engine test, tg3_test_dma.
7773 */
7774 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
7775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007776
7777 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
7778 GRC_MODE_4X_NIC_SEND_RINGS |
7779 GRC_MODE_NO_TX_PHDR_CSUM |
7780 GRC_MODE_NO_RX_PHDR_CSUM);
7781 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07007782
7783 /* Pseudo-header checksum is done by hardware logic and not
7784 * the offload processers, so make the chip do the pseudo-
7785 * header checksums on receive. For transmit it is more
7786 * convenient to do the pseudo-header checksum in software
7787 * as Linux does that on transmit for us in all cases.
7788 */
7789 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007790
7791 tw32(GRC_MODE,
7792 tp->grc_mode |
7793 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
7794
7795 /* Setup the timer prescalar register. Clock is always 66Mhz. */
7796 val = tr32(GRC_MISC_CFG);
7797 val &= ~0xff;
7798 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
7799 tw32(GRC_MISC_CFG, val);
7800
7801 /* Initialize MBUF/DESC pool. */
John W. Linvillecbf46852005-04-21 17:01:29 -07007802 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007803 /* Do nothing. */
7804 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
7805 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
7806 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
7807 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
7808 else
7809 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
7810 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
7811 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Matt Carlson859a588792010-04-05 10:19:28 +00007812 } else if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007813 int fw_len;
7814
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007815 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007816 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
7817 tw32(BUFMGR_MB_POOL_ADDR,
7818 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
7819 tw32(BUFMGR_MB_POOL_SIZE,
7820 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
7821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007822
Michael Chan0f893dc2005-07-25 12:30:38 -07007823 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007824 tw32(BUFMGR_MB_RDMA_LOW_WATER,
7825 tp->bufmgr_config.mbuf_read_dma_low_water);
7826 tw32(BUFMGR_MB_MACRX_LOW_WATER,
7827 tp->bufmgr_config.mbuf_mac_rx_low_water);
7828 tw32(BUFMGR_MB_HIGH_WATER,
7829 tp->bufmgr_config.mbuf_high_water);
7830 } else {
7831 tw32(BUFMGR_MB_RDMA_LOW_WATER,
7832 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
7833 tw32(BUFMGR_MB_MACRX_LOW_WATER,
7834 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
7835 tw32(BUFMGR_MB_HIGH_WATER,
7836 tp->bufmgr_config.mbuf_high_water_jumbo);
7837 }
7838 tw32(BUFMGR_DMA_LOW_WATER,
7839 tp->bufmgr_config.dma_low_water);
7840 tw32(BUFMGR_DMA_HIGH_WATER,
7841 tp->bufmgr_config.dma_high_water);
7842
7843 tw32(BUFMGR_MODE, BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE);
7844 for (i = 0; i < 2000; i++) {
7845 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
7846 break;
7847 udelay(10);
7848 }
7849 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00007850 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007851 return -ENODEV;
7852 }
7853
7854 /* Setup replenish threshold. */
Michael Chanf92905d2006-06-29 20:14:29 -07007855 val = tp->rx_pending / 8;
7856 if (val == 0)
7857 val = 1;
7858 else if (val > tp->rx_std_max_post)
7859 val = tp->rx_std_max_post;
Michael Chanb5d37722006-09-27 16:06:21 -07007860 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7861 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
7862 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
7863
7864 if (val > (TG3_RX_INTERNAL_RING_SZ_5906 / 2))
7865 val = TG3_RX_INTERNAL_RING_SZ_5906 / 2;
7866 }
Michael Chanf92905d2006-06-29 20:14:29 -07007867
7868 tw32(RCVBDI_STD_THRESH, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007869
7870 /* Initialize TG3_BDINFO's at:
7871 * RCVDBDI_STD_BD: standard eth size rx ring
7872 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
7873 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
7874 *
7875 * like so:
7876 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
7877 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
7878 * ring attribute flags
7879 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
7880 *
7881 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
7882 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
7883 *
7884 * The size of each ring is fixed in the firmware, but the location is
7885 * configurable.
7886 */
7887 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00007888 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007889 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00007890 ((u64) tpr->rx_std_mapping & 0xffffffff));
Matt Carlsona50d0792010-06-05 17:24:37 +00007891 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
7892 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5719)
Matt Carlson87668d32009-11-13 13:03:34 +00007893 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
7894 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007895
Matt Carlsonfdb72b32009-08-28 13:57:12 +00007896 /* Disable the mini ring */
7897 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007898 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
7899 BDINFO_FLAGS_DISABLED);
7900
Matt Carlsonfdb72b32009-08-28 13:57:12 +00007901 /* Program the jumbo buffer descriptor ring control
7902 * blocks on those devices that have them.
7903 */
Matt Carlson8f666b02009-08-28 13:58:24 +00007904 if ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) &&
Matt Carlsonfdb72b32009-08-28 13:57:12 +00007905 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007906 /* Setup replenish threshold. */
7907 tw32(RCVBDI_JUMBO_THRESH, tp->rx_jumbo_pending / 8);
7908
Michael Chan0f893dc2005-07-25 12:30:38 -07007909 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007910 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00007911 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007912 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00007913 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007914 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlson79ed5ac2009-08-28 14:00:55 +00007915 (RX_JUMBO_MAX_SIZE << BDINFO_FLAGS_MAXLEN_SHIFT) |
7916 BDINFO_FLAGS_USE_EXT_RECV);
Matt Carlsona50d0792010-06-05 17:24:37 +00007917 if (!(tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) ||
7918 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson87668d32009-11-13 13:03:34 +00007919 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
7920 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007921 } else {
7922 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
7923 BDINFO_FLAGS_DISABLED);
7924 }
7925
Matt Carlsonc885e822010-08-02 11:25:57 +00007926 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007927 val = (RX_STD_MAX_SIZE_5705 << BDINFO_FLAGS_MAXLEN_SHIFT) |
Matt Carlson04380d42010-04-12 06:58:29 +00007928 (TG3_RX_STD_DMA_SZ << 2);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007929 else
Matt Carlson04380d42010-04-12 06:58:29 +00007930 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00007931 } else
7932 val = RX_STD_MAX_SIZE_5705 << BDINFO_FLAGS_MAXLEN_SHIFT;
7933
7934 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007935
Matt Carlson411da642009-11-13 13:03:46 +00007936 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e62009-11-13 13:03:49 +00007937 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007938
Matt Carlson411da642009-11-13 13:03:46 +00007939 tpr->rx_jmb_prod_idx = (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) ?
Matt Carlson21f581a2009-08-28 14:00:25 +00007940 tp->rx_jumbo_pending : 0;
Matt Carlson66711e62009-11-13 13:03:49 +00007941 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007942
Matt Carlsonc885e822010-08-02 11:25:57 +00007943 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007944 tw32(STD_REPLENISH_LWM, 32);
7945 tw32(JMB_REPLENISH_LWM, 16);
7946 }
7947
Matt Carlson2d31eca2009-09-01 12:53:31 +00007948 tg3_rings_reset(tp);
7949
Linus Torvalds1da177e2005-04-16 15:20:36 -07007950 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07007951 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007952
7953 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00007954 tw32(MAC_RX_MTU_SIZE,
7955 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007956
7957 /* The slot time is changed by tg3_setup_phy if we
7958 * run at gigabit with half duplex.
7959 */
7960 tw32(MAC_TX_LENGTHS,
7961 (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
7962 (6 << TX_LENGTHS_IPG_SHIFT) |
7963 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
7964
7965 /* Receive rules. */
7966 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
7967 tw32(RCVLPC_CONFIG, 0x0181);
7968
7969 /* Calculate RDMAC_MODE setting early, we need it to determine
7970 * the RCVLPC_STATE_ENABLE mask.
7971 */
7972 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
7973 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
7974 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
7975 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
7976 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07007977
Matt Carlsona50d0792010-06-05 17:24:37 +00007978 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
7979 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlson0339e4e2010-02-12 14:47:09 +00007980 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
7981
Matt Carlson57e69832008-05-25 23:48:31 -07007982 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08007983 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
7984 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07007985 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
7986 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
7987 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
7988
Michael Chan85e94ce2005-04-21 17:05:28 -07007989 /* If statement applies to 5705 and 5750 PCI devices only */
7990 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
7991 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) ||
7992 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007993 if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07007994 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007995 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
7996 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
7997 !(tp->tg3_flags2 & TG3_FLG2_IS_5788)) {
7998 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
7999 }
8000 }
8001
Michael Chan85e94ce2005-04-21 17:05:28 -07008002 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)
8003 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8004
Linus Torvalds1da177e2005-04-16 15:20:36 -07008005 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
Matt Carlson027455a2008-12-21 20:19:30 -08008006 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
8007
Matt Carlsone849cdc2009-11-13 13:03:38 +00008008 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
8009 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson027455a2008-12-21 20:19:30 -08008010 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
8011 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008012
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008013 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
8014 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
8015 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8016 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
8017 (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)) {
8018 val = tr32(TG3_RDMA_RSRVCTRL_REG);
8019 tw32(TG3_RDMA_RSRVCTRL_REG,
8020 val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
8021 }
8022
Linus Torvalds1da177e2005-04-16 15:20:36 -07008023 /* Receive/send statistics. */
Michael Chan16613942006-06-29 20:15:13 -07008024 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
8025 val = tr32(RCVLPC_STATS_ENABLE);
8026 val &= ~RCVLPC_STATSENAB_DACK_FIX;
8027 tw32(RCVLPC_STATS_ENABLE, val);
8028 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
8029 (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008030 val = tr32(RCVLPC_STATS_ENABLE);
8031 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
8032 tw32(RCVLPC_STATS_ENABLE, val);
8033 } else {
8034 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
8035 }
8036 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
8037 tw32(SNDDATAI_STATSENAB, 0xffffff);
8038 tw32(SNDDATAI_STATSCTRL,
8039 (SNDDATAI_SCTRL_ENABLE |
8040 SNDDATAI_SCTRL_FASTUPD));
8041
8042 /* Setup host coalescing engine. */
8043 tw32(HOSTCC_MODE, 0);
8044 for (i = 0; i < 2000; i++) {
8045 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
8046 break;
8047 udelay(10);
8048 }
8049
Michael Chand244c892005-07-05 14:42:33 -07008050 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008051
Linus Torvalds1da177e2005-04-16 15:20:36 -07008052 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
8053 /* Status/statistics block address. See tg3_timer,
8054 * the tg3_periodic_fetch_stats call there, and
8055 * tg3_get_stats to see how this works for 5705/5750 chips.
8056 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008057 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8058 ((u64) tp->stats_mapping >> 32));
8059 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8060 ((u64) tp->stats_mapping & 0xffffffff));
8061 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008062
Linus Torvalds1da177e2005-04-16 15:20:36 -07008063 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008064
8065 /* Clear statistics and status block memory areas */
8066 for (i = NIC_SRAM_STATS_BLK;
8067 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
8068 i += sizeof(u32)) {
8069 tg3_write_mem(tp, i, 0);
8070 udelay(40);
8071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008072 }
8073
8074 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
8075
8076 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
8077 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
8078 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
8079 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
8080
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008081 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
8082 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -07008083 /* reset to prevent losing 1st rx packet intermittently */
8084 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8085 udelay(10);
8086 }
8087
Matt Carlson3bda1252008-08-15 14:08:22 -07008088 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
8089 tp->mac_mode &= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
8090 else
8091 tp->mac_mode = 0;
8092 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Linus Torvalds1da177e2005-04-16 15:20:36 -07008093 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE | MAC_MODE_FHDE_ENABLE;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07008094 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008095 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07008096 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
8097 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008098 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
8099 udelay(40);
8100
Michael Chan314fba32005-04-21 17:07:04 -07008101 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Michael Chan9d26e212006-12-07 00:21:14 -08008102 * If TG3_FLG2_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07008103 * register to preserve the GPIO settings for LOMs. The GPIOs,
8104 * whether used as inputs or outputs, are set by boot code after
8105 * reset.
8106 */
Michael Chan9d26e212006-12-07 00:21:14 -08008107 if (!(tp->tg3_flags2 & TG3_FLG2_IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07008108 u32 gpio_mask;
8109
Michael Chan9d26e212006-12-07 00:21:14 -08008110 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
8111 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
8112 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07008113
8114 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
8115 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
8116 GRC_LCLCTRL_GPIO_OUTPUT3;
8117
Michael Chanaf36e6b2006-03-23 01:28:06 -08008118 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
8119 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
8120
Gary Zambranoaaf84462007-05-05 11:51:45 -07008121 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07008122 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
8123
8124 /* GPIO1 must be driven high for eeprom write protect */
Michael Chan9d26e212006-12-07 00:21:14 -08008125 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT)
8126 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
8127 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07008128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008129 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8130 udelay(100);
8131
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008132 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX) {
8133 val = tr32(MSGINT_MODE);
8134 val |= MSGINT_MODE_MULTIVEC_EN | MSGINT_MODE_ENABLE;
8135 tw32(MSGINT_MODE, val);
8136 }
8137
Linus Torvalds1da177e2005-04-16 15:20:36 -07008138 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
8139 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
8140 udelay(40);
8141 }
8142
8143 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
8144 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
8145 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
8146 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
8147 WDMAC_MODE_LNGREAD_ENAB);
8148
Michael Chan85e94ce2005-04-21 17:05:28 -07008149 /* If statement applies to 5705 and 5750 PCI devices only */
8150 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8151 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) ||
8152 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750) {
Matt Carlson29ea0952009-08-25 10:07:54 +00008153 if ((tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008154 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
8155 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
8156 /* nothing */
8157 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
8158 !(tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
8159 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)) {
8160 val |= WDMAC_MODE_RX_ACCEL;
8161 }
8162 }
8163
Michael Chand9ab5ad2006-03-20 22:27:35 -08008164 /* Enable host coalescing bug fix */
Matt Carlson321d32a2008-11-21 17:22:19 -08008165 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Matt Carlsonf51f3562008-05-25 23:45:08 -07008166 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad2006-03-20 22:27:35 -08008167
Matt Carlson788a0352009-11-02 14:26:03 +00008168 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
8169 val |= WDMAC_MODE_BURST_ALL_DATA;
8170
Linus Torvalds1da177e2005-04-16 15:20:36 -07008171 tw32_f(WDMAC_MODE, val);
8172 udelay(40);
8173
Matt Carlson9974a352007-10-07 23:27:28 -07008174 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
8175 u16 pcix_cmd;
8176
8177 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8178 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008179 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07008180 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
8181 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008182 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07008183 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
8184 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008185 }
Matt Carlson9974a352007-10-07 23:27:28 -07008186 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8187 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008188 }
8189
8190 tw32_f(RDMAC_MODE, rdmac_mode);
8191 udelay(40);
8192
8193 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
8194 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
8195 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07008196
8197 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
8198 tw32(SNDDATAC_MODE,
8199 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
8200 else
8201 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
8202
Linus Torvalds1da177e2005-04-16 15:20:36 -07008203 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
8204 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
8205 tw32(RCVDBDI_MODE, RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ);
8206 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008207 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
8208 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008209 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Matt Carlson19cfaec2009-12-03 08:36:20 +00008210 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008211 val |= SNDBDI_MODE_MULTI_TXQ_EN;
8212 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008213 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
8214
8215 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
8216 err = tg3_load_5701_a0_firmware_fix(tp);
8217 if (err)
8218 return err;
8219 }
8220
Linus Torvalds1da177e2005-04-16 15:20:36 -07008221 if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) {
8222 err = tg3_load_tso_firmware(tp);
8223 if (err)
8224 return err;
8225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008226
8227 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonb1d05212010-06-05 17:24:31 +00008228 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
8229 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
8230 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008231 tw32_f(MAC_TX_MODE, tp->tx_mode);
8232 udelay(100);
8233
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008234 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS) {
8235 u32 reg = MAC_RSS_INDIR_TBL_0;
8236 u8 *ent = (u8 *)&val;
8237
8238 /* Setup the indirection table */
8239 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
8240 int idx = i % sizeof(val);
8241
Matt Carlson5efeeea2010-07-11 09:31:40 +00008242 ent[idx] = i % (tp->irq_cnt - 1);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008243 if (idx == sizeof(val) - 1) {
8244 tw32(reg, val);
8245 reg += 4;
8246 }
8247 }
8248
8249 /* Setup the "secret" hash key. */
8250 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
8251 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
8252 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
8253 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
8254 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
8255 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
8256 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
8257 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
8258 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
8259 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
8260 }
8261
Linus Torvalds1da177e2005-04-16 15:20:36 -07008262 tp->rx_mode = RX_MODE_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08008263 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Michael Chanaf36e6b2006-03-23 01:28:06 -08008264 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
8265
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008266 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)
8267 tp->rx_mode |= RX_MODE_RSS_ENABLE |
8268 RX_MODE_RSS_ITBL_HASH_BITS_7 |
8269 RX_MODE_RSS_IPV6_HASH_EN |
8270 RX_MODE_RSS_TCP_IPV6_HASH_EN |
8271 RX_MODE_RSS_IPV4_HASH_EN |
8272 RX_MODE_RSS_TCP_IPV4_HASH_EN;
8273
Linus Torvalds1da177e2005-04-16 15:20:36 -07008274 tw32_f(MAC_RX_MODE, tp->rx_mode);
8275 udelay(10);
8276
Linus Torvalds1da177e2005-04-16 15:20:36 -07008277 tw32(MAC_LED_CTRL, tp->led_ctrl);
8278
8279 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008280 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008281 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8282 udelay(10);
8283 }
8284 tw32_f(MAC_RX_MODE, tp->rx_mode);
8285 udelay(10);
8286
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008287 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008288 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008289 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008290 /* Set drive transmission level to 1.2V */
8291 /* only if the signal pre-emphasis bit is not set */
8292 val = tr32(MAC_SERDES_CFG);
8293 val &= 0xfffff000;
8294 val |= 0x880;
8295 tw32(MAC_SERDES_CFG, val);
8296 }
8297 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
8298 tw32(MAC_SERDES_CFG, 0x616000);
8299 }
8300
8301 /* Prevent chip from dropping frames when flow control
8302 * is enabled.
8303 */
Matt Carlson666bc832010-01-20 16:58:03 +00008304 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8305 val = 1;
8306 else
8307 val = 2;
8308 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008309
8310 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008311 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008312 /* Use hardware link auto-negotiation */
8313 tp->tg3_flags2 |= TG3_FLG2_HW_AUTONEG;
8314 }
8315
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008316 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Michael Chand4d2c552006-03-20 17:47:20 -08008317 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)) {
8318 u32 tmp;
8319
8320 tmp = tr32(SERDES_RX_CTRL);
8321 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
8322 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
8323 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
8324 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8325 }
8326
Matt Carlsondd477002008-05-25 23:45:58 -07008327 if (!(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)) {
Matt Carlson800960682010-08-02 11:26:06 +00008328 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
8329 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07008330 tp->link_config.speed = tp->link_config.orig_speed;
8331 tp->link_config.duplex = tp->link_config.orig_duplex;
8332 tp->link_config.autoneg = tp->link_config.orig_autoneg;
8333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008334
Matt Carlsondd477002008-05-25 23:45:58 -07008335 err = tg3_setup_phy(tp, 0);
8336 if (err)
8337 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008338
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008339 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
8340 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07008341 u32 tmp;
8342
8343 /* Clear CRC stats. */
8344 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
8345 tg3_writephy(tp, MII_TG3_TEST1,
8346 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00008347 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -07008348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008349 }
8350 }
8351
8352 __tg3_set_rx_mode(tp->dev);
8353
8354 /* Initialize receive rules. */
8355 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
8356 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
8357 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
8358 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
8359
Michael Chan4cf78e42005-07-25 12:29:19 -07008360 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Michael Chana4e2b342005-10-26 15:46:52 -07008361 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008362 limit = 8;
8363 else
8364 limit = 16;
8365 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF)
8366 limit -= 4;
8367 switch (limit) {
8368 case 16:
8369 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
8370 case 15:
8371 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
8372 case 14:
8373 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
8374 case 13:
8375 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
8376 case 12:
8377 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
8378 case 11:
8379 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
8380 case 10:
8381 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
8382 case 9:
8383 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
8384 case 8:
8385 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
8386 case 7:
8387 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
8388 case 6:
8389 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
8390 case 5:
8391 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
8392 case 4:
8393 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
8394 case 3:
8395 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
8396 case 2:
8397 case 1:
8398
8399 default:
8400 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07008401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008402
Matt Carlson9ce768e2007-10-11 19:49:11 -07008403 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
8404 /* Write our heartbeat update interval to APE. */
8405 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
8406 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07008407
Linus Torvalds1da177e2005-04-16 15:20:36 -07008408 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
8409
Linus Torvalds1da177e2005-04-16 15:20:36 -07008410 return 0;
8411}
8412
8413/* Called at device open time to get the chip ready for
8414 * packet processing. Invoked with tp->lock held.
8415 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008416static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008417{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008418 tg3_switch_clocks(tp);
8419
8420 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
8421
Matt Carlson2f751b62008-08-04 23:17:34 -07008422 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008423}
8424
8425#define TG3_STAT_ADD32(PSTAT, REG) \
8426do { u32 __val = tr32(REG); \
8427 (PSTAT)->low += __val; \
8428 if ((PSTAT)->low < __val) \
8429 (PSTAT)->high += 1; \
8430} while (0)
8431
8432static void tg3_periodic_fetch_stats(struct tg3 *tp)
8433{
8434 struct tg3_hw_stats *sp = tp->hw_stats;
8435
8436 if (!netif_carrier_ok(tp->dev))
8437 return;
8438
8439 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
8440 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
8441 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
8442 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
8443 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
8444 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
8445 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
8446 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
8447 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
8448 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
8449 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
8450 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
8451 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
8452
8453 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
8454 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
8455 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
8456 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
8457 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
8458 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
8459 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
8460 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
8461 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
8462 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
8463 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
8464 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
8465 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
8466 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07008467
8468 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
8469 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
8470 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008471}
8472
8473static void tg3_timer(unsigned long __opaque)
8474{
8475 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008476
Michael Chanf475f162006-03-27 23:20:14 -08008477 if (tp->irq_sync)
8478 goto restart_timer;
8479
David S. Millerf47c11e2005-06-24 20:18:35 -07008480 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008481
David S. Millerfac9b832005-05-18 22:46:34 -07008482 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)) {
8483 /* All of this garbage is because when using non-tagged
8484 * IRQ status the mailbox/status_block protocol the chip
8485 * uses with the cpu is race prone.
8486 */
Matt Carlson898a56f2009-08-28 14:02:40 +00008487 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -07008488 tw32(GRC_LOCAL_CTRL,
8489 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
8490 } else {
8491 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00008492 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -07008493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008494
David S. Millerfac9b832005-05-18 22:46:34 -07008495 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
8496 tp->tg3_flags2 |= TG3_FLG2_RESTART_TIMER;
David S. Millerf47c11e2005-06-24 20:18:35 -07008497 spin_unlock(&tp->lock);
David S. Millerfac9b832005-05-18 22:46:34 -07008498 schedule_work(&tp->reset_task);
8499 return;
8500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008501 }
8502
Linus Torvalds1da177e2005-04-16 15:20:36 -07008503 /* This part only runs once per second. */
8504 if (!--tp->timer_counter) {
David S. Millerfac9b832005-05-18 22:46:34 -07008505 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
8506 tg3_periodic_fetch_stats(tp);
8507
Linus Torvalds1da177e2005-04-16 15:20:36 -07008508 if (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) {
8509 u32 mac_stat;
8510 int phy_event;
8511
8512 mac_stat = tr32(MAC_STATUS);
8513
8514 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008515 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008516 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
8517 phy_event = 1;
8518 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
8519 phy_event = 1;
8520
8521 if (phy_event)
8522 tg3_setup_phy(tp, 0);
8523 } else if (tp->tg3_flags & TG3_FLAG_POLL_SERDES) {
8524 u32 mac_stat = tr32(MAC_STATUS);
8525 int need_setup = 0;
8526
8527 if (netif_carrier_ok(tp->dev) &&
8528 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
8529 need_setup = 1;
8530 }
Matt Carlsonbe98da62010-07-11 09:31:46 +00008531 if (!netif_carrier_ok(tp->dev) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008532 (mac_stat & (MAC_STATUS_PCS_SYNCED |
8533 MAC_STATUS_SIGNAL_DET))) {
8534 need_setup = 1;
8535 }
8536 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07008537 if (!tp->serdes_counter) {
8538 tw32_f(MAC_MODE,
8539 (tp->mac_mode &
8540 ~MAC_MODE_PORT_MODE_MASK));
8541 udelay(40);
8542 tw32_f(MAC_MODE, tp->mac_mode);
8543 udelay(40);
8544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008545 tg3_setup_phy(tp, 0);
8546 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008547 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Matt Carlson2138c002010-07-11 09:31:43 +00008548 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -07008549 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +00008550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008551
8552 tp->timer_counter = tp->timer_multiplier;
8553 }
8554
Michael Chan130b8e42006-09-27 16:00:40 -07008555 /* Heartbeat is only sent once every 2 seconds.
8556 *
8557 * The heartbeat is to tell the ASF firmware that the host
8558 * driver is still alive. In the event that the OS crashes,
8559 * ASF needs to reset the hardware to free up the FIFO space
8560 * that may be filled with rx packets destined for the host.
8561 * If the FIFO is full, ASF will no longer function properly.
8562 *
8563 * Unintended resets have been reported on real time kernels
8564 * where the timer doesn't run on time. Netpoll will also have
8565 * same problem.
8566 *
8567 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
8568 * to check the ring condition when the heartbeat is expiring
8569 * before doing the reset. This will prevent most unintended
8570 * resets.
8571 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008572 if (!--tp->asf_counter) {
Matt Carlsonbc7959b2008-08-15 14:08:55 -07008573 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
8574 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07008575 tg3_wait_for_event_ack(tp);
8576
Michael Chanbbadf502006-04-06 21:46:34 -07008577 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07008578 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07008579 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +00008580 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
8581 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -07008582
8583 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008584 }
8585 tp->asf_counter = tp->asf_multiplier;
8586 }
8587
David S. Millerf47c11e2005-06-24 20:18:35 -07008588 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008589
Michael Chanf475f162006-03-27 23:20:14 -08008590restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07008591 tp->timer.expires = jiffies + tp->timer_offset;
8592 add_timer(&tp->timer);
8593}
8594
Matt Carlson4f125f42009-09-01 12:55:02 +00008595static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -08008596{
David Howells7d12e782006-10-05 14:55:46 +01008597 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008598 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +00008599 char *name;
8600 struct tg3_napi *tnapi = &tp->napi[irq_num];
8601
8602 if (tp->irq_cnt == 1)
8603 name = tp->dev->name;
8604 else {
8605 name = &tnapi->irq_lbl[0];
8606 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
8607 name[IFNAMSIZ-1] = 0;
8608 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08008609
Matt Carlson679563f2009-09-01 12:55:46 +00008610 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI_OR_MSIX) {
Michael Chanfcfa0a32006-03-20 22:28:41 -08008611 fn = tg3_msi;
8612 if (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)
8613 fn = tg3_msi_1shot;
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07008614 flags = IRQF_SAMPLE_RANDOM;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008615 } else {
8616 fn = tg3_interrupt;
8617 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)
8618 fn = tg3_interrupt_tagged;
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07008619 flags = IRQF_SHARED | IRQF_SAMPLE_RANDOM;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008620 }
Matt Carlson4f125f42009-09-01 12:55:02 +00008621
8622 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08008623}
8624
Michael Chan79381092005-04-21 17:13:59 -07008625static int tg3_test_interrupt(struct tg3 *tp)
8626{
Matt Carlson09943a12009-08-28 14:01:57 +00008627 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -07008628 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -07008629 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008630 u32 val;
Michael Chan79381092005-04-21 17:13:59 -07008631
Michael Chand4bc3922005-05-29 14:59:20 -07008632 if (!netif_running(dev))
8633 return -ENODEV;
8634
Michael Chan79381092005-04-21 17:13:59 -07008635 tg3_disable_ints(tp);
8636
Matt Carlson4f125f42009-09-01 12:55:02 +00008637 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07008638
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008639 /*
8640 * Turn off MSI one shot mode. Otherwise this test has no
8641 * observable way to know whether the interrupt was delivered.
8642 */
Matt Carlsonc885e822010-08-02 11:25:57 +00008643 if ((tp->tg3_flags3 & TG3_FLG3_5717_PLUS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008644 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
8645 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
8646 tw32(MSGINT_MODE, val);
8647 }
8648
Matt Carlson4f125f42009-09-01 12:55:02 +00008649 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Matt Carlson09943a12009-08-28 14:01:57 +00008650 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07008651 if (err)
8652 return err;
8653
Matt Carlson898a56f2009-08-28 14:02:40 +00008654 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -07008655 tg3_enable_ints(tp);
8656
8657 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00008658 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -07008659
8660 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -07008661 u32 int_mbox, misc_host_ctrl;
8662
Matt Carlson898a56f2009-08-28 14:02:40 +00008663 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -07008664 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
8665
8666 if ((int_mbox != 0) ||
8667 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
8668 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -07008669 break;
Michael Chanb16250e2006-09-27 16:10:14 -07008670 }
8671
Michael Chan79381092005-04-21 17:13:59 -07008672 msleep(10);
8673 }
8674
8675 tg3_disable_ints(tp);
8676
Matt Carlson4f125f42009-09-01 12:55:02 +00008677 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008678
Matt Carlson4f125f42009-09-01 12:55:02 +00008679 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07008680
8681 if (err)
8682 return err;
8683
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008684 if (intr_ok) {
8685 /* Reenable MSI one shot mode. */
Matt Carlsonc885e822010-08-02 11:25:57 +00008686 if ((tp->tg3_flags3 & TG3_FLG3_5717_PLUS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008687 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
8688 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
8689 tw32(MSGINT_MODE, val);
8690 }
Michael Chan79381092005-04-21 17:13:59 -07008691 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008692 }
Michael Chan79381092005-04-21 17:13:59 -07008693
8694 return -EIO;
8695}
8696
8697/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
8698 * successfully restored
8699 */
8700static int tg3_test_msi(struct tg3 *tp)
8701{
Michael Chan79381092005-04-21 17:13:59 -07008702 int err;
8703 u16 pci_cmd;
8704
8705 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSI))
8706 return 0;
8707
8708 /* Turn off SERR reporting in case MSI terminates with Master
8709 * Abort.
8710 */
8711 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
8712 pci_write_config_word(tp->pdev, PCI_COMMAND,
8713 pci_cmd & ~PCI_COMMAND_SERR);
8714
8715 err = tg3_test_interrupt(tp);
8716
8717 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
8718
8719 if (!err)
8720 return 0;
8721
8722 /* other failures */
8723 if (err != -EIO)
8724 return err;
8725
8726 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +00008727 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
8728 "to INTx mode. Please report this failure to the PCI "
8729 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -07008730
Matt Carlson4f125f42009-09-01 12:55:02 +00008731 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +00008732
Michael Chan79381092005-04-21 17:13:59 -07008733 pci_disable_msi(tp->pdev);
8734
8735 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI;
Andre Detschdc8bf1b2010-04-26 07:27:07 +00008736 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -07008737
Matt Carlson4f125f42009-09-01 12:55:02 +00008738 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07008739 if (err)
8740 return err;
8741
8742 /* Need to reset the chip because the MSI cycle may have terminated
8743 * with Master Abort.
8744 */
David S. Millerf47c11e2005-06-24 20:18:35 -07008745 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07008746
Michael Chan944d9802005-05-29 14:57:48 -07008747 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008748 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07008749
David S. Millerf47c11e2005-06-24 20:18:35 -07008750 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07008751
8752 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +00008753 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -07008754
8755 return err;
8756}
8757
Matt Carlson9e9fd122009-01-19 16:57:45 -08008758static int tg3_request_firmware(struct tg3 *tp)
8759{
8760 const __be32 *fw_data;
8761
8762 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00008763 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
8764 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08008765 return -ENOENT;
8766 }
8767
8768 fw_data = (void *)tp->fw->data;
8769
8770 /* Firmware blob starts with version numbers, followed by
8771 * start address and _full_ length including BSS sections
8772 * (which must be longer than the actual data, of course
8773 */
8774
8775 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
8776 if (tp->fw_len < (tp->fw->size - 12)) {
Joe Perches05dbe002010-02-17 19:44:19 +00008777 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
8778 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08008779 release_firmware(tp->fw);
8780 tp->fw = NULL;
8781 return -EINVAL;
8782 }
8783
8784 /* We no longer need firmware; we have it. */
8785 tp->fw_needed = NULL;
8786 return 0;
8787}
8788
Matt Carlson679563f2009-09-01 12:55:46 +00008789static bool tg3_enable_msix(struct tg3 *tp)
8790{
8791 int i, rc, cpus = num_online_cpus();
8792 struct msix_entry msix_ent[tp->irq_max];
8793
8794 if (cpus == 1)
8795 /* Just fallback to the simpler MSI mode. */
8796 return false;
8797
8798 /*
8799 * We want as many rx rings enabled as there are cpus.
8800 * The first MSIX vector only deals with link interrupts, etc,
8801 * so we add one to the number of vectors we are requesting.
8802 */
8803 tp->irq_cnt = min_t(unsigned, cpus + 1, tp->irq_max);
8804
8805 for (i = 0; i < tp->irq_max; i++) {
8806 msix_ent[i].entry = i;
8807 msix_ent[i].vector = 0;
8808 }
8809
8810 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +00008811 if (rc < 0) {
8812 return false;
8813 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +00008814 if (pci_enable_msix(tp->pdev, msix_ent, rc))
8815 return false;
Joe Perches05dbe002010-02-17 19:44:19 +00008816 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
8817 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +00008818 tp->irq_cnt = rc;
8819 }
8820
8821 for (i = 0; i < tp->irq_max; i++)
8822 tp->napi[i].irq_vec = msix_ent[i].vector;
8823
Matt Carlson2430b032010-06-05 17:24:34 +00008824 tp->dev->real_num_tx_queues = 1;
Matt Carlsonf0392d22010-09-15 08:59:54 +00008825 if (tp->irq_cnt > 1)
Matt Carlson2430b032010-06-05 17:24:34 +00008826 tp->tg3_flags3 |= TG3_FLG3_ENABLE_RSS;
8827
Matt Carlson679563f2009-09-01 12:55:46 +00008828 return true;
8829}
8830
Matt Carlson07b01732009-08-28 14:01:15 +00008831static void tg3_ints_init(struct tg3 *tp)
8832{
Matt Carlson679563f2009-09-01 12:55:46 +00008833 if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSI_OR_MSIX) &&
8834 !(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +00008835 /* All MSI supporting chips should support tagged
8836 * status. Assert that this is the case.
8837 */
Matt Carlson5129c3a2010-04-05 10:19:23 +00008838 netdev_warn(tp->dev,
8839 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +00008840 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +00008841 }
Matt Carlson4f125f42009-09-01 12:55:02 +00008842
Matt Carlson679563f2009-09-01 12:55:46 +00008843 if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX) && tg3_enable_msix(tp))
8844 tp->tg3_flags2 |= TG3_FLG2_USING_MSIX;
8845 else if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSI) &&
8846 pci_enable_msi(tp->pdev) == 0)
8847 tp->tg3_flags2 |= TG3_FLG2_USING_MSI;
8848
8849 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI_OR_MSIX) {
8850 u32 msi_mode = tr32(MSGINT_MODE);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008851 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
8852 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson679563f2009-09-01 12:55:46 +00008853 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
8854 }
8855defcfg:
8856 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSIX)) {
8857 tp->irq_cnt = 1;
8858 tp->napi[0].irq_vec = tp->pdev->irq;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00008859 tp->dev->real_num_tx_queues = 1;
Matt Carlson679563f2009-09-01 12:55:46 +00008860 }
Matt Carlson07b01732009-08-28 14:01:15 +00008861}
8862
8863static void tg3_ints_fini(struct tg3 *tp)
8864{
Matt Carlson679563f2009-09-01 12:55:46 +00008865 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
8866 pci_disable_msix(tp->pdev);
8867 else if (tp->tg3_flags2 & TG3_FLG2_USING_MSI)
8868 pci_disable_msi(tp->pdev);
8869 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI_OR_MSIX;
Matt Carlson774ee752010-08-02 11:25:56 +00008870 tp->tg3_flags3 &= ~(TG3_FLG3_ENABLE_RSS | TG3_FLG3_ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +00008871}
8872
Linus Torvalds1da177e2005-04-16 15:20:36 -07008873static int tg3_open(struct net_device *dev)
8874{
8875 struct tg3 *tp = netdev_priv(dev);
Matt Carlson4f125f42009-09-01 12:55:02 +00008876 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008877
Matt Carlson9e9fd122009-01-19 16:57:45 -08008878 if (tp->fw_needed) {
8879 err = tg3_request_firmware(tp);
8880 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
8881 if (err)
8882 return err;
8883 } else if (err) {
Joe Perches05dbe002010-02-17 19:44:19 +00008884 netdev_warn(tp->dev, "TSO capability disabled\n");
Matt Carlson9e9fd122009-01-19 16:57:45 -08008885 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
8886 } else if (!(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
Joe Perches05dbe002010-02-17 19:44:19 +00008887 netdev_notice(tp->dev, "TSO capability restored\n");
Matt Carlson9e9fd122009-01-19 16:57:45 -08008888 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
8889 }
8890 }
8891
Michael Chanc49a1562006-12-17 17:07:29 -08008892 netif_carrier_off(tp->dev);
8893
Michael Chanbc1c7562006-03-20 17:48:03 -08008894 err = tg3_set_power_state(tp, PCI_D0);
Matt Carlson2f751b62008-08-04 23:17:34 -07008895 if (err)
Michael Chanbc1c7562006-03-20 17:48:03 -08008896 return err;
Matt Carlson2f751b62008-08-04 23:17:34 -07008897
8898 tg3_full_lock(tp, 0);
Michael Chanbc1c7562006-03-20 17:48:03 -08008899
Linus Torvalds1da177e2005-04-16 15:20:36 -07008900 tg3_disable_ints(tp);
8901 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
8902
David S. Millerf47c11e2005-06-24 20:18:35 -07008903 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008904
Matt Carlson679563f2009-09-01 12:55:46 +00008905 /*
8906 * Setup interrupts first so we know how
8907 * many NAPI resources to allocate
8908 */
8909 tg3_ints_init(tp);
8910
Linus Torvalds1da177e2005-04-16 15:20:36 -07008911 /* The placement of this call is tied
8912 * to the setup and use of Host TX descriptors.
8913 */
8914 err = tg3_alloc_consistent(tp);
8915 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00008916 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008917
Matt Carlsonfed97812009-09-01 13:10:19 +00008918 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07008919
Matt Carlson4f125f42009-09-01 12:55:02 +00008920 for (i = 0; i < tp->irq_cnt; i++) {
8921 struct tg3_napi *tnapi = &tp->napi[i];
8922 err = tg3_request_irq(tp, i);
8923 if (err) {
8924 for (i--; i >= 0; i--)
8925 free_irq(tnapi->irq_vec, tnapi);
8926 break;
8927 }
8928 }
Matt Carlson07b01732009-08-28 14:01:15 +00008929
8930 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00008931 goto err_out2;
Matt Carlson07b01732009-08-28 14:01:15 +00008932
David S. Millerf47c11e2005-06-24 20:18:35 -07008933 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008934
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008935 err = tg3_init_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008936 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -07008937 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008938 tg3_free_rings(tp);
8939 } else {
David S. Millerfac9b832005-05-18 22:46:34 -07008940 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)
8941 tp->timer_offset = HZ;
8942 else
8943 tp->timer_offset = HZ / 10;
8944
8945 BUG_ON(tp->timer_offset > HZ);
8946 tp->timer_counter = tp->timer_multiplier =
8947 (HZ / tp->timer_offset);
8948 tp->asf_counter = tp->asf_multiplier =
Michael Chan28fbef72005-10-26 15:48:35 -07008949 ((HZ / tp->timer_offset) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008950
8951 init_timer(&tp->timer);
8952 tp->timer.expires = jiffies + tp->timer_offset;
8953 tp->timer.data = (unsigned long) tp;
8954 tp->timer.function = tg3_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008955 }
8956
David S. Millerf47c11e2005-06-24 20:18:35 -07008957 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008958
Matt Carlson07b01732009-08-28 14:01:15 +00008959 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00008960 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008961
Michael Chan79381092005-04-21 17:13:59 -07008962 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
8963 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -07008964
Michael Chan79381092005-04-21 17:13:59 -07008965 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -07008966 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -07008967 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -07008968 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07008969 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07008970
Matt Carlson679563f2009-09-01 12:55:46 +00008971 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -07008972 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08008973
Matt Carlsonc885e822010-08-02 11:25:57 +00008974 if (!(tp->tg3_flags3 & TG3_FLG3_5717_PLUS) &&
8975 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008976 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -08008977
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008978 tw32(PCIE_TRANSACTION_CFG,
8979 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -08008980 }
Michael Chan79381092005-04-21 17:13:59 -07008981 }
8982
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07008983 tg3_phy_start(tp);
8984
David S. Millerf47c11e2005-06-24 20:18:35 -07008985 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008986
Michael Chan79381092005-04-21 17:13:59 -07008987 add_timer(&tp->timer);
8988 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008989 tg3_enable_ints(tp);
8990
David S. Millerf47c11e2005-06-24 20:18:35 -07008991 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008992
Matt Carlsonfe5f5782009-09-01 13:09:39 +00008993 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008994
8995 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +00008996
Matt Carlson679563f2009-09-01 12:55:46 +00008997err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +00008998 for (i = tp->irq_cnt - 1; i >= 0; i--) {
8999 struct tg3_napi *tnapi = &tp->napi[i];
9000 free_irq(tnapi->irq_vec, tnapi);
9001 }
Matt Carlson07b01732009-08-28 14:01:15 +00009002
Matt Carlson679563f2009-09-01 12:55:46 +00009003err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +00009004 tg3_napi_disable(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00009005 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +00009006
9007err_out1:
9008 tg3_ints_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00009009 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009010}
9011
Eric Dumazet511d2222010-07-07 20:44:24 +00009012static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *,
9013 struct rtnl_link_stats64 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009014static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *);
9015
9016static int tg3_close(struct net_device *dev)
9017{
Matt Carlson4f125f42009-09-01 12:55:02 +00009018 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009019 struct tg3 *tp = netdev_priv(dev);
9020
Matt Carlsonfed97812009-09-01 13:10:19 +00009021 tg3_napi_disable(tp);
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07009022 cancel_work_sync(&tp->reset_task);
Michael Chan7faa0062006-02-02 17:29:28 -08009023
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009024 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009025
9026 del_timer_sync(&tp->timer);
9027
Matt Carlson24bb4fb2009-10-05 17:55:29 +00009028 tg3_phy_stop(tp);
9029
David S. Millerf47c11e2005-06-24 20:18:35 -07009030 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009031
9032 tg3_disable_ints(tp);
9033
Michael Chan944d9802005-05-29 14:57:48 -07009034 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009035 tg3_free_rings(tp);
Michael Chan5cf64b8a2007-05-05 12:11:21 -07009036 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009037
David S. Millerf47c11e2005-06-24 20:18:35 -07009038 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009039
Matt Carlson4f125f42009-09-01 12:55:02 +00009040 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9041 struct tg3_napi *tnapi = &tp->napi[i];
9042 free_irq(tnapi->irq_vec, tnapi);
9043 }
Matt Carlson07b01732009-08-28 14:01:15 +00009044
9045 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009046
Eric Dumazet511d2222010-07-07 20:44:24 +00009047 tg3_get_stats64(tp->dev, &tp->net_stats_prev);
9048
Linus Torvalds1da177e2005-04-16 15:20:36 -07009049 memcpy(&tp->estats_prev, tg3_get_estats(tp),
9050 sizeof(tp->estats_prev));
9051
9052 tg3_free_consistent(tp);
9053
Michael Chanbc1c7562006-03-20 17:48:03 -08009054 tg3_set_power_state(tp, PCI_D3hot);
9055
9056 netif_carrier_off(tp->dev);
9057
Linus Torvalds1da177e2005-04-16 15:20:36 -07009058 return 0;
9059}
9060
Eric Dumazet511d2222010-07-07 20:44:24 +00009061static inline u64 get_stat64(tg3_stat64_t *val)
Stefan Buehler816f8b82008-08-15 14:10:54 -07009062{
9063 return ((u64)val->high << 32) | ((u64)val->low);
9064}
9065
Eric Dumazet511d2222010-07-07 20:44:24 +00009066static u64 calc_crc_errors(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009067{
9068 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9069
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009070 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009071 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
9072 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009073 u32 val;
9074
David S. Millerf47c11e2005-06-24 20:18:35 -07009075 spin_lock_bh(&tp->lock);
Michael Chan569a5df2007-02-13 12:18:15 -08009076 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
9077 tg3_writephy(tp, MII_TG3_TEST1,
9078 val | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009079 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009080 } else
9081 val = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07009082 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009083
9084 tp->phy_crc_errors += val;
9085
9086 return tp->phy_crc_errors;
9087 }
9088
9089 return get_stat64(&hw_stats->rx_fcs_errors);
9090}
9091
9092#define ESTAT_ADD(member) \
9093 estats->member = old_estats->member + \
Eric Dumazet511d2222010-07-07 20:44:24 +00009094 get_stat64(&hw_stats->member)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009095
9096static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *tp)
9097{
9098 struct tg3_ethtool_stats *estats = &tp->estats;
9099 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
9100 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9101
9102 if (!hw_stats)
9103 return old_estats;
9104
9105 ESTAT_ADD(rx_octets);
9106 ESTAT_ADD(rx_fragments);
9107 ESTAT_ADD(rx_ucast_packets);
9108 ESTAT_ADD(rx_mcast_packets);
9109 ESTAT_ADD(rx_bcast_packets);
9110 ESTAT_ADD(rx_fcs_errors);
9111 ESTAT_ADD(rx_align_errors);
9112 ESTAT_ADD(rx_xon_pause_rcvd);
9113 ESTAT_ADD(rx_xoff_pause_rcvd);
9114 ESTAT_ADD(rx_mac_ctrl_rcvd);
9115 ESTAT_ADD(rx_xoff_entered);
9116 ESTAT_ADD(rx_frame_too_long_errors);
9117 ESTAT_ADD(rx_jabbers);
9118 ESTAT_ADD(rx_undersize_packets);
9119 ESTAT_ADD(rx_in_length_errors);
9120 ESTAT_ADD(rx_out_length_errors);
9121 ESTAT_ADD(rx_64_or_less_octet_packets);
9122 ESTAT_ADD(rx_65_to_127_octet_packets);
9123 ESTAT_ADD(rx_128_to_255_octet_packets);
9124 ESTAT_ADD(rx_256_to_511_octet_packets);
9125 ESTAT_ADD(rx_512_to_1023_octet_packets);
9126 ESTAT_ADD(rx_1024_to_1522_octet_packets);
9127 ESTAT_ADD(rx_1523_to_2047_octet_packets);
9128 ESTAT_ADD(rx_2048_to_4095_octet_packets);
9129 ESTAT_ADD(rx_4096_to_8191_octet_packets);
9130 ESTAT_ADD(rx_8192_to_9022_octet_packets);
9131
9132 ESTAT_ADD(tx_octets);
9133 ESTAT_ADD(tx_collisions);
9134 ESTAT_ADD(tx_xon_sent);
9135 ESTAT_ADD(tx_xoff_sent);
9136 ESTAT_ADD(tx_flow_control);
9137 ESTAT_ADD(tx_mac_errors);
9138 ESTAT_ADD(tx_single_collisions);
9139 ESTAT_ADD(tx_mult_collisions);
9140 ESTAT_ADD(tx_deferred);
9141 ESTAT_ADD(tx_excessive_collisions);
9142 ESTAT_ADD(tx_late_collisions);
9143 ESTAT_ADD(tx_collide_2times);
9144 ESTAT_ADD(tx_collide_3times);
9145 ESTAT_ADD(tx_collide_4times);
9146 ESTAT_ADD(tx_collide_5times);
9147 ESTAT_ADD(tx_collide_6times);
9148 ESTAT_ADD(tx_collide_7times);
9149 ESTAT_ADD(tx_collide_8times);
9150 ESTAT_ADD(tx_collide_9times);
9151 ESTAT_ADD(tx_collide_10times);
9152 ESTAT_ADD(tx_collide_11times);
9153 ESTAT_ADD(tx_collide_12times);
9154 ESTAT_ADD(tx_collide_13times);
9155 ESTAT_ADD(tx_collide_14times);
9156 ESTAT_ADD(tx_collide_15times);
9157 ESTAT_ADD(tx_ucast_packets);
9158 ESTAT_ADD(tx_mcast_packets);
9159 ESTAT_ADD(tx_bcast_packets);
9160 ESTAT_ADD(tx_carrier_sense_errors);
9161 ESTAT_ADD(tx_discards);
9162 ESTAT_ADD(tx_errors);
9163
9164 ESTAT_ADD(dma_writeq_full);
9165 ESTAT_ADD(dma_write_prioq_full);
9166 ESTAT_ADD(rxbds_empty);
9167 ESTAT_ADD(rx_discards);
9168 ESTAT_ADD(rx_errors);
9169 ESTAT_ADD(rx_threshold_hit);
9170
9171 ESTAT_ADD(dma_readq_full);
9172 ESTAT_ADD(dma_read_prioq_full);
9173 ESTAT_ADD(tx_comp_queue_full);
9174
9175 ESTAT_ADD(ring_set_send_prod_index);
9176 ESTAT_ADD(ring_status_update);
9177 ESTAT_ADD(nic_irqs);
9178 ESTAT_ADD(nic_avoided_irqs);
9179 ESTAT_ADD(nic_tx_threshold_hit);
9180
9181 return estats;
9182}
9183
Eric Dumazet511d2222010-07-07 20:44:24 +00009184static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
9185 struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009186{
9187 struct tg3 *tp = netdev_priv(dev);
Eric Dumazet511d2222010-07-07 20:44:24 +00009188 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009189 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9190
9191 if (!hw_stats)
9192 return old_stats;
9193
9194 stats->rx_packets = old_stats->rx_packets +
9195 get_stat64(&hw_stats->rx_ucast_packets) +
9196 get_stat64(&hw_stats->rx_mcast_packets) +
9197 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009198
Linus Torvalds1da177e2005-04-16 15:20:36 -07009199 stats->tx_packets = old_stats->tx_packets +
9200 get_stat64(&hw_stats->tx_ucast_packets) +
9201 get_stat64(&hw_stats->tx_mcast_packets) +
9202 get_stat64(&hw_stats->tx_bcast_packets);
9203
9204 stats->rx_bytes = old_stats->rx_bytes +
9205 get_stat64(&hw_stats->rx_octets);
9206 stats->tx_bytes = old_stats->tx_bytes +
9207 get_stat64(&hw_stats->tx_octets);
9208
9209 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -07009210 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009211 stats->tx_errors = old_stats->tx_errors +
9212 get_stat64(&hw_stats->tx_errors) +
9213 get_stat64(&hw_stats->tx_mac_errors) +
9214 get_stat64(&hw_stats->tx_carrier_sense_errors) +
9215 get_stat64(&hw_stats->tx_discards);
9216
9217 stats->multicast = old_stats->multicast +
9218 get_stat64(&hw_stats->rx_mcast_packets);
9219 stats->collisions = old_stats->collisions +
9220 get_stat64(&hw_stats->tx_collisions);
9221
9222 stats->rx_length_errors = old_stats->rx_length_errors +
9223 get_stat64(&hw_stats->rx_frame_too_long_errors) +
9224 get_stat64(&hw_stats->rx_undersize_packets);
9225
9226 stats->rx_over_errors = old_stats->rx_over_errors +
9227 get_stat64(&hw_stats->rxbds_empty);
9228 stats->rx_frame_errors = old_stats->rx_frame_errors +
9229 get_stat64(&hw_stats->rx_align_errors);
9230 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
9231 get_stat64(&hw_stats->tx_discards);
9232 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
9233 get_stat64(&hw_stats->tx_carrier_sense_errors);
9234
9235 stats->rx_crc_errors = old_stats->rx_crc_errors +
9236 calc_crc_errors(tp);
9237
John W. Linville4f63b872005-09-12 14:43:18 -07009238 stats->rx_missed_errors = old_stats->rx_missed_errors +
9239 get_stat64(&hw_stats->rx_discards);
9240
Linus Torvalds1da177e2005-04-16 15:20:36 -07009241 return stats;
9242}
9243
9244static inline u32 calc_crc(unsigned char *buf, int len)
9245{
9246 u32 reg;
9247 u32 tmp;
9248 int j, k;
9249
9250 reg = 0xffffffff;
9251
9252 for (j = 0; j < len; j++) {
9253 reg ^= buf[j];
9254
9255 for (k = 0; k < 8; k++) {
9256 tmp = reg & 0x01;
9257
9258 reg >>= 1;
9259
Matt Carlson859a588792010-04-05 10:19:28 +00009260 if (tmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009261 reg ^= 0xedb88320;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009262 }
9263 }
9264
9265 return ~reg;
9266}
9267
9268static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
9269{
9270 /* accept or reject all multicast frames */
9271 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
9272 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
9273 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
9274 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
9275}
9276
9277static void __tg3_set_rx_mode(struct net_device *dev)
9278{
9279 struct tg3 *tp = netdev_priv(dev);
9280 u32 rx_mode;
9281
9282 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
9283 RX_MODE_KEEP_VLAN_TAG);
9284
9285 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
9286 * flag clear.
9287 */
9288#if TG3_VLAN_TAG_USED
9289 if (!tp->vlgrp &&
9290 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
9291 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9292#else
9293 /* By definition, VLAN is disabled always in this
9294 * case.
9295 */
9296 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
9297 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9298#endif
9299
9300 if (dev->flags & IFF_PROMISC) {
9301 /* Promiscuous mode. */
9302 rx_mode |= RX_MODE_PROMISC;
9303 } else if (dev->flags & IFF_ALLMULTI) {
9304 /* Accept all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +00009305 tg3_set_multi(tp, 1);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00009306 } else if (netdev_mc_empty(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009307 /* Reject all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +00009308 tg3_set_multi(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009309 } else {
9310 /* Accept one or more multicast(s). */
Jiri Pirko22bedad32010-04-01 21:22:57 +00009311 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009312 u32 mc_filter[4] = { 0, };
9313 u32 regidx;
9314 u32 bit;
9315 u32 crc;
9316
Jiri Pirko22bedad32010-04-01 21:22:57 +00009317 netdev_for_each_mc_addr(ha, dev) {
9318 crc = calc_crc(ha->addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009319 bit = ~crc & 0x7f;
9320 regidx = (bit & 0x60) >> 5;
9321 bit &= 0x1f;
9322 mc_filter[regidx] |= (1 << bit);
9323 }
9324
9325 tw32(MAC_HASH_REG_0, mc_filter[0]);
9326 tw32(MAC_HASH_REG_1, mc_filter[1]);
9327 tw32(MAC_HASH_REG_2, mc_filter[2]);
9328 tw32(MAC_HASH_REG_3, mc_filter[3]);
9329 }
9330
9331 if (rx_mode != tp->rx_mode) {
9332 tp->rx_mode = rx_mode;
9333 tw32_f(MAC_RX_MODE, rx_mode);
9334 udelay(10);
9335 }
9336}
9337
9338static void tg3_set_rx_mode(struct net_device *dev)
9339{
9340 struct tg3 *tp = netdev_priv(dev);
9341
Michael Chane75f7c92006-03-20 21:33:26 -08009342 if (!netif_running(dev))
9343 return;
9344
David S. Millerf47c11e2005-06-24 20:18:35 -07009345 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009346 __tg3_set_rx_mode(dev);
David S. Millerf47c11e2005-06-24 20:18:35 -07009347 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009348}
9349
9350#define TG3_REGDUMP_LEN (32 * 1024)
9351
9352static int tg3_get_regs_len(struct net_device *dev)
9353{
9354 return TG3_REGDUMP_LEN;
9355}
9356
9357static void tg3_get_regs(struct net_device *dev,
9358 struct ethtool_regs *regs, void *_p)
9359{
9360 u32 *p = _p;
9361 struct tg3 *tp = netdev_priv(dev);
9362 u8 *orig_p = _p;
9363 int i;
9364
9365 regs->version = 0;
9366
9367 memset(p, 0, TG3_REGDUMP_LEN);
9368
Matt Carlson800960682010-08-02 11:26:06 +00009369 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -08009370 return;
9371
David S. Millerf47c11e2005-06-24 20:18:35 -07009372 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009373
9374#define __GET_REG32(reg) (*(p)++ = tr32(reg))
Matt Carlsonbe98da62010-07-11 09:31:46 +00009375#define GET_REG32_LOOP(base, len) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07009376do { p = (u32 *)(orig_p + (base)); \
9377 for (i = 0; i < len; i += 4) \
9378 __GET_REG32((base) + i); \
9379} while (0)
9380#define GET_REG32_1(reg) \
9381do { p = (u32 *)(orig_p + (reg)); \
9382 __GET_REG32((reg)); \
9383} while (0)
9384
9385 GET_REG32_LOOP(TG3PCI_VENDOR, 0xb0);
9386 GET_REG32_LOOP(MAILBOX_INTERRUPT_0, 0x200);
9387 GET_REG32_LOOP(MAC_MODE, 0x4f0);
9388 GET_REG32_LOOP(SNDDATAI_MODE, 0xe0);
9389 GET_REG32_1(SNDDATAC_MODE);
9390 GET_REG32_LOOP(SNDBDS_MODE, 0x80);
9391 GET_REG32_LOOP(SNDBDI_MODE, 0x48);
9392 GET_REG32_1(SNDBDC_MODE);
9393 GET_REG32_LOOP(RCVLPC_MODE, 0x20);
9394 GET_REG32_LOOP(RCVLPC_SELLST_BASE, 0x15c);
9395 GET_REG32_LOOP(RCVDBDI_MODE, 0x0c);
9396 GET_REG32_LOOP(RCVDBDI_JUMBO_BD, 0x3c);
9397 GET_REG32_LOOP(RCVDBDI_BD_PROD_IDX_0, 0x44);
9398 GET_REG32_1(RCVDCC_MODE);
9399 GET_REG32_LOOP(RCVBDI_MODE, 0x20);
9400 GET_REG32_LOOP(RCVCC_MODE, 0x14);
9401 GET_REG32_LOOP(RCVLSC_MODE, 0x08);
9402 GET_REG32_1(MBFREE_MODE);
9403 GET_REG32_LOOP(HOSTCC_MODE, 0x100);
9404 GET_REG32_LOOP(MEMARB_MODE, 0x10);
9405 GET_REG32_LOOP(BUFMGR_MODE, 0x58);
9406 GET_REG32_LOOP(RDMAC_MODE, 0x08);
9407 GET_REG32_LOOP(WDMAC_MODE, 0x08);
Chris Elmquist091465d2005-12-20 13:25:19 -08009408 GET_REG32_1(RX_CPU_MODE);
9409 GET_REG32_1(RX_CPU_STATE);
9410 GET_REG32_1(RX_CPU_PGMCTR);
9411 GET_REG32_1(RX_CPU_HWBKPT);
9412 GET_REG32_1(TX_CPU_MODE);
9413 GET_REG32_1(TX_CPU_STATE);
9414 GET_REG32_1(TX_CPU_PGMCTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009415 GET_REG32_LOOP(GRCMBOX_INTERRUPT_0, 0x110);
9416 GET_REG32_LOOP(FTQ_RESET, 0x120);
9417 GET_REG32_LOOP(MSGINT_MODE, 0x0c);
9418 GET_REG32_1(DMAC_MODE);
9419 GET_REG32_LOOP(GRC_MODE, 0x4c);
9420 if (tp->tg3_flags & TG3_FLAG_NVRAM)
9421 GET_REG32_LOOP(NVRAM_CMD, 0x24);
9422
9423#undef __GET_REG32
9424#undef GET_REG32_LOOP
9425#undef GET_REG32_1
9426
David S. Millerf47c11e2005-06-24 20:18:35 -07009427 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009428}
9429
9430static int tg3_get_eeprom_len(struct net_device *dev)
9431{
9432 struct tg3 *tp = netdev_priv(dev);
9433
9434 return tp->nvram_size;
9435}
9436
Linus Torvalds1da177e2005-04-16 15:20:36 -07009437static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9438{
9439 struct tg3 *tp = netdev_priv(dev);
9440 int ret;
9441 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -08009442 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009443 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009444
Matt Carlsondf259d82009-04-20 06:57:14 +00009445 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM)
9446 return -EINVAL;
9447
Matt Carlson800960682010-08-02 11:26:06 +00009448 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -08009449 return -EAGAIN;
9450
Linus Torvalds1da177e2005-04-16 15:20:36 -07009451 offset = eeprom->offset;
9452 len = eeprom->len;
9453 eeprom->len = 0;
9454
9455 eeprom->magic = TG3_EEPROM_MAGIC;
9456
9457 if (offset & 3) {
9458 /* adjustments to start on required 4 byte boundary */
9459 b_offset = offset & 3;
9460 b_count = 4 - b_offset;
9461 if (b_count > len) {
9462 /* i.e. offset=1 len=2 */
9463 b_count = len;
9464 }
Matt Carlsona9dc5292009-02-25 14:25:30 +00009465 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009466 if (ret)
9467 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +00009468 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009469 len -= b_count;
9470 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00009471 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009472 }
9473
9474 /* read bytes upto the last 4 byte boundary */
9475 pd = &data[eeprom->len];
9476 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +00009477 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009478 if (ret) {
9479 eeprom->len += i;
9480 return ret;
9481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009482 memcpy(pd + i, &val, 4);
9483 }
9484 eeprom->len += i;
9485
9486 if (len & 3) {
9487 /* read last bytes not ending on 4 byte boundary */
9488 pd = &data[eeprom->len];
9489 b_count = len & 3;
9490 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009491 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009492 if (ret)
9493 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009494 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009495 eeprom->len += b_count;
9496 }
9497 return 0;
9498}
9499
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009500static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009501
9502static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9503{
9504 struct tg3 *tp = netdev_priv(dev);
9505 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009506 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009507 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009508 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009509
Matt Carlson800960682010-08-02 11:26:06 +00009510 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -08009511 return -EAGAIN;
9512
Matt Carlsondf259d82009-04-20 06:57:14 +00009513 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
9514 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009515 return -EINVAL;
9516
9517 offset = eeprom->offset;
9518 len = eeprom->len;
9519
9520 if ((b_offset = (offset & 3))) {
9521 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +00009522 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009523 if (ret)
9524 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009525 len += b_offset;
9526 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -07009527 if (len < 4)
9528 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009529 }
9530
9531 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -07009532 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009533 /* adjustments to end on required 4 byte boundary */
9534 odd_len = 1;
9535 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009536 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009537 if (ret)
9538 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009539 }
9540
9541 buf = data;
9542 if (b_offset || odd_len) {
9543 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009544 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009545 return -ENOMEM;
9546 if (b_offset)
9547 memcpy(buf, &start, 4);
9548 if (odd_len)
9549 memcpy(buf+len-4, &end, 4);
9550 memcpy(buf + b_offset, data, eeprom->len);
9551 }
9552
9553 ret = tg3_nvram_write_block(tp, offset, len, buf);
9554
9555 if (buf != data)
9556 kfree(buf);
9557
9558 return ret;
9559}
9560
9561static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9562{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009563 struct tg3 *tp = netdev_priv(dev);
9564
9565 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009566 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009567 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009568 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009569 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
9570 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009571 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009572
Linus Torvalds1da177e2005-04-16 15:20:36 -07009573 cmd->supported = (SUPPORTED_Autoneg);
9574
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009575 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009576 cmd->supported |= (SUPPORTED_1000baseT_Half |
9577 SUPPORTED_1000baseT_Full);
9578
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009579 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009580 cmd->supported |= (SUPPORTED_100baseT_Half |
9581 SUPPORTED_100baseT_Full |
9582 SUPPORTED_10baseT_Half |
9583 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -08009584 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -07009585 cmd->port = PORT_TP;
9586 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009587 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -07009588 cmd->port = PORT_FIBRE;
9589 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009590
Linus Torvalds1da177e2005-04-16 15:20:36 -07009591 cmd->advertising = tp->link_config.advertising;
9592 if (netif_running(dev)) {
9593 cmd->speed = tp->link_config.active_speed;
9594 cmd->duplex = tp->link_config.active_duplex;
9595 }
Matt Carlson882e9792009-09-01 13:21:36 +00009596 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +00009597 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009598 cmd->autoneg = tp->link_config.autoneg;
9599 cmd->maxtxpkt = 0;
9600 cmd->maxrxpkt = 0;
9601 return 0;
9602}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009603
Linus Torvalds1da177e2005-04-16 15:20:36 -07009604static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9605{
9606 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009607
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009608 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009609 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009610 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009611 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009612 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
9613 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009614 }
9615
Matt Carlson7e5856b2009-02-25 14:23:01 +00009616 if (cmd->autoneg != AUTONEG_ENABLE &&
9617 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -07009618 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +00009619
9620 if (cmd->autoneg == AUTONEG_DISABLE &&
9621 cmd->duplex != DUPLEX_FULL &&
9622 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -07009623 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009624
Matt Carlson7e5856b2009-02-25 14:23:01 +00009625 if (cmd->autoneg == AUTONEG_ENABLE) {
9626 u32 mask = ADVERTISED_Autoneg |
9627 ADVERTISED_Pause |
9628 ADVERTISED_Asym_Pause;
9629
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009630 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +00009631 mask |= ADVERTISED_1000baseT_Half |
9632 ADVERTISED_1000baseT_Full;
9633
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009634 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +00009635 mask |= ADVERTISED_100baseT_Half |
9636 ADVERTISED_100baseT_Full |
9637 ADVERTISED_10baseT_Half |
9638 ADVERTISED_10baseT_Full |
9639 ADVERTISED_TP;
9640 else
9641 mask |= ADVERTISED_FIBRE;
9642
9643 if (cmd->advertising & ~mask)
9644 return -EINVAL;
9645
9646 mask &= (ADVERTISED_1000baseT_Half |
9647 ADVERTISED_1000baseT_Full |
9648 ADVERTISED_100baseT_Half |
9649 ADVERTISED_100baseT_Full |
9650 ADVERTISED_10baseT_Half |
9651 ADVERTISED_10baseT_Full);
9652
9653 cmd->advertising &= mask;
9654 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009655 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
Matt Carlson7e5856b2009-02-25 14:23:01 +00009656 if (cmd->speed != SPEED_1000)
9657 return -EINVAL;
9658
9659 if (cmd->duplex != DUPLEX_FULL)
9660 return -EINVAL;
9661 } else {
9662 if (cmd->speed != SPEED_100 &&
9663 cmd->speed != SPEED_10)
9664 return -EINVAL;
9665 }
9666 }
9667
David S. Millerf47c11e2005-06-24 20:18:35 -07009668 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009669
9670 tp->link_config.autoneg = cmd->autoneg;
9671 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -07009672 tp->link_config.advertising = (cmd->advertising |
9673 ADVERTISED_Autoneg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009674 tp->link_config.speed = SPEED_INVALID;
9675 tp->link_config.duplex = DUPLEX_INVALID;
9676 } else {
9677 tp->link_config.advertising = 0;
9678 tp->link_config.speed = cmd->speed;
9679 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009680 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009681
Michael Chan24fcad62006-12-17 17:06:46 -08009682 tp->link_config.orig_speed = tp->link_config.speed;
9683 tp->link_config.orig_duplex = tp->link_config.duplex;
9684 tp->link_config.orig_autoneg = tp->link_config.autoneg;
9685
Linus Torvalds1da177e2005-04-16 15:20:36 -07009686 if (netif_running(dev))
9687 tg3_setup_phy(tp, 1);
9688
David S. Millerf47c11e2005-06-24 20:18:35 -07009689 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009690
Linus Torvalds1da177e2005-04-16 15:20:36 -07009691 return 0;
9692}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009693
Linus Torvalds1da177e2005-04-16 15:20:36 -07009694static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
9695{
9696 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009697
Linus Torvalds1da177e2005-04-16 15:20:36 -07009698 strcpy(info->driver, DRV_MODULE_NAME);
9699 strcpy(info->version, DRV_MODULE_VERSION);
Michael Chanc4e65752006-03-20 22:29:32 -08009700 strcpy(info->fw_version, tp->fw_ver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009701 strcpy(info->bus_info, pci_name(tp->pdev));
9702}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009703
Linus Torvalds1da177e2005-04-16 15:20:36 -07009704static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
9705{
9706 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009707
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009708 if ((tp->tg3_flags & TG3_FLAG_WOL_CAP) &&
9709 device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -07009710 wol->supported = WAKE_MAGIC;
9711 else
9712 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009713 wol->wolopts = 0;
Matt Carlson05ac4cb2008-11-03 16:53:46 -08009714 if ((tp->tg3_flags & TG3_FLAG_WOL_ENABLE) &&
9715 device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009716 wol->wolopts = WAKE_MAGIC;
9717 memset(&wol->sopass, 0, sizeof(wol->sopass));
9718}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009719
Linus Torvalds1da177e2005-04-16 15:20:36 -07009720static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
9721{
9722 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009723 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009724
Linus Torvalds1da177e2005-04-16 15:20:36 -07009725 if (wol->wolopts & ~WAKE_MAGIC)
9726 return -EINVAL;
9727 if ((wol->wolopts & WAKE_MAGIC) &&
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009728 !((tp->tg3_flags & TG3_FLAG_WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009729 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009730
David S. Millerf47c11e2005-06-24 20:18:35 -07009731 spin_lock_bh(&tp->lock);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009732 if (wol->wolopts & WAKE_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009733 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009734 device_set_wakeup_enable(dp, true);
9735 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009736 tp->tg3_flags &= ~TG3_FLAG_WOL_ENABLE;
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009737 device_set_wakeup_enable(dp, false);
9738 }
David S. Millerf47c11e2005-06-24 20:18:35 -07009739 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009740
Linus Torvalds1da177e2005-04-16 15:20:36 -07009741 return 0;
9742}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009743
Linus Torvalds1da177e2005-04-16 15:20:36 -07009744static u32 tg3_get_msglevel(struct net_device *dev)
9745{
9746 struct tg3 *tp = netdev_priv(dev);
9747 return tp->msg_enable;
9748}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009749
Linus Torvalds1da177e2005-04-16 15:20:36 -07009750static void tg3_set_msglevel(struct net_device *dev, u32 value)
9751{
9752 struct tg3 *tp = netdev_priv(dev);
9753 tp->msg_enable = value;
9754}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009755
Linus Torvalds1da177e2005-04-16 15:20:36 -07009756static int tg3_set_tso(struct net_device *dev, u32 value)
9757{
9758 struct tg3 *tp = netdev_priv(dev);
9759
9760 if (!(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
9761 if (value)
9762 return -EINVAL;
9763 return 0;
9764 }
Matt Carlson027455a2008-12-21 20:19:30 -08009765 if ((dev->features & NETIF_F_IPV6_CSUM) &&
Matt Carlsone849cdc2009-11-13 13:03:38 +00009766 ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_2) ||
9767 (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3))) {
Matt Carlson9936bcf2007-10-10 18:03:07 -07009768 if (value) {
Michael Chanb0026622006-07-03 19:42:14 -07009769 dev->features |= NETIF_F_TSO6;
Matt Carlsone849cdc2009-11-13 13:03:38 +00009770 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
9771 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -07009772 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
9773 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Matt Carlson321d32a2008-11-21 17:22:19 -08009774 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsone849cdc2009-11-13 13:03:38 +00009775 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson9936bcf2007-10-10 18:03:07 -07009776 dev->features |= NETIF_F_TSO_ECN;
9777 } else
9778 dev->features &= ~(NETIF_F_TSO6 | NETIF_F_TSO_ECN);
Michael Chanb0026622006-07-03 19:42:14 -07009779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009780 return ethtool_op_set_tso(dev, value);
9781}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009782
Linus Torvalds1da177e2005-04-16 15:20:36 -07009783static int tg3_nway_reset(struct net_device *dev)
9784{
9785 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009786 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009787
Linus Torvalds1da177e2005-04-16 15:20:36 -07009788 if (!netif_running(dev))
9789 return -EAGAIN;
9790
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009791 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -07009792 return -EINVAL;
9793
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009794 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009795 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009796 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009797 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009798 } else {
9799 u32 bmcr;
9800
9801 spin_lock_bh(&tp->lock);
9802 r = -EINVAL;
9803 tg3_readphy(tp, MII_BMCR, &bmcr);
9804 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
9805 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009806 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009807 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
9808 BMCR_ANENABLE);
9809 r = 0;
9810 }
9811 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009812 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009813
Linus Torvalds1da177e2005-04-16 15:20:36 -07009814 return r;
9815}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009816
Linus Torvalds1da177e2005-04-16 15:20:36 -07009817static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
9818{
9819 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009820
Linus Torvalds1da177e2005-04-16 15:20:36 -07009821 ering->rx_max_pending = TG3_RX_RING_SIZE - 1;
9822 ering->rx_mini_max_pending = 0;
Michael Chan4f81c322006-03-20 21:33:42 -08009823 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE)
9824 ering->rx_jumbo_max_pending = TG3_RX_JUMBO_RING_SIZE - 1;
9825 else
9826 ering->rx_jumbo_max_pending = 0;
9827
9828 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009829
9830 ering->rx_pending = tp->rx_pending;
9831 ering->rx_mini_pending = 0;
Michael Chan4f81c322006-03-20 21:33:42 -08009832 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE)
9833 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
9834 else
9835 ering->rx_jumbo_pending = 0;
9836
Matt Carlsonf3f3f272009-08-28 14:03:21 +00009837 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009838}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009839
Linus Torvalds1da177e2005-04-16 15:20:36 -07009840static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
9841{
9842 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +00009843 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009844
Linus Torvalds1da177e2005-04-16 15:20:36 -07009845 if ((ering->rx_pending > TG3_RX_RING_SIZE - 1) ||
9846 (ering->rx_jumbo_pending > TG3_RX_JUMBO_RING_SIZE - 1) ||
Michael Chanbc3a9252006-10-18 20:55:18 -07009847 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
9848 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Michael Chan7f62ad52007-02-20 23:25:40 -08009849 ((tp->tg3_flags2 & TG3_FLG2_TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -07009850 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009851 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009852
Michael Chanbbe832c2005-06-24 20:20:04 -07009853 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009854 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009855 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -07009856 irq_sync = 1;
9857 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009858
Michael Chanbbe832c2005-06-24 20:20:04 -07009859 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009860
Linus Torvalds1da177e2005-04-16 15:20:36 -07009861 tp->rx_pending = ering->rx_pending;
9862
9863 if ((tp->tg3_flags2 & TG3_FLG2_MAX_RXPEND_64) &&
9864 tp->rx_pending > 63)
9865 tp->rx_pending = 63;
9866 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +00009867
Matt Carlson6fd45cb2010-09-15 08:59:57 +00009868 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +00009869 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009870
9871 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -07009872 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -07009873 err = tg3_restart_hw(tp, 1);
9874 if (!err)
9875 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009876 }
9877
David S. Millerf47c11e2005-06-24 20:18:35 -07009878 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009879
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009880 if (irq_sync && !err)
9881 tg3_phy_start(tp);
9882
Michael Chanb9ec6c12006-07-25 16:37:27 -07009883 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009884}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009885
Linus Torvalds1da177e2005-04-16 15:20:36 -07009886static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
9887{
9888 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009889
Linus Torvalds1da177e2005-04-16 15:20:36 -07009890 epause->autoneg = (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG) != 0;
Matt Carlson8d018622007-12-20 20:05:44 -08009891
Steve Glendinninge18ce342008-12-16 02:00:00 -08009892 if (tp->link_config.active_flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -08009893 epause->rx_pause = 1;
9894 else
9895 epause->rx_pause = 0;
9896
Steve Glendinninge18ce342008-12-16 02:00:00 -08009897 if (tp->link_config.active_flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -08009898 epause->tx_pause = 1;
9899 else
9900 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009901}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009902
Linus Torvalds1da177e2005-04-16 15:20:36 -07009903static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
9904{
9905 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009906 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009907
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009908 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson27121682010-02-17 15:16:57 +00009909 u32 newadv;
9910 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009911
Matt Carlson27121682010-02-17 15:16:57 +00009912 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009913
Matt Carlson27121682010-02-17 15:16:57 +00009914 if (!(phydev->supported & SUPPORTED_Pause) ||
9915 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
9916 ((epause->rx_pause && !epause->tx_pause) ||
9917 (!epause->rx_pause && epause->tx_pause))))
9918 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009919
Matt Carlson27121682010-02-17 15:16:57 +00009920 tp->link_config.flowctrl = 0;
9921 if (epause->rx_pause) {
9922 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009923
Matt Carlson27121682010-02-17 15:16:57 +00009924 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -08009925 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +00009926 newadv = ADVERTISED_Pause;
9927 } else
9928 newadv = ADVERTISED_Pause |
9929 ADVERTISED_Asym_Pause;
9930 } else if (epause->tx_pause) {
9931 tp->link_config.flowctrl |= FLOW_CTRL_TX;
9932 newadv = ADVERTISED_Asym_Pause;
9933 } else
9934 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009935
Matt Carlson27121682010-02-17 15:16:57 +00009936 if (epause->autoneg)
9937 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
9938 else
9939 tp->tg3_flags &= ~TG3_FLAG_PAUSE_AUTONEG;
9940
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009941 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +00009942 u32 oldadv = phydev->advertising &
9943 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
9944 if (oldadv != newadv) {
9945 phydev->advertising &=
9946 ~(ADVERTISED_Pause |
9947 ADVERTISED_Asym_Pause);
9948 phydev->advertising |= newadv;
9949 if (phydev->autoneg) {
9950 /*
9951 * Always renegotiate the link to
9952 * inform our link partner of our
9953 * flow control settings, even if the
9954 * flow control is forced. Let
9955 * tg3_adjust_link() do the final
9956 * flow control setup.
9957 */
9958 return phy_start_aneg(phydev);
9959 }
9960 }
9961
9962 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009963 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +00009964 } else {
9965 tp->link_config.orig_advertising &=
9966 ~(ADVERTISED_Pause |
9967 ADVERTISED_Asym_Pause);
9968 tp->link_config.orig_advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009969 }
9970 } else {
9971 int irq_sync = 0;
9972
9973 if (netif_running(dev)) {
9974 tg3_netif_stop(tp);
9975 irq_sync = 1;
9976 }
9977
9978 tg3_full_lock(tp, irq_sync);
9979
9980 if (epause->autoneg)
9981 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
9982 else
9983 tp->tg3_flags &= ~TG3_FLAG_PAUSE_AUTONEG;
9984 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -08009985 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009986 else
Steve Glendinninge18ce342008-12-16 02:00:00 -08009987 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009988 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -08009989 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009990 else
Steve Glendinninge18ce342008-12-16 02:00:00 -08009991 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009992
9993 if (netif_running(dev)) {
9994 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
9995 err = tg3_restart_hw(tp, 1);
9996 if (!err)
9997 tg3_netif_start(tp);
9998 }
9999
10000 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010002
Michael Chanb9ec6c12006-07-25 16:37:27 -070010003 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010004}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010005
Linus Torvalds1da177e2005-04-16 15:20:36 -070010006static u32 tg3_get_rx_csum(struct net_device *dev)
10007{
10008 struct tg3 *tp = netdev_priv(dev);
10009 return (tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) != 0;
10010}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010011
Linus Torvalds1da177e2005-04-16 15:20:36 -070010012static int tg3_set_rx_csum(struct net_device *dev, u32 data)
10013{
10014 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010015
Linus Torvalds1da177e2005-04-16 15:20:36 -070010016 if (tp->tg3_flags & TG3_FLAG_BROKEN_CHECKSUMS) {
10017 if (data != 0)
10018 return -EINVAL;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010019 return 0;
10020 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010021
David S. Millerf47c11e2005-06-24 20:18:35 -070010022 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010023 if (data)
10024 tp->tg3_flags |= TG3_FLAG_RX_CHECKSUMS;
10025 else
10026 tp->tg3_flags &= ~TG3_FLAG_RX_CHECKSUMS;
David S. Millerf47c11e2005-06-24 20:18:35 -070010027 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010028
Linus Torvalds1da177e2005-04-16 15:20:36 -070010029 return 0;
10030}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010031
Linus Torvalds1da177e2005-04-16 15:20:36 -070010032static int tg3_set_tx_csum(struct net_device *dev, u32 data)
10033{
10034 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010035
Linus Torvalds1da177e2005-04-16 15:20:36 -070010036 if (tp->tg3_flags & TG3_FLAG_BROKEN_CHECKSUMS) {
10037 if (data != 0)
10038 return -EINVAL;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010039 return 0;
10040 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010041
Matt Carlson321d32a2008-11-21 17:22:19 -080010042 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Michael Chan6460d942007-07-14 19:07:52 -070010043 ethtool_op_set_tx_ipv6_csum(dev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010044 else
Michael Chan9c27dbd2006-03-20 22:28:27 -080010045 ethtool_op_set_tx_csum(dev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010046
10047 return 0;
10048}
10049
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010050static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010051{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010052 switch (sset) {
10053 case ETH_SS_TEST:
10054 return TG3_NUM_TEST;
10055 case ETH_SS_STATS:
10056 return TG3_NUM_STATS;
10057 default:
10058 return -EOPNOTSUPP;
10059 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070010060}
10061
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010062static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010063{
10064 switch (stringset) {
10065 case ETH_SS_STATS:
10066 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
10067 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070010068 case ETH_SS_TEST:
10069 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
10070 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010071 default:
10072 WARN_ON(1); /* we need a WARN() */
10073 break;
10074 }
10075}
10076
Michael Chan4009a932005-09-05 17:52:54 -070010077static int tg3_phys_id(struct net_device *dev, u32 data)
10078{
10079 struct tg3 *tp = netdev_priv(dev);
10080 int i;
10081
10082 if (!netif_running(tp->dev))
10083 return -EAGAIN;
10084
10085 if (data == 0)
Stephen Hemminger759afc32008-02-23 19:51:59 -080010086 data = UINT_MAX / 2;
Michael Chan4009a932005-09-05 17:52:54 -070010087
10088 for (i = 0; i < (data * 2); i++) {
10089 if ((i % 2) == 0)
10090 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10091 LED_CTRL_1000MBPS_ON |
10092 LED_CTRL_100MBPS_ON |
10093 LED_CTRL_10MBPS_ON |
10094 LED_CTRL_TRAFFIC_OVERRIDE |
10095 LED_CTRL_TRAFFIC_BLINK |
10096 LED_CTRL_TRAFFIC_LED);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010097
Michael Chan4009a932005-09-05 17:52:54 -070010098 else
10099 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10100 LED_CTRL_TRAFFIC_OVERRIDE);
10101
10102 if (msleep_interruptible(500))
10103 break;
10104 }
10105 tw32(MAC_LED_CTRL, tp->led_ctrl);
10106 return 0;
10107}
10108
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010109static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010110 struct ethtool_stats *estats, u64 *tmp_stats)
10111{
10112 struct tg3 *tp = netdev_priv(dev);
10113 memcpy(tmp_stats, tg3_get_estats(tp), sizeof(tp->estats));
10114}
10115
Michael Chan566f86a2005-05-29 14:56:58 -070010116#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080010117#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
10118#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
10119#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Michael Chanb16250e2006-09-27 16:10:14 -070010120#define NVRAM_SELFBOOT_HW_SIZE 0x20
10121#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070010122
10123static int tg3_test_nvram(struct tg3 *tp)
10124{
Al Virob9fc7dc2007-12-17 22:59:57 -080010125 u32 csum, magic;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010126 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010127 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070010128
Matt Carlsondf259d82009-04-20 06:57:14 +000010129 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM)
10130 return 0;
10131
Matt Carlsone4f34112009-02-25 14:25:00 +000010132 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080010133 return -EIO;
10134
Michael Chan1b277772006-03-20 22:27:48 -080010135 if (magic == TG3_EEPROM_MAGIC)
10136 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070010137 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080010138 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
10139 TG3_EEPROM_SB_FORMAT_1) {
10140 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
10141 case TG3_EEPROM_SB_REVISION_0:
10142 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
10143 break;
10144 case TG3_EEPROM_SB_REVISION_2:
10145 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
10146 break;
10147 case TG3_EEPROM_SB_REVISION_3:
10148 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
10149 break;
10150 default:
10151 return 0;
10152 }
10153 } else
Michael Chan1b277772006-03-20 22:27:48 -080010154 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070010155 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
10156 size = NVRAM_SELFBOOT_HW_SIZE;
10157 else
Michael Chan1b277772006-03-20 22:27:48 -080010158 return -EIO;
10159
10160 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070010161 if (buf == NULL)
10162 return -ENOMEM;
10163
Michael Chan1b277772006-03-20 22:27:48 -080010164 err = -EIO;
10165 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010166 err = tg3_nvram_read_be32(tp, i, &buf[j]);
10167 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070010168 break;
Michael Chan566f86a2005-05-29 14:56:58 -070010169 }
Michael Chan1b277772006-03-20 22:27:48 -080010170 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070010171 goto out;
10172
Michael Chan1b277772006-03-20 22:27:48 -080010173 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010174 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080010175 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010176 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080010177 u8 *buf8 = (u8 *) buf, csum8 = 0;
10178
Al Virob9fc7dc2007-12-17 22:59:57 -080010179 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080010180 TG3_EEPROM_SB_REVISION_2) {
10181 /* For rev 2, the csum doesn't include the MBA. */
10182 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
10183 csum8 += buf8[i];
10184 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
10185 csum8 += buf8[i];
10186 } else {
10187 for (i = 0; i < size; i++)
10188 csum8 += buf8[i];
10189 }
Michael Chan1b277772006-03-20 22:27:48 -080010190
Adrian Bunkad96b482006-04-05 22:21:04 -070010191 if (csum8 == 0) {
10192 err = 0;
10193 goto out;
10194 }
10195
10196 err = -EIO;
10197 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080010198 }
Michael Chan566f86a2005-05-29 14:56:58 -070010199
Al Virob9fc7dc2007-12-17 22:59:57 -080010200 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010201 TG3_EEPROM_MAGIC_HW) {
10202 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000010203 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070010204 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070010205
10206 /* Separate the parity bits and the data bytes. */
10207 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
10208 if ((i == 0) || (i == 8)) {
10209 int l;
10210 u8 msk;
10211
10212 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
10213 parity[k++] = buf8[i] & msk;
10214 i++;
Matt Carlson859a588792010-04-05 10:19:28 +000010215 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070010216 int l;
10217 u8 msk;
10218
10219 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
10220 parity[k++] = buf8[i] & msk;
10221 i++;
10222
10223 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
10224 parity[k++] = buf8[i] & msk;
10225 i++;
10226 }
10227 data[j++] = buf8[i];
10228 }
10229
10230 err = -EIO;
10231 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
10232 u8 hw8 = hweight8(data[i]);
10233
10234 if ((hw8 & 0x1) && parity[i])
10235 goto out;
10236 else if (!(hw8 & 0x1) && !parity[i])
10237 goto out;
10238 }
10239 err = 0;
10240 goto out;
10241 }
10242
Michael Chan566f86a2005-05-29 14:56:58 -070010243 /* Bootstrap checksum at offset 0x10 */
10244 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlsona9dc5292009-02-25 14:25:30 +000010245 if (csum != be32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070010246 goto out;
10247
10248 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
10249 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlsona9dc5292009-02-25 14:25:30 +000010250 if (csum != be32_to_cpu(buf[0xfc/4]))
10251 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070010252
10253 err = 0;
10254
10255out:
10256 kfree(buf);
10257 return err;
10258}
10259
Michael Chanca430072005-05-29 14:57:23 -070010260#define TG3_SERDES_TIMEOUT_SEC 2
10261#define TG3_COPPER_TIMEOUT_SEC 6
10262
10263static int tg3_test_link(struct tg3 *tp)
10264{
10265 int i, max;
10266
10267 if (!netif_running(tp->dev))
10268 return -ENODEV;
10269
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010270 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070010271 max = TG3_SERDES_TIMEOUT_SEC;
10272 else
10273 max = TG3_COPPER_TIMEOUT_SEC;
10274
10275 for (i = 0; i < max; i++) {
10276 if (netif_carrier_ok(tp->dev))
10277 return 0;
10278
10279 if (msleep_interruptible(1000))
10280 break;
10281 }
10282
10283 return -EIO;
10284}
10285
Michael Chana71116d2005-05-29 14:58:11 -070010286/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080010287static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070010288{
Michael Chanb16250e2006-09-27 16:10:14 -070010289 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070010290 u32 offset, read_mask, write_mask, val, save_val, read_val;
10291 static struct {
10292 u16 offset;
10293 u16 flags;
10294#define TG3_FL_5705 0x1
10295#define TG3_FL_NOT_5705 0x2
10296#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070010297#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070010298 u32 read_mask;
10299 u32 write_mask;
10300 } reg_tbl[] = {
10301 /* MAC Control Registers */
10302 { MAC_MODE, TG3_FL_NOT_5705,
10303 0x00000000, 0x00ef6f8c },
10304 { MAC_MODE, TG3_FL_5705,
10305 0x00000000, 0x01ef6b8c },
10306 { MAC_STATUS, TG3_FL_NOT_5705,
10307 0x03800107, 0x00000000 },
10308 { MAC_STATUS, TG3_FL_5705,
10309 0x03800100, 0x00000000 },
10310 { MAC_ADDR_0_HIGH, 0x0000,
10311 0x00000000, 0x0000ffff },
10312 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010313 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070010314 { MAC_RX_MTU_SIZE, 0x0000,
10315 0x00000000, 0x0000ffff },
10316 { MAC_TX_MODE, 0x0000,
10317 0x00000000, 0x00000070 },
10318 { MAC_TX_LENGTHS, 0x0000,
10319 0x00000000, 0x00003fff },
10320 { MAC_RX_MODE, TG3_FL_NOT_5705,
10321 0x00000000, 0x000007fc },
10322 { MAC_RX_MODE, TG3_FL_5705,
10323 0x00000000, 0x000007dc },
10324 { MAC_HASH_REG_0, 0x0000,
10325 0x00000000, 0xffffffff },
10326 { MAC_HASH_REG_1, 0x0000,
10327 0x00000000, 0xffffffff },
10328 { MAC_HASH_REG_2, 0x0000,
10329 0x00000000, 0xffffffff },
10330 { MAC_HASH_REG_3, 0x0000,
10331 0x00000000, 0xffffffff },
10332
10333 /* Receive Data and Receive BD Initiator Control Registers. */
10334 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
10335 0x00000000, 0xffffffff },
10336 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
10337 0x00000000, 0xffffffff },
10338 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
10339 0x00000000, 0x00000003 },
10340 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
10341 0x00000000, 0xffffffff },
10342 { RCVDBDI_STD_BD+0, 0x0000,
10343 0x00000000, 0xffffffff },
10344 { RCVDBDI_STD_BD+4, 0x0000,
10345 0x00000000, 0xffffffff },
10346 { RCVDBDI_STD_BD+8, 0x0000,
10347 0x00000000, 0xffff0002 },
10348 { RCVDBDI_STD_BD+0xc, 0x0000,
10349 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010350
Michael Chana71116d2005-05-29 14:58:11 -070010351 /* Receive BD Initiator Control Registers. */
10352 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
10353 0x00000000, 0xffffffff },
10354 { RCVBDI_STD_THRESH, TG3_FL_5705,
10355 0x00000000, 0x000003ff },
10356 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
10357 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010358
Michael Chana71116d2005-05-29 14:58:11 -070010359 /* Host Coalescing Control Registers. */
10360 { HOSTCC_MODE, TG3_FL_NOT_5705,
10361 0x00000000, 0x00000004 },
10362 { HOSTCC_MODE, TG3_FL_5705,
10363 0x00000000, 0x000000f6 },
10364 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
10365 0x00000000, 0xffffffff },
10366 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
10367 0x00000000, 0x000003ff },
10368 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
10369 0x00000000, 0xffffffff },
10370 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
10371 0x00000000, 0x000003ff },
10372 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
10373 0x00000000, 0xffffffff },
10374 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10375 0x00000000, 0x000000ff },
10376 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
10377 0x00000000, 0xffffffff },
10378 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10379 0x00000000, 0x000000ff },
10380 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
10381 0x00000000, 0xffffffff },
10382 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
10383 0x00000000, 0xffffffff },
10384 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10385 0x00000000, 0xffffffff },
10386 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10387 0x00000000, 0x000000ff },
10388 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10389 0x00000000, 0xffffffff },
10390 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10391 0x00000000, 0x000000ff },
10392 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
10393 0x00000000, 0xffffffff },
10394 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
10395 0x00000000, 0xffffffff },
10396 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
10397 0x00000000, 0xffffffff },
10398 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
10399 0x00000000, 0xffffffff },
10400 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
10401 0x00000000, 0xffffffff },
10402 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
10403 0xffffffff, 0x00000000 },
10404 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
10405 0xffffffff, 0x00000000 },
10406
10407 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070010408 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010409 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070010410 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010411 0x00000000, 0x007fffff },
10412 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
10413 0x00000000, 0x0000003f },
10414 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
10415 0x00000000, 0x000001ff },
10416 { BUFMGR_MB_HIGH_WATER, 0x0000,
10417 0x00000000, 0x000001ff },
10418 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
10419 0xffffffff, 0x00000000 },
10420 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
10421 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010422
Michael Chana71116d2005-05-29 14:58:11 -070010423 /* Mailbox Registers */
10424 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
10425 0x00000000, 0x000001ff },
10426 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
10427 0x00000000, 0x000001ff },
10428 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
10429 0x00000000, 0x000007ff },
10430 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
10431 0x00000000, 0x000001ff },
10432
10433 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
10434 };
10435
Michael Chanb16250e2006-09-27 16:10:14 -070010436 is_5705 = is_5750 = 0;
10437 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
Michael Chana71116d2005-05-29 14:58:11 -070010438 is_5705 = 1;
Michael Chanb16250e2006-09-27 16:10:14 -070010439 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
10440 is_5750 = 1;
10441 }
Michael Chana71116d2005-05-29 14:58:11 -070010442
10443 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
10444 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
10445 continue;
10446
10447 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
10448 continue;
10449
10450 if ((tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
10451 (reg_tbl[i].flags & TG3_FL_NOT_5788))
10452 continue;
10453
Michael Chanb16250e2006-09-27 16:10:14 -070010454 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
10455 continue;
10456
Michael Chana71116d2005-05-29 14:58:11 -070010457 offset = (u32) reg_tbl[i].offset;
10458 read_mask = reg_tbl[i].read_mask;
10459 write_mask = reg_tbl[i].write_mask;
10460
10461 /* Save the original register content */
10462 save_val = tr32(offset);
10463
10464 /* Determine the read-only value. */
10465 read_val = save_val & read_mask;
10466
10467 /* Write zero to the register, then make sure the read-only bits
10468 * are not changed and the read/write bits are all zeros.
10469 */
10470 tw32(offset, 0);
10471
10472 val = tr32(offset);
10473
10474 /* Test the read-only and read/write bits. */
10475 if (((val & read_mask) != read_val) || (val & write_mask))
10476 goto out;
10477
10478 /* Write ones to all the bits defined by RdMask and WrMask, then
10479 * make sure the read-only bits are not changed and the
10480 * read/write bits are all ones.
10481 */
10482 tw32(offset, read_mask | write_mask);
10483
10484 val = tr32(offset);
10485
10486 /* Test the read-only bits. */
10487 if ((val & read_mask) != read_val)
10488 goto out;
10489
10490 /* Test the read/write bits. */
10491 if ((val & write_mask) != write_mask)
10492 goto out;
10493
10494 tw32(offset, save_val);
10495 }
10496
10497 return 0;
10498
10499out:
Michael Chan9f88f292006-12-07 00:22:54 -080010500 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000010501 netdev_err(tp->dev,
10502 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070010503 tw32(offset, save_val);
10504 return -EIO;
10505}
10506
Michael Chan7942e1d2005-05-29 14:58:36 -070010507static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
10508{
Arjan van de Venf71e1302006-03-03 21:33:57 -050010509 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070010510 int i;
10511 u32 j;
10512
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020010513 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070010514 for (j = 0; j < len; j += 4) {
10515 u32 val;
10516
10517 tg3_write_mem(tp, offset + j, test_pattern[i]);
10518 tg3_read_mem(tp, offset + j, &val);
10519 if (val != test_pattern[i])
10520 return -EIO;
10521 }
10522 }
10523 return 0;
10524}
10525
10526static int tg3_test_memory(struct tg3 *tp)
10527{
10528 static struct mem_entry {
10529 u32 offset;
10530 u32 len;
10531 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080010532 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070010533 { 0x00002000, 0x1c000},
10534 { 0xffffffff, 0x00000}
10535 }, mem_tbl_5705[] = {
10536 { 0x00000100, 0x0000c},
10537 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070010538 { 0x00004000, 0x00800},
10539 { 0x00006000, 0x01000},
10540 { 0x00008000, 0x02000},
10541 { 0x00010000, 0x0e000},
10542 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080010543 }, mem_tbl_5755[] = {
10544 { 0x00000200, 0x00008},
10545 { 0x00004000, 0x00800},
10546 { 0x00006000, 0x00800},
10547 { 0x00008000, 0x02000},
10548 { 0x00010000, 0x0c000},
10549 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070010550 }, mem_tbl_5906[] = {
10551 { 0x00000200, 0x00008},
10552 { 0x00004000, 0x00400},
10553 { 0x00006000, 0x00400},
10554 { 0x00008000, 0x01000},
10555 { 0x00010000, 0x01000},
10556 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000010557 }, mem_tbl_5717[] = {
10558 { 0x00000200, 0x00008},
10559 { 0x00010000, 0x0a000},
10560 { 0x00020000, 0x13c00},
10561 { 0xffffffff, 0x00000}
10562 }, mem_tbl_57765[] = {
10563 { 0x00000200, 0x00008},
10564 { 0x00004000, 0x00800},
10565 { 0x00006000, 0x09800},
10566 { 0x00010000, 0x0a000},
10567 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070010568 };
10569 struct mem_entry *mem_tbl;
10570 int err = 0;
10571 int i;
10572
Matt Carlsona50d0792010-06-05 17:24:37 +000010573 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
10574 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlson8b5a6c42010-01-20 16:58:06 +000010575 mem_tbl = mem_tbl_5717;
10576 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
10577 mem_tbl = mem_tbl_57765;
10578 else if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Matt Carlson321d32a2008-11-21 17:22:19 -080010579 mem_tbl = mem_tbl_5755;
10580 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
10581 mem_tbl = mem_tbl_5906;
10582 else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
10583 mem_tbl = mem_tbl_5705;
10584 else
Michael Chan7942e1d2005-05-29 14:58:36 -070010585 mem_tbl = mem_tbl_570x;
10586
10587 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000010588 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
10589 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070010590 break;
10591 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010592
Michael Chan7942e1d2005-05-29 14:58:36 -070010593 return err;
10594}
10595
Michael Chan9f40dea2005-09-05 17:53:06 -070010596#define TG3_MAC_LOOPBACK 0
10597#define TG3_PHY_LOOPBACK 1
10598
10599static int tg3_run_loopback(struct tg3 *tp, int loopback_mode)
Michael Chanc76949a2005-05-29 14:58:59 -070010600{
Michael Chan9f40dea2005-09-05 17:53:06 -070010601 u32 mac_mode, rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010602 u32 desc_idx, coal_now;
Michael Chanc76949a2005-05-29 14:58:59 -070010603 struct sk_buff *skb, *rx_skb;
10604 u8 *tx_data;
10605 dma_addr_t map;
10606 int num_pkts, tx_len, rx_len, i, err;
10607 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000010608 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000010609 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070010610
Matt Carlsonc8873402010-02-12 14:47:11 +000010611 tnapi = &tp->napi[0];
10612 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000010613 if (tp->irq_cnt > 1) {
Matt Carlson0c1d0e22009-09-01 13:16:33 +000010614 rnapi = &tp->napi[1];
Matt Carlsonc8873402010-02-12 14:47:11 +000010615 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
10616 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000010617 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010618 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000010619
Michael Chan9f40dea2005-09-05 17:53:06 -070010620 if (loopback_mode == TG3_MAC_LOOPBACK) {
Michael Chanc94e3942005-09-27 12:12:42 -070010621 /* HW errata - mac loopback fails in some cases on 5780.
10622 * Normal traffic and PHY loopback are not affected by
10623 * errata.
10624 */
10625 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780)
10626 return 0;
10627
Michael Chan9f40dea2005-09-05 17:53:06 -070010628 mac_mode = (tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK) |
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010629 MAC_MODE_PORT_INT_LPBACK;
10630 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
10631 mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010632 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
Michael Chan3f7045c2006-09-27 16:02:29 -070010633 mac_mode |= MAC_MODE_PORT_MODE_MII;
10634 else
10635 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chan9f40dea2005-09-05 17:53:06 -070010636 tw32(MAC_MODE, mac_mode);
10637 } else if (loopback_mode == TG3_PHY_LOOPBACK) {
Michael Chan3f7045c2006-09-27 16:02:29 -070010638 u32 val;
10639
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010640 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +000010641 tg3_phy_fet_toggle_apd(tp, false);
Michael Chan5d64ad32006-12-07 00:19:40 -080010642 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED100;
10643 } else
10644 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED1000;
Michael Chan3f7045c2006-09-27 16:02:29 -070010645
Matt Carlson9ef8ca92007-07-11 19:48:29 -070010646 tg3_phy_toggle_automdix(tp, 0);
10647
Michael Chan3f7045c2006-09-27 16:02:29 -070010648 tg3_writephy(tp, MII_BMCR, val);
Michael Chanc94e3942005-09-27 12:12:42 -070010649 udelay(40);
Michael Chan5d64ad32006-12-07 00:19:40 -080010650
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010651 mac_mode = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010652 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson1061b7c2010-02-12 14:47:12 +000010653 tg3_writephy(tp, MII_TG3_FET_PTEST,
10654 MII_TG3_FET_PTEST_FRC_TX_LINK |
10655 MII_TG3_FET_PTEST_FRC_TX_LOCK);
10656 /* The write needs to be flushed for the AC131 */
10657 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
10658 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
Michael Chan5d64ad32006-12-07 00:19:40 -080010659 mac_mode |= MAC_MODE_PORT_MODE_MII;
10660 } else
10661 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chanb16250e2006-09-27 16:10:14 -070010662
Michael Chanc94e3942005-09-27 12:12:42 -070010663 /* reset to prevent losing 1st rx packet intermittently */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010664 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Michael Chanc94e3942005-09-27 12:12:42 -070010665 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
10666 udelay(10);
10667 tw32_f(MAC_RX_MODE, tp->rx_mode);
10668 }
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010669 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlson79eb6902010-02-17 15:17:03 +000010670 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
10671 if (masked_phy_id == TG3_PHY_ID_BCM5401)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010672 mac_mode &= ~MAC_MODE_LINK_POLARITY;
Matt Carlson79eb6902010-02-17 15:17:03 +000010673 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010674 mac_mode |= MAC_MODE_LINK_POLARITY;
Michael Chanff18ff02006-03-27 23:17:27 -080010675 tg3_writephy(tp, MII_TG3_EXT_CTRL,
10676 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
10677 }
Michael Chan9f40dea2005-09-05 17:53:06 -070010678 tw32(MAC_MODE, mac_mode);
Matt Carlson859a588792010-04-05 10:19:28 +000010679 } else {
Michael Chan9f40dea2005-09-05 17:53:06 -070010680 return -EINVAL;
Matt Carlson859a588792010-04-05 10:19:28 +000010681 }
Michael Chanc76949a2005-05-29 14:58:59 -070010682
10683 err = -EIO;
10684
Michael Chanc76949a2005-05-29 14:58:59 -070010685 tx_len = 1514;
David S. Millera20e9c62006-07-31 22:38:16 -070010686 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070010687 if (!skb)
10688 return -ENOMEM;
10689
Michael Chanc76949a2005-05-29 14:58:59 -070010690 tx_data = skb_put(skb, tx_len);
10691 memcpy(tx_data, tp->dev->dev_addr, 6);
10692 memset(tx_data + 6, 0x0, 8);
10693
10694 tw32(MAC_RX_MTU_SIZE, tx_len + 4);
10695
10696 for (i = 14; i < tx_len; i++)
10697 tx_data[i] = (u8) (i & 0xff);
10698
Alexander Duyckf4188d82009-12-02 16:48:38 +000010699 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
10700 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000010701 dev_kfree_skb(skb);
10702 return -EIO;
10703 }
Michael Chanc76949a2005-05-29 14:58:59 -070010704
10705 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010706 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070010707
10708 udelay(10);
10709
Matt Carlson898a56f2009-08-28 14:02:40 +000010710 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070010711
Michael Chanc76949a2005-05-29 14:58:59 -070010712 num_pkts = 0;
10713
Alexander Duyckf4188d82009-12-02 16:48:38 +000010714 tg3_set_txd(tnapi, tnapi->tx_prod, map, tx_len, 0, 1);
Michael Chanc76949a2005-05-29 14:58:59 -070010715
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010716 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070010717 num_pkts++;
10718
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010719 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
10720 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070010721
10722 udelay(10);
10723
Matt Carlson303fc922009-11-02 14:27:34 +000010724 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
10725 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070010726 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010727 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070010728
10729 udelay(10);
10730
Matt Carlson898a56f2009-08-28 14:02:40 +000010731 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
10732 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010733 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070010734 (rx_idx == (rx_start_idx + num_pkts)))
10735 break;
10736 }
10737
Alexander Duyckf4188d82009-12-02 16:48:38 +000010738 pci_unmap_single(tp->pdev, map, tx_len, PCI_DMA_TODEVICE);
Michael Chanc76949a2005-05-29 14:58:59 -070010739 dev_kfree_skb(skb);
10740
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010741 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070010742 goto out;
10743
10744 if (rx_idx != rx_start_idx + num_pkts)
10745 goto out;
10746
Matt Carlson72334482009-08-28 14:03:01 +000010747 desc = &rnapi->rx_rcb[rx_start_idx];
Michael Chanc76949a2005-05-29 14:58:59 -070010748 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
10749 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
10750 if (opaque_key != RXD_OPAQUE_RING_STD)
10751 goto out;
10752
10753 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
10754 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
10755 goto out;
10756
10757 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) - 4;
10758 if (rx_len != tx_len)
10759 goto out;
10760
Matt Carlson21f581a2009-08-28 14:00:25 +000010761 rx_skb = tpr->rx_std_buffers[desc_idx].skb;
Michael Chanc76949a2005-05-29 14:58:59 -070010762
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +000010763 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx], mapping);
Michael Chanc76949a2005-05-29 14:58:59 -070010764 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len, PCI_DMA_FROMDEVICE);
10765
10766 for (i = 14; i < tx_len; i++) {
10767 if (*(rx_skb->data + i) != (u8) (i & 0xff))
10768 goto out;
10769 }
10770 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010771
Michael Chanc76949a2005-05-29 14:58:59 -070010772 /* tg3_free_rings will unmap and free the rx_skb */
10773out:
10774 return err;
10775}
10776
Michael Chan9f40dea2005-09-05 17:53:06 -070010777#define TG3_MAC_LOOPBACK_FAILED 1
10778#define TG3_PHY_LOOPBACK_FAILED 2
10779#define TG3_LOOPBACK_FAILED (TG3_MAC_LOOPBACK_FAILED | \
10780 TG3_PHY_LOOPBACK_FAILED)
10781
10782static int tg3_test_loopback(struct tg3 *tp)
10783{
10784 int err = 0;
Matt Carlson9936bcf2007-10-10 18:03:07 -070010785 u32 cpmuctrl = 0;
Michael Chan9f40dea2005-09-05 17:53:06 -070010786
10787 if (!netif_running(tp->dev))
10788 return TG3_LOOPBACK_FAILED;
10789
Michael Chanb9ec6c12006-07-25 16:37:27 -070010790 err = tg3_reset_hw(tp, 1);
10791 if (err)
10792 return TG3_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070010793
Matt Carlson6833c042008-11-21 17:18:59 -080010794 /* Turn off gphy autopowerdown. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010795 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -080010796 tg3_phy_toggle_apd(tp, false);
10797
Matt Carlson321d32a2008-11-21 17:22:19 -080010798 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070010799 int i;
10800 u32 status;
10801
10802 tw32(TG3_CPMU_MUTEX_REQ, CPMU_MUTEX_REQ_DRIVER);
10803
10804 /* Wait for up to 40 microseconds to acquire lock. */
10805 for (i = 0; i < 4; i++) {
10806 status = tr32(TG3_CPMU_MUTEX_GNT);
10807 if (status == CPMU_MUTEX_GNT_DRIVER)
10808 break;
10809 udelay(10);
10810 }
10811
10812 if (status != CPMU_MUTEX_GNT_DRIVER)
10813 return TG3_LOOPBACK_FAILED;
10814
Matt Carlsonb2a5c192008-04-03 21:44:44 -070010815 /* Turn off link-based power management. */
Matt Carlsone8750932007-11-12 21:11:51 -080010816 cpmuctrl = tr32(TG3_CPMU_CTRL);
Matt Carlson109115e2008-05-02 16:48:59 -070010817 tw32(TG3_CPMU_CTRL,
10818 cpmuctrl & ~(CPMU_CTRL_LINK_SPEED_MODE |
10819 CPMU_CTRL_LINK_AWARE_MODE));
Matt Carlson9936bcf2007-10-10 18:03:07 -070010820 }
10821
Michael Chan9f40dea2005-09-05 17:53:06 -070010822 if (tg3_run_loopback(tp, TG3_MAC_LOOPBACK))
10823 err |= TG3_MAC_LOOPBACK_FAILED;
Matt Carlson9936bcf2007-10-10 18:03:07 -070010824
Matt Carlson321d32a2008-11-21 17:22:19 -080010825 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070010826 tw32(TG3_CPMU_CTRL, cpmuctrl);
10827
10828 /* Release the mutex */
10829 tw32(TG3_CPMU_MUTEX_GNT, CPMU_MUTEX_GNT_DRIVER);
10830 }
10831
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010832 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Matt Carlsondd477002008-05-25 23:45:58 -070010833 !(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)) {
Michael Chan9f40dea2005-09-05 17:53:06 -070010834 if (tg3_run_loopback(tp, TG3_PHY_LOOPBACK))
10835 err |= TG3_PHY_LOOPBACK_FAILED;
10836 }
10837
Matt Carlson6833c042008-11-21 17:18:59 -080010838 /* Re-enable gphy autopowerdown. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010839 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -080010840 tg3_phy_toggle_apd(tp, true);
10841
Michael Chan9f40dea2005-09-05 17:53:06 -070010842 return err;
10843}
10844
Michael Chan4cafd3f2005-05-29 14:56:34 -070010845static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
10846 u64 *data)
10847{
Michael Chan566f86a2005-05-29 14:56:58 -070010848 struct tg3 *tp = netdev_priv(dev);
10849
Matt Carlson800960682010-08-02 11:26:06 +000010850 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010851 tg3_set_power_state(tp, PCI_D0);
10852
Michael Chan566f86a2005-05-29 14:56:58 -070010853 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
10854
10855 if (tg3_test_nvram(tp) != 0) {
10856 etest->flags |= ETH_TEST_FL_FAILED;
10857 data[0] = 1;
10858 }
Michael Chanca430072005-05-29 14:57:23 -070010859 if (tg3_test_link(tp) != 0) {
10860 etest->flags |= ETH_TEST_FL_FAILED;
10861 data[1] = 1;
10862 }
Michael Chana71116d2005-05-29 14:58:11 -070010863 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010864 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070010865
Michael Chanbbe832c2005-06-24 20:20:04 -070010866 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010867 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010868 tg3_netif_stop(tp);
10869 irq_sync = 1;
10870 }
10871
10872 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070010873
10874 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080010875 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070010876 tg3_halt_cpu(tp, RX_CPU_BASE);
10877 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
10878 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080010879 if (!err)
10880 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070010881
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010882 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad2006-03-20 22:27:35 -080010883 tg3_phy_reset(tp);
10884
Michael Chana71116d2005-05-29 14:58:11 -070010885 if (tg3_test_registers(tp) != 0) {
10886 etest->flags |= ETH_TEST_FL_FAILED;
10887 data[2] = 1;
10888 }
Michael Chan7942e1d2005-05-29 14:58:36 -070010889 if (tg3_test_memory(tp) != 0) {
10890 etest->flags |= ETH_TEST_FL_FAILED;
10891 data[3] = 1;
10892 }
Michael Chan9f40dea2005-09-05 17:53:06 -070010893 if ((data[4] = tg3_test_loopback(tp)) != 0)
Michael Chanc76949a2005-05-29 14:58:59 -070010894 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070010895
David S. Millerf47c11e2005-06-24 20:18:35 -070010896 tg3_full_unlock(tp);
10897
Michael Chand4bc3922005-05-29 14:59:20 -070010898 if (tg3_test_interrupt(tp) != 0) {
10899 etest->flags |= ETH_TEST_FL_FAILED;
10900 data[5] = 1;
10901 }
David S. Millerf47c11e2005-06-24 20:18:35 -070010902
10903 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070010904
Michael Chana71116d2005-05-29 14:58:11 -070010905 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10906 if (netif_running(dev)) {
10907 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010908 err2 = tg3_restart_hw(tp, 1);
10909 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070010910 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070010911 }
David S. Millerf47c11e2005-06-24 20:18:35 -070010912
10913 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010914
10915 if (irq_sync && !err2)
10916 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070010917 }
Matt Carlson800960682010-08-02 11:26:06 +000010918 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010919 tg3_set_power_state(tp, PCI_D3hot);
10920
Michael Chan4cafd3f2005-05-29 14:56:34 -070010921}
10922
Linus Torvalds1da177e2005-04-16 15:20:36 -070010923static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
10924{
10925 struct mii_ioctl_data *data = if_mii(ifr);
10926 struct tg3 *tp = netdev_priv(dev);
10927 int err;
10928
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010929 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010930 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010931 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010932 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010933 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000010934 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010935 }
10936
Matt Carlson33f401a2010-04-05 10:19:27 +000010937 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010938 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000010939 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010940
10941 /* fallthru */
10942 case SIOCGMIIREG: {
10943 u32 mii_regval;
10944
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010945 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010946 break; /* We have no PHY */
10947
Matt Carlson800960682010-08-02 11:26:06 +000010948 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010949 return -EAGAIN;
10950
David S. Millerf47c11e2005-06-24 20:18:35 -070010951 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010952 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070010953 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010954
10955 data->val_out = mii_regval;
10956
10957 return err;
10958 }
10959
10960 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010961 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010962 break; /* We have no PHY */
10963
Matt Carlson800960682010-08-02 11:26:06 +000010964 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010965 return -EAGAIN;
10966
David S. Millerf47c11e2005-06-24 20:18:35 -070010967 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010968 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070010969 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010970
10971 return err;
10972
10973 default:
10974 /* do nothing */
10975 break;
10976 }
10977 return -EOPNOTSUPP;
10978}
10979
10980#if TG3_VLAN_TAG_USED
10981static void tg3_vlan_rx_register(struct net_device *dev, struct vlan_group *grp)
10982{
10983 struct tg3 *tp = netdev_priv(dev);
10984
Matt Carlson844b3ee2009-02-25 14:23:56 +000010985 if (!netif_running(dev)) {
10986 tp->vlgrp = grp;
10987 return;
10988 }
10989
10990 tg3_netif_stop(tp);
Michael Chan29315e82006-06-29 20:12:30 -070010991
David S. Millerf47c11e2005-06-24 20:18:35 -070010992 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010993
10994 tp->vlgrp = grp;
10995
10996 /* Update RX_MODE_KEEP_VLAN_TAG bit in RX_MODE register. */
10997 __tg3_set_rx_mode(dev);
10998
Matt Carlson844b3ee2009-02-25 14:23:56 +000010999 tg3_netif_start(tp);
Michael Chan46966542007-07-11 19:47:19 -070011000
11001 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011002}
Linus Torvalds1da177e2005-04-16 15:20:36 -070011003#endif
11004
David S. Miller15f98502005-05-18 22:49:26 -070011005static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11006{
11007 struct tg3 *tp = netdev_priv(dev);
11008
11009 memcpy(ec, &tp->coal, sizeof(*ec));
11010 return 0;
11011}
11012
Michael Chand244c892005-07-05 14:42:33 -070011013static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11014{
11015 struct tg3 *tp = netdev_priv(dev);
11016 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
11017 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
11018
11019 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
11020 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
11021 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
11022 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
11023 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
11024 }
11025
11026 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
11027 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
11028 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
11029 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
11030 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
11031 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
11032 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
11033 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
11034 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
11035 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
11036 return -EINVAL;
11037
11038 /* No rx interrupts will be generated if both are zero */
11039 if ((ec->rx_coalesce_usecs == 0) &&
11040 (ec->rx_max_coalesced_frames == 0))
11041 return -EINVAL;
11042
11043 /* No tx interrupts will be generated if both are zero */
11044 if ((ec->tx_coalesce_usecs == 0) &&
11045 (ec->tx_max_coalesced_frames == 0))
11046 return -EINVAL;
11047
11048 /* Only copy relevant parameters, ignore all others. */
11049 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
11050 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
11051 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
11052 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
11053 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
11054 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
11055 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
11056 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
11057 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
11058
11059 if (netif_running(dev)) {
11060 tg3_full_lock(tp, 0);
11061 __tg3_set_coalesce(tp, &tp->coal);
11062 tg3_full_unlock(tp);
11063 }
11064 return 0;
11065}
11066
Jeff Garzik7282d492006-09-13 14:30:00 -040011067static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011068 .get_settings = tg3_get_settings,
11069 .set_settings = tg3_set_settings,
11070 .get_drvinfo = tg3_get_drvinfo,
11071 .get_regs_len = tg3_get_regs_len,
11072 .get_regs = tg3_get_regs,
11073 .get_wol = tg3_get_wol,
11074 .set_wol = tg3_set_wol,
11075 .get_msglevel = tg3_get_msglevel,
11076 .set_msglevel = tg3_set_msglevel,
11077 .nway_reset = tg3_nway_reset,
11078 .get_link = ethtool_op_get_link,
11079 .get_eeprom_len = tg3_get_eeprom_len,
11080 .get_eeprom = tg3_get_eeprom,
11081 .set_eeprom = tg3_set_eeprom,
11082 .get_ringparam = tg3_get_ringparam,
11083 .set_ringparam = tg3_set_ringparam,
11084 .get_pauseparam = tg3_get_pauseparam,
11085 .set_pauseparam = tg3_set_pauseparam,
11086 .get_rx_csum = tg3_get_rx_csum,
11087 .set_rx_csum = tg3_set_rx_csum,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011088 .set_tx_csum = tg3_set_tx_csum,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011089 .set_sg = ethtool_op_set_sg,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011090 .set_tso = tg3_set_tso,
Michael Chan4cafd3f2005-05-29 14:56:34 -070011091 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011092 .get_strings = tg3_get_strings,
Michael Chan4009a932005-09-05 17:52:54 -070011093 .phys_id = tg3_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011094 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070011095 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070011096 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070011097 .get_sset_count = tg3_get_sset_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011098};
11099
11100static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
11101{
Michael Chan1b277772006-03-20 22:27:48 -080011102 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011103
11104 tp->nvram_size = EEPROM_CHIP_SIZE;
11105
Matt Carlsone4f34112009-02-25 14:25:00 +000011106 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011107 return;
11108
Michael Chanb16250e2006-09-27 16:10:14 -070011109 if ((magic != TG3_EEPROM_MAGIC) &&
11110 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
11111 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011112 return;
11113
11114 /*
11115 * Size the chip by reading offsets at increasing powers of two.
11116 * When we encounter our validation signature, we know the addressing
11117 * has wrapped around, and thus have our chip size.
11118 */
Michael Chan1b277772006-03-20 22:27:48 -080011119 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011120
11121 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000011122 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011123 return;
11124
Michael Chan18201802006-03-20 22:29:15 -080011125 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011126 break;
11127
11128 cursize <<= 1;
11129 }
11130
11131 tp->nvram_size = cursize;
11132}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011133
Linus Torvalds1da177e2005-04-16 15:20:36 -070011134static void __devinit tg3_get_nvram_size(struct tg3 *tp)
11135{
11136 u32 val;
11137
Matt Carlsondf259d82009-04-20 06:57:14 +000011138 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
11139 tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080011140 return;
11141
11142 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080011143 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080011144 tg3_get_eeprom_size(tp);
11145 return;
11146 }
11147
Matt Carlson6d348f22009-02-25 14:25:52 +000011148 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011149 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000011150 /* This is confusing. We want to operate on the
11151 * 16-bit value at offset 0xf2. The tg3_nvram_read()
11152 * call will read from NVRAM and byteswap the data
11153 * according to the byteswapping settings for all
11154 * other register accesses. This ensures the data we
11155 * want will always reside in the lower 16-bits.
11156 * However, the data in NVRAM is in LE format, which
11157 * means the data from the NVRAM read will always be
11158 * opposite the endianness of the CPU. The 16-bit
11159 * byteswap then brings the data to CPU endianness.
11160 */
11161 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011162 return;
11163 }
11164 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070011165 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011166}
11167
11168static void __devinit tg3_get_nvram_info(struct tg3 *tp)
11169{
11170 u32 nvcfg1;
11171
11172 nvcfg1 = tr32(NVRAM_CFG1);
11173 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
11174 tp->tg3_flags2 |= TG3_FLG2_FLASH;
Matt Carlson8590a602009-08-28 12:29:16 +000011175 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011176 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11177 tw32(NVRAM_CFG1, nvcfg1);
11178 }
11179
Michael Chan4c987482005-09-05 17:52:38 -070011180 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750) ||
Michael Chana4e2b342005-10-26 15:46:52 -070011181 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011182 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011183 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
11184 tp->nvram_jedecnum = JEDEC_ATMEL;
11185 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
11186 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11187 break;
11188 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
11189 tp->nvram_jedecnum = JEDEC_ATMEL;
11190 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
11191 break;
11192 case FLASH_VENDOR_ATMEL_EEPROM:
11193 tp->nvram_jedecnum = JEDEC_ATMEL;
11194 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11195 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11196 break;
11197 case FLASH_VENDOR_ST:
11198 tp->nvram_jedecnum = JEDEC_ST;
11199 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
11200 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11201 break;
11202 case FLASH_VENDOR_SAIFUN:
11203 tp->nvram_jedecnum = JEDEC_SAIFUN;
11204 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
11205 break;
11206 case FLASH_VENDOR_SST_SMALL:
11207 case FLASH_VENDOR_SST_LARGE:
11208 tp->nvram_jedecnum = JEDEC_SST;
11209 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
11210 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011211 }
Matt Carlson8590a602009-08-28 12:29:16 +000011212 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011213 tp->nvram_jedecnum = JEDEC_ATMEL;
11214 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
11215 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11216 }
11217}
11218
Matt Carlsona1b950d2009-09-01 13:20:17 +000011219static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
11220{
11221 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
11222 case FLASH_5752PAGE_SIZE_256:
11223 tp->nvram_pagesize = 256;
11224 break;
11225 case FLASH_5752PAGE_SIZE_512:
11226 tp->nvram_pagesize = 512;
11227 break;
11228 case FLASH_5752PAGE_SIZE_1K:
11229 tp->nvram_pagesize = 1024;
11230 break;
11231 case FLASH_5752PAGE_SIZE_2K:
11232 tp->nvram_pagesize = 2048;
11233 break;
11234 case FLASH_5752PAGE_SIZE_4K:
11235 tp->nvram_pagesize = 4096;
11236 break;
11237 case FLASH_5752PAGE_SIZE_264:
11238 tp->nvram_pagesize = 264;
11239 break;
11240 case FLASH_5752PAGE_SIZE_528:
11241 tp->nvram_pagesize = 528;
11242 break;
11243 }
11244}
11245
Michael Chan361b4ac2005-04-21 17:11:21 -070011246static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
11247{
11248 u32 nvcfg1;
11249
11250 nvcfg1 = tr32(NVRAM_CFG1);
11251
Michael Chane6af3012005-04-21 17:12:05 -070011252 /* NVRAM protection for TPM */
11253 if (nvcfg1 & (1 << 27))
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011254 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Michael Chane6af3012005-04-21 17:12:05 -070011255
Michael Chan361b4ac2005-04-21 17:11:21 -070011256 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011257 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
11258 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
11259 tp->nvram_jedecnum = JEDEC_ATMEL;
11260 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11261 break;
11262 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11263 tp->nvram_jedecnum = JEDEC_ATMEL;
11264 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11265 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11266 break;
11267 case FLASH_5752VENDOR_ST_M45PE10:
11268 case FLASH_5752VENDOR_ST_M45PE20:
11269 case FLASH_5752VENDOR_ST_M45PE40:
11270 tp->nvram_jedecnum = JEDEC_ST;
11271 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11272 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11273 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070011274 }
11275
11276 if (tp->tg3_flags2 & TG3_FLG2_FLASH) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000011277 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000011278 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070011279 /* For eeprom, set pagesize to maximum eeprom size */
11280 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11281
11282 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11283 tw32(NVRAM_CFG1, nvcfg1);
11284 }
11285}
11286
Michael Chand3c7b882006-03-23 01:28:25 -080011287static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
11288{
Matt Carlson989a9d22007-05-05 11:51:05 -070011289 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080011290
11291 nvcfg1 = tr32(NVRAM_CFG1);
11292
11293 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070011294 if (nvcfg1 & (1 << 27)) {
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011295 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Matt Carlson989a9d22007-05-05 11:51:05 -070011296 protect = 1;
11297 }
Michael Chand3c7b882006-03-23 01:28:25 -080011298
Matt Carlson989a9d22007-05-05 11:51:05 -070011299 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11300 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011301 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11302 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11303 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11304 case FLASH_5755VENDOR_ATMEL_FLASH_5:
11305 tp->nvram_jedecnum = JEDEC_ATMEL;
11306 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11307 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11308 tp->nvram_pagesize = 264;
11309 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
11310 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
11311 tp->nvram_size = (protect ? 0x3e200 :
11312 TG3_NVRAM_SIZE_512KB);
11313 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
11314 tp->nvram_size = (protect ? 0x1f200 :
11315 TG3_NVRAM_SIZE_256KB);
11316 else
11317 tp->nvram_size = (protect ? 0x1f200 :
11318 TG3_NVRAM_SIZE_128KB);
11319 break;
11320 case FLASH_5752VENDOR_ST_M45PE10:
11321 case FLASH_5752VENDOR_ST_M45PE20:
11322 case FLASH_5752VENDOR_ST_M45PE40:
11323 tp->nvram_jedecnum = JEDEC_ST;
11324 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11325 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11326 tp->nvram_pagesize = 256;
11327 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
11328 tp->nvram_size = (protect ?
11329 TG3_NVRAM_SIZE_64KB :
11330 TG3_NVRAM_SIZE_128KB);
11331 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
11332 tp->nvram_size = (protect ?
11333 TG3_NVRAM_SIZE_64KB :
11334 TG3_NVRAM_SIZE_256KB);
11335 else
11336 tp->nvram_size = (protect ?
11337 TG3_NVRAM_SIZE_128KB :
11338 TG3_NVRAM_SIZE_512KB);
11339 break;
Michael Chand3c7b882006-03-23 01:28:25 -080011340 }
11341}
11342
Michael Chan1b277772006-03-20 22:27:48 -080011343static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
11344{
11345 u32 nvcfg1;
11346
11347 nvcfg1 = tr32(NVRAM_CFG1);
11348
11349 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011350 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
11351 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11352 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
11353 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11354 tp->nvram_jedecnum = JEDEC_ATMEL;
11355 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11356 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080011357
Matt Carlson8590a602009-08-28 12:29:16 +000011358 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11359 tw32(NVRAM_CFG1, nvcfg1);
11360 break;
11361 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11362 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11363 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11364 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11365 tp->nvram_jedecnum = JEDEC_ATMEL;
11366 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11367 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11368 tp->nvram_pagesize = 264;
11369 break;
11370 case FLASH_5752VENDOR_ST_M45PE10:
11371 case FLASH_5752VENDOR_ST_M45PE20:
11372 case FLASH_5752VENDOR_ST_M45PE40:
11373 tp->nvram_jedecnum = JEDEC_ST;
11374 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11375 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11376 tp->nvram_pagesize = 256;
11377 break;
Michael Chan1b277772006-03-20 22:27:48 -080011378 }
11379}
11380
Matt Carlson6b91fa02007-10-10 18:01:09 -070011381static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
11382{
11383 u32 nvcfg1, protect = 0;
11384
11385 nvcfg1 = tr32(NVRAM_CFG1);
11386
11387 /* NVRAM protection for TPM */
11388 if (nvcfg1 & (1 << 27)) {
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011389 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011390 protect = 1;
11391 }
11392
11393 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11394 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011395 case FLASH_5761VENDOR_ATMEL_ADB021D:
11396 case FLASH_5761VENDOR_ATMEL_ADB041D:
11397 case FLASH_5761VENDOR_ATMEL_ADB081D:
11398 case FLASH_5761VENDOR_ATMEL_ADB161D:
11399 case FLASH_5761VENDOR_ATMEL_MDB021D:
11400 case FLASH_5761VENDOR_ATMEL_MDB041D:
11401 case FLASH_5761VENDOR_ATMEL_MDB081D:
11402 case FLASH_5761VENDOR_ATMEL_MDB161D:
11403 tp->nvram_jedecnum = JEDEC_ATMEL;
11404 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11405 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11406 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
11407 tp->nvram_pagesize = 256;
11408 break;
11409 case FLASH_5761VENDOR_ST_A_M45PE20:
11410 case FLASH_5761VENDOR_ST_A_M45PE40:
11411 case FLASH_5761VENDOR_ST_A_M45PE80:
11412 case FLASH_5761VENDOR_ST_A_M45PE16:
11413 case FLASH_5761VENDOR_ST_M_M45PE20:
11414 case FLASH_5761VENDOR_ST_M_M45PE40:
11415 case FLASH_5761VENDOR_ST_M_M45PE80:
11416 case FLASH_5761VENDOR_ST_M_M45PE16:
11417 tp->nvram_jedecnum = JEDEC_ST;
11418 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11419 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11420 tp->nvram_pagesize = 256;
11421 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011422 }
11423
11424 if (protect) {
11425 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
11426 } else {
11427 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011428 case FLASH_5761VENDOR_ATMEL_ADB161D:
11429 case FLASH_5761VENDOR_ATMEL_MDB161D:
11430 case FLASH_5761VENDOR_ST_A_M45PE16:
11431 case FLASH_5761VENDOR_ST_M_M45PE16:
11432 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
11433 break;
11434 case FLASH_5761VENDOR_ATMEL_ADB081D:
11435 case FLASH_5761VENDOR_ATMEL_MDB081D:
11436 case FLASH_5761VENDOR_ST_A_M45PE80:
11437 case FLASH_5761VENDOR_ST_M_M45PE80:
11438 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
11439 break;
11440 case FLASH_5761VENDOR_ATMEL_ADB041D:
11441 case FLASH_5761VENDOR_ATMEL_MDB041D:
11442 case FLASH_5761VENDOR_ST_A_M45PE40:
11443 case FLASH_5761VENDOR_ST_M_M45PE40:
11444 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11445 break;
11446 case FLASH_5761VENDOR_ATMEL_ADB021D:
11447 case FLASH_5761VENDOR_ATMEL_MDB021D:
11448 case FLASH_5761VENDOR_ST_A_M45PE20:
11449 case FLASH_5761VENDOR_ST_M_M45PE20:
11450 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11451 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011452 }
11453 }
11454}
11455
Michael Chanb5d37722006-09-27 16:06:21 -070011456static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
11457{
11458 tp->nvram_jedecnum = JEDEC_ATMEL;
11459 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11460 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11461}
11462
Matt Carlson321d32a2008-11-21 17:22:19 -080011463static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
11464{
11465 u32 nvcfg1;
11466
11467 nvcfg1 = tr32(NVRAM_CFG1);
11468
11469 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11470 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11471 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11472 tp->nvram_jedecnum = JEDEC_ATMEL;
11473 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11474 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11475
11476 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11477 tw32(NVRAM_CFG1, nvcfg1);
11478 return;
11479 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11480 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11481 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11482 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11483 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11484 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11485 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
11486 tp->nvram_jedecnum = JEDEC_ATMEL;
11487 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11488 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11489
11490 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11491 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11492 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11493 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11494 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11495 break;
11496 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11497 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11498 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11499 break;
11500 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11501 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
11502 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11503 break;
11504 }
11505 break;
11506 case FLASH_5752VENDOR_ST_M45PE10:
11507 case FLASH_5752VENDOR_ST_M45PE20:
11508 case FLASH_5752VENDOR_ST_M45PE40:
11509 tp->nvram_jedecnum = JEDEC_ST;
11510 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11511 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11512
11513 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11514 case FLASH_5752VENDOR_ST_M45PE10:
11515 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11516 break;
11517 case FLASH_5752VENDOR_ST_M45PE20:
11518 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11519 break;
11520 case FLASH_5752VENDOR_ST_M45PE40:
11521 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11522 break;
11523 }
11524 break;
11525 default:
Matt Carlsondf259d82009-04-20 06:57:14 +000011526 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
Matt Carlson321d32a2008-11-21 17:22:19 -080011527 return;
11528 }
11529
Matt Carlsona1b950d2009-09-01 13:20:17 +000011530 tg3_nvram_get_pagesize(tp, nvcfg1);
11531 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Matt Carlson321d32a2008-11-21 17:22:19 -080011532 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011533}
11534
11535
11536static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
11537{
11538 u32 nvcfg1;
11539
11540 nvcfg1 = tr32(NVRAM_CFG1);
11541
11542 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11543 case FLASH_5717VENDOR_ATMEL_EEPROM:
11544 case FLASH_5717VENDOR_MICRO_EEPROM:
11545 tp->nvram_jedecnum = JEDEC_ATMEL;
11546 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11547 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11548
11549 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11550 tw32(NVRAM_CFG1, nvcfg1);
11551 return;
11552 case FLASH_5717VENDOR_ATMEL_MDB011D:
11553 case FLASH_5717VENDOR_ATMEL_ADB011B:
11554 case FLASH_5717VENDOR_ATMEL_ADB011D:
11555 case FLASH_5717VENDOR_ATMEL_MDB021D:
11556 case FLASH_5717VENDOR_ATMEL_ADB021B:
11557 case FLASH_5717VENDOR_ATMEL_ADB021D:
11558 case FLASH_5717VENDOR_ATMEL_45USPT:
11559 tp->nvram_jedecnum = JEDEC_ATMEL;
11560 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11561 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11562
11563 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11564 case FLASH_5717VENDOR_ATMEL_MDB021D:
11565 case FLASH_5717VENDOR_ATMEL_ADB021B:
11566 case FLASH_5717VENDOR_ATMEL_ADB021D:
11567 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11568 break;
11569 default:
11570 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11571 break;
11572 }
Matt Carlson321d32a2008-11-21 17:22:19 -080011573 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011574 case FLASH_5717VENDOR_ST_M_M25PE10:
11575 case FLASH_5717VENDOR_ST_A_M25PE10:
11576 case FLASH_5717VENDOR_ST_M_M45PE10:
11577 case FLASH_5717VENDOR_ST_A_M45PE10:
11578 case FLASH_5717VENDOR_ST_M_M25PE20:
11579 case FLASH_5717VENDOR_ST_A_M25PE20:
11580 case FLASH_5717VENDOR_ST_M_M45PE20:
11581 case FLASH_5717VENDOR_ST_A_M45PE20:
11582 case FLASH_5717VENDOR_ST_25USPT:
11583 case FLASH_5717VENDOR_ST_45USPT:
11584 tp->nvram_jedecnum = JEDEC_ST;
11585 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11586 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11587
11588 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
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 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11594 break;
11595 default:
11596 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11597 break;
11598 }
Matt Carlson321d32a2008-11-21 17:22:19 -080011599 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011600 default:
11601 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
11602 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080011603 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000011604
11605 tg3_nvram_get_pagesize(tp, nvcfg1);
11606 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
11607 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
Matt Carlson321d32a2008-11-21 17:22:19 -080011608}
11609
Linus Torvalds1da177e2005-04-16 15:20:36 -070011610/* Chips other than 5700/5701 use the NVRAM for fetching info. */
11611static void __devinit tg3_nvram_init(struct tg3 *tp)
11612{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011613 tw32_f(GRC_EEPROM_ADDR,
11614 (EEPROM_ADDR_FSM_RESET |
11615 (EEPROM_DEFAULT_CLOCK_PERIOD <<
11616 EEPROM_ADDR_CLKPERD_SHIFT)));
11617
Michael Chan9d57f012006-12-07 00:23:25 -080011618 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011619
11620 /* Enable seeprom accesses. */
11621 tw32_f(GRC_LOCAL_CTRL,
11622 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
11623 udelay(100);
11624
11625 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
11626 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
11627 tp->tg3_flags |= TG3_FLAG_NVRAM;
11628
Michael Chanec41c7d2006-01-17 02:40:55 -080011629 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000011630 netdev_warn(tp->dev,
11631 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000011632 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080011633 return;
11634 }
Michael Chane6af3012005-04-21 17:12:05 -070011635 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011636
Matt Carlson989a9d22007-05-05 11:51:05 -070011637 tp->nvram_size = 0;
11638
Michael Chan361b4ac2005-04-21 17:11:21 -070011639 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
11640 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080011641 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
11642 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070011643 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070011644 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
11645 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080011646 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070011647 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
11648 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070011649 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
11650 tg3_get_5906_nvram_info(tp);
Matt Carlsonb703df62009-12-03 08:36:21 +000011651 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
11652 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson321d32a2008-11-21 17:22:19 -080011653 tg3_get_57780_nvram_info(tp);
Matt Carlsona50d0792010-06-05 17:24:37 +000011654 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
11655 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000011656 tg3_get_5717_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070011657 else
11658 tg3_get_nvram_info(tp);
11659
Matt Carlson989a9d22007-05-05 11:51:05 -070011660 if (tp->nvram_size == 0)
11661 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011662
Michael Chane6af3012005-04-21 17:12:05 -070011663 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080011664 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011665
11666 } else {
11667 tp->tg3_flags &= ~(TG3_FLAG_NVRAM | TG3_FLAG_NVRAM_BUFFERED);
11668
11669 tg3_get_eeprom_size(tp);
11670 }
11671}
11672
Linus Torvalds1da177e2005-04-16 15:20:36 -070011673static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
11674 u32 offset, u32 len, u8 *buf)
11675{
11676 int i, j, rc = 0;
11677 u32 val;
11678
11679 for (i = 0; i < len; i += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080011680 u32 addr;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011681 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011682
11683 addr = offset + i;
11684
11685 memcpy(&data, buf + i, 4);
11686
Matt Carlson62cedd12009-04-20 14:52:29 -070011687 /*
11688 * The SEEPROM interface expects the data to always be opposite
11689 * the native endian format. We accomplish this by reversing
11690 * all the operations that would have been performed on the
11691 * data from a call to tg3_nvram_read_be32().
11692 */
11693 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011694
11695 val = tr32(GRC_EEPROM_ADDR);
11696 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
11697
11698 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
11699 EEPROM_ADDR_READ);
11700 tw32(GRC_EEPROM_ADDR, val |
11701 (0 << EEPROM_ADDR_DEVID_SHIFT) |
11702 (addr & EEPROM_ADDR_ADDR_MASK) |
11703 EEPROM_ADDR_START |
11704 EEPROM_ADDR_WRITE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011705
Michael Chan9d57f012006-12-07 00:23:25 -080011706 for (j = 0; j < 1000; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011707 val = tr32(GRC_EEPROM_ADDR);
11708
11709 if (val & EEPROM_ADDR_COMPLETE)
11710 break;
Michael Chan9d57f012006-12-07 00:23:25 -080011711 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011712 }
11713 if (!(val & EEPROM_ADDR_COMPLETE)) {
11714 rc = -EBUSY;
11715 break;
11716 }
11717 }
11718
11719 return rc;
11720}
11721
11722/* offset and length are dword aligned */
11723static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
11724 u8 *buf)
11725{
11726 int ret = 0;
11727 u32 pagesize = tp->nvram_pagesize;
11728 u32 pagemask = pagesize - 1;
11729 u32 nvram_cmd;
11730 u8 *tmp;
11731
11732 tmp = kmalloc(pagesize, GFP_KERNEL);
11733 if (tmp == NULL)
11734 return -ENOMEM;
11735
11736 while (len) {
11737 int j;
Michael Chane6af3012005-04-21 17:12:05 -070011738 u32 phy_addr, page_off, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011739
11740 phy_addr = offset & ~pagemask;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011741
Linus Torvalds1da177e2005-04-16 15:20:36 -070011742 for (j = 0; j < pagesize; j += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000011743 ret = tg3_nvram_read_be32(tp, phy_addr + j,
11744 (__be32 *) (tmp + j));
11745 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011746 break;
11747 }
11748 if (ret)
11749 break;
11750
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011751 page_off = offset & pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011752 size = pagesize;
11753 if (len < size)
11754 size = len;
11755
11756 len -= size;
11757
11758 memcpy(tmp + page_off, buf, size);
11759
11760 offset = offset + (pagesize - page_off);
11761
Michael Chane6af3012005-04-21 17:12:05 -070011762 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011763
11764 /*
11765 * Before we can erase the flash page, we need
11766 * to issue a special "write enable" command.
11767 */
11768 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
11769
11770 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
11771 break;
11772
11773 /* Erase the target page */
11774 tw32(NVRAM_ADDR, phy_addr);
11775
11776 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
11777 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
11778
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011779 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011780 break;
11781
11782 /* Issue another write enable to start the write. */
11783 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
11784
11785 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
11786 break;
11787
11788 for (j = 0; j < pagesize; j += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080011789 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011790
Al Virob9fc7dc2007-12-17 22:59:57 -080011791 data = *((__be32 *) (tmp + j));
Matt Carlsona9dc5292009-02-25 14:25:30 +000011792
Al Virob9fc7dc2007-12-17 22:59:57 -080011793 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011794
11795 tw32(NVRAM_ADDR, phy_addr + j);
11796
11797 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
11798 NVRAM_CMD_WR;
11799
11800 if (j == 0)
11801 nvram_cmd |= NVRAM_CMD_FIRST;
11802 else if (j == (pagesize - 4))
11803 nvram_cmd |= NVRAM_CMD_LAST;
11804
11805 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
11806 break;
11807 }
11808 if (ret)
11809 break;
11810 }
11811
11812 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
11813 tg3_nvram_exec_cmd(tp, nvram_cmd);
11814
11815 kfree(tmp);
11816
11817 return ret;
11818}
11819
11820/* offset and length are dword aligned */
11821static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
11822 u8 *buf)
11823{
11824 int i, ret = 0;
11825
11826 for (i = 0; i < len; i += 4, offset += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080011827 u32 page_off, phy_addr, nvram_cmd;
11828 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011829
11830 memcpy(&data, buf + i, 4);
Al Virob9fc7dc2007-12-17 22:59:57 -080011831 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011832
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011833 page_off = offset % tp->nvram_pagesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011834
Michael Chan18201802006-03-20 22:29:15 -080011835 phy_addr = tg3_nvram_phys_addr(tp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011836
11837 tw32(NVRAM_ADDR, phy_addr);
11838
11839 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
11840
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011841 if (page_off == 0 || i == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011842 nvram_cmd |= NVRAM_CMD_FIRST;
Michael Chanf6d9a252006-04-29 19:00:24 -070011843 if (page_off == (tp->nvram_pagesize - 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011844 nvram_cmd |= NVRAM_CMD_LAST;
11845
11846 if (i == (len - 4))
11847 nvram_cmd |= NVRAM_CMD_LAST;
11848
Matt Carlson321d32a2008-11-21 17:22:19 -080011849 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
11850 !(tp->tg3_flags3 & TG3_FLG3_5755_PLUS) &&
Michael Chan4c987482005-09-05 17:52:38 -070011851 (tp->nvram_jedecnum == JEDEC_ST) &&
11852 (nvram_cmd & NVRAM_CMD_FIRST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011853
11854 if ((ret = tg3_nvram_exec_cmd(tp,
11855 NVRAM_CMD_WREN | NVRAM_CMD_GO |
11856 NVRAM_CMD_DONE)))
11857
11858 break;
11859 }
11860 if (!(tp->tg3_flags2 & TG3_FLG2_FLASH)) {
11861 /* We always do complete word writes to eeprom. */
11862 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
11863 }
11864
11865 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
11866 break;
11867 }
11868 return ret;
11869}
11870
11871/* offset and length are dword aligned */
11872static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
11873{
11874 int ret;
11875
Linus Torvalds1da177e2005-04-16 15:20:36 -070011876 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT) {
Michael Chan314fba32005-04-21 17:07:04 -070011877 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
11878 ~GRC_LCLCTRL_GPIO_OUTPUT1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011879 udelay(40);
11880 }
11881
11882 if (!(tp->tg3_flags & TG3_FLAG_NVRAM)) {
11883 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
Matt Carlson859a588792010-04-05 10:19:28 +000011884 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011885 u32 grc_mode;
11886
Michael Chanec41c7d2006-01-17 02:40:55 -080011887 ret = tg3_nvram_lock(tp);
11888 if (ret)
11889 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011890
Michael Chane6af3012005-04-21 17:12:05 -070011891 tg3_enable_nvram_access(tp);
11892 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011893 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011894 tw32(NVRAM_WRITE1, 0x406);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011895
11896 grc_mode = tr32(GRC_MODE);
11897 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
11898
11899 if ((tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) ||
11900 !(tp->tg3_flags2 & TG3_FLG2_FLASH)) {
11901
11902 ret = tg3_nvram_write_block_buffered(tp, offset, len,
11903 buf);
Matt Carlson859a588792010-04-05 10:19:28 +000011904 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011905 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
11906 buf);
11907 }
11908
11909 grc_mode = tr32(GRC_MODE);
11910 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
11911
Michael Chane6af3012005-04-21 17:12:05 -070011912 tg3_disable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011913 tg3_nvram_unlock(tp);
11914 }
11915
11916 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT) {
Michael Chan314fba32005-04-21 17:07:04 -070011917 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011918 udelay(40);
11919 }
11920
11921 return ret;
11922}
11923
11924struct subsys_tbl_ent {
11925 u16 subsys_vendor, subsys_devid;
11926 u32 phy_id;
11927};
11928
Matt Carlson24daf2b2010-02-17 15:17:02 +000011929static struct subsys_tbl_ent subsys_id_to_phy_id[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011930 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000011931 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011932 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011933 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011934 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011935 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011936 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011937 { TG3PCI_SUBVENDOR_ID_BROADCOM,
11938 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
11939 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011940 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011941 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011942 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011943 { TG3PCI_SUBVENDOR_ID_BROADCOM,
11944 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
11945 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011946 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011947 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011948 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011949 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011950 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011951 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011952 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070011953
11954 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000011955 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011956 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011957 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011958 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011959 { TG3PCI_SUBVENDOR_ID_3COM,
11960 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
11961 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011962 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011963 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011964 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070011965
11966 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000011967 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000011968 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011969 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000011970 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011971 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000011972 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011973 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000011974 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070011975
11976 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000011977 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000011978 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011979 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000011980 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011981 { TG3PCI_SUBVENDOR_ID_COMPAQ,
11982 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
11983 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000011984 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011985 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000011986 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070011987
11988 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000011989 { TG3PCI_SUBVENDOR_ID_IBM,
11990 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011991};
11992
Matt Carlson24daf2b2010-02-17 15:17:02 +000011993static struct subsys_tbl_ent * __devinit tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011994{
11995 int i;
11996
11997 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
11998 if ((subsys_id_to_phy_id[i].subsys_vendor ==
11999 tp->pdev->subsystem_vendor) &&
12000 (subsys_id_to_phy_id[i].subsys_devid ==
12001 tp->pdev->subsystem_device))
12002 return &subsys_id_to_phy_id[i];
12003 }
12004 return NULL;
12005}
12006
Michael Chan7d0c41e2005-04-21 17:06:20 -070012007static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012008{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012009 u32 val;
Michael Chancaf636c72006-03-22 01:05:31 -080012010 u16 pmcsr;
12011
12012 /* On some early chips the SRAM cannot be accessed in D3hot state,
12013 * so need make sure we're in D0.
12014 */
12015 pci_read_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, &pmcsr);
12016 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
12017 pci_write_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, pmcsr);
12018 msleep(1);
Michael Chan7d0c41e2005-04-21 17:06:20 -070012019
12020 /* Make sure register accesses (indirect or otherwise)
12021 * will function correctly.
12022 */
12023 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
12024 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012025
David S. Millerf49639e2006-06-09 11:58:36 -070012026 /* The memory arbiter has to be enabled in order for SRAM accesses
12027 * to succeed. Normally on powerup the tg3 chip firmware will make
12028 * sure it is enabled, but other entities such as system netboot
12029 * code might disable it.
12030 */
12031 val = tr32(MEMARB_MODE);
12032 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
12033
Matt Carlson79eb6902010-02-17 15:17:03 +000012034 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012035 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12036
Gary Zambranoa85feb82007-05-05 11:52:19 -070012037 /* Assume an onboard device and WOL capable by default. */
12038 tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT | TG3_FLAG_WOL_CAP;
David S. Miller72b845e2006-03-14 14:11:48 -080012039
Michael Chanb5d37722006-09-27 16:06:21 -070012040 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080012041 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Michael Chanb5d37722006-09-27 16:06:21 -070012042 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080012043 tp->tg3_flags2 |= TG3_FLG2_IS_NIC;
12044 }
Matt Carlson0527ba32007-10-10 18:03:30 -070012045 val = tr32(VCPU_CFGSHDW);
12046 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Matt Carlson8ed5d972007-05-07 00:25:49 -070012047 tp->tg3_flags |= TG3_FLAG_ASPM_WORKAROUND;
Matt Carlson0527ba32007-10-10 18:03:30 -070012048 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Matt Carlson20232762008-12-21 20:18:56 -080012049 (val & VCPU_CFGSHDW_WOL_MAGPKT))
Matt Carlson0527ba32007-10-10 18:03:30 -070012050 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012051 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070012052 }
12053
Linus Torvalds1da177e2005-04-16 15:20:36 -070012054 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
12055 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
12056 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070012057 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012058 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012059
12060 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
12061 tp->nic_sram_data_cfg = nic_cfg;
12062
12063 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
12064 ver >>= NIC_SRAM_DATA_VER_SHIFT;
12065 if ((GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700) &&
12066 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) &&
12067 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703) &&
12068 (ver > 0) && (ver < 0x100))
12069 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
12070
Matt Carlsona9daf362008-05-25 23:49:44 -070012071 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
12072 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
12073
Linus Torvalds1da177e2005-04-16 15:20:36 -070012074 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
12075 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
12076 eeprom_phy_serdes = 1;
12077
12078 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
12079 if (nic_phy_id != 0) {
12080 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
12081 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
12082
12083 eeprom_phy_id = (id1 >> 16) << 10;
12084 eeprom_phy_id |= (id2 & 0xfc00) << 16;
12085 eeprom_phy_id |= (id2 & 0x03ff) << 0;
12086 } else
12087 eeprom_phy_id = 0;
12088
Michael Chan7d0c41e2005-04-21 17:06:20 -070012089 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070012090 if (eeprom_phy_serdes) {
Matt Carlsona50d0792010-06-05 17:24:37 +000012091 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012092 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000012093 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012094 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070012095 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070012096
John W. Linvillecbf46852005-04-21 17:01:29 -070012097 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012098 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
12099 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070012100 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070012101 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
12102
12103 switch (led_cfg) {
12104 default:
12105 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
12106 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12107 break;
12108
12109 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
12110 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12111 break;
12112
12113 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
12114 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070012115
12116 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
12117 * read on some older 5700/5701 bootcode.
12118 */
12119 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
12120 ASIC_REV_5700 ||
12121 GET_ASIC_REV(tp->pci_chip_rev_id) ==
12122 ASIC_REV_5701)
12123 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12124
Linus Torvalds1da177e2005-04-16 15:20:36 -070012125 break;
12126
12127 case SHASTA_EXT_LED_SHARED:
12128 tp->led_ctrl = LED_CTRL_MODE_SHARED;
12129 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
12130 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
12131 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12132 LED_CTRL_MODE_PHY_2);
12133 break;
12134
12135 case SHASTA_EXT_LED_MAC:
12136 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
12137 break;
12138
12139 case SHASTA_EXT_LED_COMBO:
12140 tp->led_ctrl = LED_CTRL_MODE_COMBO;
12141 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
12142 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12143 LED_CTRL_MODE_PHY_2);
12144 break;
12145
Stephen Hemminger855e1112008-04-16 16:37:28 -070012146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012147
12148 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
12149 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
12150 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
12151 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12152
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012153 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
12154 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080012155
Michael Chan9d26e212006-12-07 00:21:14 -080012156 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012157 tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080012158 if ((tp->pdev->subsystem_vendor ==
12159 PCI_VENDOR_ID_ARIMA) &&
12160 (tp->pdev->subsystem_device == 0x205a ||
12161 tp->pdev->subsystem_device == 0x2063))
12162 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
12163 } else {
David S. Millerf49639e2006-06-09 11:58:36 -070012164 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080012165 tp->tg3_flags2 |= TG3_FLG2_IS_NIC;
12166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012167
12168 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
12169 tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
John W. Linvillecbf46852005-04-21 17:01:29 -070012170 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012171 tp->tg3_flags2 |= TG3_FLG2_ASF_NEW_HANDSHAKE;
12172 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012173
12174 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
12175 (tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
Matt Carlson0d3031d2007-10-10 18:02:43 -070012176 tp->tg3_flags3 |= TG3_FLG3_ENABLE_APE;
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012177
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012178 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070012179 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
12180 tp->tg3_flags &= ~TG3_FLAG_WOL_CAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012181
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070012182 if ((tp->tg3_flags & TG3_FLAG_WOL_CAP) &&
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012183 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE))
Matt Carlson0527ba32007-10-10 18:03:30 -070012184 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
12185
Linus Torvalds1da177e2005-04-16 15:20:36 -070012186 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012187 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012188
12189 /* serdes signal pre-emphasis in register 0x590 set by */
12190 /* bootcode if bit 18 is set */
12191 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012192 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070012193
Matt Carlson321d32a2008-11-21 17:22:19 -080012194 if (((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
12195 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080012196 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012197 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080012198
Matt Carlson8c69b1e2010-08-02 11:26:00 +000012199 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
12200 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
12201 !(tp->tg3_flags3 & TG3_FLG3_5717_PLUS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070012202 u32 cfg3;
12203
12204 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
12205 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
12206 tp->tg3_flags |= TG3_FLAG_ASPM_WORKAROUND;
12207 }
Matt Carlsona9daf362008-05-25 23:49:44 -070012208
Matt Carlson14417062010-02-17 15:16:59 +000012209 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
12210 tp->tg3_flags3 |= TG3_FLG3_RGMII_INBAND_DISABLE;
Matt Carlsona9daf362008-05-25 23:49:44 -070012211 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
12212 tp->tg3_flags3 |= TG3_FLG3_RGMII_EXT_IBND_RX_EN;
12213 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
12214 tp->tg3_flags3 |= TG3_FLG3_RGMII_EXT_IBND_TX_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012215 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012216done:
12217 device_init_wakeup(&tp->pdev->dev, tp->tg3_flags & TG3_FLAG_WOL_CAP);
12218 device_set_wakeup_enable(&tp->pdev->dev,
12219 tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
Michael Chan7d0c41e2005-04-21 17:06:20 -070012220}
12221
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012222static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
12223{
12224 int i;
12225 u32 val;
12226
12227 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
12228 tw32(OTP_CTRL, cmd);
12229
12230 /* Wait for up to 1 ms for command to execute. */
12231 for (i = 0; i < 100; i++) {
12232 val = tr32(OTP_STATUS);
12233 if (val & OTP_STATUS_CMD_DONE)
12234 break;
12235 udelay(10);
12236 }
12237
12238 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
12239}
12240
12241/* Read the gphy configuration from the OTP region of the chip. The gphy
12242 * configuration is a 32-bit value that straddles the alignment boundary.
12243 * We do two 32-bit reads and then shift and merge the results.
12244 */
12245static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
12246{
12247 u32 bhalf_otp, thalf_otp;
12248
12249 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
12250
12251 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
12252 return 0;
12253
12254 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
12255
12256 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12257 return 0;
12258
12259 thalf_otp = tr32(OTP_READ_DATA);
12260
12261 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
12262
12263 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12264 return 0;
12265
12266 bhalf_otp = tr32(OTP_READ_DATA);
12267
12268 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
12269}
12270
Michael Chan7d0c41e2005-04-21 17:06:20 -070012271static int __devinit tg3_phy_probe(struct tg3 *tp)
12272{
12273 u32 hw_phy_id_1, hw_phy_id_2;
12274 u32 hw_phy_id, hw_phy_id_masked;
12275 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012276
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012277 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)
12278 return tg3_phy_init(tp);
12279
Linus Torvalds1da177e2005-04-16 15:20:36 -070012280 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010012281 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012282 */
12283 err = 0;
Matt Carlson0d3031d2007-10-10 18:02:43 -070012284 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
12285 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000012286 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012287 } else {
12288 /* Now read the physical PHY_ID from the chip and verify
12289 * that it is sane. If it doesn't look good, we fall back
12290 * to either the hard-coded table based PHY_ID and failing
12291 * that the value found in the eeprom area.
12292 */
12293 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
12294 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
12295
12296 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
12297 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
12298 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
12299
Matt Carlson79eb6902010-02-17 15:17:03 +000012300 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012301 }
12302
Matt Carlson79eb6902010-02-17 15:17:03 +000012303 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012304 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000012305 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012306 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070012307 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012308 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012309 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000012310 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070012311 /* Do nothing, phy ID already set up in
12312 * tg3_get_eeprom_hw_cfg().
12313 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070012314 } else {
12315 struct subsys_tbl_ent *p;
12316
12317 /* No eeprom signature? Try the hardcoded
12318 * subsys device table.
12319 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012320 p = tg3_lookup_by_subsys(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012321 if (!p)
12322 return -ENODEV;
12323
12324 tp->phy_id = p->phy_id;
12325 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000012326 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012327 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012328 }
12329 }
12330
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012331 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Matt Carlson0d3031d2007-10-10 18:02:43 -070012332 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070012333 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chan3600d912006-12-07 00:21:48 -080012334 u32 bmsr, adv_reg, tg3_ctrl, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012335
12336 tg3_readphy(tp, MII_BMSR, &bmsr);
12337 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
12338 (bmsr & BMSR_LSTATUS))
12339 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012340
Linus Torvalds1da177e2005-04-16 15:20:36 -070012341 err = tg3_phy_reset(tp);
12342 if (err)
12343 return err;
12344
12345 adv_reg = (ADVERTISE_10HALF | ADVERTISE_10FULL |
12346 ADVERTISE_100HALF | ADVERTISE_100FULL |
12347 ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
12348 tg3_ctrl = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012349 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012350 tg3_ctrl = (MII_TG3_CTRL_ADV_1000_HALF |
12351 MII_TG3_CTRL_ADV_1000_FULL);
12352 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
12353 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
12354 tg3_ctrl |= (MII_TG3_CTRL_AS_MASTER |
12355 MII_TG3_CTRL_ENABLE_AS_MASTER);
12356 }
12357
Michael Chan3600d912006-12-07 00:21:48 -080012358 mask = (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
12359 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
12360 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full);
12361 if (!tg3_copper_is_advertising_all(tp, mask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012362 tg3_writephy(tp, MII_ADVERTISE, adv_reg);
12363
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012364 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012365 tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
12366
12367 tg3_writephy(tp, MII_BMCR,
12368 BMCR_ANENABLE | BMCR_ANRESTART);
12369 }
12370 tg3_phy_set_wirespeed(tp);
12371
12372 tg3_writephy(tp, MII_ADVERTISE, adv_reg);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012373 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012374 tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
12375 }
12376
12377skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000012378 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012379 err = tg3_init_5401phy_dsp(tp);
12380 if (err)
12381 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012382
Linus Torvalds1da177e2005-04-16 15:20:36 -070012383 err = tg3_init_5401phy_dsp(tp);
12384 }
12385
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012386 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012387 tp->link_config.advertising =
12388 (ADVERTISED_1000baseT_Half |
12389 ADVERTISED_1000baseT_Full |
12390 ADVERTISED_Autoneg |
12391 ADVERTISED_FIBRE);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012392 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012393 tp->link_config.advertising &=
12394 ~(ADVERTISED_1000baseT_Half |
12395 ADVERTISED_1000baseT_Full);
12396
12397 return err;
12398}
12399
Matt Carlson184b8902010-04-05 10:19:25 +000012400static void __devinit tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012401{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000012402 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000012403 unsigned int block_end, rosize, len;
Matt Carlson184b8902010-04-05 10:19:25 +000012404 int j, i = 0;
Michael Chan1b277772006-03-20 22:27:48 -080012405 u32 magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012406
Matt Carlsondf259d82009-04-20 06:57:14 +000012407 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
12408 tg3_nvram_read(tp, 0x0, &magic))
Matt Carlsona4a8bb12010-09-15 09:00:00 +000012409 goto out_no_vpd;
12410
12411 vpd_data = kmalloc(TG3_NVM_VPD_LEN, GFP_KERNEL);
12412 if (!vpd_data)
12413 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012414
Michael Chan18201802006-03-20 22:29:15 -080012415 if (magic == TG3_EEPROM_MAGIC) {
Matt Carlson141518c2009-12-03 08:36:22 +000012416 for (i = 0; i < TG3_NVM_VPD_LEN; i += 4) {
Michael Chan1b277772006-03-20 22:27:48 -080012417 u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012418
Matt Carlson6d348f22009-02-25 14:25:52 +000012419 /* The data is in little-endian format in NVRAM.
12420 * Use the big-endian read routines to preserve
12421 * the byte order as it exists in NVRAM.
12422 */
Matt Carlson141518c2009-12-03 08:36:22 +000012423 if (tg3_nvram_read_be32(tp, TG3_NVM_VPD_OFF + i, &tmp))
Michael Chan1b277772006-03-20 22:27:48 -080012424 goto out_not_found;
12425
Matt Carlson6d348f22009-02-25 14:25:52 +000012426 memcpy(&vpd_data[i], &tmp, sizeof(tmp));
Michael Chan1b277772006-03-20 22:27:48 -080012427 }
12428 } else {
Matt Carlson94c982b2009-12-03 08:36:23 +000012429 ssize_t cnt;
Matt Carlson4181b2c2010-02-26 14:04:45 +000012430 unsigned int pos = 0;
Michael Chan1b277772006-03-20 22:27:48 -080012431
Matt Carlson94c982b2009-12-03 08:36:23 +000012432 for (; pos < TG3_NVM_VPD_LEN && i < 3; i++, pos += cnt) {
12433 cnt = pci_read_vpd(tp->pdev, pos,
12434 TG3_NVM_VPD_LEN - pos,
12435 &vpd_data[pos]);
12436 if (cnt == -ETIMEDOUT || -EINTR)
12437 cnt = 0;
12438 else if (cnt < 0)
David S. Millerf49639e2006-06-09 11:58:36 -070012439 goto out_not_found;
Michael Chan1b277772006-03-20 22:27:48 -080012440 }
Matt Carlson94c982b2009-12-03 08:36:23 +000012441 if (pos != TG3_NVM_VPD_LEN)
12442 goto out_not_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012443 }
12444
Matt Carlson4181b2c2010-02-26 14:04:45 +000012445 i = pci_vpd_find_tag(vpd_data, 0, TG3_NVM_VPD_LEN,
12446 PCI_VPD_LRDT_RO_DATA);
12447 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012448 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000012449
12450 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
12451 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
12452 i += PCI_VPD_LRDT_TAG_SIZE;
12453
12454 if (block_end > TG3_NVM_VPD_LEN)
12455 goto out_not_found;
12456
Matt Carlson184b8902010-04-05 10:19:25 +000012457 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
12458 PCI_VPD_RO_KEYWORD_MFR_ID);
12459 if (j > 0) {
12460 len = pci_vpd_info_field_size(&vpd_data[j]);
12461
12462 j += PCI_VPD_INFO_FLD_HDR_SIZE;
12463 if (j + len > block_end || len != 4 ||
12464 memcmp(&vpd_data[j], "1028", 4))
12465 goto partno;
12466
12467 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
12468 PCI_VPD_RO_KEYWORD_VENDOR0);
12469 if (j < 0)
12470 goto partno;
12471
12472 len = pci_vpd_info_field_size(&vpd_data[j]);
12473
12474 j += PCI_VPD_INFO_FLD_HDR_SIZE;
12475 if (j + len > block_end)
12476 goto partno;
12477
12478 memcpy(tp->fw_ver, &vpd_data[j], len);
12479 strncat(tp->fw_ver, " bc ", TG3_NVM_VPD_LEN - len - 1);
12480 }
12481
12482partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000012483 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
12484 PCI_VPD_RO_KEYWORD_PARTNO);
12485 if (i < 0)
12486 goto out_not_found;
12487
12488 len = pci_vpd_info_field_size(&vpd_data[i]);
12489
12490 i += PCI_VPD_INFO_FLD_HDR_SIZE;
12491 if (len > TG3_BPN_SIZE ||
12492 (len + i) > TG3_NVM_VPD_LEN)
12493 goto out_not_found;
12494
12495 memcpy(tp->board_part_number, &vpd_data[i], len);
12496
Linus Torvalds1da177e2005-04-16 15:20:36 -070012497out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000012498 kfree(vpd_data);
12499 if (!tp->board_part_number[0])
12500 return;
12501
12502out_no_vpd:
Michael Chanb5d37722006-09-27 16:06:21 -070012503 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12504 strcpy(tp->board_part_number, "BCM95906");
Matt Carlsondf259d82009-04-20 06:57:14 +000012505 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12506 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
12507 strcpy(tp->board_part_number, "BCM57780");
12508 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12509 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
12510 strcpy(tp->board_part_number, "BCM57760");
12511 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12512 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
12513 strcpy(tp->board_part_number, "BCM57790");
Matt Carlson5e7ccf22009-08-25 10:08:42 +000012514 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12515 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
12516 strcpy(tp->board_part_number, "BCM57788");
Matt Carlsonb474eca2010-02-17 15:16:58 +000012517 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12518 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
12519 strcpy(tp->board_part_number, "BCM57761");
12520 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12521 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
Matt Carlsonb703df62009-12-03 08:36:21 +000012522 strcpy(tp->board_part_number, "BCM57765");
Matt Carlsonb474eca2010-02-17 15:16:58 +000012523 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12524 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
12525 strcpy(tp->board_part_number, "BCM57781");
12526 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12527 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
12528 strcpy(tp->board_part_number, "BCM57785");
12529 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12530 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
12531 strcpy(tp->board_part_number, "BCM57791");
12532 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12533 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
12534 strcpy(tp->board_part_number, "BCM57795");
Michael Chanb5d37722006-09-27 16:06:21 -070012535 else
12536 strcpy(tp->board_part_number, "none");
Linus Torvalds1da177e2005-04-16 15:20:36 -070012537}
12538
Matt Carlson9c8a6202007-10-21 16:16:08 -070012539static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
12540{
12541 u32 val;
12542
Matt Carlsone4f34112009-02-25 14:25:00 +000012543 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070012544 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000012545 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070012546 val != 0)
12547 return 0;
12548
12549 return 1;
12550}
12551
Matt Carlsonacd9c112009-02-25 14:26:33 +000012552static void __devinit tg3_read_bc_ver(struct tg3 *tp)
12553{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012554 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000012555 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012556 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000012557
12558 if (tg3_nvram_read(tp, 0xc, &offset) ||
12559 tg3_nvram_read(tp, 0x4, &start))
12560 return;
12561
12562 offset = tg3_nvram_logical_addr(tp, offset);
12563
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012564 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000012565 return;
12566
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012567 if ((val & 0xfc000000) == 0x0c000000) {
12568 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000012569 return;
12570
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012571 if (val == 0)
12572 newver = true;
12573 }
12574
Matt Carlson75f99362010-04-05 10:19:24 +000012575 dst_off = strlen(tp->fw_ver);
12576
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012577 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000012578 if (TG3_VER_SIZE - dst_off < 16 ||
12579 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012580 return;
12581
12582 offset = offset + ver_offset - start;
12583 for (i = 0; i < 16; i += 4) {
12584 __be32 v;
12585 if (tg3_nvram_read_be32(tp, offset + i, &v))
12586 return;
12587
Matt Carlson75f99362010-04-05 10:19:24 +000012588 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012589 }
12590 } else {
12591 u32 major, minor;
12592
12593 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
12594 return;
12595
12596 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
12597 TG3_NVM_BCVER_MAJSFT;
12598 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000012599 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
12600 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000012601 }
12602}
12603
Matt Carlsona6f6cb12009-02-25 14:27:43 +000012604static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
12605{
12606 u32 val, major, minor;
12607
12608 /* Use native endian representation */
12609 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
12610 return;
12611
12612 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
12613 TG3_NVM_HWSB_CFG1_MAJSFT;
12614 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
12615 TG3_NVM_HWSB_CFG1_MINSFT;
12616
12617 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
12618}
12619
Matt Carlsondfe00d72008-11-21 17:19:41 -080012620static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
12621{
12622 u32 offset, major, minor, build;
12623
Matt Carlson75f99362010-04-05 10:19:24 +000012624 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080012625
12626 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
12627 return;
12628
12629 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
12630 case TG3_EEPROM_SB_REVISION_0:
12631 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
12632 break;
12633 case TG3_EEPROM_SB_REVISION_2:
12634 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
12635 break;
12636 case TG3_EEPROM_SB_REVISION_3:
12637 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
12638 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000012639 case TG3_EEPROM_SB_REVISION_4:
12640 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
12641 break;
12642 case TG3_EEPROM_SB_REVISION_5:
12643 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
12644 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080012645 default:
12646 return;
12647 }
12648
Matt Carlsone4f34112009-02-25 14:25:00 +000012649 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080012650 return;
12651
12652 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
12653 TG3_EEPROM_SB_EDH_BLD_SHFT;
12654 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
12655 TG3_EEPROM_SB_EDH_MAJ_SHFT;
12656 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
12657
12658 if (minor > 99 || build > 26)
12659 return;
12660
Matt Carlson75f99362010-04-05 10:19:24 +000012661 offset = strlen(tp->fw_ver);
12662 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
12663 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080012664
12665 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000012666 offset = strlen(tp->fw_ver);
12667 if (offset < TG3_VER_SIZE - 1)
12668 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080012669 }
12670}
12671
Matt Carlsonacd9c112009-02-25 14:26:33 +000012672static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080012673{
12674 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000012675 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070012676
12677 for (offset = TG3_NVM_DIR_START;
12678 offset < TG3_NVM_DIR_END;
12679 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000012680 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012681 return;
12682
12683 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
12684 break;
12685 }
12686
12687 if (offset == TG3_NVM_DIR_END)
12688 return;
12689
12690 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
12691 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000012692 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012693 return;
12694
Matt Carlsone4f34112009-02-25 14:25:00 +000012695 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070012696 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000012697 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012698 return;
12699
12700 offset += val - start;
12701
Matt Carlsonacd9c112009-02-25 14:26:33 +000012702 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012703
Matt Carlsonacd9c112009-02-25 14:26:33 +000012704 tp->fw_ver[vlen++] = ',';
12705 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070012706
12707 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012708 __be32 v;
12709 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012710 return;
12711
Al Virob9fc7dc2007-12-17 22:59:57 -080012712 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012713
Matt Carlsonacd9c112009-02-25 14:26:33 +000012714 if (vlen > TG3_VER_SIZE - sizeof(v)) {
12715 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012716 break;
12717 }
12718
Matt Carlsonacd9c112009-02-25 14:26:33 +000012719 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
12720 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012721 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000012722}
12723
Matt Carlson7fd76442009-02-25 14:27:20 +000012724static void __devinit tg3_read_dash_ver(struct tg3 *tp)
12725{
12726 int vlen;
12727 u32 apedata;
Matt Carlsonecc79642010-08-02 11:26:01 +000012728 char *fwtype;
Matt Carlson7fd76442009-02-25 14:27:20 +000012729
12730 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) ||
12731 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
12732 return;
12733
12734 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
12735 if (apedata != APE_SEG_SIG_MAGIC)
12736 return;
12737
12738 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
12739 if (!(apedata & APE_FW_STATUS_READY))
12740 return;
12741
12742 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
12743
Matt Carlsondc6d0742010-09-15 08:59:55 +000012744 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI) {
12745 tp->tg3_flags3 |= TG3_FLG3_APE_HAS_NCSI;
Matt Carlsonecc79642010-08-02 11:26:01 +000012746 fwtype = "NCSI";
Matt Carlsondc6d0742010-09-15 08:59:55 +000012747 } else {
Matt Carlsonecc79642010-08-02 11:26:01 +000012748 fwtype = "DASH";
Matt Carlsondc6d0742010-09-15 08:59:55 +000012749 }
Matt Carlsonecc79642010-08-02 11:26:01 +000012750
Matt Carlson7fd76442009-02-25 14:27:20 +000012751 vlen = strlen(tp->fw_ver);
12752
Matt Carlsonecc79642010-08-02 11:26:01 +000012753 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
12754 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000012755 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
12756 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
12757 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
12758 (apedata & APE_FW_VERSION_BLDMSK));
12759}
12760
Matt Carlsonacd9c112009-02-25 14:26:33 +000012761static void __devinit tg3_read_fw_ver(struct tg3 *tp)
12762{
12763 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000012764 bool vpd_vers = false;
12765
12766 if (tp->fw_ver[0] != 0)
12767 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000012768
Matt Carlsondf259d82009-04-20 06:57:14 +000012769 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) {
Matt Carlson75f99362010-04-05 10:19:24 +000012770 strcat(tp->fw_ver, "sb");
Matt Carlsondf259d82009-04-20 06:57:14 +000012771 return;
12772 }
12773
Matt Carlsonacd9c112009-02-25 14:26:33 +000012774 if (tg3_nvram_read(tp, 0, &val))
12775 return;
12776
12777 if (val == TG3_EEPROM_MAGIC)
12778 tg3_read_bc_ver(tp);
12779 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
12780 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000012781 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
12782 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000012783 else
12784 return;
12785
12786 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
Matt Carlson75f99362010-04-05 10:19:24 +000012787 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) || vpd_vers)
12788 goto done;
Matt Carlsonacd9c112009-02-25 14:26:33 +000012789
12790 tg3_read_mgmtfw_ver(tp);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012791
Matt Carlson75f99362010-04-05 10:19:24 +000012792done:
Matt Carlson9c8a6202007-10-21 16:16:08 -070012793 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080012794}
12795
Michael Chan7544b092007-05-05 13:08:32 -070012796static struct pci_dev * __devinit tg3_find_peer(struct tg3 *);
12797
Eric Dumazet7fe876a2010-07-08 06:14:55 +000012798static void inline vlan_features_add(struct net_device *dev, unsigned long flags)
12799{
12800#if TG3_VLAN_TAG_USED
12801 dev->vlan_features |= flags;
12802#endif
12803}
12804
Linus Torvalds1da177e2005-04-16 15:20:36 -070012805static int __devinit tg3_get_invariants(struct tg3 *tp)
12806{
12807 static struct pci_device_id write_reorder_chipsets[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012808 { PCI_DEVICE(PCI_VENDOR_ID_AMD,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012809 PCI_DEVICE_ID_AMD_FE_GATE_700C) },
John W. Linvillec165b002006-07-08 13:28:53 -070012810 { PCI_DEVICE(PCI_VENDOR_ID_AMD,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012811 PCI_DEVICE_ID_AMD_8131_BRIDGE) },
Michael Chan399de502005-10-03 14:02:39 -070012812 { PCI_DEVICE(PCI_VENDOR_ID_VIA,
12813 PCI_DEVICE_ID_VIA_8385_0) },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012814 { },
12815 };
12816 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012817 u32 pci_state_reg, grc_misc_cfg;
12818 u32 val;
12819 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080012820 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012821
Linus Torvalds1da177e2005-04-16 15:20:36 -070012822 /* Force memory write invalidate off. If we leave it on,
12823 * then on 5700_BX chips we have to enable a workaround.
12824 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
12825 * to match the cacheline size. The Broadcom driver have this
12826 * workaround but turns MWI off all the times so never uses
12827 * it. This seems to suggest that the workaround is insufficient.
12828 */
12829 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
12830 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
12831 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
12832
12833 /* It is absolutely critical that TG3PCI_MISC_HOST_CTRL
12834 * has the register indirect write enable bit set before
12835 * we try to access any of the MMIO registers. It is also
12836 * critical that the PCI-X hw workaround situation is decided
12837 * before that as well.
12838 */
12839 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
12840 &misc_ctrl_reg);
12841
12842 tp->pci_chip_rev_id = (misc_ctrl_reg >>
12843 MISC_HOST_CTRL_CHIPREV_SHIFT);
Matt Carlson795d01c2007-10-07 23:28:17 -070012844 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
12845 u32 prod_id_asic_rev;
12846
Matt Carlson5001e2f2009-11-13 13:03:51 +000012847 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
12848 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
Matt Carlsona50d0792010-06-05 17:24:37 +000012849 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5724 ||
12850 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012851 pci_read_config_dword(tp->pdev,
12852 TG3PCI_GEN2_PRODID_ASICREV,
12853 &prod_id_asic_rev);
Matt Carlsonb703df62009-12-03 08:36:21 +000012854 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
12855 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
12856 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
12857 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
12858 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
12859 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
12860 pci_read_config_dword(tp->pdev,
12861 TG3PCI_GEN15_PRODID_ASICREV,
12862 &prod_id_asic_rev);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012863 else
12864 pci_read_config_dword(tp->pdev, TG3PCI_PRODID_ASICREV,
12865 &prod_id_asic_rev);
12866
Matt Carlson321d32a2008-11-21 17:22:19 -080012867 tp->pci_chip_rev_id = prod_id_asic_rev;
Matt Carlson795d01c2007-10-07 23:28:17 -070012868 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012869
Michael Chanff645be2005-04-21 17:09:53 -070012870 /* Wrong chip ID in 5752 A0. This code can be removed later
12871 * as A0 is not in production.
12872 */
12873 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
12874 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
12875
Michael Chan68929142005-08-09 20:17:14 -070012876 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
12877 * we need to disable memory and use config. cycles
12878 * only to access all registers. The 5702/03 chips
12879 * can mistakenly decode the special cycles from the
12880 * ICH chipsets as memory write cycles, causing corruption
12881 * of register and memory space. Only certain ICH bridges
12882 * will drive special cycles with non-zero data during the
12883 * address phase which can fall within the 5703's address
12884 * range. This is not an ICH bug as the PCI spec allows
12885 * non-zero address during special cycles. However, only
12886 * these ICH bridges are known to drive non-zero addresses
12887 * during special cycles.
12888 *
12889 * Since special cycles do not cross PCI bridges, we only
12890 * enable this workaround if the 5703 is on the secondary
12891 * bus of these ICH bridges.
12892 */
12893 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
12894 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
12895 static struct tg3_dev_id {
12896 u32 vendor;
12897 u32 device;
12898 u32 rev;
12899 } ich_chipsets[] = {
12900 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
12901 PCI_ANY_ID },
12902 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
12903 PCI_ANY_ID },
12904 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
12905 0xa },
12906 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
12907 PCI_ANY_ID },
12908 { },
12909 };
12910 struct tg3_dev_id *pci_id = &ich_chipsets[0];
12911 struct pci_dev *bridge = NULL;
12912
12913 while (pci_id->vendor != 0) {
12914 bridge = pci_get_device(pci_id->vendor, pci_id->device,
12915 bridge);
12916 if (!bridge) {
12917 pci_id++;
12918 continue;
12919 }
12920 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070012921 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070012922 continue;
12923 }
12924 if (bridge->subordinate &&
12925 (bridge->subordinate->number ==
12926 tp->pdev->bus->number)) {
12927
12928 tp->tg3_flags2 |= TG3_FLG2_ICH_WORKAROUND;
12929 pci_dev_put(bridge);
12930 break;
12931 }
12932 }
12933 }
12934
Matt Carlson41588ba2008-04-19 18:12:33 -070012935 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
12936 static struct tg3_dev_id {
12937 u32 vendor;
12938 u32 device;
12939 } bridge_chipsets[] = {
12940 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
12941 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
12942 { },
12943 };
12944 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
12945 struct pci_dev *bridge = NULL;
12946
12947 while (pci_id->vendor != 0) {
12948 bridge = pci_get_device(pci_id->vendor,
12949 pci_id->device,
12950 bridge);
12951 if (!bridge) {
12952 pci_id++;
12953 continue;
12954 }
12955 if (bridge->subordinate &&
12956 (bridge->subordinate->number <=
12957 tp->pdev->bus->number) &&
12958 (bridge->subordinate->subordinate >=
12959 tp->pdev->bus->number)) {
12960 tp->tg3_flags3 |= TG3_FLG3_5701_DMA_BUG;
12961 pci_dev_put(bridge);
12962 break;
12963 }
12964 }
12965 }
12966
Michael Chan4a29cc22006-03-19 13:21:12 -080012967 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
12968 * DMA addresses > 40-bit. This bridge may have other additional
12969 * 57xx devices behind it in some 4-port NIC designs for example.
12970 * Any tg3 device found behind the bridge will also need the 40-bit
12971 * DMA workaround.
12972 */
Michael Chana4e2b342005-10-26 15:46:52 -070012973 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
12974 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
12975 tp->tg3_flags2 |= TG3_FLG2_5780_CLASS;
Michael Chan4a29cc22006-03-19 13:21:12 -080012976 tp->tg3_flags |= TG3_FLAG_40BIT_DMA_BUG;
Michael Chan4cf78e42005-07-25 12:29:19 -070012977 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a588792010-04-05 10:19:28 +000012978 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080012979 struct pci_dev *bridge = NULL;
12980
12981 do {
12982 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
12983 PCI_DEVICE_ID_SERVERWORKS_EPB,
12984 bridge);
12985 if (bridge && bridge->subordinate &&
12986 (bridge->subordinate->number <=
12987 tp->pdev->bus->number) &&
12988 (bridge->subordinate->subordinate >=
12989 tp->pdev->bus->number)) {
12990 tp->tg3_flags |= TG3_FLAG_40BIT_DMA_BUG;
12991 pci_dev_put(bridge);
12992 break;
12993 }
12994 } while (bridge);
12995 }
Michael Chan4cf78e42005-07-25 12:29:19 -070012996
Linus Torvalds1da177e2005-04-16 15:20:36 -070012997 /* Initialize misc host control in PCI block. */
12998 tp->misc_host_ctrl |= (misc_ctrl_reg &
12999 MISC_HOST_CTRL_CHIPREV);
13000 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13001 tp->misc_host_ctrl);
13002
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013003 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
13004 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
13005 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Michael Chan7544b092007-05-05 13:08:32 -070013006 tp->pdev_peer = tg3_find_peer(tp);
13007
Matt Carlsonc885e822010-08-02 11:25:57 +000013008 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13009 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13010 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
13011 tp->tg3_flags3 |= TG3_FLG3_5717_PLUS;
13012
Matt Carlson321d32a2008-11-21 17:22:19 -080013013 /* Intentionally exclude ASIC_REV_5906 */
13014 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Michael Chand9ab5ad2006-03-20 22:27:35 -080013015 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013016 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013017 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013018 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013019 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlsonc885e822010-08-02 11:25:57 +000013020 (tp->tg3_flags3 & TG3_FLG3_5717_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080013021 tp->tg3_flags3 |= TG3_FLG3_5755_PLUS;
13022
13023 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13024 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Michael Chanb5d37722006-09-27 16:06:21 -070013025 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013026 (tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
Michael Chana4e2b342005-10-26 15:46:52 -070013027 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
John W. Linville6708e5c2005-04-21 17:00:52 -070013028 tp->tg3_flags2 |= TG3_FLG2_5750_PLUS;
13029
John W. Linville1b440c562005-04-21 17:03:18 -070013030 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) ||
13031 (tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
13032 tp->tg3_flags2 |= TG3_FLG2_5705_PLUS;
13033
Matt Carlson027455a2008-12-21 20:19:30 -080013034 /* 5700 B0 chips do not support checksumming correctly due
13035 * to hardware bugs.
13036 */
13037 if (tp->pci_chip_rev_id == CHIPREV_ID_5700_B0)
13038 tp->tg3_flags |= TG3_FLAG_BROKEN_CHECKSUMS;
13039 else {
Eric Dumazet7fe876a2010-07-08 06:14:55 +000013040 unsigned long features = NETIF_F_IP_CSUM | NETIF_F_SG | NETIF_F_GRO;
13041
Matt Carlson027455a2008-12-21 20:19:30 -080013042 tp->tg3_flags |= TG3_FLAG_RX_CHECKSUMS;
Matt Carlson027455a2008-12-21 20:19:30 -080013043 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Eric Dumazet7fe876a2010-07-08 06:14:55 +000013044 features |= NETIF_F_IPV6_CSUM;
13045 tp->dev->features |= features;
13046 vlan_features_add(tp->dev, features);
Matt Carlson027455a2008-12-21 20:19:30 -080013047 }
13048
Matt Carlson507399f2009-11-13 13:03:37 +000013049 /* Determine TSO capabilities */
Matt Carlsonc885e822010-08-02 11:25:57 +000013050 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)
Matt Carlsone849cdc2009-11-13 13:03:38 +000013051 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_3;
13052 else if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
13053 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlson507399f2009-11-13 13:03:37 +000013054 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_2;
13055 else if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
13056 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_1 | TG3_FLG2_TSO_BUG;
13057 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
13058 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
13059 tp->tg3_flags2 &= ~TG3_FLG2_TSO_BUG;
13060 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13061 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13062 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
13063 tp->tg3_flags2 |= TG3_FLG2_TSO_BUG;
13064 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
13065 tp->fw_needed = FIRMWARE_TG3TSO5;
13066 else
13067 tp->fw_needed = FIRMWARE_TG3TSO;
13068 }
13069
13070 tp->irq_max = 1;
13071
Michael Chan5a6f3072006-03-20 22:28:05 -080013072 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
Michael Chan7544b092007-05-05 13:08:32 -070013073 tp->tg3_flags |= TG3_FLAG_SUPPORT_MSI;
13074 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
13075 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
13076 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
13077 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
13078 tp->pdev_peer == tp->pdev))
13079 tp->tg3_flags &= ~TG3_FLAG_SUPPORT_MSI;
13080
Matt Carlson321d32a2008-11-21 17:22:19 -080013081 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070013082 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanfcfa0a32006-03-20 22:28:41 -080013083 tp->tg3_flags2 |= TG3_FLG2_1SHOT_MSI;
Michael Chan52c0fd82006-06-29 20:15:54 -070013084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013085
Matt Carlsonc885e822010-08-02 11:25:57 +000013086 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
Matt Carlson507399f2009-11-13 13:03:37 +000013087 tp->tg3_flags |= TG3_FLAG_SUPPORT_MSIX;
13088 tp->irq_max = TG3_IRQ_MAX_VECS;
13089 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013090 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000013091
Matt Carlson615774f2009-11-13 13:03:39 +000013092 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsona50d0792010-06-05 17:24:37 +000013093 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlson615774f2009-11-13 13:03:39 +000013094 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
13095 tp->tg3_flags3 |= TG3_FLG3_SHORT_DMA_BUG;
13096 else if (!(tp->tg3_flags3 & TG3_FLG3_5755_PLUS)) {
13097 tp->tg3_flags3 |= TG3_FLG3_4G_DMA_BNDRY_BUG;
13098 tp->tg3_flags3 |= TG3_FLG3_40BIT_DMA_LIMIT_BUG;
Matt Carlson0e1406d2009-11-02 12:33:33 +000013099 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013100
Matt Carlsonc885e822010-08-02 11:25:57 +000013101 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)
Matt Carlsonb703df62009-12-03 08:36:21 +000013102 tp->tg3_flags3 |= TG3_FLG3_USE_JUMBO_BDFLAG;
13103
Matt Carlsonf51f3562008-05-25 23:45:08 -070013104 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
Matt Carlsonc6cdf432010-04-05 10:19:26 +000013105 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
13106 (tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG))
Matt Carlson8f666b02009-08-28 13:58:24 +000013107 tp->tg3_flags |= TG3_FLAG_JUMBO_CAPABLE;
Michael Chan0f893dc2005-07-25 12:30:38 -070013108
Matt Carlson52f44902008-11-21 17:17:04 -080013109 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
13110 &pci_state_reg);
13111
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013112 tp->pcie_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_EXP);
13113 if (tp->pcie_cap != 0) {
13114 u16 lnkctl;
13115
Linus Torvalds1da177e2005-04-16 15:20:36 -070013116 tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS;
Matt Carlson5f5c51e2007-11-12 21:19:37 -080013117
13118 pcie_set_readrq(tp->pdev, 4096);
13119
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013120 pci_read_config_word(tp->pdev,
13121 tp->pcie_cap + PCI_EXP_LNKCTL,
13122 &lnkctl);
13123 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
13124 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanc7835a72006-11-15 21:14:42 -080013125 tp->tg3_flags2 &= ~TG3_FLG2_HW_TSO_2;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013126 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013127 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000013128 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
13129 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013130 tp->tg3_flags3 |= TG3_FLG3_CLKREQ_BUG;
Matt Carlson614b0592010-01-20 16:58:02 +000013131 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
13132 tp->tg3_flags3 |= TG3_FLG3_L1PLLPD_EN;
Michael Chanc7835a72006-11-15 21:14:42 -080013133 }
Matt Carlson52f44902008-11-21 17:17:04 -080013134 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Matt Carlsonfcb389d2008-11-03 16:55:44 -080013135 tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS;
Matt Carlson52f44902008-11-21 17:17:04 -080013136 } else if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
13137 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
13138 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
13139 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000013140 dev_err(&tp->pdev->dev,
13141 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080013142 return -EIO;
13143 }
13144
13145 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
13146 tp->tg3_flags |= TG3_FLAG_PCIX_MODE;
13147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013148
Michael Chan399de502005-10-03 14:02:39 -070013149 /* If we have an AMD 762 or VIA K8T800 chipset, write
13150 * reordering to the mailbox registers done by the host
13151 * controller can cause major troubles. We read back from
13152 * every mailbox register write to force the writes to be
13153 * posted to the chip in order.
13154 */
13155 if (pci_dev_present(write_reorder_chipsets) &&
13156 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
13157 tp->tg3_flags |= TG3_FLAG_MBOX_WRITE_REORDER;
13158
Matt Carlson69fc4052008-12-21 20:19:57 -080013159 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
13160 &tp->pci_cacheline_sz);
13161 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13162 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013163 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
13164 tp->pci_lat_timer < 64) {
13165 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080013166 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13167 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013168 }
13169
Matt Carlson52f44902008-11-21 17:17:04 -080013170 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
13171 /* 5700 BX chips need to have their TX producer index
13172 * mailboxes written twice to workaround a bug.
13173 */
13174 tp->tg3_flags |= TG3_FLAG_TXD_MBOX_HWBUG;
Matt Carlson9974a352007-10-07 23:27:28 -070013175
Matt Carlson52f44902008-11-21 17:17:04 -080013176 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013177 *
13178 * The workaround is to use indirect register accesses
13179 * for all chip writes not to mailbox registers.
13180 */
Matt Carlson52f44902008-11-21 17:17:04 -080013181 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013182 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013183
13184 tp->tg3_flags |= TG3_FLAG_PCIX_TARGET_HWBUG;
13185
13186 /* The chip can have it's power management PCI config
13187 * space registers clobbered due to this bug.
13188 * So explicitly force the chip into D0 here.
13189 */
Matt Carlson9974a352007-10-07 23:27:28 -070013190 pci_read_config_dword(tp->pdev,
13191 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013192 &pm_reg);
13193 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
13194 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070013195 pci_write_config_dword(tp->pdev,
13196 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013197 pm_reg);
13198
13199 /* Also, force SERR#/PERR# in PCI command. */
13200 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13201 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
13202 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13203 }
13204 }
13205
Linus Torvalds1da177e2005-04-16 15:20:36 -070013206 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
13207 tp->tg3_flags |= TG3_FLAG_PCI_HIGH_SPEED;
13208 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
13209 tp->tg3_flags |= TG3_FLAG_PCI_32BIT;
13210
13211 /* Chip-specific fixup from Broadcom driver */
13212 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
13213 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
13214 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
13215 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
13216 }
13217
Michael Chan1ee582d2005-08-09 20:16:46 -070013218 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070013219 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013220 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070013221 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070013222 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013223 tp->write32_tx_mbox = tg3_write32;
13224 tp->write32_rx_mbox = tg3_write32;
13225
13226 /* Various workaround register access methods */
13227 if (tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG)
13228 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013229 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
13230 ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
13231 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
13232 /*
13233 * Back to back register writes can cause problems on these
13234 * chips, the workaround is to read back all reg writes
13235 * except those to mailbox regs.
13236 *
13237 * See tg3_write_indirect_reg32().
13238 */
Michael Chan1ee582d2005-08-09 20:16:46 -070013239 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013240 }
13241
Michael Chan1ee582d2005-08-09 20:16:46 -070013242 if ((tp->tg3_flags & TG3_FLAG_TXD_MBOX_HWBUG) ||
13243 (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)) {
13244 tp->write32_tx_mbox = tg3_write32_tx_mbox;
13245 if (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)
13246 tp->write32_rx_mbox = tg3_write_flush_reg32;
13247 }
Michael Chan20094932005-08-09 20:16:32 -070013248
Michael Chan68929142005-08-09 20:17:14 -070013249 if (tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND) {
13250 tp->read32 = tg3_read_indirect_reg32;
13251 tp->write32 = tg3_write_indirect_reg32;
13252 tp->read32_mbox = tg3_read_indirect_mbox;
13253 tp->write32_mbox = tg3_write_indirect_mbox;
13254 tp->write32_tx_mbox = tg3_write_indirect_mbox;
13255 tp->write32_rx_mbox = tg3_write_indirect_mbox;
13256
13257 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070013258 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070013259
13260 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13261 pci_cmd &= ~PCI_COMMAND_MEMORY;
13262 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13263 }
Michael Chanb5d37722006-09-27 16:06:21 -070013264 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
13265 tp->read32_mbox = tg3_read32_mbox_5906;
13266 tp->write32_mbox = tg3_write32_mbox_5906;
13267 tp->write32_tx_mbox = tg3_write32_mbox_5906;
13268 tp->write32_rx_mbox = tg3_write32_mbox_5906;
13269 }
Michael Chan68929142005-08-09 20:17:14 -070013270
Michael Chanbbadf502006-04-06 21:46:34 -070013271 if (tp->write32 == tg3_write_indirect_reg32 ||
13272 ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) &&
13273 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070013274 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Michael Chanbbadf502006-04-06 21:46:34 -070013275 tp->tg3_flags |= TG3_FLAG_SRAM_USE_CONFIG;
13276
Michael Chan7d0c41e2005-04-21 17:06:20 -070013277 /* Get eeprom hw config before calling tg3_set_power_state().
Michael Chan9d26e212006-12-07 00:21:14 -080013278 * In particular, the TG3_FLG2_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070013279 * determined before calling tg3_set_power_state() so that
13280 * we know whether or not to switch out of Vaux power.
13281 * When the flag is set, it means that GPIO1 is used for eeprom
13282 * write protect and also implies that it is a LOM where GPIOs
13283 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013284 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070013285 tg3_get_eeprom_hw_cfg(tp);
13286
Matt Carlson0d3031d2007-10-10 18:02:43 -070013287 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
13288 /* Allow reads and writes to the
13289 * APE register and memory space.
13290 */
13291 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +000013292 PCISTATE_ALLOW_APE_SHMEM_WR |
13293 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070013294 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
13295 pci_state_reg);
13296 }
13297
Matt Carlson9936bcf2007-10-10 18:03:07 -070013298 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013299 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013300 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013301 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlsonc885e822010-08-02 11:25:57 +000013302 (tp->tg3_flags3 & TG3_FLG3_5717_PLUS))
Matt Carlsond30cdd22007-10-07 23:28:35 -070013303 tp->tg3_flags |= TG3_FLAG_CPMU_PRESENT;
13304
Michael Chan314fba32005-04-21 17:07:04 -070013305 /* Set up tp->grc_local_ctrl before calling tg3_set_power_state().
13306 * GPIO1 driven high will bring 5700's external PHY out of reset.
13307 * It is also used as eeprom write protect on LOMs.
13308 */
13309 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
13310 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
13311 (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT))
13312 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
13313 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070013314 /* Unused GPIO3 must be driven as output on 5752 because there
13315 * are no pull-up resistors on unused GPIO pins.
13316 */
13317 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
13318 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070013319
Matt Carlson321d32a2008-11-21 17:22:19 -080013320 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsoncb4ed1f2010-01-20 16:58:09 +000013321 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
13322 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Michael Chanaf36e6b2006-03-23 01:28:06 -080013323 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
13324
Matt Carlson8d519ab2009-04-20 06:58:01 +000013325 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
13326 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070013327 /* Turn off the debug UART. */
13328 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
13329 if (tp->tg3_flags2 & TG3_FLG2_IS_NIC)
13330 /* Keep VMain power. */
13331 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
13332 GRC_LCLCTRL_GPIO_OUTPUT0;
13333 }
13334
Linus Torvalds1da177e2005-04-16 15:20:36 -070013335 /* Force the chip into D0. */
Michael Chanbc1c7562006-03-20 17:48:03 -080013336 err = tg3_set_power_state(tp, PCI_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013337 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000013338 dev_err(&tp->pdev->dev, "Transition to D0 failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070013339 return err;
13340 }
13341
Linus Torvalds1da177e2005-04-16 15:20:36 -070013342 /* Derive initial jumbo mode from MTU assigned in
13343 * ether_setup() via the alloc_etherdev() call
13344 */
Michael Chan0f893dc2005-07-25 12:30:38 -070013345 if (tp->dev->mtu > ETH_DATA_LEN &&
Michael Chana4e2b342005-10-26 15:46:52 -070013346 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Michael Chan0f893dc2005-07-25 12:30:38 -070013347 tp->tg3_flags |= TG3_FLAG_JUMBO_RING_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013348
13349 /* Determine WakeOnLan speed to use. */
13350 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13351 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
13352 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
13353 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
13354 tp->tg3_flags &= ~(TG3_FLAG_WOL_SPEED_100MB);
13355 } else {
13356 tp->tg3_flags |= TG3_FLAG_WOL_SPEED_100MB;
13357 }
13358
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013359 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013360 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013361
Linus Torvalds1da177e2005-04-16 15:20:36 -070013362 /* A few boards don't want Ethernet@WireSpeed phy feature */
13363 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
13364 ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
13365 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070013366 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013367 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
13368 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
13369 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013370
13371 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
13372 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013373 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013374 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013375 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013376
Matt Carlson321d32a2008-11-21 17:22:19 -080013377 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013378 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080013379 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013380 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
Matt Carlsonc885e822010-08-02 11:25:57 +000013381 !(tp->tg3_flags3 & TG3_FLG3_5717_PLUS)) {
Michael Chanc424cb22006-04-29 18:56:34 -070013382 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013383 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013384 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
13385 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080013386 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
13387 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013388 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080013389 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013390 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080013391 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013392 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070013393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013394
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013395 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
13396 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
13397 tp->phy_otp = tg3_read_otp_phycfg(tp);
13398 if (tp->phy_otp == 0)
13399 tp->phy_otp = TG3_OTP_DEFAULT;
13400 }
13401
Matt Carlsonf51f3562008-05-25 23:45:08 -070013402 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT)
Matt Carlson8ef21422008-05-02 16:47:53 -070013403 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
13404 else
13405 tp->mi_mode = MAC_MI_MODE_BASE;
13406
Linus Torvalds1da177e2005-04-16 15:20:36 -070013407 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013408 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
13409 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
13410 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
13411
Matt Carlson321d32a2008-11-21 17:22:19 -080013412 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
13413 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson57e69832008-05-25 23:48:31 -070013414 tp->tg3_flags3 |= TG3_FLG3_USE_PHYLIB;
13415
Matt Carlson158d7ab2008-05-29 01:37:54 -070013416 err = tg3_mdio_init(tp);
13417 if (err)
13418 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013419
13420 /* Initialize data/descriptor byte/word swapping. */
13421 val = tr32(GRC_MODE);
13422 val &= GRC_MODE_HOST_STACKUP;
13423 tw32(GRC_MODE, val | tp->grc_mode);
13424
13425 tg3_switch_clocks(tp);
13426
13427 /* Clear this out for sanity. */
13428 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
13429
13430 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
13431 &pci_state_reg);
13432 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
13433 (tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG) == 0) {
13434 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
13435
13436 if (chiprevid == CHIPREV_ID_5701_A0 ||
13437 chiprevid == CHIPREV_ID_5701_B0 ||
13438 chiprevid == CHIPREV_ID_5701_B2 ||
13439 chiprevid == CHIPREV_ID_5701_B5) {
13440 void __iomem *sram_base;
13441
13442 /* Write some dummy words into the SRAM status block
13443 * area, see if it reads back correctly. If the return
13444 * value is bad, force enable the PCIX workaround.
13445 */
13446 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
13447
13448 writel(0x00000000, sram_base);
13449 writel(0x00000000, sram_base + 4);
13450 writel(0xffffffff, sram_base + 4);
13451 if (readl(sram_base) != 0x00000000)
13452 tp->tg3_flags |= TG3_FLAG_PCIX_TARGET_HWBUG;
13453 }
13454 }
13455
13456 udelay(50);
13457 tg3_nvram_init(tp);
13458
13459 grc_misc_cfg = tr32(GRC_MISC_CFG);
13460 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
13461
Linus Torvalds1da177e2005-04-16 15:20:36 -070013462 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
13463 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
13464 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
13465 tp->tg3_flags2 |= TG3_FLG2_IS_5788;
13466
David S. Millerfac9b832005-05-18 22:46:34 -070013467 if (!(tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
13468 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700))
13469 tp->tg3_flags |= TG3_FLAG_TAGGED_STATUS;
13470 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) {
13471 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
13472 HOSTCC_MODE_CLRTICK_TXBD);
13473
13474 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
13475 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13476 tp->misc_host_ctrl);
13477 }
13478
Matt Carlson3bda1252008-08-15 14:08:22 -070013479 /* Preserve the APE MAC_MODE bits */
13480 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
13481 tp->mac_mode = tr32(MAC_MODE) |
13482 MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
13483 else
13484 tp->mac_mode = TG3_DEF_MAC_MODE;
13485
Linus Torvalds1da177e2005-04-16 15:20:36 -070013486 /* these are limited to 10/100 only */
13487 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
13488 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
13489 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
13490 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
13491 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
13492 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
13493 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
13494 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
13495 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
Michael Chan676917d2006-12-07 00:20:22 -080013496 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
13497 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013498 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
Matt Carlsond1101142010-02-17 15:16:55 +000013499 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
13500 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013501 (tp->phy_flags & TG3_PHYFLG_IS_FET))
13502 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013503
13504 err = tg3_phy_probe(tp);
13505 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000013506 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013507 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013508 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013509 }
13510
Matt Carlson184b8902010-04-05 10:19:25 +000013511 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080013512 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013513
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013514 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
13515 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013516 } else {
13517 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013518 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013519 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013520 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013521 }
13522
13523 /* 5700 {AX,BX} chips have a broken status block link
13524 * change bit implementation, so we must use the
13525 * status register in those cases.
13526 */
13527 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
13528 tp->tg3_flags |= TG3_FLAG_USE_LINKCHG_REG;
13529 else
13530 tp->tg3_flags &= ~TG3_FLAG_USE_LINKCHG_REG;
13531
13532 /* The led_ctrl is set during tg3_phy_probe, here we might
13533 * have to force the link status polling mechanism based
13534 * upon subsystem IDs.
13535 */
13536 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070013537 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013538 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
13539 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
13540 tp->tg3_flags |= TG3_FLAG_USE_LINKCHG_REG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013541 }
13542
13543 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013544 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013545 tp->tg3_flags |= TG3_FLAG_POLL_SERDES;
13546 else
13547 tp->tg3_flags &= ~TG3_FLAG_POLL_SERDES;
13548
Matt Carlson9dc7a112010-04-12 06:58:28 +000013549 tp->rx_offset = NET_IP_ALIGN + TG3_RX_HEADROOM;
Matt Carlsond2757fc2010-04-12 06:58:27 +000013550 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013551 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsond2757fc2010-04-12 06:58:27 +000013552 (tp->tg3_flags & TG3_FLAG_PCIX_MODE) != 0) {
Matt Carlson9dc7a112010-04-12 06:58:28 +000013553 tp->rx_offset -= NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000013554#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000013555 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000013556#endif
13557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013558
Michael Chanf92905d2006-06-29 20:14:29 -070013559 tp->rx_std_max_post = TG3_RX_RING_SIZE;
13560
13561 /* Increment the rx prod index on the rx std ring by at most
13562 * 8 for these chips to workaround hw errata.
13563 */
13564 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13565 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
13566 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
13567 tp->rx_std_max_post = 8;
13568
Matt Carlson8ed5d972007-05-07 00:25:49 -070013569 if (tp->tg3_flags & TG3_FLAG_ASPM_WORKAROUND)
13570 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
13571 PCIE_PWR_MGMT_L1_THRESH_MSK;
13572
Linus Torvalds1da177e2005-04-16 15:20:36 -070013573 return err;
13574}
13575
David S. Miller49b6e95f2007-03-29 01:38:42 -070013576#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070013577static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
13578{
13579 struct net_device *dev = tp->dev;
13580 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070013581 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070013582 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070013583 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013584
David S. Miller49b6e95f2007-03-29 01:38:42 -070013585 addr = of_get_property(dp, "local-mac-address", &len);
13586 if (addr && len == 6) {
13587 memcpy(dev->dev_addr, addr, 6);
13588 memcpy(dev->perm_addr, dev->dev_addr, 6);
13589 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013590 }
13591 return -ENODEV;
13592}
13593
13594static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
13595{
13596 struct net_device *dev = tp->dev;
13597
13598 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070013599 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013600 return 0;
13601}
13602#endif
13603
13604static int __devinit tg3_get_device_address(struct tg3 *tp)
13605{
13606 struct net_device *dev = tp->dev;
13607 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080013608 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013609
David S. Miller49b6e95f2007-03-29 01:38:42 -070013610#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070013611 if (!tg3_get_macaddr_sparc(tp))
13612 return 0;
13613#endif
13614
13615 mac_offset = 0x7c;
David S. Millerf49639e2006-06-09 11:58:36 -070013616 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) ||
Michael Chana4e2b342005-10-26 15:46:52 -070013617 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013618 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
13619 mac_offset = 0xcc;
13620 if (tg3_nvram_lock(tp))
13621 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
13622 else
13623 tg3_nvram_unlock(tp);
Matt Carlsona50d0792010-06-05 17:24:37 +000013624 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13625 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) {
13626 if (PCI_FUNC(tp->pdev->devfn) & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000013627 mac_offset = 0xcc;
Matt Carlsona50d0792010-06-05 17:24:37 +000013628 if (PCI_FUNC(tp->pdev->devfn) > 1)
13629 mac_offset += 0x18c;
Matt Carlsona1b950d2009-09-01 13:20:17 +000013630 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070013631 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013632
13633 /* First try to get it from MAC address mailbox. */
13634 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
13635 if ((hi >> 16) == 0x484b) {
13636 dev->dev_addr[0] = (hi >> 8) & 0xff;
13637 dev->dev_addr[1] = (hi >> 0) & 0xff;
13638
13639 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
13640 dev->dev_addr[2] = (lo >> 24) & 0xff;
13641 dev->dev_addr[3] = (lo >> 16) & 0xff;
13642 dev->dev_addr[4] = (lo >> 8) & 0xff;
13643 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013644
Michael Chan008652b2006-03-27 23:14:53 -080013645 /* Some old bootcode may report a 0 MAC address in SRAM */
13646 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
13647 }
13648 if (!addr_ok) {
13649 /* Next, try NVRAM. */
Matt Carlsondf259d82009-04-20 06:57:14 +000013650 if (!(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) &&
13651 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000013652 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070013653 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
13654 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080013655 }
13656 /* Finally just fetch it out of the MAC control regs. */
13657 else {
13658 hi = tr32(MAC_ADDR_0_HIGH);
13659 lo = tr32(MAC_ADDR_0_LOW);
13660
13661 dev->dev_addr[5] = lo & 0xff;
13662 dev->dev_addr[4] = (lo >> 8) & 0xff;
13663 dev->dev_addr[3] = (lo >> 16) & 0xff;
13664 dev->dev_addr[2] = (lo >> 24) & 0xff;
13665 dev->dev_addr[1] = hi & 0xff;
13666 dev->dev_addr[0] = (hi >> 8) & 0xff;
13667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013668 }
13669
13670 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070013671#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070013672 if (!tg3_get_default_macaddr_sparc(tp))
13673 return 0;
13674#endif
13675 return -EINVAL;
13676 }
John W. Linville2ff43692005-09-12 14:44:20 -070013677 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013678 return 0;
13679}
13680
David S. Miller59e6b432005-05-18 22:50:10 -070013681#define BOUNDARY_SINGLE_CACHELINE 1
13682#define BOUNDARY_MULTI_CACHELINE 2
13683
13684static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
13685{
13686 int cacheline_size;
13687 u8 byte;
13688 int goal;
13689
13690 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
13691 if (byte == 0)
13692 cacheline_size = 1024;
13693 else
13694 cacheline_size = (int) byte * 4;
13695
13696 /* On 5703 and later chips, the boundary bits have no
13697 * effect.
13698 */
13699 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13700 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13701 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
13702 goto out;
13703
13704#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
13705 goal = BOUNDARY_MULTI_CACHELINE;
13706#else
13707#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
13708 goal = BOUNDARY_SINGLE_CACHELINE;
13709#else
13710 goal = 0;
13711#endif
13712#endif
13713
Matt Carlsonc885e822010-08-02 11:25:57 +000013714 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000013715 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
13716 goto out;
13717 }
13718
David S. Miller59e6b432005-05-18 22:50:10 -070013719 if (!goal)
13720 goto out;
13721
13722 /* PCI controllers on most RISC systems tend to disconnect
13723 * when a device tries to burst across a cache-line boundary.
13724 * Therefore, letting tg3 do so just wastes PCI bandwidth.
13725 *
13726 * Unfortunately, for PCI-E there are only limited
13727 * write-side controls for this, and thus for reads
13728 * we will still get the disconnects. We'll also waste
13729 * these PCI cycles for both read and write for chips
13730 * other than 5700 and 5701 which do not implement the
13731 * boundary bits.
13732 */
13733 if ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) &&
13734 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)) {
13735 switch (cacheline_size) {
13736 case 16:
13737 case 32:
13738 case 64:
13739 case 128:
13740 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13741 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
13742 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
13743 } else {
13744 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
13745 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
13746 }
13747 break;
13748
13749 case 256:
13750 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
13751 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
13752 break;
13753
13754 default:
13755 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
13756 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
13757 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070013758 }
David S. Miller59e6b432005-05-18 22:50:10 -070013759 } else if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
13760 switch (cacheline_size) {
13761 case 16:
13762 case 32:
13763 case 64:
13764 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13765 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
13766 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
13767 break;
13768 }
13769 /* fallthrough */
13770 case 128:
13771 default:
13772 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
13773 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
13774 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070013775 }
David S. Miller59e6b432005-05-18 22:50:10 -070013776 } else {
13777 switch (cacheline_size) {
13778 case 16:
13779 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13780 val |= (DMA_RWCTRL_READ_BNDRY_16 |
13781 DMA_RWCTRL_WRITE_BNDRY_16);
13782 break;
13783 }
13784 /* fallthrough */
13785 case 32:
13786 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13787 val |= (DMA_RWCTRL_READ_BNDRY_32 |
13788 DMA_RWCTRL_WRITE_BNDRY_32);
13789 break;
13790 }
13791 /* fallthrough */
13792 case 64:
13793 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13794 val |= (DMA_RWCTRL_READ_BNDRY_64 |
13795 DMA_RWCTRL_WRITE_BNDRY_64);
13796 break;
13797 }
13798 /* fallthrough */
13799 case 128:
13800 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13801 val |= (DMA_RWCTRL_READ_BNDRY_128 |
13802 DMA_RWCTRL_WRITE_BNDRY_128);
13803 break;
13804 }
13805 /* fallthrough */
13806 case 256:
13807 val |= (DMA_RWCTRL_READ_BNDRY_256 |
13808 DMA_RWCTRL_WRITE_BNDRY_256);
13809 break;
13810 case 512:
13811 val |= (DMA_RWCTRL_READ_BNDRY_512 |
13812 DMA_RWCTRL_WRITE_BNDRY_512);
13813 break;
13814 case 1024:
13815 default:
13816 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
13817 DMA_RWCTRL_WRITE_BNDRY_1024);
13818 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070013819 }
David S. Miller59e6b432005-05-18 22:50:10 -070013820 }
13821
13822out:
13823 return val;
13824}
13825
Linus Torvalds1da177e2005-04-16 15:20:36 -070013826static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
13827{
13828 struct tg3_internal_buffer_desc test_desc;
13829 u32 sram_dma_descs;
13830 int i, ret;
13831
13832 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
13833
13834 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
13835 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
13836 tw32(RDMAC_STATUS, 0);
13837 tw32(WDMAC_STATUS, 0);
13838
13839 tw32(BUFMGR_MODE, 0);
13840 tw32(FTQ_RESET, 0);
13841
13842 test_desc.addr_hi = ((u64) buf_dma) >> 32;
13843 test_desc.addr_lo = buf_dma & 0xffffffff;
13844 test_desc.nic_mbuf = 0x00002100;
13845 test_desc.len = size;
13846
13847 /*
13848 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
13849 * the *second* time the tg3 driver was getting loaded after an
13850 * initial scan.
13851 *
13852 * Broadcom tells me:
13853 * ...the DMA engine is connected to the GRC block and a DMA
13854 * reset may affect the GRC block in some unpredictable way...
13855 * The behavior of resets to individual blocks has not been tested.
13856 *
13857 * Broadcom noted the GRC reset will also reset all sub-components.
13858 */
13859 if (to_device) {
13860 test_desc.cqid_sqid = (13 << 8) | 2;
13861
13862 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
13863 udelay(40);
13864 } else {
13865 test_desc.cqid_sqid = (16 << 8) | 7;
13866
13867 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
13868 udelay(40);
13869 }
13870 test_desc.flags = 0x00000005;
13871
13872 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
13873 u32 val;
13874
13875 val = *(((u32 *)&test_desc) + i);
13876 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
13877 sram_dma_descs + (i * sizeof(u32)));
13878 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
13879 }
13880 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
13881
Matt Carlson859a588792010-04-05 10:19:28 +000013882 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013883 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a588792010-04-05 10:19:28 +000013884 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070013885 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013886
13887 ret = -ENODEV;
13888 for (i = 0; i < 40; i++) {
13889 u32 val;
13890
13891 if (to_device)
13892 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
13893 else
13894 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
13895 if ((val & 0xffff) == sram_dma_descs) {
13896 ret = 0;
13897 break;
13898 }
13899
13900 udelay(100);
13901 }
13902
13903 return ret;
13904}
13905
David S. Millerded73402005-05-23 13:59:47 -070013906#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070013907
13908static int __devinit tg3_test_dma(struct tg3 *tp)
13909{
13910 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070013911 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000013912 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013913
13914 buf = pci_alloc_consistent(tp->pdev, TEST_BUFFER_SIZE, &buf_dma);
13915 if (!buf) {
13916 ret = -ENOMEM;
13917 goto out_nofree;
13918 }
13919
13920 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
13921 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
13922
David S. Miller59e6b432005-05-18 22:50:10 -070013923 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013924
Matt Carlsonc885e822010-08-02 11:25:57 +000013925 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000013926 goto out;
13927
Linus Torvalds1da177e2005-04-16 15:20:36 -070013928 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
13929 /* DMA read watermark not used on PCIE */
13930 tp->dma_rwctrl |= 0x00180000;
13931 } else if (!(tp->tg3_flags & TG3_FLAG_PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070013932 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
13933 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013934 tp->dma_rwctrl |= 0x003f0000;
13935 else
13936 tp->dma_rwctrl |= 0x003f000f;
13937 } else {
13938 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
13939 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
13940 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080013941 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013942
Michael Chan4a29cc22006-03-19 13:21:12 -080013943 /* If the 5704 is behind the EPB bridge, we can
13944 * do the less restrictive ONE_DMA workaround for
13945 * better performance.
13946 */
13947 if ((tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG) &&
13948 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
13949 tp->dma_rwctrl |= 0x8000;
13950 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013951 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
13952
Michael Chan49afdeb2007-02-13 12:17:03 -080013953 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
13954 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070013955 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080013956 tp->dma_rwctrl |=
13957 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
13958 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
13959 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070013960 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
13961 /* 5780 always in PCIX mode */
13962 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070013963 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
13964 /* 5714 always in PCIX mode */
13965 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013966 } else {
13967 tp->dma_rwctrl |= 0x001b000f;
13968 }
13969 }
13970
13971 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
13972 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
13973 tp->dma_rwctrl &= 0xfffffff0;
13974
13975 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13976 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
13977 /* Remove this if it causes problems for some boards. */
13978 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
13979
13980 /* On 5700/5701 chips, we need to set this bit.
13981 * Otherwise the chip will issue cacheline transactions
13982 * to streamable DMA memory with not all the byte
13983 * enables turned on. This is an error on several
13984 * RISC PCI controllers, in particular sparc64.
13985 *
13986 * On 5703/5704 chips, this bit has been reassigned
13987 * a different meaning. In particular, it is used
13988 * on those chips to enable a PCI-X workaround.
13989 */
13990 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
13991 }
13992
13993 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
13994
13995#if 0
13996 /* Unneeded, already done by tg3_get_invariants. */
13997 tg3_switch_clocks(tp);
13998#endif
13999
Linus Torvalds1da177e2005-04-16 15:20:36 -070014000 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14001 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
14002 goto out;
14003
David S. Miller59e6b432005-05-18 22:50:10 -070014004 /* It is best to perform DMA test with maximum write burst size
14005 * to expose the 5700/5701 write DMA bug.
14006 */
14007 saved_dma_rwctrl = tp->dma_rwctrl;
14008 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14009 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14010
Linus Torvalds1da177e2005-04-16 15:20:36 -070014011 while (1) {
14012 u32 *p = buf, i;
14013
14014 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
14015 p[i] = i;
14016
14017 /* Send the buffer to the chip. */
14018 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
14019 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000014020 dev_err(&tp->pdev->dev,
14021 "%s: Buffer write failed. err = %d\n",
14022 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014023 break;
14024 }
14025
14026#if 0
14027 /* validate data reached card RAM correctly. */
14028 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14029 u32 val;
14030 tg3_read_mem(tp, 0x2100 + (i*4), &val);
14031 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000014032 dev_err(&tp->pdev->dev,
14033 "%s: Buffer corrupted on device! "
14034 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014035 /* ret = -ENODEV here? */
14036 }
14037 p[i] = 0;
14038 }
14039#endif
14040 /* Now read it back. */
14041 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
14042 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000014043 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
14044 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014045 break;
14046 }
14047
14048 /* Verify it. */
14049 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14050 if (p[i] == i)
14051 continue;
14052
David S. Miller59e6b432005-05-18 22:50:10 -070014053 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14054 DMA_RWCTRL_WRITE_BNDRY_16) {
14055 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014056 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
14057 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14058 break;
14059 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000014060 dev_err(&tp->pdev->dev,
14061 "%s: Buffer corrupted on read back! "
14062 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014063 ret = -ENODEV;
14064 goto out;
14065 }
14066 }
14067
14068 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
14069 /* Success. */
14070 ret = 0;
14071 break;
14072 }
14073 }
David S. Miller59e6b432005-05-18 22:50:10 -070014074 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14075 DMA_RWCTRL_WRITE_BNDRY_16) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014076 static struct pci_device_id dma_wait_state_chipsets[] = {
14077 { PCI_DEVICE(PCI_VENDOR_ID_APPLE,
14078 PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
14079 { },
14080 };
14081
David S. Miller59e6b432005-05-18 22:50:10 -070014082 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070014083 * now look for chipsets that are known to expose the
14084 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070014085 */
Michael Chan6d1cfba2005-06-08 14:13:14 -070014086 if (pci_dev_present(dma_wait_state_chipsets)) {
14087 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14088 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a588792010-04-05 10:19:28 +000014089 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014090 /* Safe to use the calculated DMA boundary. */
14091 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a588792010-04-05 10:19:28 +000014092 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070014093
David S. Miller59e6b432005-05-18 22:50:10 -070014094 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014096
14097out:
14098 pci_free_consistent(tp->pdev, TEST_BUFFER_SIZE, buf, buf_dma);
14099out_nofree:
14100 return ret;
14101}
14102
14103static void __devinit tg3_init_link_config(struct tg3 *tp)
14104{
14105 tp->link_config.advertising =
14106 (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
14107 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
14108 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full |
14109 ADVERTISED_Autoneg | ADVERTISED_MII);
14110 tp->link_config.speed = SPEED_INVALID;
14111 tp->link_config.duplex = DUPLEX_INVALID;
14112 tp->link_config.autoneg = AUTONEG_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014113 tp->link_config.active_speed = SPEED_INVALID;
14114 tp->link_config.active_duplex = DUPLEX_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014115 tp->link_config.orig_speed = SPEED_INVALID;
14116 tp->link_config.orig_duplex = DUPLEX_INVALID;
14117 tp->link_config.orig_autoneg = AUTONEG_INVALID;
14118}
14119
14120static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
14121{
Matt Carlsonc885e822010-08-02 11:25:57 +000014122 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
Matt Carlson666bc832010-01-20 16:58:03 +000014123 tp->bufmgr_config.mbuf_read_dma_low_water =
14124 DEFAULT_MB_RDMA_LOW_WATER_5705;
14125 tp->bufmgr_config.mbuf_mac_rx_low_water =
14126 DEFAULT_MB_MACRX_LOW_WATER_57765;
14127 tp->bufmgr_config.mbuf_high_water =
14128 DEFAULT_MB_HIGH_WATER_57765;
14129
14130 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14131 DEFAULT_MB_RDMA_LOW_WATER_5705;
14132 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14133 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
14134 tp->bufmgr_config.mbuf_high_water_jumbo =
14135 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
14136 } else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
Michael Chanfdfec1722005-07-25 12:31:48 -070014137 tp->bufmgr_config.mbuf_read_dma_low_water =
14138 DEFAULT_MB_RDMA_LOW_WATER_5705;
14139 tp->bufmgr_config.mbuf_mac_rx_low_water =
14140 DEFAULT_MB_MACRX_LOW_WATER_5705;
14141 tp->bufmgr_config.mbuf_high_water =
14142 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070014143 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
14144 tp->bufmgr_config.mbuf_mac_rx_low_water =
14145 DEFAULT_MB_MACRX_LOW_WATER_5906;
14146 tp->bufmgr_config.mbuf_high_water =
14147 DEFAULT_MB_HIGH_WATER_5906;
14148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014149
Michael Chanfdfec1722005-07-25 12:31:48 -070014150 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14151 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
14152 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14153 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
14154 tp->bufmgr_config.mbuf_high_water_jumbo =
14155 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
14156 } else {
14157 tp->bufmgr_config.mbuf_read_dma_low_water =
14158 DEFAULT_MB_RDMA_LOW_WATER;
14159 tp->bufmgr_config.mbuf_mac_rx_low_water =
14160 DEFAULT_MB_MACRX_LOW_WATER;
14161 tp->bufmgr_config.mbuf_high_water =
14162 DEFAULT_MB_HIGH_WATER;
14163
14164 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14165 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
14166 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14167 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
14168 tp->bufmgr_config.mbuf_high_water_jumbo =
14169 DEFAULT_MB_HIGH_WATER_JUMBO;
14170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014171
14172 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
14173 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
14174}
14175
14176static char * __devinit tg3_phy_string(struct tg3 *tp)
14177{
Matt Carlson79eb6902010-02-17 15:17:03 +000014178 switch (tp->phy_id & TG3_PHY_ID_MASK) {
14179 case TG3_PHY_ID_BCM5400: return "5400";
14180 case TG3_PHY_ID_BCM5401: return "5401";
14181 case TG3_PHY_ID_BCM5411: return "5411";
14182 case TG3_PHY_ID_BCM5701: return "5701";
14183 case TG3_PHY_ID_BCM5703: return "5703";
14184 case TG3_PHY_ID_BCM5704: return "5704";
14185 case TG3_PHY_ID_BCM5705: return "5705";
14186 case TG3_PHY_ID_BCM5750: return "5750";
14187 case TG3_PHY_ID_BCM5752: return "5752";
14188 case TG3_PHY_ID_BCM5714: return "5714";
14189 case TG3_PHY_ID_BCM5780: return "5780";
14190 case TG3_PHY_ID_BCM5755: return "5755";
14191 case TG3_PHY_ID_BCM5787: return "5787";
14192 case TG3_PHY_ID_BCM5784: return "5784";
14193 case TG3_PHY_ID_BCM5756: return "5722/5756";
14194 case TG3_PHY_ID_BCM5906: return "5906";
14195 case TG3_PHY_ID_BCM5761: return "5761";
14196 case TG3_PHY_ID_BCM5718C: return "5718C";
14197 case TG3_PHY_ID_BCM5718S: return "5718S";
14198 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000014199 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson79eb6902010-02-17 15:17:03 +000014200 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070014201 case 0: return "serdes";
14202 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070014203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014204}
14205
Michael Chanf9804dd2005-09-27 12:13:10 -070014206static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
14207{
14208 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
14209 strcpy(str, "PCI Express");
14210 return str;
14211 } else if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
14212 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
14213
14214 strcpy(str, "PCIX:");
14215
14216 if ((clock_ctrl == 7) ||
14217 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
14218 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
14219 strcat(str, "133MHz");
14220 else if (clock_ctrl == 0)
14221 strcat(str, "33MHz");
14222 else if (clock_ctrl == 2)
14223 strcat(str, "50MHz");
14224 else if (clock_ctrl == 4)
14225 strcat(str, "66MHz");
14226 else if (clock_ctrl == 6)
14227 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070014228 } else {
14229 strcpy(str, "PCI:");
14230 if (tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED)
14231 strcat(str, "66MHz");
14232 else
14233 strcat(str, "33MHz");
14234 }
14235 if (tp->tg3_flags & TG3_FLAG_PCI_32BIT)
14236 strcat(str, ":32-bit");
14237 else
14238 strcat(str, ":64-bit");
14239 return str;
14240}
14241
Michael Chan8c2dc7e2005-12-19 16:26:02 -080014242static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014243{
14244 struct pci_dev *peer;
14245 unsigned int func, devnr = tp->pdev->devfn & ~7;
14246
14247 for (func = 0; func < 8; func++) {
14248 peer = pci_get_slot(tp->pdev->bus, devnr | func);
14249 if (peer && peer != tp->pdev)
14250 break;
14251 pci_dev_put(peer);
14252 }
Michael Chan16fe9d72005-12-13 21:09:54 -080014253 /* 5704 can be configured in single-port mode, set peer to
14254 * tp->pdev in that case.
14255 */
14256 if (!peer) {
14257 peer = tp->pdev;
14258 return peer;
14259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014260
14261 /*
14262 * We don't need to keep the refcount elevated; there's no way
14263 * to remove one half of this device without removing the other
14264 */
14265 pci_dev_put(peer);
14266
14267 return peer;
14268}
14269
David S. Miller15f98502005-05-18 22:49:26 -070014270static void __devinit tg3_init_coal(struct tg3 *tp)
14271{
14272 struct ethtool_coalesce *ec = &tp->coal;
14273
14274 memset(ec, 0, sizeof(*ec));
14275 ec->cmd = ETHTOOL_GCOALESCE;
14276 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
14277 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
14278 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
14279 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
14280 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
14281 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
14282 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
14283 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
14284 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
14285
14286 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
14287 HOSTCC_MODE_CLRTICK_TXBD)) {
14288 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
14289 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
14290 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
14291 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
14292 }
Michael Chand244c892005-07-05 14:42:33 -070014293
14294 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
14295 ec->rx_coalesce_usecs_irq = 0;
14296 ec->tx_coalesce_usecs_irq = 0;
14297 ec->stats_block_coalesce_usecs = 0;
14298 }
David S. Miller15f98502005-05-18 22:49:26 -070014299}
14300
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080014301static const struct net_device_ops tg3_netdev_ops = {
14302 .ndo_open = tg3_open,
14303 .ndo_stop = tg3_close,
Stephen Hemminger00829822008-11-20 20:14:53 -080014304 .ndo_start_xmit = tg3_start_xmit,
Eric Dumazet511d2222010-07-07 20:44:24 +000014305 .ndo_get_stats64 = tg3_get_stats64,
Stephen Hemminger00829822008-11-20 20:14:53 -080014306 .ndo_validate_addr = eth_validate_addr,
14307 .ndo_set_multicast_list = tg3_set_rx_mode,
14308 .ndo_set_mac_address = tg3_set_mac_addr,
14309 .ndo_do_ioctl = tg3_ioctl,
14310 .ndo_tx_timeout = tg3_tx_timeout,
14311 .ndo_change_mtu = tg3_change_mtu,
14312#if TG3_VLAN_TAG_USED
14313 .ndo_vlan_rx_register = tg3_vlan_rx_register,
14314#endif
14315#ifdef CONFIG_NET_POLL_CONTROLLER
14316 .ndo_poll_controller = tg3_poll_controller,
14317#endif
14318};
14319
14320static const struct net_device_ops tg3_netdev_ops_dma_bug = {
14321 .ndo_open = tg3_open,
14322 .ndo_stop = tg3_close,
14323 .ndo_start_xmit = tg3_start_xmit_dma_bug,
Eric Dumazet511d2222010-07-07 20:44:24 +000014324 .ndo_get_stats64 = tg3_get_stats64,
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080014325 .ndo_validate_addr = eth_validate_addr,
14326 .ndo_set_multicast_list = tg3_set_rx_mode,
14327 .ndo_set_mac_address = tg3_set_mac_addr,
14328 .ndo_do_ioctl = tg3_ioctl,
14329 .ndo_tx_timeout = tg3_tx_timeout,
14330 .ndo_change_mtu = tg3_change_mtu,
14331#if TG3_VLAN_TAG_USED
14332 .ndo_vlan_rx_register = tg3_vlan_rx_register,
14333#endif
14334#ifdef CONFIG_NET_POLL_CONTROLLER
14335 .ndo_poll_controller = tg3_poll_controller,
14336#endif
14337};
14338
Linus Torvalds1da177e2005-04-16 15:20:36 -070014339static int __devinit tg3_init_one(struct pci_dev *pdev,
14340 const struct pci_device_id *ent)
14341{
Linus Torvalds1da177e2005-04-16 15:20:36 -070014342 struct net_device *dev;
14343 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000014344 int i, err, pm_cap;
14345 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070014346 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080014347 u64 dma_mask, persist_dma_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014348
Joe Perches05dbe002010-02-17 19:44:19 +000014349 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014350
14351 err = pci_enable_device(pdev);
14352 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014353 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014354 return err;
14355 }
14356
Linus Torvalds1da177e2005-04-16 15:20:36 -070014357 err = pci_request_regions(pdev, DRV_MODULE_NAME);
14358 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014359 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014360 goto err_out_disable_pdev;
14361 }
14362
14363 pci_set_master(pdev);
14364
14365 /* Find power-management capability. */
14366 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
14367 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000014368 dev_err(&pdev->dev,
14369 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014370 err = -EIO;
14371 goto err_out_free_res;
14372 }
14373
Matt Carlsonfe5f5782009-09-01 13:09:39 +000014374 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014375 if (!dev) {
Matt Carlson2445e462010-04-05 10:19:21 +000014376 dev_err(&pdev->dev, "Etherdev alloc failed, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014377 err = -ENOMEM;
14378 goto err_out_free_res;
14379 }
14380
Linus Torvalds1da177e2005-04-16 15:20:36 -070014381 SET_NETDEV_DEV(dev, &pdev->dev);
14382
Linus Torvalds1da177e2005-04-16 15:20:36 -070014383#if TG3_VLAN_TAG_USED
14384 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014385#endif
14386
14387 tp = netdev_priv(dev);
14388 tp->pdev = pdev;
14389 tp->dev = dev;
14390 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014391 tp->rx_mode = TG3_DEF_RX_MODE;
14392 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070014393
Linus Torvalds1da177e2005-04-16 15:20:36 -070014394 if (tg3_debug > 0)
14395 tp->msg_enable = tg3_debug;
14396 else
14397 tp->msg_enable = TG3_DEF_MSG_ENABLE;
14398
14399 /* The word/byte swap controls here control register access byte
14400 * swapping. DMA data byte swapping is controlled in the GRC_MODE
14401 * setting below.
14402 */
14403 tp->misc_host_ctrl =
14404 MISC_HOST_CTRL_MASK_PCI_INT |
14405 MISC_HOST_CTRL_WORD_SWAP |
14406 MISC_HOST_CTRL_INDIR_ACCESS |
14407 MISC_HOST_CTRL_PCISTATE_RW;
14408
14409 /* The NONFRM (non-frame) byte/word swap controls take effect
14410 * on descriptor entries, anything which isn't packet data.
14411 *
14412 * The StrongARM chips on the board (one for tx, one for rx)
14413 * are running in big-endian mode.
14414 */
14415 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
14416 GRC_MODE_WSWAP_NONFRM_DATA);
14417#ifdef __BIG_ENDIAN
14418 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
14419#endif
14420 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014421 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000014422 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014423
Matt Carlsond5fe4882008-11-21 17:20:32 -080014424 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010014425 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014426 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014427 err = -ENOMEM;
14428 goto err_out_free_dev;
14429 }
14430
14431 tg3_init_link_config(tp);
14432
Linus Torvalds1da177e2005-04-16 15:20:36 -070014433 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
14434 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014435
Linus Torvalds1da177e2005-04-16 15:20:36 -070014436 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014437 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014438 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014439
14440 err = tg3_get_invariants(tp);
14441 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014442 dev_err(&pdev->dev,
14443 "Problem fetching invariants of chip, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014444 goto err_out_iounmap;
14445 }
14446
Matt Carlson615774f2009-11-13 13:03:39 +000014447 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) &&
Matt Carlson2e9f7a72010-09-15 08:59:56 +000014448 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
Matt Carlsona50d0792010-06-05 17:24:37 +000014449 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5719)
Stephen Hemminger00829822008-11-20 20:14:53 -080014450 dev->netdev_ops = &tg3_netdev_ops;
14451 else
14452 dev->netdev_ops = &tg3_netdev_ops_dma_bug;
14453
14454
Michael Chan4a29cc22006-03-19 13:21:12 -080014455 /* The EPB bridge inside 5714, 5715, and 5780 and any
14456 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080014457 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
14458 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
14459 * do DMA address check in tg3_start_xmit().
14460 */
Michael Chan4a29cc22006-03-19 13:21:12 -080014461 if (tp->tg3_flags2 & TG3_FLG2_IS_5788)
Yang Hongyang284901a2009-04-06 19:01:15 -070014462 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Michael Chan4a29cc22006-03-19 13:21:12 -080014463 else if (tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070014464 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080014465#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070014466 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080014467#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080014468 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070014469 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080014470
14471 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070014472 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080014473 err = pci_set_dma_mask(pdev, dma_mask);
14474 if (!err) {
14475 dev->features |= NETIF_F_HIGHDMA;
14476 err = pci_set_consistent_dma_mask(pdev,
14477 persist_dma_mask);
14478 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014479 dev_err(&pdev->dev, "Unable to obtain 64 bit "
14480 "DMA for consistent allocations\n");
Michael Chan72f2afb2006-03-06 19:28:35 -080014481 goto err_out_iounmap;
14482 }
14483 }
14484 }
Yang Hongyang284901a2009-04-06 19:01:15 -070014485 if (err || dma_mask == DMA_BIT_MASK(32)) {
14486 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080014487 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014488 dev_err(&pdev->dev,
14489 "No usable DMA configuration, aborting\n");
Michael Chan72f2afb2006-03-06 19:28:35 -080014490 goto err_out_iounmap;
14491 }
14492 }
14493
Michael Chanfdfec1722005-07-25 12:31:48 -070014494 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014495
Matt Carlson507399f2009-11-13 13:03:37 +000014496 /* Selectively allow TSO based on operating conditions */
14497 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO) ||
14498 (tp->fw_needed && !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)))
14499 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
14500 else {
14501 tp->tg3_flags2 &= ~(TG3_FLG2_TSO_CAPABLE | TG3_FLG2_TSO_BUG);
14502 tp->fw_needed = NULL;
14503 }
14504
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014505 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
Matt Carlson9e9fd122009-01-19 16:57:45 -080014506 tp->fw_needed = FIRMWARE_TG3;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014507
Michael Chan4e3a7aa2006-03-20 17:47:44 -080014508 /* TSO is on by default on chips that support hardware TSO.
14509 * Firmware TSO on older chips gives lower performance, so it
14510 * is off by default, but can be enabled using ethtool.
14511 */
Matt Carlsone849cdc2009-11-13 13:03:38 +000014512 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO) &&
Eric Dumazet7fe876a2010-07-08 06:14:55 +000014513 (dev->features & NETIF_F_IP_CSUM)) {
Matt Carlsone849cdc2009-11-13 13:03:38 +000014514 dev->features |= NETIF_F_TSO;
Eric Dumazet7fe876a2010-07-08 06:14:55 +000014515 vlan_features_add(dev, NETIF_F_TSO);
14516 }
Matt Carlsone849cdc2009-11-13 13:03:38 +000014517 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_2) ||
14518 (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3)) {
Eric Dumazet7fe876a2010-07-08 06:14:55 +000014519 if (dev->features & NETIF_F_IPV6_CSUM) {
Michael Chanb0026622006-07-03 19:42:14 -070014520 dev->features |= NETIF_F_TSO6;
Eric Dumazet7fe876a2010-07-08 06:14:55 +000014521 vlan_features_add(dev, NETIF_F_TSO6);
14522 }
Matt Carlsone849cdc2009-11-13 13:03:38 +000014523 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
14524 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070014525 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
14526 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014527 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Eric Dumazet7fe876a2010-07-08 06:14:55 +000014528 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070014529 dev->features |= NETIF_F_TSO_ECN;
Eric Dumazet7fe876a2010-07-08 06:14:55 +000014530 vlan_features_add(dev, NETIF_F_TSO_ECN);
14531 }
Michael Chanb0026622006-07-03 19:42:14 -070014532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014533
Linus Torvalds1da177e2005-04-16 15:20:36 -070014534 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
14535 !(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) &&
14536 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
14537 tp->tg3_flags2 |= TG3_FLG2_MAX_RXPEND_64;
14538 tp->rx_pending = 63;
14539 }
14540
Linus Torvalds1da177e2005-04-16 15:20:36 -070014541 err = tg3_get_device_address(tp);
14542 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014543 dev_err(&pdev->dev,
14544 "Could not obtain valid ethernet address, aborting\n");
Matt Carlson026a6c22009-12-03 08:36:24 +000014545 goto err_out_iounmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014546 }
14547
Matt Carlson0d3031d2007-10-10 18:02:43 -070014548 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
Matt Carlson63532392008-11-03 16:49:57 -080014549 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
Al Viro79ea13c2008-01-24 02:06:46 -080014550 if (!tp->aperegs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014551 dev_err(&pdev->dev,
14552 "Cannot map APE registers, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070014553 err = -ENOMEM;
Matt Carlson026a6c22009-12-03 08:36:24 +000014554 goto err_out_iounmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070014555 }
14556
14557 tg3_ape_lock_init(tp);
Matt Carlson7fd76442009-02-25 14:27:20 +000014558
14559 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF)
14560 tg3_read_dash_ver(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070014561 }
14562
Matt Carlsonc88864d2007-11-12 21:07:01 -080014563 /*
14564 * Reset chip in case UNDI or EFI driver did not shutdown
14565 * DMA self test will enable WDMAC and we'll see (spurious)
14566 * pending DMA on the PCI bus at that point.
14567 */
14568 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
14569 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
14570 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
14571 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
14572 }
14573
14574 err = tg3_test_dma(tp);
14575 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014576 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080014577 goto err_out_apeunmap;
14578 }
14579
Matt Carlsonc88864d2007-11-12 21:07:01 -080014580 /* flow control autonegotiation is default behavior */
14581 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
Steve Glendinninge18ce342008-12-16 02:00:00 -080014582 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlsonc88864d2007-11-12 21:07:01 -080014583
Matt Carlson78f90dc2009-11-13 13:03:42 +000014584 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
14585 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
14586 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000014587 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000014588 struct tg3_napi *tnapi = &tp->napi[i];
14589
14590 tnapi->tp = tp;
14591 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
14592
14593 tnapi->int_mbox = intmbx;
14594 if (i < 4)
14595 intmbx += 0x8;
14596 else
14597 intmbx += 0x4;
14598
14599 tnapi->consmbox = rcvmbx;
14600 tnapi->prodmbox = sndmbx;
14601
14602 if (i) {
14603 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
14604 netif_napi_add(dev, &tnapi->napi, tg3_poll_msix, 64);
14605 } else {
14606 tnapi->coal_now = HOSTCC_MODE_NOW;
14607 netif_napi_add(dev, &tnapi->napi, tg3_poll, 64);
14608 }
14609
14610 if (!(tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX))
14611 break;
14612
14613 /*
14614 * If we support MSIX, we'll be using RSS. If we're using
14615 * RSS, the first vector only handles link interrupts and the
14616 * remaining vectors handle rx and tx interrupts. Reuse the
14617 * mailbox values for the next iteration. The values we setup
14618 * above are still useful for the single vectored mode.
14619 */
14620 if (!i)
14621 continue;
14622
14623 rcvmbx += 0x8;
14624
14625 if (sndmbx & 0x4)
14626 sndmbx -= 0x4;
14627 else
14628 sndmbx += 0xc;
14629 }
14630
Matt Carlsonc88864d2007-11-12 21:07:01 -080014631 tg3_init_coal(tp);
14632
Michael Chanc49a1562006-12-17 17:07:29 -080014633 pci_set_drvdata(pdev, dev);
14634
Linus Torvalds1da177e2005-04-16 15:20:36 -070014635 err = register_netdev(dev);
14636 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014637 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070014638 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014639 }
14640
Joe Perches05dbe002010-02-17 19:44:19 +000014641 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
14642 tp->board_part_number,
14643 tp->pci_chip_rev_id,
14644 tg3_bus_string(tp, str),
14645 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014646
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014647 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000014648 struct phy_device *phydev;
14649 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000014650 netdev_info(dev,
14651 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000014652 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014653 } else {
14654 char *ethtype;
14655
14656 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
14657 ethtype = "10/100Base-TX";
14658 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
14659 ethtype = "1000Base-SX";
14660 else
14661 ethtype = "10/100/1000Base-T";
14662
Matt Carlson5129c3a2010-04-05 10:19:23 +000014663 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014664 "(WireSpeed[%d])\n", tg3_phy_string(tp), ethtype,
14665 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0);
14666 }
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,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014671 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches05dbe002010-02-17 19:44:19 +000014672 (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);