blob: 7892b0034c4f956709a63f6d6b4e6b4ec137b7c0 [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) {
6932 if (tr32(0x7e2c) == 0x60) {
6933 tw32(0x7e2c, 0x20);
6934 }
6935 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
6936 tw32(GRC_MISC_CFG, (1 << 29));
6937 val |= (1 << 29);
6938 }
6939 }
6940
Michael Chanb5d37722006-09-27 16:06:21 -07006941 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
6942 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
6943 tw32(GRC_VCPU_EXT_CTRL,
6944 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
6945 }
6946
Linus Torvalds1da177e2005-04-16 15:20:36 -07006947 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
6948 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
6949 tw32(GRC_MISC_CFG, val);
6950
Michael Chan1ee582d2005-08-09 20:16:46 -07006951 /* restore 5701 hardware bug workaround write method */
6952 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006953
6954 /* Unfortunately, we have to delay before the PCI read back.
6955 * Some 575X chips even will not respond to a PCI cfg access
6956 * when the reset command is given to the chip.
6957 *
6958 * How do these hardware designers expect things to work
6959 * properly if the PCI write is posted for a long period
6960 * of time? It is always necessary to have some method by
6961 * which a register read back can occur to push the write
6962 * out which does the reset.
6963 *
6964 * For most tg3 variants the trick below was working.
6965 * Ho hum...
6966 */
6967 udelay(120);
6968
6969 /* Flush PCI posted writes. The normal MMIO registers
6970 * are inaccessible at this time so this is the only
6971 * way to make this reliably (actually, this is no longer
6972 * the case, see above). I tried to use indirect
6973 * register read/write but this upset some 5701 variants.
6974 */
6975 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
6976
6977 udelay(120);
6978
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006979 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) && tp->pcie_cap) {
Matt Carlsone7126992009-08-25 10:08:16 +00006980 u16 val16;
6981
Linus Torvalds1da177e2005-04-16 15:20:36 -07006982 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
6983 int i;
6984 u32 cfg_val;
6985
6986 /* Wait for link training to complete. */
6987 for (i = 0; i < 5000; i++)
6988 udelay(100);
6989
6990 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
6991 pci_write_config_dword(tp->pdev, 0xc4,
6992 cfg_val | (1 << 15));
6993 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006994
Matt Carlsone7126992009-08-25 10:08:16 +00006995 /* Clear the "no snoop" and "relaxed ordering" bits. */
6996 pci_read_config_word(tp->pdev,
6997 tp->pcie_cap + PCI_EXP_DEVCTL,
6998 &val16);
6999 val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
7000 PCI_EXP_DEVCTL_NOSNOOP_EN);
7001 /*
7002 * Older PCIe devices only support the 128 byte
7003 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007004 */
Matt Carlson6de34cb2010-08-02 11:25:55 +00007005 if (!(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT))
Matt Carlsone7126992009-08-25 10:08:16 +00007006 val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007007 pci_write_config_word(tp->pdev,
7008 tp->pcie_cap + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007009 val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007010
7011 pcie_set_readrq(tp->pdev, 4096);
7012
7013 /* Clear error status */
7014 pci_write_config_word(tp->pdev,
7015 tp->pcie_cap + PCI_EXP_DEVSTA,
7016 PCI_EXP_DEVSTA_CED |
7017 PCI_EXP_DEVSTA_NFED |
7018 PCI_EXP_DEVSTA_FED |
7019 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007020 }
7021
Michael Chanee6a99b2007-07-18 21:49:10 -07007022 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007023
Michael Chand18edcb2007-03-24 20:57:11 -07007024 tp->tg3_flags &= ~TG3_FLAG_CHIP_RESETTING;
7025
Michael Chanee6a99b2007-07-18 21:49:10 -07007026 val = 0;
7027 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
Michael Chan4cf78e42005-07-25 12:29:19 -07007028 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07007029 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007030
7031 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
7032 tg3_stop_fw(tp);
7033 tw32(0x5000, 0x400);
7034 }
7035
7036 tw32(GRC_MODE, tp->grc_mode);
7037
7038 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007039 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007040
7041 tw32(0xc4, val | (1 << 15));
7042 }
7043
7044 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
7045 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
7046 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
7047 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
7048 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
7049 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7050 }
7051
7052 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
7053 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
7054 tw32_f(MAC_MODE, tp->mac_mode);
Michael Chan747e8f82005-07-25 12:33:22 -07007055 } else if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES) {
7056 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
7057 tw32_f(MAC_MODE, tp->mac_mode);
Matt Carlson3bda1252008-08-15 14:08:22 -07007058 } else if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
7059 tp->mac_mode &= (MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN);
7060 if (tp->mac_mode & MAC_MODE_APE_TX_EN)
7061 tp->mac_mode |= MAC_MODE_TDE_ENABLE;
7062 tw32_f(MAC_MODE, tp->mac_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007063 } else
7064 tw32_f(MAC_MODE, 0);
7065 udelay(40);
7066
Matt Carlson77b483f2008-08-15 14:07:24 -07007067 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
7068
Michael Chan7a6f4362006-09-27 16:03:31 -07007069 err = tg3_poll_fw(tp);
7070 if (err)
7071 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007072
Matt Carlson0a9140c2009-08-28 12:27:50 +00007073 tg3_mdio_start(tp);
7074
Linus Torvalds1da177e2005-04-16 15:20:36 -07007075 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007076 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
7077 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonc885e822010-08-02 11:25:57 +00007078 !(tp->tg3_flags3 & TG3_FLG3_5717_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007079 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007080
7081 tw32(0x7c00, val | (1 << 25));
7082 }
7083
7084 /* Reprobe ASF enable state. */
7085 tp->tg3_flags &= ~TG3_FLAG_ENABLE_ASF;
7086 tp->tg3_flags2 &= ~TG3_FLG2_ASF_NEW_HANDSHAKE;
7087 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
7088 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
7089 u32 nic_cfg;
7090
7091 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
7092 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
7093 tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
Matt Carlson4ba526c2008-08-15 14:10:04 -07007094 tp->last_event_jiffies = jiffies;
John W. Linvillecbf46852005-04-21 17:01:29 -07007095 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007096 tp->tg3_flags2 |= TG3_FLG2_ASF_NEW_HANDSHAKE;
7097 }
7098 }
7099
7100 return 0;
7101}
7102
7103/* tp->lock is held. */
7104static void tg3_stop_fw(struct tg3 *tp)
7105{
Matt Carlson0d3031d2007-10-10 18:02:43 -07007106 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
7107 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07007108 /* Wait for RX cpu to ACK the previous event. */
7109 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007110
7111 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007112
7113 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007114
Matt Carlson7c5026a2008-05-02 16:49:29 -07007115 /* Wait for RX cpu to ACK this event. */
7116 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007117 }
7118}
7119
7120/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07007121static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007122{
7123 int err;
7124
7125 tg3_stop_fw(tp);
7126
Michael Chan944d9802005-05-29 14:57:48 -07007127 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007128
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007129 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007130 err = tg3_chip_reset(tp);
7131
Matt Carlsondaba2a62009-04-20 06:58:52 +00007132 __tg3_set_mac_addr(tp, 0);
7133
Michael Chan944d9802005-05-29 14:57:48 -07007134 tg3_write_sig_legacy(tp, kind);
7135 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007136
7137 if (err)
7138 return err;
7139
7140 return 0;
7141}
7142
Linus Torvalds1da177e2005-04-16 15:20:36 -07007143#define RX_CPU_SCRATCH_BASE 0x30000
7144#define RX_CPU_SCRATCH_SIZE 0x04000
7145#define TX_CPU_SCRATCH_BASE 0x34000
7146#define TX_CPU_SCRATCH_SIZE 0x04000
7147
7148/* tp->lock is held. */
7149static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
7150{
7151 int i;
7152
Eric Sesterhenn5d9428d2006-04-02 13:52:48 +02007153 BUG_ON(offset == TX_CPU_BASE &&
7154 (tp->tg3_flags2 & TG3_FLG2_5705_PLUS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007155
Michael Chanb5d37722006-09-27 16:06:21 -07007156 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7157 u32 val = tr32(GRC_VCPU_EXT_CTRL);
7158
7159 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
7160 return 0;
7161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007162 if (offset == RX_CPU_BASE) {
7163 for (i = 0; i < 10000; i++) {
7164 tw32(offset + CPU_STATE, 0xffffffff);
7165 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7166 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7167 break;
7168 }
7169
7170 tw32(offset + CPU_STATE, 0xffffffff);
7171 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
7172 udelay(10);
7173 } else {
7174 for (i = 0; i < 10000; i++) {
7175 tw32(offset + CPU_STATE, 0xffffffff);
7176 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7177 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7178 break;
7179 }
7180 }
7181
7182 if (i >= 10000) {
Joe Perches05dbe002010-02-17 19:44:19 +00007183 netdev_err(tp->dev, "%s timed out, %s CPU\n",
7184 __func__, offset == RX_CPU_BASE ? "RX" : "TX");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007185 return -ENODEV;
7186 }
Michael Chanec41c7d2006-01-17 02:40:55 -08007187
7188 /* Clear firmware's nvram arbitration. */
7189 if (tp->tg3_flags & TG3_FLAG_NVRAM)
7190 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007191 return 0;
7192}
7193
7194struct fw_info {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007195 unsigned int fw_base;
7196 unsigned int fw_len;
7197 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007198};
7199
7200/* tp->lock is held. */
7201static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base, u32 cpu_scratch_base,
7202 int cpu_scratch_size, struct fw_info *info)
7203{
Michael Chanec41c7d2006-01-17 02:40:55 -08007204 int err, lock_err, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007205 void (*write_op)(struct tg3 *, u32, u32);
7206
7207 if (cpu_base == TX_CPU_BASE &&
7208 (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007209 netdev_err(tp->dev,
7210 "%s: Trying to load TX cpu firmware which is 5705\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007211 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007212 return -EINVAL;
7213 }
7214
7215 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
7216 write_op = tg3_write_mem;
7217 else
7218 write_op = tg3_write_indirect_reg32;
7219
Michael Chan1b628152005-05-29 14:59:49 -07007220 /* It is possible that bootcode is still loading at this point.
7221 * Get the nvram lock first before halting the cpu.
7222 */
Michael Chanec41c7d2006-01-17 02:40:55 -08007223 lock_err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007224 err = tg3_halt_cpu(tp, cpu_base);
Michael Chanec41c7d2006-01-17 02:40:55 -08007225 if (!lock_err)
7226 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007227 if (err)
7228 goto out;
7229
7230 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
7231 write_op(tp, cpu_scratch_base + i, 0);
7232 tw32(cpu_base + CPU_STATE, 0xffffffff);
7233 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007234 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007235 write_op(tp, (cpu_scratch_base +
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007236 (info->fw_base & 0xffff) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07007237 (i * sizeof(u32))),
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007238 be32_to_cpu(info->fw_data[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007239
7240 err = 0;
7241
7242out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007243 return err;
7244}
7245
7246/* tp->lock is held. */
7247static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
7248{
7249 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007250 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007251 int err, i;
7252
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007253 fw_data = (void *)tp->fw->data;
7254
7255 /* Firmware blob starts with version numbers, followed by
7256 start address and length. We are setting complete length.
7257 length = end_address_of_bss - start_address_of_text.
7258 Remainder is the blob to be loaded contiguously
7259 from start address. */
7260
7261 info.fw_base = be32_to_cpu(fw_data[1]);
7262 info.fw_len = tp->fw->size - 12;
7263 info.fw_data = &fw_data[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007264
7265 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
7266 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
7267 &info);
7268 if (err)
7269 return err;
7270
7271 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
7272 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
7273 &info);
7274 if (err)
7275 return err;
7276
7277 /* Now startup only the RX cpu. */
7278 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007279 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007280
7281 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007282 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007283 break;
7284 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7285 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007286 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007287 udelay(1000);
7288 }
7289 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007290 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
7291 "should be %08x\n", __func__,
Joe Perches05dbe002010-02-17 19:44:19 +00007292 tr32(RX_CPU_BASE + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007293 return -ENODEV;
7294 }
7295 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7296 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
7297
7298 return 0;
7299}
7300
Linus Torvalds1da177e2005-04-16 15:20:36 -07007301/* 5705 needs a special version of the TSO firmware. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007302
7303/* tp->lock is held. */
7304static int tg3_load_tso_firmware(struct tg3 *tp)
7305{
7306 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007307 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007308 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
7309 int err, i;
7310
7311 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
7312 return 0;
7313
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007314 fw_data = (void *)tp->fw->data;
7315
7316 /* Firmware blob starts with version numbers, followed by
7317 start address and length. We are setting complete length.
7318 length = end_address_of_bss - start_address_of_text.
7319 Remainder is the blob to be loaded contiguously
7320 from start address. */
7321
7322 info.fw_base = be32_to_cpu(fw_data[1]);
7323 cpu_scratch_size = tp->fw_len;
7324 info.fw_len = tp->fw->size - 12;
7325 info.fw_data = &fw_data[3];
7326
Linus Torvalds1da177e2005-04-16 15:20:36 -07007327 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007328 cpu_base = RX_CPU_BASE;
7329 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007330 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007331 cpu_base = TX_CPU_BASE;
7332 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
7333 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
7334 }
7335
7336 err = tg3_load_firmware_cpu(tp, cpu_base,
7337 cpu_scratch_base, cpu_scratch_size,
7338 &info);
7339 if (err)
7340 return err;
7341
7342 /* Now startup the cpu. */
7343 tw32(cpu_base + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007344 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007345
7346 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007347 if (tr32(cpu_base + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007348 break;
7349 tw32(cpu_base + CPU_STATE, 0xffffffff);
7350 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007351 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007352 udelay(1000);
7353 }
7354 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007355 netdev_err(tp->dev,
7356 "%s fails to set CPU PC, is %08x should be %08x\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007357 __func__, tr32(cpu_base + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007358 return -ENODEV;
7359 }
7360 tw32(cpu_base + CPU_STATE, 0xffffffff);
7361 tw32_f(cpu_base + CPU_MODE, 0x00000000);
7362 return 0;
7363}
7364
Linus Torvalds1da177e2005-04-16 15:20:36 -07007365
Linus Torvalds1da177e2005-04-16 15:20:36 -07007366static int tg3_set_mac_addr(struct net_device *dev, void *p)
7367{
7368 struct tg3 *tp = netdev_priv(dev);
7369 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07007370 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007371
Michael Chanf9804dd2005-09-27 12:13:10 -07007372 if (!is_valid_ether_addr(addr->sa_data))
7373 return -EINVAL;
7374
Linus Torvalds1da177e2005-04-16 15:20:36 -07007375 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7376
Michael Chane75f7c92006-03-20 21:33:26 -08007377 if (!netif_running(dev))
7378 return 0;
7379
Michael Chan58712ef2006-04-29 18:58:01 -07007380 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) {
Michael Chan986e0ae2007-05-05 12:10:20 -07007381 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07007382
Michael Chan986e0ae2007-05-05 12:10:20 -07007383 addr0_high = tr32(MAC_ADDR_0_HIGH);
7384 addr0_low = tr32(MAC_ADDR_0_LOW);
7385 addr1_high = tr32(MAC_ADDR_1_HIGH);
7386 addr1_low = tr32(MAC_ADDR_1_LOW);
7387
7388 /* Skip MAC addr 1 if ASF is using it. */
7389 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
7390 !(addr1_high == 0 && addr1_low == 0))
7391 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07007392 }
Michael Chan986e0ae2007-05-05 12:10:20 -07007393 spin_lock_bh(&tp->lock);
7394 __tg3_set_mac_addr(tp, skip_mac_1);
7395 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007396
Michael Chanb9ec6c12006-07-25 16:37:27 -07007397 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007398}
7399
7400/* tp->lock is held. */
7401static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
7402 dma_addr_t mapping, u32 maxlen_flags,
7403 u32 nic_addr)
7404{
7405 tg3_write_mem(tp,
7406 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
7407 ((u64) mapping >> 32));
7408 tg3_write_mem(tp,
7409 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
7410 ((u64) mapping & 0xffffffff));
7411 tg3_write_mem(tp,
7412 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
7413 maxlen_flags);
7414
7415 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7416 tg3_write_mem(tp,
7417 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
7418 nic_addr);
7419}
7420
7421static void __tg3_set_rx_mode(struct net_device *);
Michael Chand244c892005-07-05 14:42:33 -07007422static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07007423{
Matt Carlsonb6080e12009-09-01 13:12:00 +00007424 int i;
7425
Matt Carlson19cfaec2009-12-03 08:36:20 +00007426 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007427 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
7428 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
7429 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007430 } else {
7431 tw32(HOSTCC_TXCOL_TICKS, 0);
7432 tw32(HOSTCC_TXMAX_FRAMES, 0);
7433 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007434 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007435
Matt Carlson20d73752010-07-11 09:31:41 +00007436 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00007437 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
7438 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
7439 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
7440 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007441 tw32(HOSTCC_RXCOL_TICKS, 0);
7442 tw32(HOSTCC_RXMAX_FRAMES, 0);
7443 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07007444 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007445
David S. Miller15f98502005-05-18 22:49:26 -07007446 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7447 u32 val = ec->stats_block_coalesce_usecs;
7448
Matt Carlsonb6080e12009-09-01 13:12:00 +00007449 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
7450 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
7451
David S. Miller15f98502005-05-18 22:49:26 -07007452 if (!netif_carrier_ok(tp->dev))
7453 val = 0;
7454
7455 tw32(HOSTCC_STAT_COAL_TICKS, val);
7456 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007457
7458 for (i = 0; i < tp->irq_cnt - 1; i++) {
7459 u32 reg;
7460
7461 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
7462 tw32(reg, ec->rx_coalesce_usecs);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007463 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
7464 tw32(reg, ec->rx_max_coalesced_frames);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007465 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
7466 tw32(reg, ec->rx_max_coalesced_frames_irq);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007467
7468 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS) {
7469 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
7470 tw32(reg, ec->tx_coalesce_usecs);
7471 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
7472 tw32(reg, ec->tx_max_coalesced_frames);
7473 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
7474 tw32(reg, ec->tx_max_coalesced_frames_irq);
7475 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007476 }
7477
7478 for (; i < tp->irq_max - 1; i++) {
7479 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007480 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007481 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007482
7483 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS) {
7484 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
7485 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
7486 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
7487 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007488 }
David S. Miller15f98502005-05-18 22:49:26 -07007489}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007490
7491/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00007492static void tg3_rings_reset(struct tg3 *tp)
7493{
7494 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007495 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007496 struct tg3_napi *tnapi = &tp->napi[0];
7497
7498 /* Disable all transmit rings but the first. */
7499 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7500 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00007501 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
7502 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007503 else
7504 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7505
7506 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7507 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
7508 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
7509 BDINFO_FLAGS_DISABLED);
7510
7511
7512 /* Disable all receive return rings but the first. */
Matt Carlsona50d0792010-06-05 17:24:37 +00007513 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
7514 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007515 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
7516 else if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00007517 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00007518 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
7519 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson2d31eca2009-09-01 12:53:31 +00007520 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
7521 else
7522 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7523
7524 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7525 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
7526 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
7527 BDINFO_FLAGS_DISABLED);
7528
7529 /* Disable interrupts */
7530 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
7531
7532 /* Zero mailbox registers. */
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007533 if (tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX) {
7534 for (i = 1; i < TG3_IRQ_MAX_VECS; i++) {
7535 tp->napi[i].tx_prod = 0;
7536 tp->napi[i].tx_cons = 0;
Matt Carlsonc2353a32010-01-20 16:58:08 +00007537 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
7538 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007539 tw32_rx_mbox(tp->napi[i].consmbox, 0);
7540 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
7541 }
Matt Carlsonc2353a32010-01-20 16:58:08 +00007542 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS))
7543 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007544 } else {
7545 tp->napi[0].tx_prod = 0;
7546 tp->napi[0].tx_cons = 0;
7547 tw32_mailbox(tp->napi[0].prodmbox, 0);
7548 tw32_rx_mbox(tp->napi[0].consmbox, 0);
7549 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007550
7551 /* Make sure the NIC-based send BD rings are disabled. */
7552 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7553 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
7554 for (i = 0; i < 16; i++)
7555 tw32_tx_mbox(mbox + i * 8, 0);
7556 }
7557
7558 txrcb = NIC_SRAM_SEND_RCB;
7559 rxrcb = NIC_SRAM_RCV_RET_RCB;
7560
7561 /* Clear status block in ram. */
7562 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7563
7564 /* Set status block DMA address */
7565 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
7566 ((u64) tnapi->status_mapping >> 32));
7567 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
7568 ((u64) tnapi->status_mapping & 0xffffffff));
7569
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007570 if (tnapi->tx_ring) {
7571 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7572 (TG3_TX_RING_SIZE <<
7573 BDINFO_FLAGS_MAXLEN_SHIFT),
7574 NIC_SRAM_TX_BUFFER_DESC);
7575 txrcb += TG3_BDINFO_SIZE;
7576 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007577
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007578 if (tnapi->rx_rcb) {
7579 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
7580 (TG3_RX_RCB_RING_SIZE(tp) <<
7581 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
7582 rxrcb += TG3_BDINFO_SIZE;
7583 }
7584
7585 stblk = HOSTCC_STATBLCK_RING1;
7586
7587 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
7588 u64 mapping = (u64)tnapi->status_mapping;
7589 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
7590 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
7591
7592 /* Clear status block in ram. */
7593 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7594
Matt Carlson19cfaec2009-12-03 08:36:20 +00007595 if (tnapi->tx_ring) {
7596 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7597 (TG3_TX_RING_SIZE <<
7598 BDINFO_FLAGS_MAXLEN_SHIFT),
7599 NIC_SRAM_TX_BUFFER_DESC);
7600 txrcb += TG3_BDINFO_SIZE;
7601 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007602
7603 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
7604 (TG3_RX_RCB_RING_SIZE(tp) <<
7605 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
7606
7607 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007608 rxrcb += TG3_BDINFO_SIZE;
7609 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007610}
7611
7612/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07007613static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007614{
7615 u32 val, rdmac_mode;
7616 int i, err, limit;
Matt Carlson21f581a2009-08-28 14:00:25 +00007617 struct tg3_rx_prodring_set *tpr = &tp->prodring[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007618
7619 tg3_disable_ints(tp);
7620
7621 tg3_stop_fw(tp);
7622
7623 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
7624
Matt Carlson859a588792010-04-05 10:19:28 +00007625 if (tp->tg3_flags & TG3_FLAG_INIT_COMPLETE)
Michael Chane6de8ad2005-05-05 14:42:41 -07007626 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007627
Matt Carlson603f1172010-02-12 14:47:10 +00007628 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08007629 tg3_phy_reset(tp);
7630
Linus Torvalds1da177e2005-04-16 15:20:36 -07007631 err = tg3_chip_reset(tp);
7632 if (err)
7633 return err;
7634
7635 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
7636
Matt Carlsonbcb37f62008-11-03 16:52:09 -08007637 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07007638 val = tr32(TG3_CPMU_CTRL);
7639 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
7640 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08007641
7642 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
7643 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
7644 val |= CPMU_LSPD_10MB_MACCLK_6_25;
7645 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
7646
7647 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
7648 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
7649 val |= CPMU_LNK_AWARE_MACCLK_6_25;
7650 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
7651
7652 val = tr32(TG3_CPMU_HST_ACC);
7653 val &= ~CPMU_HST_ACC_MACCLK_MASK;
7654 val |= CPMU_HST_ACC_MACCLK_6_25;
7655 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07007656 }
7657
Matt Carlson33466d92009-04-20 06:57:41 +00007658 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7659 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
7660 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
7661 PCIE_PWR_MGMT_L1_THRESH_4MS;
7662 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00007663
7664 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
7665 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
7666
7667 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00007668
Matt Carlsonf40386c2009-11-02 14:24:02 +00007669 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7670 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00007671 }
7672
Matt Carlson614b0592010-01-20 16:58:02 +00007673 if (tp->tg3_flags3 & TG3_FLG3_L1PLLPD_EN) {
7674 u32 grc_mode = tr32(GRC_MODE);
7675
7676 /* Access the lower 1K of PL PCIE block registers. */
7677 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
7678 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
7679
7680 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
7681 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
7682 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
7683
7684 tw32(GRC_MODE, grc_mode);
7685 }
7686
Matt Carlsoncea46462010-04-12 06:58:24 +00007687 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
7688 u32 grc_mode = tr32(GRC_MODE);
7689
7690 /* Access the lower 1K of PL PCIE block registers. */
7691 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
7692 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
7693
7694 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5);
7695 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
7696 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
7697
7698 tw32(GRC_MODE, grc_mode);
Matt Carlsona977dbe2010-04-12 06:58:26 +00007699
7700 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
7701 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
7702 val |= CPMU_LSPD_10MB_MACCLK_6_25;
7703 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00007704 }
7705
Linus Torvalds1da177e2005-04-16 15:20:36 -07007706 /* This works around an issue with Athlon chipsets on
7707 * B3 tigon3 silicon. This bit has no effect on any
7708 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07007709 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007710 */
Matt Carlson795d01c2007-10-07 23:28:17 -07007711 if (!(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT)) {
7712 if (!(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
7713 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
7714 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007716
7717 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
7718 (tp->tg3_flags & TG3_FLAG_PCIX_MODE)) {
7719 val = tr32(TG3PCI_PCISTATE);
7720 val |= PCISTATE_RETRY_SAME_DMA;
7721 tw32(TG3PCI_PCISTATE, val);
7722 }
7723
Matt Carlson0d3031d2007-10-10 18:02:43 -07007724 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
7725 /* Allow reads and writes to the
7726 * APE register and memory space.
7727 */
7728 val = tr32(TG3PCI_PCISTATE);
7729 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00007730 PCISTATE_ALLOW_APE_SHMEM_WR |
7731 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07007732 tw32(TG3PCI_PCISTATE, val);
7733 }
7734
Linus Torvalds1da177e2005-04-16 15:20:36 -07007735 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
7736 /* Enable some hw fixes. */
7737 val = tr32(TG3PCI_MSI_DATA);
7738 val |= (1 << 26) | (1 << 28) | (1 << 29);
7739 tw32(TG3PCI_MSI_DATA, val);
7740 }
7741
7742 /* Descriptor ring init may make accesses to the
7743 * NIC SRAM area to setup the TX descriptors, so we
7744 * can only do this after the hardware has been
7745 * successfully reset.
7746 */
Michael Chan32d8c572006-07-25 16:38:29 -07007747 err = tg3_init_rings(tp);
7748 if (err)
7749 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007750
Matt Carlsonc885e822010-08-02 11:25:57 +00007751 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00007752 val = tr32(TG3PCI_DMA_RW_CTRL) &
7753 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Matt Carlson1a319022010-04-12 06:58:25 +00007754 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
7755 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00007756 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
7757 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
7758 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07007759 /* This value is determined during the probe time DMA
7760 * engine test, tg3_test_dma.
7761 */
7762 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
7763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007764
7765 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
7766 GRC_MODE_4X_NIC_SEND_RINGS |
7767 GRC_MODE_NO_TX_PHDR_CSUM |
7768 GRC_MODE_NO_RX_PHDR_CSUM);
7769 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07007770
7771 /* Pseudo-header checksum is done by hardware logic and not
7772 * the offload processers, so make the chip do the pseudo-
7773 * header checksums on receive. For transmit it is more
7774 * convenient to do the pseudo-header checksum in software
7775 * as Linux does that on transmit for us in all cases.
7776 */
7777 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007778
7779 tw32(GRC_MODE,
7780 tp->grc_mode |
7781 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
7782
7783 /* Setup the timer prescalar register. Clock is always 66Mhz. */
7784 val = tr32(GRC_MISC_CFG);
7785 val &= ~0xff;
7786 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
7787 tw32(GRC_MISC_CFG, val);
7788
7789 /* Initialize MBUF/DESC pool. */
John W. Linvillecbf46852005-04-21 17:01:29 -07007790 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007791 /* Do nothing. */
7792 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
7793 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
7794 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
7795 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
7796 else
7797 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
7798 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
7799 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Matt Carlson859a588792010-04-05 10:19:28 +00007800 } else if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007801 int fw_len;
7802
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007803 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007804 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
7805 tw32(BUFMGR_MB_POOL_ADDR,
7806 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
7807 tw32(BUFMGR_MB_POOL_SIZE,
7808 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
7809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007810
Michael Chan0f893dc2005-07-25 12:30:38 -07007811 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007812 tw32(BUFMGR_MB_RDMA_LOW_WATER,
7813 tp->bufmgr_config.mbuf_read_dma_low_water);
7814 tw32(BUFMGR_MB_MACRX_LOW_WATER,
7815 tp->bufmgr_config.mbuf_mac_rx_low_water);
7816 tw32(BUFMGR_MB_HIGH_WATER,
7817 tp->bufmgr_config.mbuf_high_water);
7818 } else {
7819 tw32(BUFMGR_MB_RDMA_LOW_WATER,
7820 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
7821 tw32(BUFMGR_MB_MACRX_LOW_WATER,
7822 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
7823 tw32(BUFMGR_MB_HIGH_WATER,
7824 tp->bufmgr_config.mbuf_high_water_jumbo);
7825 }
7826 tw32(BUFMGR_DMA_LOW_WATER,
7827 tp->bufmgr_config.dma_low_water);
7828 tw32(BUFMGR_DMA_HIGH_WATER,
7829 tp->bufmgr_config.dma_high_water);
7830
7831 tw32(BUFMGR_MODE, BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE);
7832 for (i = 0; i < 2000; i++) {
7833 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
7834 break;
7835 udelay(10);
7836 }
7837 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00007838 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007839 return -ENODEV;
7840 }
7841
7842 /* Setup replenish threshold. */
Michael Chanf92905d2006-06-29 20:14:29 -07007843 val = tp->rx_pending / 8;
7844 if (val == 0)
7845 val = 1;
7846 else if (val > tp->rx_std_max_post)
7847 val = tp->rx_std_max_post;
Michael Chanb5d37722006-09-27 16:06:21 -07007848 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7849 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
7850 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
7851
7852 if (val > (TG3_RX_INTERNAL_RING_SZ_5906 / 2))
7853 val = TG3_RX_INTERNAL_RING_SZ_5906 / 2;
7854 }
Michael Chanf92905d2006-06-29 20:14:29 -07007855
7856 tw32(RCVBDI_STD_THRESH, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007857
7858 /* Initialize TG3_BDINFO's at:
7859 * RCVDBDI_STD_BD: standard eth size rx ring
7860 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
7861 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
7862 *
7863 * like so:
7864 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
7865 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
7866 * ring attribute flags
7867 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
7868 *
7869 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
7870 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
7871 *
7872 * The size of each ring is fixed in the firmware, but the location is
7873 * configurable.
7874 */
7875 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00007876 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007877 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00007878 ((u64) tpr->rx_std_mapping & 0xffffffff));
Matt Carlsona50d0792010-06-05 17:24:37 +00007879 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
7880 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5719)
Matt Carlson87668d32009-11-13 13:03:34 +00007881 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
7882 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007883
Matt Carlsonfdb72b32009-08-28 13:57:12 +00007884 /* Disable the mini ring */
7885 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007886 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
7887 BDINFO_FLAGS_DISABLED);
7888
Matt Carlsonfdb72b32009-08-28 13:57:12 +00007889 /* Program the jumbo buffer descriptor ring control
7890 * blocks on those devices that have them.
7891 */
Matt Carlson8f666b02009-08-28 13:58:24 +00007892 if ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) &&
Matt Carlsonfdb72b32009-08-28 13:57:12 +00007893 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007894 /* Setup replenish threshold. */
7895 tw32(RCVBDI_JUMBO_THRESH, tp->rx_jumbo_pending / 8);
7896
Michael Chan0f893dc2005-07-25 12:30:38 -07007897 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007898 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00007899 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007900 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00007901 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007902 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlson79ed5ac2009-08-28 14:00:55 +00007903 (RX_JUMBO_MAX_SIZE << BDINFO_FLAGS_MAXLEN_SHIFT) |
7904 BDINFO_FLAGS_USE_EXT_RECV);
Matt Carlsona50d0792010-06-05 17:24:37 +00007905 if (!(tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) ||
7906 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson87668d32009-11-13 13:03:34 +00007907 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
7908 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007909 } else {
7910 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
7911 BDINFO_FLAGS_DISABLED);
7912 }
7913
Matt Carlsonc885e822010-08-02 11:25:57 +00007914 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007915 val = (RX_STD_MAX_SIZE_5705 << BDINFO_FLAGS_MAXLEN_SHIFT) |
Matt Carlson04380d42010-04-12 06:58:29 +00007916 (TG3_RX_STD_DMA_SZ << 2);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007917 else
Matt Carlson04380d42010-04-12 06:58:29 +00007918 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00007919 } else
7920 val = RX_STD_MAX_SIZE_5705 << BDINFO_FLAGS_MAXLEN_SHIFT;
7921
7922 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007923
Matt Carlson411da642009-11-13 13:03:46 +00007924 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e62009-11-13 13:03:49 +00007925 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007926
Matt Carlson411da642009-11-13 13:03:46 +00007927 tpr->rx_jmb_prod_idx = (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) ?
Matt Carlson21f581a2009-08-28 14:00:25 +00007928 tp->rx_jumbo_pending : 0;
Matt Carlson66711e62009-11-13 13:03:49 +00007929 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007930
Matt Carlsonc885e822010-08-02 11:25:57 +00007931 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007932 tw32(STD_REPLENISH_LWM, 32);
7933 tw32(JMB_REPLENISH_LWM, 16);
7934 }
7935
Matt Carlson2d31eca2009-09-01 12:53:31 +00007936 tg3_rings_reset(tp);
7937
Linus Torvalds1da177e2005-04-16 15:20:36 -07007938 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07007939 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007940
7941 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00007942 tw32(MAC_RX_MTU_SIZE,
7943 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007944
7945 /* The slot time is changed by tg3_setup_phy if we
7946 * run at gigabit with half duplex.
7947 */
7948 tw32(MAC_TX_LENGTHS,
7949 (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
7950 (6 << TX_LENGTHS_IPG_SHIFT) |
7951 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
7952
7953 /* Receive rules. */
7954 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
7955 tw32(RCVLPC_CONFIG, 0x0181);
7956
7957 /* Calculate RDMAC_MODE setting early, we need it to determine
7958 * the RCVLPC_STATE_ENABLE mask.
7959 */
7960 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
7961 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
7962 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
7963 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
7964 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07007965
Matt Carlsona50d0792010-06-05 17:24:37 +00007966 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
7967 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlson0339e4e2010-02-12 14:47:09 +00007968 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
7969
Matt Carlson57e69832008-05-25 23:48:31 -07007970 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08007971 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
7972 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07007973 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
7974 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
7975 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
7976
Michael Chan85e94ce2005-04-21 17:05:28 -07007977 /* If statement applies to 5705 and 5750 PCI devices only */
7978 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
7979 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) ||
7980 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007981 if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07007982 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007983 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
7984 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
7985 !(tp->tg3_flags2 & TG3_FLG2_IS_5788)) {
7986 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
7987 }
7988 }
7989
Michael Chan85e94ce2005-04-21 17:05:28 -07007990 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)
7991 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
7992
Linus Torvalds1da177e2005-04-16 15:20:36 -07007993 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
Matt Carlson027455a2008-12-21 20:19:30 -08007994 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
7995
Matt Carlsone849cdc2009-11-13 13:03:38 +00007996 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
7997 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson027455a2008-12-21 20:19:30 -08007998 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
7999 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008000
8001 /* Receive/send statistics. */
Michael Chan16613942006-06-29 20:15:13 -07008002 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
8003 val = tr32(RCVLPC_STATS_ENABLE);
8004 val &= ~RCVLPC_STATSENAB_DACK_FIX;
8005 tw32(RCVLPC_STATS_ENABLE, val);
8006 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
8007 (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008008 val = tr32(RCVLPC_STATS_ENABLE);
8009 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
8010 tw32(RCVLPC_STATS_ENABLE, val);
8011 } else {
8012 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
8013 }
8014 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
8015 tw32(SNDDATAI_STATSENAB, 0xffffff);
8016 tw32(SNDDATAI_STATSCTRL,
8017 (SNDDATAI_SCTRL_ENABLE |
8018 SNDDATAI_SCTRL_FASTUPD));
8019
8020 /* Setup host coalescing engine. */
8021 tw32(HOSTCC_MODE, 0);
8022 for (i = 0; i < 2000; i++) {
8023 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
8024 break;
8025 udelay(10);
8026 }
8027
Michael Chand244c892005-07-05 14:42:33 -07008028 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008029
Linus Torvalds1da177e2005-04-16 15:20:36 -07008030 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
8031 /* Status/statistics block address. See tg3_timer,
8032 * the tg3_periodic_fetch_stats call there, and
8033 * tg3_get_stats to see how this works for 5705/5750 chips.
8034 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008035 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8036 ((u64) tp->stats_mapping >> 32));
8037 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8038 ((u64) tp->stats_mapping & 0xffffffff));
8039 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008040
Linus Torvalds1da177e2005-04-16 15:20:36 -07008041 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008042
8043 /* Clear statistics and status block memory areas */
8044 for (i = NIC_SRAM_STATS_BLK;
8045 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
8046 i += sizeof(u32)) {
8047 tg3_write_mem(tp, i, 0);
8048 udelay(40);
8049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008050 }
8051
8052 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
8053
8054 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
8055 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
8056 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
8057 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
8058
Michael Chanc94e3942005-09-27 12:12:42 -07008059 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES) {
8060 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
8061 /* reset to prevent losing 1st rx packet intermittently */
8062 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8063 udelay(10);
8064 }
8065
Matt Carlson3bda1252008-08-15 14:08:22 -07008066 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
8067 tp->mac_mode &= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
8068 else
8069 tp->mac_mode = 0;
8070 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Linus Torvalds1da177e2005-04-16 15:20:36 -07008071 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE | MAC_MODE_FHDE_ENABLE;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07008072 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
8073 !(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
8074 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
8075 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008076 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
8077 udelay(40);
8078
Michael Chan314fba32005-04-21 17:07:04 -07008079 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Michael Chan9d26e212006-12-07 00:21:14 -08008080 * If TG3_FLG2_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07008081 * register to preserve the GPIO settings for LOMs. The GPIOs,
8082 * whether used as inputs or outputs, are set by boot code after
8083 * reset.
8084 */
Michael Chan9d26e212006-12-07 00:21:14 -08008085 if (!(tp->tg3_flags2 & TG3_FLG2_IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07008086 u32 gpio_mask;
8087
Michael Chan9d26e212006-12-07 00:21:14 -08008088 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
8089 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
8090 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07008091
8092 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
8093 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
8094 GRC_LCLCTRL_GPIO_OUTPUT3;
8095
Michael Chanaf36e6b2006-03-23 01:28:06 -08008096 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
8097 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
8098
Gary Zambranoaaf84462007-05-05 11:51:45 -07008099 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07008100 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
8101
8102 /* GPIO1 must be driven high for eeprom write protect */
Michael Chan9d26e212006-12-07 00:21:14 -08008103 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT)
8104 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
8105 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07008106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008107 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8108 udelay(100);
8109
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008110 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX) {
8111 val = tr32(MSGINT_MODE);
8112 val |= MSGINT_MODE_MULTIVEC_EN | MSGINT_MODE_ENABLE;
8113 tw32(MSGINT_MODE, val);
8114 }
8115
Linus Torvalds1da177e2005-04-16 15:20:36 -07008116 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
8117 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
8118 udelay(40);
8119 }
8120
8121 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
8122 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
8123 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
8124 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
8125 WDMAC_MODE_LNGREAD_ENAB);
8126
Michael Chan85e94ce2005-04-21 17:05:28 -07008127 /* If statement applies to 5705 and 5750 PCI devices only */
8128 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8129 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) ||
8130 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750) {
Matt Carlson29ea0952009-08-25 10:07:54 +00008131 if ((tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008132 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
8133 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
8134 /* nothing */
8135 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
8136 !(tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
8137 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)) {
8138 val |= WDMAC_MODE_RX_ACCEL;
8139 }
8140 }
8141
Michael Chand9ab5ad2006-03-20 22:27:35 -08008142 /* Enable host coalescing bug fix */
Matt Carlson321d32a2008-11-21 17:22:19 -08008143 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Matt Carlsonf51f3562008-05-25 23:45:08 -07008144 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad2006-03-20 22:27:35 -08008145
Matt Carlson788a0352009-11-02 14:26:03 +00008146 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
8147 val |= WDMAC_MODE_BURST_ALL_DATA;
8148
Linus Torvalds1da177e2005-04-16 15:20:36 -07008149 tw32_f(WDMAC_MODE, val);
8150 udelay(40);
8151
Matt Carlson9974a352007-10-07 23:27:28 -07008152 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
8153 u16 pcix_cmd;
8154
8155 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8156 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008157 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07008158 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
8159 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008160 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07008161 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
8162 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008163 }
Matt Carlson9974a352007-10-07 23:27:28 -07008164 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8165 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008166 }
8167
8168 tw32_f(RDMAC_MODE, rdmac_mode);
8169 udelay(40);
8170
8171 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
8172 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
8173 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07008174
8175 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
8176 tw32(SNDDATAC_MODE,
8177 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
8178 else
8179 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
8180
Linus Torvalds1da177e2005-04-16 15:20:36 -07008181 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
8182 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
8183 tw32(RCVDBDI_MODE, RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ);
8184 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008185 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
8186 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008187 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Matt Carlson19cfaec2009-12-03 08:36:20 +00008188 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008189 val |= SNDBDI_MODE_MULTI_TXQ_EN;
8190 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008191 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
8192
8193 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
8194 err = tg3_load_5701_a0_firmware_fix(tp);
8195 if (err)
8196 return err;
8197 }
8198
Linus Torvalds1da177e2005-04-16 15:20:36 -07008199 if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) {
8200 err = tg3_load_tso_firmware(tp);
8201 if (err)
8202 return err;
8203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008204
8205 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonb1d05212010-06-05 17:24:31 +00008206 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
8207 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
8208 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008209 tw32_f(MAC_TX_MODE, tp->tx_mode);
8210 udelay(100);
8211
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008212 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS) {
8213 u32 reg = MAC_RSS_INDIR_TBL_0;
8214 u8 *ent = (u8 *)&val;
8215
8216 /* Setup the indirection table */
8217 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
8218 int idx = i % sizeof(val);
8219
Matt Carlson5efeeea2010-07-11 09:31:40 +00008220 ent[idx] = i % (tp->irq_cnt - 1);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008221 if (idx == sizeof(val) - 1) {
8222 tw32(reg, val);
8223 reg += 4;
8224 }
8225 }
8226
8227 /* Setup the "secret" hash key. */
8228 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
8229 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
8230 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
8231 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
8232 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
8233 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
8234 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
8235 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
8236 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
8237 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
8238 }
8239
Linus Torvalds1da177e2005-04-16 15:20:36 -07008240 tp->rx_mode = RX_MODE_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08008241 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Michael Chanaf36e6b2006-03-23 01:28:06 -08008242 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
8243
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008244 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)
8245 tp->rx_mode |= RX_MODE_RSS_ENABLE |
8246 RX_MODE_RSS_ITBL_HASH_BITS_7 |
8247 RX_MODE_RSS_IPV6_HASH_EN |
8248 RX_MODE_RSS_TCP_IPV6_HASH_EN |
8249 RX_MODE_RSS_IPV4_HASH_EN |
8250 RX_MODE_RSS_TCP_IPV4_HASH_EN;
8251
Linus Torvalds1da177e2005-04-16 15:20:36 -07008252 tw32_f(MAC_RX_MODE, tp->rx_mode);
8253 udelay(10);
8254
Linus Torvalds1da177e2005-04-16 15:20:36 -07008255 tw32(MAC_LED_CTRL, tp->led_ctrl);
8256
8257 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Michael Chanc94e3942005-09-27 12:12:42 -07008258 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008259 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8260 udelay(10);
8261 }
8262 tw32_f(MAC_RX_MODE, tp->rx_mode);
8263 udelay(10);
8264
8265 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
8266 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
8267 !(tp->tg3_flags2 & TG3_FLG2_SERDES_PREEMPHASIS)) {
8268 /* Set drive transmission level to 1.2V */
8269 /* only if the signal pre-emphasis bit is not set */
8270 val = tr32(MAC_SERDES_CFG);
8271 val &= 0xfffff000;
8272 val |= 0x880;
8273 tw32(MAC_SERDES_CFG, val);
8274 }
8275 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
8276 tw32(MAC_SERDES_CFG, 0x616000);
8277 }
8278
8279 /* Prevent chip from dropping frames when flow control
8280 * is enabled.
8281 */
Matt Carlson666bc832010-01-20 16:58:03 +00008282 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8283 val = 1;
8284 else
8285 val = 2;
8286 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008287
8288 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
8289 (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)) {
8290 /* Use hardware link auto-negotiation */
8291 tp->tg3_flags2 |= TG3_FLG2_HW_AUTONEG;
8292 }
8293
Michael Chand4d2c552006-03-20 17:47:20 -08008294 if ((tp->tg3_flags2 & TG3_FLG2_MII_SERDES) &&
8295 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)) {
8296 u32 tmp;
8297
8298 tmp = tr32(SERDES_RX_CTRL);
8299 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
8300 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
8301 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
8302 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8303 }
8304
Matt Carlsondd477002008-05-25 23:45:58 -07008305 if (!(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)) {
8306 if (tp->link_config.phy_is_low_power) {
8307 tp->link_config.phy_is_low_power = 0;
8308 tp->link_config.speed = tp->link_config.orig_speed;
8309 tp->link_config.duplex = tp->link_config.orig_duplex;
8310 tp->link_config.autoneg = tp->link_config.orig_autoneg;
8311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008312
Matt Carlsondd477002008-05-25 23:45:58 -07008313 err = tg3_setup_phy(tp, 0);
8314 if (err)
8315 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008316
Matt Carlsondd477002008-05-25 23:45:58 -07008317 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
Matt Carlson7f97a4b2009-08-25 10:10:03 +00008318 !(tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07008319 u32 tmp;
8320
8321 /* Clear CRC stats. */
8322 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
8323 tg3_writephy(tp, MII_TG3_TEST1,
8324 tmp | MII_TG3_TEST1_CRC_EN);
8325 tg3_readphy(tp, 0x14, &tmp);
8326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008327 }
8328 }
8329
8330 __tg3_set_rx_mode(tp->dev);
8331
8332 /* Initialize receive rules. */
8333 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
8334 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
8335 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
8336 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
8337
Michael Chan4cf78e42005-07-25 12:29:19 -07008338 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Michael Chana4e2b342005-10-26 15:46:52 -07008339 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008340 limit = 8;
8341 else
8342 limit = 16;
8343 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF)
8344 limit -= 4;
8345 switch (limit) {
8346 case 16:
8347 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
8348 case 15:
8349 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
8350 case 14:
8351 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
8352 case 13:
8353 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
8354 case 12:
8355 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
8356 case 11:
8357 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
8358 case 10:
8359 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
8360 case 9:
8361 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
8362 case 8:
8363 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
8364 case 7:
8365 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
8366 case 6:
8367 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
8368 case 5:
8369 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
8370 case 4:
8371 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
8372 case 3:
8373 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
8374 case 2:
8375 case 1:
8376
8377 default:
8378 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07008379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008380
Matt Carlson9ce768e2007-10-11 19:49:11 -07008381 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
8382 /* Write our heartbeat update interval to APE. */
8383 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
8384 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07008385
Linus Torvalds1da177e2005-04-16 15:20:36 -07008386 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
8387
Linus Torvalds1da177e2005-04-16 15:20:36 -07008388 return 0;
8389}
8390
8391/* Called at device open time to get the chip ready for
8392 * packet processing. Invoked with tp->lock held.
8393 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008394static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008395{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008396 tg3_switch_clocks(tp);
8397
8398 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
8399
Matt Carlson2f751b62008-08-04 23:17:34 -07008400 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008401}
8402
8403#define TG3_STAT_ADD32(PSTAT, REG) \
8404do { u32 __val = tr32(REG); \
8405 (PSTAT)->low += __val; \
8406 if ((PSTAT)->low < __val) \
8407 (PSTAT)->high += 1; \
8408} while (0)
8409
8410static void tg3_periodic_fetch_stats(struct tg3 *tp)
8411{
8412 struct tg3_hw_stats *sp = tp->hw_stats;
8413
8414 if (!netif_carrier_ok(tp->dev))
8415 return;
8416
8417 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
8418 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
8419 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
8420 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
8421 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
8422 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
8423 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
8424 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
8425 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
8426 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
8427 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
8428 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
8429 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
8430
8431 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
8432 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
8433 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
8434 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
8435 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
8436 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
8437 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
8438 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
8439 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
8440 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
8441 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
8442 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
8443 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
8444 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07008445
8446 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
8447 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
8448 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008449}
8450
8451static void tg3_timer(unsigned long __opaque)
8452{
8453 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008454
Michael Chanf475f162006-03-27 23:20:14 -08008455 if (tp->irq_sync)
8456 goto restart_timer;
8457
David S. Millerf47c11e2005-06-24 20:18:35 -07008458 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008459
David S. Millerfac9b832005-05-18 22:46:34 -07008460 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)) {
8461 /* All of this garbage is because when using non-tagged
8462 * IRQ status the mailbox/status_block protocol the chip
8463 * uses with the cpu is race prone.
8464 */
Matt Carlson898a56f2009-08-28 14:02:40 +00008465 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -07008466 tw32(GRC_LOCAL_CTRL,
8467 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
8468 } else {
8469 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00008470 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -07008471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008472
David S. Millerfac9b832005-05-18 22:46:34 -07008473 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
8474 tp->tg3_flags2 |= TG3_FLG2_RESTART_TIMER;
David S. Millerf47c11e2005-06-24 20:18:35 -07008475 spin_unlock(&tp->lock);
David S. Millerfac9b832005-05-18 22:46:34 -07008476 schedule_work(&tp->reset_task);
8477 return;
8478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008479 }
8480
Linus Torvalds1da177e2005-04-16 15:20:36 -07008481 /* This part only runs once per second. */
8482 if (!--tp->timer_counter) {
David S. Millerfac9b832005-05-18 22:46:34 -07008483 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
8484 tg3_periodic_fetch_stats(tp);
8485
Linus Torvalds1da177e2005-04-16 15:20:36 -07008486 if (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) {
8487 u32 mac_stat;
8488 int phy_event;
8489
8490 mac_stat = tr32(MAC_STATUS);
8491
8492 phy_event = 0;
8493 if (tp->tg3_flags & TG3_FLAG_USE_MI_INTERRUPT) {
8494 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
8495 phy_event = 1;
8496 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
8497 phy_event = 1;
8498
8499 if (phy_event)
8500 tg3_setup_phy(tp, 0);
8501 } else if (tp->tg3_flags & TG3_FLAG_POLL_SERDES) {
8502 u32 mac_stat = tr32(MAC_STATUS);
8503 int need_setup = 0;
8504
8505 if (netif_carrier_ok(tp->dev) &&
8506 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
8507 need_setup = 1;
8508 }
Matt Carlsonbe98da62010-07-11 09:31:46 +00008509 if (!netif_carrier_ok(tp->dev) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008510 (mac_stat & (MAC_STATUS_PCS_SYNCED |
8511 MAC_STATUS_SIGNAL_DET))) {
8512 need_setup = 1;
8513 }
8514 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07008515 if (!tp->serdes_counter) {
8516 tw32_f(MAC_MODE,
8517 (tp->mac_mode &
8518 ~MAC_MODE_PORT_MODE_MASK));
8519 udelay(40);
8520 tw32_f(MAC_MODE, tp->mac_mode);
8521 udelay(40);
8522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008523 tg3_setup_phy(tp, 0);
8524 }
Matt Carlson57d8b882010-06-05 17:24:35 +00008525 } else if ((tp->tg3_flags2 & TG3_FLG2_MII_SERDES) &&
Matt Carlson2138c002010-07-11 09:31:43 +00008526 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -07008527 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +00008528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008529
8530 tp->timer_counter = tp->timer_multiplier;
8531 }
8532
Michael Chan130b8e42006-09-27 16:00:40 -07008533 /* Heartbeat is only sent once every 2 seconds.
8534 *
8535 * The heartbeat is to tell the ASF firmware that the host
8536 * driver is still alive. In the event that the OS crashes,
8537 * ASF needs to reset the hardware to free up the FIFO space
8538 * that may be filled with rx packets destined for the host.
8539 * If the FIFO is full, ASF will no longer function properly.
8540 *
8541 * Unintended resets have been reported on real time kernels
8542 * where the timer doesn't run on time. Netpoll will also have
8543 * same problem.
8544 *
8545 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
8546 * to check the ring condition when the heartbeat is expiring
8547 * before doing the reset. This will prevent most unintended
8548 * resets.
8549 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008550 if (!--tp->asf_counter) {
Matt Carlsonbc7959b2008-08-15 14:08:55 -07008551 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
8552 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07008553 tg3_wait_for_event_ack(tp);
8554
Michael Chanbbadf502006-04-06 21:46:34 -07008555 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07008556 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07008557 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +00008558 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
8559 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -07008560
8561 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008562 }
8563 tp->asf_counter = tp->asf_multiplier;
8564 }
8565
David S. Millerf47c11e2005-06-24 20:18:35 -07008566 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008567
Michael Chanf475f162006-03-27 23:20:14 -08008568restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07008569 tp->timer.expires = jiffies + tp->timer_offset;
8570 add_timer(&tp->timer);
8571}
8572
Matt Carlson4f125f42009-09-01 12:55:02 +00008573static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -08008574{
David Howells7d12e782006-10-05 14:55:46 +01008575 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008576 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +00008577 char *name;
8578 struct tg3_napi *tnapi = &tp->napi[irq_num];
8579
8580 if (tp->irq_cnt == 1)
8581 name = tp->dev->name;
8582 else {
8583 name = &tnapi->irq_lbl[0];
8584 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
8585 name[IFNAMSIZ-1] = 0;
8586 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08008587
Matt Carlson679563f2009-09-01 12:55:46 +00008588 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI_OR_MSIX) {
Michael Chanfcfa0a32006-03-20 22:28:41 -08008589 fn = tg3_msi;
8590 if (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)
8591 fn = tg3_msi_1shot;
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07008592 flags = IRQF_SAMPLE_RANDOM;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008593 } else {
8594 fn = tg3_interrupt;
8595 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)
8596 fn = tg3_interrupt_tagged;
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07008597 flags = IRQF_SHARED | IRQF_SAMPLE_RANDOM;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008598 }
Matt Carlson4f125f42009-09-01 12:55:02 +00008599
8600 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08008601}
8602
Michael Chan79381092005-04-21 17:13:59 -07008603static int tg3_test_interrupt(struct tg3 *tp)
8604{
Matt Carlson09943a12009-08-28 14:01:57 +00008605 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -07008606 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -07008607 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008608 u32 val;
Michael Chan79381092005-04-21 17:13:59 -07008609
Michael Chand4bc3922005-05-29 14:59:20 -07008610 if (!netif_running(dev))
8611 return -ENODEV;
8612
Michael Chan79381092005-04-21 17:13:59 -07008613 tg3_disable_ints(tp);
8614
Matt Carlson4f125f42009-09-01 12:55:02 +00008615 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07008616
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008617 /*
8618 * Turn off MSI one shot mode. Otherwise this test has no
8619 * observable way to know whether the interrupt was delivered.
8620 */
Matt Carlsonc885e822010-08-02 11:25:57 +00008621 if ((tp->tg3_flags3 & TG3_FLG3_5717_PLUS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008622 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
8623 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
8624 tw32(MSGINT_MODE, val);
8625 }
8626
Matt Carlson4f125f42009-09-01 12:55:02 +00008627 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Matt Carlson09943a12009-08-28 14:01:57 +00008628 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07008629 if (err)
8630 return err;
8631
Matt Carlson898a56f2009-08-28 14:02:40 +00008632 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -07008633 tg3_enable_ints(tp);
8634
8635 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00008636 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -07008637
8638 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -07008639 u32 int_mbox, misc_host_ctrl;
8640
Matt Carlson898a56f2009-08-28 14:02:40 +00008641 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -07008642 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
8643
8644 if ((int_mbox != 0) ||
8645 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
8646 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -07008647 break;
Michael Chanb16250e2006-09-27 16:10:14 -07008648 }
8649
Michael Chan79381092005-04-21 17:13:59 -07008650 msleep(10);
8651 }
8652
8653 tg3_disable_ints(tp);
8654
Matt Carlson4f125f42009-09-01 12:55:02 +00008655 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008656
Matt Carlson4f125f42009-09-01 12:55:02 +00008657 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07008658
8659 if (err)
8660 return err;
8661
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008662 if (intr_ok) {
8663 /* Reenable MSI one shot mode. */
Matt Carlsonc885e822010-08-02 11:25:57 +00008664 if ((tp->tg3_flags3 & TG3_FLG3_5717_PLUS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008665 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
8666 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
8667 tw32(MSGINT_MODE, val);
8668 }
Michael Chan79381092005-04-21 17:13:59 -07008669 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008670 }
Michael Chan79381092005-04-21 17:13:59 -07008671
8672 return -EIO;
8673}
8674
8675/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
8676 * successfully restored
8677 */
8678static int tg3_test_msi(struct tg3 *tp)
8679{
Michael Chan79381092005-04-21 17:13:59 -07008680 int err;
8681 u16 pci_cmd;
8682
8683 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSI))
8684 return 0;
8685
8686 /* Turn off SERR reporting in case MSI terminates with Master
8687 * Abort.
8688 */
8689 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
8690 pci_write_config_word(tp->pdev, PCI_COMMAND,
8691 pci_cmd & ~PCI_COMMAND_SERR);
8692
8693 err = tg3_test_interrupt(tp);
8694
8695 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
8696
8697 if (!err)
8698 return 0;
8699
8700 /* other failures */
8701 if (err != -EIO)
8702 return err;
8703
8704 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +00008705 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
8706 "to INTx mode. Please report this failure to the PCI "
8707 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -07008708
Matt Carlson4f125f42009-09-01 12:55:02 +00008709 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +00008710
Michael Chan79381092005-04-21 17:13:59 -07008711 pci_disable_msi(tp->pdev);
8712
8713 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI;
Andre Detschdc8bf1b2010-04-26 07:27:07 +00008714 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -07008715
Matt Carlson4f125f42009-09-01 12:55:02 +00008716 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07008717 if (err)
8718 return err;
8719
8720 /* Need to reset the chip because the MSI cycle may have terminated
8721 * with Master Abort.
8722 */
David S. Millerf47c11e2005-06-24 20:18:35 -07008723 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07008724
Michael Chan944d9802005-05-29 14:57:48 -07008725 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008726 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07008727
David S. Millerf47c11e2005-06-24 20:18:35 -07008728 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07008729
8730 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +00008731 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -07008732
8733 return err;
8734}
8735
Matt Carlson9e9fd122009-01-19 16:57:45 -08008736static int tg3_request_firmware(struct tg3 *tp)
8737{
8738 const __be32 *fw_data;
8739
8740 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00008741 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
8742 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08008743 return -ENOENT;
8744 }
8745
8746 fw_data = (void *)tp->fw->data;
8747
8748 /* Firmware blob starts with version numbers, followed by
8749 * start address and _full_ length including BSS sections
8750 * (which must be longer than the actual data, of course
8751 */
8752
8753 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
8754 if (tp->fw_len < (tp->fw->size - 12)) {
Joe Perches05dbe002010-02-17 19:44:19 +00008755 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
8756 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08008757 release_firmware(tp->fw);
8758 tp->fw = NULL;
8759 return -EINVAL;
8760 }
8761
8762 /* We no longer need firmware; we have it. */
8763 tp->fw_needed = NULL;
8764 return 0;
8765}
8766
Matt Carlson679563f2009-09-01 12:55:46 +00008767static bool tg3_enable_msix(struct tg3 *tp)
8768{
8769 int i, rc, cpus = num_online_cpus();
8770 struct msix_entry msix_ent[tp->irq_max];
8771
8772 if (cpus == 1)
8773 /* Just fallback to the simpler MSI mode. */
8774 return false;
8775
8776 /*
8777 * We want as many rx rings enabled as there are cpus.
8778 * The first MSIX vector only deals with link interrupts, etc,
8779 * so we add one to the number of vectors we are requesting.
8780 */
8781 tp->irq_cnt = min_t(unsigned, cpus + 1, tp->irq_max);
8782
8783 for (i = 0; i < tp->irq_max; i++) {
8784 msix_ent[i].entry = i;
8785 msix_ent[i].vector = 0;
8786 }
8787
8788 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +00008789 if (rc < 0) {
8790 return false;
8791 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +00008792 if (pci_enable_msix(tp->pdev, msix_ent, rc))
8793 return false;
Joe Perches05dbe002010-02-17 19:44:19 +00008794 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
8795 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +00008796 tp->irq_cnt = rc;
8797 }
8798
8799 for (i = 0; i < tp->irq_max; i++)
8800 tp->napi[i].irq_vec = msix_ent[i].vector;
8801
Matt Carlson2430b032010-06-05 17:24:34 +00008802 tp->dev->real_num_tx_queues = 1;
8803 if (tp->irq_cnt > 1) {
8804 tp->tg3_flags3 |= TG3_FLG3_ENABLE_RSS;
8805
Matt Carlsona50d0792010-06-05 17:24:37 +00008806 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
8807 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) {
Matt Carlson2430b032010-06-05 17:24:34 +00008808 tp->tg3_flags3 |= TG3_FLG3_ENABLE_TSS;
8809 tp->dev->real_num_tx_queues = tp->irq_cnt - 1;
8810 }
8811 }
Matt Carlsonfe5f5782009-09-01 13:09:39 +00008812
Matt Carlson679563f2009-09-01 12:55:46 +00008813 return true;
8814}
8815
Matt Carlson07b01732009-08-28 14:01:15 +00008816static void tg3_ints_init(struct tg3 *tp)
8817{
Matt Carlson679563f2009-09-01 12:55:46 +00008818 if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSI_OR_MSIX) &&
8819 !(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +00008820 /* All MSI supporting chips should support tagged
8821 * status. Assert that this is the case.
8822 */
Matt Carlson5129c3a2010-04-05 10:19:23 +00008823 netdev_warn(tp->dev,
8824 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +00008825 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +00008826 }
Matt Carlson4f125f42009-09-01 12:55:02 +00008827
Matt Carlson679563f2009-09-01 12:55:46 +00008828 if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX) && tg3_enable_msix(tp))
8829 tp->tg3_flags2 |= TG3_FLG2_USING_MSIX;
8830 else if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSI) &&
8831 pci_enable_msi(tp->pdev) == 0)
8832 tp->tg3_flags2 |= TG3_FLG2_USING_MSI;
8833
8834 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI_OR_MSIX) {
8835 u32 msi_mode = tr32(MSGINT_MODE);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008836 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
8837 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson679563f2009-09-01 12:55:46 +00008838 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
8839 }
8840defcfg:
8841 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSIX)) {
8842 tp->irq_cnt = 1;
8843 tp->napi[0].irq_vec = tp->pdev->irq;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00008844 tp->dev->real_num_tx_queues = 1;
Matt Carlson679563f2009-09-01 12:55:46 +00008845 }
Matt Carlson07b01732009-08-28 14:01:15 +00008846}
8847
8848static void tg3_ints_fini(struct tg3 *tp)
8849{
Matt Carlson679563f2009-09-01 12:55:46 +00008850 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
8851 pci_disable_msix(tp->pdev);
8852 else if (tp->tg3_flags2 & TG3_FLG2_USING_MSI)
8853 pci_disable_msi(tp->pdev);
8854 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI_OR_MSIX;
Matt Carlson774ee752010-08-02 11:25:56 +00008855 tp->tg3_flags3 &= ~(TG3_FLG3_ENABLE_RSS | TG3_FLG3_ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +00008856}
8857
Linus Torvalds1da177e2005-04-16 15:20:36 -07008858static int tg3_open(struct net_device *dev)
8859{
8860 struct tg3 *tp = netdev_priv(dev);
Matt Carlson4f125f42009-09-01 12:55:02 +00008861 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008862
Matt Carlson9e9fd122009-01-19 16:57:45 -08008863 if (tp->fw_needed) {
8864 err = tg3_request_firmware(tp);
8865 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
8866 if (err)
8867 return err;
8868 } else if (err) {
Joe Perches05dbe002010-02-17 19:44:19 +00008869 netdev_warn(tp->dev, "TSO capability disabled\n");
Matt Carlson9e9fd122009-01-19 16:57:45 -08008870 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
8871 } else if (!(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
Joe Perches05dbe002010-02-17 19:44:19 +00008872 netdev_notice(tp->dev, "TSO capability restored\n");
Matt Carlson9e9fd122009-01-19 16:57:45 -08008873 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
8874 }
8875 }
8876
Michael Chanc49a1562006-12-17 17:07:29 -08008877 netif_carrier_off(tp->dev);
8878
Michael Chanbc1c7562006-03-20 17:48:03 -08008879 err = tg3_set_power_state(tp, PCI_D0);
Matt Carlson2f751b62008-08-04 23:17:34 -07008880 if (err)
Michael Chanbc1c7562006-03-20 17:48:03 -08008881 return err;
Matt Carlson2f751b62008-08-04 23:17:34 -07008882
8883 tg3_full_lock(tp, 0);
Michael Chanbc1c7562006-03-20 17:48:03 -08008884
Linus Torvalds1da177e2005-04-16 15:20:36 -07008885 tg3_disable_ints(tp);
8886 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
8887
David S. Millerf47c11e2005-06-24 20:18:35 -07008888 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008889
Matt Carlson679563f2009-09-01 12:55:46 +00008890 /*
8891 * Setup interrupts first so we know how
8892 * many NAPI resources to allocate
8893 */
8894 tg3_ints_init(tp);
8895
Linus Torvalds1da177e2005-04-16 15:20:36 -07008896 /* The placement of this call is tied
8897 * to the setup and use of Host TX descriptors.
8898 */
8899 err = tg3_alloc_consistent(tp);
8900 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00008901 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008902
Matt Carlsonfed97812009-09-01 13:10:19 +00008903 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07008904
Matt Carlson4f125f42009-09-01 12:55:02 +00008905 for (i = 0; i < tp->irq_cnt; i++) {
8906 struct tg3_napi *tnapi = &tp->napi[i];
8907 err = tg3_request_irq(tp, i);
8908 if (err) {
8909 for (i--; i >= 0; i--)
8910 free_irq(tnapi->irq_vec, tnapi);
8911 break;
8912 }
8913 }
Matt Carlson07b01732009-08-28 14:01:15 +00008914
8915 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00008916 goto err_out2;
Matt Carlson07b01732009-08-28 14:01:15 +00008917
David S. Millerf47c11e2005-06-24 20:18:35 -07008918 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008919
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008920 err = tg3_init_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008921 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -07008922 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008923 tg3_free_rings(tp);
8924 } else {
David S. Millerfac9b832005-05-18 22:46:34 -07008925 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)
8926 tp->timer_offset = HZ;
8927 else
8928 tp->timer_offset = HZ / 10;
8929
8930 BUG_ON(tp->timer_offset > HZ);
8931 tp->timer_counter = tp->timer_multiplier =
8932 (HZ / tp->timer_offset);
8933 tp->asf_counter = tp->asf_multiplier =
Michael Chan28fbef72005-10-26 15:48:35 -07008934 ((HZ / tp->timer_offset) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008935
8936 init_timer(&tp->timer);
8937 tp->timer.expires = jiffies + tp->timer_offset;
8938 tp->timer.data = (unsigned long) tp;
8939 tp->timer.function = tg3_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008940 }
8941
David S. Millerf47c11e2005-06-24 20:18:35 -07008942 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008943
Matt Carlson07b01732009-08-28 14:01:15 +00008944 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00008945 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008946
Michael Chan79381092005-04-21 17:13:59 -07008947 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
8948 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -07008949
Michael Chan79381092005-04-21 17:13:59 -07008950 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -07008951 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -07008952 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -07008953 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07008954 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07008955
Matt Carlson679563f2009-09-01 12:55:46 +00008956 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -07008957 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08008958
Matt Carlsonc885e822010-08-02 11:25:57 +00008959 if (!(tp->tg3_flags3 & TG3_FLG3_5717_PLUS) &&
8960 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008961 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -08008962
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008963 tw32(PCIE_TRANSACTION_CFG,
8964 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -08008965 }
Michael Chan79381092005-04-21 17:13:59 -07008966 }
8967
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07008968 tg3_phy_start(tp);
8969
David S. Millerf47c11e2005-06-24 20:18:35 -07008970 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008971
Michael Chan79381092005-04-21 17:13:59 -07008972 add_timer(&tp->timer);
8973 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008974 tg3_enable_ints(tp);
8975
David S. Millerf47c11e2005-06-24 20:18:35 -07008976 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008977
Matt Carlsonfe5f5782009-09-01 13:09:39 +00008978 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008979
8980 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +00008981
Matt Carlson679563f2009-09-01 12:55:46 +00008982err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +00008983 for (i = tp->irq_cnt - 1; i >= 0; i--) {
8984 struct tg3_napi *tnapi = &tp->napi[i];
8985 free_irq(tnapi->irq_vec, tnapi);
8986 }
Matt Carlson07b01732009-08-28 14:01:15 +00008987
Matt Carlson679563f2009-09-01 12:55:46 +00008988err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +00008989 tg3_napi_disable(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00008990 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +00008991
8992err_out1:
8993 tg3_ints_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00008994 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008995}
8996
Eric Dumazet511d2222010-07-07 20:44:24 +00008997static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *,
8998 struct rtnl_link_stats64 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008999static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *);
9000
9001static int tg3_close(struct net_device *dev)
9002{
Matt Carlson4f125f42009-09-01 12:55:02 +00009003 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009004 struct tg3 *tp = netdev_priv(dev);
9005
Matt Carlsonfed97812009-09-01 13:10:19 +00009006 tg3_napi_disable(tp);
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07009007 cancel_work_sync(&tp->reset_task);
Michael Chan7faa0062006-02-02 17:29:28 -08009008
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009009 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009010
9011 del_timer_sync(&tp->timer);
9012
Matt Carlson24bb4fb2009-10-05 17:55:29 +00009013 tg3_phy_stop(tp);
9014
David S. Millerf47c11e2005-06-24 20:18:35 -07009015 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009016
9017 tg3_disable_ints(tp);
9018
Michael Chan944d9802005-05-29 14:57:48 -07009019 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009020 tg3_free_rings(tp);
Michael Chan5cf64b8a2007-05-05 12:11:21 -07009021 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009022
David S. Millerf47c11e2005-06-24 20:18:35 -07009023 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009024
Matt Carlson4f125f42009-09-01 12:55:02 +00009025 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9026 struct tg3_napi *tnapi = &tp->napi[i];
9027 free_irq(tnapi->irq_vec, tnapi);
9028 }
Matt Carlson07b01732009-08-28 14:01:15 +00009029
9030 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009031
Eric Dumazet511d2222010-07-07 20:44:24 +00009032 tg3_get_stats64(tp->dev, &tp->net_stats_prev);
9033
Linus Torvalds1da177e2005-04-16 15:20:36 -07009034 memcpy(&tp->estats_prev, tg3_get_estats(tp),
9035 sizeof(tp->estats_prev));
9036
9037 tg3_free_consistent(tp);
9038
Michael Chanbc1c7562006-03-20 17:48:03 -08009039 tg3_set_power_state(tp, PCI_D3hot);
9040
9041 netif_carrier_off(tp->dev);
9042
Linus Torvalds1da177e2005-04-16 15:20:36 -07009043 return 0;
9044}
9045
Eric Dumazet511d2222010-07-07 20:44:24 +00009046static inline u64 get_stat64(tg3_stat64_t *val)
Stefan Buehler816f8b82008-08-15 14:10:54 -07009047{
9048 return ((u64)val->high << 32) | ((u64)val->low);
9049}
9050
Eric Dumazet511d2222010-07-07 20:44:24 +00009051static u64 calc_crc_errors(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009052{
9053 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9054
9055 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
9056 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
9057 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009058 u32 val;
9059
David S. Millerf47c11e2005-06-24 20:18:35 -07009060 spin_lock_bh(&tp->lock);
Michael Chan569a5df2007-02-13 12:18:15 -08009061 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
9062 tg3_writephy(tp, MII_TG3_TEST1,
9063 val | MII_TG3_TEST1_CRC_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009064 tg3_readphy(tp, 0x14, &val);
9065 } else
9066 val = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07009067 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009068
9069 tp->phy_crc_errors += val;
9070
9071 return tp->phy_crc_errors;
9072 }
9073
9074 return get_stat64(&hw_stats->rx_fcs_errors);
9075}
9076
9077#define ESTAT_ADD(member) \
9078 estats->member = old_estats->member + \
Eric Dumazet511d2222010-07-07 20:44:24 +00009079 get_stat64(&hw_stats->member)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009080
9081static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *tp)
9082{
9083 struct tg3_ethtool_stats *estats = &tp->estats;
9084 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
9085 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9086
9087 if (!hw_stats)
9088 return old_estats;
9089
9090 ESTAT_ADD(rx_octets);
9091 ESTAT_ADD(rx_fragments);
9092 ESTAT_ADD(rx_ucast_packets);
9093 ESTAT_ADD(rx_mcast_packets);
9094 ESTAT_ADD(rx_bcast_packets);
9095 ESTAT_ADD(rx_fcs_errors);
9096 ESTAT_ADD(rx_align_errors);
9097 ESTAT_ADD(rx_xon_pause_rcvd);
9098 ESTAT_ADD(rx_xoff_pause_rcvd);
9099 ESTAT_ADD(rx_mac_ctrl_rcvd);
9100 ESTAT_ADD(rx_xoff_entered);
9101 ESTAT_ADD(rx_frame_too_long_errors);
9102 ESTAT_ADD(rx_jabbers);
9103 ESTAT_ADD(rx_undersize_packets);
9104 ESTAT_ADD(rx_in_length_errors);
9105 ESTAT_ADD(rx_out_length_errors);
9106 ESTAT_ADD(rx_64_or_less_octet_packets);
9107 ESTAT_ADD(rx_65_to_127_octet_packets);
9108 ESTAT_ADD(rx_128_to_255_octet_packets);
9109 ESTAT_ADD(rx_256_to_511_octet_packets);
9110 ESTAT_ADD(rx_512_to_1023_octet_packets);
9111 ESTAT_ADD(rx_1024_to_1522_octet_packets);
9112 ESTAT_ADD(rx_1523_to_2047_octet_packets);
9113 ESTAT_ADD(rx_2048_to_4095_octet_packets);
9114 ESTAT_ADD(rx_4096_to_8191_octet_packets);
9115 ESTAT_ADD(rx_8192_to_9022_octet_packets);
9116
9117 ESTAT_ADD(tx_octets);
9118 ESTAT_ADD(tx_collisions);
9119 ESTAT_ADD(tx_xon_sent);
9120 ESTAT_ADD(tx_xoff_sent);
9121 ESTAT_ADD(tx_flow_control);
9122 ESTAT_ADD(tx_mac_errors);
9123 ESTAT_ADD(tx_single_collisions);
9124 ESTAT_ADD(tx_mult_collisions);
9125 ESTAT_ADD(tx_deferred);
9126 ESTAT_ADD(tx_excessive_collisions);
9127 ESTAT_ADD(tx_late_collisions);
9128 ESTAT_ADD(tx_collide_2times);
9129 ESTAT_ADD(tx_collide_3times);
9130 ESTAT_ADD(tx_collide_4times);
9131 ESTAT_ADD(tx_collide_5times);
9132 ESTAT_ADD(tx_collide_6times);
9133 ESTAT_ADD(tx_collide_7times);
9134 ESTAT_ADD(tx_collide_8times);
9135 ESTAT_ADD(tx_collide_9times);
9136 ESTAT_ADD(tx_collide_10times);
9137 ESTAT_ADD(tx_collide_11times);
9138 ESTAT_ADD(tx_collide_12times);
9139 ESTAT_ADD(tx_collide_13times);
9140 ESTAT_ADD(tx_collide_14times);
9141 ESTAT_ADD(tx_collide_15times);
9142 ESTAT_ADD(tx_ucast_packets);
9143 ESTAT_ADD(tx_mcast_packets);
9144 ESTAT_ADD(tx_bcast_packets);
9145 ESTAT_ADD(tx_carrier_sense_errors);
9146 ESTAT_ADD(tx_discards);
9147 ESTAT_ADD(tx_errors);
9148
9149 ESTAT_ADD(dma_writeq_full);
9150 ESTAT_ADD(dma_write_prioq_full);
9151 ESTAT_ADD(rxbds_empty);
9152 ESTAT_ADD(rx_discards);
9153 ESTAT_ADD(rx_errors);
9154 ESTAT_ADD(rx_threshold_hit);
9155
9156 ESTAT_ADD(dma_readq_full);
9157 ESTAT_ADD(dma_read_prioq_full);
9158 ESTAT_ADD(tx_comp_queue_full);
9159
9160 ESTAT_ADD(ring_set_send_prod_index);
9161 ESTAT_ADD(ring_status_update);
9162 ESTAT_ADD(nic_irqs);
9163 ESTAT_ADD(nic_avoided_irqs);
9164 ESTAT_ADD(nic_tx_threshold_hit);
9165
9166 return estats;
9167}
9168
Eric Dumazet511d2222010-07-07 20:44:24 +00009169static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
9170 struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009171{
9172 struct tg3 *tp = netdev_priv(dev);
Eric Dumazet511d2222010-07-07 20:44:24 +00009173 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009174 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9175
9176 if (!hw_stats)
9177 return old_stats;
9178
9179 stats->rx_packets = old_stats->rx_packets +
9180 get_stat64(&hw_stats->rx_ucast_packets) +
9181 get_stat64(&hw_stats->rx_mcast_packets) +
9182 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009183
Linus Torvalds1da177e2005-04-16 15:20:36 -07009184 stats->tx_packets = old_stats->tx_packets +
9185 get_stat64(&hw_stats->tx_ucast_packets) +
9186 get_stat64(&hw_stats->tx_mcast_packets) +
9187 get_stat64(&hw_stats->tx_bcast_packets);
9188
9189 stats->rx_bytes = old_stats->rx_bytes +
9190 get_stat64(&hw_stats->rx_octets);
9191 stats->tx_bytes = old_stats->tx_bytes +
9192 get_stat64(&hw_stats->tx_octets);
9193
9194 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -07009195 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009196 stats->tx_errors = old_stats->tx_errors +
9197 get_stat64(&hw_stats->tx_errors) +
9198 get_stat64(&hw_stats->tx_mac_errors) +
9199 get_stat64(&hw_stats->tx_carrier_sense_errors) +
9200 get_stat64(&hw_stats->tx_discards);
9201
9202 stats->multicast = old_stats->multicast +
9203 get_stat64(&hw_stats->rx_mcast_packets);
9204 stats->collisions = old_stats->collisions +
9205 get_stat64(&hw_stats->tx_collisions);
9206
9207 stats->rx_length_errors = old_stats->rx_length_errors +
9208 get_stat64(&hw_stats->rx_frame_too_long_errors) +
9209 get_stat64(&hw_stats->rx_undersize_packets);
9210
9211 stats->rx_over_errors = old_stats->rx_over_errors +
9212 get_stat64(&hw_stats->rxbds_empty);
9213 stats->rx_frame_errors = old_stats->rx_frame_errors +
9214 get_stat64(&hw_stats->rx_align_errors);
9215 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
9216 get_stat64(&hw_stats->tx_discards);
9217 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
9218 get_stat64(&hw_stats->tx_carrier_sense_errors);
9219
9220 stats->rx_crc_errors = old_stats->rx_crc_errors +
9221 calc_crc_errors(tp);
9222
John W. Linville4f63b872005-09-12 14:43:18 -07009223 stats->rx_missed_errors = old_stats->rx_missed_errors +
9224 get_stat64(&hw_stats->rx_discards);
9225
Linus Torvalds1da177e2005-04-16 15:20:36 -07009226 return stats;
9227}
9228
9229static inline u32 calc_crc(unsigned char *buf, int len)
9230{
9231 u32 reg;
9232 u32 tmp;
9233 int j, k;
9234
9235 reg = 0xffffffff;
9236
9237 for (j = 0; j < len; j++) {
9238 reg ^= buf[j];
9239
9240 for (k = 0; k < 8; k++) {
9241 tmp = reg & 0x01;
9242
9243 reg >>= 1;
9244
Matt Carlson859a588792010-04-05 10:19:28 +00009245 if (tmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009246 reg ^= 0xedb88320;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009247 }
9248 }
9249
9250 return ~reg;
9251}
9252
9253static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
9254{
9255 /* accept or reject all multicast frames */
9256 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
9257 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
9258 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
9259 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
9260}
9261
9262static void __tg3_set_rx_mode(struct net_device *dev)
9263{
9264 struct tg3 *tp = netdev_priv(dev);
9265 u32 rx_mode;
9266
9267 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
9268 RX_MODE_KEEP_VLAN_TAG);
9269
9270 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
9271 * flag clear.
9272 */
9273#if TG3_VLAN_TAG_USED
9274 if (!tp->vlgrp &&
9275 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
9276 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9277#else
9278 /* By definition, VLAN is disabled always in this
9279 * case.
9280 */
9281 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
9282 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9283#endif
9284
9285 if (dev->flags & IFF_PROMISC) {
9286 /* Promiscuous mode. */
9287 rx_mode |= RX_MODE_PROMISC;
9288 } else if (dev->flags & IFF_ALLMULTI) {
9289 /* Accept all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +00009290 tg3_set_multi(tp, 1);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00009291 } else if (netdev_mc_empty(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009292 /* Reject all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +00009293 tg3_set_multi(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009294 } else {
9295 /* Accept one or more multicast(s). */
Jiri Pirko22bedad32010-04-01 21:22:57 +00009296 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009297 u32 mc_filter[4] = { 0, };
9298 u32 regidx;
9299 u32 bit;
9300 u32 crc;
9301
Jiri Pirko22bedad32010-04-01 21:22:57 +00009302 netdev_for_each_mc_addr(ha, dev) {
9303 crc = calc_crc(ha->addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009304 bit = ~crc & 0x7f;
9305 regidx = (bit & 0x60) >> 5;
9306 bit &= 0x1f;
9307 mc_filter[regidx] |= (1 << bit);
9308 }
9309
9310 tw32(MAC_HASH_REG_0, mc_filter[0]);
9311 tw32(MAC_HASH_REG_1, mc_filter[1]);
9312 tw32(MAC_HASH_REG_2, mc_filter[2]);
9313 tw32(MAC_HASH_REG_3, mc_filter[3]);
9314 }
9315
9316 if (rx_mode != tp->rx_mode) {
9317 tp->rx_mode = rx_mode;
9318 tw32_f(MAC_RX_MODE, rx_mode);
9319 udelay(10);
9320 }
9321}
9322
9323static void tg3_set_rx_mode(struct net_device *dev)
9324{
9325 struct tg3 *tp = netdev_priv(dev);
9326
Michael Chane75f7c92006-03-20 21:33:26 -08009327 if (!netif_running(dev))
9328 return;
9329
David S. Millerf47c11e2005-06-24 20:18:35 -07009330 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009331 __tg3_set_rx_mode(dev);
David S. Millerf47c11e2005-06-24 20:18:35 -07009332 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009333}
9334
9335#define TG3_REGDUMP_LEN (32 * 1024)
9336
9337static int tg3_get_regs_len(struct net_device *dev)
9338{
9339 return TG3_REGDUMP_LEN;
9340}
9341
9342static void tg3_get_regs(struct net_device *dev,
9343 struct ethtool_regs *regs, void *_p)
9344{
9345 u32 *p = _p;
9346 struct tg3 *tp = netdev_priv(dev);
9347 u8 *orig_p = _p;
9348 int i;
9349
9350 regs->version = 0;
9351
9352 memset(p, 0, TG3_REGDUMP_LEN);
9353
Michael Chanbc1c7562006-03-20 17:48:03 -08009354 if (tp->link_config.phy_is_low_power)
9355 return;
9356
David S. Millerf47c11e2005-06-24 20:18:35 -07009357 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009358
9359#define __GET_REG32(reg) (*(p)++ = tr32(reg))
Matt Carlsonbe98da62010-07-11 09:31:46 +00009360#define GET_REG32_LOOP(base, len) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07009361do { p = (u32 *)(orig_p + (base)); \
9362 for (i = 0; i < len; i += 4) \
9363 __GET_REG32((base) + i); \
9364} while (0)
9365#define GET_REG32_1(reg) \
9366do { p = (u32 *)(orig_p + (reg)); \
9367 __GET_REG32((reg)); \
9368} while (0)
9369
9370 GET_REG32_LOOP(TG3PCI_VENDOR, 0xb0);
9371 GET_REG32_LOOP(MAILBOX_INTERRUPT_0, 0x200);
9372 GET_REG32_LOOP(MAC_MODE, 0x4f0);
9373 GET_REG32_LOOP(SNDDATAI_MODE, 0xe0);
9374 GET_REG32_1(SNDDATAC_MODE);
9375 GET_REG32_LOOP(SNDBDS_MODE, 0x80);
9376 GET_REG32_LOOP(SNDBDI_MODE, 0x48);
9377 GET_REG32_1(SNDBDC_MODE);
9378 GET_REG32_LOOP(RCVLPC_MODE, 0x20);
9379 GET_REG32_LOOP(RCVLPC_SELLST_BASE, 0x15c);
9380 GET_REG32_LOOP(RCVDBDI_MODE, 0x0c);
9381 GET_REG32_LOOP(RCVDBDI_JUMBO_BD, 0x3c);
9382 GET_REG32_LOOP(RCVDBDI_BD_PROD_IDX_0, 0x44);
9383 GET_REG32_1(RCVDCC_MODE);
9384 GET_REG32_LOOP(RCVBDI_MODE, 0x20);
9385 GET_REG32_LOOP(RCVCC_MODE, 0x14);
9386 GET_REG32_LOOP(RCVLSC_MODE, 0x08);
9387 GET_REG32_1(MBFREE_MODE);
9388 GET_REG32_LOOP(HOSTCC_MODE, 0x100);
9389 GET_REG32_LOOP(MEMARB_MODE, 0x10);
9390 GET_REG32_LOOP(BUFMGR_MODE, 0x58);
9391 GET_REG32_LOOP(RDMAC_MODE, 0x08);
9392 GET_REG32_LOOP(WDMAC_MODE, 0x08);
Chris Elmquist091465d2005-12-20 13:25:19 -08009393 GET_REG32_1(RX_CPU_MODE);
9394 GET_REG32_1(RX_CPU_STATE);
9395 GET_REG32_1(RX_CPU_PGMCTR);
9396 GET_REG32_1(RX_CPU_HWBKPT);
9397 GET_REG32_1(TX_CPU_MODE);
9398 GET_REG32_1(TX_CPU_STATE);
9399 GET_REG32_1(TX_CPU_PGMCTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009400 GET_REG32_LOOP(GRCMBOX_INTERRUPT_0, 0x110);
9401 GET_REG32_LOOP(FTQ_RESET, 0x120);
9402 GET_REG32_LOOP(MSGINT_MODE, 0x0c);
9403 GET_REG32_1(DMAC_MODE);
9404 GET_REG32_LOOP(GRC_MODE, 0x4c);
9405 if (tp->tg3_flags & TG3_FLAG_NVRAM)
9406 GET_REG32_LOOP(NVRAM_CMD, 0x24);
9407
9408#undef __GET_REG32
9409#undef GET_REG32_LOOP
9410#undef GET_REG32_1
9411
David S. Millerf47c11e2005-06-24 20:18:35 -07009412 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009413}
9414
9415static int tg3_get_eeprom_len(struct net_device *dev)
9416{
9417 struct tg3 *tp = netdev_priv(dev);
9418
9419 return tp->nvram_size;
9420}
9421
Linus Torvalds1da177e2005-04-16 15:20:36 -07009422static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9423{
9424 struct tg3 *tp = netdev_priv(dev);
9425 int ret;
9426 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -08009427 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009428 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009429
Matt Carlsondf259d82009-04-20 06:57:14 +00009430 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM)
9431 return -EINVAL;
9432
Michael Chanbc1c7562006-03-20 17:48:03 -08009433 if (tp->link_config.phy_is_low_power)
9434 return -EAGAIN;
9435
Linus Torvalds1da177e2005-04-16 15:20:36 -07009436 offset = eeprom->offset;
9437 len = eeprom->len;
9438 eeprom->len = 0;
9439
9440 eeprom->magic = TG3_EEPROM_MAGIC;
9441
9442 if (offset & 3) {
9443 /* adjustments to start on required 4 byte boundary */
9444 b_offset = offset & 3;
9445 b_count = 4 - b_offset;
9446 if (b_count > len) {
9447 /* i.e. offset=1 len=2 */
9448 b_count = len;
9449 }
Matt Carlsona9dc5292009-02-25 14:25:30 +00009450 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009451 if (ret)
9452 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +00009453 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009454 len -= b_count;
9455 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00009456 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009457 }
9458
9459 /* read bytes upto the last 4 byte boundary */
9460 pd = &data[eeprom->len];
9461 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +00009462 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009463 if (ret) {
9464 eeprom->len += i;
9465 return ret;
9466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009467 memcpy(pd + i, &val, 4);
9468 }
9469 eeprom->len += i;
9470
9471 if (len & 3) {
9472 /* read last bytes not ending on 4 byte boundary */
9473 pd = &data[eeprom->len];
9474 b_count = len & 3;
9475 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009476 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009477 if (ret)
9478 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009479 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009480 eeprom->len += b_count;
9481 }
9482 return 0;
9483}
9484
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009485static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009486
9487static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9488{
9489 struct tg3 *tp = netdev_priv(dev);
9490 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009491 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009492 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009493 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009494
Michael Chanbc1c7562006-03-20 17:48:03 -08009495 if (tp->link_config.phy_is_low_power)
9496 return -EAGAIN;
9497
Matt Carlsondf259d82009-04-20 06:57:14 +00009498 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
9499 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009500 return -EINVAL;
9501
9502 offset = eeprom->offset;
9503 len = eeprom->len;
9504
9505 if ((b_offset = (offset & 3))) {
9506 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +00009507 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009508 if (ret)
9509 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009510 len += b_offset;
9511 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -07009512 if (len < 4)
9513 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009514 }
9515
9516 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -07009517 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009518 /* adjustments to end on required 4 byte boundary */
9519 odd_len = 1;
9520 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009521 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009522 if (ret)
9523 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009524 }
9525
9526 buf = data;
9527 if (b_offset || odd_len) {
9528 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009529 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009530 return -ENOMEM;
9531 if (b_offset)
9532 memcpy(buf, &start, 4);
9533 if (odd_len)
9534 memcpy(buf+len-4, &end, 4);
9535 memcpy(buf + b_offset, data, eeprom->len);
9536 }
9537
9538 ret = tg3_nvram_write_block(tp, offset, len, buf);
9539
9540 if (buf != data)
9541 kfree(buf);
9542
9543 return ret;
9544}
9545
9546static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9547{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009548 struct tg3 *tp = netdev_priv(dev);
9549
9550 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009551 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009552 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
9553 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009554 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
9555 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009556 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009557
Linus Torvalds1da177e2005-04-16 15:20:36 -07009558 cmd->supported = (SUPPORTED_Autoneg);
9559
9560 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY))
9561 cmd->supported |= (SUPPORTED_1000baseT_Half |
9562 SUPPORTED_1000baseT_Full);
9563
Karsten Keilef348142006-05-12 12:49:08 -07009564 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009565 cmd->supported |= (SUPPORTED_100baseT_Half |
9566 SUPPORTED_100baseT_Full |
9567 SUPPORTED_10baseT_Half |
9568 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -08009569 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -07009570 cmd->port = PORT_TP;
9571 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009572 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -07009573 cmd->port = PORT_FIBRE;
9574 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009575
Linus Torvalds1da177e2005-04-16 15:20:36 -07009576 cmd->advertising = tp->link_config.advertising;
9577 if (netif_running(dev)) {
9578 cmd->speed = tp->link_config.active_speed;
9579 cmd->duplex = tp->link_config.active_duplex;
9580 }
Matt Carlson882e9792009-09-01 13:21:36 +00009581 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +00009582 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009583 cmd->autoneg = tp->link_config.autoneg;
9584 cmd->maxtxpkt = 0;
9585 cmd->maxrxpkt = 0;
9586 return 0;
9587}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009588
Linus Torvalds1da177e2005-04-16 15:20:36 -07009589static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9590{
9591 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009592
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009593 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009594 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009595 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
9596 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009597 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
9598 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009599 }
9600
Matt Carlson7e5856b2009-02-25 14:23:01 +00009601 if (cmd->autoneg != AUTONEG_ENABLE &&
9602 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -07009603 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +00009604
9605 if (cmd->autoneg == AUTONEG_DISABLE &&
9606 cmd->duplex != DUPLEX_FULL &&
9607 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -07009608 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009609
Matt Carlson7e5856b2009-02-25 14:23:01 +00009610 if (cmd->autoneg == AUTONEG_ENABLE) {
9611 u32 mask = ADVERTISED_Autoneg |
9612 ADVERTISED_Pause |
9613 ADVERTISED_Asym_Pause;
9614
Julia Lawall3f07d122010-03-13 12:22:16 -08009615 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +00009616 mask |= ADVERTISED_1000baseT_Half |
9617 ADVERTISED_1000baseT_Full;
9618
9619 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES))
9620 mask |= ADVERTISED_100baseT_Half |
9621 ADVERTISED_100baseT_Full |
9622 ADVERTISED_10baseT_Half |
9623 ADVERTISED_10baseT_Full |
9624 ADVERTISED_TP;
9625 else
9626 mask |= ADVERTISED_FIBRE;
9627
9628 if (cmd->advertising & ~mask)
9629 return -EINVAL;
9630
9631 mask &= (ADVERTISED_1000baseT_Half |
9632 ADVERTISED_1000baseT_Full |
9633 ADVERTISED_100baseT_Half |
9634 ADVERTISED_100baseT_Full |
9635 ADVERTISED_10baseT_Half |
9636 ADVERTISED_10baseT_Full);
9637
9638 cmd->advertising &= mask;
9639 } else {
9640 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES) {
9641 if (cmd->speed != SPEED_1000)
9642 return -EINVAL;
9643
9644 if (cmd->duplex != DUPLEX_FULL)
9645 return -EINVAL;
9646 } else {
9647 if (cmd->speed != SPEED_100 &&
9648 cmd->speed != SPEED_10)
9649 return -EINVAL;
9650 }
9651 }
9652
David S. Millerf47c11e2005-06-24 20:18:35 -07009653 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009654
9655 tp->link_config.autoneg = cmd->autoneg;
9656 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -07009657 tp->link_config.advertising = (cmd->advertising |
9658 ADVERTISED_Autoneg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009659 tp->link_config.speed = SPEED_INVALID;
9660 tp->link_config.duplex = DUPLEX_INVALID;
9661 } else {
9662 tp->link_config.advertising = 0;
9663 tp->link_config.speed = cmd->speed;
9664 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009665 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009666
Michael Chan24fcad62006-12-17 17:06:46 -08009667 tp->link_config.orig_speed = tp->link_config.speed;
9668 tp->link_config.orig_duplex = tp->link_config.duplex;
9669 tp->link_config.orig_autoneg = tp->link_config.autoneg;
9670
Linus Torvalds1da177e2005-04-16 15:20:36 -07009671 if (netif_running(dev))
9672 tg3_setup_phy(tp, 1);
9673
David S. Millerf47c11e2005-06-24 20:18:35 -07009674 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009675
Linus Torvalds1da177e2005-04-16 15:20:36 -07009676 return 0;
9677}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009678
Linus Torvalds1da177e2005-04-16 15:20:36 -07009679static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
9680{
9681 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009682
Linus Torvalds1da177e2005-04-16 15:20:36 -07009683 strcpy(info->driver, DRV_MODULE_NAME);
9684 strcpy(info->version, DRV_MODULE_VERSION);
Michael Chanc4e65752006-03-20 22:29:32 -08009685 strcpy(info->fw_version, tp->fw_ver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009686 strcpy(info->bus_info, pci_name(tp->pdev));
9687}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009688
Linus Torvalds1da177e2005-04-16 15:20:36 -07009689static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
9690{
9691 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009692
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009693 if ((tp->tg3_flags & TG3_FLAG_WOL_CAP) &&
9694 device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -07009695 wol->supported = WAKE_MAGIC;
9696 else
9697 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009698 wol->wolopts = 0;
Matt Carlson05ac4cb2008-11-03 16:53:46 -08009699 if ((tp->tg3_flags & TG3_FLAG_WOL_ENABLE) &&
9700 device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009701 wol->wolopts = WAKE_MAGIC;
9702 memset(&wol->sopass, 0, sizeof(wol->sopass));
9703}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009704
Linus Torvalds1da177e2005-04-16 15:20:36 -07009705static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
9706{
9707 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009708 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009709
Linus Torvalds1da177e2005-04-16 15:20:36 -07009710 if (wol->wolopts & ~WAKE_MAGIC)
9711 return -EINVAL;
9712 if ((wol->wolopts & WAKE_MAGIC) &&
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009713 !((tp->tg3_flags & TG3_FLAG_WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009714 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009715
David S. Millerf47c11e2005-06-24 20:18:35 -07009716 spin_lock_bh(&tp->lock);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009717 if (wol->wolopts & WAKE_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009718 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009719 device_set_wakeup_enable(dp, true);
9720 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009721 tp->tg3_flags &= ~TG3_FLAG_WOL_ENABLE;
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009722 device_set_wakeup_enable(dp, false);
9723 }
David S. Millerf47c11e2005-06-24 20:18:35 -07009724 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009725
Linus Torvalds1da177e2005-04-16 15:20:36 -07009726 return 0;
9727}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009728
Linus Torvalds1da177e2005-04-16 15:20:36 -07009729static u32 tg3_get_msglevel(struct net_device *dev)
9730{
9731 struct tg3 *tp = netdev_priv(dev);
9732 return tp->msg_enable;
9733}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009734
Linus Torvalds1da177e2005-04-16 15:20:36 -07009735static void tg3_set_msglevel(struct net_device *dev, u32 value)
9736{
9737 struct tg3 *tp = netdev_priv(dev);
9738 tp->msg_enable = value;
9739}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009740
Linus Torvalds1da177e2005-04-16 15:20:36 -07009741static int tg3_set_tso(struct net_device *dev, u32 value)
9742{
9743 struct tg3 *tp = netdev_priv(dev);
9744
9745 if (!(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
9746 if (value)
9747 return -EINVAL;
9748 return 0;
9749 }
Matt Carlson027455a2008-12-21 20:19:30 -08009750 if ((dev->features & NETIF_F_IPV6_CSUM) &&
Matt Carlsone849cdc2009-11-13 13:03:38 +00009751 ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_2) ||
9752 (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3))) {
Matt Carlson9936bcf2007-10-10 18:03:07 -07009753 if (value) {
Michael Chanb0026622006-07-03 19:42:14 -07009754 dev->features |= NETIF_F_TSO6;
Matt Carlsone849cdc2009-11-13 13:03:38 +00009755 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
9756 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -07009757 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
9758 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Matt Carlson321d32a2008-11-21 17:22:19 -08009759 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsone849cdc2009-11-13 13:03:38 +00009760 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson9936bcf2007-10-10 18:03:07 -07009761 dev->features |= NETIF_F_TSO_ECN;
9762 } else
9763 dev->features &= ~(NETIF_F_TSO6 | NETIF_F_TSO_ECN);
Michael Chanb0026622006-07-03 19:42:14 -07009764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009765 return ethtool_op_set_tso(dev, value);
9766}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009767
Linus Torvalds1da177e2005-04-16 15:20:36 -07009768static int tg3_nway_reset(struct net_device *dev)
9769{
9770 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009771 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009772
Linus Torvalds1da177e2005-04-16 15:20:36 -07009773 if (!netif_running(dev))
9774 return -EAGAIN;
9775
Michael Chanc94e3942005-09-27 12:12:42 -07009776 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
9777 return -EINVAL;
9778
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009779 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
9780 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
9781 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009782 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009783 } else {
9784 u32 bmcr;
9785
9786 spin_lock_bh(&tp->lock);
9787 r = -EINVAL;
9788 tg3_readphy(tp, MII_BMCR, &bmcr);
9789 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
9790 ((bmcr & BMCR_ANENABLE) ||
9791 (tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT))) {
9792 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
9793 BMCR_ANENABLE);
9794 r = 0;
9795 }
9796 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009797 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009798
Linus Torvalds1da177e2005-04-16 15:20:36 -07009799 return r;
9800}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009801
Linus Torvalds1da177e2005-04-16 15:20:36 -07009802static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
9803{
9804 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009805
Linus Torvalds1da177e2005-04-16 15:20:36 -07009806 ering->rx_max_pending = TG3_RX_RING_SIZE - 1;
9807 ering->rx_mini_max_pending = 0;
Michael Chan4f81c322006-03-20 21:33:42 -08009808 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE)
9809 ering->rx_jumbo_max_pending = TG3_RX_JUMBO_RING_SIZE - 1;
9810 else
9811 ering->rx_jumbo_max_pending = 0;
9812
9813 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009814
9815 ering->rx_pending = tp->rx_pending;
9816 ering->rx_mini_pending = 0;
Michael Chan4f81c322006-03-20 21:33:42 -08009817 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE)
9818 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
9819 else
9820 ering->rx_jumbo_pending = 0;
9821
Matt Carlsonf3f3f272009-08-28 14:03:21 +00009822 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009823}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009824
Linus Torvalds1da177e2005-04-16 15:20:36 -07009825static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
9826{
9827 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +00009828 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009829
Linus Torvalds1da177e2005-04-16 15:20:36 -07009830 if ((ering->rx_pending > TG3_RX_RING_SIZE - 1) ||
9831 (ering->rx_jumbo_pending > TG3_RX_JUMBO_RING_SIZE - 1) ||
Michael Chanbc3a9252006-10-18 20:55:18 -07009832 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
9833 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Michael Chan7f62ad52007-02-20 23:25:40 -08009834 ((tp->tg3_flags2 & TG3_FLG2_TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -07009835 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009836 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009837
Michael Chanbbe832c2005-06-24 20:20:04 -07009838 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009839 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009840 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -07009841 irq_sync = 1;
9842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009843
Michael Chanbbe832c2005-06-24 20:20:04 -07009844 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009845
Linus Torvalds1da177e2005-04-16 15:20:36 -07009846 tp->rx_pending = ering->rx_pending;
9847
9848 if ((tp->tg3_flags2 & TG3_FLG2_MAX_RXPEND_64) &&
9849 tp->rx_pending > 63)
9850 tp->rx_pending = 63;
9851 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +00009852
9853 for (i = 0; i < TG3_IRQ_MAX_VECS; i++)
9854 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009855
9856 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -07009857 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -07009858 err = tg3_restart_hw(tp, 1);
9859 if (!err)
9860 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009861 }
9862
David S. Millerf47c11e2005-06-24 20:18:35 -07009863 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009864
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009865 if (irq_sync && !err)
9866 tg3_phy_start(tp);
9867
Michael Chanb9ec6c12006-07-25 16:37:27 -07009868 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009869}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009870
Linus Torvalds1da177e2005-04-16 15:20:36 -07009871static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
9872{
9873 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009874
Linus Torvalds1da177e2005-04-16 15:20:36 -07009875 epause->autoneg = (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG) != 0;
Matt Carlson8d018622007-12-20 20:05:44 -08009876
Steve Glendinninge18ce342008-12-16 02:00:00 -08009877 if (tp->link_config.active_flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -08009878 epause->rx_pause = 1;
9879 else
9880 epause->rx_pause = 0;
9881
Steve Glendinninge18ce342008-12-16 02:00:00 -08009882 if (tp->link_config.active_flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -08009883 epause->tx_pause = 1;
9884 else
9885 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009886}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009887
Linus Torvalds1da177e2005-04-16 15:20:36 -07009888static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
9889{
9890 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009891 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009892
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009893 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson27121682010-02-17 15:16:57 +00009894 u32 newadv;
9895 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009896
Matt Carlson27121682010-02-17 15:16:57 +00009897 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009898
Matt Carlson27121682010-02-17 15:16:57 +00009899 if (!(phydev->supported & SUPPORTED_Pause) ||
9900 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
9901 ((epause->rx_pause && !epause->tx_pause) ||
9902 (!epause->rx_pause && epause->tx_pause))))
9903 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009904
Matt Carlson27121682010-02-17 15:16:57 +00009905 tp->link_config.flowctrl = 0;
9906 if (epause->rx_pause) {
9907 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009908
Matt Carlson27121682010-02-17 15:16:57 +00009909 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -08009910 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +00009911 newadv = ADVERTISED_Pause;
9912 } else
9913 newadv = ADVERTISED_Pause |
9914 ADVERTISED_Asym_Pause;
9915 } else if (epause->tx_pause) {
9916 tp->link_config.flowctrl |= FLOW_CTRL_TX;
9917 newadv = ADVERTISED_Asym_Pause;
9918 } else
9919 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009920
Matt Carlson27121682010-02-17 15:16:57 +00009921 if (epause->autoneg)
9922 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
9923 else
9924 tp->tg3_flags &= ~TG3_FLAG_PAUSE_AUTONEG;
9925
9926 if (tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED) {
9927 u32 oldadv = phydev->advertising &
9928 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
9929 if (oldadv != newadv) {
9930 phydev->advertising &=
9931 ~(ADVERTISED_Pause |
9932 ADVERTISED_Asym_Pause);
9933 phydev->advertising |= newadv;
9934 if (phydev->autoneg) {
9935 /*
9936 * Always renegotiate the link to
9937 * inform our link partner of our
9938 * flow control settings, even if the
9939 * flow control is forced. Let
9940 * tg3_adjust_link() do the final
9941 * flow control setup.
9942 */
9943 return phy_start_aneg(phydev);
9944 }
9945 }
9946
9947 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009948 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +00009949 } else {
9950 tp->link_config.orig_advertising &=
9951 ~(ADVERTISED_Pause |
9952 ADVERTISED_Asym_Pause);
9953 tp->link_config.orig_advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009954 }
9955 } else {
9956 int irq_sync = 0;
9957
9958 if (netif_running(dev)) {
9959 tg3_netif_stop(tp);
9960 irq_sync = 1;
9961 }
9962
9963 tg3_full_lock(tp, irq_sync);
9964
9965 if (epause->autoneg)
9966 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
9967 else
9968 tp->tg3_flags &= ~TG3_FLAG_PAUSE_AUTONEG;
9969 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -08009970 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009971 else
Steve Glendinninge18ce342008-12-16 02:00:00 -08009972 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009973 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -08009974 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009975 else
Steve Glendinninge18ce342008-12-16 02:00:00 -08009976 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009977
9978 if (netif_running(dev)) {
9979 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
9980 err = tg3_restart_hw(tp, 1);
9981 if (!err)
9982 tg3_netif_start(tp);
9983 }
9984
9985 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -07009986 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009987
Michael Chanb9ec6c12006-07-25 16:37:27 -07009988 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009989}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009990
Linus Torvalds1da177e2005-04-16 15:20:36 -07009991static u32 tg3_get_rx_csum(struct net_device *dev)
9992{
9993 struct tg3 *tp = netdev_priv(dev);
9994 return (tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) != 0;
9995}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009996
Linus Torvalds1da177e2005-04-16 15:20:36 -07009997static int tg3_set_rx_csum(struct net_device *dev, u32 data)
9998{
9999 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010000
Linus Torvalds1da177e2005-04-16 15:20:36 -070010001 if (tp->tg3_flags & TG3_FLAG_BROKEN_CHECKSUMS) {
10002 if (data != 0)
10003 return -EINVAL;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010004 return 0;
10005 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010006
David S. Millerf47c11e2005-06-24 20:18:35 -070010007 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010008 if (data)
10009 tp->tg3_flags |= TG3_FLAG_RX_CHECKSUMS;
10010 else
10011 tp->tg3_flags &= ~TG3_FLAG_RX_CHECKSUMS;
David S. Millerf47c11e2005-06-24 20:18:35 -070010012 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010013
Linus Torvalds1da177e2005-04-16 15:20:36 -070010014 return 0;
10015}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010016
Linus Torvalds1da177e2005-04-16 15:20:36 -070010017static int tg3_set_tx_csum(struct net_device *dev, u32 data)
10018{
10019 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010020
Linus Torvalds1da177e2005-04-16 15:20:36 -070010021 if (tp->tg3_flags & TG3_FLAG_BROKEN_CHECKSUMS) {
10022 if (data != 0)
10023 return -EINVAL;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010024 return 0;
10025 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010026
Matt Carlson321d32a2008-11-21 17:22:19 -080010027 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Michael Chan6460d942007-07-14 19:07:52 -070010028 ethtool_op_set_tx_ipv6_csum(dev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010029 else
Michael Chan9c27dbd2006-03-20 22:28:27 -080010030 ethtool_op_set_tx_csum(dev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010031
10032 return 0;
10033}
10034
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010035static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010036{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010037 switch (sset) {
10038 case ETH_SS_TEST:
10039 return TG3_NUM_TEST;
10040 case ETH_SS_STATS:
10041 return TG3_NUM_STATS;
10042 default:
10043 return -EOPNOTSUPP;
10044 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070010045}
10046
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010047static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010048{
10049 switch (stringset) {
10050 case ETH_SS_STATS:
10051 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
10052 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070010053 case ETH_SS_TEST:
10054 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
10055 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010056 default:
10057 WARN_ON(1); /* we need a WARN() */
10058 break;
10059 }
10060}
10061
Michael Chan4009a932005-09-05 17:52:54 -070010062static int tg3_phys_id(struct net_device *dev, u32 data)
10063{
10064 struct tg3 *tp = netdev_priv(dev);
10065 int i;
10066
10067 if (!netif_running(tp->dev))
10068 return -EAGAIN;
10069
10070 if (data == 0)
Stephen Hemminger759afc32008-02-23 19:51:59 -080010071 data = UINT_MAX / 2;
Michael Chan4009a932005-09-05 17:52:54 -070010072
10073 for (i = 0; i < (data * 2); i++) {
10074 if ((i % 2) == 0)
10075 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10076 LED_CTRL_1000MBPS_ON |
10077 LED_CTRL_100MBPS_ON |
10078 LED_CTRL_10MBPS_ON |
10079 LED_CTRL_TRAFFIC_OVERRIDE |
10080 LED_CTRL_TRAFFIC_BLINK |
10081 LED_CTRL_TRAFFIC_LED);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010082
Michael Chan4009a932005-09-05 17:52:54 -070010083 else
10084 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10085 LED_CTRL_TRAFFIC_OVERRIDE);
10086
10087 if (msleep_interruptible(500))
10088 break;
10089 }
10090 tw32(MAC_LED_CTRL, tp->led_ctrl);
10091 return 0;
10092}
10093
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010094static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010095 struct ethtool_stats *estats, u64 *tmp_stats)
10096{
10097 struct tg3 *tp = netdev_priv(dev);
10098 memcpy(tmp_stats, tg3_get_estats(tp), sizeof(tp->estats));
10099}
10100
Michael Chan566f86a2005-05-29 14:56:58 -070010101#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080010102#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
10103#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
10104#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Michael Chanb16250e2006-09-27 16:10:14 -070010105#define NVRAM_SELFBOOT_HW_SIZE 0x20
10106#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070010107
10108static int tg3_test_nvram(struct tg3 *tp)
10109{
Al Virob9fc7dc2007-12-17 22:59:57 -080010110 u32 csum, magic;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010111 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010112 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070010113
Matt Carlsondf259d82009-04-20 06:57:14 +000010114 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM)
10115 return 0;
10116
Matt Carlsone4f34112009-02-25 14:25:00 +000010117 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080010118 return -EIO;
10119
Michael Chan1b277772006-03-20 22:27:48 -080010120 if (magic == TG3_EEPROM_MAGIC)
10121 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070010122 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080010123 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
10124 TG3_EEPROM_SB_FORMAT_1) {
10125 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
10126 case TG3_EEPROM_SB_REVISION_0:
10127 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
10128 break;
10129 case TG3_EEPROM_SB_REVISION_2:
10130 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
10131 break;
10132 case TG3_EEPROM_SB_REVISION_3:
10133 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
10134 break;
10135 default:
10136 return 0;
10137 }
10138 } else
Michael Chan1b277772006-03-20 22:27:48 -080010139 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070010140 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
10141 size = NVRAM_SELFBOOT_HW_SIZE;
10142 else
Michael Chan1b277772006-03-20 22:27:48 -080010143 return -EIO;
10144
10145 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070010146 if (buf == NULL)
10147 return -ENOMEM;
10148
Michael Chan1b277772006-03-20 22:27:48 -080010149 err = -EIO;
10150 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010151 err = tg3_nvram_read_be32(tp, i, &buf[j]);
10152 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070010153 break;
Michael Chan566f86a2005-05-29 14:56:58 -070010154 }
Michael Chan1b277772006-03-20 22:27:48 -080010155 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070010156 goto out;
10157
Michael Chan1b277772006-03-20 22:27:48 -080010158 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010159 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080010160 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010161 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080010162 u8 *buf8 = (u8 *) buf, csum8 = 0;
10163
Al Virob9fc7dc2007-12-17 22:59:57 -080010164 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080010165 TG3_EEPROM_SB_REVISION_2) {
10166 /* For rev 2, the csum doesn't include the MBA. */
10167 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
10168 csum8 += buf8[i];
10169 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
10170 csum8 += buf8[i];
10171 } else {
10172 for (i = 0; i < size; i++)
10173 csum8 += buf8[i];
10174 }
Michael Chan1b277772006-03-20 22:27:48 -080010175
Adrian Bunkad96b482006-04-05 22:21:04 -070010176 if (csum8 == 0) {
10177 err = 0;
10178 goto out;
10179 }
10180
10181 err = -EIO;
10182 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080010183 }
Michael Chan566f86a2005-05-29 14:56:58 -070010184
Al Virob9fc7dc2007-12-17 22:59:57 -080010185 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010186 TG3_EEPROM_MAGIC_HW) {
10187 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000010188 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070010189 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070010190
10191 /* Separate the parity bits and the data bytes. */
10192 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
10193 if ((i == 0) || (i == 8)) {
10194 int l;
10195 u8 msk;
10196
10197 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
10198 parity[k++] = buf8[i] & msk;
10199 i++;
Matt Carlson859a588792010-04-05 10:19:28 +000010200 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070010201 int l;
10202 u8 msk;
10203
10204 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
10205 parity[k++] = buf8[i] & msk;
10206 i++;
10207
10208 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
10209 parity[k++] = buf8[i] & msk;
10210 i++;
10211 }
10212 data[j++] = buf8[i];
10213 }
10214
10215 err = -EIO;
10216 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
10217 u8 hw8 = hweight8(data[i]);
10218
10219 if ((hw8 & 0x1) && parity[i])
10220 goto out;
10221 else if (!(hw8 & 0x1) && !parity[i])
10222 goto out;
10223 }
10224 err = 0;
10225 goto out;
10226 }
10227
Michael Chan566f86a2005-05-29 14:56:58 -070010228 /* Bootstrap checksum at offset 0x10 */
10229 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlsona9dc5292009-02-25 14:25:30 +000010230 if (csum != be32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070010231 goto out;
10232
10233 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
10234 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlsona9dc5292009-02-25 14:25:30 +000010235 if (csum != be32_to_cpu(buf[0xfc/4]))
10236 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070010237
10238 err = 0;
10239
10240out:
10241 kfree(buf);
10242 return err;
10243}
10244
Michael Chanca430072005-05-29 14:57:23 -070010245#define TG3_SERDES_TIMEOUT_SEC 2
10246#define TG3_COPPER_TIMEOUT_SEC 6
10247
10248static int tg3_test_link(struct tg3 *tp)
10249{
10250 int i, max;
10251
10252 if (!netif_running(tp->dev))
10253 return -ENODEV;
10254
Michael Chan4c987482005-09-05 17:52:38 -070010255 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070010256 max = TG3_SERDES_TIMEOUT_SEC;
10257 else
10258 max = TG3_COPPER_TIMEOUT_SEC;
10259
10260 for (i = 0; i < max; i++) {
10261 if (netif_carrier_ok(tp->dev))
10262 return 0;
10263
10264 if (msleep_interruptible(1000))
10265 break;
10266 }
10267
10268 return -EIO;
10269}
10270
Michael Chana71116d2005-05-29 14:58:11 -070010271/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080010272static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070010273{
Michael Chanb16250e2006-09-27 16:10:14 -070010274 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070010275 u32 offset, read_mask, write_mask, val, save_val, read_val;
10276 static struct {
10277 u16 offset;
10278 u16 flags;
10279#define TG3_FL_5705 0x1
10280#define TG3_FL_NOT_5705 0x2
10281#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070010282#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070010283 u32 read_mask;
10284 u32 write_mask;
10285 } reg_tbl[] = {
10286 /* MAC Control Registers */
10287 { MAC_MODE, TG3_FL_NOT_5705,
10288 0x00000000, 0x00ef6f8c },
10289 { MAC_MODE, TG3_FL_5705,
10290 0x00000000, 0x01ef6b8c },
10291 { MAC_STATUS, TG3_FL_NOT_5705,
10292 0x03800107, 0x00000000 },
10293 { MAC_STATUS, TG3_FL_5705,
10294 0x03800100, 0x00000000 },
10295 { MAC_ADDR_0_HIGH, 0x0000,
10296 0x00000000, 0x0000ffff },
10297 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010298 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070010299 { MAC_RX_MTU_SIZE, 0x0000,
10300 0x00000000, 0x0000ffff },
10301 { MAC_TX_MODE, 0x0000,
10302 0x00000000, 0x00000070 },
10303 { MAC_TX_LENGTHS, 0x0000,
10304 0x00000000, 0x00003fff },
10305 { MAC_RX_MODE, TG3_FL_NOT_5705,
10306 0x00000000, 0x000007fc },
10307 { MAC_RX_MODE, TG3_FL_5705,
10308 0x00000000, 0x000007dc },
10309 { MAC_HASH_REG_0, 0x0000,
10310 0x00000000, 0xffffffff },
10311 { MAC_HASH_REG_1, 0x0000,
10312 0x00000000, 0xffffffff },
10313 { MAC_HASH_REG_2, 0x0000,
10314 0x00000000, 0xffffffff },
10315 { MAC_HASH_REG_3, 0x0000,
10316 0x00000000, 0xffffffff },
10317
10318 /* Receive Data and Receive BD Initiator Control Registers. */
10319 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
10320 0x00000000, 0xffffffff },
10321 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
10322 0x00000000, 0xffffffff },
10323 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
10324 0x00000000, 0x00000003 },
10325 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
10326 0x00000000, 0xffffffff },
10327 { RCVDBDI_STD_BD+0, 0x0000,
10328 0x00000000, 0xffffffff },
10329 { RCVDBDI_STD_BD+4, 0x0000,
10330 0x00000000, 0xffffffff },
10331 { RCVDBDI_STD_BD+8, 0x0000,
10332 0x00000000, 0xffff0002 },
10333 { RCVDBDI_STD_BD+0xc, 0x0000,
10334 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010335
Michael Chana71116d2005-05-29 14:58:11 -070010336 /* Receive BD Initiator Control Registers. */
10337 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
10338 0x00000000, 0xffffffff },
10339 { RCVBDI_STD_THRESH, TG3_FL_5705,
10340 0x00000000, 0x000003ff },
10341 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
10342 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010343
Michael Chana71116d2005-05-29 14:58:11 -070010344 /* Host Coalescing Control Registers. */
10345 { HOSTCC_MODE, TG3_FL_NOT_5705,
10346 0x00000000, 0x00000004 },
10347 { HOSTCC_MODE, TG3_FL_5705,
10348 0x00000000, 0x000000f6 },
10349 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
10350 0x00000000, 0xffffffff },
10351 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
10352 0x00000000, 0x000003ff },
10353 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
10354 0x00000000, 0xffffffff },
10355 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
10356 0x00000000, 0x000003ff },
10357 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
10358 0x00000000, 0xffffffff },
10359 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10360 0x00000000, 0x000000ff },
10361 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
10362 0x00000000, 0xffffffff },
10363 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10364 0x00000000, 0x000000ff },
10365 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
10366 0x00000000, 0xffffffff },
10367 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
10368 0x00000000, 0xffffffff },
10369 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10370 0x00000000, 0xffffffff },
10371 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10372 0x00000000, 0x000000ff },
10373 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10374 0x00000000, 0xffffffff },
10375 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10376 0x00000000, 0x000000ff },
10377 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
10378 0x00000000, 0xffffffff },
10379 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
10380 0x00000000, 0xffffffff },
10381 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
10382 0x00000000, 0xffffffff },
10383 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
10384 0x00000000, 0xffffffff },
10385 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
10386 0x00000000, 0xffffffff },
10387 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
10388 0xffffffff, 0x00000000 },
10389 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
10390 0xffffffff, 0x00000000 },
10391
10392 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070010393 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010394 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070010395 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010396 0x00000000, 0x007fffff },
10397 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
10398 0x00000000, 0x0000003f },
10399 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
10400 0x00000000, 0x000001ff },
10401 { BUFMGR_MB_HIGH_WATER, 0x0000,
10402 0x00000000, 0x000001ff },
10403 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
10404 0xffffffff, 0x00000000 },
10405 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
10406 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010407
Michael Chana71116d2005-05-29 14:58:11 -070010408 /* Mailbox Registers */
10409 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
10410 0x00000000, 0x000001ff },
10411 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
10412 0x00000000, 0x000001ff },
10413 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
10414 0x00000000, 0x000007ff },
10415 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
10416 0x00000000, 0x000001ff },
10417
10418 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
10419 };
10420
Michael Chanb16250e2006-09-27 16:10:14 -070010421 is_5705 = is_5750 = 0;
10422 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
Michael Chana71116d2005-05-29 14:58:11 -070010423 is_5705 = 1;
Michael Chanb16250e2006-09-27 16:10:14 -070010424 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
10425 is_5750 = 1;
10426 }
Michael Chana71116d2005-05-29 14:58:11 -070010427
10428 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
10429 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
10430 continue;
10431
10432 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
10433 continue;
10434
10435 if ((tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
10436 (reg_tbl[i].flags & TG3_FL_NOT_5788))
10437 continue;
10438
Michael Chanb16250e2006-09-27 16:10:14 -070010439 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
10440 continue;
10441
Michael Chana71116d2005-05-29 14:58:11 -070010442 offset = (u32) reg_tbl[i].offset;
10443 read_mask = reg_tbl[i].read_mask;
10444 write_mask = reg_tbl[i].write_mask;
10445
10446 /* Save the original register content */
10447 save_val = tr32(offset);
10448
10449 /* Determine the read-only value. */
10450 read_val = save_val & read_mask;
10451
10452 /* Write zero to the register, then make sure the read-only bits
10453 * are not changed and the read/write bits are all zeros.
10454 */
10455 tw32(offset, 0);
10456
10457 val = tr32(offset);
10458
10459 /* Test the read-only and read/write bits. */
10460 if (((val & read_mask) != read_val) || (val & write_mask))
10461 goto out;
10462
10463 /* Write ones to all the bits defined by RdMask and WrMask, then
10464 * make sure the read-only bits are not changed and the
10465 * read/write bits are all ones.
10466 */
10467 tw32(offset, read_mask | write_mask);
10468
10469 val = tr32(offset);
10470
10471 /* Test the read-only bits. */
10472 if ((val & read_mask) != read_val)
10473 goto out;
10474
10475 /* Test the read/write bits. */
10476 if ((val & write_mask) != write_mask)
10477 goto out;
10478
10479 tw32(offset, save_val);
10480 }
10481
10482 return 0;
10483
10484out:
Michael Chan9f88f292006-12-07 00:22:54 -080010485 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000010486 netdev_err(tp->dev,
10487 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070010488 tw32(offset, save_val);
10489 return -EIO;
10490}
10491
Michael Chan7942e1d2005-05-29 14:58:36 -070010492static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
10493{
Arjan van de Venf71e1302006-03-03 21:33:57 -050010494 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070010495 int i;
10496 u32 j;
10497
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020010498 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070010499 for (j = 0; j < len; j += 4) {
10500 u32 val;
10501
10502 tg3_write_mem(tp, offset + j, test_pattern[i]);
10503 tg3_read_mem(tp, offset + j, &val);
10504 if (val != test_pattern[i])
10505 return -EIO;
10506 }
10507 }
10508 return 0;
10509}
10510
10511static int tg3_test_memory(struct tg3 *tp)
10512{
10513 static struct mem_entry {
10514 u32 offset;
10515 u32 len;
10516 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080010517 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070010518 { 0x00002000, 0x1c000},
10519 { 0xffffffff, 0x00000}
10520 }, mem_tbl_5705[] = {
10521 { 0x00000100, 0x0000c},
10522 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070010523 { 0x00004000, 0x00800},
10524 { 0x00006000, 0x01000},
10525 { 0x00008000, 0x02000},
10526 { 0x00010000, 0x0e000},
10527 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080010528 }, mem_tbl_5755[] = {
10529 { 0x00000200, 0x00008},
10530 { 0x00004000, 0x00800},
10531 { 0x00006000, 0x00800},
10532 { 0x00008000, 0x02000},
10533 { 0x00010000, 0x0c000},
10534 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070010535 }, mem_tbl_5906[] = {
10536 { 0x00000200, 0x00008},
10537 { 0x00004000, 0x00400},
10538 { 0x00006000, 0x00400},
10539 { 0x00008000, 0x01000},
10540 { 0x00010000, 0x01000},
10541 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000010542 }, mem_tbl_5717[] = {
10543 { 0x00000200, 0x00008},
10544 { 0x00010000, 0x0a000},
10545 { 0x00020000, 0x13c00},
10546 { 0xffffffff, 0x00000}
10547 }, mem_tbl_57765[] = {
10548 { 0x00000200, 0x00008},
10549 { 0x00004000, 0x00800},
10550 { 0x00006000, 0x09800},
10551 { 0x00010000, 0x0a000},
10552 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070010553 };
10554 struct mem_entry *mem_tbl;
10555 int err = 0;
10556 int i;
10557
Matt Carlsona50d0792010-06-05 17:24:37 +000010558 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
10559 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlson8b5a6c42010-01-20 16:58:06 +000010560 mem_tbl = mem_tbl_5717;
10561 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
10562 mem_tbl = mem_tbl_57765;
10563 else if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Matt Carlson321d32a2008-11-21 17:22:19 -080010564 mem_tbl = mem_tbl_5755;
10565 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
10566 mem_tbl = mem_tbl_5906;
10567 else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
10568 mem_tbl = mem_tbl_5705;
10569 else
Michael Chan7942e1d2005-05-29 14:58:36 -070010570 mem_tbl = mem_tbl_570x;
10571
10572 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000010573 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
10574 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070010575 break;
10576 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010577
Michael Chan7942e1d2005-05-29 14:58:36 -070010578 return err;
10579}
10580
Michael Chan9f40dea2005-09-05 17:53:06 -070010581#define TG3_MAC_LOOPBACK 0
10582#define TG3_PHY_LOOPBACK 1
10583
10584static int tg3_run_loopback(struct tg3 *tp, int loopback_mode)
Michael Chanc76949a2005-05-29 14:58:59 -070010585{
Michael Chan9f40dea2005-09-05 17:53:06 -070010586 u32 mac_mode, rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010587 u32 desc_idx, coal_now;
Michael Chanc76949a2005-05-29 14:58:59 -070010588 struct sk_buff *skb, *rx_skb;
10589 u8 *tx_data;
10590 dma_addr_t map;
10591 int num_pkts, tx_len, rx_len, i, err;
10592 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000010593 struct tg3_napi *tnapi, *rnapi;
Matt Carlson21f581a2009-08-28 14:00:25 +000010594 struct tg3_rx_prodring_set *tpr = &tp->prodring[0];
Michael Chanc76949a2005-05-29 14:58:59 -070010595
Matt Carlsonc8873402010-02-12 14:47:11 +000010596 tnapi = &tp->napi[0];
10597 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000010598 if (tp->irq_cnt > 1) {
Matt Carlson0c1d0e22009-09-01 13:16:33 +000010599 rnapi = &tp->napi[1];
Matt Carlsonc8873402010-02-12 14:47:11 +000010600 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
10601 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000010602 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010603 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000010604
Michael Chan9f40dea2005-09-05 17:53:06 -070010605 if (loopback_mode == TG3_MAC_LOOPBACK) {
Michael Chanc94e3942005-09-27 12:12:42 -070010606 /* HW errata - mac loopback fails in some cases on 5780.
10607 * Normal traffic and PHY loopback are not affected by
10608 * errata.
10609 */
10610 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780)
10611 return 0;
10612
Michael Chan9f40dea2005-09-05 17:53:06 -070010613 mac_mode = (tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK) |
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010614 MAC_MODE_PORT_INT_LPBACK;
10615 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
10616 mac_mode |= MAC_MODE_LINK_POLARITY;
Michael Chan3f7045c2006-09-27 16:02:29 -070010617 if (tp->tg3_flags & TG3_FLAG_10_100_ONLY)
10618 mac_mode |= MAC_MODE_PORT_MODE_MII;
10619 else
10620 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chan9f40dea2005-09-05 17:53:06 -070010621 tw32(MAC_MODE, mac_mode);
10622 } else if (loopback_mode == TG3_PHY_LOOPBACK) {
Michael Chan3f7045c2006-09-27 16:02:29 -070010623 u32 val;
10624
Matt Carlson7f97a4b2009-08-25 10:10:03 +000010625 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
10626 tg3_phy_fet_toggle_apd(tp, false);
Michael Chan5d64ad32006-12-07 00:19:40 -080010627 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED100;
10628 } else
10629 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED1000;
Michael Chan3f7045c2006-09-27 16:02:29 -070010630
Matt Carlson9ef8ca92007-07-11 19:48:29 -070010631 tg3_phy_toggle_automdix(tp, 0);
10632
Michael Chan3f7045c2006-09-27 16:02:29 -070010633 tg3_writephy(tp, MII_BMCR, val);
Michael Chanc94e3942005-09-27 12:12:42 -070010634 udelay(40);
Michael Chan5d64ad32006-12-07 00:19:40 -080010635
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010636 mac_mode = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000010637 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
Matt Carlson1061b7c2010-02-12 14:47:12 +000010638 tg3_writephy(tp, MII_TG3_FET_PTEST,
10639 MII_TG3_FET_PTEST_FRC_TX_LINK |
10640 MII_TG3_FET_PTEST_FRC_TX_LOCK);
10641 /* The write needs to be flushed for the AC131 */
10642 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
10643 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
Michael Chan5d64ad32006-12-07 00:19:40 -080010644 mac_mode |= MAC_MODE_PORT_MODE_MII;
10645 } else
10646 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chanb16250e2006-09-27 16:10:14 -070010647
Michael Chanc94e3942005-09-27 12:12:42 -070010648 /* reset to prevent losing 1st rx packet intermittently */
10649 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES) {
10650 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
10651 udelay(10);
10652 tw32_f(MAC_RX_MODE, tp->rx_mode);
10653 }
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010654 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlson79eb6902010-02-17 15:17:03 +000010655 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
10656 if (masked_phy_id == TG3_PHY_ID_BCM5401)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010657 mac_mode &= ~MAC_MODE_LINK_POLARITY;
Matt Carlson79eb6902010-02-17 15:17:03 +000010658 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010659 mac_mode |= MAC_MODE_LINK_POLARITY;
Michael Chanff18ff02006-03-27 23:17:27 -080010660 tg3_writephy(tp, MII_TG3_EXT_CTRL,
10661 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
10662 }
Michael Chan9f40dea2005-09-05 17:53:06 -070010663 tw32(MAC_MODE, mac_mode);
Matt Carlson859a588792010-04-05 10:19:28 +000010664 } else {
Michael Chan9f40dea2005-09-05 17:53:06 -070010665 return -EINVAL;
Matt Carlson859a588792010-04-05 10:19:28 +000010666 }
Michael Chanc76949a2005-05-29 14:58:59 -070010667
10668 err = -EIO;
10669
Michael Chanc76949a2005-05-29 14:58:59 -070010670 tx_len = 1514;
David S. Millera20e9c62006-07-31 22:38:16 -070010671 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070010672 if (!skb)
10673 return -ENOMEM;
10674
Michael Chanc76949a2005-05-29 14:58:59 -070010675 tx_data = skb_put(skb, tx_len);
10676 memcpy(tx_data, tp->dev->dev_addr, 6);
10677 memset(tx_data + 6, 0x0, 8);
10678
10679 tw32(MAC_RX_MTU_SIZE, tx_len + 4);
10680
10681 for (i = 14; i < tx_len; i++)
10682 tx_data[i] = (u8) (i & 0xff);
10683
Alexander Duyckf4188d82009-12-02 16:48:38 +000010684 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
10685 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000010686 dev_kfree_skb(skb);
10687 return -EIO;
10688 }
Michael Chanc76949a2005-05-29 14:58:59 -070010689
10690 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010691 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070010692
10693 udelay(10);
10694
Matt Carlson898a56f2009-08-28 14:02:40 +000010695 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070010696
Michael Chanc76949a2005-05-29 14:58:59 -070010697 num_pkts = 0;
10698
Alexander Duyckf4188d82009-12-02 16:48:38 +000010699 tg3_set_txd(tnapi, tnapi->tx_prod, map, tx_len, 0, 1);
Michael Chanc76949a2005-05-29 14:58:59 -070010700
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010701 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070010702 num_pkts++;
10703
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010704 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
10705 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070010706
10707 udelay(10);
10708
Matt Carlson303fc922009-11-02 14:27:34 +000010709 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
10710 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070010711 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010712 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070010713
10714 udelay(10);
10715
Matt Carlson898a56f2009-08-28 14:02:40 +000010716 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
10717 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010718 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070010719 (rx_idx == (rx_start_idx + num_pkts)))
10720 break;
10721 }
10722
Alexander Duyckf4188d82009-12-02 16:48:38 +000010723 pci_unmap_single(tp->pdev, map, tx_len, PCI_DMA_TODEVICE);
Michael Chanc76949a2005-05-29 14:58:59 -070010724 dev_kfree_skb(skb);
10725
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010726 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070010727 goto out;
10728
10729 if (rx_idx != rx_start_idx + num_pkts)
10730 goto out;
10731
Matt Carlson72334482009-08-28 14:03:01 +000010732 desc = &rnapi->rx_rcb[rx_start_idx];
Michael Chanc76949a2005-05-29 14:58:59 -070010733 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
10734 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
10735 if (opaque_key != RXD_OPAQUE_RING_STD)
10736 goto out;
10737
10738 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
10739 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
10740 goto out;
10741
10742 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) - 4;
10743 if (rx_len != tx_len)
10744 goto out;
10745
Matt Carlson21f581a2009-08-28 14:00:25 +000010746 rx_skb = tpr->rx_std_buffers[desc_idx].skb;
Michael Chanc76949a2005-05-29 14:58:59 -070010747
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +000010748 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx], mapping);
Michael Chanc76949a2005-05-29 14:58:59 -070010749 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len, PCI_DMA_FROMDEVICE);
10750
10751 for (i = 14; i < tx_len; i++) {
10752 if (*(rx_skb->data + i) != (u8) (i & 0xff))
10753 goto out;
10754 }
10755 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010756
Michael Chanc76949a2005-05-29 14:58:59 -070010757 /* tg3_free_rings will unmap and free the rx_skb */
10758out:
10759 return err;
10760}
10761
Michael Chan9f40dea2005-09-05 17:53:06 -070010762#define TG3_MAC_LOOPBACK_FAILED 1
10763#define TG3_PHY_LOOPBACK_FAILED 2
10764#define TG3_LOOPBACK_FAILED (TG3_MAC_LOOPBACK_FAILED | \
10765 TG3_PHY_LOOPBACK_FAILED)
10766
10767static int tg3_test_loopback(struct tg3 *tp)
10768{
10769 int err = 0;
Matt Carlson9936bcf2007-10-10 18:03:07 -070010770 u32 cpmuctrl = 0;
Michael Chan9f40dea2005-09-05 17:53:06 -070010771
10772 if (!netif_running(tp->dev))
10773 return TG3_LOOPBACK_FAILED;
10774
Michael Chanb9ec6c12006-07-25 16:37:27 -070010775 err = tg3_reset_hw(tp, 1);
10776 if (err)
10777 return TG3_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070010778
Matt Carlson6833c042008-11-21 17:18:59 -080010779 /* Turn off gphy autopowerdown. */
10780 if (tp->tg3_flags3 & TG3_FLG3_PHY_ENABLE_APD)
10781 tg3_phy_toggle_apd(tp, false);
10782
Matt Carlson321d32a2008-11-21 17:22:19 -080010783 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070010784 int i;
10785 u32 status;
10786
10787 tw32(TG3_CPMU_MUTEX_REQ, CPMU_MUTEX_REQ_DRIVER);
10788
10789 /* Wait for up to 40 microseconds to acquire lock. */
10790 for (i = 0; i < 4; i++) {
10791 status = tr32(TG3_CPMU_MUTEX_GNT);
10792 if (status == CPMU_MUTEX_GNT_DRIVER)
10793 break;
10794 udelay(10);
10795 }
10796
10797 if (status != CPMU_MUTEX_GNT_DRIVER)
10798 return TG3_LOOPBACK_FAILED;
10799
Matt Carlsonb2a5c192008-04-03 21:44:44 -070010800 /* Turn off link-based power management. */
Matt Carlsone8750932007-11-12 21:11:51 -080010801 cpmuctrl = tr32(TG3_CPMU_CTRL);
Matt Carlson109115e2008-05-02 16:48:59 -070010802 tw32(TG3_CPMU_CTRL,
10803 cpmuctrl & ~(CPMU_CTRL_LINK_SPEED_MODE |
10804 CPMU_CTRL_LINK_AWARE_MODE));
Matt Carlson9936bcf2007-10-10 18:03:07 -070010805 }
10806
Michael Chan9f40dea2005-09-05 17:53:06 -070010807 if (tg3_run_loopback(tp, TG3_MAC_LOOPBACK))
10808 err |= TG3_MAC_LOOPBACK_FAILED;
Matt Carlson9936bcf2007-10-10 18:03:07 -070010809
Matt Carlson321d32a2008-11-21 17:22:19 -080010810 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070010811 tw32(TG3_CPMU_CTRL, cpmuctrl);
10812
10813 /* Release the mutex */
10814 tw32(TG3_CPMU_MUTEX_GNT, CPMU_MUTEX_GNT_DRIVER);
10815 }
10816
Matt Carlsondd477002008-05-25 23:45:58 -070010817 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
10818 !(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)) {
Michael Chan9f40dea2005-09-05 17:53:06 -070010819 if (tg3_run_loopback(tp, TG3_PHY_LOOPBACK))
10820 err |= TG3_PHY_LOOPBACK_FAILED;
10821 }
10822
Matt Carlson6833c042008-11-21 17:18:59 -080010823 /* Re-enable gphy autopowerdown. */
10824 if (tp->tg3_flags3 & TG3_FLG3_PHY_ENABLE_APD)
10825 tg3_phy_toggle_apd(tp, true);
10826
Michael Chan9f40dea2005-09-05 17:53:06 -070010827 return err;
10828}
10829
Michael Chan4cafd3f2005-05-29 14:56:34 -070010830static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
10831 u64 *data)
10832{
Michael Chan566f86a2005-05-29 14:56:58 -070010833 struct tg3 *tp = netdev_priv(dev);
10834
Michael Chanbc1c7562006-03-20 17:48:03 -080010835 if (tp->link_config.phy_is_low_power)
10836 tg3_set_power_state(tp, PCI_D0);
10837
Michael Chan566f86a2005-05-29 14:56:58 -070010838 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
10839
10840 if (tg3_test_nvram(tp) != 0) {
10841 etest->flags |= ETH_TEST_FL_FAILED;
10842 data[0] = 1;
10843 }
Michael Chanca430072005-05-29 14:57:23 -070010844 if (tg3_test_link(tp) != 0) {
10845 etest->flags |= ETH_TEST_FL_FAILED;
10846 data[1] = 1;
10847 }
Michael Chana71116d2005-05-29 14:58:11 -070010848 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010849 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070010850
Michael Chanbbe832c2005-06-24 20:20:04 -070010851 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010852 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010853 tg3_netif_stop(tp);
10854 irq_sync = 1;
10855 }
10856
10857 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070010858
10859 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080010860 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070010861 tg3_halt_cpu(tp, RX_CPU_BASE);
10862 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
10863 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080010864 if (!err)
10865 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070010866
Michael Chand9ab5ad2006-03-20 22:27:35 -080010867 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)
10868 tg3_phy_reset(tp);
10869
Michael Chana71116d2005-05-29 14:58:11 -070010870 if (tg3_test_registers(tp) != 0) {
10871 etest->flags |= ETH_TEST_FL_FAILED;
10872 data[2] = 1;
10873 }
Michael Chan7942e1d2005-05-29 14:58:36 -070010874 if (tg3_test_memory(tp) != 0) {
10875 etest->flags |= ETH_TEST_FL_FAILED;
10876 data[3] = 1;
10877 }
Michael Chan9f40dea2005-09-05 17:53:06 -070010878 if ((data[4] = tg3_test_loopback(tp)) != 0)
Michael Chanc76949a2005-05-29 14:58:59 -070010879 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070010880
David S. Millerf47c11e2005-06-24 20:18:35 -070010881 tg3_full_unlock(tp);
10882
Michael Chand4bc3922005-05-29 14:59:20 -070010883 if (tg3_test_interrupt(tp) != 0) {
10884 etest->flags |= ETH_TEST_FL_FAILED;
10885 data[5] = 1;
10886 }
David S. Millerf47c11e2005-06-24 20:18:35 -070010887
10888 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070010889
Michael Chana71116d2005-05-29 14:58:11 -070010890 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10891 if (netif_running(dev)) {
10892 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010893 err2 = tg3_restart_hw(tp, 1);
10894 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070010895 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070010896 }
David S. Millerf47c11e2005-06-24 20:18:35 -070010897
10898 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010899
10900 if (irq_sync && !err2)
10901 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070010902 }
Michael Chanbc1c7562006-03-20 17:48:03 -080010903 if (tp->link_config.phy_is_low_power)
10904 tg3_set_power_state(tp, PCI_D3hot);
10905
Michael Chan4cafd3f2005-05-29 14:56:34 -070010906}
10907
Linus Torvalds1da177e2005-04-16 15:20:36 -070010908static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
10909{
10910 struct mii_ioctl_data *data = if_mii(ifr);
10911 struct tg3 *tp = netdev_priv(dev);
10912 int err;
10913
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010914 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010915 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010916 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
10917 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010918 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000010919 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010920 }
10921
Matt Carlson33f401a2010-04-05 10:19:27 +000010922 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010923 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000010924 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010925
10926 /* fallthru */
10927 case SIOCGMIIREG: {
10928 u32 mii_regval;
10929
10930 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
10931 break; /* We have no PHY */
10932
Michael Chanbc1c7562006-03-20 17:48:03 -080010933 if (tp->link_config.phy_is_low_power)
10934 return -EAGAIN;
10935
David S. Millerf47c11e2005-06-24 20:18:35 -070010936 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010937 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070010938 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010939
10940 data->val_out = mii_regval;
10941
10942 return err;
10943 }
10944
10945 case SIOCSMIIREG:
10946 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
10947 break; /* We have no PHY */
10948
Michael Chanbc1c7562006-03-20 17:48:03 -080010949 if (tp->link_config.phy_is_low_power)
10950 return -EAGAIN;
10951
David S. Millerf47c11e2005-06-24 20:18:35 -070010952 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010953 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070010954 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010955
10956 return err;
10957
10958 default:
10959 /* do nothing */
10960 break;
10961 }
10962 return -EOPNOTSUPP;
10963}
10964
10965#if TG3_VLAN_TAG_USED
10966static void tg3_vlan_rx_register(struct net_device *dev, struct vlan_group *grp)
10967{
10968 struct tg3 *tp = netdev_priv(dev);
10969
Matt Carlson844b3ee2009-02-25 14:23:56 +000010970 if (!netif_running(dev)) {
10971 tp->vlgrp = grp;
10972 return;
10973 }
10974
10975 tg3_netif_stop(tp);
Michael Chan29315e82006-06-29 20:12:30 -070010976
David S. Millerf47c11e2005-06-24 20:18:35 -070010977 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010978
10979 tp->vlgrp = grp;
10980
10981 /* Update RX_MODE_KEEP_VLAN_TAG bit in RX_MODE register. */
10982 __tg3_set_rx_mode(dev);
10983
Matt Carlson844b3ee2009-02-25 14:23:56 +000010984 tg3_netif_start(tp);
Michael Chan46966542007-07-11 19:47:19 -070010985
10986 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010987}
Linus Torvalds1da177e2005-04-16 15:20:36 -070010988#endif
10989
David S. Miller15f98502005-05-18 22:49:26 -070010990static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
10991{
10992 struct tg3 *tp = netdev_priv(dev);
10993
10994 memcpy(ec, &tp->coal, sizeof(*ec));
10995 return 0;
10996}
10997
Michael Chand244c892005-07-05 14:42:33 -070010998static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
10999{
11000 struct tg3 *tp = netdev_priv(dev);
11001 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
11002 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
11003
11004 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
11005 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
11006 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
11007 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
11008 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
11009 }
11010
11011 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
11012 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
11013 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
11014 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
11015 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
11016 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
11017 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
11018 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
11019 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
11020 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
11021 return -EINVAL;
11022
11023 /* No rx interrupts will be generated if both are zero */
11024 if ((ec->rx_coalesce_usecs == 0) &&
11025 (ec->rx_max_coalesced_frames == 0))
11026 return -EINVAL;
11027
11028 /* No tx interrupts will be generated if both are zero */
11029 if ((ec->tx_coalesce_usecs == 0) &&
11030 (ec->tx_max_coalesced_frames == 0))
11031 return -EINVAL;
11032
11033 /* Only copy relevant parameters, ignore all others. */
11034 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
11035 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
11036 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
11037 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
11038 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
11039 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
11040 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
11041 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
11042 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
11043
11044 if (netif_running(dev)) {
11045 tg3_full_lock(tp, 0);
11046 __tg3_set_coalesce(tp, &tp->coal);
11047 tg3_full_unlock(tp);
11048 }
11049 return 0;
11050}
11051
Jeff Garzik7282d492006-09-13 14:30:00 -040011052static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011053 .get_settings = tg3_get_settings,
11054 .set_settings = tg3_set_settings,
11055 .get_drvinfo = tg3_get_drvinfo,
11056 .get_regs_len = tg3_get_regs_len,
11057 .get_regs = tg3_get_regs,
11058 .get_wol = tg3_get_wol,
11059 .set_wol = tg3_set_wol,
11060 .get_msglevel = tg3_get_msglevel,
11061 .set_msglevel = tg3_set_msglevel,
11062 .nway_reset = tg3_nway_reset,
11063 .get_link = ethtool_op_get_link,
11064 .get_eeprom_len = tg3_get_eeprom_len,
11065 .get_eeprom = tg3_get_eeprom,
11066 .set_eeprom = tg3_set_eeprom,
11067 .get_ringparam = tg3_get_ringparam,
11068 .set_ringparam = tg3_set_ringparam,
11069 .get_pauseparam = tg3_get_pauseparam,
11070 .set_pauseparam = tg3_set_pauseparam,
11071 .get_rx_csum = tg3_get_rx_csum,
11072 .set_rx_csum = tg3_set_rx_csum,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011073 .set_tx_csum = tg3_set_tx_csum,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011074 .set_sg = ethtool_op_set_sg,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011075 .set_tso = tg3_set_tso,
Michael Chan4cafd3f2005-05-29 14:56:34 -070011076 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011077 .get_strings = tg3_get_strings,
Michael Chan4009a932005-09-05 17:52:54 -070011078 .phys_id = tg3_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011079 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070011080 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070011081 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070011082 .get_sset_count = tg3_get_sset_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011083};
11084
11085static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
11086{
Michael Chan1b277772006-03-20 22:27:48 -080011087 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011088
11089 tp->nvram_size = EEPROM_CHIP_SIZE;
11090
Matt Carlsone4f34112009-02-25 14:25:00 +000011091 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011092 return;
11093
Michael Chanb16250e2006-09-27 16:10:14 -070011094 if ((magic != TG3_EEPROM_MAGIC) &&
11095 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
11096 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011097 return;
11098
11099 /*
11100 * Size the chip by reading offsets at increasing powers of two.
11101 * When we encounter our validation signature, we know the addressing
11102 * has wrapped around, and thus have our chip size.
11103 */
Michael Chan1b277772006-03-20 22:27:48 -080011104 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011105
11106 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000011107 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011108 return;
11109
Michael Chan18201802006-03-20 22:29:15 -080011110 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011111 break;
11112
11113 cursize <<= 1;
11114 }
11115
11116 tp->nvram_size = cursize;
11117}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011118
Linus Torvalds1da177e2005-04-16 15:20:36 -070011119static void __devinit tg3_get_nvram_size(struct tg3 *tp)
11120{
11121 u32 val;
11122
Matt Carlsondf259d82009-04-20 06:57:14 +000011123 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
11124 tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080011125 return;
11126
11127 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080011128 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080011129 tg3_get_eeprom_size(tp);
11130 return;
11131 }
11132
Matt Carlson6d348f22009-02-25 14:25:52 +000011133 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011134 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000011135 /* This is confusing. We want to operate on the
11136 * 16-bit value at offset 0xf2. The tg3_nvram_read()
11137 * call will read from NVRAM and byteswap the data
11138 * according to the byteswapping settings for all
11139 * other register accesses. This ensures the data we
11140 * want will always reside in the lower 16-bits.
11141 * However, the data in NVRAM is in LE format, which
11142 * means the data from the NVRAM read will always be
11143 * opposite the endianness of the CPU. The 16-bit
11144 * byteswap then brings the data to CPU endianness.
11145 */
11146 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011147 return;
11148 }
11149 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070011150 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011151}
11152
11153static void __devinit tg3_get_nvram_info(struct tg3 *tp)
11154{
11155 u32 nvcfg1;
11156
11157 nvcfg1 = tr32(NVRAM_CFG1);
11158 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
11159 tp->tg3_flags2 |= TG3_FLG2_FLASH;
Matt Carlson8590a602009-08-28 12:29:16 +000011160 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011161 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11162 tw32(NVRAM_CFG1, nvcfg1);
11163 }
11164
Michael Chan4c987482005-09-05 17:52:38 -070011165 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750) ||
Michael Chana4e2b342005-10-26 15:46:52 -070011166 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011167 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011168 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
11169 tp->nvram_jedecnum = JEDEC_ATMEL;
11170 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
11171 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11172 break;
11173 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
11174 tp->nvram_jedecnum = JEDEC_ATMEL;
11175 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
11176 break;
11177 case FLASH_VENDOR_ATMEL_EEPROM:
11178 tp->nvram_jedecnum = JEDEC_ATMEL;
11179 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11180 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11181 break;
11182 case FLASH_VENDOR_ST:
11183 tp->nvram_jedecnum = JEDEC_ST;
11184 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
11185 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11186 break;
11187 case FLASH_VENDOR_SAIFUN:
11188 tp->nvram_jedecnum = JEDEC_SAIFUN;
11189 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
11190 break;
11191 case FLASH_VENDOR_SST_SMALL:
11192 case FLASH_VENDOR_SST_LARGE:
11193 tp->nvram_jedecnum = JEDEC_SST;
11194 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
11195 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011196 }
Matt Carlson8590a602009-08-28 12:29:16 +000011197 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011198 tp->nvram_jedecnum = JEDEC_ATMEL;
11199 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
11200 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11201 }
11202}
11203
Matt Carlsona1b950d2009-09-01 13:20:17 +000011204static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
11205{
11206 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
11207 case FLASH_5752PAGE_SIZE_256:
11208 tp->nvram_pagesize = 256;
11209 break;
11210 case FLASH_5752PAGE_SIZE_512:
11211 tp->nvram_pagesize = 512;
11212 break;
11213 case FLASH_5752PAGE_SIZE_1K:
11214 tp->nvram_pagesize = 1024;
11215 break;
11216 case FLASH_5752PAGE_SIZE_2K:
11217 tp->nvram_pagesize = 2048;
11218 break;
11219 case FLASH_5752PAGE_SIZE_4K:
11220 tp->nvram_pagesize = 4096;
11221 break;
11222 case FLASH_5752PAGE_SIZE_264:
11223 tp->nvram_pagesize = 264;
11224 break;
11225 case FLASH_5752PAGE_SIZE_528:
11226 tp->nvram_pagesize = 528;
11227 break;
11228 }
11229}
11230
Michael Chan361b4ac2005-04-21 17:11:21 -070011231static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
11232{
11233 u32 nvcfg1;
11234
11235 nvcfg1 = tr32(NVRAM_CFG1);
11236
Michael Chane6af3012005-04-21 17:12:05 -070011237 /* NVRAM protection for TPM */
11238 if (nvcfg1 & (1 << 27))
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011239 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Michael Chane6af3012005-04-21 17:12:05 -070011240
Michael Chan361b4ac2005-04-21 17:11:21 -070011241 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011242 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
11243 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
11244 tp->nvram_jedecnum = JEDEC_ATMEL;
11245 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11246 break;
11247 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11248 tp->nvram_jedecnum = JEDEC_ATMEL;
11249 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11250 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11251 break;
11252 case FLASH_5752VENDOR_ST_M45PE10:
11253 case FLASH_5752VENDOR_ST_M45PE20:
11254 case FLASH_5752VENDOR_ST_M45PE40:
11255 tp->nvram_jedecnum = JEDEC_ST;
11256 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11257 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11258 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070011259 }
11260
11261 if (tp->tg3_flags2 & TG3_FLG2_FLASH) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000011262 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000011263 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070011264 /* For eeprom, set pagesize to maximum eeprom size */
11265 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11266
11267 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11268 tw32(NVRAM_CFG1, nvcfg1);
11269 }
11270}
11271
Michael Chand3c7b882006-03-23 01:28:25 -080011272static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
11273{
Matt Carlson989a9d22007-05-05 11:51:05 -070011274 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080011275
11276 nvcfg1 = tr32(NVRAM_CFG1);
11277
11278 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070011279 if (nvcfg1 & (1 << 27)) {
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011280 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Matt Carlson989a9d22007-05-05 11:51:05 -070011281 protect = 1;
11282 }
Michael Chand3c7b882006-03-23 01:28:25 -080011283
Matt Carlson989a9d22007-05-05 11:51:05 -070011284 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11285 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011286 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11287 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11288 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11289 case FLASH_5755VENDOR_ATMEL_FLASH_5:
11290 tp->nvram_jedecnum = JEDEC_ATMEL;
11291 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11292 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11293 tp->nvram_pagesize = 264;
11294 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
11295 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
11296 tp->nvram_size = (protect ? 0x3e200 :
11297 TG3_NVRAM_SIZE_512KB);
11298 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
11299 tp->nvram_size = (protect ? 0x1f200 :
11300 TG3_NVRAM_SIZE_256KB);
11301 else
11302 tp->nvram_size = (protect ? 0x1f200 :
11303 TG3_NVRAM_SIZE_128KB);
11304 break;
11305 case FLASH_5752VENDOR_ST_M45PE10:
11306 case FLASH_5752VENDOR_ST_M45PE20:
11307 case FLASH_5752VENDOR_ST_M45PE40:
11308 tp->nvram_jedecnum = JEDEC_ST;
11309 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11310 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11311 tp->nvram_pagesize = 256;
11312 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
11313 tp->nvram_size = (protect ?
11314 TG3_NVRAM_SIZE_64KB :
11315 TG3_NVRAM_SIZE_128KB);
11316 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
11317 tp->nvram_size = (protect ?
11318 TG3_NVRAM_SIZE_64KB :
11319 TG3_NVRAM_SIZE_256KB);
11320 else
11321 tp->nvram_size = (protect ?
11322 TG3_NVRAM_SIZE_128KB :
11323 TG3_NVRAM_SIZE_512KB);
11324 break;
Michael Chand3c7b882006-03-23 01:28:25 -080011325 }
11326}
11327
Michael Chan1b277772006-03-20 22:27:48 -080011328static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
11329{
11330 u32 nvcfg1;
11331
11332 nvcfg1 = tr32(NVRAM_CFG1);
11333
11334 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011335 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
11336 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11337 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
11338 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11339 tp->nvram_jedecnum = JEDEC_ATMEL;
11340 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11341 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080011342
Matt Carlson8590a602009-08-28 12:29:16 +000011343 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11344 tw32(NVRAM_CFG1, nvcfg1);
11345 break;
11346 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11347 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11348 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11349 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11350 tp->nvram_jedecnum = JEDEC_ATMEL;
11351 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11352 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11353 tp->nvram_pagesize = 264;
11354 break;
11355 case FLASH_5752VENDOR_ST_M45PE10:
11356 case FLASH_5752VENDOR_ST_M45PE20:
11357 case FLASH_5752VENDOR_ST_M45PE40:
11358 tp->nvram_jedecnum = JEDEC_ST;
11359 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11360 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11361 tp->nvram_pagesize = 256;
11362 break;
Michael Chan1b277772006-03-20 22:27:48 -080011363 }
11364}
11365
Matt Carlson6b91fa02007-10-10 18:01:09 -070011366static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
11367{
11368 u32 nvcfg1, protect = 0;
11369
11370 nvcfg1 = tr32(NVRAM_CFG1);
11371
11372 /* NVRAM protection for TPM */
11373 if (nvcfg1 & (1 << 27)) {
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011374 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011375 protect = 1;
11376 }
11377
11378 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11379 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011380 case FLASH_5761VENDOR_ATMEL_ADB021D:
11381 case FLASH_5761VENDOR_ATMEL_ADB041D:
11382 case FLASH_5761VENDOR_ATMEL_ADB081D:
11383 case FLASH_5761VENDOR_ATMEL_ADB161D:
11384 case FLASH_5761VENDOR_ATMEL_MDB021D:
11385 case FLASH_5761VENDOR_ATMEL_MDB041D:
11386 case FLASH_5761VENDOR_ATMEL_MDB081D:
11387 case FLASH_5761VENDOR_ATMEL_MDB161D:
11388 tp->nvram_jedecnum = JEDEC_ATMEL;
11389 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11390 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11391 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
11392 tp->nvram_pagesize = 256;
11393 break;
11394 case FLASH_5761VENDOR_ST_A_M45PE20:
11395 case FLASH_5761VENDOR_ST_A_M45PE40:
11396 case FLASH_5761VENDOR_ST_A_M45PE80:
11397 case FLASH_5761VENDOR_ST_A_M45PE16:
11398 case FLASH_5761VENDOR_ST_M_M45PE20:
11399 case FLASH_5761VENDOR_ST_M_M45PE40:
11400 case FLASH_5761VENDOR_ST_M_M45PE80:
11401 case FLASH_5761VENDOR_ST_M_M45PE16:
11402 tp->nvram_jedecnum = JEDEC_ST;
11403 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11404 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11405 tp->nvram_pagesize = 256;
11406 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011407 }
11408
11409 if (protect) {
11410 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
11411 } else {
11412 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011413 case FLASH_5761VENDOR_ATMEL_ADB161D:
11414 case FLASH_5761VENDOR_ATMEL_MDB161D:
11415 case FLASH_5761VENDOR_ST_A_M45PE16:
11416 case FLASH_5761VENDOR_ST_M_M45PE16:
11417 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
11418 break;
11419 case FLASH_5761VENDOR_ATMEL_ADB081D:
11420 case FLASH_5761VENDOR_ATMEL_MDB081D:
11421 case FLASH_5761VENDOR_ST_A_M45PE80:
11422 case FLASH_5761VENDOR_ST_M_M45PE80:
11423 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
11424 break;
11425 case FLASH_5761VENDOR_ATMEL_ADB041D:
11426 case FLASH_5761VENDOR_ATMEL_MDB041D:
11427 case FLASH_5761VENDOR_ST_A_M45PE40:
11428 case FLASH_5761VENDOR_ST_M_M45PE40:
11429 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11430 break;
11431 case FLASH_5761VENDOR_ATMEL_ADB021D:
11432 case FLASH_5761VENDOR_ATMEL_MDB021D:
11433 case FLASH_5761VENDOR_ST_A_M45PE20:
11434 case FLASH_5761VENDOR_ST_M_M45PE20:
11435 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11436 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011437 }
11438 }
11439}
11440
Michael Chanb5d37722006-09-27 16:06:21 -070011441static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
11442{
11443 tp->nvram_jedecnum = JEDEC_ATMEL;
11444 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11445 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11446}
11447
Matt Carlson321d32a2008-11-21 17:22:19 -080011448static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
11449{
11450 u32 nvcfg1;
11451
11452 nvcfg1 = tr32(NVRAM_CFG1);
11453
11454 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11455 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11456 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11457 tp->nvram_jedecnum = JEDEC_ATMEL;
11458 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11459 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11460
11461 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11462 tw32(NVRAM_CFG1, nvcfg1);
11463 return;
11464 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11465 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11466 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11467 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11468 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11469 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11470 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
11471 tp->nvram_jedecnum = JEDEC_ATMEL;
11472 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11473 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11474
11475 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11476 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11477 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11478 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11479 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11480 break;
11481 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11482 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11483 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11484 break;
11485 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11486 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
11487 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11488 break;
11489 }
11490 break;
11491 case FLASH_5752VENDOR_ST_M45PE10:
11492 case FLASH_5752VENDOR_ST_M45PE20:
11493 case FLASH_5752VENDOR_ST_M45PE40:
11494 tp->nvram_jedecnum = JEDEC_ST;
11495 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11496 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11497
11498 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11499 case FLASH_5752VENDOR_ST_M45PE10:
11500 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11501 break;
11502 case FLASH_5752VENDOR_ST_M45PE20:
11503 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11504 break;
11505 case FLASH_5752VENDOR_ST_M45PE40:
11506 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11507 break;
11508 }
11509 break;
11510 default:
Matt Carlsondf259d82009-04-20 06:57:14 +000011511 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
Matt Carlson321d32a2008-11-21 17:22:19 -080011512 return;
11513 }
11514
Matt Carlsona1b950d2009-09-01 13:20:17 +000011515 tg3_nvram_get_pagesize(tp, nvcfg1);
11516 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Matt Carlson321d32a2008-11-21 17:22:19 -080011517 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011518}
11519
11520
11521static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
11522{
11523 u32 nvcfg1;
11524
11525 nvcfg1 = tr32(NVRAM_CFG1);
11526
11527 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11528 case FLASH_5717VENDOR_ATMEL_EEPROM:
11529 case FLASH_5717VENDOR_MICRO_EEPROM:
11530 tp->nvram_jedecnum = JEDEC_ATMEL;
11531 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11532 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11533
11534 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11535 tw32(NVRAM_CFG1, nvcfg1);
11536 return;
11537 case FLASH_5717VENDOR_ATMEL_MDB011D:
11538 case FLASH_5717VENDOR_ATMEL_ADB011B:
11539 case FLASH_5717VENDOR_ATMEL_ADB011D:
11540 case FLASH_5717VENDOR_ATMEL_MDB021D:
11541 case FLASH_5717VENDOR_ATMEL_ADB021B:
11542 case FLASH_5717VENDOR_ATMEL_ADB021D:
11543 case FLASH_5717VENDOR_ATMEL_45USPT:
11544 tp->nvram_jedecnum = JEDEC_ATMEL;
11545 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11546 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11547
11548 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11549 case FLASH_5717VENDOR_ATMEL_MDB021D:
11550 case FLASH_5717VENDOR_ATMEL_ADB021B:
11551 case FLASH_5717VENDOR_ATMEL_ADB021D:
11552 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11553 break;
11554 default:
11555 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11556 break;
11557 }
Matt Carlson321d32a2008-11-21 17:22:19 -080011558 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011559 case FLASH_5717VENDOR_ST_M_M25PE10:
11560 case FLASH_5717VENDOR_ST_A_M25PE10:
11561 case FLASH_5717VENDOR_ST_M_M45PE10:
11562 case FLASH_5717VENDOR_ST_A_M45PE10:
11563 case FLASH_5717VENDOR_ST_M_M25PE20:
11564 case FLASH_5717VENDOR_ST_A_M25PE20:
11565 case FLASH_5717VENDOR_ST_M_M45PE20:
11566 case FLASH_5717VENDOR_ST_A_M45PE20:
11567 case FLASH_5717VENDOR_ST_25USPT:
11568 case FLASH_5717VENDOR_ST_45USPT:
11569 tp->nvram_jedecnum = JEDEC_ST;
11570 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11571 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11572
11573 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11574 case FLASH_5717VENDOR_ST_M_M25PE20:
11575 case FLASH_5717VENDOR_ST_A_M25PE20:
11576 case FLASH_5717VENDOR_ST_M_M45PE20:
11577 case FLASH_5717VENDOR_ST_A_M45PE20:
11578 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11579 break;
11580 default:
11581 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11582 break;
11583 }
Matt Carlson321d32a2008-11-21 17:22:19 -080011584 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011585 default:
11586 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
11587 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080011588 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000011589
11590 tg3_nvram_get_pagesize(tp, nvcfg1);
11591 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
11592 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
Matt Carlson321d32a2008-11-21 17:22:19 -080011593}
11594
Linus Torvalds1da177e2005-04-16 15:20:36 -070011595/* Chips other than 5700/5701 use the NVRAM for fetching info. */
11596static void __devinit tg3_nvram_init(struct tg3 *tp)
11597{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011598 tw32_f(GRC_EEPROM_ADDR,
11599 (EEPROM_ADDR_FSM_RESET |
11600 (EEPROM_DEFAULT_CLOCK_PERIOD <<
11601 EEPROM_ADDR_CLKPERD_SHIFT)));
11602
Michael Chan9d57f012006-12-07 00:23:25 -080011603 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011604
11605 /* Enable seeprom accesses. */
11606 tw32_f(GRC_LOCAL_CTRL,
11607 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
11608 udelay(100);
11609
11610 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
11611 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
11612 tp->tg3_flags |= TG3_FLAG_NVRAM;
11613
Michael Chanec41c7d2006-01-17 02:40:55 -080011614 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000011615 netdev_warn(tp->dev,
11616 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000011617 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080011618 return;
11619 }
Michael Chane6af3012005-04-21 17:12:05 -070011620 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011621
Matt Carlson989a9d22007-05-05 11:51:05 -070011622 tp->nvram_size = 0;
11623
Michael Chan361b4ac2005-04-21 17:11:21 -070011624 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
11625 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080011626 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
11627 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070011628 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070011629 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
11630 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080011631 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070011632 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
11633 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070011634 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
11635 tg3_get_5906_nvram_info(tp);
Matt Carlsonb703df62009-12-03 08:36:21 +000011636 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
11637 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson321d32a2008-11-21 17:22:19 -080011638 tg3_get_57780_nvram_info(tp);
Matt Carlsona50d0792010-06-05 17:24:37 +000011639 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
11640 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000011641 tg3_get_5717_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070011642 else
11643 tg3_get_nvram_info(tp);
11644
Matt Carlson989a9d22007-05-05 11:51:05 -070011645 if (tp->nvram_size == 0)
11646 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011647
Michael Chane6af3012005-04-21 17:12:05 -070011648 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080011649 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011650
11651 } else {
11652 tp->tg3_flags &= ~(TG3_FLAG_NVRAM | TG3_FLAG_NVRAM_BUFFERED);
11653
11654 tg3_get_eeprom_size(tp);
11655 }
11656}
11657
Linus Torvalds1da177e2005-04-16 15:20:36 -070011658static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
11659 u32 offset, u32 len, u8 *buf)
11660{
11661 int i, j, rc = 0;
11662 u32 val;
11663
11664 for (i = 0; i < len; i += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080011665 u32 addr;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011666 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011667
11668 addr = offset + i;
11669
11670 memcpy(&data, buf + i, 4);
11671
Matt Carlson62cedd12009-04-20 14:52:29 -070011672 /*
11673 * The SEEPROM interface expects the data to always be opposite
11674 * the native endian format. We accomplish this by reversing
11675 * all the operations that would have been performed on the
11676 * data from a call to tg3_nvram_read_be32().
11677 */
11678 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011679
11680 val = tr32(GRC_EEPROM_ADDR);
11681 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
11682
11683 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
11684 EEPROM_ADDR_READ);
11685 tw32(GRC_EEPROM_ADDR, val |
11686 (0 << EEPROM_ADDR_DEVID_SHIFT) |
11687 (addr & EEPROM_ADDR_ADDR_MASK) |
11688 EEPROM_ADDR_START |
11689 EEPROM_ADDR_WRITE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011690
Michael Chan9d57f012006-12-07 00:23:25 -080011691 for (j = 0; j < 1000; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011692 val = tr32(GRC_EEPROM_ADDR);
11693
11694 if (val & EEPROM_ADDR_COMPLETE)
11695 break;
Michael Chan9d57f012006-12-07 00:23:25 -080011696 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011697 }
11698 if (!(val & EEPROM_ADDR_COMPLETE)) {
11699 rc = -EBUSY;
11700 break;
11701 }
11702 }
11703
11704 return rc;
11705}
11706
11707/* offset and length are dword aligned */
11708static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
11709 u8 *buf)
11710{
11711 int ret = 0;
11712 u32 pagesize = tp->nvram_pagesize;
11713 u32 pagemask = pagesize - 1;
11714 u32 nvram_cmd;
11715 u8 *tmp;
11716
11717 tmp = kmalloc(pagesize, GFP_KERNEL);
11718 if (tmp == NULL)
11719 return -ENOMEM;
11720
11721 while (len) {
11722 int j;
Michael Chane6af3012005-04-21 17:12:05 -070011723 u32 phy_addr, page_off, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011724
11725 phy_addr = offset & ~pagemask;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011726
Linus Torvalds1da177e2005-04-16 15:20:36 -070011727 for (j = 0; j < pagesize; j += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000011728 ret = tg3_nvram_read_be32(tp, phy_addr + j,
11729 (__be32 *) (tmp + j));
11730 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011731 break;
11732 }
11733 if (ret)
11734 break;
11735
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011736 page_off = offset & pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011737 size = pagesize;
11738 if (len < size)
11739 size = len;
11740
11741 len -= size;
11742
11743 memcpy(tmp + page_off, buf, size);
11744
11745 offset = offset + (pagesize - page_off);
11746
Michael Chane6af3012005-04-21 17:12:05 -070011747 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011748
11749 /*
11750 * Before we can erase the flash page, we need
11751 * to issue a special "write enable" command.
11752 */
11753 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
11754
11755 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
11756 break;
11757
11758 /* Erase the target page */
11759 tw32(NVRAM_ADDR, phy_addr);
11760
11761 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
11762 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
11763
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011764 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011765 break;
11766
11767 /* Issue another write enable to start the write. */
11768 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
11769
11770 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
11771 break;
11772
11773 for (j = 0; j < pagesize; j += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080011774 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011775
Al Virob9fc7dc2007-12-17 22:59:57 -080011776 data = *((__be32 *) (tmp + j));
Matt Carlsona9dc5292009-02-25 14:25:30 +000011777
Al Virob9fc7dc2007-12-17 22:59:57 -080011778 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011779
11780 tw32(NVRAM_ADDR, phy_addr + j);
11781
11782 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
11783 NVRAM_CMD_WR;
11784
11785 if (j == 0)
11786 nvram_cmd |= NVRAM_CMD_FIRST;
11787 else if (j == (pagesize - 4))
11788 nvram_cmd |= NVRAM_CMD_LAST;
11789
11790 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
11791 break;
11792 }
11793 if (ret)
11794 break;
11795 }
11796
11797 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
11798 tg3_nvram_exec_cmd(tp, nvram_cmd);
11799
11800 kfree(tmp);
11801
11802 return ret;
11803}
11804
11805/* offset and length are dword aligned */
11806static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
11807 u8 *buf)
11808{
11809 int i, ret = 0;
11810
11811 for (i = 0; i < len; i += 4, offset += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080011812 u32 page_off, phy_addr, nvram_cmd;
11813 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011814
11815 memcpy(&data, buf + i, 4);
Al Virob9fc7dc2007-12-17 22:59:57 -080011816 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011817
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011818 page_off = offset % tp->nvram_pagesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011819
Michael Chan18201802006-03-20 22:29:15 -080011820 phy_addr = tg3_nvram_phys_addr(tp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011821
11822 tw32(NVRAM_ADDR, phy_addr);
11823
11824 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
11825
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011826 if (page_off == 0 || i == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011827 nvram_cmd |= NVRAM_CMD_FIRST;
Michael Chanf6d9a252006-04-29 19:00:24 -070011828 if (page_off == (tp->nvram_pagesize - 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011829 nvram_cmd |= NVRAM_CMD_LAST;
11830
11831 if (i == (len - 4))
11832 nvram_cmd |= NVRAM_CMD_LAST;
11833
Matt Carlson321d32a2008-11-21 17:22:19 -080011834 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
11835 !(tp->tg3_flags3 & TG3_FLG3_5755_PLUS) &&
Michael Chan4c987482005-09-05 17:52:38 -070011836 (tp->nvram_jedecnum == JEDEC_ST) &&
11837 (nvram_cmd & NVRAM_CMD_FIRST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011838
11839 if ((ret = tg3_nvram_exec_cmd(tp,
11840 NVRAM_CMD_WREN | NVRAM_CMD_GO |
11841 NVRAM_CMD_DONE)))
11842
11843 break;
11844 }
11845 if (!(tp->tg3_flags2 & TG3_FLG2_FLASH)) {
11846 /* We always do complete word writes to eeprom. */
11847 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
11848 }
11849
11850 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
11851 break;
11852 }
11853 return ret;
11854}
11855
11856/* offset and length are dword aligned */
11857static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
11858{
11859 int ret;
11860
Linus Torvalds1da177e2005-04-16 15:20:36 -070011861 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT) {
Michael Chan314fba32005-04-21 17:07:04 -070011862 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
11863 ~GRC_LCLCTRL_GPIO_OUTPUT1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011864 udelay(40);
11865 }
11866
11867 if (!(tp->tg3_flags & TG3_FLAG_NVRAM)) {
11868 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
Matt Carlson859a588792010-04-05 10:19:28 +000011869 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011870 u32 grc_mode;
11871
Michael Chanec41c7d2006-01-17 02:40:55 -080011872 ret = tg3_nvram_lock(tp);
11873 if (ret)
11874 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011875
Michael Chane6af3012005-04-21 17:12:05 -070011876 tg3_enable_nvram_access(tp);
11877 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011878 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011879 tw32(NVRAM_WRITE1, 0x406);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011880
11881 grc_mode = tr32(GRC_MODE);
11882 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
11883
11884 if ((tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) ||
11885 !(tp->tg3_flags2 & TG3_FLG2_FLASH)) {
11886
11887 ret = tg3_nvram_write_block_buffered(tp, offset, len,
11888 buf);
Matt Carlson859a588792010-04-05 10:19:28 +000011889 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011890 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
11891 buf);
11892 }
11893
11894 grc_mode = tr32(GRC_MODE);
11895 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
11896
Michael Chane6af3012005-04-21 17:12:05 -070011897 tg3_disable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011898 tg3_nvram_unlock(tp);
11899 }
11900
11901 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT) {
Michael Chan314fba32005-04-21 17:07:04 -070011902 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011903 udelay(40);
11904 }
11905
11906 return ret;
11907}
11908
11909struct subsys_tbl_ent {
11910 u16 subsys_vendor, subsys_devid;
11911 u32 phy_id;
11912};
11913
Matt Carlson24daf2b2010-02-17 15:17:02 +000011914static struct subsys_tbl_ent subsys_id_to_phy_id[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011915 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000011916 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011917 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011918 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011919 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011920 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011921 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011922 { TG3PCI_SUBVENDOR_ID_BROADCOM,
11923 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
11924 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011925 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011926 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011927 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011928 { TG3PCI_SUBVENDOR_ID_BROADCOM,
11929 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
11930 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011931 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011932 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011933 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011934 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011935 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011936 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011937 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070011938
11939 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000011940 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011941 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011942 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011943 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011944 { TG3PCI_SUBVENDOR_ID_3COM,
11945 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
11946 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011947 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011948 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011949 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070011950
11951 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000011952 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000011953 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011954 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000011955 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011956 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000011957 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011958 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000011959 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070011960
11961 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000011962 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000011963 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011964 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000011965 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011966 { TG3PCI_SUBVENDOR_ID_COMPAQ,
11967 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
11968 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000011969 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011970 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000011971 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070011972
11973 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000011974 { TG3PCI_SUBVENDOR_ID_IBM,
11975 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011976};
11977
Matt Carlson24daf2b2010-02-17 15:17:02 +000011978static struct subsys_tbl_ent * __devinit tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011979{
11980 int i;
11981
11982 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
11983 if ((subsys_id_to_phy_id[i].subsys_vendor ==
11984 tp->pdev->subsystem_vendor) &&
11985 (subsys_id_to_phy_id[i].subsys_devid ==
11986 tp->pdev->subsystem_device))
11987 return &subsys_id_to_phy_id[i];
11988 }
11989 return NULL;
11990}
11991
Michael Chan7d0c41e2005-04-21 17:06:20 -070011992static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011993{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011994 u32 val;
Michael Chancaf636c72006-03-22 01:05:31 -080011995 u16 pmcsr;
11996
11997 /* On some early chips the SRAM cannot be accessed in D3hot state,
11998 * so need make sure we're in D0.
11999 */
12000 pci_read_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, &pmcsr);
12001 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
12002 pci_write_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, pmcsr);
12003 msleep(1);
Michael Chan7d0c41e2005-04-21 17:06:20 -070012004
12005 /* Make sure register accesses (indirect or otherwise)
12006 * will function correctly.
12007 */
12008 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
12009 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012010
David S. Millerf49639e2006-06-09 11:58:36 -070012011 /* The memory arbiter has to be enabled in order for SRAM accesses
12012 * to succeed. Normally on powerup the tg3 chip firmware will make
12013 * sure it is enabled, but other entities such as system netboot
12014 * code might disable it.
12015 */
12016 val = tr32(MEMARB_MODE);
12017 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
12018
Matt Carlson79eb6902010-02-17 15:17:03 +000012019 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012020 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12021
Gary Zambranoa85feb82007-05-05 11:52:19 -070012022 /* Assume an onboard device and WOL capable by default. */
12023 tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT | TG3_FLAG_WOL_CAP;
David S. Miller72b845e2006-03-14 14:11:48 -080012024
Michael Chanb5d37722006-09-27 16:06:21 -070012025 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080012026 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Michael Chanb5d37722006-09-27 16:06:21 -070012027 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080012028 tp->tg3_flags2 |= TG3_FLG2_IS_NIC;
12029 }
Matt Carlson0527ba32007-10-10 18:03:30 -070012030 val = tr32(VCPU_CFGSHDW);
12031 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Matt Carlson8ed5d972007-05-07 00:25:49 -070012032 tp->tg3_flags |= TG3_FLAG_ASPM_WORKAROUND;
Matt Carlson0527ba32007-10-10 18:03:30 -070012033 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Matt Carlson20232762008-12-21 20:18:56 -080012034 (val & VCPU_CFGSHDW_WOL_MAGPKT))
Matt Carlson0527ba32007-10-10 18:03:30 -070012035 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012036 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070012037 }
12038
Linus Torvalds1da177e2005-04-16 15:20:36 -070012039 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
12040 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
12041 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070012042 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012043 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012044
12045 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
12046 tp->nic_sram_data_cfg = nic_cfg;
12047
12048 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
12049 ver >>= NIC_SRAM_DATA_VER_SHIFT;
12050 if ((GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700) &&
12051 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) &&
12052 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703) &&
12053 (ver > 0) && (ver < 0x100))
12054 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
12055
Matt Carlsona9daf362008-05-25 23:49:44 -070012056 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
12057 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
12058
Linus Torvalds1da177e2005-04-16 15:20:36 -070012059 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
12060 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
12061 eeprom_phy_serdes = 1;
12062
12063 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
12064 if (nic_phy_id != 0) {
12065 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
12066 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
12067
12068 eeprom_phy_id = (id1 >> 16) << 10;
12069 eeprom_phy_id |= (id2 & 0xfc00) << 16;
12070 eeprom_phy_id |= (id2 & 0x03ff) << 0;
12071 } else
12072 eeprom_phy_id = 0;
12073
Michael Chan7d0c41e2005-04-21 17:06:20 -070012074 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070012075 if (eeprom_phy_serdes) {
Matt Carlsona50d0792010-06-05 17:24:37 +000012076 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Michael Chan747e8f82005-07-25 12:33:22 -070012077 tp->tg3_flags2 |= TG3_FLG2_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000012078 else
12079 tp->tg3_flags2 |= TG3_FLG2_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070012080 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070012081
John W. Linvillecbf46852005-04-21 17:01:29 -070012082 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012083 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
12084 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070012085 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070012086 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
12087
12088 switch (led_cfg) {
12089 default:
12090 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
12091 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12092 break;
12093
12094 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
12095 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12096 break;
12097
12098 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
12099 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070012100
12101 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
12102 * read on some older 5700/5701 bootcode.
12103 */
12104 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
12105 ASIC_REV_5700 ||
12106 GET_ASIC_REV(tp->pci_chip_rev_id) ==
12107 ASIC_REV_5701)
12108 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12109
Linus Torvalds1da177e2005-04-16 15:20:36 -070012110 break;
12111
12112 case SHASTA_EXT_LED_SHARED:
12113 tp->led_ctrl = LED_CTRL_MODE_SHARED;
12114 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
12115 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
12116 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12117 LED_CTRL_MODE_PHY_2);
12118 break;
12119
12120 case SHASTA_EXT_LED_MAC:
12121 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
12122 break;
12123
12124 case SHASTA_EXT_LED_COMBO:
12125 tp->led_ctrl = LED_CTRL_MODE_COMBO;
12126 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
12127 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12128 LED_CTRL_MODE_PHY_2);
12129 break;
12130
Stephen Hemminger855e1112008-04-16 16:37:28 -070012131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012132
12133 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
12134 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
12135 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
12136 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12137
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012138 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
12139 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080012140
Michael Chan9d26e212006-12-07 00:21:14 -080012141 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012142 tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080012143 if ((tp->pdev->subsystem_vendor ==
12144 PCI_VENDOR_ID_ARIMA) &&
12145 (tp->pdev->subsystem_device == 0x205a ||
12146 tp->pdev->subsystem_device == 0x2063))
12147 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
12148 } else {
David S. Millerf49639e2006-06-09 11:58:36 -070012149 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080012150 tp->tg3_flags2 |= TG3_FLG2_IS_NIC;
12151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012152
12153 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
12154 tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
John W. Linvillecbf46852005-04-21 17:01:29 -070012155 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012156 tp->tg3_flags2 |= TG3_FLG2_ASF_NEW_HANDSHAKE;
12157 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012158
12159 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
12160 (tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
Matt Carlson0d3031d2007-10-10 18:02:43 -070012161 tp->tg3_flags3 |= TG3_FLG3_ENABLE_APE;
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012162
Gary Zambranoa85feb82007-05-05 11:52:19 -070012163 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES &&
12164 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
12165 tp->tg3_flags &= ~TG3_FLAG_WOL_CAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012166
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070012167 if ((tp->tg3_flags & TG3_FLAG_WOL_CAP) &&
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012168 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE))
Matt Carlson0527ba32007-10-10 18:03:30 -070012169 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
12170
Linus Torvalds1da177e2005-04-16 15:20:36 -070012171 if (cfg2 & (1 << 17))
12172 tp->tg3_flags2 |= TG3_FLG2_CAPACITIVE_COUPLING;
12173
12174 /* serdes signal pre-emphasis in register 0x590 set by */
12175 /* bootcode if bit 18 is set */
12176 if (cfg2 & (1 << 18))
12177 tp->tg3_flags2 |= TG3_FLG2_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070012178
Matt Carlson321d32a2008-11-21 17:22:19 -080012179 if (((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
12180 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080012181 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
12182 tp->tg3_flags3 |= TG3_FLG3_PHY_ENABLE_APD;
12183
Matt Carlson8ed5d972007-05-07 00:25:49 -070012184 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
12185 u32 cfg3;
12186
12187 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
12188 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
12189 tp->tg3_flags |= TG3_FLAG_ASPM_WORKAROUND;
12190 }
Matt Carlsona9daf362008-05-25 23:49:44 -070012191
Matt Carlson14417062010-02-17 15:16:59 +000012192 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
12193 tp->tg3_flags3 |= TG3_FLG3_RGMII_INBAND_DISABLE;
Matt Carlsona9daf362008-05-25 23:49:44 -070012194 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
12195 tp->tg3_flags3 |= TG3_FLG3_RGMII_EXT_IBND_RX_EN;
12196 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
12197 tp->tg3_flags3 |= TG3_FLG3_RGMII_EXT_IBND_TX_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012198 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012199done:
12200 device_init_wakeup(&tp->pdev->dev, tp->tg3_flags & TG3_FLAG_WOL_CAP);
12201 device_set_wakeup_enable(&tp->pdev->dev,
12202 tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
Michael Chan7d0c41e2005-04-21 17:06:20 -070012203}
12204
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012205static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
12206{
12207 int i;
12208 u32 val;
12209
12210 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
12211 tw32(OTP_CTRL, cmd);
12212
12213 /* Wait for up to 1 ms for command to execute. */
12214 for (i = 0; i < 100; i++) {
12215 val = tr32(OTP_STATUS);
12216 if (val & OTP_STATUS_CMD_DONE)
12217 break;
12218 udelay(10);
12219 }
12220
12221 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
12222}
12223
12224/* Read the gphy configuration from the OTP region of the chip. The gphy
12225 * configuration is a 32-bit value that straddles the alignment boundary.
12226 * We do two 32-bit reads and then shift and merge the results.
12227 */
12228static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
12229{
12230 u32 bhalf_otp, thalf_otp;
12231
12232 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
12233
12234 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
12235 return 0;
12236
12237 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
12238
12239 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12240 return 0;
12241
12242 thalf_otp = tr32(OTP_READ_DATA);
12243
12244 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
12245
12246 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12247 return 0;
12248
12249 bhalf_otp = tr32(OTP_READ_DATA);
12250
12251 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
12252}
12253
Michael Chan7d0c41e2005-04-21 17:06:20 -070012254static int __devinit tg3_phy_probe(struct tg3 *tp)
12255{
12256 u32 hw_phy_id_1, hw_phy_id_2;
12257 u32 hw_phy_id, hw_phy_id_masked;
12258 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012259
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012260 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)
12261 return tg3_phy_init(tp);
12262
Linus Torvalds1da177e2005-04-16 15:20:36 -070012263 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010012264 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012265 */
12266 err = 0;
Matt Carlson0d3031d2007-10-10 18:02:43 -070012267 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
12268 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000012269 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012270 } else {
12271 /* Now read the physical PHY_ID from the chip and verify
12272 * that it is sane. If it doesn't look good, we fall back
12273 * to either the hard-coded table based PHY_ID and failing
12274 * that the value found in the eeprom area.
12275 */
12276 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
12277 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
12278
12279 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
12280 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
12281 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
12282
Matt Carlson79eb6902010-02-17 15:17:03 +000012283 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012284 }
12285
Matt Carlson79eb6902010-02-17 15:17:03 +000012286 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012287 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000012288 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012289 tp->tg3_flags2 |= TG3_FLG2_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070012290 else
12291 tp->tg3_flags2 &= ~TG3_FLG2_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012292 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000012293 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070012294 /* Do nothing, phy ID already set up in
12295 * tg3_get_eeprom_hw_cfg().
12296 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070012297 } else {
12298 struct subsys_tbl_ent *p;
12299
12300 /* No eeprom signature? Try the hardcoded
12301 * subsys device table.
12302 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012303 p = tg3_lookup_by_subsys(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012304 if (!p)
12305 return -ENODEV;
12306
12307 tp->phy_id = p->phy_id;
12308 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000012309 tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012310 tp->tg3_flags2 |= TG3_FLG2_PHY_SERDES;
12311 }
12312 }
12313
Michael Chan747e8f82005-07-25 12:33:22 -070012314 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES) &&
Matt Carlson0d3031d2007-10-10 18:02:43 -070012315 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070012316 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chan3600d912006-12-07 00:21:48 -080012317 u32 bmsr, adv_reg, tg3_ctrl, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012318
12319 tg3_readphy(tp, MII_BMSR, &bmsr);
12320 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
12321 (bmsr & BMSR_LSTATUS))
12322 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012323
Linus Torvalds1da177e2005-04-16 15:20:36 -070012324 err = tg3_phy_reset(tp);
12325 if (err)
12326 return err;
12327
12328 adv_reg = (ADVERTISE_10HALF | ADVERTISE_10FULL |
12329 ADVERTISE_100HALF | ADVERTISE_100FULL |
12330 ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
12331 tg3_ctrl = 0;
12332 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY)) {
12333 tg3_ctrl = (MII_TG3_CTRL_ADV_1000_HALF |
12334 MII_TG3_CTRL_ADV_1000_FULL);
12335 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
12336 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
12337 tg3_ctrl |= (MII_TG3_CTRL_AS_MASTER |
12338 MII_TG3_CTRL_ENABLE_AS_MASTER);
12339 }
12340
Michael Chan3600d912006-12-07 00:21:48 -080012341 mask = (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
12342 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
12343 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full);
12344 if (!tg3_copper_is_advertising_all(tp, mask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012345 tg3_writephy(tp, MII_ADVERTISE, adv_reg);
12346
12347 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY))
12348 tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
12349
12350 tg3_writephy(tp, MII_BMCR,
12351 BMCR_ANENABLE | BMCR_ANRESTART);
12352 }
12353 tg3_phy_set_wirespeed(tp);
12354
12355 tg3_writephy(tp, MII_ADVERTISE, adv_reg);
12356 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY))
12357 tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
12358 }
12359
12360skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000012361 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012362 err = tg3_init_5401phy_dsp(tp);
12363 if (err)
12364 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012365
Linus Torvalds1da177e2005-04-16 15:20:36 -070012366 err = tg3_init_5401phy_dsp(tp);
12367 }
12368
Michael Chan747e8f82005-07-25 12:33:22 -070012369 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012370 tp->link_config.advertising =
12371 (ADVERTISED_1000baseT_Half |
12372 ADVERTISED_1000baseT_Full |
12373 ADVERTISED_Autoneg |
12374 ADVERTISED_FIBRE);
12375 if (tp->tg3_flags & TG3_FLAG_10_100_ONLY)
12376 tp->link_config.advertising &=
12377 ~(ADVERTISED_1000baseT_Half |
12378 ADVERTISED_1000baseT_Full);
12379
12380 return err;
12381}
12382
Matt Carlson184b8902010-04-05 10:19:25 +000012383static void __devinit tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012384{
Matt Carlson184b8902010-04-05 10:19:25 +000012385 u8 vpd_data[TG3_NVM_VPD_LEN];
Matt Carlson4181b2c2010-02-26 14:04:45 +000012386 unsigned int block_end, rosize, len;
Matt Carlson184b8902010-04-05 10:19:25 +000012387 int j, i = 0;
Michael Chan1b277772006-03-20 22:27:48 -080012388 u32 magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012389
Matt Carlsondf259d82009-04-20 06:57:14 +000012390 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
12391 tg3_nvram_read(tp, 0x0, &magic))
David S. Millerf49639e2006-06-09 11:58:36 -070012392 goto out_not_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012393
Michael Chan18201802006-03-20 22:29:15 -080012394 if (magic == TG3_EEPROM_MAGIC) {
Matt Carlson141518c2009-12-03 08:36:22 +000012395 for (i = 0; i < TG3_NVM_VPD_LEN; i += 4) {
Michael Chan1b277772006-03-20 22:27:48 -080012396 u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012397
Matt Carlson6d348f22009-02-25 14:25:52 +000012398 /* The data is in little-endian format in NVRAM.
12399 * Use the big-endian read routines to preserve
12400 * the byte order as it exists in NVRAM.
12401 */
Matt Carlson141518c2009-12-03 08:36:22 +000012402 if (tg3_nvram_read_be32(tp, TG3_NVM_VPD_OFF + i, &tmp))
Michael Chan1b277772006-03-20 22:27:48 -080012403 goto out_not_found;
12404
Matt Carlson6d348f22009-02-25 14:25:52 +000012405 memcpy(&vpd_data[i], &tmp, sizeof(tmp));
Michael Chan1b277772006-03-20 22:27:48 -080012406 }
12407 } else {
Matt Carlson94c982b2009-12-03 08:36:23 +000012408 ssize_t cnt;
Matt Carlson4181b2c2010-02-26 14:04:45 +000012409 unsigned int pos = 0;
Michael Chan1b277772006-03-20 22:27:48 -080012410
Matt Carlson94c982b2009-12-03 08:36:23 +000012411 for (; pos < TG3_NVM_VPD_LEN && i < 3; i++, pos += cnt) {
12412 cnt = pci_read_vpd(tp->pdev, pos,
12413 TG3_NVM_VPD_LEN - pos,
12414 &vpd_data[pos]);
12415 if (cnt == -ETIMEDOUT || -EINTR)
12416 cnt = 0;
12417 else if (cnt < 0)
David S. Millerf49639e2006-06-09 11:58:36 -070012418 goto out_not_found;
Michael Chan1b277772006-03-20 22:27:48 -080012419 }
Matt Carlson94c982b2009-12-03 08:36:23 +000012420 if (pos != TG3_NVM_VPD_LEN)
12421 goto out_not_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012422 }
12423
Matt Carlson4181b2c2010-02-26 14:04:45 +000012424 i = pci_vpd_find_tag(vpd_data, 0, TG3_NVM_VPD_LEN,
12425 PCI_VPD_LRDT_RO_DATA);
12426 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012427 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000012428
12429 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
12430 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
12431 i += PCI_VPD_LRDT_TAG_SIZE;
12432
12433 if (block_end > TG3_NVM_VPD_LEN)
12434 goto out_not_found;
12435
Matt Carlson184b8902010-04-05 10:19:25 +000012436 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
12437 PCI_VPD_RO_KEYWORD_MFR_ID);
12438 if (j > 0) {
12439 len = pci_vpd_info_field_size(&vpd_data[j]);
12440
12441 j += PCI_VPD_INFO_FLD_HDR_SIZE;
12442 if (j + len > block_end || len != 4 ||
12443 memcmp(&vpd_data[j], "1028", 4))
12444 goto partno;
12445
12446 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
12447 PCI_VPD_RO_KEYWORD_VENDOR0);
12448 if (j < 0)
12449 goto partno;
12450
12451 len = pci_vpd_info_field_size(&vpd_data[j]);
12452
12453 j += PCI_VPD_INFO_FLD_HDR_SIZE;
12454 if (j + len > block_end)
12455 goto partno;
12456
12457 memcpy(tp->fw_ver, &vpd_data[j], len);
12458 strncat(tp->fw_ver, " bc ", TG3_NVM_VPD_LEN - len - 1);
12459 }
12460
12461partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000012462 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
12463 PCI_VPD_RO_KEYWORD_PARTNO);
12464 if (i < 0)
12465 goto out_not_found;
12466
12467 len = pci_vpd_info_field_size(&vpd_data[i]);
12468
12469 i += PCI_VPD_INFO_FLD_HDR_SIZE;
12470 if (len > TG3_BPN_SIZE ||
12471 (len + i) > TG3_NVM_VPD_LEN)
12472 goto out_not_found;
12473
12474 memcpy(tp->board_part_number, &vpd_data[i], len);
12475
12476 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012477
12478out_not_found:
Michael Chanb5d37722006-09-27 16:06:21 -070012479 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12480 strcpy(tp->board_part_number, "BCM95906");
Matt Carlsondf259d82009-04-20 06:57:14 +000012481 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12482 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
12483 strcpy(tp->board_part_number, "BCM57780");
12484 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12485 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
12486 strcpy(tp->board_part_number, "BCM57760");
12487 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12488 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
12489 strcpy(tp->board_part_number, "BCM57790");
Matt Carlson5e7ccf22009-08-25 10:08:42 +000012490 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12491 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
12492 strcpy(tp->board_part_number, "BCM57788");
Matt Carlsonb474eca2010-02-17 15:16:58 +000012493 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12494 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
12495 strcpy(tp->board_part_number, "BCM57761");
12496 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12497 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
Matt Carlsonb703df62009-12-03 08:36:21 +000012498 strcpy(tp->board_part_number, "BCM57765");
Matt Carlsonb474eca2010-02-17 15:16:58 +000012499 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12500 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
12501 strcpy(tp->board_part_number, "BCM57781");
12502 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12503 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
12504 strcpy(tp->board_part_number, "BCM57785");
12505 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12506 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
12507 strcpy(tp->board_part_number, "BCM57791");
12508 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12509 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
12510 strcpy(tp->board_part_number, "BCM57795");
Michael Chanb5d37722006-09-27 16:06:21 -070012511 else
12512 strcpy(tp->board_part_number, "none");
Linus Torvalds1da177e2005-04-16 15:20:36 -070012513}
12514
Matt Carlson9c8a6202007-10-21 16:16:08 -070012515static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
12516{
12517 u32 val;
12518
Matt Carlsone4f34112009-02-25 14:25:00 +000012519 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070012520 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000012521 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070012522 val != 0)
12523 return 0;
12524
12525 return 1;
12526}
12527
Matt Carlsonacd9c112009-02-25 14:26:33 +000012528static void __devinit tg3_read_bc_ver(struct tg3 *tp)
12529{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012530 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000012531 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012532 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000012533
12534 if (tg3_nvram_read(tp, 0xc, &offset) ||
12535 tg3_nvram_read(tp, 0x4, &start))
12536 return;
12537
12538 offset = tg3_nvram_logical_addr(tp, offset);
12539
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012540 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000012541 return;
12542
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012543 if ((val & 0xfc000000) == 0x0c000000) {
12544 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000012545 return;
12546
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012547 if (val == 0)
12548 newver = true;
12549 }
12550
Matt Carlson75f99362010-04-05 10:19:24 +000012551 dst_off = strlen(tp->fw_ver);
12552
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012553 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000012554 if (TG3_VER_SIZE - dst_off < 16 ||
12555 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012556 return;
12557
12558 offset = offset + ver_offset - start;
12559 for (i = 0; i < 16; i += 4) {
12560 __be32 v;
12561 if (tg3_nvram_read_be32(tp, offset + i, &v))
12562 return;
12563
Matt Carlson75f99362010-04-05 10:19:24 +000012564 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012565 }
12566 } else {
12567 u32 major, minor;
12568
12569 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
12570 return;
12571
12572 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
12573 TG3_NVM_BCVER_MAJSFT;
12574 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000012575 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
12576 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000012577 }
12578}
12579
Matt Carlsona6f6cb12009-02-25 14:27:43 +000012580static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
12581{
12582 u32 val, major, minor;
12583
12584 /* Use native endian representation */
12585 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
12586 return;
12587
12588 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
12589 TG3_NVM_HWSB_CFG1_MAJSFT;
12590 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
12591 TG3_NVM_HWSB_CFG1_MINSFT;
12592
12593 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
12594}
12595
Matt Carlsondfe00d72008-11-21 17:19:41 -080012596static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
12597{
12598 u32 offset, major, minor, build;
12599
Matt Carlson75f99362010-04-05 10:19:24 +000012600 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080012601
12602 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
12603 return;
12604
12605 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
12606 case TG3_EEPROM_SB_REVISION_0:
12607 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
12608 break;
12609 case TG3_EEPROM_SB_REVISION_2:
12610 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
12611 break;
12612 case TG3_EEPROM_SB_REVISION_3:
12613 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
12614 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000012615 case TG3_EEPROM_SB_REVISION_4:
12616 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
12617 break;
12618 case TG3_EEPROM_SB_REVISION_5:
12619 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
12620 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080012621 default:
12622 return;
12623 }
12624
Matt Carlsone4f34112009-02-25 14:25:00 +000012625 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080012626 return;
12627
12628 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
12629 TG3_EEPROM_SB_EDH_BLD_SHFT;
12630 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
12631 TG3_EEPROM_SB_EDH_MAJ_SHFT;
12632 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
12633
12634 if (minor > 99 || build > 26)
12635 return;
12636
Matt Carlson75f99362010-04-05 10:19:24 +000012637 offset = strlen(tp->fw_ver);
12638 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
12639 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080012640
12641 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000012642 offset = strlen(tp->fw_ver);
12643 if (offset < TG3_VER_SIZE - 1)
12644 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080012645 }
12646}
12647
Matt Carlsonacd9c112009-02-25 14:26:33 +000012648static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080012649{
12650 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000012651 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070012652
12653 for (offset = TG3_NVM_DIR_START;
12654 offset < TG3_NVM_DIR_END;
12655 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000012656 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012657 return;
12658
12659 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
12660 break;
12661 }
12662
12663 if (offset == TG3_NVM_DIR_END)
12664 return;
12665
12666 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
12667 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000012668 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012669 return;
12670
Matt Carlsone4f34112009-02-25 14:25:00 +000012671 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070012672 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000012673 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012674 return;
12675
12676 offset += val - start;
12677
Matt Carlsonacd9c112009-02-25 14:26:33 +000012678 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012679
Matt Carlsonacd9c112009-02-25 14:26:33 +000012680 tp->fw_ver[vlen++] = ',';
12681 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070012682
12683 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012684 __be32 v;
12685 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012686 return;
12687
Al Virob9fc7dc2007-12-17 22:59:57 -080012688 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012689
Matt Carlsonacd9c112009-02-25 14:26:33 +000012690 if (vlen > TG3_VER_SIZE - sizeof(v)) {
12691 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012692 break;
12693 }
12694
Matt Carlsonacd9c112009-02-25 14:26:33 +000012695 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
12696 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012697 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000012698}
12699
Matt Carlson7fd76442009-02-25 14:27:20 +000012700static void __devinit tg3_read_dash_ver(struct tg3 *tp)
12701{
12702 int vlen;
12703 u32 apedata;
12704
12705 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) ||
12706 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
12707 return;
12708
12709 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
12710 if (apedata != APE_SEG_SIG_MAGIC)
12711 return;
12712
12713 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
12714 if (!(apedata & APE_FW_STATUS_READY))
12715 return;
12716
12717 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
12718
12719 vlen = strlen(tp->fw_ver);
12720
12721 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " DASH v%d.%d.%d.%d",
12722 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
12723 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
12724 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
12725 (apedata & APE_FW_VERSION_BLDMSK));
12726}
12727
Matt Carlsonacd9c112009-02-25 14:26:33 +000012728static void __devinit tg3_read_fw_ver(struct tg3 *tp)
12729{
12730 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000012731 bool vpd_vers = false;
12732
12733 if (tp->fw_ver[0] != 0)
12734 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000012735
Matt Carlsondf259d82009-04-20 06:57:14 +000012736 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) {
Matt Carlson75f99362010-04-05 10:19:24 +000012737 strcat(tp->fw_ver, "sb");
Matt Carlsondf259d82009-04-20 06:57:14 +000012738 return;
12739 }
12740
Matt Carlsonacd9c112009-02-25 14:26:33 +000012741 if (tg3_nvram_read(tp, 0, &val))
12742 return;
12743
12744 if (val == TG3_EEPROM_MAGIC)
12745 tg3_read_bc_ver(tp);
12746 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
12747 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000012748 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
12749 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000012750 else
12751 return;
12752
12753 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
Matt Carlson75f99362010-04-05 10:19:24 +000012754 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) || vpd_vers)
12755 goto done;
Matt Carlsonacd9c112009-02-25 14:26:33 +000012756
12757 tg3_read_mgmtfw_ver(tp);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012758
Matt Carlson75f99362010-04-05 10:19:24 +000012759done:
Matt Carlson9c8a6202007-10-21 16:16:08 -070012760 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080012761}
12762
Michael Chan7544b092007-05-05 13:08:32 -070012763static struct pci_dev * __devinit tg3_find_peer(struct tg3 *);
12764
Eric Dumazet7fe876a2010-07-08 06:14:55 +000012765static void inline vlan_features_add(struct net_device *dev, unsigned long flags)
12766{
12767#if TG3_VLAN_TAG_USED
12768 dev->vlan_features |= flags;
12769#endif
12770}
12771
Linus Torvalds1da177e2005-04-16 15:20:36 -070012772static int __devinit tg3_get_invariants(struct tg3 *tp)
12773{
12774 static struct pci_device_id write_reorder_chipsets[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012775 { PCI_DEVICE(PCI_VENDOR_ID_AMD,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012776 PCI_DEVICE_ID_AMD_FE_GATE_700C) },
John W. Linvillec165b002006-07-08 13:28:53 -070012777 { PCI_DEVICE(PCI_VENDOR_ID_AMD,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012778 PCI_DEVICE_ID_AMD_8131_BRIDGE) },
Michael Chan399de502005-10-03 14:02:39 -070012779 { PCI_DEVICE(PCI_VENDOR_ID_VIA,
12780 PCI_DEVICE_ID_VIA_8385_0) },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012781 { },
12782 };
12783 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012784 u32 pci_state_reg, grc_misc_cfg;
12785 u32 val;
12786 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080012787 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012788
Linus Torvalds1da177e2005-04-16 15:20:36 -070012789 /* Force memory write invalidate off. If we leave it on,
12790 * then on 5700_BX chips we have to enable a workaround.
12791 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
12792 * to match the cacheline size. The Broadcom driver have this
12793 * workaround but turns MWI off all the times so never uses
12794 * it. This seems to suggest that the workaround is insufficient.
12795 */
12796 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
12797 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
12798 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
12799
12800 /* It is absolutely critical that TG3PCI_MISC_HOST_CTRL
12801 * has the register indirect write enable bit set before
12802 * we try to access any of the MMIO registers. It is also
12803 * critical that the PCI-X hw workaround situation is decided
12804 * before that as well.
12805 */
12806 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
12807 &misc_ctrl_reg);
12808
12809 tp->pci_chip_rev_id = (misc_ctrl_reg >>
12810 MISC_HOST_CTRL_CHIPREV_SHIFT);
Matt Carlson795d01c2007-10-07 23:28:17 -070012811 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
12812 u32 prod_id_asic_rev;
12813
Matt Carlson5001e2f2009-11-13 13:03:51 +000012814 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
12815 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
Matt Carlsona50d0792010-06-05 17:24:37 +000012816 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5724 ||
12817 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012818 pci_read_config_dword(tp->pdev,
12819 TG3PCI_GEN2_PRODID_ASICREV,
12820 &prod_id_asic_rev);
Matt Carlsonb703df62009-12-03 08:36:21 +000012821 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
12822 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
12823 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
12824 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
12825 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
12826 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
12827 pci_read_config_dword(tp->pdev,
12828 TG3PCI_GEN15_PRODID_ASICREV,
12829 &prod_id_asic_rev);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012830 else
12831 pci_read_config_dword(tp->pdev, TG3PCI_PRODID_ASICREV,
12832 &prod_id_asic_rev);
12833
Matt Carlson321d32a2008-11-21 17:22:19 -080012834 tp->pci_chip_rev_id = prod_id_asic_rev;
Matt Carlson795d01c2007-10-07 23:28:17 -070012835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012836
Michael Chanff645be2005-04-21 17:09:53 -070012837 /* Wrong chip ID in 5752 A0. This code can be removed later
12838 * as A0 is not in production.
12839 */
12840 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
12841 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
12842
Michael Chan68929142005-08-09 20:17:14 -070012843 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
12844 * we need to disable memory and use config. cycles
12845 * only to access all registers. The 5702/03 chips
12846 * can mistakenly decode the special cycles from the
12847 * ICH chipsets as memory write cycles, causing corruption
12848 * of register and memory space. Only certain ICH bridges
12849 * will drive special cycles with non-zero data during the
12850 * address phase which can fall within the 5703's address
12851 * range. This is not an ICH bug as the PCI spec allows
12852 * non-zero address during special cycles. However, only
12853 * these ICH bridges are known to drive non-zero addresses
12854 * during special cycles.
12855 *
12856 * Since special cycles do not cross PCI bridges, we only
12857 * enable this workaround if the 5703 is on the secondary
12858 * bus of these ICH bridges.
12859 */
12860 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
12861 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
12862 static struct tg3_dev_id {
12863 u32 vendor;
12864 u32 device;
12865 u32 rev;
12866 } ich_chipsets[] = {
12867 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
12868 PCI_ANY_ID },
12869 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
12870 PCI_ANY_ID },
12871 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
12872 0xa },
12873 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
12874 PCI_ANY_ID },
12875 { },
12876 };
12877 struct tg3_dev_id *pci_id = &ich_chipsets[0];
12878 struct pci_dev *bridge = NULL;
12879
12880 while (pci_id->vendor != 0) {
12881 bridge = pci_get_device(pci_id->vendor, pci_id->device,
12882 bridge);
12883 if (!bridge) {
12884 pci_id++;
12885 continue;
12886 }
12887 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070012888 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070012889 continue;
12890 }
12891 if (bridge->subordinate &&
12892 (bridge->subordinate->number ==
12893 tp->pdev->bus->number)) {
12894
12895 tp->tg3_flags2 |= TG3_FLG2_ICH_WORKAROUND;
12896 pci_dev_put(bridge);
12897 break;
12898 }
12899 }
12900 }
12901
Matt Carlson41588ba2008-04-19 18:12:33 -070012902 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
12903 static struct tg3_dev_id {
12904 u32 vendor;
12905 u32 device;
12906 } bridge_chipsets[] = {
12907 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
12908 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
12909 { },
12910 };
12911 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
12912 struct pci_dev *bridge = NULL;
12913
12914 while (pci_id->vendor != 0) {
12915 bridge = pci_get_device(pci_id->vendor,
12916 pci_id->device,
12917 bridge);
12918 if (!bridge) {
12919 pci_id++;
12920 continue;
12921 }
12922 if (bridge->subordinate &&
12923 (bridge->subordinate->number <=
12924 tp->pdev->bus->number) &&
12925 (bridge->subordinate->subordinate >=
12926 tp->pdev->bus->number)) {
12927 tp->tg3_flags3 |= TG3_FLG3_5701_DMA_BUG;
12928 pci_dev_put(bridge);
12929 break;
12930 }
12931 }
12932 }
12933
Michael Chan4a29cc22006-03-19 13:21:12 -080012934 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
12935 * DMA addresses > 40-bit. This bridge may have other additional
12936 * 57xx devices behind it in some 4-port NIC designs for example.
12937 * Any tg3 device found behind the bridge will also need the 40-bit
12938 * DMA workaround.
12939 */
Michael Chana4e2b342005-10-26 15:46:52 -070012940 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
12941 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
12942 tp->tg3_flags2 |= TG3_FLG2_5780_CLASS;
Michael Chan4a29cc22006-03-19 13:21:12 -080012943 tp->tg3_flags |= TG3_FLAG_40BIT_DMA_BUG;
Michael Chan4cf78e42005-07-25 12:29:19 -070012944 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a588792010-04-05 10:19:28 +000012945 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080012946 struct pci_dev *bridge = NULL;
12947
12948 do {
12949 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
12950 PCI_DEVICE_ID_SERVERWORKS_EPB,
12951 bridge);
12952 if (bridge && bridge->subordinate &&
12953 (bridge->subordinate->number <=
12954 tp->pdev->bus->number) &&
12955 (bridge->subordinate->subordinate >=
12956 tp->pdev->bus->number)) {
12957 tp->tg3_flags |= TG3_FLAG_40BIT_DMA_BUG;
12958 pci_dev_put(bridge);
12959 break;
12960 }
12961 } while (bridge);
12962 }
Michael Chan4cf78e42005-07-25 12:29:19 -070012963
Linus Torvalds1da177e2005-04-16 15:20:36 -070012964 /* Initialize misc host control in PCI block. */
12965 tp->misc_host_ctrl |= (misc_ctrl_reg &
12966 MISC_HOST_CTRL_CHIPREV);
12967 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
12968 tp->misc_host_ctrl);
12969
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012970 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
12971 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
12972 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Michael Chan7544b092007-05-05 13:08:32 -070012973 tp->pdev_peer = tg3_find_peer(tp);
12974
Matt Carlsonc885e822010-08-02 11:25:57 +000012975 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
12976 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
12977 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
12978 tp->tg3_flags3 |= TG3_FLG3_5717_PLUS;
12979
Matt Carlson321d32a2008-11-21 17:22:19 -080012980 /* Intentionally exclude ASIC_REV_5906 */
12981 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Michael Chand9ab5ad2006-03-20 22:27:35 -080012982 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070012983 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070012984 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070012985 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012986 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlsonc885e822010-08-02 11:25:57 +000012987 (tp->tg3_flags3 & TG3_FLG3_5717_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080012988 tp->tg3_flags3 |= TG3_FLG3_5755_PLUS;
12989
12990 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
12991 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Michael Chanb5d37722006-09-27 16:06:21 -070012992 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080012993 (tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
Michael Chana4e2b342005-10-26 15:46:52 -070012994 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
John W. Linville6708e5c2005-04-21 17:00:52 -070012995 tp->tg3_flags2 |= TG3_FLG2_5750_PLUS;
12996
John W. Linville1b440c562005-04-21 17:03:18 -070012997 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) ||
12998 (tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
12999 tp->tg3_flags2 |= TG3_FLG2_5705_PLUS;
13000
Matt Carlson027455a2008-12-21 20:19:30 -080013001 /* 5700 B0 chips do not support checksumming correctly due
13002 * to hardware bugs.
13003 */
13004 if (tp->pci_chip_rev_id == CHIPREV_ID_5700_B0)
13005 tp->tg3_flags |= TG3_FLAG_BROKEN_CHECKSUMS;
13006 else {
Eric Dumazet7fe876a2010-07-08 06:14:55 +000013007 unsigned long features = NETIF_F_IP_CSUM | NETIF_F_SG | NETIF_F_GRO;
13008
Matt Carlson027455a2008-12-21 20:19:30 -080013009 tp->tg3_flags |= TG3_FLAG_RX_CHECKSUMS;
Matt Carlson027455a2008-12-21 20:19:30 -080013010 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Eric Dumazet7fe876a2010-07-08 06:14:55 +000013011 features |= NETIF_F_IPV6_CSUM;
13012 tp->dev->features |= features;
13013 vlan_features_add(tp->dev, features);
Matt Carlson027455a2008-12-21 20:19:30 -080013014 }
13015
Matt Carlson507399f2009-11-13 13:03:37 +000013016 /* Determine TSO capabilities */
Matt Carlsonc885e822010-08-02 11:25:57 +000013017 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)
Matt Carlsone849cdc2009-11-13 13:03:38 +000013018 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_3;
13019 else if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
13020 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlson507399f2009-11-13 13:03:37 +000013021 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_2;
13022 else if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
13023 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_1 | TG3_FLG2_TSO_BUG;
13024 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
13025 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
13026 tp->tg3_flags2 &= ~TG3_FLG2_TSO_BUG;
13027 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13028 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13029 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
13030 tp->tg3_flags2 |= TG3_FLG2_TSO_BUG;
13031 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
13032 tp->fw_needed = FIRMWARE_TG3TSO5;
13033 else
13034 tp->fw_needed = FIRMWARE_TG3TSO;
13035 }
13036
13037 tp->irq_max = 1;
13038
Michael Chan5a6f3072006-03-20 22:28:05 -080013039 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
Michael Chan7544b092007-05-05 13:08:32 -070013040 tp->tg3_flags |= TG3_FLAG_SUPPORT_MSI;
13041 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
13042 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
13043 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
13044 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
13045 tp->pdev_peer == tp->pdev))
13046 tp->tg3_flags &= ~TG3_FLAG_SUPPORT_MSI;
13047
Matt Carlson321d32a2008-11-21 17:22:19 -080013048 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070013049 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanfcfa0a32006-03-20 22:28:41 -080013050 tp->tg3_flags2 |= TG3_FLG2_1SHOT_MSI;
Michael Chan52c0fd82006-06-29 20:15:54 -070013051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013052
Matt Carlsonc885e822010-08-02 11:25:57 +000013053 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
Matt Carlson507399f2009-11-13 13:03:37 +000013054 tp->tg3_flags |= TG3_FLAG_SUPPORT_MSIX;
13055 tp->irq_max = TG3_IRQ_MAX_VECS;
13056 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013057 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000013058
Matt Carlson615774f2009-11-13 13:03:39 +000013059 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsona50d0792010-06-05 17:24:37 +000013060 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlson615774f2009-11-13 13:03:39 +000013061 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
13062 tp->tg3_flags3 |= TG3_FLG3_SHORT_DMA_BUG;
13063 else if (!(tp->tg3_flags3 & TG3_FLG3_5755_PLUS)) {
13064 tp->tg3_flags3 |= TG3_FLG3_4G_DMA_BNDRY_BUG;
13065 tp->tg3_flags3 |= TG3_FLG3_40BIT_DMA_LIMIT_BUG;
Matt Carlson0e1406d2009-11-02 12:33:33 +000013066 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013067
Matt Carlsonc885e822010-08-02 11:25:57 +000013068 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)
Matt Carlsonb703df62009-12-03 08:36:21 +000013069 tp->tg3_flags3 |= TG3_FLG3_USE_JUMBO_BDFLAG;
13070
Matt Carlsonf51f3562008-05-25 23:45:08 -070013071 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
Matt Carlsonc6cdf432010-04-05 10:19:26 +000013072 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
13073 (tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG))
Matt Carlson8f666b02009-08-28 13:58:24 +000013074 tp->tg3_flags |= TG3_FLAG_JUMBO_CAPABLE;
Michael Chan0f893dc2005-07-25 12:30:38 -070013075
Matt Carlson52f44902008-11-21 17:17:04 -080013076 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
13077 &pci_state_reg);
13078
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013079 tp->pcie_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_EXP);
13080 if (tp->pcie_cap != 0) {
13081 u16 lnkctl;
13082
Linus Torvalds1da177e2005-04-16 15:20:36 -070013083 tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS;
Matt Carlson5f5c51e2007-11-12 21:19:37 -080013084
13085 pcie_set_readrq(tp->pdev, 4096);
13086
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013087 pci_read_config_word(tp->pdev,
13088 tp->pcie_cap + PCI_EXP_LNKCTL,
13089 &lnkctl);
13090 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
13091 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanc7835a72006-11-15 21:14:42 -080013092 tp->tg3_flags2 &= ~TG3_FLG2_HW_TSO_2;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013093 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013094 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000013095 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
13096 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013097 tp->tg3_flags3 |= TG3_FLG3_CLKREQ_BUG;
Matt Carlson614b0592010-01-20 16:58:02 +000013098 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
13099 tp->tg3_flags3 |= TG3_FLG3_L1PLLPD_EN;
Michael Chanc7835a72006-11-15 21:14:42 -080013100 }
Matt Carlson52f44902008-11-21 17:17:04 -080013101 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Matt Carlsonfcb389d2008-11-03 16:55:44 -080013102 tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS;
Matt Carlson52f44902008-11-21 17:17:04 -080013103 } else if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
13104 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
13105 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
13106 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000013107 dev_err(&tp->pdev->dev,
13108 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080013109 return -EIO;
13110 }
13111
13112 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
13113 tp->tg3_flags |= TG3_FLAG_PCIX_MODE;
13114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013115
Michael Chan399de502005-10-03 14:02:39 -070013116 /* If we have an AMD 762 or VIA K8T800 chipset, write
13117 * reordering to the mailbox registers done by the host
13118 * controller can cause major troubles. We read back from
13119 * every mailbox register write to force the writes to be
13120 * posted to the chip in order.
13121 */
13122 if (pci_dev_present(write_reorder_chipsets) &&
13123 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
13124 tp->tg3_flags |= TG3_FLAG_MBOX_WRITE_REORDER;
13125
Matt Carlson69fc4052008-12-21 20:19:57 -080013126 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
13127 &tp->pci_cacheline_sz);
13128 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13129 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013130 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
13131 tp->pci_lat_timer < 64) {
13132 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080013133 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13134 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013135 }
13136
Matt Carlson52f44902008-11-21 17:17:04 -080013137 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
13138 /* 5700 BX chips need to have their TX producer index
13139 * mailboxes written twice to workaround a bug.
13140 */
13141 tp->tg3_flags |= TG3_FLAG_TXD_MBOX_HWBUG;
Matt Carlson9974a352007-10-07 23:27:28 -070013142
Matt Carlson52f44902008-11-21 17:17:04 -080013143 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013144 *
13145 * The workaround is to use indirect register accesses
13146 * for all chip writes not to mailbox registers.
13147 */
Matt Carlson52f44902008-11-21 17:17:04 -080013148 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013149 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013150
13151 tp->tg3_flags |= TG3_FLAG_PCIX_TARGET_HWBUG;
13152
13153 /* The chip can have it's power management PCI config
13154 * space registers clobbered due to this bug.
13155 * So explicitly force the chip into D0 here.
13156 */
Matt Carlson9974a352007-10-07 23:27:28 -070013157 pci_read_config_dword(tp->pdev,
13158 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013159 &pm_reg);
13160 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
13161 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070013162 pci_write_config_dword(tp->pdev,
13163 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013164 pm_reg);
13165
13166 /* Also, force SERR#/PERR# in PCI command. */
13167 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13168 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
13169 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13170 }
13171 }
13172
Linus Torvalds1da177e2005-04-16 15:20:36 -070013173 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
13174 tp->tg3_flags |= TG3_FLAG_PCI_HIGH_SPEED;
13175 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
13176 tp->tg3_flags |= TG3_FLAG_PCI_32BIT;
13177
13178 /* Chip-specific fixup from Broadcom driver */
13179 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
13180 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
13181 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
13182 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
13183 }
13184
Michael Chan1ee582d2005-08-09 20:16:46 -070013185 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070013186 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013187 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070013188 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070013189 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013190 tp->write32_tx_mbox = tg3_write32;
13191 tp->write32_rx_mbox = tg3_write32;
13192
13193 /* Various workaround register access methods */
13194 if (tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG)
13195 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013196 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
13197 ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
13198 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
13199 /*
13200 * Back to back register writes can cause problems on these
13201 * chips, the workaround is to read back all reg writes
13202 * except those to mailbox regs.
13203 *
13204 * See tg3_write_indirect_reg32().
13205 */
Michael Chan1ee582d2005-08-09 20:16:46 -070013206 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013207 }
13208
Michael Chan1ee582d2005-08-09 20:16:46 -070013209 if ((tp->tg3_flags & TG3_FLAG_TXD_MBOX_HWBUG) ||
13210 (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)) {
13211 tp->write32_tx_mbox = tg3_write32_tx_mbox;
13212 if (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)
13213 tp->write32_rx_mbox = tg3_write_flush_reg32;
13214 }
Michael Chan20094932005-08-09 20:16:32 -070013215
Michael Chan68929142005-08-09 20:17:14 -070013216 if (tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND) {
13217 tp->read32 = tg3_read_indirect_reg32;
13218 tp->write32 = tg3_write_indirect_reg32;
13219 tp->read32_mbox = tg3_read_indirect_mbox;
13220 tp->write32_mbox = tg3_write_indirect_mbox;
13221 tp->write32_tx_mbox = tg3_write_indirect_mbox;
13222 tp->write32_rx_mbox = tg3_write_indirect_mbox;
13223
13224 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070013225 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070013226
13227 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13228 pci_cmd &= ~PCI_COMMAND_MEMORY;
13229 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13230 }
Michael Chanb5d37722006-09-27 16:06:21 -070013231 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
13232 tp->read32_mbox = tg3_read32_mbox_5906;
13233 tp->write32_mbox = tg3_write32_mbox_5906;
13234 tp->write32_tx_mbox = tg3_write32_mbox_5906;
13235 tp->write32_rx_mbox = tg3_write32_mbox_5906;
13236 }
Michael Chan68929142005-08-09 20:17:14 -070013237
Michael Chanbbadf502006-04-06 21:46:34 -070013238 if (tp->write32 == tg3_write_indirect_reg32 ||
13239 ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) &&
13240 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070013241 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Michael Chanbbadf502006-04-06 21:46:34 -070013242 tp->tg3_flags |= TG3_FLAG_SRAM_USE_CONFIG;
13243
Michael Chan7d0c41e2005-04-21 17:06:20 -070013244 /* Get eeprom hw config before calling tg3_set_power_state().
Michael Chan9d26e212006-12-07 00:21:14 -080013245 * In particular, the TG3_FLG2_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070013246 * determined before calling tg3_set_power_state() so that
13247 * we know whether or not to switch out of Vaux power.
13248 * When the flag is set, it means that GPIO1 is used for eeprom
13249 * write protect and also implies that it is a LOM where GPIOs
13250 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013251 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070013252 tg3_get_eeprom_hw_cfg(tp);
13253
Matt Carlson0d3031d2007-10-10 18:02:43 -070013254 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
13255 /* Allow reads and writes to the
13256 * APE register and memory space.
13257 */
13258 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +000013259 PCISTATE_ALLOW_APE_SHMEM_WR |
13260 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070013261 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
13262 pci_state_reg);
13263 }
13264
Matt Carlson9936bcf2007-10-10 18:03:07 -070013265 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013266 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013267 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013268 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlsonc885e822010-08-02 11:25:57 +000013269 (tp->tg3_flags3 & TG3_FLG3_5717_PLUS))
Matt Carlsond30cdd22007-10-07 23:28:35 -070013270 tp->tg3_flags |= TG3_FLAG_CPMU_PRESENT;
13271
Michael Chan314fba32005-04-21 17:07:04 -070013272 /* Set up tp->grc_local_ctrl before calling tg3_set_power_state().
13273 * GPIO1 driven high will bring 5700's external PHY out of reset.
13274 * It is also used as eeprom write protect on LOMs.
13275 */
13276 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
13277 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
13278 (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT))
13279 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
13280 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070013281 /* Unused GPIO3 must be driven as output on 5752 because there
13282 * are no pull-up resistors on unused GPIO pins.
13283 */
13284 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
13285 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070013286
Matt Carlson321d32a2008-11-21 17:22:19 -080013287 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsoncb4ed1f2010-01-20 16:58:09 +000013288 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
13289 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Michael Chanaf36e6b2006-03-23 01:28:06 -080013290 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
13291
Matt Carlson8d519ab2009-04-20 06:58:01 +000013292 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
13293 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070013294 /* Turn off the debug UART. */
13295 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
13296 if (tp->tg3_flags2 & TG3_FLG2_IS_NIC)
13297 /* Keep VMain power. */
13298 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
13299 GRC_LCLCTRL_GPIO_OUTPUT0;
13300 }
13301
Linus Torvalds1da177e2005-04-16 15:20:36 -070013302 /* Force the chip into D0. */
Michael Chanbc1c7562006-03-20 17:48:03 -080013303 err = tg3_set_power_state(tp, PCI_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013304 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000013305 dev_err(&tp->pdev->dev, "Transition to D0 failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070013306 return err;
13307 }
13308
Linus Torvalds1da177e2005-04-16 15:20:36 -070013309 /* Derive initial jumbo mode from MTU assigned in
13310 * ether_setup() via the alloc_etherdev() call
13311 */
Michael Chan0f893dc2005-07-25 12:30:38 -070013312 if (tp->dev->mtu > ETH_DATA_LEN &&
Michael Chana4e2b342005-10-26 15:46:52 -070013313 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Michael Chan0f893dc2005-07-25 12:30:38 -070013314 tp->tg3_flags |= TG3_FLAG_JUMBO_RING_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013315
13316 /* Determine WakeOnLan speed to use. */
13317 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13318 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
13319 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
13320 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
13321 tp->tg3_flags &= ~(TG3_FLAG_WOL_SPEED_100MB);
13322 } else {
13323 tp->tg3_flags |= TG3_FLAG_WOL_SPEED_100MB;
13324 }
13325
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013326 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
13327 tp->tg3_flags3 |= TG3_FLG3_PHY_IS_FET;
13328
Linus Torvalds1da177e2005-04-16 15:20:36 -070013329 /* A few boards don't want Ethernet@WireSpeed phy feature */
13330 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
13331 ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
13332 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070013333 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013334 (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) ||
Michael Chan747e8f82005-07-25 12:33:22 -070013335 (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013336 tp->tg3_flags2 |= TG3_FLG2_NO_ETH_WIRE_SPEED;
13337
13338 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
13339 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
13340 tp->tg3_flags2 |= TG3_FLG2_PHY_ADC_BUG;
13341 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
13342 tp->tg3_flags2 |= TG3_FLG2_PHY_5704_A0_BUG;
13343
Matt Carlson321d32a2008-11-21 17:22:19 -080013344 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013345 !(tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080013346 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013347 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
Matt Carlsonc885e822010-08-02 11:25:57 +000013348 !(tp->tg3_flags3 & TG3_FLG3_5717_PLUS)) {
Michael Chanc424cb22006-04-29 18:56:34 -070013349 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013350 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013351 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
13352 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080013353 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
13354 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
13355 tp->tg3_flags2 |= TG3_FLG2_PHY_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080013356 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
13357 tp->tg3_flags2 |= TG3_FLG2_PHY_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080013358 } else
Michael Chanc424cb22006-04-29 18:56:34 -070013359 tp->tg3_flags2 |= TG3_FLG2_PHY_BER_BUG;
13360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013361
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013362 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
13363 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
13364 tp->phy_otp = tg3_read_otp_phycfg(tp);
13365 if (tp->phy_otp == 0)
13366 tp->phy_otp = TG3_OTP_DEFAULT;
13367 }
13368
Matt Carlsonf51f3562008-05-25 23:45:08 -070013369 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT)
Matt Carlson8ef21422008-05-02 16:47:53 -070013370 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
13371 else
13372 tp->mi_mode = MAC_MI_MODE_BASE;
13373
Linus Torvalds1da177e2005-04-16 15:20:36 -070013374 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013375 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
13376 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
13377 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
13378
Matt Carlson321d32a2008-11-21 17:22:19 -080013379 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
13380 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson57e69832008-05-25 23:48:31 -070013381 tp->tg3_flags3 |= TG3_FLG3_USE_PHYLIB;
13382
Matt Carlson158d7ab2008-05-29 01:37:54 -070013383 err = tg3_mdio_init(tp);
13384 if (err)
13385 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013386
Matt Carlson55dffe72010-01-12 10:11:39 +000013387 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 &&
Matt Carlson2138c002010-07-11 09:31:43 +000013388 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0)
Matt Carlson55dffe72010-01-12 10:11:39 +000013389 return -ENOTSUPP;
13390
Linus Torvalds1da177e2005-04-16 15:20:36 -070013391 /* Initialize data/descriptor byte/word swapping. */
13392 val = tr32(GRC_MODE);
13393 val &= GRC_MODE_HOST_STACKUP;
13394 tw32(GRC_MODE, val | tp->grc_mode);
13395
13396 tg3_switch_clocks(tp);
13397
13398 /* Clear this out for sanity. */
13399 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
13400
13401 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
13402 &pci_state_reg);
13403 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
13404 (tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG) == 0) {
13405 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
13406
13407 if (chiprevid == CHIPREV_ID_5701_A0 ||
13408 chiprevid == CHIPREV_ID_5701_B0 ||
13409 chiprevid == CHIPREV_ID_5701_B2 ||
13410 chiprevid == CHIPREV_ID_5701_B5) {
13411 void __iomem *sram_base;
13412
13413 /* Write some dummy words into the SRAM status block
13414 * area, see if it reads back correctly. If the return
13415 * value is bad, force enable the PCIX workaround.
13416 */
13417 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
13418
13419 writel(0x00000000, sram_base);
13420 writel(0x00000000, sram_base + 4);
13421 writel(0xffffffff, sram_base + 4);
13422 if (readl(sram_base) != 0x00000000)
13423 tp->tg3_flags |= TG3_FLAG_PCIX_TARGET_HWBUG;
13424 }
13425 }
13426
13427 udelay(50);
13428 tg3_nvram_init(tp);
13429
13430 grc_misc_cfg = tr32(GRC_MISC_CFG);
13431 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
13432
Linus Torvalds1da177e2005-04-16 15:20:36 -070013433 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
13434 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
13435 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
13436 tp->tg3_flags2 |= TG3_FLG2_IS_5788;
13437
David S. Millerfac9b832005-05-18 22:46:34 -070013438 if (!(tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
13439 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700))
13440 tp->tg3_flags |= TG3_FLAG_TAGGED_STATUS;
13441 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) {
13442 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
13443 HOSTCC_MODE_CLRTICK_TXBD);
13444
13445 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
13446 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13447 tp->misc_host_ctrl);
13448 }
13449
Matt Carlson3bda1252008-08-15 14:08:22 -070013450 /* Preserve the APE MAC_MODE bits */
13451 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
13452 tp->mac_mode = tr32(MAC_MODE) |
13453 MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
13454 else
13455 tp->mac_mode = TG3_DEF_MAC_MODE;
13456
Linus Torvalds1da177e2005-04-16 15:20:36 -070013457 /* these are limited to 10/100 only */
13458 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
13459 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
13460 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
13461 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
13462 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
13463 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
13464 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
13465 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
13466 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
Michael Chan676917d2006-12-07 00:20:22 -080013467 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
13468 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013469 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
Matt Carlsond1101142010-02-17 15:16:55 +000013470 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
13471 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013472 (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013473 tp->tg3_flags |= TG3_FLAG_10_100_ONLY;
13474
13475 err = tg3_phy_probe(tp);
13476 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000013477 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013478 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013479 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013480 }
13481
Matt Carlson184b8902010-04-05 10:19:25 +000013482 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080013483 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013484
13485 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
13486 tp->tg3_flags &= ~TG3_FLAG_USE_MI_INTERRUPT;
13487 } else {
13488 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
13489 tp->tg3_flags |= TG3_FLAG_USE_MI_INTERRUPT;
13490 else
13491 tp->tg3_flags &= ~TG3_FLAG_USE_MI_INTERRUPT;
13492 }
13493
13494 /* 5700 {AX,BX} chips have a broken status block link
13495 * change bit implementation, so we must use the
13496 * status register in those cases.
13497 */
13498 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
13499 tp->tg3_flags |= TG3_FLAG_USE_LINKCHG_REG;
13500 else
13501 tp->tg3_flags &= ~TG3_FLAG_USE_LINKCHG_REG;
13502
13503 /* The led_ctrl is set during tg3_phy_probe, here we might
13504 * have to force the link status polling mechanism based
13505 * upon subsystem IDs.
13506 */
13507 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070013508 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070013509 !(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)) {
13510 tp->tg3_flags |= (TG3_FLAG_USE_MI_INTERRUPT |
13511 TG3_FLAG_USE_LINKCHG_REG);
13512 }
13513
13514 /* For all SERDES we poll the MAC status register. */
13515 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
13516 tp->tg3_flags |= TG3_FLAG_POLL_SERDES;
13517 else
13518 tp->tg3_flags &= ~TG3_FLAG_POLL_SERDES;
13519
Matt Carlson9dc7a112010-04-12 06:58:28 +000013520 tp->rx_offset = NET_IP_ALIGN + TG3_RX_HEADROOM;
Matt Carlsond2757fc2010-04-12 06:58:27 +000013521 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013522 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsond2757fc2010-04-12 06:58:27 +000013523 (tp->tg3_flags & TG3_FLAG_PCIX_MODE) != 0) {
Matt Carlson9dc7a112010-04-12 06:58:28 +000013524 tp->rx_offset -= NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000013525#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000013526 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000013527#endif
13528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013529
Michael Chanf92905d2006-06-29 20:14:29 -070013530 tp->rx_std_max_post = TG3_RX_RING_SIZE;
13531
13532 /* Increment the rx prod index on the rx std ring by at most
13533 * 8 for these chips to workaround hw errata.
13534 */
13535 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13536 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
13537 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
13538 tp->rx_std_max_post = 8;
13539
Matt Carlson8ed5d972007-05-07 00:25:49 -070013540 if (tp->tg3_flags & TG3_FLAG_ASPM_WORKAROUND)
13541 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
13542 PCIE_PWR_MGMT_L1_THRESH_MSK;
13543
Linus Torvalds1da177e2005-04-16 15:20:36 -070013544 return err;
13545}
13546
David S. Miller49b6e95f2007-03-29 01:38:42 -070013547#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070013548static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
13549{
13550 struct net_device *dev = tp->dev;
13551 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070013552 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070013553 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070013554 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013555
David S. Miller49b6e95f2007-03-29 01:38:42 -070013556 addr = of_get_property(dp, "local-mac-address", &len);
13557 if (addr && len == 6) {
13558 memcpy(dev->dev_addr, addr, 6);
13559 memcpy(dev->perm_addr, dev->dev_addr, 6);
13560 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013561 }
13562 return -ENODEV;
13563}
13564
13565static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
13566{
13567 struct net_device *dev = tp->dev;
13568
13569 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070013570 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013571 return 0;
13572}
13573#endif
13574
13575static int __devinit tg3_get_device_address(struct tg3 *tp)
13576{
13577 struct net_device *dev = tp->dev;
13578 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080013579 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013580
David S. Miller49b6e95f2007-03-29 01:38:42 -070013581#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070013582 if (!tg3_get_macaddr_sparc(tp))
13583 return 0;
13584#endif
13585
13586 mac_offset = 0x7c;
David S. Millerf49639e2006-06-09 11:58:36 -070013587 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) ||
Michael Chana4e2b342005-10-26 15:46:52 -070013588 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013589 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
13590 mac_offset = 0xcc;
13591 if (tg3_nvram_lock(tp))
13592 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
13593 else
13594 tg3_nvram_unlock(tp);
Matt Carlsona50d0792010-06-05 17:24:37 +000013595 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13596 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) {
13597 if (PCI_FUNC(tp->pdev->devfn) & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000013598 mac_offset = 0xcc;
Matt Carlsona50d0792010-06-05 17:24:37 +000013599 if (PCI_FUNC(tp->pdev->devfn) > 1)
13600 mac_offset += 0x18c;
Matt Carlsona1b950d2009-09-01 13:20:17 +000013601 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070013602 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013603
13604 /* First try to get it from MAC address mailbox. */
13605 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
13606 if ((hi >> 16) == 0x484b) {
13607 dev->dev_addr[0] = (hi >> 8) & 0xff;
13608 dev->dev_addr[1] = (hi >> 0) & 0xff;
13609
13610 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
13611 dev->dev_addr[2] = (lo >> 24) & 0xff;
13612 dev->dev_addr[3] = (lo >> 16) & 0xff;
13613 dev->dev_addr[4] = (lo >> 8) & 0xff;
13614 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013615
Michael Chan008652b2006-03-27 23:14:53 -080013616 /* Some old bootcode may report a 0 MAC address in SRAM */
13617 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
13618 }
13619 if (!addr_ok) {
13620 /* Next, try NVRAM. */
Matt Carlsondf259d82009-04-20 06:57:14 +000013621 if (!(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) &&
13622 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000013623 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070013624 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
13625 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080013626 }
13627 /* Finally just fetch it out of the MAC control regs. */
13628 else {
13629 hi = tr32(MAC_ADDR_0_HIGH);
13630 lo = tr32(MAC_ADDR_0_LOW);
13631
13632 dev->dev_addr[5] = lo & 0xff;
13633 dev->dev_addr[4] = (lo >> 8) & 0xff;
13634 dev->dev_addr[3] = (lo >> 16) & 0xff;
13635 dev->dev_addr[2] = (lo >> 24) & 0xff;
13636 dev->dev_addr[1] = hi & 0xff;
13637 dev->dev_addr[0] = (hi >> 8) & 0xff;
13638 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013639 }
13640
13641 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070013642#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070013643 if (!tg3_get_default_macaddr_sparc(tp))
13644 return 0;
13645#endif
13646 return -EINVAL;
13647 }
John W. Linville2ff43692005-09-12 14:44:20 -070013648 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013649 return 0;
13650}
13651
David S. Miller59e6b432005-05-18 22:50:10 -070013652#define BOUNDARY_SINGLE_CACHELINE 1
13653#define BOUNDARY_MULTI_CACHELINE 2
13654
13655static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
13656{
13657 int cacheline_size;
13658 u8 byte;
13659 int goal;
13660
13661 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
13662 if (byte == 0)
13663 cacheline_size = 1024;
13664 else
13665 cacheline_size = (int) byte * 4;
13666
13667 /* On 5703 and later chips, the boundary bits have no
13668 * effect.
13669 */
13670 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13671 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13672 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
13673 goto out;
13674
13675#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
13676 goal = BOUNDARY_MULTI_CACHELINE;
13677#else
13678#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
13679 goal = BOUNDARY_SINGLE_CACHELINE;
13680#else
13681 goal = 0;
13682#endif
13683#endif
13684
Matt Carlsonc885e822010-08-02 11:25:57 +000013685 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000013686 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
13687 goto out;
13688 }
13689
David S. Miller59e6b432005-05-18 22:50:10 -070013690 if (!goal)
13691 goto out;
13692
13693 /* PCI controllers on most RISC systems tend to disconnect
13694 * when a device tries to burst across a cache-line boundary.
13695 * Therefore, letting tg3 do so just wastes PCI bandwidth.
13696 *
13697 * Unfortunately, for PCI-E there are only limited
13698 * write-side controls for this, and thus for reads
13699 * we will still get the disconnects. We'll also waste
13700 * these PCI cycles for both read and write for chips
13701 * other than 5700 and 5701 which do not implement the
13702 * boundary bits.
13703 */
13704 if ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) &&
13705 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)) {
13706 switch (cacheline_size) {
13707 case 16:
13708 case 32:
13709 case 64:
13710 case 128:
13711 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13712 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
13713 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
13714 } else {
13715 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
13716 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
13717 }
13718 break;
13719
13720 case 256:
13721 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
13722 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
13723 break;
13724
13725 default:
13726 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
13727 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
13728 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070013729 }
David S. Miller59e6b432005-05-18 22:50:10 -070013730 } else if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
13731 switch (cacheline_size) {
13732 case 16:
13733 case 32:
13734 case 64:
13735 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13736 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
13737 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
13738 break;
13739 }
13740 /* fallthrough */
13741 case 128:
13742 default:
13743 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
13744 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
13745 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070013746 }
David S. Miller59e6b432005-05-18 22:50:10 -070013747 } else {
13748 switch (cacheline_size) {
13749 case 16:
13750 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13751 val |= (DMA_RWCTRL_READ_BNDRY_16 |
13752 DMA_RWCTRL_WRITE_BNDRY_16);
13753 break;
13754 }
13755 /* fallthrough */
13756 case 32:
13757 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13758 val |= (DMA_RWCTRL_READ_BNDRY_32 |
13759 DMA_RWCTRL_WRITE_BNDRY_32);
13760 break;
13761 }
13762 /* fallthrough */
13763 case 64:
13764 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13765 val |= (DMA_RWCTRL_READ_BNDRY_64 |
13766 DMA_RWCTRL_WRITE_BNDRY_64);
13767 break;
13768 }
13769 /* fallthrough */
13770 case 128:
13771 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13772 val |= (DMA_RWCTRL_READ_BNDRY_128 |
13773 DMA_RWCTRL_WRITE_BNDRY_128);
13774 break;
13775 }
13776 /* fallthrough */
13777 case 256:
13778 val |= (DMA_RWCTRL_READ_BNDRY_256 |
13779 DMA_RWCTRL_WRITE_BNDRY_256);
13780 break;
13781 case 512:
13782 val |= (DMA_RWCTRL_READ_BNDRY_512 |
13783 DMA_RWCTRL_WRITE_BNDRY_512);
13784 break;
13785 case 1024:
13786 default:
13787 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
13788 DMA_RWCTRL_WRITE_BNDRY_1024);
13789 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070013790 }
David S. Miller59e6b432005-05-18 22:50:10 -070013791 }
13792
13793out:
13794 return val;
13795}
13796
Linus Torvalds1da177e2005-04-16 15:20:36 -070013797static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
13798{
13799 struct tg3_internal_buffer_desc test_desc;
13800 u32 sram_dma_descs;
13801 int i, ret;
13802
13803 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
13804
13805 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
13806 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
13807 tw32(RDMAC_STATUS, 0);
13808 tw32(WDMAC_STATUS, 0);
13809
13810 tw32(BUFMGR_MODE, 0);
13811 tw32(FTQ_RESET, 0);
13812
13813 test_desc.addr_hi = ((u64) buf_dma) >> 32;
13814 test_desc.addr_lo = buf_dma & 0xffffffff;
13815 test_desc.nic_mbuf = 0x00002100;
13816 test_desc.len = size;
13817
13818 /*
13819 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
13820 * the *second* time the tg3 driver was getting loaded after an
13821 * initial scan.
13822 *
13823 * Broadcom tells me:
13824 * ...the DMA engine is connected to the GRC block and a DMA
13825 * reset may affect the GRC block in some unpredictable way...
13826 * The behavior of resets to individual blocks has not been tested.
13827 *
13828 * Broadcom noted the GRC reset will also reset all sub-components.
13829 */
13830 if (to_device) {
13831 test_desc.cqid_sqid = (13 << 8) | 2;
13832
13833 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
13834 udelay(40);
13835 } else {
13836 test_desc.cqid_sqid = (16 << 8) | 7;
13837
13838 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
13839 udelay(40);
13840 }
13841 test_desc.flags = 0x00000005;
13842
13843 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
13844 u32 val;
13845
13846 val = *(((u32 *)&test_desc) + i);
13847 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
13848 sram_dma_descs + (i * sizeof(u32)));
13849 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
13850 }
13851 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
13852
Matt Carlson859a588792010-04-05 10:19:28 +000013853 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013854 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a588792010-04-05 10:19:28 +000013855 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070013856 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013857
13858 ret = -ENODEV;
13859 for (i = 0; i < 40; i++) {
13860 u32 val;
13861
13862 if (to_device)
13863 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
13864 else
13865 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
13866 if ((val & 0xffff) == sram_dma_descs) {
13867 ret = 0;
13868 break;
13869 }
13870
13871 udelay(100);
13872 }
13873
13874 return ret;
13875}
13876
David S. Millerded73402005-05-23 13:59:47 -070013877#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070013878
13879static int __devinit tg3_test_dma(struct tg3 *tp)
13880{
13881 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070013882 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000013883 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013884
13885 buf = pci_alloc_consistent(tp->pdev, TEST_BUFFER_SIZE, &buf_dma);
13886 if (!buf) {
13887 ret = -ENOMEM;
13888 goto out_nofree;
13889 }
13890
13891 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
13892 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
13893
David S. Miller59e6b432005-05-18 22:50:10 -070013894 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013895
Matt Carlsonc885e822010-08-02 11:25:57 +000013896 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000013897 goto out;
13898
Linus Torvalds1da177e2005-04-16 15:20:36 -070013899 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
13900 /* DMA read watermark not used on PCIE */
13901 tp->dma_rwctrl |= 0x00180000;
13902 } else if (!(tp->tg3_flags & TG3_FLAG_PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070013903 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
13904 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013905 tp->dma_rwctrl |= 0x003f0000;
13906 else
13907 tp->dma_rwctrl |= 0x003f000f;
13908 } else {
13909 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
13910 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
13911 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080013912 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013913
Michael Chan4a29cc22006-03-19 13:21:12 -080013914 /* If the 5704 is behind the EPB bridge, we can
13915 * do the less restrictive ONE_DMA workaround for
13916 * better performance.
13917 */
13918 if ((tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG) &&
13919 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
13920 tp->dma_rwctrl |= 0x8000;
13921 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013922 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
13923
Michael Chan49afdeb2007-02-13 12:17:03 -080013924 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
13925 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070013926 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080013927 tp->dma_rwctrl |=
13928 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
13929 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
13930 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070013931 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
13932 /* 5780 always in PCIX mode */
13933 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070013934 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
13935 /* 5714 always in PCIX mode */
13936 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013937 } else {
13938 tp->dma_rwctrl |= 0x001b000f;
13939 }
13940 }
13941
13942 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
13943 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
13944 tp->dma_rwctrl &= 0xfffffff0;
13945
13946 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13947 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
13948 /* Remove this if it causes problems for some boards. */
13949 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
13950
13951 /* On 5700/5701 chips, we need to set this bit.
13952 * Otherwise the chip will issue cacheline transactions
13953 * to streamable DMA memory with not all the byte
13954 * enables turned on. This is an error on several
13955 * RISC PCI controllers, in particular sparc64.
13956 *
13957 * On 5703/5704 chips, this bit has been reassigned
13958 * a different meaning. In particular, it is used
13959 * on those chips to enable a PCI-X workaround.
13960 */
13961 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
13962 }
13963
13964 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
13965
13966#if 0
13967 /* Unneeded, already done by tg3_get_invariants. */
13968 tg3_switch_clocks(tp);
13969#endif
13970
Linus Torvalds1da177e2005-04-16 15:20:36 -070013971 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13972 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
13973 goto out;
13974
David S. Miller59e6b432005-05-18 22:50:10 -070013975 /* It is best to perform DMA test with maximum write burst size
13976 * to expose the 5700/5701 write DMA bug.
13977 */
13978 saved_dma_rwctrl = tp->dma_rwctrl;
13979 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
13980 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
13981
Linus Torvalds1da177e2005-04-16 15:20:36 -070013982 while (1) {
13983 u32 *p = buf, i;
13984
13985 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
13986 p[i] = i;
13987
13988 /* Send the buffer to the chip. */
13989 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
13990 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000013991 dev_err(&tp->pdev->dev,
13992 "%s: Buffer write failed. err = %d\n",
13993 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013994 break;
13995 }
13996
13997#if 0
13998 /* validate data reached card RAM correctly. */
13999 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14000 u32 val;
14001 tg3_read_mem(tp, 0x2100 + (i*4), &val);
14002 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000014003 dev_err(&tp->pdev->dev,
14004 "%s: Buffer corrupted on device! "
14005 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014006 /* ret = -ENODEV here? */
14007 }
14008 p[i] = 0;
14009 }
14010#endif
14011 /* Now read it back. */
14012 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
14013 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000014014 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
14015 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014016 break;
14017 }
14018
14019 /* Verify it. */
14020 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14021 if (p[i] == i)
14022 continue;
14023
David S. Miller59e6b432005-05-18 22:50:10 -070014024 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14025 DMA_RWCTRL_WRITE_BNDRY_16) {
14026 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014027 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
14028 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14029 break;
14030 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000014031 dev_err(&tp->pdev->dev,
14032 "%s: Buffer corrupted on read back! "
14033 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014034 ret = -ENODEV;
14035 goto out;
14036 }
14037 }
14038
14039 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
14040 /* Success. */
14041 ret = 0;
14042 break;
14043 }
14044 }
David S. Miller59e6b432005-05-18 22:50:10 -070014045 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14046 DMA_RWCTRL_WRITE_BNDRY_16) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014047 static struct pci_device_id dma_wait_state_chipsets[] = {
14048 { PCI_DEVICE(PCI_VENDOR_ID_APPLE,
14049 PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
14050 { },
14051 };
14052
David S. Miller59e6b432005-05-18 22:50:10 -070014053 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070014054 * now look for chipsets that are known to expose the
14055 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070014056 */
Michael Chan6d1cfba2005-06-08 14:13:14 -070014057 if (pci_dev_present(dma_wait_state_chipsets)) {
14058 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14059 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a588792010-04-05 10:19:28 +000014060 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014061 /* Safe to use the calculated DMA boundary. */
14062 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a588792010-04-05 10:19:28 +000014063 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070014064
David S. Miller59e6b432005-05-18 22:50:10 -070014065 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014067
14068out:
14069 pci_free_consistent(tp->pdev, TEST_BUFFER_SIZE, buf, buf_dma);
14070out_nofree:
14071 return ret;
14072}
14073
14074static void __devinit tg3_init_link_config(struct tg3 *tp)
14075{
14076 tp->link_config.advertising =
14077 (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
14078 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
14079 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full |
14080 ADVERTISED_Autoneg | ADVERTISED_MII);
14081 tp->link_config.speed = SPEED_INVALID;
14082 tp->link_config.duplex = DUPLEX_INVALID;
14083 tp->link_config.autoneg = AUTONEG_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014084 tp->link_config.active_speed = SPEED_INVALID;
14085 tp->link_config.active_duplex = DUPLEX_INVALID;
14086 tp->link_config.phy_is_low_power = 0;
14087 tp->link_config.orig_speed = SPEED_INVALID;
14088 tp->link_config.orig_duplex = DUPLEX_INVALID;
14089 tp->link_config.orig_autoneg = AUTONEG_INVALID;
14090}
14091
14092static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
14093{
Matt Carlsonc885e822010-08-02 11:25:57 +000014094 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
Matt Carlson666bc832010-01-20 16:58:03 +000014095 tp->bufmgr_config.mbuf_read_dma_low_water =
14096 DEFAULT_MB_RDMA_LOW_WATER_5705;
14097 tp->bufmgr_config.mbuf_mac_rx_low_water =
14098 DEFAULT_MB_MACRX_LOW_WATER_57765;
14099 tp->bufmgr_config.mbuf_high_water =
14100 DEFAULT_MB_HIGH_WATER_57765;
14101
14102 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14103 DEFAULT_MB_RDMA_LOW_WATER_5705;
14104 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14105 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
14106 tp->bufmgr_config.mbuf_high_water_jumbo =
14107 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
14108 } else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
Michael Chanfdfec1722005-07-25 12:31:48 -070014109 tp->bufmgr_config.mbuf_read_dma_low_water =
14110 DEFAULT_MB_RDMA_LOW_WATER_5705;
14111 tp->bufmgr_config.mbuf_mac_rx_low_water =
14112 DEFAULT_MB_MACRX_LOW_WATER_5705;
14113 tp->bufmgr_config.mbuf_high_water =
14114 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070014115 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
14116 tp->bufmgr_config.mbuf_mac_rx_low_water =
14117 DEFAULT_MB_MACRX_LOW_WATER_5906;
14118 tp->bufmgr_config.mbuf_high_water =
14119 DEFAULT_MB_HIGH_WATER_5906;
14120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014121
Michael Chanfdfec1722005-07-25 12:31:48 -070014122 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14123 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
14124 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14125 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
14126 tp->bufmgr_config.mbuf_high_water_jumbo =
14127 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
14128 } else {
14129 tp->bufmgr_config.mbuf_read_dma_low_water =
14130 DEFAULT_MB_RDMA_LOW_WATER;
14131 tp->bufmgr_config.mbuf_mac_rx_low_water =
14132 DEFAULT_MB_MACRX_LOW_WATER;
14133 tp->bufmgr_config.mbuf_high_water =
14134 DEFAULT_MB_HIGH_WATER;
14135
14136 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14137 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
14138 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14139 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
14140 tp->bufmgr_config.mbuf_high_water_jumbo =
14141 DEFAULT_MB_HIGH_WATER_JUMBO;
14142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014143
14144 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
14145 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
14146}
14147
14148static char * __devinit tg3_phy_string(struct tg3 *tp)
14149{
Matt Carlson79eb6902010-02-17 15:17:03 +000014150 switch (tp->phy_id & TG3_PHY_ID_MASK) {
14151 case TG3_PHY_ID_BCM5400: return "5400";
14152 case TG3_PHY_ID_BCM5401: return "5401";
14153 case TG3_PHY_ID_BCM5411: return "5411";
14154 case TG3_PHY_ID_BCM5701: return "5701";
14155 case TG3_PHY_ID_BCM5703: return "5703";
14156 case TG3_PHY_ID_BCM5704: return "5704";
14157 case TG3_PHY_ID_BCM5705: return "5705";
14158 case TG3_PHY_ID_BCM5750: return "5750";
14159 case TG3_PHY_ID_BCM5752: return "5752";
14160 case TG3_PHY_ID_BCM5714: return "5714";
14161 case TG3_PHY_ID_BCM5780: return "5780";
14162 case TG3_PHY_ID_BCM5755: return "5755";
14163 case TG3_PHY_ID_BCM5787: return "5787";
14164 case TG3_PHY_ID_BCM5784: return "5784";
14165 case TG3_PHY_ID_BCM5756: return "5722/5756";
14166 case TG3_PHY_ID_BCM5906: return "5906";
14167 case TG3_PHY_ID_BCM5761: return "5761";
14168 case TG3_PHY_ID_BCM5718C: return "5718C";
14169 case TG3_PHY_ID_BCM5718S: return "5718S";
14170 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000014171 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson79eb6902010-02-17 15:17:03 +000014172 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070014173 case 0: return "serdes";
14174 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070014175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014176}
14177
Michael Chanf9804dd2005-09-27 12:13:10 -070014178static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
14179{
14180 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
14181 strcpy(str, "PCI Express");
14182 return str;
14183 } else if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
14184 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
14185
14186 strcpy(str, "PCIX:");
14187
14188 if ((clock_ctrl == 7) ||
14189 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
14190 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
14191 strcat(str, "133MHz");
14192 else if (clock_ctrl == 0)
14193 strcat(str, "33MHz");
14194 else if (clock_ctrl == 2)
14195 strcat(str, "50MHz");
14196 else if (clock_ctrl == 4)
14197 strcat(str, "66MHz");
14198 else if (clock_ctrl == 6)
14199 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070014200 } else {
14201 strcpy(str, "PCI:");
14202 if (tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED)
14203 strcat(str, "66MHz");
14204 else
14205 strcat(str, "33MHz");
14206 }
14207 if (tp->tg3_flags & TG3_FLAG_PCI_32BIT)
14208 strcat(str, ":32-bit");
14209 else
14210 strcat(str, ":64-bit");
14211 return str;
14212}
14213
Michael Chan8c2dc7e2005-12-19 16:26:02 -080014214static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014215{
14216 struct pci_dev *peer;
14217 unsigned int func, devnr = tp->pdev->devfn & ~7;
14218
14219 for (func = 0; func < 8; func++) {
14220 peer = pci_get_slot(tp->pdev->bus, devnr | func);
14221 if (peer && peer != tp->pdev)
14222 break;
14223 pci_dev_put(peer);
14224 }
Michael Chan16fe9d72005-12-13 21:09:54 -080014225 /* 5704 can be configured in single-port mode, set peer to
14226 * tp->pdev in that case.
14227 */
14228 if (!peer) {
14229 peer = tp->pdev;
14230 return peer;
14231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014232
14233 /*
14234 * We don't need to keep the refcount elevated; there's no way
14235 * to remove one half of this device without removing the other
14236 */
14237 pci_dev_put(peer);
14238
14239 return peer;
14240}
14241
David S. Miller15f98502005-05-18 22:49:26 -070014242static void __devinit tg3_init_coal(struct tg3 *tp)
14243{
14244 struct ethtool_coalesce *ec = &tp->coal;
14245
14246 memset(ec, 0, sizeof(*ec));
14247 ec->cmd = ETHTOOL_GCOALESCE;
14248 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
14249 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
14250 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
14251 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
14252 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
14253 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
14254 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
14255 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
14256 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
14257
14258 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
14259 HOSTCC_MODE_CLRTICK_TXBD)) {
14260 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
14261 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
14262 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
14263 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
14264 }
Michael Chand244c892005-07-05 14:42:33 -070014265
14266 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
14267 ec->rx_coalesce_usecs_irq = 0;
14268 ec->tx_coalesce_usecs_irq = 0;
14269 ec->stats_block_coalesce_usecs = 0;
14270 }
David S. Miller15f98502005-05-18 22:49:26 -070014271}
14272
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080014273static const struct net_device_ops tg3_netdev_ops = {
14274 .ndo_open = tg3_open,
14275 .ndo_stop = tg3_close,
Stephen Hemminger00829822008-11-20 20:14:53 -080014276 .ndo_start_xmit = tg3_start_xmit,
Eric Dumazet511d2222010-07-07 20:44:24 +000014277 .ndo_get_stats64 = tg3_get_stats64,
Stephen Hemminger00829822008-11-20 20:14:53 -080014278 .ndo_validate_addr = eth_validate_addr,
14279 .ndo_set_multicast_list = tg3_set_rx_mode,
14280 .ndo_set_mac_address = tg3_set_mac_addr,
14281 .ndo_do_ioctl = tg3_ioctl,
14282 .ndo_tx_timeout = tg3_tx_timeout,
14283 .ndo_change_mtu = tg3_change_mtu,
14284#if TG3_VLAN_TAG_USED
14285 .ndo_vlan_rx_register = tg3_vlan_rx_register,
14286#endif
14287#ifdef CONFIG_NET_POLL_CONTROLLER
14288 .ndo_poll_controller = tg3_poll_controller,
14289#endif
14290};
14291
14292static const struct net_device_ops tg3_netdev_ops_dma_bug = {
14293 .ndo_open = tg3_open,
14294 .ndo_stop = tg3_close,
14295 .ndo_start_xmit = tg3_start_xmit_dma_bug,
Eric Dumazet511d2222010-07-07 20:44:24 +000014296 .ndo_get_stats64 = tg3_get_stats64,
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080014297 .ndo_validate_addr = eth_validate_addr,
14298 .ndo_set_multicast_list = tg3_set_rx_mode,
14299 .ndo_set_mac_address = tg3_set_mac_addr,
14300 .ndo_do_ioctl = tg3_ioctl,
14301 .ndo_tx_timeout = tg3_tx_timeout,
14302 .ndo_change_mtu = tg3_change_mtu,
14303#if TG3_VLAN_TAG_USED
14304 .ndo_vlan_rx_register = tg3_vlan_rx_register,
14305#endif
14306#ifdef CONFIG_NET_POLL_CONTROLLER
14307 .ndo_poll_controller = tg3_poll_controller,
14308#endif
14309};
14310
Linus Torvalds1da177e2005-04-16 15:20:36 -070014311static int __devinit tg3_init_one(struct pci_dev *pdev,
14312 const struct pci_device_id *ent)
14313{
Linus Torvalds1da177e2005-04-16 15:20:36 -070014314 struct net_device *dev;
14315 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000014316 int i, err, pm_cap;
14317 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070014318 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080014319 u64 dma_mask, persist_dma_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014320
Joe Perches05dbe002010-02-17 19:44:19 +000014321 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014322
14323 err = pci_enable_device(pdev);
14324 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014325 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014326 return err;
14327 }
14328
Linus Torvalds1da177e2005-04-16 15:20:36 -070014329 err = pci_request_regions(pdev, DRV_MODULE_NAME);
14330 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014331 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014332 goto err_out_disable_pdev;
14333 }
14334
14335 pci_set_master(pdev);
14336
14337 /* Find power-management capability. */
14338 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
14339 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000014340 dev_err(&pdev->dev,
14341 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014342 err = -EIO;
14343 goto err_out_free_res;
14344 }
14345
Matt Carlsonfe5f5782009-09-01 13:09:39 +000014346 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014347 if (!dev) {
Matt Carlson2445e462010-04-05 10:19:21 +000014348 dev_err(&pdev->dev, "Etherdev alloc failed, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014349 err = -ENOMEM;
14350 goto err_out_free_res;
14351 }
14352
Linus Torvalds1da177e2005-04-16 15:20:36 -070014353 SET_NETDEV_DEV(dev, &pdev->dev);
14354
Linus Torvalds1da177e2005-04-16 15:20:36 -070014355#if TG3_VLAN_TAG_USED
14356 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014357#endif
14358
14359 tp = netdev_priv(dev);
14360 tp->pdev = pdev;
14361 tp->dev = dev;
14362 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014363 tp->rx_mode = TG3_DEF_RX_MODE;
14364 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070014365
Linus Torvalds1da177e2005-04-16 15:20:36 -070014366 if (tg3_debug > 0)
14367 tp->msg_enable = tg3_debug;
14368 else
14369 tp->msg_enable = TG3_DEF_MSG_ENABLE;
14370
14371 /* The word/byte swap controls here control register access byte
14372 * swapping. DMA data byte swapping is controlled in the GRC_MODE
14373 * setting below.
14374 */
14375 tp->misc_host_ctrl =
14376 MISC_HOST_CTRL_MASK_PCI_INT |
14377 MISC_HOST_CTRL_WORD_SWAP |
14378 MISC_HOST_CTRL_INDIR_ACCESS |
14379 MISC_HOST_CTRL_PCISTATE_RW;
14380
14381 /* The NONFRM (non-frame) byte/word swap controls take effect
14382 * on descriptor entries, anything which isn't packet data.
14383 *
14384 * The StrongARM chips on the board (one for tx, one for rx)
14385 * are running in big-endian mode.
14386 */
14387 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
14388 GRC_MODE_WSWAP_NONFRM_DATA);
14389#ifdef __BIG_ENDIAN
14390 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
14391#endif
14392 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014393 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000014394 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014395
Matt Carlsond5fe4882008-11-21 17:20:32 -080014396 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010014397 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014398 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014399 err = -ENOMEM;
14400 goto err_out_free_dev;
14401 }
14402
14403 tg3_init_link_config(tp);
14404
Linus Torvalds1da177e2005-04-16 15:20:36 -070014405 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
14406 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014407
Linus Torvalds1da177e2005-04-16 15:20:36 -070014408 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014409 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014410 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014411
14412 err = tg3_get_invariants(tp);
14413 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014414 dev_err(&pdev->dev,
14415 "Problem fetching invariants of chip, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014416 goto err_out_iounmap;
14417 }
14418
Matt Carlson615774f2009-11-13 13:03:39 +000014419 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) &&
Matt Carlsona50d0792010-06-05 17:24:37 +000014420 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0 &&
14421 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5719)
Stephen Hemminger00829822008-11-20 20:14:53 -080014422 dev->netdev_ops = &tg3_netdev_ops;
14423 else
14424 dev->netdev_ops = &tg3_netdev_ops_dma_bug;
14425
14426
Michael Chan4a29cc22006-03-19 13:21:12 -080014427 /* The EPB bridge inside 5714, 5715, and 5780 and any
14428 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080014429 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
14430 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
14431 * do DMA address check in tg3_start_xmit().
14432 */
Michael Chan4a29cc22006-03-19 13:21:12 -080014433 if (tp->tg3_flags2 & TG3_FLG2_IS_5788)
Yang Hongyang284901a2009-04-06 19:01:15 -070014434 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Michael Chan4a29cc22006-03-19 13:21:12 -080014435 else if (tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070014436 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080014437#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070014438 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080014439#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080014440 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070014441 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080014442
14443 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070014444 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080014445 err = pci_set_dma_mask(pdev, dma_mask);
14446 if (!err) {
14447 dev->features |= NETIF_F_HIGHDMA;
14448 err = pci_set_consistent_dma_mask(pdev,
14449 persist_dma_mask);
14450 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014451 dev_err(&pdev->dev, "Unable to obtain 64 bit "
14452 "DMA for consistent allocations\n");
Michael Chan72f2afb2006-03-06 19:28:35 -080014453 goto err_out_iounmap;
14454 }
14455 }
14456 }
Yang Hongyang284901a2009-04-06 19:01:15 -070014457 if (err || dma_mask == DMA_BIT_MASK(32)) {
14458 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080014459 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014460 dev_err(&pdev->dev,
14461 "No usable DMA configuration, aborting\n");
Michael Chan72f2afb2006-03-06 19:28:35 -080014462 goto err_out_iounmap;
14463 }
14464 }
14465
Michael Chanfdfec1722005-07-25 12:31:48 -070014466 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014467
Matt Carlson507399f2009-11-13 13:03:37 +000014468 /* Selectively allow TSO based on operating conditions */
14469 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO) ||
14470 (tp->fw_needed && !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)))
14471 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
14472 else {
14473 tp->tg3_flags2 &= ~(TG3_FLG2_TSO_CAPABLE | TG3_FLG2_TSO_BUG);
14474 tp->fw_needed = NULL;
14475 }
14476
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014477 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
Matt Carlson9e9fd122009-01-19 16:57:45 -080014478 tp->fw_needed = FIRMWARE_TG3;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014479
Michael Chan4e3a7aa2006-03-20 17:47:44 -080014480 /* TSO is on by default on chips that support hardware TSO.
14481 * Firmware TSO on older chips gives lower performance, so it
14482 * is off by default, but can be enabled using ethtool.
14483 */
Matt Carlsone849cdc2009-11-13 13:03:38 +000014484 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO) &&
Eric Dumazet7fe876a2010-07-08 06:14:55 +000014485 (dev->features & NETIF_F_IP_CSUM)) {
Matt Carlsone849cdc2009-11-13 13:03:38 +000014486 dev->features |= NETIF_F_TSO;
Eric Dumazet7fe876a2010-07-08 06:14:55 +000014487 vlan_features_add(dev, NETIF_F_TSO);
14488 }
Matt Carlsone849cdc2009-11-13 13:03:38 +000014489 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_2) ||
14490 (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3)) {
Eric Dumazet7fe876a2010-07-08 06:14:55 +000014491 if (dev->features & NETIF_F_IPV6_CSUM) {
Michael Chanb0026622006-07-03 19:42:14 -070014492 dev->features |= NETIF_F_TSO6;
Eric Dumazet7fe876a2010-07-08 06:14:55 +000014493 vlan_features_add(dev, NETIF_F_TSO6);
14494 }
Matt Carlsone849cdc2009-11-13 13:03:38 +000014495 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
14496 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070014497 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
14498 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014499 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Eric Dumazet7fe876a2010-07-08 06:14:55 +000014500 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070014501 dev->features |= NETIF_F_TSO_ECN;
Eric Dumazet7fe876a2010-07-08 06:14:55 +000014502 vlan_features_add(dev, NETIF_F_TSO_ECN);
14503 }
Michael Chanb0026622006-07-03 19:42:14 -070014504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014505
Linus Torvalds1da177e2005-04-16 15:20:36 -070014506 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
14507 !(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) &&
14508 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
14509 tp->tg3_flags2 |= TG3_FLG2_MAX_RXPEND_64;
14510 tp->rx_pending = 63;
14511 }
14512
Linus Torvalds1da177e2005-04-16 15:20:36 -070014513 err = tg3_get_device_address(tp);
14514 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014515 dev_err(&pdev->dev,
14516 "Could not obtain valid ethernet address, aborting\n");
Matt Carlson026a6c22009-12-03 08:36:24 +000014517 goto err_out_iounmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014518 }
14519
Matt Carlson0d3031d2007-10-10 18:02:43 -070014520 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
Matt Carlson63532392008-11-03 16:49:57 -080014521 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
Al Viro79ea13c2008-01-24 02:06:46 -080014522 if (!tp->aperegs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014523 dev_err(&pdev->dev,
14524 "Cannot map APE registers, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070014525 err = -ENOMEM;
Matt Carlson026a6c22009-12-03 08:36:24 +000014526 goto err_out_iounmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070014527 }
14528
14529 tg3_ape_lock_init(tp);
Matt Carlson7fd76442009-02-25 14:27:20 +000014530
14531 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF)
14532 tg3_read_dash_ver(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070014533 }
14534
Matt Carlsonc88864d2007-11-12 21:07:01 -080014535 /*
14536 * Reset chip in case UNDI or EFI driver did not shutdown
14537 * DMA self test will enable WDMAC and we'll see (spurious)
14538 * pending DMA on the PCI bus at that point.
14539 */
14540 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
14541 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
14542 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
14543 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
14544 }
14545
14546 err = tg3_test_dma(tp);
14547 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014548 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080014549 goto err_out_apeunmap;
14550 }
14551
Matt Carlsonc88864d2007-11-12 21:07:01 -080014552 /* flow control autonegotiation is default behavior */
14553 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
Steve Glendinninge18ce342008-12-16 02:00:00 -080014554 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlsonc88864d2007-11-12 21:07:01 -080014555
Matt Carlson78f90dc2009-11-13 13:03:42 +000014556 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
14557 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
14558 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
14559 for (i = 0; i < TG3_IRQ_MAX_VECS; i++) {
14560 struct tg3_napi *tnapi = &tp->napi[i];
14561
14562 tnapi->tp = tp;
14563 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
14564
14565 tnapi->int_mbox = intmbx;
14566 if (i < 4)
14567 intmbx += 0x8;
14568 else
14569 intmbx += 0x4;
14570
14571 tnapi->consmbox = rcvmbx;
14572 tnapi->prodmbox = sndmbx;
14573
14574 if (i) {
14575 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
14576 netif_napi_add(dev, &tnapi->napi, tg3_poll_msix, 64);
14577 } else {
14578 tnapi->coal_now = HOSTCC_MODE_NOW;
14579 netif_napi_add(dev, &tnapi->napi, tg3_poll, 64);
14580 }
14581
14582 if (!(tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX))
14583 break;
14584
14585 /*
14586 * If we support MSIX, we'll be using RSS. If we're using
14587 * RSS, the first vector only handles link interrupts and the
14588 * remaining vectors handle rx and tx interrupts. Reuse the
14589 * mailbox values for the next iteration. The values we setup
14590 * above are still useful for the single vectored mode.
14591 */
14592 if (!i)
14593 continue;
14594
14595 rcvmbx += 0x8;
14596
14597 if (sndmbx & 0x4)
14598 sndmbx -= 0x4;
14599 else
14600 sndmbx += 0xc;
14601 }
14602
Matt Carlsonc88864d2007-11-12 21:07:01 -080014603 tg3_init_coal(tp);
14604
Michael Chanc49a1562006-12-17 17:07:29 -080014605 pci_set_drvdata(pdev, dev);
14606
Linus Torvalds1da177e2005-04-16 15:20:36 -070014607 err = register_netdev(dev);
14608 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014609 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070014610 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014611 }
14612
Joe Perches05dbe002010-02-17 19:44:19 +000014613 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
14614 tp->board_part_number,
14615 tp->pci_chip_rev_id,
14616 tg3_bus_string(tp, str),
14617 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014618
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000014619 if (tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED) {
14620 struct phy_device *phydev;
14621 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000014622 netdev_info(dev,
14623 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000014624 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000014625 } else
Matt Carlson5129c3a2010-04-05 10:19:23 +000014626 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
14627 "(WireSpeed[%d])\n", tg3_phy_string(tp),
Joe Perches05dbe002010-02-17 19:44:19 +000014628 ((tp->tg3_flags & TG3_FLAG_10_100_ONLY) ? "10/100Base-TX" :
14629 ((tp->tg3_flags2 & TG3_FLG2_ANY_SERDES) ? "1000Base-SX" :
14630 "10/100/1000Base-T")),
14631 (tp->tg3_flags2 & TG3_FLG2_NO_ETH_WIRE_SPEED) == 0);
Matt Carlsondf59c942008-11-03 16:52:56 -080014632
Joe Perches05dbe002010-02-17 19:44:19 +000014633 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
14634 (tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) != 0,
14635 (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) != 0,
14636 (tp->tg3_flags & TG3_FLAG_USE_MI_INTERRUPT) != 0,
14637 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0,
14638 (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) != 0);
14639 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
14640 tp->dma_rwctrl,
14641 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
14642 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014643
14644 return 0;
14645
Matt Carlson0d3031d2007-10-10 18:02:43 -070014646err_out_apeunmap:
14647 if (tp->aperegs) {
14648 iounmap(tp->aperegs);
14649 tp->aperegs = NULL;
14650 }
14651
Linus Torvalds1da177e2005-04-16 15:20:36 -070014652err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070014653 if (tp->regs) {
14654 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070014655 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070014656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014657
14658err_out_free_dev:
14659 free_netdev(dev);
14660
14661err_out_free_res:
14662 pci_release_regions(pdev);
14663
14664err_out_disable_pdev:
14665 pci_disable_device(pdev);
14666 pci_set_drvdata(pdev, NULL);
14667 return err;
14668}
14669
14670static void __devexit tg3_remove_one(struct pci_dev *pdev)
14671{
14672 struct net_device *dev = pci_get_drvdata(pdev);
14673
14674 if (dev) {
14675 struct tg3 *tp = netdev_priv(dev);
14676
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014677 if (tp->fw)
14678 release_firmware(tp->fw);
14679
Michael Chan7faa0062006-02-02 17:29:28 -080014680 flush_scheduled_work();
Matt Carlson158d7ab2008-05-29 01:37:54 -070014681
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014682 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
14683 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070014684 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014685 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070014686
Linus Torvalds1da177e2005-04-16 15:20:36 -070014687 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070014688 if (tp->aperegs) {
14689 iounmap(tp->aperegs);
14690 tp->aperegs = NULL;
14691 }
Michael Chan68929142005-08-09 20:17:14 -070014692 if (tp->regs) {
14693 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070014694 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070014695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014696 free_netdev(dev);
14697 pci_release_regions(pdev);
14698 pci_disable_device(pdev);
14699 pci_set_drvdata(pdev, NULL);
14700 }
14701}
14702
14703static int tg3_suspend(struct pci_dev *pdev, pm_message_t state)
14704{
14705 struct net_device *dev = pci_get_drvdata(pdev);
14706 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070014707 pci_power_t target_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014708 int err;
14709
Michael Chan3e0c95f2007-08-03 20:56:54 -070014710 /* PCI register 4 needs to be saved whether netif_running() or not.
14711 * MSI address and data need to be saved if using MSI and
14712 * netif_running().
14713 */
14714 pci_save_state(pdev);
14715
Linus Torvalds1da177e2005-04-16 15:20:36 -070014716 if (!netif_running(dev))
14717 return 0;
14718
Michael Chan7faa0062006-02-02 17:29:28 -080014719 flush_scheduled_work();
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014720 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014721 tg3_netif_stop(tp);
14722
14723 del_timer_sync(&tp->timer);
14724
David S. Millerf47c11e2005-06-24 20:18:35 -070014725 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014726 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070014727 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014728
14729 netif_device_detach(dev);
14730
David S. Millerf47c11e2005-06-24 20:18:35 -070014731 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070014732 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan6a9eba12005-12-13 21:08:58 -080014733 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
David S. Millerf47c11e2005-06-24 20:18:35 -070014734 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014735
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070014736 target_state = pdev->pm_cap ? pci_target_state(pdev) : PCI_D3hot;
14737
14738 err = tg3_set_power_state(tp, target_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014739 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014740 int err2;
14741
David S. Millerf47c11e2005-06-24 20:18:35 -070014742 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014743
Michael Chan6a9eba12005-12-13 21:08:58 -080014744 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014745 err2 = tg3_restart_hw(tp, 1);
14746 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070014747 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014748
14749 tp->timer.expires = jiffies + tp->timer_offset;
14750 add_timer(&tp->timer);
14751
14752 netif_device_attach(dev);
14753 tg3_netif_start(tp);
14754
Michael Chanb9ec6c12006-07-25 16:37:27 -070014755out:
David S. Millerf47c11e2005-06-24 20:18:35 -070014756 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014757
14758 if (!err2)
14759 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014760 }
14761
14762 return err;
14763}
14764
14765static int tg3_resume(struct pci_dev *pdev)
14766{
14767 struct net_device *dev = pci_get_drvdata(pdev);
14768 struct tg3 *tp = netdev_priv(dev);
14769 int err;
14770
Michael Chan3e0c95f2007-08-03 20:56:54 -070014771 pci_restore_state(tp->pdev);
14772
Linus Torvalds1da177e2005-04-16 15:20:36 -070014773 if (!netif_running(dev))
14774 return 0;
14775
Michael Chanbc1c7562006-03-20 17:48:03 -080014776 err = tg3_set_power_state(tp, PCI_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014777 if (err)
14778 return err;
14779
14780 netif_device_attach(dev);
14781
David S. Millerf47c11e2005-06-24 20:18:35 -070014782 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014783
Michael Chan6a9eba12005-12-13 21:08:58 -080014784 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Michael Chanb9ec6c12006-07-25 16:37:27 -070014785 err = tg3_restart_hw(tp, 1);
14786 if (err)
14787 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014788
14789 tp->timer.expires = jiffies + tp->timer_offset;
14790 add_timer(&tp->timer);
14791
Linus Torvalds1da177e2005-04-16 15:20:36 -070014792 tg3_netif_start(tp);
14793
Michael Chanb9ec6c12006-07-25 16:37:27 -070014794out:
David S. Millerf47c11e2005-06-24 20:18:35 -070014795 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014796
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014797 if (!err)
14798 tg3_phy_start(tp);
14799
Michael Chanb9ec6c12006-07-25 16:37:27 -070014800 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014801}
14802
14803static struct pci_driver tg3_driver = {
14804 .name = DRV_MODULE_NAME,
14805 .id_table = tg3_pci_tbl,
14806 .probe = tg3_init_one,
14807 .remove = __devexit_p(tg3_remove_one),
14808 .suspend = tg3_suspend,
14809 .resume = tg3_resume
14810};
14811
14812static int __init tg3_init(void)
14813{
Jeff Garzik29917622006-08-19 17:48:59 -040014814 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014815}
14816
14817static void __exit tg3_cleanup(void)
14818{
14819 pci_unregister_driver(&tg3_driver);
14820}
14821
14822module_init(tg3_init);
14823module_exit(tg3_cleanup);