blob: 5d155c50e6e3afa4e435ee6fac0de64e5c0f9a67 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * tg3.c: Broadcom Tigon3 ethernet driver.
3 *
4 * Copyright (C) 2001, 2002, 2003, 2004 David S. Miller (davem@redhat.com)
5 * Copyright (C) 2001, 2002, 2003 Jeff Garzik (jgarzik@pobox.com)
6 * Copyright (C) 2004 Sun Microsystems Inc.
Matt Carlsonba5b0bf2010-01-12 10:11:40 +00007 * Copyright (C) 2005-2010 Broadcom Corporation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * Firmware is:
Michael Chan49cabf42005-06-06 15:15:17 -070010 * Derived from proprietary unpublished source code,
11 * Copyright (C) 2000-2003 Broadcom Corporation.
12 *
13 * Permission is hereby granted for the distribution of this firmware
14 * data in hexadecimal or equivalent format, provided this copyright
15 * notice is accompanying it.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 */
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#include <linux/module.h>
20#include <linux/moduleparam.h>
Matt Carlson6867c842010-07-11 09:31:44 +000021#include <linux/stringify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/kernel.h>
23#include <linux/types.h>
24#include <linux/compiler.h>
25#include <linux/slab.h>
26#include <linux/delay.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020027#include <linux/in.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/init.h>
29#include <linux/ioport.h>
30#include <linux/pci.h>
31#include <linux/netdevice.h>
32#include <linux/etherdevice.h>
33#include <linux/skbuff.h>
34#include <linux/ethtool.h>
35#include <linux/mii.h>
Matt Carlson158d7ab2008-05-29 01:37:54 -070036#include <linux/phy.h>
Matt Carlsona9daf362008-05-25 23:49:44 -070037#include <linux/brcmphy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/if_vlan.h>
39#include <linux/ip.h>
40#include <linux/tcp.h>
41#include <linux/workqueue.h>
Michael Chan61487482005-09-05 17:53:19 -070042#include <linux/prefetch.h>
Tobias Klauserf9a5f7d2005-10-29 15:09:26 +020043#include <linux/dma-mapping.h>
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080044#include <linux/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#include <net/checksum.h>
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030047#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49#include <asm/system.h>
50#include <asm/io.h>
51#include <asm/byteorder.h>
52#include <asm/uaccess.h>
53
David S. Miller49b6e95f2007-03-29 01:38:42 -070054#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <asm/idprom.h>
David S. Miller49b6e95f2007-03-29 01:38:42 -070056#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#endif
58
Matt Carlson63532392008-11-03 16:49:57 -080059#define BAR_0 0
60#define BAR_2 2
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
63#define TG3_VLAN_TAG_USED 1
64#else
65#define TG3_VLAN_TAG_USED 0
66#endif
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include "tg3.h"
69
70#define DRV_MODULE_NAME "tg3"
Matt Carlson6867c842010-07-11 09:31:44 +000071#define TG3_MAJ_NUM 3
Matt Carlson98e32a92010-07-11 09:31:47 +000072#define TG3_MIN_NUM 112
Matt Carlson6867c842010-07-11 09:31:44 +000073#define DRV_MODULE_VERSION \
74 __stringify(TG3_MAJ_NUM) "." __stringify(TG3_MIN_NUM)
Matt Carlson98e32a92010-07-11 09:31:47 +000075#define DRV_MODULE_RELDATE "July 11, 2010"
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77#define TG3_DEF_MAC_MODE 0
78#define TG3_DEF_RX_MODE 0
79#define TG3_DEF_TX_MODE 0
80#define TG3_DEF_MSG_ENABLE \
81 (NETIF_MSG_DRV | \
82 NETIF_MSG_PROBE | \
83 NETIF_MSG_LINK | \
84 NETIF_MSG_TIMER | \
85 NETIF_MSG_IFDOWN | \
86 NETIF_MSG_IFUP | \
87 NETIF_MSG_RX_ERR | \
88 NETIF_MSG_TX_ERR)
89
90/* length of time before we decide the hardware is borked,
91 * and dev->tx_timeout() should be called to fix the problem
92 */
93#define TG3_TX_TIMEOUT (5 * HZ)
94
95/* hardware minimum and maximum for a single frame's data payload */
96#define TG3_MIN_MTU 60
97#define TG3_MAX_MTU(tp) \
Matt Carlson8f666b02009-08-28 13:58:24 +000098 ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) ? 9000 : 1500)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100/* These numbers seem to be hard coded in the NIC firmware somehow.
101 * You can't change the ring sizes, but you can change where you place
102 * them in the NIC onboard memory.
103 */
104#define TG3_RX_RING_SIZE 512
105#define TG3_DEF_RX_RING_PENDING 200
106#define TG3_RX_JUMBO_RING_SIZE 256
107#define TG3_DEF_RX_JUMBO_RING_PENDING 100
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000108#define TG3_RSS_INDIR_TBL_SIZE 128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110/* Do not place this n-ring entries value into the tp struct itself,
111 * we really want to expose these constants to GCC so that modulo et
112 * al. operations are done with shifts and masks instead of with
113 * hw multiply/modulo instructions. Another solution would be to
114 * replace things like '% foo' with '& (foo - 1)'.
115 */
116#define TG3_RX_RCB_RING_SIZE(tp) \
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000117 (((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) && \
Matt Carlson5ea1c502009-09-11 16:50:16 -0700118 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) ? 1024 : 512)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120#define TG3_TX_RING_SIZE 512
121#define TG3_DEF_TX_RING_PENDING (TG3_TX_RING_SIZE - 1)
122
123#define TG3_RX_RING_BYTES (sizeof(struct tg3_rx_buffer_desc) * \
124 TG3_RX_RING_SIZE)
Matt Carlson79ed5ac2009-08-28 14:00:55 +0000125#define TG3_RX_JUMBO_RING_BYTES (sizeof(struct tg3_ext_rx_buffer_desc) * \
126 TG3_RX_JUMBO_RING_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127#define TG3_RX_RCB_RING_BYTES(tp) (sizeof(struct tg3_rx_buffer_desc) * \
Matt Carlson79ed5ac2009-08-28 14:00:55 +0000128 TG3_RX_RCB_RING_SIZE(tp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129#define TG3_TX_RING_BYTES (sizeof(struct tg3_tx_buffer_desc) * \
130 TG3_TX_RING_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131#define NEXT_TX(N) (((N) + 1) & (TG3_TX_RING_SIZE - 1))
132
Matt Carlson9dc7a112010-04-12 06:58:28 +0000133#define TG3_RX_DMA_ALIGN 16
134#define TG3_RX_HEADROOM ALIGN(VLAN_HLEN, TG3_RX_DMA_ALIGN)
135
Matt Carlson287be122009-08-28 13:58:46 +0000136#define TG3_DMA_BYTE_ENAB 64
137
138#define TG3_RX_STD_DMA_SZ 1536
139#define TG3_RX_JMB_DMA_SZ 9046
140
141#define TG3_RX_DMA_TO_MAP_SZ(x) ((x) + TG3_DMA_BYTE_ENAB)
142
143#define TG3_RX_STD_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_STD_DMA_SZ)
144#define TG3_RX_JMB_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_JMB_DMA_SZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000146#define TG3_RX_STD_BUFF_RING_SIZE \
147 (sizeof(struct ring_info) * TG3_RX_RING_SIZE)
148
149#define TG3_RX_JMB_BUFF_RING_SIZE \
150 (sizeof(struct ring_info) * TG3_RX_JUMBO_RING_SIZE)
151
Matt Carlsond2757fc2010-04-12 06:58:27 +0000152/* Due to a hardware bug, the 5701 can only DMA to memory addresses
153 * that are at least dword aligned when used in PCIX mode. The driver
154 * works around this bug by double copying the packet. This workaround
155 * is built into the normal double copy length check for efficiency.
156 *
157 * However, the double copy is only necessary on those architectures
158 * where unaligned memory accesses are inefficient. For those architectures
159 * where unaligned memory accesses incur little penalty, we can reintegrate
160 * the 5701 in the normal rx path. Doing so saves a device structure
161 * dereference by hardcoding the double copy threshold in place.
162 */
163#define TG3_RX_COPY_THRESHOLD 256
164#if NET_IP_ALIGN == 0 || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
165 #define TG3_RX_COPY_THRESH(tp) TG3_RX_COPY_THRESHOLD
166#else
167 #define TG3_RX_COPY_THRESH(tp) ((tp)->rx_copy_thresh)
168#endif
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170/* minimum number of free TX descriptors required to wake up TX process */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000171#define TG3_TX_WAKEUP_THRESH(tnapi) ((tnapi)->tx_pending / 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Matt Carlsonad829262008-11-21 17:16:16 -0800173#define TG3_RAW_IP_ALIGN 2
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175/* number of ETHTOOL_GSTATS u64's */
176#define TG3_NUM_STATS (sizeof(struct tg3_ethtool_stats)/sizeof(u64))
177
Michael Chan4cafd3f2005-05-29 14:56:34 -0700178#define TG3_NUM_TEST 6
179
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000180#define TG3_FW_UPDATE_TIMEOUT_SEC 5
181
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800182#define FIRMWARE_TG3 "tigon/tg3.bin"
183#define FIRMWARE_TG3TSO "tigon/tg3_tso.bin"
184#define FIRMWARE_TG3TSO5 "tigon/tg3_tso5.bin"
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186static char version[] __devinitdata =
Joe Perches05dbe002010-02-17 19:44:19 +0000187 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189MODULE_AUTHOR("David S. Miller (davem@redhat.com) and Jeff Garzik (jgarzik@pobox.com)");
190MODULE_DESCRIPTION("Broadcom Tigon3 ethernet driver");
191MODULE_LICENSE("GPL");
192MODULE_VERSION(DRV_MODULE_VERSION);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800193MODULE_FIRMWARE(FIRMWARE_TG3);
194MODULE_FIRMWARE(FIRMWARE_TG3TSO);
195MODULE_FIRMWARE(FIRMWARE_TG3TSO5);
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197static int tg3_debug = -1; /* -1 == use TG3_DEF_MSG_ENABLE as value */
198module_param(tg3_debug, int, 0);
199MODULE_PARM_DESC(tg3_debug, "Tigon3 bitmapped debugging message enable value");
200
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000201static DEFINE_PCI_DEVICE_TABLE(tg3_pci_tbl) = {
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700202 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5700)},
203 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5701)},
204 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702)},
205 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703)},
206 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704)},
207 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702FE)},
208 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705)},
209 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705_2)},
210 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M)},
211 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M_2)},
212 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702X)},
213 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703X)},
214 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S)},
215 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702A3)},
216 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703A3)},
217 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5782)},
218 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5788)},
219 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5789)},
220 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901)},
221 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901_2)},
222 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S_2)},
223 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705F)},
224 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5720)},
225 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5721)},
Michael Chan126a3362006-09-27 16:03:07 -0700226 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5722)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700227 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5750)},
228 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751)},
229 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5750M)},
230 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751M)},
231 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751F)},
232 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752)},
233 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752M)},
234 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753)},
235 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753M)},
236 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753F)},
237 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754)},
238 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754M)},
239 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755)},
240 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755M)},
Michael Chan126a3362006-09-27 16:03:07 -0700241 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5756)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700242 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5786)},
243 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787)},
244 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787M)},
Michael Chan676917d2006-12-07 00:20:22 -0800245 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787F)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700246 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714)},
247 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714S)},
248 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715)},
249 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715S)},
250 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780)},
251 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780S)},
252 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5781)},
Michael Chanb5d37722006-09-27 16:06:21 -0700253 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906)},
254 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906M)},
Matt Carlsond30cdd22007-10-07 23:28:35 -0700255 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5784)},
256 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5764)},
Matt Carlson6c7af272007-10-21 16:12:02 -0700257 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5723)},
Matt Carlson9936bcf2007-10-10 18:03:07 -0700258 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761)},
259 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761E)},
Matt Carlsonc88e6682008-11-03 16:49:18 -0800260 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761S)},
261 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761SE)},
Matt Carlson2befdce2009-08-28 12:28:45 +0000262 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_G)},
263 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_F)},
Matt Carlson321d32a2008-11-21 17:22:19 -0800264 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780)},
265 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57760)},
266 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57790)},
Matt Carlson5e7ccf22009-08-25 10:08:42 +0000267 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57788)},
Matt Carlson5001e2f2009-11-13 13:03:51 +0000268 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717)},
269 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5718)},
270 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5724)},
Matt Carlsonb0f75222010-01-20 16:58:11 +0000271 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57781)},
272 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57785)},
273 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57761)},
274 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57765)},
275 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57791)},
276 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57795)},
Matt Carlson302b5002010-06-05 17:24:38 +0000277 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5719)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700278 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9DXX)},
279 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9MXX)},
280 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1000)},
281 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1001)},
282 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1003)},
283 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC9100)},
284 {PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_TIGON3)},
285 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286};
287
288MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
289
Andreas Mohr50da8592006-08-14 23:54:30 -0700290static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 const char string[ETH_GSTRING_LEN];
292} ethtool_stats_keys[TG3_NUM_STATS] = {
293 { "rx_octets" },
294 { "rx_fragments" },
295 { "rx_ucast_packets" },
296 { "rx_mcast_packets" },
297 { "rx_bcast_packets" },
298 { "rx_fcs_errors" },
299 { "rx_align_errors" },
300 { "rx_xon_pause_rcvd" },
301 { "rx_xoff_pause_rcvd" },
302 { "rx_mac_ctrl_rcvd" },
303 { "rx_xoff_entered" },
304 { "rx_frame_too_long_errors" },
305 { "rx_jabbers" },
306 { "rx_undersize_packets" },
307 { "rx_in_length_errors" },
308 { "rx_out_length_errors" },
309 { "rx_64_or_less_octet_packets" },
310 { "rx_65_to_127_octet_packets" },
311 { "rx_128_to_255_octet_packets" },
312 { "rx_256_to_511_octet_packets" },
313 { "rx_512_to_1023_octet_packets" },
314 { "rx_1024_to_1522_octet_packets" },
315 { "rx_1523_to_2047_octet_packets" },
316 { "rx_2048_to_4095_octet_packets" },
317 { "rx_4096_to_8191_octet_packets" },
318 { "rx_8192_to_9022_octet_packets" },
319
320 { "tx_octets" },
321 { "tx_collisions" },
322
323 { "tx_xon_sent" },
324 { "tx_xoff_sent" },
325 { "tx_flow_control" },
326 { "tx_mac_errors" },
327 { "tx_single_collisions" },
328 { "tx_mult_collisions" },
329 { "tx_deferred" },
330 { "tx_excessive_collisions" },
331 { "tx_late_collisions" },
332 { "tx_collide_2times" },
333 { "tx_collide_3times" },
334 { "tx_collide_4times" },
335 { "tx_collide_5times" },
336 { "tx_collide_6times" },
337 { "tx_collide_7times" },
338 { "tx_collide_8times" },
339 { "tx_collide_9times" },
340 { "tx_collide_10times" },
341 { "tx_collide_11times" },
342 { "tx_collide_12times" },
343 { "tx_collide_13times" },
344 { "tx_collide_14times" },
345 { "tx_collide_15times" },
346 { "tx_ucast_packets" },
347 { "tx_mcast_packets" },
348 { "tx_bcast_packets" },
349 { "tx_carrier_sense_errors" },
350 { "tx_discards" },
351 { "tx_errors" },
352
353 { "dma_writeq_full" },
354 { "dma_write_prioq_full" },
355 { "rxbds_empty" },
356 { "rx_discards" },
357 { "rx_errors" },
358 { "rx_threshold_hit" },
359
360 { "dma_readq_full" },
361 { "dma_read_prioq_full" },
362 { "tx_comp_queue_full" },
363
364 { "ring_set_send_prod_index" },
365 { "ring_status_update" },
366 { "nic_irqs" },
367 { "nic_avoided_irqs" },
368 { "nic_tx_threshold_hit" }
369};
370
Andreas Mohr50da8592006-08-14 23:54:30 -0700371static const struct {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700372 const char string[ETH_GSTRING_LEN];
373} ethtool_test_keys[TG3_NUM_TEST] = {
374 { "nvram test (online) " },
375 { "link test (online) " },
376 { "register test (offline)" },
377 { "memory test (offline)" },
378 { "loopback test (offline)" },
379 { "interrupt test (offline)" },
380};
381
Michael Chanb401e9e2005-12-19 16:27:04 -0800382static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
383{
384 writel(val, tp->regs + off);
385}
386
387static u32 tg3_read32(struct tg3 *tp, u32 off)
388{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000389 return readl(tp->regs + off);
Michael Chanb401e9e2005-12-19 16:27:04 -0800390}
391
Matt Carlson0d3031d2007-10-10 18:02:43 -0700392static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
393{
394 writel(val, tp->aperegs + off);
395}
396
397static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
398{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000399 return readl(tp->aperegs + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700400}
401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
403{
Michael Chan68929142005-08-09 20:17:14 -0700404 unsigned long flags;
405
406 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700407 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
408 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
Michael Chan68929142005-08-09 20:17:14 -0700409 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700410}
411
412static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
413{
414 writel(val, tp->regs + off);
415 readl(tp->regs + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416}
417
Michael Chan68929142005-08-09 20:17:14 -0700418static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
419{
420 unsigned long flags;
421 u32 val;
422
423 spin_lock_irqsave(&tp->indirect_lock, flags);
424 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
425 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
426 spin_unlock_irqrestore(&tp->indirect_lock, flags);
427 return val;
428}
429
430static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
431{
432 unsigned long flags;
433
434 if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
435 pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
436 TG3_64BIT_REG_LOW, val);
437 return;
438 }
Matt Carlson66711e62009-11-13 13:03:49 +0000439 if (off == TG3_RX_STD_PROD_IDX_REG) {
Michael Chan68929142005-08-09 20:17:14 -0700440 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
441 TG3_64BIT_REG_LOW, val);
442 return;
443 }
444
445 spin_lock_irqsave(&tp->indirect_lock, flags);
446 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
447 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
448 spin_unlock_irqrestore(&tp->indirect_lock, flags);
449
450 /* In indirect mode when disabling interrupts, we also need
451 * to clear the interrupt bit in the GRC local ctrl register.
452 */
453 if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
454 (val == 0x1)) {
455 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
456 tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
457 }
458}
459
460static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
461{
462 unsigned long flags;
463 u32 val;
464
465 spin_lock_irqsave(&tp->indirect_lock, flags);
466 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
467 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
468 spin_unlock_irqrestore(&tp->indirect_lock, flags);
469 return val;
470}
471
Michael Chanb401e9e2005-12-19 16:27:04 -0800472/* usec_wait specifies the wait time in usec when writing to certain registers
473 * where it is unsafe to read back the register without some delay.
474 * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
475 * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
476 */
477static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
Michael Chanb401e9e2005-12-19 16:27:04 -0800479 if ((tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG) ||
480 (tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND))
481 /* Non-posted methods */
482 tp->write32(tp, off, val);
483 else {
484 /* Posted method */
485 tg3_write32(tp, off, val);
486 if (usec_wait)
487 udelay(usec_wait);
488 tp->read32(tp, off);
489 }
490 /* Wait again after the read for the posted method to guarantee that
491 * the wait time is met.
492 */
493 if (usec_wait)
494 udelay(usec_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495}
496
Michael Chan09ee9292005-08-09 20:17:00 -0700497static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
498{
499 tp->write32_mbox(tp, off, val);
Michael Chan68929142005-08-09 20:17:14 -0700500 if (!(tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER) &&
501 !(tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND))
502 tp->read32_mbox(tp, off);
Michael Chan09ee9292005-08-09 20:17:00 -0700503}
504
Michael Chan20094932005-08-09 20:16:32 -0700505static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
507 void __iomem *mbox = tp->regs + off;
508 writel(val, mbox);
509 if (tp->tg3_flags & TG3_FLAG_TXD_MBOX_HWBUG)
510 writel(val, mbox);
511 if (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)
512 readl(mbox);
513}
514
Michael Chanb5d37722006-09-27 16:06:21 -0700515static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
516{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000517 return readl(tp->regs + off + GRCMBOX_BASE);
Michael Chanb5d37722006-09-27 16:06:21 -0700518}
519
520static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
521{
522 writel(val, tp->regs + off + GRCMBOX_BASE);
523}
524
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000525#define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700526#define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000527#define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
528#define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
529#define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
Michael Chan20094932005-08-09 20:16:32 -0700530
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000531#define tw32(reg, val) tp->write32(tp, reg, val)
532#define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
533#define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
534#define tr32(reg) tp->read32(tp, reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
537{
Michael Chan68929142005-08-09 20:17:14 -0700538 unsigned long flags;
539
Michael Chanb5d37722006-09-27 16:06:21 -0700540 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) &&
541 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
542 return;
543
Michael Chan68929142005-08-09 20:17:14 -0700544 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chanbbadf502006-04-06 21:46:34 -0700545 if (tp->tg3_flags & TG3_FLAG_SRAM_USE_CONFIG) {
546 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
547 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Michael Chanbbadf502006-04-06 21:46:34 -0700549 /* Always leave this as zero. */
550 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
551 } else {
552 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
553 tw32_f(TG3PCI_MEM_WIN_DATA, val);
554
555 /* Always leave this as zero. */
556 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
557 }
Michael Chan68929142005-08-09 20:17:14 -0700558 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560
561static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
562{
Michael Chan68929142005-08-09 20:17:14 -0700563 unsigned long flags;
564
Michael Chanb5d37722006-09-27 16:06:21 -0700565 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) &&
566 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
567 *val = 0;
568 return;
569 }
570
Michael Chan68929142005-08-09 20:17:14 -0700571 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chanbbadf502006-04-06 21:46:34 -0700572 if (tp->tg3_flags & TG3_FLAG_SRAM_USE_CONFIG) {
573 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
574 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Michael Chanbbadf502006-04-06 21:46:34 -0700576 /* Always leave this as zero. */
577 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
578 } else {
579 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
580 *val = tr32(TG3PCI_MEM_WIN_DATA);
581
582 /* Always leave this as zero. */
583 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
584 }
Michael Chan68929142005-08-09 20:17:14 -0700585 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586}
587
Matt Carlson0d3031d2007-10-10 18:02:43 -0700588static void tg3_ape_lock_init(struct tg3 *tp)
589{
590 int i;
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000591 u32 regbase;
592
593 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
594 regbase = TG3_APE_LOCK_GRANT;
595 else
596 regbase = TG3_APE_PER_LOCK_GRANT;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700597
598 /* Make sure the driver hasn't any stale locks. */
599 for (i = 0; i < 8; i++)
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000600 tg3_ape_write32(tp, regbase + 4 * i, APE_LOCK_GRANT_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700601}
602
603static int tg3_ape_lock(struct tg3 *tp, int locknum)
604{
605 int i, off;
606 int ret = 0;
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000607 u32 status, req, gnt;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700608
609 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
610 return 0;
611
612 switch (locknum) {
Matt Carlson33f401a2010-04-05 10:19:27 +0000613 case TG3_APE_LOCK_GRC:
614 case TG3_APE_LOCK_MEM:
615 break;
616 default:
617 return -EINVAL;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700618 }
619
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000620 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
621 req = TG3_APE_LOCK_REQ;
622 gnt = TG3_APE_LOCK_GRANT;
623 } else {
624 req = TG3_APE_PER_LOCK_REQ;
625 gnt = TG3_APE_PER_LOCK_GRANT;
626 }
627
Matt Carlson0d3031d2007-10-10 18:02:43 -0700628 off = 4 * locknum;
629
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000630 tg3_ape_write32(tp, req + off, APE_LOCK_REQ_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700631
632 /* Wait for up to 1 millisecond to acquire lock. */
633 for (i = 0; i < 100; i++) {
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000634 status = tg3_ape_read32(tp, gnt + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700635 if (status == APE_LOCK_GRANT_DRIVER)
636 break;
637 udelay(10);
638 }
639
640 if (status != APE_LOCK_GRANT_DRIVER) {
641 /* Revoke the lock request. */
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000642 tg3_ape_write32(tp, gnt + off,
Matt Carlson0d3031d2007-10-10 18:02:43 -0700643 APE_LOCK_GRANT_DRIVER);
644
645 ret = -EBUSY;
646 }
647
648 return ret;
649}
650
651static void tg3_ape_unlock(struct tg3 *tp, int locknum)
652{
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000653 u32 gnt;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700654
655 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
656 return;
657
658 switch (locknum) {
Matt Carlson33f401a2010-04-05 10:19:27 +0000659 case TG3_APE_LOCK_GRC:
660 case TG3_APE_LOCK_MEM:
661 break;
662 default:
663 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700664 }
665
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000666 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
667 gnt = TG3_APE_LOCK_GRANT;
668 else
669 gnt = TG3_APE_PER_LOCK_GRANT;
670
671 tg3_ape_write32(tp, gnt + 4 * locknum, APE_LOCK_GRANT_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700672}
673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674static void tg3_disable_ints(struct tg3 *tp)
675{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000676 int i;
677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 tw32(TG3PCI_MISC_HOST_CTRL,
679 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000680 for (i = 0; i < tp->irq_max; i++)
681 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682}
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684static void tg3_enable_ints(struct tg3 *tp)
685{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000686 int i;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000687
Michael Chanbbe832c2005-06-24 20:20:04 -0700688 tp->irq_sync = 0;
689 wmb();
690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 tw32(TG3PCI_MISC_HOST_CTRL,
692 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000693
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000694 tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000695 for (i = 0; i < tp->irq_cnt; i++) {
696 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000697
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000698 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
699 if (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)
700 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
701
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000702 tp->coal_now |= tnapi->coal_now;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000703 }
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000704
705 /* Force an initial interrupt */
706 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) &&
707 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
708 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
709 else
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000710 tw32(HOSTCC_MODE, tp->coal_now);
711
712 tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713}
714
Matt Carlson17375d22009-08-28 14:02:18 +0000715static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
Michael Chan04237dd2005-04-25 15:17:17 -0700716{
Matt Carlson17375d22009-08-28 14:02:18 +0000717 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +0000718 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan04237dd2005-04-25 15:17:17 -0700719 unsigned int work_exists = 0;
720
721 /* check for phy events */
722 if (!(tp->tg3_flags &
723 (TG3_FLAG_USE_LINKCHG_REG |
724 TG3_FLAG_POLL_SERDES))) {
725 if (sblk->status & SD_STATUS_LINK_CHG)
726 work_exists = 1;
727 }
728 /* check for RX/TX work to do */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000729 if (sblk->idx[0].tx_consumer != tnapi->tx_cons ||
Matt Carlson8d9d7cf2009-09-01 13:19:05 +0000730 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -0700731 work_exists = 1;
732
733 return work_exists;
734}
735
Matt Carlson17375d22009-08-28 14:02:18 +0000736/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -0700737 * similar to tg3_enable_ints, but it accurately determines whether there
738 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400739 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 */
Matt Carlson17375d22009-08-28 14:02:18 +0000741static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
Matt Carlson17375d22009-08-28 14:02:18 +0000743 struct tg3 *tp = tnapi->tp;
744
Matt Carlson898a56f2009-08-28 14:02:40 +0000745 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 mmiowb();
747
David S. Millerfac9b832005-05-18 22:46:34 -0700748 /* When doing tagged status, this work check is unnecessary.
749 * The last_tag we write above tells the chip which piece of
750 * work we've completed.
751 */
752 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) &&
Matt Carlson17375d22009-08-28 14:02:18 +0000753 tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -0700754 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +0000755 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756}
757
Matt Carlsonfed97812009-09-01 13:10:19 +0000758static void tg3_napi_disable(struct tg3 *tp)
759{
760 int i;
761
762 for (i = tp->irq_cnt - 1; i >= 0; i--)
763 napi_disable(&tp->napi[i].napi);
764}
765
766static void tg3_napi_enable(struct tg3 *tp)
767{
768 int i;
769
770 for (i = 0; i < tp->irq_cnt; i++)
771 napi_enable(&tp->napi[i].napi);
772}
773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774static inline void tg3_netif_stop(struct tg3 *tp)
775{
Michael Chanbbe832c2005-06-24 20:20:04 -0700776 tp->dev->trans_start = jiffies; /* prevent tx timeout */
Matt Carlsonfed97812009-09-01 13:10:19 +0000777 tg3_napi_disable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 netif_tx_disable(tp->dev);
779}
780
781static inline void tg3_netif_start(struct tg3 *tp)
782{
Matt Carlsonfe5f5782009-09-01 13:09:39 +0000783 /* NOTE: unconditional netif_tx_wake_all_queues is only
784 * appropriate so long as all callers are assured to
785 * have free tx slots (such as after tg3_init_hw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 */
Matt Carlsonfe5f5782009-09-01 13:09:39 +0000787 netif_tx_wake_all_queues(tp->dev);
788
Matt Carlsonfed97812009-09-01 13:10:19 +0000789 tg3_napi_enable(tp);
790 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
David S. Millerf47c11e2005-06-24 20:18:35 -0700791 tg3_enable_ints(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792}
793
794static void tg3_switch_clocks(struct tg3 *tp)
795{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000796 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 u32 orig_clock_ctrl;
798
Matt Carlson795d01c2007-10-07 23:28:17 -0700799 if ((tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
800 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -0700801 return;
802
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000803 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 orig_clock_ctrl = clock_ctrl;
806 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
807 CLOCK_CTRL_CLKRUN_OENABLE |
808 0x1f);
809 tp->pci_clock_ctrl = clock_ctrl;
810
811 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
812 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800813 tw32_wait_f(TG3PCI_CLOCK_CTRL,
814 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 }
816 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800817 tw32_wait_f(TG3PCI_CLOCK_CTRL,
818 clock_ctrl |
819 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
820 40);
821 tw32_wait_f(TG3PCI_CLOCK_CTRL,
822 clock_ctrl | (CLOCK_CTRL_ALTCLK),
823 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 }
Michael Chanb401e9e2005-12-19 16:27:04 -0800825 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826}
827
828#define PHY_BUSY_LOOPS 5000
829
830static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
831{
832 u32 frame_val;
833 unsigned int loops;
834 int ret;
835
836 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
837 tw32_f(MAC_MI_MODE,
838 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
839 udelay(80);
840 }
841
842 *val = 0x0;
843
Matt Carlson882e9792009-09-01 13:21:36 +0000844 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 MI_COM_PHY_ADDR_MASK);
846 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
847 MI_COM_REG_ADDR_MASK);
848 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 tw32_f(MAC_MI_COM, frame_val);
851
852 loops = PHY_BUSY_LOOPS;
853 while (loops != 0) {
854 udelay(10);
855 frame_val = tr32(MAC_MI_COM);
856
857 if ((frame_val & MI_COM_BUSY) == 0) {
858 udelay(5);
859 frame_val = tr32(MAC_MI_COM);
860 break;
861 }
862 loops -= 1;
863 }
864
865 ret = -EBUSY;
866 if (loops != 0) {
867 *val = frame_val & MI_COM_DATA_MASK;
868 ret = 0;
869 }
870
871 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
872 tw32_f(MAC_MI_MODE, tp->mi_mode);
873 udelay(80);
874 }
875
876 return ret;
877}
878
879static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
880{
881 u32 frame_val;
882 unsigned int loops;
883 int ret;
884
Matt Carlson7f97a4b2009-08-25 10:10:03 +0000885 if ((tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) &&
Michael Chanb5d37722006-09-27 16:06:21 -0700886 (reg == MII_TG3_CTRL || reg == MII_TG3_AUX_CTRL))
887 return 0;
888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
890 tw32_f(MAC_MI_MODE,
891 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
892 udelay(80);
893 }
894
Matt Carlson882e9792009-09-01 13:21:36 +0000895 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 MI_COM_PHY_ADDR_MASK);
897 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
898 MI_COM_REG_ADDR_MASK);
899 frame_val |= (val & MI_COM_DATA_MASK);
900 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 tw32_f(MAC_MI_COM, frame_val);
903
904 loops = PHY_BUSY_LOOPS;
905 while (loops != 0) {
906 udelay(10);
907 frame_val = tr32(MAC_MI_COM);
908 if ((frame_val & MI_COM_BUSY) == 0) {
909 udelay(5);
910 frame_val = tr32(MAC_MI_COM);
911 break;
912 }
913 loops -= 1;
914 }
915
916 ret = -EBUSY;
917 if (loops != 0)
918 ret = 0;
919
920 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
921 tw32_f(MAC_MI_MODE, tp->mi_mode);
922 udelay(80);
923 }
924
925 return ret;
926}
927
Matt Carlson95e28692008-05-25 23:44:14 -0700928static int tg3_bmcr_reset(struct tg3 *tp)
929{
930 u32 phy_control;
931 int limit, err;
932
933 /* OK, reset it, and poll the BMCR_RESET bit until it
934 * clears or we time out.
935 */
936 phy_control = BMCR_RESET;
937 err = tg3_writephy(tp, MII_BMCR, phy_control);
938 if (err != 0)
939 return -EBUSY;
940
941 limit = 5000;
942 while (limit--) {
943 err = tg3_readphy(tp, MII_BMCR, &phy_control);
944 if (err != 0)
945 return -EBUSY;
946
947 if ((phy_control & BMCR_RESET) == 0) {
948 udelay(40);
949 break;
950 }
951 udelay(10);
952 }
Roel Kluind4675b52009-02-12 16:33:27 -0800953 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -0700954 return -EBUSY;
955
956 return 0;
957}
958
Matt Carlson158d7ab2008-05-29 01:37:54 -0700959static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
960{
Francois Romieu3d165432009-01-19 16:56:50 -0800961 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700962 u32 val;
963
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000964 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -0700965
966 if (tg3_readphy(tp, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000967 val = -EIO;
968
969 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -0700970
971 return val;
972}
973
974static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
975{
Francois Romieu3d165432009-01-19 16:56:50 -0800976 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000977 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700978
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000979 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -0700980
981 if (tg3_writephy(tp, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000982 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700983
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000984 spin_unlock_bh(&tp->lock);
985
986 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700987}
988
989static int tg3_mdio_reset(struct mii_bus *bp)
990{
991 return 0;
992}
993
Matt Carlson9c61d6b2008-11-03 16:54:56 -0800994static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -0700995{
996 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800997 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -0700998
Matt Carlson3f0e3ad2009-11-02 14:24:36 +0000999 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001000 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001001 case PHY_ID_BCM50610:
1002 case PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001003 val = MAC_PHYCFG2_50610_LED_MODES;
1004 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001005 case PHY_ID_BCMAC131:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001006 val = MAC_PHYCFG2_AC131_LED_MODES;
1007 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001008 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001009 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
1010 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001011 case PHY_ID_RTL8201E:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001012 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
1013 break;
1014 default:
Matt Carlsona9daf362008-05-25 23:49:44 -07001015 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001016 }
1017
1018 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
1019 tw32(MAC_PHYCFG2, val);
1020
1021 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001022 val &= ~(MAC_PHYCFG1_RGMII_INT |
1023 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
1024 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001025 tw32(MAC_PHYCFG1, val);
1026
1027 return;
1028 }
1029
Matt Carlson14417062010-02-17 15:16:59 +00001030 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE))
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001031 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
1032 MAC_PHYCFG2_FMODE_MASK_MASK |
1033 MAC_PHYCFG2_GMODE_MASK_MASK |
1034 MAC_PHYCFG2_ACT_MASK_MASK |
1035 MAC_PHYCFG2_QUAL_MASK_MASK |
1036 MAC_PHYCFG2_INBAND_ENABLE;
1037
1038 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001039
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001040 val = tr32(MAC_PHYCFG1);
1041 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
1042 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
Matt Carlson14417062010-02-17 15:16:59 +00001043 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE)) {
Matt Carlsona9daf362008-05-25 23:49:44 -07001044 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
1045 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
1046 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
1047 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1048 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001049 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1050 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1051 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001052
Matt Carlsona9daf362008-05-25 23:49:44 -07001053 val = tr32(MAC_EXT_RGMII_MODE);
1054 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1055 MAC_RGMII_MODE_RX_QUALITY |
1056 MAC_RGMII_MODE_RX_ACTIVITY |
1057 MAC_RGMII_MODE_RX_ENG_DET |
1058 MAC_RGMII_MODE_TX_ENABLE |
1059 MAC_RGMII_MODE_TX_LOWPWR |
1060 MAC_RGMII_MODE_TX_RESET);
Matt Carlson14417062010-02-17 15:16:59 +00001061 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE)) {
Matt Carlsona9daf362008-05-25 23:49:44 -07001062 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
1063 val |= MAC_RGMII_MODE_RX_INT_B |
1064 MAC_RGMII_MODE_RX_QUALITY |
1065 MAC_RGMII_MODE_RX_ACTIVITY |
1066 MAC_RGMII_MODE_RX_ENG_DET;
1067 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
1068 val |= MAC_RGMII_MODE_TX_ENABLE |
1069 MAC_RGMII_MODE_TX_LOWPWR |
1070 MAC_RGMII_MODE_TX_RESET;
1071 }
1072 tw32(MAC_EXT_RGMII_MODE, val);
1073}
1074
Matt Carlson158d7ab2008-05-29 01:37:54 -07001075static void tg3_mdio_start(struct tg3 *tp)
1076{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001077 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1078 tw32_f(MAC_MI_MODE, tp->mi_mode);
1079 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001080
Matt Carlson9ea48182010-02-17 15:17:01 +00001081 if ((tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED) &&
1082 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1083 tg3_mdio_config_5785(tp);
1084}
1085
1086static int tg3_mdio_init(struct tg3 *tp)
1087{
1088 int i;
1089 u32 reg;
1090 struct phy_device *phydev;
1091
Matt Carlsona50d0792010-06-05 17:24:37 +00001092 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
1093 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) {
Matt Carlson9c7df912010-06-05 17:24:36 +00001094 u32 is_serdes;
Matt Carlson882e9792009-09-01 13:21:36 +00001095
Matt Carlson9c7df912010-06-05 17:24:36 +00001096 tp->phy_addr = PCI_FUNC(tp->pdev->devfn) + 1;
Matt Carlson882e9792009-09-01 13:21:36 +00001097
Matt Carlsond1ec96a2010-01-12 10:11:38 +00001098 if (tp->pci_chip_rev_id != CHIPREV_ID_5717_A0)
1099 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1100 else
1101 is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1102 TG3_CPMU_PHY_STRAP_IS_SERDES;
Matt Carlson882e9792009-09-01 13:21:36 +00001103 if (is_serdes)
1104 tp->phy_addr += 7;
1105 } else
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001106 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001107
Matt Carlson158d7ab2008-05-29 01:37:54 -07001108 tg3_mdio_start(tp);
1109
1110 if (!(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) ||
1111 (tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED))
1112 return 0;
1113
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001114 tp->mdio_bus = mdiobus_alloc();
1115 if (tp->mdio_bus == NULL)
1116 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001117
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001118 tp->mdio_bus->name = "tg3 mdio bus";
1119 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001120 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001121 tp->mdio_bus->priv = tp;
1122 tp->mdio_bus->parent = &tp->pdev->dev;
1123 tp->mdio_bus->read = &tg3_mdio_read;
1124 tp->mdio_bus->write = &tg3_mdio_write;
1125 tp->mdio_bus->reset = &tg3_mdio_reset;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001126 tp->mdio_bus->phy_mask = ~(1 << TG3_PHY_MII_ADDR);
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001127 tp->mdio_bus->irq = &tp->mdio_irq[0];
Matt Carlson158d7ab2008-05-29 01:37:54 -07001128
1129 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001130 tp->mdio_bus->irq[i] = PHY_POLL;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001131
1132 /* The bus registration will look for all the PHYs on the mdio bus.
1133 * Unfortunately, it does not ensure the PHY is powered up before
1134 * accessing the PHY ID registers. A chip reset is the
1135 * quickest way to bring the device back to an operational state..
1136 */
1137 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1138 tg3_bmcr_reset(tp);
1139
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001140 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001141 if (i) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001142 dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001143 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001144 return i;
1145 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001146
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001147 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsona9daf362008-05-25 23:49:44 -07001148
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001149 if (!phydev || !phydev->drv) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001150 dev_warn(&tp->pdev->dev, "No PHY devices\n");
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001151 mdiobus_unregister(tp->mdio_bus);
1152 mdiobus_free(tp->mdio_bus);
1153 return -ENODEV;
1154 }
1155
1156 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001157 case PHY_ID_BCM57780:
Matt Carlson321d32a2008-11-21 17:22:19 -08001158 phydev->interface = PHY_INTERFACE_MODE_GMII;
Matt Carlsonc704dc22009-11-02 14:32:12 +00001159 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08001160 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001161 case PHY_ID_BCM50610:
1162 case PHY_ID_BCM50610M:
Matt Carlson32e5a8d2009-11-02 14:31:39 +00001163 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001164 PHY_BRCM_RX_REFCLK_UNUSED |
Matt Carlson52fae082009-11-02 14:32:38 +00001165 PHY_BRCM_DIS_TXCRXC_NOENRGY |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001166 PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson14417062010-02-17 15:16:59 +00001167 if (tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE)
Matt Carlsona9daf362008-05-25 23:49:44 -07001168 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
1169 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
1170 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
1171 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
1172 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001173 /* fallthru */
Matt Carlson6a443a02010-02-17 15:17:04 +00001174 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001175 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001176 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001177 case PHY_ID_RTL8201E:
1178 case PHY_ID_BCMAC131:
Matt Carlsona9daf362008-05-25 23:49:44 -07001179 phydev->interface = PHY_INTERFACE_MODE_MII;
Matt Carlsoncdd4e09d2009-11-02 14:31:11 +00001180 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001181 tp->tg3_flags3 |= TG3_FLG3_PHY_IS_FET;
Matt Carlsona9daf362008-05-25 23:49:44 -07001182 break;
1183 }
1184
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001185 tp->tg3_flags3 |= TG3_FLG3_MDIOBUS_INITED;
1186
1187 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1188 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001189
1190 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001191}
1192
1193static void tg3_mdio_fini(struct tg3 *tp)
1194{
1195 if (tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED) {
1196 tp->tg3_flags3 &= ~TG3_FLG3_MDIOBUS_INITED;
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001197 mdiobus_unregister(tp->mdio_bus);
1198 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001199 }
1200}
1201
Matt Carlson95e28692008-05-25 23:44:14 -07001202/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001203static inline void tg3_generate_fw_event(struct tg3 *tp)
1204{
1205 u32 val;
1206
1207 val = tr32(GRC_RX_CPU_EVENT);
1208 val |= GRC_RX_CPU_DRIVER_EVENT;
1209 tw32_f(GRC_RX_CPU_EVENT, val);
1210
1211 tp->last_event_jiffies = jiffies;
1212}
1213
1214#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1215
1216/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001217static void tg3_wait_for_event_ack(struct tg3 *tp)
1218{
1219 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001220 unsigned int delay_cnt;
1221 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001222
Matt Carlson4ba526c2008-08-15 14:10:04 -07001223 /* If enough time has passed, no wait is necessary. */
1224 time_remain = (long)(tp->last_event_jiffies + 1 +
1225 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1226 (long)jiffies;
1227 if (time_remain < 0)
1228 return;
1229
1230 /* Check if we can shorten the wait time. */
1231 delay_cnt = jiffies_to_usecs(time_remain);
1232 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1233 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1234 delay_cnt = (delay_cnt >> 3) + 1;
1235
1236 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001237 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1238 break;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001239 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001240 }
1241}
1242
1243/* tp->lock is held. */
1244static void tg3_ump_link_report(struct tg3 *tp)
1245{
1246 u32 reg;
1247 u32 val;
1248
1249 if (!(tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
1250 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
1251 return;
1252
1253 tg3_wait_for_event_ack(tp);
1254
1255 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1256
1257 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1258
1259 val = 0;
1260 if (!tg3_readphy(tp, MII_BMCR, &reg))
1261 val = reg << 16;
1262 if (!tg3_readphy(tp, MII_BMSR, &reg))
1263 val |= (reg & 0xffff);
1264 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX, val);
1265
1266 val = 0;
1267 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1268 val = reg << 16;
1269 if (!tg3_readphy(tp, MII_LPA, &reg))
1270 val |= (reg & 0xffff);
1271 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 4, val);
1272
1273 val = 0;
1274 if (!(tp->tg3_flags2 & TG3_FLG2_MII_SERDES)) {
1275 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1276 val = reg << 16;
1277 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1278 val |= (reg & 0xffff);
1279 }
1280 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 8, val);
1281
1282 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1283 val = reg << 16;
1284 else
1285 val = 0;
1286 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 12, val);
1287
Matt Carlson4ba526c2008-08-15 14:10:04 -07001288 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001289}
1290
1291static void tg3_link_report(struct tg3 *tp)
1292{
1293 if (!netif_carrier_ok(tp->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001294 netif_info(tp, link, tp->dev, "Link is down\n");
Matt Carlson95e28692008-05-25 23:44:14 -07001295 tg3_ump_link_report(tp);
1296 } else if (netif_msg_link(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001297 netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
1298 (tp->link_config.active_speed == SPEED_1000 ?
1299 1000 :
1300 (tp->link_config.active_speed == SPEED_100 ?
1301 100 : 10)),
1302 (tp->link_config.active_duplex == DUPLEX_FULL ?
1303 "full" : "half"));
Matt Carlson95e28692008-05-25 23:44:14 -07001304
Joe Perches05dbe002010-02-17 19:44:19 +00001305 netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
1306 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
1307 "on" : "off",
1308 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
1309 "on" : "off");
Matt Carlson95e28692008-05-25 23:44:14 -07001310 tg3_ump_link_report(tp);
1311 }
1312}
1313
1314static u16 tg3_advert_flowctrl_1000T(u8 flow_ctrl)
1315{
1316 u16 miireg;
1317
Steve Glendinninge18ce342008-12-16 02:00:00 -08001318 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001319 miireg = ADVERTISE_PAUSE_CAP;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001320 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001321 miireg = ADVERTISE_PAUSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001322 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001323 miireg = ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1324 else
1325 miireg = 0;
1326
1327 return miireg;
1328}
1329
1330static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1331{
1332 u16 miireg;
1333
Steve Glendinninge18ce342008-12-16 02:00:00 -08001334 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001335 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001336 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001337 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001338 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001339 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1340 else
1341 miireg = 0;
1342
1343 return miireg;
1344}
1345
Matt Carlson95e28692008-05-25 23:44:14 -07001346static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1347{
1348 u8 cap = 0;
1349
1350 if (lcladv & ADVERTISE_1000XPAUSE) {
1351 if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1352 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001353 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001354 else if (rmtadv & LPA_1000XPAUSE_ASYM)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001355 cap = FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001356 } else {
1357 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001358 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001359 }
1360 } else if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1361 if ((rmtadv & LPA_1000XPAUSE) && (rmtadv & LPA_1000XPAUSE_ASYM))
Steve Glendinninge18ce342008-12-16 02:00:00 -08001362 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001363 }
1364
1365 return cap;
1366}
1367
Matt Carlsonf51f3562008-05-25 23:45:08 -07001368static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001369{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001370 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001371 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001372 u32 old_rx_mode = tp->rx_mode;
1373 u32 old_tx_mode = tp->tx_mode;
1374
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001375 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001376 autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001377 else
1378 autoneg = tp->link_config.autoneg;
1379
1380 if (autoneg == AUTONEG_ENABLE &&
Matt Carlson95e28692008-05-25 23:44:14 -07001381 (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG)) {
1382 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001383 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001384 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001385 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001386 } else
1387 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001388
Matt Carlsonf51f3562008-05-25 23:45:08 -07001389 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001390
Steve Glendinninge18ce342008-12-16 02:00:00 -08001391 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001392 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1393 else
1394 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1395
Matt Carlsonf51f3562008-05-25 23:45:08 -07001396 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001397 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001398
Steve Glendinninge18ce342008-12-16 02:00:00 -08001399 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001400 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1401 else
1402 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1403
Matt Carlsonf51f3562008-05-25 23:45:08 -07001404 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001405 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001406}
1407
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001408static void tg3_adjust_link(struct net_device *dev)
1409{
1410 u8 oldflowctrl, linkmesg = 0;
1411 u32 mac_mode, lcl_adv, rmt_adv;
1412 struct tg3 *tp = netdev_priv(dev);
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001413 struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001414
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001415 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001416
1417 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
1418 MAC_MODE_HALF_DUPLEX);
1419
1420 oldflowctrl = tp->link_config.active_flowctrl;
1421
1422 if (phydev->link) {
1423 lcl_adv = 0;
1424 rmt_adv = 0;
1425
1426 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
1427 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001428 else if (phydev->speed == SPEED_1000 ||
1429 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001430 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001431 else
1432 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001433
1434 if (phydev->duplex == DUPLEX_HALF)
1435 mac_mode |= MAC_MODE_HALF_DUPLEX;
1436 else {
1437 lcl_adv = tg3_advert_flowctrl_1000T(
1438 tp->link_config.flowctrl);
1439
1440 if (phydev->pause)
1441 rmt_adv = LPA_PAUSE_CAP;
1442 if (phydev->asym_pause)
1443 rmt_adv |= LPA_PAUSE_ASYM;
1444 }
1445
1446 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
1447 } else
1448 mac_mode |= MAC_MODE_PORT_MODE_GMII;
1449
1450 if (mac_mode != tp->mac_mode) {
1451 tp->mac_mode = mac_mode;
1452 tw32_f(MAC_MODE, tp->mac_mode);
1453 udelay(40);
1454 }
1455
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001456 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
1457 if (phydev->speed == SPEED_10)
1458 tw32(MAC_MI_STAT,
1459 MAC_MI_STAT_10MBPS_MODE |
1460 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1461 else
1462 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1463 }
1464
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001465 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
1466 tw32(MAC_TX_LENGTHS,
1467 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1468 (6 << TX_LENGTHS_IPG_SHIFT) |
1469 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
1470 else
1471 tw32(MAC_TX_LENGTHS,
1472 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1473 (6 << TX_LENGTHS_IPG_SHIFT) |
1474 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
1475
1476 if ((phydev->link && tp->link_config.active_speed == SPEED_INVALID) ||
1477 (!phydev->link && tp->link_config.active_speed != SPEED_INVALID) ||
1478 phydev->speed != tp->link_config.active_speed ||
1479 phydev->duplex != tp->link_config.active_duplex ||
1480 oldflowctrl != tp->link_config.active_flowctrl)
Matt Carlsonc6cdf432010-04-05 10:19:26 +00001481 linkmesg = 1;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001482
1483 tp->link_config.active_speed = phydev->speed;
1484 tp->link_config.active_duplex = phydev->duplex;
1485
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001486 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001487
1488 if (linkmesg)
1489 tg3_link_report(tp);
1490}
1491
1492static int tg3_phy_init(struct tg3 *tp)
1493{
1494 struct phy_device *phydev;
1495
1496 if (tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED)
1497 return 0;
1498
1499 /* Bring the PHY back to a known state. */
1500 tg3_bmcr_reset(tp);
1501
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001502 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001503
1504 /* Attach the MAC to the PHY. */
Kay Sieversfb28ad32008-11-10 13:55:14 -08001505 phydev = phy_connect(tp->dev, dev_name(&phydev->dev), tg3_adjust_link,
Matt Carlsona9daf362008-05-25 23:49:44 -07001506 phydev->dev_flags, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001507 if (IS_ERR(phydev)) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001508 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001509 return PTR_ERR(phydev);
1510 }
1511
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001512 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001513 switch (phydev->interface) {
1514 case PHY_INTERFACE_MODE_GMII:
1515 case PHY_INTERFACE_MODE_RGMII:
Matt Carlson321d32a2008-11-21 17:22:19 -08001516 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY)) {
1517 phydev->supported &= (PHY_GBIT_FEATURES |
1518 SUPPORTED_Pause |
1519 SUPPORTED_Asym_Pause);
1520 break;
1521 }
1522 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001523 case PHY_INTERFACE_MODE_MII:
1524 phydev->supported &= (PHY_BASIC_FEATURES |
1525 SUPPORTED_Pause |
1526 SUPPORTED_Asym_Pause);
1527 break;
1528 default:
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001529 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001530 return -EINVAL;
1531 }
1532
1533 tp->tg3_flags3 |= TG3_FLG3_PHY_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001534
1535 phydev->advertising = phydev->supported;
1536
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001537 return 0;
1538}
1539
1540static void tg3_phy_start(struct tg3 *tp)
1541{
1542 struct phy_device *phydev;
1543
1544 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
1545 return;
1546
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001547 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001548
1549 if (tp->link_config.phy_is_low_power) {
1550 tp->link_config.phy_is_low_power = 0;
1551 phydev->speed = tp->link_config.orig_speed;
1552 phydev->duplex = tp->link_config.orig_duplex;
1553 phydev->autoneg = tp->link_config.orig_autoneg;
1554 phydev->advertising = tp->link_config.orig_advertising;
1555 }
1556
1557 phy_start(phydev);
1558
1559 phy_start_aneg(phydev);
1560}
1561
1562static void tg3_phy_stop(struct tg3 *tp)
1563{
1564 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
1565 return;
1566
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001567 phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001568}
1569
1570static void tg3_phy_fini(struct tg3 *tp)
1571{
1572 if (tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001573 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001574 tp->tg3_flags3 &= ~TG3_FLG3_PHY_CONNECTED;
1575 }
1576}
1577
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001578static void tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
1579{
1580 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1581 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
1582}
1583
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001584static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
1585{
1586 u32 phytest;
1587
1588 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
1589 u32 phy;
1590
1591 tg3_writephy(tp, MII_TG3_FET_TEST,
1592 phytest | MII_TG3_FET_SHADOW_EN);
1593 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
1594 if (enable)
1595 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
1596 else
1597 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
1598 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
1599 }
1600 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
1601 }
1602}
1603
Matt Carlson6833c042008-11-21 17:18:59 -08001604static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
1605{
1606 u32 reg;
1607
Matt Carlsonecf14102010-01-20 16:58:05 +00001608 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
Matt Carlsona50d0792010-06-05 17:24:37 +00001609 ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
1610 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) &&
Matt Carlsonecf14102010-01-20 16:58:05 +00001611 (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08001612 return;
1613
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001614 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
1615 tg3_phy_fet_toggle_apd(tp, enable);
1616 return;
1617 }
1618
Matt Carlson6833c042008-11-21 17:18:59 -08001619 reg = MII_TG3_MISC_SHDW_WREN |
1620 MII_TG3_MISC_SHDW_SCR5_SEL |
1621 MII_TG3_MISC_SHDW_SCR5_LPED |
1622 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
1623 MII_TG3_MISC_SHDW_SCR5_SDTL |
1624 MII_TG3_MISC_SHDW_SCR5_C125OE;
1625 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 || !enable)
1626 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
1627
1628 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1629
1630
1631 reg = MII_TG3_MISC_SHDW_WREN |
1632 MII_TG3_MISC_SHDW_APD_SEL |
1633 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
1634 if (enable)
1635 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
1636
1637 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1638}
1639
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001640static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
1641{
1642 u32 phy;
1643
1644 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
1645 (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES))
1646 return;
1647
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001648 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001649 u32 ephy;
1650
Matt Carlson535ef6e2009-08-25 10:09:36 +00001651 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
1652 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
1653
1654 tg3_writephy(tp, MII_TG3_FET_TEST,
1655 ephy | MII_TG3_FET_SHADOW_EN);
1656 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001657 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00001658 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001659 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00001660 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
1661 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001662 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00001663 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001664 }
1665 } else {
1666 phy = MII_TG3_AUXCTL_MISC_RDSEL_MISC |
1667 MII_TG3_AUXCTL_SHDWSEL_MISC;
1668 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, phy) &&
1669 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &phy)) {
1670 if (enable)
1671 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1672 else
1673 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1674 phy |= MII_TG3_AUXCTL_MISC_WREN;
1675 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1676 }
1677 }
1678}
1679
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680static void tg3_phy_set_wirespeed(struct tg3 *tp)
1681{
1682 u32 val;
1683
1684 if (tp->tg3_flags2 & TG3_FLG2_NO_ETH_WIRE_SPEED)
1685 return;
1686
1687 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x7007) &&
1688 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &val))
1689 tg3_writephy(tp, MII_TG3_AUX_CTRL,
1690 (val | (1 << 15) | (1 << 4)));
1691}
1692
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001693static void tg3_phy_apply_otp(struct tg3 *tp)
1694{
1695 u32 otp, phy;
1696
1697 if (!tp->phy_otp)
1698 return;
1699
1700 otp = tp->phy_otp;
1701
1702 /* Enable SM_DSP clock and tx 6dB coding. */
1703 phy = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1704 MII_TG3_AUXCTL_ACTL_SMDSP_ENA |
1705 MII_TG3_AUXCTL_ACTL_TX_6DB;
1706 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1707
1708 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
1709 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
1710 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
1711
1712 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
1713 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
1714 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
1715
1716 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
1717 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
1718 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
1719
1720 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
1721 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
1722
1723 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
1724 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
1725
1726 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
1727 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
1728 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
1729
1730 /* Turn off SM_DSP clock. */
1731 phy = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1732 MII_TG3_AUXCTL_ACTL_TX_6DB;
1733 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1734}
1735
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736static int tg3_wait_macro_done(struct tg3 *tp)
1737{
1738 int limit = 100;
1739
1740 while (limit--) {
1741 u32 tmp32;
1742
1743 if (!tg3_readphy(tp, 0x16, &tmp32)) {
1744 if ((tmp32 & 0x1000) == 0)
1745 break;
1746 }
1747 }
Roel Kluind4675b52009-02-12 16:33:27 -08001748 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 return -EBUSY;
1750
1751 return 0;
1752}
1753
1754static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
1755{
1756 static const u32 test_pat[4][6] = {
1757 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
1758 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
1759 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
1760 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
1761 };
1762 int chan;
1763
1764 for (chan = 0; chan < 4; chan++) {
1765 int i;
1766
1767 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1768 (chan * 0x2000) | 0x0200);
1769 tg3_writephy(tp, 0x16, 0x0002);
1770
1771 for (i = 0; i < 6; i++)
1772 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
1773 test_pat[chan][i]);
1774
1775 tg3_writephy(tp, 0x16, 0x0202);
1776 if (tg3_wait_macro_done(tp)) {
1777 *resetp = 1;
1778 return -EBUSY;
1779 }
1780
1781 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1782 (chan * 0x2000) | 0x0200);
1783 tg3_writephy(tp, 0x16, 0x0082);
1784 if (tg3_wait_macro_done(tp)) {
1785 *resetp = 1;
1786 return -EBUSY;
1787 }
1788
1789 tg3_writephy(tp, 0x16, 0x0802);
1790 if (tg3_wait_macro_done(tp)) {
1791 *resetp = 1;
1792 return -EBUSY;
1793 }
1794
1795 for (i = 0; i < 6; i += 2) {
1796 u32 low, high;
1797
1798 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
1799 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
1800 tg3_wait_macro_done(tp)) {
1801 *resetp = 1;
1802 return -EBUSY;
1803 }
1804 low &= 0x7fff;
1805 high &= 0x000f;
1806 if (low != test_pat[chan][i] ||
1807 high != test_pat[chan][i+1]) {
1808 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
1809 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
1810 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
1811
1812 return -EBUSY;
1813 }
1814 }
1815 }
1816
1817 return 0;
1818}
1819
1820static int tg3_phy_reset_chanpat(struct tg3 *tp)
1821{
1822 int chan;
1823
1824 for (chan = 0; chan < 4; chan++) {
1825 int i;
1826
1827 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1828 (chan * 0x2000) | 0x0200);
1829 tg3_writephy(tp, 0x16, 0x0002);
1830 for (i = 0; i < 6; i++)
1831 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
1832 tg3_writephy(tp, 0x16, 0x0202);
1833 if (tg3_wait_macro_done(tp))
1834 return -EBUSY;
1835 }
1836
1837 return 0;
1838}
1839
1840static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
1841{
1842 u32 reg32, phy9_orig;
1843 int retries, do_phy_reset, err;
1844
1845 retries = 10;
1846 do_phy_reset = 1;
1847 do {
1848 if (do_phy_reset) {
1849 err = tg3_bmcr_reset(tp);
1850 if (err)
1851 return err;
1852 do_phy_reset = 0;
1853 }
1854
1855 /* Disable transmitter and interrupt. */
1856 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
1857 continue;
1858
1859 reg32 |= 0x3000;
1860 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
1861
1862 /* Set full-duplex, 1000 mbps. */
1863 tg3_writephy(tp, MII_BMCR,
1864 BMCR_FULLDPLX | TG3_BMCR_SPEED1000);
1865
1866 /* Set to master mode. */
1867 if (tg3_readphy(tp, MII_TG3_CTRL, &phy9_orig))
1868 continue;
1869
1870 tg3_writephy(tp, MII_TG3_CTRL,
1871 (MII_TG3_CTRL_AS_MASTER |
1872 MII_TG3_CTRL_ENABLE_AS_MASTER));
1873
1874 /* Enable SM_DSP_CLOCK and 6dB. */
1875 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
1876
1877 /* Block the PHY control access. */
1878 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8005);
1879 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0800);
1880
1881 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
1882 if (!err)
1883 break;
1884 } while (--retries);
1885
1886 err = tg3_phy_reset_chanpat(tp);
1887 if (err)
1888 return err;
1889
1890 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8005);
1891 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0000);
1892
1893 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
1894 tg3_writephy(tp, 0x16, 0x0000);
1895
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 Carlsonb2a5c192008-04-03 21:44:44 -07001920 u32 cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 u32 phy_status;
1922 int err;
1923
Michael Chan60189dd2006-12-17 17:08:07 -08001924 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
1925 u32 val;
1926
1927 val = tr32(GRC_MISC_CFG);
1928 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
1929 udelay(40);
1930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 err = tg3_readphy(tp, MII_BMSR, &phy_status);
1932 err |= tg3_readphy(tp, MII_BMSR, &phy_status);
1933 if (err != 0)
1934 return -EBUSY;
1935
Michael Chanc8e1e822006-04-29 18:55:17 -07001936 if (netif_running(tp->dev) && netif_carrier_ok(tp->dev)) {
1937 netif_carrier_off(tp->dev);
1938 tg3_link_report(tp);
1939 }
1940
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
1942 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
1943 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
1944 err = tg3_phy_reset_5703_4_5(tp);
1945 if (err)
1946 return err;
1947 goto out;
1948 }
1949
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001950 cpmuctrl = 0;
1951 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
1952 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
1953 cpmuctrl = tr32(TG3_CPMU_CTRL);
1954 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
1955 tw32(TG3_CPMU_CTRL,
1956 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
1957 }
1958
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 err = tg3_bmcr_reset(tp);
1960 if (err)
1961 return err;
1962
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001963 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
1964 u32 phy;
1965
1966 phy = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
1967 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, phy);
1968
1969 tw32(TG3_CPMU_CTRL, cpmuctrl);
1970 }
1971
Matt Carlsonbcb37f62008-11-03 16:52:09 -08001972 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
1973 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08001974 u32 val;
1975
1976 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
1977 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
1978 CPMU_LSPD_1000MB_MACCLK_12_5) {
1979 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
1980 udelay(40);
1981 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
1982 }
1983 }
1984
Matt Carlsona50d0792010-06-05 17:24:37 +00001985 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
1986 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) &&
Matt Carlsonecf14102010-01-20 16:58:05 +00001987 (tp->tg3_flags2 & TG3_FLG2_MII_SERDES))
1988 return 0;
1989
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001990 tg3_phy_apply_otp(tp);
1991
Matt Carlson6833c042008-11-21 17:18:59 -08001992 if (tp->tg3_flags3 & TG3_FLG3_PHY_ENABLE_APD)
1993 tg3_phy_toggle_apd(tp, true);
1994 else
1995 tg3_phy_toggle_apd(tp, false);
1996
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997out:
1998 if (tp->tg3_flags2 & TG3_FLG2_PHY_ADC_BUG) {
1999 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
2000 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x201f);
2001 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x2aaa);
2002 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
2003 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0323);
2004 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
2005 }
2006 if (tp->tg3_flags2 & TG3_FLG2_PHY_5704_A0_BUG) {
2007 tg3_writephy(tp, 0x1c, 0x8d68);
2008 tg3_writephy(tp, 0x1c, 0x8d68);
2009 }
2010 if (tp->tg3_flags2 & TG3_FLG2_PHY_BER_BUG) {
2011 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
2012 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
2013 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x310b);
2014 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x201f);
2015 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x9506);
2016 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x401f);
2017 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x14e2);
2018 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
Matt Carlson859a588792010-04-05 10:19:28 +00002019 } else if (tp->tg3_flags2 & TG3_FLG2_PHY_JITTER_BUG) {
Michael Chanc424cb22006-04-29 18:56:34 -07002020 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
2021 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
Michael Chanc1d2a192007-01-08 19:57:20 -08002022 if (tp->tg3_flags2 & TG3_FLG2_PHY_ADJUST_TRIM) {
2023 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2024 tg3_writephy(tp, MII_TG3_TEST1,
2025 MII_TG3_TEST1_TRIM_EN | 0x4);
2026 } else
2027 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
Michael Chanc424cb22006-04-29 18:56:34 -07002028 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
2029 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 /* Set Extended packet length bit (bit 14) on all chips that */
2031 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002032 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 /* Cannot do read-modify-write on 5401 */
2034 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4c20);
Matt Carlson8f666b02009-08-28 13:58:24 +00002035 } else if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 u32 phy_reg;
2037
2038 /* Set bit 14 with read-modify-write to preserve other bits */
2039 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0007) &&
2040 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &phy_reg))
2041 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy_reg | 0x4000);
2042 }
2043
2044 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2045 * jumbo frames transmission.
2046 */
Matt Carlson8f666b02009-08-28 13:58:24 +00002047 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 u32 phy_reg;
2049
2050 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &phy_reg))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002051 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2052 phy_reg | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 }
2054
Michael Chan715116a2006-09-27 16:09:25 -07002055 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002056 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002057 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002058 }
2059
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002060 tg3_phy_toggle_automdix(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 tg3_phy_set_wirespeed(tp);
2062 return 0;
2063}
2064
2065static void tg3_frob_aux_power(struct tg3 *tp)
2066{
2067 struct tg3 *tp_peer = tp;
2068
Matt Carlson334355a2010-01-20 16:58:10 +00002069 /* The GPIOs do something completely different on 57765. */
2070 if ((tp->tg3_flags2 & TG3_FLG2_IS_NIC) == 0 ||
Matt Carlsona50d0792010-06-05 17:24:37 +00002071 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlson334355a2010-01-20 16:58:10 +00002072 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 return;
2074
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00002075 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2076 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
2077 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002078 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002080 dev_peer = pci_get_drvdata(tp->pdev_peer);
Michael Chanbc1c7562006-03-20 17:48:03 -08002081 /* remove_one() may have been run on the peer. */
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002082 if (!dev_peer)
Michael Chanbc1c7562006-03-20 17:48:03 -08002083 tp_peer = tp;
2084 else
2085 tp_peer = netdev_priv(dev_peer);
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
2088 if ((tp->tg3_flags & TG3_FLAG_WOL_ENABLE) != 0 ||
Michael Chan6921d202005-12-13 21:15:53 -08002089 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0 ||
2090 (tp_peer->tg3_flags & TG3_FLAG_WOL_ENABLE) != 0 ||
2091 (tp_peer->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2093 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Michael Chanb401e9e2005-12-19 16:27:04 -08002094 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2095 (GRC_LCLCTRL_GPIO_OE0 |
2096 GRC_LCLCTRL_GPIO_OE1 |
2097 GRC_LCLCTRL_GPIO_OE2 |
2098 GRC_LCLCTRL_GPIO_OUTPUT0 |
2099 GRC_LCLCTRL_GPIO_OUTPUT1),
2100 100);
Matt Carlson8d519ab2009-04-20 06:58:01 +00002101 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2102 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -07002103 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2104 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2105 GRC_LCLCTRL_GPIO_OE1 |
2106 GRC_LCLCTRL_GPIO_OE2 |
2107 GRC_LCLCTRL_GPIO_OUTPUT0 |
2108 GRC_LCLCTRL_GPIO_OUTPUT1 |
2109 tp->grc_local_ctrl;
2110 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2111
2112 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2113 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2114
2115 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2116 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 } else {
2118 u32 no_gpio2;
Michael Chandc56b7d2005-12-19 16:26:28 -08002119 u32 grc_local_ctrl = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
2121 if (tp_peer != tp &&
2122 (tp_peer->tg3_flags & TG3_FLAG_INIT_COMPLETE) != 0)
2123 return;
2124
Michael Chandc56b7d2005-12-19 16:26:28 -08002125 /* Workaround to prevent overdrawing Amps. */
2126 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2127 ASIC_REV_5714) {
2128 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chanb401e9e2005-12-19 16:27:04 -08002129 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2130 grc_local_ctrl, 100);
Michael Chandc56b7d2005-12-19 16:26:28 -08002131 }
2132
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 /* On 5753 and variants, GPIO2 cannot be used. */
2134 no_gpio2 = tp->nic_sram_data_cfg &
2135 NIC_SRAM_DATA_CFG_NO_GPIO2;
2136
Michael Chandc56b7d2005-12-19 16:26:28 -08002137 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 GRC_LCLCTRL_GPIO_OE1 |
2139 GRC_LCLCTRL_GPIO_OE2 |
2140 GRC_LCLCTRL_GPIO_OUTPUT1 |
2141 GRC_LCLCTRL_GPIO_OUTPUT2;
2142 if (no_gpio2) {
2143 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2144 GRC_LCLCTRL_GPIO_OUTPUT2);
2145 }
Michael Chanb401e9e2005-12-19 16:27:04 -08002146 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2147 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
2149 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2150
Michael Chanb401e9e2005-12-19 16:27:04 -08002151 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2152 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
2154 if (!no_gpio2) {
2155 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chanb401e9e2005-12-19 16:27:04 -08002156 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2157 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 }
2159 }
2160 } else {
2161 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
2162 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
2163 if (tp_peer != tp &&
2164 (tp_peer->tg3_flags & TG3_FLAG_INIT_COMPLETE) != 0)
2165 return;
2166
Michael Chanb401e9e2005-12-19 16:27:04 -08002167 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2168 (GRC_LCLCTRL_GPIO_OE1 |
2169 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
Michael Chanb401e9e2005-12-19 16:27:04 -08002171 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2172 GRC_LCLCTRL_GPIO_OE1, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
Michael Chanb401e9e2005-12-19 16:27:04 -08002174 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2175 (GRC_LCLCTRL_GPIO_OE1 |
2176 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 }
2178 }
2179}
2180
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002181static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2182{
2183 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2184 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00002185 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002186 if (speed != SPEED_10)
2187 return 1;
2188 } else if (speed == SPEED_10)
2189 return 1;
2190
2191 return 0;
2192}
2193
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194static int tg3_setup_phy(struct tg3 *, int);
2195
2196#define RESET_KIND_SHUTDOWN 0
2197#define RESET_KIND_INIT 1
2198#define RESET_KIND_SUSPEND 2
2199
2200static void tg3_write_sig_post_reset(struct tg3 *, int);
2201static int tg3_halt_cpu(struct tg3 *, u32);
2202
Matt Carlson0a459aa2008-11-03 16:54:15 -08002203static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002204{
Matt Carlsonce057f02007-11-12 21:08:03 -08002205 u32 val;
2206
Michael Chan51297242007-02-13 12:17:57 -08002207 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
2208 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2209 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2210 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2211
2212 sg_dig_ctrl |=
2213 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2214 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2215 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2216 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002217 return;
Michael Chan51297242007-02-13 12:17:57 -08002218 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002219
Michael Chan60189dd2006-12-17 17:08:07 -08002220 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002221 tg3_bmcr_reset(tp);
2222 val = tr32(GRC_MISC_CFG);
2223 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2224 udelay(40);
2225 return;
Matt Carlson0e5f7842009-11-02 14:26:38 +00002226 } else if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
2227 u32 phytest;
2228 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2229 u32 phy;
2230
2231 tg3_writephy(tp, MII_ADVERTISE, 0);
2232 tg3_writephy(tp, MII_BMCR,
2233 BMCR_ANENABLE | BMCR_ANRESTART);
2234
2235 tg3_writephy(tp, MII_TG3_FET_TEST,
2236 phytest | MII_TG3_FET_SHADOW_EN);
2237 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
2238 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
2239 tg3_writephy(tp,
2240 MII_TG3_FET_SHDW_AUXMODE4,
2241 phy);
2242 }
2243 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2244 }
2245 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002246 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07002247 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2248 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002249
2250 tg3_writephy(tp, MII_TG3_AUX_CTRL,
2251 MII_TG3_AUXCTL_SHDWSEL_PWRCTL |
2252 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2253 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2254 MII_TG3_AUXCTL_PCTL_VREG_11V);
Michael Chan715116a2006-09-27 16:09:25 -07002255 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002256
Michael Chan15c3b692006-03-22 01:06:52 -08002257 /* The PHY should not be powered down on some chips because
2258 * of bugs.
2259 */
2260 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2261 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2262 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
2263 (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)))
2264 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08002265
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002266 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2267 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002268 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2269 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2270 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2271 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2272 }
2273
Michael Chan15c3b692006-03-22 01:06:52 -08002274 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2275}
2276
Matt Carlson3f007892008-11-03 16:51:36 -08002277/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002278static int tg3_nvram_lock(struct tg3 *tp)
2279{
2280 if (tp->tg3_flags & TG3_FLAG_NVRAM) {
2281 int i;
2282
2283 if (tp->nvram_lock_cnt == 0) {
2284 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2285 for (i = 0; i < 8000; i++) {
2286 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2287 break;
2288 udelay(20);
2289 }
2290 if (i == 8000) {
2291 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2292 return -ENODEV;
2293 }
2294 }
2295 tp->nvram_lock_cnt++;
2296 }
2297 return 0;
2298}
2299
2300/* tp->lock is held. */
2301static void tg3_nvram_unlock(struct tg3 *tp)
2302{
2303 if (tp->tg3_flags & TG3_FLAG_NVRAM) {
2304 if (tp->nvram_lock_cnt > 0)
2305 tp->nvram_lock_cnt--;
2306 if (tp->nvram_lock_cnt == 0)
2307 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
2308 }
2309}
2310
2311/* tp->lock is held. */
2312static void tg3_enable_nvram_access(struct tg3 *tp)
2313{
2314 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Matt Carlsonf66a29b2009-11-13 13:03:36 +00002315 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002316 u32 nvaccess = tr32(NVRAM_ACCESS);
2317
2318 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
2319 }
2320}
2321
2322/* tp->lock is held. */
2323static void tg3_disable_nvram_access(struct tg3 *tp)
2324{
2325 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Matt Carlsonf66a29b2009-11-13 13:03:36 +00002326 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002327 u32 nvaccess = tr32(NVRAM_ACCESS);
2328
2329 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
2330 }
2331}
2332
2333static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
2334 u32 offset, u32 *val)
2335{
2336 u32 tmp;
2337 int i;
2338
2339 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
2340 return -EINVAL;
2341
2342 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
2343 EEPROM_ADDR_DEVID_MASK |
2344 EEPROM_ADDR_READ);
2345 tw32(GRC_EEPROM_ADDR,
2346 tmp |
2347 (0 << EEPROM_ADDR_DEVID_SHIFT) |
2348 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
2349 EEPROM_ADDR_ADDR_MASK) |
2350 EEPROM_ADDR_READ | EEPROM_ADDR_START);
2351
2352 for (i = 0; i < 1000; i++) {
2353 tmp = tr32(GRC_EEPROM_ADDR);
2354
2355 if (tmp & EEPROM_ADDR_COMPLETE)
2356 break;
2357 msleep(1);
2358 }
2359 if (!(tmp & EEPROM_ADDR_COMPLETE))
2360 return -EBUSY;
2361
Matt Carlson62cedd12009-04-20 14:52:29 -07002362 tmp = tr32(GRC_EEPROM_DATA);
2363
2364 /*
2365 * The data will always be opposite the native endian
2366 * format. Perform a blind byteswap to compensate.
2367 */
2368 *val = swab32(tmp);
2369
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002370 return 0;
2371}
2372
2373#define NVRAM_CMD_TIMEOUT 10000
2374
2375static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
2376{
2377 int i;
2378
2379 tw32(NVRAM_CMD, nvram_cmd);
2380 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
2381 udelay(10);
2382 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
2383 udelay(10);
2384 break;
2385 }
2386 }
2387
2388 if (i == NVRAM_CMD_TIMEOUT)
2389 return -EBUSY;
2390
2391 return 0;
2392}
2393
2394static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
2395{
2396 if ((tp->tg3_flags & TG3_FLAG_NVRAM) &&
2397 (tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) &&
2398 (tp->tg3_flags2 & TG3_FLG2_FLASH) &&
2399 !(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM_ADDR_TRANS) &&
2400 (tp->nvram_jedecnum == JEDEC_ATMEL))
2401
2402 addr = ((addr / tp->nvram_pagesize) <<
2403 ATMEL_AT45DB0X1B_PAGE_POS) +
2404 (addr % tp->nvram_pagesize);
2405
2406 return addr;
2407}
2408
2409static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
2410{
2411 if ((tp->tg3_flags & TG3_FLAG_NVRAM) &&
2412 (tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) &&
2413 (tp->tg3_flags2 & TG3_FLG2_FLASH) &&
2414 !(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM_ADDR_TRANS) &&
2415 (tp->nvram_jedecnum == JEDEC_ATMEL))
2416
2417 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
2418 tp->nvram_pagesize) +
2419 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
2420
2421 return addr;
2422}
2423
Matt Carlsone4f34112009-02-25 14:25:00 +00002424/* NOTE: Data read in from NVRAM is byteswapped according to
2425 * the byteswapping settings for all other register accesses.
2426 * tg3 devices are BE devices, so on a BE machine, the data
2427 * returned will be exactly as it is seen in NVRAM. On a LE
2428 * machine, the 32-bit value will be byteswapped.
2429 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002430static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
2431{
2432 int ret;
2433
2434 if (!(tp->tg3_flags & TG3_FLAG_NVRAM))
2435 return tg3_nvram_read_using_eeprom(tp, offset, val);
2436
2437 offset = tg3_nvram_phys_addr(tp, offset);
2438
2439 if (offset > NVRAM_ADDR_MSK)
2440 return -EINVAL;
2441
2442 ret = tg3_nvram_lock(tp);
2443 if (ret)
2444 return ret;
2445
2446 tg3_enable_nvram_access(tp);
2447
2448 tw32(NVRAM_ADDR, offset);
2449 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
2450 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
2451
2452 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00002453 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002454
2455 tg3_disable_nvram_access(tp);
2456
2457 tg3_nvram_unlock(tp);
2458
2459 return ret;
2460}
2461
Matt Carlsona9dc5292009-02-25 14:25:30 +00002462/* Ensures NVRAM data is in bytestream format. */
2463static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002464{
2465 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00002466 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002467 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00002468 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002469 return res;
2470}
2471
2472/* tp->lock is held. */
Matt Carlson3f007892008-11-03 16:51:36 -08002473static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
2474{
2475 u32 addr_high, addr_low;
2476 int i;
2477
2478 addr_high = ((tp->dev->dev_addr[0] << 8) |
2479 tp->dev->dev_addr[1]);
2480 addr_low = ((tp->dev->dev_addr[2] << 24) |
2481 (tp->dev->dev_addr[3] << 16) |
2482 (tp->dev->dev_addr[4] << 8) |
2483 (tp->dev->dev_addr[5] << 0));
2484 for (i = 0; i < 4; i++) {
2485 if (i == 1 && skip_mac_1)
2486 continue;
2487 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
2488 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
2489 }
2490
2491 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2492 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2493 for (i = 0; i < 12; i++) {
2494 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
2495 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
2496 }
2497 }
2498
2499 addr_high = (tp->dev->dev_addr[0] +
2500 tp->dev->dev_addr[1] +
2501 tp->dev->dev_addr[2] +
2502 tp->dev->dev_addr[3] +
2503 tp->dev->dev_addr[4] +
2504 tp->dev->dev_addr[5]) &
2505 TX_BACKOFF_SEED_MASK;
2506 tw32(MAC_TX_BACKOFF_SEED, addr_high);
2507}
2508
Michael Chanbc1c7562006-03-20 17:48:03 -08002509static int tg3_set_power_state(struct tg3 *tp, pci_power_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510{
2511 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002512 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513
2514 /* Make sure register accesses (indirect or otherwise)
2515 * will function correctly.
2516 */
2517 pci_write_config_dword(tp->pdev,
2518 TG3PCI_MISC_HOST_CTRL,
2519 tp->misc_host_ctrl);
2520
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 switch (state) {
Michael Chanbc1c7562006-03-20 17:48:03 -08002522 case PCI_D0:
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07002523 pci_enable_wake(tp->pdev, state, false);
2524 pci_set_power_state(tp->pdev, PCI_D0);
Michael Chan8c6bda12005-04-21 17:09:08 -07002525
Michael Chan9d26e212006-12-07 00:21:14 -08002526 /* Switch out of Vaux if it is a NIC */
2527 if (tp->tg3_flags2 & TG3_FLG2_IS_NIC)
Michael Chanb401e9e2005-12-19 16:27:04 -08002528 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529
2530 return 0;
2531
Michael Chanbc1c7562006-03-20 17:48:03 -08002532 case PCI_D1:
Michael Chanbc1c7562006-03-20 17:48:03 -08002533 case PCI_D2:
Michael Chanbc1c7562006-03-20 17:48:03 -08002534 case PCI_D3hot:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 break;
2536
2537 default:
Joe Perches05dbe002010-02-17 19:44:19 +00002538 netdev_err(tp->dev, "Invalid power state (D%d) requested\n",
2539 state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002541 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002542
2543 /* Restore the CLKREQ setting. */
2544 if (tp->tg3_flags3 & TG3_FLG3_CLKREQ_BUG) {
2545 u16 lnkctl;
2546
2547 pci_read_config_word(tp->pdev,
2548 tp->pcie_cap + PCI_EXP_LNKCTL,
2549 &lnkctl);
2550 lnkctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
2551 pci_write_config_word(tp->pdev,
2552 tp->pcie_cap + PCI_EXP_LNKCTL,
2553 lnkctl);
2554 }
2555
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
2557 tw32(TG3PCI_MISC_HOST_CTRL,
2558 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
2559
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002560 device_should_wake = pci_pme_capable(tp->pdev, state) &&
2561 device_may_wakeup(&tp->pdev->dev) &&
2562 (tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
2563
Matt Carlsondd477002008-05-25 23:45:58 -07002564 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08002565 do_low_power = false;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002566 if ((tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED) &&
2567 !tp->link_config.phy_is_low_power) {
2568 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002569 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002570
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002571 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002572
2573 tp->link_config.phy_is_low_power = 1;
2574
2575 tp->link_config.orig_speed = phydev->speed;
2576 tp->link_config.orig_duplex = phydev->duplex;
2577 tp->link_config.orig_autoneg = phydev->autoneg;
2578 tp->link_config.orig_advertising = phydev->advertising;
2579
2580 advertising = ADVERTISED_TP |
2581 ADVERTISED_Pause |
2582 ADVERTISED_Autoneg |
2583 ADVERTISED_10baseT_Half;
2584
2585 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002586 device_should_wake) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002587 if (tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB)
2588 advertising |=
2589 ADVERTISED_100baseT_Half |
2590 ADVERTISED_100baseT_Full |
2591 ADVERTISED_10baseT_Full;
2592 else
2593 advertising |= ADVERTISED_10baseT_Full;
2594 }
2595
2596 phydev->advertising = advertising;
2597
2598 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002599
2600 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00002601 if (phyid != PHY_ID_BCMAC131) {
2602 phyid &= PHY_BCM_OUI_MASK;
2603 if (phyid == PHY_BCM_OUI_1 ||
2604 phyid == PHY_BCM_OUI_2 ||
2605 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08002606 do_low_power = true;
2607 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002608 }
Matt Carlsondd477002008-05-25 23:45:58 -07002609 } else {
Matt Carlson20232762008-12-21 20:18:56 -08002610 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002611
Matt Carlsondd477002008-05-25 23:45:58 -07002612 if (tp->link_config.phy_is_low_power == 0) {
2613 tp->link_config.phy_is_low_power = 1;
2614 tp->link_config.orig_speed = tp->link_config.speed;
2615 tp->link_config.orig_duplex = tp->link_config.duplex;
2616 tp->link_config.orig_autoneg = tp->link_config.autoneg;
2617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618
Matt Carlsondd477002008-05-25 23:45:58 -07002619 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)) {
2620 tp->link_config.speed = SPEED_10;
2621 tp->link_config.duplex = DUPLEX_HALF;
2622 tp->link_config.autoneg = AUTONEG_ENABLE;
2623 tg3_setup_phy(tp, 0);
2624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 }
2626
Michael Chanb5d37722006-09-27 16:06:21 -07002627 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
2628 u32 val;
2629
2630 val = tr32(GRC_VCPU_EXT_CTRL);
2631 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
2632 } else if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08002633 int i;
2634 u32 val;
2635
2636 for (i = 0; i < 200; i++) {
2637 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
2638 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
2639 break;
2640 msleep(1);
2641 }
2642 }
Gary Zambranoa85feb82007-05-05 11:52:19 -07002643 if (tp->tg3_flags & TG3_FLAG_WOL_CAP)
2644 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
2645 WOL_DRV_STATE_SHUTDOWN |
2646 WOL_DRV_WOL |
2647 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08002648
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002649 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 u32 mac_mode;
2651
2652 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08002653 if (do_low_power) {
Matt Carlsondd477002008-05-25 23:45:58 -07002654 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x5a);
2655 udelay(40);
2656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657
Michael Chan3f7045c2006-09-27 16:02:29 -07002658 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)
2659 mac_mode = MAC_MODE_PORT_MODE_GMII;
2660 else
2661 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002663 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
2664 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2665 ASIC_REV_5700) {
2666 u32 speed = (tp->tg3_flags &
2667 TG3_FLAG_WOL_SPEED_100MB) ?
2668 SPEED_100 : SPEED_10;
2669 if (tg3_5700_link_polarity(tp, speed))
2670 mac_mode |= MAC_MODE_LINK_POLARITY;
2671 else
2672 mac_mode &= ~MAC_MODE_LINK_POLARITY;
2673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 } else {
2675 mac_mode = MAC_MODE_PORT_MODE_TBI;
2676 }
2677
John W. Linvillecbf46852005-04-21 17:01:29 -07002678 if (!(tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 tw32(MAC_LED_CTRL, tp->led_ctrl);
2680
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002681 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
2682 if (((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
2683 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) &&
2684 ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
2685 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)))
2686 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687
Matt Carlson3bda1252008-08-15 14:08:22 -07002688 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
2689 mac_mode |= tp->mac_mode &
2690 (MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN);
2691 if (mac_mode & MAC_MODE_APE_TX_EN)
2692 mac_mode |= MAC_MODE_TDE_ENABLE;
2693 }
2694
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 tw32_f(MAC_MODE, mac_mode);
2696 udelay(100);
2697
2698 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
2699 udelay(10);
2700 }
2701
2702 if (!(tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB) &&
2703 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2704 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
2705 u32 base_val;
2706
2707 base_val = tp->pci_clock_ctrl;
2708 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
2709 CLOCK_CTRL_TXCLK_DISABLE);
2710
Michael Chanb401e9e2005-12-19 16:27:04 -08002711 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
2712 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Michael Chand7b0a852007-02-13 12:17:38 -08002713 } else if ((tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
Matt Carlson795d01c2007-10-07 23:28:17 -07002714 (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
Michael Chand7b0a852007-02-13 12:17:38 -08002715 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)) {
Michael Chan4cf78e42005-07-25 12:29:19 -07002716 /* do nothing */
Michael Chan85e94ce2005-04-21 17:05:28 -07002717 } else if (!((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF))) {
2719 u32 newbits1, newbits2;
2720
2721 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2722 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2723 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
2724 CLOCK_CTRL_TXCLK_DISABLE |
2725 CLOCK_CTRL_ALTCLK);
2726 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2727 } else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
2728 newbits1 = CLOCK_CTRL_625_CORE;
2729 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
2730 } else {
2731 newbits1 = CLOCK_CTRL_ALTCLK;
2732 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2733 }
2734
Michael Chanb401e9e2005-12-19 16:27:04 -08002735 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
2736 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737
Michael Chanb401e9e2005-12-19 16:27:04 -08002738 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
2739 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740
2741 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
2742 u32 newbits3;
2743
2744 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2745 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2746 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
2747 CLOCK_CTRL_TXCLK_DISABLE |
2748 CLOCK_CTRL_44MHZ_CORE);
2749 } else {
2750 newbits3 = CLOCK_CTRL_44MHZ_CORE;
2751 }
2752
Michael Chanb401e9e2005-12-19 16:27:04 -08002753 tw32_wait_f(TG3PCI_CLOCK_CTRL,
2754 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 }
2756 }
2757
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002758 if (!(device_should_wake) &&
Matt Carlson22435842008-11-21 17:21:13 -08002759 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08002760 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08002761
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 tg3_frob_aux_power(tp);
2763
2764 /* Workaround for unstable PLL clock */
2765 if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
2766 (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
2767 u32 val = tr32(0x7d00);
2768
2769 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
2770 tw32(0x7d00, val);
Michael Chan6921d202005-12-13 21:15:53 -08002771 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08002772 int err;
2773
2774 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08002776 if (!err)
2777 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08002778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 }
2780
Michael Chanbbadf502006-04-06 21:46:34 -07002781 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
2782
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002783 if (device_should_wake)
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07002784 pci_enable_wake(tp->pdev, state, true);
2785
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 /* Finally, set the new power state. */
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07002787 pci_set_power_state(tp->pdev, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 return 0;
2790}
2791
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
2793{
2794 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
2795 case MII_TG3_AUX_STAT_10HALF:
2796 *speed = SPEED_10;
2797 *duplex = DUPLEX_HALF;
2798 break;
2799
2800 case MII_TG3_AUX_STAT_10FULL:
2801 *speed = SPEED_10;
2802 *duplex = DUPLEX_FULL;
2803 break;
2804
2805 case MII_TG3_AUX_STAT_100HALF:
2806 *speed = SPEED_100;
2807 *duplex = DUPLEX_HALF;
2808 break;
2809
2810 case MII_TG3_AUX_STAT_100FULL:
2811 *speed = SPEED_100;
2812 *duplex = DUPLEX_FULL;
2813 break;
2814
2815 case MII_TG3_AUX_STAT_1000HALF:
2816 *speed = SPEED_1000;
2817 *duplex = DUPLEX_HALF;
2818 break;
2819
2820 case MII_TG3_AUX_STAT_1000FULL:
2821 *speed = SPEED_1000;
2822 *duplex = DUPLEX_FULL;
2823 break;
2824
2825 default:
Matt Carlson7f97a4b2009-08-25 10:10:03 +00002826 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07002827 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
2828 SPEED_10;
2829 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
2830 DUPLEX_HALF;
2831 break;
2832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 *speed = SPEED_INVALID;
2834 *duplex = DUPLEX_INVALID;
2835 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837}
2838
2839static void tg3_phy_copper_begin(struct tg3 *tp)
2840{
2841 u32 new_adv;
2842 int i;
2843
2844 if (tp->link_config.phy_is_low_power) {
2845 /* Entering low power mode. Disable gigabit and
2846 * 100baseT advertisements.
2847 */
2848 tg3_writephy(tp, MII_TG3_CTRL, 0);
2849
2850 new_adv = (ADVERTISE_10HALF | ADVERTISE_10FULL |
2851 ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
2852 if (tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB)
2853 new_adv |= (ADVERTISE_100HALF | ADVERTISE_100FULL);
2854
2855 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2856 } else if (tp->link_config.speed == SPEED_INVALID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 if (tp->tg3_flags & TG3_FLAG_10_100_ONLY)
2858 tp->link_config.advertising &=
2859 ~(ADVERTISED_1000baseT_Half |
2860 ADVERTISED_1000baseT_Full);
2861
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002862 new_adv = ADVERTISE_CSMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 if (tp->link_config.advertising & ADVERTISED_10baseT_Half)
2864 new_adv |= ADVERTISE_10HALF;
2865 if (tp->link_config.advertising & ADVERTISED_10baseT_Full)
2866 new_adv |= ADVERTISE_10FULL;
2867 if (tp->link_config.advertising & ADVERTISED_100baseT_Half)
2868 new_adv |= ADVERTISE_100HALF;
2869 if (tp->link_config.advertising & ADVERTISED_100baseT_Full)
2870 new_adv |= ADVERTISE_100FULL;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002871
2872 new_adv |= tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2873
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2875
2876 if (tp->link_config.advertising &
2877 (ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full)) {
2878 new_adv = 0;
2879 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
2880 new_adv |= MII_TG3_CTRL_ADV_1000_HALF;
2881 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
2882 new_adv |= MII_TG3_CTRL_ADV_1000_FULL;
2883 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY) &&
2884 (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2885 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0))
2886 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2887 MII_TG3_CTRL_ENABLE_AS_MASTER);
2888 tg3_writephy(tp, MII_TG3_CTRL, new_adv);
2889 } else {
2890 tg3_writephy(tp, MII_TG3_CTRL, 0);
2891 }
2892 } else {
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002893 new_adv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2894 new_adv |= ADVERTISE_CSMA;
2895
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 /* Asking for a specific link mode. */
2897 if (tp->link_config.speed == SPEED_1000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2899
2900 if (tp->link_config.duplex == DUPLEX_FULL)
2901 new_adv = MII_TG3_CTRL_ADV_1000_FULL;
2902 else
2903 new_adv = MII_TG3_CTRL_ADV_1000_HALF;
2904 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2905 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
2906 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2907 MII_TG3_CTRL_ENABLE_AS_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 if (tp->link_config.speed == SPEED_100) {
2910 if (tp->link_config.duplex == DUPLEX_FULL)
2911 new_adv |= ADVERTISE_100FULL;
2912 else
2913 new_adv |= ADVERTISE_100HALF;
2914 } else {
2915 if (tp->link_config.duplex == DUPLEX_FULL)
2916 new_adv |= ADVERTISE_10FULL;
2917 else
2918 new_adv |= ADVERTISE_10HALF;
2919 }
2920 tg3_writephy(tp, MII_ADVERTISE, new_adv);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002921
2922 new_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002924
2925 tg3_writephy(tp, MII_TG3_CTRL, new_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 }
2927
2928 if (tp->link_config.autoneg == AUTONEG_DISABLE &&
2929 tp->link_config.speed != SPEED_INVALID) {
2930 u32 bmcr, orig_bmcr;
2931
2932 tp->link_config.active_speed = tp->link_config.speed;
2933 tp->link_config.active_duplex = tp->link_config.duplex;
2934
2935 bmcr = 0;
2936 switch (tp->link_config.speed) {
2937 default:
2938 case SPEED_10:
2939 break;
2940
2941 case SPEED_100:
2942 bmcr |= BMCR_SPEED100;
2943 break;
2944
2945 case SPEED_1000:
2946 bmcr |= TG3_BMCR_SPEED1000;
2947 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949
2950 if (tp->link_config.duplex == DUPLEX_FULL)
2951 bmcr |= BMCR_FULLDPLX;
2952
2953 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
2954 (bmcr != orig_bmcr)) {
2955 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
2956 for (i = 0; i < 1500; i++) {
2957 u32 tmp;
2958
2959 udelay(10);
2960 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
2961 tg3_readphy(tp, MII_BMSR, &tmp))
2962 continue;
2963 if (!(tmp & BMSR_LSTATUS)) {
2964 udelay(40);
2965 break;
2966 }
2967 }
2968 tg3_writephy(tp, MII_BMCR, bmcr);
2969 udelay(40);
2970 }
2971 } else {
2972 tg3_writephy(tp, MII_BMCR,
2973 BMCR_ANENABLE | BMCR_ANRESTART);
2974 }
2975}
2976
2977static int tg3_init_5401phy_dsp(struct tg3 *tp)
2978{
2979 int err;
2980
2981 /* Turn off tap power management. */
2982 /* Set Extended packet length bit */
2983 err = tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4c20);
2984
2985 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x0012);
2986 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x1804);
2987
2988 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x0013);
2989 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x1204);
2990
2991 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8006);
2992 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0132);
2993
2994 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8006);
2995 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0232);
2996
2997 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x201f);
2998 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0a20);
2999
3000 udelay(40);
3001
3002 return err;
3003}
3004
Michael Chan3600d912006-12-07 00:21:48 -08003005static int tg3_copper_is_advertising_all(struct tg3 *tp, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006{
Michael Chan3600d912006-12-07 00:21:48 -08003007 u32 adv_reg, all_mask = 0;
3008
3009 if (mask & ADVERTISED_10baseT_Half)
3010 all_mask |= ADVERTISE_10HALF;
3011 if (mask & ADVERTISED_10baseT_Full)
3012 all_mask |= ADVERTISE_10FULL;
3013 if (mask & ADVERTISED_100baseT_Half)
3014 all_mask |= ADVERTISE_100HALF;
3015 if (mask & ADVERTISED_100baseT_Full)
3016 all_mask |= ADVERTISE_100FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017
3018 if (tg3_readphy(tp, MII_ADVERTISE, &adv_reg))
3019 return 0;
3020
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 if ((adv_reg & all_mask) != all_mask)
3022 return 0;
3023 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY)) {
3024 u32 tg3_ctrl;
3025
Michael Chan3600d912006-12-07 00:21:48 -08003026 all_mask = 0;
3027 if (mask & ADVERTISED_1000baseT_Half)
3028 all_mask |= ADVERTISE_1000HALF;
3029 if (mask & ADVERTISED_1000baseT_Full)
3030 all_mask |= ADVERTISE_1000FULL;
3031
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 if (tg3_readphy(tp, MII_TG3_CTRL, &tg3_ctrl))
3033 return 0;
3034
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 if ((tg3_ctrl & all_mask) != all_mask)
3036 return 0;
3037 }
3038 return 1;
3039}
3040
Matt Carlsonef167e22007-12-20 20:10:01 -08003041static int tg3_adv_1000T_flowctrl_ok(struct tg3 *tp, u32 *lcladv, u32 *rmtadv)
3042{
3043 u32 curadv, reqadv;
3044
3045 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
3046 return 1;
3047
3048 curadv = *lcladv & (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
3049 reqadv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
3050
3051 if (tp->link_config.active_duplex == DUPLEX_FULL) {
3052 if (curadv != reqadv)
3053 return 0;
3054
3055 if (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG)
3056 tg3_readphy(tp, MII_LPA, rmtadv);
3057 } else {
3058 /* Reprogram the advertisement register, even if it
3059 * does not affect the current link. If the link
3060 * gets renegotiated in the future, we can save an
3061 * additional renegotiation cycle by advertising
3062 * it correctly in the first place.
3063 */
3064 if (curadv != reqadv) {
3065 *lcladv &= ~(ADVERTISE_PAUSE_CAP |
3066 ADVERTISE_PAUSE_ASYM);
3067 tg3_writephy(tp, MII_ADVERTISE, *lcladv | reqadv);
3068 }
3069 }
3070
3071 return 1;
3072}
3073
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
3075{
3076 int current_link_up;
3077 u32 bmsr, dummy;
Matt Carlsonef167e22007-12-20 20:10:01 -08003078 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 u16 current_speed;
3080 u8 current_duplex;
3081 int i, err;
3082
3083 tw32(MAC_EVENT, 0);
3084
3085 tw32_f(MAC_STATUS,
3086 (MAC_STATUS_SYNC_CHANGED |
3087 MAC_STATUS_CFG_CHANGED |
3088 MAC_STATUS_MI_COMPLETION |
3089 MAC_STATUS_LNKSTATE_CHANGED));
3090 udelay(40);
3091
Matt Carlson8ef21422008-05-02 16:47:53 -07003092 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
3093 tw32_f(MAC_MI_MODE,
3094 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
3095 udelay(80);
3096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097
3098 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x02);
3099
3100 /* Some third-party PHYs need to be reset on link going
3101 * down.
3102 */
3103 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3104 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
3105 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
3106 netif_carrier_ok(tp->dev)) {
3107 tg3_readphy(tp, MII_BMSR, &bmsr);
3108 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3109 !(bmsr & BMSR_LSTATUS))
3110 force_reset = 1;
3111 }
3112 if (force_reset)
3113 tg3_phy_reset(tp);
3114
Matt Carlson79eb6902010-02-17 15:17:03 +00003115 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 tg3_readphy(tp, MII_BMSR, &bmsr);
3117 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
3118 !(tp->tg3_flags & TG3_FLAG_INIT_COMPLETE))
3119 bmsr = 0;
3120
3121 if (!(bmsr & BMSR_LSTATUS)) {
3122 err = tg3_init_5401phy_dsp(tp);
3123 if (err)
3124 return err;
3125
3126 tg3_readphy(tp, MII_BMSR, &bmsr);
3127 for (i = 0; i < 1000; i++) {
3128 udelay(10);
3129 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3130 (bmsr & BMSR_LSTATUS)) {
3131 udelay(40);
3132 break;
3133 }
3134 }
3135
Matt Carlson79eb6902010-02-17 15:17:03 +00003136 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
3137 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 !(bmsr & BMSR_LSTATUS) &&
3139 tp->link_config.active_speed == SPEED_1000) {
3140 err = tg3_phy_reset(tp);
3141 if (!err)
3142 err = tg3_init_5401phy_dsp(tp);
3143 if (err)
3144 return err;
3145 }
3146 }
3147 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3148 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
3149 /* 5701 {A0,B0} CRC bug workaround */
3150 tg3_writephy(tp, 0x15, 0x0a75);
3151 tg3_writephy(tp, 0x1c, 0x8c68);
3152 tg3_writephy(tp, 0x1c, 0x8d68);
3153 tg3_writephy(tp, 0x1c, 0x8c68);
3154 }
3155
3156 /* Clear pending interrupts... */
3157 tg3_readphy(tp, MII_TG3_ISTAT, &dummy);
3158 tg3_readphy(tp, MII_TG3_ISTAT, &dummy);
3159
3160 if (tp->tg3_flags & TG3_FLAG_USE_MI_INTERRUPT)
3161 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlson7f97a4b2009-08-25 10:10:03 +00003162 else if (!(tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 tg3_writephy(tp, MII_TG3_IMASK, ~0);
3164
3165 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3166 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3167 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
3168 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3169 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
3170 else
3171 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
3172 }
3173
3174 current_link_up = 0;
3175 current_speed = SPEED_INVALID;
3176 current_duplex = DUPLEX_INVALID;
3177
3178 if (tp->tg3_flags2 & TG3_FLG2_CAPACITIVE_COUPLING) {
3179 u32 val;
3180
3181 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4007);
3182 tg3_readphy(tp, MII_TG3_AUX_CTRL, &val);
3183 if (!(val & (1 << 10))) {
3184 val |= (1 << 10);
3185 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
3186 goto relink;
3187 }
3188 }
3189
3190 bmsr = 0;
3191 for (i = 0; i < 100; i++) {
3192 tg3_readphy(tp, MII_BMSR, &bmsr);
3193 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3194 (bmsr & BMSR_LSTATUS))
3195 break;
3196 udelay(40);
3197 }
3198
3199 if (bmsr & BMSR_LSTATUS) {
3200 u32 aux_stat, bmcr;
3201
3202 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
3203 for (i = 0; i < 2000; i++) {
3204 udelay(10);
3205 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
3206 aux_stat)
3207 break;
3208 }
3209
3210 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
3211 &current_speed,
3212 &current_duplex);
3213
3214 bmcr = 0;
3215 for (i = 0; i < 200; i++) {
3216 tg3_readphy(tp, MII_BMCR, &bmcr);
3217 if (tg3_readphy(tp, MII_BMCR, &bmcr))
3218 continue;
3219 if (bmcr && bmcr != 0x7fff)
3220 break;
3221 udelay(10);
3222 }
3223
Matt Carlsonef167e22007-12-20 20:10:01 -08003224 lcl_adv = 0;
3225 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226
Matt Carlsonef167e22007-12-20 20:10:01 -08003227 tp->link_config.active_speed = current_speed;
3228 tp->link_config.active_duplex = current_duplex;
3229
3230 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3231 if ((bmcr & BMCR_ANENABLE) &&
3232 tg3_copper_is_advertising_all(tp,
3233 tp->link_config.advertising)) {
3234 if (tg3_adv_1000T_flowctrl_ok(tp, &lcl_adv,
3235 &rmt_adv))
3236 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 }
3238 } else {
3239 if (!(bmcr & BMCR_ANENABLE) &&
3240 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08003241 tp->link_config.duplex == current_duplex &&
3242 tp->link_config.flowctrl ==
3243 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 }
3246 }
3247
Matt Carlsonef167e22007-12-20 20:10:01 -08003248 if (current_link_up == 1 &&
3249 tp->link_config.active_duplex == DUPLEX_FULL)
3250 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 }
3252
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253relink:
Michael Chan6921d202005-12-13 21:15:53 -08003254 if (current_link_up == 0 || tp->link_config.phy_is_low_power) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 u32 tmp;
3256
3257 tg3_phy_copper_begin(tp);
3258
3259 tg3_readphy(tp, MII_BMSR, &tmp);
3260 if (!tg3_readphy(tp, MII_BMSR, &tmp) &&
3261 (tmp & BMSR_LSTATUS))
3262 current_link_up = 1;
3263 }
3264
3265 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
3266 if (current_link_up == 1) {
3267 if (tp->link_config.active_speed == SPEED_100 ||
3268 tp->link_config.active_speed == SPEED_10)
3269 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3270 else
3271 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlson7f97a4b2009-08-25 10:10:03 +00003272 } else if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET)
3273 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3274 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
3276
3277 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
3278 if (tp->link_config.active_duplex == DUPLEX_HALF)
3279 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
3280
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003282 if (current_link_up == 1 &&
3283 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003285 else
3286 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 }
3288
3289 /* ??? Without this setting Netgear GA302T PHY does not
3290 * ??? send/receive packets...
3291 */
Matt Carlson79eb6902010-02-17 15:17:03 +00003292 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
3294 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
3295 tw32_f(MAC_MI_MODE, tp->mi_mode);
3296 udelay(80);
3297 }
3298
3299 tw32_f(MAC_MODE, tp->mac_mode);
3300 udelay(40);
3301
3302 if (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) {
3303 /* Polled via timer. */
3304 tw32_f(MAC_EVENT, 0);
3305 } else {
3306 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
3307 }
3308 udelay(40);
3309
3310 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
3311 current_link_up == 1 &&
3312 tp->link_config.active_speed == SPEED_1000 &&
3313 ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) ||
3314 (tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED))) {
3315 udelay(120);
3316 tw32_f(MAC_STATUS,
3317 (MAC_STATUS_SYNC_CHANGED |
3318 MAC_STATUS_CFG_CHANGED));
3319 udelay(40);
3320 tg3_write_mem(tp,
3321 NIC_SRAM_FIRMWARE_MBOX,
3322 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
3323 }
3324
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003325 /* Prevent send BD corruption. */
3326 if (tp->tg3_flags3 & TG3_FLG3_CLKREQ_BUG) {
3327 u16 oldlnkctl, newlnkctl;
3328
3329 pci_read_config_word(tp->pdev,
3330 tp->pcie_cap + PCI_EXP_LNKCTL,
3331 &oldlnkctl);
3332 if (tp->link_config.active_speed == SPEED_100 ||
3333 tp->link_config.active_speed == SPEED_10)
3334 newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
3335 else
3336 newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
3337 if (newlnkctl != oldlnkctl)
3338 pci_write_config_word(tp->pdev,
3339 tp->pcie_cap + PCI_EXP_LNKCTL,
3340 newlnkctl);
3341 }
3342
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 if (current_link_up != netif_carrier_ok(tp->dev)) {
3344 if (current_link_up)
3345 netif_carrier_on(tp->dev);
3346 else
3347 netif_carrier_off(tp->dev);
3348 tg3_link_report(tp);
3349 }
3350
3351 return 0;
3352}
3353
3354struct tg3_fiber_aneginfo {
3355 int state;
3356#define ANEG_STATE_UNKNOWN 0
3357#define ANEG_STATE_AN_ENABLE 1
3358#define ANEG_STATE_RESTART_INIT 2
3359#define ANEG_STATE_RESTART 3
3360#define ANEG_STATE_DISABLE_LINK_OK 4
3361#define ANEG_STATE_ABILITY_DETECT_INIT 5
3362#define ANEG_STATE_ABILITY_DETECT 6
3363#define ANEG_STATE_ACK_DETECT_INIT 7
3364#define ANEG_STATE_ACK_DETECT 8
3365#define ANEG_STATE_COMPLETE_ACK_INIT 9
3366#define ANEG_STATE_COMPLETE_ACK 10
3367#define ANEG_STATE_IDLE_DETECT_INIT 11
3368#define ANEG_STATE_IDLE_DETECT 12
3369#define ANEG_STATE_LINK_OK 13
3370#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
3371#define ANEG_STATE_NEXT_PAGE_WAIT 15
3372
3373 u32 flags;
3374#define MR_AN_ENABLE 0x00000001
3375#define MR_RESTART_AN 0x00000002
3376#define MR_AN_COMPLETE 0x00000004
3377#define MR_PAGE_RX 0x00000008
3378#define MR_NP_LOADED 0x00000010
3379#define MR_TOGGLE_TX 0x00000020
3380#define MR_LP_ADV_FULL_DUPLEX 0x00000040
3381#define MR_LP_ADV_HALF_DUPLEX 0x00000080
3382#define MR_LP_ADV_SYM_PAUSE 0x00000100
3383#define MR_LP_ADV_ASYM_PAUSE 0x00000200
3384#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
3385#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
3386#define MR_LP_ADV_NEXT_PAGE 0x00001000
3387#define MR_TOGGLE_RX 0x00002000
3388#define MR_NP_RX 0x00004000
3389
3390#define MR_LINK_OK 0x80000000
3391
3392 unsigned long link_time, cur_time;
3393
3394 u32 ability_match_cfg;
3395 int ability_match_count;
3396
3397 char ability_match, idle_match, ack_match;
3398
3399 u32 txconfig, rxconfig;
3400#define ANEG_CFG_NP 0x00000080
3401#define ANEG_CFG_ACK 0x00000040
3402#define ANEG_CFG_RF2 0x00000020
3403#define ANEG_CFG_RF1 0x00000010
3404#define ANEG_CFG_PS2 0x00000001
3405#define ANEG_CFG_PS1 0x00008000
3406#define ANEG_CFG_HD 0x00004000
3407#define ANEG_CFG_FD 0x00002000
3408#define ANEG_CFG_INVAL 0x00001f06
3409
3410};
3411#define ANEG_OK 0
3412#define ANEG_DONE 1
3413#define ANEG_TIMER_ENAB 2
3414#define ANEG_FAILED -1
3415
3416#define ANEG_STATE_SETTLE_TIME 10000
3417
3418static int tg3_fiber_aneg_smachine(struct tg3 *tp,
3419 struct tg3_fiber_aneginfo *ap)
3420{
Matt Carlson5be73b42007-12-20 20:09:29 -08003421 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 unsigned long delta;
3423 u32 rx_cfg_reg;
3424 int ret;
3425
3426 if (ap->state == ANEG_STATE_UNKNOWN) {
3427 ap->rxconfig = 0;
3428 ap->link_time = 0;
3429 ap->cur_time = 0;
3430 ap->ability_match_cfg = 0;
3431 ap->ability_match_count = 0;
3432 ap->ability_match = 0;
3433 ap->idle_match = 0;
3434 ap->ack_match = 0;
3435 }
3436 ap->cur_time++;
3437
3438 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
3439 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
3440
3441 if (rx_cfg_reg != ap->ability_match_cfg) {
3442 ap->ability_match_cfg = rx_cfg_reg;
3443 ap->ability_match = 0;
3444 ap->ability_match_count = 0;
3445 } else {
3446 if (++ap->ability_match_count > 1) {
3447 ap->ability_match = 1;
3448 ap->ability_match_cfg = rx_cfg_reg;
3449 }
3450 }
3451 if (rx_cfg_reg & ANEG_CFG_ACK)
3452 ap->ack_match = 1;
3453 else
3454 ap->ack_match = 0;
3455
3456 ap->idle_match = 0;
3457 } else {
3458 ap->idle_match = 1;
3459 ap->ability_match_cfg = 0;
3460 ap->ability_match_count = 0;
3461 ap->ability_match = 0;
3462 ap->ack_match = 0;
3463
3464 rx_cfg_reg = 0;
3465 }
3466
3467 ap->rxconfig = rx_cfg_reg;
3468 ret = ANEG_OK;
3469
Matt Carlson33f401a2010-04-05 10:19:27 +00003470 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 case ANEG_STATE_UNKNOWN:
3472 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
3473 ap->state = ANEG_STATE_AN_ENABLE;
3474
3475 /* fallthru */
3476 case ANEG_STATE_AN_ENABLE:
3477 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
3478 if (ap->flags & MR_AN_ENABLE) {
3479 ap->link_time = 0;
3480 ap->cur_time = 0;
3481 ap->ability_match_cfg = 0;
3482 ap->ability_match_count = 0;
3483 ap->ability_match = 0;
3484 ap->idle_match = 0;
3485 ap->ack_match = 0;
3486
3487 ap->state = ANEG_STATE_RESTART_INIT;
3488 } else {
3489 ap->state = ANEG_STATE_DISABLE_LINK_OK;
3490 }
3491 break;
3492
3493 case ANEG_STATE_RESTART_INIT:
3494 ap->link_time = ap->cur_time;
3495 ap->flags &= ~(MR_NP_LOADED);
3496 ap->txconfig = 0;
3497 tw32(MAC_TX_AUTO_NEG, 0);
3498 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3499 tw32_f(MAC_MODE, tp->mac_mode);
3500 udelay(40);
3501
3502 ret = ANEG_TIMER_ENAB;
3503 ap->state = ANEG_STATE_RESTART;
3504
3505 /* fallthru */
3506 case ANEG_STATE_RESTART:
3507 delta = ap->cur_time - ap->link_time;
Matt Carlson859a588792010-04-05 10:19:28 +00003508 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a588792010-04-05 10:19:28 +00003510 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 break;
3513
3514 case ANEG_STATE_DISABLE_LINK_OK:
3515 ret = ANEG_DONE;
3516 break;
3517
3518 case ANEG_STATE_ABILITY_DETECT_INIT:
3519 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08003520 ap->txconfig = ANEG_CFG_FD;
3521 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3522 if (flowctrl & ADVERTISE_1000XPAUSE)
3523 ap->txconfig |= ANEG_CFG_PS1;
3524 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3525 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3527 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3528 tw32_f(MAC_MODE, tp->mac_mode);
3529 udelay(40);
3530
3531 ap->state = ANEG_STATE_ABILITY_DETECT;
3532 break;
3533
3534 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a588792010-04-05 10:19:28 +00003535 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 break;
3538
3539 case ANEG_STATE_ACK_DETECT_INIT:
3540 ap->txconfig |= ANEG_CFG_ACK;
3541 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3542 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3543 tw32_f(MAC_MODE, tp->mac_mode);
3544 udelay(40);
3545
3546 ap->state = ANEG_STATE_ACK_DETECT;
3547
3548 /* fallthru */
3549 case ANEG_STATE_ACK_DETECT:
3550 if (ap->ack_match != 0) {
3551 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
3552 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
3553 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
3554 } else {
3555 ap->state = ANEG_STATE_AN_ENABLE;
3556 }
3557 } else if (ap->ability_match != 0 &&
3558 ap->rxconfig == 0) {
3559 ap->state = ANEG_STATE_AN_ENABLE;
3560 }
3561 break;
3562
3563 case ANEG_STATE_COMPLETE_ACK_INIT:
3564 if (ap->rxconfig & ANEG_CFG_INVAL) {
3565 ret = ANEG_FAILED;
3566 break;
3567 }
3568 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
3569 MR_LP_ADV_HALF_DUPLEX |
3570 MR_LP_ADV_SYM_PAUSE |
3571 MR_LP_ADV_ASYM_PAUSE |
3572 MR_LP_ADV_REMOTE_FAULT1 |
3573 MR_LP_ADV_REMOTE_FAULT2 |
3574 MR_LP_ADV_NEXT_PAGE |
3575 MR_TOGGLE_RX |
3576 MR_NP_RX);
3577 if (ap->rxconfig & ANEG_CFG_FD)
3578 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
3579 if (ap->rxconfig & ANEG_CFG_HD)
3580 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
3581 if (ap->rxconfig & ANEG_CFG_PS1)
3582 ap->flags |= MR_LP_ADV_SYM_PAUSE;
3583 if (ap->rxconfig & ANEG_CFG_PS2)
3584 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
3585 if (ap->rxconfig & ANEG_CFG_RF1)
3586 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
3587 if (ap->rxconfig & ANEG_CFG_RF2)
3588 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
3589 if (ap->rxconfig & ANEG_CFG_NP)
3590 ap->flags |= MR_LP_ADV_NEXT_PAGE;
3591
3592 ap->link_time = ap->cur_time;
3593
3594 ap->flags ^= (MR_TOGGLE_TX);
3595 if (ap->rxconfig & 0x0008)
3596 ap->flags |= MR_TOGGLE_RX;
3597 if (ap->rxconfig & ANEG_CFG_NP)
3598 ap->flags |= MR_NP_RX;
3599 ap->flags |= MR_PAGE_RX;
3600
3601 ap->state = ANEG_STATE_COMPLETE_ACK;
3602 ret = ANEG_TIMER_ENAB;
3603 break;
3604
3605 case ANEG_STATE_COMPLETE_ACK:
3606 if (ap->ability_match != 0 &&
3607 ap->rxconfig == 0) {
3608 ap->state = ANEG_STATE_AN_ENABLE;
3609 break;
3610 }
3611 delta = ap->cur_time - ap->link_time;
3612 if (delta > ANEG_STATE_SETTLE_TIME) {
3613 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
3614 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3615 } else {
3616 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
3617 !(ap->flags & MR_NP_RX)) {
3618 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3619 } else {
3620 ret = ANEG_FAILED;
3621 }
3622 }
3623 }
3624 break;
3625
3626 case ANEG_STATE_IDLE_DETECT_INIT:
3627 ap->link_time = ap->cur_time;
3628 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3629 tw32_f(MAC_MODE, tp->mac_mode);
3630 udelay(40);
3631
3632 ap->state = ANEG_STATE_IDLE_DETECT;
3633 ret = ANEG_TIMER_ENAB;
3634 break;
3635
3636 case ANEG_STATE_IDLE_DETECT:
3637 if (ap->ability_match != 0 &&
3638 ap->rxconfig == 0) {
3639 ap->state = ANEG_STATE_AN_ENABLE;
3640 break;
3641 }
3642 delta = ap->cur_time - ap->link_time;
3643 if (delta > ANEG_STATE_SETTLE_TIME) {
3644 /* XXX another gem from the Broadcom driver :( */
3645 ap->state = ANEG_STATE_LINK_OK;
3646 }
3647 break;
3648
3649 case ANEG_STATE_LINK_OK:
3650 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
3651 ret = ANEG_DONE;
3652 break;
3653
3654 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
3655 /* ??? unimplemented */
3656 break;
3657
3658 case ANEG_STATE_NEXT_PAGE_WAIT:
3659 /* ??? unimplemented */
3660 break;
3661
3662 default:
3663 ret = ANEG_FAILED;
3664 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666
3667 return ret;
3668}
3669
Matt Carlson5be73b42007-12-20 20:09:29 -08003670static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671{
3672 int res = 0;
3673 struct tg3_fiber_aneginfo aninfo;
3674 int status = ANEG_FAILED;
3675 unsigned int tick;
3676 u32 tmp;
3677
3678 tw32_f(MAC_TX_AUTO_NEG, 0);
3679
3680 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
3681 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
3682 udelay(40);
3683
3684 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
3685 udelay(40);
3686
3687 memset(&aninfo, 0, sizeof(aninfo));
3688 aninfo.flags |= MR_AN_ENABLE;
3689 aninfo.state = ANEG_STATE_UNKNOWN;
3690 aninfo.cur_time = 0;
3691 tick = 0;
3692 while (++tick < 195000) {
3693 status = tg3_fiber_aneg_smachine(tp, &aninfo);
3694 if (status == ANEG_DONE || status == ANEG_FAILED)
3695 break;
3696
3697 udelay(1);
3698 }
3699
3700 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3701 tw32_f(MAC_MODE, tp->mac_mode);
3702 udelay(40);
3703
Matt Carlson5be73b42007-12-20 20:09:29 -08003704 *txflags = aninfo.txconfig;
3705 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706
3707 if (status == ANEG_DONE &&
3708 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
3709 MR_LP_ADV_FULL_DUPLEX)))
3710 res = 1;
3711
3712 return res;
3713}
3714
3715static void tg3_init_bcm8002(struct tg3 *tp)
3716{
3717 u32 mac_status = tr32(MAC_STATUS);
3718 int i;
3719
3720 /* Reset when initting first time or we have a link. */
3721 if ((tp->tg3_flags & TG3_FLAG_INIT_COMPLETE) &&
3722 !(mac_status & MAC_STATUS_PCS_SYNCED))
3723 return;
3724
3725 /* Set PLL lock range. */
3726 tg3_writephy(tp, 0x16, 0x8007);
3727
3728 /* SW reset */
3729 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
3730
3731 /* Wait for reset to complete. */
3732 /* XXX schedule_timeout() ... */
3733 for (i = 0; i < 500; i++)
3734 udelay(10);
3735
3736 /* Config mode; select PMA/Ch 1 regs. */
3737 tg3_writephy(tp, 0x10, 0x8411);
3738
3739 /* Enable auto-lock and comdet, select txclk for tx. */
3740 tg3_writephy(tp, 0x11, 0x0a10);
3741
3742 tg3_writephy(tp, 0x18, 0x00a0);
3743 tg3_writephy(tp, 0x16, 0x41ff);
3744
3745 /* Assert and deassert POR. */
3746 tg3_writephy(tp, 0x13, 0x0400);
3747 udelay(40);
3748 tg3_writephy(tp, 0x13, 0x0000);
3749
3750 tg3_writephy(tp, 0x11, 0x0a50);
3751 udelay(40);
3752 tg3_writephy(tp, 0x11, 0x0a10);
3753
3754 /* Wait for signal to stabilize */
3755 /* XXX schedule_timeout() ... */
3756 for (i = 0; i < 15000; i++)
3757 udelay(10);
3758
3759 /* Deselect the channel register so we can read the PHYID
3760 * later.
3761 */
3762 tg3_writephy(tp, 0x10, 0x8011);
3763}
3764
3765static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
3766{
Matt Carlson82cd3d12007-12-20 20:09:00 -08003767 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 u32 sg_dig_ctrl, sg_dig_status;
3769 u32 serdes_cfg, expected_sg_dig_ctrl;
3770 int workaround, port_a;
3771 int current_link_up;
3772
3773 serdes_cfg = 0;
3774 expected_sg_dig_ctrl = 0;
3775 workaround = 0;
3776 port_a = 1;
3777 current_link_up = 0;
3778
3779 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
3780 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
3781 workaround = 1;
3782 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
3783 port_a = 0;
3784
3785 /* preserve bits 0-11,13,14 for signal pre-emphasis */
3786 /* preserve bits 20-23 for voltage regulator */
3787 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
3788 }
3789
3790 sg_dig_ctrl = tr32(SG_DIG_CTRL);
3791
3792 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003793 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 if (workaround) {
3795 u32 val = serdes_cfg;
3796
3797 if (port_a)
3798 val |= 0xc010000;
3799 else
3800 val |= 0x4010000;
3801 tw32_f(MAC_SERDES_CFG, val);
3802 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003803
3804 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 }
3806 if (mac_status & MAC_STATUS_PCS_SYNCED) {
3807 tg3_setup_flow_control(tp, 0, 0);
3808 current_link_up = 1;
3809 }
3810 goto out;
3811 }
3812
3813 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003814 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815
Matt Carlson82cd3d12007-12-20 20:09:00 -08003816 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3817 if (flowctrl & ADVERTISE_1000XPAUSE)
3818 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
3819 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3820 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821
3822 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003823 if ((tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT) &&
3824 tp->serdes_counter &&
3825 ((mac_status & (MAC_STATUS_PCS_SYNCED |
3826 MAC_STATUS_RCVD_CFG)) ==
3827 MAC_STATUS_PCS_SYNCED)) {
3828 tp->serdes_counter--;
3829 current_link_up = 1;
3830 goto out;
3831 }
3832restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 if (workaround)
3834 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003835 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 udelay(5);
3837 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
3838
Michael Chan3d3ebe72006-09-27 15:59:15 -07003839 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
3840 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
3842 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003843 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 mac_status = tr32(MAC_STATUS);
3845
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003846 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08003848 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849
Matt Carlson82cd3d12007-12-20 20:09:00 -08003850 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
3851 local_adv |= ADVERTISE_1000XPAUSE;
3852 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
3853 local_adv |= ADVERTISE_1000XPSE_ASYM;
3854
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003855 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003856 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003857 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003858 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859
3860 tg3_setup_flow_control(tp, local_adv, remote_adv);
3861 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003862 tp->serdes_counter = 0;
3863 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003864 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003865 if (tp->serdes_counter)
3866 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 else {
3868 if (workaround) {
3869 u32 val = serdes_cfg;
3870
3871 if (port_a)
3872 val |= 0xc010000;
3873 else
3874 val |= 0x4010000;
3875
3876 tw32_f(MAC_SERDES_CFG, val);
3877 }
3878
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003879 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 udelay(40);
3881
3882 /* Link parallel detection - link is up */
3883 /* only if we have PCS_SYNC and not */
3884 /* receiving config code words */
3885 mac_status = tr32(MAC_STATUS);
3886 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
3887 !(mac_status & MAC_STATUS_RCVD_CFG)) {
3888 tg3_setup_flow_control(tp, 0, 0);
3889 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003890 tp->tg3_flags2 |=
3891 TG3_FLG2_PARALLEL_DETECT;
3892 tp->serdes_counter =
3893 SERDES_PARALLEL_DET_TIMEOUT;
3894 } else
3895 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 }
3897 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07003898 } else {
3899 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
3900 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 }
3902
3903out:
3904 return current_link_up;
3905}
3906
3907static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
3908{
3909 int current_link_up = 0;
3910
Michael Chan5cf64b8a2007-05-05 12:11:21 -07003911 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913
3914 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08003915 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003917
Matt Carlson5be73b42007-12-20 20:09:29 -08003918 if (fiber_autoneg(tp, &txflags, &rxflags)) {
3919 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920
Matt Carlson5be73b42007-12-20 20:09:29 -08003921 if (txflags & ANEG_CFG_PS1)
3922 local_adv |= ADVERTISE_1000XPAUSE;
3923 if (txflags & ANEG_CFG_PS2)
3924 local_adv |= ADVERTISE_1000XPSE_ASYM;
3925
3926 if (rxflags & MR_LP_ADV_SYM_PAUSE)
3927 remote_adv |= LPA_1000XPAUSE;
3928 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
3929 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930
3931 tg3_setup_flow_control(tp, local_adv, remote_adv);
3932
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 current_link_up = 1;
3934 }
3935 for (i = 0; i < 30; i++) {
3936 udelay(20);
3937 tw32_f(MAC_STATUS,
3938 (MAC_STATUS_SYNC_CHANGED |
3939 MAC_STATUS_CFG_CHANGED));
3940 udelay(40);
3941 if ((tr32(MAC_STATUS) &
3942 (MAC_STATUS_SYNC_CHANGED |
3943 MAC_STATUS_CFG_CHANGED)) == 0)
3944 break;
3945 }
3946
3947 mac_status = tr32(MAC_STATUS);
3948 if (current_link_up == 0 &&
3949 (mac_status & MAC_STATUS_PCS_SYNCED) &&
3950 !(mac_status & MAC_STATUS_RCVD_CFG))
3951 current_link_up = 1;
3952 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08003953 tg3_setup_flow_control(tp, 0, 0);
3954
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 /* Forcing 1000FD link up. */
3956 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957
3958 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
3959 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003960
3961 tw32_f(MAC_MODE, tp->mac_mode);
3962 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 }
3964
3965out:
3966 return current_link_up;
3967}
3968
3969static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
3970{
3971 u32 orig_pause_cfg;
3972 u16 orig_active_speed;
3973 u8 orig_active_duplex;
3974 u32 mac_status;
3975 int current_link_up;
3976 int i;
3977
Matt Carlson8d018622007-12-20 20:05:44 -08003978 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 orig_active_speed = tp->link_config.active_speed;
3980 orig_active_duplex = tp->link_config.active_duplex;
3981
3982 if (!(tp->tg3_flags2 & TG3_FLG2_HW_AUTONEG) &&
3983 netif_carrier_ok(tp->dev) &&
3984 (tp->tg3_flags & TG3_FLAG_INIT_COMPLETE)) {
3985 mac_status = tr32(MAC_STATUS);
3986 mac_status &= (MAC_STATUS_PCS_SYNCED |
3987 MAC_STATUS_SIGNAL_DET |
3988 MAC_STATUS_CFG_CHANGED |
3989 MAC_STATUS_RCVD_CFG);
3990 if (mac_status == (MAC_STATUS_PCS_SYNCED |
3991 MAC_STATUS_SIGNAL_DET)) {
3992 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
3993 MAC_STATUS_CFG_CHANGED));
3994 return 0;
3995 }
3996 }
3997
3998 tw32_f(MAC_TX_AUTO_NEG, 0);
3999
4000 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
4001 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
4002 tw32_f(MAC_MODE, tp->mac_mode);
4003 udelay(40);
4004
Matt Carlson79eb6902010-02-17 15:17:03 +00004005 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 tg3_init_bcm8002(tp);
4007
4008 /* Enable link change event even when serdes polling. */
4009 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4010 udelay(40);
4011
4012 current_link_up = 0;
4013 mac_status = tr32(MAC_STATUS);
4014
4015 if (tp->tg3_flags2 & TG3_FLG2_HW_AUTONEG)
4016 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
4017 else
4018 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
4019
Matt Carlson898a56f2009-08-28 14:02:40 +00004020 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00004022 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023
4024 for (i = 0; i < 100; i++) {
4025 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4026 MAC_STATUS_CFG_CHANGED));
4027 udelay(5);
4028 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07004029 MAC_STATUS_CFG_CHANGED |
4030 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 break;
4032 }
4033
4034 mac_status = tr32(MAC_STATUS);
4035 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
4036 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004037 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
4038 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 tw32_f(MAC_MODE, (tp->mac_mode |
4040 MAC_MODE_SEND_CONFIGS));
4041 udelay(1);
4042 tw32_f(MAC_MODE, tp->mac_mode);
4043 }
4044 }
4045
4046 if (current_link_up == 1) {
4047 tp->link_config.active_speed = SPEED_1000;
4048 tp->link_config.active_duplex = DUPLEX_FULL;
4049 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4050 LED_CTRL_LNKLED_OVERRIDE |
4051 LED_CTRL_1000MBPS_ON));
4052 } else {
4053 tp->link_config.active_speed = SPEED_INVALID;
4054 tp->link_config.active_duplex = DUPLEX_INVALID;
4055 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4056 LED_CTRL_LNKLED_OVERRIDE |
4057 LED_CTRL_TRAFFIC_OVERRIDE));
4058 }
4059
4060 if (current_link_up != netif_carrier_ok(tp->dev)) {
4061 if (current_link_up)
4062 netif_carrier_on(tp->dev);
4063 else
4064 netif_carrier_off(tp->dev);
4065 tg3_link_report(tp);
4066 } else {
Matt Carlson8d018622007-12-20 20:05:44 -08004067 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 if (orig_pause_cfg != now_pause_cfg ||
4069 orig_active_speed != tp->link_config.active_speed ||
4070 orig_active_duplex != tp->link_config.active_duplex)
4071 tg3_link_report(tp);
4072 }
4073
4074 return 0;
4075}
4076
Michael Chan747e8f82005-07-25 12:33:22 -07004077static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
4078{
4079 int current_link_up, err = 0;
4080 u32 bmsr, bmcr;
4081 u16 current_speed;
4082 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08004083 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07004084
4085 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4086 tw32_f(MAC_MODE, tp->mac_mode);
4087 udelay(40);
4088
4089 tw32(MAC_EVENT, 0);
4090
4091 tw32_f(MAC_STATUS,
4092 (MAC_STATUS_SYNC_CHANGED |
4093 MAC_STATUS_CFG_CHANGED |
4094 MAC_STATUS_MI_COMPLETION |
4095 MAC_STATUS_LNKSTATE_CHANGED));
4096 udelay(40);
4097
4098 if (force_reset)
4099 tg3_phy_reset(tp);
4100
4101 current_link_up = 0;
4102 current_speed = SPEED_INVALID;
4103 current_duplex = DUPLEX_INVALID;
4104
4105 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4106 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004107 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
4108 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4109 bmsr |= BMSR_LSTATUS;
4110 else
4111 bmsr &= ~BMSR_LSTATUS;
4112 }
Michael Chan747e8f82005-07-25 12:33:22 -07004113
4114 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
4115
4116 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlson2bd3ed02008-06-09 15:39:55 -07004117 (tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004118 /* do nothing, just check for link up at the end */
4119 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4120 u32 adv, new_adv;
4121
4122 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4123 new_adv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
4124 ADVERTISE_1000XPAUSE |
4125 ADVERTISE_1000XPSE_ASYM |
4126 ADVERTISE_SLCT);
4127
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004128 new_adv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Michael Chan747e8f82005-07-25 12:33:22 -07004129
4130 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
4131 new_adv |= ADVERTISE_1000XHALF;
4132 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
4133 new_adv |= ADVERTISE_1000XFULL;
4134
4135 if ((new_adv != adv) || !(bmcr & BMCR_ANENABLE)) {
4136 tg3_writephy(tp, MII_ADVERTISE, new_adv);
4137 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
4138 tg3_writephy(tp, MII_BMCR, bmcr);
4139
4140 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07004141 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Michael Chan747e8f82005-07-25 12:33:22 -07004142 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
4143
4144 return err;
4145 }
4146 } else {
4147 u32 new_bmcr;
4148
4149 bmcr &= ~BMCR_SPEED1000;
4150 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
4151
4152 if (tp->link_config.duplex == DUPLEX_FULL)
4153 new_bmcr |= BMCR_FULLDPLX;
4154
4155 if (new_bmcr != bmcr) {
4156 /* BMCR_SPEED1000 is a reserved bit that needs
4157 * to be set on write.
4158 */
4159 new_bmcr |= BMCR_SPEED1000;
4160
4161 /* Force a linkdown */
4162 if (netif_carrier_ok(tp->dev)) {
4163 u32 adv;
4164
4165 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4166 adv &= ~(ADVERTISE_1000XFULL |
4167 ADVERTISE_1000XHALF |
4168 ADVERTISE_SLCT);
4169 tg3_writephy(tp, MII_ADVERTISE, adv);
4170 tg3_writephy(tp, MII_BMCR, bmcr |
4171 BMCR_ANRESTART |
4172 BMCR_ANENABLE);
4173 udelay(10);
4174 netif_carrier_off(tp->dev);
4175 }
4176 tg3_writephy(tp, MII_BMCR, new_bmcr);
4177 bmcr = new_bmcr;
4178 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4179 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004180 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
4181 ASIC_REV_5714) {
4182 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4183 bmsr |= BMSR_LSTATUS;
4184 else
4185 bmsr &= ~BMSR_LSTATUS;
4186 }
Michael Chan747e8f82005-07-25 12:33:22 -07004187 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
4188 }
4189 }
4190
4191 if (bmsr & BMSR_LSTATUS) {
4192 current_speed = SPEED_1000;
4193 current_link_up = 1;
4194 if (bmcr & BMCR_FULLDPLX)
4195 current_duplex = DUPLEX_FULL;
4196 else
4197 current_duplex = DUPLEX_HALF;
4198
Matt Carlsonef167e22007-12-20 20:10:01 -08004199 local_adv = 0;
4200 remote_adv = 0;
4201
Michael Chan747e8f82005-07-25 12:33:22 -07004202 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08004203 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07004204
4205 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
4206 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
4207 common = local_adv & remote_adv;
4208 if (common & (ADVERTISE_1000XHALF |
4209 ADVERTISE_1000XFULL)) {
4210 if (common & ADVERTISE_1000XFULL)
4211 current_duplex = DUPLEX_FULL;
4212 else
4213 current_duplex = DUPLEX_HALF;
Matt Carlson57d8b882010-06-05 17:24:35 +00004214 } else if (!(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
4215 /* Link is up via parallel detect */
Matt Carlson859a588792010-04-05 10:19:28 +00004216 } else {
Michael Chan747e8f82005-07-25 12:33:22 -07004217 current_link_up = 0;
Matt Carlson859a588792010-04-05 10:19:28 +00004218 }
Michael Chan747e8f82005-07-25 12:33:22 -07004219 }
4220 }
4221
Matt Carlsonef167e22007-12-20 20:10:01 -08004222 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
4223 tg3_setup_flow_control(tp, local_adv, remote_adv);
4224
Michael Chan747e8f82005-07-25 12:33:22 -07004225 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4226 if (tp->link_config.active_duplex == DUPLEX_HALF)
4227 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4228
4229 tw32_f(MAC_MODE, tp->mac_mode);
4230 udelay(40);
4231
4232 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4233
4234 tp->link_config.active_speed = current_speed;
4235 tp->link_config.active_duplex = current_duplex;
4236
4237 if (current_link_up != netif_carrier_ok(tp->dev)) {
4238 if (current_link_up)
4239 netif_carrier_on(tp->dev);
4240 else {
4241 netif_carrier_off(tp->dev);
4242 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
4243 }
4244 tg3_link_report(tp);
4245 }
4246 return err;
4247}
4248
4249static void tg3_serdes_parallel_detect(struct tg3 *tp)
4250{
Michael Chan3d3ebe72006-09-27 15:59:15 -07004251 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07004252 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07004253 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07004254 return;
4255 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00004256
Michael Chan747e8f82005-07-25 12:33:22 -07004257 if (!netif_carrier_ok(tp->dev) &&
4258 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
4259 u32 bmcr;
4260
4261 tg3_readphy(tp, MII_BMCR, &bmcr);
4262 if (bmcr & BMCR_ANENABLE) {
4263 u32 phy1, phy2;
4264
4265 /* Select shadow register 0x1f */
4266 tg3_writephy(tp, 0x1c, 0x7c00);
4267 tg3_readphy(tp, 0x1c, &phy1);
4268
4269 /* Select expansion interrupt status register */
4270 tg3_writephy(tp, 0x17, 0x0f01);
4271 tg3_readphy(tp, 0x15, &phy2);
4272 tg3_readphy(tp, 0x15, &phy2);
4273
4274 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
4275 /* We have signal detect and not receiving
4276 * config code words, link is up by parallel
4277 * detection.
4278 */
4279
4280 bmcr &= ~BMCR_ANENABLE;
4281 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
4282 tg3_writephy(tp, MII_BMCR, bmcr);
4283 tp->tg3_flags2 |= TG3_FLG2_PARALLEL_DETECT;
4284 }
4285 }
Matt Carlson859a588792010-04-05 10:19:28 +00004286 } else if (netif_carrier_ok(tp->dev) &&
4287 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
4288 (tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004289 u32 phy2;
4290
4291 /* Select expansion interrupt status register */
4292 tg3_writephy(tp, 0x17, 0x0f01);
4293 tg3_readphy(tp, 0x15, &phy2);
4294 if (phy2 & 0x20) {
4295 u32 bmcr;
4296
4297 /* Config code words received, turn on autoneg. */
4298 tg3_readphy(tp, MII_BMCR, &bmcr);
4299 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
4300
4301 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
4302
4303 }
4304 }
4305}
4306
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307static int tg3_setup_phy(struct tg3 *tp, int force_reset)
4308{
4309 int err;
4310
Matt Carlson859a588792010-04-05 10:19:28 +00004311 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlson859a588792010-04-05 10:19:28 +00004313 else if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07004314 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a588792010-04-05 10:19:28 +00004315 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317
Matt Carlsonbcb37f62008-11-03 16:52:09 -08004318 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08004319 u32 val, scale;
4320
4321 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
4322 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
4323 scale = 65;
4324 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
4325 scale = 6;
4326 else
4327 scale = 12;
4328
4329 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
4330 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
4331 tw32(GRC_MISC_CFG, val);
4332 }
4333
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 if (tp->link_config.active_speed == SPEED_1000 &&
4335 tp->link_config.active_duplex == DUPLEX_HALF)
4336 tw32(MAC_TX_LENGTHS,
4337 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4338 (6 << TX_LENGTHS_IPG_SHIFT) |
4339 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
4340 else
4341 tw32(MAC_TX_LENGTHS,
4342 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4343 (6 << TX_LENGTHS_IPG_SHIFT) |
4344 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
4345
4346 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
4347 if (netif_carrier_ok(tp->dev)) {
4348 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07004349 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 } else {
4351 tw32(HOSTCC_STAT_COAL_TICKS, 0);
4352 }
4353 }
4354
Matt Carlson8ed5d972007-05-07 00:25:49 -07004355 if (tp->tg3_flags & TG3_FLAG_ASPM_WORKAROUND) {
4356 u32 val = tr32(PCIE_PWR_MGMT_THRESH);
4357 if (!netif_carrier_ok(tp->dev))
4358 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
4359 tp->pwrmgmt_thresh;
4360 else
4361 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
4362 tw32(PCIE_PWR_MGMT_THRESH, val);
4363 }
4364
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 return err;
4366}
4367
Michael Chandf3e6542006-05-26 17:48:07 -07004368/* This is called whenever we suspect that the system chipset is re-
4369 * ordering the sequence of MMIO to the tx send mailbox. The symptom
4370 * is bogus tx completions. We try to recover by setting the
4371 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
4372 * in the workqueue.
4373 */
4374static void tg3_tx_recover(struct tg3 *tp)
4375{
4376 BUG_ON((tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER) ||
4377 tp->write32_tx_mbox == tg3_write_indirect_mbox);
4378
Matt Carlson5129c3a2010-04-05 10:19:23 +00004379 netdev_warn(tp->dev,
4380 "The system may be re-ordering memory-mapped I/O "
4381 "cycles to the network device, attempting to recover. "
4382 "Please report the problem to the driver maintainer "
4383 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07004384
4385 spin_lock(&tp->lock);
Michael Chandf3e6542006-05-26 17:48:07 -07004386 tp->tg3_flags |= TG3_FLAG_TX_RECOVERY_PENDING;
Michael Chandf3e6542006-05-26 17:48:07 -07004387 spin_unlock(&tp->lock);
4388}
4389
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004390static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07004391{
4392 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004393 return tnapi->tx_pending -
4394 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07004395}
4396
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397/* Tigon3 never reports partial packet sends. So we do not
4398 * need special logic to handle SKBs that have not had all
4399 * of their frags sent yet, like SunGEM does.
4400 */
Matt Carlson17375d22009-08-28 14:02:18 +00004401static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402{
Matt Carlson17375d22009-08-28 14:02:18 +00004403 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00004404 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004405 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004406 struct netdev_queue *txq;
4407 int index = tnapi - tp->napi;
4408
Matt Carlson19cfaec2009-12-03 08:36:20 +00004409 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004410 index--;
4411
4412 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413
4414 while (sw_idx != hw_idx) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00004415 struct ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07004417 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418
Michael Chandf3e6542006-05-26 17:48:07 -07004419 if (unlikely(skb == NULL)) {
4420 tg3_tx_recover(tp);
4421 return;
4422 }
4423
Alexander Duyckf4188d82009-12-02 16:48:38 +00004424 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004425 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00004426 skb_headlen(skb),
4427 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428
4429 ri->skb = NULL;
4430
4431 sw_idx = NEXT_TX(sw_idx);
4432
4433 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004434 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07004435 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
4436 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00004437
4438 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004439 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00004440 skb_shinfo(skb)->frags[i].size,
4441 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 sw_idx = NEXT_TX(sw_idx);
4443 }
4444
David S. Millerf47c11e2005-06-24 20:18:35 -07004445 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07004446
4447 if (unlikely(tx_bug)) {
4448 tg3_tx_recover(tp);
4449 return;
4450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 }
4452
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004453 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454
Michael Chan1b2a7202006-08-07 21:46:02 -07004455 /* Need to make the tx_cons update visible to tg3_start_xmit()
4456 * before checking for netif_queue_stopped(). Without the
4457 * memory barrier, there is a small possibility that tg3_start_xmit()
4458 * will miss it and cause the queue to be stopped forever.
4459 */
4460 smp_mb();
4461
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004462 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004463 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004464 __netif_tx_lock(txq, smp_processor_id());
4465 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004466 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004467 netif_tx_wake_queue(txq);
4468 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07004469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470}
4471
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004472static void tg3_rx_skb_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
4473{
4474 if (!ri->skb)
4475 return;
4476
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004477 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004478 map_sz, PCI_DMA_FROMDEVICE);
4479 dev_kfree_skb_any(ri->skb);
4480 ri->skb = NULL;
4481}
4482
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483/* Returns size of skb allocated or < 0 on error.
4484 *
4485 * We only need to fill in the address because the other members
4486 * of the RX descriptor are invariant, see tg3_init_rings.
4487 *
4488 * Note the purposeful assymetry of cpu vs. chip accesses. For
4489 * posting buffers we only dirty the first cache line of the RX
4490 * descriptor (containing the address). Whereas for the RX status
4491 * buffers the cpu only reads the last cacheline of the RX descriptor
4492 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
4493 */
Matt Carlson86b21e52009-11-13 13:03:45 +00004494static int tg3_alloc_rx_skb(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Matt Carlsona3896162009-11-13 13:03:44 +00004495 u32 opaque_key, u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496{
4497 struct tg3_rx_buffer_desc *desc;
4498 struct ring_info *map, *src_map;
4499 struct sk_buff *skb;
4500 dma_addr_t mapping;
4501 int skb_size, dest_idx;
4502
4503 src_map = NULL;
4504 switch (opaque_key) {
4505 case RXD_OPAQUE_RING_STD:
4506 dest_idx = dest_idx_unmasked % TG3_RX_RING_SIZE;
Matt Carlson21f581a2009-08-28 14:00:25 +00004507 desc = &tpr->rx_std[dest_idx];
4508 map = &tpr->rx_std_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004509 skb_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 break;
4511
4512 case RXD_OPAQUE_RING_JUMBO:
4513 dest_idx = dest_idx_unmasked % TG3_RX_JUMBO_RING_SIZE;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00004514 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00004515 map = &tpr->rx_jmb_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004516 skb_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517 break;
4518
4519 default:
4520 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522
4523 /* Do not overwrite any of the map or rp information
4524 * until we are sure we can commit to a new buffer.
4525 *
4526 * Callers depend upon this behavior and assume that
4527 * we leave everything unchanged if we fail.
4528 */
Matt Carlson287be122009-08-28 13:58:46 +00004529 skb = netdev_alloc_skb(tp->dev, skb_size + tp->rx_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530 if (skb == NULL)
4531 return -ENOMEM;
4532
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 skb_reserve(skb, tp->rx_offset);
4534
Matt Carlson287be122009-08-28 13:58:46 +00004535 mapping = pci_map_single(tp->pdev, skb->data, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536 PCI_DMA_FROMDEVICE);
Matt Carlsona21771d2009-11-02 14:25:31 +00004537 if (pci_dma_mapping_error(tp->pdev, mapping)) {
4538 dev_kfree_skb(skb);
4539 return -EIO;
4540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541
4542 map->skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004543 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545 desc->addr_hi = ((u64)mapping >> 32);
4546 desc->addr_lo = ((u64)mapping & 0xffffffff);
4547
4548 return skb_size;
4549}
4550
4551/* We only need to move over in the address because the other
4552 * members of the RX descriptor are invariant. See notes above
4553 * tg3_alloc_rx_skb for full details.
4554 */
Matt Carlsona3896162009-11-13 13:03:44 +00004555static void tg3_recycle_rx(struct tg3_napi *tnapi,
4556 struct tg3_rx_prodring_set *dpr,
4557 u32 opaque_key, int src_idx,
4558 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559{
Matt Carlson17375d22009-08-28 14:02:18 +00004560 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
4562 struct ring_info *src_map, *dest_map;
Matt Carlsona3896162009-11-13 13:03:44 +00004563 struct tg3_rx_prodring_set *spr = &tp->prodring[0];
Matt Carlsonc6cdf432010-04-05 10:19:26 +00004564 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565
4566 switch (opaque_key) {
4567 case RXD_OPAQUE_RING_STD:
4568 dest_idx = dest_idx_unmasked % TG3_RX_RING_SIZE;
Matt Carlsona3896162009-11-13 13:03:44 +00004569 dest_desc = &dpr->rx_std[dest_idx];
4570 dest_map = &dpr->rx_std_buffers[dest_idx];
4571 src_desc = &spr->rx_std[src_idx];
4572 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573 break;
4574
4575 case RXD_OPAQUE_RING_JUMBO:
4576 dest_idx = dest_idx_unmasked % TG3_RX_JUMBO_RING_SIZE;
Matt Carlsona3896162009-11-13 13:03:44 +00004577 dest_desc = &dpr->rx_jmb[dest_idx].std;
4578 dest_map = &dpr->rx_jmb_buffers[dest_idx];
4579 src_desc = &spr->rx_jmb[src_idx].std;
4580 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 break;
4582
4583 default:
4584 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586
4587 dest_map->skb = src_map->skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004588 dma_unmap_addr_set(dest_map, mapping,
4589 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 dest_desc->addr_hi = src_desc->addr_hi;
4591 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00004592
4593 /* Ensure that the update to the skb happens after the physical
4594 * addresses have been transferred to the new BD location.
4595 */
4596 smp_wmb();
4597
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598 src_map->skb = NULL;
4599}
4600
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601/* The RX ring scheme is composed of multiple rings which post fresh
4602 * buffers to the chip, and one special ring the chip uses to report
4603 * status back to the host.
4604 *
4605 * The special ring reports the status of received packets to the
4606 * host. The chip does not write into the original descriptor the
4607 * RX buffer was obtained from. The chip simply takes the original
4608 * descriptor as provided by the host, updates the status and length
4609 * field, then writes this into the next status ring entry.
4610 *
4611 * Each ring the host uses to post buffers to the chip is described
4612 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
4613 * it is first placed into the on-chip ram. When the packet's length
4614 * is known, it walks down the TG3_BDINFO entries to select the ring.
4615 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
4616 * which is within the range of the new packet's length is chosen.
4617 *
4618 * The "separate ring for rx status" scheme may sound queer, but it makes
4619 * sense from a cache coherency perspective. If only the host writes
4620 * to the buffer post rings, and only the chip writes to the rx status
4621 * rings, then cache lines never move beyond shared-modified state.
4622 * If both the host and chip were to write into the same ring, cache line
4623 * eviction could occur since both entities want it in an exclusive state.
4624 */
Matt Carlson17375d22009-08-28 14:02:18 +00004625static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626{
Matt Carlson17375d22009-08-28 14:02:18 +00004627 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07004628 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00004629 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00004630 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07004631 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632 int received;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004633 struct tg3_rx_prodring_set *tpr = tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004635 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636 /*
4637 * We need to order the read of hw_idx and the read of
4638 * the opaque cookie.
4639 */
4640 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 work_mask = 0;
4642 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00004643 std_prod_idx = tpr->rx_std_prod_idx;
4644 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00004646 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00004647 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 unsigned int len;
4649 struct sk_buff *skb;
4650 dma_addr_t dma_addr;
4651 u32 opaque_key, desc_idx, *post_ptr;
Matt Carlson9dc7a112010-04-12 06:58:28 +00004652 bool hw_vlan __maybe_unused = false;
4653 u16 vtag __maybe_unused = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654
4655 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
4656 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
4657 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004658 ri = &tp->prodring[0].rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004659 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00004660 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00004661 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07004662 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004664 ri = &tp->prodring[0].rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004665 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00004666 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00004667 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00004668 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670
4671 work_mask |= opaque_key;
4672
4673 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
4674 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
4675 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00004676 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677 desc_idx, *post_ptr);
4678 drop_it_no_recycle:
4679 /* Other statistics kept track of by card. */
4680 tp->net_stats.rx_dropped++;
4681 goto next_pkt;
4682 }
4683
Matt Carlsonad829262008-11-21 17:16:16 -08004684 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
4685 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686
Matt Carlsond2757fc2010-04-12 06:58:27 +00004687 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 int skb_size;
4689
Matt Carlson86b21e52009-11-13 13:03:45 +00004690 skb_size = tg3_alloc_rx_skb(tp, tpr, opaque_key,
Matt Carlsonafc081f2009-11-13 13:03:43 +00004691 *post_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 if (skb_size < 0)
4693 goto drop_it;
4694
Matt Carlson287be122009-08-28 13:58:46 +00004695 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696 PCI_DMA_FROMDEVICE);
4697
Matt Carlson61e800c2010-02-17 15:16:54 +00004698 /* Ensure that the update to the skb happens
4699 * after the usage of the old DMA mapping.
4700 */
4701 smp_wmb();
4702
4703 ri->skb = NULL;
4704
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705 skb_put(skb, len);
4706 } else {
4707 struct sk_buff *copy_skb;
4708
Matt Carlsona3896162009-11-13 13:03:44 +00004709 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710 desc_idx, *post_ptr);
4711
Matt Carlson9dc7a112010-04-12 06:58:28 +00004712 copy_skb = netdev_alloc_skb(tp->dev, len + VLAN_HLEN +
4713 TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714 if (copy_skb == NULL)
4715 goto drop_it_no_recycle;
4716
Matt Carlson9dc7a112010-04-12 06:58:28 +00004717 skb_reserve(copy_skb, TG3_RAW_IP_ALIGN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718 skb_put(copy_skb, len);
4719 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03004720 skb_copy_from_linear_data(skb, copy_skb->data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
4722
4723 /* We'll reuse the original ring buffer. */
4724 skb = copy_skb;
4725 }
4726
4727 if ((tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) &&
4728 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
4729 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
4730 >> RXD_TCPCSUM_SHIFT) == 0xffff))
4731 skb->ip_summed = CHECKSUM_UNNECESSARY;
4732 else
4733 skb->ip_summed = CHECKSUM_NONE;
4734
4735 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00004736
4737 if (len > (tp->dev->mtu + ETH_HLEN) &&
4738 skb->protocol != htons(ETH_P_8021Q)) {
4739 dev_kfree_skb(skb);
4740 goto next_pkt;
4741 }
4742
Matt Carlson9dc7a112010-04-12 06:58:28 +00004743 if (desc->type_flags & RXD_FLAG_VLAN &&
4744 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG)) {
4745 vtag = desc->err_vlan & RXD_VLAN_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746#if TG3_VLAN_TAG_USED
Matt Carlson9dc7a112010-04-12 06:58:28 +00004747 if (tp->vlgrp)
4748 hw_vlan = true;
4749 else
4750#endif
4751 {
4752 struct vlan_ethhdr *ve = (struct vlan_ethhdr *)
4753 __skb_push(skb, VLAN_HLEN);
4754
4755 memmove(ve, skb->data + VLAN_HLEN,
4756 ETH_ALEN * 2);
4757 ve->h_vlan_proto = htons(ETH_P_8021Q);
4758 ve->h_vlan_TCI = htons(vtag);
4759 }
4760 }
4761
4762#if TG3_VLAN_TAG_USED
4763 if (hw_vlan)
4764 vlan_gro_receive(&tnapi->napi, tp->vlgrp, vtag, skb);
4765 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766#endif
Matt Carlson17375d22009-08-28 14:02:18 +00004767 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769 received++;
4770 budget--;
4771
4772next_pkt:
4773 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07004774
4775 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson86cfe4f2010-01-12 10:11:37 +00004776 tpr->rx_std_prod_idx = std_prod_idx % TG3_RX_RING_SIZE;
4777 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
4778 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07004779 work_mask &= ~RXD_OPAQUE_RING_STD;
4780 rx_std_posted = 0;
4781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07004783 sw_idx++;
Eric Dumazet6b31a512007-02-06 13:29:21 -08004784 sw_idx &= (TG3_RX_RCB_RING_SIZE(tp) - 1);
Michael Chan52f6d692005-04-25 15:14:32 -07004785
4786 /* Refresh hw_idx to see if there is new work */
4787 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004788 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07004789 rmb();
4790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 }
4792
4793 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00004794 tnapi->rx_rcb_ptr = sw_idx;
4795 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796
4797 /* Refill RX ring(s). */
Matt Carlsone4af1af2010-02-12 14:47:05 +00004798 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004799 if (work_mask & RXD_OPAQUE_RING_STD) {
4800 tpr->rx_std_prod_idx = std_prod_idx % TG3_RX_RING_SIZE;
4801 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
4802 tpr->rx_std_prod_idx);
4803 }
4804 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
4805 tpr->rx_jmb_prod_idx = jmb_prod_idx %
4806 TG3_RX_JUMBO_RING_SIZE;
4807 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
4808 tpr->rx_jmb_prod_idx);
4809 }
4810 mmiowb();
4811 } else if (work_mask) {
4812 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
4813 * updated before the producer indices can be updated.
4814 */
4815 smp_wmb();
4816
Matt Carlson43619352009-11-13 13:03:47 +00004817 tpr->rx_std_prod_idx = std_prod_idx % TG3_RX_RING_SIZE;
Matt Carlson43619352009-11-13 13:03:47 +00004818 tpr->rx_jmb_prod_idx = jmb_prod_idx % TG3_RX_JUMBO_RING_SIZE;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004819
Matt Carlsone4af1af2010-02-12 14:47:05 +00004820 if (tnapi != &tp->napi[1])
4821 napi_schedule(&tp->napi[1].napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823
4824 return received;
4825}
4826
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004827static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 /* handle link change and other phy events */
4830 if (!(tp->tg3_flags &
4831 (TG3_FLAG_USE_LINKCHG_REG |
4832 TG3_FLAG_POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004833 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
4834
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835 if (sblk->status & SD_STATUS_LINK_CHG) {
4836 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004837 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07004838 spin_lock(&tp->lock);
Matt Carlsondd477002008-05-25 23:45:58 -07004839 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
4840 tw32_f(MAC_STATUS,
4841 (MAC_STATUS_SYNC_CHANGED |
4842 MAC_STATUS_CFG_CHANGED |
4843 MAC_STATUS_MI_COMPLETION |
4844 MAC_STATUS_LNKSTATE_CHANGED));
4845 udelay(40);
4846 } else
4847 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07004848 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849 }
4850 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004851}
4852
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004853static int tg3_rx_prodring_xfer(struct tg3 *tp,
4854 struct tg3_rx_prodring_set *dpr,
4855 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004856{
4857 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004858 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004859
4860 while (1) {
4861 src_prod_idx = spr->rx_std_prod_idx;
4862
4863 /* Make sure updates to the rx_std_buffers[] entries and the
4864 * standard producer index are seen in the correct order.
4865 */
4866 smp_rmb();
4867
4868 if (spr->rx_std_cons_idx == src_prod_idx)
4869 break;
4870
4871 if (spr->rx_std_cons_idx < src_prod_idx)
4872 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
4873 else
4874 cpycnt = TG3_RX_RING_SIZE - spr->rx_std_cons_idx;
4875
4876 cpycnt = min(cpycnt, TG3_RX_RING_SIZE - dpr->rx_std_prod_idx);
4877
4878 si = spr->rx_std_cons_idx;
4879 di = dpr->rx_std_prod_idx;
4880
Matt Carlsone92967b2010-02-12 14:47:06 +00004881 for (i = di; i < di + cpycnt; i++) {
4882 if (dpr->rx_std_buffers[i].skb) {
4883 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004884 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00004885 break;
4886 }
4887 }
4888
4889 if (!cpycnt)
4890 break;
4891
4892 /* Ensure that updates to the rx_std_buffers ring and the
4893 * shadowed hardware producer ring from tg3_recycle_skb() are
4894 * ordered correctly WRT the skb check above.
4895 */
4896 smp_rmb();
4897
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004898 memcpy(&dpr->rx_std_buffers[di],
4899 &spr->rx_std_buffers[si],
4900 cpycnt * sizeof(struct ring_info));
4901
4902 for (i = 0; i < cpycnt; i++, di++, si++) {
4903 struct tg3_rx_buffer_desc *sbd, *dbd;
4904 sbd = &spr->rx_std[si];
4905 dbd = &dpr->rx_std[di];
4906 dbd->addr_hi = sbd->addr_hi;
4907 dbd->addr_lo = sbd->addr_lo;
4908 }
4909
4910 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) %
4911 TG3_RX_RING_SIZE;
4912 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) %
4913 TG3_RX_RING_SIZE;
4914 }
4915
4916 while (1) {
4917 src_prod_idx = spr->rx_jmb_prod_idx;
4918
4919 /* Make sure updates to the rx_jmb_buffers[] entries and
4920 * the jumbo producer index are seen in the correct order.
4921 */
4922 smp_rmb();
4923
4924 if (spr->rx_jmb_cons_idx == src_prod_idx)
4925 break;
4926
4927 if (spr->rx_jmb_cons_idx < src_prod_idx)
4928 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
4929 else
4930 cpycnt = TG3_RX_JUMBO_RING_SIZE - spr->rx_jmb_cons_idx;
4931
4932 cpycnt = min(cpycnt,
4933 TG3_RX_JUMBO_RING_SIZE - dpr->rx_jmb_prod_idx);
4934
4935 si = spr->rx_jmb_cons_idx;
4936 di = dpr->rx_jmb_prod_idx;
4937
Matt Carlsone92967b2010-02-12 14:47:06 +00004938 for (i = di; i < di + cpycnt; i++) {
4939 if (dpr->rx_jmb_buffers[i].skb) {
4940 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004941 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00004942 break;
4943 }
4944 }
4945
4946 if (!cpycnt)
4947 break;
4948
4949 /* Ensure that updates to the rx_jmb_buffers ring and the
4950 * shadowed hardware producer ring from tg3_recycle_skb() are
4951 * ordered correctly WRT the skb check above.
4952 */
4953 smp_rmb();
4954
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004955 memcpy(&dpr->rx_jmb_buffers[di],
4956 &spr->rx_jmb_buffers[si],
4957 cpycnt * sizeof(struct ring_info));
4958
4959 for (i = 0; i < cpycnt; i++, di++, si++) {
4960 struct tg3_rx_buffer_desc *sbd, *dbd;
4961 sbd = &spr->rx_jmb[si].std;
4962 dbd = &dpr->rx_jmb[di].std;
4963 dbd->addr_hi = sbd->addr_hi;
4964 dbd->addr_lo = sbd->addr_lo;
4965 }
4966
4967 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) %
4968 TG3_RX_JUMBO_RING_SIZE;
4969 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) %
4970 TG3_RX_JUMBO_RING_SIZE;
4971 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004972
4973 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004974}
4975
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004976static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
4977{
4978 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979
4980 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004981 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00004982 tg3_tx(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07004983 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
Michael Chan4fd7ab52007-10-12 01:39:50 -07004984 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985 }
4986
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987 /* run RX thread, within the bounds set by NAPI.
4988 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004989 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004991 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00004992 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004994 if ((tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00004995 struct tg3_rx_prodring_set *dpr = &tp->prodring[0];
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004996 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00004997 u32 std_prod_idx = dpr->rx_std_prod_idx;
4998 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004999
Matt Carlsone4af1af2010-02-12 14:47:05 +00005000 for (i = 1; i < tp->irq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005001 err |= tg3_rx_prodring_xfer(tp, dpr,
5002 tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005003
5004 wmb();
5005
Matt Carlsone4af1af2010-02-12 14:47:05 +00005006 if (std_prod_idx != dpr->rx_std_prod_idx)
5007 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5008 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005009
Matt Carlsone4af1af2010-02-12 14:47:05 +00005010 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
5011 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5012 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005013
5014 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005015
5016 if (err)
5017 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005018 }
5019
David S. Miller6f535762007-10-11 18:08:29 -07005020 return work_done;
5021}
David S. Millerf7383c22005-05-18 22:50:53 -07005022
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005023static int tg3_poll_msix(struct napi_struct *napi, int budget)
5024{
5025 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5026 struct tg3 *tp = tnapi->tp;
5027 int work_done = 0;
5028 struct tg3_hw_status *sblk = tnapi->hw_status;
5029
5030 while (1) {
5031 work_done = tg3_poll_work(tnapi, work_done, budget);
5032
5033 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
5034 goto tx_recovery;
5035
5036 if (unlikely(work_done >= budget))
5037 break;
5038
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005039 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005040 * to tell the hw how much work has been processed,
5041 * so we must read it before checking for more work.
5042 */
5043 tnapi->last_tag = sblk->status_tag;
5044 tnapi->last_irq_tag = tnapi->last_tag;
5045 rmb();
5046
5047 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00005048 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
5049 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005050 napi_complete(napi);
5051 /* Reenable interrupts. */
5052 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
5053 mmiowb();
5054 break;
5055 }
5056 }
5057
5058 return work_done;
5059
5060tx_recovery:
5061 /* work_done is guaranteed to be less than budget. */
5062 napi_complete(napi);
5063 schedule_work(&tp->reset_task);
5064 return work_done;
5065}
5066
David S. Miller6f535762007-10-11 18:08:29 -07005067static int tg3_poll(struct napi_struct *napi, int budget)
5068{
Matt Carlson8ef04422009-08-28 14:01:37 +00005069 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5070 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07005071 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00005072 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07005073
5074 while (1) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005075 tg3_poll_link(tp);
5076
Matt Carlson17375d22009-08-28 14:02:18 +00005077 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07005078
5079 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
5080 goto tx_recovery;
5081
5082 if (unlikely(work_done >= budget))
5083 break;
5084
Michael Chan4fd7ab52007-10-12 01:39:50 -07005085 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) {
Matt Carlson17375d22009-08-28 14:02:18 +00005086 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07005087 * to tell the hw how much work has been processed,
5088 * so we must read it before checking for more work.
5089 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005090 tnapi->last_tag = sblk->status_tag;
5091 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07005092 rmb();
5093 } else
5094 sblk->status &= ~SD_STATUS_UPDATED;
5095
Matt Carlson17375d22009-08-28 14:02:18 +00005096 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005097 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00005098 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07005099 break;
5100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101 }
5102
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005103 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07005104
5105tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07005106 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08005107 napi_complete(napi);
David S. Miller6f535762007-10-11 18:08:29 -07005108 schedule_work(&tp->reset_task);
Michael Chan4fd7ab52007-10-12 01:39:50 -07005109 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110}
5111
David S. Millerf47c11e2005-06-24 20:18:35 -07005112static void tg3_irq_quiesce(struct tg3 *tp)
5113{
Matt Carlson4f125f42009-09-01 12:55:02 +00005114 int i;
5115
David S. Millerf47c11e2005-06-24 20:18:35 -07005116 BUG_ON(tp->irq_sync);
5117
5118 tp->irq_sync = 1;
5119 smp_mb();
5120
Matt Carlson4f125f42009-09-01 12:55:02 +00005121 for (i = 0; i < tp->irq_cnt; i++)
5122 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07005123}
5124
5125static inline int tg3_irq_sync(struct tg3 *tp)
5126{
5127 return tp->irq_sync;
5128}
5129
5130/* Fully shutdown all tg3 driver activity elsewhere in the system.
5131 * If irq_sync is non-zero, then the IRQ handler must be synchronized
5132 * with as well. Most of the time, this is not necessary except when
5133 * shutting down the device.
5134 */
5135static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
5136{
Michael Chan46966542007-07-11 19:47:19 -07005137 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07005138 if (irq_sync)
5139 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07005140}
5141
5142static inline void tg3_full_unlock(struct tg3 *tp)
5143{
David S. Millerf47c11e2005-06-24 20:18:35 -07005144 spin_unlock_bh(&tp->lock);
5145}
5146
Michael Chanfcfa0a32006-03-20 22:28:41 -08005147/* One-shot MSI handler - Chip automatically disables interrupt
5148 * after sending MSI so driver doesn't have to do it.
5149 */
David Howells7d12e782006-10-05 14:55:46 +01005150static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08005151{
Matt Carlson09943a12009-08-28 14:01:57 +00005152 struct tg3_napi *tnapi = dev_id;
5153 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08005154
Matt Carlson898a56f2009-08-28 14:02:40 +00005155 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005156 if (tnapi->rx_rcb)
5157 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005158
5159 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005160 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005161
5162 return IRQ_HANDLED;
5163}
5164
Michael Chan88b06bc22005-04-21 17:13:25 -07005165/* MSI ISR - No need to check for interrupt sharing and no need to
5166 * flush status block and interrupt mailbox. PCI ordering rules
5167 * guarantee that MSI will arrive after the status block.
5168 */
David Howells7d12e782006-10-05 14:55:46 +01005169static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07005170{
Matt Carlson09943a12009-08-28 14:01:57 +00005171 struct tg3_napi *tnapi = dev_id;
5172 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07005173
Matt Carlson898a56f2009-08-28 14:02:40 +00005174 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005175 if (tnapi->rx_rcb)
5176 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07005177 /*
David S. Millerfac9b832005-05-18 22:46:34 -07005178 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07005179 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07005180 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07005181 * NIC to stop sending us irqs, engaging "in-intr-handler"
5182 * event coalescing.
5183 */
5184 tw32_mailbox(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07005185 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005186 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07005187
Michael Chan88b06bc22005-04-21 17:13:25 -07005188 return IRQ_RETVAL(1);
5189}
5190
David Howells7d12e782006-10-05 14:55:46 +01005191static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192{
Matt Carlson09943a12009-08-28 14:01:57 +00005193 struct tg3_napi *tnapi = dev_id;
5194 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005195 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196 unsigned int handled = 1;
5197
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198 /* In INTx mode, it is possible for the interrupt to arrive at
5199 * the CPU before the status block posted prior to the interrupt.
5200 * Reading the PCI State register will confirm whether the
5201 * interrupt is ours and will flush the status block.
5202 */
Michael Chand18edcb2007-03-24 20:57:11 -07005203 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
5204 if ((tp->tg3_flags & TG3_FLAG_CHIP_RESETTING) ||
5205 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5206 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005207 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07005208 }
Michael Chand18edcb2007-03-24 20:57:11 -07005209 }
5210
5211 /*
5212 * Writing any value to intr-mbox-0 clears PCI INTA# and
5213 * chip-internal interrupt pending events.
5214 * Writing non-zero to intr-mbox-0 additional tells the
5215 * NIC to stop sending us irqs, engaging "in-intr-handler"
5216 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005217 *
5218 * Flush the mailbox to de-assert the IRQ immediately to prevent
5219 * spurious interrupts. The flush impacts performance but
5220 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005221 */
Michael Chanc04cb342007-05-07 00:26:15 -07005222 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07005223 if (tg3_irq_sync(tp))
5224 goto out;
5225 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00005226 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00005227 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00005228 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07005229 } else {
5230 /* No work, shared interrupt perhaps? re-enable
5231 * interrupts, and flush that PCI write
5232 */
5233 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
5234 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07005235 }
David S. Millerf47c11e2005-06-24 20:18:35 -07005236out:
David S. Millerfac9b832005-05-18 22:46:34 -07005237 return IRQ_RETVAL(handled);
5238}
5239
David Howells7d12e782006-10-05 14:55:46 +01005240static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07005241{
Matt Carlson09943a12009-08-28 14:01:57 +00005242 struct tg3_napi *tnapi = dev_id;
5243 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005244 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07005245 unsigned int handled = 1;
5246
David S. Millerfac9b832005-05-18 22:46:34 -07005247 /* In INTx mode, it is possible for the interrupt to arrive at
5248 * the CPU before the status block posted prior to the interrupt.
5249 * Reading the PCI State register will confirm whether the
5250 * interrupt is ours and will flush the status block.
5251 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005252 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Michael Chand18edcb2007-03-24 20:57:11 -07005253 if ((tp->tg3_flags & TG3_FLAG_CHIP_RESETTING) ||
5254 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5255 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005256 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257 }
Michael Chand18edcb2007-03-24 20:57:11 -07005258 }
5259
5260 /*
5261 * writing any value to intr-mbox-0 clears PCI INTA# and
5262 * chip-internal interrupt pending events.
5263 * writing non-zero to intr-mbox-0 additional tells the
5264 * NIC to stop sending us irqs, engaging "in-intr-handler"
5265 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005266 *
5267 * Flush the mailbox to de-assert the IRQ immediately to prevent
5268 * spurious interrupts. The flush impacts performance but
5269 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005270 */
Michael Chanc04cb342007-05-07 00:26:15 -07005271 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00005272
5273 /*
5274 * In a shared interrupt configuration, sometimes other devices'
5275 * interrupts will scream. We record the current status tag here
5276 * so that the above check can report that the screaming interrupts
5277 * are unhandled. Eventually they will be silenced.
5278 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005279 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00005280
Michael Chand18edcb2007-03-24 20:57:11 -07005281 if (tg3_irq_sync(tp))
5282 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00005283
Matt Carlson72334482009-08-28 14:03:01 +00005284 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00005285
Matt Carlson09943a12009-08-28 14:01:57 +00005286 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00005287
David S. Millerf47c11e2005-06-24 20:18:35 -07005288out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289 return IRQ_RETVAL(handled);
5290}
5291
Michael Chan79381092005-04-21 17:13:59 -07005292/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01005293static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07005294{
Matt Carlson09943a12009-08-28 14:01:57 +00005295 struct tg3_napi *tnapi = dev_id;
5296 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005297 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07005298
Michael Chanf9804dd2005-09-27 12:13:10 -07005299 if ((sblk->status & SD_STATUS_UPDATED) ||
5300 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07005301 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07005302 return IRQ_RETVAL(1);
5303 }
5304 return IRQ_RETVAL(0);
5305}
5306
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07005307static int tg3_init_hw(struct tg3 *, int);
Michael Chan944d9802005-05-29 14:57:48 -07005308static int tg3_halt(struct tg3 *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309
Michael Chanb9ec6c12006-07-25 16:37:27 -07005310/* Restart hardware after configuration changes, self-test, etc.
5311 * Invoked with tp->lock held.
5312 */
5313static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
Eric Dumazet78c61462008-04-24 23:33:06 -07005314 __releases(tp->lock)
5315 __acquires(tp->lock)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005316{
5317 int err;
5318
5319 err = tg3_init_hw(tp, reset_phy);
5320 if (err) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00005321 netdev_err(tp->dev,
5322 "Failed to re-initialize device, aborting\n");
Michael Chanb9ec6c12006-07-25 16:37:27 -07005323 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
5324 tg3_full_unlock(tp);
5325 del_timer_sync(&tp->timer);
5326 tp->irq_sync = 0;
Matt Carlsonfed97812009-09-01 13:10:19 +00005327 tg3_napi_enable(tp);
Michael Chanb9ec6c12006-07-25 16:37:27 -07005328 dev_close(tp->dev);
5329 tg3_full_lock(tp, 0);
5330 }
5331 return err;
5332}
5333
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334#ifdef CONFIG_NET_POLL_CONTROLLER
5335static void tg3_poll_controller(struct net_device *dev)
5336{
Matt Carlson4f125f42009-09-01 12:55:02 +00005337 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07005338 struct tg3 *tp = netdev_priv(dev);
5339
Matt Carlson4f125f42009-09-01 12:55:02 +00005340 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00005341 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342}
5343#endif
5344
David Howellsc4028952006-11-22 14:57:56 +00005345static void tg3_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346{
David Howellsc4028952006-11-22 14:57:56 +00005347 struct tg3 *tp = container_of(work, struct tg3, reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005348 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349 unsigned int restart_timer;
5350
Michael Chan7faa0062006-02-02 17:29:28 -08005351 tg3_full_lock(tp, 0);
Michael Chan7faa0062006-02-02 17:29:28 -08005352
5353 if (!netif_running(tp->dev)) {
Michael Chan7faa0062006-02-02 17:29:28 -08005354 tg3_full_unlock(tp);
5355 return;
5356 }
5357
5358 tg3_full_unlock(tp);
5359
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005360 tg3_phy_stop(tp);
5361
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362 tg3_netif_stop(tp);
5363
David S. Millerf47c11e2005-06-24 20:18:35 -07005364 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365
5366 restart_timer = tp->tg3_flags2 & TG3_FLG2_RESTART_TIMER;
5367 tp->tg3_flags2 &= ~TG3_FLG2_RESTART_TIMER;
5368
Michael Chandf3e6542006-05-26 17:48:07 -07005369 if (tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING) {
5370 tp->write32_tx_mbox = tg3_write32_tx_mbox;
5371 tp->write32_rx_mbox = tg3_write_flush_reg32;
5372 tp->tg3_flags |= TG3_FLAG_MBOX_WRITE_REORDER;
5373 tp->tg3_flags &= ~TG3_FLAG_TX_RECOVERY_PENDING;
5374 }
5375
Michael Chan944d9802005-05-29 14:57:48 -07005376 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005377 err = tg3_init_hw(tp, 1);
5378 if (err)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005379 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380
5381 tg3_netif_start(tp);
5382
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383 if (restart_timer)
5384 mod_timer(&tp->timer, jiffies + 1);
Michael Chan7faa0062006-02-02 17:29:28 -08005385
Michael Chanb9ec6c12006-07-25 16:37:27 -07005386out:
Michael Chan7faa0062006-02-02 17:29:28 -08005387 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005388
5389 if (!err)
5390 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391}
5392
Michael Chanb0408752007-02-13 12:18:30 -08005393static void tg3_dump_short_state(struct tg3 *tp)
5394{
Joe Perches05dbe002010-02-17 19:44:19 +00005395 netdev_err(tp->dev, "DEBUG: MAC_TX_STATUS[%08x] MAC_RX_STATUS[%08x]\n",
5396 tr32(MAC_TX_STATUS), tr32(MAC_RX_STATUS));
5397 netdev_err(tp->dev, "DEBUG: RDMAC_STATUS[%08x] WDMAC_STATUS[%08x]\n",
5398 tr32(RDMAC_STATUS), tr32(WDMAC_STATUS));
Michael Chanb0408752007-02-13 12:18:30 -08005399}
5400
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401static void tg3_tx_timeout(struct net_device *dev)
5402{
5403 struct tg3 *tp = netdev_priv(dev);
5404
Michael Chanb0408752007-02-13 12:18:30 -08005405 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00005406 netdev_err(dev, "transmit timed out, resetting\n");
Michael Chanb0408752007-02-13 12:18:30 -08005407 tg3_dump_short_state(tp);
5408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409
5410 schedule_work(&tp->reset_task);
5411}
5412
Michael Chanc58ec932005-09-17 00:46:27 -07005413/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
5414static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
5415{
5416 u32 base = (u32) mapping & 0xffffffff;
5417
5418 return ((base > 0xffffdcc0) &&
5419 (base + len + 8 < base));
5420}
5421
Michael Chan72f2afb2006-03-06 19:28:35 -08005422/* Test for DMA addresses > 40-bit */
5423static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
5424 int len)
5425{
5426#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Michael Chan6728a8e2006-03-27 23:16:49 -08005427 if (tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG)
Yang Hongyang50cf1562009-04-06 19:01:14 -07005428 return (((u64) mapping + len) > DMA_BIT_MASK(40));
Michael Chan72f2afb2006-03-06 19:28:35 -08005429 return 0;
5430#else
5431 return 0;
5432#endif
5433}
5434
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005435static void tg3_set_txd(struct tg3_napi *, int, dma_addr_t, int, u32, u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436
Michael Chan72f2afb2006-03-06 19:28:35 -08005437/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00005438static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
5439 struct sk_buff *skb, u32 last_plus_one,
5440 u32 *start, u32 base_flags, u32 mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441{
Matt Carlson24f4efd2009-11-13 13:03:35 +00005442 struct tg3 *tp = tnapi->tp;
Matt Carlson41588ba2008-04-19 18:12:33 -07005443 struct sk_buff *new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07005444 dma_addr_t new_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445 u32 entry = *start;
Michael Chanc58ec932005-09-17 00:46:27 -07005446 int i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447
Matt Carlson41588ba2008-04-19 18:12:33 -07005448 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
5449 new_skb = skb_copy(skb, GFP_ATOMIC);
5450 else {
5451 int more_headroom = 4 - ((unsigned long)skb->data & 3);
5452
5453 new_skb = skb_copy_expand(skb,
5454 skb_headroom(skb) + more_headroom,
5455 skb_tailroom(skb), GFP_ATOMIC);
5456 }
5457
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07005459 ret = -1;
5460 } else {
5461 /* New SKB is guaranteed to be linear. */
5462 entry = *start;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005463 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
5464 PCI_DMA_TODEVICE);
5465 /* Make sure the mapping succeeded */
5466 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
5467 ret = -1;
5468 dev_kfree_skb(new_skb);
5469 new_skb = NULL;
David S. Miller90079ce2008-09-11 04:52:51 -07005470
Michael Chanc58ec932005-09-17 00:46:27 -07005471 /* Make sure new skb does not cross any 4G boundaries.
5472 * Drop the packet if it does.
5473 */
Alexander Duyckf4188d82009-12-02 16:48:38 +00005474 } else if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
5475 tg3_4g_overflow_test(new_addr, new_skb->len)) {
5476 pci_unmap_single(tp->pdev, new_addr, new_skb->len,
5477 PCI_DMA_TODEVICE);
Michael Chanc58ec932005-09-17 00:46:27 -07005478 ret = -1;
5479 dev_kfree_skb(new_skb);
5480 new_skb = NULL;
5481 } else {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005482 tg3_set_txd(tnapi, entry, new_addr, new_skb->len,
Michael Chanc58ec932005-09-17 00:46:27 -07005483 base_flags, 1 | (mss << 1));
5484 *start = NEXT_TX(entry);
5485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486 }
5487
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488 /* Now clean up the sw ring entries. */
5489 i = 0;
5490 while (entry != last_plus_one) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00005491 int len;
5492
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005493 if (i == 0)
Alexander Duyckf4188d82009-12-02 16:48:38 +00005494 len = skb_headlen(skb);
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005495 else
Alexander Duyckf4188d82009-12-02 16:48:38 +00005496 len = skb_shinfo(skb)->frags[i-1].size;
5497
5498 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005499 dma_unmap_addr(&tnapi->tx_buffers[entry],
Alexander Duyckf4188d82009-12-02 16:48:38 +00005500 mapping),
5501 len, PCI_DMA_TODEVICE);
5502 if (i == 0) {
5503 tnapi->tx_buffers[entry].skb = new_skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005504 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00005505 new_addr);
5506 } else {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005507 tnapi->tx_buffers[entry].skb = NULL;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509 entry = NEXT_TX(entry);
5510 i++;
5511 }
5512
5513 dev_kfree_skb(skb);
5514
Michael Chanc58ec932005-09-17 00:46:27 -07005515 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516}
5517
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005518static void tg3_set_txd(struct tg3_napi *tnapi, int entry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519 dma_addr_t mapping, int len, u32 flags,
5520 u32 mss_and_is_end)
5521{
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005522 struct tg3_tx_buffer_desc *txd = &tnapi->tx_ring[entry];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523 int is_end = (mss_and_is_end & 0x1);
5524 u32 mss = (mss_and_is_end >> 1);
5525 u32 vlan_tag = 0;
5526
5527 if (is_end)
5528 flags |= TXD_FLAG_END;
5529 if (flags & TXD_FLAG_VLAN) {
5530 vlan_tag = flags >> 16;
5531 flags &= 0xffff;
5532 }
5533 vlan_tag |= (mss << TXD_MSS_SHIFT);
5534
5535 txd->addr_hi = ((u64) mapping >> 32);
5536 txd->addr_lo = ((u64) mapping & 0xffffffff);
5537 txd->len_flags = (len << TXD_LEN_SHIFT) | flags;
5538 txd->vlan_tag = vlan_tag << TXD_VLAN_TAG_SHIFT;
5539}
5540
Michael Chan5a6f3072006-03-20 22:28:05 -08005541/* hard_start_xmit for devices that don't have any bugs and
Matt Carlsone849cdc2009-11-13 13:03:38 +00005542 * support TG3_FLG2_HW_TSO_2 and TG3_FLG2_HW_TSO_3 only.
Michael Chan5a6f3072006-03-20 22:28:05 -08005543 */
Stephen Hemminger613573252009-08-31 19:50:58 +00005544static netdev_tx_t tg3_start_xmit(struct sk_buff *skb,
5545 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546{
5547 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548 u32 len, entry, base_flags, mss;
David S. Miller90079ce2008-09-11 04:52:51 -07005549 dma_addr_t mapping;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005550 struct tg3_napi *tnapi;
5551 struct netdev_queue *txq;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005552 unsigned int i, last;
5553
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005554 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
5555 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Matt Carlson19cfaec2009-12-03 08:36:20 +00005556 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005557 tnapi++;
Michael Chan5a6f3072006-03-20 22:28:05 -08005558
Michael Chan00b70502006-06-17 21:58:45 -07005559 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005560 * and TX reclaim runs via tp->napi.poll inside of a software
Michael Chan5a6f3072006-03-20 22:28:05 -08005561 * interrupt. Furthermore, IRQ processing runs lockless so we have
5562 * no IRQ context deadlocks to worry about either. Rejoice!
5563 */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005564 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005565 if (!netif_tx_queue_stopped(txq)) {
5566 netif_tx_stop_queue(txq);
Michael Chan5a6f3072006-03-20 22:28:05 -08005567
5568 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00005569 netdev_err(dev,
5570 "BUG! Tx Ring full when queue awake!\n");
Michael Chan5a6f3072006-03-20 22:28:05 -08005571 }
Michael Chan5a6f3072006-03-20 22:28:05 -08005572 return NETDEV_TX_BUSY;
5573 }
5574
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005575 entry = tnapi->tx_prod;
Michael Chan5a6f3072006-03-20 22:28:05 -08005576 base_flags = 0;
Matt Carlsonbe98da62010-07-11 09:31:46 +00005577 mss = skb_shinfo(skb)->gso_size;
5578 if (mss) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005579 int tcp_opt_len, ip_tcp_len;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005580 u32 hdrlen;
Michael Chan5a6f3072006-03-20 22:28:05 -08005581
5582 if (skb_header_cloned(skb) &&
5583 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
5584 dev_kfree_skb(skb);
5585 goto out_unlock;
5586 }
5587
Michael Chanb0026622006-07-03 19:42:14 -07005588 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005589 hdrlen = skb_headlen(skb) - ETH_HLEN;
Michael Chanb0026622006-07-03 19:42:14 -07005590 else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005591 struct iphdr *iph = ip_hdr(skb);
5592
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07005593 tcp_opt_len = tcp_optlen(skb);
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -03005594 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
Michael Chanb0026622006-07-03 19:42:14 -07005595
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005596 iph->check = 0;
5597 iph->tot_len = htons(mss + ip_tcp_len + tcp_opt_len);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005598 hdrlen = ip_tcp_len + tcp_opt_len;
Michael Chanb0026622006-07-03 19:42:14 -07005599 }
Michael Chan5a6f3072006-03-20 22:28:05 -08005600
Matt Carlsone849cdc2009-11-13 13:03:38 +00005601 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005602 mss |= (hdrlen & 0xc) << 12;
5603 if (hdrlen & 0x10)
5604 base_flags |= 0x00000010;
5605 base_flags |= (hdrlen & 0x3e0) << 5;
5606 } else
5607 mss |= hdrlen << 9;
5608
Michael Chan5a6f3072006-03-20 22:28:05 -08005609 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
5610 TXD_FLAG_CPU_POST_DMA);
5611
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005612 tcp_hdr(skb)->check = 0;
Michael Chan5a6f3072006-03-20 22:28:05 -08005613
Matt Carlson859a588792010-04-05 10:19:28 +00005614 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005615 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson859a588792010-04-05 10:19:28 +00005616 }
5617
Michael Chan5a6f3072006-03-20 22:28:05 -08005618#if TG3_VLAN_TAG_USED
5619 if (tp->vlgrp != NULL && vlan_tx_tag_present(skb))
5620 base_flags |= (TXD_FLAG_VLAN |
5621 (vlan_tx_tag_get(skb) << 16));
5622#endif
5623
Alexander Duyckf4188d82009-12-02 16:48:38 +00005624 len = skb_headlen(skb);
5625
5626 /* Queue skb data, a.k.a. the main skb fragment. */
5627 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
5628 if (pci_dma_mapping_error(tp->pdev, mapping)) {
David S. Miller90079ce2008-09-11 04:52:51 -07005629 dev_kfree_skb(skb);
5630 goto out_unlock;
5631 }
5632
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005633 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005634 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005635
Matt Carlsonb703df62009-12-03 08:36:21 +00005636 if ((tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005637 !mss && skb->len > ETH_DATA_LEN)
5638 base_flags |= TXD_FLAG_JMB_PKT;
5639
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005640 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
Michael Chan5a6f3072006-03-20 22:28:05 -08005641 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
5642
5643 entry = NEXT_TX(entry);
5644
5645 /* Now loop through additional data fragments, and queue them. */
5646 if (skb_shinfo(skb)->nr_frags > 0) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005647 last = skb_shinfo(skb)->nr_frags - 1;
5648 for (i = 0; i <= last; i++) {
5649 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5650
5651 len = frag->size;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005652 mapping = pci_map_page(tp->pdev,
5653 frag->page,
5654 frag->page_offset,
5655 len, PCI_DMA_TODEVICE);
5656 if (pci_dma_mapping_error(tp->pdev, mapping))
5657 goto dma_error;
5658
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005659 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005660 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00005661 mapping);
Michael Chan5a6f3072006-03-20 22:28:05 -08005662
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005663 tg3_set_txd(tnapi, entry, mapping, len,
Michael Chan5a6f3072006-03-20 22:28:05 -08005664 base_flags, (i == last) | (mss << 1));
5665
5666 entry = NEXT_TX(entry);
5667 }
5668 }
5669
5670 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005671 tw32_tx_mbox(tnapi->prodmbox, entry);
Michael Chan5a6f3072006-03-20 22:28:05 -08005672
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005673 tnapi->tx_prod = entry;
5674 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005675 netif_tx_stop_queue(txq);
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005676 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005677 netif_tx_wake_queue(txq);
Michael Chan5a6f3072006-03-20 22:28:05 -08005678 }
5679
5680out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00005681 mmiowb();
Michael Chan5a6f3072006-03-20 22:28:05 -08005682
5683 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005684
5685dma_error:
5686 last = i;
5687 entry = tnapi->tx_prod;
5688 tnapi->tx_buffers[entry].skb = NULL;
5689 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005690 dma_unmap_addr(&tnapi->tx_buffers[entry], mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005691 skb_headlen(skb),
5692 PCI_DMA_TODEVICE);
5693 for (i = 0; i <= last; i++) {
5694 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5695 entry = NEXT_TX(entry);
5696
5697 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005698 dma_unmap_addr(&tnapi->tx_buffers[entry],
Alexander Duyckf4188d82009-12-02 16:48:38 +00005699 mapping),
5700 frag->size, PCI_DMA_TODEVICE);
5701 }
5702
5703 dev_kfree_skb(skb);
5704 return NETDEV_TX_OK;
Michael Chan5a6f3072006-03-20 22:28:05 -08005705}
5706
Stephen Hemminger613573252009-08-31 19:50:58 +00005707static netdev_tx_t tg3_start_xmit_dma_bug(struct sk_buff *,
5708 struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07005709
5710/* Use GSO to workaround a rare TSO bug that may be triggered when the
5711 * TSO header is greater than 80 bytes.
5712 */
5713static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
5714{
5715 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005716 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07005717
5718 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005719 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07005720 netif_stop_queue(tp->dev);
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005721 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08005722 return NETDEV_TX_BUSY;
5723
5724 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07005725 }
5726
5727 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07005728 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07005729 goto tg3_tso_bug_end;
5730
5731 do {
5732 nskb = segs;
5733 segs = segs->next;
5734 nskb->next = NULL;
5735 tg3_start_xmit_dma_bug(nskb, tp->dev);
5736 } while (segs);
5737
5738tg3_tso_bug_end:
5739 dev_kfree_skb(skb);
5740
5741 return NETDEV_TX_OK;
5742}
Michael Chan52c0fd82006-06-29 20:15:54 -07005743
Michael Chan5a6f3072006-03-20 22:28:05 -08005744/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
5745 * support TG3_FLG2_HW_TSO_1 or firmware TSO only.
5746 */
Stephen Hemminger613573252009-08-31 19:50:58 +00005747static netdev_tx_t tg3_start_xmit_dma_bug(struct sk_buff *skb,
5748 struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08005749{
5750 struct tg3 *tp = netdev_priv(dev);
Michael Chan5a6f3072006-03-20 22:28:05 -08005751 u32 len, entry, base_flags, mss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752 int would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07005753 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00005754 struct tg3_napi *tnapi;
5755 struct netdev_queue *txq;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005756 unsigned int i, last;
5757
Matt Carlson24f4efd2009-11-13 13:03:35 +00005758 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
5759 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Matt Carlson19cfaec2009-12-03 08:36:20 +00005760 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlson24f4efd2009-11-13 13:03:35 +00005761 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005762
Michael Chan00b70502006-06-17 21:58:45 -07005763 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005764 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07005765 * interrupt. Furthermore, IRQ processing runs lockless so we have
5766 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767 */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005768 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00005769 if (!netif_tx_queue_stopped(txq)) {
5770 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08005771
5772 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00005773 netdev_err(dev,
5774 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08005775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005776 return NETDEV_TX_BUSY;
5777 }
5778
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005779 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07005781 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00005783
Matt Carlsonbe98da62010-07-11 09:31:46 +00005784 mss = skb_shinfo(skb)->gso_size;
5785 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005786 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00005787 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788
5789 if (skb_header_cloned(skb) &&
5790 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
5791 dev_kfree_skb(skb);
5792 goto out_unlock;
5793 }
5794
Matt Carlson34195c32010-07-11 09:31:42 +00005795 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07005796 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797
Matt Carlson34195c32010-07-11 09:31:42 +00005798 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) {
5799 hdr_len = skb_headlen(skb) - ETH_HLEN;
5800 } else {
5801 u32 ip_tcp_len;
5802
5803 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
5804 hdr_len = ip_tcp_len + tcp_opt_len;
5805
5806 iph->check = 0;
5807 iph->tot_len = htons(mss + hdr_len);
5808 }
5809
Michael Chan52c0fd82006-06-29 20:15:54 -07005810 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Michael Chan7f62ad52007-02-20 23:25:40 -08005811 (tp->tg3_flags2 & TG3_FLG2_TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00005812 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07005813
Linus Torvalds1da177e2005-04-16 15:20:36 -07005814 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
5815 TXD_FLAG_CPU_POST_DMA);
5816
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005818 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005819 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005820 } else
5821 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
5822 iph->daddr, 0,
5823 IPPROTO_TCP,
5824 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825
Matt Carlson615774f2009-11-13 13:03:39 +00005826 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) {
5827 mss |= (hdr_len & 0xc) << 12;
5828 if (hdr_len & 0x10)
5829 base_flags |= 0x00000010;
5830 base_flags |= (hdr_len & 0x3e0) << 5;
5831 } else if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_2)
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005832 mss |= hdr_len << 9;
5833 else if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_1) ||
5834 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005835 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836 int tsflags;
5837
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005838 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839 mss |= (tsflags << 11);
5840 }
5841 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005842 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843 int tsflags;
5844
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005845 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846 base_flags |= tsflags << 12;
5847 }
5848 }
5849 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005850#if TG3_VLAN_TAG_USED
5851 if (tp->vlgrp != NULL && vlan_tx_tag_present(skb))
5852 base_flags |= (TXD_FLAG_VLAN |
5853 (vlan_tx_tag_get(skb) << 16));
5854#endif
5855
Matt Carlsonb703df62009-12-03 08:36:21 +00005856 if ((tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) &&
Matt Carlson615774f2009-11-13 13:03:39 +00005857 !mss && skb->len > ETH_DATA_LEN)
5858 base_flags |= TXD_FLAG_JMB_PKT;
5859
Alexander Duyckf4188d82009-12-02 16:48:38 +00005860 len = skb_headlen(skb);
5861
5862 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
5863 if (pci_dma_mapping_error(tp->pdev, mapping)) {
David S. Miller90079ce2008-09-11 04:52:51 -07005864 dev_kfree_skb(skb);
5865 goto out_unlock;
5866 }
5867
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005868 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005869 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870
5871 would_hit_hwbug = 0;
5872
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005873 if ((tp->tg3_flags3 & TG3_FLG3_SHORT_DMA_BUG) && len <= 8)
5874 would_hit_hwbug = 1;
5875
Matt Carlson0e1406d2009-11-02 12:33:33 +00005876 if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
5877 tg3_4g_overflow_test(mapping, len))
Matt Carlson41588ba2008-04-19 18:12:33 -07005878 would_hit_hwbug = 1;
Matt Carlson0e1406d2009-11-02 12:33:33 +00005879
5880 if ((tp->tg3_flags3 & TG3_FLG3_40BIT_DMA_LIMIT_BUG) &&
5881 tg3_40bit_overflow_test(tp, mapping, len))
5882 would_hit_hwbug = 1;
5883
5884 if (tp->tg3_flags3 & TG3_FLG3_5701_DMA_BUG)
Michael Chanc58ec932005-09-17 00:46:27 -07005885 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005886
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005887 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
5889
5890 entry = NEXT_TX(entry);
5891
5892 /* Now loop through additional data fragments, and queue them. */
5893 if (skb_shinfo(skb)->nr_frags > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894 last = skb_shinfo(skb)->nr_frags - 1;
5895 for (i = 0; i <= last; i++) {
5896 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5897
5898 len = frag->size;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005899 mapping = pci_map_page(tp->pdev,
5900 frag->page,
5901 frag->page_offset,
5902 len, PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005904 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005905 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00005906 mapping);
5907 if (pci_dma_mapping_error(tp->pdev, mapping))
5908 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005909
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005910 if ((tp->tg3_flags3 & TG3_FLG3_SHORT_DMA_BUG) &&
5911 len <= 8)
5912 would_hit_hwbug = 1;
5913
Matt Carlson0e1406d2009-11-02 12:33:33 +00005914 if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
5915 tg3_4g_overflow_test(mapping, len))
Michael Chanc58ec932005-09-17 00:46:27 -07005916 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005917
Matt Carlson0e1406d2009-11-02 12:33:33 +00005918 if ((tp->tg3_flags3 & TG3_FLG3_40BIT_DMA_LIMIT_BUG) &&
5919 tg3_40bit_overflow_test(tp, mapping, len))
Michael Chan72f2afb2006-03-06 19:28:35 -08005920 would_hit_hwbug = 1;
5921
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005923 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005924 base_flags, (i == last)|(mss << 1));
5925 else
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005926 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005927 base_flags, (i == last));
5928
5929 entry = NEXT_TX(entry);
5930 }
5931 }
5932
5933 if (would_hit_hwbug) {
5934 u32 last_plus_one = entry;
5935 u32 start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005936
Michael Chanc58ec932005-09-17 00:46:27 -07005937 start = entry - 1 - skb_shinfo(skb)->nr_frags;
5938 start &= (TG3_TX_RING_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939
5940 /* If the workaround fails due to memory/mapping
5941 * failure, silently drop this packet.
5942 */
Matt Carlson24f4efd2009-11-13 13:03:35 +00005943 if (tigon3_dma_hwbug_workaround(tnapi, skb, last_plus_one,
Michael Chanc58ec932005-09-17 00:46:27 -07005944 &start, base_flags, mss))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005945 goto out_unlock;
5946
5947 entry = start;
5948 }
5949
5950 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00005951 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005952
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005953 tnapi->tx_prod = entry;
5954 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00005955 netif_tx_stop_queue(txq);
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005956 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00005957 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07005958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005959
5960out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00005961 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005962
5963 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005964
5965dma_error:
5966 last = i;
5967 entry = tnapi->tx_prod;
5968 tnapi->tx_buffers[entry].skb = NULL;
5969 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005970 dma_unmap_addr(&tnapi->tx_buffers[entry], mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005971 skb_headlen(skb),
5972 PCI_DMA_TODEVICE);
5973 for (i = 0; i <= last; i++) {
5974 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5975 entry = NEXT_TX(entry);
5976
5977 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005978 dma_unmap_addr(&tnapi->tx_buffers[entry],
Alexander Duyckf4188d82009-12-02 16:48:38 +00005979 mapping),
5980 frag->size, PCI_DMA_TODEVICE);
5981 }
5982
5983 dev_kfree_skb(skb);
5984 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005985}
5986
5987static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
5988 int new_mtu)
5989{
5990 dev->mtu = new_mtu;
5991
Michael Chanef7f5ec2005-07-25 12:32:25 -07005992 if (new_mtu > ETH_DATA_LEN) {
Michael Chana4e2b342005-10-26 15:46:52 -07005993 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
Michael Chanef7f5ec2005-07-25 12:32:25 -07005994 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
5995 ethtool_op_set_tso(dev, 0);
Matt Carlson859a588792010-04-05 10:19:28 +00005996 } else {
Michael Chanef7f5ec2005-07-25 12:32:25 -07005997 tp->tg3_flags |= TG3_FLAG_JUMBO_RING_ENABLE;
Matt Carlson859a588792010-04-05 10:19:28 +00005998 }
Michael Chanef7f5ec2005-07-25 12:32:25 -07005999 } else {
Michael Chana4e2b342005-10-26 15:46:52 -07006000 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
Michael Chanef7f5ec2005-07-25 12:32:25 -07006001 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
Michael Chan0f893dc2005-07-25 12:30:38 -07006002 tp->tg3_flags &= ~TG3_FLAG_JUMBO_RING_ENABLE;
Michael Chanef7f5ec2005-07-25 12:32:25 -07006003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006004}
6005
6006static int tg3_change_mtu(struct net_device *dev, int new_mtu)
6007{
6008 struct tg3 *tp = netdev_priv(dev);
Michael Chanb9ec6c12006-07-25 16:37:27 -07006009 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010
6011 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
6012 return -EINVAL;
6013
6014 if (!netif_running(dev)) {
6015 /* We'll just catch it later when the
6016 * device is up'd.
6017 */
6018 tg3_set_mtu(dev, tp, new_mtu);
6019 return 0;
6020 }
6021
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006022 tg3_phy_stop(tp);
6023
Linus Torvalds1da177e2005-04-16 15:20:36 -07006024 tg3_netif_stop(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07006025
6026 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027
Michael Chan944d9802005-05-29 14:57:48 -07006028 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006029
6030 tg3_set_mtu(dev, tp, new_mtu);
6031
Michael Chanb9ec6c12006-07-25 16:37:27 -07006032 err = tg3_restart_hw(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033
Michael Chanb9ec6c12006-07-25 16:37:27 -07006034 if (!err)
6035 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036
David S. Millerf47c11e2005-06-24 20:18:35 -07006037 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006038
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006039 if (!err)
6040 tg3_phy_start(tp);
6041
Michael Chanb9ec6c12006-07-25 16:37:27 -07006042 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006043}
6044
Matt Carlson21f581a2009-08-28 14:00:25 +00006045static void tg3_rx_prodring_free(struct tg3 *tp,
6046 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006048 int i;
6049
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006050 if (tpr != &tp->prodring[0]) {
6051 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
6052 i = (i + 1) % TG3_RX_RING_SIZE)
6053 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6054 tp->rx_pkt_map_sz);
6055
6056 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
6057 for (i = tpr->rx_jmb_cons_idx;
6058 i != tpr->rx_jmb_prod_idx;
6059 i = (i + 1) % TG3_RX_JUMBO_RING_SIZE) {
6060 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6061 TG3_RX_JMB_MAP_SZ);
6062 }
6063 }
6064
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006065 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006068 for (i = 0; i < TG3_RX_RING_SIZE; i++)
6069 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6070 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006071
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006072 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006073 for (i = 0; i < TG3_RX_JUMBO_RING_SIZE; i++)
6074 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6075 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006076 }
6077}
6078
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006079/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080 *
6081 * The chip has been shut down and the driver detached from
6082 * the networking, so no interrupts or new tx packets will
6083 * end up in the driver. tp->{tx,}lock are held and thus
6084 * we may not sleep.
6085 */
Matt Carlson21f581a2009-08-28 14:00:25 +00006086static int tg3_rx_prodring_alloc(struct tg3 *tp,
6087 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006088{
Matt Carlson287be122009-08-28 13:58:46 +00006089 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006090
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006091 tpr->rx_std_cons_idx = 0;
6092 tpr->rx_std_prod_idx = 0;
6093 tpr->rx_jmb_cons_idx = 0;
6094 tpr->rx_jmb_prod_idx = 0;
6095
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006096 if (tpr != &tp->prodring[0]) {
6097 memset(&tpr->rx_std_buffers[0], 0, TG3_RX_STD_BUFF_RING_SIZE);
6098 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE)
6099 memset(&tpr->rx_jmb_buffers[0], 0,
6100 TG3_RX_JMB_BUFF_RING_SIZE);
6101 goto done;
6102 }
6103
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104 /* Zero out all descriptors. */
Matt Carlson21f581a2009-08-28 14:00:25 +00006105 memset(tpr->rx_std, 0, TG3_RX_RING_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006106
Matt Carlson287be122009-08-28 13:58:46 +00006107 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Michael Chana4e2b342005-10-26 15:46:52 -07006108 if ((tp->tg3_flags2 & TG3_FLG2_5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00006109 tp->dev->mtu > ETH_DATA_LEN)
6110 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
6111 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07006112
Linus Torvalds1da177e2005-04-16 15:20:36 -07006113 /* Initialize invariants of the rings, we only set this
6114 * stuff once. This works because the card does not
6115 * write into the rx buffer posting rings.
6116 */
6117 for (i = 0; i < TG3_RX_RING_SIZE; i++) {
6118 struct tg3_rx_buffer_desc *rxd;
6119
Matt Carlson21f581a2009-08-28 14:00:25 +00006120 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00006121 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006122 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
6123 rxd->opaque = (RXD_OPAQUE_RING_STD |
6124 (i << RXD_OPAQUE_INDEX_SHIFT));
6125 }
6126
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006127 /* Now allocate fresh SKBs for each rx ring. */
6128 for (i = 0; i < tp->rx_pending; i++) {
Matt Carlson86b21e52009-11-13 13:03:45 +00006129 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_STD, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006130 netdev_warn(tp->dev,
6131 "Using a smaller RX standard ring. Only "
6132 "%d out of %d buffers were allocated "
6133 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006134 if (i == 0)
6135 goto initfail;
6136 tp->rx_pending = i;
6137 break;
6138 }
6139 }
6140
6141 if (!(tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE))
6142 goto done;
6143
Matt Carlson21f581a2009-08-28 14:00:25 +00006144 memset(tpr->rx_jmb, 0, TG3_RX_JUMBO_RING_BYTES);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006145
Matt Carlson0d86df82010-02-17 15:17:00 +00006146 if (!(tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE))
6147 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006148
Matt Carlson0d86df82010-02-17 15:17:00 +00006149 for (i = 0; i < TG3_RX_JUMBO_RING_SIZE; i++) {
6150 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006151
Matt Carlson0d86df82010-02-17 15:17:00 +00006152 rxd = &tpr->rx_jmb[i].std;
6153 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
6154 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
6155 RXD_FLAG_JUMBO;
6156 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
6157 (i << RXD_OPAQUE_INDEX_SHIFT));
6158 }
6159
6160 for (i = 0; i < tp->rx_jumbo_pending; i++) {
6161 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_JUMBO, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006162 netdev_warn(tp->dev,
6163 "Using a smaller RX jumbo ring. Only %d "
6164 "out of %d buffers were allocated "
6165 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00006166 if (i == 0)
6167 goto initfail;
6168 tp->rx_jumbo_pending = i;
6169 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006170 }
6171 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006172
6173done:
Michael Chan32d8c572006-07-25 16:38:29 -07006174 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006175
6176initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00006177 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006178 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006179}
6180
Matt Carlson21f581a2009-08-28 14:00:25 +00006181static void tg3_rx_prodring_fini(struct tg3 *tp,
6182 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006183{
Matt Carlson21f581a2009-08-28 14:00:25 +00006184 kfree(tpr->rx_std_buffers);
6185 tpr->rx_std_buffers = NULL;
6186 kfree(tpr->rx_jmb_buffers);
6187 tpr->rx_jmb_buffers = NULL;
6188 if (tpr->rx_std) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006189 pci_free_consistent(tp->pdev, TG3_RX_RING_BYTES,
Matt Carlson21f581a2009-08-28 14:00:25 +00006190 tpr->rx_std, tpr->rx_std_mapping);
6191 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006192 }
Matt Carlson21f581a2009-08-28 14:00:25 +00006193 if (tpr->rx_jmb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006194 pci_free_consistent(tp->pdev, TG3_RX_JUMBO_RING_BYTES,
Matt Carlson21f581a2009-08-28 14:00:25 +00006195 tpr->rx_jmb, tpr->rx_jmb_mapping);
6196 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006197 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006198}
6199
Matt Carlson21f581a2009-08-28 14:00:25 +00006200static int tg3_rx_prodring_init(struct tg3 *tp,
6201 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006202{
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006203 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE, GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006204 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006205 return -ENOMEM;
6206
Matt Carlson21f581a2009-08-28 14:00:25 +00006207 tpr->rx_std = pci_alloc_consistent(tp->pdev, TG3_RX_RING_BYTES,
6208 &tpr->rx_std_mapping);
6209 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006210 goto err_out;
6211
6212 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006213 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE,
Matt Carlson21f581a2009-08-28 14:00:25 +00006214 GFP_KERNEL);
6215 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006216 goto err_out;
6217
Matt Carlson21f581a2009-08-28 14:00:25 +00006218 tpr->rx_jmb = pci_alloc_consistent(tp->pdev,
6219 TG3_RX_JUMBO_RING_BYTES,
6220 &tpr->rx_jmb_mapping);
6221 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006222 goto err_out;
6223 }
6224
6225 return 0;
6226
6227err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00006228 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006229 return -ENOMEM;
6230}
6231
6232/* Free up pending packets in all rx/tx rings.
6233 *
6234 * The chip has been shut down and the driver detached from
6235 * the networking, so no interrupts or new tx packets will
6236 * end up in the driver. tp->{tx,}lock is not held and we are not
6237 * in an interrupt context and thus may sleep.
6238 */
6239static void tg3_free_rings(struct tg3 *tp)
6240{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006241 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006242
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006243 for (j = 0; j < tp->irq_cnt; j++) {
6244 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006245
Matt Carlsonb28f6422010-06-05 17:24:32 +00006246 tg3_rx_prodring_free(tp, &tp->prodring[j]);
6247
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006248 if (!tnapi->tx_buffers)
6249 continue;
6250
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006251 for (i = 0; i < TG3_TX_RING_SIZE; ) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00006252 struct ring_info *txp;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006253 struct sk_buff *skb;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006254 unsigned int k;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006255
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006256 txp = &tnapi->tx_buffers[i];
6257 skb = txp->skb;
6258
6259 if (skb == NULL) {
6260 i++;
6261 continue;
6262 }
6263
Alexander Duyckf4188d82009-12-02 16:48:38 +00006264 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006265 dma_unmap_addr(txp, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006266 skb_headlen(skb),
6267 PCI_DMA_TODEVICE);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006268 txp->skb = NULL;
6269
Alexander Duyckf4188d82009-12-02 16:48:38 +00006270 i++;
6271
6272 for (k = 0; k < skb_shinfo(skb)->nr_frags; k++) {
6273 txp = &tnapi->tx_buffers[i & (TG3_TX_RING_SIZE - 1)];
6274 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006275 dma_unmap_addr(txp, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006276 skb_shinfo(skb)->frags[k].size,
6277 PCI_DMA_TODEVICE);
6278 i++;
6279 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006280
6281 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006282 }
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006283 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006284}
6285
6286/* Initialize tx/rx rings for packet processing.
6287 *
6288 * The chip has been shut down and the driver detached from
6289 * the networking, so no interrupts or new tx packets will
6290 * end up in the driver. tp->{tx,}lock are held and thus
6291 * we may not sleep.
6292 */
6293static int tg3_init_rings(struct tg3 *tp)
6294{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006295 int i;
Matt Carlson72334482009-08-28 14:03:01 +00006296
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006297 /* Free up all the SKBs. */
6298 tg3_free_rings(tp);
6299
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006300 for (i = 0; i < tp->irq_cnt; i++) {
6301 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006302
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006303 tnapi->last_tag = 0;
6304 tnapi->last_irq_tag = 0;
6305 tnapi->hw_status->status = 0;
6306 tnapi->hw_status->status_tag = 0;
6307 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6308
6309 tnapi->tx_prod = 0;
6310 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006311 if (tnapi->tx_ring)
6312 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006313
6314 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006315 if (tnapi->rx_rcb)
6316 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006317
Matt Carlsone4af1af2010-02-12 14:47:05 +00006318 if (tg3_rx_prodring_alloc(tp, &tp->prodring[i])) {
6319 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006320 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006321 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006322 }
Matt Carlson72334482009-08-28 14:03:01 +00006323
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006324 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006325}
6326
6327/*
6328 * Must not be invoked with interrupt sources disabled and
6329 * the hardware shutdown down.
6330 */
6331static void tg3_free_consistent(struct tg3 *tp)
6332{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006333 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006334
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006335 for (i = 0; i < tp->irq_cnt; i++) {
6336 struct tg3_napi *tnapi = &tp->napi[i];
6337
6338 if (tnapi->tx_ring) {
6339 pci_free_consistent(tp->pdev, TG3_TX_RING_BYTES,
6340 tnapi->tx_ring, tnapi->tx_desc_mapping);
6341 tnapi->tx_ring = NULL;
6342 }
6343
6344 kfree(tnapi->tx_buffers);
6345 tnapi->tx_buffers = NULL;
6346
6347 if (tnapi->rx_rcb) {
6348 pci_free_consistent(tp->pdev, TG3_RX_RCB_RING_BYTES(tp),
6349 tnapi->rx_rcb,
6350 tnapi->rx_rcb_mapping);
6351 tnapi->rx_rcb = NULL;
6352 }
6353
6354 if (tnapi->hw_status) {
6355 pci_free_consistent(tp->pdev, TG3_HW_STATUS_SIZE,
6356 tnapi->hw_status,
6357 tnapi->status_mapping);
6358 tnapi->hw_status = NULL;
6359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006360 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006361
Linus Torvalds1da177e2005-04-16 15:20:36 -07006362 if (tp->hw_stats) {
6363 pci_free_consistent(tp->pdev, sizeof(struct tg3_hw_stats),
6364 tp->hw_stats, tp->stats_mapping);
6365 tp->hw_stats = NULL;
6366 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006367
Matt Carlsone4af1af2010-02-12 14:47:05 +00006368 for (i = 0; i < tp->irq_cnt; i++)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006369 tg3_rx_prodring_fini(tp, &tp->prodring[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006370}
6371
6372/*
6373 * Must not be invoked with interrupt sources disabled and
6374 * the hardware shutdown down. Can sleep.
6375 */
6376static int tg3_alloc_consistent(struct tg3 *tp)
6377{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006378 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006379
Matt Carlsone4af1af2010-02-12 14:47:05 +00006380 for (i = 0; i < tp->irq_cnt; i++) {
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006381 if (tg3_rx_prodring_init(tp, &tp->prodring[i]))
6382 goto err_out;
6383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006384
Linus Torvalds1da177e2005-04-16 15:20:36 -07006385 tp->hw_stats = pci_alloc_consistent(tp->pdev,
6386 sizeof(struct tg3_hw_stats),
6387 &tp->stats_mapping);
6388 if (!tp->hw_stats)
6389 goto err_out;
6390
Linus Torvalds1da177e2005-04-16 15:20:36 -07006391 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6392
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006393 for (i = 0; i < tp->irq_cnt; i++) {
6394 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006395 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006396
6397 tnapi->hw_status = pci_alloc_consistent(tp->pdev,
6398 TG3_HW_STATUS_SIZE,
6399 &tnapi->status_mapping);
6400 if (!tnapi->hw_status)
6401 goto err_out;
6402
6403 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006404 sblk = tnapi->hw_status;
6405
Matt Carlson19cfaec2009-12-03 08:36:20 +00006406 /* If multivector TSS is enabled, vector 0 does not handle
6407 * tx interrupts. Don't allocate any resources for it.
6408 */
6409 if ((!i && !(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)) ||
6410 (i && (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS))) {
6411 tnapi->tx_buffers = kzalloc(sizeof(struct ring_info) *
6412 TG3_TX_RING_SIZE,
6413 GFP_KERNEL);
6414 if (!tnapi->tx_buffers)
6415 goto err_out;
6416
6417 tnapi->tx_ring = pci_alloc_consistent(tp->pdev,
6418 TG3_TX_RING_BYTES,
6419 &tnapi->tx_desc_mapping);
6420 if (!tnapi->tx_ring)
6421 goto err_out;
6422 }
6423
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006424 /*
6425 * When RSS is enabled, the status block format changes
6426 * slightly. The "rx_jumbo_consumer", "reserved",
6427 * and "rx_mini_consumer" members get mapped to the
6428 * other three rx return ring producer indexes.
6429 */
6430 switch (i) {
6431 default:
6432 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
6433 break;
6434 case 2:
6435 tnapi->rx_rcb_prod_idx = &sblk->rx_jumbo_consumer;
6436 break;
6437 case 3:
6438 tnapi->rx_rcb_prod_idx = &sblk->reserved;
6439 break;
6440 case 4:
6441 tnapi->rx_rcb_prod_idx = &sblk->rx_mini_consumer;
6442 break;
6443 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006444
Matt Carlsone4af1af2010-02-12 14:47:05 +00006445 tnapi->prodring = &tp->prodring[i];
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006446
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006447 /*
6448 * If multivector RSS is enabled, vector 0 does not handle
6449 * rx or tx interrupts. Don't allocate any resources for it.
6450 */
6451 if (!i && (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS))
6452 continue;
6453
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006454 tnapi->rx_rcb = pci_alloc_consistent(tp->pdev,
6455 TG3_RX_RCB_RING_BYTES(tp),
6456 &tnapi->rx_rcb_mapping);
6457 if (!tnapi->rx_rcb)
6458 goto err_out;
6459
6460 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006461 }
6462
Linus Torvalds1da177e2005-04-16 15:20:36 -07006463 return 0;
6464
6465err_out:
6466 tg3_free_consistent(tp);
6467 return -ENOMEM;
6468}
6469
6470#define MAX_WAIT_CNT 1000
6471
6472/* To stop a block, clear the enable bit and poll till it
6473 * clears. tp->lock is held.
6474 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006475static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006476{
6477 unsigned int i;
6478 u32 val;
6479
6480 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
6481 switch (ofs) {
6482 case RCVLSC_MODE:
6483 case DMAC_MODE:
6484 case MBFREE_MODE:
6485 case BUFMGR_MODE:
6486 case MEMARB_MODE:
6487 /* We can't enable/disable these bits of the
6488 * 5705/5750, just say success.
6489 */
6490 return 0;
6491
6492 default:
6493 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006495 }
6496
6497 val = tr32(ofs);
6498 val &= ~enable_bit;
6499 tw32_f(ofs, val);
6500
6501 for (i = 0; i < MAX_WAIT_CNT; i++) {
6502 udelay(100);
6503 val = tr32(ofs);
6504 if ((val & enable_bit) == 0)
6505 break;
6506 }
6507
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006508 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00006509 dev_err(&tp->pdev->dev,
6510 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
6511 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006512 return -ENODEV;
6513 }
6514
6515 return 0;
6516}
6517
6518/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006519static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006520{
6521 int i, err;
6522
6523 tg3_disable_ints(tp);
6524
6525 tp->rx_mode &= ~RX_MODE_ENABLE;
6526 tw32_f(MAC_RX_MODE, tp->rx_mode);
6527 udelay(10);
6528
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006529 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
6530 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
6531 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
6532 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
6533 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
6534 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006535
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006536 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
6537 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
6538 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
6539 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
6540 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
6541 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
6542 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006543
6544 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
6545 tw32_f(MAC_MODE, tp->mac_mode);
6546 udelay(40);
6547
6548 tp->tx_mode &= ~TX_MODE_ENABLE;
6549 tw32_f(MAC_TX_MODE, tp->tx_mode);
6550
6551 for (i = 0; i < MAX_WAIT_CNT; i++) {
6552 udelay(100);
6553 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
6554 break;
6555 }
6556 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00006557 dev_err(&tp->pdev->dev,
6558 "%s timed out, TX_MODE_ENABLE will not clear "
6559 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07006560 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006561 }
6562
Michael Chane6de8ad2005-05-05 14:42:41 -07006563 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006564 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
6565 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006566
6567 tw32(FTQ_RESET, 0xffffffff);
6568 tw32(FTQ_RESET, 0x00000000);
6569
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006570 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
6571 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006572
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006573 for (i = 0; i < tp->irq_cnt; i++) {
6574 struct tg3_napi *tnapi = &tp->napi[i];
6575 if (tnapi->hw_status)
6576 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006578 if (tp->hw_stats)
6579 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6580
Linus Torvalds1da177e2005-04-16 15:20:36 -07006581 return err;
6582}
6583
Matt Carlson0d3031d2007-10-10 18:02:43 -07006584static void tg3_ape_send_event(struct tg3 *tp, u32 event)
6585{
6586 int i;
6587 u32 apedata;
6588
6589 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
6590 if (apedata != APE_SEG_SIG_MAGIC)
6591 return;
6592
6593 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
Matt Carlson731fd792008-08-15 14:07:51 -07006594 if (!(apedata & APE_FW_STATUS_READY))
Matt Carlson0d3031d2007-10-10 18:02:43 -07006595 return;
6596
6597 /* Wait for up to 1 millisecond for APE to service previous event. */
6598 for (i = 0; i < 10; i++) {
6599 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
6600 return;
6601
6602 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
6603
6604 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6605 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
6606 event | APE_EVENT_STATUS_EVENT_PENDING);
6607
6608 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
6609
6610 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6611 break;
6612
6613 udelay(100);
6614 }
6615
6616 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6617 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
6618}
6619
6620static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
6621{
6622 u32 event;
6623 u32 apedata;
6624
6625 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
6626 return;
6627
6628 switch (kind) {
Matt Carlson33f401a2010-04-05 10:19:27 +00006629 case RESET_KIND_INIT:
6630 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
6631 APE_HOST_SEG_SIG_MAGIC);
6632 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
6633 APE_HOST_SEG_LEN_MAGIC);
6634 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
6635 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
6636 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
Matt Carlson6867c842010-07-11 09:31:44 +00006637 APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
Matt Carlson33f401a2010-04-05 10:19:27 +00006638 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
6639 APE_HOST_BEHAV_NO_PHYLOCK);
Matt Carlson0d3031d2007-10-10 18:02:43 -07006640
Matt Carlson33f401a2010-04-05 10:19:27 +00006641 event = APE_EVENT_STATUS_STATE_START;
6642 break;
6643 case RESET_KIND_SHUTDOWN:
6644 /* With the interface we are currently using,
6645 * APE does not track driver state. Wiping
6646 * out the HOST SEGMENT SIGNATURE forces
6647 * the APE to assume OS absent status.
6648 */
6649 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
Matt Carlsonb2aee152008-11-03 16:51:11 -08006650
Matt Carlson33f401a2010-04-05 10:19:27 +00006651 event = APE_EVENT_STATUS_STATE_UNLOAD;
6652 break;
6653 case RESET_KIND_SUSPEND:
6654 event = APE_EVENT_STATUS_STATE_SUSPEND;
6655 break;
6656 default:
6657 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -07006658 }
6659
6660 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
6661
6662 tg3_ape_send_event(tp, event);
6663}
6664
Michael Chane6af3012005-04-21 17:12:05 -07006665/* tp->lock is held. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006666static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
6667{
David S. Millerf49639e2006-06-09 11:58:36 -07006668 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
6669 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006670
6671 if (tp->tg3_flags2 & TG3_FLG2_ASF_NEW_HANDSHAKE) {
6672 switch (kind) {
6673 case RESET_KIND_INIT:
6674 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6675 DRV_STATE_START);
6676 break;
6677
6678 case RESET_KIND_SHUTDOWN:
6679 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6680 DRV_STATE_UNLOAD);
6681 break;
6682
6683 case RESET_KIND_SUSPEND:
6684 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6685 DRV_STATE_SUSPEND);
6686 break;
6687
6688 default:
6689 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006691 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07006692
6693 if (kind == RESET_KIND_INIT ||
6694 kind == RESET_KIND_SUSPEND)
6695 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006696}
6697
6698/* tp->lock is held. */
6699static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
6700{
6701 if (tp->tg3_flags2 & TG3_FLG2_ASF_NEW_HANDSHAKE) {
6702 switch (kind) {
6703 case RESET_KIND_INIT:
6704 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6705 DRV_STATE_START_DONE);
6706 break;
6707
6708 case RESET_KIND_SHUTDOWN:
6709 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6710 DRV_STATE_UNLOAD_DONE);
6711 break;
6712
6713 default:
6714 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006716 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07006717
6718 if (kind == RESET_KIND_SHUTDOWN)
6719 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006720}
6721
6722/* tp->lock is held. */
6723static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
6724{
6725 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) {
6726 switch (kind) {
6727 case RESET_KIND_INIT:
6728 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6729 DRV_STATE_START);
6730 break;
6731
6732 case RESET_KIND_SHUTDOWN:
6733 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6734 DRV_STATE_UNLOAD);
6735 break;
6736
6737 case RESET_KIND_SUSPEND:
6738 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6739 DRV_STATE_SUSPEND);
6740 break;
6741
6742 default:
6743 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006745 }
6746}
6747
Michael Chan7a6f4362006-09-27 16:03:31 -07006748static int tg3_poll_fw(struct tg3 *tp)
6749{
6750 int i;
6751 u32 val;
6752
Michael Chanb5d37722006-09-27 16:06:21 -07006753 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Gary Zambrano0ccead12006-11-14 16:34:00 -08006754 /* Wait up to 20ms for init done. */
6755 for (i = 0; i < 200; i++) {
Michael Chanb5d37722006-09-27 16:06:21 -07006756 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
6757 return 0;
Gary Zambrano0ccead12006-11-14 16:34:00 -08006758 udelay(100);
Michael Chanb5d37722006-09-27 16:06:21 -07006759 }
6760 return -ENODEV;
6761 }
6762
Michael Chan7a6f4362006-09-27 16:03:31 -07006763 /* Wait for firmware initialization to complete. */
6764 for (i = 0; i < 100000; i++) {
6765 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
6766 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
6767 break;
6768 udelay(10);
6769 }
6770
6771 /* Chip might not be fitted with firmware. Some Sun onboard
6772 * parts are configured like that. So don't signal the timeout
6773 * of the above loop as an error, but do report the lack of
6774 * running firmware once.
6775 */
6776 if (i >= 100000 &&
6777 !(tp->tg3_flags2 & TG3_FLG2_NO_FWARE_REPORTED)) {
6778 tp->tg3_flags2 |= TG3_FLG2_NO_FWARE_REPORTED;
6779
Joe Perches05dbe002010-02-17 19:44:19 +00006780 netdev_info(tp->dev, "No firmware running\n");
Michael Chan7a6f4362006-09-27 16:03:31 -07006781 }
6782
Matt Carlson6b10c162010-02-12 14:47:08 +00006783 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
6784 /* The 57765 A0 needs a little more
6785 * time to do some important work.
6786 */
6787 mdelay(10);
6788 }
6789
Michael Chan7a6f4362006-09-27 16:03:31 -07006790 return 0;
6791}
6792
Michael Chanee6a99b2007-07-18 21:49:10 -07006793/* Save PCI command register before chip reset */
6794static void tg3_save_pci_state(struct tg3 *tp)
6795{
Matt Carlson8a6eac92007-10-21 16:17:55 -07006796 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07006797}
6798
6799/* Restore PCI state after chip reset */
6800static void tg3_restore_pci_state(struct tg3 *tp)
6801{
6802 u32 val;
6803
6804 /* Re-enable indirect register accesses. */
6805 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
6806 tp->misc_host_ctrl);
6807
6808 /* Set MAX PCI retry to zero. */
6809 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
6810 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
6811 (tp->tg3_flags & TG3_FLAG_PCIX_MODE))
6812 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07006813 /* Allow reads and writes to the APE register and memory space. */
6814 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
6815 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00006816 PCISTATE_ALLOW_APE_SHMEM_WR |
6817 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07006818 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
6819
Matt Carlson8a6eac92007-10-21 16:17:55 -07006820 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07006821
Matt Carlsonfcb389d2008-11-03 16:55:44 -08006822 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785) {
6823 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)
6824 pcie_set_readrq(tp->pdev, 4096);
6825 else {
6826 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
6827 tp->pci_cacheline_sz);
6828 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
6829 tp->pci_lat_timer);
6830 }
Michael Chan114342f2007-10-15 02:12:26 -07006831 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08006832
Michael Chanee6a99b2007-07-18 21:49:10 -07006833 /* Make sure PCI-X relaxed ordering bit is clear. */
Matt Carlson52f44902008-11-21 17:17:04 -08006834 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
Matt Carlson9974a352007-10-07 23:27:28 -07006835 u16 pcix_cmd;
6836
6837 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
6838 &pcix_cmd);
6839 pcix_cmd &= ~PCI_X_CMD_ERO;
6840 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
6841 pcix_cmd);
6842 }
Michael Chanee6a99b2007-07-18 21:49:10 -07006843
6844 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
Michael Chanee6a99b2007-07-18 21:49:10 -07006845
6846 /* Chip reset on 5780 will reset MSI enable bit,
6847 * so need to restore it.
6848 */
6849 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
6850 u16 ctrl;
6851
6852 pci_read_config_word(tp->pdev,
6853 tp->msi_cap + PCI_MSI_FLAGS,
6854 &ctrl);
6855 pci_write_config_word(tp->pdev,
6856 tp->msi_cap + PCI_MSI_FLAGS,
6857 ctrl | PCI_MSI_FLAGS_ENABLE);
6858 val = tr32(MSGINT_MODE);
6859 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
6860 }
6861 }
6862}
6863
Linus Torvalds1da177e2005-04-16 15:20:36 -07006864static void tg3_stop_fw(struct tg3 *);
6865
6866/* tp->lock is held. */
6867static int tg3_chip_reset(struct tg3 *tp)
6868{
6869 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07006870 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00006871 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006872
David S. Millerf49639e2006-06-09 11:58:36 -07006873 tg3_nvram_lock(tp);
6874
Matt Carlson77b483f2008-08-15 14:07:24 -07006875 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
6876
David S. Millerf49639e2006-06-09 11:58:36 -07006877 /* No matching tg3_nvram_unlock() after this because
6878 * chip reset below will undo the nvram lock.
6879 */
6880 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006881
Michael Chanee6a99b2007-07-18 21:49:10 -07006882 /* GRC_MISC_CFG core clock reset will clear the memory
6883 * enable bit in PCI register 4 and the MSI enable bit
6884 * on some chips, so we save relevant registers here.
6885 */
6886 tg3_save_pci_state(tp);
6887
Michael Chand9ab5ad2006-03-20 22:27:35 -08006888 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08006889 (tp->tg3_flags3 & TG3_FLG3_5755_PLUS))
Michael Chand9ab5ad2006-03-20 22:27:35 -08006890 tw32(GRC_FASTBOOT_PC, 0);
6891
Linus Torvalds1da177e2005-04-16 15:20:36 -07006892 /*
6893 * We must avoid the readl() that normally takes place.
6894 * It locks machines, causes machine checks, and other
6895 * fun things. So, temporarily disable the 5701
6896 * hardware workaround, while we do the reset.
6897 */
Michael Chan1ee582d2005-08-09 20:16:46 -07006898 write_op = tp->write32;
6899 if (write_op == tg3_write_flush_reg32)
6900 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006901
Michael Chand18edcb2007-03-24 20:57:11 -07006902 /* Prevent the irq handler from reading or writing PCI registers
6903 * during chip reset when the memory enable bit in the PCI command
6904 * register may be cleared. The chip does not generate interrupt
6905 * at this time, but the irq handler may still be called due to irq
6906 * sharing or irqpoll.
6907 */
6908 tp->tg3_flags |= TG3_FLAG_CHIP_RESETTING;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006909 for (i = 0; i < tp->irq_cnt; i++) {
6910 struct tg3_napi *tnapi = &tp->napi[i];
6911 if (tnapi->hw_status) {
6912 tnapi->hw_status->status = 0;
6913 tnapi->hw_status->status_tag = 0;
6914 }
6915 tnapi->last_tag = 0;
6916 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07006917 }
Michael Chand18edcb2007-03-24 20:57:11 -07006918 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00006919
6920 for (i = 0; i < tp->irq_cnt; i++)
6921 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07006922
Matt Carlson255ca312009-08-25 10:07:27 +00006923 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
6924 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
6925 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
6926 }
6927
Linus Torvalds1da177e2005-04-16 15:20:36 -07006928 /* do the reset */
6929 val = GRC_MISC_CFG_CORECLK_RESET;
6930
6931 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
Matt Carlson88075d92010-08-02 11:25:58 +00006932 /* Force PCIe 1.0a mode */
6933 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
6934 !(tp->tg3_flags3 & TG3_FLG3_5717_PLUS) &&
6935 tr32(TG3_PCIE_PHY_TSTCTL) ==
6936 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
6937 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
6938
Linus Torvalds1da177e2005-04-16 15:20:36 -07006939 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
6940 tw32(GRC_MISC_CFG, (1 << 29));
6941 val |= (1 << 29);
6942 }
6943 }
6944
Michael Chanb5d37722006-09-27 16:06:21 -07006945 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
6946 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
6947 tw32(GRC_VCPU_EXT_CTRL,
6948 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
6949 }
6950
Linus Torvalds1da177e2005-04-16 15:20:36 -07006951 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
6952 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
6953 tw32(GRC_MISC_CFG, val);
6954
Michael Chan1ee582d2005-08-09 20:16:46 -07006955 /* restore 5701 hardware bug workaround write method */
6956 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006957
6958 /* Unfortunately, we have to delay before the PCI read back.
6959 * Some 575X chips even will not respond to a PCI cfg access
6960 * when the reset command is given to the chip.
6961 *
6962 * How do these hardware designers expect things to work
6963 * properly if the PCI write is posted for a long period
6964 * of time? It is always necessary to have some method by
6965 * which a register read back can occur to push the write
6966 * out which does the reset.
6967 *
6968 * For most tg3 variants the trick below was working.
6969 * Ho hum...
6970 */
6971 udelay(120);
6972
6973 /* Flush PCI posted writes. The normal MMIO registers
6974 * are inaccessible at this time so this is the only
6975 * way to make this reliably (actually, this is no longer
6976 * the case, see above). I tried to use indirect
6977 * register read/write but this upset some 5701 variants.
6978 */
6979 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
6980
6981 udelay(120);
6982
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006983 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) && tp->pcie_cap) {
Matt Carlsone7126992009-08-25 10:08:16 +00006984 u16 val16;
6985
Linus Torvalds1da177e2005-04-16 15:20:36 -07006986 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
6987 int i;
6988 u32 cfg_val;
6989
6990 /* Wait for link training to complete. */
6991 for (i = 0; i < 5000; i++)
6992 udelay(100);
6993
6994 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
6995 pci_write_config_dword(tp->pdev, 0xc4,
6996 cfg_val | (1 << 15));
6997 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006998
Matt Carlsone7126992009-08-25 10:08:16 +00006999 /* Clear the "no snoop" and "relaxed ordering" bits. */
7000 pci_read_config_word(tp->pdev,
7001 tp->pcie_cap + PCI_EXP_DEVCTL,
7002 &val16);
7003 val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
7004 PCI_EXP_DEVCTL_NOSNOOP_EN);
7005 /*
7006 * Older PCIe devices only support the 128 byte
7007 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007008 */
Matt Carlson6de34cb2010-08-02 11:25:55 +00007009 if (!(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT))
Matt Carlsone7126992009-08-25 10:08:16 +00007010 val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007011 pci_write_config_word(tp->pdev,
7012 tp->pcie_cap + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007013 val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007014
7015 pcie_set_readrq(tp->pdev, 4096);
7016
7017 /* Clear error status */
7018 pci_write_config_word(tp->pdev,
7019 tp->pcie_cap + PCI_EXP_DEVSTA,
7020 PCI_EXP_DEVSTA_CED |
7021 PCI_EXP_DEVSTA_NFED |
7022 PCI_EXP_DEVSTA_FED |
7023 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007024 }
7025
Michael Chanee6a99b2007-07-18 21:49:10 -07007026 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007027
Michael Chand18edcb2007-03-24 20:57:11 -07007028 tp->tg3_flags &= ~TG3_FLAG_CHIP_RESETTING;
7029
Michael Chanee6a99b2007-07-18 21:49:10 -07007030 val = 0;
7031 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
Michael Chan4cf78e42005-07-25 12:29:19 -07007032 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07007033 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007034
7035 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
7036 tg3_stop_fw(tp);
7037 tw32(0x5000, 0x400);
7038 }
7039
7040 tw32(GRC_MODE, tp->grc_mode);
7041
7042 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007043 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007044
7045 tw32(0xc4, val | (1 << 15));
7046 }
7047
7048 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
7049 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
7050 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
7051 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
7052 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
7053 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7054 }
7055
7056 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
7057 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
7058 tw32_f(MAC_MODE, tp->mac_mode);
Michael Chan747e8f82005-07-25 12:33:22 -07007059 } else if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES) {
7060 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
7061 tw32_f(MAC_MODE, tp->mac_mode);
Matt Carlson3bda1252008-08-15 14:08:22 -07007062 } else if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
7063 tp->mac_mode &= (MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN);
7064 if (tp->mac_mode & MAC_MODE_APE_TX_EN)
7065 tp->mac_mode |= MAC_MODE_TDE_ENABLE;
7066 tw32_f(MAC_MODE, tp->mac_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007067 } else
7068 tw32_f(MAC_MODE, 0);
7069 udelay(40);
7070
Matt Carlson77b483f2008-08-15 14:07:24 -07007071 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
7072
Michael Chan7a6f4362006-09-27 16:03:31 -07007073 err = tg3_poll_fw(tp);
7074 if (err)
7075 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007076
Matt Carlson0a9140c2009-08-28 12:27:50 +00007077 tg3_mdio_start(tp);
7078
Linus Torvalds1da177e2005-04-16 15:20:36 -07007079 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007080 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
7081 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonc885e822010-08-02 11:25:57 +00007082 !(tp->tg3_flags3 & TG3_FLG3_5717_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007083 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007084
7085 tw32(0x7c00, val | (1 << 25));
7086 }
7087
7088 /* Reprobe ASF enable state. */
7089 tp->tg3_flags &= ~TG3_FLAG_ENABLE_ASF;
7090 tp->tg3_flags2 &= ~TG3_FLG2_ASF_NEW_HANDSHAKE;
7091 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
7092 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
7093 u32 nic_cfg;
7094
7095 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
7096 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
7097 tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
Matt Carlson4ba526c2008-08-15 14:10:04 -07007098 tp->last_event_jiffies = jiffies;
John W. Linvillecbf46852005-04-21 17:01:29 -07007099 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007100 tp->tg3_flags2 |= TG3_FLG2_ASF_NEW_HANDSHAKE;
7101 }
7102 }
7103
7104 return 0;
7105}
7106
7107/* tp->lock is held. */
7108static void tg3_stop_fw(struct tg3 *tp)
7109{
Matt Carlson0d3031d2007-10-10 18:02:43 -07007110 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
7111 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07007112 /* Wait for RX cpu to ACK the previous event. */
7113 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007114
7115 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007116
7117 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007118
Matt Carlson7c5026a2008-05-02 16:49:29 -07007119 /* Wait for RX cpu to ACK this event. */
7120 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007121 }
7122}
7123
7124/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07007125static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007126{
7127 int err;
7128
7129 tg3_stop_fw(tp);
7130
Michael Chan944d9802005-05-29 14:57:48 -07007131 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007132
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007133 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007134 err = tg3_chip_reset(tp);
7135
Matt Carlsondaba2a62009-04-20 06:58:52 +00007136 __tg3_set_mac_addr(tp, 0);
7137
Michael Chan944d9802005-05-29 14:57:48 -07007138 tg3_write_sig_legacy(tp, kind);
7139 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007140
7141 if (err)
7142 return err;
7143
7144 return 0;
7145}
7146
Linus Torvalds1da177e2005-04-16 15:20:36 -07007147#define RX_CPU_SCRATCH_BASE 0x30000
7148#define RX_CPU_SCRATCH_SIZE 0x04000
7149#define TX_CPU_SCRATCH_BASE 0x34000
7150#define TX_CPU_SCRATCH_SIZE 0x04000
7151
7152/* tp->lock is held. */
7153static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
7154{
7155 int i;
7156
Eric Sesterhenn5d9428d2006-04-02 13:52:48 +02007157 BUG_ON(offset == TX_CPU_BASE &&
7158 (tp->tg3_flags2 & TG3_FLG2_5705_PLUS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007159
Michael Chanb5d37722006-09-27 16:06:21 -07007160 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7161 u32 val = tr32(GRC_VCPU_EXT_CTRL);
7162
7163 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
7164 return 0;
7165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007166 if (offset == RX_CPU_BASE) {
7167 for (i = 0; i < 10000; i++) {
7168 tw32(offset + CPU_STATE, 0xffffffff);
7169 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7170 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7171 break;
7172 }
7173
7174 tw32(offset + CPU_STATE, 0xffffffff);
7175 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
7176 udelay(10);
7177 } else {
7178 for (i = 0; i < 10000; i++) {
7179 tw32(offset + CPU_STATE, 0xffffffff);
7180 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7181 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7182 break;
7183 }
7184 }
7185
7186 if (i >= 10000) {
Joe Perches05dbe002010-02-17 19:44:19 +00007187 netdev_err(tp->dev, "%s timed out, %s CPU\n",
7188 __func__, offset == RX_CPU_BASE ? "RX" : "TX");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007189 return -ENODEV;
7190 }
Michael Chanec41c7d2006-01-17 02:40:55 -08007191
7192 /* Clear firmware's nvram arbitration. */
7193 if (tp->tg3_flags & TG3_FLAG_NVRAM)
7194 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007195 return 0;
7196}
7197
7198struct fw_info {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007199 unsigned int fw_base;
7200 unsigned int fw_len;
7201 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007202};
7203
7204/* tp->lock is held. */
7205static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base, u32 cpu_scratch_base,
7206 int cpu_scratch_size, struct fw_info *info)
7207{
Michael Chanec41c7d2006-01-17 02:40:55 -08007208 int err, lock_err, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007209 void (*write_op)(struct tg3 *, u32, u32);
7210
7211 if (cpu_base == TX_CPU_BASE &&
7212 (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007213 netdev_err(tp->dev,
7214 "%s: Trying to load TX cpu firmware which is 5705\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007215 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007216 return -EINVAL;
7217 }
7218
7219 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
7220 write_op = tg3_write_mem;
7221 else
7222 write_op = tg3_write_indirect_reg32;
7223
Michael Chan1b628152005-05-29 14:59:49 -07007224 /* It is possible that bootcode is still loading at this point.
7225 * Get the nvram lock first before halting the cpu.
7226 */
Michael Chanec41c7d2006-01-17 02:40:55 -08007227 lock_err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007228 err = tg3_halt_cpu(tp, cpu_base);
Michael Chanec41c7d2006-01-17 02:40:55 -08007229 if (!lock_err)
7230 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007231 if (err)
7232 goto out;
7233
7234 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
7235 write_op(tp, cpu_scratch_base + i, 0);
7236 tw32(cpu_base + CPU_STATE, 0xffffffff);
7237 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007238 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007239 write_op(tp, (cpu_scratch_base +
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007240 (info->fw_base & 0xffff) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07007241 (i * sizeof(u32))),
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007242 be32_to_cpu(info->fw_data[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007243
7244 err = 0;
7245
7246out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007247 return err;
7248}
7249
7250/* tp->lock is held. */
7251static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
7252{
7253 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007254 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007255 int err, i;
7256
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007257 fw_data = (void *)tp->fw->data;
7258
7259 /* Firmware blob starts with version numbers, followed by
7260 start address and length. We are setting complete length.
7261 length = end_address_of_bss - start_address_of_text.
7262 Remainder is the blob to be loaded contiguously
7263 from start address. */
7264
7265 info.fw_base = be32_to_cpu(fw_data[1]);
7266 info.fw_len = tp->fw->size - 12;
7267 info.fw_data = &fw_data[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007268
7269 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
7270 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
7271 &info);
7272 if (err)
7273 return err;
7274
7275 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
7276 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
7277 &info);
7278 if (err)
7279 return err;
7280
7281 /* Now startup only the RX cpu. */
7282 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007283 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007284
7285 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007286 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007287 break;
7288 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7289 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007290 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007291 udelay(1000);
7292 }
7293 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007294 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
7295 "should be %08x\n", __func__,
Joe Perches05dbe002010-02-17 19:44:19 +00007296 tr32(RX_CPU_BASE + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007297 return -ENODEV;
7298 }
7299 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7300 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
7301
7302 return 0;
7303}
7304
Linus Torvalds1da177e2005-04-16 15:20:36 -07007305/* 5705 needs a special version of the TSO firmware. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007306
7307/* tp->lock is held. */
7308static int tg3_load_tso_firmware(struct tg3 *tp)
7309{
7310 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007311 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007312 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
7313 int err, i;
7314
7315 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
7316 return 0;
7317
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007318 fw_data = (void *)tp->fw->data;
7319
7320 /* Firmware blob starts with version numbers, followed by
7321 start address and length. We are setting complete length.
7322 length = end_address_of_bss - start_address_of_text.
7323 Remainder is the blob to be loaded contiguously
7324 from start address. */
7325
7326 info.fw_base = be32_to_cpu(fw_data[1]);
7327 cpu_scratch_size = tp->fw_len;
7328 info.fw_len = tp->fw->size - 12;
7329 info.fw_data = &fw_data[3];
7330
Linus Torvalds1da177e2005-04-16 15:20:36 -07007331 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007332 cpu_base = RX_CPU_BASE;
7333 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007334 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007335 cpu_base = TX_CPU_BASE;
7336 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
7337 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
7338 }
7339
7340 err = tg3_load_firmware_cpu(tp, cpu_base,
7341 cpu_scratch_base, cpu_scratch_size,
7342 &info);
7343 if (err)
7344 return err;
7345
7346 /* Now startup the cpu. */
7347 tw32(cpu_base + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007348 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007349
7350 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007351 if (tr32(cpu_base + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007352 break;
7353 tw32(cpu_base + CPU_STATE, 0xffffffff);
7354 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007355 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007356 udelay(1000);
7357 }
7358 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007359 netdev_err(tp->dev,
7360 "%s fails to set CPU PC, is %08x should be %08x\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007361 __func__, tr32(cpu_base + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007362 return -ENODEV;
7363 }
7364 tw32(cpu_base + CPU_STATE, 0xffffffff);
7365 tw32_f(cpu_base + CPU_MODE, 0x00000000);
7366 return 0;
7367}
7368
Linus Torvalds1da177e2005-04-16 15:20:36 -07007369
Linus Torvalds1da177e2005-04-16 15:20:36 -07007370static int tg3_set_mac_addr(struct net_device *dev, void *p)
7371{
7372 struct tg3 *tp = netdev_priv(dev);
7373 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07007374 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007375
Michael Chanf9804dd2005-09-27 12:13:10 -07007376 if (!is_valid_ether_addr(addr->sa_data))
7377 return -EINVAL;
7378
Linus Torvalds1da177e2005-04-16 15:20:36 -07007379 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7380
Michael Chane75f7c92006-03-20 21:33:26 -08007381 if (!netif_running(dev))
7382 return 0;
7383
Michael Chan58712ef2006-04-29 18:58:01 -07007384 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) {
Michael Chan986e0ae2007-05-05 12:10:20 -07007385 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07007386
Michael Chan986e0ae2007-05-05 12:10:20 -07007387 addr0_high = tr32(MAC_ADDR_0_HIGH);
7388 addr0_low = tr32(MAC_ADDR_0_LOW);
7389 addr1_high = tr32(MAC_ADDR_1_HIGH);
7390 addr1_low = tr32(MAC_ADDR_1_LOW);
7391
7392 /* Skip MAC addr 1 if ASF is using it. */
7393 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
7394 !(addr1_high == 0 && addr1_low == 0))
7395 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07007396 }
Michael Chan986e0ae2007-05-05 12:10:20 -07007397 spin_lock_bh(&tp->lock);
7398 __tg3_set_mac_addr(tp, skip_mac_1);
7399 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007400
Michael Chanb9ec6c12006-07-25 16:37:27 -07007401 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007402}
7403
7404/* tp->lock is held. */
7405static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
7406 dma_addr_t mapping, u32 maxlen_flags,
7407 u32 nic_addr)
7408{
7409 tg3_write_mem(tp,
7410 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
7411 ((u64) mapping >> 32));
7412 tg3_write_mem(tp,
7413 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
7414 ((u64) mapping & 0xffffffff));
7415 tg3_write_mem(tp,
7416 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
7417 maxlen_flags);
7418
7419 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7420 tg3_write_mem(tp,
7421 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
7422 nic_addr);
7423}
7424
7425static void __tg3_set_rx_mode(struct net_device *);
Michael Chand244c892005-07-05 14:42:33 -07007426static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07007427{
Matt Carlsonb6080e12009-09-01 13:12:00 +00007428 int i;
7429
Matt Carlson19cfaec2009-12-03 08:36:20 +00007430 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007431 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
7432 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
7433 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007434 } else {
7435 tw32(HOSTCC_TXCOL_TICKS, 0);
7436 tw32(HOSTCC_TXMAX_FRAMES, 0);
7437 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007438 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007439
Matt Carlson20d73752010-07-11 09:31:41 +00007440 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00007441 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
7442 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
7443 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
7444 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007445 tw32(HOSTCC_RXCOL_TICKS, 0);
7446 tw32(HOSTCC_RXMAX_FRAMES, 0);
7447 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07007448 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007449
David S. Miller15f98502005-05-18 22:49:26 -07007450 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7451 u32 val = ec->stats_block_coalesce_usecs;
7452
Matt Carlsonb6080e12009-09-01 13:12:00 +00007453 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
7454 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
7455
David S. Miller15f98502005-05-18 22:49:26 -07007456 if (!netif_carrier_ok(tp->dev))
7457 val = 0;
7458
7459 tw32(HOSTCC_STAT_COAL_TICKS, val);
7460 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007461
7462 for (i = 0; i < tp->irq_cnt - 1; i++) {
7463 u32 reg;
7464
7465 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
7466 tw32(reg, ec->rx_coalesce_usecs);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007467 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
7468 tw32(reg, ec->rx_max_coalesced_frames);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007469 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
7470 tw32(reg, ec->rx_max_coalesced_frames_irq);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007471
7472 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS) {
7473 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
7474 tw32(reg, ec->tx_coalesce_usecs);
7475 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
7476 tw32(reg, ec->tx_max_coalesced_frames);
7477 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
7478 tw32(reg, ec->tx_max_coalesced_frames_irq);
7479 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007480 }
7481
7482 for (; i < tp->irq_max - 1; i++) {
7483 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007484 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007485 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007486
7487 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS) {
7488 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
7489 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
7490 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
7491 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007492 }
David S. Miller15f98502005-05-18 22:49:26 -07007493}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007494
7495/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00007496static void tg3_rings_reset(struct tg3 *tp)
7497{
7498 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007499 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007500 struct tg3_napi *tnapi = &tp->napi[0];
7501
7502 /* Disable all transmit rings but the first. */
7503 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7504 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00007505 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
7506 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007507 else
7508 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7509
7510 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7511 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
7512 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
7513 BDINFO_FLAGS_DISABLED);
7514
7515
7516 /* Disable all receive return rings but the first. */
Matt Carlsona50d0792010-06-05 17:24:37 +00007517 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
7518 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007519 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
7520 else if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00007521 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00007522 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
7523 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson2d31eca2009-09-01 12:53:31 +00007524 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
7525 else
7526 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7527
7528 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7529 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
7530 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
7531 BDINFO_FLAGS_DISABLED);
7532
7533 /* Disable interrupts */
7534 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
7535
7536 /* Zero mailbox registers. */
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007537 if (tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX) {
7538 for (i = 1; i < TG3_IRQ_MAX_VECS; i++) {
7539 tp->napi[i].tx_prod = 0;
7540 tp->napi[i].tx_cons = 0;
Matt Carlsonc2353a32010-01-20 16:58:08 +00007541 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
7542 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007543 tw32_rx_mbox(tp->napi[i].consmbox, 0);
7544 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
7545 }
Matt Carlsonc2353a32010-01-20 16:58:08 +00007546 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS))
7547 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007548 } else {
7549 tp->napi[0].tx_prod = 0;
7550 tp->napi[0].tx_cons = 0;
7551 tw32_mailbox(tp->napi[0].prodmbox, 0);
7552 tw32_rx_mbox(tp->napi[0].consmbox, 0);
7553 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007554
7555 /* Make sure the NIC-based send BD rings are disabled. */
7556 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7557 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
7558 for (i = 0; i < 16; i++)
7559 tw32_tx_mbox(mbox + i * 8, 0);
7560 }
7561
7562 txrcb = NIC_SRAM_SEND_RCB;
7563 rxrcb = NIC_SRAM_RCV_RET_RCB;
7564
7565 /* Clear status block in ram. */
7566 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7567
7568 /* Set status block DMA address */
7569 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
7570 ((u64) tnapi->status_mapping >> 32));
7571 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
7572 ((u64) tnapi->status_mapping & 0xffffffff));
7573
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007574 if (tnapi->tx_ring) {
7575 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7576 (TG3_TX_RING_SIZE <<
7577 BDINFO_FLAGS_MAXLEN_SHIFT),
7578 NIC_SRAM_TX_BUFFER_DESC);
7579 txrcb += TG3_BDINFO_SIZE;
7580 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007581
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007582 if (tnapi->rx_rcb) {
7583 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
7584 (TG3_RX_RCB_RING_SIZE(tp) <<
7585 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
7586 rxrcb += TG3_BDINFO_SIZE;
7587 }
7588
7589 stblk = HOSTCC_STATBLCK_RING1;
7590
7591 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
7592 u64 mapping = (u64)tnapi->status_mapping;
7593 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
7594 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
7595
7596 /* Clear status block in ram. */
7597 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7598
Matt Carlson19cfaec2009-12-03 08:36:20 +00007599 if (tnapi->tx_ring) {
7600 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7601 (TG3_TX_RING_SIZE <<
7602 BDINFO_FLAGS_MAXLEN_SHIFT),
7603 NIC_SRAM_TX_BUFFER_DESC);
7604 txrcb += TG3_BDINFO_SIZE;
7605 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007606
7607 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
7608 (TG3_RX_RCB_RING_SIZE(tp) <<
7609 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
7610
7611 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007612 rxrcb += TG3_BDINFO_SIZE;
7613 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007614}
7615
7616/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07007617static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007618{
7619 u32 val, rdmac_mode;
7620 int i, err, limit;
Matt Carlson21f581a2009-08-28 14:00:25 +00007621 struct tg3_rx_prodring_set *tpr = &tp->prodring[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007622
7623 tg3_disable_ints(tp);
7624
7625 tg3_stop_fw(tp);
7626
7627 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
7628
Matt Carlson859a588792010-04-05 10:19:28 +00007629 if (tp->tg3_flags & TG3_FLAG_INIT_COMPLETE)
Michael Chane6de8ad2005-05-05 14:42:41 -07007630 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007631
Matt Carlson603f1172010-02-12 14:47:10 +00007632 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08007633 tg3_phy_reset(tp);
7634
Linus Torvalds1da177e2005-04-16 15:20:36 -07007635 err = tg3_chip_reset(tp);
7636 if (err)
7637 return err;
7638
7639 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
7640
Matt Carlsonbcb37f62008-11-03 16:52:09 -08007641 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07007642 val = tr32(TG3_CPMU_CTRL);
7643 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
7644 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08007645
7646 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
7647 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
7648 val |= CPMU_LSPD_10MB_MACCLK_6_25;
7649 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
7650
7651 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
7652 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
7653 val |= CPMU_LNK_AWARE_MACCLK_6_25;
7654 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
7655
7656 val = tr32(TG3_CPMU_HST_ACC);
7657 val &= ~CPMU_HST_ACC_MACCLK_MASK;
7658 val |= CPMU_HST_ACC_MACCLK_6_25;
7659 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07007660 }
7661
Matt Carlson33466d92009-04-20 06:57:41 +00007662 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7663 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
7664 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
7665 PCIE_PWR_MGMT_L1_THRESH_4MS;
7666 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00007667
7668 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
7669 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
7670
7671 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00007672
Matt Carlsonf40386c2009-11-02 14:24:02 +00007673 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7674 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00007675 }
7676
Matt Carlson614b0592010-01-20 16:58:02 +00007677 if (tp->tg3_flags3 & TG3_FLG3_L1PLLPD_EN) {
7678 u32 grc_mode = tr32(GRC_MODE);
7679
7680 /* Access the lower 1K of PL PCIE block registers. */
7681 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
7682 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
7683
7684 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
7685 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
7686 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
7687
7688 tw32(GRC_MODE, grc_mode);
7689 }
7690
Matt Carlsoncea46462010-04-12 06:58:24 +00007691 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
7692 u32 grc_mode = tr32(GRC_MODE);
7693
7694 /* Access the lower 1K of PL PCIE block registers. */
7695 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
7696 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
7697
7698 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5);
7699 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
7700 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
7701
7702 tw32(GRC_MODE, grc_mode);
Matt Carlsona977dbe2010-04-12 06:58:26 +00007703
7704 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
7705 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
7706 val |= CPMU_LSPD_10MB_MACCLK_6_25;
7707 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00007708 }
7709
Linus Torvalds1da177e2005-04-16 15:20:36 -07007710 /* This works around an issue with Athlon chipsets on
7711 * B3 tigon3 silicon. This bit has no effect on any
7712 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07007713 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007714 */
Matt Carlson795d01c2007-10-07 23:28:17 -07007715 if (!(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT)) {
7716 if (!(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
7717 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
7718 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007720
7721 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
7722 (tp->tg3_flags & TG3_FLAG_PCIX_MODE)) {
7723 val = tr32(TG3PCI_PCISTATE);
7724 val |= PCISTATE_RETRY_SAME_DMA;
7725 tw32(TG3PCI_PCISTATE, val);
7726 }
7727
Matt Carlson0d3031d2007-10-10 18:02:43 -07007728 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
7729 /* Allow reads and writes to the
7730 * APE register and memory space.
7731 */
7732 val = tr32(TG3PCI_PCISTATE);
7733 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00007734 PCISTATE_ALLOW_APE_SHMEM_WR |
7735 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07007736 tw32(TG3PCI_PCISTATE, val);
7737 }
7738
Linus Torvalds1da177e2005-04-16 15:20:36 -07007739 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
7740 /* Enable some hw fixes. */
7741 val = tr32(TG3PCI_MSI_DATA);
7742 val |= (1 << 26) | (1 << 28) | (1 << 29);
7743 tw32(TG3PCI_MSI_DATA, val);
7744 }
7745
7746 /* Descriptor ring init may make accesses to the
7747 * NIC SRAM area to setup the TX descriptors, so we
7748 * can only do this after the hardware has been
7749 * successfully reset.
7750 */
Michael Chan32d8c572006-07-25 16:38:29 -07007751 err = tg3_init_rings(tp);
7752 if (err)
7753 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007754
Matt Carlsonc885e822010-08-02 11:25:57 +00007755 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00007756 val = tr32(TG3PCI_DMA_RW_CTRL) &
7757 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Matt Carlson1a319022010-04-12 06:58:25 +00007758 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
7759 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00007760 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
7761 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
7762 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07007763 /* This value is determined during the probe time DMA
7764 * engine test, tg3_test_dma.
7765 */
7766 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
7767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007768
7769 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
7770 GRC_MODE_4X_NIC_SEND_RINGS |
7771 GRC_MODE_NO_TX_PHDR_CSUM |
7772 GRC_MODE_NO_RX_PHDR_CSUM);
7773 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07007774
7775 /* Pseudo-header checksum is done by hardware logic and not
7776 * the offload processers, so make the chip do the pseudo-
7777 * header checksums on receive. For transmit it is more
7778 * convenient to do the pseudo-header checksum in software
7779 * as Linux does that on transmit for us in all cases.
7780 */
7781 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007782
7783 tw32(GRC_MODE,
7784 tp->grc_mode |
7785 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
7786
7787 /* Setup the timer prescalar register. Clock is always 66Mhz. */
7788 val = tr32(GRC_MISC_CFG);
7789 val &= ~0xff;
7790 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
7791 tw32(GRC_MISC_CFG, val);
7792
7793 /* Initialize MBUF/DESC pool. */
John W. Linvillecbf46852005-04-21 17:01:29 -07007794 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007795 /* Do nothing. */
7796 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
7797 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
7798 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
7799 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
7800 else
7801 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
7802 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
7803 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Matt Carlson859a588792010-04-05 10:19:28 +00007804 } else if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007805 int fw_len;
7806
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007807 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007808 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
7809 tw32(BUFMGR_MB_POOL_ADDR,
7810 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
7811 tw32(BUFMGR_MB_POOL_SIZE,
7812 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
7813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007814
Michael Chan0f893dc2005-07-25 12:30:38 -07007815 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007816 tw32(BUFMGR_MB_RDMA_LOW_WATER,
7817 tp->bufmgr_config.mbuf_read_dma_low_water);
7818 tw32(BUFMGR_MB_MACRX_LOW_WATER,
7819 tp->bufmgr_config.mbuf_mac_rx_low_water);
7820 tw32(BUFMGR_MB_HIGH_WATER,
7821 tp->bufmgr_config.mbuf_high_water);
7822 } else {
7823 tw32(BUFMGR_MB_RDMA_LOW_WATER,
7824 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
7825 tw32(BUFMGR_MB_MACRX_LOW_WATER,
7826 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
7827 tw32(BUFMGR_MB_HIGH_WATER,
7828 tp->bufmgr_config.mbuf_high_water_jumbo);
7829 }
7830 tw32(BUFMGR_DMA_LOW_WATER,
7831 tp->bufmgr_config.dma_low_water);
7832 tw32(BUFMGR_DMA_HIGH_WATER,
7833 tp->bufmgr_config.dma_high_water);
7834
7835 tw32(BUFMGR_MODE, BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE);
7836 for (i = 0; i < 2000; i++) {
7837 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
7838 break;
7839 udelay(10);
7840 }
7841 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00007842 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007843 return -ENODEV;
7844 }
7845
7846 /* Setup replenish threshold. */
Michael Chanf92905d2006-06-29 20:14:29 -07007847 val = tp->rx_pending / 8;
7848 if (val == 0)
7849 val = 1;
7850 else if (val > tp->rx_std_max_post)
7851 val = tp->rx_std_max_post;
Michael Chanb5d37722006-09-27 16:06:21 -07007852 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7853 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
7854 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
7855
7856 if (val > (TG3_RX_INTERNAL_RING_SZ_5906 / 2))
7857 val = TG3_RX_INTERNAL_RING_SZ_5906 / 2;
7858 }
Michael Chanf92905d2006-06-29 20:14:29 -07007859
7860 tw32(RCVBDI_STD_THRESH, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007861
7862 /* Initialize TG3_BDINFO's at:
7863 * RCVDBDI_STD_BD: standard eth size rx ring
7864 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
7865 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
7866 *
7867 * like so:
7868 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
7869 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
7870 * ring attribute flags
7871 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
7872 *
7873 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
7874 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
7875 *
7876 * The size of each ring is fixed in the firmware, but the location is
7877 * configurable.
7878 */
7879 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00007880 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007881 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00007882 ((u64) tpr->rx_std_mapping & 0xffffffff));
Matt Carlsona50d0792010-06-05 17:24:37 +00007883 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
7884 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5719)
Matt Carlson87668d32009-11-13 13:03:34 +00007885 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
7886 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007887
Matt Carlsonfdb72b32009-08-28 13:57:12 +00007888 /* Disable the mini ring */
7889 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007890 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
7891 BDINFO_FLAGS_DISABLED);
7892
Matt Carlsonfdb72b32009-08-28 13:57:12 +00007893 /* Program the jumbo buffer descriptor ring control
7894 * blocks on those devices that have them.
7895 */
Matt Carlson8f666b02009-08-28 13:58:24 +00007896 if ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) &&
Matt Carlsonfdb72b32009-08-28 13:57:12 +00007897 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007898 /* Setup replenish threshold. */
7899 tw32(RCVBDI_JUMBO_THRESH, tp->rx_jumbo_pending / 8);
7900
Michael Chan0f893dc2005-07-25 12:30:38 -07007901 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007902 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00007903 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007904 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00007905 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007906 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlson79ed5ac2009-08-28 14:00:55 +00007907 (RX_JUMBO_MAX_SIZE << BDINFO_FLAGS_MAXLEN_SHIFT) |
7908 BDINFO_FLAGS_USE_EXT_RECV);
Matt Carlsona50d0792010-06-05 17:24:37 +00007909 if (!(tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) ||
7910 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson87668d32009-11-13 13:03:34 +00007911 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
7912 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007913 } else {
7914 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
7915 BDINFO_FLAGS_DISABLED);
7916 }
7917
Matt Carlsonc885e822010-08-02 11:25:57 +00007918 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007919 val = (RX_STD_MAX_SIZE_5705 << BDINFO_FLAGS_MAXLEN_SHIFT) |
Matt Carlson04380d42010-04-12 06:58:29 +00007920 (TG3_RX_STD_DMA_SZ << 2);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007921 else
Matt Carlson04380d42010-04-12 06:58:29 +00007922 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00007923 } else
7924 val = RX_STD_MAX_SIZE_5705 << BDINFO_FLAGS_MAXLEN_SHIFT;
7925
7926 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007927
Matt Carlson411da642009-11-13 13:03:46 +00007928 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e62009-11-13 13:03:49 +00007929 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007930
Matt Carlson411da642009-11-13 13:03:46 +00007931 tpr->rx_jmb_prod_idx = (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) ?
Matt Carlson21f581a2009-08-28 14:00:25 +00007932 tp->rx_jumbo_pending : 0;
Matt Carlson66711e62009-11-13 13:03:49 +00007933 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007934
Matt Carlsonc885e822010-08-02 11:25:57 +00007935 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007936 tw32(STD_REPLENISH_LWM, 32);
7937 tw32(JMB_REPLENISH_LWM, 16);
7938 }
7939
Matt Carlson2d31eca2009-09-01 12:53:31 +00007940 tg3_rings_reset(tp);
7941
Linus Torvalds1da177e2005-04-16 15:20:36 -07007942 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07007943 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007944
7945 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00007946 tw32(MAC_RX_MTU_SIZE,
7947 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007948
7949 /* The slot time is changed by tg3_setup_phy if we
7950 * run at gigabit with half duplex.
7951 */
7952 tw32(MAC_TX_LENGTHS,
7953 (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
7954 (6 << TX_LENGTHS_IPG_SHIFT) |
7955 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
7956
7957 /* Receive rules. */
7958 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
7959 tw32(RCVLPC_CONFIG, 0x0181);
7960
7961 /* Calculate RDMAC_MODE setting early, we need it to determine
7962 * the RCVLPC_STATE_ENABLE mask.
7963 */
7964 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
7965 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
7966 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
7967 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
7968 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07007969
Matt Carlsona50d0792010-06-05 17:24:37 +00007970 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
7971 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlson0339e4e2010-02-12 14:47:09 +00007972 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
7973
Matt Carlson57e69832008-05-25 23:48:31 -07007974 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08007975 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
7976 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07007977 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
7978 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
7979 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
7980
Michael Chan85e94ce2005-04-21 17:05:28 -07007981 /* If statement applies to 5705 and 5750 PCI devices only */
7982 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
7983 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) ||
7984 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007985 if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07007986 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007987 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
7988 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
7989 !(tp->tg3_flags2 & TG3_FLG2_IS_5788)) {
7990 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
7991 }
7992 }
7993
Michael Chan85e94ce2005-04-21 17:05:28 -07007994 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)
7995 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
7996
Linus Torvalds1da177e2005-04-16 15:20:36 -07007997 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
Matt Carlson027455a2008-12-21 20:19:30 -08007998 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
7999
Matt Carlsone849cdc2009-11-13 13:03:38 +00008000 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
8001 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson027455a2008-12-21 20:19:30 -08008002 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
8003 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008004
8005 /* Receive/send statistics. */
Michael Chan16613942006-06-29 20:15:13 -07008006 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
8007 val = tr32(RCVLPC_STATS_ENABLE);
8008 val &= ~RCVLPC_STATSENAB_DACK_FIX;
8009 tw32(RCVLPC_STATS_ENABLE, val);
8010 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
8011 (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008012 val = tr32(RCVLPC_STATS_ENABLE);
8013 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
8014 tw32(RCVLPC_STATS_ENABLE, val);
8015 } else {
8016 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
8017 }
8018 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
8019 tw32(SNDDATAI_STATSENAB, 0xffffff);
8020 tw32(SNDDATAI_STATSCTRL,
8021 (SNDDATAI_SCTRL_ENABLE |
8022 SNDDATAI_SCTRL_FASTUPD));
8023
8024 /* Setup host coalescing engine. */
8025 tw32(HOSTCC_MODE, 0);
8026 for (i = 0; i < 2000; i++) {
8027 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
8028 break;
8029 udelay(10);
8030 }
8031
Michael Chand244c892005-07-05 14:42:33 -07008032 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008033
Linus Torvalds1da177e2005-04-16 15:20:36 -07008034 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
8035 /* Status/statistics block address. See tg3_timer,
8036 * the tg3_periodic_fetch_stats call there, and
8037 * tg3_get_stats to see how this works for 5705/5750 chips.
8038 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008039 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8040 ((u64) tp->stats_mapping >> 32));
8041 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8042 ((u64) tp->stats_mapping & 0xffffffff));
8043 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008044
Linus Torvalds1da177e2005-04-16 15:20:36 -07008045 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008046
8047 /* Clear statistics and status block memory areas */
8048 for (i = NIC_SRAM_STATS_BLK;
8049 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
8050 i += sizeof(u32)) {
8051 tg3_write_mem(tp, i, 0);
8052 udelay(40);
8053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008054 }
8055
8056 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
8057
8058 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
8059 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
8060 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
8061 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
8062
Michael Chanc94e3942005-09-27 12:12:42 -07008063 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES) {
8064 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
8065 /* reset to prevent losing 1st rx packet intermittently */
8066 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8067 udelay(10);
8068 }
8069
Matt Carlson3bda1252008-08-15 14:08:22 -07008070 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
8071 tp->mac_mode &= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
8072 else
8073 tp->mac_mode = 0;
8074 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Linus Torvalds1da177e2005-04-16 15:20:36 -07008075 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE | MAC_MODE_FHDE_ENABLE;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07008076 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
8077 !(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
8078 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
8079 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008080 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
8081 udelay(40);
8082
Michael Chan314fba32005-04-21 17:07:04 -07008083 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Michael Chan9d26e212006-12-07 00:21:14 -08008084 * If TG3_FLG2_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07008085 * register to preserve the GPIO settings for LOMs. The GPIOs,
8086 * whether used as inputs or outputs, are set by boot code after
8087 * reset.
8088 */
Michael Chan9d26e212006-12-07 00:21:14 -08008089 if (!(tp->tg3_flags2 & TG3_FLG2_IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07008090 u32 gpio_mask;
8091
Michael Chan9d26e212006-12-07 00:21:14 -08008092 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
8093 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
8094 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07008095
8096 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
8097 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
8098 GRC_LCLCTRL_GPIO_OUTPUT3;
8099
Michael Chanaf36e6b2006-03-23 01:28:06 -08008100 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
8101 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
8102
Gary Zambranoaaf84462007-05-05 11:51:45 -07008103 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07008104 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
8105
8106 /* GPIO1 must be driven high for eeprom write protect */
Michael Chan9d26e212006-12-07 00:21:14 -08008107 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT)
8108 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
8109 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07008110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008111 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8112 udelay(100);
8113
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008114 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX) {
8115 val = tr32(MSGINT_MODE);
8116 val |= MSGINT_MODE_MULTIVEC_EN | MSGINT_MODE_ENABLE;
8117 tw32(MSGINT_MODE, val);
8118 }
8119
Linus Torvalds1da177e2005-04-16 15:20:36 -07008120 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
8121 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
8122 udelay(40);
8123 }
8124
8125 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
8126 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
8127 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
8128 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
8129 WDMAC_MODE_LNGREAD_ENAB);
8130
Michael Chan85e94ce2005-04-21 17:05:28 -07008131 /* If statement applies to 5705 and 5750 PCI devices only */
8132 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8133 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) ||
8134 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750) {
Matt Carlson29ea0952009-08-25 10:07:54 +00008135 if ((tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008136 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
8137 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
8138 /* nothing */
8139 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
8140 !(tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
8141 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)) {
8142 val |= WDMAC_MODE_RX_ACCEL;
8143 }
8144 }
8145
Michael Chand9ab5ad2006-03-20 22:27:35 -08008146 /* Enable host coalescing bug fix */
Matt Carlson321d32a2008-11-21 17:22:19 -08008147 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Matt Carlsonf51f3562008-05-25 23:45:08 -07008148 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad2006-03-20 22:27:35 -08008149
Matt Carlson788a0352009-11-02 14:26:03 +00008150 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
8151 val |= WDMAC_MODE_BURST_ALL_DATA;
8152
Linus Torvalds1da177e2005-04-16 15:20:36 -07008153 tw32_f(WDMAC_MODE, val);
8154 udelay(40);
8155
Matt Carlson9974a352007-10-07 23:27:28 -07008156 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
8157 u16 pcix_cmd;
8158
8159 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8160 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008161 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07008162 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
8163 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008164 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07008165 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
8166 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008167 }
Matt Carlson9974a352007-10-07 23:27:28 -07008168 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8169 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008170 }
8171
8172 tw32_f(RDMAC_MODE, rdmac_mode);
8173 udelay(40);
8174
8175 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
8176 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
8177 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07008178
8179 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
8180 tw32(SNDDATAC_MODE,
8181 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
8182 else
8183 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
8184
Linus Torvalds1da177e2005-04-16 15:20:36 -07008185 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
8186 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
8187 tw32(RCVDBDI_MODE, RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ);
8188 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008189 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
8190 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008191 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Matt Carlson19cfaec2009-12-03 08:36:20 +00008192 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008193 val |= SNDBDI_MODE_MULTI_TXQ_EN;
8194 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008195 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
8196
8197 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
8198 err = tg3_load_5701_a0_firmware_fix(tp);
8199 if (err)
8200 return err;
8201 }
8202
Linus Torvalds1da177e2005-04-16 15:20:36 -07008203 if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) {
8204 err = tg3_load_tso_firmware(tp);
8205 if (err)
8206 return err;
8207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008208
8209 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonb1d05212010-06-05 17:24:31 +00008210 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
8211 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
8212 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008213 tw32_f(MAC_TX_MODE, tp->tx_mode);
8214 udelay(100);
8215
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008216 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS) {
8217 u32 reg = MAC_RSS_INDIR_TBL_0;
8218 u8 *ent = (u8 *)&val;
8219
8220 /* Setup the indirection table */
8221 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
8222 int idx = i % sizeof(val);
8223
Matt Carlson5efeeea2010-07-11 09:31:40 +00008224 ent[idx] = i % (tp->irq_cnt - 1);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008225 if (idx == sizeof(val) - 1) {
8226 tw32(reg, val);
8227 reg += 4;
8228 }
8229 }
8230
8231 /* Setup the "secret" hash key. */
8232 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
8233 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
8234 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
8235 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
8236 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
8237 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
8238 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
8239 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
8240 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
8241 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
8242 }
8243
Linus Torvalds1da177e2005-04-16 15:20:36 -07008244 tp->rx_mode = RX_MODE_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08008245 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Michael Chanaf36e6b2006-03-23 01:28:06 -08008246 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
8247
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008248 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)
8249 tp->rx_mode |= RX_MODE_RSS_ENABLE |
8250 RX_MODE_RSS_ITBL_HASH_BITS_7 |
8251 RX_MODE_RSS_IPV6_HASH_EN |
8252 RX_MODE_RSS_TCP_IPV6_HASH_EN |
8253 RX_MODE_RSS_IPV4_HASH_EN |
8254 RX_MODE_RSS_TCP_IPV4_HASH_EN;
8255
Linus Torvalds1da177e2005-04-16 15:20:36 -07008256 tw32_f(MAC_RX_MODE, tp->rx_mode);
8257 udelay(10);
8258
Linus Torvalds1da177e2005-04-16 15:20:36 -07008259 tw32(MAC_LED_CTRL, tp->led_ctrl);
8260
8261 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Michael Chanc94e3942005-09-27 12:12:42 -07008262 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008263 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8264 udelay(10);
8265 }
8266 tw32_f(MAC_RX_MODE, tp->rx_mode);
8267 udelay(10);
8268
8269 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
8270 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
8271 !(tp->tg3_flags2 & TG3_FLG2_SERDES_PREEMPHASIS)) {
8272 /* Set drive transmission level to 1.2V */
8273 /* only if the signal pre-emphasis bit is not set */
8274 val = tr32(MAC_SERDES_CFG);
8275 val &= 0xfffff000;
8276 val |= 0x880;
8277 tw32(MAC_SERDES_CFG, val);
8278 }
8279 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
8280 tw32(MAC_SERDES_CFG, 0x616000);
8281 }
8282
8283 /* Prevent chip from dropping frames when flow control
8284 * is enabled.
8285 */
Matt Carlson666bc832010-01-20 16:58:03 +00008286 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8287 val = 1;
8288 else
8289 val = 2;
8290 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008291
8292 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
8293 (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)) {
8294 /* Use hardware link auto-negotiation */
8295 tp->tg3_flags2 |= TG3_FLG2_HW_AUTONEG;
8296 }
8297
Michael Chand4d2c552006-03-20 17:47:20 -08008298 if ((tp->tg3_flags2 & TG3_FLG2_MII_SERDES) &&
8299 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)) {
8300 u32 tmp;
8301
8302 tmp = tr32(SERDES_RX_CTRL);
8303 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
8304 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
8305 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
8306 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8307 }
8308
Matt Carlsondd477002008-05-25 23:45:58 -07008309 if (!(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)) {
8310 if (tp->link_config.phy_is_low_power) {
8311 tp->link_config.phy_is_low_power = 0;
8312 tp->link_config.speed = tp->link_config.orig_speed;
8313 tp->link_config.duplex = tp->link_config.orig_duplex;
8314 tp->link_config.autoneg = tp->link_config.orig_autoneg;
8315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008316
Matt Carlsondd477002008-05-25 23:45:58 -07008317 err = tg3_setup_phy(tp, 0);
8318 if (err)
8319 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008320
Matt Carlsondd477002008-05-25 23:45:58 -07008321 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
Matt Carlson7f97a4b2009-08-25 10:10:03 +00008322 !(tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07008323 u32 tmp;
8324
8325 /* Clear CRC stats. */
8326 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
8327 tg3_writephy(tp, MII_TG3_TEST1,
8328 tmp | MII_TG3_TEST1_CRC_EN);
8329 tg3_readphy(tp, 0x14, &tmp);
8330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008331 }
8332 }
8333
8334 __tg3_set_rx_mode(tp->dev);
8335
8336 /* Initialize receive rules. */
8337 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
8338 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
8339 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
8340 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
8341
Michael Chan4cf78e42005-07-25 12:29:19 -07008342 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Michael Chana4e2b342005-10-26 15:46:52 -07008343 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008344 limit = 8;
8345 else
8346 limit = 16;
8347 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF)
8348 limit -= 4;
8349 switch (limit) {
8350 case 16:
8351 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
8352 case 15:
8353 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
8354 case 14:
8355 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
8356 case 13:
8357 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
8358 case 12:
8359 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
8360 case 11:
8361 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
8362 case 10:
8363 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
8364 case 9:
8365 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
8366 case 8:
8367 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
8368 case 7:
8369 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
8370 case 6:
8371 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
8372 case 5:
8373 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
8374 case 4:
8375 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
8376 case 3:
8377 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
8378 case 2:
8379 case 1:
8380
8381 default:
8382 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07008383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008384
Matt Carlson9ce768e2007-10-11 19:49:11 -07008385 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
8386 /* Write our heartbeat update interval to APE. */
8387 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
8388 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07008389
Linus Torvalds1da177e2005-04-16 15:20:36 -07008390 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
8391
Linus Torvalds1da177e2005-04-16 15:20:36 -07008392 return 0;
8393}
8394
8395/* Called at device open time to get the chip ready for
8396 * packet processing. Invoked with tp->lock held.
8397 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008398static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008399{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008400 tg3_switch_clocks(tp);
8401
8402 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
8403
Matt Carlson2f751b62008-08-04 23:17:34 -07008404 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008405}
8406
8407#define TG3_STAT_ADD32(PSTAT, REG) \
8408do { u32 __val = tr32(REG); \
8409 (PSTAT)->low += __val; \
8410 if ((PSTAT)->low < __val) \
8411 (PSTAT)->high += 1; \
8412} while (0)
8413
8414static void tg3_periodic_fetch_stats(struct tg3 *tp)
8415{
8416 struct tg3_hw_stats *sp = tp->hw_stats;
8417
8418 if (!netif_carrier_ok(tp->dev))
8419 return;
8420
8421 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
8422 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
8423 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
8424 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
8425 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
8426 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
8427 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
8428 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
8429 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
8430 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
8431 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
8432 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
8433 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
8434
8435 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
8436 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
8437 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
8438 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
8439 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
8440 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
8441 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
8442 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
8443 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
8444 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
8445 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
8446 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
8447 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
8448 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07008449
8450 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
8451 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
8452 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008453}
8454
8455static void tg3_timer(unsigned long __opaque)
8456{
8457 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008458
Michael Chanf475f162006-03-27 23:20:14 -08008459 if (tp->irq_sync)
8460 goto restart_timer;
8461
David S. Millerf47c11e2005-06-24 20:18:35 -07008462 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008463
David S. Millerfac9b832005-05-18 22:46:34 -07008464 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)) {
8465 /* All of this garbage is because when using non-tagged
8466 * IRQ status the mailbox/status_block protocol the chip
8467 * uses with the cpu is race prone.
8468 */
Matt Carlson898a56f2009-08-28 14:02:40 +00008469 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -07008470 tw32(GRC_LOCAL_CTRL,
8471 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
8472 } else {
8473 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00008474 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -07008475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008476
David S. Millerfac9b832005-05-18 22:46:34 -07008477 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
8478 tp->tg3_flags2 |= TG3_FLG2_RESTART_TIMER;
David S. Millerf47c11e2005-06-24 20:18:35 -07008479 spin_unlock(&tp->lock);
David S. Millerfac9b832005-05-18 22:46:34 -07008480 schedule_work(&tp->reset_task);
8481 return;
8482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008483 }
8484
Linus Torvalds1da177e2005-04-16 15:20:36 -07008485 /* This part only runs once per second. */
8486 if (!--tp->timer_counter) {
David S. Millerfac9b832005-05-18 22:46:34 -07008487 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
8488 tg3_periodic_fetch_stats(tp);
8489
Linus Torvalds1da177e2005-04-16 15:20:36 -07008490 if (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) {
8491 u32 mac_stat;
8492 int phy_event;
8493
8494 mac_stat = tr32(MAC_STATUS);
8495
8496 phy_event = 0;
8497 if (tp->tg3_flags & TG3_FLAG_USE_MI_INTERRUPT) {
8498 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
8499 phy_event = 1;
8500 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
8501 phy_event = 1;
8502
8503 if (phy_event)
8504 tg3_setup_phy(tp, 0);
8505 } else if (tp->tg3_flags & TG3_FLAG_POLL_SERDES) {
8506 u32 mac_stat = tr32(MAC_STATUS);
8507 int need_setup = 0;
8508
8509 if (netif_carrier_ok(tp->dev) &&
8510 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
8511 need_setup = 1;
8512 }
Matt Carlsonbe98da62010-07-11 09:31:46 +00008513 if (!netif_carrier_ok(tp->dev) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008514 (mac_stat & (MAC_STATUS_PCS_SYNCED |
8515 MAC_STATUS_SIGNAL_DET))) {
8516 need_setup = 1;
8517 }
8518 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07008519 if (!tp->serdes_counter) {
8520 tw32_f(MAC_MODE,
8521 (tp->mac_mode &
8522 ~MAC_MODE_PORT_MODE_MASK));
8523 udelay(40);
8524 tw32_f(MAC_MODE, tp->mac_mode);
8525 udelay(40);
8526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008527 tg3_setup_phy(tp, 0);
8528 }
Matt Carlson57d8b882010-06-05 17:24:35 +00008529 } else if ((tp->tg3_flags2 & TG3_FLG2_MII_SERDES) &&
Matt Carlson2138c002010-07-11 09:31:43 +00008530 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -07008531 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +00008532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008533
8534 tp->timer_counter = tp->timer_multiplier;
8535 }
8536
Michael Chan130b8e42006-09-27 16:00:40 -07008537 /* Heartbeat is only sent once every 2 seconds.
8538 *
8539 * The heartbeat is to tell the ASF firmware that the host
8540 * driver is still alive. In the event that the OS crashes,
8541 * ASF needs to reset the hardware to free up the FIFO space
8542 * that may be filled with rx packets destined for the host.
8543 * If the FIFO is full, ASF will no longer function properly.
8544 *
8545 * Unintended resets have been reported on real time kernels
8546 * where the timer doesn't run on time. Netpoll will also have
8547 * same problem.
8548 *
8549 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
8550 * to check the ring condition when the heartbeat is expiring
8551 * before doing the reset. This will prevent most unintended
8552 * resets.
8553 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008554 if (!--tp->asf_counter) {
Matt Carlsonbc7959b2008-08-15 14:08:55 -07008555 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
8556 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07008557 tg3_wait_for_event_ack(tp);
8558
Michael Chanbbadf502006-04-06 21:46:34 -07008559 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07008560 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07008561 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +00008562 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
8563 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -07008564
8565 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008566 }
8567 tp->asf_counter = tp->asf_multiplier;
8568 }
8569
David S. Millerf47c11e2005-06-24 20:18:35 -07008570 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008571
Michael Chanf475f162006-03-27 23:20:14 -08008572restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07008573 tp->timer.expires = jiffies + tp->timer_offset;
8574 add_timer(&tp->timer);
8575}
8576
Matt Carlson4f125f42009-09-01 12:55:02 +00008577static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -08008578{
David Howells7d12e782006-10-05 14:55:46 +01008579 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008580 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +00008581 char *name;
8582 struct tg3_napi *tnapi = &tp->napi[irq_num];
8583
8584 if (tp->irq_cnt == 1)
8585 name = tp->dev->name;
8586 else {
8587 name = &tnapi->irq_lbl[0];
8588 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
8589 name[IFNAMSIZ-1] = 0;
8590 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08008591
Matt Carlson679563f2009-09-01 12:55:46 +00008592 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI_OR_MSIX) {
Michael Chanfcfa0a32006-03-20 22:28:41 -08008593 fn = tg3_msi;
8594 if (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)
8595 fn = tg3_msi_1shot;
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07008596 flags = IRQF_SAMPLE_RANDOM;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008597 } else {
8598 fn = tg3_interrupt;
8599 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)
8600 fn = tg3_interrupt_tagged;
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07008601 flags = IRQF_SHARED | IRQF_SAMPLE_RANDOM;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008602 }
Matt Carlson4f125f42009-09-01 12:55:02 +00008603
8604 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08008605}
8606
Michael Chan79381092005-04-21 17:13:59 -07008607static int tg3_test_interrupt(struct tg3 *tp)
8608{
Matt Carlson09943a12009-08-28 14:01:57 +00008609 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -07008610 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -07008611 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008612 u32 val;
Michael Chan79381092005-04-21 17:13:59 -07008613
Michael Chand4bc3922005-05-29 14:59:20 -07008614 if (!netif_running(dev))
8615 return -ENODEV;
8616
Michael Chan79381092005-04-21 17:13:59 -07008617 tg3_disable_ints(tp);
8618
Matt Carlson4f125f42009-09-01 12:55:02 +00008619 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07008620
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008621 /*
8622 * Turn off MSI one shot mode. Otherwise this test has no
8623 * observable way to know whether the interrupt was delivered.
8624 */
Matt Carlsonc885e822010-08-02 11:25:57 +00008625 if ((tp->tg3_flags3 & TG3_FLG3_5717_PLUS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008626 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
8627 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
8628 tw32(MSGINT_MODE, val);
8629 }
8630
Matt Carlson4f125f42009-09-01 12:55:02 +00008631 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Matt Carlson09943a12009-08-28 14:01:57 +00008632 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07008633 if (err)
8634 return err;
8635
Matt Carlson898a56f2009-08-28 14:02:40 +00008636 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -07008637 tg3_enable_ints(tp);
8638
8639 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00008640 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -07008641
8642 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -07008643 u32 int_mbox, misc_host_ctrl;
8644
Matt Carlson898a56f2009-08-28 14:02:40 +00008645 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -07008646 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
8647
8648 if ((int_mbox != 0) ||
8649 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
8650 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -07008651 break;
Michael Chanb16250e2006-09-27 16:10:14 -07008652 }
8653
Michael Chan79381092005-04-21 17:13:59 -07008654 msleep(10);
8655 }
8656
8657 tg3_disable_ints(tp);
8658
Matt Carlson4f125f42009-09-01 12:55:02 +00008659 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008660
Matt Carlson4f125f42009-09-01 12:55:02 +00008661 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07008662
8663 if (err)
8664 return err;
8665
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008666 if (intr_ok) {
8667 /* Reenable MSI one shot mode. */
Matt Carlsonc885e822010-08-02 11:25:57 +00008668 if ((tp->tg3_flags3 & TG3_FLG3_5717_PLUS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008669 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
8670 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
8671 tw32(MSGINT_MODE, val);
8672 }
Michael Chan79381092005-04-21 17:13:59 -07008673 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008674 }
Michael Chan79381092005-04-21 17:13:59 -07008675
8676 return -EIO;
8677}
8678
8679/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
8680 * successfully restored
8681 */
8682static int tg3_test_msi(struct tg3 *tp)
8683{
Michael Chan79381092005-04-21 17:13:59 -07008684 int err;
8685 u16 pci_cmd;
8686
8687 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSI))
8688 return 0;
8689
8690 /* Turn off SERR reporting in case MSI terminates with Master
8691 * Abort.
8692 */
8693 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
8694 pci_write_config_word(tp->pdev, PCI_COMMAND,
8695 pci_cmd & ~PCI_COMMAND_SERR);
8696
8697 err = tg3_test_interrupt(tp);
8698
8699 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
8700
8701 if (!err)
8702 return 0;
8703
8704 /* other failures */
8705 if (err != -EIO)
8706 return err;
8707
8708 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +00008709 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
8710 "to INTx mode. Please report this failure to the PCI "
8711 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -07008712
Matt Carlson4f125f42009-09-01 12:55:02 +00008713 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +00008714
Michael Chan79381092005-04-21 17:13:59 -07008715 pci_disable_msi(tp->pdev);
8716
8717 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI;
Andre Detschdc8bf1b2010-04-26 07:27:07 +00008718 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -07008719
Matt Carlson4f125f42009-09-01 12:55:02 +00008720 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07008721 if (err)
8722 return err;
8723
8724 /* Need to reset the chip because the MSI cycle may have terminated
8725 * with Master Abort.
8726 */
David S. Millerf47c11e2005-06-24 20:18:35 -07008727 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07008728
Michael Chan944d9802005-05-29 14:57:48 -07008729 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008730 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07008731
David S. Millerf47c11e2005-06-24 20:18:35 -07008732 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07008733
8734 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +00008735 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -07008736
8737 return err;
8738}
8739
Matt Carlson9e9fd122009-01-19 16:57:45 -08008740static int tg3_request_firmware(struct tg3 *tp)
8741{
8742 const __be32 *fw_data;
8743
8744 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00008745 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
8746 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08008747 return -ENOENT;
8748 }
8749
8750 fw_data = (void *)tp->fw->data;
8751
8752 /* Firmware blob starts with version numbers, followed by
8753 * start address and _full_ length including BSS sections
8754 * (which must be longer than the actual data, of course
8755 */
8756
8757 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
8758 if (tp->fw_len < (tp->fw->size - 12)) {
Joe Perches05dbe002010-02-17 19:44:19 +00008759 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
8760 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08008761 release_firmware(tp->fw);
8762 tp->fw = NULL;
8763 return -EINVAL;
8764 }
8765
8766 /* We no longer need firmware; we have it. */
8767 tp->fw_needed = NULL;
8768 return 0;
8769}
8770
Matt Carlson679563f2009-09-01 12:55:46 +00008771static bool tg3_enable_msix(struct tg3 *tp)
8772{
8773 int i, rc, cpus = num_online_cpus();
8774 struct msix_entry msix_ent[tp->irq_max];
8775
8776 if (cpus == 1)
8777 /* Just fallback to the simpler MSI mode. */
8778 return false;
8779
8780 /*
8781 * We want as many rx rings enabled as there are cpus.
8782 * The first MSIX vector only deals with link interrupts, etc,
8783 * so we add one to the number of vectors we are requesting.
8784 */
8785 tp->irq_cnt = min_t(unsigned, cpus + 1, tp->irq_max);
8786
8787 for (i = 0; i < tp->irq_max; i++) {
8788 msix_ent[i].entry = i;
8789 msix_ent[i].vector = 0;
8790 }
8791
8792 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +00008793 if (rc < 0) {
8794 return false;
8795 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +00008796 if (pci_enable_msix(tp->pdev, msix_ent, rc))
8797 return false;
Joe Perches05dbe002010-02-17 19:44:19 +00008798 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
8799 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +00008800 tp->irq_cnt = rc;
8801 }
8802
8803 for (i = 0; i < tp->irq_max; i++)
8804 tp->napi[i].irq_vec = msix_ent[i].vector;
8805
Matt Carlson2430b032010-06-05 17:24:34 +00008806 tp->dev->real_num_tx_queues = 1;
8807 if (tp->irq_cnt > 1) {
8808 tp->tg3_flags3 |= TG3_FLG3_ENABLE_RSS;
8809
Matt Carlsona50d0792010-06-05 17:24:37 +00008810 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
8811 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) {
Matt Carlson2430b032010-06-05 17:24:34 +00008812 tp->tg3_flags3 |= TG3_FLG3_ENABLE_TSS;
8813 tp->dev->real_num_tx_queues = tp->irq_cnt - 1;
8814 }
8815 }
Matt Carlsonfe5f5782009-09-01 13:09:39 +00008816
Matt Carlson679563f2009-09-01 12:55:46 +00008817 return true;
8818}
8819
Matt Carlson07b01732009-08-28 14:01:15 +00008820static void tg3_ints_init(struct tg3 *tp)
8821{
Matt Carlson679563f2009-09-01 12:55:46 +00008822 if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSI_OR_MSIX) &&
8823 !(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +00008824 /* All MSI supporting chips should support tagged
8825 * status. Assert that this is the case.
8826 */
Matt Carlson5129c3a2010-04-05 10:19:23 +00008827 netdev_warn(tp->dev,
8828 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +00008829 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +00008830 }
Matt Carlson4f125f42009-09-01 12:55:02 +00008831
Matt Carlson679563f2009-09-01 12:55:46 +00008832 if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX) && tg3_enable_msix(tp))
8833 tp->tg3_flags2 |= TG3_FLG2_USING_MSIX;
8834 else if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSI) &&
8835 pci_enable_msi(tp->pdev) == 0)
8836 tp->tg3_flags2 |= TG3_FLG2_USING_MSI;
8837
8838 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI_OR_MSIX) {
8839 u32 msi_mode = tr32(MSGINT_MODE);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008840 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
8841 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson679563f2009-09-01 12:55:46 +00008842 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
8843 }
8844defcfg:
8845 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSIX)) {
8846 tp->irq_cnt = 1;
8847 tp->napi[0].irq_vec = tp->pdev->irq;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00008848 tp->dev->real_num_tx_queues = 1;
Matt Carlson679563f2009-09-01 12:55:46 +00008849 }
Matt Carlson07b01732009-08-28 14:01:15 +00008850}
8851
8852static void tg3_ints_fini(struct tg3 *tp)
8853{
Matt Carlson679563f2009-09-01 12:55:46 +00008854 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
8855 pci_disable_msix(tp->pdev);
8856 else if (tp->tg3_flags2 & TG3_FLG2_USING_MSI)
8857 pci_disable_msi(tp->pdev);
8858 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI_OR_MSIX;
Matt Carlson774ee752010-08-02 11:25:56 +00008859 tp->tg3_flags3 &= ~(TG3_FLG3_ENABLE_RSS | TG3_FLG3_ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +00008860}
8861
Linus Torvalds1da177e2005-04-16 15:20:36 -07008862static int tg3_open(struct net_device *dev)
8863{
8864 struct tg3 *tp = netdev_priv(dev);
Matt Carlson4f125f42009-09-01 12:55:02 +00008865 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008866
Matt Carlson9e9fd122009-01-19 16:57:45 -08008867 if (tp->fw_needed) {
8868 err = tg3_request_firmware(tp);
8869 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
8870 if (err)
8871 return err;
8872 } else if (err) {
Joe Perches05dbe002010-02-17 19:44:19 +00008873 netdev_warn(tp->dev, "TSO capability disabled\n");
Matt Carlson9e9fd122009-01-19 16:57:45 -08008874 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
8875 } else if (!(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
Joe Perches05dbe002010-02-17 19:44:19 +00008876 netdev_notice(tp->dev, "TSO capability restored\n");
Matt Carlson9e9fd122009-01-19 16:57:45 -08008877 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
8878 }
8879 }
8880
Michael Chanc49a1562006-12-17 17:07:29 -08008881 netif_carrier_off(tp->dev);
8882
Michael Chanbc1c7562006-03-20 17:48:03 -08008883 err = tg3_set_power_state(tp, PCI_D0);
Matt Carlson2f751b62008-08-04 23:17:34 -07008884 if (err)
Michael Chanbc1c7562006-03-20 17:48:03 -08008885 return err;
Matt Carlson2f751b62008-08-04 23:17:34 -07008886
8887 tg3_full_lock(tp, 0);
Michael Chanbc1c7562006-03-20 17:48:03 -08008888
Linus Torvalds1da177e2005-04-16 15:20:36 -07008889 tg3_disable_ints(tp);
8890 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
8891
David S. Millerf47c11e2005-06-24 20:18:35 -07008892 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008893
Matt Carlson679563f2009-09-01 12:55:46 +00008894 /*
8895 * Setup interrupts first so we know how
8896 * many NAPI resources to allocate
8897 */
8898 tg3_ints_init(tp);
8899
Linus Torvalds1da177e2005-04-16 15:20:36 -07008900 /* The placement of this call is tied
8901 * to the setup and use of Host TX descriptors.
8902 */
8903 err = tg3_alloc_consistent(tp);
8904 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00008905 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008906
Matt Carlsonfed97812009-09-01 13:10:19 +00008907 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07008908
Matt Carlson4f125f42009-09-01 12:55:02 +00008909 for (i = 0; i < tp->irq_cnt; i++) {
8910 struct tg3_napi *tnapi = &tp->napi[i];
8911 err = tg3_request_irq(tp, i);
8912 if (err) {
8913 for (i--; i >= 0; i--)
8914 free_irq(tnapi->irq_vec, tnapi);
8915 break;
8916 }
8917 }
Matt Carlson07b01732009-08-28 14:01:15 +00008918
8919 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00008920 goto err_out2;
Matt Carlson07b01732009-08-28 14:01:15 +00008921
David S. Millerf47c11e2005-06-24 20:18:35 -07008922 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008923
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008924 err = tg3_init_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008925 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -07008926 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008927 tg3_free_rings(tp);
8928 } else {
David S. Millerfac9b832005-05-18 22:46:34 -07008929 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)
8930 tp->timer_offset = HZ;
8931 else
8932 tp->timer_offset = HZ / 10;
8933
8934 BUG_ON(tp->timer_offset > HZ);
8935 tp->timer_counter = tp->timer_multiplier =
8936 (HZ / tp->timer_offset);
8937 tp->asf_counter = tp->asf_multiplier =
Michael Chan28fbef72005-10-26 15:48:35 -07008938 ((HZ / tp->timer_offset) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008939
8940 init_timer(&tp->timer);
8941 tp->timer.expires = jiffies + tp->timer_offset;
8942 tp->timer.data = (unsigned long) tp;
8943 tp->timer.function = tg3_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008944 }
8945
David S. Millerf47c11e2005-06-24 20:18:35 -07008946 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008947
Matt Carlson07b01732009-08-28 14:01:15 +00008948 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00008949 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008950
Michael Chan79381092005-04-21 17:13:59 -07008951 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
8952 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -07008953
Michael Chan79381092005-04-21 17:13:59 -07008954 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -07008955 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -07008956 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -07008957 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07008958 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07008959
Matt Carlson679563f2009-09-01 12:55:46 +00008960 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -07008961 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08008962
Matt Carlsonc885e822010-08-02 11:25:57 +00008963 if (!(tp->tg3_flags3 & TG3_FLG3_5717_PLUS) &&
8964 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008965 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -08008966
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008967 tw32(PCIE_TRANSACTION_CFG,
8968 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -08008969 }
Michael Chan79381092005-04-21 17:13:59 -07008970 }
8971
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07008972 tg3_phy_start(tp);
8973
David S. Millerf47c11e2005-06-24 20:18:35 -07008974 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008975
Michael Chan79381092005-04-21 17:13:59 -07008976 add_timer(&tp->timer);
8977 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008978 tg3_enable_ints(tp);
8979
David S. Millerf47c11e2005-06-24 20:18:35 -07008980 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008981
Matt Carlsonfe5f5782009-09-01 13:09:39 +00008982 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008983
8984 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +00008985
Matt Carlson679563f2009-09-01 12:55:46 +00008986err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +00008987 for (i = tp->irq_cnt - 1; i >= 0; i--) {
8988 struct tg3_napi *tnapi = &tp->napi[i];
8989 free_irq(tnapi->irq_vec, tnapi);
8990 }
Matt Carlson07b01732009-08-28 14:01:15 +00008991
Matt Carlson679563f2009-09-01 12:55:46 +00008992err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +00008993 tg3_napi_disable(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00008994 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +00008995
8996err_out1:
8997 tg3_ints_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00008998 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008999}
9000
Eric Dumazet511d2222010-07-07 20:44:24 +00009001static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *,
9002 struct rtnl_link_stats64 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009003static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *);
9004
9005static int tg3_close(struct net_device *dev)
9006{
Matt Carlson4f125f42009-09-01 12:55:02 +00009007 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009008 struct tg3 *tp = netdev_priv(dev);
9009
Matt Carlsonfed97812009-09-01 13:10:19 +00009010 tg3_napi_disable(tp);
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07009011 cancel_work_sync(&tp->reset_task);
Michael Chan7faa0062006-02-02 17:29:28 -08009012
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009013 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009014
9015 del_timer_sync(&tp->timer);
9016
Matt Carlson24bb4fb2009-10-05 17:55:29 +00009017 tg3_phy_stop(tp);
9018
David S. Millerf47c11e2005-06-24 20:18:35 -07009019 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009020
9021 tg3_disable_ints(tp);
9022
Michael Chan944d9802005-05-29 14:57:48 -07009023 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009024 tg3_free_rings(tp);
Michael Chan5cf64b8a2007-05-05 12:11:21 -07009025 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009026
David S. Millerf47c11e2005-06-24 20:18:35 -07009027 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009028
Matt Carlson4f125f42009-09-01 12:55:02 +00009029 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9030 struct tg3_napi *tnapi = &tp->napi[i];
9031 free_irq(tnapi->irq_vec, tnapi);
9032 }
Matt Carlson07b01732009-08-28 14:01:15 +00009033
9034 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009035
Eric Dumazet511d2222010-07-07 20:44:24 +00009036 tg3_get_stats64(tp->dev, &tp->net_stats_prev);
9037
Linus Torvalds1da177e2005-04-16 15:20:36 -07009038 memcpy(&tp->estats_prev, tg3_get_estats(tp),
9039 sizeof(tp->estats_prev));
9040
9041 tg3_free_consistent(tp);
9042
Michael Chanbc1c7562006-03-20 17:48:03 -08009043 tg3_set_power_state(tp, PCI_D3hot);
9044
9045 netif_carrier_off(tp->dev);
9046
Linus Torvalds1da177e2005-04-16 15:20:36 -07009047 return 0;
9048}
9049
Eric Dumazet511d2222010-07-07 20:44:24 +00009050static inline u64 get_stat64(tg3_stat64_t *val)
Stefan Buehler816f8b82008-08-15 14:10:54 -07009051{
9052 return ((u64)val->high << 32) | ((u64)val->low);
9053}
9054
Eric Dumazet511d2222010-07-07 20:44:24 +00009055static u64 calc_crc_errors(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009056{
9057 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9058
9059 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
9060 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
9061 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009062 u32 val;
9063
David S. Millerf47c11e2005-06-24 20:18:35 -07009064 spin_lock_bh(&tp->lock);
Michael Chan569a5df2007-02-13 12:18:15 -08009065 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
9066 tg3_writephy(tp, MII_TG3_TEST1,
9067 val | MII_TG3_TEST1_CRC_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009068 tg3_readphy(tp, 0x14, &val);
9069 } else
9070 val = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07009071 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009072
9073 tp->phy_crc_errors += val;
9074
9075 return tp->phy_crc_errors;
9076 }
9077
9078 return get_stat64(&hw_stats->rx_fcs_errors);
9079}
9080
9081#define ESTAT_ADD(member) \
9082 estats->member = old_estats->member + \
Eric Dumazet511d2222010-07-07 20:44:24 +00009083 get_stat64(&hw_stats->member)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009084
9085static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *tp)
9086{
9087 struct tg3_ethtool_stats *estats = &tp->estats;
9088 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
9089 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9090
9091 if (!hw_stats)
9092 return old_estats;
9093
9094 ESTAT_ADD(rx_octets);
9095 ESTAT_ADD(rx_fragments);
9096 ESTAT_ADD(rx_ucast_packets);
9097 ESTAT_ADD(rx_mcast_packets);
9098 ESTAT_ADD(rx_bcast_packets);
9099 ESTAT_ADD(rx_fcs_errors);
9100 ESTAT_ADD(rx_align_errors);
9101 ESTAT_ADD(rx_xon_pause_rcvd);
9102 ESTAT_ADD(rx_xoff_pause_rcvd);
9103 ESTAT_ADD(rx_mac_ctrl_rcvd);
9104 ESTAT_ADD(rx_xoff_entered);
9105 ESTAT_ADD(rx_frame_too_long_errors);
9106 ESTAT_ADD(rx_jabbers);
9107 ESTAT_ADD(rx_undersize_packets);
9108 ESTAT_ADD(rx_in_length_errors);
9109 ESTAT_ADD(rx_out_length_errors);
9110 ESTAT_ADD(rx_64_or_less_octet_packets);
9111 ESTAT_ADD(rx_65_to_127_octet_packets);
9112 ESTAT_ADD(rx_128_to_255_octet_packets);
9113 ESTAT_ADD(rx_256_to_511_octet_packets);
9114 ESTAT_ADD(rx_512_to_1023_octet_packets);
9115 ESTAT_ADD(rx_1024_to_1522_octet_packets);
9116 ESTAT_ADD(rx_1523_to_2047_octet_packets);
9117 ESTAT_ADD(rx_2048_to_4095_octet_packets);
9118 ESTAT_ADD(rx_4096_to_8191_octet_packets);
9119 ESTAT_ADD(rx_8192_to_9022_octet_packets);
9120
9121 ESTAT_ADD(tx_octets);
9122 ESTAT_ADD(tx_collisions);
9123 ESTAT_ADD(tx_xon_sent);
9124 ESTAT_ADD(tx_xoff_sent);
9125 ESTAT_ADD(tx_flow_control);
9126 ESTAT_ADD(tx_mac_errors);
9127 ESTAT_ADD(tx_single_collisions);
9128 ESTAT_ADD(tx_mult_collisions);
9129 ESTAT_ADD(tx_deferred);
9130 ESTAT_ADD(tx_excessive_collisions);
9131 ESTAT_ADD(tx_late_collisions);
9132 ESTAT_ADD(tx_collide_2times);
9133 ESTAT_ADD(tx_collide_3times);
9134 ESTAT_ADD(tx_collide_4times);
9135 ESTAT_ADD(tx_collide_5times);
9136 ESTAT_ADD(tx_collide_6times);
9137 ESTAT_ADD(tx_collide_7times);
9138 ESTAT_ADD(tx_collide_8times);
9139 ESTAT_ADD(tx_collide_9times);
9140 ESTAT_ADD(tx_collide_10times);
9141 ESTAT_ADD(tx_collide_11times);
9142 ESTAT_ADD(tx_collide_12times);
9143 ESTAT_ADD(tx_collide_13times);
9144 ESTAT_ADD(tx_collide_14times);
9145 ESTAT_ADD(tx_collide_15times);
9146 ESTAT_ADD(tx_ucast_packets);
9147 ESTAT_ADD(tx_mcast_packets);
9148 ESTAT_ADD(tx_bcast_packets);
9149 ESTAT_ADD(tx_carrier_sense_errors);
9150 ESTAT_ADD(tx_discards);
9151 ESTAT_ADD(tx_errors);
9152
9153 ESTAT_ADD(dma_writeq_full);
9154 ESTAT_ADD(dma_write_prioq_full);
9155 ESTAT_ADD(rxbds_empty);
9156 ESTAT_ADD(rx_discards);
9157 ESTAT_ADD(rx_errors);
9158 ESTAT_ADD(rx_threshold_hit);
9159
9160 ESTAT_ADD(dma_readq_full);
9161 ESTAT_ADD(dma_read_prioq_full);
9162 ESTAT_ADD(tx_comp_queue_full);
9163
9164 ESTAT_ADD(ring_set_send_prod_index);
9165 ESTAT_ADD(ring_status_update);
9166 ESTAT_ADD(nic_irqs);
9167 ESTAT_ADD(nic_avoided_irqs);
9168 ESTAT_ADD(nic_tx_threshold_hit);
9169
9170 return estats;
9171}
9172
Eric Dumazet511d2222010-07-07 20:44:24 +00009173static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
9174 struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009175{
9176 struct tg3 *tp = netdev_priv(dev);
Eric Dumazet511d2222010-07-07 20:44:24 +00009177 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009178 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9179
9180 if (!hw_stats)
9181 return old_stats;
9182
9183 stats->rx_packets = old_stats->rx_packets +
9184 get_stat64(&hw_stats->rx_ucast_packets) +
9185 get_stat64(&hw_stats->rx_mcast_packets) +
9186 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009187
Linus Torvalds1da177e2005-04-16 15:20:36 -07009188 stats->tx_packets = old_stats->tx_packets +
9189 get_stat64(&hw_stats->tx_ucast_packets) +
9190 get_stat64(&hw_stats->tx_mcast_packets) +
9191 get_stat64(&hw_stats->tx_bcast_packets);
9192
9193 stats->rx_bytes = old_stats->rx_bytes +
9194 get_stat64(&hw_stats->rx_octets);
9195 stats->tx_bytes = old_stats->tx_bytes +
9196 get_stat64(&hw_stats->tx_octets);
9197
9198 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -07009199 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009200 stats->tx_errors = old_stats->tx_errors +
9201 get_stat64(&hw_stats->tx_errors) +
9202 get_stat64(&hw_stats->tx_mac_errors) +
9203 get_stat64(&hw_stats->tx_carrier_sense_errors) +
9204 get_stat64(&hw_stats->tx_discards);
9205
9206 stats->multicast = old_stats->multicast +
9207 get_stat64(&hw_stats->rx_mcast_packets);
9208 stats->collisions = old_stats->collisions +
9209 get_stat64(&hw_stats->tx_collisions);
9210
9211 stats->rx_length_errors = old_stats->rx_length_errors +
9212 get_stat64(&hw_stats->rx_frame_too_long_errors) +
9213 get_stat64(&hw_stats->rx_undersize_packets);
9214
9215 stats->rx_over_errors = old_stats->rx_over_errors +
9216 get_stat64(&hw_stats->rxbds_empty);
9217 stats->rx_frame_errors = old_stats->rx_frame_errors +
9218 get_stat64(&hw_stats->rx_align_errors);
9219 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
9220 get_stat64(&hw_stats->tx_discards);
9221 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
9222 get_stat64(&hw_stats->tx_carrier_sense_errors);
9223
9224 stats->rx_crc_errors = old_stats->rx_crc_errors +
9225 calc_crc_errors(tp);
9226
John W. Linville4f63b872005-09-12 14:43:18 -07009227 stats->rx_missed_errors = old_stats->rx_missed_errors +
9228 get_stat64(&hw_stats->rx_discards);
9229
Linus Torvalds1da177e2005-04-16 15:20:36 -07009230 return stats;
9231}
9232
9233static inline u32 calc_crc(unsigned char *buf, int len)
9234{
9235 u32 reg;
9236 u32 tmp;
9237 int j, k;
9238
9239 reg = 0xffffffff;
9240
9241 for (j = 0; j < len; j++) {
9242 reg ^= buf[j];
9243
9244 for (k = 0; k < 8; k++) {
9245 tmp = reg & 0x01;
9246
9247 reg >>= 1;
9248
Matt Carlson859a588792010-04-05 10:19:28 +00009249 if (tmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009250 reg ^= 0xedb88320;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009251 }
9252 }
9253
9254 return ~reg;
9255}
9256
9257static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
9258{
9259 /* accept or reject all multicast frames */
9260 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
9261 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
9262 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
9263 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
9264}
9265
9266static void __tg3_set_rx_mode(struct net_device *dev)
9267{
9268 struct tg3 *tp = netdev_priv(dev);
9269 u32 rx_mode;
9270
9271 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
9272 RX_MODE_KEEP_VLAN_TAG);
9273
9274 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
9275 * flag clear.
9276 */
9277#if TG3_VLAN_TAG_USED
9278 if (!tp->vlgrp &&
9279 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
9280 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9281#else
9282 /* By definition, VLAN is disabled always in this
9283 * case.
9284 */
9285 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
9286 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9287#endif
9288
9289 if (dev->flags & IFF_PROMISC) {
9290 /* Promiscuous mode. */
9291 rx_mode |= RX_MODE_PROMISC;
9292 } else if (dev->flags & IFF_ALLMULTI) {
9293 /* Accept all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +00009294 tg3_set_multi(tp, 1);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00009295 } else if (netdev_mc_empty(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009296 /* Reject all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +00009297 tg3_set_multi(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009298 } else {
9299 /* Accept one or more multicast(s). */
Jiri Pirko22bedad32010-04-01 21:22:57 +00009300 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009301 u32 mc_filter[4] = { 0, };
9302 u32 regidx;
9303 u32 bit;
9304 u32 crc;
9305
Jiri Pirko22bedad32010-04-01 21:22:57 +00009306 netdev_for_each_mc_addr(ha, dev) {
9307 crc = calc_crc(ha->addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009308 bit = ~crc & 0x7f;
9309 regidx = (bit & 0x60) >> 5;
9310 bit &= 0x1f;
9311 mc_filter[regidx] |= (1 << bit);
9312 }
9313
9314 tw32(MAC_HASH_REG_0, mc_filter[0]);
9315 tw32(MAC_HASH_REG_1, mc_filter[1]);
9316 tw32(MAC_HASH_REG_2, mc_filter[2]);
9317 tw32(MAC_HASH_REG_3, mc_filter[3]);
9318 }
9319
9320 if (rx_mode != tp->rx_mode) {
9321 tp->rx_mode = rx_mode;
9322 tw32_f(MAC_RX_MODE, rx_mode);
9323 udelay(10);
9324 }
9325}
9326
9327static void tg3_set_rx_mode(struct net_device *dev)
9328{
9329 struct tg3 *tp = netdev_priv(dev);
9330
Michael Chane75f7c92006-03-20 21:33:26 -08009331 if (!netif_running(dev))
9332 return;
9333
David S. Millerf47c11e2005-06-24 20:18:35 -07009334 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009335 __tg3_set_rx_mode(dev);
David S. Millerf47c11e2005-06-24 20:18:35 -07009336 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009337}
9338
9339#define TG3_REGDUMP_LEN (32 * 1024)
9340
9341static int tg3_get_regs_len(struct net_device *dev)
9342{
9343 return TG3_REGDUMP_LEN;
9344}
9345
9346static void tg3_get_regs(struct net_device *dev,
9347 struct ethtool_regs *regs, void *_p)
9348{
9349 u32 *p = _p;
9350 struct tg3 *tp = netdev_priv(dev);
9351 u8 *orig_p = _p;
9352 int i;
9353
9354 regs->version = 0;
9355
9356 memset(p, 0, TG3_REGDUMP_LEN);
9357
Michael Chanbc1c7562006-03-20 17:48:03 -08009358 if (tp->link_config.phy_is_low_power)
9359 return;
9360
David S. Millerf47c11e2005-06-24 20:18:35 -07009361 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009362
9363#define __GET_REG32(reg) (*(p)++ = tr32(reg))
Matt Carlsonbe98da62010-07-11 09:31:46 +00009364#define GET_REG32_LOOP(base, len) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07009365do { p = (u32 *)(orig_p + (base)); \
9366 for (i = 0; i < len; i += 4) \
9367 __GET_REG32((base) + i); \
9368} while (0)
9369#define GET_REG32_1(reg) \
9370do { p = (u32 *)(orig_p + (reg)); \
9371 __GET_REG32((reg)); \
9372} while (0)
9373
9374 GET_REG32_LOOP(TG3PCI_VENDOR, 0xb0);
9375 GET_REG32_LOOP(MAILBOX_INTERRUPT_0, 0x200);
9376 GET_REG32_LOOP(MAC_MODE, 0x4f0);
9377 GET_REG32_LOOP(SNDDATAI_MODE, 0xe0);
9378 GET_REG32_1(SNDDATAC_MODE);
9379 GET_REG32_LOOP(SNDBDS_MODE, 0x80);
9380 GET_REG32_LOOP(SNDBDI_MODE, 0x48);
9381 GET_REG32_1(SNDBDC_MODE);
9382 GET_REG32_LOOP(RCVLPC_MODE, 0x20);
9383 GET_REG32_LOOP(RCVLPC_SELLST_BASE, 0x15c);
9384 GET_REG32_LOOP(RCVDBDI_MODE, 0x0c);
9385 GET_REG32_LOOP(RCVDBDI_JUMBO_BD, 0x3c);
9386 GET_REG32_LOOP(RCVDBDI_BD_PROD_IDX_0, 0x44);
9387 GET_REG32_1(RCVDCC_MODE);
9388 GET_REG32_LOOP(RCVBDI_MODE, 0x20);
9389 GET_REG32_LOOP(RCVCC_MODE, 0x14);
9390 GET_REG32_LOOP(RCVLSC_MODE, 0x08);
9391 GET_REG32_1(MBFREE_MODE);
9392 GET_REG32_LOOP(HOSTCC_MODE, 0x100);
9393 GET_REG32_LOOP(MEMARB_MODE, 0x10);
9394 GET_REG32_LOOP(BUFMGR_MODE, 0x58);
9395 GET_REG32_LOOP(RDMAC_MODE, 0x08);
9396 GET_REG32_LOOP(WDMAC_MODE, 0x08);
Chris Elmquist091465d2005-12-20 13:25:19 -08009397 GET_REG32_1(RX_CPU_MODE);
9398 GET_REG32_1(RX_CPU_STATE);
9399 GET_REG32_1(RX_CPU_PGMCTR);
9400 GET_REG32_1(RX_CPU_HWBKPT);
9401 GET_REG32_1(TX_CPU_MODE);
9402 GET_REG32_1(TX_CPU_STATE);
9403 GET_REG32_1(TX_CPU_PGMCTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009404 GET_REG32_LOOP(GRCMBOX_INTERRUPT_0, 0x110);
9405 GET_REG32_LOOP(FTQ_RESET, 0x120);
9406 GET_REG32_LOOP(MSGINT_MODE, 0x0c);
9407 GET_REG32_1(DMAC_MODE);
9408 GET_REG32_LOOP(GRC_MODE, 0x4c);
9409 if (tp->tg3_flags & TG3_FLAG_NVRAM)
9410 GET_REG32_LOOP(NVRAM_CMD, 0x24);
9411
9412#undef __GET_REG32
9413#undef GET_REG32_LOOP
9414#undef GET_REG32_1
9415
David S. Millerf47c11e2005-06-24 20:18:35 -07009416 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009417}
9418
9419static int tg3_get_eeprom_len(struct net_device *dev)
9420{
9421 struct tg3 *tp = netdev_priv(dev);
9422
9423 return tp->nvram_size;
9424}
9425
Linus Torvalds1da177e2005-04-16 15:20:36 -07009426static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9427{
9428 struct tg3 *tp = netdev_priv(dev);
9429 int ret;
9430 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -08009431 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009432 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009433
Matt Carlsondf259d82009-04-20 06:57:14 +00009434 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM)
9435 return -EINVAL;
9436
Michael Chanbc1c7562006-03-20 17:48:03 -08009437 if (tp->link_config.phy_is_low_power)
9438 return -EAGAIN;
9439
Linus Torvalds1da177e2005-04-16 15:20:36 -07009440 offset = eeprom->offset;
9441 len = eeprom->len;
9442 eeprom->len = 0;
9443
9444 eeprom->magic = TG3_EEPROM_MAGIC;
9445
9446 if (offset & 3) {
9447 /* adjustments to start on required 4 byte boundary */
9448 b_offset = offset & 3;
9449 b_count = 4 - b_offset;
9450 if (b_count > len) {
9451 /* i.e. offset=1 len=2 */
9452 b_count = len;
9453 }
Matt Carlsona9dc5292009-02-25 14:25:30 +00009454 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009455 if (ret)
9456 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +00009457 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009458 len -= b_count;
9459 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00009460 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009461 }
9462
9463 /* read bytes upto the last 4 byte boundary */
9464 pd = &data[eeprom->len];
9465 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +00009466 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009467 if (ret) {
9468 eeprom->len += i;
9469 return ret;
9470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009471 memcpy(pd + i, &val, 4);
9472 }
9473 eeprom->len += i;
9474
9475 if (len & 3) {
9476 /* read last bytes not ending on 4 byte boundary */
9477 pd = &data[eeprom->len];
9478 b_count = len & 3;
9479 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009480 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009481 if (ret)
9482 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009483 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009484 eeprom->len += b_count;
9485 }
9486 return 0;
9487}
9488
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009489static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009490
9491static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9492{
9493 struct tg3 *tp = netdev_priv(dev);
9494 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009495 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009496 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009497 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009498
Michael Chanbc1c7562006-03-20 17:48:03 -08009499 if (tp->link_config.phy_is_low_power)
9500 return -EAGAIN;
9501
Matt Carlsondf259d82009-04-20 06:57:14 +00009502 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
9503 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009504 return -EINVAL;
9505
9506 offset = eeprom->offset;
9507 len = eeprom->len;
9508
9509 if ((b_offset = (offset & 3))) {
9510 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +00009511 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009512 if (ret)
9513 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009514 len += b_offset;
9515 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -07009516 if (len < 4)
9517 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009518 }
9519
9520 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -07009521 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009522 /* adjustments to end on required 4 byte boundary */
9523 odd_len = 1;
9524 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009525 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009526 if (ret)
9527 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009528 }
9529
9530 buf = data;
9531 if (b_offset || odd_len) {
9532 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009533 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009534 return -ENOMEM;
9535 if (b_offset)
9536 memcpy(buf, &start, 4);
9537 if (odd_len)
9538 memcpy(buf+len-4, &end, 4);
9539 memcpy(buf + b_offset, data, eeprom->len);
9540 }
9541
9542 ret = tg3_nvram_write_block(tp, offset, len, buf);
9543
9544 if (buf != data)
9545 kfree(buf);
9546
9547 return ret;
9548}
9549
9550static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9551{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009552 struct tg3 *tp = netdev_priv(dev);
9553
9554 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009555 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009556 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
9557 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009558 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
9559 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009560 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009561
Linus Torvalds1da177e2005-04-16 15:20:36 -07009562 cmd->supported = (SUPPORTED_Autoneg);
9563
9564 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY))
9565 cmd->supported |= (SUPPORTED_1000baseT_Half |
9566 SUPPORTED_1000baseT_Full);
9567
Karsten Keilef348142006-05-12 12:49:08 -07009568 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009569 cmd->supported |= (SUPPORTED_100baseT_Half |
9570 SUPPORTED_100baseT_Full |
9571 SUPPORTED_10baseT_Half |
9572 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -08009573 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -07009574 cmd->port = PORT_TP;
9575 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009576 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -07009577 cmd->port = PORT_FIBRE;
9578 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009579
Linus Torvalds1da177e2005-04-16 15:20:36 -07009580 cmd->advertising = tp->link_config.advertising;
9581 if (netif_running(dev)) {
9582 cmd->speed = tp->link_config.active_speed;
9583 cmd->duplex = tp->link_config.active_duplex;
9584 }
Matt Carlson882e9792009-09-01 13:21:36 +00009585 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +00009586 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009587 cmd->autoneg = tp->link_config.autoneg;
9588 cmd->maxtxpkt = 0;
9589 cmd->maxrxpkt = 0;
9590 return 0;
9591}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009592
Linus Torvalds1da177e2005-04-16 15:20:36 -07009593static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9594{
9595 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009596
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009597 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009598 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009599 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
9600 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009601 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
9602 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009603 }
9604
Matt Carlson7e5856b2009-02-25 14:23:01 +00009605 if (cmd->autoneg != AUTONEG_ENABLE &&
9606 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -07009607 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +00009608
9609 if (cmd->autoneg == AUTONEG_DISABLE &&
9610 cmd->duplex != DUPLEX_FULL &&
9611 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -07009612 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009613
Matt Carlson7e5856b2009-02-25 14:23:01 +00009614 if (cmd->autoneg == AUTONEG_ENABLE) {
9615 u32 mask = ADVERTISED_Autoneg |
9616 ADVERTISED_Pause |
9617 ADVERTISED_Asym_Pause;
9618
Julia Lawall3f07d122010-03-13 12:22:16 -08009619 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +00009620 mask |= ADVERTISED_1000baseT_Half |
9621 ADVERTISED_1000baseT_Full;
9622
9623 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES))
9624 mask |= ADVERTISED_100baseT_Half |
9625 ADVERTISED_100baseT_Full |
9626 ADVERTISED_10baseT_Half |
9627 ADVERTISED_10baseT_Full |
9628 ADVERTISED_TP;
9629 else
9630 mask |= ADVERTISED_FIBRE;
9631
9632 if (cmd->advertising & ~mask)
9633 return -EINVAL;
9634
9635 mask &= (ADVERTISED_1000baseT_Half |
9636 ADVERTISED_1000baseT_Full |
9637 ADVERTISED_100baseT_Half |
9638 ADVERTISED_100baseT_Full |
9639 ADVERTISED_10baseT_Half |
9640 ADVERTISED_10baseT_Full);
9641
9642 cmd->advertising &= mask;
9643 } else {
9644 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES) {
9645 if (cmd->speed != SPEED_1000)
9646 return -EINVAL;
9647
9648 if (cmd->duplex != DUPLEX_FULL)
9649 return -EINVAL;
9650 } else {
9651 if (cmd->speed != SPEED_100 &&
9652 cmd->speed != SPEED_10)
9653 return -EINVAL;
9654 }
9655 }
9656
David S. Millerf47c11e2005-06-24 20:18:35 -07009657 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009658
9659 tp->link_config.autoneg = cmd->autoneg;
9660 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -07009661 tp->link_config.advertising = (cmd->advertising |
9662 ADVERTISED_Autoneg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009663 tp->link_config.speed = SPEED_INVALID;
9664 tp->link_config.duplex = DUPLEX_INVALID;
9665 } else {
9666 tp->link_config.advertising = 0;
9667 tp->link_config.speed = cmd->speed;
9668 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009669 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009670
Michael Chan24fcad62006-12-17 17:06:46 -08009671 tp->link_config.orig_speed = tp->link_config.speed;
9672 tp->link_config.orig_duplex = tp->link_config.duplex;
9673 tp->link_config.orig_autoneg = tp->link_config.autoneg;
9674
Linus Torvalds1da177e2005-04-16 15:20:36 -07009675 if (netif_running(dev))
9676 tg3_setup_phy(tp, 1);
9677
David S. Millerf47c11e2005-06-24 20:18:35 -07009678 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009679
Linus Torvalds1da177e2005-04-16 15:20:36 -07009680 return 0;
9681}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009682
Linus Torvalds1da177e2005-04-16 15:20:36 -07009683static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
9684{
9685 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009686
Linus Torvalds1da177e2005-04-16 15:20:36 -07009687 strcpy(info->driver, DRV_MODULE_NAME);
9688 strcpy(info->version, DRV_MODULE_VERSION);
Michael Chanc4e65752006-03-20 22:29:32 -08009689 strcpy(info->fw_version, tp->fw_ver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009690 strcpy(info->bus_info, pci_name(tp->pdev));
9691}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009692
Linus Torvalds1da177e2005-04-16 15:20:36 -07009693static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
9694{
9695 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009696
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009697 if ((tp->tg3_flags & TG3_FLAG_WOL_CAP) &&
9698 device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -07009699 wol->supported = WAKE_MAGIC;
9700 else
9701 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009702 wol->wolopts = 0;
Matt Carlson05ac4cb2008-11-03 16:53:46 -08009703 if ((tp->tg3_flags & TG3_FLAG_WOL_ENABLE) &&
9704 device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009705 wol->wolopts = WAKE_MAGIC;
9706 memset(&wol->sopass, 0, sizeof(wol->sopass));
9707}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009708
Linus Torvalds1da177e2005-04-16 15:20:36 -07009709static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
9710{
9711 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009712 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009713
Linus Torvalds1da177e2005-04-16 15:20:36 -07009714 if (wol->wolopts & ~WAKE_MAGIC)
9715 return -EINVAL;
9716 if ((wol->wolopts & WAKE_MAGIC) &&
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009717 !((tp->tg3_flags & TG3_FLAG_WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009718 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009719
David S. Millerf47c11e2005-06-24 20:18:35 -07009720 spin_lock_bh(&tp->lock);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009721 if (wol->wolopts & WAKE_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009722 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009723 device_set_wakeup_enable(dp, true);
9724 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009725 tp->tg3_flags &= ~TG3_FLAG_WOL_ENABLE;
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009726 device_set_wakeup_enable(dp, false);
9727 }
David S. Millerf47c11e2005-06-24 20:18:35 -07009728 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009729
Linus Torvalds1da177e2005-04-16 15:20:36 -07009730 return 0;
9731}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009732
Linus Torvalds1da177e2005-04-16 15:20:36 -07009733static u32 tg3_get_msglevel(struct net_device *dev)
9734{
9735 struct tg3 *tp = netdev_priv(dev);
9736 return tp->msg_enable;
9737}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009738
Linus Torvalds1da177e2005-04-16 15:20:36 -07009739static void tg3_set_msglevel(struct net_device *dev, u32 value)
9740{
9741 struct tg3 *tp = netdev_priv(dev);
9742 tp->msg_enable = value;
9743}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009744
Linus Torvalds1da177e2005-04-16 15:20:36 -07009745static int tg3_set_tso(struct net_device *dev, u32 value)
9746{
9747 struct tg3 *tp = netdev_priv(dev);
9748
9749 if (!(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
9750 if (value)
9751 return -EINVAL;
9752 return 0;
9753 }
Matt Carlson027455a2008-12-21 20:19:30 -08009754 if ((dev->features & NETIF_F_IPV6_CSUM) &&
Matt Carlsone849cdc2009-11-13 13:03:38 +00009755 ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_2) ||
9756 (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3))) {
Matt Carlson9936bcf2007-10-10 18:03:07 -07009757 if (value) {
Michael Chanb0026622006-07-03 19:42:14 -07009758 dev->features |= NETIF_F_TSO6;
Matt Carlsone849cdc2009-11-13 13:03:38 +00009759 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
9760 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -07009761 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
9762 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Matt Carlson321d32a2008-11-21 17:22:19 -08009763 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsone849cdc2009-11-13 13:03:38 +00009764 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson9936bcf2007-10-10 18:03:07 -07009765 dev->features |= NETIF_F_TSO_ECN;
9766 } else
9767 dev->features &= ~(NETIF_F_TSO6 | NETIF_F_TSO_ECN);
Michael Chanb0026622006-07-03 19:42:14 -07009768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009769 return ethtool_op_set_tso(dev, value);
9770}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009771
Linus Torvalds1da177e2005-04-16 15:20:36 -07009772static int tg3_nway_reset(struct net_device *dev)
9773{
9774 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009775 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009776
Linus Torvalds1da177e2005-04-16 15:20:36 -07009777 if (!netif_running(dev))
9778 return -EAGAIN;
9779
Michael Chanc94e3942005-09-27 12:12:42 -07009780 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
9781 return -EINVAL;
9782
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009783 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
9784 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
9785 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009786 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009787 } else {
9788 u32 bmcr;
9789
9790 spin_lock_bh(&tp->lock);
9791 r = -EINVAL;
9792 tg3_readphy(tp, MII_BMCR, &bmcr);
9793 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
9794 ((bmcr & BMCR_ANENABLE) ||
9795 (tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT))) {
9796 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
9797 BMCR_ANENABLE);
9798 r = 0;
9799 }
9800 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009801 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009802
Linus Torvalds1da177e2005-04-16 15:20:36 -07009803 return r;
9804}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009805
Linus Torvalds1da177e2005-04-16 15:20:36 -07009806static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
9807{
9808 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009809
Linus Torvalds1da177e2005-04-16 15:20:36 -07009810 ering->rx_max_pending = TG3_RX_RING_SIZE - 1;
9811 ering->rx_mini_max_pending = 0;
Michael Chan4f81c322006-03-20 21:33:42 -08009812 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE)
9813 ering->rx_jumbo_max_pending = TG3_RX_JUMBO_RING_SIZE - 1;
9814 else
9815 ering->rx_jumbo_max_pending = 0;
9816
9817 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009818
9819 ering->rx_pending = tp->rx_pending;
9820 ering->rx_mini_pending = 0;
Michael Chan4f81c322006-03-20 21:33:42 -08009821 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE)
9822 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
9823 else
9824 ering->rx_jumbo_pending = 0;
9825
Matt Carlsonf3f3f272009-08-28 14:03:21 +00009826 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009827}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009828
Linus Torvalds1da177e2005-04-16 15:20:36 -07009829static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
9830{
9831 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +00009832 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009833
Linus Torvalds1da177e2005-04-16 15:20:36 -07009834 if ((ering->rx_pending > TG3_RX_RING_SIZE - 1) ||
9835 (ering->rx_jumbo_pending > TG3_RX_JUMBO_RING_SIZE - 1) ||
Michael Chanbc3a9252006-10-18 20:55:18 -07009836 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
9837 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Michael Chan7f62ad52007-02-20 23:25:40 -08009838 ((tp->tg3_flags2 & TG3_FLG2_TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -07009839 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009840 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009841
Michael Chanbbe832c2005-06-24 20:20:04 -07009842 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009843 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009844 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -07009845 irq_sync = 1;
9846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009847
Michael Chanbbe832c2005-06-24 20:20:04 -07009848 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009849
Linus Torvalds1da177e2005-04-16 15:20:36 -07009850 tp->rx_pending = ering->rx_pending;
9851
9852 if ((tp->tg3_flags2 & TG3_FLG2_MAX_RXPEND_64) &&
9853 tp->rx_pending > 63)
9854 tp->rx_pending = 63;
9855 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +00009856
9857 for (i = 0; i < TG3_IRQ_MAX_VECS; i++)
9858 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009859
9860 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -07009861 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -07009862 err = tg3_restart_hw(tp, 1);
9863 if (!err)
9864 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009865 }
9866
David S. Millerf47c11e2005-06-24 20:18:35 -07009867 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009868
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009869 if (irq_sync && !err)
9870 tg3_phy_start(tp);
9871
Michael Chanb9ec6c12006-07-25 16:37:27 -07009872 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009873}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009874
Linus Torvalds1da177e2005-04-16 15:20:36 -07009875static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
9876{
9877 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009878
Linus Torvalds1da177e2005-04-16 15:20:36 -07009879 epause->autoneg = (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG) != 0;
Matt Carlson8d018622007-12-20 20:05:44 -08009880
Steve Glendinninge18ce342008-12-16 02:00:00 -08009881 if (tp->link_config.active_flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -08009882 epause->rx_pause = 1;
9883 else
9884 epause->rx_pause = 0;
9885
Steve Glendinninge18ce342008-12-16 02:00:00 -08009886 if (tp->link_config.active_flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -08009887 epause->tx_pause = 1;
9888 else
9889 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009890}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009891
Linus Torvalds1da177e2005-04-16 15:20:36 -07009892static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
9893{
9894 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009895 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009896
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009897 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson27121682010-02-17 15:16:57 +00009898 u32 newadv;
9899 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009900
Matt Carlson27121682010-02-17 15:16:57 +00009901 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009902
Matt Carlson27121682010-02-17 15:16:57 +00009903 if (!(phydev->supported & SUPPORTED_Pause) ||
9904 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
9905 ((epause->rx_pause && !epause->tx_pause) ||
9906 (!epause->rx_pause && epause->tx_pause))))
9907 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009908
Matt Carlson27121682010-02-17 15:16:57 +00009909 tp->link_config.flowctrl = 0;
9910 if (epause->rx_pause) {
9911 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009912
Matt Carlson27121682010-02-17 15:16:57 +00009913 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -08009914 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +00009915 newadv = ADVERTISED_Pause;
9916 } else
9917 newadv = ADVERTISED_Pause |
9918 ADVERTISED_Asym_Pause;
9919 } else if (epause->tx_pause) {
9920 tp->link_config.flowctrl |= FLOW_CTRL_TX;
9921 newadv = ADVERTISED_Asym_Pause;
9922 } else
9923 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009924
Matt Carlson27121682010-02-17 15:16:57 +00009925 if (epause->autoneg)
9926 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
9927 else
9928 tp->tg3_flags &= ~TG3_FLAG_PAUSE_AUTONEG;
9929
9930 if (tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED) {
9931 u32 oldadv = phydev->advertising &
9932 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
9933 if (oldadv != newadv) {
9934 phydev->advertising &=
9935 ~(ADVERTISED_Pause |
9936 ADVERTISED_Asym_Pause);
9937 phydev->advertising |= newadv;
9938 if (phydev->autoneg) {
9939 /*
9940 * Always renegotiate the link to
9941 * inform our link partner of our
9942 * flow control settings, even if the
9943 * flow control is forced. Let
9944 * tg3_adjust_link() do the final
9945 * flow control setup.
9946 */
9947 return phy_start_aneg(phydev);
9948 }
9949 }
9950
9951 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009952 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +00009953 } else {
9954 tp->link_config.orig_advertising &=
9955 ~(ADVERTISED_Pause |
9956 ADVERTISED_Asym_Pause);
9957 tp->link_config.orig_advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009958 }
9959 } else {
9960 int irq_sync = 0;
9961
9962 if (netif_running(dev)) {
9963 tg3_netif_stop(tp);
9964 irq_sync = 1;
9965 }
9966
9967 tg3_full_lock(tp, irq_sync);
9968
9969 if (epause->autoneg)
9970 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
9971 else
9972 tp->tg3_flags &= ~TG3_FLAG_PAUSE_AUTONEG;
9973 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -08009974 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009975 else
Steve Glendinninge18ce342008-12-16 02:00:00 -08009976 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009977 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -08009978 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009979 else
Steve Glendinninge18ce342008-12-16 02:00:00 -08009980 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009981
9982 if (netif_running(dev)) {
9983 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
9984 err = tg3_restart_hw(tp, 1);
9985 if (!err)
9986 tg3_netif_start(tp);
9987 }
9988
9989 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -07009990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009991
Michael Chanb9ec6c12006-07-25 16:37:27 -07009992 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009993}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009994
Linus Torvalds1da177e2005-04-16 15:20:36 -07009995static u32 tg3_get_rx_csum(struct net_device *dev)
9996{
9997 struct tg3 *tp = netdev_priv(dev);
9998 return (tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) != 0;
9999}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010000
Linus Torvalds1da177e2005-04-16 15:20:36 -070010001static int tg3_set_rx_csum(struct net_device *dev, u32 data)
10002{
10003 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010004
Linus Torvalds1da177e2005-04-16 15:20:36 -070010005 if (tp->tg3_flags & TG3_FLAG_BROKEN_CHECKSUMS) {
10006 if (data != 0)
10007 return -EINVAL;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010008 return 0;
10009 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010010
David S. Millerf47c11e2005-06-24 20:18:35 -070010011 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010012 if (data)
10013 tp->tg3_flags |= TG3_FLAG_RX_CHECKSUMS;
10014 else
10015 tp->tg3_flags &= ~TG3_FLAG_RX_CHECKSUMS;
David S. Millerf47c11e2005-06-24 20:18:35 -070010016 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010017
Linus Torvalds1da177e2005-04-16 15:20:36 -070010018 return 0;
10019}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010020
Linus Torvalds1da177e2005-04-16 15:20:36 -070010021static int tg3_set_tx_csum(struct net_device *dev, u32 data)
10022{
10023 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010024
Linus Torvalds1da177e2005-04-16 15:20:36 -070010025 if (tp->tg3_flags & TG3_FLAG_BROKEN_CHECKSUMS) {
10026 if (data != 0)
10027 return -EINVAL;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010028 return 0;
10029 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010030
Matt Carlson321d32a2008-11-21 17:22:19 -080010031 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Michael Chan6460d942007-07-14 19:07:52 -070010032 ethtool_op_set_tx_ipv6_csum(dev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010033 else
Michael Chan9c27dbd2006-03-20 22:28:27 -080010034 ethtool_op_set_tx_csum(dev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010035
10036 return 0;
10037}
10038
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010039static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010040{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010041 switch (sset) {
10042 case ETH_SS_TEST:
10043 return TG3_NUM_TEST;
10044 case ETH_SS_STATS:
10045 return TG3_NUM_STATS;
10046 default:
10047 return -EOPNOTSUPP;
10048 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070010049}
10050
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010051static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010052{
10053 switch (stringset) {
10054 case ETH_SS_STATS:
10055 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
10056 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070010057 case ETH_SS_TEST:
10058 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
10059 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010060 default:
10061 WARN_ON(1); /* we need a WARN() */
10062 break;
10063 }
10064}
10065
Michael Chan4009a932005-09-05 17:52:54 -070010066static int tg3_phys_id(struct net_device *dev, u32 data)
10067{
10068 struct tg3 *tp = netdev_priv(dev);
10069 int i;
10070
10071 if (!netif_running(tp->dev))
10072 return -EAGAIN;
10073
10074 if (data == 0)
Stephen Hemminger759afc32008-02-23 19:51:59 -080010075 data = UINT_MAX / 2;
Michael Chan4009a932005-09-05 17:52:54 -070010076
10077 for (i = 0; i < (data * 2); i++) {
10078 if ((i % 2) == 0)
10079 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10080 LED_CTRL_1000MBPS_ON |
10081 LED_CTRL_100MBPS_ON |
10082 LED_CTRL_10MBPS_ON |
10083 LED_CTRL_TRAFFIC_OVERRIDE |
10084 LED_CTRL_TRAFFIC_BLINK |
10085 LED_CTRL_TRAFFIC_LED);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010086
Michael Chan4009a932005-09-05 17:52:54 -070010087 else
10088 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10089 LED_CTRL_TRAFFIC_OVERRIDE);
10090
10091 if (msleep_interruptible(500))
10092 break;
10093 }
10094 tw32(MAC_LED_CTRL, tp->led_ctrl);
10095 return 0;
10096}
10097
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010098static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010099 struct ethtool_stats *estats, u64 *tmp_stats)
10100{
10101 struct tg3 *tp = netdev_priv(dev);
10102 memcpy(tmp_stats, tg3_get_estats(tp), sizeof(tp->estats));
10103}
10104
Michael Chan566f86a2005-05-29 14:56:58 -070010105#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080010106#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
10107#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
10108#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Michael Chanb16250e2006-09-27 16:10:14 -070010109#define NVRAM_SELFBOOT_HW_SIZE 0x20
10110#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070010111
10112static int tg3_test_nvram(struct tg3 *tp)
10113{
Al Virob9fc7dc2007-12-17 22:59:57 -080010114 u32 csum, magic;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010115 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010116 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070010117
Matt Carlsondf259d82009-04-20 06:57:14 +000010118 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM)
10119 return 0;
10120
Matt Carlsone4f34112009-02-25 14:25:00 +000010121 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080010122 return -EIO;
10123
Michael Chan1b277772006-03-20 22:27:48 -080010124 if (magic == TG3_EEPROM_MAGIC)
10125 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070010126 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080010127 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
10128 TG3_EEPROM_SB_FORMAT_1) {
10129 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
10130 case TG3_EEPROM_SB_REVISION_0:
10131 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
10132 break;
10133 case TG3_EEPROM_SB_REVISION_2:
10134 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
10135 break;
10136 case TG3_EEPROM_SB_REVISION_3:
10137 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
10138 break;
10139 default:
10140 return 0;
10141 }
10142 } else
Michael Chan1b277772006-03-20 22:27:48 -080010143 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070010144 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
10145 size = NVRAM_SELFBOOT_HW_SIZE;
10146 else
Michael Chan1b277772006-03-20 22:27:48 -080010147 return -EIO;
10148
10149 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070010150 if (buf == NULL)
10151 return -ENOMEM;
10152
Michael Chan1b277772006-03-20 22:27:48 -080010153 err = -EIO;
10154 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010155 err = tg3_nvram_read_be32(tp, i, &buf[j]);
10156 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070010157 break;
Michael Chan566f86a2005-05-29 14:56:58 -070010158 }
Michael Chan1b277772006-03-20 22:27:48 -080010159 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070010160 goto out;
10161
Michael Chan1b277772006-03-20 22:27:48 -080010162 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010163 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080010164 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010165 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080010166 u8 *buf8 = (u8 *) buf, csum8 = 0;
10167
Al Virob9fc7dc2007-12-17 22:59:57 -080010168 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080010169 TG3_EEPROM_SB_REVISION_2) {
10170 /* For rev 2, the csum doesn't include the MBA. */
10171 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
10172 csum8 += buf8[i];
10173 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
10174 csum8 += buf8[i];
10175 } else {
10176 for (i = 0; i < size; i++)
10177 csum8 += buf8[i];
10178 }
Michael Chan1b277772006-03-20 22:27:48 -080010179
Adrian Bunkad96b482006-04-05 22:21:04 -070010180 if (csum8 == 0) {
10181 err = 0;
10182 goto out;
10183 }
10184
10185 err = -EIO;
10186 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080010187 }
Michael Chan566f86a2005-05-29 14:56:58 -070010188
Al Virob9fc7dc2007-12-17 22:59:57 -080010189 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010190 TG3_EEPROM_MAGIC_HW) {
10191 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000010192 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070010193 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070010194
10195 /* Separate the parity bits and the data bytes. */
10196 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
10197 if ((i == 0) || (i == 8)) {
10198 int l;
10199 u8 msk;
10200
10201 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
10202 parity[k++] = buf8[i] & msk;
10203 i++;
Matt Carlson859a588792010-04-05 10:19:28 +000010204 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070010205 int l;
10206 u8 msk;
10207
10208 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
10209 parity[k++] = buf8[i] & msk;
10210 i++;
10211
10212 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
10213 parity[k++] = buf8[i] & msk;
10214 i++;
10215 }
10216 data[j++] = buf8[i];
10217 }
10218
10219 err = -EIO;
10220 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
10221 u8 hw8 = hweight8(data[i]);
10222
10223 if ((hw8 & 0x1) && parity[i])
10224 goto out;
10225 else if (!(hw8 & 0x1) && !parity[i])
10226 goto out;
10227 }
10228 err = 0;
10229 goto out;
10230 }
10231
Michael Chan566f86a2005-05-29 14:56:58 -070010232 /* Bootstrap checksum at offset 0x10 */
10233 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlsona9dc5292009-02-25 14:25:30 +000010234 if (csum != be32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070010235 goto out;
10236
10237 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
10238 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlsona9dc5292009-02-25 14:25:30 +000010239 if (csum != be32_to_cpu(buf[0xfc/4]))
10240 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070010241
10242 err = 0;
10243
10244out:
10245 kfree(buf);
10246 return err;
10247}
10248
Michael Chanca430072005-05-29 14:57:23 -070010249#define TG3_SERDES_TIMEOUT_SEC 2
10250#define TG3_COPPER_TIMEOUT_SEC 6
10251
10252static int tg3_test_link(struct tg3 *tp)
10253{
10254 int i, max;
10255
10256 if (!netif_running(tp->dev))
10257 return -ENODEV;
10258
Michael Chan4c987482005-09-05 17:52:38 -070010259 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070010260 max = TG3_SERDES_TIMEOUT_SEC;
10261 else
10262 max = TG3_COPPER_TIMEOUT_SEC;
10263
10264 for (i = 0; i < max; i++) {
10265 if (netif_carrier_ok(tp->dev))
10266 return 0;
10267
10268 if (msleep_interruptible(1000))
10269 break;
10270 }
10271
10272 return -EIO;
10273}
10274
Michael Chana71116d2005-05-29 14:58:11 -070010275/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080010276static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070010277{
Michael Chanb16250e2006-09-27 16:10:14 -070010278 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070010279 u32 offset, read_mask, write_mask, val, save_val, read_val;
10280 static struct {
10281 u16 offset;
10282 u16 flags;
10283#define TG3_FL_5705 0x1
10284#define TG3_FL_NOT_5705 0x2
10285#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070010286#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070010287 u32 read_mask;
10288 u32 write_mask;
10289 } reg_tbl[] = {
10290 /* MAC Control Registers */
10291 { MAC_MODE, TG3_FL_NOT_5705,
10292 0x00000000, 0x00ef6f8c },
10293 { MAC_MODE, TG3_FL_5705,
10294 0x00000000, 0x01ef6b8c },
10295 { MAC_STATUS, TG3_FL_NOT_5705,
10296 0x03800107, 0x00000000 },
10297 { MAC_STATUS, TG3_FL_5705,
10298 0x03800100, 0x00000000 },
10299 { MAC_ADDR_0_HIGH, 0x0000,
10300 0x00000000, 0x0000ffff },
10301 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010302 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070010303 { MAC_RX_MTU_SIZE, 0x0000,
10304 0x00000000, 0x0000ffff },
10305 { MAC_TX_MODE, 0x0000,
10306 0x00000000, 0x00000070 },
10307 { MAC_TX_LENGTHS, 0x0000,
10308 0x00000000, 0x00003fff },
10309 { MAC_RX_MODE, TG3_FL_NOT_5705,
10310 0x00000000, 0x000007fc },
10311 { MAC_RX_MODE, TG3_FL_5705,
10312 0x00000000, 0x000007dc },
10313 { MAC_HASH_REG_0, 0x0000,
10314 0x00000000, 0xffffffff },
10315 { MAC_HASH_REG_1, 0x0000,
10316 0x00000000, 0xffffffff },
10317 { MAC_HASH_REG_2, 0x0000,
10318 0x00000000, 0xffffffff },
10319 { MAC_HASH_REG_3, 0x0000,
10320 0x00000000, 0xffffffff },
10321
10322 /* Receive Data and Receive BD Initiator Control Registers. */
10323 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
10324 0x00000000, 0xffffffff },
10325 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
10326 0x00000000, 0xffffffff },
10327 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
10328 0x00000000, 0x00000003 },
10329 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
10330 0x00000000, 0xffffffff },
10331 { RCVDBDI_STD_BD+0, 0x0000,
10332 0x00000000, 0xffffffff },
10333 { RCVDBDI_STD_BD+4, 0x0000,
10334 0x00000000, 0xffffffff },
10335 { RCVDBDI_STD_BD+8, 0x0000,
10336 0x00000000, 0xffff0002 },
10337 { RCVDBDI_STD_BD+0xc, 0x0000,
10338 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010339
Michael Chana71116d2005-05-29 14:58:11 -070010340 /* Receive BD Initiator Control Registers. */
10341 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
10342 0x00000000, 0xffffffff },
10343 { RCVBDI_STD_THRESH, TG3_FL_5705,
10344 0x00000000, 0x000003ff },
10345 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
10346 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010347
Michael Chana71116d2005-05-29 14:58:11 -070010348 /* Host Coalescing Control Registers. */
10349 { HOSTCC_MODE, TG3_FL_NOT_5705,
10350 0x00000000, 0x00000004 },
10351 { HOSTCC_MODE, TG3_FL_5705,
10352 0x00000000, 0x000000f6 },
10353 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
10354 0x00000000, 0xffffffff },
10355 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
10356 0x00000000, 0x000003ff },
10357 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
10358 0x00000000, 0xffffffff },
10359 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
10360 0x00000000, 0x000003ff },
10361 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
10362 0x00000000, 0xffffffff },
10363 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10364 0x00000000, 0x000000ff },
10365 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
10366 0x00000000, 0xffffffff },
10367 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10368 0x00000000, 0x000000ff },
10369 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
10370 0x00000000, 0xffffffff },
10371 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
10372 0x00000000, 0xffffffff },
10373 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10374 0x00000000, 0xffffffff },
10375 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10376 0x00000000, 0x000000ff },
10377 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10378 0x00000000, 0xffffffff },
10379 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10380 0x00000000, 0x000000ff },
10381 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
10382 0x00000000, 0xffffffff },
10383 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
10384 0x00000000, 0xffffffff },
10385 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
10386 0x00000000, 0xffffffff },
10387 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
10388 0x00000000, 0xffffffff },
10389 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
10390 0x00000000, 0xffffffff },
10391 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
10392 0xffffffff, 0x00000000 },
10393 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
10394 0xffffffff, 0x00000000 },
10395
10396 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070010397 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010398 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070010399 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010400 0x00000000, 0x007fffff },
10401 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
10402 0x00000000, 0x0000003f },
10403 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
10404 0x00000000, 0x000001ff },
10405 { BUFMGR_MB_HIGH_WATER, 0x0000,
10406 0x00000000, 0x000001ff },
10407 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
10408 0xffffffff, 0x00000000 },
10409 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
10410 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010411
Michael Chana71116d2005-05-29 14:58:11 -070010412 /* Mailbox Registers */
10413 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
10414 0x00000000, 0x000001ff },
10415 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
10416 0x00000000, 0x000001ff },
10417 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
10418 0x00000000, 0x000007ff },
10419 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
10420 0x00000000, 0x000001ff },
10421
10422 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
10423 };
10424
Michael Chanb16250e2006-09-27 16:10:14 -070010425 is_5705 = is_5750 = 0;
10426 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
Michael Chana71116d2005-05-29 14:58:11 -070010427 is_5705 = 1;
Michael Chanb16250e2006-09-27 16:10:14 -070010428 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
10429 is_5750 = 1;
10430 }
Michael Chana71116d2005-05-29 14:58:11 -070010431
10432 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
10433 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
10434 continue;
10435
10436 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
10437 continue;
10438
10439 if ((tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
10440 (reg_tbl[i].flags & TG3_FL_NOT_5788))
10441 continue;
10442
Michael Chanb16250e2006-09-27 16:10:14 -070010443 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
10444 continue;
10445
Michael Chana71116d2005-05-29 14:58:11 -070010446 offset = (u32) reg_tbl[i].offset;
10447 read_mask = reg_tbl[i].read_mask;
10448 write_mask = reg_tbl[i].write_mask;
10449
10450 /* Save the original register content */
10451 save_val = tr32(offset);
10452
10453 /* Determine the read-only value. */
10454 read_val = save_val & read_mask;
10455
10456 /* Write zero to the register, then make sure the read-only bits
10457 * are not changed and the read/write bits are all zeros.
10458 */
10459 tw32(offset, 0);
10460
10461 val = tr32(offset);
10462
10463 /* Test the read-only and read/write bits. */
10464 if (((val & read_mask) != read_val) || (val & write_mask))
10465 goto out;
10466
10467 /* Write ones to all the bits defined by RdMask and WrMask, then
10468 * make sure the read-only bits are not changed and the
10469 * read/write bits are all ones.
10470 */
10471 tw32(offset, read_mask | write_mask);
10472
10473 val = tr32(offset);
10474
10475 /* Test the read-only bits. */
10476 if ((val & read_mask) != read_val)
10477 goto out;
10478
10479 /* Test the read/write bits. */
10480 if ((val & write_mask) != write_mask)
10481 goto out;
10482
10483 tw32(offset, save_val);
10484 }
10485
10486 return 0;
10487
10488out:
Michael Chan9f88f292006-12-07 00:22:54 -080010489 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000010490 netdev_err(tp->dev,
10491 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070010492 tw32(offset, save_val);
10493 return -EIO;
10494}
10495
Michael Chan7942e1d2005-05-29 14:58:36 -070010496static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
10497{
Arjan van de Venf71e1302006-03-03 21:33:57 -050010498 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070010499 int i;
10500 u32 j;
10501
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020010502 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070010503 for (j = 0; j < len; j += 4) {
10504 u32 val;
10505
10506 tg3_write_mem(tp, offset + j, test_pattern[i]);
10507 tg3_read_mem(tp, offset + j, &val);
10508 if (val != test_pattern[i])
10509 return -EIO;
10510 }
10511 }
10512 return 0;
10513}
10514
10515static int tg3_test_memory(struct tg3 *tp)
10516{
10517 static struct mem_entry {
10518 u32 offset;
10519 u32 len;
10520 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080010521 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070010522 { 0x00002000, 0x1c000},
10523 { 0xffffffff, 0x00000}
10524 }, mem_tbl_5705[] = {
10525 { 0x00000100, 0x0000c},
10526 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070010527 { 0x00004000, 0x00800},
10528 { 0x00006000, 0x01000},
10529 { 0x00008000, 0x02000},
10530 { 0x00010000, 0x0e000},
10531 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080010532 }, mem_tbl_5755[] = {
10533 { 0x00000200, 0x00008},
10534 { 0x00004000, 0x00800},
10535 { 0x00006000, 0x00800},
10536 { 0x00008000, 0x02000},
10537 { 0x00010000, 0x0c000},
10538 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070010539 }, mem_tbl_5906[] = {
10540 { 0x00000200, 0x00008},
10541 { 0x00004000, 0x00400},
10542 { 0x00006000, 0x00400},
10543 { 0x00008000, 0x01000},
10544 { 0x00010000, 0x01000},
10545 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000010546 }, mem_tbl_5717[] = {
10547 { 0x00000200, 0x00008},
10548 { 0x00010000, 0x0a000},
10549 { 0x00020000, 0x13c00},
10550 { 0xffffffff, 0x00000}
10551 }, mem_tbl_57765[] = {
10552 { 0x00000200, 0x00008},
10553 { 0x00004000, 0x00800},
10554 { 0x00006000, 0x09800},
10555 { 0x00010000, 0x0a000},
10556 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070010557 };
10558 struct mem_entry *mem_tbl;
10559 int err = 0;
10560 int i;
10561
Matt Carlsona50d0792010-06-05 17:24:37 +000010562 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
10563 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlson8b5a6c42010-01-20 16:58:06 +000010564 mem_tbl = mem_tbl_5717;
10565 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
10566 mem_tbl = mem_tbl_57765;
10567 else if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Matt Carlson321d32a2008-11-21 17:22:19 -080010568 mem_tbl = mem_tbl_5755;
10569 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
10570 mem_tbl = mem_tbl_5906;
10571 else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
10572 mem_tbl = mem_tbl_5705;
10573 else
Michael Chan7942e1d2005-05-29 14:58:36 -070010574 mem_tbl = mem_tbl_570x;
10575
10576 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000010577 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
10578 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070010579 break;
10580 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010581
Michael Chan7942e1d2005-05-29 14:58:36 -070010582 return err;
10583}
10584
Michael Chan9f40dea2005-09-05 17:53:06 -070010585#define TG3_MAC_LOOPBACK 0
10586#define TG3_PHY_LOOPBACK 1
10587
10588static int tg3_run_loopback(struct tg3 *tp, int loopback_mode)
Michael Chanc76949a2005-05-29 14:58:59 -070010589{
Michael Chan9f40dea2005-09-05 17:53:06 -070010590 u32 mac_mode, rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010591 u32 desc_idx, coal_now;
Michael Chanc76949a2005-05-29 14:58:59 -070010592 struct sk_buff *skb, *rx_skb;
10593 u8 *tx_data;
10594 dma_addr_t map;
10595 int num_pkts, tx_len, rx_len, i, err;
10596 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000010597 struct tg3_napi *tnapi, *rnapi;
Matt Carlson21f581a2009-08-28 14:00:25 +000010598 struct tg3_rx_prodring_set *tpr = &tp->prodring[0];
Michael Chanc76949a2005-05-29 14:58:59 -070010599
Matt Carlsonc8873402010-02-12 14:47:11 +000010600 tnapi = &tp->napi[0];
10601 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000010602 if (tp->irq_cnt > 1) {
Matt Carlson0c1d0e22009-09-01 13:16:33 +000010603 rnapi = &tp->napi[1];
Matt Carlsonc8873402010-02-12 14:47:11 +000010604 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
10605 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000010606 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010607 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000010608
Michael Chan9f40dea2005-09-05 17:53:06 -070010609 if (loopback_mode == TG3_MAC_LOOPBACK) {
Michael Chanc94e3942005-09-27 12:12:42 -070010610 /* HW errata - mac loopback fails in some cases on 5780.
10611 * Normal traffic and PHY loopback are not affected by
10612 * errata.
10613 */
10614 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780)
10615 return 0;
10616
Michael Chan9f40dea2005-09-05 17:53:06 -070010617 mac_mode = (tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK) |
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010618 MAC_MODE_PORT_INT_LPBACK;
10619 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
10620 mac_mode |= MAC_MODE_LINK_POLARITY;
Michael Chan3f7045c2006-09-27 16:02:29 -070010621 if (tp->tg3_flags & TG3_FLAG_10_100_ONLY)
10622 mac_mode |= MAC_MODE_PORT_MODE_MII;
10623 else
10624 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chan9f40dea2005-09-05 17:53:06 -070010625 tw32(MAC_MODE, mac_mode);
10626 } else if (loopback_mode == TG3_PHY_LOOPBACK) {
Michael Chan3f7045c2006-09-27 16:02:29 -070010627 u32 val;
10628
Matt Carlson7f97a4b2009-08-25 10:10:03 +000010629 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
10630 tg3_phy_fet_toggle_apd(tp, false);
Michael Chan5d64ad32006-12-07 00:19:40 -080010631 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED100;
10632 } else
10633 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED1000;
Michael Chan3f7045c2006-09-27 16:02:29 -070010634
Matt Carlson9ef8ca92007-07-11 19:48:29 -070010635 tg3_phy_toggle_automdix(tp, 0);
10636
Michael Chan3f7045c2006-09-27 16:02:29 -070010637 tg3_writephy(tp, MII_BMCR, val);
Michael Chanc94e3942005-09-27 12:12:42 -070010638 udelay(40);
Michael Chan5d64ad32006-12-07 00:19:40 -080010639
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010640 mac_mode = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000010641 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
Matt Carlson1061b7c2010-02-12 14:47:12 +000010642 tg3_writephy(tp, MII_TG3_FET_PTEST,
10643 MII_TG3_FET_PTEST_FRC_TX_LINK |
10644 MII_TG3_FET_PTEST_FRC_TX_LOCK);
10645 /* The write needs to be flushed for the AC131 */
10646 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
10647 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
Michael Chan5d64ad32006-12-07 00:19:40 -080010648 mac_mode |= MAC_MODE_PORT_MODE_MII;
10649 } else
10650 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chanb16250e2006-09-27 16:10:14 -070010651
Michael Chanc94e3942005-09-27 12:12:42 -070010652 /* reset to prevent losing 1st rx packet intermittently */
10653 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES) {
10654 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
10655 udelay(10);
10656 tw32_f(MAC_RX_MODE, tp->rx_mode);
10657 }
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010658 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlson79eb6902010-02-17 15:17:03 +000010659 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
10660 if (masked_phy_id == TG3_PHY_ID_BCM5401)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010661 mac_mode &= ~MAC_MODE_LINK_POLARITY;
Matt Carlson79eb6902010-02-17 15:17:03 +000010662 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010663 mac_mode |= MAC_MODE_LINK_POLARITY;
Michael Chanff18ff02006-03-27 23:17:27 -080010664 tg3_writephy(tp, MII_TG3_EXT_CTRL,
10665 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
10666 }
Michael Chan9f40dea2005-09-05 17:53:06 -070010667 tw32(MAC_MODE, mac_mode);
Matt Carlson859a588792010-04-05 10:19:28 +000010668 } else {
Michael Chan9f40dea2005-09-05 17:53:06 -070010669 return -EINVAL;
Matt Carlson859a588792010-04-05 10:19:28 +000010670 }
Michael Chanc76949a2005-05-29 14:58:59 -070010671
10672 err = -EIO;
10673
Michael Chanc76949a2005-05-29 14:58:59 -070010674 tx_len = 1514;
David S. Millera20e9c62006-07-31 22:38:16 -070010675 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070010676 if (!skb)
10677 return -ENOMEM;
10678
Michael Chanc76949a2005-05-29 14:58:59 -070010679 tx_data = skb_put(skb, tx_len);
10680 memcpy(tx_data, tp->dev->dev_addr, 6);
10681 memset(tx_data + 6, 0x0, 8);
10682
10683 tw32(MAC_RX_MTU_SIZE, tx_len + 4);
10684
10685 for (i = 14; i < tx_len; i++)
10686 tx_data[i] = (u8) (i & 0xff);
10687
Alexander Duyckf4188d82009-12-02 16:48:38 +000010688 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
10689 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000010690 dev_kfree_skb(skb);
10691 return -EIO;
10692 }
Michael Chanc76949a2005-05-29 14:58:59 -070010693
10694 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010695 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070010696
10697 udelay(10);
10698
Matt Carlson898a56f2009-08-28 14:02:40 +000010699 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070010700
Michael Chanc76949a2005-05-29 14:58:59 -070010701 num_pkts = 0;
10702
Alexander Duyckf4188d82009-12-02 16:48:38 +000010703 tg3_set_txd(tnapi, tnapi->tx_prod, map, tx_len, 0, 1);
Michael Chanc76949a2005-05-29 14:58:59 -070010704
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010705 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070010706 num_pkts++;
10707
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010708 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
10709 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070010710
10711 udelay(10);
10712
Matt Carlson303fc922009-11-02 14:27:34 +000010713 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
10714 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070010715 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010716 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070010717
10718 udelay(10);
10719
Matt Carlson898a56f2009-08-28 14:02:40 +000010720 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
10721 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010722 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070010723 (rx_idx == (rx_start_idx + num_pkts)))
10724 break;
10725 }
10726
Alexander Duyckf4188d82009-12-02 16:48:38 +000010727 pci_unmap_single(tp->pdev, map, tx_len, PCI_DMA_TODEVICE);
Michael Chanc76949a2005-05-29 14:58:59 -070010728 dev_kfree_skb(skb);
10729
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010730 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070010731 goto out;
10732
10733 if (rx_idx != rx_start_idx + num_pkts)
10734 goto out;
10735
Matt Carlson72334482009-08-28 14:03:01 +000010736 desc = &rnapi->rx_rcb[rx_start_idx];
Michael Chanc76949a2005-05-29 14:58:59 -070010737 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
10738 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
10739 if (opaque_key != RXD_OPAQUE_RING_STD)
10740 goto out;
10741
10742 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
10743 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
10744 goto out;
10745
10746 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) - 4;
10747 if (rx_len != tx_len)
10748 goto out;
10749
Matt Carlson21f581a2009-08-28 14:00:25 +000010750 rx_skb = tpr->rx_std_buffers[desc_idx].skb;
Michael Chanc76949a2005-05-29 14:58:59 -070010751
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +000010752 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx], mapping);
Michael Chanc76949a2005-05-29 14:58:59 -070010753 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len, PCI_DMA_FROMDEVICE);
10754
10755 for (i = 14; i < tx_len; i++) {
10756 if (*(rx_skb->data + i) != (u8) (i & 0xff))
10757 goto out;
10758 }
10759 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010760
Michael Chanc76949a2005-05-29 14:58:59 -070010761 /* tg3_free_rings will unmap and free the rx_skb */
10762out:
10763 return err;
10764}
10765
Michael Chan9f40dea2005-09-05 17:53:06 -070010766#define TG3_MAC_LOOPBACK_FAILED 1
10767#define TG3_PHY_LOOPBACK_FAILED 2
10768#define TG3_LOOPBACK_FAILED (TG3_MAC_LOOPBACK_FAILED | \
10769 TG3_PHY_LOOPBACK_FAILED)
10770
10771static int tg3_test_loopback(struct tg3 *tp)
10772{
10773 int err = 0;
Matt Carlson9936bcf2007-10-10 18:03:07 -070010774 u32 cpmuctrl = 0;
Michael Chan9f40dea2005-09-05 17:53:06 -070010775
10776 if (!netif_running(tp->dev))
10777 return TG3_LOOPBACK_FAILED;
10778
Michael Chanb9ec6c12006-07-25 16:37:27 -070010779 err = tg3_reset_hw(tp, 1);
10780 if (err)
10781 return TG3_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070010782
Matt Carlson6833c042008-11-21 17:18:59 -080010783 /* Turn off gphy autopowerdown. */
10784 if (tp->tg3_flags3 & TG3_FLG3_PHY_ENABLE_APD)
10785 tg3_phy_toggle_apd(tp, false);
10786
Matt Carlson321d32a2008-11-21 17:22:19 -080010787 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070010788 int i;
10789 u32 status;
10790
10791 tw32(TG3_CPMU_MUTEX_REQ, CPMU_MUTEX_REQ_DRIVER);
10792
10793 /* Wait for up to 40 microseconds to acquire lock. */
10794 for (i = 0; i < 4; i++) {
10795 status = tr32(TG3_CPMU_MUTEX_GNT);
10796 if (status == CPMU_MUTEX_GNT_DRIVER)
10797 break;
10798 udelay(10);
10799 }
10800
10801 if (status != CPMU_MUTEX_GNT_DRIVER)
10802 return TG3_LOOPBACK_FAILED;
10803
Matt Carlsonb2a5c192008-04-03 21:44:44 -070010804 /* Turn off link-based power management. */
Matt Carlsone8750932007-11-12 21:11:51 -080010805 cpmuctrl = tr32(TG3_CPMU_CTRL);
Matt Carlson109115e2008-05-02 16:48:59 -070010806 tw32(TG3_CPMU_CTRL,
10807 cpmuctrl & ~(CPMU_CTRL_LINK_SPEED_MODE |
10808 CPMU_CTRL_LINK_AWARE_MODE));
Matt Carlson9936bcf2007-10-10 18:03:07 -070010809 }
10810
Michael Chan9f40dea2005-09-05 17:53:06 -070010811 if (tg3_run_loopback(tp, TG3_MAC_LOOPBACK))
10812 err |= TG3_MAC_LOOPBACK_FAILED;
Matt Carlson9936bcf2007-10-10 18:03:07 -070010813
Matt Carlson321d32a2008-11-21 17:22:19 -080010814 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070010815 tw32(TG3_CPMU_CTRL, cpmuctrl);
10816
10817 /* Release the mutex */
10818 tw32(TG3_CPMU_MUTEX_GNT, CPMU_MUTEX_GNT_DRIVER);
10819 }
10820
Matt Carlsondd477002008-05-25 23:45:58 -070010821 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
10822 !(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)) {
Michael Chan9f40dea2005-09-05 17:53:06 -070010823 if (tg3_run_loopback(tp, TG3_PHY_LOOPBACK))
10824 err |= TG3_PHY_LOOPBACK_FAILED;
10825 }
10826
Matt Carlson6833c042008-11-21 17:18:59 -080010827 /* Re-enable gphy autopowerdown. */
10828 if (tp->tg3_flags3 & TG3_FLG3_PHY_ENABLE_APD)
10829 tg3_phy_toggle_apd(tp, true);
10830
Michael Chan9f40dea2005-09-05 17:53:06 -070010831 return err;
10832}
10833
Michael Chan4cafd3f2005-05-29 14:56:34 -070010834static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
10835 u64 *data)
10836{
Michael Chan566f86a2005-05-29 14:56:58 -070010837 struct tg3 *tp = netdev_priv(dev);
10838
Michael Chanbc1c7562006-03-20 17:48:03 -080010839 if (tp->link_config.phy_is_low_power)
10840 tg3_set_power_state(tp, PCI_D0);
10841
Michael Chan566f86a2005-05-29 14:56:58 -070010842 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
10843
10844 if (tg3_test_nvram(tp) != 0) {
10845 etest->flags |= ETH_TEST_FL_FAILED;
10846 data[0] = 1;
10847 }
Michael Chanca430072005-05-29 14:57:23 -070010848 if (tg3_test_link(tp) != 0) {
10849 etest->flags |= ETH_TEST_FL_FAILED;
10850 data[1] = 1;
10851 }
Michael Chana71116d2005-05-29 14:58:11 -070010852 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010853 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070010854
Michael Chanbbe832c2005-06-24 20:20:04 -070010855 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010856 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010857 tg3_netif_stop(tp);
10858 irq_sync = 1;
10859 }
10860
10861 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070010862
10863 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080010864 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070010865 tg3_halt_cpu(tp, RX_CPU_BASE);
10866 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
10867 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080010868 if (!err)
10869 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070010870
Michael Chand9ab5ad2006-03-20 22:27:35 -080010871 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)
10872 tg3_phy_reset(tp);
10873
Michael Chana71116d2005-05-29 14:58:11 -070010874 if (tg3_test_registers(tp) != 0) {
10875 etest->flags |= ETH_TEST_FL_FAILED;
10876 data[2] = 1;
10877 }
Michael Chan7942e1d2005-05-29 14:58:36 -070010878 if (tg3_test_memory(tp) != 0) {
10879 etest->flags |= ETH_TEST_FL_FAILED;
10880 data[3] = 1;
10881 }
Michael Chan9f40dea2005-09-05 17:53:06 -070010882 if ((data[4] = tg3_test_loopback(tp)) != 0)
Michael Chanc76949a2005-05-29 14:58:59 -070010883 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070010884
David S. Millerf47c11e2005-06-24 20:18:35 -070010885 tg3_full_unlock(tp);
10886
Michael Chand4bc3922005-05-29 14:59:20 -070010887 if (tg3_test_interrupt(tp) != 0) {
10888 etest->flags |= ETH_TEST_FL_FAILED;
10889 data[5] = 1;
10890 }
David S. Millerf47c11e2005-06-24 20:18:35 -070010891
10892 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070010893
Michael Chana71116d2005-05-29 14:58:11 -070010894 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10895 if (netif_running(dev)) {
10896 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010897 err2 = tg3_restart_hw(tp, 1);
10898 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070010899 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070010900 }
David S. Millerf47c11e2005-06-24 20:18:35 -070010901
10902 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010903
10904 if (irq_sync && !err2)
10905 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070010906 }
Michael Chanbc1c7562006-03-20 17:48:03 -080010907 if (tp->link_config.phy_is_low_power)
10908 tg3_set_power_state(tp, PCI_D3hot);
10909
Michael Chan4cafd3f2005-05-29 14:56:34 -070010910}
10911
Linus Torvalds1da177e2005-04-16 15:20:36 -070010912static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
10913{
10914 struct mii_ioctl_data *data = if_mii(ifr);
10915 struct tg3 *tp = netdev_priv(dev);
10916 int err;
10917
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010918 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010919 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010920 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
10921 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010922 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000010923 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010924 }
10925
Matt Carlson33f401a2010-04-05 10:19:27 +000010926 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010927 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000010928 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010929
10930 /* fallthru */
10931 case SIOCGMIIREG: {
10932 u32 mii_regval;
10933
10934 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
10935 break; /* We have no PHY */
10936
Michael Chanbc1c7562006-03-20 17:48:03 -080010937 if (tp->link_config.phy_is_low_power)
10938 return -EAGAIN;
10939
David S. Millerf47c11e2005-06-24 20:18:35 -070010940 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010941 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070010942 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010943
10944 data->val_out = mii_regval;
10945
10946 return err;
10947 }
10948
10949 case SIOCSMIIREG:
10950 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
10951 break; /* We have no PHY */
10952
Michael Chanbc1c7562006-03-20 17:48:03 -080010953 if (tp->link_config.phy_is_low_power)
10954 return -EAGAIN;
10955
David S. Millerf47c11e2005-06-24 20:18:35 -070010956 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010957 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070010958 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010959
10960 return err;
10961
10962 default:
10963 /* do nothing */
10964 break;
10965 }
10966 return -EOPNOTSUPP;
10967}
10968
10969#if TG3_VLAN_TAG_USED
10970static void tg3_vlan_rx_register(struct net_device *dev, struct vlan_group *grp)
10971{
10972 struct tg3 *tp = netdev_priv(dev);
10973
Matt Carlson844b3ee2009-02-25 14:23:56 +000010974 if (!netif_running(dev)) {
10975 tp->vlgrp = grp;
10976 return;
10977 }
10978
10979 tg3_netif_stop(tp);
Michael Chan29315e82006-06-29 20:12:30 -070010980
David S. Millerf47c11e2005-06-24 20:18:35 -070010981 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010982
10983 tp->vlgrp = grp;
10984
10985 /* Update RX_MODE_KEEP_VLAN_TAG bit in RX_MODE register. */
10986 __tg3_set_rx_mode(dev);
10987
Matt Carlson844b3ee2009-02-25 14:23:56 +000010988 tg3_netif_start(tp);
Michael Chan46966542007-07-11 19:47:19 -070010989
10990 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010991}
Linus Torvalds1da177e2005-04-16 15:20:36 -070010992#endif
10993
David S. Miller15f98502005-05-18 22:49:26 -070010994static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
10995{
10996 struct tg3 *tp = netdev_priv(dev);
10997
10998 memcpy(ec, &tp->coal, sizeof(*ec));
10999 return 0;
11000}
11001
Michael Chand244c892005-07-05 14:42:33 -070011002static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11003{
11004 struct tg3 *tp = netdev_priv(dev);
11005 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
11006 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
11007
11008 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
11009 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
11010 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
11011 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
11012 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
11013 }
11014
11015 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
11016 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
11017 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
11018 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
11019 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
11020 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
11021 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
11022 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
11023 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
11024 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
11025 return -EINVAL;
11026
11027 /* No rx interrupts will be generated if both are zero */
11028 if ((ec->rx_coalesce_usecs == 0) &&
11029 (ec->rx_max_coalesced_frames == 0))
11030 return -EINVAL;
11031
11032 /* No tx interrupts will be generated if both are zero */
11033 if ((ec->tx_coalesce_usecs == 0) &&
11034 (ec->tx_max_coalesced_frames == 0))
11035 return -EINVAL;
11036
11037 /* Only copy relevant parameters, ignore all others. */
11038 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
11039 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
11040 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
11041 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
11042 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
11043 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
11044 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
11045 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
11046 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
11047
11048 if (netif_running(dev)) {
11049 tg3_full_lock(tp, 0);
11050 __tg3_set_coalesce(tp, &tp->coal);
11051 tg3_full_unlock(tp);
11052 }
11053 return 0;
11054}
11055
Jeff Garzik7282d492006-09-13 14:30:00 -040011056static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011057 .get_settings = tg3_get_settings,
11058 .set_settings = tg3_set_settings,
11059 .get_drvinfo = tg3_get_drvinfo,
11060 .get_regs_len = tg3_get_regs_len,
11061 .get_regs = tg3_get_regs,
11062 .get_wol = tg3_get_wol,
11063 .set_wol = tg3_set_wol,
11064 .get_msglevel = tg3_get_msglevel,
11065 .set_msglevel = tg3_set_msglevel,
11066 .nway_reset = tg3_nway_reset,
11067 .get_link = ethtool_op_get_link,
11068 .get_eeprom_len = tg3_get_eeprom_len,
11069 .get_eeprom = tg3_get_eeprom,
11070 .set_eeprom = tg3_set_eeprom,
11071 .get_ringparam = tg3_get_ringparam,
11072 .set_ringparam = tg3_set_ringparam,
11073 .get_pauseparam = tg3_get_pauseparam,
11074 .set_pauseparam = tg3_set_pauseparam,
11075 .get_rx_csum = tg3_get_rx_csum,
11076 .set_rx_csum = tg3_set_rx_csum,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011077 .set_tx_csum = tg3_set_tx_csum,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011078 .set_sg = ethtool_op_set_sg,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011079 .set_tso = tg3_set_tso,
Michael Chan4cafd3f2005-05-29 14:56:34 -070011080 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011081 .get_strings = tg3_get_strings,
Michael Chan4009a932005-09-05 17:52:54 -070011082 .phys_id = tg3_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011083 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070011084 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070011085 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070011086 .get_sset_count = tg3_get_sset_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011087};
11088
11089static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
11090{
Michael Chan1b277772006-03-20 22:27:48 -080011091 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011092
11093 tp->nvram_size = EEPROM_CHIP_SIZE;
11094
Matt Carlsone4f34112009-02-25 14:25:00 +000011095 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011096 return;
11097
Michael Chanb16250e2006-09-27 16:10:14 -070011098 if ((magic != TG3_EEPROM_MAGIC) &&
11099 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
11100 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011101 return;
11102
11103 /*
11104 * Size the chip by reading offsets at increasing powers of two.
11105 * When we encounter our validation signature, we know the addressing
11106 * has wrapped around, and thus have our chip size.
11107 */
Michael Chan1b277772006-03-20 22:27:48 -080011108 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011109
11110 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000011111 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011112 return;
11113
Michael Chan18201802006-03-20 22:29:15 -080011114 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011115 break;
11116
11117 cursize <<= 1;
11118 }
11119
11120 tp->nvram_size = cursize;
11121}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011122
Linus Torvalds1da177e2005-04-16 15:20:36 -070011123static void __devinit tg3_get_nvram_size(struct tg3 *tp)
11124{
11125 u32 val;
11126
Matt Carlsondf259d82009-04-20 06:57:14 +000011127 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
11128 tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080011129 return;
11130
11131 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080011132 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080011133 tg3_get_eeprom_size(tp);
11134 return;
11135 }
11136
Matt Carlson6d348f22009-02-25 14:25:52 +000011137 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011138 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000011139 /* This is confusing. We want to operate on the
11140 * 16-bit value at offset 0xf2. The tg3_nvram_read()
11141 * call will read from NVRAM and byteswap the data
11142 * according to the byteswapping settings for all
11143 * other register accesses. This ensures the data we
11144 * want will always reside in the lower 16-bits.
11145 * However, the data in NVRAM is in LE format, which
11146 * means the data from the NVRAM read will always be
11147 * opposite the endianness of the CPU. The 16-bit
11148 * byteswap then brings the data to CPU endianness.
11149 */
11150 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011151 return;
11152 }
11153 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070011154 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011155}
11156
11157static void __devinit tg3_get_nvram_info(struct tg3 *tp)
11158{
11159 u32 nvcfg1;
11160
11161 nvcfg1 = tr32(NVRAM_CFG1);
11162 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
11163 tp->tg3_flags2 |= TG3_FLG2_FLASH;
Matt Carlson8590a602009-08-28 12:29:16 +000011164 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011165 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11166 tw32(NVRAM_CFG1, nvcfg1);
11167 }
11168
Michael Chan4c987482005-09-05 17:52:38 -070011169 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750) ||
Michael Chana4e2b342005-10-26 15:46:52 -070011170 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011171 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011172 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
11173 tp->nvram_jedecnum = JEDEC_ATMEL;
11174 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
11175 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11176 break;
11177 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
11178 tp->nvram_jedecnum = JEDEC_ATMEL;
11179 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
11180 break;
11181 case FLASH_VENDOR_ATMEL_EEPROM:
11182 tp->nvram_jedecnum = JEDEC_ATMEL;
11183 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11184 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11185 break;
11186 case FLASH_VENDOR_ST:
11187 tp->nvram_jedecnum = JEDEC_ST;
11188 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
11189 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11190 break;
11191 case FLASH_VENDOR_SAIFUN:
11192 tp->nvram_jedecnum = JEDEC_SAIFUN;
11193 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
11194 break;
11195 case FLASH_VENDOR_SST_SMALL:
11196 case FLASH_VENDOR_SST_LARGE:
11197 tp->nvram_jedecnum = JEDEC_SST;
11198 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
11199 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011200 }
Matt Carlson8590a602009-08-28 12:29:16 +000011201 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011202 tp->nvram_jedecnum = JEDEC_ATMEL;
11203 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
11204 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11205 }
11206}
11207
Matt Carlsona1b950d2009-09-01 13:20:17 +000011208static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
11209{
11210 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
11211 case FLASH_5752PAGE_SIZE_256:
11212 tp->nvram_pagesize = 256;
11213 break;
11214 case FLASH_5752PAGE_SIZE_512:
11215 tp->nvram_pagesize = 512;
11216 break;
11217 case FLASH_5752PAGE_SIZE_1K:
11218 tp->nvram_pagesize = 1024;
11219 break;
11220 case FLASH_5752PAGE_SIZE_2K:
11221 tp->nvram_pagesize = 2048;
11222 break;
11223 case FLASH_5752PAGE_SIZE_4K:
11224 tp->nvram_pagesize = 4096;
11225 break;
11226 case FLASH_5752PAGE_SIZE_264:
11227 tp->nvram_pagesize = 264;
11228 break;
11229 case FLASH_5752PAGE_SIZE_528:
11230 tp->nvram_pagesize = 528;
11231 break;
11232 }
11233}
11234
Michael Chan361b4ac2005-04-21 17:11:21 -070011235static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
11236{
11237 u32 nvcfg1;
11238
11239 nvcfg1 = tr32(NVRAM_CFG1);
11240
Michael Chane6af3012005-04-21 17:12:05 -070011241 /* NVRAM protection for TPM */
11242 if (nvcfg1 & (1 << 27))
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011243 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Michael Chane6af3012005-04-21 17:12:05 -070011244
Michael Chan361b4ac2005-04-21 17:11:21 -070011245 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011246 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
11247 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
11248 tp->nvram_jedecnum = JEDEC_ATMEL;
11249 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11250 break;
11251 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11252 tp->nvram_jedecnum = JEDEC_ATMEL;
11253 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11254 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11255 break;
11256 case FLASH_5752VENDOR_ST_M45PE10:
11257 case FLASH_5752VENDOR_ST_M45PE20:
11258 case FLASH_5752VENDOR_ST_M45PE40:
11259 tp->nvram_jedecnum = JEDEC_ST;
11260 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11261 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11262 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070011263 }
11264
11265 if (tp->tg3_flags2 & TG3_FLG2_FLASH) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000011266 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000011267 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070011268 /* For eeprom, set pagesize to maximum eeprom size */
11269 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11270
11271 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11272 tw32(NVRAM_CFG1, nvcfg1);
11273 }
11274}
11275
Michael Chand3c7b882006-03-23 01:28:25 -080011276static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
11277{
Matt Carlson989a9d22007-05-05 11:51:05 -070011278 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080011279
11280 nvcfg1 = tr32(NVRAM_CFG1);
11281
11282 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070011283 if (nvcfg1 & (1 << 27)) {
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011284 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Matt Carlson989a9d22007-05-05 11:51:05 -070011285 protect = 1;
11286 }
Michael Chand3c7b882006-03-23 01:28:25 -080011287
Matt Carlson989a9d22007-05-05 11:51:05 -070011288 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11289 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011290 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11291 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11292 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11293 case FLASH_5755VENDOR_ATMEL_FLASH_5:
11294 tp->nvram_jedecnum = JEDEC_ATMEL;
11295 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11296 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11297 tp->nvram_pagesize = 264;
11298 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
11299 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
11300 tp->nvram_size = (protect ? 0x3e200 :
11301 TG3_NVRAM_SIZE_512KB);
11302 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
11303 tp->nvram_size = (protect ? 0x1f200 :
11304 TG3_NVRAM_SIZE_256KB);
11305 else
11306 tp->nvram_size = (protect ? 0x1f200 :
11307 TG3_NVRAM_SIZE_128KB);
11308 break;
11309 case FLASH_5752VENDOR_ST_M45PE10:
11310 case FLASH_5752VENDOR_ST_M45PE20:
11311 case FLASH_5752VENDOR_ST_M45PE40:
11312 tp->nvram_jedecnum = JEDEC_ST;
11313 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11314 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11315 tp->nvram_pagesize = 256;
11316 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
11317 tp->nvram_size = (protect ?
11318 TG3_NVRAM_SIZE_64KB :
11319 TG3_NVRAM_SIZE_128KB);
11320 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
11321 tp->nvram_size = (protect ?
11322 TG3_NVRAM_SIZE_64KB :
11323 TG3_NVRAM_SIZE_256KB);
11324 else
11325 tp->nvram_size = (protect ?
11326 TG3_NVRAM_SIZE_128KB :
11327 TG3_NVRAM_SIZE_512KB);
11328 break;
Michael Chand3c7b882006-03-23 01:28:25 -080011329 }
11330}
11331
Michael Chan1b277772006-03-20 22:27:48 -080011332static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
11333{
11334 u32 nvcfg1;
11335
11336 nvcfg1 = tr32(NVRAM_CFG1);
11337
11338 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011339 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
11340 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11341 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
11342 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11343 tp->nvram_jedecnum = JEDEC_ATMEL;
11344 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11345 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080011346
Matt Carlson8590a602009-08-28 12:29:16 +000011347 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11348 tw32(NVRAM_CFG1, nvcfg1);
11349 break;
11350 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11351 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11352 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11353 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11354 tp->nvram_jedecnum = JEDEC_ATMEL;
11355 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11356 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11357 tp->nvram_pagesize = 264;
11358 break;
11359 case FLASH_5752VENDOR_ST_M45PE10:
11360 case FLASH_5752VENDOR_ST_M45PE20:
11361 case FLASH_5752VENDOR_ST_M45PE40:
11362 tp->nvram_jedecnum = JEDEC_ST;
11363 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11364 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11365 tp->nvram_pagesize = 256;
11366 break;
Michael Chan1b277772006-03-20 22:27:48 -080011367 }
11368}
11369
Matt Carlson6b91fa02007-10-10 18:01:09 -070011370static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
11371{
11372 u32 nvcfg1, protect = 0;
11373
11374 nvcfg1 = tr32(NVRAM_CFG1);
11375
11376 /* NVRAM protection for TPM */
11377 if (nvcfg1 & (1 << 27)) {
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011378 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011379 protect = 1;
11380 }
11381
11382 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11383 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011384 case FLASH_5761VENDOR_ATMEL_ADB021D:
11385 case FLASH_5761VENDOR_ATMEL_ADB041D:
11386 case FLASH_5761VENDOR_ATMEL_ADB081D:
11387 case FLASH_5761VENDOR_ATMEL_ADB161D:
11388 case FLASH_5761VENDOR_ATMEL_MDB021D:
11389 case FLASH_5761VENDOR_ATMEL_MDB041D:
11390 case FLASH_5761VENDOR_ATMEL_MDB081D:
11391 case FLASH_5761VENDOR_ATMEL_MDB161D:
11392 tp->nvram_jedecnum = JEDEC_ATMEL;
11393 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11394 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11395 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
11396 tp->nvram_pagesize = 256;
11397 break;
11398 case FLASH_5761VENDOR_ST_A_M45PE20:
11399 case FLASH_5761VENDOR_ST_A_M45PE40:
11400 case FLASH_5761VENDOR_ST_A_M45PE80:
11401 case FLASH_5761VENDOR_ST_A_M45PE16:
11402 case FLASH_5761VENDOR_ST_M_M45PE20:
11403 case FLASH_5761VENDOR_ST_M_M45PE40:
11404 case FLASH_5761VENDOR_ST_M_M45PE80:
11405 case FLASH_5761VENDOR_ST_M_M45PE16:
11406 tp->nvram_jedecnum = JEDEC_ST;
11407 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11408 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11409 tp->nvram_pagesize = 256;
11410 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011411 }
11412
11413 if (protect) {
11414 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
11415 } else {
11416 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011417 case FLASH_5761VENDOR_ATMEL_ADB161D:
11418 case FLASH_5761VENDOR_ATMEL_MDB161D:
11419 case FLASH_5761VENDOR_ST_A_M45PE16:
11420 case FLASH_5761VENDOR_ST_M_M45PE16:
11421 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
11422 break;
11423 case FLASH_5761VENDOR_ATMEL_ADB081D:
11424 case FLASH_5761VENDOR_ATMEL_MDB081D:
11425 case FLASH_5761VENDOR_ST_A_M45PE80:
11426 case FLASH_5761VENDOR_ST_M_M45PE80:
11427 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
11428 break;
11429 case FLASH_5761VENDOR_ATMEL_ADB041D:
11430 case FLASH_5761VENDOR_ATMEL_MDB041D:
11431 case FLASH_5761VENDOR_ST_A_M45PE40:
11432 case FLASH_5761VENDOR_ST_M_M45PE40:
11433 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11434 break;
11435 case FLASH_5761VENDOR_ATMEL_ADB021D:
11436 case FLASH_5761VENDOR_ATMEL_MDB021D:
11437 case FLASH_5761VENDOR_ST_A_M45PE20:
11438 case FLASH_5761VENDOR_ST_M_M45PE20:
11439 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11440 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011441 }
11442 }
11443}
11444
Michael Chanb5d37722006-09-27 16:06:21 -070011445static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
11446{
11447 tp->nvram_jedecnum = JEDEC_ATMEL;
11448 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11449 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11450}
11451
Matt Carlson321d32a2008-11-21 17:22:19 -080011452static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
11453{
11454 u32 nvcfg1;
11455
11456 nvcfg1 = tr32(NVRAM_CFG1);
11457
11458 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11459 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11460 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11461 tp->nvram_jedecnum = JEDEC_ATMEL;
11462 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11463 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11464
11465 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11466 tw32(NVRAM_CFG1, nvcfg1);
11467 return;
11468 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11469 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11470 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11471 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11472 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11473 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11474 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
11475 tp->nvram_jedecnum = JEDEC_ATMEL;
11476 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11477 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11478
11479 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11480 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11481 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11482 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11483 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11484 break;
11485 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11486 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11487 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11488 break;
11489 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11490 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
11491 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11492 break;
11493 }
11494 break;
11495 case FLASH_5752VENDOR_ST_M45PE10:
11496 case FLASH_5752VENDOR_ST_M45PE20:
11497 case FLASH_5752VENDOR_ST_M45PE40:
11498 tp->nvram_jedecnum = JEDEC_ST;
11499 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11500 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11501
11502 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11503 case FLASH_5752VENDOR_ST_M45PE10:
11504 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11505 break;
11506 case FLASH_5752VENDOR_ST_M45PE20:
11507 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11508 break;
11509 case FLASH_5752VENDOR_ST_M45PE40:
11510 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11511 break;
11512 }
11513 break;
11514 default:
Matt Carlsondf259d82009-04-20 06:57:14 +000011515 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
Matt Carlson321d32a2008-11-21 17:22:19 -080011516 return;
11517 }
11518
Matt Carlsona1b950d2009-09-01 13:20:17 +000011519 tg3_nvram_get_pagesize(tp, nvcfg1);
11520 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Matt Carlson321d32a2008-11-21 17:22:19 -080011521 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011522}
11523
11524
11525static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
11526{
11527 u32 nvcfg1;
11528
11529 nvcfg1 = tr32(NVRAM_CFG1);
11530
11531 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11532 case FLASH_5717VENDOR_ATMEL_EEPROM:
11533 case FLASH_5717VENDOR_MICRO_EEPROM:
11534 tp->nvram_jedecnum = JEDEC_ATMEL;
11535 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11536 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11537
11538 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11539 tw32(NVRAM_CFG1, nvcfg1);
11540 return;
11541 case FLASH_5717VENDOR_ATMEL_MDB011D:
11542 case FLASH_5717VENDOR_ATMEL_ADB011B:
11543 case FLASH_5717VENDOR_ATMEL_ADB011D:
11544 case FLASH_5717VENDOR_ATMEL_MDB021D:
11545 case FLASH_5717VENDOR_ATMEL_ADB021B:
11546 case FLASH_5717VENDOR_ATMEL_ADB021D:
11547 case FLASH_5717VENDOR_ATMEL_45USPT:
11548 tp->nvram_jedecnum = JEDEC_ATMEL;
11549 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11550 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11551
11552 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11553 case FLASH_5717VENDOR_ATMEL_MDB021D:
11554 case FLASH_5717VENDOR_ATMEL_ADB021B:
11555 case FLASH_5717VENDOR_ATMEL_ADB021D:
11556 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11557 break;
11558 default:
11559 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11560 break;
11561 }
Matt Carlson321d32a2008-11-21 17:22:19 -080011562 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011563 case FLASH_5717VENDOR_ST_M_M25PE10:
11564 case FLASH_5717VENDOR_ST_A_M25PE10:
11565 case FLASH_5717VENDOR_ST_M_M45PE10:
11566 case FLASH_5717VENDOR_ST_A_M45PE10:
11567 case FLASH_5717VENDOR_ST_M_M25PE20:
11568 case FLASH_5717VENDOR_ST_A_M25PE20:
11569 case FLASH_5717VENDOR_ST_M_M45PE20:
11570 case FLASH_5717VENDOR_ST_A_M45PE20:
11571 case FLASH_5717VENDOR_ST_25USPT:
11572 case FLASH_5717VENDOR_ST_45USPT:
11573 tp->nvram_jedecnum = JEDEC_ST;
11574 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11575 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11576
11577 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
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 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11583 break;
11584 default:
11585 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11586 break;
11587 }
Matt Carlson321d32a2008-11-21 17:22:19 -080011588 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011589 default:
11590 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
11591 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080011592 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000011593
11594 tg3_nvram_get_pagesize(tp, nvcfg1);
11595 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
11596 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
Matt Carlson321d32a2008-11-21 17:22:19 -080011597}
11598
Linus Torvalds1da177e2005-04-16 15:20:36 -070011599/* Chips other than 5700/5701 use the NVRAM for fetching info. */
11600static void __devinit tg3_nvram_init(struct tg3 *tp)
11601{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011602 tw32_f(GRC_EEPROM_ADDR,
11603 (EEPROM_ADDR_FSM_RESET |
11604 (EEPROM_DEFAULT_CLOCK_PERIOD <<
11605 EEPROM_ADDR_CLKPERD_SHIFT)));
11606
Michael Chan9d57f012006-12-07 00:23:25 -080011607 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011608
11609 /* Enable seeprom accesses. */
11610 tw32_f(GRC_LOCAL_CTRL,
11611 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
11612 udelay(100);
11613
11614 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
11615 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
11616 tp->tg3_flags |= TG3_FLAG_NVRAM;
11617
Michael Chanec41c7d2006-01-17 02:40:55 -080011618 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000011619 netdev_warn(tp->dev,
11620 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000011621 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080011622 return;
11623 }
Michael Chane6af3012005-04-21 17:12:05 -070011624 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011625
Matt Carlson989a9d22007-05-05 11:51:05 -070011626 tp->nvram_size = 0;
11627
Michael Chan361b4ac2005-04-21 17:11:21 -070011628 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
11629 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080011630 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
11631 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070011632 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070011633 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
11634 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080011635 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070011636 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
11637 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070011638 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
11639 tg3_get_5906_nvram_info(tp);
Matt Carlsonb703df62009-12-03 08:36:21 +000011640 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
11641 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson321d32a2008-11-21 17:22:19 -080011642 tg3_get_57780_nvram_info(tp);
Matt Carlsona50d0792010-06-05 17:24:37 +000011643 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
11644 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000011645 tg3_get_5717_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070011646 else
11647 tg3_get_nvram_info(tp);
11648
Matt Carlson989a9d22007-05-05 11:51:05 -070011649 if (tp->nvram_size == 0)
11650 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011651
Michael Chane6af3012005-04-21 17:12:05 -070011652 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080011653 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011654
11655 } else {
11656 tp->tg3_flags &= ~(TG3_FLAG_NVRAM | TG3_FLAG_NVRAM_BUFFERED);
11657
11658 tg3_get_eeprom_size(tp);
11659 }
11660}
11661
Linus Torvalds1da177e2005-04-16 15:20:36 -070011662static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
11663 u32 offset, u32 len, u8 *buf)
11664{
11665 int i, j, rc = 0;
11666 u32 val;
11667
11668 for (i = 0; i < len; i += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080011669 u32 addr;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011670 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011671
11672 addr = offset + i;
11673
11674 memcpy(&data, buf + i, 4);
11675
Matt Carlson62cedd12009-04-20 14:52:29 -070011676 /*
11677 * The SEEPROM interface expects the data to always be opposite
11678 * the native endian format. We accomplish this by reversing
11679 * all the operations that would have been performed on the
11680 * data from a call to tg3_nvram_read_be32().
11681 */
11682 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011683
11684 val = tr32(GRC_EEPROM_ADDR);
11685 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
11686
11687 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
11688 EEPROM_ADDR_READ);
11689 tw32(GRC_EEPROM_ADDR, val |
11690 (0 << EEPROM_ADDR_DEVID_SHIFT) |
11691 (addr & EEPROM_ADDR_ADDR_MASK) |
11692 EEPROM_ADDR_START |
11693 EEPROM_ADDR_WRITE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011694
Michael Chan9d57f012006-12-07 00:23:25 -080011695 for (j = 0; j < 1000; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011696 val = tr32(GRC_EEPROM_ADDR);
11697
11698 if (val & EEPROM_ADDR_COMPLETE)
11699 break;
Michael Chan9d57f012006-12-07 00:23:25 -080011700 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011701 }
11702 if (!(val & EEPROM_ADDR_COMPLETE)) {
11703 rc = -EBUSY;
11704 break;
11705 }
11706 }
11707
11708 return rc;
11709}
11710
11711/* offset and length are dword aligned */
11712static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
11713 u8 *buf)
11714{
11715 int ret = 0;
11716 u32 pagesize = tp->nvram_pagesize;
11717 u32 pagemask = pagesize - 1;
11718 u32 nvram_cmd;
11719 u8 *tmp;
11720
11721 tmp = kmalloc(pagesize, GFP_KERNEL);
11722 if (tmp == NULL)
11723 return -ENOMEM;
11724
11725 while (len) {
11726 int j;
Michael Chane6af3012005-04-21 17:12:05 -070011727 u32 phy_addr, page_off, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011728
11729 phy_addr = offset & ~pagemask;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011730
Linus Torvalds1da177e2005-04-16 15:20:36 -070011731 for (j = 0; j < pagesize; j += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000011732 ret = tg3_nvram_read_be32(tp, phy_addr + j,
11733 (__be32 *) (tmp + j));
11734 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011735 break;
11736 }
11737 if (ret)
11738 break;
11739
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011740 page_off = offset & pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011741 size = pagesize;
11742 if (len < size)
11743 size = len;
11744
11745 len -= size;
11746
11747 memcpy(tmp + page_off, buf, size);
11748
11749 offset = offset + (pagesize - page_off);
11750
Michael Chane6af3012005-04-21 17:12:05 -070011751 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011752
11753 /*
11754 * Before we can erase the flash page, we need
11755 * to issue a special "write enable" command.
11756 */
11757 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
11758
11759 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
11760 break;
11761
11762 /* Erase the target page */
11763 tw32(NVRAM_ADDR, phy_addr);
11764
11765 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
11766 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
11767
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011768 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011769 break;
11770
11771 /* Issue another write enable to start the write. */
11772 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
11773
11774 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
11775 break;
11776
11777 for (j = 0; j < pagesize; j += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080011778 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011779
Al Virob9fc7dc2007-12-17 22:59:57 -080011780 data = *((__be32 *) (tmp + j));
Matt Carlsona9dc5292009-02-25 14:25:30 +000011781
Al Virob9fc7dc2007-12-17 22:59:57 -080011782 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011783
11784 tw32(NVRAM_ADDR, phy_addr + j);
11785
11786 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
11787 NVRAM_CMD_WR;
11788
11789 if (j == 0)
11790 nvram_cmd |= NVRAM_CMD_FIRST;
11791 else if (j == (pagesize - 4))
11792 nvram_cmd |= NVRAM_CMD_LAST;
11793
11794 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
11795 break;
11796 }
11797 if (ret)
11798 break;
11799 }
11800
11801 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
11802 tg3_nvram_exec_cmd(tp, nvram_cmd);
11803
11804 kfree(tmp);
11805
11806 return ret;
11807}
11808
11809/* offset and length are dword aligned */
11810static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
11811 u8 *buf)
11812{
11813 int i, ret = 0;
11814
11815 for (i = 0; i < len; i += 4, offset += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080011816 u32 page_off, phy_addr, nvram_cmd;
11817 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011818
11819 memcpy(&data, buf + i, 4);
Al Virob9fc7dc2007-12-17 22:59:57 -080011820 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011821
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011822 page_off = offset % tp->nvram_pagesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011823
Michael Chan18201802006-03-20 22:29:15 -080011824 phy_addr = tg3_nvram_phys_addr(tp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011825
11826 tw32(NVRAM_ADDR, phy_addr);
11827
11828 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
11829
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011830 if (page_off == 0 || i == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011831 nvram_cmd |= NVRAM_CMD_FIRST;
Michael Chanf6d9a252006-04-29 19:00:24 -070011832 if (page_off == (tp->nvram_pagesize - 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011833 nvram_cmd |= NVRAM_CMD_LAST;
11834
11835 if (i == (len - 4))
11836 nvram_cmd |= NVRAM_CMD_LAST;
11837
Matt Carlson321d32a2008-11-21 17:22:19 -080011838 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
11839 !(tp->tg3_flags3 & TG3_FLG3_5755_PLUS) &&
Michael Chan4c987482005-09-05 17:52:38 -070011840 (tp->nvram_jedecnum == JEDEC_ST) &&
11841 (nvram_cmd & NVRAM_CMD_FIRST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011842
11843 if ((ret = tg3_nvram_exec_cmd(tp,
11844 NVRAM_CMD_WREN | NVRAM_CMD_GO |
11845 NVRAM_CMD_DONE)))
11846
11847 break;
11848 }
11849 if (!(tp->tg3_flags2 & TG3_FLG2_FLASH)) {
11850 /* We always do complete word writes to eeprom. */
11851 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
11852 }
11853
11854 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
11855 break;
11856 }
11857 return ret;
11858}
11859
11860/* offset and length are dword aligned */
11861static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
11862{
11863 int ret;
11864
Linus Torvalds1da177e2005-04-16 15:20:36 -070011865 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT) {
Michael Chan314fba32005-04-21 17:07:04 -070011866 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
11867 ~GRC_LCLCTRL_GPIO_OUTPUT1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011868 udelay(40);
11869 }
11870
11871 if (!(tp->tg3_flags & TG3_FLAG_NVRAM)) {
11872 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
Matt Carlson859a588792010-04-05 10:19:28 +000011873 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011874 u32 grc_mode;
11875
Michael Chanec41c7d2006-01-17 02:40:55 -080011876 ret = tg3_nvram_lock(tp);
11877 if (ret)
11878 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011879
Michael Chane6af3012005-04-21 17:12:05 -070011880 tg3_enable_nvram_access(tp);
11881 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011882 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011883 tw32(NVRAM_WRITE1, 0x406);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011884
11885 grc_mode = tr32(GRC_MODE);
11886 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
11887
11888 if ((tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) ||
11889 !(tp->tg3_flags2 & TG3_FLG2_FLASH)) {
11890
11891 ret = tg3_nvram_write_block_buffered(tp, offset, len,
11892 buf);
Matt Carlson859a588792010-04-05 10:19:28 +000011893 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011894 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
11895 buf);
11896 }
11897
11898 grc_mode = tr32(GRC_MODE);
11899 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
11900
Michael Chane6af3012005-04-21 17:12:05 -070011901 tg3_disable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011902 tg3_nvram_unlock(tp);
11903 }
11904
11905 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT) {
Michael Chan314fba32005-04-21 17:07:04 -070011906 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011907 udelay(40);
11908 }
11909
11910 return ret;
11911}
11912
11913struct subsys_tbl_ent {
11914 u16 subsys_vendor, subsys_devid;
11915 u32 phy_id;
11916};
11917
Matt Carlson24daf2b2010-02-17 15:17:02 +000011918static struct subsys_tbl_ent subsys_id_to_phy_id[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011919 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000011920 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011921 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011922 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011923 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011924 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011925 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011926 { TG3PCI_SUBVENDOR_ID_BROADCOM,
11927 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
11928 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011929 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011930 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011931 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011932 { TG3PCI_SUBVENDOR_ID_BROADCOM,
11933 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
11934 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011935 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011936 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011937 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011938 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011939 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011940 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011941 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070011942
11943 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000011944 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011945 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011946 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011947 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011948 { TG3PCI_SUBVENDOR_ID_3COM,
11949 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
11950 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011951 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011952 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011953 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070011954
11955 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000011956 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000011957 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011958 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000011959 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011960 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000011961 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011962 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000011963 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070011964
11965 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000011966 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000011967 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011968 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000011969 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011970 { TG3PCI_SUBVENDOR_ID_COMPAQ,
11971 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
11972 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000011973 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011974 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000011975 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070011976
11977 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000011978 { TG3PCI_SUBVENDOR_ID_IBM,
11979 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011980};
11981
Matt Carlson24daf2b2010-02-17 15:17:02 +000011982static struct subsys_tbl_ent * __devinit tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011983{
11984 int i;
11985
11986 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
11987 if ((subsys_id_to_phy_id[i].subsys_vendor ==
11988 tp->pdev->subsystem_vendor) &&
11989 (subsys_id_to_phy_id[i].subsys_devid ==
11990 tp->pdev->subsystem_device))
11991 return &subsys_id_to_phy_id[i];
11992 }
11993 return NULL;
11994}
11995
Michael Chan7d0c41e2005-04-21 17:06:20 -070011996static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011997{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011998 u32 val;
Michael Chancaf636c72006-03-22 01:05:31 -080011999 u16 pmcsr;
12000
12001 /* On some early chips the SRAM cannot be accessed in D3hot state,
12002 * so need make sure we're in D0.
12003 */
12004 pci_read_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, &pmcsr);
12005 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
12006 pci_write_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, pmcsr);
12007 msleep(1);
Michael Chan7d0c41e2005-04-21 17:06:20 -070012008
12009 /* Make sure register accesses (indirect or otherwise)
12010 * will function correctly.
12011 */
12012 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
12013 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012014
David S. Millerf49639e2006-06-09 11:58:36 -070012015 /* The memory arbiter has to be enabled in order for SRAM accesses
12016 * to succeed. Normally on powerup the tg3 chip firmware will make
12017 * sure it is enabled, but other entities such as system netboot
12018 * code might disable it.
12019 */
12020 val = tr32(MEMARB_MODE);
12021 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
12022
Matt Carlson79eb6902010-02-17 15:17:03 +000012023 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012024 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12025
Gary Zambranoa85feb82007-05-05 11:52:19 -070012026 /* Assume an onboard device and WOL capable by default. */
12027 tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT | TG3_FLAG_WOL_CAP;
David S. Miller72b845e2006-03-14 14:11:48 -080012028
Michael Chanb5d37722006-09-27 16:06:21 -070012029 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080012030 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Michael Chanb5d37722006-09-27 16:06:21 -070012031 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080012032 tp->tg3_flags2 |= TG3_FLG2_IS_NIC;
12033 }
Matt Carlson0527ba32007-10-10 18:03:30 -070012034 val = tr32(VCPU_CFGSHDW);
12035 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Matt Carlson8ed5d972007-05-07 00:25:49 -070012036 tp->tg3_flags |= TG3_FLAG_ASPM_WORKAROUND;
Matt Carlson0527ba32007-10-10 18:03:30 -070012037 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Matt Carlson20232762008-12-21 20:18:56 -080012038 (val & VCPU_CFGSHDW_WOL_MAGPKT))
Matt Carlson0527ba32007-10-10 18:03:30 -070012039 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012040 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070012041 }
12042
Linus Torvalds1da177e2005-04-16 15:20:36 -070012043 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
12044 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
12045 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070012046 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012047 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012048
12049 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
12050 tp->nic_sram_data_cfg = nic_cfg;
12051
12052 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
12053 ver >>= NIC_SRAM_DATA_VER_SHIFT;
12054 if ((GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700) &&
12055 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) &&
12056 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703) &&
12057 (ver > 0) && (ver < 0x100))
12058 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
12059
Matt Carlsona9daf362008-05-25 23:49:44 -070012060 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
12061 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
12062
Linus Torvalds1da177e2005-04-16 15:20:36 -070012063 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
12064 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
12065 eeprom_phy_serdes = 1;
12066
12067 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
12068 if (nic_phy_id != 0) {
12069 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
12070 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
12071
12072 eeprom_phy_id = (id1 >> 16) << 10;
12073 eeprom_phy_id |= (id2 & 0xfc00) << 16;
12074 eeprom_phy_id |= (id2 & 0x03ff) << 0;
12075 } else
12076 eeprom_phy_id = 0;
12077
Michael Chan7d0c41e2005-04-21 17:06:20 -070012078 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070012079 if (eeprom_phy_serdes) {
Matt Carlsona50d0792010-06-05 17:24:37 +000012080 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Michael Chan747e8f82005-07-25 12:33:22 -070012081 tp->tg3_flags2 |= TG3_FLG2_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000012082 else
12083 tp->tg3_flags2 |= TG3_FLG2_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070012084 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070012085
John W. Linvillecbf46852005-04-21 17:01:29 -070012086 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012087 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
12088 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070012089 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070012090 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
12091
12092 switch (led_cfg) {
12093 default:
12094 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
12095 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12096 break;
12097
12098 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
12099 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12100 break;
12101
12102 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
12103 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070012104
12105 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
12106 * read on some older 5700/5701 bootcode.
12107 */
12108 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
12109 ASIC_REV_5700 ||
12110 GET_ASIC_REV(tp->pci_chip_rev_id) ==
12111 ASIC_REV_5701)
12112 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12113
Linus Torvalds1da177e2005-04-16 15:20:36 -070012114 break;
12115
12116 case SHASTA_EXT_LED_SHARED:
12117 tp->led_ctrl = LED_CTRL_MODE_SHARED;
12118 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
12119 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
12120 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12121 LED_CTRL_MODE_PHY_2);
12122 break;
12123
12124 case SHASTA_EXT_LED_MAC:
12125 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
12126 break;
12127
12128 case SHASTA_EXT_LED_COMBO:
12129 tp->led_ctrl = LED_CTRL_MODE_COMBO;
12130 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
12131 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12132 LED_CTRL_MODE_PHY_2);
12133 break;
12134
Stephen Hemminger855e1112008-04-16 16:37:28 -070012135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012136
12137 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
12138 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
12139 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
12140 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12141
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012142 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
12143 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080012144
Michael Chan9d26e212006-12-07 00:21:14 -080012145 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012146 tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080012147 if ((tp->pdev->subsystem_vendor ==
12148 PCI_VENDOR_ID_ARIMA) &&
12149 (tp->pdev->subsystem_device == 0x205a ||
12150 tp->pdev->subsystem_device == 0x2063))
12151 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
12152 } else {
David S. Millerf49639e2006-06-09 11:58:36 -070012153 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080012154 tp->tg3_flags2 |= TG3_FLG2_IS_NIC;
12155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012156
12157 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
12158 tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
John W. Linvillecbf46852005-04-21 17:01:29 -070012159 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012160 tp->tg3_flags2 |= TG3_FLG2_ASF_NEW_HANDSHAKE;
12161 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012162
12163 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
12164 (tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
Matt Carlson0d3031d2007-10-10 18:02:43 -070012165 tp->tg3_flags3 |= TG3_FLG3_ENABLE_APE;
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012166
Gary Zambranoa85feb82007-05-05 11:52:19 -070012167 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES &&
12168 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
12169 tp->tg3_flags &= ~TG3_FLAG_WOL_CAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012170
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070012171 if ((tp->tg3_flags & TG3_FLAG_WOL_CAP) &&
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012172 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE))
Matt Carlson0527ba32007-10-10 18:03:30 -070012173 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
12174
Linus Torvalds1da177e2005-04-16 15:20:36 -070012175 if (cfg2 & (1 << 17))
12176 tp->tg3_flags2 |= TG3_FLG2_CAPACITIVE_COUPLING;
12177
12178 /* serdes signal pre-emphasis in register 0x590 set by */
12179 /* bootcode if bit 18 is set */
12180 if (cfg2 & (1 << 18))
12181 tp->tg3_flags2 |= TG3_FLG2_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070012182
Matt Carlson321d32a2008-11-21 17:22:19 -080012183 if (((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
12184 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080012185 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
12186 tp->tg3_flags3 |= TG3_FLG3_PHY_ENABLE_APD;
12187
Matt Carlson8ed5d972007-05-07 00:25:49 -070012188 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
12189 u32 cfg3;
12190
12191 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
12192 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
12193 tp->tg3_flags |= TG3_FLAG_ASPM_WORKAROUND;
12194 }
Matt Carlsona9daf362008-05-25 23:49:44 -070012195
Matt Carlson14417062010-02-17 15:16:59 +000012196 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
12197 tp->tg3_flags3 |= TG3_FLG3_RGMII_INBAND_DISABLE;
Matt Carlsona9daf362008-05-25 23:49:44 -070012198 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
12199 tp->tg3_flags3 |= TG3_FLG3_RGMII_EXT_IBND_RX_EN;
12200 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
12201 tp->tg3_flags3 |= TG3_FLG3_RGMII_EXT_IBND_TX_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012202 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012203done:
12204 device_init_wakeup(&tp->pdev->dev, tp->tg3_flags & TG3_FLAG_WOL_CAP);
12205 device_set_wakeup_enable(&tp->pdev->dev,
12206 tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
Michael Chan7d0c41e2005-04-21 17:06:20 -070012207}
12208
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012209static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
12210{
12211 int i;
12212 u32 val;
12213
12214 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
12215 tw32(OTP_CTRL, cmd);
12216
12217 /* Wait for up to 1 ms for command to execute. */
12218 for (i = 0; i < 100; i++) {
12219 val = tr32(OTP_STATUS);
12220 if (val & OTP_STATUS_CMD_DONE)
12221 break;
12222 udelay(10);
12223 }
12224
12225 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
12226}
12227
12228/* Read the gphy configuration from the OTP region of the chip. The gphy
12229 * configuration is a 32-bit value that straddles the alignment boundary.
12230 * We do two 32-bit reads and then shift and merge the results.
12231 */
12232static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
12233{
12234 u32 bhalf_otp, thalf_otp;
12235
12236 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
12237
12238 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
12239 return 0;
12240
12241 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
12242
12243 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12244 return 0;
12245
12246 thalf_otp = tr32(OTP_READ_DATA);
12247
12248 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
12249
12250 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12251 return 0;
12252
12253 bhalf_otp = tr32(OTP_READ_DATA);
12254
12255 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
12256}
12257
Michael Chan7d0c41e2005-04-21 17:06:20 -070012258static int __devinit tg3_phy_probe(struct tg3 *tp)
12259{
12260 u32 hw_phy_id_1, hw_phy_id_2;
12261 u32 hw_phy_id, hw_phy_id_masked;
12262 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012263
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012264 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)
12265 return tg3_phy_init(tp);
12266
Linus Torvalds1da177e2005-04-16 15:20:36 -070012267 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010012268 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012269 */
12270 err = 0;
Matt Carlson0d3031d2007-10-10 18:02:43 -070012271 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
12272 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000012273 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012274 } else {
12275 /* Now read the physical PHY_ID from the chip and verify
12276 * that it is sane. If it doesn't look good, we fall back
12277 * to either the hard-coded table based PHY_ID and failing
12278 * that the value found in the eeprom area.
12279 */
12280 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
12281 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
12282
12283 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
12284 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
12285 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
12286
Matt Carlson79eb6902010-02-17 15:17:03 +000012287 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012288 }
12289
Matt Carlson79eb6902010-02-17 15:17:03 +000012290 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012291 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000012292 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012293 tp->tg3_flags2 |= TG3_FLG2_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070012294 else
12295 tp->tg3_flags2 &= ~TG3_FLG2_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012296 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000012297 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070012298 /* Do nothing, phy ID already set up in
12299 * tg3_get_eeprom_hw_cfg().
12300 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070012301 } else {
12302 struct subsys_tbl_ent *p;
12303
12304 /* No eeprom signature? Try the hardcoded
12305 * subsys device table.
12306 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012307 p = tg3_lookup_by_subsys(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012308 if (!p)
12309 return -ENODEV;
12310
12311 tp->phy_id = p->phy_id;
12312 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000012313 tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012314 tp->tg3_flags2 |= TG3_FLG2_PHY_SERDES;
12315 }
12316 }
12317
Michael Chan747e8f82005-07-25 12:33:22 -070012318 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES) &&
Matt Carlson0d3031d2007-10-10 18:02:43 -070012319 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070012320 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chan3600d912006-12-07 00:21:48 -080012321 u32 bmsr, adv_reg, tg3_ctrl, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012322
12323 tg3_readphy(tp, MII_BMSR, &bmsr);
12324 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
12325 (bmsr & BMSR_LSTATUS))
12326 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012327
Linus Torvalds1da177e2005-04-16 15:20:36 -070012328 err = tg3_phy_reset(tp);
12329 if (err)
12330 return err;
12331
12332 adv_reg = (ADVERTISE_10HALF | ADVERTISE_10FULL |
12333 ADVERTISE_100HALF | ADVERTISE_100FULL |
12334 ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
12335 tg3_ctrl = 0;
12336 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY)) {
12337 tg3_ctrl = (MII_TG3_CTRL_ADV_1000_HALF |
12338 MII_TG3_CTRL_ADV_1000_FULL);
12339 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
12340 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
12341 tg3_ctrl |= (MII_TG3_CTRL_AS_MASTER |
12342 MII_TG3_CTRL_ENABLE_AS_MASTER);
12343 }
12344
Michael Chan3600d912006-12-07 00:21:48 -080012345 mask = (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
12346 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
12347 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full);
12348 if (!tg3_copper_is_advertising_all(tp, mask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012349 tg3_writephy(tp, MII_ADVERTISE, adv_reg);
12350
12351 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY))
12352 tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
12353
12354 tg3_writephy(tp, MII_BMCR,
12355 BMCR_ANENABLE | BMCR_ANRESTART);
12356 }
12357 tg3_phy_set_wirespeed(tp);
12358
12359 tg3_writephy(tp, MII_ADVERTISE, adv_reg);
12360 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY))
12361 tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
12362 }
12363
12364skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000012365 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012366 err = tg3_init_5401phy_dsp(tp);
12367 if (err)
12368 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012369
Linus Torvalds1da177e2005-04-16 15:20:36 -070012370 err = tg3_init_5401phy_dsp(tp);
12371 }
12372
Michael Chan747e8f82005-07-25 12:33:22 -070012373 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012374 tp->link_config.advertising =
12375 (ADVERTISED_1000baseT_Half |
12376 ADVERTISED_1000baseT_Full |
12377 ADVERTISED_Autoneg |
12378 ADVERTISED_FIBRE);
12379 if (tp->tg3_flags & TG3_FLAG_10_100_ONLY)
12380 tp->link_config.advertising &=
12381 ~(ADVERTISED_1000baseT_Half |
12382 ADVERTISED_1000baseT_Full);
12383
12384 return err;
12385}
12386
Matt Carlson184b8902010-04-05 10:19:25 +000012387static void __devinit tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012388{
Matt Carlson184b8902010-04-05 10:19:25 +000012389 u8 vpd_data[TG3_NVM_VPD_LEN];
Matt Carlson4181b2c2010-02-26 14:04:45 +000012390 unsigned int block_end, rosize, len;
Matt Carlson184b8902010-04-05 10:19:25 +000012391 int j, i = 0;
Michael Chan1b277772006-03-20 22:27:48 -080012392 u32 magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012393
Matt Carlsondf259d82009-04-20 06:57:14 +000012394 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
12395 tg3_nvram_read(tp, 0x0, &magic))
David S. Millerf49639e2006-06-09 11:58:36 -070012396 goto out_not_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012397
Michael Chan18201802006-03-20 22:29:15 -080012398 if (magic == TG3_EEPROM_MAGIC) {
Matt Carlson141518c2009-12-03 08:36:22 +000012399 for (i = 0; i < TG3_NVM_VPD_LEN; i += 4) {
Michael Chan1b277772006-03-20 22:27:48 -080012400 u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012401
Matt Carlson6d348f22009-02-25 14:25:52 +000012402 /* The data is in little-endian format in NVRAM.
12403 * Use the big-endian read routines to preserve
12404 * the byte order as it exists in NVRAM.
12405 */
Matt Carlson141518c2009-12-03 08:36:22 +000012406 if (tg3_nvram_read_be32(tp, TG3_NVM_VPD_OFF + i, &tmp))
Michael Chan1b277772006-03-20 22:27:48 -080012407 goto out_not_found;
12408
Matt Carlson6d348f22009-02-25 14:25:52 +000012409 memcpy(&vpd_data[i], &tmp, sizeof(tmp));
Michael Chan1b277772006-03-20 22:27:48 -080012410 }
12411 } else {
Matt Carlson94c982b2009-12-03 08:36:23 +000012412 ssize_t cnt;
Matt Carlson4181b2c2010-02-26 14:04:45 +000012413 unsigned int pos = 0;
Michael Chan1b277772006-03-20 22:27:48 -080012414
Matt Carlson94c982b2009-12-03 08:36:23 +000012415 for (; pos < TG3_NVM_VPD_LEN && i < 3; i++, pos += cnt) {
12416 cnt = pci_read_vpd(tp->pdev, pos,
12417 TG3_NVM_VPD_LEN - pos,
12418 &vpd_data[pos]);
12419 if (cnt == -ETIMEDOUT || -EINTR)
12420 cnt = 0;
12421 else if (cnt < 0)
David S. Millerf49639e2006-06-09 11:58:36 -070012422 goto out_not_found;
Michael Chan1b277772006-03-20 22:27:48 -080012423 }
Matt Carlson94c982b2009-12-03 08:36:23 +000012424 if (pos != TG3_NVM_VPD_LEN)
12425 goto out_not_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012426 }
12427
Matt Carlson4181b2c2010-02-26 14:04:45 +000012428 i = pci_vpd_find_tag(vpd_data, 0, TG3_NVM_VPD_LEN,
12429 PCI_VPD_LRDT_RO_DATA);
12430 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012431 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000012432
12433 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
12434 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
12435 i += PCI_VPD_LRDT_TAG_SIZE;
12436
12437 if (block_end > TG3_NVM_VPD_LEN)
12438 goto out_not_found;
12439
Matt Carlson184b8902010-04-05 10:19:25 +000012440 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
12441 PCI_VPD_RO_KEYWORD_MFR_ID);
12442 if (j > 0) {
12443 len = pci_vpd_info_field_size(&vpd_data[j]);
12444
12445 j += PCI_VPD_INFO_FLD_HDR_SIZE;
12446 if (j + len > block_end || len != 4 ||
12447 memcmp(&vpd_data[j], "1028", 4))
12448 goto partno;
12449
12450 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
12451 PCI_VPD_RO_KEYWORD_VENDOR0);
12452 if (j < 0)
12453 goto partno;
12454
12455 len = pci_vpd_info_field_size(&vpd_data[j]);
12456
12457 j += PCI_VPD_INFO_FLD_HDR_SIZE;
12458 if (j + len > block_end)
12459 goto partno;
12460
12461 memcpy(tp->fw_ver, &vpd_data[j], len);
12462 strncat(tp->fw_ver, " bc ", TG3_NVM_VPD_LEN - len - 1);
12463 }
12464
12465partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000012466 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
12467 PCI_VPD_RO_KEYWORD_PARTNO);
12468 if (i < 0)
12469 goto out_not_found;
12470
12471 len = pci_vpd_info_field_size(&vpd_data[i]);
12472
12473 i += PCI_VPD_INFO_FLD_HDR_SIZE;
12474 if (len > TG3_BPN_SIZE ||
12475 (len + i) > TG3_NVM_VPD_LEN)
12476 goto out_not_found;
12477
12478 memcpy(tp->board_part_number, &vpd_data[i], len);
12479
12480 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012481
12482out_not_found:
Michael Chanb5d37722006-09-27 16:06:21 -070012483 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12484 strcpy(tp->board_part_number, "BCM95906");
Matt Carlsondf259d82009-04-20 06:57:14 +000012485 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12486 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
12487 strcpy(tp->board_part_number, "BCM57780");
12488 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12489 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
12490 strcpy(tp->board_part_number, "BCM57760");
12491 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12492 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
12493 strcpy(tp->board_part_number, "BCM57790");
Matt Carlson5e7ccf22009-08-25 10:08:42 +000012494 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12495 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
12496 strcpy(tp->board_part_number, "BCM57788");
Matt Carlsonb474eca2010-02-17 15:16:58 +000012497 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12498 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
12499 strcpy(tp->board_part_number, "BCM57761");
12500 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12501 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
Matt Carlsonb703df62009-12-03 08:36:21 +000012502 strcpy(tp->board_part_number, "BCM57765");
Matt Carlsonb474eca2010-02-17 15:16:58 +000012503 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12504 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
12505 strcpy(tp->board_part_number, "BCM57781");
12506 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12507 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
12508 strcpy(tp->board_part_number, "BCM57785");
12509 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12510 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
12511 strcpy(tp->board_part_number, "BCM57791");
12512 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12513 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
12514 strcpy(tp->board_part_number, "BCM57795");
Michael Chanb5d37722006-09-27 16:06:21 -070012515 else
12516 strcpy(tp->board_part_number, "none");
Linus Torvalds1da177e2005-04-16 15:20:36 -070012517}
12518
Matt Carlson9c8a6202007-10-21 16:16:08 -070012519static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
12520{
12521 u32 val;
12522
Matt Carlsone4f34112009-02-25 14:25:00 +000012523 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070012524 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000012525 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070012526 val != 0)
12527 return 0;
12528
12529 return 1;
12530}
12531
Matt Carlsonacd9c112009-02-25 14:26:33 +000012532static void __devinit tg3_read_bc_ver(struct tg3 *tp)
12533{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012534 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000012535 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012536 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000012537
12538 if (tg3_nvram_read(tp, 0xc, &offset) ||
12539 tg3_nvram_read(tp, 0x4, &start))
12540 return;
12541
12542 offset = tg3_nvram_logical_addr(tp, offset);
12543
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012544 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000012545 return;
12546
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012547 if ((val & 0xfc000000) == 0x0c000000) {
12548 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000012549 return;
12550
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012551 if (val == 0)
12552 newver = true;
12553 }
12554
Matt Carlson75f99362010-04-05 10:19:24 +000012555 dst_off = strlen(tp->fw_ver);
12556
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012557 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000012558 if (TG3_VER_SIZE - dst_off < 16 ||
12559 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012560 return;
12561
12562 offset = offset + ver_offset - start;
12563 for (i = 0; i < 16; i += 4) {
12564 __be32 v;
12565 if (tg3_nvram_read_be32(tp, offset + i, &v))
12566 return;
12567
Matt Carlson75f99362010-04-05 10:19:24 +000012568 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012569 }
12570 } else {
12571 u32 major, minor;
12572
12573 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
12574 return;
12575
12576 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
12577 TG3_NVM_BCVER_MAJSFT;
12578 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000012579 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
12580 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000012581 }
12582}
12583
Matt Carlsona6f6cb12009-02-25 14:27:43 +000012584static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
12585{
12586 u32 val, major, minor;
12587
12588 /* Use native endian representation */
12589 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
12590 return;
12591
12592 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
12593 TG3_NVM_HWSB_CFG1_MAJSFT;
12594 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
12595 TG3_NVM_HWSB_CFG1_MINSFT;
12596
12597 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
12598}
12599
Matt Carlsondfe00d72008-11-21 17:19:41 -080012600static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
12601{
12602 u32 offset, major, minor, build;
12603
Matt Carlson75f99362010-04-05 10:19:24 +000012604 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080012605
12606 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
12607 return;
12608
12609 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
12610 case TG3_EEPROM_SB_REVISION_0:
12611 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
12612 break;
12613 case TG3_EEPROM_SB_REVISION_2:
12614 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
12615 break;
12616 case TG3_EEPROM_SB_REVISION_3:
12617 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
12618 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000012619 case TG3_EEPROM_SB_REVISION_4:
12620 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
12621 break;
12622 case TG3_EEPROM_SB_REVISION_5:
12623 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
12624 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080012625 default:
12626 return;
12627 }
12628
Matt Carlsone4f34112009-02-25 14:25:00 +000012629 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080012630 return;
12631
12632 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
12633 TG3_EEPROM_SB_EDH_BLD_SHFT;
12634 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
12635 TG3_EEPROM_SB_EDH_MAJ_SHFT;
12636 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
12637
12638 if (minor > 99 || build > 26)
12639 return;
12640
Matt Carlson75f99362010-04-05 10:19:24 +000012641 offset = strlen(tp->fw_ver);
12642 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
12643 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080012644
12645 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000012646 offset = strlen(tp->fw_ver);
12647 if (offset < TG3_VER_SIZE - 1)
12648 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080012649 }
12650}
12651
Matt Carlsonacd9c112009-02-25 14:26:33 +000012652static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080012653{
12654 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000012655 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070012656
12657 for (offset = TG3_NVM_DIR_START;
12658 offset < TG3_NVM_DIR_END;
12659 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000012660 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012661 return;
12662
12663 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
12664 break;
12665 }
12666
12667 if (offset == TG3_NVM_DIR_END)
12668 return;
12669
12670 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
12671 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000012672 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012673 return;
12674
Matt Carlsone4f34112009-02-25 14:25:00 +000012675 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070012676 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000012677 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012678 return;
12679
12680 offset += val - start;
12681
Matt Carlsonacd9c112009-02-25 14:26:33 +000012682 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012683
Matt Carlsonacd9c112009-02-25 14:26:33 +000012684 tp->fw_ver[vlen++] = ',';
12685 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070012686
12687 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012688 __be32 v;
12689 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012690 return;
12691
Al Virob9fc7dc2007-12-17 22:59:57 -080012692 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012693
Matt Carlsonacd9c112009-02-25 14:26:33 +000012694 if (vlen > TG3_VER_SIZE - sizeof(v)) {
12695 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012696 break;
12697 }
12698
Matt Carlsonacd9c112009-02-25 14:26:33 +000012699 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
12700 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012701 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000012702}
12703
Matt Carlson7fd76442009-02-25 14:27:20 +000012704static void __devinit tg3_read_dash_ver(struct tg3 *tp)
12705{
12706 int vlen;
12707 u32 apedata;
12708
12709 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) ||
12710 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
12711 return;
12712
12713 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
12714 if (apedata != APE_SEG_SIG_MAGIC)
12715 return;
12716
12717 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
12718 if (!(apedata & APE_FW_STATUS_READY))
12719 return;
12720
12721 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
12722
12723 vlen = strlen(tp->fw_ver);
12724
12725 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " DASH v%d.%d.%d.%d",
12726 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
12727 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
12728 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
12729 (apedata & APE_FW_VERSION_BLDMSK));
12730}
12731
Matt Carlsonacd9c112009-02-25 14:26:33 +000012732static void __devinit tg3_read_fw_ver(struct tg3 *tp)
12733{
12734 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000012735 bool vpd_vers = false;
12736
12737 if (tp->fw_ver[0] != 0)
12738 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000012739
Matt Carlsondf259d82009-04-20 06:57:14 +000012740 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) {
Matt Carlson75f99362010-04-05 10:19:24 +000012741 strcat(tp->fw_ver, "sb");
Matt Carlsondf259d82009-04-20 06:57:14 +000012742 return;
12743 }
12744
Matt Carlsonacd9c112009-02-25 14:26:33 +000012745 if (tg3_nvram_read(tp, 0, &val))
12746 return;
12747
12748 if (val == TG3_EEPROM_MAGIC)
12749 tg3_read_bc_ver(tp);
12750 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
12751 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000012752 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
12753 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000012754 else
12755 return;
12756
12757 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
Matt Carlson75f99362010-04-05 10:19:24 +000012758 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) || vpd_vers)
12759 goto done;
Matt Carlsonacd9c112009-02-25 14:26:33 +000012760
12761 tg3_read_mgmtfw_ver(tp);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012762
Matt Carlson75f99362010-04-05 10:19:24 +000012763done:
Matt Carlson9c8a6202007-10-21 16:16:08 -070012764 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080012765}
12766
Michael Chan7544b092007-05-05 13:08:32 -070012767static struct pci_dev * __devinit tg3_find_peer(struct tg3 *);
12768
Eric Dumazet7fe876a2010-07-08 06:14:55 +000012769static void inline vlan_features_add(struct net_device *dev, unsigned long flags)
12770{
12771#if TG3_VLAN_TAG_USED
12772 dev->vlan_features |= flags;
12773#endif
12774}
12775
Linus Torvalds1da177e2005-04-16 15:20:36 -070012776static int __devinit tg3_get_invariants(struct tg3 *tp)
12777{
12778 static struct pci_device_id write_reorder_chipsets[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012779 { PCI_DEVICE(PCI_VENDOR_ID_AMD,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012780 PCI_DEVICE_ID_AMD_FE_GATE_700C) },
John W. Linvillec165b002006-07-08 13:28:53 -070012781 { PCI_DEVICE(PCI_VENDOR_ID_AMD,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012782 PCI_DEVICE_ID_AMD_8131_BRIDGE) },
Michael Chan399de502005-10-03 14:02:39 -070012783 { PCI_DEVICE(PCI_VENDOR_ID_VIA,
12784 PCI_DEVICE_ID_VIA_8385_0) },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012785 { },
12786 };
12787 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012788 u32 pci_state_reg, grc_misc_cfg;
12789 u32 val;
12790 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080012791 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012792
Linus Torvalds1da177e2005-04-16 15:20:36 -070012793 /* Force memory write invalidate off. If we leave it on,
12794 * then on 5700_BX chips we have to enable a workaround.
12795 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
12796 * to match the cacheline size. The Broadcom driver have this
12797 * workaround but turns MWI off all the times so never uses
12798 * it. This seems to suggest that the workaround is insufficient.
12799 */
12800 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
12801 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
12802 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
12803
12804 /* It is absolutely critical that TG3PCI_MISC_HOST_CTRL
12805 * has the register indirect write enable bit set before
12806 * we try to access any of the MMIO registers. It is also
12807 * critical that the PCI-X hw workaround situation is decided
12808 * before that as well.
12809 */
12810 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
12811 &misc_ctrl_reg);
12812
12813 tp->pci_chip_rev_id = (misc_ctrl_reg >>
12814 MISC_HOST_CTRL_CHIPREV_SHIFT);
Matt Carlson795d01c2007-10-07 23:28:17 -070012815 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
12816 u32 prod_id_asic_rev;
12817
Matt Carlson5001e2f2009-11-13 13:03:51 +000012818 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
12819 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
Matt Carlsona50d0792010-06-05 17:24:37 +000012820 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5724 ||
12821 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012822 pci_read_config_dword(tp->pdev,
12823 TG3PCI_GEN2_PRODID_ASICREV,
12824 &prod_id_asic_rev);
Matt Carlsonb703df62009-12-03 08:36:21 +000012825 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
12826 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
12827 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
12828 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
12829 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
12830 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
12831 pci_read_config_dword(tp->pdev,
12832 TG3PCI_GEN15_PRODID_ASICREV,
12833 &prod_id_asic_rev);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012834 else
12835 pci_read_config_dword(tp->pdev, TG3PCI_PRODID_ASICREV,
12836 &prod_id_asic_rev);
12837
Matt Carlson321d32a2008-11-21 17:22:19 -080012838 tp->pci_chip_rev_id = prod_id_asic_rev;
Matt Carlson795d01c2007-10-07 23:28:17 -070012839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012840
Michael Chanff645be2005-04-21 17:09:53 -070012841 /* Wrong chip ID in 5752 A0. This code can be removed later
12842 * as A0 is not in production.
12843 */
12844 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
12845 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
12846
Michael Chan68929142005-08-09 20:17:14 -070012847 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
12848 * we need to disable memory and use config. cycles
12849 * only to access all registers. The 5702/03 chips
12850 * can mistakenly decode the special cycles from the
12851 * ICH chipsets as memory write cycles, causing corruption
12852 * of register and memory space. Only certain ICH bridges
12853 * will drive special cycles with non-zero data during the
12854 * address phase which can fall within the 5703's address
12855 * range. This is not an ICH bug as the PCI spec allows
12856 * non-zero address during special cycles. However, only
12857 * these ICH bridges are known to drive non-zero addresses
12858 * during special cycles.
12859 *
12860 * Since special cycles do not cross PCI bridges, we only
12861 * enable this workaround if the 5703 is on the secondary
12862 * bus of these ICH bridges.
12863 */
12864 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
12865 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
12866 static struct tg3_dev_id {
12867 u32 vendor;
12868 u32 device;
12869 u32 rev;
12870 } ich_chipsets[] = {
12871 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
12872 PCI_ANY_ID },
12873 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
12874 PCI_ANY_ID },
12875 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
12876 0xa },
12877 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
12878 PCI_ANY_ID },
12879 { },
12880 };
12881 struct tg3_dev_id *pci_id = &ich_chipsets[0];
12882 struct pci_dev *bridge = NULL;
12883
12884 while (pci_id->vendor != 0) {
12885 bridge = pci_get_device(pci_id->vendor, pci_id->device,
12886 bridge);
12887 if (!bridge) {
12888 pci_id++;
12889 continue;
12890 }
12891 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070012892 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070012893 continue;
12894 }
12895 if (bridge->subordinate &&
12896 (bridge->subordinate->number ==
12897 tp->pdev->bus->number)) {
12898
12899 tp->tg3_flags2 |= TG3_FLG2_ICH_WORKAROUND;
12900 pci_dev_put(bridge);
12901 break;
12902 }
12903 }
12904 }
12905
Matt Carlson41588ba2008-04-19 18:12:33 -070012906 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
12907 static struct tg3_dev_id {
12908 u32 vendor;
12909 u32 device;
12910 } bridge_chipsets[] = {
12911 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
12912 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
12913 { },
12914 };
12915 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
12916 struct pci_dev *bridge = NULL;
12917
12918 while (pci_id->vendor != 0) {
12919 bridge = pci_get_device(pci_id->vendor,
12920 pci_id->device,
12921 bridge);
12922 if (!bridge) {
12923 pci_id++;
12924 continue;
12925 }
12926 if (bridge->subordinate &&
12927 (bridge->subordinate->number <=
12928 tp->pdev->bus->number) &&
12929 (bridge->subordinate->subordinate >=
12930 tp->pdev->bus->number)) {
12931 tp->tg3_flags3 |= TG3_FLG3_5701_DMA_BUG;
12932 pci_dev_put(bridge);
12933 break;
12934 }
12935 }
12936 }
12937
Michael Chan4a29cc22006-03-19 13:21:12 -080012938 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
12939 * DMA addresses > 40-bit. This bridge may have other additional
12940 * 57xx devices behind it in some 4-port NIC designs for example.
12941 * Any tg3 device found behind the bridge will also need the 40-bit
12942 * DMA workaround.
12943 */
Michael Chana4e2b342005-10-26 15:46:52 -070012944 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
12945 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
12946 tp->tg3_flags2 |= TG3_FLG2_5780_CLASS;
Michael Chan4a29cc22006-03-19 13:21:12 -080012947 tp->tg3_flags |= TG3_FLAG_40BIT_DMA_BUG;
Michael Chan4cf78e42005-07-25 12:29:19 -070012948 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a588792010-04-05 10:19:28 +000012949 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080012950 struct pci_dev *bridge = NULL;
12951
12952 do {
12953 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
12954 PCI_DEVICE_ID_SERVERWORKS_EPB,
12955 bridge);
12956 if (bridge && bridge->subordinate &&
12957 (bridge->subordinate->number <=
12958 tp->pdev->bus->number) &&
12959 (bridge->subordinate->subordinate >=
12960 tp->pdev->bus->number)) {
12961 tp->tg3_flags |= TG3_FLAG_40BIT_DMA_BUG;
12962 pci_dev_put(bridge);
12963 break;
12964 }
12965 } while (bridge);
12966 }
Michael Chan4cf78e42005-07-25 12:29:19 -070012967
Linus Torvalds1da177e2005-04-16 15:20:36 -070012968 /* Initialize misc host control in PCI block. */
12969 tp->misc_host_ctrl |= (misc_ctrl_reg &
12970 MISC_HOST_CTRL_CHIPREV);
12971 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
12972 tp->misc_host_ctrl);
12973
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012974 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
12975 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
12976 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Michael Chan7544b092007-05-05 13:08:32 -070012977 tp->pdev_peer = tg3_find_peer(tp);
12978
Matt Carlsonc885e822010-08-02 11:25:57 +000012979 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
12980 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
12981 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
12982 tp->tg3_flags3 |= TG3_FLG3_5717_PLUS;
12983
Matt Carlson321d32a2008-11-21 17:22:19 -080012984 /* Intentionally exclude ASIC_REV_5906 */
12985 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Michael Chand9ab5ad2006-03-20 22:27:35 -080012986 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070012987 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070012988 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070012989 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012990 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlsonc885e822010-08-02 11:25:57 +000012991 (tp->tg3_flags3 & TG3_FLG3_5717_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080012992 tp->tg3_flags3 |= TG3_FLG3_5755_PLUS;
12993
12994 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
12995 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Michael Chanb5d37722006-09-27 16:06:21 -070012996 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080012997 (tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
Michael Chana4e2b342005-10-26 15:46:52 -070012998 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
John W. Linville6708e5c2005-04-21 17:00:52 -070012999 tp->tg3_flags2 |= TG3_FLG2_5750_PLUS;
13000
John W. Linville1b440c562005-04-21 17:03:18 -070013001 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) ||
13002 (tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
13003 tp->tg3_flags2 |= TG3_FLG2_5705_PLUS;
13004
Matt Carlson027455a2008-12-21 20:19:30 -080013005 /* 5700 B0 chips do not support checksumming correctly due
13006 * to hardware bugs.
13007 */
13008 if (tp->pci_chip_rev_id == CHIPREV_ID_5700_B0)
13009 tp->tg3_flags |= TG3_FLAG_BROKEN_CHECKSUMS;
13010 else {
Eric Dumazet7fe876a2010-07-08 06:14:55 +000013011 unsigned long features = NETIF_F_IP_CSUM | NETIF_F_SG | NETIF_F_GRO;
13012
Matt Carlson027455a2008-12-21 20:19:30 -080013013 tp->tg3_flags |= TG3_FLAG_RX_CHECKSUMS;
Matt Carlson027455a2008-12-21 20:19:30 -080013014 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Eric Dumazet7fe876a2010-07-08 06:14:55 +000013015 features |= NETIF_F_IPV6_CSUM;
13016 tp->dev->features |= features;
13017 vlan_features_add(tp->dev, features);
Matt Carlson027455a2008-12-21 20:19:30 -080013018 }
13019
Matt Carlson507399f2009-11-13 13:03:37 +000013020 /* Determine TSO capabilities */
Matt Carlsonc885e822010-08-02 11:25:57 +000013021 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)
Matt Carlsone849cdc2009-11-13 13:03:38 +000013022 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_3;
13023 else if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
13024 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlson507399f2009-11-13 13:03:37 +000013025 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_2;
13026 else if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
13027 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_1 | TG3_FLG2_TSO_BUG;
13028 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
13029 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
13030 tp->tg3_flags2 &= ~TG3_FLG2_TSO_BUG;
13031 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13032 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13033 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
13034 tp->tg3_flags2 |= TG3_FLG2_TSO_BUG;
13035 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
13036 tp->fw_needed = FIRMWARE_TG3TSO5;
13037 else
13038 tp->fw_needed = FIRMWARE_TG3TSO;
13039 }
13040
13041 tp->irq_max = 1;
13042
Michael Chan5a6f3072006-03-20 22:28:05 -080013043 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
Michael Chan7544b092007-05-05 13:08:32 -070013044 tp->tg3_flags |= TG3_FLAG_SUPPORT_MSI;
13045 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
13046 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
13047 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
13048 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
13049 tp->pdev_peer == tp->pdev))
13050 tp->tg3_flags &= ~TG3_FLAG_SUPPORT_MSI;
13051
Matt Carlson321d32a2008-11-21 17:22:19 -080013052 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070013053 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanfcfa0a32006-03-20 22:28:41 -080013054 tp->tg3_flags2 |= TG3_FLG2_1SHOT_MSI;
Michael Chan52c0fd82006-06-29 20:15:54 -070013055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013056
Matt Carlsonc885e822010-08-02 11:25:57 +000013057 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
Matt Carlson507399f2009-11-13 13:03:37 +000013058 tp->tg3_flags |= TG3_FLAG_SUPPORT_MSIX;
13059 tp->irq_max = TG3_IRQ_MAX_VECS;
13060 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013061 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000013062
Matt Carlson615774f2009-11-13 13:03:39 +000013063 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsona50d0792010-06-05 17:24:37 +000013064 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlson615774f2009-11-13 13:03:39 +000013065 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
13066 tp->tg3_flags3 |= TG3_FLG3_SHORT_DMA_BUG;
13067 else if (!(tp->tg3_flags3 & TG3_FLG3_5755_PLUS)) {
13068 tp->tg3_flags3 |= TG3_FLG3_4G_DMA_BNDRY_BUG;
13069 tp->tg3_flags3 |= TG3_FLG3_40BIT_DMA_LIMIT_BUG;
Matt Carlson0e1406d2009-11-02 12:33:33 +000013070 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013071
Matt Carlsonc885e822010-08-02 11:25:57 +000013072 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)
Matt Carlsonb703df62009-12-03 08:36:21 +000013073 tp->tg3_flags3 |= TG3_FLG3_USE_JUMBO_BDFLAG;
13074
Matt Carlsonf51f3562008-05-25 23:45:08 -070013075 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
Matt Carlsonc6cdf432010-04-05 10:19:26 +000013076 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
13077 (tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG))
Matt Carlson8f666b02009-08-28 13:58:24 +000013078 tp->tg3_flags |= TG3_FLAG_JUMBO_CAPABLE;
Michael Chan0f893dc2005-07-25 12:30:38 -070013079
Matt Carlson52f44902008-11-21 17:17:04 -080013080 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
13081 &pci_state_reg);
13082
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013083 tp->pcie_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_EXP);
13084 if (tp->pcie_cap != 0) {
13085 u16 lnkctl;
13086
Linus Torvalds1da177e2005-04-16 15:20:36 -070013087 tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS;
Matt Carlson5f5c51e2007-11-12 21:19:37 -080013088
13089 pcie_set_readrq(tp->pdev, 4096);
13090
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013091 pci_read_config_word(tp->pdev,
13092 tp->pcie_cap + PCI_EXP_LNKCTL,
13093 &lnkctl);
13094 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
13095 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanc7835a72006-11-15 21:14:42 -080013096 tp->tg3_flags2 &= ~TG3_FLG2_HW_TSO_2;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013097 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013098 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000013099 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
13100 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013101 tp->tg3_flags3 |= TG3_FLG3_CLKREQ_BUG;
Matt Carlson614b0592010-01-20 16:58:02 +000013102 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
13103 tp->tg3_flags3 |= TG3_FLG3_L1PLLPD_EN;
Michael Chanc7835a72006-11-15 21:14:42 -080013104 }
Matt Carlson52f44902008-11-21 17:17:04 -080013105 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Matt Carlsonfcb389d2008-11-03 16:55:44 -080013106 tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS;
Matt Carlson52f44902008-11-21 17:17:04 -080013107 } else if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
13108 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
13109 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
13110 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000013111 dev_err(&tp->pdev->dev,
13112 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080013113 return -EIO;
13114 }
13115
13116 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
13117 tp->tg3_flags |= TG3_FLAG_PCIX_MODE;
13118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013119
Michael Chan399de502005-10-03 14:02:39 -070013120 /* If we have an AMD 762 or VIA K8T800 chipset, write
13121 * reordering to the mailbox registers done by the host
13122 * controller can cause major troubles. We read back from
13123 * every mailbox register write to force the writes to be
13124 * posted to the chip in order.
13125 */
13126 if (pci_dev_present(write_reorder_chipsets) &&
13127 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
13128 tp->tg3_flags |= TG3_FLAG_MBOX_WRITE_REORDER;
13129
Matt Carlson69fc4052008-12-21 20:19:57 -080013130 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
13131 &tp->pci_cacheline_sz);
13132 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13133 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013134 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
13135 tp->pci_lat_timer < 64) {
13136 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080013137 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13138 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013139 }
13140
Matt Carlson52f44902008-11-21 17:17:04 -080013141 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
13142 /* 5700 BX chips need to have their TX producer index
13143 * mailboxes written twice to workaround a bug.
13144 */
13145 tp->tg3_flags |= TG3_FLAG_TXD_MBOX_HWBUG;
Matt Carlson9974a352007-10-07 23:27:28 -070013146
Matt Carlson52f44902008-11-21 17:17:04 -080013147 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013148 *
13149 * The workaround is to use indirect register accesses
13150 * for all chip writes not to mailbox registers.
13151 */
Matt Carlson52f44902008-11-21 17:17:04 -080013152 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013153 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013154
13155 tp->tg3_flags |= TG3_FLAG_PCIX_TARGET_HWBUG;
13156
13157 /* The chip can have it's power management PCI config
13158 * space registers clobbered due to this bug.
13159 * So explicitly force the chip into D0 here.
13160 */
Matt Carlson9974a352007-10-07 23:27:28 -070013161 pci_read_config_dword(tp->pdev,
13162 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013163 &pm_reg);
13164 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
13165 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070013166 pci_write_config_dword(tp->pdev,
13167 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013168 pm_reg);
13169
13170 /* Also, force SERR#/PERR# in PCI command. */
13171 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13172 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
13173 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13174 }
13175 }
13176
Linus Torvalds1da177e2005-04-16 15:20:36 -070013177 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
13178 tp->tg3_flags |= TG3_FLAG_PCI_HIGH_SPEED;
13179 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
13180 tp->tg3_flags |= TG3_FLAG_PCI_32BIT;
13181
13182 /* Chip-specific fixup from Broadcom driver */
13183 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
13184 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
13185 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
13186 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
13187 }
13188
Michael Chan1ee582d2005-08-09 20:16:46 -070013189 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070013190 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013191 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070013192 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070013193 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013194 tp->write32_tx_mbox = tg3_write32;
13195 tp->write32_rx_mbox = tg3_write32;
13196
13197 /* Various workaround register access methods */
13198 if (tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG)
13199 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013200 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
13201 ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
13202 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
13203 /*
13204 * Back to back register writes can cause problems on these
13205 * chips, the workaround is to read back all reg writes
13206 * except those to mailbox regs.
13207 *
13208 * See tg3_write_indirect_reg32().
13209 */
Michael Chan1ee582d2005-08-09 20:16:46 -070013210 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013211 }
13212
Michael Chan1ee582d2005-08-09 20:16:46 -070013213 if ((tp->tg3_flags & TG3_FLAG_TXD_MBOX_HWBUG) ||
13214 (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)) {
13215 tp->write32_tx_mbox = tg3_write32_tx_mbox;
13216 if (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)
13217 tp->write32_rx_mbox = tg3_write_flush_reg32;
13218 }
Michael Chan20094932005-08-09 20:16:32 -070013219
Michael Chan68929142005-08-09 20:17:14 -070013220 if (tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND) {
13221 tp->read32 = tg3_read_indirect_reg32;
13222 tp->write32 = tg3_write_indirect_reg32;
13223 tp->read32_mbox = tg3_read_indirect_mbox;
13224 tp->write32_mbox = tg3_write_indirect_mbox;
13225 tp->write32_tx_mbox = tg3_write_indirect_mbox;
13226 tp->write32_rx_mbox = tg3_write_indirect_mbox;
13227
13228 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070013229 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070013230
13231 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13232 pci_cmd &= ~PCI_COMMAND_MEMORY;
13233 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13234 }
Michael Chanb5d37722006-09-27 16:06:21 -070013235 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
13236 tp->read32_mbox = tg3_read32_mbox_5906;
13237 tp->write32_mbox = tg3_write32_mbox_5906;
13238 tp->write32_tx_mbox = tg3_write32_mbox_5906;
13239 tp->write32_rx_mbox = tg3_write32_mbox_5906;
13240 }
Michael Chan68929142005-08-09 20:17:14 -070013241
Michael Chanbbadf502006-04-06 21:46:34 -070013242 if (tp->write32 == tg3_write_indirect_reg32 ||
13243 ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) &&
13244 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070013245 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Michael Chanbbadf502006-04-06 21:46:34 -070013246 tp->tg3_flags |= TG3_FLAG_SRAM_USE_CONFIG;
13247
Michael Chan7d0c41e2005-04-21 17:06:20 -070013248 /* Get eeprom hw config before calling tg3_set_power_state().
Michael Chan9d26e212006-12-07 00:21:14 -080013249 * In particular, the TG3_FLG2_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070013250 * determined before calling tg3_set_power_state() so that
13251 * we know whether or not to switch out of Vaux power.
13252 * When the flag is set, it means that GPIO1 is used for eeprom
13253 * write protect and also implies that it is a LOM where GPIOs
13254 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013255 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070013256 tg3_get_eeprom_hw_cfg(tp);
13257
Matt Carlson0d3031d2007-10-10 18:02:43 -070013258 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
13259 /* Allow reads and writes to the
13260 * APE register and memory space.
13261 */
13262 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +000013263 PCISTATE_ALLOW_APE_SHMEM_WR |
13264 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070013265 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
13266 pci_state_reg);
13267 }
13268
Matt Carlson9936bcf2007-10-10 18:03:07 -070013269 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013270 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013271 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013272 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlsonc885e822010-08-02 11:25:57 +000013273 (tp->tg3_flags3 & TG3_FLG3_5717_PLUS))
Matt Carlsond30cdd22007-10-07 23:28:35 -070013274 tp->tg3_flags |= TG3_FLAG_CPMU_PRESENT;
13275
Michael Chan314fba32005-04-21 17:07:04 -070013276 /* Set up tp->grc_local_ctrl before calling tg3_set_power_state().
13277 * GPIO1 driven high will bring 5700's external PHY out of reset.
13278 * It is also used as eeprom write protect on LOMs.
13279 */
13280 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
13281 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
13282 (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT))
13283 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
13284 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070013285 /* Unused GPIO3 must be driven as output on 5752 because there
13286 * are no pull-up resistors on unused GPIO pins.
13287 */
13288 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
13289 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070013290
Matt Carlson321d32a2008-11-21 17:22:19 -080013291 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsoncb4ed1f2010-01-20 16:58:09 +000013292 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
13293 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Michael Chanaf36e6b2006-03-23 01:28:06 -080013294 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
13295
Matt Carlson8d519ab2009-04-20 06:58:01 +000013296 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
13297 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070013298 /* Turn off the debug UART. */
13299 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
13300 if (tp->tg3_flags2 & TG3_FLG2_IS_NIC)
13301 /* Keep VMain power. */
13302 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
13303 GRC_LCLCTRL_GPIO_OUTPUT0;
13304 }
13305
Linus Torvalds1da177e2005-04-16 15:20:36 -070013306 /* Force the chip into D0. */
Michael Chanbc1c7562006-03-20 17:48:03 -080013307 err = tg3_set_power_state(tp, PCI_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013308 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000013309 dev_err(&tp->pdev->dev, "Transition to D0 failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070013310 return err;
13311 }
13312
Linus Torvalds1da177e2005-04-16 15:20:36 -070013313 /* Derive initial jumbo mode from MTU assigned in
13314 * ether_setup() via the alloc_etherdev() call
13315 */
Michael Chan0f893dc2005-07-25 12:30:38 -070013316 if (tp->dev->mtu > ETH_DATA_LEN &&
Michael Chana4e2b342005-10-26 15:46:52 -070013317 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Michael Chan0f893dc2005-07-25 12:30:38 -070013318 tp->tg3_flags |= TG3_FLAG_JUMBO_RING_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013319
13320 /* Determine WakeOnLan speed to use. */
13321 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13322 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
13323 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
13324 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
13325 tp->tg3_flags &= ~(TG3_FLAG_WOL_SPEED_100MB);
13326 } else {
13327 tp->tg3_flags |= TG3_FLAG_WOL_SPEED_100MB;
13328 }
13329
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013330 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
13331 tp->tg3_flags3 |= TG3_FLG3_PHY_IS_FET;
13332
Linus Torvalds1da177e2005-04-16 15:20:36 -070013333 /* A few boards don't want Ethernet@WireSpeed phy feature */
13334 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
13335 ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
13336 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070013337 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013338 (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) ||
Michael Chan747e8f82005-07-25 12:33:22 -070013339 (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013340 tp->tg3_flags2 |= TG3_FLG2_NO_ETH_WIRE_SPEED;
13341
13342 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
13343 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
13344 tp->tg3_flags2 |= TG3_FLG2_PHY_ADC_BUG;
13345 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
13346 tp->tg3_flags2 |= TG3_FLG2_PHY_5704_A0_BUG;
13347
Matt Carlson321d32a2008-11-21 17:22:19 -080013348 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013349 !(tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080013350 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013351 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
Matt Carlsonc885e822010-08-02 11:25:57 +000013352 !(tp->tg3_flags3 & TG3_FLG3_5717_PLUS)) {
Michael Chanc424cb22006-04-29 18:56:34 -070013353 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013354 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013355 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
13356 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080013357 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
13358 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
13359 tp->tg3_flags2 |= TG3_FLG2_PHY_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080013360 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
13361 tp->tg3_flags2 |= TG3_FLG2_PHY_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080013362 } else
Michael Chanc424cb22006-04-29 18:56:34 -070013363 tp->tg3_flags2 |= TG3_FLG2_PHY_BER_BUG;
13364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013365
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013366 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
13367 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
13368 tp->phy_otp = tg3_read_otp_phycfg(tp);
13369 if (tp->phy_otp == 0)
13370 tp->phy_otp = TG3_OTP_DEFAULT;
13371 }
13372
Matt Carlsonf51f3562008-05-25 23:45:08 -070013373 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT)
Matt Carlson8ef21422008-05-02 16:47:53 -070013374 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
13375 else
13376 tp->mi_mode = MAC_MI_MODE_BASE;
13377
Linus Torvalds1da177e2005-04-16 15:20:36 -070013378 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013379 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
13380 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
13381 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
13382
Matt Carlson321d32a2008-11-21 17:22:19 -080013383 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
13384 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson57e69832008-05-25 23:48:31 -070013385 tp->tg3_flags3 |= TG3_FLG3_USE_PHYLIB;
13386
Matt Carlson158d7ab2008-05-29 01:37:54 -070013387 err = tg3_mdio_init(tp);
13388 if (err)
13389 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013390
Matt Carlson55dffe72010-01-12 10:11:39 +000013391 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 &&
Matt Carlson2138c002010-07-11 09:31:43 +000013392 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0)
Matt Carlson55dffe72010-01-12 10:11:39 +000013393 return -ENOTSUPP;
13394
Linus Torvalds1da177e2005-04-16 15:20:36 -070013395 /* Initialize data/descriptor byte/word swapping. */
13396 val = tr32(GRC_MODE);
13397 val &= GRC_MODE_HOST_STACKUP;
13398 tw32(GRC_MODE, val | tp->grc_mode);
13399
13400 tg3_switch_clocks(tp);
13401
13402 /* Clear this out for sanity. */
13403 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
13404
13405 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
13406 &pci_state_reg);
13407 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
13408 (tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG) == 0) {
13409 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
13410
13411 if (chiprevid == CHIPREV_ID_5701_A0 ||
13412 chiprevid == CHIPREV_ID_5701_B0 ||
13413 chiprevid == CHIPREV_ID_5701_B2 ||
13414 chiprevid == CHIPREV_ID_5701_B5) {
13415 void __iomem *sram_base;
13416
13417 /* Write some dummy words into the SRAM status block
13418 * area, see if it reads back correctly. If the return
13419 * value is bad, force enable the PCIX workaround.
13420 */
13421 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
13422
13423 writel(0x00000000, sram_base);
13424 writel(0x00000000, sram_base + 4);
13425 writel(0xffffffff, sram_base + 4);
13426 if (readl(sram_base) != 0x00000000)
13427 tp->tg3_flags |= TG3_FLAG_PCIX_TARGET_HWBUG;
13428 }
13429 }
13430
13431 udelay(50);
13432 tg3_nvram_init(tp);
13433
13434 grc_misc_cfg = tr32(GRC_MISC_CFG);
13435 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
13436
Linus Torvalds1da177e2005-04-16 15:20:36 -070013437 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
13438 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
13439 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
13440 tp->tg3_flags2 |= TG3_FLG2_IS_5788;
13441
David S. Millerfac9b832005-05-18 22:46:34 -070013442 if (!(tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
13443 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700))
13444 tp->tg3_flags |= TG3_FLAG_TAGGED_STATUS;
13445 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) {
13446 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
13447 HOSTCC_MODE_CLRTICK_TXBD);
13448
13449 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
13450 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13451 tp->misc_host_ctrl);
13452 }
13453
Matt Carlson3bda1252008-08-15 14:08:22 -070013454 /* Preserve the APE MAC_MODE bits */
13455 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
13456 tp->mac_mode = tr32(MAC_MODE) |
13457 MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
13458 else
13459 tp->mac_mode = TG3_DEF_MAC_MODE;
13460
Linus Torvalds1da177e2005-04-16 15:20:36 -070013461 /* these are limited to 10/100 only */
13462 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
13463 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
13464 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
13465 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
13466 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
13467 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
13468 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
13469 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
13470 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
Michael Chan676917d2006-12-07 00:20:22 -080013471 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
13472 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013473 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
Matt Carlsond1101142010-02-17 15:16:55 +000013474 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
13475 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013476 (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013477 tp->tg3_flags |= TG3_FLAG_10_100_ONLY;
13478
13479 err = tg3_phy_probe(tp);
13480 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000013481 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013482 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013483 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013484 }
13485
Matt Carlson184b8902010-04-05 10:19:25 +000013486 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080013487 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013488
13489 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
13490 tp->tg3_flags &= ~TG3_FLAG_USE_MI_INTERRUPT;
13491 } else {
13492 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
13493 tp->tg3_flags |= TG3_FLAG_USE_MI_INTERRUPT;
13494 else
13495 tp->tg3_flags &= ~TG3_FLAG_USE_MI_INTERRUPT;
13496 }
13497
13498 /* 5700 {AX,BX} chips have a broken status block link
13499 * change bit implementation, so we must use the
13500 * status register in those cases.
13501 */
13502 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
13503 tp->tg3_flags |= TG3_FLAG_USE_LINKCHG_REG;
13504 else
13505 tp->tg3_flags &= ~TG3_FLAG_USE_LINKCHG_REG;
13506
13507 /* The led_ctrl is set during tg3_phy_probe, here we might
13508 * have to force the link status polling mechanism based
13509 * upon subsystem IDs.
13510 */
13511 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070013512 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070013513 !(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)) {
13514 tp->tg3_flags |= (TG3_FLAG_USE_MI_INTERRUPT |
13515 TG3_FLAG_USE_LINKCHG_REG);
13516 }
13517
13518 /* For all SERDES we poll the MAC status register. */
13519 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
13520 tp->tg3_flags |= TG3_FLAG_POLL_SERDES;
13521 else
13522 tp->tg3_flags &= ~TG3_FLAG_POLL_SERDES;
13523
Matt Carlson9dc7a112010-04-12 06:58:28 +000013524 tp->rx_offset = NET_IP_ALIGN + TG3_RX_HEADROOM;
Matt Carlsond2757fc2010-04-12 06:58:27 +000013525 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013526 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsond2757fc2010-04-12 06:58:27 +000013527 (tp->tg3_flags & TG3_FLAG_PCIX_MODE) != 0) {
Matt Carlson9dc7a112010-04-12 06:58:28 +000013528 tp->rx_offset -= NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000013529#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000013530 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000013531#endif
13532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013533
Michael Chanf92905d2006-06-29 20:14:29 -070013534 tp->rx_std_max_post = TG3_RX_RING_SIZE;
13535
13536 /* Increment the rx prod index on the rx std ring by at most
13537 * 8 for these chips to workaround hw errata.
13538 */
13539 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13540 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
13541 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
13542 tp->rx_std_max_post = 8;
13543
Matt Carlson8ed5d972007-05-07 00:25:49 -070013544 if (tp->tg3_flags & TG3_FLAG_ASPM_WORKAROUND)
13545 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
13546 PCIE_PWR_MGMT_L1_THRESH_MSK;
13547
Linus Torvalds1da177e2005-04-16 15:20:36 -070013548 return err;
13549}
13550
David S. Miller49b6e95f2007-03-29 01:38:42 -070013551#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070013552static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
13553{
13554 struct net_device *dev = tp->dev;
13555 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070013556 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070013557 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070013558 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013559
David S. Miller49b6e95f2007-03-29 01:38:42 -070013560 addr = of_get_property(dp, "local-mac-address", &len);
13561 if (addr && len == 6) {
13562 memcpy(dev->dev_addr, addr, 6);
13563 memcpy(dev->perm_addr, dev->dev_addr, 6);
13564 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013565 }
13566 return -ENODEV;
13567}
13568
13569static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
13570{
13571 struct net_device *dev = tp->dev;
13572
13573 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070013574 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013575 return 0;
13576}
13577#endif
13578
13579static int __devinit tg3_get_device_address(struct tg3 *tp)
13580{
13581 struct net_device *dev = tp->dev;
13582 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080013583 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013584
David S. Miller49b6e95f2007-03-29 01:38:42 -070013585#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070013586 if (!tg3_get_macaddr_sparc(tp))
13587 return 0;
13588#endif
13589
13590 mac_offset = 0x7c;
David S. Millerf49639e2006-06-09 11:58:36 -070013591 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) ||
Michael Chana4e2b342005-10-26 15:46:52 -070013592 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013593 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
13594 mac_offset = 0xcc;
13595 if (tg3_nvram_lock(tp))
13596 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
13597 else
13598 tg3_nvram_unlock(tp);
Matt Carlsona50d0792010-06-05 17:24:37 +000013599 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13600 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) {
13601 if (PCI_FUNC(tp->pdev->devfn) & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000013602 mac_offset = 0xcc;
Matt Carlsona50d0792010-06-05 17:24:37 +000013603 if (PCI_FUNC(tp->pdev->devfn) > 1)
13604 mac_offset += 0x18c;
Matt Carlsona1b950d2009-09-01 13:20:17 +000013605 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070013606 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013607
13608 /* First try to get it from MAC address mailbox. */
13609 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
13610 if ((hi >> 16) == 0x484b) {
13611 dev->dev_addr[0] = (hi >> 8) & 0xff;
13612 dev->dev_addr[1] = (hi >> 0) & 0xff;
13613
13614 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
13615 dev->dev_addr[2] = (lo >> 24) & 0xff;
13616 dev->dev_addr[3] = (lo >> 16) & 0xff;
13617 dev->dev_addr[4] = (lo >> 8) & 0xff;
13618 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013619
Michael Chan008652b2006-03-27 23:14:53 -080013620 /* Some old bootcode may report a 0 MAC address in SRAM */
13621 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
13622 }
13623 if (!addr_ok) {
13624 /* Next, try NVRAM. */
Matt Carlsondf259d82009-04-20 06:57:14 +000013625 if (!(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) &&
13626 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000013627 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070013628 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
13629 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080013630 }
13631 /* Finally just fetch it out of the MAC control regs. */
13632 else {
13633 hi = tr32(MAC_ADDR_0_HIGH);
13634 lo = tr32(MAC_ADDR_0_LOW);
13635
13636 dev->dev_addr[5] = lo & 0xff;
13637 dev->dev_addr[4] = (lo >> 8) & 0xff;
13638 dev->dev_addr[3] = (lo >> 16) & 0xff;
13639 dev->dev_addr[2] = (lo >> 24) & 0xff;
13640 dev->dev_addr[1] = hi & 0xff;
13641 dev->dev_addr[0] = (hi >> 8) & 0xff;
13642 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013643 }
13644
13645 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070013646#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070013647 if (!tg3_get_default_macaddr_sparc(tp))
13648 return 0;
13649#endif
13650 return -EINVAL;
13651 }
John W. Linville2ff43692005-09-12 14:44:20 -070013652 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013653 return 0;
13654}
13655
David S. Miller59e6b432005-05-18 22:50:10 -070013656#define BOUNDARY_SINGLE_CACHELINE 1
13657#define BOUNDARY_MULTI_CACHELINE 2
13658
13659static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
13660{
13661 int cacheline_size;
13662 u8 byte;
13663 int goal;
13664
13665 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
13666 if (byte == 0)
13667 cacheline_size = 1024;
13668 else
13669 cacheline_size = (int) byte * 4;
13670
13671 /* On 5703 and later chips, the boundary bits have no
13672 * effect.
13673 */
13674 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13675 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13676 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
13677 goto out;
13678
13679#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
13680 goal = BOUNDARY_MULTI_CACHELINE;
13681#else
13682#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
13683 goal = BOUNDARY_SINGLE_CACHELINE;
13684#else
13685 goal = 0;
13686#endif
13687#endif
13688
Matt Carlsonc885e822010-08-02 11:25:57 +000013689 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000013690 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
13691 goto out;
13692 }
13693
David S. Miller59e6b432005-05-18 22:50:10 -070013694 if (!goal)
13695 goto out;
13696
13697 /* PCI controllers on most RISC systems tend to disconnect
13698 * when a device tries to burst across a cache-line boundary.
13699 * Therefore, letting tg3 do so just wastes PCI bandwidth.
13700 *
13701 * Unfortunately, for PCI-E there are only limited
13702 * write-side controls for this, and thus for reads
13703 * we will still get the disconnects. We'll also waste
13704 * these PCI cycles for both read and write for chips
13705 * other than 5700 and 5701 which do not implement the
13706 * boundary bits.
13707 */
13708 if ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) &&
13709 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)) {
13710 switch (cacheline_size) {
13711 case 16:
13712 case 32:
13713 case 64:
13714 case 128:
13715 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13716 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
13717 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
13718 } else {
13719 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
13720 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
13721 }
13722 break;
13723
13724 case 256:
13725 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
13726 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
13727 break;
13728
13729 default:
13730 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
13731 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
13732 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070013733 }
David S. Miller59e6b432005-05-18 22:50:10 -070013734 } else if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
13735 switch (cacheline_size) {
13736 case 16:
13737 case 32:
13738 case 64:
13739 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13740 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
13741 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
13742 break;
13743 }
13744 /* fallthrough */
13745 case 128:
13746 default:
13747 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
13748 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
13749 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070013750 }
David S. Miller59e6b432005-05-18 22:50:10 -070013751 } else {
13752 switch (cacheline_size) {
13753 case 16:
13754 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13755 val |= (DMA_RWCTRL_READ_BNDRY_16 |
13756 DMA_RWCTRL_WRITE_BNDRY_16);
13757 break;
13758 }
13759 /* fallthrough */
13760 case 32:
13761 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13762 val |= (DMA_RWCTRL_READ_BNDRY_32 |
13763 DMA_RWCTRL_WRITE_BNDRY_32);
13764 break;
13765 }
13766 /* fallthrough */
13767 case 64:
13768 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13769 val |= (DMA_RWCTRL_READ_BNDRY_64 |
13770 DMA_RWCTRL_WRITE_BNDRY_64);
13771 break;
13772 }
13773 /* fallthrough */
13774 case 128:
13775 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13776 val |= (DMA_RWCTRL_READ_BNDRY_128 |
13777 DMA_RWCTRL_WRITE_BNDRY_128);
13778 break;
13779 }
13780 /* fallthrough */
13781 case 256:
13782 val |= (DMA_RWCTRL_READ_BNDRY_256 |
13783 DMA_RWCTRL_WRITE_BNDRY_256);
13784 break;
13785 case 512:
13786 val |= (DMA_RWCTRL_READ_BNDRY_512 |
13787 DMA_RWCTRL_WRITE_BNDRY_512);
13788 break;
13789 case 1024:
13790 default:
13791 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
13792 DMA_RWCTRL_WRITE_BNDRY_1024);
13793 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070013794 }
David S. Miller59e6b432005-05-18 22:50:10 -070013795 }
13796
13797out:
13798 return val;
13799}
13800
Linus Torvalds1da177e2005-04-16 15:20:36 -070013801static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
13802{
13803 struct tg3_internal_buffer_desc test_desc;
13804 u32 sram_dma_descs;
13805 int i, ret;
13806
13807 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
13808
13809 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
13810 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
13811 tw32(RDMAC_STATUS, 0);
13812 tw32(WDMAC_STATUS, 0);
13813
13814 tw32(BUFMGR_MODE, 0);
13815 tw32(FTQ_RESET, 0);
13816
13817 test_desc.addr_hi = ((u64) buf_dma) >> 32;
13818 test_desc.addr_lo = buf_dma & 0xffffffff;
13819 test_desc.nic_mbuf = 0x00002100;
13820 test_desc.len = size;
13821
13822 /*
13823 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
13824 * the *second* time the tg3 driver was getting loaded after an
13825 * initial scan.
13826 *
13827 * Broadcom tells me:
13828 * ...the DMA engine is connected to the GRC block and a DMA
13829 * reset may affect the GRC block in some unpredictable way...
13830 * The behavior of resets to individual blocks has not been tested.
13831 *
13832 * Broadcom noted the GRC reset will also reset all sub-components.
13833 */
13834 if (to_device) {
13835 test_desc.cqid_sqid = (13 << 8) | 2;
13836
13837 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
13838 udelay(40);
13839 } else {
13840 test_desc.cqid_sqid = (16 << 8) | 7;
13841
13842 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
13843 udelay(40);
13844 }
13845 test_desc.flags = 0x00000005;
13846
13847 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
13848 u32 val;
13849
13850 val = *(((u32 *)&test_desc) + i);
13851 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
13852 sram_dma_descs + (i * sizeof(u32)));
13853 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
13854 }
13855 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
13856
Matt Carlson859a588792010-04-05 10:19:28 +000013857 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013858 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a588792010-04-05 10:19:28 +000013859 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070013860 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013861
13862 ret = -ENODEV;
13863 for (i = 0; i < 40; i++) {
13864 u32 val;
13865
13866 if (to_device)
13867 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
13868 else
13869 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
13870 if ((val & 0xffff) == sram_dma_descs) {
13871 ret = 0;
13872 break;
13873 }
13874
13875 udelay(100);
13876 }
13877
13878 return ret;
13879}
13880
David S. Millerded73402005-05-23 13:59:47 -070013881#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070013882
13883static int __devinit tg3_test_dma(struct tg3 *tp)
13884{
13885 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070013886 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000013887 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013888
13889 buf = pci_alloc_consistent(tp->pdev, TEST_BUFFER_SIZE, &buf_dma);
13890 if (!buf) {
13891 ret = -ENOMEM;
13892 goto out_nofree;
13893 }
13894
13895 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
13896 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
13897
David S. Miller59e6b432005-05-18 22:50:10 -070013898 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013899
Matt Carlsonc885e822010-08-02 11:25:57 +000013900 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000013901 goto out;
13902
Linus Torvalds1da177e2005-04-16 15:20:36 -070013903 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
13904 /* DMA read watermark not used on PCIE */
13905 tp->dma_rwctrl |= 0x00180000;
13906 } else if (!(tp->tg3_flags & TG3_FLAG_PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070013907 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
13908 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013909 tp->dma_rwctrl |= 0x003f0000;
13910 else
13911 tp->dma_rwctrl |= 0x003f000f;
13912 } else {
13913 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
13914 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
13915 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080013916 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013917
Michael Chan4a29cc22006-03-19 13:21:12 -080013918 /* If the 5704 is behind the EPB bridge, we can
13919 * do the less restrictive ONE_DMA workaround for
13920 * better performance.
13921 */
13922 if ((tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG) &&
13923 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
13924 tp->dma_rwctrl |= 0x8000;
13925 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013926 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
13927
Michael Chan49afdeb2007-02-13 12:17:03 -080013928 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
13929 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070013930 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080013931 tp->dma_rwctrl |=
13932 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
13933 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
13934 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070013935 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
13936 /* 5780 always in PCIX mode */
13937 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070013938 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
13939 /* 5714 always in PCIX mode */
13940 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013941 } else {
13942 tp->dma_rwctrl |= 0x001b000f;
13943 }
13944 }
13945
13946 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
13947 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
13948 tp->dma_rwctrl &= 0xfffffff0;
13949
13950 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13951 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
13952 /* Remove this if it causes problems for some boards. */
13953 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
13954
13955 /* On 5700/5701 chips, we need to set this bit.
13956 * Otherwise the chip will issue cacheline transactions
13957 * to streamable DMA memory with not all the byte
13958 * enables turned on. This is an error on several
13959 * RISC PCI controllers, in particular sparc64.
13960 *
13961 * On 5703/5704 chips, this bit has been reassigned
13962 * a different meaning. In particular, it is used
13963 * on those chips to enable a PCI-X workaround.
13964 */
13965 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
13966 }
13967
13968 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
13969
13970#if 0
13971 /* Unneeded, already done by tg3_get_invariants. */
13972 tg3_switch_clocks(tp);
13973#endif
13974
Linus Torvalds1da177e2005-04-16 15:20:36 -070013975 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 goto out;
13978
David S. Miller59e6b432005-05-18 22:50:10 -070013979 /* It is best to perform DMA test with maximum write burst size
13980 * to expose the 5700/5701 write DMA bug.
13981 */
13982 saved_dma_rwctrl = tp->dma_rwctrl;
13983 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
13984 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
13985
Linus Torvalds1da177e2005-04-16 15:20:36 -070013986 while (1) {
13987 u32 *p = buf, i;
13988
13989 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
13990 p[i] = i;
13991
13992 /* Send the buffer to the chip. */
13993 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
13994 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000013995 dev_err(&tp->pdev->dev,
13996 "%s: Buffer write failed. err = %d\n",
13997 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013998 break;
13999 }
14000
14001#if 0
14002 /* validate data reached card RAM correctly. */
14003 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14004 u32 val;
14005 tg3_read_mem(tp, 0x2100 + (i*4), &val);
14006 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000014007 dev_err(&tp->pdev->dev,
14008 "%s: Buffer corrupted on device! "
14009 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014010 /* ret = -ENODEV here? */
14011 }
14012 p[i] = 0;
14013 }
14014#endif
14015 /* Now read it back. */
14016 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
14017 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000014018 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
14019 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014020 break;
14021 }
14022
14023 /* Verify it. */
14024 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14025 if (p[i] == i)
14026 continue;
14027
David S. Miller59e6b432005-05-18 22:50:10 -070014028 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14029 DMA_RWCTRL_WRITE_BNDRY_16) {
14030 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014031 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
14032 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14033 break;
14034 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000014035 dev_err(&tp->pdev->dev,
14036 "%s: Buffer corrupted on read back! "
14037 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014038 ret = -ENODEV;
14039 goto out;
14040 }
14041 }
14042
14043 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
14044 /* Success. */
14045 ret = 0;
14046 break;
14047 }
14048 }
David S. Miller59e6b432005-05-18 22:50:10 -070014049 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14050 DMA_RWCTRL_WRITE_BNDRY_16) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014051 static struct pci_device_id dma_wait_state_chipsets[] = {
14052 { PCI_DEVICE(PCI_VENDOR_ID_APPLE,
14053 PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
14054 { },
14055 };
14056
David S. Miller59e6b432005-05-18 22:50:10 -070014057 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070014058 * now look for chipsets that are known to expose the
14059 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070014060 */
Michael Chan6d1cfba2005-06-08 14:13:14 -070014061 if (pci_dev_present(dma_wait_state_chipsets)) {
14062 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14063 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a588792010-04-05 10:19:28 +000014064 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014065 /* Safe to use the calculated DMA boundary. */
14066 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a588792010-04-05 10:19:28 +000014067 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070014068
David S. Miller59e6b432005-05-18 22:50:10 -070014069 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014071
14072out:
14073 pci_free_consistent(tp->pdev, TEST_BUFFER_SIZE, buf, buf_dma);
14074out_nofree:
14075 return ret;
14076}
14077
14078static void __devinit tg3_init_link_config(struct tg3 *tp)
14079{
14080 tp->link_config.advertising =
14081 (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
14082 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
14083 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full |
14084 ADVERTISED_Autoneg | ADVERTISED_MII);
14085 tp->link_config.speed = SPEED_INVALID;
14086 tp->link_config.duplex = DUPLEX_INVALID;
14087 tp->link_config.autoneg = AUTONEG_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014088 tp->link_config.active_speed = SPEED_INVALID;
14089 tp->link_config.active_duplex = DUPLEX_INVALID;
14090 tp->link_config.phy_is_low_power = 0;
14091 tp->link_config.orig_speed = SPEED_INVALID;
14092 tp->link_config.orig_duplex = DUPLEX_INVALID;
14093 tp->link_config.orig_autoneg = AUTONEG_INVALID;
14094}
14095
14096static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
14097{
Matt Carlsonc885e822010-08-02 11:25:57 +000014098 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
Matt Carlson666bc832010-01-20 16:58:03 +000014099 tp->bufmgr_config.mbuf_read_dma_low_water =
14100 DEFAULT_MB_RDMA_LOW_WATER_5705;
14101 tp->bufmgr_config.mbuf_mac_rx_low_water =
14102 DEFAULT_MB_MACRX_LOW_WATER_57765;
14103 tp->bufmgr_config.mbuf_high_water =
14104 DEFAULT_MB_HIGH_WATER_57765;
14105
14106 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14107 DEFAULT_MB_RDMA_LOW_WATER_5705;
14108 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14109 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
14110 tp->bufmgr_config.mbuf_high_water_jumbo =
14111 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
14112 } else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
Michael Chanfdfec1722005-07-25 12:31:48 -070014113 tp->bufmgr_config.mbuf_read_dma_low_water =
14114 DEFAULT_MB_RDMA_LOW_WATER_5705;
14115 tp->bufmgr_config.mbuf_mac_rx_low_water =
14116 DEFAULT_MB_MACRX_LOW_WATER_5705;
14117 tp->bufmgr_config.mbuf_high_water =
14118 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070014119 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
14120 tp->bufmgr_config.mbuf_mac_rx_low_water =
14121 DEFAULT_MB_MACRX_LOW_WATER_5906;
14122 tp->bufmgr_config.mbuf_high_water =
14123 DEFAULT_MB_HIGH_WATER_5906;
14124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014125
Michael Chanfdfec1722005-07-25 12:31:48 -070014126 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14127 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
14128 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14129 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
14130 tp->bufmgr_config.mbuf_high_water_jumbo =
14131 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
14132 } else {
14133 tp->bufmgr_config.mbuf_read_dma_low_water =
14134 DEFAULT_MB_RDMA_LOW_WATER;
14135 tp->bufmgr_config.mbuf_mac_rx_low_water =
14136 DEFAULT_MB_MACRX_LOW_WATER;
14137 tp->bufmgr_config.mbuf_high_water =
14138 DEFAULT_MB_HIGH_WATER;
14139
14140 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14141 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
14142 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14143 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
14144 tp->bufmgr_config.mbuf_high_water_jumbo =
14145 DEFAULT_MB_HIGH_WATER_JUMBO;
14146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014147
14148 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
14149 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
14150}
14151
14152static char * __devinit tg3_phy_string(struct tg3 *tp)
14153{
Matt Carlson79eb6902010-02-17 15:17:03 +000014154 switch (tp->phy_id & TG3_PHY_ID_MASK) {
14155 case TG3_PHY_ID_BCM5400: return "5400";
14156 case TG3_PHY_ID_BCM5401: return "5401";
14157 case TG3_PHY_ID_BCM5411: return "5411";
14158 case TG3_PHY_ID_BCM5701: return "5701";
14159 case TG3_PHY_ID_BCM5703: return "5703";
14160 case TG3_PHY_ID_BCM5704: return "5704";
14161 case TG3_PHY_ID_BCM5705: return "5705";
14162 case TG3_PHY_ID_BCM5750: return "5750";
14163 case TG3_PHY_ID_BCM5752: return "5752";
14164 case TG3_PHY_ID_BCM5714: return "5714";
14165 case TG3_PHY_ID_BCM5780: return "5780";
14166 case TG3_PHY_ID_BCM5755: return "5755";
14167 case TG3_PHY_ID_BCM5787: return "5787";
14168 case TG3_PHY_ID_BCM5784: return "5784";
14169 case TG3_PHY_ID_BCM5756: return "5722/5756";
14170 case TG3_PHY_ID_BCM5906: return "5906";
14171 case TG3_PHY_ID_BCM5761: return "5761";
14172 case TG3_PHY_ID_BCM5718C: return "5718C";
14173 case TG3_PHY_ID_BCM5718S: return "5718S";
14174 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000014175 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson79eb6902010-02-17 15:17:03 +000014176 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070014177 case 0: return "serdes";
14178 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070014179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014180}
14181
Michael Chanf9804dd2005-09-27 12:13:10 -070014182static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
14183{
14184 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
14185 strcpy(str, "PCI Express");
14186 return str;
14187 } else if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
14188 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
14189
14190 strcpy(str, "PCIX:");
14191
14192 if ((clock_ctrl == 7) ||
14193 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
14194 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
14195 strcat(str, "133MHz");
14196 else if (clock_ctrl == 0)
14197 strcat(str, "33MHz");
14198 else if (clock_ctrl == 2)
14199 strcat(str, "50MHz");
14200 else if (clock_ctrl == 4)
14201 strcat(str, "66MHz");
14202 else if (clock_ctrl == 6)
14203 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070014204 } else {
14205 strcpy(str, "PCI:");
14206 if (tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED)
14207 strcat(str, "66MHz");
14208 else
14209 strcat(str, "33MHz");
14210 }
14211 if (tp->tg3_flags & TG3_FLAG_PCI_32BIT)
14212 strcat(str, ":32-bit");
14213 else
14214 strcat(str, ":64-bit");
14215 return str;
14216}
14217
Michael Chan8c2dc7e2005-12-19 16:26:02 -080014218static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014219{
14220 struct pci_dev *peer;
14221 unsigned int func, devnr = tp->pdev->devfn & ~7;
14222
14223 for (func = 0; func < 8; func++) {
14224 peer = pci_get_slot(tp->pdev->bus, devnr | func);
14225 if (peer && peer != tp->pdev)
14226 break;
14227 pci_dev_put(peer);
14228 }
Michael Chan16fe9d72005-12-13 21:09:54 -080014229 /* 5704 can be configured in single-port mode, set peer to
14230 * tp->pdev in that case.
14231 */
14232 if (!peer) {
14233 peer = tp->pdev;
14234 return peer;
14235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014236
14237 /*
14238 * We don't need to keep the refcount elevated; there's no way
14239 * to remove one half of this device without removing the other
14240 */
14241 pci_dev_put(peer);
14242
14243 return peer;
14244}
14245
David S. Miller15f98502005-05-18 22:49:26 -070014246static void __devinit tg3_init_coal(struct tg3 *tp)
14247{
14248 struct ethtool_coalesce *ec = &tp->coal;
14249
14250 memset(ec, 0, sizeof(*ec));
14251 ec->cmd = ETHTOOL_GCOALESCE;
14252 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
14253 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
14254 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
14255 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
14256 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
14257 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
14258 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
14259 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
14260 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
14261
14262 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
14263 HOSTCC_MODE_CLRTICK_TXBD)) {
14264 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
14265 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
14266 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
14267 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
14268 }
Michael Chand244c892005-07-05 14:42:33 -070014269
14270 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
14271 ec->rx_coalesce_usecs_irq = 0;
14272 ec->tx_coalesce_usecs_irq = 0;
14273 ec->stats_block_coalesce_usecs = 0;
14274 }
David S. Miller15f98502005-05-18 22:49:26 -070014275}
14276
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080014277static const struct net_device_ops tg3_netdev_ops = {
14278 .ndo_open = tg3_open,
14279 .ndo_stop = tg3_close,
Stephen Hemminger00829822008-11-20 20:14:53 -080014280 .ndo_start_xmit = tg3_start_xmit,
Eric Dumazet511d2222010-07-07 20:44:24 +000014281 .ndo_get_stats64 = tg3_get_stats64,
Stephen Hemminger00829822008-11-20 20:14:53 -080014282 .ndo_validate_addr = eth_validate_addr,
14283 .ndo_set_multicast_list = tg3_set_rx_mode,
14284 .ndo_set_mac_address = tg3_set_mac_addr,
14285 .ndo_do_ioctl = tg3_ioctl,
14286 .ndo_tx_timeout = tg3_tx_timeout,
14287 .ndo_change_mtu = tg3_change_mtu,
14288#if TG3_VLAN_TAG_USED
14289 .ndo_vlan_rx_register = tg3_vlan_rx_register,
14290#endif
14291#ifdef CONFIG_NET_POLL_CONTROLLER
14292 .ndo_poll_controller = tg3_poll_controller,
14293#endif
14294};
14295
14296static const struct net_device_ops tg3_netdev_ops_dma_bug = {
14297 .ndo_open = tg3_open,
14298 .ndo_stop = tg3_close,
14299 .ndo_start_xmit = tg3_start_xmit_dma_bug,
Eric Dumazet511d2222010-07-07 20:44:24 +000014300 .ndo_get_stats64 = tg3_get_stats64,
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080014301 .ndo_validate_addr = eth_validate_addr,
14302 .ndo_set_multicast_list = tg3_set_rx_mode,
14303 .ndo_set_mac_address = tg3_set_mac_addr,
14304 .ndo_do_ioctl = tg3_ioctl,
14305 .ndo_tx_timeout = tg3_tx_timeout,
14306 .ndo_change_mtu = tg3_change_mtu,
14307#if TG3_VLAN_TAG_USED
14308 .ndo_vlan_rx_register = tg3_vlan_rx_register,
14309#endif
14310#ifdef CONFIG_NET_POLL_CONTROLLER
14311 .ndo_poll_controller = tg3_poll_controller,
14312#endif
14313};
14314
Linus Torvalds1da177e2005-04-16 15:20:36 -070014315static int __devinit tg3_init_one(struct pci_dev *pdev,
14316 const struct pci_device_id *ent)
14317{
Linus Torvalds1da177e2005-04-16 15:20:36 -070014318 struct net_device *dev;
14319 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000014320 int i, err, pm_cap;
14321 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070014322 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080014323 u64 dma_mask, persist_dma_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014324
Joe Perches05dbe002010-02-17 19:44:19 +000014325 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014326
14327 err = pci_enable_device(pdev);
14328 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014329 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014330 return err;
14331 }
14332
Linus Torvalds1da177e2005-04-16 15:20:36 -070014333 err = pci_request_regions(pdev, DRV_MODULE_NAME);
14334 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014335 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014336 goto err_out_disable_pdev;
14337 }
14338
14339 pci_set_master(pdev);
14340
14341 /* Find power-management capability. */
14342 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
14343 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000014344 dev_err(&pdev->dev,
14345 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014346 err = -EIO;
14347 goto err_out_free_res;
14348 }
14349
Matt Carlsonfe5f5782009-09-01 13:09:39 +000014350 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014351 if (!dev) {
Matt Carlson2445e462010-04-05 10:19:21 +000014352 dev_err(&pdev->dev, "Etherdev alloc failed, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014353 err = -ENOMEM;
14354 goto err_out_free_res;
14355 }
14356
Linus Torvalds1da177e2005-04-16 15:20:36 -070014357 SET_NETDEV_DEV(dev, &pdev->dev);
14358
Linus Torvalds1da177e2005-04-16 15:20:36 -070014359#if TG3_VLAN_TAG_USED
14360 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014361#endif
14362
14363 tp = netdev_priv(dev);
14364 tp->pdev = pdev;
14365 tp->dev = dev;
14366 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014367 tp->rx_mode = TG3_DEF_RX_MODE;
14368 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070014369
Linus Torvalds1da177e2005-04-16 15:20:36 -070014370 if (tg3_debug > 0)
14371 tp->msg_enable = tg3_debug;
14372 else
14373 tp->msg_enable = TG3_DEF_MSG_ENABLE;
14374
14375 /* The word/byte swap controls here control register access byte
14376 * swapping. DMA data byte swapping is controlled in the GRC_MODE
14377 * setting below.
14378 */
14379 tp->misc_host_ctrl =
14380 MISC_HOST_CTRL_MASK_PCI_INT |
14381 MISC_HOST_CTRL_WORD_SWAP |
14382 MISC_HOST_CTRL_INDIR_ACCESS |
14383 MISC_HOST_CTRL_PCISTATE_RW;
14384
14385 /* The NONFRM (non-frame) byte/word swap controls take effect
14386 * on descriptor entries, anything which isn't packet data.
14387 *
14388 * The StrongARM chips on the board (one for tx, one for rx)
14389 * are running in big-endian mode.
14390 */
14391 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
14392 GRC_MODE_WSWAP_NONFRM_DATA);
14393#ifdef __BIG_ENDIAN
14394 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
14395#endif
14396 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014397 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000014398 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014399
Matt Carlsond5fe4882008-11-21 17:20:32 -080014400 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010014401 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014402 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014403 err = -ENOMEM;
14404 goto err_out_free_dev;
14405 }
14406
14407 tg3_init_link_config(tp);
14408
Linus Torvalds1da177e2005-04-16 15:20:36 -070014409 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
14410 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014411
Linus Torvalds1da177e2005-04-16 15:20:36 -070014412 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014413 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014414 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014415
14416 err = tg3_get_invariants(tp);
14417 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014418 dev_err(&pdev->dev,
14419 "Problem fetching invariants of chip, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014420 goto err_out_iounmap;
14421 }
14422
Matt Carlson615774f2009-11-13 13:03:39 +000014423 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) &&
Matt Carlsona50d0792010-06-05 17:24:37 +000014424 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0 &&
14425 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5719)
Stephen Hemminger00829822008-11-20 20:14:53 -080014426 dev->netdev_ops = &tg3_netdev_ops;
14427 else
14428 dev->netdev_ops = &tg3_netdev_ops_dma_bug;
14429
14430
Michael Chan4a29cc22006-03-19 13:21:12 -080014431 /* The EPB bridge inside 5714, 5715, and 5780 and any
14432 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080014433 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
14434 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
14435 * do DMA address check in tg3_start_xmit().
14436 */
Michael Chan4a29cc22006-03-19 13:21:12 -080014437 if (tp->tg3_flags2 & TG3_FLG2_IS_5788)
Yang Hongyang284901a2009-04-06 19:01:15 -070014438 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Michael Chan4a29cc22006-03-19 13:21:12 -080014439 else if (tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070014440 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080014441#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070014442 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080014443#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080014444 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070014445 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080014446
14447 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070014448 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080014449 err = pci_set_dma_mask(pdev, dma_mask);
14450 if (!err) {
14451 dev->features |= NETIF_F_HIGHDMA;
14452 err = pci_set_consistent_dma_mask(pdev,
14453 persist_dma_mask);
14454 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014455 dev_err(&pdev->dev, "Unable to obtain 64 bit "
14456 "DMA for consistent allocations\n");
Michael Chan72f2afb2006-03-06 19:28:35 -080014457 goto err_out_iounmap;
14458 }
14459 }
14460 }
Yang Hongyang284901a2009-04-06 19:01:15 -070014461 if (err || dma_mask == DMA_BIT_MASK(32)) {
14462 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080014463 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014464 dev_err(&pdev->dev,
14465 "No usable DMA configuration, aborting\n");
Michael Chan72f2afb2006-03-06 19:28:35 -080014466 goto err_out_iounmap;
14467 }
14468 }
14469
Michael Chanfdfec1722005-07-25 12:31:48 -070014470 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014471
Matt Carlson507399f2009-11-13 13:03:37 +000014472 /* Selectively allow TSO based on operating conditions */
14473 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO) ||
14474 (tp->fw_needed && !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)))
14475 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
14476 else {
14477 tp->tg3_flags2 &= ~(TG3_FLG2_TSO_CAPABLE | TG3_FLG2_TSO_BUG);
14478 tp->fw_needed = NULL;
14479 }
14480
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014481 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
Matt Carlson9e9fd122009-01-19 16:57:45 -080014482 tp->fw_needed = FIRMWARE_TG3;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014483
Michael Chan4e3a7aa2006-03-20 17:47:44 -080014484 /* TSO is on by default on chips that support hardware TSO.
14485 * Firmware TSO on older chips gives lower performance, so it
14486 * is off by default, but can be enabled using ethtool.
14487 */
Matt Carlsone849cdc2009-11-13 13:03:38 +000014488 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO) &&
Eric Dumazet7fe876a2010-07-08 06:14:55 +000014489 (dev->features & NETIF_F_IP_CSUM)) {
Matt Carlsone849cdc2009-11-13 13:03:38 +000014490 dev->features |= NETIF_F_TSO;
Eric Dumazet7fe876a2010-07-08 06:14:55 +000014491 vlan_features_add(dev, NETIF_F_TSO);
14492 }
Matt Carlsone849cdc2009-11-13 13:03:38 +000014493 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_2) ||
14494 (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3)) {
Eric Dumazet7fe876a2010-07-08 06:14:55 +000014495 if (dev->features & NETIF_F_IPV6_CSUM) {
Michael Chanb0026622006-07-03 19:42:14 -070014496 dev->features |= NETIF_F_TSO6;
Eric Dumazet7fe876a2010-07-08 06:14:55 +000014497 vlan_features_add(dev, NETIF_F_TSO6);
14498 }
Matt Carlsone849cdc2009-11-13 13:03:38 +000014499 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
14500 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070014501 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
14502 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014503 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Eric Dumazet7fe876a2010-07-08 06:14:55 +000014504 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070014505 dev->features |= NETIF_F_TSO_ECN;
Eric Dumazet7fe876a2010-07-08 06:14:55 +000014506 vlan_features_add(dev, NETIF_F_TSO_ECN);
14507 }
Michael Chanb0026622006-07-03 19:42:14 -070014508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014509
Linus Torvalds1da177e2005-04-16 15:20:36 -070014510 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
14511 !(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) &&
14512 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
14513 tp->tg3_flags2 |= TG3_FLG2_MAX_RXPEND_64;
14514 tp->rx_pending = 63;
14515 }
14516
Linus Torvalds1da177e2005-04-16 15:20:36 -070014517 err = tg3_get_device_address(tp);
14518 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014519 dev_err(&pdev->dev,
14520 "Could not obtain valid ethernet address, aborting\n");
Matt Carlson026a6c22009-12-03 08:36:24 +000014521 goto err_out_iounmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014522 }
14523
Matt Carlson0d3031d2007-10-10 18:02:43 -070014524 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
Matt Carlson63532392008-11-03 16:49:57 -080014525 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
Al Viro79ea13c2008-01-24 02:06:46 -080014526 if (!tp->aperegs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014527 dev_err(&pdev->dev,
14528 "Cannot map APE registers, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070014529 err = -ENOMEM;
Matt Carlson026a6c22009-12-03 08:36:24 +000014530 goto err_out_iounmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070014531 }
14532
14533 tg3_ape_lock_init(tp);
Matt Carlson7fd76442009-02-25 14:27:20 +000014534
14535 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF)
14536 tg3_read_dash_ver(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070014537 }
14538
Matt Carlsonc88864d2007-11-12 21:07:01 -080014539 /*
14540 * Reset chip in case UNDI or EFI driver did not shutdown
14541 * DMA self test will enable WDMAC and we'll see (spurious)
14542 * pending DMA on the PCI bus at that point.
14543 */
14544 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
14545 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
14546 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
14547 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
14548 }
14549
14550 err = tg3_test_dma(tp);
14551 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014552 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080014553 goto err_out_apeunmap;
14554 }
14555
Matt Carlsonc88864d2007-11-12 21:07:01 -080014556 /* flow control autonegotiation is default behavior */
14557 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
Steve Glendinninge18ce342008-12-16 02:00:00 -080014558 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlsonc88864d2007-11-12 21:07:01 -080014559
Matt Carlson78f90dc2009-11-13 13:03:42 +000014560 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
14561 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
14562 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
14563 for (i = 0; i < TG3_IRQ_MAX_VECS; i++) {
14564 struct tg3_napi *tnapi = &tp->napi[i];
14565
14566 tnapi->tp = tp;
14567 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
14568
14569 tnapi->int_mbox = intmbx;
14570 if (i < 4)
14571 intmbx += 0x8;
14572 else
14573 intmbx += 0x4;
14574
14575 tnapi->consmbox = rcvmbx;
14576 tnapi->prodmbox = sndmbx;
14577
14578 if (i) {
14579 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
14580 netif_napi_add(dev, &tnapi->napi, tg3_poll_msix, 64);
14581 } else {
14582 tnapi->coal_now = HOSTCC_MODE_NOW;
14583 netif_napi_add(dev, &tnapi->napi, tg3_poll, 64);
14584 }
14585
14586 if (!(tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX))
14587 break;
14588
14589 /*
14590 * If we support MSIX, we'll be using RSS. If we're using
14591 * RSS, the first vector only handles link interrupts and the
14592 * remaining vectors handle rx and tx interrupts. Reuse the
14593 * mailbox values for the next iteration. The values we setup
14594 * above are still useful for the single vectored mode.
14595 */
14596 if (!i)
14597 continue;
14598
14599 rcvmbx += 0x8;
14600
14601 if (sndmbx & 0x4)
14602 sndmbx -= 0x4;
14603 else
14604 sndmbx += 0xc;
14605 }
14606
Matt Carlsonc88864d2007-11-12 21:07:01 -080014607 tg3_init_coal(tp);
14608
Michael Chanc49a1562006-12-17 17:07:29 -080014609 pci_set_drvdata(pdev, dev);
14610
Linus Torvalds1da177e2005-04-16 15:20:36 -070014611 err = register_netdev(dev);
14612 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014613 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070014614 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014615 }
14616
Joe Perches05dbe002010-02-17 19:44:19 +000014617 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
14618 tp->board_part_number,
14619 tp->pci_chip_rev_id,
14620 tg3_bus_string(tp, str),
14621 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014622
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000014623 if (tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED) {
14624 struct phy_device *phydev;
14625 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000014626 netdev_info(dev,
14627 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000014628 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000014629 } else
Matt Carlson5129c3a2010-04-05 10:19:23 +000014630 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
14631 "(WireSpeed[%d])\n", tg3_phy_string(tp),
Joe Perches05dbe002010-02-17 19:44:19 +000014632 ((tp->tg3_flags & TG3_FLAG_10_100_ONLY) ? "10/100Base-TX" :
14633 ((tp->tg3_flags2 & TG3_FLG2_ANY_SERDES) ? "1000Base-SX" :
14634 "10/100/1000Base-T")),
14635 (tp->tg3_flags2 & TG3_FLG2_NO_ETH_WIRE_SPEED) == 0);
Matt Carlsondf59c942008-11-03 16:52:56 -080014636
Joe Perches05dbe002010-02-17 19:44:19 +000014637 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
14638 (tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) != 0,
14639 (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) != 0,
14640 (tp->tg3_flags & TG3_FLAG_USE_MI_INTERRUPT) != 0,
14641 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0,
14642 (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) != 0);
14643 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
14644 tp->dma_rwctrl,
14645 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
14646 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014647
14648 return 0;
14649
Matt Carlson0d3031d2007-10-10 18:02:43 -070014650err_out_apeunmap:
14651 if (tp->aperegs) {
14652 iounmap(tp->aperegs);
14653 tp->aperegs = NULL;
14654 }
14655
Linus Torvalds1da177e2005-04-16 15:20:36 -070014656err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070014657 if (tp->regs) {
14658 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070014659 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070014660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014661
14662err_out_free_dev:
14663 free_netdev(dev);
14664
14665err_out_free_res:
14666 pci_release_regions(pdev);
14667
14668err_out_disable_pdev:
14669 pci_disable_device(pdev);
14670 pci_set_drvdata(pdev, NULL);
14671 return err;
14672}
14673
14674static void __devexit tg3_remove_one(struct pci_dev *pdev)
14675{
14676 struct net_device *dev = pci_get_drvdata(pdev);
14677
14678 if (dev) {
14679 struct tg3 *tp = netdev_priv(dev);
14680
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014681 if (tp->fw)
14682 release_firmware(tp->fw);
14683
Michael Chan7faa0062006-02-02 17:29:28 -080014684 flush_scheduled_work();
Matt Carlson158d7ab2008-05-29 01:37:54 -070014685
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014686 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
14687 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070014688 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014689 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070014690
Linus Torvalds1da177e2005-04-16 15:20:36 -070014691 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070014692 if (tp->aperegs) {
14693 iounmap(tp->aperegs);
14694 tp->aperegs = NULL;
14695 }
Michael Chan68929142005-08-09 20:17:14 -070014696 if (tp->regs) {
14697 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070014698 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070014699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014700 free_netdev(dev);
14701 pci_release_regions(pdev);
14702 pci_disable_device(pdev);
14703 pci_set_drvdata(pdev, NULL);
14704 }
14705}
14706
14707static int tg3_suspend(struct pci_dev *pdev, pm_message_t state)
14708{
14709 struct net_device *dev = pci_get_drvdata(pdev);
14710 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070014711 pci_power_t target_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014712 int err;
14713
Michael Chan3e0c95f2007-08-03 20:56:54 -070014714 /* PCI register 4 needs to be saved whether netif_running() or not.
14715 * MSI address and data need to be saved if using MSI and
14716 * netif_running().
14717 */
14718 pci_save_state(pdev);
14719
Linus Torvalds1da177e2005-04-16 15:20:36 -070014720 if (!netif_running(dev))
14721 return 0;
14722
Michael Chan7faa0062006-02-02 17:29:28 -080014723 flush_scheduled_work();
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014724 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014725 tg3_netif_stop(tp);
14726
14727 del_timer_sync(&tp->timer);
14728
David S. Millerf47c11e2005-06-24 20:18:35 -070014729 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014730 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070014731 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014732
14733 netif_device_detach(dev);
14734
David S. Millerf47c11e2005-06-24 20:18:35 -070014735 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070014736 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan6a9eba12005-12-13 21:08:58 -080014737 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
David S. Millerf47c11e2005-06-24 20:18:35 -070014738 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014739
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070014740 target_state = pdev->pm_cap ? pci_target_state(pdev) : PCI_D3hot;
14741
14742 err = tg3_set_power_state(tp, target_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014743 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014744 int err2;
14745
David S. Millerf47c11e2005-06-24 20:18:35 -070014746 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014747
Michael Chan6a9eba12005-12-13 21:08:58 -080014748 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014749 err2 = tg3_restart_hw(tp, 1);
14750 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070014751 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014752
14753 tp->timer.expires = jiffies + tp->timer_offset;
14754 add_timer(&tp->timer);
14755
14756 netif_device_attach(dev);
14757 tg3_netif_start(tp);
14758
Michael Chanb9ec6c12006-07-25 16:37:27 -070014759out:
David S. Millerf47c11e2005-06-24 20:18:35 -070014760 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014761
14762 if (!err2)
14763 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014764 }
14765
14766 return err;
14767}
14768
14769static int tg3_resume(struct pci_dev *pdev)
14770{
14771 struct net_device *dev = pci_get_drvdata(pdev);
14772 struct tg3 *tp = netdev_priv(dev);
14773 int err;
14774
Michael Chan3e0c95f2007-08-03 20:56:54 -070014775 pci_restore_state(tp->pdev);
14776
Linus Torvalds1da177e2005-04-16 15:20:36 -070014777 if (!netif_running(dev))
14778 return 0;
14779
Michael Chanbc1c7562006-03-20 17:48:03 -080014780 err = tg3_set_power_state(tp, PCI_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014781 if (err)
14782 return err;
14783
14784 netif_device_attach(dev);
14785
David S. Millerf47c11e2005-06-24 20:18:35 -070014786 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014787
Michael Chan6a9eba12005-12-13 21:08:58 -080014788 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Michael Chanb9ec6c12006-07-25 16:37:27 -070014789 err = tg3_restart_hw(tp, 1);
14790 if (err)
14791 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014792
14793 tp->timer.expires = jiffies + tp->timer_offset;
14794 add_timer(&tp->timer);
14795
Linus Torvalds1da177e2005-04-16 15:20:36 -070014796 tg3_netif_start(tp);
14797
Michael Chanb9ec6c12006-07-25 16:37:27 -070014798out:
David S. Millerf47c11e2005-06-24 20:18:35 -070014799 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014800
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014801 if (!err)
14802 tg3_phy_start(tp);
14803
Michael Chanb9ec6c12006-07-25 16:37:27 -070014804 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014805}
14806
14807static struct pci_driver tg3_driver = {
14808 .name = DRV_MODULE_NAME,
14809 .id_table = tg3_pci_tbl,
14810 .probe = tg3_init_one,
14811 .remove = __devexit_p(tg3_remove_one),
14812 .suspend = tg3_suspend,
14813 .resume = tg3_resume
14814};
14815
14816static int __init tg3_init(void)
14817{
Jeff Garzik29917622006-08-19 17:48:59 -040014818 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014819}
14820
14821static void __exit tg3_cleanup(void)
14822{
14823 pci_unregister_driver(&tg3_driver);
14824}
14825
14826module_init(tg3_init);
14827module_exit(tg3_cleanup);