blob: 4fa8ee30b70f81d9014fa4926e2ae97a66305ec4 [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 Carlson7c1a96a2010-10-14 10:37:45 +000072#define TG3_MIN_NUM 115
Matt Carlson6867c842010-07-11 09:31:44 +000073#define DRV_MODULE_VERSION \
74 __stringify(TG3_MAJ_NUM) "." __stringify(TG3_MIN_NUM)
Matt Carlson7c1a96a2010-10-14 10:37:45 +000075#define DRV_MODULE_RELDATE "October 14, 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 */
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000104#define TG3_RX_STD_RING_SIZE(tp) \
105 ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 || \
106 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) ? \
107 RX_STD_MAX_SIZE_5717 : 512)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108#define TG3_DEF_RX_RING_PENDING 200
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000109#define TG3_RX_JMB_RING_SIZE(tp) \
110 ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 || \
111 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) ? \
112 1024 : 256)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113#define TG3_DEF_RX_JUMBO_RING_PENDING 100
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000114#define TG3_RSS_INDIR_TBL_SIZE 128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116/* Do not place this n-ring entries value into the tp struct itself,
117 * we really want to expose these constants to GCC so that modulo et
118 * al. operations are done with shifts and masks instead of with
119 * hw multiply/modulo instructions. Another solution would be to
120 * replace things like '% foo' with '& (foo - 1)'.
121 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123#define TG3_TX_RING_SIZE 512
124#define TG3_DEF_TX_RING_PENDING (TG3_TX_RING_SIZE - 1)
125
Matt Carlson2c49a442010-09-30 10:34:35 +0000126#define TG3_RX_STD_RING_BYTES(tp) \
127 (sizeof(struct tg3_rx_buffer_desc) * TG3_RX_STD_RING_SIZE(tp))
128#define TG3_RX_JMB_RING_BYTES(tp) \
129 (sizeof(struct tg3_ext_rx_buffer_desc) * TG3_RX_JMB_RING_SIZE(tp))
130#define TG3_RX_RCB_RING_BYTES(tp) \
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000131 (sizeof(struct tg3_rx_buffer_desc) * (tp->rx_ret_ring_mask + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132#define TG3_TX_RING_BYTES (sizeof(struct tg3_tx_buffer_desc) * \
133 TG3_TX_RING_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134#define NEXT_TX(N) (((N) + 1) & (TG3_TX_RING_SIZE - 1))
135
Matt Carlson9dc7a112010-04-12 06:58:28 +0000136#define TG3_RX_DMA_ALIGN 16
137#define TG3_RX_HEADROOM ALIGN(VLAN_HLEN, TG3_RX_DMA_ALIGN)
138
Matt Carlson287be122009-08-28 13:58:46 +0000139#define TG3_DMA_BYTE_ENAB 64
140
141#define TG3_RX_STD_DMA_SZ 1536
142#define TG3_RX_JMB_DMA_SZ 9046
143
144#define TG3_RX_DMA_TO_MAP_SZ(x) ((x) + TG3_DMA_BYTE_ENAB)
145
146#define TG3_RX_STD_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_STD_DMA_SZ)
147#define TG3_RX_JMB_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_JMB_DMA_SZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Matt Carlson2c49a442010-09-30 10:34:35 +0000149#define TG3_RX_STD_BUFF_RING_SIZE(tp) \
150 (sizeof(struct ring_info) * TG3_RX_STD_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000151
Matt Carlson2c49a442010-09-30 10:34:35 +0000152#define TG3_RX_JMB_BUFF_RING_SIZE(tp) \
153 (sizeof(struct ring_info) * TG3_RX_JMB_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000154
Matt Carlsond2757fc2010-04-12 06:58:27 +0000155/* Due to a hardware bug, the 5701 can only DMA to memory addresses
156 * that are at least dword aligned when used in PCIX mode. The driver
157 * works around this bug by double copying the packet. This workaround
158 * is built into the normal double copy length check for efficiency.
159 *
160 * However, the double copy is only necessary on those architectures
161 * where unaligned memory accesses are inefficient. For those architectures
162 * where unaligned memory accesses incur little penalty, we can reintegrate
163 * the 5701 in the normal rx path. Doing so saves a device structure
164 * dereference by hardcoding the double copy threshold in place.
165 */
166#define TG3_RX_COPY_THRESHOLD 256
167#if NET_IP_ALIGN == 0 || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
168 #define TG3_RX_COPY_THRESH(tp) TG3_RX_COPY_THRESHOLD
169#else
170 #define TG3_RX_COPY_THRESH(tp) ((tp)->rx_copy_thresh)
171#endif
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173/* minimum number of free TX descriptors required to wake up TX process */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000174#define TG3_TX_WAKEUP_THRESH(tnapi) ((tnapi)->tx_pending / 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Matt Carlsonad829262008-11-21 17:16:16 -0800176#define TG3_RAW_IP_ALIGN 2
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178/* number of ETHTOOL_GSTATS u64's */
179#define TG3_NUM_STATS (sizeof(struct tg3_ethtool_stats)/sizeof(u64))
180
Michael Chan4cafd3f2005-05-29 14:56:34 -0700181#define TG3_NUM_TEST 6
182
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000183#define TG3_FW_UPDATE_TIMEOUT_SEC 5
184
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800185#define FIRMWARE_TG3 "tigon/tg3.bin"
186#define FIRMWARE_TG3TSO "tigon/tg3_tso.bin"
187#define FIRMWARE_TG3TSO5 "tigon/tg3_tso5.bin"
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189static char version[] __devinitdata =
Joe Perches05dbe002010-02-17 19:44:19 +0000190 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192MODULE_AUTHOR("David S. Miller (davem@redhat.com) and Jeff Garzik (jgarzik@pobox.com)");
193MODULE_DESCRIPTION("Broadcom Tigon3 ethernet driver");
194MODULE_LICENSE("GPL");
195MODULE_VERSION(DRV_MODULE_VERSION);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800196MODULE_FIRMWARE(FIRMWARE_TG3);
197MODULE_FIRMWARE(FIRMWARE_TG3TSO);
198MODULE_FIRMWARE(FIRMWARE_TG3TSO5);
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200static int tg3_debug = -1; /* -1 == use TG3_DEF_MSG_ENABLE as value */
201module_param(tg3_debug, int, 0);
202MODULE_PARM_DESC(tg3_debug, "Tigon3 bitmapped debugging message enable value");
203
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000204static DEFINE_PCI_DEVICE_TABLE(tg3_pci_tbl) = {
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700205 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5700)},
206 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5701)},
207 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702)},
208 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703)},
209 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704)},
210 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702FE)},
211 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705)},
212 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705_2)},
213 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M)},
214 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M_2)},
215 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702X)},
216 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703X)},
217 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S)},
218 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702A3)},
219 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703A3)},
220 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5782)},
221 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5788)},
222 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5789)},
223 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901)},
224 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901_2)},
225 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S_2)},
226 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705F)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700227 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5721)},
Michael Chan126a3362006-09-27 16:03:07 -0700228 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5722)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700229 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700230 {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)},
Matt Carlsonb0f75222010-01-20 16:58:11 +0000270 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57781)},
271 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57785)},
272 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57761)},
273 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57765)},
274 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57791)},
275 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57795)},
Matt Carlson302b5002010-06-05 17:24:38 +0000276 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5719)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700277 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9DXX)},
278 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9MXX)},
279 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1000)},
280 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1001)},
281 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1003)},
282 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC9100)},
283 {PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_TIGON3)},
284 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285};
286
287MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
288
Andreas Mohr50da8592006-08-14 23:54:30 -0700289static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 const char string[ETH_GSTRING_LEN];
291} ethtool_stats_keys[TG3_NUM_STATS] = {
292 { "rx_octets" },
293 { "rx_fragments" },
294 { "rx_ucast_packets" },
295 { "rx_mcast_packets" },
296 { "rx_bcast_packets" },
297 { "rx_fcs_errors" },
298 { "rx_align_errors" },
299 { "rx_xon_pause_rcvd" },
300 { "rx_xoff_pause_rcvd" },
301 { "rx_mac_ctrl_rcvd" },
302 { "rx_xoff_entered" },
303 { "rx_frame_too_long_errors" },
304 { "rx_jabbers" },
305 { "rx_undersize_packets" },
306 { "rx_in_length_errors" },
307 { "rx_out_length_errors" },
308 { "rx_64_or_less_octet_packets" },
309 { "rx_65_to_127_octet_packets" },
310 { "rx_128_to_255_octet_packets" },
311 { "rx_256_to_511_octet_packets" },
312 { "rx_512_to_1023_octet_packets" },
313 { "rx_1024_to_1522_octet_packets" },
314 { "rx_1523_to_2047_octet_packets" },
315 { "rx_2048_to_4095_octet_packets" },
316 { "rx_4096_to_8191_octet_packets" },
317 { "rx_8192_to_9022_octet_packets" },
318
319 { "tx_octets" },
320 { "tx_collisions" },
321
322 { "tx_xon_sent" },
323 { "tx_xoff_sent" },
324 { "tx_flow_control" },
325 { "tx_mac_errors" },
326 { "tx_single_collisions" },
327 { "tx_mult_collisions" },
328 { "tx_deferred" },
329 { "tx_excessive_collisions" },
330 { "tx_late_collisions" },
331 { "tx_collide_2times" },
332 { "tx_collide_3times" },
333 { "tx_collide_4times" },
334 { "tx_collide_5times" },
335 { "tx_collide_6times" },
336 { "tx_collide_7times" },
337 { "tx_collide_8times" },
338 { "tx_collide_9times" },
339 { "tx_collide_10times" },
340 { "tx_collide_11times" },
341 { "tx_collide_12times" },
342 { "tx_collide_13times" },
343 { "tx_collide_14times" },
344 { "tx_collide_15times" },
345 { "tx_ucast_packets" },
346 { "tx_mcast_packets" },
347 { "tx_bcast_packets" },
348 { "tx_carrier_sense_errors" },
349 { "tx_discards" },
350 { "tx_errors" },
351
352 { "dma_writeq_full" },
353 { "dma_write_prioq_full" },
354 { "rxbds_empty" },
355 { "rx_discards" },
356 { "rx_errors" },
357 { "rx_threshold_hit" },
358
359 { "dma_readq_full" },
360 { "dma_read_prioq_full" },
361 { "tx_comp_queue_full" },
362
363 { "ring_set_send_prod_index" },
364 { "ring_status_update" },
365 { "nic_irqs" },
366 { "nic_avoided_irqs" },
367 { "nic_tx_threshold_hit" }
368};
369
Andreas Mohr50da8592006-08-14 23:54:30 -0700370static const struct {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700371 const char string[ETH_GSTRING_LEN];
372} ethtool_test_keys[TG3_NUM_TEST] = {
373 { "nvram test (online) " },
374 { "link test (online) " },
375 { "register test (offline)" },
376 { "memory test (offline)" },
377 { "loopback test (offline)" },
378 { "interrupt test (offline)" },
379};
380
Michael Chanb401e9e2005-12-19 16:27:04 -0800381static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
382{
383 writel(val, tp->regs + off);
384}
385
386static u32 tg3_read32(struct tg3 *tp, u32 off)
387{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000388 return readl(tp->regs + off);
Michael Chanb401e9e2005-12-19 16:27:04 -0800389}
390
Matt Carlson0d3031d2007-10-10 18:02:43 -0700391static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
392{
393 writel(val, tp->aperegs + off);
394}
395
396static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
397{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000398 return readl(tp->aperegs + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700399}
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
402{
Michael Chan68929142005-08-09 20:17:14 -0700403 unsigned long flags;
404
405 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700406 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
407 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
Michael Chan68929142005-08-09 20:17:14 -0700408 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700409}
410
411static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
412{
413 writel(val, tp->regs + off);
414 readl(tp->regs + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
416
Michael Chan68929142005-08-09 20:17:14 -0700417static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
418{
419 unsigned long flags;
420 u32 val;
421
422 spin_lock_irqsave(&tp->indirect_lock, flags);
423 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
424 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
425 spin_unlock_irqrestore(&tp->indirect_lock, flags);
426 return val;
427}
428
429static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
430{
431 unsigned long flags;
432
433 if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
434 pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
435 TG3_64BIT_REG_LOW, val);
436 return;
437 }
Matt Carlson66711e62009-11-13 13:03:49 +0000438 if (off == TG3_RX_STD_PROD_IDX_REG) {
Michael Chan68929142005-08-09 20:17:14 -0700439 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
440 TG3_64BIT_REG_LOW, val);
441 return;
442 }
443
444 spin_lock_irqsave(&tp->indirect_lock, flags);
445 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
446 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
447 spin_unlock_irqrestore(&tp->indirect_lock, flags);
448
449 /* In indirect mode when disabling interrupts, we also need
450 * to clear the interrupt bit in the GRC local ctrl register.
451 */
452 if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
453 (val == 0x1)) {
454 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
455 tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
456 }
457}
458
459static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
460{
461 unsigned long flags;
462 u32 val;
463
464 spin_lock_irqsave(&tp->indirect_lock, flags);
465 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
466 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
467 spin_unlock_irqrestore(&tp->indirect_lock, flags);
468 return val;
469}
470
Michael Chanb401e9e2005-12-19 16:27:04 -0800471/* usec_wait specifies the wait time in usec when writing to certain registers
472 * where it is unsafe to read back the register without some delay.
473 * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
474 * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
475 */
476static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
Michael Chanb401e9e2005-12-19 16:27:04 -0800478 if ((tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG) ||
479 (tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND))
480 /* Non-posted methods */
481 tp->write32(tp, off, val);
482 else {
483 /* Posted method */
484 tg3_write32(tp, off, val);
485 if (usec_wait)
486 udelay(usec_wait);
487 tp->read32(tp, off);
488 }
489 /* Wait again after the read for the posted method to guarantee that
490 * the wait time is met.
491 */
492 if (usec_wait)
493 udelay(usec_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
495
Michael Chan09ee9292005-08-09 20:17:00 -0700496static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
497{
498 tp->write32_mbox(tp, off, val);
Michael Chan68929142005-08-09 20:17:14 -0700499 if (!(tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER) &&
500 !(tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND))
501 tp->read32_mbox(tp, off);
Michael Chan09ee9292005-08-09 20:17:00 -0700502}
503
Michael Chan20094932005-08-09 20:16:32 -0700504static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
506 void __iomem *mbox = tp->regs + off;
507 writel(val, mbox);
508 if (tp->tg3_flags & TG3_FLAG_TXD_MBOX_HWBUG)
509 writel(val, mbox);
510 if (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)
511 readl(mbox);
512}
513
Michael Chanb5d37722006-09-27 16:06:21 -0700514static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
515{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000516 return readl(tp->regs + off + GRCMBOX_BASE);
Michael Chanb5d37722006-09-27 16:06:21 -0700517}
518
519static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
520{
521 writel(val, tp->regs + off + GRCMBOX_BASE);
522}
523
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000524#define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700525#define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000526#define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
527#define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
528#define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
Michael Chan20094932005-08-09 20:16:32 -0700529
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000530#define tw32(reg, val) tp->write32(tp, reg, val)
531#define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
532#define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
533#define tr32(reg) tp->read32(tp, reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
536{
Michael Chan68929142005-08-09 20:17:14 -0700537 unsigned long flags;
538
Michael Chanb5d37722006-09-27 16:06:21 -0700539 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) &&
540 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
541 return;
542
Michael Chan68929142005-08-09 20:17:14 -0700543 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chanbbadf502006-04-06 21:46:34 -0700544 if (tp->tg3_flags & TG3_FLAG_SRAM_USE_CONFIG) {
545 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
546 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Michael Chanbbadf502006-04-06 21:46:34 -0700548 /* Always leave this as zero. */
549 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
550 } else {
551 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
552 tw32_f(TG3PCI_MEM_WIN_DATA, val);
553
554 /* Always leave this as zero. */
555 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
556 }
Michael Chan68929142005-08-09 20:17:14 -0700557 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
559
560static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
561{
Michael Chan68929142005-08-09 20:17:14 -0700562 unsigned long flags;
563
Michael Chanb5d37722006-09-27 16:06:21 -0700564 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) &&
565 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
566 *val = 0;
567 return;
568 }
569
Michael Chan68929142005-08-09 20:17:14 -0700570 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chanbbadf502006-04-06 21:46:34 -0700571 if (tp->tg3_flags & TG3_FLAG_SRAM_USE_CONFIG) {
572 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
573 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Michael Chanbbadf502006-04-06 21:46:34 -0700575 /* Always leave this as zero. */
576 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
577 } else {
578 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
579 *val = tr32(TG3PCI_MEM_WIN_DATA);
580
581 /* Always leave this as zero. */
582 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
583 }
Michael Chan68929142005-08-09 20:17:14 -0700584 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585}
586
Matt Carlson0d3031d2007-10-10 18:02:43 -0700587static void tg3_ape_lock_init(struct tg3 *tp)
588{
589 int i;
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000590 u32 regbase;
591
592 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
593 regbase = TG3_APE_LOCK_GRANT;
594 else
595 regbase = TG3_APE_PER_LOCK_GRANT;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700596
597 /* Make sure the driver hasn't any stale locks. */
598 for (i = 0; i < 8; i++)
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000599 tg3_ape_write32(tp, regbase + 4 * i, APE_LOCK_GRANT_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700600}
601
602static int tg3_ape_lock(struct tg3 *tp, int locknum)
603{
604 int i, off;
605 int ret = 0;
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000606 u32 status, req, gnt;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700607
608 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
609 return 0;
610
611 switch (locknum) {
Matt Carlson33f401a2010-04-05 10:19:27 +0000612 case TG3_APE_LOCK_GRC:
613 case TG3_APE_LOCK_MEM:
614 break;
615 default:
616 return -EINVAL;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700617 }
618
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000619 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
620 req = TG3_APE_LOCK_REQ;
621 gnt = TG3_APE_LOCK_GRANT;
622 } else {
623 req = TG3_APE_PER_LOCK_REQ;
624 gnt = TG3_APE_PER_LOCK_GRANT;
625 }
626
Matt Carlson0d3031d2007-10-10 18:02:43 -0700627 off = 4 * locknum;
628
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000629 tg3_ape_write32(tp, req + off, APE_LOCK_REQ_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700630
631 /* Wait for up to 1 millisecond to acquire lock. */
632 for (i = 0; i < 100; i++) {
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000633 status = tg3_ape_read32(tp, gnt + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700634 if (status == APE_LOCK_GRANT_DRIVER)
635 break;
636 udelay(10);
637 }
638
639 if (status != APE_LOCK_GRANT_DRIVER) {
640 /* Revoke the lock request. */
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000641 tg3_ape_write32(tp, gnt + off,
Matt Carlson0d3031d2007-10-10 18:02:43 -0700642 APE_LOCK_GRANT_DRIVER);
643
644 ret = -EBUSY;
645 }
646
647 return ret;
648}
649
650static void tg3_ape_unlock(struct tg3 *tp, int locknum)
651{
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000652 u32 gnt;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700653
654 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
655 return;
656
657 switch (locknum) {
Matt Carlson33f401a2010-04-05 10:19:27 +0000658 case TG3_APE_LOCK_GRC:
659 case TG3_APE_LOCK_MEM:
660 break;
661 default:
662 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700663 }
664
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000665 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
666 gnt = TG3_APE_LOCK_GRANT;
667 else
668 gnt = TG3_APE_PER_LOCK_GRANT;
669
670 tg3_ape_write32(tp, gnt + 4 * locknum, APE_LOCK_GRANT_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700671}
672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673static void tg3_disable_ints(struct tg3 *tp)
674{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000675 int i;
676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 tw32(TG3PCI_MISC_HOST_CTRL,
678 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000679 for (i = 0; i < tp->irq_max; i++)
680 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681}
682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683static void tg3_enable_ints(struct tg3 *tp)
684{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000685 int i;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000686
Michael Chanbbe832c2005-06-24 20:20:04 -0700687 tp->irq_sync = 0;
688 wmb();
689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 tw32(TG3PCI_MISC_HOST_CTRL,
691 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000692
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000693 tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000694 for (i = 0; i < tp->irq_cnt; i++) {
695 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000696
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000697 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
698 if (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)
699 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
700
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000701 tp->coal_now |= tnapi->coal_now;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000702 }
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000703
704 /* Force an initial interrupt */
705 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) &&
706 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
707 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
708 else
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000709 tw32(HOSTCC_MODE, tp->coal_now);
710
711 tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712}
713
Matt Carlson17375d22009-08-28 14:02:18 +0000714static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
Michael Chan04237dd2005-04-25 15:17:17 -0700715{
Matt Carlson17375d22009-08-28 14:02:18 +0000716 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +0000717 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan04237dd2005-04-25 15:17:17 -0700718 unsigned int work_exists = 0;
719
720 /* check for phy events */
721 if (!(tp->tg3_flags &
722 (TG3_FLAG_USE_LINKCHG_REG |
723 TG3_FLAG_POLL_SERDES))) {
724 if (sblk->status & SD_STATUS_LINK_CHG)
725 work_exists = 1;
726 }
727 /* check for RX/TX work to do */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000728 if (sblk->idx[0].tx_consumer != tnapi->tx_cons ||
Matt Carlson8d9d7cf2009-09-01 13:19:05 +0000729 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -0700730 work_exists = 1;
731
732 return work_exists;
733}
734
Matt Carlson17375d22009-08-28 14:02:18 +0000735/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -0700736 * similar to tg3_enable_ints, but it accurately determines whether there
737 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400738 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 */
Matt Carlson17375d22009-08-28 14:02:18 +0000740static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
Matt Carlson17375d22009-08-28 14:02:18 +0000742 struct tg3 *tp = tnapi->tp;
743
Matt Carlson898a56f2009-08-28 14:02:40 +0000744 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 mmiowb();
746
David S. Millerfac9b832005-05-18 22:46:34 -0700747 /* When doing tagged status, this work check is unnecessary.
748 * The last_tag we write above tells the chip which piece of
749 * work we've completed.
750 */
751 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) &&
Matt Carlson17375d22009-08-28 14:02:18 +0000752 tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -0700753 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +0000754 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755}
756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757static void tg3_switch_clocks(struct tg3 *tp)
758{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000759 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 u32 orig_clock_ctrl;
761
Matt Carlson795d01c2007-10-07 23:28:17 -0700762 if ((tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
763 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -0700764 return;
765
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000766 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 orig_clock_ctrl = clock_ctrl;
769 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
770 CLOCK_CTRL_CLKRUN_OENABLE |
771 0x1f);
772 tp->pci_clock_ctrl = clock_ctrl;
773
774 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
775 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800776 tw32_wait_f(TG3PCI_CLOCK_CTRL,
777 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 }
779 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800780 tw32_wait_f(TG3PCI_CLOCK_CTRL,
781 clock_ctrl |
782 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
783 40);
784 tw32_wait_f(TG3PCI_CLOCK_CTRL,
785 clock_ctrl | (CLOCK_CTRL_ALTCLK),
786 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 }
Michael Chanb401e9e2005-12-19 16:27:04 -0800788 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789}
790
791#define PHY_BUSY_LOOPS 5000
792
793static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
794{
795 u32 frame_val;
796 unsigned int loops;
797 int ret;
798
799 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
800 tw32_f(MAC_MI_MODE,
801 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
802 udelay(80);
803 }
804
805 *val = 0x0;
806
Matt Carlson882e9792009-09-01 13:21:36 +0000807 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 MI_COM_PHY_ADDR_MASK);
809 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
810 MI_COM_REG_ADDR_MASK);
811 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 tw32_f(MAC_MI_COM, frame_val);
814
815 loops = PHY_BUSY_LOOPS;
816 while (loops != 0) {
817 udelay(10);
818 frame_val = tr32(MAC_MI_COM);
819
820 if ((frame_val & MI_COM_BUSY) == 0) {
821 udelay(5);
822 frame_val = tr32(MAC_MI_COM);
823 break;
824 }
825 loops -= 1;
826 }
827
828 ret = -EBUSY;
829 if (loops != 0) {
830 *val = frame_val & MI_COM_DATA_MASK;
831 ret = 0;
832 }
833
834 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
835 tw32_f(MAC_MI_MODE, tp->mi_mode);
836 udelay(80);
837 }
838
839 return ret;
840}
841
842static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
843{
844 u32 frame_val;
845 unsigned int loops;
846 int ret;
847
Matt Carlsonf07e9af2010-08-02 11:26:07 +0000848 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Michael Chanb5d37722006-09-27 16:06:21 -0700849 (reg == MII_TG3_CTRL || reg == MII_TG3_AUX_CTRL))
850 return 0;
851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
853 tw32_f(MAC_MI_MODE,
854 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
855 udelay(80);
856 }
857
Matt Carlson882e9792009-09-01 13:21:36 +0000858 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 MI_COM_PHY_ADDR_MASK);
860 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
861 MI_COM_REG_ADDR_MASK);
862 frame_val |= (val & MI_COM_DATA_MASK);
863 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 tw32_f(MAC_MI_COM, frame_val);
866
867 loops = PHY_BUSY_LOOPS;
868 while (loops != 0) {
869 udelay(10);
870 frame_val = tr32(MAC_MI_COM);
871 if ((frame_val & MI_COM_BUSY) == 0) {
872 udelay(5);
873 frame_val = tr32(MAC_MI_COM);
874 break;
875 }
876 loops -= 1;
877 }
878
879 ret = -EBUSY;
880 if (loops != 0)
881 ret = 0;
882
883 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
884 tw32_f(MAC_MI_MODE, tp->mi_mode);
885 udelay(80);
886 }
887
888 return ret;
889}
890
Matt Carlson95e28692008-05-25 23:44:14 -0700891static int tg3_bmcr_reset(struct tg3 *tp)
892{
893 u32 phy_control;
894 int limit, err;
895
896 /* OK, reset it, and poll the BMCR_RESET bit until it
897 * clears or we time out.
898 */
899 phy_control = BMCR_RESET;
900 err = tg3_writephy(tp, MII_BMCR, phy_control);
901 if (err != 0)
902 return -EBUSY;
903
904 limit = 5000;
905 while (limit--) {
906 err = tg3_readphy(tp, MII_BMCR, &phy_control);
907 if (err != 0)
908 return -EBUSY;
909
910 if ((phy_control & BMCR_RESET) == 0) {
911 udelay(40);
912 break;
913 }
914 udelay(10);
915 }
Roel Kluind4675b52009-02-12 16:33:27 -0800916 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -0700917 return -EBUSY;
918
919 return 0;
920}
921
Matt Carlson158d7ab2008-05-29 01:37:54 -0700922static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
923{
Francois Romieu3d165432009-01-19 16:56:50 -0800924 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700925 u32 val;
926
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000927 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -0700928
929 if (tg3_readphy(tp, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000930 val = -EIO;
931
932 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -0700933
934 return val;
935}
936
937static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
938{
Francois Romieu3d165432009-01-19 16:56:50 -0800939 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000940 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700941
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000942 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -0700943
944 if (tg3_writephy(tp, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000945 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700946
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000947 spin_unlock_bh(&tp->lock);
948
949 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700950}
951
952static int tg3_mdio_reset(struct mii_bus *bp)
953{
954 return 0;
955}
956
Matt Carlson9c61d6b2008-11-03 16:54:56 -0800957static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -0700958{
959 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800960 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -0700961
Matt Carlson3f0e3ad2009-11-02 14:24:36 +0000962 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800963 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +0000964 case PHY_ID_BCM50610:
965 case PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800966 val = MAC_PHYCFG2_50610_LED_MODES;
967 break;
Matt Carlson6a443a02010-02-17 15:17:04 +0000968 case PHY_ID_BCMAC131:
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800969 val = MAC_PHYCFG2_AC131_LED_MODES;
970 break;
Matt Carlson6a443a02010-02-17 15:17:04 +0000971 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800972 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
973 break;
Matt Carlson6a443a02010-02-17 15:17:04 +0000974 case PHY_ID_RTL8201E:
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800975 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
976 break;
977 default:
Matt Carlsona9daf362008-05-25 23:49:44 -0700978 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800979 }
980
981 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
982 tw32(MAC_PHYCFG2, val);
983
984 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +0000985 val &= ~(MAC_PHYCFG1_RGMII_INT |
986 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
987 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800988 tw32(MAC_PHYCFG1, val);
989
990 return;
991 }
992
Matt Carlson14417062010-02-17 15:16:59 +0000993 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE))
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800994 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
995 MAC_PHYCFG2_FMODE_MASK_MASK |
996 MAC_PHYCFG2_GMODE_MASK_MASK |
997 MAC_PHYCFG2_ACT_MASK_MASK |
998 MAC_PHYCFG2_QUAL_MASK_MASK |
999 MAC_PHYCFG2_INBAND_ENABLE;
1000
1001 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001002
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001003 val = tr32(MAC_PHYCFG1);
1004 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
1005 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
Matt Carlson14417062010-02-17 15:16:59 +00001006 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE)) {
Matt Carlsona9daf362008-05-25 23:49:44 -07001007 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
1008 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
1009 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
1010 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1011 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001012 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1013 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1014 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001015
Matt Carlsona9daf362008-05-25 23:49:44 -07001016 val = tr32(MAC_EXT_RGMII_MODE);
1017 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1018 MAC_RGMII_MODE_RX_QUALITY |
1019 MAC_RGMII_MODE_RX_ACTIVITY |
1020 MAC_RGMII_MODE_RX_ENG_DET |
1021 MAC_RGMII_MODE_TX_ENABLE |
1022 MAC_RGMII_MODE_TX_LOWPWR |
1023 MAC_RGMII_MODE_TX_RESET);
Matt Carlson14417062010-02-17 15:16:59 +00001024 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE)) {
Matt Carlsona9daf362008-05-25 23:49:44 -07001025 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
1026 val |= MAC_RGMII_MODE_RX_INT_B |
1027 MAC_RGMII_MODE_RX_QUALITY |
1028 MAC_RGMII_MODE_RX_ACTIVITY |
1029 MAC_RGMII_MODE_RX_ENG_DET;
1030 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
1031 val |= MAC_RGMII_MODE_TX_ENABLE |
1032 MAC_RGMII_MODE_TX_LOWPWR |
1033 MAC_RGMII_MODE_TX_RESET;
1034 }
1035 tw32(MAC_EXT_RGMII_MODE, val);
1036}
1037
Matt Carlson158d7ab2008-05-29 01:37:54 -07001038static void tg3_mdio_start(struct tg3 *tp)
1039{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001040 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1041 tw32_f(MAC_MI_MODE, tp->mi_mode);
1042 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001043
Matt Carlson9ea48182010-02-17 15:17:01 +00001044 if ((tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED) &&
1045 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1046 tg3_mdio_config_5785(tp);
1047}
1048
1049static int tg3_mdio_init(struct tg3 *tp)
1050{
1051 int i;
1052 u32 reg;
1053 struct phy_device *phydev;
1054
Matt Carlsona50d0792010-06-05 17:24:37 +00001055 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
1056 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) {
Matt Carlson9c7df912010-06-05 17:24:36 +00001057 u32 is_serdes;
Matt Carlson882e9792009-09-01 13:21:36 +00001058
Matt Carlson9c7df912010-06-05 17:24:36 +00001059 tp->phy_addr = PCI_FUNC(tp->pdev->devfn) + 1;
Matt Carlson882e9792009-09-01 13:21:36 +00001060
Matt Carlsond1ec96a2010-01-12 10:11:38 +00001061 if (tp->pci_chip_rev_id != CHIPREV_ID_5717_A0)
1062 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1063 else
1064 is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1065 TG3_CPMU_PHY_STRAP_IS_SERDES;
Matt Carlson882e9792009-09-01 13:21:36 +00001066 if (is_serdes)
1067 tp->phy_addr += 7;
1068 } else
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001069 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001070
Matt Carlson158d7ab2008-05-29 01:37:54 -07001071 tg3_mdio_start(tp);
1072
1073 if (!(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) ||
1074 (tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED))
1075 return 0;
1076
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001077 tp->mdio_bus = mdiobus_alloc();
1078 if (tp->mdio_bus == NULL)
1079 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001080
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001081 tp->mdio_bus->name = "tg3 mdio bus";
1082 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001083 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001084 tp->mdio_bus->priv = tp;
1085 tp->mdio_bus->parent = &tp->pdev->dev;
1086 tp->mdio_bus->read = &tg3_mdio_read;
1087 tp->mdio_bus->write = &tg3_mdio_write;
1088 tp->mdio_bus->reset = &tg3_mdio_reset;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001089 tp->mdio_bus->phy_mask = ~(1 << TG3_PHY_MII_ADDR);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001090 tp->mdio_bus->irq = &tp->mdio_irq[0];
Matt Carlson158d7ab2008-05-29 01:37:54 -07001091
1092 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001093 tp->mdio_bus->irq[i] = PHY_POLL;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001094
1095 /* The bus registration will look for all the PHYs on the mdio bus.
1096 * Unfortunately, it does not ensure the PHY is powered up before
1097 * accessing the PHY ID registers. A chip reset is the
1098 * quickest way to bring the device back to an operational state..
1099 */
1100 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1101 tg3_bmcr_reset(tp);
1102
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001103 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001104 if (i) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001105 dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001106 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001107 return i;
1108 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001109
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001110 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsona9daf362008-05-25 23:49:44 -07001111
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001112 if (!phydev || !phydev->drv) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001113 dev_warn(&tp->pdev->dev, "No PHY devices\n");
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001114 mdiobus_unregister(tp->mdio_bus);
1115 mdiobus_free(tp->mdio_bus);
1116 return -ENODEV;
1117 }
1118
1119 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001120 case PHY_ID_BCM57780:
Matt Carlson321d32a2008-11-21 17:22:19 -08001121 phydev->interface = PHY_INTERFACE_MODE_GMII;
Matt Carlsonc704dc22009-11-02 14:32:12 +00001122 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08001123 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001124 case PHY_ID_BCM50610:
1125 case PHY_ID_BCM50610M:
Matt Carlson32e5a8d2009-11-02 14:31:39 +00001126 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001127 PHY_BRCM_RX_REFCLK_UNUSED |
Matt Carlson52fae082009-11-02 14:32:38 +00001128 PHY_BRCM_DIS_TXCRXC_NOENRGY |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001129 PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson14417062010-02-17 15:16:59 +00001130 if (tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE)
Matt Carlsona9daf362008-05-25 23:49:44 -07001131 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
1132 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
1133 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
1134 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
1135 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001136 /* fallthru */
Matt Carlson6a443a02010-02-17 15:17:04 +00001137 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001138 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001139 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001140 case PHY_ID_RTL8201E:
1141 case PHY_ID_BCMAC131:
Matt Carlsona9daf362008-05-25 23:49:44 -07001142 phydev->interface = PHY_INTERFACE_MODE_MII;
Matt Carlsoncdd4e092009-11-02 14:31:11 +00001143 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001144 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlsona9daf362008-05-25 23:49:44 -07001145 break;
1146 }
1147
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001148 tp->tg3_flags3 |= TG3_FLG3_MDIOBUS_INITED;
1149
1150 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1151 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001152
1153 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001154}
1155
1156static void tg3_mdio_fini(struct tg3 *tp)
1157{
1158 if (tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED) {
1159 tp->tg3_flags3 &= ~TG3_FLG3_MDIOBUS_INITED;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001160 mdiobus_unregister(tp->mdio_bus);
1161 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001162 }
1163}
1164
Matt Carlsonddfc87b2010-10-14 10:37:40 +00001165static int tg3_phy_cl45_write(struct tg3 *tp, u32 devad, u32 addr, u32 val)
1166{
1167 int err;
1168
1169 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1170 if (err)
1171 goto done;
1172
1173 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1174 if (err)
1175 goto done;
1176
1177 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1178 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1179 if (err)
1180 goto done;
1181
1182 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, val);
1183
1184done:
1185 return err;
1186}
1187
1188static int tg3_phy_cl45_read(struct tg3 *tp, u32 devad, u32 addr, u32 *val)
1189{
1190 int err;
1191
1192 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1193 if (err)
1194 goto done;
1195
1196 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1197 if (err)
1198 goto done;
1199
1200 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1201 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1202 if (err)
1203 goto done;
1204
1205 err = tg3_readphy(tp, MII_TG3_MMD_ADDRESS, val);
1206
1207done:
1208 return err;
1209}
1210
Matt Carlson95e28692008-05-25 23:44:14 -07001211/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001212static inline void tg3_generate_fw_event(struct tg3 *tp)
1213{
1214 u32 val;
1215
1216 val = tr32(GRC_RX_CPU_EVENT);
1217 val |= GRC_RX_CPU_DRIVER_EVENT;
1218 tw32_f(GRC_RX_CPU_EVENT, val);
1219
1220 tp->last_event_jiffies = jiffies;
1221}
1222
1223#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1224
1225/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001226static void tg3_wait_for_event_ack(struct tg3 *tp)
1227{
1228 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001229 unsigned int delay_cnt;
1230 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001231
Matt Carlson4ba526c2008-08-15 14:10:04 -07001232 /* If enough time has passed, no wait is necessary. */
1233 time_remain = (long)(tp->last_event_jiffies + 1 +
1234 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1235 (long)jiffies;
1236 if (time_remain < 0)
1237 return;
1238
1239 /* Check if we can shorten the wait time. */
1240 delay_cnt = jiffies_to_usecs(time_remain);
1241 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1242 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1243 delay_cnt = (delay_cnt >> 3) + 1;
1244
1245 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001246 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1247 break;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001248 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001249 }
1250}
1251
1252/* tp->lock is held. */
1253static void tg3_ump_link_report(struct tg3 *tp)
1254{
1255 u32 reg;
1256 u32 val;
1257
1258 if (!(tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
1259 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
1260 return;
1261
1262 tg3_wait_for_event_ack(tp);
1263
1264 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1265
1266 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1267
1268 val = 0;
1269 if (!tg3_readphy(tp, MII_BMCR, &reg))
1270 val = reg << 16;
1271 if (!tg3_readphy(tp, MII_BMSR, &reg))
1272 val |= (reg & 0xffff);
1273 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX, val);
1274
1275 val = 0;
1276 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1277 val = reg << 16;
1278 if (!tg3_readphy(tp, MII_LPA, &reg))
1279 val |= (reg & 0xffff);
1280 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 4, val);
1281
1282 val = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001283 if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
Matt Carlson95e28692008-05-25 23:44:14 -07001284 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1285 val = reg << 16;
1286 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1287 val |= (reg & 0xffff);
1288 }
1289 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 8, val);
1290
1291 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1292 val = reg << 16;
1293 else
1294 val = 0;
1295 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 12, val);
1296
Matt Carlson4ba526c2008-08-15 14:10:04 -07001297 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001298}
1299
1300static void tg3_link_report(struct tg3 *tp)
1301{
1302 if (!netif_carrier_ok(tp->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001303 netif_info(tp, link, tp->dev, "Link is down\n");
Matt Carlson95e28692008-05-25 23:44:14 -07001304 tg3_ump_link_report(tp);
1305 } else if (netif_msg_link(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001306 netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
1307 (tp->link_config.active_speed == SPEED_1000 ?
1308 1000 :
1309 (tp->link_config.active_speed == SPEED_100 ?
1310 100 : 10)),
1311 (tp->link_config.active_duplex == DUPLEX_FULL ?
1312 "full" : "half"));
Matt Carlson95e28692008-05-25 23:44:14 -07001313
Joe Perches05dbe002010-02-17 19:44:19 +00001314 netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
1315 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
1316 "on" : "off",
1317 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
1318 "on" : "off");
Matt Carlson95e28692008-05-25 23:44:14 -07001319 tg3_ump_link_report(tp);
1320 }
1321}
1322
1323static u16 tg3_advert_flowctrl_1000T(u8 flow_ctrl)
1324{
1325 u16 miireg;
1326
Steve Glendinninge18ce342008-12-16 02:00:00 -08001327 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001328 miireg = ADVERTISE_PAUSE_CAP;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001329 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001330 miireg = ADVERTISE_PAUSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001331 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001332 miireg = ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1333 else
1334 miireg = 0;
1335
1336 return miireg;
1337}
1338
1339static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1340{
1341 u16 miireg;
1342
Steve Glendinninge18ce342008-12-16 02:00:00 -08001343 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001344 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001345 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001346 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001347 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001348 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1349 else
1350 miireg = 0;
1351
1352 return miireg;
1353}
1354
Matt Carlson95e28692008-05-25 23:44:14 -07001355static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1356{
1357 u8 cap = 0;
1358
1359 if (lcladv & ADVERTISE_1000XPAUSE) {
1360 if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1361 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001362 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001363 else if (rmtadv & LPA_1000XPAUSE_ASYM)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001364 cap = FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001365 } else {
1366 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001367 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001368 }
1369 } else if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1370 if ((rmtadv & LPA_1000XPAUSE) && (rmtadv & LPA_1000XPAUSE_ASYM))
Steve Glendinninge18ce342008-12-16 02:00:00 -08001371 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001372 }
1373
1374 return cap;
1375}
1376
Matt Carlsonf51f3562008-05-25 23:45:08 -07001377static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001378{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001379 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001380 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001381 u32 old_rx_mode = tp->rx_mode;
1382 u32 old_tx_mode = tp->tx_mode;
1383
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001384 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001385 autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001386 else
1387 autoneg = tp->link_config.autoneg;
1388
1389 if (autoneg == AUTONEG_ENABLE &&
Matt Carlson95e28692008-05-25 23:44:14 -07001390 (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001391 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001392 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001393 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001394 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001395 } else
1396 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001397
Matt Carlsonf51f3562008-05-25 23:45:08 -07001398 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001399
Steve Glendinninge18ce342008-12-16 02:00:00 -08001400 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001401 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1402 else
1403 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1404
Matt Carlsonf51f3562008-05-25 23:45:08 -07001405 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001406 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001407
Steve Glendinninge18ce342008-12-16 02:00:00 -08001408 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001409 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1410 else
1411 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1412
Matt Carlsonf51f3562008-05-25 23:45:08 -07001413 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001414 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001415}
1416
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001417static void tg3_adjust_link(struct net_device *dev)
1418{
1419 u8 oldflowctrl, linkmesg = 0;
1420 u32 mac_mode, lcl_adv, rmt_adv;
1421 struct tg3 *tp = netdev_priv(dev);
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001422 struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001423
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001424 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001425
1426 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
1427 MAC_MODE_HALF_DUPLEX);
1428
1429 oldflowctrl = tp->link_config.active_flowctrl;
1430
1431 if (phydev->link) {
1432 lcl_adv = 0;
1433 rmt_adv = 0;
1434
1435 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
1436 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001437 else if (phydev->speed == SPEED_1000 ||
1438 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001439 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001440 else
1441 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001442
1443 if (phydev->duplex == DUPLEX_HALF)
1444 mac_mode |= MAC_MODE_HALF_DUPLEX;
1445 else {
1446 lcl_adv = tg3_advert_flowctrl_1000T(
1447 tp->link_config.flowctrl);
1448
1449 if (phydev->pause)
1450 rmt_adv = LPA_PAUSE_CAP;
1451 if (phydev->asym_pause)
1452 rmt_adv |= LPA_PAUSE_ASYM;
1453 }
1454
1455 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
1456 } else
1457 mac_mode |= MAC_MODE_PORT_MODE_GMII;
1458
1459 if (mac_mode != tp->mac_mode) {
1460 tp->mac_mode = mac_mode;
1461 tw32_f(MAC_MODE, tp->mac_mode);
1462 udelay(40);
1463 }
1464
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001465 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
1466 if (phydev->speed == SPEED_10)
1467 tw32(MAC_MI_STAT,
1468 MAC_MI_STAT_10MBPS_MODE |
1469 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1470 else
1471 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1472 }
1473
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001474 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
1475 tw32(MAC_TX_LENGTHS,
1476 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1477 (6 << TX_LENGTHS_IPG_SHIFT) |
1478 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
1479 else
1480 tw32(MAC_TX_LENGTHS,
1481 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1482 (6 << TX_LENGTHS_IPG_SHIFT) |
1483 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
1484
1485 if ((phydev->link && tp->link_config.active_speed == SPEED_INVALID) ||
1486 (!phydev->link && tp->link_config.active_speed != SPEED_INVALID) ||
1487 phydev->speed != tp->link_config.active_speed ||
1488 phydev->duplex != tp->link_config.active_duplex ||
1489 oldflowctrl != tp->link_config.active_flowctrl)
Matt Carlsonc6cdf432010-04-05 10:19:26 +00001490 linkmesg = 1;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001491
1492 tp->link_config.active_speed = phydev->speed;
1493 tp->link_config.active_duplex = phydev->duplex;
1494
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001495 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001496
1497 if (linkmesg)
1498 tg3_link_report(tp);
1499}
1500
1501static int tg3_phy_init(struct tg3 *tp)
1502{
1503 struct phy_device *phydev;
1504
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001505 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001506 return 0;
1507
1508 /* Bring the PHY back to a known state. */
1509 tg3_bmcr_reset(tp);
1510
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001511 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001512
1513 /* Attach the MAC to the PHY. */
Kay Sieversfb28ad32008-11-10 13:55:14 -08001514 phydev = phy_connect(tp->dev, dev_name(&phydev->dev), tg3_adjust_link,
Matt Carlsona9daf362008-05-25 23:49:44 -07001515 phydev->dev_flags, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001516 if (IS_ERR(phydev)) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001517 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001518 return PTR_ERR(phydev);
1519 }
1520
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001521 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001522 switch (phydev->interface) {
1523 case PHY_INTERFACE_MODE_GMII:
1524 case PHY_INTERFACE_MODE_RGMII:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001525 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Matt Carlson321d32a2008-11-21 17:22:19 -08001526 phydev->supported &= (PHY_GBIT_FEATURES |
1527 SUPPORTED_Pause |
1528 SUPPORTED_Asym_Pause);
1529 break;
1530 }
1531 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001532 case PHY_INTERFACE_MODE_MII:
1533 phydev->supported &= (PHY_BASIC_FEATURES |
1534 SUPPORTED_Pause |
1535 SUPPORTED_Asym_Pause);
1536 break;
1537 default:
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001538 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001539 return -EINVAL;
1540 }
1541
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001542 tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001543
1544 phydev->advertising = phydev->supported;
1545
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001546 return 0;
1547}
1548
1549static void tg3_phy_start(struct tg3 *tp)
1550{
1551 struct phy_device *phydev;
1552
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001553 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001554 return;
1555
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001556 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001557
Matt Carlson80096062010-08-02 11:26:06 +00001558 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
1559 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001560 phydev->speed = tp->link_config.orig_speed;
1561 phydev->duplex = tp->link_config.orig_duplex;
1562 phydev->autoneg = tp->link_config.orig_autoneg;
1563 phydev->advertising = tp->link_config.orig_advertising;
1564 }
1565
1566 phy_start(phydev);
1567
1568 phy_start_aneg(phydev);
1569}
1570
1571static void tg3_phy_stop(struct tg3 *tp)
1572{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001573 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001574 return;
1575
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001576 phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001577}
1578
1579static void tg3_phy_fini(struct tg3 *tp)
1580{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001581 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001582 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001583 tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001584 }
1585}
1586
Matt Carlson52b02d02010-10-14 10:37:41 +00001587static int tg3_phydsp_read(struct tg3 *tp, u32 reg, u32 *val)
1588{
1589 int err;
1590
1591 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1592 if (!err)
1593 err = tg3_readphy(tp, MII_TG3_DSP_RW_PORT, val);
1594
1595 return err;
1596}
1597
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00001598static int tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001599{
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00001600 int err;
1601
1602 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1603 if (!err)
1604 err = tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
1605
1606 return err;
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001607}
1608
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001609static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
1610{
1611 u32 phytest;
1612
1613 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
1614 u32 phy;
1615
1616 tg3_writephy(tp, MII_TG3_FET_TEST,
1617 phytest | MII_TG3_FET_SHADOW_EN);
1618 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
1619 if (enable)
1620 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
1621 else
1622 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
1623 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
1624 }
1625 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
1626 }
1627}
1628
Matt Carlson6833c042008-11-21 17:18:59 -08001629static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
1630{
1631 u32 reg;
1632
Matt Carlsonecf14102010-01-20 16:58:05 +00001633 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
Matt Carlsona50d0792010-06-05 17:24:37 +00001634 ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
1635 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001636 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08001637 return;
1638
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001639 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001640 tg3_phy_fet_toggle_apd(tp, enable);
1641 return;
1642 }
1643
Matt Carlson6833c042008-11-21 17:18:59 -08001644 reg = MII_TG3_MISC_SHDW_WREN |
1645 MII_TG3_MISC_SHDW_SCR5_SEL |
1646 MII_TG3_MISC_SHDW_SCR5_LPED |
1647 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
1648 MII_TG3_MISC_SHDW_SCR5_SDTL |
1649 MII_TG3_MISC_SHDW_SCR5_C125OE;
1650 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 || !enable)
1651 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
1652
1653 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1654
1655
1656 reg = MII_TG3_MISC_SHDW_WREN |
1657 MII_TG3_MISC_SHDW_APD_SEL |
1658 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
1659 if (enable)
1660 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
1661
1662 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1663}
1664
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001665static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
1666{
1667 u32 phy;
1668
1669 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001670 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001671 return;
1672
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001673 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001674 u32 ephy;
1675
Matt Carlson535ef6e2009-08-25 10:09:36 +00001676 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
1677 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
1678
1679 tg3_writephy(tp, MII_TG3_FET_TEST,
1680 ephy | MII_TG3_FET_SHADOW_EN);
1681 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001682 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00001683 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001684 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00001685 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
1686 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001687 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00001688 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001689 }
1690 } else {
1691 phy = MII_TG3_AUXCTL_MISC_RDSEL_MISC |
1692 MII_TG3_AUXCTL_SHDWSEL_MISC;
1693 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, phy) &&
1694 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &phy)) {
1695 if (enable)
1696 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1697 else
1698 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1699 phy |= MII_TG3_AUXCTL_MISC_WREN;
1700 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1701 }
1702 }
1703}
1704
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705static void tg3_phy_set_wirespeed(struct tg3 *tp)
1706{
1707 u32 val;
1708
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001709 if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 return;
1711
1712 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x7007) &&
1713 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &val))
1714 tg3_writephy(tp, MII_TG3_AUX_CTRL,
1715 (val | (1 << 15) | (1 << 4)));
1716}
1717
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001718static void tg3_phy_apply_otp(struct tg3 *tp)
1719{
1720 u32 otp, phy;
1721
1722 if (!tp->phy_otp)
1723 return;
1724
1725 otp = tp->phy_otp;
1726
1727 /* Enable SM_DSP clock and tx 6dB coding. */
1728 phy = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1729 MII_TG3_AUXCTL_ACTL_SMDSP_ENA |
1730 MII_TG3_AUXCTL_ACTL_TX_6DB;
1731 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1732
1733 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
1734 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
1735 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
1736
1737 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
1738 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
1739 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
1740
1741 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
1742 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
1743 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
1744
1745 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
1746 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
1747
1748 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
1749 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
1750
1751 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
1752 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
1753 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
1754
1755 /* Turn off SM_DSP clock. */
1756 phy = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1757 MII_TG3_AUXCTL_ACTL_TX_6DB;
1758 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1759}
1760
Matt Carlson52b02d02010-10-14 10:37:41 +00001761static void tg3_phy_eee_adjust(struct tg3 *tp, u32 current_link_up)
1762{
1763 u32 val;
1764
1765 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
1766 return;
1767
1768 tp->setlpicnt = 0;
1769
1770 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
1771 current_link_up == 1 &&
1772 (tp->link_config.active_speed == SPEED_1000 ||
1773 (tp->link_config.active_speed == SPEED_100 &&
1774 tp->link_config.active_duplex == DUPLEX_FULL))) {
1775 u32 eeectl;
1776
1777 if (tp->link_config.active_speed == SPEED_1000)
1778 eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
1779 else
1780 eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
1781
1782 tw32(TG3_CPMU_EEE_CTRL, eeectl);
1783
1784 tg3_phy_cl45_read(tp, 0x7, TG3_CL45_D7_EEERES_STAT, &val);
1785
1786 if (val == TG3_CL45_D7_EEERES_STAT_LP_1000T ||
1787 val == TG3_CL45_D7_EEERES_STAT_LP_100TX)
1788 tp->setlpicnt = 2;
1789 }
1790
1791 if (!tp->setlpicnt) {
1792 val = tr32(TG3_CPMU_EEE_MODE);
1793 tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
1794 }
1795}
1796
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797static int tg3_wait_macro_done(struct tg3 *tp)
1798{
1799 int limit = 100;
1800
1801 while (limit--) {
1802 u32 tmp32;
1803
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001804 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 if ((tmp32 & 0x1000) == 0)
1806 break;
1807 }
1808 }
Roel Kluind4675b52009-02-12 16:33:27 -08001809 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 return -EBUSY;
1811
1812 return 0;
1813}
1814
1815static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
1816{
1817 static const u32 test_pat[4][6] = {
1818 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
1819 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
1820 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
1821 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
1822 };
1823 int chan;
1824
1825 for (chan = 0; chan < 4; chan++) {
1826 int i;
1827
1828 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1829 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001830 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
1832 for (i = 0; i < 6; i++)
1833 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
1834 test_pat[chan][i]);
1835
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001836 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 if (tg3_wait_macro_done(tp)) {
1838 *resetp = 1;
1839 return -EBUSY;
1840 }
1841
1842 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1843 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001844 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 if (tg3_wait_macro_done(tp)) {
1846 *resetp = 1;
1847 return -EBUSY;
1848 }
1849
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001850 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 if (tg3_wait_macro_done(tp)) {
1852 *resetp = 1;
1853 return -EBUSY;
1854 }
1855
1856 for (i = 0; i < 6; i += 2) {
1857 u32 low, high;
1858
1859 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
1860 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
1861 tg3_wait_macro_done(tp)) {
1862 *resetp = 1;
1863 return -EBUSY;
1864 }
1865 low &= 0x7fff;
1866 high &= 0x000f;
1867 if (low != test_pat[chan][i] ||
1868 high != test_pat[chan][i+1]) {
1869 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
1870 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
1871 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
1872
1873 return -EBUSY;
1874 }
1875 }
1876 }
1877
1878 return 0;
1879}
1880
1881static int tg3_phy_reset_chanpat(struct tg3 *tp)
1882{
1883 int chan;
1884
1885 for (chan = 0; chan < 4; chan++) {
1886 int i;
1887
1888 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1889 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001890 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 for (i = 0; i < 6; i++)
1892 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001893 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 if (tg3_wait_macro_done(tp))
1895 return -EBUSY;
1896 }
1897
1898 return 0;
1899}
1900
1901static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
1902{
1903 u32 reg32, phy9_orig;
1904 int retries, do_phy_reset, err;
1905
1906 retries = 10;
1907 do_phy_reset = 1;
1908 do {
1909 if (do_phy_reset) {
1910 err = tg3_bmcr_reset(tp);
1911 if (err)
1912 return err;
1913 do_phy_reset = 0;
1914 }
1915
1916 /* Disable transmitter and interrupt. */
1917 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
1918 continue;
1919
1920 reg32 |= 0x3000;
1921 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
1922
1923 /* Set full-duplex, 1000 mbps. */
1924 tg3_writephy(tp, MII_BMCR,
1925 BMCR_FULLDPLX | TG3_BMCR_SPEED1000);
1926
1927 /* Set to master mode. */
1928 if (tg3_readphy(tp, MII_TG3_CTRL, &phy9_orig))
1929 continue;
1930
1931 tg3_writephy(tp, MII_TG3_CTRL,
1932 (MII_TG3_CTRL_AS_MASTER |
1933 MII_TG3_CTRL_ENABLE_AS_MASTER));
1934
1935 /* Enable SM_DSP_CLOCK and 6dB. */
1936 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
1937
1938 /* Block the PHY control access. */
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00001939 tg3_phydsp_write(tp, 0x8005, 0x0800);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
1941 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
1942 if (!err)
1943 break;
1944 } while (--retries);
1945
1946 err = tg3_phy_reset_chanpat(tp);
1947 if (err)
1948 return err;
1949
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00001950 tg3_phydsp_write(tp, 0x8005, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
1952 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001953 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
1955 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
1956 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
1957 /* Set Extended packet length bit for jumbo frames */
1958 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4400);
Matt Carlson859a5882010-04-05 10:19:28 +00001959 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
1961 }
1962
1963 tg3_writephy(tp, MII_TG3_CTRL, phy9_orig);
1964
1965 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
1966 reg32 &= ~0x3000;
1967 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
1968 } else if (!err)
1969 err = -EBUSY;
1970
1971 return err;
1972}
1973
1974/* This will reset the tigon3 PHY if there is no valid
1975 * link unless the FORCE argument is non-zero.
1976 */
1977static int tg3_phy_reset(struct tg3 *tp)
1978{
Matt Carlsonf833c4c2010-09-15 09:00:01 +00001979 u32 val, cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 int err;
1981
Michael Chan60189dd2006-12-17 17:08:07 -08001982 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08001983 val = tr32(GRC_MISC_CFG);
1984 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
1985 udelay(40);
1986 }
Matt Carlsonf833c4c2010-09-15 09:00:01 +00001987 err = tg3_readphy(tp, MII_BMSR, &val);
1988 err |= tg3_readphy(tp, MII_BMSR, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 if (err != 0)
1990 return -EBUSY;
1991
Michael Chanc8e1e822006-04-29 18:55:17 -07001992 if (netif_running(tp->dev) && netif_carrier_ok(tp->dev)) {
1993 netif_carrier_off(tp->dev);
1994 tg3_link_report(tp);
1995 }
1996
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
1998 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
1999 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
2000 err = tg3_phy_reset_5703_4_5(tp);
2001 if (err)
2002 return err;
2003 goto out;
2004 }
2005
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002006 cpmuctrl = 0;
2007 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
2008 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
2009 cpmuctrl = tr32(TG3_CPMU_CTRL);
2010 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
2011 tw32(TG3_CPMU_CTRL,
2012 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
2013 }
2014
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 err = tg3_bmcr_reset(tp);
2016 if (err)
2017 return err;
2018
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002019 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002020 val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
2021 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002022
2023 tw32(TG3_CPMU_CTRL, cpmuctrl);
2024 }
2025
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002026 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2027 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002028 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2029 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
2030 CPMU_LSPD_1000MB_MACCLK_12_5) {
2031 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2032 udelay(40);
2033 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2034 }
2035 }
2036
Matt Carlsona50d0792010-06-05 17:24:37 +00002037 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2038 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002039 (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
Matt Carlsonecf14102010-01-20 16:58:05 +00002040 return 0;
2041
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002042 tg3_phy_apply_otp(tp);
2043
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002044 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -08002045 tg3_phy_toggle_apd(tp, true);
2046 else
2047 tg3_phy_toggle_apd(tp, false);
2048
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049out:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002050 if (tp->phy_flags & TG3_PHYFLG_ADC_BUG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002052 tg3_phydsp_write(tp, 0x201f, 0x2aaa);
2053 tg3_phydsp_write(tp, 0x000a, 0x0323);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
2055 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002056 if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002057 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2058 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002060 if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002062 tg3_phydsp_write(tp, 0x000a, 0x310b);
2063 tg3_phydsp_write(tp, 0x201f, 0x9506);
2064 tg3_phydsp_write(tp, 0x401f, 0x14e2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002066 } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
Michael Chanc424cb22006-04-29 18:56:34 -07002067 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
2068 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002069 if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
Michael Chanc1d2a192007-01-08 19:57:20 -08002070 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2071 tg3_writephy(tp, MII_TG3_TEST1,
2072 MII_TG3_TEST1_TRIM_EN | 0x4);
2073 } else
2074 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
Michael Chanc424cb22006-04-29 18:56:34 -07002075 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
2076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 /* Set Extended packet length bit (bit 14) on all chips that */
2078 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002079 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 /* Cannot do read-modify-write on 5401 */
2081 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4c20);
Matt Carlson8f666b02009-08-28 13:58:24 +00002082 } else if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 /* Set bit 14 with read-modify-write to preserve other bits */
2084 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0007) &&
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002085 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &val))
2086 tg3_writephy(tp, MII_TG3_AUX_CTRL, val | 0x4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 }
2088
2089 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2090 * jumbo frames transmission.
2091 */
Matt Carlson8f666b02009-08-28 13:58:24 +00002092 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002093 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002094 tg3_writephy(tp, MII_TG3_EXT_CTRL,
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002095 val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 }
2097
Michael Chan715116a2006-09-27 16:09:25 -07002098 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002099 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002100 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002101 }
2102
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002103 tg3_phy_toggle_automdix(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 tg3_phy_set_wirespeed(tp);
2105 return 0;
2106}
2107
2108static void tg3_frob_aux_power(struct tg3 *tp)
2109{
2110 struct tg3 *tp_peer = tp;
2111
Matt Carlson334355a2010-01-20 16:58:10 +00002112 /* The GPIOs do something completely different on 57765. */
2113 if ((tp->tg3_flags2 & TG3_FLG2_IS_NIC) == 0 ||
Matt Carlsona50d0792010-06-05 17:24:37 +00002114 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlson334355a2010-01-20 16:58:10 +00002115 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 return;
2117
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00002118 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2119 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
2120 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002121 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002123 dev_peer = pci_get_drvdata(tp->pdev_peer);
Michael Chanbc1c7562006-03-20 17:48:03 -08002124 /* remove_one() may have been run on the peer. */
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002125 if (!dev_peer)
Michael Chanbc1c7562006-03-20 17:48:03 -08002126 tp_peer = tp;
2127 else
2128 tp_peer = netdev_priv(dev_peer);
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
2131 if ((tp->tg3_flags & TG3_FLAG_WOL_ENABLE) != 0 ||
Michael Chan6921d202005-12-13 21:15:53 -08002132 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0 ||
2133 (tp_peer->tg3_flags & TG3_FLAG_WOL_ENABLE) != 0 ||
2134 (tp_peer->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2136 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Michael Chanb401e9e2005-12-19 16:27:04 -08002137 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2138 (GRC_LCLCTRL_GPIO_OE0 |
2139 GRC_LCLCTRL_GPIO_OE1 |
2140 GRC_LCLCTRL_GPIO_OE2 |
2141 GRC_LCLCTRL_GPIO_OUTPUT0 |
2142 GRC_LCLCTRL_GPIO_OUTPUT1),
2143 100);
Matt Carlson8d519ab2009-04-20 06:58:01 +00002144 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2145 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -07002146 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2147 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2148 GRC_LCLCTRL_GPIO_OE1 |
2149 GRC_LCLCTRL_GPIO_OE2 |
2150 GRC_LCLCTRL_GPIO_OUTPUT0 |
2151 GRC_LCLCTRL_GPIO_OUTPUT1 |
2152 tp->grc_local_ctrl;
2153 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2154
2155 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2156 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2157
2158 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2159 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 } else {
2161 u32 no_gpio2;
Michael Chandc56b7d2005-12-19 16:26:28 -08002162 u32 grc_local_ctrl = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
2164 if (tp_peer != tp &&
2165 (tp_peer->tg3_flags & TG3_FLAG_INIT_COMPLETE) != 0)
2166 return;
2167
Michael Chandc56b7d2005-12-19 16:26:28 -08002168 /* Workaround to prevent overdrawing Amps. */
2169 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2170 ASIC_REV_5714) {
2171 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chanb401e9e2005-12-19 16:27:04 -08002172 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2173 grc_local_ctrl, 100);
Michael Chandc56b7d2005-12-19 16:26:28 -08002174 }
2175
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 /* On 5753 and variants, GPIO2 cannot be used. */
2177 no_gpio2 = tp->nic_sram_data_cfg &
2178 NIC_SRAM_DATA_CFG_NO_GPIO2;
2179
Michael Chandc56b7d2005-12-19 16:26:28 -08002180 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 GRC_LCLCTRL_GPIO_OE1 |
2182 GRC_LCLCTRL_GPIO_OE2 |
2183 GRC_LCLCTRL_GPIO_OUTPUT1 |
2184 GRC_LCLCTRL_GPIO_OUTPUT2;
2185 if (no_gpio2) {
2186 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2187 GRC_LCLCTRL_GPIO_OUTPUT2);
2188 }
Michael Chanb401e9e2005-12-19 16:27:04 -08002189 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2190 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
2192 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2193
Michael Chanb401e9e2005-12-19 16:27:04 -08002194 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2195 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196
2197 if (!no_gpio2) {
2198 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chanb401e9e2005-12-19 16:27:04 -08002199 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2200 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 }
2202 }
2203 } else {
2204 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
2205 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
2206 if (tp_peer != tp &&
2207 (tp_peer->tg3_flags & TG3_FLAG_INIT_COMPLETE) != 0)
2208 return;
2209
Michael Chanb401e9e2005-12-19 16:27:04 -08002210 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2211 (GRC_LCLCTRL_GPIO_OE1 |
2212 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
Michael Chanb401e9e2005-12-19 16:27:04 -08002214 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2215 GRC_LCLCTRL_GPIO_OE1, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216
Michael Chanb401e9e2005-12-19 16:27:04 -08002217 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2218 (GRC_LCLCTRL_GPIO_OE1 |
2219 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 }
2221 }
2222}
2223
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002224static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2225{
2226 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2227 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00002228 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002229 if (speed != SPEED_10)
2230 return 1;
2231 } else if (speed == SPEED_10)
2232 return 1;
2233
2234 return 0;
2235}
2236
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237static int tg3_setup_phy(struct tg3 *, int);
2238
2239#define RESET_KIND_SHUTDOWN 0
2240#define RESET_KIND_INIT 1
2241#define RESET_KIND_SUSPEND 2
2242
2243static void tg3_write_sig_post_reset(struct tg3 *, int);
2244static int tg3_halt_cpu(struct tg3 *, u32);
2245
Matt Carlson0a459aa2008-11-03 16:54:15 -08002246static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002247{
Matt Carlsonce057f02007-11-12 21:08:03 -08002248 u32 val;
2249
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002250 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Michael Chan51297242007-02-13 12:17:57 -08002251 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2252 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2253 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2254
2255 sg_dig_ctrl |=
2256 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2257 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2258 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2259 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002260 return;
Michael Chan51297242007-02-13 12:17:57 -08002261 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002262
Michael Chan60189dd2006-12-17 17:08:07 -08002263 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002264 tg3_bmcr_reset(tp);
2265 val = tr32(GRC_MISC_CFG);
2266 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2267 udelay(40);
2268 return;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002269 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson0e5f7842009-11-02 14:26:38 +00002270 u32 phytest;
2271 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2272 u32 phy;
2273
2274 tg3_writephy(tp, MII_ADVERTISE, 0);
2275 tg3_writephy(tp, MII_BMCR,
2276 BMCR_ANENABLE | BMCR_ANRESTART);
2277
2278 tg3_writephy(tp, MII_TG3_FET_TEST,
2279 phytest | MII_TG3_FET_SHADOW_EN);
2280 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
2281 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
2282 tg3_writephy(tp,
2283 MII_TG3_FET_SHDW_AUXMODE4,
2284 phy);
2285 }
2286 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2287 }
2288 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002289 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07002290 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2291 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002292
2293 tg3_writephy(tp, MII_TG3_AUX_CTRL,
2294 MII_TG3_AUXCTL_SHDWSEL_PWRCTL |
2295 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2296 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2297 MII_TG3_AUXCTL_PCTL_VREG_11V);
Michael Chan715116a2006-09-27 16:09:25 -07002298 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002299
Michael Chan15c3b692006-03-22 01:06:52 -08002300 /* The PHY should not be powered down on some chips because
2301 * of bugs.
2302 */
2303 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2304 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2305 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002306 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Michael Chan15c3b692006-03-22 01:06:52 -08002307 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08002308
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002309 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2310 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002311 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2312 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2313 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2314 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2315 }
2316
Michael Chan15c3b692006-03-22 01:06:52 -08002317 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2318}
2319
Matt Carlson3f007892008-11-03 16:51:36 -08002320/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002321static int tg3_nvram_lock(struct tg3 *tp)
2322{
2323 if (tp->tg3_flags & TG3_FLAG_NVRAM) {
2324 int i;
2325
2326 if (tp->nvram_lock_cnt == 0) {
2327 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2328 for (i = 0; i < 8000; i++) {
2329 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2330 break;
2331 udelay(20);
2332 }
2333 if (i == 8000) {
2334 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2335 return -ENODEV;
2336 }
2337 }
2338 tp->nvram_lock_cnt++;
2339 }
2340 return 0;
2341}
2342
2343/* tp->lock is held. */
2344static void tg3_nvram_unlock(struct tg3 *tp)
2345{
2346 if (tp->tg3_flags & TG3_FLAG_NVRAM) {
2347 if (tp->nvram_lock_cnt > 0)
2348 tp->nvram_lock_cnt--;
2349 if (tp->nvram_lock_cnt == 0)
2350 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
2351 }
2352}
2353
2354/* tp->lock is held. */
2355static void tg3_enable_nvram_access(struct tg3 *tp)
2356{
2357 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Matt Carlsonf66a29b2009-11-13 13:03:36 +00002358 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002359 u32 nvaccess = tr32(NVRAM_ACCESS);
2360
2361 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
2362 }
2363}
2364
2365/* tp->lock is held. */
2366static void tg3_disable_nvram_access(struct tg3 *tp)
2367{
2368 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Matt Carlsonf66a29b2009-11-13 13:03:36 +00002369 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002370 u32 nvaccess = tr32(NVRAM_ACCESS);
2371
2372 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
2373 }
2374}
2375
2376static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
2377 u32 offset, u32 *val)
2378{
2379 u32 tmp;
2380 int i;
2381
2382 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
2383 return -EINVAL;
2384
2385 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
2386 EEPROM_ADDR_DEVID_MASK |
2387 EEPROM_ADDR_READ);
2388 tw32(GRC_EEPROM_ADDR,
2389 tmp |
2390 (0 << EEPROM_ADDR_DEVID_SHIFT) |
2391 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
2392 EEPROM_ADDR_ADDR_MASK) |
2393 EEPROM_ADDR_READ | EEPROM_ADDR_START);
2394
2395 for (i = 0; i < 1000; i++) {
2396 tmp = tr32(GRC_EEPROM_ADDR);
2397
2398 if (tmp & EEPROM_ADDR_COMPLETE)
2399 break;
2400 msleep(1);
2401 }
2402 if (!(tmp & EEPROM_ADDR_COMPLETE))
2403 return -EBUSY;
2404
Matt Carlson62cedd12009-04-20 14:52:29 -07002405 tmp = tr32(GRC_EEPROM_DATA);
2406
2407 /*
2408 * The data will always be opposite the native endian
2409 * format. Perform a blind byteswap to compensate.
2410 */
2411 *val = swab32(tmp);
2412
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002413 return 0;
2414}
2415
2416#define NVRAM_CMD_TIMEOUT 10000
2417
2418static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
2419{
2420 int i;
2421
2422 tw32(NVRAM_CMD, nvram_cmd);
2423 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
2424 udelay(10);
2425 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
2426 udelay(10);
2427 break;
2428 }
2429 }
2430
2431 if (i == NVRAM_CMD_TIMEOUT)
2432 return -EBUSY;
2433
2434 return 0;
2435}
2436
2437static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
2438{
2439 if ((tp->tg3_flags & TG3_FLAG_NVRAM) &&
2440 (tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) &&
2441 (tp->tg3_flags2 & TG3_FLG2_FLASH) &&
2442 !(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM_ADDR_TRANS) &&
2443 (tp->nvram_jedecnum == JEDEC_ATMEL))
2444
2445 addr = ((addr / tp->nvram_pagesize) <<
2446 ATMEL_AT45DB0X1B_PAGE_POS) +
2447 (addr % tp->nvram_pagesize);
2448
2449 return addr;
2450}
2451
2452static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
2453{
2454 if ((tp->tg3_flags & TG3_FLAG_NVRAM) &&
2455 (tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) &&
2456 (tp->tg3_flags2 & TG3_FLG2_FLASH) &&
2457 !(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM_ADDR_TRANS) &&
2458 (tp->nvram_jedecnum == JEDEC_ATMEL))
2459
2460 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
2461 tp->nvram_pagesize) +
2462 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
2463
2464 return addr;
2465}
2466
Matt Carlsone4f34112009-02-25 14:25:00 +00002467/* NOTE: Data read in from NVRAM is byteswapped according to
2468 * the byteswapping settings for all other register accesses.
2469 * tg3 devices are BE devices, so on a BE machine, the data
2470 * returned will be exactly as it is seen in NVRAM. On a LE
2471 * machine, the 32-bit value will be byteswapped.
2472 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002473static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
2474{
2475 int ret;
2476
2477 if (!(tp->tg3_flags & TG3_FLAG_NVRAM))
2478 return tg3_nvram_read_using_eeprom(tp, offset, val);
2479
2480 offset = tg3_nvram_phys_addr(tp, offset);
2481
2482 if (offset > NVRAM_ADDR_MSK)
2483 return -EINVAL;
2484
2485 ret = tg3_nvram_lock(tp);
2486 if (ret)
2487 return ret;
2488
2489 tg3_enable_nvram_access(tp);
2490
2491 tw32(NVRAM_ADDR, offset);
2492 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
2493 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
2494
2495 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00002496 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002497
2498 tg3_disable_nvram_access(tp);
2499
2500 tg3_nvram_unlock(tp);
2501
2502 return ret;
2503}
2504
Matt Carlsona9dc5292009-02-25 14:25:30 +00002505/* Ensures NVRAM data is in bytestream format. */
2506static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002507{
2508 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00002509 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002510 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00002511 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002512 return res;
2513}
2514
2515/* tp->lock is held. */
Matt Carlson3f007892008-11-03 16:51:36 -08002516static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
2517{
2518 u32 addr_high, addr_low;
2519 int i;
2520
2521 addr_high = ((tp->dev->dev_addr[0] << 8) |
2522 tp->dev->dev_addr[1]);
2523 addr_low = ((tp->dev->dev_addr[2] << 24) |
2524 (tp->dev->dev_addr[3] << 16) |
2525 (tp->dev->dev_addr[4] << 8) |
2526 (tp->dev->dev_addr[5] << 0));
2527 for (i = 0; i < 4; i++) {
2528 if (i == 1 && skip_mac_1)
2529 continue;
2530 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
2531 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
2532 }
2533
2534 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2535 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2536 for (i = 0; i < 12; i++) {
2537 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
2538 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
2539 }
2540 }
2541
2542 addr_high = (tp->dev->dev_addr[0] +
2543 tp->dev->dev_addr[1] +
2544 tp->dev->dev_addr[2] +
2545 tp->dev->dev_addr[3] +
2546 tp->dev->dev_addr[4] +
2547 tp->dev->dev_addr[5]) &
2548 TX_BACKOFF_SEED_MASK;
2549 tw32(MAC_TX_BACKOFF_SEED, addr_high);
2550}
2551
Michael Chanbc1c7562006-03-20 17:48:03 -08002552static int tg3_set_power_state(struct tg3 *tp, pci_power_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553{
2554 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002555 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556
2557 /* Make sure register accesses (indirect or otherwise)
2558 * will function correctly.
2559 */
2560 pci_write_config_dword(tp->pdev,
2561 TG3PCI_MISC_HOST_CTRL,
2562 tp->misc_host_ctrl);
2563
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 switch (state) {
Michael Chanbc1c7562006-03-20 17:48:03 -08002565 case PCI_D0:
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07002566 pci_enable_wake(tp->pdev, state, false);
2567 pci_set_power_state(tp->pdev, PCI_D0);
Michael Chan8c6bda12005-04-21 17:09:08 -07002568
Michael Chan9d26e212006-12-07 00:21:14 -08002569 /* Switch out of Vaux if it is a NIC */
2570 if (tp->tg3_flags2 & TG3_FLG2_IS_NIC)
Michael Chanb401e9e2005-12-19 16:27:04 -08002571 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572
2573 return 0;
2574
Michael Chanbc1c7562006-03-20 17:48:03 -08002575 case PCI_D1:
Michael Chanbc1c7562006-03-20 17:48:03 -08002576 case PCI_D2:
Michael Chanbc1c7562006-03-20 17:48:03 -08002577 case PCI_D3hot:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 break;
2579
2580 default:
Joe Perches05dbe002010-02-17 19:44:19 +00002581 netdev_err(tp->dev, "Invalid power state (D%d) requested\n",
2582 state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002584 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002585
2586 /* Restore the CLKREQ setting. */
2587 if (tp->tg3_flags3 & TG3_FLG3_CLKREQ_BUG) {
2588 u16 lnkctl;
2589
2590 pci_read_config_word(tp->pdev,
2591 tp->pcie_cap + PCI_EXP_LNKCTL,
2592 &lnkctl);
2593 lnkctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
2594 pci_write_config_word(tp->pdev,
2595 tp->pcie_cap + PCI_EXP_LNKCTL,
2596 lnkctl);
2597 }
2598
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
2600 tw32(TG3PCI_MISC_HOST_CTRL,
2601 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
2602
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002603 device_should_wake = pci_pme_capable(tp->pdev, state) &&
2604 device_may_wakeup(&tp->pdev->dev) &&
2605 (tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
2606
Matt Carlsondd477002008-05-25 23:45:58 -07002607 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08002608 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002609 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson80096062010-08-02 11:26:06 +00002610 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002611 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002612 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002613
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002614 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002615
Matt Carlson80096062010-08-02 11:26:06 +00002616 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002617
2618 tp->link_config.orig_speed = phydev->speed;
2619 tp->link_config.orig_duplex = phydev->duplex;
2620 tp->link_config.orig_autoneg = phydev->autoneg;
2621 tp->link_config.orig_advertising = phydev->advertising;
2622
2623 advertising = ADVERTISED_TP |
2624 ADVERTISED_Pause |
2625 ADVERTISED_Autoneg |
2626 ADVERTISED_10baseT_Half;
2627
2628 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002629 device_should_wake) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002630 if (tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB)
2631 advertising |=
2632 ADVERTISED_100baseT_Half |
2633 ADVERTISED_100baseT_Full |
2634 ADVERTISED_10baseT_Full;
2635 else
2636 advertising |= ADVERTISED_10baseT_Full;
2637 }
2638
2639 phydev->advertising = advertising;
2640
2641 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002642
2643 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00002644 if (phyid != PHY_ID_BCMAC131) {
2645 phyid &= PHY_BCM_OUI_MASK;
2646 if (phyid == PHY_BCM_OUI_1 ||
2647 phyid == PHY_BCM_OUI_2 ||
2648 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08002649 do_low_power = true;
2650 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002651 }
Matt Carlsondd477002008-05-25 23:45:58 -07002652 } else {
Matt Carlson20232762008-12-21 20:18:56 -08002653 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002654
Matt Carlson80096062010-08-02 11:26:06 +00002655 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
2656 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07002657 tp->link_config.orig_speed = tp->link_config.speed;
2658 tp->link_config.orig_duplex = tp->link_config.duplex;
2659 tp->link_config.orig_autoneg = tp->link_config.autoneg;
2660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002662 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Matt Carlsondd477002008-05-25 23:45:58 -07002663 tp->link_config.speed = SPEED_10;
2664 tp->link_config.duplex = DUPLEX_HALF;
2665 tp->link_config.autoneg = AUTONEG_ENABLE;
2666 tg3_setup_phy(tp, 0);
2667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 }
2669
Michael Chanb5d37722006-09-27 16:06:21 -07002670 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
2671 u32 val;
2672
2673 val = tr32(GRC_VCPU_EXT_CTRL);
2674 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
2675 } else if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08002676 int i;
2677 u32 val;
2678
2679 for (i = 0; i < 200; i++) {
2680 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
2681 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
2682 break;
2683 msleep(1);
2684 }
2685 }
Gary Zambranoa85feb82007-05-05 11:52:19 -07002686 if (tp->tg3_flags & TG3_FLAG_WOL_CAP)
2687 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
2688 WOL_DRV_STATE_SHUTDOWN |
2689 WOL_DRV_WOL |
2690 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08002691
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002692 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 u32 mac_mode;
2694
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002695 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08002696 if (do_low_power) {
Matt Carlsondd477002008-05-25 23:45:58 -07002697 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x5a);
2698 udelay(40);
2699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002701 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07002702 mac_mode = MAC_MODE_PORT_MODE_GMII;
2703 else
2704 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002706 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
2707 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2708 ASIC_REV_5700) {
2709 u32 speed = (tp->tg3_flags &
2710 TG3_FLAG_WOL_SPEED_100MB) ?
2711 SPEED_100 : SPEED_10;
2712 if (tg3_5700_link_polarity(tp, speed))
2713 mac_mode |= MAC_MODE_LINK_POLARITY;
2714 else
2715 mac_mode &= ~MAC_MODE_LINK_POLARITY;
2716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 } else {
2718 mac_mode = MAC_MODE_PORT_MODE_TBI;
2719 }
2720
John W. Linvillecbf46852005-04-21 17:01:29 -07002721 if (!(tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 tw32(MAC_LED_CTRL, tp->led_ctrl);
2723
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002724 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
2725 if (((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
2726 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) &&
2727 ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
2728 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)))
2729 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730
Matt Carlsond2394e6b2010-11-24 08:31:47 +00002731 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
2732 mac_mode |= MAC_MODE_APE_TX_EN |
2733 MAC_MODE_APE_RX_EN |
2734 MAC_MODE_TDE_ENABLE;
Matt Carlson3bda1252008-08-15 14:08:22 -07002735
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 tw32_f(MAC_MODE, mac_mode);
2737 udelay(100);
2738
2739 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
2740 udelay(10);
2741 }
2742
2743 if (!(tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB) &&
2744 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2745 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
2746 u32 base_val;
2747
2748 base_val = tp->pci_clock_ctrl;
2749 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
2750 CLOCK_CTRL_TXCLK_DISABLE);
2751
Michael Chanb401e9e2005-12-19 16:27:04 -08002752 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
2753 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Michael Chand7b0a852007-02-13 12:17:38 -08002754 } else if ((tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
Matt Carlson795d01c2007-10-07 23:28:17 -07002755 (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
Michael Chand7b0a852007-02-13 12:17:38 -08002756 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)) {
Michael Chan4cf78e42005-07-25 12:29:19 -07002757 /* do nothing */
Michael Chan85e94ce2005-04-21 17:05:28 -07002758 } else if (!((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF))) {
2760 u32 newbits1, newbits2;
2761
2762 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2763 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2764 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
2765 CLOCK_CTRL_TXCLK_DISABLE |
2766 CLOCK_CTRL_ALTCLK);
2767 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2768 } else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
2769 newbits1 = CLOCK_CTRL_625_CORE;
2770 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
2771 } else {
2772 newbits1 = CLOCK_CTRL_ALTCLK;
2773 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2774 }
2775
Michael Chanb401e9e2005-12-19 16:27:04 -08002776 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
2777 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778
Michael Chanb401e9e2005-12-19 16:27:04 -08002779 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
2780 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781
2782 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
2783 u32 newbits3;
2784
2785 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2786 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2787 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
2788 CLOCK_CTRL_TXCLK_DISABLE |
2789 CLOCK_CTRL_44MHZ_CORE);
2790 } else {
2791 newbits3 = CLOCK_CTRL_44MHZ_CORE;
2792 }
2793
Michael Chanb401e9e2005-12-19 16:27:04 -08002794 tw32_wait_f(TG3PCI_CLOCK_CTRL,
2795 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 }
2797 }
2798
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002799 if (!(device_should_wake) &&
Matt Carlson22435842008-11-21 17:21:13 -08002800 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08002801 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08002802
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 tg3_frob_aux_power(tp);
2804
2805 /* Workaround for unstable PLL clock */
2806 if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
2807 (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
2808 u32 val = tr32(0x7d00);
2809
2810 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
2811 tw32(0x7d00, val);
Michael Chan6921d202005-12-13 21:15:53 -08002812 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08002813 int err;
2814
2815 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08002817 if (!err)
2818 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08002819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 }
2821
Michael Chanbbadf502006-04-06 21:46:34 -07002822 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
2823
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002824 if (device_should_wake)
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07002825 pci_enable_wake(tp->pdev, state, true);
2826
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 /* Finally, set the new power state. */
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07002828 pci_set_power_state(tp->pdev, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 return 0;
2831}
2832
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
2834{
2835 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
2836 case MII_TG3_AUX_STAT_10HALF:
2837 *speed = SPEED_10;
2838 *duplex = DUPLEX_HALF;
2839 break;
2840
2841 case MII_TG3_AUX_STAT_10FULL:
2842 *speed = SPEED_10;
2843 *duplex = DUPLEX_FULL;
2844 break;
2845
2846 case MII_TG3_AUX_STAT_100HALF:
2847 *speed = SPEED_100;
2848 *duplex = DUPLEX_HALF;
2849 break;
2850
2851 case MII_TG3_AUX_STAT_100FULL:
2852 *speed = SPEED_100;
2853 *duplex = DUPLEX_FULL;
2854 break;
2855
2856 case MII_TG3_AUX_STAT_1000HALF:
2857 *speed = SPEED_1000;
2858 *duplex = DUPLEX_HALF;
2859 break;
2860
2861 case MII_TG3_AUX_STAT_1000FULL:
2862 *speed = SPEED_1000;
2863 *duplex = DUPLEX_FULL;
2864 break;
2865
2866 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002867 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07002868 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
2869 SPEED_10;
2870 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
2871 DUPLEX_HALF;
2872 break;
2873 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 *speed = SPEED_INVALID;
2875 *duplex = DUPLEX_INVALID;
2876 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002877 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878}
2879
2880static void tg3_phy_copper_begin(struct tg3 *tp)
2881{
2882 u32 new_adv;
2883 int i;
2884
Matt Carlson80096062010-08-02 11:26:06 +00002885 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 /* Entering low power mode. Disable gigabit and
2887 * 100baseT advertisements.
2888 */
2889 tg3_writephy(tp, MII_TG3_CTRL, 0);
2890
2891 new_adv = (ADVERTISE_10HALF | ADVERTISE_10FULL |
2892 ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
2893 if (tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB)
2894 new_adv |= (ADVERTISE_100HALF | ADVERTISE_100FULL);
2895
2896 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2897 } else if (tp->link_config.speed == SPEED_INVALID) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002898 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 tp->link_config.advertising &=
2900 ~(ADVERTISED_1000baseT_Half |
2901 ADVERTISED_1000baseT_Full);
2902
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002903 new_adv = ADVERTISE_CSMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 if (tp->link_config.advertising & ADVERTISED_10baseT_Half)
2905 new_adv |= ADVERTISE_10HALF;
2906 if (tp->link_config.advertising & ADVERTISED_10baseT_Full)
2907 new_adv |= ADVERTISE_10FULL;
2908 if (tp->link_config.advertising & ADVERTISED_100baseT_Half)
2909 new_adv |= ADVERTISE_100HALF;
2910 if (tp->link_config.advertising & ADVERTISED_100baseT_Full)
2911 new_adv |= ADVERTISE_100FULL;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002912
2913 new_adv |= tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2914
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2916
2917 if (tp->link_config.advertising &
2918 (ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full)) {
2919 new_adv = 0;
2920 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
2921 new_adv |= MII_TG3_CTRL_ADV_1000_HALF;
2922 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
2923 new_adv |= MII_TG3_CTRL_ADV_1000_FULL;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002924 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2926 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0))
2927 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2928 MII_TG3_CTRL_ENABLE_AS_MASTER);
2929 tg3_writephy(tp, MII_TG3_CTRL, new_adv);
2930 } else {
2931 tg3_writephy(tp, MII_TG3_CTRL, 0);
2932 }
2933 } else {
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002934 new_adv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2935 new_adv |= ADVERTISE_CSMA;
2936
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 /* Asking for a specific link mode. */
2938 if (tp->link_config.speed == SPEED_1000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2940
2941 if (tp->link_config.duplex == DUPLEX_FULL)
2942 new_adv = MII_TG3_CTRL_ADV_1000_FULL;
2943 else
2944 new_adv = MII_TG3_CTRL_ADV_1000_HALF;
2945 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2946 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
2947 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2948 MII_TG3_CTRL_ENABLE_AS_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 if (tp->link_config.speed == SPEED_100) {
2951 if (tp->link_config.duplex == DUPLEX_FULL)
2952 new_adv |= ADVERTISE_100FULL;
2953 else
2954 new_adv |= ADVERTISE_100HALF;
2955 } else {
2956 if (tp->link_config.duplex == DUPLEX_FULL)
2957 new_adv |= ADVERTISE_10FULL;
2958 else
2959 new_adv |= ADVERTISE_10HALF;
2960 }
2961 tg3_writephy(tp, MII_ADVERTISE, new_adv);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002962
2963 new_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002965
2966 tg3_writephy(tp, MII_TG3_CTRL, new_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 }
2968
Matt Carlson52b02d02010-10-14 10:37:41 +00002969 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
2970 u32 val = 0;
2971
2972 tw32(TG3_CPMU_EEE_MODE,
2973 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
2974
2975 /* Enable SM_DSP clock and tx 6dB coding. */
2976 val = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
2977 MII_TG3_AUXCTL_ACTL_SMDSP_ENA |
2978 MII_TG3_AUXCTL_ACTL_TX_6DB;
2979 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
2980
2981 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2982 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) &&
2983 !tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
2984 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2,
2985 val | MII_TG3_DSP_CH34TP2_HIBW01);
2986
2987 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
2988 /* Advertise 100-BaseTX EEE ability */
2989 if (tp->link_config.advertising &
2990 (ADVERTISED_100baseT_Half |
2991 ADVERTISED_100baseT_Full))
2992 val |= TG3_CL45_D7_EEEADV_CAP_100TX;
2993 /* Advertise 1000-BaseT EEE ability */
2994 if (tp->link_config.advertising &
2995 (ADVERTISED_1000baseT_Half |
2996 ADVERTISED_1000baseT_Full))
2997 val |= TG3_CL45_D7_EEEADV_CAP_1000T;
2998 }
2999 tg3_phy_cl45_write(tp, 0x7, TG3_CL45_D7_EEEADV_CAP, val);
3000
3001 /* Turn off SM_DSP clock. */
3002 val = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
3003 MII_TG3_AUXCTL_ACTL_TX_6DB;
3004 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
3005 }
3006
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 if (tp->link_config.autoneg == AUTONEG_DISABLE &&
3008 tp->link_config.speed != SPEED_INVALID) {
3009 u32 bmcr, orig_bmcr;
3010
3011 tp->link_config.active_speed = tp->link_config.speed;
3012 tp->link_config.active_duplex = tp->link_config.duplex;
3013
3014 bmcr = 0;
3015 switch (tp->link_config.speed) {
3016 default:
3017 case SPEED_10:
3018 break;
3019
3020 case SPEED_100:
3021 bmcr |= BMCR_SPEED100;
3022 break;
3023
3024 case SPEED_1000:
3025 bmcr |= TG3_BMCR_SPEED1000;
3026 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003027 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028
3029 if (tp->link_config.duplex == DUPLEX_FULL)
3030 bmcr |= BMCR_FULLDPLX;
3031
3032 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
3033 (bmcr != orig_bmcr)) {
3034 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
3035 for (i = 0; i < 1500; i++) {
3036 u32 tmp;
3037
3038 udelay(10);
3039 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
3040 tg3_readphy(tp, MII_BMSR, &tmp))
3041 continue;
3042 if (!(tmp & BMSR_LSTATUS)) {
3043 udelay(40);
3044 break;
3045 }
3046 }
3047 tg3_writephy(tp, MII_BMCR, bmcr);
3048 udelay(40);
3049 }
3050 } else {
3051 tg3_writephy(tp, MII_BMCR,
3052 BMCR_ANENABLE | BMCR_ANRESTART);
3053 }
3054}
3055
3056static int tg3_init_5401phy_dsp(struct tg3 *tp)
3057{
3058 int err;
3059
3060 /* Turn off tap power management. */
3061 /* Set Extended packet length bit */
3062 err = tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4c20);
3063
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00003064 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
3065 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
3066 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
3067 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
3068 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069
3070 udelay(40);
3071
3072 return err;
3073}
3074
Michael Chan3600d912006-12-07 00:21:48 -08003075static int tg3_copper_is_advertising_all(struct tg3 *tp, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076{
Michael Chan3600d912006-12-07 00:21:48 -08003077 u32 adv_reg, all_mask = 0;
3078
3079 if (mask & ADVERTISED_10baseT_Half)
3080 all_mask |= ADVERTISE_10HALF;
3081 if (mask & ADVERTISED_10baseT_Full)
3082 all_mask |= ADVERTISE_10FULL;
3083 if (mask & ADVERTISED_100baseT_Half)
3084 all_mask |= ADVERTISE_100HALF;
3085 if (mask & ADVERTISED_100baseT_Full)
3086 all_mask |= ADVERTISE_100FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087
3088 if (tg3_readphy(tp, MII_ADVERTISE, &adv_reg))
3089 return 0;
3090
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 if ((adv_reg & all_mask) != all_mask)
3092 return 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003093 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 u32 tg3_ctrl;
3095
Michael Chan3600d912006-12-07 00:21:48 -08003096 all_mask = 0;
3097 if (mask & ADVERTISED_1000baseT_Half)
3098 all_mask |= ADVERTISE_1000HALF;
3099 if (mask & ADVERTISED_1000baseT_Full)
3100 all_mask |= ADVERTISE_1000FULL;
3101
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 if (tg3_readphy(tp, MII_TG3_CTRL, &tg3_ctrl))
3103 return 0;
3104
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 if ((tg3_ctrl & all_mask) != all_mask)
3106 return 0;
3107 }
3108 return 1;
3109}
3110
Matt Carlsonef167e22007-12-20 20:10:01 -08003111static int tg3_adv_1000T_flowctrl_ok(struct tg3 *tp, u32 *lcladv, u32 *rmtadv)
3112{
3113 u32 curadv, reqadv;
3114
3115 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
3116 return 1;
3117
3118 curadv = *lcladv & (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
3119 reqadv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
3120
3121 if (tp->link_config.active_duplex == DUPLEX_FULL) {
3122 if (curadv != reqadv)
3123 return 0;
3124
3125 if (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG)
3126 tg3_readphy(tp, MII_LPA, rmtadv);
3127 } else {
3128 /* Reprogram the advertisement register, even if it
3129 * does not affect the current link. If the link
3130 * gets renegotiated in the future, we can save an
3131 * additional renegotiation cycle by advertising
3132 * it correctly in the first place.
3133 */
3134 if (curadv != reqadv) {
3135 *lcladv &= ~(ADVERTISE_PAUSE_CAP |
3136 ADVERTISE_PAUSE_ASYM);
3137 tg3_writephy(tp, MII_ADVERTISE, *lcladv | reqadv);
3138 }
3139 }
3140
3141 return 1;
3142}
3143
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
3145{
3146 int current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003147 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08003148 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 u16 current_speed;
3150 u8 current_duplex;
3151 int i, err;
3152
3153 tw32(MAC_EVENT, 0);
3154
3155 tw32_f(MAC_STATUS,
3156 (MAC_STATUS_SYNC_CHANGED |
3157 MAC_STATUS_CFG_CHANGED |
3158 MAC_STATUS_MI_COMPLETION |
3159 MAC_STATUS_LNKSTATE_CHANGED));
3160 udelay(40);
3161
Matt Carlson8ef21422008-05-02 16:47:53 -07003162 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
3163 tw32_f(MAC_MI_MODE,
3164 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
3165 udelay(80);
3166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167
3168 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x02);
3169
3170 /* Some third-party PHYs need to be reset on link going
3171 * down.
3172 */
3173 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3174 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
3175 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
3176 netif_carrier_ok(tp->dev)) {
3177 tg3_readphy(tp, MII_BMSR, &bmsr);
3178 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3179 !(bmsr & BMSR_LSTATUS))
3180 force_reset = 1;
3181 }
3182 if (force_reset)
3183 tg3_phy_reset(tp);
3184
Matt Carlson79eb6902010-02-17 15:17:03 +00003185 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 tg3_readphy(tp, MII_BMSR, &bmsr);
3187 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
3188 !(tp->tg3_flags & TG3_FLAG_INIT_COMPLETE))
3189 bmsr = 0;
3190
3191 if (!(bmsr & BMSR_LSTATUS)) {
3192 err = tg3_init_5401phy_dsp(tp);
3193 if (err)
3194 return err;
3195
3196 tg3_readphy(tp, MII_BMSR, &bmsr);
3197 for (i = 0; i < 1000; i++) {
3198 udelay(10);
3199 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3200 (bmsr & BMSR_LSTATUS)) {
3201 udelay(40);
3202 break;
3203 }
3204 }
3205
Matt Carlson79eb6902010-02-17 15:17:03 +00003206 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
3207 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 !(bmsr & BMSR_LSTATUS) &&
3209 tp->link_config.active_speed == SPEED_1000) {
3210 err = tg3_phy_reset(tp);
3211 if (!err)
3212 err = tg3_init_5401phy_dsp(tp);
3213 if (err)
3214 return err;
3215 }
3216 }
3217 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3218 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
3219 /* 5701 {A0,B0} CRC bug workaround */
3220 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00003221 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
3222 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
3223 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 }
3225
3226 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003227 tg3_readphy(tp, MII_TG3_ISTAT, &val);
3228 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003230 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003232 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 tg3_writephy(tp, MII_TG3_IMASK, ~0);
3234
3235 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3236 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3237 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
3238 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3239 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
3240 else
3241 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
3242 }
3243
3244 current_link_up = 0;
3245 current_speed = SPEED_INVALID;
3246 current_duplex = DUPLEX_INVALID;
3247
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003248 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4007);
3250 tg3_readphy(tp, MII_TG3_AUX_CTRL, &val);
3251 if (!(val & (1 << 10))) {
3252 val |= (1 << 10);
3253 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
3254 goto relink;
3255 }
3256 }
3257
3258 bmsr = 0;
3259 for (i = 0; i < 100; i++) {
3260 tg3_readphy(tp, MII_BMSR, &bmsr);
3261 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3262 (bmsr & BMSR_LSTATUS))
3263 break;
3264 udelay(40);
3265 }
3266
3267 if (bmsr & BMSR_LSTATUS) {
3268 u32 aux_stat, bmcr;
3269
3270 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
3271 for (i = 0; i < 2000; i++) {
3272 udelay(10);
3273 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
3274 aux_stat)
3275 break;
3276 }
3277
3278 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
3279 &current_speed,
3280 &current_duplex);
3281
3282 bmcr = 0;
3283 for (i = 0; i < 200; i++) {
3284 tg3_readphy(tp, MII_BMCR, &bmcr);
3285 if (tg3_readphy(tp, MII_BMCR, &bmcr))
3286 continue;
3287 if (bmcr && bmcr != 0x7fff)
3288 break;
3289 udelay(10);
3290 }
3291
Matt Carlsonef167e22007-12-20 20:10:01 -08003292 lcl_adv = 0;
3293 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294
Matt Carlsonef167e22007-12-20 20:10:01 -08003295 tp->link_config.active_speed = current_speed;
3296 tp->link_config.active_duplex = current_duplex;
3297
3298 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3299 if ((bmcr & BMCR_ANENABLE) &&
3300 tg3_copper_is_advertising_all(tp,
3301 tp->link_config.advertising)) {
3302 if (tg3_adv_1000T_flowctrl_ok(tp, &lcl_adv,
3303 &rmt_adv))
3304 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 }
3306 } else {
3307 if (!(bmcr & BMCR_ANENABLE) &&
3308 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08003309 tp->link_config.duplex == current_duplex &&
3310 tp->link_config.flowctrl ==
3311 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 }
3314 }
3315
Matt Carlsonef167e22007-12-20 20:10:01 -08003316 if (current_link_up == 1 &&
3317 tp->link_config.active_duplex == DUPLEX_FULL)
3318 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 }
3320
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321relink:
Matt Carlson80096062010-08-02 11:26:06 +00003322 if (current_link_up == 0 || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 tg3_phy_copper_begin(tp);
3324
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003325 tg3_readphy(tp, MII_BMSR, &bmsr);
3326 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3327 (bmsr & BMSR_LSTATUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 current_link_up = 1;
3329 }
3330
3331 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
3332 if (current_link_up == 1) {
3333 if (tp->link_config.active_speed == SPEED_100 ||
3334 tp->link_config.active_speed == SPEED_10)
3335 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3336 else
3337 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003338 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00003339 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3340 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
3342
3343 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
3344 if (tp->link_config.active_duplex == DUPLEX_HALF)
3345 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
3346
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003348 if (current_link_up == 1 &&
3349 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003351 else
3352 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 }
3354
3355 /* ??? Without this setting Netgear GA302T PHY does not
3356 * ??? send/receive packets...
3357 */
Matt Carlson79eb6902010-02-17 15:17:03 +00003358 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
3360 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
3361 tw32_f(MAC_MI_MODE, tp->mi_mode);
3362 udelay(80);
3363 }
3364
3365 tw32_f(MAC_MODE, tp->mac_mode);
3366 udelay(40);
3367
Matt Carlson52b02d02010-10-14 10:37:41 +00003368 tg3_phy_eee_adjust(tp, current_link_up);
3369
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 if (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) {
3371 /* Polled via timer. */
3372 tw32_f(MAC_EVENT, 0);
3373 } else {
3374 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
3375 }
3376 udelay(40);
3377
3378 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
3379 current_link_up == 1 &&
3380 tp->link_config.active_speed == SPEED_1000 &&
3381 ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) ||
3382 (tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED))) {
3383 udelay(120);
3384 tw32_f(MAC_STATUS,
3385 (MAC_STATUS_SYNC_CHANGED |
3386 MAC_STATUS_CFG_CHANGED));
3387 udelay(40);
3388 tg3_write_mem(tp,
3389 NIC_SRAM_FIRMWARE_MBOX,
3390 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
3391 }
3392
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003393 /* Prevent send BD corruption. */
3394 if (tp->tg3_flags3 & TG3_FLG3_CLKREQ_BUG) {
3395 u16 oldlnkctl, newlnkctl;
3396
3397 pci_read_config_word(tp->pdev,
3398 tp->pcie_cap + PCI_EXP_LNKCTL,
3399 &oldlnkctl);
3400 if (tp->link_config.active_speed == SPEED_100 ||
3401 tp->link_config.active_speed == SPEED_10)
3402 newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
3403 else
3404 newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
3405 if (newlnkctl != oldlnkctl)
3406 pci_write_config_word(tp->pdev,
3407 tp->pcie_cap + PCI_EXP_LNKCTL,
3408 newlnkctl);
3409 }
3410
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 if (current_link_up != netif_carrier_ok(tp->dev)) {
3412 if (current_link_up)
3413 netif_carrier_on(tp->dev);
3414 else
3415 netif_carrier_off(tp->dev);
3416 tg3_link_report(tp);
3417 }
3418
3419 return 0;
3420}
3421
3422struct tg3_fiber_aneginfo {
3423 int state;
3424#define ANEG_STATE_UNKNOWN 0
3425#define ANEG_STATE_AN_ENABLE 1
3426#define ANEG_STATE_RESTART_INIT 2
3427#define ANEG_STATE_RESTART 3
3428#define ANEG_STATE_DISABLE_LINK_OK 4
3429#define ANEG_STATE_ABILITY_DETECT_INIT 5
3430#define ANEG_STATE_ABILITY_DETECT 6
3431#define ANEG_STATE_ACK_DETECT_INIT 7
3432#define ANEG_STATE_ACK_DETECT 8
3433#define ANEG_STATE_COMPLETE_ACK_INIT 9
3434#define ANEG_STATE_COMPLETE_ACK 10
3435#define ANEG_STATE_IDLE_DETECT_INIT 11
3436#define ANEG_STATE_IDLE_DETECT 12
3437#define ANEG_STATE_LINK_OK 13
3438#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
3439#define ANEG_STATE_NEXT_PAGE_WAIT 15
3440
3441 u32 flags;
3442#define MR_AN_ENABLE 0x00000001
3443#define MR_RESTART_AN 0x00000002
3444#define MR_AN_COMPLETE 0x00000004
3445#define MR_PAGE_RX 0x00000008
3446#define MR_NP_LOADED 0x00000010
3447#define MR_TOGGLE_TX 0x00000020
3448#define MR_LP_ADV_FULL_DUPLEX 0x00000040
3449#define MR_LP_ADV_HALF_DUPLEX 0x00000080
3450#define MR_LP_ADV_SYM_PAUSE 0x00000100
3451#define MR_LP_ADV_ASYM_PAUSE 0x00000200
3452#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
3453#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
3454#define MR_LP_ADV_NEXT_PAGE 0x00001000
3455#define MR_TOGGLE_RX 0x00002000
3456#define MR_NP_RX 0x00004000
3457
3458#define MR_LINK_OK 0x80000000
3459
3460 unsigned long link_time, cur_time;
3461
3462 u32 ability_match_cfg;
3463 int ability_match_count;
3464
3465 char ability_match, idle_match, ack_match;
3466
3467 u32 txconfig, rxconfig;
3468#define ANEG_CFG_NP 0x00000080
3469#define ANEG_CFG_ACK 0x00000040
3470#define ANEG_CFG_RF2 0x00000020
3471#define ANEG_CFG_RF1 0x00000010
3472#define ANEG_CFG_PS2 0x00000001
3473#define ANEG_CFG_PS1 0x00008000
3474#define ANEG_CFG_HD 0x00004000
3475#define ANEG_CFG_FD 0x00002000
3476#define ANEG_CFG_INVAL 0x00001f06
3477
3478};
3479#define ANEG_OK 0
3480#define ANEG_DONE 1
3481#define ANEG_TIMER_ENAB 2
3482#define ANEG_FAILED -1
3483
3484#define ANEG_STATE_SETTLE_TIME 10000
3485
3486static int tg3_fiber_aneg_smachine(struct tg3 *tp,
3487 struct tg3_fiber_aneginfo *ap)
3488{
Matt Carlson5be73b42007-12-20 20:09:29 -08003489 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 unsigned long delta;
3491 u32 rx_cfg_reg;
3492 int ret;
3493
3494 if (ap->state == ANEG_STATE_UNKNOWN) {
3495 ap->rxconfig = 0;
3496 ap->link_time = 0;
3497 ap->cur_time = 0;
3498 ap->ability_match_cfg = 0;
3499 ap->ability_match_count = 0;
3500 ap->ability_match = 0;
3501 ap->idle_match = 0;
3502 ap->ack_match = 0;
3503 }
3504 ap->cur_time++;
3505
3506 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
3507 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
3508
3509 if (rx_cfg_reg != ap->ability_match_cfg) {
3510 ap->ability_match_cfg = rx_cfg_reg;
3511 ap->ability_match = 0;
3512 ap->ability_match_count = 0;
3513 } else {
3514 if (++ap->ability_match_count > 1) {
3515 ap->ability_match = 1;
3516 ap->ability_match_cfg = rx_cfg_reg;
3517 }
3518 }
3519 if (rx_cfg_reg & ANEG_CFG_ACK)
3520 ap->ack_match = 1;
3521 else
3522 ap->ack_match = 0;
3523
3524 ap->idle_match = 0;
3525 } else {
3526 ap->idle_match = 1;
3527 ap->ability_match_cfg = 0;
3528 ap->ability_match_count = 0;
3529 ap->ability_match = 0;
3530 ap->ack_match = 0;
3531
3532 rx_cfg_reg = 0;
3533 }
3534
3535 ap->rxconfig = rx_cfg_reg;
3536 ret = ANEG_OK;
3537
Matt Carlson33f401a2010-04-05 10:19:27 +00003538 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 case ANEG_STATE_UNKNOWN:
3540 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
3541 ap->state = ANEG_STATE_AN_ENABLE;
3542
3543 /* fallthru */
3544 case ANEG_STATE_AN_ENABLE:
3545 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
3546 if (ap->flags & MR_AN_ENABLE) {
3547 ap->link_time = 0;
3548 ap->cur_time = 0;
3549 ap->ability_match_cfg = 0;
3550 ap->ability_match_count = 0;
3551 ap->ability_match = 0;
3552 ap->idle_match = 0;
3553 ap->ack_match = 0;
3554
3555 ap->state = ANEG_STATE_RESTART_INIT;
3556 } else {
3557 ap->state = ANEG_STATE_DISABLE_LINK_OK;
3558 }
3559 break;
3560
3561 case ANEG_STATE_RESTART_INIT:
3562 ap->link_time = ap->cur_time;
3563 ap->flags &= ~(MR_NP_LOADED);
3564 ap->txconfig = 0;
3565 tw32(MAC_TX_AUTO_NEG, 0);
3566 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3567 tw32_f(MAC_MODE, tp->mac_mode);
3568 udelay(40);
3569
3570 ret = ANEG_TIMER_ENAB;
3571 ap->state = ANEG_STATE_RESTART;
3572
3573 /* fallthru */
3574 case ANEG_STATE_RESTART:
3575 delta = ap->cur_time - ap->link_time;
Matt Carlson859a5882010-04-05 10:19:28 +00003576 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a5882010-04-05 10:19:28 +00003578 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 break;
3581
3582 case ANEG_STATE_DISABLE_LINK_OK:
3583 ret = ANEG_DONE;
3584 break;
3585
3586 case ANEG_STATE_ABILITY_DETECT_INIT:
3587 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08003588 ap->txconfig = ANEG_CFG_FD;
3589 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3590 if (flowctrl & ADVERTISE_1000XPAUSE)
3591 ap->txconfig |= ANEG_CFG_PS1;
3592 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3593 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3595 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3596 tw32_f(MAC_MODE, tp->mac_mode);
3597 udelay(40);
3598
3599 ap->state = ANEG_STATE_ABILITY_DETECT;
3600 break;
3601
3602 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a5882010-04-05 10:19:28 +00003603 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 break;
3606
3607 case ANEG_STATE_ACK_DETECT_INIT:
3608 ap->txconfig |= ANEG_CFG_ACK;
3609 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3610 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3611 tw32_f(MAC_MODE, tp->mac_mode);
3612 udelay(40);
3613
3614 ap->state = ANEG_STATE_ACK_DETECT;
3615
3616 /* fallthru */
3617 case ANEG_STATE_ACK_DETECT:
3618 if (ap->ack_match != 0) {
3619 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
3620 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
3621 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
3622 } else {
3623 ap->state = ANEG_STATE_AN_ENABLE;
3624 }
3625 } else if (ap->ability_match != 0 &&
3626 ap->rxconfig == 0) {
3627 ap->state = ANEG_STATE_AN_ENABLE;
3628 }
3629 break;
3630
3631 case ANEG_STATE_COMPLETE_ACK_INIT:
3632 if (ap->rxconfig & ANEG_CFG_INVAL) {
3633 ret = ANEG_FAILED;
3634 break;
3635 }
3636 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
3637 MR_LP_ADV_HALF_DUPLEX |
3638 MR_LP_ADV_SYM_PAUSE |
3639 MR_LP_ADV_ASYM_PAUSE |
3640 MR_LP_ADV_REMOTE_FAULT1 |
3641 MR_LP_ADV_REMOTE_FAULT2 |
3642 MR_LP_ADV_NEXT_PAGE |
3643 MR_TOGGLE_RX |
3644 MR_NP_RX);
3645 if (ap->rxconfig & ANEG_CFG_FD)
3646 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
3647 if (ap->rxconfig & ANEG_CFG_HD)
3648 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
3649 if (ap->rxconfig & ANEG_CFG_PS1)
3650 ap->flags |= MR_LP_ADV_SYM_PAUSE;
3651 if (ap->rxconfig & ANEG_CFG_PS2)
3652 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
3653 if (ap->rxconfig & ANEG_CFG_RF1)
3654 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
3655 if (ap->rxconfig & ANEG_CFG_RF2)
3656 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
3657 if (ap->rxconfig & ANEG_CFG_NP)
3658 ap->flags |= MR_LP_ADV_NEXT_PAGE;
3659
3660 ap->link_time = ap->cur_time;
3661
3662 ap->flags ^= (MR_TOGGLE_TX);
3663 if (ap->rxconfig & 0x0008)
3664 ap->flags |= MR_TOGGLE_RX;
3665 if (ap->rxconfig & ANEG_CFG_NP)
3666 ap->flags |= MR_NP_RX;
3667 ap->flags |= MR_PAGE_RX;
3668
3669 ap->state = ANEG_STATE_COMPLETE_ACK;
3670 ret = ANEG_TIMER_ENAB;
3671 break;
3672
3673 case ANEG_STATE_COMPLETE_ACK:
3674 if (ap->ability_match != 0 &&
3675 ap->rxconfig == 0) {
3676 ap->state = ANEG_STATE_AN_ENABLE;
3677 break;
3678 }
3679 delta = ap->cur_time - ap->link_time;
3680 if (delta > ANEG_STATE_SETTLE_TIME) {
3681 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
3682 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3683 } else {
3684 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
3685 !(ap->flags & MR_NP_RX)) {
3686 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3687 } else {
3688 ret = ANEG_FAILED;
3689 }
3690 }
3691 }
3692 break;
3693
3694 case ANEG_STATE_IDLE_DETECT_INIT:
3695 ap->link_time = ap->cur_time;
3696 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3697 tw32_f(MAC_MODE, tp->mac_mode);
3698 udelay(40);
3699
3700 ap->state = ANEG_STATE_IDLE_DETECT;
3701 ret = ANEG_TIMER_ENAB;
3702 break;
3703
3704 case ANEG_STATE_IDLE_DETECT:
3705 if (ap->ability_match != 0 &&
3706 ap->rxconfig == 0) {
3707 ap->state = ANEG_STATE_AN_ENABLE;
3708 break;
3709 }
3710 delta = ap->cur_time - ap->link_time;
3711 if (delta > ANEG_STATE_SETTLE_TIME) {
3712 /* XXX another gem from the Broadcom driver :( */
3713 ap->state = ANEG_STATE_LINK_OK;
3714 }
3715 break;
3716
3717 case ANEG_STATE_LINK_OK:
3718 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
3719 ret = ANEG_DONE;
3720 break;
3721
3722 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
3723 /* ??? unimplemented */
3724 break;
3725
3726 case ANEG_STATE_NEXT_PAGE_WAIT:
3727 /* ??? unimplemented */
3728 break;
3729
3730 default:
3731 ret = ANEG_FAILED;
3732 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734
3735 return ret;
3736}
3737
Matt Carlson5be73b42007-12-20 20:09:29 -08003738static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739{
3740 int res = 0;
3741 struct tg3_fiber_aneginfo aninfo;
3742 int status = ANEG_FAILED;
3743 unsigned int tick;
3744 u32 tmp;
3745
3746 tw32_f(MAC_TX_AUTO_NEG, 0);
3747
3748 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
3749 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
3750 udelay(40);
3751
3752 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
3753 udelay(40);
3754
3755 memset(&aninfo, 0, sizeof(aninfo));
3756 aninfo.flags |= MR_AN_ENABLE;
3757 aninfo.state = ANEG_STATE_UNKNOWN;
3758 aninfo.cur_time = 0;
3759 tick = 0;
3760 while (++tick < 195000) {
3761 status = tg3_fiber_aneg_smachine(tp, &aninfo);
3762 if (status == ANEG_DONE || status == ANEG_FAILED)
3763 break;
3764
3765 udelay(1);
3766 }
3767
3768 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3769 tw32_f(MAC_MODE, tp->mac_mode);
3770 udelay(40);
3771
Matt Carlson5be73b42007-12-20 20:09:29 -08003772 *txflags = aninfo.txconfig;
3773 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774
3775 if (status == ANEG_DONE &&
3776 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
3777 MR_LP_ADV_FULL_DUPLEX)))
3778 res = 1;
3779
3780 return res;
3781}
3782
3783static void tg3_init_bcm8002(struct tg3 *tp)
3784{
3785 u32 mac_status = tr32(MAC_STATUS);
3786 int i;
3787
3788 /* Reset when initting first time or we have a link. */
3789 if ((tp->tg3_flags & TG3_FLAG_INIT_COMPLETE) &&
3790 !(mac_status & MAC_STATUS_PCS_SYNCED))
3791 return;
3792
3793 /* Set PLL lock range. */
3794 tg3_writephy(tp, 0x16, 0x8007);
3795
3796 /* SW reset */
3797 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
3798
3799 /* Wait for reset to complete. */
3800 /* XXX schedule_timeout() ... */
3801 for (i = 0; i < 500; i++)
3802 udelay(10);
3803
3804 /* Config mode; select PMA/Ch 1 regs. */
3805 tg3_writephy(tp, 0x10, 0x8411);
3806
3807 /* Enable auto-lock and comdet, select txclk for tx. */
3808 tg3_writephy(tp, 0x11, 0x0a10);
3809
3810 tg3_writephy(tp, 0x18, 0x00a0);
3811 tg3_writephy(tp, 0x16, 0x41ff);
3812
3813 /* Assert and deassert POR. */
3814 tg3_writephy(tp, 0x13, 0x0400);
3815 udelay(40);
3816 tg3_writephy(tp, 0x13, 0x0000);
3817
3818 tg3_writephy(tp, 0x11, 0x0a50);
3819 udelay(40);
3820 tg3_writephy(tp, 0x11, 0x0a10);
3821
3822 /* Wait for signal to stabilize */
3823 /* XXX schedule_timeout() ... */
3824 for (i = 0; i < 15000; i++)
3825 udelay(10);
3826
3827 /* Deselect the channel register so we can read the PHYID
3828 * later.
3829 */
3830 tg3_writephy(tp, 0x10, 0x8011);
3831}
3832
3833static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
3834{
Matt Carlson82cd3d12007-12-20 20:09:00 -08003835 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 u32 sg_dig_ctrl, sg_dig_status;
3837 u32 serdes_cfg, expected_sg_dig_ctrl;
3838 int workaround, port_a;
3839 int current_link_up;
3840
3841 serdes_cfg = 0;
3842 expected_sg_dig_ctrl = 0;
3843 workaround = 0;
3844 port_a = 1;
3845 current_link_up = 0;
3846
3847 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
3848 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
3849 workaround = 1;
3850 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
3851 port_a = 0;
3852
3853 /* preserve bits 0-11,13,14 for signal pre-emphasis */
3854 /* preserve bits 20-23 for voltage regulator */
3855 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
3856 }
3857
3858 sg_dig_ctrl = tr32(SG_DIG_CTRL);
3859
3860 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003861 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862 if (workaround) {
3863 u32 val = serdes_cfg;
3864
3865 if (port_a)
3866 val |= 0xc010000;
3867 else
3868 val |= 0x4010000;
3869 tw32_f(MAC_SERDES_CFG, val);
3870 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003871
3872 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 }
3874 if (mac_status & MAC_STATUS_PCS_SYNCED) {
3875 tg3_setup_flow_control(tp, 0, 0);
3876 current_link_up = 1;
3877 }
3878 goto out;
3879 }
3880
3881 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003882 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883
Matt Carlson82cd3d12007-12-20 20:09:00 -08003884 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3885 if (flowctrl & ADVERTISE_1000XPAUSE)
3886 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
3887 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3888 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889
3890 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003891 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07003892 tp->serdes_counter &&
3893 ((mac_status & (MAC_STATUS_PCS_SYNCED |
3894 MAC_STATUS_RCVD_CFG)) ==
3895 MAC_STATUS_PCS_SYNCED)) {
3896 tp->serdes_counter--;
3897 current_link_up = 1;
3898 goto out;
3899 }
3900restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 if (workaround)
3902 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003903 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 udelay(5);
3905 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
3906
Michael Chan3d3ebe72006-09-27 15:59:15 -07003907 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003908 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
3910 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003911 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 mac_status = tr32(MAC_STATUS);
3913
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003914 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08003916 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917
Matt Carlson82cd3d12007-12-20 20:09:00 -08003918 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
3919 local_adv |= ADVERTISE_1000XPAUSE;
3920 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
3921 local_adv |= ADVERTISE_1000XPSE_ASYM;
3922
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003923 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003924 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003925 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003926 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927
3928 tg3_setup_flow_control(tp, local_adv, remote_adv);
3929 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003930 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003931 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003932 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003933 if (tp->serdes_counter)
3934 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 else {
3936 if (workaround) {
3937 u32 val = serdes_cfg;
3938
3939 if (port_a)
3940 val |= 0xc010000;
3941 else
3942 val |= 0x4010000;
3943
3944 tw32_f(MAC_SERDES_CFG, val);
3945 }
3946
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003947 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 udelay(40);
3949
3950 /* Link parallel detection - link is up */
3951 /* only if we have PCS_SYNC and not */
3952 /* receiving config code words */
3953 mac_status = tr32(MAC_STATUS);
3954 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
3955 !(mac_status & MAC_STATUS_RCVD_CFG)) {
3956 tg3_setup_flow_control(tp, 0, 0);
3957 current_link_up = 1;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003958 tp->phy_flags |=
3959 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003960 tp->serdes_counter =
3961 SERDES_PARALLEL_DET_TIMEOUT;
3962 } else
3963 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 }
3965 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07003966 } else {
3967 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003968 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 }
3970
3971out:
3972 return current_link_up;
3973}
3974
3975static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
3976{
3977 int current_link_up = 0;
3978
Michael Chan5cf64b82007-05-05 12:11:21 -07003979 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981
3982 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08003983 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003985
Matt Carlson5be73b42007-12-20 20:09:29 -08003986 if (fiber_autoneg(tp, &txflags, &rxflags)) {
3987 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988
Matt Carlson5be73b42007-12-20 20:09:29 -08003989 if (txflags & ANEG_CFG_PS1)
3990 local_adv |= ADVERTISE_1000XPAUSE;
3991 if (txflags & ANEG_CFG_PS2)
3992 local_adv |= ADVERTISE_1000XPSE_ASYM;
3993
3994 if (rxflags & MR_LP_ADV_SYM_PAUSE)
3995 remote_adv |= LPA_1000XPAUSE;
3996 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
3997 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998
3999 tg3_setup_flow_control(tp, local_adv, remote_adv);
4000
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 current_link_up = 1;
4002 }
4003 for (i = 0; i < 30; i++) {
4004 udelay(20);
4005 tw32_f(MAC_STATUS,
4006 (MAC_STATUS_SYNC_CHANGED |
4007 MAC_STATUS_CFG_CHANGED));
4008 udelay(40);
4009 if ((tr32(MAC_STATUS) &
4010 (MAC_STATUS_SYNC_CHANGED |
4011 MAC_STATUS_CFG_CHANGED)) == 0)
4012 break;
4013 }
4014
4015 mac_status = tr32(MAC_STATUS);
4016 if (current_link_up == 0 &&
4017 (mac_status & MAC_STATUS_PCS_SYNCED) &&
4018 !(mac_status & MAC_STATUS_RCVD_CFG))
4019 current_link_up = 1;
4020 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08004021 tg3_setup_flow_control(tp, 0, 0);
4022
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 /* Forcing 1000FD link up. */
4024 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025
4026 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
4027 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004028
4029 tw32_f(MAC_MODE, tp->mac_mode);
4030 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 }
4032
4033out:
4034 return current_link_up;
4035}
4036
4037static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
4038{
4039 u32 orig_pause_cfg;
4040 u16 orig_active_speed;
4041 u8 orig_active_duplex;
4042 u32 mac_status;
4043 int current_link_up;
4044 int i;
4045
Matt Carlson8d018622007-12-20 20:05:44 -08004046 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 orig_active_speed = tp->link_config.active_speed;
4048 orig_active_duplex = tp->link_config.active_duplex;
4049
4050 if (!(tp->tg3_flags2 & TG3_FLG2_HW_AUTONEG) &&
4051 netif_carrier_ok(tp->dev) &&
4052 (tp->tg3_flags & TG3_FLAG_INIT_COMPLETE)) {
4053 mac_status = tr32(MAC_STATUS);
4054 mac_status &= (MAC_STATUS_PCS_SYNCED |
4055 MAC_STATUS_SIGNAL_DET |
4056 MAC_STATUS_CFG_CHANGED |
4057 MAC_STATUS_RCVD_CFG);
4058 if (mac_status == (MAC_STATUS_PCS_SYNCED |
4059 MAC_STATUS_SIGNAL_DET)) {
4060 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4061 MAC_STATUS_CFG_CHANGED));
4062 return 0;
4063 }
4064 }
4065
4066 tw32_f(MAC_TX_AUTO_NEG, 0);
4067
4068 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
4069 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
4070 tw32_f(MAC_MODE, tp->mac_mode);
4071 udelay(40);
4072
Matt Carlson79eb6902010-02-17 15:17:03 +00004073 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 tg3_init_bcm8002(tp);
4075
4076 /* Enable link change event even when serdes polling. */
4077 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4078 udelay(40);
4079
4080 current_link_up = 0;
4081 mac_status = tr32(MAC_STATUS);
4082
4083 if (tp->tg3_flags2 & TG3_FLG2_HW_AUTONEG)
4084 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
4085 else
4086 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
4087
Matt Carlson898a56f2009-08-28 14:02:40 +00004088 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00004090 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091
4092 for (i = 0; i < 100; i++) {
4093 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4094 MAC_STATUS_CFG_CHANGED));
4095 udelay(5);
4096 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07004097 MAC_STATUS_CFG_CHANGED |
4098 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 break;
4100 }
4101
4102 mac_status = tr32(MAC_STATUS);
4103 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
4104 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004105 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
4106 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 tw32_f(MAC_MODE, (tp->mac_mode |
4108 MAC_MODE_SEND_CONFIGS));
4109 udelay(1);
4110 tw32_f(MAC_MODE, tp->mac_mode);
4111 }
4112 }
4113
4114 if (current_link_up == 1) {
4115 tp->link_config.active_speed = SPEED_1000;
4116 tp->link_config.active_duplex = DUPLEX_FULL;
4117 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4118 LED_CTRL_LNKLED_OVERRIDE |
4119 LED_CTRL_1000MBPS_ON));
4120 } else {
4121 tp->link_config.active_speed = SPEED_INVALID;
4122 tp->link_config.active_duplex = DUPLEX_INVALID;
4123 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4124 LED_CTRL_LNKLED_OVERRIDE |
4125 LED_CTRL_TRAFFIC_OVERRIDE));
4126 }
4127
4128 if (current_link_up != netif_carrier_ok(tp->dev)) {
4129 if (current_link_up)
4130 netif_carrier_on(tp->dev);
4131 else
4132 netif_carrier_off(tp->dev);
4133 tg3_link_report(tp);
4134 } else {
Matt Carlson8d018622007-12-20 20:05:44 -08004135 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 if (orig_pause_cfg != now_pause_cfg ||
4137 orig_active_speed != tp->link_config.active_speed ||
4138 orig_active_duplex != tp->link_config.active_duplex)
4139 tg3_link_report(tp);
4140 }
4141
4142 return 0;
4143}
4144
Michael Chan747e8f82005-07-25 12:33:22 -07004145static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
4146{
4147 int current_link_up, err = 0;
4148 u32 bmsr, bmcr;
4149 u16 current_speed;
4150 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08004151 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07004152
4153 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4154 tw32_f(MAC_MODE, tp->mac_mode);
4155 udelay(40);
4156
4157 tw32(MAC_EVENT, 0);
4158
4159 tw32_f(MAC_STATUS,
4160 (MAC_STATUS_SYNC_CHANGED |
4161 MAC_STATUS_CFG_CHANGED |
4162 MAC_STATUS_MI_COMPLETION |
4163 MAC_STATUS_LNKSTATE_CHANGED));
4164 udelay(40);
4165
4166 if (force_reset)
4167 tg3_phy_reset(tp);
4168
4169 current_link_up = 0;
4170 current_speed = SPEED_INVALID;
4171 current_duplex = DUPLEX_INVALID;
4172
4173 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4174 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004175 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
4176 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4177 bmsr |= BMSR_LSTATUS;
4178 else
4179 bmsr &= ~BMSR_LSTATUS;
4180 }
Michael Chan747e8f82005-07-25 12:33:22 -07004181
4182 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
4183
4184 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004185 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004186 /* do nothing, just check for link up at the end */
4187 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4188 u32 adv, new_adv;
4189
4190 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4191 new_adv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
4192 ADVERTISE_1000XPAUSE |
4193 ADVERTISE_1000XPSE_ASYM |
4194 ADVERTISE_SLCT);
4195
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004196 new_adv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Michael Chan747e8f82005-07-25 12:33:22 -07004197
4198 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
4199 new_adv |= ADVERTISE_1000XHALF;
4200 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
4201 new_adv |= ADVERTISE_1000XFULL;
4202
4203 if ((new_adv != adv) || !(bmcr & BMCR_ANENABLE)) {
4204 tg3_writephy(tp, MII_ADVERTISE, new_adv);
4205 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
4206 tg3_writephy(tp, MII_BMCR, bmcr);
4207
4208 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07004209 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004210 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004211
4212 return err;
4213 }
4214 } else {
4215 u32 new_bmcr;
4216
4217 bmcr &= ~BMCR_SPEED1000;
4218 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
4219
4220 if (tp->link_config.duplex == DUPLEX_FULL)
4221 new_bmcr |= BMCR_FULLDPLX;
4222
4223 if (new_bmcr != bmcr) {
4224 /* BMCR_SPEED1000 is a reserved bit that needs
4225 * to be set on write.
4226 */
4227 new_bmcr |= BMCR_SPEED1000;
4228
4229 /* Force a linkdown */
4230 if (netif_carrier_ok(tp->dev)) {
4231 u32 adv;
4232
4233 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4234 adv &= ~(ADVERTISE_1000XFULL |
4235 ADVERTISE_1000XHALF |
4236 ADVERTISE_SLCT);
4237 tg3_writephy(tp, MII_ADVERTISE, adv);
4238 tg3_writephy(tp, MII_BMCR, bmcr |
4239 BMCR_ANRESTART |
4240 BMCR_ANENABLE);
4241 udelay(10);
4242 netif_carrier_off(tp->dev);
4243 }
4244 tg3_writephy(tp, MII_BMCR, new_bmcr);
4245 bmcr = new_bmcr;
4246 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4247 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004248 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
4249 ASIC_REV_5714) {
4250 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4251 bmsr |= BMSR_LSTATUS;
4252 else
4253 bmsr &= ~BMSR_LSTATUS;
4254 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004255 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004256 }
4257 }
4258
4259 if (bmsr & BMSR_LSTATUS) {
4260 current_speed = SPEED_1000;
4261 current_link_up = 1;
4262 if (bmcr & BMCR_FULLDPLX)
4263 current_duplex = DUPLEX_FULL;
4264 else
4265 current_duplex = DUPLEX_HALF;
4266
Matt Carlsonef167e22007-12-20 20:10:01 -08004267 local_adv = 0;
4268 remote_adv = 0;
4269
Michael Chan747e8f82005-07-25 12:33:22 -07004270 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08004271 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07004272
4273 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
4274 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
4275 common = local_adv & remote_adv;
4276 if (common & (ADVERTISE_1000XHALF |
4277 ADVERTISE_1000XFULL)) {
4278 if (common & ADVERTISE_1000XFULL)
4279 current_duplex = DUPLEX_FULL;
4280 else
4281 current_duplex = DUPLEX_HALF;
Matt Carlson57d8b882010-06-05 17:24:35 +00004282 } else if (!(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
4283 /* Link is up via parallel detect */
Matt Carlson859a5882010-04-05 10:19:28 +00004284 } else {
Michael Chan747e8f82005-07-25 12:33:22 -07004285 current_link_up = 0;
Matt Carlson859a5882010-04-05 10:19:28 +00004286 }
Michael Chan747e8f82005-07-25 12:33:22 -07004287 }
4288 }
4289
Matt Carlsonef167e22007-12-20 20:10:01 -08004290 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
4291 tg3_setup_flow_control(tp, local_adv, remote_adv);
4292
Michael Chan747e8f82005-07-25 12:33:22 -07004293 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4294 if (tp->link_config.active_duplex == DUPLEX_HALF)
4295 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4296
4297 tw32_f(MAC_MODE, tp->mac_mode);
4298 udelay(40);
4299
4300 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4301
4302 tp->link_config.active_speed = current_speed;
4303 tp->link_config.active_duplex = current_duplex;
4304
4305 if (current_link_up != netif_carrier_ok(tp->dev)) {
4306 if (current_link_up)
4307 netif_carrier_on(tp->dev);
4308 else {
4309 netif_carrier_off(tp->dev);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004310 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004311 }
4312 tg3_link_report(tp);
4313 }
4314 return err;
4315}
4316
4317static void tg3_serdes_parallel_detect(struct tg3 *tp)
4318{
Michael Chan3d3ebe72006-09-27 15:59:15 -07004319 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07004320 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07004321 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07004322 return;
4323 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00004324
Michael Chan747e8f82005-07-25 12:33:22 -07004325 if (!netif_carrier_ok(tp->dev) &&
4326 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
4327 u32 bmcr;
4328
4329 tg3_readphy(tp, MII_BMCR, &bmcr);
4330 if (bmcr & BMCR_ANENABLE) {
4331 u32 phy1, phy2;
4332
4333 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004334 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
4335 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07004336
4337 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004338 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
4339 MII_TG3_DSP_EXP1_INT_STAT);
4340 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
4341 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07004342
4343 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
4344 /* We have signal detect and not receiving
4345 * config code words, link is up by parallel
4346 * detection.
4347 */
4348
4349 bmcr &= ~BMCR_ANENABLE;
4350 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
4351 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004352 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004353 }
4354 }
Matt Carlson859a5882010-04-05 10:19:28 +00004355 } else if (netif_carrier_ok(tp->dev) &&
4356 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004357 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004358 u32 phy2;
4359
4360 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004361 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
4362 MII_TG3_DSP_EXP1_INT_STAT);
4363 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07004364 if (phy2 & 0x20) {
4365 u32 bmcr;
4366
4367 /* Config code words received, turn on autoneg. */
4368 tg3_readphy(tp, MII_BMCR, &bmcr);
4369 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
4370
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004371 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004372
4373 }
4374 }
4375}
4376
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377static int tg3_setup_phy(struct tg3 *tp, int force_reset)
4378{
4379 int err;
4380
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004381 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004383 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07004384 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a5882010-04-05 10:19:28 +00004385 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387
Matt Carlsonbcb37f62008-11-03 16:52:09 -08004388 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08004389 u32 val, scale;
4390
4391 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
4392 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
4393 scale = 65;
4394 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
4395 scale = 6;
4396 else
4397 scale = 12;
4398
4399 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
4400 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
4401 tw32(GRC_MISC_CFG, val);
4402 }
4403
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 if (tp->link_config.active_speed == SPEED_1000 &&
4405 tp->link_config.active_duplex == DUPLEX_HALF)
4406 tw32(MAC_TX_LENGTHS,
4407 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4408 (6 << TX_LENGTHS_IPG_SHIFT) |
4409 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
4410 else
4411 tw32(MAC_TX_LENGTHS,
4412 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4413 (6 << TX_LENGTHS_IPG_SHIFT) |
4414 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
4415
4416 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
4417 if (netif_carrier_ok(tp->dev)) {
4418 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07004419 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 } else {
4421 tw32(HOSTCC_STAT_COAL_TICKS, 0);
4422 }
4423 }
4424
Matt Carlson8ed5d972007-05-07 00:25:49 -07004425 if (tp->tg3_flags & TG3_FLAG_ASPM_WORKAROUND) {
4426 u32 val = tr32(PCIE_PWR_MGMT_THRESH);
4427 if (!netif_carrier_ok(tp->dev))
4428 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
4429 tp->pwrmgmt_thresh;
4430 else
4431 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
4432 tw32(PCIE_PWR_MGMT_THRESH, val);
4433 }
4434
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 return err;
4436}
4437
Matt Carlson66cfd1b2010-09-30 10:34:30 +00004438static inline int tg3_irq_sync(struct tg3 *tp)
4439{
4440 return tp->irq_sync;
4441}
4442
Michael Chandf3e6542006-05-26 17:48:07 -07004443/* This is called whenever we suspect that the system chipset is re-
4444 * ordering the sequence of MMIO to the tx send mailbox. The symptom
4445 * is bogus tx completions. We try to recover by setting the
4446 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
4447 * in the workqueue.
4448 */
4449static void tg3_tx_recover(struct tg3 *tp)
4450{
4451 BUG_ON((tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER) ||
4452 tp->write32_tx_mbox == tg3_write_indirect_mbox);
4453
Matt Carlson5129c3a2010-04-05 10:19:23 +00004454 netdev_warn(tp->dev,
4455 "The system may be re-ordering memory-mapped I/O "
4456 "cycles to the network device, attempting to recover. "
4457 "Please report the problem to the driver maintainer "
4458 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07004459
4460 spin_lock(&tp->lock);
Michael Chandf3e6542006-05-26 17:48:07 -07004461 tp->tg3_flags |= TG3_FLAG_TX_RECOVERY_PENDING;
Michael Chandf3e6542006-05-26 17:48:07 -07004462 spin_unlock(&tp->lock);
4463}
4464
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004465static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07004466{
Matt Carlsonf65aac12010-08-02 11:26:03 +00004467 /* Tell compiler to fetch tx indices from memory. */
4468 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004469 return tnapi->tx_pending -
4470 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07004471}
4472
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473/* Tigon3 never reports partial packet sends. So we do not
4474 * need special logic to handle SKBs that have not had all
4475 * of their frags sent yet, like SunGEM does.
4476 */
Matt Carlson17375d22009-08-28 14:02:18 +00004477static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478{
Matt Carlson17375d22009-08-28 14:02:18 +00004479 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00004480 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004481 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004482 struct netdev_queue *txq;
4483 int index = tnapi - tp->napi;
4484
Matt Carlson19cfaec2009-12-03 08:36:20 +00004485 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004486 index--;
4487
4488 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489
4490 while (sw_idx != hw_idx) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00004491 struct ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07004493 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494
Michael Chandf3e6542006-05-26 17:48:07 -07004495 if (unlikely(skb == NULL)) {
4496 tg3_tx_recover(tp);
4497 return;
4498 }
4499
Alexander Duyckf4188d82009-12-02 16:48:38 +00004500 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004501 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00004502 skb_headlen(skb),
4503 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504
4505 ri->skb = NULL;
4506
4507 sw_idx = NEXT_TX(sw_idx);
4508
4509 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004510 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07004511 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
4512 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00004513
4514 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004515 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00004516 skb_shinfo(skb)->frags[i].size,
4517 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 sw_idx = NEXT_TX(sw_idx);
4519 }
4520
David S. Millerf47c11e2005-06-24 20:18:35 -07004521 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07004522
4523 if (unlikely(tx_bug)) {
4524 tg3_tx_recover(tp);
4525 return;
4526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527 }
4528
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004529 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530
Michael Chan1b2a7202006-08-07 21:46:02 -07004531 /* Need to make the tx_cons update visible to tg3_start_xmit()
4532 * before checking for netif_queue_stopped(). Without the
4533 * memory barrier, there is a small possibility that tg3_start_xmit()
4534 * will miss it and cause the queue to be stopped forever.
4535 */
4536 smp_mb();
4537
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004538 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004539 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004540 __netif_tx_lock(txq, smp_processor_id());
4541 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004542 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004543 netif_tx_wake_queue(txq);
4544 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07004545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546}
4547
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004548static void tg3_rx_skb_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
4549{
4550 if (!ri->skb)
4551 return;
4552
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004553 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004554 map_sz, PCI_DMA_FROMDEVICE);
4555 dev_kfree_skb_any(ri->skb);
4556 ri->skb = NULL;
4557}
4558
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559/* Returns size of skb allocated or < 0 on error.
4560 *
4561 * We only need to fill in the address because the other members
4562 * of the RX descriptor are invariant, see tg3_init_rings.
4563 *
4564 * Note the purposeful assymetry of cpu vs. chip accesses. For
4565 * posting buffers we only dirty the first cache line of the RX
4566 * descriptor (containing the address). Whereas for the RX status
4567 * buffers the cpu only reads the last cacheline of the RX descriptor
4568 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
4569 */
Matt Carlson86b21e52009-11-13 13:03:45 +00004570static int tg3_alloc_rx_skb(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Matt Carlsona3896162009-11-13 13:03:44 +00004571 u32 opaque_key, u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572{
4573 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00004574 struct ring_info *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575 struct sk_buff *skb;
4576 dma_addr_t mapping;
4577 int skb_size, dest_idx;
4578
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 switch (opaque_key) {
4580 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00004581 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00004582 desc = &tpr->rx_std[dest_idx];
4583 map = &tpr->rx_std_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004584 skb_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 break;
4586
4587 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00004588 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00004589 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00004590 map = &tpr->rx_jmb_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004591 skb_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592 break;
4593
4594 default:
4595 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597
4598 /* Do not overwrite any of the map or rp information
4599 * until we are sure we can commit to a new buffer.
4600 *
4601 * Callers depend upon this behavior and assume that
4602 * we leave everything unchanged if we fail.
4603 */
Matt Carlson287be122009-08-28 13:58:46 +00004604 skb = netdev_alloc_skb(tp->dev, skb_size + tp->rx_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605 if (skb == NULL)
4606 return -ENOMEM;
4607
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 skb_reserve(skb, tp->rx_offset);
4609
Matt Carlson287be122009-08-28 13:58:46 +00004610 mapping = pci_map_single(tp->pdev, skb->data, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 PCI_DMA_FROMDEVICE);
Matt Carlsona21771d2009-11-02 14:25:31 +00004612 if (pci_dma_mapping_error(tp->pdev, mapping)) {
4613 dev_kfree_skb(skb);
4614 return -EIO;
4615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616
4617 map->skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004618 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620 desc->addr_hi = ((u64)mapping >> 32);
4621 desc->addr_lo = ((u64)mapping & 0xffffffff);
4622
4623 return skb_size;
4624}
4625
4626/* We only need to move over in the address because the other
4627 * members of the RX descriptor are invariant. See notes above
4628 * tg3_alloc_rx_skb for full details.
4629 */
Matt Carlsona3896162009-11-13 13:03:44 +00004630static void tg3_recycle_rx(struct tg3_napi *tnapi,
4631 struct tg3_rx_prodring_set *dpr,
4632 u32 opaque_key, int src_idx,
4633 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634{
Matt Carlson17375d22009-08-28 14:02:18 +00004635 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
4637 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00004638 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00004639 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640
4641 switch (opaque_key) {
4642 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00004643 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00004644 dest_desc = &dpr->rx_std[dest_idx];
4645 dest_map = &dpr->rx_std_buffers[dest_idx];
4646 src_desc = &spr->rx_std[src_idx];
4647 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 break;
4649
4650 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00004651 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00004652 dest_desc = &dpr->rx_jmb[dest_idx].std;
4653 dest_map = &dpr->rx_jmb_buffers[dest_idx];
4654 src_desc = &spr->rx_jmb[src_idx].std;
4655 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 break;
4657
4658 default:
4659 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661
4662 dest_map->skb = src_map->skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004663 dma_unmap_addr_set(dest_map, mapping,
4664 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 dest_desc->addr_hi = src_desc->addr_hi;
4666 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00004667
4668 /* Ensure that the update to the skb happens after the physical
4669 * addresses have been transferred to the new BD location.
4670 */
4671 smp_wmb();
4672
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 src_map->skb = NULL;
4674}
4675
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676/* The RX ring scheme is composed of multiple rings which post fresh
4677 * buffers to the chip, and one special ring the chip uses to report
4678 * status back to the host.
4679 *
4680 * The special ring reports the status of received packets to the
4681 * host. The chip does not write into the original descriptor the
4682 * RX buffer was obtained from. The chip simply takes the original
4683 * descriptor as provided by the host, updates the status and length
4684 * field, then writes this into the next status ring entry.
4685 *
4686 * Each ring the host uses to post buffers to the chip is described
4687 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
4688 * it is first placed into the on-chip ram. When the packet's length
4689 * is known, it walks down the TG3_BDINFO entries to select the ring.
4690 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
4691 * which is within the range of the new packet's length is chosen.
4692 *
4693 * The "separate ring for rx status" scheme may sound queer, but it makes
4694 * sense from a cache coherency perspective. If only the host writes
4695 * to the buffer post rings, and only the chip writes to the rx status
4696 * rings, then cache lines never move beyond shared-modified state.
4697 * If both the host and chip were to write into the same ring, cache line
4698 * eviction could occur since both entities want it in an exclusive state.
4699 */
Matt Carlson17375d22009-08-28 14:02:18 +00004700static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701{
Matt Carlson17375d22009-08-28 14:02:18 +00004702 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07004703 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00004704 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00004705 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07004706 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00004708 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004710 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711 /*
4712 * We need to order the read of hw_idx and the read of
4713 * the opaque cookie.
4714 */
4715 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716 work_mask = 0;
4717 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00004718 std_prod_idx = tpr->rx_std_prod_idx;
4719 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00004721 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00004722 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723 unsigned int len;
4724 struct sk_buff *skb;
4725 dma_addr_t dma_addr;
4726 u32 opaque_key, desc_idx, *post_ptr;
Matt Carlson9dc7a112010-04-12 06:58:28 +00004727 bool hw_vlan __maybe_unused = false;
4728 u16 vtag __maybe_unused = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729
4730 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
4731 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
4732 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00004733 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004734 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00004735 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00004736 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07004737 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00004739 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004740 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00004741 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00004742 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00004743 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745
4746 work_mask |= opaque_key;
4747
4748 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
4749 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
4750 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00004751 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752 desc_idx, *post_ptr);
4753 drop_it_no_recycle:
4754 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00004755 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756 goto next_pkt;
4757 }
4758
Matt Carlsonad829262008-11-21 17:16:16 -08004759 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
4760 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761
Matt Carlsond2757fc2010-04-12 06:58:27 +00004762 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763 int skb_size;
4764
Matt Carlson86b21e52009-11-13 13:03:45 +00004765 skb_size = tg3_alloc_rx_skb(tp, tpr, opaque_key,
Matt Carlsonafc081f2009-11-13 13:03:43 +00004766 *post_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767 if (skb_size < 0)
4768 goto drop_it;
4769
Matt Carlson287be122009-08-28 13:58:46 +00004770 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771 PCI_DMA_FROMDEVICE);
4772
Matt Carlson61e800c2010-02-17 15:16:54 +00004773 /* Ensure that the update to the skb happens
4774 * after the usage of the old DMA mapping.
4775 */
4776 smp_wmb();
4777
4778 ri->skb = NULL;
4779
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780 skb_put(skb, len);
4781 } else {
4782 struct sk_buff *copy_skb;
4783
Matt Carlsona3896162009-11-13 13:03:44 +00004784 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785 desc_idx, *post_ptr);
4786
Matt Carlson9dc7a112010-04-12 06:58:28 +00004787 copy_skb = netdev_alloc_skb(tp->dev, len + VLAN_HLEN +
4788 TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789 if (copy_skb == NULL)
4790 goto drop_it_no_recycle;
4791
Matt Carlson9dc7a112010-04-12 06:58:28 +00004792 skb_reserve(copy_skb, TG3_RAW_IP_ALIGN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793 skb_put(copy_skb, len);
4794 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03004795 skb_copy_from_linear_data(skb, copy_skb->data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
4797
4798 /* We'll reuse the original ring buffer. */
4799 skb = copy_skb;
4800 }
4801
4802 if ((tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) &&
4803 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
4804 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
4805 >> RXD_TCPCSUM_SHIFT) == 0xffff))
4806 skb->ip_summed = CHECKSUM_UNNECESSARY;
4807 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07004808 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809
4810 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00004811
4812 if (len > (tp->dev->mtu + ETH_HLEN) &&
4813 skb->protocol != htons(ETH_P_8021Q)) {
4814 dev_kfree_skb(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00004815 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00004816 }
4817
Matt Carlson9dc7a112010-04-12 06:58:28 +00004818 if (desc->type_flags & RXD_FLAG_VLAN &&
4819 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG)) {
4820 vtag = desc->err_vlan & RXD_VLAN_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821#if TG3_VLAN_TAG_USED
Matt Carlson9dc7a112010-04-12 06:58:28 +00004822 if (tp->vlgrp)
4823 hw_vlan = true;
4824 else
4825#endif
4826 {
4827 struct vlan_ethhdr *ve = (struct vlan_ethhdr *)
4828 __skb_push(skb, VLAN_HLEN);
4829
4830 memmove(ve, skb->data + VLAN_HLEN,
4831 ETH_ALEN * 2);
4832 ve->h_vlan_proto = htons(ETH_P_8021Q);
4833 ve->h_vlan_TCI = htons(vtag);
4834 }
4835 }
4836
4837#if TG3_VLAN_TAG_USED
4838 if (hw_vlan)
4839 vlan_gro_receive(&tnapi->napi, tp->vlgrp, vtag, skb);
4840 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841#endif
Matt Carlson17375d22009-08-28 14:02:18 +00004842 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 received++;
4845 budget--;
4846
4847next_pkt:
4848 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07004849
4850 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00004851 tpr->rx_std_prod_idx = std_prod_idx &
4852 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00004853 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
4854 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07004855 work_mask &= ~RXD_OPAQUE_RING_STD;
4856 rx_std_posted = 0;
4857 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07004859 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00004860 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07004861
4862 /* Refresh hw_idx to see if there is new work */
4863 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004864 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07004865 rmb();
4866 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 }
4868
4869 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00004870 tnapi->rx_rcb_ptr = sw_idx;
4871 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872
4873 /* Refill RX ring(s). */
Matt Carlsone4af1af2010-02-12 14:47:05 +00004874 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004875 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00004876 tpr->rx_std_prod_idx = std_prod_idx &
4877 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004878 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
4879 tpr->rx_std_prod_idx);
4880 }
4881 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00004882 tpr->rx_jmb_prod_idx = jmb_prod_idx &
4883 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004884 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
4885 tpr->rx_jmb_prod_idx);
4886 }
4887 mmiowb();
4888 } else if (work_mask) {
4889 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
4890 * updated before the producer indices can be updated.
4891 */
4892 smp_wmb();
4893
Matt Carlson2c49a442010-09-30 10:34:35 +00004894 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
4895 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004896
Matt Carlsone4af1af2010-02-12 14:47:05 +00004897 if (tnapi != &tp->napi[1])
4898 napi_schedule(&tp->napi[1].napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900
4901 return received;
4902}
4903
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004904static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906 /* handle link change and other phy events */
4907 if (!(tp->tg3_flags &
4908 (TG3_FLAG_USE_LINKCHG_REG |
4909 TG3_FLAG_POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004910 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
4911
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912 if (sblk->status & SD_STATUS_LINK_CHG) {
4913 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004914 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07004915 spin_lock(&tp->lock);
Matt Carlsondd477002008-05-25 23:45:58 -07004916 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
4917 tw32_f(MAC_STATUS,
4918 (MAC_STATUS_SYNC_CHANGED |
4919 MAC_STATUS_CFG_CHANGED |
4920 MAC_STATUS_MI_COMPLETION |
4921 MAC_STATUS_LNKSTATE_CHANGED));
4922 udelay(40);
4923 } else
4924 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07004925 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926 }
4927 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004928}
4929
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004930static int tg3_rx_prodring_xfer(struct tg3 *tp,
4931 struct tg3_rx_prodring_set *dpr,
4932 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004933{
4934 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004935 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004936
4937 while (1) {
4938 src_prod_idx = spr->rx_std_prod_idx;
4939
4940 /* Make sure updates to the rx_std_buffers[] entries and the
4941 * standard producer index are seen in the correct order.
4942 */
4943 smp_rmb();
4944
4945 if (spr->rx_std_cons_idx == src_prod_idx)
4946 break;
4947
4948 if (spr->rx_std_cons_idx < src_prod_idx)
4949 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
4950 else
Matt Carlson2c49a442010-09-30 10:34:35 +00004951 cpycnt = tp->rx_std_ring_mask + 1 -
4952 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004953
Matt Carlson2c49a442010-09-30 10:34:35 +00004954 cpycnt = min(cpycnt,
4955 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004956
4957 si = spr->rx_std_cons_idx;
4958 di = dpr->rx_std_prod_idx;
4959
Matt Carlsone92967b2010-02-12 14:47:06 +00004960 for (i = di; i < di + cpycnt; i++) {
4961 if (dpr->rx_std_buffers[i].skb) {
4962 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004963 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00004964 break;
4965 }
4966 }
4967
4968 if (!cpycnt)
4969 break;
4970
4971 /* Ensure that updates to the rx_std_buffers ring and the
4972 * shadowed hardware producer ring from tg3_recycle_skb() are
4973 * ordered correctly WRT the skb check above.
4974 */
4975 smp_rmb();
4976
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004977 memcpy(&dpr->rx_std_buffers[di],
4978 &spr->rx_std_buffers[si],
4979 cpycnt * sizeof(struct ring_info));
4980
4981 for (i = 0; i < cpycnt; i++, di++, si++) {
4982 struct tg3_rx_buffer_desc *sbd, *dbd;
4983 sbd = &spr->rx_std[si];
4984 dbd = &dpr->rx_std[di];
4985 dbd->addr_hi = sbd->addr_hi;
4986 dbd->addr_lo = sbd->addr_lo;
4987 }
4988
Matt Carlson2c49a442010-09-30 10:34:35 +00004989 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
4990 tp->rx_std_ring_mask;
4991 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
4992 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004993 }
4994
4995 while (1) {
4996 src_prod_idx = spr->rx_jmb_prod_idx;
4997
4998 /* Make sure updates to the rx_jmb_buffers[] entries and
4999 * the jumbo producer index are seen in the correct order.
5000 */
5001 smp_rmb();
5002
5003 if (spr->rx_jmb_cons_idx == src_prod_idx)
5004 break;
5005
5006 if (spr->rx_jmb_cons_idx < src_prod_idx)
5007 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
5008 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005009 cpycnt = tp->rx_jmb_ring_mask + 1 -
5010 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005011
5012 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00005013 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005014
5015 si = spr->rx_jmb_cons_idx;
5016 di = dpr->rx_jmb_prod_idx;
5017
Matt Carlsone92967b2010-02-12 14:47:06 +00005018 for (i = di; i < di + cpycnt; i++) {
5019 if (dpr->rx_jmb_buffers[i].skb) {
5020 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005021 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005022 break;
5023 }
5024 }
5025
5026 if (!cpycnt)
5027 break;
5028
5029 /* Ensure that updates to the rx_jmb_buffers ring and the
5030 * shadowed hardware producer ring from tg3_recycle_skb() are
5031 * ordered correctly WRT the skb check above.
5032 */
5033 smp_rmb();
5034
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005035 memcpy(&dpr->rx_jmb_buffers[di],
5036 &spr->rx_jmb_buffers[si],
5037 cpycnt * sizeof(struct ring_info));
5038
5039 for (i = 0; i < cpycnt; i++, di++, si++) {
5040 struct tg3_rx_buffer_desc *sbd, *dbd;
5041 sbd = &spr->rx_jmb[si].std;
5042 dbd = &dpr->rx_jmb[di].std;
5043 dbd->addr_hi = sbd->addr_hi;
5044 dbd->addr_lo = sbd->addr_lo;
5045 }
5046
Matt Carlson2c49a442010-09-30 10:34:35 +00005047 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
5048 tp->rx_jmb_ring_mask;
5049 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
5050 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005051 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005052
5053 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005054}
5055
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005056static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
5057{
5058 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059
5060 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005061 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00005062 tg3_tx(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07005063 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
Michael Chan4fd7ab52007-10-12 01:39:50 -07005064 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065 }
5066
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067 /* run RX thread, within the bounds set by NAPI.
5068 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005069 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005071 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00005072 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005074 if ((tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005075 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005076 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00005077 u32 std_prod_idx = dpr->rx_std_prod_idx;
5078 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005079
Matt Carlsone4af1af2010-02-12 14:47:05 +00005080 for (i = 1; i < tp->irq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005081 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00005082 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005083
5084 wmb();
5085
Matt Carlsone4af1af2010-02-12 14:47:05 +00005086 if (std_prod_idx != dpr->rx_std_prod_idx)
5087 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5088 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005089
Matt Carlsone4af1af2010-02-12 14:47:05 +00005090 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
5091 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5092 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005093
5094 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005095
5096 if (err)
5097 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005098 }
5099
David S. Miller6f535762007-10-11 18:08:29 -07005100 return work_done;
5101}
David S. Millerf7383c22005-05-18 22:50:53 -07005102
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005103static int tg3_poll_msix(struct napi_struct *napi, int budget)
5104{
5105 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5106 struct tg3 *tp = tnapi->tp;
5107 int work_done = 0;
5108 struct tg3_hw_status *sblk = tnapi->hw_status;
5109
5110 while (1) {
5111 work_done = tg3_poll_work(tnapi, work_done, budget);
5112
5113 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
5114 goto tx_recovery;
5115
5116 if (unlikely(work_done >= budget))
5117 break;
5118
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005119 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005120 * to tell the hw how much work has been processed,
5121 * so we must read it before checking for more work.
5122 */
5123 tnapi->last_tag = sblk->status_tag;
5124 tnapi->last_irq_tag = tnapi->last_tag;
5125 rmb();
5126
5127 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00005128 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
5129 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005130 napi_complete(napi);
5131 /* Reenable interrupts. */
5132 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
5133 mmiowb();
5134 break;
5135 }
5136 }
5137
5138 return work_done;
5139
5140tx_recovery:
5141 /* work_done is guaranteed to be less than budget. */
5142 napi_complete(napi);
5143 schedule_work(&tp->reset_task);
5144 return work_done;
5145}
5146
David S. Miller6f535762007-10-11 18:08:29 -07005147static int tg3_poll(struct napi_struct *napi, int budget)
5148{
Matt Carlson8ef04422009-08-28 14:01:37 +00005149 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5150 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07005151 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00005152 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07005153
5154 while (1) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005155 tg3_poll_link(tp);
5156
Matt Carlson17375d22009-08-28 14:02:18 +00005157 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07005158
5159 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
5160 goto tx_recovery;
5161
5162 if (unlikely(work_done >= budget))
5163 break;
5164
Michael Chan4fd7ab52007-10-12 01:39:50 -07005165 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) {
Matt Carlson17375d22009-08-28 14:02:18 +00005166 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07005167 * to tell the hw how much work has been processed,
5168 * so we must read it before checking for more work.
5169 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005170 tnapi->last_tag = sblk->status_tag;
5171 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07005172 rmb();
5173 } else
5174 sblk->status &= ~SD_STATUS_UPDATED;
5175
Matt Carlson17375d22009-08-28 14:02:18 +00005176 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005177 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00005178 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07005179 break;
5180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181 }
5182
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005183 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07005184
5185tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07005186 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08005187 napi_complete(napi);
David S. Miller6f535762007-10-11 18:08:29 -07005188 schedule_work(&tp->reset_task);
Michael Chan4fd7ab52007-10-12 01:39:50 -07005189 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190}
5191
Matt Carlson66cfd1b2010-09-30 10:34:30 +00005192static void tg3_napi_disable(struct tg3 *tp)
5193{
5194 int i;
5195
5196 for (i = tp->irq_cnt - 1; i >= 0; i--)
5197 napi_disable(&tp->napi[i].napi);
5198}
5199
5200static void tg3_napi_enable(struct tg3 *tp)
5201{
5202 int i;
5203
5204 for (i = 0; i < tp->irq_cnt; i++)
5205 napi_enable(&tp->napi[i].napi);
5206}
5207
5208static void tg3_napi_init(struct tg3 *tp)
5209{
5210 int i;
5211
5212 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
5213 for (i = 1; i < tp->irq_cnt; i++)
5214 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
5215}
5216
5217static void tg3_napi_fini(struct tg3 *tp)
5218{
5219 int i;
5220
5221 for (i = 0; i < tp->irq_cnt; i++)
5222 netif_napi_del(&tp->napi[i].napi);
5223}
5224
5225static inline void tg3_netif_stop(struct tg3 *tp)
5226{
5227 tp->dev->trans_start = jiffies; /* prevent tx timeout */
5228 tg3_napi_disable(tp);
5229 netif_tx_disable(tp->dev);
5230}
5231
5232static inline void tg3_netif_start(struct tg3 *tp)
5233{
5234 /* NOTE: unconditional netif_tx_wake_all_queues is only
5235 * appropriate so long as all callers are assured to
5236 * have free tx slots (such as after tg3_init_hw)
5237 */
5238 netif_tx_wake_all_queues(tp->dev);
5239
5240 tg3_napi_enable(tp);
5241 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
5242 tg3_enable_ints(tp);
5243}
5244
David S. Millerf47c11e2005-06-24 20:18:35 -07005245static void tg3_irq_quiesce(struct tg3 *tp)
5246{
Matt Carlson4f125f42009-09-01 12:55:02 +00005247 int i;
5248
David S. Millerf47c11e2005-06-24 20:18:35 -07005249 BUG_ON(tp->irq_sync);
5250
5251 tp->irq_sync = 1;
5252 smp_mb();
5253
Matt Carlson4f125f42009-09-01 12:55:02 +00005254 for (i = 0; i < tp->irq_cnt; i++)
5255 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07005256}
5257
David S. Millerf47c11e2005-06-24 20:18:35 -07005258/* Fully shutdown all tg3 driver activity elsewhere in the system.
5259 * If irq_sync is non-zero, then the IRQ handler must be synchronized
5260 * with as well. Most of the time, this is not necessary except when
5261 * shutting down the device.
5262 */
5263static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
5264{
Michael Chan46966542007-07-11 19:47:19 -07005265 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07005266 if (irq_sync)
5267 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07005268}
5269
5270static inline void tg3_full_unlock(struct tg3 *tp)
5271{
David S. Millerf47c11e2005-06-24 20:18:35 -07005272 spin_unlock_bh(&tp->lock);
5273}
5274
Michael Chanfcfa0a32006-03-20 22:28:41 -08005275/* One-shot MSI handler - Chip automatically disables interrupt
5276 * after sending MSI so driver doesn't have to do it.
5277 */
David Howells7d12e782006-10-05 14:55:46 +01005278static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08005279{
Matt Carlson09943a12009-08-28 14:01:57 +00005280 struct tg3_napi *tnapi = dev_id;
5281 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08005282
Matt Carlson898a56f2009-08-28 14:02:40 +00005283 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005284 if (tnapi->rx_rcb)
5285 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005286
5287 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005288 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005289
5290 return IRQ_HANDLED;
5291}
5292
Michael Chan88b06bc2005-04-21 17:13:25 -07005293/* MSI ISR - No need to check for interrupt sharing and no need to
5294 * flush status block and interrupt mailbox. PCI ordering rules
5295 * guarantee that MSI will arrive after the status block.
5296 */
David Howells7d12e782006-10-05 14:55:46 +01005297static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc2005-04-21 17:13:25 -07005298{
Matt Carlson09943a12009-08-28 14:01:57 +00005299 struct tg3_napi *tnapi = dev_id;
5300 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc2005-04-21 17:13:25 -07005301
Matt Carlson898a56f2009-08-28 14:02:40 +00005302 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005303 if (tnapi->rx_rcb)
5304 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc2005-04-21 17:13:25 -07005305 /*
David S. Millerfac9b832005-05-18 22:46:34 -07005306 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc2005-04-21 17:13:25 -07005307 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07005308 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc2005-04-21 17:13:25 -07005309 * NIC to stop sending us irqs, engaging "in-intr-handler"
5310 * event coalescing.
5311 */
5312 tw32_mailbox(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07005313 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005314 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07005315
Michael Chan88b06bc2005-04-21 17:13:25 -07005316 return IRQ_RETVAL(1);
5317}
5318
David Howells7d12e782006-10-05 14:55:46 +01005319static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320{
Matt Carlson09943a12009-08-28 14:01:57 +00005321 struct tg3_napi *tnapi = dev_id;
5322 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005323 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324 unsigned int handled = 1;
5325
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326 /* In INTx mode, it is possible for the interrupt to arrive at
5327 * the CPU before the status block posted prior to the interrupt.
5328 * Reading the PCI State register will confirm whether the
5329 * interrupt is ours and will flush the status block.
5330 */
Michael Chand18edcb2007-03-24 20:57:11 -07005331 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
5332 if ((tp->tg3_flags & TG3_FLAG_CHIP_RESETTING) ||
5333 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5334 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005335 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07005336 }
Michael Chand18edcb2007-03-24 20:57:11 -07005337 }
5338
5339 /*
5340 * Writing any value to intr-mbox-0 clears PCI INTA# and
5341 * chip-internal interrupt pending events.
5342 * Writing non-zero to intr-mbox-0 additional tells the
5343 * NIC to stop sending us irqs, engaging "in-intr-handler"
5344 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005345 *
5346 * Flush the mailbox to de-assert the IRQ immediately to prevent
5347 * spurious interrupts. The flush impacts performance but
5348 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005349 */
Michael Chanc04cb342007-05-07 00:26:15 -07005350 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07005351 if (tg3_irq_sync(tp))
5352 goto out;
5353 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00005354 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00005355 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00005356 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07005357 } else {
5358 /* No work, shared interrupt perhaps? re-enable
5359 * interrupts, and flush that PCI write
5360 */
5361 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
5362 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07005363 }
David S. Millerf47c11e2005-06-24 20:18:35 -07005364out:
David S. Millerfac9b832005-05-18 22:46:34 -07005365 return IRQ_RETVAL(handled);
5366}
5367
David Howells7d12e782006-10-05 14:55:46 +01005368static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07005369{
Matt Carlson09943a12009-08-28 14:01:57 +00005370 struct tg3_napi *tnapi = dev_id;
5371 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005372 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07005373 unsigned int handled = 1;
5374
David S. Millerfac9b832005-05-18 22:46:34 -07005375 /* In INTx mode, it is possible for the interrupt to arrive at
5376 * the CPU before the status block posted prior to the interrupt.
5377 * Reading the PCI State register will confirm whether the
5378 * interrupt is ours and will flush the status block.
5379 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005380 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Michael Chand18edcb2007-03-24 20:57:11 -07005381 if ((tp->tg3_flags & TG3_FLAG_CHIP_RESETTING) ||
5382 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5383 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005384 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385 }
Michael Chand18edcb2007-03-24 20:57:11 -07005386 }
5387
5388 /*
5389 * writing any value to intr-mbox-0 clears PCI INTA# and
5390 * chip-internal interrupt pending events.
5391 * writing non-zero to intr-mbox-0 additional tells the
5392 * NIC to stop sending us irqs, engaging "in-intr-handler"
5393 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005394 *
5395 * Flush the mailbox to de-assert the IRQ immediately to prevent
5396 * spurious interrupts. The flush impacts performance but
5397 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005398 */
Michael Chanc04cb342007-05-07 00:26:15 -07005399 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00005400
5401 /*
5402 * In a shared interrupt configuration, sometimes other devices'
5403 * interrupts will scream. We record the current status tag here
5404 * so that the above check can report that the screaming interrupts
5405 * are unhandled. Eventually they will be silenced.
5406 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005407 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00005408
Michael Chand18edcb2007-03-24 20:57:11 -07005409 if (tg3_irq_sync(tp))
5410 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00005411
Matt Carlson72334482009-08-28 14:03:01 +00005412 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00005413
Matt Carlson09943a12009-08-28 14:01:57 +00005414 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00005415
David S. Millerf47c11e2005-06-24 20:18:35 -07005416out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417 return IRQ_RETVAL(handled);
5418}
5419
Michael Chan79381092005-04-21 17:13:59 -07005420/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01005421static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07005422{
Matt Carlson09943a12009-08-28 14:01:57 +00005423 struct tg3_napi *tnapi = dev_id;
5424 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005425 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07005426
Michael Chanf9804dd2005-09-27 12:13:10 -07005427 if ((sblk->status & SD_STATUS_UPDATED) ||
5428 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07005429 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07005430 return IRQ_RETVAL(1);
5431 }
5432 return IRQ_RETVAL(0);
5433}
5434
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07005435static int tg3_init_hw(struct tg3 *, int);
Michael Chan944d9802005-05-29 14:57:48 -07005436static int tg3_halt(struct tg3 *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437
Michael Chanb9ec6c12006-07-25 16:37:27 -07005438/* Restart hardware after configuration changes, self-test, etc.
5439 * Invoked with tp->lock held.
5440 */
5441static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
Eric Dumazet78c61462008-04-24 23:33:06 -07005442 __releases(tp->lock)
5443 __acquires(tp->lock)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005444{
5445 int err;
5446
5447 err = tg3_init_hw(tp, reset_phy);
5448 if (err) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00005449 netdev_err(tp->dev,
5450 "Failed to re-initialize device, aborting\n");
Michael Chanb9ec6c12006-07-25 16:37:27 -07005451 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
5452 tg3_full_unlock(tp);
5453 del_timer_sync(&tp->timer);
5454 tp->irq_sync = 0;
Matt Carlsonfed97812009-09-01 13:10:19 +00005455 tg3_napi_enable(tp);
Michael Chanb9ec6c12006-07-25 16:37:27 -07005456 dev_close(tp->dev);
5457 tg3_full_lock(tp, 0);
5458 }
5459 return err;
5460}
5461
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462#ifdef CONFIG_NET_POLL_CONTROLLER
5463static void tg3_poll_controller(struct net_device *dev)
5464{
Matt Carlson4f125f42009-09-01 12:55:02 +00005465 int i;
Michael Chan88b06bc2005-04-21 17:13:25 -07005466 struct tg3 *tp = netdev_priv(dev);
5467
Matt Carlson4f125f42009-09-01 12:55:02 +00005468 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00005469 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470}
5471#endif
5472
David Howellsc4028952006-11-22 14:57:56 +00005473static void tg3_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474{
David Howellsc4028952006-11-22 14:57:56 +00005475 struct tg3 *tp = container_of(work, struct tg3, reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005476 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477 unsigned int restart_timer;
5478
Michael Chan7faa0062006-02-02 17:29:28 -08005479 tg3_full_lock(tp, 0);
Michael Chan7faa0062006-02-02 17:29:28 -08005480
5481 if (!netif_running(tp->dev)) {
Michael Chan7faa0062006-02-02 17:29:28 -08005482 tg3_full_unlock(tp);
5483 return;
5484 }
5485
5486 tg3_full_unlock(tp);
5487
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005488 tg3_phy_stop(tp);
5489
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490 tg3_netif_stop(tp);
5491
David S. Millerf47c11e2005-06-24 20:18:35 -07005492 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493
5494 restart_timer = tp->tg3_flags2 & TG3_FLG2_RESTART_TIMER;
5495 tp->tg3_flags2 &= ~TG3_FLG2_RESTART_TIMER;
5496
Michael Chandf3e6542006-05-26 17:48:07 -07005497 if (tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING) {
5498 tp->write32_tx_mbox = tg3_write32_tx_mbox;
5499 tp->write32_rx_mbox = tg3_write_flush_reg32;
5500 tp->tg3_flags |= TG3_FLAG_MBOX_WRITE_REORDER;
5501 tp->tg3_flags &= ~TG3_FLAG_TX_RECOVERY_PENDING;
5502 }
5503
Michael Chan944d9802005-05-29 14:57:48 -07005504 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005505 err = tg3_init_hw(tp, 1);
5506 if (err)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005507 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508
5509 tg3_netif_start(tp);
5510
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511 if (restart_timer)
5512 mod_timer(&tp->timer, jiffies + 1);
Michael Chan7faa0062006-02-02 17:29:28 -08005513
Michael Chanb9ec6c12006-07-25 16:37:27 -07005514out:
Michael Chan7faa0062006-02-02 17:29:28 -08005515 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005516
5517 if (!err)
5518 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519}
5520
Michael Chanb0408752007-02-13 12:18:30 -08005521static void tg3_dump_short_state(struct tg3 *tp)
5522{
Joe Perches05dbe002010-02-17 19:44:19 +00005523 netdev_err(tp->dev, "DEBUG: MAC_TX_STATUS[%08x] MAC_RX_STATUS[%08x]\n",
5524 tr32(MAC_TX_STATUS), tr32(MAC_RX_STATUS));
5525 netdev_err(tp->dev, "DEBUG: RDMAC_STATUS[%08x] WDMAC_STATUS[%08x]\n",
5526 tr32(RDMAC_STATUS), tr32(WDMAC_STATUS));
Michael Chanb0408752007-02-13 12:18:30 -08005527}
5528
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529static void tg3_tx_timeout(struct net_device *dev)
5530{
5531 struct tg3 *tp = netdev_priv(dev);
5532
Michael Chanb0408752007-02-13 12:18:30 -08005533 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00005534 netdev_err(dev, "transmit timed out, resetting\n");
Michael Chanb0408752007-02-13 12:18:30 -08005535 tg3_dump_short_state(tp);
5536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537
5538 schedule_work(&tp->reset_task);
5539}
5540
Michael Chanc58ec932005-09-17 00:46:27 -07005541/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
5542static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
5543{
5544 u32 base = (u32) mapping & 0xffffffff;
5545
Eric Dumazet807540b2010-09-23 05:40:09 +00005546 return (base > 0xffffdcc0) && (base + len + 8 < base);
Michael Chanc58ec932005-09-17 00:46:27 -07005547}
5548
Michael Chan72f2afb2006-03-06 19:28:35 -08005549/* Test for DMA addresses > 40-bit */
5550static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
5551 int len)
5552{
5553#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Michael Chan6728a8e2006-03-27 23:16:49 -08005554 if (tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG)
Eric Dumazet807540b2010-09-23 05:40:09 +00005555 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08005556 return 0;
5557#else
5558 return 0;
5559#endif
5560}
5561
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005562static void tg3_set_txd(struct tg3_napi *, int, dma_addr_t, int, u32, u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563
Michael Chan72f2afb2006-03-06 19:28:35 -08005564/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00005565static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
5566 struct sk_buff *skb, u32 last_plus_one,
5567 u32 *start, u32 base_flags, u32 mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568{
Matt Carlson24f4efd2009-11-13 13:03:35 +00005569 struct tg3 *tp = tnapi->tp;
Matt Carlson41588ba2008-04-19 18:12:33 -07005570 struct sk_buff *new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07005571 dma_addr_t new_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572 u32 entry = *start;
Michael Chanc58ec932005-09-17 00:46:27 -07005573 int i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574
Matt Carlson41588ba2008-04-19 18:12:33 -07005575 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
5576 new_skb = skb_copy(skb, GFP_ATOMIC);
5577 else {
5578 int more_headroom = 4 - ((unsigned long)skb->data & 3);
5579
5580 new_skb = skb_copy_expand(skb,
5581 skb_headroom(skb) + more_headroom,
5582 skb_tailroom(skb), GFP_ATOMIC);
5583 }
5584
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07005586 ret = -1;
5587 } else {
5588 /* New SKB is guaranteed to be linear. */
5589 entry = *start;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005590 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
5591 PCI_DMA_TODEVICE);
5592 /* Make sure the mapping succeeded */
5593 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
5594 ret = -1;
5595 dev_kfree_skb(new_skb);
5596 new_skb = NULL;
David S. Miller90079ce2008-09-11 04:52:51 -07005597
Michael Chanc58ec932005-09-17 00:46:27 -07005598 /* Make sure new skb does not cross any 4G boundaries.
5599 * Drop the packet if it does.
5600 */
Alexander Duyckf4188d82009-12-02 16:48:38 +00005601 } else if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
5602 tg3_4g_overflow_test(new_addr, new_skb->len)) {
5603 pci_unmap_single(tp->pdev, new_addr, new_skb->len,
5604 PCI_DMA_TODEVICE);
Michael Chanc58ec932005-09-17 00:46:27 -07005605 ret = -1;
5606 dev_kfree_skb(new_skb);
5607 new_skb = NULL;
5608 } else {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005609 tg3_set_txd(tnapi, entry, new_addr, new_skb->len,
Michael Chanc58ec932005-09-17 00:46:27 -07005610 base_flags, 1 | (mss << 1));
5611 *start = NEXT_TX(entry);
5612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613 }
5614
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615 /* Now clean up the sw ring entries. */
5616 i = 0;
5617 while (entry != last_plus_one) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00005618 int len;
5619
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005620 if (i == 0)
Alexander Duyckf4188d82009-12-02 16:48:38 +00005621 len = skb_headlen(skb);
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005622 else
Alexander Duyckf4188d82009-12-02 16:48:38 +00005623 len = skb_shinfo(skb)->frags[i-1].size;
5624
5625 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005626 dma_unmap_addr(&tnapi->tx_buffers[entry],
Alexander Duyckf4188d82009-12-02 16:48:38 +00005627 mapping),
5628 len, PCI_DMA_TODEVICE);
5629 if (i == 0) {
5630 tnapi->tx_buffers[entry].skb = new_skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005631 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00005632 new_addr);
5633 } else {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005634 tnapi->tx_buffers[entry].skb = NULL;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636 entry = NEXT_TX(entry);
5637 i++;
5638 }
5639
5640 dev_kfree_skb(skb);
5641
Michael Chanc58ec932005-09-17 00:46:27 -07005642 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643}
5644
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005645static void tg3_set_txd(struct tg3_napi *tnapi, int entry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646 dma_addr_t mapping, int len, u32 flags,
5647 u32 mss_and_is_end)
5648{
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005649 struct tg3_tx_buffer_desc *txd = &tnapi->tx_ring[entry];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650 int is_end = (mss_and_is_end & 0x1);
5651 u32 mss = (mss_and_is_end >> 1);
5652 u32 vlan_tag = 0;
5653
5654 if (is_end)
5655 flags |= TXD_FLAG_END;
5656 if (flags & TXD_FLAG_VLAN) {
5657 vlan_tag = flags >> 16;
5658 flags &= 0xffff;
5659 }
5660 vlan_tag |= (mss << TXD_MSS_SHIFT);
5661
5662 txd->addr_hi = ((u64) mapping >> 32);
5663 txd->addr_lo = ((u64) mapping & 0xffffffff);
5664 txd->len_flags = (len << TXD_LEN_SHIFT) | flags;
5665 txd->vlan_tag = vlan_tag << TXD_VLAN_TAG_SHIFT;
5666}
5667
Michael Chan5a6f3072006-03-20 22:28:05 -08005668/* hard_start_xmit for devices that don't have any bugs and
Matt Carlsone849cdc2009-11-13 13:03:38 +00005669 * support TG3_FLG2_HW_TSO_2 and TG3_FLG2_HW_TSO_3 only.
Michael Chan5a6f3072006-03-20 22:28:05 -08005670 */
Stephen Hemminger613573252009-08-31 19:50:58 +00005671static netdev_tx_t tg3_start_xmit(struct sk_buff *skb,
5672 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673{
5674 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675 u32 len, entry, base_flags, mss;
David S. Miller90079ce2008-09-11 04:52:51 -07005676 dma_addr_t mapping;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005677 struct tg3_napi *tnapi;
5678 struct netdev_queue *txq;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005679 unsigned int i, last;
5680
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005681 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
5682 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Matt Carlson19cfaec2009-12-03 08:36:20 +00005683 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005684 tnapi++;
Michael Chan5a6f3072006-03-20 22:28:05 -08005685
Michael Chan00b70502006-06-17 21:58:45 -07005686 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005687 * and TX reclaim runs via tp->napi.poll inside of a software
Michael Chan5a6f3072006-03-20 22:28:05 -08005688 * interrupt. Furthermore, IRQ processing runs lockless so we have
5689 * no IRQ context deadlocks to worry about either. Rejoice!
5690 */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005691 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005692 if (!netif_tx_queue_stopped(txq)) {
5693 netif_tx_stop_queue(txq);
Michael Chan5a6f3072006-03-20 22:28:05 -08005694
5695 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00005696 netdev_err(dev,
5697 "BUG! Tx Ring full when queue awake!\n");
Michael Chan5a6f3072006-03-20 22:28:05 -08005698 }
Michael Chan5a6f3072006-03-20 22:28:05 -08005699 return NETDEV_TX_BUSY;
5700 }
5701
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005702 entry = tnapi->tx_prod;
Michael Chan5a6f3072006-03-20 22:28:05 -08005703 base_flags = 0;
Matt Carlsonbe98da62010-07-11 09:31:46 +00005704 mss = skb_shinfo(skb)->gso_size;
5705 if (mss) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005706 int tcp_opt_len, ip_tcp_len;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005707 u32 hdrlen;
Michael Chan5a6f3072006-03-20 22:28:05 -08005708
5709 if (skb_header_cloned(skb) &&
5710 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
5711 dev_kfree_skb(skb);
5712 goto out_unlock;
5713 }
5714
Matt Carlson02e96082010-09-15 08:59:59 +00005715 if (skb_is_gso_v6(skb)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005716 hdrlen = skb_headlen(skb) - ETH_HLEN;
Matt Carlson02e96082010-09-15 08:59:59 +00005717 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005718 struct iphdr *iph = ip_hdr(skb);
5719
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07005720 tcp_opt_len = tcp_optlen(skb);
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -03005721 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
Michael Chanb0026622006-07-03 19:42:14 -07005722
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005723 iph->check = 0;
5724 iph->tot_len = htons(mss + ip_tcp_len + tcp_opt_len);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005725 hdrlen = ip_tcp_len + tcp_opt_len;
Michael Chanb0026622006-07-03 19:42:14 -07005726 }
Michael Chan5a6f3072006-03-20 22:28:05 -08005727
Matt Carlsone849cdc2009-11-13 13:03:38 +00005728 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005729 mss |= (hdrlen & 0xc) << 12;
5730 if (hdrlen & 0x10)
5731 base_flags |= 0x00000010;
5732 base_flags |= (hdrlen & 0x3e0) << 5;
5733 } else
5734 mss |= hdrlen << 9;
5735
Michael Chan5a6f3072006-03-20 22:28:05 -08005736 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
5737 TXD_FLAG_CPU_POST_DMA);
5738
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005739 tcp_hdr(skb)->check = 0;
Michael Chan5a6f3072006-03-20 22:28:05 -08005740
Matt Carlson859a5882010-04-05 10:19:28 +00005741 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005742 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson859a5882010-04-05 10:19:28 +00005743 }
5744
Michael Chan5a6f3072006-03-20 22:28:05 -08005745#if TG3_VLAN_TAG_USED
Jesse Grosseab6d182010-10-20 13:56:03 +00005746 if (vlan_tx_tag_present(skb))
Michael Chan5a6f3072006-03-20 22:28:05 -08005747 base_flags |= (TXD_FLAG_VLAN |
5748 (vlan_tx_tag_get(skb) << 16));
5749#endif
5750
Alexander Duyckf4188d82009-12-02 16:48:38 +00005751 len = skb_headlen(skb);
5752
5753 /* Queue skb data, a.k.a. the main skb fragment. */
5754 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
5755 if (pci_dma_mapping_error(tp->pdev, mapping)) {
David S. Miller90079ce2008-09-11 04:52:51 -07005756 dev_kfree_skb(skb);
5757 goto out_unlock;
5758 }
5759
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005760 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005761 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005762
Matt Carlsonb703df62009-12-03 08:36:21 +00005763 if ((tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005764 !mss && skb->len > ETH_DATA_LEN)
5765 base_flags |= TXD_FLAG_JMB_PKT;
5766
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005767 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
Michael Chan5a6f3072006-03-20 22:28:05 -08005768 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
5769
5770 entry = NEXT_TX(entry);
5771
5772 /* Now loop through additional data fragments, and queue them. */
5773 if (skb_shinfo(skb)->nr_frags > 0) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005774 last = skb_shinfo(skb)->nr_frags - 1;
5775 for (i = 0; i <= last; i++) {
5776 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5777
5778 len = frag->size;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005779 mapping = pci_map_page(tp->pdev,
5780 frag->page,
5781 frag->page_offset,
5782 len, PCI_DMA_TODEVICE);
5783 if (pci_dma_mapping_error(tp->pdev, mapping))
5784 goto dma_error;
5785
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005786 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005787 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00005788 mapping);
Michael Chan5a6f3072006-03-20 22:28:05 -08005789
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005790 tg3_set_txd(tnapi, entry, mapping, len,
Michael Chan5a6f3072006-03-20 22:28:05 -08005791 base_flags, (i == last) | (mss << 1));
5792
5793 entry = NEXT_TX(entry);
5794 }
5795 }
5796
5797 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005798 tw32_tx_mbox(tnapi->prodmbox, entry);
Michael Chan5a6f3072006-03-20 22:28:05 -08005799
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005800 tnapi->tx_prod = entry;
5801 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005802 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00005803
5804 /* netif_tx_stop_queue() must be done before checking
5805 * checking tx index in tg3_tx_avail() below, because in
5806 * tg3_tx(), we update tx index before checking for
5807 * netif_tx_queue_stopped().
5808 */
5809 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005810 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005811 netif_tx_wake_queue(txq);
Michael Chan5a6f3072006-03-20 22:28:05 -08005812 }
5813
5814out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00005815 mmiowb();
Michael Chan5a6f3072006-03-20 22:28:05 -08005816
5817 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005818
5819dma_error:
5820 last = i;
5821 entry = tnapi->tx_prod;
5822 tnapi->tx_buffers[entry].skb = NULL;
5823 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005824 dma_unmap_addr(&tnapi->tx_buffers[entry], mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005825 skb_headlen(skb),
5826 PCI_DMA_TODEVICE);
5827 for (i = 0; i <= last; i++) {
5828 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5829 entry = NEXT_TX(entry);
5830
5831 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005832 dma_unmap_addr(&tnapi->tx_buffers[entry],
Alexander Duyckf4188d82009-12-02 16:48:38 +00005833 mapping),
5834 frag->size, PCI_DMA_TODEVICE);
5835 }
5836
5837 dev_kfree_skb(skb);
5838 return NETDEV_TX_OK;
Michael Chan5a6f3072006-03-20 22:28:05 -08005839}
5840
Stephen Hemminger613573252009-08-31 19:50:58 +00005841static netdev_tx_t tg3_start_xmit_dma_bug(struct sk_buff *,
5842 struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07005843
5844/* Use GSO to workaround a rare TSO bug that may be triggered when the
5845 * TSO header is greater than 80 bytes.
5846 */
5847static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
5848{
5849 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005850 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07005851
5852 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005853 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07005854 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00005855
5856 /* netif_tx_stop_queue() must be done before checking
5857 * checking tx index in tg3_tx_avail() below, because in
5858 * tg3_tx(), we update tx index before checking for
5859 * netif_tx_queue_stopped().
5860 */
5861 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005862 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08005863 return NETDEV_TX_BUSY;
5864
5865 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07005866 }
5867
5868 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07005869 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07005870 goto tg3_tso_bug_end;
5871
5872 do {
5873 nskb = segs;
5874 segs = segs->next;
5875 nskb->next = NULL;
5876 tg3_start_xmit_dma_bug(nskb, tp->dev);
5877 } while (segs);
5878
5879tg3_tso_bug_end:
5880 dev_kfree_skb(skb);
5881
5882 return NETDEV_TX_OK;
5883}
Michael Chan52c0fd82006-06-29 20:15:54 -07005884
Michael Chan5a6f3072006-03-20 22:28:05 -08005885/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
5886 * support TG3_FLG2_HW_TSO_1 or firmware TSO only.
5887 */
Stephen Hemminger613573252009-08-31 19:50:58 +00005888static netdev_tx_t tg3_start_xmit_dma_bug(struct sk_buff *skb,
5889 struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08005890{
5891 struct tg3 *tp = netdev_priv(dev);
Michael Chan5a6f3072006-03-20 22:28:05 -08005892 u32 len, entry, base_flags, mss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005893 int would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07005894 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00005895 struct tg3_napi *tnapi;
5896 struct netdev_queue *txq;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005897 unsigned int i, last;
5898
Matt Carlson24f4efd2009-11-13 13:03:35 +00005899 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
5900 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Matt Carlson19cfaec2009-12-03 08:36:20 +00005901 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlson24f4efd2009-11-13 13:03:35 +00005902 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903
Michael Chan00b70502006-06-17 21:58:45 -07005904 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005905 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07005906 * interrupt. Furthermore, IRQ processing runs lockless so we have
5907 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908 */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005909 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00005910 if (!netif_tx_queue_stopped(txq)) {
5911 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08005912
5913 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00005914 netdev_err(dev,
5915 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08005916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005917 return NETDEV_TX_BUSY;
5918 }
5919
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005920 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07005922 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00005924
Matt Carlsonbe98da62010-07-11 09:31:46 +00005925 mss = skb_shinfo(skb)->gso_size;
5926 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005927 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00005928 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005929
5930 if (skb_header_cloned(skb) &&
5931 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
5932 dev_kfree_skb(skb);
5933 goto out_unlock;
5934 }
5935
Matt Carlson34195c32010-07-11 09:31:42 +00005936 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07005937 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005938
Matt Carlson02e96082010-09-15 08:59:59 +00005939 if (skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00005940 hdr_len = skb_headlen(skb) - ETH_HLEN;
5941 } else {
5942 u32 ip_tcp_len;
5943
5944 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
5945 hdr_len = ip_tcp_len + tcp_opt_len;
5946
5947 iph->check = 0;
5948 iph->tot_len = htons(mss + hdr_len);
5949 }
5950
Michael Chan52c0fd82006-06-29 20:15:54 -07005951 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Michael Chan7f62ad52007-02-20 23:25:40 -08005952 (tp->tg3_flags2 & TG3_FLG2_TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00005953 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07005954
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
5956 TXD_FLAG_CPU_POST_DMA);
5957
Linus Torvalds1da177e2005-04-16 15:20:36 -07005958 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005959 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005960 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005961 } else
5962 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
5963 iph->daddr, 0,
5964 IPPROTO_TCP,
5965 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005966
Matt Carlson615774f2009-11-13 13:03:39 +00005967 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) {
5968 mss |= (hdr_len & 0xc) << 12;
5969 if (hdr_len & 0x10)
5970 base_flags |= 0x00000010;
5971 base_flags |= (hdr_len & 0x3e0) << 5;
5972 } else if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_2)
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005973 mss |= hdr_len << 9;
5974 else if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_1) ||
5975 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005976 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977 int tsflags;
5978
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005979 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005980 mss |= (tsflags << 11);
5981 }
5982 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005983 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005984 int tsflags;
5985
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005986 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005987 base_flags |= tsflags << 12;
5988 }
5989 }
5990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991#if TG3_VLAN_TAG_USED
Jesse Grosseab6d182010-10-20 13:56:03 +00005992 if (vlan_tx_tag_present(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993 base_flags |= (TXD_FLAG_VLAN |
5994 (vlan_tx_tag_get(skb) << 16));
5995#endif
5996
Matt Carlsonb703df62009-12-03 08:36:21 +00005997 if ((tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) &&
Matt Carlson615774f2009-11-13 13:03:39 +00005998 !mss && skb->len > ETH_DATA_LEN)
5999 base_flags |= TXD_FLAG_JMB_PKT;
6000
Alexander Duyckf4188d82009-12-02 16:48:38 +00006001 len = skb_headlen(skb);
6002
6003 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
6004 if (pci_dma_mapping_error(tp->pdev, mapping)) {
David S. Miller90079ce2008-09-11 04:52:51 -07006005 dev_kfree_skb(skb);
6006 goto out_unlock;
6007 }
6008
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006009 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006010 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006011
6012 would_hit_hwbug = 0;
6013
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006014 if ((tp->tg3_flags3 & TG3_FLG3_SHORT_DMA_BUG) && len <= 8)
6015 would_hit_hwbug = 1;
6016
Matt Carlson0e1406d2009-11-02 12:33:33 +00006017 if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
6018 tg3_4g_overflow_test(mapping, len))
Matt Carlson41588ba2008-04-19 18:12:33 -07006019 would_hit_hwbug = 1;
Matt Carlson0e1406d2009-11-02 12:33:33 +00006020
6021 if ((tp->tg3_flags3 & TG3_FLG3_40BIT_DMA_LIMIT_BUG) &&
6022 tg3_40bit_overflow_test(tp, mapping, len))
6023 would_hit_hwbug = 1;
6024
6025 if (tp->tg3_flags3 & TG3_FLG3_5701_DMA_BUG)
Michael Chanc58ec932005-09-17 00:46:27 -07006026 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006028 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006029 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
6030
6031 entry = NEXT_TX(entry);
6032
6033 /* Now loop through additional data fragments, and queue them. */
6034 if (skb_shinfo(skb)->nr_frags > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006035 last = skb_shinfo(skb)->nr_frags - 1;
6036 for (i = 0; i <= last; i++) {
6037 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6038
6039 len = frag->size;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006040 mapping = pci_map_page(tp->pdev,
6041 frag->page,
6042 frag->page_offset,
6043 len, PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006045 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006046 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00006047 mapping);
6048 if (pci_dma_mapping_error(tp->pdev, mapping))
6049 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006050
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006051 if ((tp->tg3_flags3 & TG3_FLG3_SHORT_DMA_BUG) &&
6052 len <= 8)
6053 would_hit_hwbug = 1;
6054
Matt Carlson0e1406d2009-11-02 12:33:33 +00006055 if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
6056 tg3_4g_overflow_test(mapping, len))
Michael Chanc58ec932005-09-17 00:46:27 -07006057 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058
Matt Carlson0e1406d2009-11-02 12:33:33 +00006059 if ((tp->tg3_flags3 & TG3_FLG3_40BIT_DMA_LIMIT_BUG) &&
6060 tg3_40bit_overflow_test(tp, mapping, len))
Michael Chan72f2afb2006-03-06 19:28:35 -08006061 would_hit_hwbug = 1;
6062
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006064 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006065 base_flags, (i == last)|(mss << 1));
6066 else
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006067 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068 base_flags, (i == last));
6069
6070 entry = NEXT_TX(entry);
6071 }
6072 }
6073
6074 if (would_hit_hwbug) {
6075 u32 last_plus_one = entry;
6076 u32 start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006077
Michael Chanc58ec932005-09-17 00:46:27 -07006078 start = entry - 1 - skb_shinfo(skb)->nr_frags;
6079 start &= (TG3_TX_RING_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080
6081 /* If the workaround fails due to memory/mapping
6082 * failure, silently drop this packet.
6083 */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006084 if (tigon3_dma_hwbug_workaround(tnapi, skb, last_plus_one,
Michael Chanc58ec932005-09-17 00:46:27 -07006085 &start, base_flags, mss))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086 goto out_unlock;
6087
6088 entry = start;
6089 }
6090
6091 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006092 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006093
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006094 tnapi->tx_prod = entry;
6095 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006096 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006097
6098 /* netif_tx_stop_queue() must be done before checking
6099 * checking tx index in tg3_tx_avail() below, because in
6100 * tg3_tx(), we update tx index before checking for
6101 * netif_tx_queue_stopped().
6102 */
6103 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006104 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006105 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07006106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006107
6108out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00006109 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006110
6111 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006112
6113dma_error:
6114 last = i;
6115 entry = tnapi->tx_prod;
6116 tnapi->tx_buffers[entry].skb = NULL;
6117 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006118 dma_unmap_addr(&tnapi->tx_buffers[entry], mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006119 skb_headlen(skb),
6120 PCI_DMA_TODEVICE);
6121 for (i = 0; i <= last; i++) {
6122 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6123 entry = NEXT_TX(entry);
6124
6125 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006126 dma_unmap_addr(&tnapi->tx_buffers[entry],
Alexander Duyckf4188d82009-12-02 16:48:38 +00006127 mapping),
6128 frag->size, PCI_DMA_TODEVICE);
6129 }
6130
6131 dev_kfree_skb(skb);
6132 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006133}
6134
6135static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
6136 int new_mtu)
6137{
6138 dev->mtu = new_mtu;
6139
Michael Chanef7f5ec2005-07-25 12:32:25 -07006140 if (new_mtu > ETH_DATA_LEN) {
Michael Chana4e2b342005-10-26 15:46:52 -07006141 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
Michael Chanef7f5ec2005-07-25 12:32:25 -07006142 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
6143 ethtool_op_set_tso(dev, 0);
Matt Carlson859a5882010-04-05 10:19:28 +00006144 } else {
Michael Chanef7f5ec2005-07-25 12:32:25 -07006145 tp->tg3_flags |= TG3_FLAG_JUMBO_RING_ENABLE;
Matt Carlson859a5882010-04-05 10:19:28 +00006146 }
Michael Chanef7f5ec2005-07-25 12:32:25 -07006147 } else {
Michael Chana4e2b342005-10-26 15:46:52 -07006148 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
Michael Chanef7f5ec2005-07-25 12:32:25 -07006149 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
Michael Chan0f893dc2005-07-25 12:30:38 -07006150 tp->tg3_flags &= ~TG3_FLAG_JUMBO_RING_ENABLE;
Michael Chanef7f5ec2005-07-25 12:32:25 -07006151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006152}
6153
6154static int tg3_change_mtu(struct net_device *dev, int new_mtu)
6155{
6156 struct tg3 *tp = netdev_priv(dev);
Michael Chanb9ec6c12006-07-25 16:37:27 -07006157 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006158
6159 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
6160 return -EINVAL;
6161
6162 if (!netif_running(dev)) {
6163 /* We'll just catch it later when the
6164 * device is up'd.
6165 */
6166 tg3_set_mtu(dev, tp, new_mtu);
6167 return 0;
6168 }
6169
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006170 tg3_phy_stop(tp);
6171
Linus Torvalds1da177e2005-04-16 15:20:36 -07006172 tg3_netif_stop(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07006173
6174 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006175
Michael Chan944d9802005-05-29 14:57:48 -07006176 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006177
6178 tg3_set_mtu(dev, tp, new_mtu);
6179
Michael Chanb9ec6c12006-07-25 16:37:27 -07006180 err = tg3_restart_hw(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006181
Michael Chanb9ec6c12006-07-25 16:37:27 -07006182 if (!err)
6183 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006184
David S. Millerf47c11e2005-06-24 20:18:35 -07006185 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006186
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006187 if (!err)
6188 tg3_phy_start(tp);
6189
Michael Chanb9ec6c12006-07-25 16:37:27 -07006190 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006191}
6192
Matt Carlson21f581a2009-08-28 14:00:25 +00006193static void tg3_rx_prodring_free(struct tg3 *tp,
6194 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006195{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006196 int i;
6197
Matt Carlson8fea32b2010-09-15 08:59:58 +00006198 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006199 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00006200 i = (i + 1) & tp->rx_std_ring_mask)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006201 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6202 tp->rx_pkt_map_sz);
6203
6204 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
6205 for (i = tpr->rx_jmb_cons_idx;
6206 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00006207 i = (i + 1) & tp->rx_jmb_ring_mask) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006208 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6209 TG3_RX_JMB_MAP_SZ);
6210 }
6211 }
6212
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006213 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006215
Matt Carlson2c49a442010-09-30 10:34:35 +00006216 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006217 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6218 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006219
Matt Carlson48035722010-10-14 10:37:43 +00006220 if ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) &&
6221 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006222 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006223 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6224 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006225 }
6226}
6227
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006228/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006229 *
6230 * The chip has been shut down and the driver detached from
6231 * the networking, so no interrupts or new tx packets will
6232 * end up in the driver. tp->{tx,}lock are held and thus
6233 * we may not sleep.
6234 */
Matt Carlson21f581a2009-08-28 14:00:25 +00006235static int tg3_rx_prodring_alloc(struct tg3 *tp,
6236 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006237{
Matt Carlson287be122009-08-28 13:58:46 +00006238 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006239
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006240 tpr->rx_std_cons_idx = 0;
6241 tpr->rx_std_prod_idx = 0;
6242 tpr->rx_jmb_cons_idx = 0;
6243 tpr->rx_jmb_prod_idx = 0;
6244
Matt Carlson8fea32b2010-09-15 08:59:58 +00006245 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006246 memset(&tpr->rx_std_buffers[0], 0,
6247 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00006248 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006249 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00006250 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006251 goto done;
6252 }
6253
Linus Torvalds1da177e2005-04-16 15:20:36 -07006254 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00006255 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006256
Matt Carlson287be122009-08-28 13:58:46 +00006257 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Michael Chana4e2b342005-10-26 15:46:52 -07006258 if ((tp->tg3_flags2 & TG3_FLG2_5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00006259 tp->dev->mtu > ETH_DATA_LEN)
6260 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
6261 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07006262
Linus Torvalds1da177e2005-04-16 15:20:36 -07006263 /* Initialize invariants of the rings, we only set this
6264 * stuff once. This works because the card does not
6265 * write into the rx buffer posting rings.
6266 */
Matt Carlson2c49a442010-09-30 10:34:35 +00006267 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006268 struct tg3_rx_buffer_desc *rxd;
6269
Matt Carlson21f581a2009-08-28 14:00:25 +00006270 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00006271 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006272 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
6273 rxd->opaque = (RXD_OPAQUE_RING_STD |
6274 (i << RXD_OPAQUE_INDEX_SHIFT));
6275 }
6276
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006277 /* Now allocate fresh SKBs for each rx ring. */
6278 for (i = 0; i < tp->rx_pending; i++) {
Matt Carlson86b21e52009-11-13 13:03:45 +00006279 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_STD, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006280 netdev_warn(tp->dev,
6281 "Using a smaller RX standard ring. Only "
6282 "%d out of %d buffers were allocated "
6283 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006284 if (i == 0)
6285 goto initfail;
6286 tp->rx_pending = i;
6287 break;
6288 }
6289 }
6290
Matt Carlson48035722010-10-14 10:37:43 +00006291 if (!(tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) ||
6292 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006293 goto done;
6294
Matt Carlson2c49a442010-09-30 10:34:35 +00006295 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006296
Matt Carlson0d86df82010-02-17 15:17:00 +00006297 if (!(tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE))
6298 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006299
Matt Carlson2c49a442010-09-30 10:34:35 +00006300 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00006301 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006302
Matt Carlson0d86df82010-02-17 15:17:00 +00006303 rxd = &tpr->rx_jmb[i].std;
6304 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
6305 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
6306 RXD_FLAG_JUMBO;
6307 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
6308 (i << RXD_OPAQUE_INDEX_SHIFT));
6309 }
6310
6311 for (i = 0; i < tp->rx_jumbo_pending; i++) {
6312 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_JUMBO, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006313 netdev_warn(tp->dev,
6314 "Using a smaller RX jumbo ring. Only %d "
6315 "out of %d buffers were allocated "
6316 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00006317 if (i == 0)
6318 goto initfail;
6319 tp->rx_jumbo_pending = i;
6320 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006321 }
6322 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006323
6324done:
Michael Chan32d8c572006-07-25 16:38:29 -07006325 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006326
6327initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00006328 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006329 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330}
6331
Matt Carlson21f581a2009-08-28 14:00:25 +00006332static void tg3_rx_prodring_fini(struct tg3 *tp,
6333 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006334{
Matt Carlson21f581a2009-08-28 14:00:25 +00006335 kfree(tpr->rx_std_buffers);
6336 tpr->rx_std_buffers = NULL;
6337 kfree(tpr->rx_jmb_buffers);
6338 tpr->rx_jmb_buffers = NULL;
6339 if (tpr->rx_std) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006340 pci_free_consistent(tp->pdev, TG3_RX_STD_RING_BYTES(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00006341 tpr->rx_std, tpr->rx_std_mapping);
6342 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006343 }
Matt Carlson21f581a2009-08-28 14:00:25 +00006344 if (tpr->rx_jmb) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006345 pci_free_consistent(tp->pdev, TG3_RX_JMB_RING_BYTES(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00006346 tpr->rx_jmb, tpr->rx_jmb_mapping);
6347 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006348 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006349}
6350
Matt Carlson21f581a2009-08-28 14:00:25 +00006351static int tg3_rx_prodring_init(struct tg3 *tp,
6352 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006353{
Matt Carlson2c49a442010-09-30 10:34:35 +00006354 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
6355 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006356 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006357 return -ENOMEM;
6358
Matt Carlson2c49a442010-09-30 10:34:35 +00006359 tpr->rx_std = pci_alloc_consistent(tp->pdev, TG3_RX_STD_RING_BYTES(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00006360 &tpr->rx_std_mapping);
6361 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006362 goto err_out;
6363
Matt Carlson48035722010-10-14 10:37:43 +00006364 if ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) &&
6365 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006366 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00006367 GFP_KERNEL);
6368 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006369 goto err_out;
6370
Matt Carlson21f581a2009-08-28 14:00:25 +00006371 tpr->rx_jmb = pci_alloc_consistent(tp->pdev,
Matt Carlson2c49a442010-09-30 10:34:35 +00006372 TG3_RX_JMB_RING_BYTES(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00006373 &tpr->rx_jmb_mapping);
6374 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006375 goto err_out;
6376 }
6377
6378 return 0;
6379
6380err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00006381 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006382 return -ENOMEM;
6383}
6384
6385/* Free up pending packets in all rx/tx rings.
6386 *
6387 * The chip has been shut down and the driver detached from
6388 * the networking, so no interrupts or new tx packets will
6389 * end up in the driver. tp->{tx,}lock is not held and we are not
6390 * in an interrupt context and thus may sleep.
6391 */
6392static void tg3_free_rings(struct tg3 *tp)
6393{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006394 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006395
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006396 for (j = 0; j < tp->irq_cnt; j++) {
6397 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006398
Matt Carlson8fea32b2010-09-15 08:59:58 +00006399 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00006400
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006401 if (!tnapi->tx_buffers)
6402 continue;
6403
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006404 for (i = 0; i < TG3_TX_RING_SIZE; ) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00006405 struct ring_info *txp;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006406 struct sk_buff *skb;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006407 unsigned int k;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006408
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006409 txp = &tnapi->tx_buffers[i];
6410 skb = txp->skb;
6411
6412 if (skb == NULL) {
6413 i++;
6414 continue;
6415 }
6416
Alexander Duyckf4188d82009-12-02 16:48:38 +00006417 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006418 dma_unmap_addr(txp, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006419 skb_headlen(skb),
6420 PCI_DMA_TODEVICE);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006421 txp->skb = NULL;
6422
Alexander Duyckf4188d82009-12-02 16:48:38 +00006423 i++;
6424
6425 for (k = 0; k < skb_shinfo(skb)->nr_frags; k++) {
6426 txp = &tnapi->tx_buffers[i & (TG3_TX_RING_SIZE - 1)];
6427 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006428 dma_unmap_addr(txp, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006429 skb_shinfo(skb)->frags[k].size,
6430 PCI_DMA_TODEVICE);
6431 i++;
6432 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006433
6434 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006435 }
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006436 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006437}
6438
6439/* Initialize tx/rx rings for packet processing.
6440 *
6441 * The chip has been shut down and the driver detached from
6442 * the networking, so no interrupts or new tx packets will
6443 * end up in the driver. tp->{tx,}lock are held and thus
6444 * we may not sleep.
6445 */
6446static int tg3_init_rings(struct tg3 *tp)
6447{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006448 int i;
Matt Carlson72334482009-08-28 14:03:01 +00006449
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006450 /* Free up all the SKBs. */
6451 tg3_free_rings(tp);
6452
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006453 for (i = 0; i < tp->irq_cnt; i++) {
6454 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006455
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006456 tnapi->last_tag = 0;
6457 tnapi->last_irq_tag = 0;
6458 tnapi->hw_status->status = 0;
6459 tnapi->hw_status->status_tag = 0;
6460 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6461
6462 tnapi->tx_prod = 0;
6463 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006464 if (tnapi->tx_ring)
6465 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006466
6467 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006468 if (tnapi->rx_rcb)
6469 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006470
Matt Carlson8fea32b2010-09-15 08:59:58 +00006471 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00006472 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006473 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006474 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006475 }
Matt Carlson72334482009-08-28 14:03:01 +00006476
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006477 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006478}
6479
6480/*
6481 * Must not be invoked with interrupt sources disabled and
6482 * the hardware shutdown down.
6483 */
6484static void tg3_free_consistent(struct tg3 *tp)
6485{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006486 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006487
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006488 for (i = 0; i < tp->irq_cnt; i++) {
6489 struct tg3_napi *tnapi = &tp->napi[i];
6490
6491 if (tnapi->tx_ring) {
6492 pci_free_consistent(tp->pdev, TG3_TX_RING_BYTES,
6493 tnapi->tx_ring, tnapi->tx_desc_mapping);
6494 tnapi->tx_ring = NULL;
6495 }
6496
6497 kfree(tnapi->tx_buffers);
6498 tnapi->tx_buffers = NULL;
6499
6500 if (tnapi->rx_rcb) {
6501 pci_free_consistent(tp->pdev, TG3_RX_RCB_RING_BYTES(tp),
6502 tnapi->rx_rcb,
6503 tnapi->rx_rcb_mapping);
6504 tnapi->rx_rcb = NULL;
6505 }
6506
Matt Carlson8fea32b2010-09-15 08:59:58 +00006507 tg3_rx_prodring_fini(tp, &tnapi->prodring);
6508
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006509 if (tnapi->hw_status) {
6510 pci_free_consistent(tp->pdev, TG3_HW_STATUS_SIZE,
6511 tnapi->hw_status,
6512 tnapi->status_mapping);
6513 tnapi->hw_status = NULL;
6514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006515 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006516
Linus Torvalds1da177e2005-04-16 15:20:36 -07006517 if (tp->hw_stats) {
6518 pci_free_consistent(tp->pdev, sizeof(struct tg3_hw_stats),
6519 tp->hw_stats, tp->stats_mapping);
6520 tp->hw_stats = NULL;
6521 }
6522}
6523
6524/*
6525 * Must not be invoked with interrupt sources disabled and
6526 * the hardware shutdown down. Can sleep.
6527 */
6528static int tg3_alloc_consistent(struct tg3 *tp)
6529{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006530 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006531
Linus Torvalds1da177e2005-04-16 15:20:36 -07006532 tp->hw_stats = pci_alloc_consistent(tp->pdev,
6533 sizeof(struct tg3_hw_stats),
6534 &tp->stats_mapping);
6535 if (!tp->hw_stats)
6536 goto err_out;
6537
Linus Torvalds1da177e2005-04-16 15:20:36 -07006538 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6539
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006540 for (i = 0; i < tp->irq_cnt; i++) {
6541 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006542 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006543
6544 tnapi->hw_status = pci_alloc_consistent(tp->pdev,
6545 TG3_HW_STATUS_SIZE,
6546 &tnapi->status_mapping);
6547 if (!tnapi->hw_status)
6548 goto err_out;
6549
6550 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006551 sblk = tnapi->hw_status;
6552
Matt Carlson8fea32b2010-09-15 08:59:58 +00006553 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
6554 goto err_out;
6555
Matt Carlson19cfaec2009-12-03 08:36:20 +00006556 /* If multivector TSS is enabled, vector 0 does not handle
6557 * tx interrupts. Don't allocate any resources for it.
6558 */
6559 if ((!i && !(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)) ||
6560 (i && (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS))) {
6561 tnapi->tx_buffers = kzalloc(sizeof(struct ring_info) *
6562 TG3_TX_RING_SIZE,
6563 GFP_KERNEL);
6564 if (!tnapi->tx_buffers)
6565 goto err_out;
6566
6567 tnapi->tx_ring = pci_alloc_consistent(tp->pdev,
6568 TG3_TX_RING_BYTES,
6569 &tnapi->tx_desc_mapping);
6570 if (!tnapi->tx_ring)
6571 goto err_out;
6572 }
6573
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006574 /*
6575 * When RSS is enabled, the status block format changes
6576 * slightly. The "rx_jumbo_consumer", "reserved",
6577 * and "rx_mini_consumer" members get mapped to the
6578 * other three rx return ring producer indexes.
6579 */
6580 switch (i) {
6581 default:
6582 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
6583 break;
6584 case 2:
6585 tnapi->rx_rcb_prod_idx = &sblk->rx_jumbo_consumer;
6586 break;
6587 case 3:
6588 tnapi->rx_rcb_prod_idx = &sblk->reserved;
6589 break;
6590 case 4:
6591 tnapi->rx_rcb_prod_idx = &sblk->rx_mini_consumer;
6592 break;
6593 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006594
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006595 /*
6596 * If multivector RSS is enabled, vector 0 does not handle
6597 * rx or tx interrupts. Don't allocate any resources for it.
6598 */
6599 if (!i && (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS))
6600 continue;
6601
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006602 tnapi->rx_rcb = pci_alloc_consistent(tp->pdev,
6603 TG3_RX_RCB_RING_BYTES(tp),
6604 &tnapi->rx_rcb_mapping);
6605 if (!tnapi->rx_rcb)
6606 goto err_out;
6607
6608 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006609 }
6610
Linus Torvalds1da177e2005-04-16 15:20:36 -07006611 return 0;
6612
6613err_out:
6614 tg3_free_consistent(tp);
6615 return -ENOMEM;
6616}
6617
6618#define MAX_WAIT_CNT 1000
6619
6620/* To stop a block, clear the enable bit and poll till it
6621 * clears. tp->lock is held.
6622 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006623static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006624{
6625 unsigned int i;
6626 u32 val;
6627
6628 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
6629 switch (ofs) {
6630 case RCVLSC_MODE:
6631 case DMAC_MODE:
6632 case MBFREE_MODE:
6633 case BUFMGR_MODE:
6634 case MEMARB_MODE:
6635 /* We can't enable/disable these bits of the
6636 * 5705/5750, just say success.
6637 */
6638 return 0;
6639
6640 default:
6641 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006642 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006643 }
6644
6645 val = tr32(ofs);
6646 val &= ~enable_bit;
6647 tw32_f(ofs, val);
6648
6649 for (i = 0; i < MAX_WAIT_CNT; i++) {
6650 udelay(100);
6651 val = tr32(ofs);
6652 if ((val & enable_bit) == 0)
6653 break;
6654 }
6655
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006656 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00006657 dev_err(&tp->pdev->dev,
6658 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
6659 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006660 return -ENODEV;
6661 }
6662
6663 return 0;
6664}
6665
6666/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006667static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006668{
6669 int i, err;
6670
6671 tg3_disable_ints(tp);
6672
6673 tp->rx_mode &= ~RX_MODE_ENABLE;
6674 tw32_f(MAC_RX_MODE, tp->rx_mode);
6675 udelay(10);
6676
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006677 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
6678 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
6679 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
6680 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
6681 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
6682 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006683
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006684 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
6685 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
6686 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
6687 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
6688 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
6689 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
6690 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006691
6692 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
6693 tw32_f(MAC_MODE, tp->mac_mode);
6694 udelay(40);
6695
6696 tp->tx_mode &= ~TX_MODE_ENABLE;
6697 tw32_f(MAC_TX_MODE, tp->tx_mode);
6698
6699 for (i = 0; i < MAX_WAIT_CNT; i++) {
6700 udelay(100);
6701 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
6702 break;
6703 }
6704 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00006705 dev_err(&tp->pdev->dev,
6706 "%s timed out, TX_MODE_ENABLE will not clear "
6707 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07006708 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006709 }
6710
Michael Chane6de8ad2005-05-05 14:42:41 -07006711 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006712 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
6713 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006714
6715 tw32(FTQ_RESET, 0xffffffff);
6716 tw32(FTQ_RESET, 0x00000000);
6717
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006718 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
6719 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006720
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006721 for (i = 0; i < tp->irq_cnt; i++) {
6722 struct tg3_napi *tnapi = &tp->napi[i];
6723 if (tnapi->hw_status)
6724 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6725 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006726 if (tp->hw_stats)
6727 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6728
Linus Torvalds1da177e2005-04-16 15:20:36 -07006729 return err;
6730}
6731
Matt Carlson0d3031d2007-10-10 18:02:43 -07006732static void tg3_ape_send_event(struct tg3 *tp, u32 event)
6733{
6734 int i;
6735 u32 apedata;
6736
Matt Carlsondc6d0742010-09-15 08:59:55 +00006737 /* NCSI does not support APE events */
6738 if (tp->tg3_flags3 & TG3_FLG3_APE_HAS_NCSI)
6739 return;
6740
Matt Carlson0d3031d2007-10-10 18:02:43 -07006741 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
6742 if (apedata != APE_SEG_SIG_MAGIC)
6743 return;
6744
6745 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
Matt Carlson731fd792008-08-15 14:07:51 -07006746 if (!(apedata & APE_FW_STATUS_READY))
Matt Carlson0d3031d2007-10-10 18:02:43 -07006747 return;
6748
6749 /* Wait for up to 1 millisecond for APE to service previous event. */
6750 for (i = 0; i < 10; i++) {
6751 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
6752 return;
6753
6754 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
6755
6756 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6757 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
6758 event | APE_EVENT_STATUS_EVENT_PENDING);
6759
6760 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
6761
6762 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6763 break;
6764
6765 udelay(100);
6766 }
6767
6768 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6769 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
6770}
6771
6772static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
6773{
6774 u32 event;
6775 u32 apedata;
6776
6777 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
6778 return;
6779
6780 switch (kind) {
Matt Carlson33f401a2010-04-05 10:19:27 +00006781 case RESET_KIND_INIT:
6782 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
6783 APE_HOST_SEG_SIG_MAGIC);
6784 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
6785 APE_HOST_SEG_LEN_MAGIC);
6786 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
6787 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
6788 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
Matt Carlson6867c842010-07-11 09:31:44 +00006789 APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
Matt Carlson33f401a2010-04-05 10:19:27 +00006790 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
6791 APE_HOST_BEHAV_NO_PHYLOCK);
Matt Carlsondc6d0742010-09-15 08:59:55 +00006792 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
6793 TG3_APE_HOST_DRVR_STATE_START);
Matt Carlson0d3031d2007-10-10 18:02:43 -07006794
Matt Carlson33f401a2010-04-05 10:19:27 +00006795 event = APE_EVENT_STATUS_STATE_START;
6796 break;
6797 case RESET_KIND_SHUTDOWN:
6798 /* With the interface we are currently using,
6799 * APE does not track driver state. Wiping
6800 * out the HOST SEGMENT SIGNATURE forces
6801 * the APE to assume OS absent status.
6802 */
6803 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
Matt Carlsonb2aee152008-11-03 16:51:11 -08006804
Matt Carlsondc6d0742010-09-15 08:59:55 +00006805 if (device_may_wakeup(&tp->pdev->dev) &&
6806 (tp->tg3_flags & TG3_FLAG_WOL_ENABLE)) {
6807 tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
6808 TG3_APE_HOST_WOL_SPEED_AUTO);
6809 apedata = TG3_APE_HOST_DRVR_STATE_WOL;
6810 } else
6811 apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
6812
6813 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
6814
Matt Carlson33f401a2010-04-05 10:19:27 +00006815 event = APE_EVENT_STATUS_STATE_UNLOAD;
6816 break;
6817 case RESET_KIND_SUSPEND:
6818 event = APE_EVENT_STATUS_STATE_SUSPEND;
6819 break;
6820 default:
6821 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -07006822 }
6823
6824 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
6825
6826 tg3_ape_send_event(tp, event);
6827}
6828
Michael Chane6af3012005-04-21 17:12:05 -07006829/* tp->lock is held. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006830static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
6831{
David S. Millerf49639e2006-06-09 11:58:36 -07006832 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
6833 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006834
6835 if (tp->tg3_flags2 & TG3_FLG2_ASF_NEW_HANDSHAKE) {
6836 switch (kind) {
6837 case RESET_KIND_INIT:
6838 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6839 DRV_STATE_START);
6840 break;
6841
6842 case RESET_KIND_SHUTDOWN:
6843 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6844 DRV_STATE_UNLOAD);
6845 break;
6846
6847 case RESET_KIND_SUSPEND:
6848 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6849 DRV_STATE_SUSPEND);
6850 break;
6851
6852 default:
6853 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006855 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07006856
6857 if (kind == RESET_KIND_INIT ||
6858 kind == RESET_KIND_SUSPEND)
6859 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006860}
6861
6862/* tp->lock is held. */
6863static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
6864{
6865 if (tp->tg3_flags2 & TG3_FLG2_ASF_NEW_HANDSHAKE) {
6866 switch (kind) {
6867 case RESET_KIND_INIT:
6868 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6869 DRV_STATE_START_DONE);
6870 break;
6871
6872 case RESET_KIND_SHUTDOWN:
6873 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6874 DRV_STATE_UNLOAD_DONE);
6875 break;
6876
6877 default:
6878 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006880 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07006881
6882 if (kind == RESET_KIND_SHUTDOWN)
6883 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006884}
6885
6886/* tp->lock is held. */
6887static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
6888{
6889 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) {
6890 switch (kind) {
6891 case RESET_KIND_INIT:
6892 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6893 DRV_STATE_START);
6894 break;
6895
6896 case RESET_KIND_SHUTDOWN:
6897 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6898 DRV_STATE_UNLOAD);
6899 break;
6900
6901 case RESET_KIND_SUSPEND:
6902 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6903 DRV_STATE_SUSPEND);
6904 break;
6905
6906 default:
6907 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006909 }
6910}
6911
Michael Chan7a6f4362006-09-27 16:03:31 -07006912static int tg3_poll_fw(struct tg3 *tp)
6913{
6914 int i;
6915 u32 val;
6916
Michael Chanb5d37722006-09-27 16:06:21 -07006917 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Gary Zambrano0ccead12006-11-14 16:34:00 -08006918 /* Wait up to 20ms for init done. */
6919 for (i = 0; i < 200; i++) {
Michael Chanb5d37722006-09-27 16:06:21 -07006920 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
6921 return 0;
Gary Zambrano0ccead12006-11-14 16:34:00 -08006922 udelay(100);
Michael Chanb5d37722006-09-27 16:06:21 -07006923 }
6924 return -ENODEV;
6925 }
6926
Michael Chan7a6f4362006-09-27 16:03:31 -07006927 /* Wait for firmware initialization to complete. */
6928 for (i = 0; i < 100000; i++) {
6929 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
6930 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
6931 break;
6932 udelay(10);
6933 }
6934
6935 /* Chip might not be fitted with firmware. Some Sun onboard
6936 * parts are configured like that. So don't signal the timeout
6937 * of the above loop as an error, but do report the lack of
6938 * running firmware once.
6939 */
6940 if (i >= 100000 &&
6941 !(tp->tg3_flags2 & TG3_FLG2_NO_FWARE_REPORTED)) {
6942 tp->tg3_flags2 |= TG3_FLG2_NO_FWARE_REPORTED;
6943
Joe Perches05dbe002010-02-17 19:44:19 +00006944 netdev_info(tp->dev, "No firmware running\n");
Michael Chan7a6f4362006-09-27 16:03:31 -07006945 }
6946
Matt Carlson6b10c162010-02-12 14:47:08 +00006947 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
6948 /* The 57765 A0 needs a little more
6949 * time to do some important work.
6950 */
6951 mdelay(10);
6952 }
6953
Michael Chan7a6f4362006-09-27 16:03:31 -07006954 return 0;
6955}
6956
Michael Chanee6a99b2007-07-18 21:49:10 -07006957/* Save PCI command register before chip reset */
6958static void tg3_save_pci_state(struct tg3 *tp)
6959{
Matt Carlson8a6eac92007-10-21 16:17:55 -07006960 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07006961}
6962
6963/* Restore PCI state after chip reset */
6964static void tg3_restore_pci_state(struct tg3 *tp)
6965{
6966 u32 val;
6967
6968 /* Re-enable indirect register accesses. */
6969 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
6970 tp->misc_host_ctrl);
6971
6972 /* Set MAX PCI retry to zero. */
6973 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
6974 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
6975 (tp->tg3_flags & TG3_FLAG_PCIX_MODE))
6976 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07006977 /* Allow reads and writes to the APE register and memory space. */
6978 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
6979 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc2010-06-05 17:24:30 +00006980 PCISTATE_ALLOW_APE_SHMEM_WR |
6981 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07006982 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
6983
Matt Carlson8a6eac92007-10-21 16:17:55 -07006984 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07006985
Matt Carlsonfcb389d2008-11-03 16:55:44 -08006986 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785) {
6987 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)
Matt Carlsoncf790032010-11-24 08:31:48 +00006988 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlsonfcb389d2008-11-03 16:55:44 -08006989 else {
6990 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
6991 tp->pci_cacheline_sz);
6992 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
6993 tp->pci_lat_timer);
6994 }
Michael Chan114342f2007-10-15 02:12:26 -07006995 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08006996
Michael Chanee6a99b2007-07-18 21:49:10 -07006997 /* Make sure PCI-X relaxed ordering bit is clear. */
Matt Carlson52f44902008-11-21 17:17:04 -08006998 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
Matt Carlson9974a352007-10-07 23:27:28 -07006999 u16 pcix_cmd;
7000
7001 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7002 &pcix_cmd);
7003 pcix_cmd &= ~PCI_X_CMD_ERO;
7004 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7005 pcix_cmd);
7006 }
Michael Chanee6a99b2007-07-18 21:49:10 -07007007
7008 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007009
7010 /* Chip reset on 5780 will reset MSI enable bit,
7011 * so need to restore it.
7012 */
7013 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
7014 u16 ctrl;
7015
7016 pci_read_config_word(tp->pdev,
7017 tp->msi_cap + PCI_MSI_FLAGS,
7018 &ctrl);
7019 pci_write_config_word(tp->pdev,
7020 tp->msi_cap + PCI_MSI_FLAGS,
7021 ctrl | PCI_MSI_FLAGS_ENABLE);
7022 val = tr32(MSGINT_MODE);
7023 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
7024 }
7025 }
7026}
7027
Linus Torvalds1da177e2005-04-16 15:20:36 -07007028static void tg3_stop_fw(struct tg3 *);
7029
7030/* tp->lock is held. */
7031static int tg3_chip_reset(struct tg3 *tp)
7032{
7033 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07007034 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00007035 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007036
David S. Millerf49639e2006-06-09 11:58:36 -07007037 tg3_nvram_lock(tp);
7038
Matt Carlson77b483f2008-08-15 14:07:24 -07007039 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
7040
David S. Millerf49639e2006-06-09 11:58:36 -07007041 /* No matching tg3_nvram_unlock() after this because
7042 * chip reset below will undo the nvram lock.
7043 */
7044 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007045
Michael Chanee6a99b2007-07-18 21:49:10 -07007046 /* GRC_MISC_CFG core clock reset will clear the memory
7047 * enable bit in PCI register 4 and the MSI enable bit
7048 * on some chips, so we save relevant registers here.
7049 */
7050 tg3_save_pci_state(tp);
7051
Michael Chand9ab5ad2006-03-20 22:27:35 -08007052 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08007053 (tp->tg3_flags3 & TG3_FLG3_5755_PLUS))
Michael Chand9ab5ad2006-03-20 22:27:35 -08007054 tw32(GRC_FASTBOOT_PC, 0);
7055
Linus Torvalds1da177e2005-04-16 15:20:36 -07007056 /*
7057 * We must avoid the readl() that normally takes place.
7058 * It locks machines, causes machine checks, and other
7059 * fun things. So, temporarily disable the 5701
7060 * hardware workaround, while we do the reset.
7061 */
Michael Chan1ee582d2005-08-09 20:16:46 -07007062 write_op = tp->write32;
7063 if (write_op == tg3_write_flush_reg32)
7064 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007065
Michael Chand18edcb2007-03-24 20:57:11 -07007066 /* Prevent the irq handler from reading or writing PCI registers
7067 * during chip reset when the memory enable bit in the PCI command
7068 * register may be cleared. The chip does not generate interrupt
7069 * at this time, but the irq handler may still be called due to irq
7070 * sharing or irqpoll.
7071 */
7072 tp->tg3_flags |= TG3_FLAG_CHIP_RESETTING;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007073 for (i = 0; i < tp->irq_cnt; i++) {
7074 struct tg3_napi *tnapi = &tp->napi[i];
7075 if (tnapi->hw_status) {
7076 tnapi->hw_status->status = 0;
7077 tnapi->hw_status->status_tag = 0;
7078 }
7079 tnapi->last_tag = 0;
7080 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07007081 }
Michael Chand18edcb2007-03-24 20:57:11 -07007082 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00007083
7084 for (i = 0; i < tp->irq_cnt; i++)
7085 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07007086
Matt Carlson255ca312009-08-25 10:07:27 +00007087 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7088 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7089 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
7090 }
7091
Linus Torvalds1da177e2005-04-16 15:20:36 -07007092 /* do the reset */
7093 val = GRC_MISC_CFG_CORECLK_RESET;
7094
7095 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
Matt Carlson88075d92010-08-02 11:25:58 +00007096 /* Force PCIe 1.0a mode */
7097 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
7098 !(tp->tg3_flags3 & TG3_FLG3_5717_PLUS) &&
7099 tr32(TG3_PCIE_PHY_TSTCTL) ==
7100 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
7101 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
7102
Linus Torvalds1da177e2005-04-16 15:20:36 -07007103 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
7104 tw32(GRC_MISC_CFG, (1 << 29));
7105 val |= (1 << 29);
7106 }
7107 }
7108
Michael Chanb5d37722006-09-27 16:06:21 -07007109 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7110 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
7111 tw32(GRC_VCPU_EXT_CTRL,
7112 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
7113 }
7114
Matt Carlsonf37500d2010-08-02 11:25:59 +00007115 /* Manage gphy power for all CPMU absent PCIe devices. */
7116 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
7117 !(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007118 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00007119
Linus Torvalds1da177e2005-04-16 15:20:36 -07007120 tw32(GRC_MISC_CFG, val);
7121
Michael Chan1ee582d2005-08-09 20:16:46 -07007122 /* restore 5701 hardware bug workaround write method */
7123 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007124
7125 /* Unfortunately, we have to delay before the PCI read back.
7126 * Some 575X chips even will not respond to a PCI cfg access
7127 * when the reset command is given to the chip.
7128 *
7129 * How do these hardware designers expect things to work
7130 * properly if the PCI write is posted for a long period
7131 * of time? It is always necessary to have some method by
7132 * which a register read back can occur to push the write
7133 * out which does the reset.
7134 *
7135 * For most tg3 variants the trick below was working.
7136 * Ho hum...
7137 */
7138 udelay(120);
7139
7140 /* Flush PCI posted writes. The normal MMIO registers
7141 * are inaccessible at this time so this is the only
7142 * way to make this reliably (actually, this is no longer
7143 * the case, see above). I tried to use indirect
7144 * register read/write but this upset some 5701 variants.
7145 */
7146 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
7147
7148 udelay(120);
7149
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007150 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) && tp->pcie_cap) {
Matt Carlsone7126992009-08-25 10:08:16 +00007151 u16 val16;
7152
Linus Torvalds1da177e2005-04-16 15:20:36 -07007153 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
7154 int i;
7155 u32 cfg_val;
7156
7157 /* Wait for link training to complete. */
7158 for (i = 0; i < 5000; i++)
7159 udelay(100);
7160
7161 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
7162 pci_write_config_dword(tp->pdev, 0xc4,
7163 cfg_val | (1 << 15));
7164 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007165
Matt Carlsone7126992009-08-25 10:08:16 +00007166 /* Clear the "no snoop" and "relaxed ordering" bits. */
7167 pci_read_config_word(tp->pdev,
7168 tp->pcie_cap + PCI_EXP_DEVCTL,
7169 &val16);
7170 val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
7171 PCI_EXP_DEVCTL_NOSNOOP_EN);
7172 /*
7173 * Older PCIe devices only support the 128 byte
7174 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007175 */
Matt Carlson6de34cb2010-08-02 11:25:55 +00007176 if (!(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT))
Matt Carlsone7126992009-08-25 10:08:16 +00007177 val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007178 pci_write_config_word(tp->pdev,
7179 tp->pcie_cap + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007180 val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007181
Matt Carlsoncf790032010-11-24 08:31:48 +00007182 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007183
7184 /* Clear error status */
7185 pci_write_config_word(tp->pdev,
7186 tp->pcie_cap + PCI_EXP_DEVSTA,
7187 PCI_EXP_DEVSTA_CED |
7188 PCI_EXP_DEVSTA_NFED |
7189 PCI_EXP_DEVSTA_FED |
7190 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007191 }
7192
Michael Chanee6a99b2007-07-18 21:49:10 -07007193 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007194
Michael Chand18edcb2007-03-24 20:57:11 -07007195 tp->tg3_flags &= ~TG3_FLAG_CHIP_RESETTING;
7196
Michael Chanee6a99b2007-07-18 21:49:10 -07007197 val = 0;
7198 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
Michael Chan4cf78e42005-07-25 12:29:19 -07007199 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07007200 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007201
7202 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
7203 tg3_stop_fw(tp);
7204 tw32(0x5000, 0x400);
7205 }
7206
7207 tw32(GRC_MODE, tp->grc_mode);
7208
7209 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007210 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007211
7212 tw32(0xc4, val | (1 << 15));
7213 }
7214
7215 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
7216 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
7217 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
7218 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
7219 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
7220 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7221 }
7222
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007223 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
7224 tp->mac_mode = MAC_MODE_APE_TX_EN |
7225 MAC_MODE_APE_RX_EN |
7226 MAC_MODE_TDE_ENABLE;
7227
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007228 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007229 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
7230 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007231 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007232 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
7233 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007234 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007235 val = 0;
7236
7237 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007238 udelay(40);
7239
Matt Carlson77b483f2008-08-15 14:07:24 -07007240 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
7241
Michael Chan7a6f4362006-09-27 16:03:31 -07007242 err = tg3_poll_fw(tp);
7243 if (err)
7244 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007245
Matt Carlson0a9140c2009-08-28 12:27:50 +00007246 tg3_mdio_start(tp);
7247
Linus Torvalds1da177e2005-04-16 15:20:36 -07007248 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007249 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
7250 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonc885e822010-08-02 11:25:57 +00007251 !(tp->tg3_flags3 & TG3_FLG3_5717_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007252 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007253
7254 tw32(0x7c00, val | (1 << 25));
7255 }
7256
7257 /* Reprobe ASF enable state. */
7258 tp->tg3_flags &= ~TG3_FLAG_ENABLE_ASF;
7259 tp->tg3_flags2 &= ~TG3_FLG2_ASF_NEW_HANDSHAKE;
7260 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
7261 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
7262 u32 nic_cfg;
7263
7264 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
7265 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
7266 tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
Matt Carlson4ba526c2008-08-15 14:10:04 -07007267 tp->last_event_jiffies = jiffies;
John W. Linvillecbf46852005-04-21 17:01:29 -07007268 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007269 tp->tg3_flags2 |= TG3_FLG2_ASF_NEW_HANDSHAKE;
7270 }
7271 }
7272
7273 return 0;
7274}
7275
7276/* tp->lock is held. */
7277static void tg3_stop_fw(struct tg3 *tp)
7278{
Matt Carlson0d3031d2007-10-10 18:02:43 -07007279 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
7280 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07007281 /* Wait for RX cpu to ACK the previous event. */
7282 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007283
7284 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007285
7286 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007287
Matt Carlson7c5026a2008-05-02 16:49:29 -07007288 /* Wait for RX cpu to ACK this event. */
7289 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007290 }
7291}
7292
7293/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07007294static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007295{
7296 int err;
7297
7298 tg3_stop_fw(tp);
7299
Michael Chan944d9802005-05-29 14:57:48 -07007300 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007301
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007302 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007303 err = tg3_chip_reset(tp);
7304
Matt Carlsondaba2a62009-04-20 06:58:52 +00007305 __tg3_set_mac_addr(tp, 0);
7306
Michael Chan944d9802005-05-29 14:57:48 -07007307 tg3_write_sig_legacy(tp, kind);
7308 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007309
7310 if (err)
7311 return err;
7312
7313 return 0;
7314}
7315
Linus Torvalds1da177e2005-04-16 15:20:36 -07007316#define RX_CPU_SCRATCH_BASE 0x30000
7317#define RX_CPU_SCRATCH_SIZE 0x04000
7318#define TX_CPU_SCRATCH_BASE 0x34000
7319#define TX_CPU_SCRATCH_SIZE 0x04000
7320
7321/* tp->lock is held. */
7322static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
7323{
7324 int i;
7325
Eric Sesterhenn5d9428d2006-04-02 13:52:48 +02007326 BUG_ON(offset == TX_CPU_BASE &&
7327 (tp->tg3_flags2 & TG3_FLG2_5705_PLUS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007328
Michael Chanb5d37722006-09-27 16:06:21 -07007329 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7330 u32 val = tr32(GRC_VCPU_EXT_CTRL);
7331
7332 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
7333 return 0;
7334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007335 if (offset == RX_CPU_BASE) {
7336 for (i = 0; i < 10000; i++) {
7337 tw32(offset + CPU_STATE, 0xffffffff);
7338 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7339 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7340 break;
7341 }
7342
7343 tw32(offset + CPU_STATE, 0xffffffff);
7344 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
7345 udelay(10);
7346 } else {
7347 for (i = 0; i < 10000; i++) {
7348 tw32(offset + CPU_STATE, 0xffffffff);
7349 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7350 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7351 break;
7352 }
7353 }
7354
7355 if (i >= 10000) {
Joe Perches05dbe002010-02-17 19:44:19 +00007356 netdev_err(tp->dev, "%s timed out, %s CPU\n",
7357 __func__, offset == RX_CPU_BASE ? "RX" : "TX");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007358 return -ENODEV;
7359 }
Michael Chanec41c7d2006-01-17 02:40:55 -08007360
7361 /* Clear firmware's nvram arbitration. */
7362 if (tp->tg3_flags & TG3_FLAG_NVRAM)
7363 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007364 return 0;
7365}
7366
7367struct fw_info {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007368 unsigned int fw_base;
7369 unsigned int fw_len;
7370 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007371};
7372
7373/* tp->lock is held. */
7374static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base, u32 cpu_scratch_base,
7375 int cpu_scratch_size, struct fw_info *info)
7376{
Michael Chanec41c7d2006-01-17 02:40:55 -08007377 int err, lock_err, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007378 void (*write_op)(struct tg3 *, u32, u32);
7379
7380 if (cpu_base == TX_CPU_BASE &&
7381 (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007382 netdev_err(tp->dev,
7383 "%s: Trying to load TX cpu firmware which is 5705\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007384 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007385 return -EINVAL;
7386 }
7387
7388 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
7389 write_op = tg3_write_mem;
7390 else
7391 write_op = tg3_write_indirect_reg32;
7392
Michael Chan1b628152005-05-29 14:59:49 -07007393 /* It is possible that bootcode is still loading at this point.
7394 * Get the nvram lock first before halting the cpu.
7395 */
Michael Chanec41c7d2006-01-17 02:40:55 -08007396 lock_err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007397 err = tg3_halt_cpu(tp, cpu_base);
Michael Chanec41c7d2006-01-17 02:40:55 -08007398 if (!lock_err)
7399 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007400 if (err)
7401 goto out;
7402
7403 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
7404 write_op(tp, cpu_scratch_base + i, 0);
7405 tw32(cpu_base + CPU_STATE, 0xffffffff);
7406 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007407 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007408 write_op(tp, (cpu_scratch_base +
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007409 (info->fw_base & 0xffff) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07007410 (i * sizeof(u32))),
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007411 be32_to_cpu(info->fw_data[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007412
7413 err = 0;
7414
7415out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007416 return err;
7417}
7418
7419/* tp->lock is held. */
7420static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
7421{
7422 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007423 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007424 int err, i;
7425
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007426 fw_data = (void *)tp->fw->data;
7427
7428 /* Firmware blob starts with version numbers, followed by
7429 start address and length. We are setting complete length.
7430 length = end_address_of_bss - start_address_of_text.
7431 Remainder is the blob to be loaded contiguously
7432 from start address. */
7433
7434 info.fw_base = be32_to_cpu(fw_data[1]);
7435 info.fw_len = tp->fw->size - 12;
7436 info.fw_data = &fw_data[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007437
7438 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
7439 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
7440 &info);
7441 if (err)
7442 return err;
7443
7444 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
7445 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
7446 &info);
7447 if (err)
7448 return err;
7449
7450 /* Now startup only the RX cpu. */
7451 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007452 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007453
7454 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007455 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007456 break;
7457 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7458 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007459 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007460 udelay(1000);
7461 }
7462 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007463 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
7464 "should be %08x\n", __func__,
Joe Perches05dbe002010-02-17 19:44:19 +00007465 tr32(RX_CPU_BASE + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007466 return -ENODEV;
7467 }
7468 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7469 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
7470
7471 return 0;
7472}
7473
Linus Torvalds1da177e2005-04-16 15:20:36 -07007474/* 5705 needs a special version of the TSO firmware. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007475
7476/* tp->lock is held. */
7477static int tg3_load_tso_firmware(struct tg3 *tp)
7478{
7479 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007480 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007481 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
7482 int err, i;
7483
7484 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
7485 return 0;
7486
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007487 fw_data = (void *)tp->fw->data;
7488
7489 /* Firmware blob starts with version numbers, followed by
7490 start address and length. We are setting complete length.
7491 length = end_address_of_bss - start_address_of_text.
7492 Remainder is the blob to be loaded contiguously
7493 from start address. */
7494
7495 info.fw_base = be32_to_cpu(fw_data[1]);
7496 cpu_scratch_size = tp->fw_len;
7497 info.fw_len = tp->fw->size - 12;
7498 info.fw_data = &fw_data[3];
7499
Linus Torvalds1da177e2005-04-16 15:20:36 -07007500 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007501 cpu_base = RX_CPU_BASE;
7502 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007503 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007504 cpu_base = TX_CPU_BASE;
7505 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
7506 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
7507 }
7508
7509 err = tg3_load_firmware_cpu(tp, cpu_base,
7510 cpu_scratch_base, cpu_scratch_size,
7511 &info);
7512 if (err)
7513 return err;
7514
7515 /* Now startup the cpu. */
7516 tw32(cpu_base + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007517 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007518
7519 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007520 if (tr32(cpu_base + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007521 break;
7522 tw32(cpu_base + CPU_STATE, 0xffffffff);
7523 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007524 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007525 udelay(1000);
7526 }
7527 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007528 netdev_err(tp->dev,
7529 "%s fails to set CPU PC, is %08x should be %08x\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007530 __func__, tr32(cpu_base + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007531 return -ENODEV;
7532 }
7533 tw32(cpu_base + CPU_STATE, 0xffffffff);
7534 tw32_f(cpu_base + CPU_MODE, 0x00000000);
7535 return 0;
7536}
7537
Linus Torvalds1da177e2005-04-16 15:20:36 -07007538
Linus Torvalds1da177e2005-04-16 15:20:36 -07007539static int tg3_set_mac_addr(struct net_device *dev, void *p)
7540{
7541 struct tg3 *tp = netdev_priv(dev);
7542 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07007543 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007544
Michael Chanf9804dd2005-09-27 12:13:10 -07007545 if (!is_valid_ether_addr(addr->sa_data))
7546 return -EINVAL;
7547
Linus Torvalds1da177e2005-04-16 15:20:36 -07007548 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7549
Michael Chane75f7c92006-03-20 21:33:26 -08007550 if (!netif_running(dev))
7551 return 0;
7552
Michael Chan58712ef2006-04-29 18:58:01 -07007553 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) {
Michael Chan986e0ae2007-05-05 12:10:20 -07007554 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07007555
Michael Chan986e0ae2007-05-05 12:10:20 -07007556 addr0_high = tr32(MAC_ADDR_0_HIGH);
7557 addr0_low = tr32(MAC_ADDR_0_LOW);
7558 addr1_high = tr32(MAC_ADDR_1_HIGH);
7559 addr1_low = tr32(MAC_ADDR_1_LOW);
7560
7561 /* Skip MAC addr 1 if ASF is using it. */
7562 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
7563 !(addr1_high == 0 && addr1_low == 0))
7564 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07007565 }
Michael Chan986e0ae2007-05-05 12:10:20 -07007566 spin_lock_bh(&tp->lock);
7567 __tg3_set_mac_addr(tp, skip_mac_1);
7568 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007569
Michael Chanb9ec6c12006-07-25 16:37:27 -07007570 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007571}
7572
7573/* tp->lock is held. */
7574static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
7575 dma_addr_t mapping, u32 maxlen_flags,
7576 u32 nic_addr)
7577{
7578 tg3_write_mem(tp,
7579 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
7580 ((u64) mapping >> 32));
7581 tg3_write_mem(tp,
7582 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
7583 ((u64) mapping & 0xffffffff));
7584 tg3_write_mem(tp,
7585 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
7586 maxlen_flags);
7587
7588 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7589 tg3_write_mem(tp,
7590 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
7591 nic_addr);
7592}
7593
7594static void __tg3_set_rx_mode(struct net_device *);
Michael Chand244c892005-07-05 14:42:33 -07007595static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07007596{
Matt Carlsonb6080e12009-09-01 13:12:00 +00007597 int i;
7598
Matt Carlson19cfaec2009-12-03 08:36:20 +00007599 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007600 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
7601 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
7602 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007603 } else {
7604 tw32(HOSTCC_TXCOL_TICKS, 0);
7605 tw32(HOSTCC_TXMAX_FRAMES, 0);
7606 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007607 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007608
Matt Carlson20d73752010-07-11 09:31:41 +00007609 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00007610 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
7611 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
7612 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
7613 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007614 tw32(HOSTCC_RXCOL_TICKS, 0);
7615 tw32(HOSTCC_RXMAX_FRAMES, 0);
7616 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07007617 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007618
David S. Miller15f98502005-05-18 22:49:26 -07007619 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7620 u32 val = ec->stats_block_coalesce_usecs;
7621
Matt Carlsonb6080e12009-09-01 13:12:00 +00007622 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
7623 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
7624
David S. Miller15f98502005-05-18 22:49:26 -07007625 if (!netif_carrier_ok(tp->dev))
7626 val = 0;
7627
7628 tw32(HOSTCC_STAT_COAL_TICKS, val);
7629 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007630
7631 for (i = 0; i < tp->irq_cnt - 1; i++) {
7632 u32 reg;
7633
7634 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
7635 tw32(reg, ec->rx_coalesce_usecs);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007636 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
7637 tw32(reg, ec->rx_max_coalesced_frames);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007638 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
7639 tw32(reg, ec->rx_max_coalesced_frames_irq);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007640
7641 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS) {
7642 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
7643 tw32(reg, ec->tx_coalesce_usecs);
7644 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
7645 tw32(reg, ec->tx_max_coalesced_frames);
7646 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
7647 tw32(reg, ec->tx_max_coalesced_frames_irq);
7648 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007649 }
7650
7651 for (; i < tp->irq_max - 1; i++) {
7652 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007653 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007654 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007655
7656 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS) {
7657 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
7658 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
7659 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
7660 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007661 }
David S. Miller15f98502005-05-18 22:49:26 -07007662}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007663
7664/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00007665static void tg3_rings_reset(struct tg3 *tp)
7666{
7667 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007668 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007669 struct tg3_napi *tnapi = &tp->napi[0];
7670
7671 /* Disable all transmit rings but the first. */
7672 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7673 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlson3d377282010-10-14 10:37:39 +00007674 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
7675 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
7676 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
Matt Carlsonb703df62009-12-03 08:36:21 +00007677 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
7678 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007679 else
7680 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7681
7682 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7683 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
7684 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
7685 BDINFO_FLAGS_DISABLED);
7686
7687
7688 /* Disable all receive return rings but the first. */
Matt Carlsona50d0792010-06-05 17:24:37 +00007689 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
7690 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007691 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
7692 else if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00007693 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00007694 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
7695 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson2d31eca2009-09-01 12:53:31 +00007696 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
7697 else
7698 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7699
7700 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7701 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
7702 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
7703 BDINFO_FLAGS_DISABLED);
7704
7705 /* Disable interrupts */
7706 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
7707
7708 /* Zero mailbox registers. */
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007709 if (tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00007710 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007711 tp->napi[i].tx_prod = 0;
7712 tp->napi[i].tx_cons = 0;
Matt Carlsonc2353a32010-01-20 16:58:08 +00007713 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
7714 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007715 tw32_rx_mbox(tp->napi[i].consmbox, 0);
7716 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
7717 }
Matt Carlsonc2353a32010-01-20 16:58:08 +00007718 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS))
7719 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007720 } else {
7721 tp->napi[0].tx_prod = 0;
7722 tp->napi[0].tx_cons = 0;
7723 tw32_mailbox(tp->napi[0].prodmbox, 0);
7724 tw32_rx_mbox(tp->napi[0].consmbox, 0);
7725 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007726
7727 /* Make sure the NIC-based send BD rings are disabled. */
7728 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7729 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
7730 for (i = 0; i < 16; i++)
7731 tw32_tx_mbox(mbox + i * 8, 0);
7732 }
7733
7734 txrcb = NIC_SRAM_SEND_RCB;
7735 rxrcb = NIC_SRAM_RCV_RET_RCB;
7736
7737 /* Clear status block in ram. */
7738 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7739
7740 /* Set status block DMA address */
7741 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
7742 ((u64) tnapi->status_mapping >> 32));
7743 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
7744 ((u64) tnapi->status_mapping & 0xffffffff));
7745
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007746 if (tnapi->tx_ring) {
7747 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7748 (TG3_TX_RING_SIZE <<
7749 BDINFO_FLAGS_MAXLEN_SHIFT),
7750 NIC_SRAM_TX_BUFFER_DESC);
7751 txrcb += TG3_BDINFO_SIZE;
7752 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007753
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007754 if (tnapi->rx_rcb) {
7755 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00007756 (tp->rx_ret_ring_mask + 1) <<
7757 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007758 rxrcb += TG3_BDINFO_SIZE;
7759 }
7760
7761 stblk = HOSTCC_STATBLCK_RING1;
7762
7763 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
7764 u64 mapping = (u64)tnapi->status_mapping;
7765 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
7766 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
7767
7768 /* Clear status block in ram. */
7769 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7770
Matt Carlson19cfaec2009-12-03 08:36:20 +00007771 if (tnapi->tx_ring) {
7772 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7773 (TG3_TX_RING_SIZE <<
7774 BDINFO_FLAGS_MAXLEN_SHIFT),
7775 NIC_SRAM_TX_BUFFER_DESC);
7776 txrcb += TG3_BDINFO_SIZE;
7777 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007778
7779 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00007780 ((tp->rx_ret_ring_mask + 1) <<
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007781 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
7782
7783 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007784 rxrcb += TG3_BDINFO_SIZE;
7785 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007786}
7787
7788/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07007789static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007790{
7791 u32 val, rdmac_mode;
7792 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00007793 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007794
7795 tg3_disable_ints(tp);
7796
7797 tg3_stop_fw(tp);
7798
7799 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
7800
Matt Carlson859a5882010-04-05 10:19:28 +00007801 if (tp->tg3_flags & TG3_FLAG_INIT_COMPLETE)
Michael Chane6de8ad2005-05-05 14:42:41 -07007802 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007803
Matt Carlson603f1172010-02-12 14:47:10 +00007804 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08007805 tg3_phy_reset(tp);
7806
Linus Torvalds1da177e2005-04-16 15:20:36 -07007807 err = tg3_chip_reset(tp);
7808 if (err)
7809 return err;
7810
7811 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
7812
Matt Carlsonbcb37f62008-11-03 16:52:09 -08007813 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07007814 val = tr32(TG3_CPMU_CTRL);
7815 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
7816 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08007817
7818 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
7819 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
7820 val |= CPMU_LSPD_10MB_MACCLK_6_25;
7821 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
7822
7823 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
7824 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
7825 val |= CPMU_LNK_AWARE_MACCLK_6_25;
7826 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
7827
7828 val = tr32(TG3_CPMU_HST_ACC);
7829 val &= ~CPMU_HST_ACC_MACCLK_MASK;
7830 val |= CPMU_HST_ACC_MACCLK_6_25;
7831 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07007832 }
7833
Matt Carlson33466d92009-04-20 06:57:41 +00007834 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7835 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
7836 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
7837 PCIE_PWR_MGMT_L1_THRESH_4MS;
7838 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00007839
7840 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
7841 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
7842
7843 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00007844
Matt Carlsonf40386c2009-11-02 14:24:02 +00007845 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7846 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00007847 }
7848
Matt Carlson614b05902010-01-20 16:58:02 +00007849 if (tp->tg3_flags3 & TG3_FLG3_L1PLLPD_EN) {
7850 u32 grc_mode = tr32(GRC_MODE);
7851
7852 /* Access the lower 1K of PL PCIE block registers. */
7853 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
7854 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
7855
7856 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
7857 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
7858 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
7859
7860 tw32(GRC_MODE, grc_mode);
7861 }
7862
Matt Carlson5093eed2010-11-24 08:31:45 +00007863 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
7864 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
7865 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00007866
Matt Carlson5093eed2010-11-24 08:31:45 +00007867 /* Access the lower 1K of PL PCIE block registers. */
7868 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
7869 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00007870
Matt Carlson5093eed2010-11-24 08:31:45 +00007871 val = tr32(TG3_PCIE_TLDLPL_PORT +
7872 TG3_PCIE_PL_LO_PHYCTL5);
7873 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
7874 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00007875
Matt Carlson5093eed2010-11-24 08:31:45 +00007876 tw32(GRC_MODE, grc_mode);
7877 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00007878
7879 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
7880 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
7881 val |= CPMU_LSPD_10MB_MACCLK_6_25;
7882 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00007883 }
7884
Matt Carlson52b02d02010-10-14 10:37:41 +00007885 /* Enable MAC control of LPI */
7886 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
7887 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL,
7888 TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
7889 TG3_CPMU_EEE_LNKIDL_UART_IDL);
7890
7891 tw32_f(TG3_CPMU_EEE_CTRL,
7892 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
7893
7894 tw32_f(TG3_CPMU_EEE_MODE,
7895 TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
7896 TG3_CPMU_EEEMD_LPI_IN_TX |
7897 TG3_CPMU_EEEMD_LPI_IN_RX |
7898 TG3_CPMU_EEEMD_EEE_ENABLE);
7899 }
7900
Linus Torvalds1da177e2005-04-16 15:20:36 -07007901 /* This works around an issue with Athlon chipsets on
7902 * B3 tigon3 silicon. This bit has no effect on any
7903 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07007904 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007905 */
Matt Carlson795d01c2007-10-07 23:28:17 -07007906 if (!(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT)) {
7907 if (!(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
7908 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
7909 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007911
7912 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
7913 (tp->tg3_flags & TG3_FLAG_PCIX_MODE)) {
7914 val = tr32(TG3PCI_PCISTATE);
7915 val |= PCISTATE_RETRY_SAME_DMA;
7916 tw32(TG3PCI_PCISTATE, val);
7917 }
7918
Matt Carlson0d3031d2007-10-10 18:02:43 -07007919 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
7920 /* Allow reads and writes to the
7921 * APE register and memory space.
7922 */
7923 val = tr32(TG3PCI_PCISTATE);
7924 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc2010-06-05 17:24:30 +00007925 PCISTATE_ALLOW_APE_SHMEM_WR |
7926 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07007927 tw32(TG3PCI_PCISTATE, val);
7928 }
7929
Linus Torvalds1da177e2005-04-16 15:20:36 -07007930 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
7931 /* Enable some hw fixes. */
7932 val = tr32(TG3PCI_MSI_DATA);
7933 val |= (1 << 26) | (1 << 28) | (1 << 29);
7934 tw32(TG3PCI_MSI_DATA, val);
7935 }
7936
7937 /* Descriptor ring init may make accesses to the
7938 * NIC SRAM area to setup the TX descriptors, so we
7939 * can only do this after the hardware has been
7940 * successfully reset.
7941 */
Michael Chan32d8c572006-07-25 16:38:29 -07007942 err = tg3_init_rings(tp);
7943 if (err)
7944 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007945
Matt Carlsonc885e822010-08-02 11:25:57 +00007946 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00007947 val = tr32(TG3PCI_DMA_RW_CTRL) &
7948 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Matt Carlson1a319022010-04-12 06:58:25 +00007949 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
7950 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00007951 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
7952 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
7953 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07007954 /* This value is determined during the probe time DMA
7955 * engine test, tg3_test_dma.
7956 */
7957 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
7958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007959
7960 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
7961 GRC_MODE_4X_NIC_SEND_RINGS |
7962 GRC_MODE_NO_TX_PHDR_CSUM |
7963 GRC_MODE_NO_RX_PHDR_CSUM);
7964 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07007965
7966 /* Pseudo-header checksum is done by hardware logic and not
7967 * the offload processers, so make the chip do the pseudo-
7968 * header checksums on receive. For transmit it is more
7969 * convenient to do the pseudo-header checksum in software
7970 * as Linux does that on transmit for us in all cases.
7971 */
7972 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007973
7974 tw32(GRC_MODE,
7975 tp->grc_mode |
7976 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
7977
7978 /* Setup the timer prescalar register. Clock is always 66Mhz. */
7979 val = tr32(GRC_MISC_CFG);
7980 val &= ~0xff;
7981 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
7982 tw32(GRC_MISC_CFG, val);
7983
7984 /* Initialize MBUF/DESC pool. */
John W. Linvillecbf46852005-04-21 17:01:29 -07007985 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007986 /* Do nothing. */
7987 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
7988 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
7989 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
7990 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
7991 else
7992 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
7993 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
7994 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Matt Carlson859a5882010-04-05 10:19:28 +00007995 } else if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007996 int fw_len;
7997
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007998 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007999 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
8000 tw32(BUFMGR_MB_POOL_ADDR,
8001 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
8002 tw32(BUFMGR_MB_POOL_SIZE,
8003 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
8004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008005
Michael Chan0f893dc2005-07-25 12:30:38 -07008006 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008007 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8008 tp->bufmgr_config.mbuf_read_dma_low_water);
8009 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8010 tp->bufmgr_config.mbuf_mac_rx_low_water);
8011 tw32(BUFMGR_MB_HIGH_WATER,
8012 tp->bufmgr_config.mbuf_high_water);
8013 } else {
8014 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8015 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
8016 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8017 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
8018 tw32(BUFMGR_MB_HIGH_WATER,
8019 tp->bufmgr_config.mbuf_high_water_jumbo);
8020 }
8021 tw32(BUFMGR_DMA_LOW_WATER,
8022 tp->bufmgr_config.dma_low_water);
8023 tw32(BUFMGR_DMA_HIGH_WATER,
8024 tp->bufmgr_config.dma_high_water);
8025
Matt Carlsond309a462010-09-30 10:34:31 +00008026 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
8027 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
8028 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
8029 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008030 for (i = 0; i < 2000; i++) {
8031 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
8032 break;
8033 udelay(10);
8034 }
8035 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00008036 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008037 return -ENODEV;
8038 }
8039
8040 /* Setup replenish threshold. */
Michael Chanf92905d2006-06-29 20:14:29 -07008041 val = tp->rx_pending / 8;
8042 if (val == 0)
8043 val = 1;
8044 else if (val > tp->rx_std_max_post)
8045 val = tp->rx_std_max_post;
Michael Chanb5d37722006-09-27 16:06:21 -07008046 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
8047 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
8048 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
8049
8050 if (val > (TG3_RX_INTERNAL_RING_SZ_5906 / 2))
8051 val = TG3_RX_INTERNAL_RING_SZ_5906 / 2;
8052 }
Michael Chanf92905d2006-06-29 20:14:29 -07008053
8054 tw32(RCVBDI_STD_THRESH, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008055
8056 /* Initialize TG3_BDINFO's at:
8057 * RCVDBDI_STD_BD: standard eth size rx ring
8058 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
8059 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
8060 *
8061 * like so:
8062 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
8063 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
8064 * ring attribute flags
8065 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
8066 *
8067 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
8068 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
8069 *
8070 * The size of each ring is fixed in the firmware, but the location is
8071 * configurable.
8072 */
8073 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008074 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008075 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008076 ((u64) tpr->rx_std_mapping & 0xffffffff));
Matt Carlsona50d0792010-06-05 17:24:37 +00008077 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
8078 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5719)
Matt Carlson87668d32009-11-13 13:03:34 +00008079 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
8080 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008081
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008082 /* Disable the mini ring */
8083 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008084 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
8085 BDINFO_FLAGS_DISABLED);
8086
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008087 /* Program the jumbo buffer descriptor ring control
8088 * blocks on those devices that have them.
8089 */
Matt Carlson8f666b02009-08-28 13:58:24 +00008090 if ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) &&
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008091 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008092 /* Setup replenish threshold. */
8093 tw32(RCVBDI_JUMBO_THRESH, tp->rx_jumbo_pending / 8);
8094
Michael Chan0f893dc2005-07-25 12:30:38 -07008095 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008096 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008097 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008098 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008099 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008100 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlson79ed5ac2009-08-28 14:00:55 +00008101 (RX_JUMBO_MAX_SIZE << BDINFO_FLAGS_MAXLEN_SHIFT) |
8102 BDINFO_FLAGS_USE_EXT_RECV);
Matt Carlsona50d0792010-06-05 17:24:37 +00008103 if (!(tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) ||
8104 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson87668d32009-11-13 13:03:34 +00008105 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
8106 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008107 } else {
8108 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
8109 BDINFO_FLAGS_DISABLED);
8110 }
8111
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008112 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
8113 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8114 val = RX_STD_MAX_SIZE_5705;
8115 else
8116 val = RX_STD_MAX_SIZE_5717;
8117 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
8118 val |= (TG3_RX_STD_DMA_SZ << 2);
8119 } else
Matt Carlson04380d42010-04-12 06:58:29 +00008120 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008121 } else
8122 val = RX_STD_MAX_SIZE_5705 << BDINFO_FLAGS_MAXLEN_SHIFT;
8123
8124 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008125
Matt Carlson411da642009-11-13 13:03:46 +00008126 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e62009-11-13 13:03:49 +00008127 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008128
Matt Carlson411da642009-11-13 13:03:46 +00008129 tpr->rx_jmb_prod_idx = (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) ?
Matt Carlson21f581a2009-08-28 14:00:25 +00008130 tp->rx_jumbo_pending : 0;
Matt Carlson66711e62009-11-13 13:03:49 +00008131 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008132
Matt Carlsonc885e822010-08-02 11:25:57 +00008133 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008134 tw32(STD_REPLENISH_LWM, 32);
8135 tw32(JMB_REPLENISH_LWM, 16);
8136 }
8137
Matt Carlson2d31eca2009-09-01 12:53:31 +00008138 tg3_rings_reset(tp);
8139
Linus Torvalds1da177e2005-04-16 15:20:36 -07008140 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07008141 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008142
8143 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00008144 tw32(MAC_RX_MTU_SIZE,
8145 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008146
8147 /* The slot time is changed by tg3_setup_phy if we
8148 * run at gigabit with half duplex.
8149 */
8150 tw32(MAC_TX_LENGTHS,
8151 (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
8152 (6 << TX_LENGTHS_IPG_SHIFT) |
8153 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
8154
8155 /* Receive rules. */
8156 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
8157 tw32(RCVLPC_CONFIG, 0x0181);
8158
8159 /* Calculate RDMAC_MODE setting early, we need it to determine
8160 * the RCVLPC_STATE_ENABLE mask.
8161 */
8162 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
8163 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
8164 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
8165 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
8166 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07008167
Matt Carlsondeabaac2010-11-24 08:31:50 +00008168 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +00008169 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
8170
Matt Carlson57e69832008-05-25 23:48:31 -07008171 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08008172 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8173 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07008174 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
8175 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
8176 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
8177
Michael Chan85e94ce2005-04-21 17:05:28 -07008178 /* If statement applies to 5705 and 5750 PCI devices only */
8179 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8180 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) ||
8181 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008182 if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07008183 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008184 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
8185 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
8186 !(tp->tg3_flags2 & TG3_FLG2_IS_5788)) {
8187 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8188 }
8189 }
8190
Michael Chan85e94ce2005-04-21 17:05:28 -07008191 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)
8192 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8193
Linus Torvalds1da177e2005-04-16 15:20:36 -07008194 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
Matt Carlson027455a2008-12-21 20:19:30 -08008195 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
8196
Matt Carlsone849cdc2009-11-13 13:03:38 +00008197 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
8198 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson027455a2008-12-21 20:19:30 -08008199 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
8200 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008201
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008202 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
8203 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
8204 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8205 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
8206 (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)) {
8207 val = tr32(TG3_RDMA_RSRVCTRL_REG);
Matt Carlsonb75cc0e2010-11-24 08:31:46 +00008208 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) {
8209 val &= ~TG3_RDMA_RSRVCTRL_TXMRGN_MASK;
8210 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B;
8211 }
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008212 tw32(TG3_RDMA_RSRVCTRL_REG,
8213 val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
8214 }
8215
Matt Carlsond309a462010-09-30 10:34:31 +00008216 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) {
8217 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
8218 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val |
8219 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
8220 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
8221 }
8222
Linus Torvalds1da177e2005-04-16 15:20:36 -07008223 /* Receive/send statistics. */
Michael Chan16613942006-06-29 20:15:13 -07008224 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
8225 val = tr32(RCVLPC_STATS_ENABLE);
8226 val &= ~RCVLPC_STATSENAB_DACK_FIX;
8227 tw32(RCVLPC_STATS_ENABLE, val);
8228 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
8229 (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008230 val = tr32(RCVLPC_STATS_ENABLE);
8231 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
8232 tw32(RCVLPC_STATS_ENABLE, val);
8233 } else {
8234 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
8235 }
8236 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
8237 tw32(SNDDATAI_STATSENAB, 0xffffff);
8238 tw32(SNDDATAI_STATSCTRL,
8239 (SNDDATAI_SCTRL_ENABLE |
8240 SNDDATAI_SCTRL_FASTUPD));
8241
8242 /* Setup host coalescing engine. */
8243 tw32(HOSTCC_MODE, 0);
8244 for (i = 0; i < 2000; i++) {
8245 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
8246 break;
8247 udelay(10);
8248 }
8249
Michael Chand244c892005-07-05 14:42:33 -07008250 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008251
Linus Torvalds1da177e2005-04-16 15:20:36 -07008252 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
8253 /* Status/statistics block address. See tg3_timer,
8254 * the tg3_periodic_fetch_stats call there, and
8255 * tg3_get_stats to see how this works for 5705/5750 chips.
8256 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008257 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8258 ((u64) tp->stats_mapping >> 32));
8259 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8260 ((u64) tp->stats_mapping & 0xffffffff));
8261 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008262
Linus Torvalds1da177e2005-04-16 15:20:36 -07008263 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008264
8265 /* Clear statistics and status block memory areas */
8266 for (i = NIC_SRAM_STATS_BLK;
8267 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
8268 i += sizeof(u32)) {
8269 tg3_write_mem(tp, i, 0);
8270 udelay(40);
8271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008272 }
8273
8274 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
8275
8276 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
8277 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
8278 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
8279 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
8280
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008281 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
8282 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -07008283 /* reset to prevent losing 1st rx packet intermittently */
8284 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8285 udelay(10);
8286 }
8287
Matt Carlson3bda1252008-08-15 14:08:22 -07008288 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008289 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -07008290 else
8291 tp->mac_mode = 0;
8292 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Linus Torvalds1da177e2005-04-16 15:20:36 -07008293 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE | MAC_MODE_FHDE_ENABLE;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07008294 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008295 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07008296 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
8297 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008298 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
8299 udelay(40);
8300
Michael Chan314fba32005-04-21 17:07:04 -07008301 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Michael Chan9d26e212006-12-07 00:21:14 -08008302 * If TG3_FLG2_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07008303 * register to preserve the GPIO settings for LOMs. The GPIOs,
8304 * whether used as inputs or outputs, are set by boot code after
8305 * reset.
8306 */
Michael Chan9d26e212006-12-07 00:21:14 -08008307 if (!(tp->tg3_flags2 & TG3_FLG2_IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07008308 u32 gpio_mask;
8309
Michael Chan9d26e212006-12-07 00:21:14 -08008310 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
8311 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
8312 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07008313
8314 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
8315 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
8316 GRC_LCLCTRL_GPIO_OUTPUT3;
8317
Michael Chanaf36e6b2006-03-23 01:28:06 -08008318 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
8319 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
8320
Gary Zambranoaaf84462007-05-05 11:51:45 -07008321 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07008322 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
8323
8324 /* GPIO1 must be driven high for eeprom write protect */
Michael Chan9d26e212006-12-07 00:21:14 -08008325 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT)
8326 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
8327 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07008328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008329 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8330 udelay(100);
8331
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008332 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX) {
8333 val = tr32(MSGINT_MODE);
8334 val |= MSGINT_MODE_MULTIVEC_EN | MSGINT_MODE_ENABLE;
8335 tw32(MSGINT_MODE, val);
8336 }
8337
Linus Torvalds1da177e2005-04-16 15:20:36 -07008338 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
8339 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
8340 udelay(40);
8341 }
8342
8343 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
8344 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
8345 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
8346 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
8347 WDMAC_MODE_LNGREAD_ENAB);
8348
Michael Chan85e94ce2005-04-21 17:05:28 -07008349 /* If statement applies to 5705 and 5750 PCI devices only */
8350 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8351 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) ||
8352 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750) {
Matt Carlson29ea0952009-08-25 10:07:54 +00008353 if ((tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008354 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
8355 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
8356 /* nothing */
8357 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
8358 !(tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
8359 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)) {
8360 val |= WDMAC_MODE_RX_ACCEL;
8361 }
8362 }
8363
Michael Chand9ab5ad2006-03-20 22:27:35 -08008364 /* Enable host coalescing bug fix */
Matt Carlson321d32a2008-11-21 17:22:19 -08008365 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Matt Carlsonf51f3562008-05-25 23:45:08 -07008366 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad2006-03-20 22:27:35 -08008367
Matt Carlson788a0352009-11-02 14:26:03 +00008368 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
8369 val |= WDMAC_MODE_BURST_ALL_DATA;
8370
Linus Torvalds1da177e2005-04-16 15:20:36 -07008371 tw32_f(WDMAC_MODE, val);
8372 udelay(40);
8373
Matt Carlson9974a352007-10-07 23:27:28 -07008374 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
8375 u16 pcix_cmd;
8376
8377 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8378 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008379 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07008380 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
8381 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008382 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07008383 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
8384 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008385 }
Matt Carlson9974a352007-10-07 23:27:28 -07008386 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8387 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008388 }
8389
8390 tw32_f(RDMAC_MODE, rdmac_mode);
8391 udelay(40);
8392
8393 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
8394 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
8395 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07008396
8397 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
8398 tw32(SNDDATAC_MODE,
8399 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
8400 else
8401 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
8402
Linus Torvalds1da177e2005-04-16 15:20:36 -07008403 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
8404 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008405 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
8406 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
8407 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
8408 val |= RCVDBDI_MODE_LRG_RING_SZ;
8409 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008410 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008411 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
8412 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008413 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Matt Carlson19cfaec2009-12-03 08:36:20 +00008414 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008415 val |= SNDBDI_MODE_MULTI_TXQ_EN;
8416 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008417 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
8418
8419 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
8420 err = tg3_load_5701_a0_firmware_fix(tp);
8421 if (err)
8422 return err;
8423 }
8424
Linus Torvalds1da177e2005-04-16 15:20:36 -07008425 if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) {
8426 err = tg3_load_tso_firmware(tp);
8427 if (err)
8428 return err;
8429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008430
8431 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonb1d05212010-06-05 17:24:31 +00008432 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
8433 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
8434 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008435 tw32_f(MAC_TX_MODE, tp->tx_mode);
8436 udelay(100);
8437
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008438 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS) {
8439 u32 reg = MAC_RSS_INDIR_TBL_0;
8440 u8 *ent = (u8 *)&val;
8441
8442 /* Setup the indirection table */
8443 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
8444 int idx = i % sizeof(val);
8445
Matt Carlson5efeeea2010-07-11 09:31:40 +00008446 ent[idx] = i % (tp->irq_cnt - 1);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008447 if (idx == sizeof(val) - 1) {
8448 tw32(reg, val);
8449 reg += 4;
8450 }
8451 }
8452
8453 /* Setup the "secret" hash key. */
8454 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
8455 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
8456 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
8457 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
8458 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
8459 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
8460 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
8461 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
8462 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
8463 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
8464 }
8465
Linus Torvalds1da177e2005-04-16 15:20:36 -07008466 tp->rx_mode = RX_MODE_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08008467 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Michael Chanaf36e6b2006-03-23 01:28:06 -08008468 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
8469
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008470 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)
8471 tp->rx_mode |= RX_MODE_RSS_ENABLE |
8472 RX_MODE_RSS_ITBL_HASH_BITS_7 |
8473 RX_MODE_RSS_IPV6_HASH_EN |
8474 RX_MODE_RSS_TCP_IPV6_HASH_EN |
8475 RX_MODE_RSS_IPV4_HASH_EN |
8476 RX_MODE_RSS_TCP_IPV4_HASH_EN;
8477
Linus Torvalds1da177e2005-04-16 15:20:36 -07008478 tw32_f(MAC_RX_MODE, tp->rx_mode);
8479 udelay(10);
8480
Linus Torvalds1da177e2005-04-16 15:20:36 -07008481 tw32(MAC_LED_CTRL, tp->led_ctrl);
8482
8483 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008484 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008485 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8486 udelay(10);
8487 }
8488 tw32_f(MAC_RX_MODE, tp->rx_mode);
8489 udelay(10);
8490
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008491 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008492 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008493 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008494 /* Set drive transmission level to 1.2V */
8495 /* only if the signal pre-emphasis bit is not set */
8496 val = tr32(MAC_SERDES_CFG);
8497 val &= 0xfffff000;
8498 val |= 0x880;
8499 tw32(MAC_SERDES_CFG, val);
8500 }
8501 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
8502 tw32(MAC_SERDES_CFG, 0x616000);
8503 }
8504
8505 /* Prevent chip from dropping frames when flow control
8506 * is enabled.
8507 */
Matt Carlson666bc832010-01-20 16:58:03 +00008508 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8509 val = 1;
8510 else
8511 val = 2;
8512 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008513
8514 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008515 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008516 /* Use hardware link auto-negotiation */
8517 tp->tg3_flags2 |= TG3_FLG2_HW_AUTONEG;
8518 }
8519
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008520 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Michael Chand4d2c552006-03-20 17:47:20 -08008521 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)) {
8522 u32 tmp;
8523
8524 tmp = tr32(SERDES_RX_CTRL);
8525 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
8526 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
8527 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
8528 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8529 }
8530
Matt Carlsondd477002008-05-25 23:45:58 -07008531 if (!(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)) {
Matt Carlson80096062010-08-02 11:26:06 +00008532 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
8533 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07008534 tp->link_config.speed = tp->link_config.orig_speed;
8535 tp->link_config.duplex = tp->link_config.orig_duplex;
8536 tp->link_config.autoneg = tp->link_config.orig_autoneg;
8537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008538
Matt Carlsondd477002008-05-25 23:45:58 -07008539 err = tg3_setup_phy(tp, 0);
8540 if (err)
8541 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008542
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008543 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
8544 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07008545 u32 tmp;
8546
8547 /* Clear CRC stats. */
8548 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
8549 tg3_writephy(tp, MII_TG3_TEST1,
8550 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00008551 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -07008552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008553 }
8554 }
8555
8556 __tg3_set_rx_mode(tp->dev);
8557
8558 /* Initialize receive rules. */
8559 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
8560 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
8561 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
8562 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
8563
Michael Chan4cf78e42005-07-25 12:29:19 -07008564 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Michael Chana4e2b342005-10-26 15:46:52 -07008565 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008566 limit = 8;
8567 else
8568 limit = 16;
8569 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF)
8570 limit -= 4;
8571 switch (limit) {
8572 case 16:
8573 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
8574 case 15:
8575 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
8576 case 14:
8577 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
8578 case 13:
8579 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
8580 case 12:
8581 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
8582 case 11:
8583 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
8584 case 10:
8585 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
8586 case 9:
8587 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
8588 case 8:
8589 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
8590 case 7:
8591 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
8592 case 6:
8593 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
8594 case 5:
8595 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
8596 case 4:
8597 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
8598 case 3:
8599 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
8600 case 2:
8601 case 1:
8602
8603 default:
8604 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07008605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008606
Matt Carlson9ce768e2007-10-11 19:49:11 -07008607 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
8608 /* Write our heartbeat update interval to APE. */
8609 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
8610 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07008611
Linus Torvalds1da177e2005-04-16 15:20:36 -07008612 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
8613
Linus Torvalds1da177e2005-04-16 15:20:36 -07008614 return 0;
8615}
8616
8617/* Called at device open time to get the chip ready for
8618 * packet processing. Invoked with tp->lock held.
8619 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008620static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008621{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008622 tg3_switch_clocks(tp);
8623
8624 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
8625
Matt Carlson2f751b62008-08-04 23:17:34 -07008626 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008627}
8628
8629#define TG3_STAT_ADD32(PSTAT, REG) \
8630do { u32 __val = tr32(REG); \
8631 (PSTAT)->low += __val; \
8632 if ((PSTAT)->low < __val) \
8633 (PSTAT)->high += 1; \
8634} while (0)
8635
8636static void tg3_periodic_fetch_stats(struct tg3 *tp)
8637{
8638 struct tg3_hw_stats *sp = tp->hw_stats;
8639
8640 if (!netif_carrier_ok(tp->dev))
8641 return;
8642
8643 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
8644 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
8645 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
8646 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
8647 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
8648 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
8649 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
8650 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
8651 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
8652 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
8653 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
8654 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
8655 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
8656
8657 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
8658 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
8659 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
8660 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
8661 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
8662 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
8663 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
8664 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
8665 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
8666 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
8667 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
8668 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
8669 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
8670 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07008671
8672 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
8673 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
8674 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008675}
8676
8677static void tg3_timer(unsigned long __opaque)
8678{
8679 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008680
Michael Chanf475f162006-03-27 23:20:14 -08008681 if (tp->irq_sync)
8682 goto restart_timer;
8683
David S. Millerf47c11e2005-06-24 20:18:35 -07008684 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008685
David S. Millerfac9b832005-05-18 22:46:34 -07008686 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)) {
8687 /* All of this garbage is because when using non-tagged
8688 * IRQ status the mailbox/status_block protocol the chip
8689 * uses with the cpu is race prone.
8690 */
Matt Carlson898a56f2009-08-28 14:02:40 +00008691 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -07008692 tw32(GRC_LOCAL_CTRL,
8693 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
8694 } else {
8695 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00008696 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -07008697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008698
David S. Millerfac9b832005-05-18 22:46:34 -07008699 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
8700 tp->tg3_flags2 |= TG3_FLG2_RESTART_TIMER;
David S. Millerf47c11e2005-06-24 20:18:35 -07008701 spin_unlock(&tp->lock);
David S. Millerfac9b832005-05-18 22:46:34 -07008702 schedule_work(&tp->reset_task);
8703 return;
8704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008705 }
8706
Linus Torvalds1da177e2005-04-16 15:20:36 -07008707 /* This part only runs once per second. */
8708 if (!--tp->timer_counter) {
David S. Millerfac9b832005-05-18 22:46:34 -07008709 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
8710 tg3_periodic_fetch_stats(tp);
8711
Matt Carlson52b02d02010-10-14 10:37:41 +00008712 if (tp->setlpicnt && !--tp->setlpicnt) {
8713 u32 val = tr32(TG3_CPMU_EEE_MODE);
8714 tw32(TG3_CPMU_EEE_MODE,
8715 val | TG3_CPMU_EEEMD_LPI_ENABLE);
8716 }
8717
Linus Torvalds1da177e2005-04-16 15:20:36 -07008718 if (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) {
8719 u32 mac_stat;
8720 int phy_event;
8721
8722 mac_stat = tr32(MAC_STATUS);
8723
8724 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008725 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008726 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
8727 phy_event = 1;
8728 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
8729 phy_event = 1;
8730
8731 if (phy_event)
8732 tg3_setup_phy(tp, 0);
8733 } else if (tp->tg3_flags & TG3_FLAG_POLL_SERDES) {
8734 u32 mac_stat = tr32(MAC_STATUS);
8735 int need_setup = 0;
8736
8737 if (netif_carrier_ok(tp->dev) &&
8738 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
8739 need_setup = 1;
8740 }
Matt Carlsonbe98da62010-07-11 09:31:46 +00008741 if (!netif_carrier_ok(tp->dev) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008742 (mac_stat & (MAC_STATUS_PCS_SYNCED |
8743 MAC_STATUS_SIGNAL_DET))) {
8744 need_setup = 1;
8745 }
8746 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07008747 if (!tp->serdes_counter) {
8748 tw32_f(MAC_MODE,
8749 (tp->mac_mode &
8750 ~MAC_MODE_PORT_MODE_MASK));
8751 udelay(40);
8752 tw32_f(MAC_MODE, tp->mac_mode);
8753 udelay(40);
8754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008755 tg3_setup_phy(tp, 0);
8756 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008757 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Matt Carlson2138c002010-07-11 09:31:43 +00008758 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -07008759 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +00008760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008761
8762 tp->timer_counter = tp->timer_multiplier;
8763 }
8764
Michael Chan130b8e42006-09-27 16:00:40 -07008765 /* Heartbeat is only sent once every 2 seconds.
8766 *
8767 * The heartbeat is to tell the ASF firmware that the host
8768 * driver is still alive. In the event that the OS crashes,
8769 * ASF needs to reset the hardware to free up the FIFO space
8770 * that may be filled with rx packets destined for the host.
8771 * If the FIFO is full, ASF will no longer function properly.
8772 *
8773 * Unintended resets have been reported on real time kernels
8774 * where the timer doesn't run on time. Netpoll will also have
8775 * same problem.
8776 *
8777 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
8778 * to check the ring condition when the heartbeat is expiring
8779 * before doing the reset. This will prevent most unintended
8780 * resets.
8781 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008782 if (!--tp->asf_counter) {
Matt Carlsonbc7959b2008-08-15 14:08:55 -07008783 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
8784 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07008785 tg3_wait_for_event_ack(tp);
8786
Michael Chanbbadf502006-04-06 21:46:34 -07008787 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07008788 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07008789 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +00008790 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
8791 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -07008792
8793 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008794 }
8795 tp->asf_counter = tp->asf_multiplier;
8796 }
8797
David S. Millerf47c11e2005-06-24 20:18:35 -07008798 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008799
Michael Chanf475f162006-03-27 23:20:14 -08008800restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07008801 tp->timer.expires = jiffies + tp->timer_offset;
8802 add_timer(&tp->timer);
8803}
8804
Matt Carlson4f125f42009-09-01 12:55:02 +00008805static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -08008806{
David Howells7d12e782006-10-05 14:55:46 +01008807 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008808 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +00008809 char *name;
8810 struct tg3_napi *tnapi = &tp->napi[irq_num];
8811
8812 if (tp->irq_cnt == 1)
8813 name = tp->dev->name;
8814 else {
8815 name = &tnapi->irq_lbl[0];
8816 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
8817 name[IFNAMSIZ-1] = 0;
8818 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08008819
Matt Carlson679563f2009-09-01 12:55:46 +00008820 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI_OR_MSIX) {
Michael Chanfcfa0a32006-03-20 22:28:41 -08008821 fn = tg3_msi;
8822 if (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)
8823 fn = tg3_msi_1shot;
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07008824 flags = IRQF_SAMPLE_RANDOM;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008825 } else {
8826 fn = tg3_interrupt;
8827 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)
8828 fn = tg3_interrupt_tagged;
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07008829 flags = IRQF_SHARED | IRQF_SAMPLE_RANDOM;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008830 }
Matt Carlson4f125f42009-09-01 12:55:02 +00008831
8832 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08008833}
8834
Michael Chan79381092005-04-21 17:13:59 -07008835static int tg3_test_interrupt(struct tg3 *tp)
8836{
Matt Carlson09943a12009-08-28 14:01:57 +00008837 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -07008838 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -07008839 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008840 u32 val;
Michael Chan79381092005-04-21 17:13:59 -07008841
Michael Chand4bc3922005-05-29 14:59:20 -07008842 if (!netif_running(dev))
8843 return -ENODEV;
8844
Michael Chan79381092005-04-21 17:13:59 -07008845 tg3_disable_ints(tp);
8846
Matt Carlson4f125f42009-09-01 12:55:02 +00008847 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07008848
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008849 /*
8850 * Turn off MSI one shot mode. Otherwise this test has no
8851 * observable way to know whether the interrupt was delivered.
8852 */
Matt Carlsonc885e822010-08-02 11:25:57 +00008853 if ((tp->tg3_flags3 & TG3_FLG3_5717_PLUS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008854 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
8855 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
8856 tw32(MSGINT_MODE, val);
8857 }
8858
Matt Carlson4f125f42009-09-01 12:55:02 +00008859 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Matt Carlson09943a12009-08-28 14:01:57 +00008860 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07008861 if (err)
8862 return err;
8863
Matt Carlson898a56f2009-08-28 14:02:40 +00008864 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -07008865 tg3_enable_ints(tp);
8866
8867 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00008868 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -07008869
8870 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -07008871 u32 int_mbox, misc_host_ctrl;
8872
Matt Carlson898a56f2009-08-28 14:02:40 +00008873 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -07008874 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
8875
8876 if ((int_mbox != 0) ||
8877 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
8878 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -07008879 break;
Michael Chanb16250e2006-09-27 16:10:14 -07008880 }
8881
Michael Chan79381092005-04-21 17:13:59 -07008882 msleep(10);
8883 }
8884
8885 tg3_disable_ints(tp);
8886
Matt Carlson4f125f42009-09-01 12:55:02 +00008887 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008888
Matt Carlson4f125f42009-09-01 12:55:02 +00008889 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07008890
8891 if (err)
8892 return err;
8893
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008894 if (intr_ok) {
8895 /* Reenable MSI one shot mode. */
Matt Carlsonc885e822010-08-02 11:25:57 +00008896 if ((tp->tg3_flags3 & TG3_FLG3_5717_PLUS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008897 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
8898 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
8899 tw32(MSGINT_MODE, val);
8900 }
Michael Chan79381092005-04-21 17:13:59 -07008901 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008902 }
Michael Chan79381092005-04-21 17:13:59 -07008903
8904 return -EIO;
8905}
8906
8907/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
8908 * successfully restored
8909 */
8910static int tg3_test_msi(struct tg3 *tp)
8911{
Michael Chan79381092005-04-21 17:13:59 -07008912 int err;
8913 u16 pci_cmd;
8914
8915 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSI))
8916 return 0;
8917
8918 /* Turn off SERR reporting in case MSI terminates with Master
8919 * Abort.
8920 */
8921 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
8922 pci_write_config_word(tp->pdev, PCI_COMMAND,
8923 pci_cmd & ~PCI_COMMAND_SERR);
8924
8925 err = tg3_test_interrupt(tp);
8926
8927 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
8928
8929 if (!err)
8930 return 0;
8931
8932 /* other failures */
8933 if (err != -EIO)
8934 return err;
8935
8936 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +00008937 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
8938 "to INTx mode. Please report this failure to the PCI "
8939 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -07008940
Matt Carlson4f125f42009-09-01 12:55:02 +00008941 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +00008942
Michael Chan79381092005-04-21 17:13:59 -07008943 pci_disable_msi(tp->pdev);
8944
8945 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI;
Andre Detschdc8bf1b2010-04-26 07:27:07 +00008946 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -07008947
Matt Carlson4f125f42009-09-01 12:55:02 +00008948 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07008949 if (err)
8950 return err;
8951
8952 /* Need to reset the chip because the MSI cycle may have terminated
8953 * with Master Abort.
8954 */
David S. Millerf47c11e2005-06-24 20:18:35 -07008955 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07008956
Michael Chan944d9802005-05-29 14:57:48 -07008957 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008958 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07008959
David S. Millerf47c11e2005-06-24 20:18:35 -07008960 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07008961
8962 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +00008963 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -07008964
8965 return err;
8966}
8967
Matt Carlson9e9fd122009-01-19 16:57:45 -08008968static int tg3_request_firmware(struct tg3 *tp)
8969{
8970 const __be32 *fw_data;
8971
8972 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00008973 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
8974 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08008975 return -ENOENT;
8976 }
8977
8978 fw_data = (void *)tp->fw->data;
8979
8980 /* Firmware blob starts with version numbers, followed by
8981 * start address and _full_ length including BSS sections
8982 * (which must be longer than the actual data, of course
8983 */
8984
8985 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
8986 if (tp->fw_len < (tp->fw->size - 12)) {
Joe Perches05dbe002010-02-17 19:44:19 +00008987 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
8988 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08008989 release_firmware(tp->fw);
8990 tp->fw = NULL;
8991 return -EINVAL;
8992 }
8993
8994 /* We no longer need firmware; we have it. */
8995 tp->fw_needed = NULL;
8996 return 0;
8997}
8998
Matt Carlson679563f2009-09-01 12:55:46 +00008999static bool tg3_enable_msix(struct tg3 *tp)
9000{
9001 int i, rc, cpus = num_online_cpus();
9002 struct msix_entry msix_ent[tp->irq_max];
9003
9004 if (cpus == 1)
9005 /* Just fallback to the simpler MSI mode. */
9006 return false;
9007
9008 /*
9009 * We want as many rx rings enabled as there are cpus.
9010 * The first MSIX vector only deals with link interrupts, etc,
9011 * so we add one to the number of vectors we are requesting.
9012 */
9013 tp->irq_cnt = min_t(unsigned, cpus + 1, tp->irq_max);
9014
9015 for (i = 0; i < tp->irq_max; i++) {
9016 msix_ent[i].entry = i;
9017 msix_ent[i].vector = 0;
9018 }
9019
9020 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +00009021 if (rc < 0) {
9022 return false;
9023 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +00009024 if (pci_enable_msix(tp->pdev, msix_ent, rc))
9025 return false;
Joe Perches05dbe002010-02-17 19:44:19 +00009026 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
9027 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +00009028 tp->irq_cnt = rc;
9029 }
9030
9031 for (i = 0; i < tp->irq_max; i++)
9032 tp->napi[i].irq_vec = msix_ent[i].vector;
9033
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009034 netif_set_real_num_tx_queues(tp->dev, 1);
9035 rc = tp->irq_cnt > 1 ? tp->irq_cnt - 1 : 1;
9036 if (netif_set_real_num_rx_queues(tp->dev, rc)) {
9037 pci_disable_msix(tp->pdev);
9038 return false;
9039 }
Matt Carlsonf0392d22010-09-15 08:59:54 +00009040 if (tp->irq_cnt > 1)
Matt Carlson2430b032010-06-05 17:24:34 +00009041 tp->tg3_flags3 |= TG3_FLG3_ENABLE_RSS;
9042
Matt Carlson679563f2009-09-01 12:55:46 +00009043 return true;
9044}
9045
Matt Carlson07b01732009-08-28 14:01:15 +00009046static void tg3_ints_init(struct tg3 *tp)
9047{
Matt Carlson679563f2009-09-01 12:55:46 +00009048 if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSI_OR_MSIX) &&
9049 !(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +00009050 /* All MSI supporting chips should support tagged
9051 * status. Assert that this is the case.
9052 */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009053 netdev_warn(tp->dev,
9054 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +00009055 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +00009056 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009057
Matt Carlson679563f2009-09-01 12:55:46 +00009058 if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX) && tg3_enable_msix(tp))
9059 tp->tg3_flags2 |= TG3_FLG2_USING_MSIX;
9060 else if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSI) &&
9061 pci_enable_msi(tp->pdev) == 0)
9062 tp->tg3_flags2 |= TG3_FLG2_USING_MSI;
9063
9064 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI_OR_MSIX) {
9065 u32 msi_mode = tr32(MSGINT_MODE);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009066 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
9067 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson679563f2009-09-01 12:55:46 +00009068 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
9069 }
9070defcfg:
9071 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSIX)) {
9072 tp->irq_cnt = 1;
9073 tp->napi[0].irq_vec = tp->pdev->irq;
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009074 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -07009075 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +00009076 }
Matt Carlson07b01732009-08-28 14:01:15 +00009077}
9078
9079static void tg3_ints_fini(struct tg3 *tp)
9080{
Matt Carlson679563f2009-09-01 12:55:46 +00009081 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
9082 pci_disable_msix(tp->pdev);
9083 else if (tp->tg3_flags2 & TG3_FLG2_USING_MSI)
9084 pci_disable_msi(tp->pdev);
9085 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI_OR_MSIX;
Matt Carlson774ee752010-08-02 11:25:56 +00009086 tp->tg3_flags3 &= ~(TG3_FLG3_ENABLE_RSS | TG3_FLG3_ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +00009087}
9088
Linus Torvalds1da177e2005-04-16 15:20:36 -07009089static int tg3_open(struct net_device *dev)
9090{
9091 struct tg3 *tp = netdev_priv(dev);
Matt Carlson4f125f42009-09-01 12:55:02 +00009092 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009093
Matt Carlson9e9fd122009-01-19 16:57:45 -08009094 if (tp->fw_needed) {
9095 err = tg3_request_firmware(tp);
9096 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
9097 if (err)
9098 return err;
9099 } else if (err) {
Joe Perches05dbe002010-02-17 19:44:19 +00009100 netdev_warn(tp->dev, "TSO capability disabled\n");
Matt Carlson9e9fd122009-01-19 16:57:45 -08009101 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
9102 } else if (!(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009103 netdev_notice(tp->dev, "TSO capability restored\n");
Matt Carlson9e9fd122009-01-19 16:57:45 -08009104 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
9105 }
9106 }
9107
Michael Chanc49a1562006-12-17 17:07:29 -08009108 netif_carrier_off(tp->dev);
9109
Michael Chanbc1c7562006-03-20 17:48:03 -08009110 err = tg3_set_power_state(tp, PCI_D0);
Matt Carlson2f751b62008-08-04 23:17:34 -07009111 if (err)
Michael Chanbc1c7562006-03-20 17:48:03 -08009112 return err;
Matt Carlson2f751b62008-08-04 23:17:34 -07009113
9114 tg3_full_lock(tp, 0);
Michael Chanbc1c7562006-03-20 17:48:03 -08009115
Linus Torvalds1da177e2005-04-16 15:20:36 -07009116 tg3_disable_ints(tp);
9117 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
9118
David S. Millerf47c11e2005-06-24 20:18:35 -07009119 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009120
Matt Carlson679563f2009-09-01 12:55:46 +00009121 /*
9122 * Setup interrupts first so we know how
9123 * many NAPI resources to allocate
9124 */
9125 tg3_ints_init(tp);
9126
Linus Torvalds1da177e2005-04-16 15:20:36 -07009127 /* The placement of this call is tied
9128 * to the setup and use of Host TX descriptors.
9129 */
9130 err = tg3_alloc_consistent(tp);
9131 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009132 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009133
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009134 tg3_napi_init(tp);
9135
Matt Carlsonfed97812009-09-01 13:10:19 +00009136 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07009137
Matt Carlson4f125f42009-09-01 12:55:02 +00009138 for (i = 0; i < tp->irq_cnt; i++) {
9139 struct tg3_napi *tnapi = &tp->napi[i];
9140 err = tg3_request_irq(tp, i);
9141 if (err) {
9142 for (i--; i >= 0; i--)
9143 free_irq(tnapi->irq_vec, tnapi);
9144 break;
9145 }
9146 }
Matt Carlson07b01732009-08-28 14:01:15 +00009147
9148 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009149 goto err_out2;
Matt Carlson07b01732009-08-28 14:01:15 +00009150
David S. Millerf47c11e2005-06-24 20:18:35 -07009151 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009152
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009153 err = tg3_init_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009154 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -07009155 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009156 tg3_free_rings(tp);
9157 } else {
David S. Millerfac9b832005-05-18 22:46:34 -07009158 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)
9159 tp->timer_offset = HZ;
9160 else
9161 tp->timer_offset = HZ / 10;
9162
9163 BUG_ON(tp->timer_offset > HZ);
9164 tp->timer_counter = tp->timer_multiplier =
9165 (HZ / tp->timer_offset);
9166 tp->asf_counter = tp->asf_multiplier =
Michael Chan28fbef72005-10-26 15:48:35 -07009167 ((HZ / tp->timer_offset) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009168
9169 init_timer(&tp->timer);
9170 tp->timer.expires = jiffies + tp->timer_offset;
9171 tp->timer.data = (unsigned long) tp;
9172 tp->timer.function = tg3_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009173 }
9174
David S. Millerf47c11e2005-06-24 20:18:35 -07009175 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009176
Matt Carlson07b01732009-08-28 14:01:15 +00009177 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009178 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009179
Michael Chan79381092005-04-21 17:13:59 -07009180 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
9181 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -07009182
Michael Chan79381092005-04-21 17:13:59 -07009183 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -07009184 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -07009185 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -07009186 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07009187 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009188
Matt Carlson679563f2009-09-01 12:55:46 +00009189 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -07009190 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009191
Matt Carlsonc885e822010-08-02 11:25:57 +00009192 if (!(tp->tg3_flags3 & TG3_FLG3_5717_PLUS) &&
9193 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009194 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009195
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009196 tw32(PCIE_TRANSACTION_CFG,
9197 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009198 }
Michael Chan79381092005-04-21 17:13:59 -07009199 }
9200
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009201 tg3_phy_start(tp);
9202
David S. Millerf47c11e2005-06-24 20:18:35 -07009203 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009204
Michael Chan79381092005-04-21 17:13:59 -07009205 add_timer(&tp->timer);
9206 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009207 tg3_enable_ints(tp);
9208
David S. Millerf47c11e2005-06-24 20:18:35 -07009209 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009210
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009211 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009212
9213 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +00009214
Matt Carlson679563f2009-09-01 12:55:46 +00009215err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +00009216 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9217 struct tg3_napi *tnapi = &tp->napi[i];
9218 free_irq(tnapi->irq_vec, tnapi);
9219 }
Matt Carlson07b01732009-08-28 14:01:15 +00009220
Matt Carlson679563f2009-09-01 12:55:46 +00009221err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +00009222 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009223 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00009224 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +00009225
9226err_out1:
9227 tg3_ints_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00009228 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009229}
9230
Eric Dumazet511d2222010-07-07 20:44:24 +00009231static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *,
9232 struct rtnl_link_stats64 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009233static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *);
9234
9235static int tg3_close(struct net_device *dev)
9236{
Matt Carlson4f125f42009-09-01 12:55:02 +00009237 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009238 struct tg3 *tp = netdev_priv(dev);
9239
Matt Carlsonfed97812009-09-01 13:10:19 +00009240 tg3_napi_disable(tp);
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07009241 cancel_work_sync(&tp->reset_task);
Michael Chan7faa0062006-02-02 17:29:28 -08009242
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009243 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009244
9245 del_timer_sync(&tp->timer);
9246
Matt Carlson24bb4fb2009-10-05 17:55:29 +00009247 tg3_phy_stop(tp);
9248
David S. Millerf47c11e2005-06-24 20:18:35 -07009249 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009250
9251 tg3_disable_ints(tp);
9252
Michael Chan944d9802005-05-29 14:57:48 -07009253 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009254 tg3_free_rings(tp);
Michael Chan5cf64b82007-05-05 12:11:21 -07009255 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009256
David S. Millerf47c11e2005-06-24 20:18:35 -07009257 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009258
Matt Carlson4f125f42009-09-01 12:55:02 +00009259 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9260 struct tg3_napi *tnapi = &tp->napi[i];
9261 free_irq(tnapi->irq_vec, tnapi);
9262 }
Matt Carlson07b01732009-08-28 14:01:15 +00009263
9264 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009265
Eric Dumazet511d2222010-07-07 20:44:24 +00009266 tg3_get_stats64(tp->dev, &tp->net_stats_prev);
9267
Linus Torvalds1da177e2005-04-16 15:20:36 -07009268 memcpy(&tp->estats_prev, tg3_get_estats(tp),
9269 sizeof(tp->estats_prev));
9270
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009271 tg3_napi_fini(tp);
9272
Linus Torvalds1da177e2005-04-16 15:20:36 -07009273 tg3_free_consistent(tp);
9274
Michael Chanbc1c7562006-03-20 17:48:03 -08009275 tg3_set_power_state(tp, PCI_D3hot);
9276
9277 netif_carrier_off(tp->dev);
9278
Linus Torvalds1da177e2005-04-16 15:20:36 -07009279 return 0;
9280}
9281
Eric Dumazet511d2222010-07-07 20:44:24 +00009282static inline u64 get_stat64(tg3_stat64_t *val)
Stefan Buehler816f8b82008-08-15 14:10:54 -07009283{
9284 return ((u64)val->high << 32) | ((u64)val->low);
9285}
9286
Eric Dumazet511d2222010-07-07 20:44:24 +00009287static u64 calc_crc_errors(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009288{
9289 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9290
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009291 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009292 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
9293 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009294 u32 val;
9295
David S. Millerf47c11e2005-06-24 20:18:35 -07009296 spin_lock_bh(&tp->lock);
Michael Chan569a5df2007-02-13 12:18:15 -08009297 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
9298 tg3_writephy(tp, MII_TG3_TEST1,
9299 val | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009300 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009301 } else
9302 val = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07009303 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009304
9305 tp->phy_crc_errors += val;
9306
9307 return tp->phy_crc_errors;
9308 }
9309
9310 return get_stat64(&hw_stats->rx_fcs_errors);
9311}
9312
9313#define ESTAT_ADD(member) \
9314 estats->member = old_estats->member + \
Eric Dumazet511d2222010-07-07 20:44:24 +00009315 get_stat64(&hw_stats->member)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009316
9317static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *tp)
9318{
9319 struct tg3_ethtool_stats *estats = &tp->estats;
9320 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
9321 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9322
9323 if (!hw_stats)
9324 return old_estats;
9325
9326 ESTAT_ADD(rx_octets);
9327 ESTAT_ADD(rx_fragments);
9328 ESTAT_ADD(rx_ucast_packets);
9329 ESTAT_ADD(rx_mcast_packets);
9330 ESTAT_ADD(rx_bcast_packets);
9331 ESTAT_ADD(rx_fcs_errors);
9332 ESTAT_ADD(rx_align_errors);
9333 ESTAT_ADD(rx_xon_pause_rcvd);
9334 ESTAT_ADD(rx_xoff_pause_rcvd);
9335 ESTAT_ADD(rx_mac_ctrl_rcvd);
9336 ESTAT_ADD(rx_xoff_entered);
9337 ESTAT_ADD(rx_frame_too_long_errors);
9338 ESTAT_ADD(rx_jabbers);
9339 ESTAT_ADD(rx_undersize_packets);
9340 ESTAT_ADD(rx_in_length_errors);
9341 ESTAT_ADD(rx_out_length_errors);
9342 ESTAT_ADD(rx_64_or_less_octet_packets);
9343 ESTAT_ADD(rx_65_to_127_octet_packets);
9344 ESTAT_ADD(rx_128_to_255_octet_packets);
9345 ESTAT_ADD(rx_256_to_511_octet_packets);
9346 ESTAT_ADD(rx_512_to_1023_octet_packets);
9347 ESTAT_ADD(rx_1024_to_1522_octet_packets);
9348 ESTAT_ADD(rx_1523_to_2047_octet_packets);
9349 ESTAT_ADD(rx_2048_to_4095_octet_packets);
9350 ESTAT_ADD(rx_4096_to_8191_octet_packets);
9351 ESTAT_ADD(rx_8192_to_9022_octet_packets);
9352
9353 ESTAT_ADD(tx_octets);
9354 ESTAT_ADD(tx_collisions);
9355 ESTAT_ADD(tx_xon_sent);
9356 ESTAT_ADD(tx_xoff_sent);
9357 ESTAT_ADD(tx_flow_control);
9358 ESTAT_ADD(tx_mac_errors);
9359 ESTAT_ADD(tx_single_collisions);
9360 ESTAT_ADD(tx_mult_collisions);
9361 ESTAT_ADD(tx_deferred);
9362 ESTAT_ADD(tx_excessive_collisions);
9363 ESTAT_ADD(tx_late_collisions);
9364 ESTAT_ADD(tx_collide_2times);
9365 ESTAT_ADD(tx_collide_3times);
9366 ESTAT_ADD(tx_collide_4times);
9367 ESTAT_ADD(tx_collide_5times);
9368 ESTAT_ADD(tx_collide_6times);
9369 ESTAT_ADD(tx_collide_7times);
9370 ESTAT_ADD(tx_collide_8times);
9371 ESTAT_ADD(tx_collide_9times);
9372 ESTAT_ADD(tx_collide_10times);
9373 ESTAT_ADD(tx_collide_11times);
9374 ESTAT_ADD(tx_collide_12times);
9375 ESTAT_ADD(tx_collide_13times);
9376 ESTAT_ADD(tx_collide_14times);
9377 ESTAT_ADD(tx_collide_15times);
9378 ESTAT_ADD(tx_ucast_packets);
9379 ESTAT_ADD(tx_mcast_packets);
9380 ESTAT_ADD(tx_bcast_packets);
9381 ESTAT_ADD(tx_carrier_sense_errors);
9382 ESTAT_ADD(tx_discards);
9383 ESTAT_ADD(tx_errors);
9384
9385 ESTAT_ADD(dma_writeq_full);
9386 ESTAT_ADD(dma_write_prioq_full);
9387 ESTAT_ADD(rxbds_empty);
9388 ESTAT_ADD(rx_discards);
9389 ESTAT_ADD(rx_errors);
9390 ESTAT_ADD(rx_threshold_hit);
9391
9392 ESTAT_ADD(dma_readq_full);
9393 ESTAT_ADD(dma_read_prioq_full);
9394 ESTAT_ADD(tx_comp_queue_full);
9395
9396 ESTAT_ADD(ring_set_send_prod_index);
9397 ESTAT_ADD(ring_status_update);
9398 ESTAT_ADD(nic_irqs);
9399 ESTAT_ADD(nic_avoided_irqs);
9400 ESTAT_ADD(nic_tx_threshold_hit);
9401
9402 return estats;
9403}
9404
Eric Dumazet511d2222010-07-07 20:44:24 +00009405static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
9406 struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009407{
9408 struct tg3 *tp = netdev_priv(dev);
Eric Dumazet511d2222010-07-07 20:44:24 +00009409 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009410 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9411
9412 if (!hw_stats)
9413 return old_stats;
9414
9415 stats->rx_packets = old_stats->rx_packets +
9416 get_stat64(&hw_stats->rx_ucast_packets) +
9417 get_stat64(&hw_stats->rx_mcast_packets) +
9418 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009419
Linus Torvalds1da177e2005-04-16 15:20:36 -07009420 stats->tx_packets = old_stats->tx_packets +
9421 get_stat64(&hw_stats->tx_ucast_packets) +
9422 get_stat64(&hw_stats->tx_mcast_packets) +
9423 get_stat64(&hw_stats->tx_bcast_packets);
9424
9425 stats->rx_bytes = old_stats->rx_bytes +
9426 get_stat64(&hw_stats->rx_octets);
9427 stats->tx_bytes = old_stats->tx_bytes +
9428 get_stat64(&hw_stats->tx_octets);
9429
9430 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -07009431 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009432 stats->tx_errors = old_stats->tx_errors +
9433 get_stat64(&hw_stats->tx_errors) +
9434 get_stat64(&hw_stats->tx_mac_errors) +
9435 get_stat64(&hw_stats->tx_carrier_sense_errors) +
9436 get_stat64(&hw_stats->tx_discards);
9437
9438 stats->multicast = old_stats->multicast +
9439 get_stat64(&hw_stats->rx_mcast_packets);
9440 stats->collisions = old_stats->collisions +
9441 get_stat64(&hw_stats->tx_collisions);
9442
9443 stats->rx_length_errors = old_stats->rx_length_errors +
9444 get_stat64(&hw_stats->rx_frame_too_long_errors) +
9445 get_stat64(&hw_stats->rx_undersize_packets);
9446
9447 stats->rx_over_errors = old_stats->rx_over_errors +
9448 get_stat64(&hw_stats->rxbds_empty);
9449 stats->rx_frame_errors = old_stats->rx_frame_errors +
9450 get_stat64(&hw_stats->rx_align_errors);
9451 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
9452 get_stat64(&hw_stats->tx_discards);
9453 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
9454 get_stat64(&hw_stats->tx_carrier_sense_errors);
9455
9456 stats->rx_crc_errors = old_stats->rx_crc_errors +
9457 calc_crc_errors(tp);
9458
John W. Linville4f63b872005-09-12 14:43:18 -07009459 stats->rx_missed_errors = old_stats->rx_missed_errors +
9460 get_stat64(&hw_stats->rx_discards);
9461
Eric Dumazetb0057c52010-10-10 19:55:52 +00009462 stats->rx_dropped = tp->rx_dropped;
9463
Linus Torvalds1da177e2005-04-16 15:20:36 -07009464 return stats;
9465}
9466
9467static inline u32 calc_crc(unsigned char *buf, int len)
9468{
9469 u32 reg;
9470 u32 tmp;
9471 int j, k;
9472
9473 reg = 0xffffffff;
9474
9475 for (j = 0; j < len; j++) {
9476 reg ^= buf[j];
9477
9478 for (k = 0; k < 8; k++) {
9479 tmp = reg & 0x01;
9480
9481 reg >>= 1;
9482
Matt Carlson859a5882010-04-05 10:19:28 +00009483 if (tmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009484 reg ^= 0xedb88320;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009485 }
9486 }
9487
9488 return ~reg;
9489}
9490
9491static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
9492{
9493 /* accept or reject all multicast frames */
9494 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
9495 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
9496 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
9497 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
9498}
9499
9500static void __tg3_set_rx_mode(struct net_device *dev)
9501{
9502 struct tg3 *tp = netdev_priv(dev);
9503 u32 rx_mode;
9504
9505 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
9506 RX_MODE_KEEP_VLAN_TAG);
9507
9508 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
9509 * flag clear.
9510 */
9511#if TG3_VLAN_TAG_USED
9512 if (!tp->vlgrp &&
9513 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
9514 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9515#else
9516 /* By definition, VLAN is disabled always in this
9517 * case.
9518 */
9519 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
9520 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9521#endif
9522
9523 if (dev->flags & IFF_PROMISC) {
9524 /* Promiscuous mode. */
9525 rx_mode |= RX_MODE_PROMISC;
9526 } else if (dev->flags & IFF_ALLMULTI) {
9527 /* Accept all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +00009528 tg3_set_multi(tp, 1);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00009529 } else if (netdev_mc_empty(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009530 /* Reject all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +00009531 tg3_set_multi(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009532 } else {
9533 /* Accept one or more multicast(s). */
Jiri Pirko22bedad2010-04-01 21:22:57 +00009534 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009535 u32 mc_filter[4] = { 0, };
9536 u32 regidx;
9537 u32 bit;
9538 u32 crc;
9539
Jiri Pirko22bedad2010-04-01 21:22:57 +00009540 netdev_for_each_mc_addr(ha, dev) {
9541 crc = calc_crc(ha->addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009542 bit = ~crc & 0x7f;
9543 regidx = (bit & 0x60) >> 5;
9544 bit &= 0x1f;
9545 mc_filter[regidx] |= (1 << bit);
9546 }
9547
9548 tw32(MAC_HASH_REG_0, mc_filter[0]);
9549 tw32(MAC_HASH_REG_1, mc_filter[1]);
9550 tw32(MAC_HASH_REG_2, mc_filter[2]);
9551 tw32(MAC_HASH_REG_3, mc_filter[3]);
9552 }
9553
9554 if (rx_mode != tp->rx_mode) {
9555 tp->rx_mode = rx_mode;
9556 tw32_f(MAC_RX_MODE, rx_mode);
9557 udelay(10);
9558 }
9559}
9560
9561static void tg3_set_rx_mode(struct net_device *dev)
9562{
9563 struct tg3 *tp = netdev_priv(dev);
9564
Michael Chane75f7c92006-03-20 21:33:26 -08009565 if (!netif_running(dev))
9566 return;
9567
David S. Millerf47c11e2005-06-24 20:18:35 -07009568 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009569 __tg3_set_rx_mode(dev);
David S. Millerf47c11e2005-06-24 20:18:35 -07009570 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009571}
9572
9573#define TG3_REGDUMP_LEN (32 * 1024)
9574
9575static int tg3_get_regs_len(struct net_device *dev)
9576{
9577 return TG3_REGDUMP_LEN;
9578}
9579
9580static void tg3_get_regs(struct net_device *dev,
9581 struct ethtool_regs *regs, void *_p)
9582{
9583 u32 *p = _p;
9584 struct tg3 *tp = netdev_priv(dev);
9585 u8 *orig_p = _p;
9586 int i;
9587
9588 regs->version = 0;
9589
9590 memset(p, 0, TG3_REGDUMP_LEN);
9591
Matt Carlson80096062010-08-02 11:26:06 +00009592 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -08009593 return;
9594
David S. Millerf47c11e2005-06-24 20:18:35 -07009595 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009596
9597#define __GET_REG32(reg) (*(p)++ = tr32(reg))
Matt Carlsonbe98da62010-07-11 09:31:46 +00009598#define GET_REG32_LOOP(base, len) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07009599do { p = (u32 *)(orig_p + (base)); \
9600 for (i = 0; i < len; i += 4) \
9601 __GET_REG32((base) + i); \
9602} while (0)
9603#define GET_REG32_1(reg) \
9604do { p = (u32 *)(orig_p + (reg)); \
9605 __GET_REG32((reg)); \
9606} while (0)
9607
9608 GET_REG32_LOOP(TG3PCI_VENDOR, 0xb0);
9609 GET_REG32_LOOP(MAILBOX_INTERRUPT_0, 0x200);
9610 GET_REG32_LOOP(MAC_MODE, 0x4f0);
9611 GET_REG32_LOOP(SNDDATAI_MODE, 0xe0);
9612 GET_REG32_1(SNDDATAC_MODE);
9613 GET_REG32_LOOP(SNDBDS_MODE, 0x80);
9614 GET_REG32_LOOP(SNDBDI_MODE, 0x48);
9615 GET_REG32_1(SNDBDC_MODE);
9616 GET_REG32_LOOP(RCVLPC_MODE, 0x20);
9617 GET_REG32_LOOP(RCVLPC_SELLST_BASE, 0x15c);
9618 GET_REG32_LOOP(RCVDBDI_MODE, 0x0c);
9619 GET_REG32_LOOP(RCVDBDI_JUMBO_BD, 0x3c);
9620 GET_REG32_LOOP(RCVDBDI_BD_PROD_IDX_0, 0x44);
9621 GET_REG32_1(RCVDCC_MODE);
9622 GET_REG32_LOOP(RCVBDI_MODE, 0x20);
9623 GET_REG32_LOOP(RCVCC_MODE, 0x14);
9624 GET_REG32_LOOP(RCVLSC_MODE, 0x08);
9625 GET_REG32_1(MBFREE_MODE);
9626 GET_REG32_LOOP(HOSTCC_MODE, 0x100);
9627 GET_REG32_LOOP(MEMARB_MODE, 0x10);
9628 GET_REG32_LOOP(BUFMGR_MODE, 0x58);
9629 GET_REG32_LOOP(RDMAC_MODE, 0x08);
9630 GET_REG32_LOOP(WDMAC_MODE, 0x08);
Chris Elmquist091465d2005-12-20 13:25:19 -08009631 GET_REG32_1(RX_CPU_MODE);
9632 GET_REG32_1(RX_CPU_STATE);
9633 GET_REG32_1(RX_CPU_PGMCTR);
9634 GET_REG32_1(RX_CPU_HWBKPT);
9635 GET_REG32_1(TX_CPU_MODE);
9636 GET_REG32_1(TX_CPU_STATE);
9637 GET_REG32_1(TX_CPU_PGMCTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009638 GET_REG32_LOOP(GRCMBOX_INTERRUPT_0, 0x110);
9639 GET_REG32_LOOP(FTQ_RESET, 0x120);
9640 GET_REG32_LOOP(MSGINT_MODE, 0x0c);
9641 GET_REG32_1(DMAC_MODE);
9642 GET_REG32_LOOP(GRC_MODE, 0x4c);
9643 if (tp->tg3_flags & TG3_FLAG_NVRAM)
9644 GET_REG32_LOOP(NVRAM_CMD, 0x24);
9645
9646#undef __GET_REG32
9647#undef GET_REG32_LOOP
9648#undef GET_REG32_1
9649
David S. Millerf47c11e2005-06-24 20:18:35 -07009650 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009651}
9652
9653static int tg3_get_eeprom_len(struct net_device *dev)
9654{
9655 struct tg3 *tp = netdev_priv(dev);
9656
9657 return tp->nvram_size;
9658}
9659
Linus Torvalds1da177e2005-04-16 15:20:36 -07009660static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9661{
9662 struct tg3 *tp = netdev_priv(dev);
9663 int ret;
9664 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -08009665 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009666 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009667
Matt Carlsondf259d82009-04-20 06:57:14 +00009668 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM)
9669 return -EINVAL;
9670
Matt Carlson80096062010-08-02 11:26:06 +00009671 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -08009672 return -EAGAIN;
9673
Linus Torvalds1da177e2005-04-16 15:20:36 -07009674 offset = eeprom->offset;
9675 len = eeprom->len;
9676 eeprom->len = 0;
9677
9678 eeprom->magic = TG3_EEPROM_MAGIC;
9679
9680 if (offset & 3) {
9681 /* adjustments to start on required 4 byte boundary */
9682 b_offset = offset & 3;
9683 b_count = 4 - b_offset;
9684 if (b_count > len) {
9685 /* i.e. offset=1 len=2 */
9686 b_count = len;
9687 }
Matt Carlsona9dc5292009-02-25 14:25:30 +00009688 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009689 if (ret)
9690 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +00009691 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009692 len -= b_count;
9693 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00009694 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009695 }
9696
9697 /* read bytes upto the last 4 byte boundary */
9698 pd = &data[eeprom->len];
9699 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +00009700 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009701 if (ret) {
9702 eeprom->len += i;
9703 return ret;
9704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009705 memcpy(pd + i, &val, 4);
9706 }
9707 eeprom->len += i;
9708
9709 if (len & 3) {
9710 /* read last bytes not ending on 4 byte boundary */
9711 pd = &data[eeprom->len];
9712 b_count = len & 3;
9713 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009714 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009715 if (ret)
9716 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009717 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009718 eeprom->len += b_count;
9719 }
9720 return 0;
9721}
9722
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009723static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009724
9725static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9726{
9727 struct tg3 *tp = netdev_priv(dev);
9728 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009729 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009730 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009731 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009732
Matt Carlson80096062010-08-02 11:26:06 +00009733 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -08009734 return -EAGAIN;
9735
Matt Carlsondf259d82009-04-20 06:57:14 +00009736 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
9737 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009738 return -EINVAL;
9739
9740 offset = eeprom->offset;
9741 len = eeprom->len;
9742
9743 if ((b_offset = (offset & 3))) {
9744 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +00009745 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009746 if (ret)
9747 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009748 len += b_offset;
9749 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -07009750 if (len < 4)
9751 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009752 }
9753
9754 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -07009755 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009756 /* adjustments to end on required 4 byte boundary */
9757 odd_len = 1;
9758 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009759 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009760 if (ret)
9761 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009762 }
9763
9764 buf = data;
9765 if (b_offset || odd_len) {
9766 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009767 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009768 return -ENOMEM;
9769 if (b_offset)
9770 memcpy(buf, &start, 4);
9771 if (odd_len)
9772 memcpy(buf+len-4, &end, 4);
9773 memcpy(buf + b_offset, data, eeprom->len);
9774 }
9775
9776 ret = tg3_nvram_write_block(tp, offset, len, buf);
9777
9778 if (buf != data)
9779 kfree(buf);
9780
9781 return ret;
9782}
9783
9784static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9785{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009786 struct tg3 *tp = netdev_priv(dev);
9787
9788 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009789 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009790 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009791 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009792 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
9793 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009794 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009795
Linus Torvalds1da177e2005-04-16 15:20:36 -07009796 cmd->supported = (SUPPORTED_Autoneg);
9797
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009798 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009799 cmd->supported |= (SUPPORTED_1000baseT_Half |
9800 SUPPORTED_1000baseT_Full);
9801
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009802 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009803 cmd->supported |= (SUPPORTED_100baseT_Half |
9804 SUPPORTED_100baseT_Full |
9805 SUPPORTED_10baseT_Half |
9806 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -08009807 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -07009808 cmd->port = PORT_TP;
9809 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009810 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -07009811 cmd->port = PORT_FIBRE;
9812 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009813
Linus Torvalds1da177e2005-04-16 15:20:36 -07009814 cmd->advertising = tp->link_config.advertising;
9815 if (netif_running(dev)) {
9816 cmd->speed = tp->link_config.active_speed;
9817 cmd->duplex = tp->link_config.active_duplex;
Matt Carlson64c22182010-10-14 10:37:44 +00009818 } else {
9819 cmd->speed = SPEED_INVALID;
9820 cmd->duplex = DUPLEX_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009821 }
Matt Carlson882e9792009-09-01 13:21:36 +00009822 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +00009823 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009824 cmd->autoneg = tp->link_config.autoneg;
9825 cmd->maxtxpkt = 0;
9826 cmd->maxrxpkt = 0;
9827 return 0;
9828}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009829
Linus Torvalds1da177e2005-04-16 15:20:36 -07009830static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9831{
9832 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009833
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009834 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009835 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009836 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009837 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009838 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
9839 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009840 }
9841
Matt Carlson7e5856b2009-02-25 14:23:01 +00009842 if (cmd->autoneg != AUTONEG_ENABLE &&
9843 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -07009844 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +00009845
9846 if (cmd->autoneg == AUTONEG_DISABLE &&
9847 cmd->duplex != DUPLEX_FULL &&
9848 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -07009849 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009850
Matt Carlson7e5856b2009-02-25 14:23:01 +00009851 if (cmd->autoneg == AUTONEG_ENABLE) {
9852 u32 mask = ADVERTISED_Autoneg |
9853 ADVERTISED_Pause |
9854 ADVERTISED_Asym_Pause;
9855
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009856 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +00009857 mask |= ADVERTISED_1000baseT_Half |
9858 ADVERTISED_1000baseT_Full;
9859
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009860 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +00009861 mask |= ADVERTISED_100baseT_Half |
9862 ADVERTISED_100baseT_Full |
9863 ADVERTISED_10baseT_Half |
9864 ADVERTISED_10baseT_Full |
9865 ADVERTISED_TP;
9866 else
9867 mask |= ADVERTISED_FIBRE;
9868
9869 if (cmd->advertising & ~mask)
9870 return -EINVAL;
9871
9872 mask &= (ADVERTISED_1000baseT_Half |
9873 ADVERTISED_1000baseT_Full |
9874 ADVERTISED_100baseT_Half |
9875 ADVERTISED_100baseT_Full |
9876 ADVERTISED_10baseT_Half |
9877 ADVERTISED_10baseT_Full);
9878
9879 cmd->advertising &= mask;
9880 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009881 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
Matt Carlson7e5856b2009-02-25 14:23:01 +00009882 if (cmd->speed != SPEED_1000)
9883 return -EINVAL;
9884
9885 if (cmd->duplex != DUPLEX_FULL)
9886 return -EINVAL;
9887 } else {
9888 if (cmd->speed != SPEED_100 &&
9889 cmd->speed != SPEED_10)
9890 return -EINVAL;
9891 }
9892 }
9893
David S. Millerf47c11e2005-06-24 20:18:35 -07009894 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009895
9896 tp->link_config.autoneg = cmd->autoneg;
9897 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -07009898 tp->link_config.advertising = (cmd->advertising |
9899 ADVERTISED_Autoneg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009900 tp->link_config.speed = SPEED_INVALID;
9901 tp->link_config.duplex = DUPLEX_INVALID;
9902 } else {
9903 tp->link_config.advertising = 0;
9904 tp->link_config.speed = cmd->speed;
9905 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009906 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009907
Michael Chan24fcad62006-12-17 17:06:46 -08009908 tp->link_config.orig_speed = tp->link_config.speed;
9909 tp->link_config.orig_duplex = tp->link_config.duplex;
9910 tp->link_config.orig_autoneg = tp->link_config.autoneg;
9911
Linus Torvalds1da177e2005-04-16 15:20:36 -07009912 if (netif_running(dev))
9913 tg3_setup_phy(tp, 1);
9914
David S. Millerf47c11e2005-06-24 20:18:35 -07009915 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009916
Linus Torvalds1da177e2005-04-16 15:20:36 -07009917 return 0;
9918}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009919
Linus Torvalds1da177e2005-04-16 15:20:36 -07009920static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
9921{
9922 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009923
Linus Torvalds1da177e2005-04-16 15:20:36 -07009924 strcpy(info->driver, DRV_MODULE_NAME);
9925 strcpy(info->version, DRV_MODULE_VERSION);
Michael Chanc4e65752006-03-20 22:29:32 -08009926 strcpy(info->fw_version, tp->fw_ver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009927 strcpy(info->bus_info, pci_name(tp->pdev));
9928}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009929
Linus Torvalds1da177e2005-04-16 15:20:36 -07009930static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
9931{
9932 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009933
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009934 if ((tp->tg3_flags & TG3_FLAG_WOL_CAP) &&
9935 device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -07009936 wol->supported = WAKE_MAGIC;
9937 else
9938 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009939 wol->wolopts = 0;
Matt Carlson05ac4cb2008-11-03 16:53:46 -08009940 if ((tp->tg3_flags & TG3_FLAG_WOL_ENABLE) &&
9941 device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009942 wol->wolopts = WAKE_MAGIC;
9943 memset(&wol->sopass, 0, sizeof(wol->sopass));
9944}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009945
Linus Torvalds1da177e2005-04-16 15:20:36 -07009946static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
9947{
9948 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009949 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009950
Linus Torvalds1da177e2005-04-16 15:20:36 -07009951 if (wol->wolopts & ~WAKE_MAGIC)
9952 return -EINVAL;
9953 if ((wol->wolopts & WAKE_MAGIC) &&
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009954 !((tp->tg3_flags & TG3_FLAG_WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009955 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009956
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +00009957 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
9958
David S. Millerf47c11e2005-06-24 20:18:35 -07009959 spin_lock_bh(&tp->lock);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +00009960 if (device_may_wakeup(dp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009961 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +00009962 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07009963 tp->tg3_flags &= ~TG3_FLAG_WOL_ENABLE;
David S. Millerf47c11e2005-06-24 20:18:35 -07009964 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009965
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +00009966
Linus Torvalds1da177e2005-04-16 15:20:36 -07009967 return 0;
9968}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009969
Linus Torvalds1da177e2005-04-16 15:20:36 -07009970static u32 tg3_get_msglevel(struct net_device *dev)
9971{
9972 struct tg3 *tp = netdev_priv(dev);
9973 return tp->msg_enable;
9974}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009975
Linus Torvalds1da177e2005-04-16 15:20:36 -07009976static void tg3_set_msglevel(struct net_device *dev, u32 value)
9977{
9978 struct tg3 *tp = netdev_priv(dev);
9979 tp->msg_enable = value;
9980}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009981
Linus Torvalds1da177e2005-04-16 15:20:36 -07009982static int tg3_set_tso(struct net_device *dev, u32 value)
9983{
9984 struct tg3 *tp = netdev_priv(dev);
9985
9986 if (!(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
9987 if (value)
9988 return -EINVAL;
9989 return 0;
9990 }
Matt Carlson027455a2008-12-21 20:19:30 -08009991 if ((dev->features & NETIF_F_IPV6_CSUM) &&
Matt Carlsone849cdc2009-11-13 13:03:38 +00009992 ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_2) ||
9993 (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3))) {
Matt Carlson9936bcf2007-10-10 18:03:07 -07009994 if (value) {
Michael Chanb0026622006-07-03 19:42:14 -07009995 dev->features |= NETIF_F_TSO6;
Matt Carlsone849cdc2009-11-13 13:03:38 +00009996 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
9997 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -07009998 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
9999 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080010000 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000010001 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson9936bcf2007-10-10 18:03:07 -070010002 dev->features |= NETIF_F_TSO_ECN;
10003 } else
10004 dev->features &= ~(NETIF_F_TSO6 | NETIF_F_TSO_ECN);
Michael Chanb0026622006-07-03 19:42:14 -070010005 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010006 return ethtool_op_set_tso(dev, value);
10007}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010008
Linus Torvalds1da177e2005-04-16 15:20:36 -070010009static int tg3_nway_reset(struct net_device *dev)
10010{
10011 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010012 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010013
Linus Torvalds1da177e2005-04-16 15:20:36 -070010014 if (!netif_running(dev))
10015 return -EAGAIN;
10016
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010017 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070010018 return -EINVAL;
10019
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010020 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010021 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010022 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010023 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010024 } else {
10025 u32 bmcr;
10026
10027 spin_lock_bh(&tp->lock);
10028 r = -EINVAL;
10029 tg3_readphy(tp, MII_BMCR, &bmcr);
10030 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
10031 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010032 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010033 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
10034 BMCR_ANENABLE);
10035 r = 0;
10036 }
10037 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010038 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010039
Linus Torvalds1da177e2005-04-16 15:20:36 -070010040 return r;
10041}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010042
Linus Torvalds1da177e2005-04-16 15:20:36 -070010043static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10044{
10045 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010046
Matt Carlson2c49a442010-09-30 10:34:35 +000010047 ering->rx_max_pending = tp->rx_std_ring_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010048 ering->rx_mini_max_pending = 0;
Michael Chan4f81c322006-03-20 21:33:42 -080010049 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE)
Matt Carlson2c49a442010-09-30 10:34:35 +000010050 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080010051 else
10052 ering->rx_jumbo_max_pending = 0;
10053
10054 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010055
10056 ering->rx_pending = tp->rx_pending;
10057 ering->rx_mini_pending = 0;
Michael Chan4f81c322006-03-20 21:33:42 -080010058 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE)
10059 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
10060 else
10061 ering->rx_jumbo_pending = 0;
10062
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010063 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010064}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010065
Linus Torvalds1da177e2005-04-16 15:20:36 -070010066static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10067{
10068 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000010069 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010070
Matt Carlson2c49a442010-09-30 10:34:35 +000010071 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
10072 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070010073 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
10074 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Michael Chan7f62ad52007-02-20 23:25:40 -080010075 ((tp->tg3_flags2 & TG3_FLG2_TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070010076 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010077 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010078
Michael Chanbbe832c2005-06-24 20:20:04 -070010079 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010080 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010081 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010082 irq_sync = 1;
10083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010084
Michael Chanbbe832c2005-06-24 20:20:04 -070010085 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010086
Linus Torvalds1da177e2005-04-16 15:20:36 -070010087 tp->rx_pending = ering->rx_pending;
10088
10089 if ((tp->tg3_flags2 & TG3_FLG2_MAX_RXPEND_64) &&
10090 tp->rx_pending > 63)
10091 tp->rx_pending = 63;
10092 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000010093
Matt Carlson6fd45cb2010-09-15 08:59:57 +000010094 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000010095 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010096
10097 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070010098 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -070010099 err = tg3_restart_hw(tp, 1);
10100 if (!err)
10101 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010102 }
10103
David S. Millerf47c11e2005-06-24 20:18:35 -070010104 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010105
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010106 if (irq_sync && !err)
10107 tg3_phy_start(tp);
10108
Michael Chanb9ec6c12006-07-25 16:37:27 -070010109 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010110}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010111
Linus Torvalds1da177e2005-04-16 15:20:36 -070010112static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10113{
10114 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010115
Linus Torvalds1da177e2005-04-16 15:20:36 -070010116 epause->autoneg = (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG) != 0;
Matt Carlson8d018622007-12-20 20:05:44 -080010117
Steve Glendinninge18ce342008-12-16 02:00:00 -080010118 if (tp->link_config.active_flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080010119 epause->rx_pause = 1;
10120 else
10121 epause->rx_pause = 0;
10122
Steve Glendinninge18ce342008-12-16 02:00:00 -080010123 if (tp->link_config.active_flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080010124 epause->tx_pause = 1;
10125 else
10126 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010127}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010128
Linus Torvalds1da177e2005-04-16 15:20:36 -070010129static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10130{
10131 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010132 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010133
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010134 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson27121682010-02-17 15:16:57 +000010135 u32 newadv;
10136 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010137
Matt Carlson27121682010-02-17 15:16:57 +000010138 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010139
Matt Carlson27121682010-02-17 15:16:57 +000010140 if (!(phydev->supported & SUPPORTED_Pause) ||
10141 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000010142 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000010143 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010144
Matt Carlson27121682010-02-17 15:16:57 +000010145 tp->link_config.flowctrl = 0;
10146 if (epause->rx_pause) {
10147 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010148
Matt Carlson27121682010-02-17 15:16:57 +000010149 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080010150 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000010151 newadv = ADVERTISED_Pause;
10152 } else
10153 newadv = ADVERTISED_Pause |
10154 ADVERTISED_Asym_Pause;
10155 } else if (epause->tx_pause) {
10156 tp->link_config.flowctrl |= FLOW_CTRL_TX;
10157 newadv = ADVERTISED_Asym_Pause;
10158 } else
10159 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010160
Matt Carlson27121682010-02-17 15:16:57 +000010161 if (epause->autoneg)
10162 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
10163 else
10164 tp->tg3_flags &= ~TG3_FLAG_PAUSE_AUTONEG;
10165
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010166 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000010167 u32 oldadv = phydev->advertising &
10168 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
10169 if (oldadv != newadv) {
10170 phydev->advertising &=
10171 ~(ADVERTISED_Pause |
10172 ADVERTISED_Asym_Pause);
10173 phydev->advertising |= newadv;
10174 if (phydev->autoneg) {
10175 /*
10176 * Always renegotiate the link to
10177 * inform our link partner of our
10178 * flow control settings, even if the
10179 * flow control is forced. Let
10180 * tg3_adjust_link() do the final
10181 * flow control setup.
10182 */
10183 return phy_start_aneg(phydev);
10184 }
10185 }
10186
10187 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010188 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000010189 } else {
10190 tp->link_config.orig_advertising &=
10191 ~(ADVERTISED_Pause |
10192 ADVERTISED_Asym_Pause);
10193 tp->link_config.orig_advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010194 }
10195 } else {
10196 int irq_sync = 0;
10197
10198 if (netif_running(dev)) {
10199 tg3_netif_stop(tp);
10200 irq_sync = 1;
10201 }
10202
10203 tg3_full_lock(tp, irq_sync);
10204
10205 if (epause->autoneg)
10206 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
10207 else
10208 tp->tg3_flags &= ~TG3_FLAG_PAUSE_AUTONEG;
10209 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010210 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010211 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010212 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010213 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010214 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010215 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010216 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010217
10218 if (netif_running(dev)) {
10219 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10220 err = tg3_restart_hw(tp, 1);
10221 if (!err)
10222 tg3_netif_start(tp);
10223 }
10224
10225 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010227
Michael Chanb9ec6c12006-07-25 16:37:27 -070010228 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010229}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010230
Linus Torvalds1da177e2005-04-16 15:20:36 -070010231static u32 tg3_get_rx_csum(struct net_device *dev)
10232{
10233 struct tg3 *tp = netdev_priv(dev);
10234 return (tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) != 0;
10235}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010236
Linus Torvalds1da177e2005-04-16 15:20:36 -070010237static int tg3_set_rx_csum(struct net_device *dev, u32 data)
10238{
10239 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010240
Linus Torvalds1da177e2005-04-16 15:20:36 -070010241 if (tp->tg3_flags & TG3_FLAG_BROKEN_CHECKSUMS) {
10242 if (data != 0)
10243 return -EINVAL;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010244 return 0;
10245 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010246
David S. Millerf47c11e2005-06-24 20:18:35 -070010247 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010248 if (data)
10249 tp->tg3_flags |= TG3_FLAG_RX_CHECKSUMS;
10250 else
10251 tp->tg3_flags &= ~TG3_FLAG_RX_CHECKSUMS;
David S. Millerf47c11e2005-06-24 20:18:35 -070010252 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010253
Linus Torvalds1da177e2005-04-16 15:20:36 -070010254 return 0;
10255}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010256
Linus Torvalds1da177e2005-04-16 15:20:36 -070010257static int tg3_set_tx_csum(struct net_device *dev, u32 data)
10258{
10259 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010260
Linus Torvalds1da177e2005-04-16 15:20:36 -070010261 if (tp->tg3_flags & TG3_FLAG_BROKEN_CHECKSUMS) {
10262 if (data != 0)
10263 return -EINVAL;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010264 return 0;
10265 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010266
Matt Carlson321d32a2008-11-21 17:22:19 -080010267 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Michael Chan6460d942007-07-14 19:07:52 -070010268 ethtool_op_set_tx_ipv6_csum(dev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010269 else
Michael Chan9c27dbd2006-03-20 22:28:27 -080010270 ethtool_op_set_tx_csum(dev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010271
10272 return 0;
10273}
10274
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010275static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010276{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010277 switch (sset) {
10278 case ETH_SS_TEST:
10279 return TG3_NUM_TEST;
10280 case ETH_SS_STATS:
10281 return TG3_NUM_STATS;
10282 default:
10283 return -EOPNOTSUPP;
10284 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070010285}
10286
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010287static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010288{
10289 switch (stringset) {
10290 case ETH_SS_STATS:
10291 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
10292 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070010293 case ETH_SS_TEST:
10294 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
10295 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010296 default:
10297 WARN_ON(1); /* we need a WARN() */
10298 break;
10299 }
10300}
10301
Michael Chan4009a932005-09-05 17:52:54 -070010302static int tg3_phys_id(struct net_device *dev, u32 data)
10303{
10304 struct tg3 *tp = netdev_priv(dev);
10305 int i;
10306
10307 if (!netif_running(tp->dev))
10308 return -EAGAIN;
10309
10310 if (data == 0)
Stephen Hemminger759afc32008-02-23 19:51:59 -080010311 data = UINT_MAX / 2;
Michael Chan4009a932005-09-05 17:52:54 -070010312
10313 for (i = 0; i < (data * 2); i++) {
10314 if ((i % 2) == 0)
10315 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10316 LED_CTRL_1000MBPS_ON |
10317 LED_CTRL_100MBPS_ON |
10318 LED_CTRL_10MBPS_ON |
10319 LED_CTRL_TRAFFIC_OVERRIDE |
10320 LED_CTRL_TRAFFIC_BLINK |
10321 LED_CTRL_TRAFFIC_LED);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010322
Michael Chan4009a932005-09-05 17:52:54 -070010323 else
10324 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10325 LED_CTRL_TRAFFIC_OVERRIDE);
10326
10327 if (msleep_interruptible(500))
10328 break;
10329 }
10330 tw32(MAC_LED_CTRL, tp->led_ctrl);
10331 return 0;
10332}
10333
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010334static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010335 struct ethtool_stats *estats, u64 *tmp_stats)
10336{
10337 struct tg3 *tp = netdev_priv(dev);
10338 memcpy(tmp_stats, tg3_get_estats(tp), sizeof(tp->estats));
10339}
10340
Michael Chan566f86a2005-05-29 14:56:58 -070010341#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080010342#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
10343#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
10344#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Michael Chanb16250e2006-09-27 16:10:14 -070010345#define NVRAM_SELFBOOT_HW_SIZE 0x20
10346#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070010347
10348static int tg3_test_nvram(struct tg3 *tp)
10349{
Al Virob9fc7dc2007-12-17 22:59:57 -080010350 u32 csum, magic;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010351 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010352 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070010353
Matt Carlsondf259d82009-04-20 06:57:14 +000010354 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM)
10355 return 0;
10356
Matt Carlsone4f34112009-02-25 14:25:00 +000010357 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080010358 return -EIO;
10359
Michael Chan1b277772006-03-20 22:27:48 -080010360 if (magic == TG3_EEPROM_MAGIC)
10361 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070010362 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080010363 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
10364 TG3_EEPROM_SB_FORMAT_1) {
10365 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
10366 case TG3_EEPROM_SB_REVISION_0:
10367 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
10368 break;
10369 case TG3_EEPROM_SB_REVISION_2:
10370 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
10371 break;
10372 case TG3_EEPROM_SB_REVISION_3:
10373 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
10374 break;
10375 default:
10376 return 0;
10377 }
10378 } else
Michael Chan1b277772006-03-20 22:27:48 -080010379 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070010380 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
10381 size = NVRAM_SELFBOOT_HW_SIZE;
10382 else
Michael Chan1b277772006-03-20 22:27:48 -080010383 return -EIO;
10384
10385 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070010386 if (buf == NULL)
10387 return -ENOMEM;
10388
Michael Chan1b277772006-03-20 22:27:48 -080010389 err = -EIO;
10390 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010391 err = tg3_nvram_read_be32(tp, i, &buf[j]);
10392 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070010393 break;
Michael Chan566f86a2005-05-29 14:56:58 -070010394 }
Michael Chan1b277772006-03-20 22:27:48 -080010395 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070010396 goto out;
10397
Michael Chan1b277772006-03-20 22:27:48 -080010398 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010399 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080010400 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010401 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080010402 u8 *buf8 = (u8 *) buf, csum8 = 0;
10403
Al Virob9fc7dc2007-12-17 22:59:57 -080010404 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080010405 TG3_EEPROM_SB_REVISION_2) {
10406 /* For rev 2, the csum doesn't include the MBA. */
10407 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
10408 csum8 += buf8[i];
10409 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
10410 csum8 += buf8[i];
10411 } else {
10412 for (i = 0; i < size; i++)
10413 csum8 += buf8[i];
10414 }
Michael Chan1b277772006-03-20 22:27:48 -080010415
Adrian Bunkad96b482006-04-05 22:21:04 -070010416 if (csum8 == 0) {
10417 err = 0;
10418 goto out;
10419 }
10420
10421 err = -EIO;
10422 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080010423 }
Michael Chan566f86a2005-05-29 14:56:58 -070010424
Al Virob9fc7dc2007-12-17 22:59:57 -080010425 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010426 TG3_EEPROM_MAGIC_HW) {
10427 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000010428 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070010429 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070010430
10431 /* Separate the parity bits and the data bytes. */
10432 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
10433 if ((i == 0) || (i == 8)) {
10434 int l;
10435 u8 msk;
10436
10437 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
10438 parity[k++] = buf8[i] & msk;
10439 i++;
Matt Carlson859a5882010-04-05 10:19:28 +000010440 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070010441 int l;
10442 u8 msk;
10443
10444 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
10445 parity[k++] = buf8[i] & msk;
10446 i++;
10447
10448 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
10449 parity[k++] = buf8[i] & msk;
10450 i++;
10451 }
10452 data[j++] = buf8[i];
10453 }
10454
10455 err = -EIO;
10456 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
10457 u8 hw8 = hweight8(data[i]);
10458
10459 if ((hw8 & 0x1) && parity[i])
10460 goto out;
10461 else if (!(hw8 & 0x1) && !parity[i])
10462 goto out;
10463 }
10464 err = 0;
10465 goto out;
10466 }
10467
Michael Chan566f86a2005-05-29 14:56:58 -070010468 /* Bootstrap checksum at offset 0x10 */
10469 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlsona9dc5292009-02-25 14:25:30 +000010470 if (csum != be32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070010471 goto out;
10472
10473 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
10474 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlsona9dc5292009-02-25 14:25:30 +000010475 if (csum != be32_to_cpu(buf[0xfc/4]))
10476 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070010477
10478 err = 0;
10479
10480out:
10481 kfree(buf);
10482 return err;
10483}
10484
Michael Chanca430072005-05-29 14:57:23 -070010485#define TG3_SERDES_TIMEOUT_SEC 2
10486#define TG3_COPPER_TIMEOUT_SEC 6
10487
10488static int tg3_test_link(struct tg3 *tp)
10489{
10490 int i, max;
10491
10492 if (!netif_running(tp->dev))
10493 return -ENODEV;
10494
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010495 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070010496 max = TG3_SERDES_TIMEOUT_SEC;
10497 else
10498 max = TG3_COPPER_TIMEOUT_SEC;
10499
10500 for (i = 0; i < max; i++) {
10501 if (netif_carrier_ok(tp->dev))
10502 return 0;
10503
10504 if (msleep_interruptible(1000))
10505 break;
10506 }
10507
10508 return -EIO;
10509}
10510
Michael Chana71116d2005-05-29 14:58:11 -070010511/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080010512static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070010513{
Michael Chanb16250e2006-09-27 16:10:14 -070010514 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070010515 u32 offset, read_mask, write_mask, val, save_val, read_val;
10516 static struct {
10517 u16 offset;
10518 u16 flags;
10519#define TG3_FL_5705 0x1
10520#define TG3_FL_NOT_5705 0x2
10521#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070010522#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070010523 u32 read_mask;
10524 u32 write_mask;
10525 } reg_tbl[] = {
10526 /* MAC Control Registers */
10527 { MAC_MODE, TG3_FL_NOT_5705,
10528 0x00000000, 0x00ef6f8c },
10529 { MAC_MODE, TG3_FL_5705,
10530 0x00000000, 0x01ef6b8c },
10531 { MAC_STATUS, TG3_FL_NOT_5705,
10532 0x03800107, 0x00000000 },
10533 { MAC_STATUS, TG3_FL_5705,
10534 0x03800100, 0x00000000 },
10535 { MAC_ADDR_0_HIGH, 0x0000,
10536 0x00000000, 0x0000ffff },
10537 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010538 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070010539 { MAC_RX_MTU_SIZE, 0x0000,
10540 0x00000000, 0x0000ffff },
10541 { MAC_TX_MODE, 0x0000,
10542 0x00000000, 0x00000070 },
10543 { MAC_TX_LENGTHS, 0x0000,
10544 0x00000000, 0x00003fff },
10545 { MAC_RX_MODE, TG3_FL_NOT_5705,
10546 0x00000000, 0x000007fc },
10547 { MAC_RX_MODE, TG3_FL_5705,
10548 0x00000000, 0x000007dc },
10549 { MAC_HASH_REG_0, 0x0000,
10550 0x00000000, 0xffffffff },
10551 { MAC_HASH_REG_1, 0x0000,
10552 0x00000000, 0xffffffff },
10553 { MAC_HASH_REG_2, 0x0000,
10554 0x00000000, 0xffffffff },
10555 { MAC_HASH_REG_3, 0x0000,
10556 0x00000000, 0xffffffff },
10557
10558 /* Receive Data and Receive BD Initiator Control Registers. */
10559 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
10560 0x00000000, 0xffffffff },
10561 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
10562 0x00000000, 0xffffffff },
10563 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
10564 0x00000000, 0x00000003 },
10565 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
10566 0x00000000, 0xffffffff },
10567 { RCVDBDI_STD_BD+0, 0x0000,
10568 0x00000000, 0xffffffff },
10569 { RCVDBDI_STD_BD+4, 0x0000,
10570 0x00000000, 0xffffffff },
10571 { RCVDBDI_STD_BD+8, 0x0000,
10572 0x00000000, 0xffff0002 },
10573 { RCVDBDI_STD_BD+0xc, 0x0000,
10574 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010575
Michael Chana71116d2005-05-29 14:58:11 -070010576 /* Receive BD Initiator Control Registers. */
10577 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
10578 0x00000000, 0xffffffff },
10579 { RCVBDI_STD_THRESH, TG3_FL_5705,
10580 0x00000000, 0x000003ff },
10581 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
10582 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010583
Michael Chana71116d2005-05-29 14:58:11 -070010584 /* Host Coalescing Control Registers. */
10585 { HOSTCC_MODE, TG3_FL_NOT_5705,
10586 0x00000000, 0x00000004 },
10587 { HOSTCC_MODE, TG3_FL_5705,
10588 0x00000000, 0x000000f6 },
10589 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
10590 0x00000000, 0xffffffff },
10591 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
10592 0x00000000, 0x000003ff },
10593 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
10594 0x00000000, 0xffffffff },
10595 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
10596 0x00000000, 0x000003ff },
10597 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
10598 0x00000000, 0xffffffff },
10599 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10600 0x00000000, 0x000000ff },
10601 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
10602 0x00000000, 0xffffffff },
10603 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10604 0x00000000, 0x000000ff },
10605 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
10606 0x00000000, 0xffffffff },
10607 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
10608 0x00000000, 0xffffffff },
10609 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10610 0x00000000, 0xffffffff },
10611 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10612 0x00000000, 0x000000ff },
10613 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10614 0x00000000, 0xffffffff },
10615 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10616 0x00000000, 0x000000ff },
10617 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
10618 0x00000000, 0xffffffff },
10619 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
10620 0x00000000, 0xffffffff },
10621 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
10622 0x00000000, 0xffffffff },
10623 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
10624 0x00000000, 0xffffffff },
10625 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
10626 0x00000000, 0xffffffff },
10627 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
10628 0xffffffff, 0x00000000 },
10629 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
10630 0xffffffff, 0x00000000 },
10631
10632 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070010633 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010634 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070010635 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010636 0x00000000, 0x007fffff },
10637 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
10638 0x00000000, 0x0000003f },
10639 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
10640 0x00000000, 0x000001ff },
10641 { BUFMGR_MB_HIGH_WATER, 0x0000,
10642 0x00000000, 0x000001ff },
10643 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
10644 0xffffffff, 0x00000000 },
10645 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
10646 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010647
Michael Chana71116d2005-05-29 14:58:11 -070010648 /* Mailbox Registers */
10649 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
10650 0x00000000, 0x000001ff },
10651 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
10652 0x00000000, 0x000001ff },
10653 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
10654 0x00000000, 0x000007ff },
10655 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
10656 0x00000000, 0x000001ff },
10657
10658 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
10659 };
10660
Michael Chanb16250e2006-09-27 16:10:14 -070010661 is_5705 = is_5750 = 0;
10662 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
Michael Chana71116d2005-05-29 14:58:11 -070010663 is_5705 = 1;
Michael Chanb16250e2006-09-27 16:10:14 -070010664 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
10665 is_5750 = 1;
10666 }
Michael Chana71116d2005-05-29 14:58:11 -070010667
10668 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
10669 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
10670 continue;
10671
10672 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
10673 continue;
10674
10675 if ((tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
10676 (reg_tbl[i].flags & TG3_FL_NOT_5788))
10677 continue;
10678
Michael Chanb16250e2006-09-27 16:10:14 -070010679 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
10680 continue;
10681
Michael Chana71116d2005-05-29 14:58:11 -070010682 offset = (u32) reg_tbl[i].offset;
10683 read_mask = reg_tbl[i].read_mask;
10684 write_mask = reg_tbl[i].write_mask;
10685
10686 /* Save the original register content */
10687 save_val = tr32(offset);
10688
10689 /* Determine the read-only value. */
10690 read_val = save_val & read_mask;
10691
10692 /* Write zero to the register, then make sure the read-only bits
10693 * are not changed and the read/write bits are all zeros.
10694 */
10695 tw32(offset, 0);
10696
10697 val = tr32(offset);
10698
10699 /* Test the read-only and read/write bits. */
10700 if (((val & read_mask) != read_val) || (val & write_mask))
10701 goto out;
10702
10703 /* Write ones to all the bits defined by RdMask and WrMask, then
10704 * make sure the read-only bits are not changed and the
10705 * read/write bits are all ones.
10706 */
10707 tw32(offset, read_mask | write_mask);
10708
10709 val = tr32(offset);
10710
10711 /* Test the read-only bits. */
10712 if ((val & read_mask) != read_val)
10713 goto out;
10714
10715 /* Test the read/write bits. */
10716 if ((val & write_mask) != write_mask)
10717 goto out;
10718
10719 tw32(offset, save_val);
10720 }
10721
10722 return 0;
10723
10724out:
Michael Chan9f88f292006-12-07 00:22:54 -080010725 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000010726 netdev_err(tp->dev,
10727 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070010728 tw32(offset, save_val);
10729 return -EIO;
10730}
10731
Michael Chan7942e1d2005-05-29 14:58:36 -070010732static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
10733{
Arjan van de Venf71e1302006-03-03 21:33:57 -050010734 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070010735 int i;
10736 u32 j;
10737
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020010738 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070010739 for (j = 0; j < len; j += 4) {
10740 u32 val;
10741
10742 tg3_write_mem(tp, offset + j, test_pattern[i]);
10743 tg3_read_mem(tp, offset + j, &val);
10744 if (val != test_pattern[i])
10745 return -EIO;
10746 }
10747 }
10748 return 0;
10749}
10750
10751static int tg3_test_memory(struct tg3 *tp)
10752{
10753 static struct mem_entry {
10754 u32 offset;
10755 u32 len;
10756 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080010757 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070010758 { 0x00002000, 0x1c000},
10759 { 0xffffffff, 0x00000}
10760 }, mem_tbl_5705[] = {
10761 { 0x00000100, 0x0000c},
10762 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070010763 { 0x00004000, 0x00800},
10764 { 0x00006000, 0x01000},
10765 { 0x00008000, 0x02000},
10766 { 0x00010000, 0x0e000},
10767 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080010768 }, mem_tbl_5755[] = {
10769 { 0x00000200, 0x00008},
10770 { 0x00004000, 0x00800},
10771 { 0x00006000, 0x00800},
10772 { 0x00008000, 0x02000},
10773 { 0x00010000, 0x0c000},
10774 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070010775 }, mem_tbl_5906[] = {
10776 { 0x00000200, 0x00008},
10777 { 0x00004000, 0x00400},
10778 { 0x00006000, 0x00400},
10779 { 0x00008000, 0x01000},
10780 { 0x00010000, 0x01000},
10781 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000010782 }, mem_tbl_5717[] = {
10783 { 0x00000200, 0x00008},
10784 { 0x00010000, 0x0a000},
10785 { 0x00020000, 0x13c00},
10786 { 0xffffffff, 0x00000}
10787 }, mem_tbl_57765[] = {
10788 { 0x00000200, 0x00008},
10789 { 0x00004000, 0x00800},
10790 { 0x00006000, 0x09800},
10791 { 0x00010000, 0x0a000},
10792 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070010793 };
10794 struct mem_entry *mem_tbl;
10795 int err = 0;
10796 int i;
10797
Matt Carlsona50d0792010-06-05 17:24:37 +000010798 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
10799 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlson8b5a6c42010-01-20 16:58:06 +000010800 mem_tbl = mem_tbl_5717;
10801 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
10802 mem_tbl = mem_tbl_57765;
10803 else if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Matt Carlson321d32a2008-11-21 17:22:19 -080010804 mem_tbl = mem_tbl_5755;
10805 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
10806 mem_tbl = mem_tbl_5906;
10807 else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
10808 mem_tbl = mem_tbl_5705;
10809 else
Michael Chan7942e1d2005-05-29 14:58:36 -070010810 mem_tbl = mem_tbl_570x;
10811
10812 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000010813 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
10814 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070010815 break;
10816 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010817
Michael Chan7942e1d2005-05-29 14:58:36 -070010818 return err;
10819}
10820
Michael Chan9f40dea2005-09-05 17:53:06 -070010821#define TG3_MAC_LOOPBACK 0
10822#define TG3_PHY_LOOPBACK 1
10823
10824static int tg3_run_loopback(struct tg3 *tp, int loopback_mode)
Michael Chanc76949a2005-05-29 14:58:59 -070010825{
Michael Chan9f40dea2005-09-05 17:53:06 -070010826 u32 mac_mode, rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010827 u32 desc_idx, coal_now;
Michael Chanc76949a2005-05-29 14:58:59 -070010828 struct sk_buff *skb, *rx_skb;
10829 u8 *tx_data;
10830 dma_addr_t map;
10831 int num_pkts, tx_len, rx_len, i, err;
10832 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000010833 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000010834 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070010835
Matt Carlsonc8873402010-02-12 14:47:11 +000010836 tnapi = &tp->napi[0];
10837 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000010838 if (tp->irq_cnt > 1) {
Matt Carlson1da85aa2010-09-30 10:34:34 +000010839 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)
10840 rnapi = &tp->napi[1];
Matt Carlsonc8873402010-02-12 14:47:11 +000010841 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
10842 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000010843 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010844 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000010845
Michael Chan9f40dea2005-09-05 17:53:06 -070010846 if (loopback_mode == TG3_MAC_LOOPBACK) {
Michael Chanc94e3942005-09-27 12:12:42 -070010847 /* HW errata - mac loopback fails in some cases on 5780.
10848 * Normal traffic and PHY loopback are not affected by
10849 * errata.
10850 */
10851 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780)
10852 return 0;
10853
Michael Chan9f40dea2005-09-05 17:53:06 -070010854 mac_mode = (tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK) |
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010855 MAC_MODE_PORT_INT_LPBACK;
10856 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
10857 mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010858 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
Michael Chan3f7045c2006-09-27 16:02:29 -070010859 mac_mode |= MAC_MODE_PORT_MODE_MII;
10860 else
10861 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chan9f40dea2005-09-05 17:53:06 -070010862 tw32(MAC_MODE, mac_mode);
10863 } else if (loopback_mode == TG3_PHY_LOOPBACK) {
Michael Chan3f7045c2006-09-27 16:02:29 -070010864 u32 val;
10865
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010866 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +000010867 tg3_phy_fet_toggle_apd(tp, false);
Michael Chan5d64ad32006-12-07 00:19:40 -080010868 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED100;
10869 } else
10870 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED1000;
Michael Chan3f7045c2006-09-27 16:02:29 -070010871
Matt Carlson9ef8ca92007-07-11 19:48:29 -070010872 tg3_phy_toggle_automdix(tp, 0);
10873
Michael Chan3f7045c2006-09-27 16:02:29 -070010874 tg3_writephy(tp, MII_BMCR, val);
Michael Chanc94e3942005-09-27 12:12:42 -070010875 udelay(40);
Michael Chan5d64ad32006-12-07 00:19:40 -080010876
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010877 mac_mode = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010878 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson1061b7c2010-02-12 14:47:12 +000010879 tg3_writephy(tp, MII_TG3_FET_PTEST,
10880 MII_TG3_FET_PTEST_FRC_TX_LINK |
10881 MII_TG3_FET_PTEST_FRC_TX_LOCK);
10882 /* The write needs to be flushed for the AC131 */
10883 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
10884 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
Michael Chan5d64ad32006-12-07 00:19:40 -080010885 mac_mode |= MAC_MODE_PORT_MODE_MII;
10886 } else
10887 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chanb16250e2006-09-27 16:10:14 -070010888
Michael Chanc94e3942005-09-27 12:12:42 -070010889 /* reset to prevent losing 1st rx packet intermittently */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010890 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Michael Chanc94e3942005-09-27 12:12:42 -070010891 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
10892 udelay(10);
10893 tw32_f(MAC_RX_MODE, tp->rx_mode);
10894 }
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010895 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlson79eb6902010-02-17 15:17:03 +000010896 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
10897 if (masked_phy_id == TG3_PHY_ID_BCM5401)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010898 mac_mode &= ~MAC_MODE_LINK_POLARITY;
Matt Carlson79eb6902010-02-17 15:17:03 +000010899 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010900 mac_mode |= MAC_MODE_LINK_POLARITY;
Michael Chanff18ff02006-03-27 23:17:27 -080010901 tg3_writephy(tp, MII_TG3_EXT_CTRL,
10902 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
10903 }
Michael Chan9f40dea2005-09-05 17:53:06 -070010904 tw32(MAC_MODE, mac_mode);
Matt Carlson859a5882010-04-05 10:19:28 +000010905 } else {
Michael Chan9f40dea2005-09-05 17:53:06 -070010906 return -EINVAL;
Matt Carlson859a5882010-04-05 10:19:28 +000010907 }
Michael Chanc76949a2005-05-29 14:58:59 -070010908
10909 err = -EIO;
10910
Michael Chanc76949a2005-05-29 14:58:59 -070010911 tx_len = 1514;
David S. Millera20e9c62006-07-31 22:38:16 -070010912 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070010913 if (!skb)
10914 return -ENOMEM;
10915
Michael Chanc76949a2005-05-29 14:58:59 -070010916 tx_data = skb_put(skb, tx_len);
10917 memcpy(tx_data, tp->dev->dev_addr, 6);
10918 memset(tx_data + 6, 0x0, 8);
10919
10920 tw32(MAC_RX_MTU_SIZE, tx_len + 4);
10921
10922 for (i = 14; i < tx_len; i++)
10923 tx_data[i] = (u8) (i & 0xff);
10924
Alexander Duyckf4188d82009-12-02 16:48:38 +000010925 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
10926 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000010927 dev_kfree_skb(skb);
10928 return -EIO;
10929 }
Michael Chanc76949a2005-05-29 14:58:59 -070010930
10931 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010932 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070010933
10934 udelay(10);
10935
Matt Carlson898a56f2009-08-28 14:02:40 +000010936 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070010937
Michael Chanc76949a2005-05-29 14:58:59 -070010938 num_pkts = 0;
10939
Alexander Duyckf4188d82009-12-02 16:48:38 +000010940 tg3_set_txd(tnapi, tnapi->tx_prod, map, tx_len, 0, 1);
Michael Chanc76949a2005-05-29 14:58:59 -070010941
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010942 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070010943 num_pkts++;
10944
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010945 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
10946 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070010947
10948 udelay(10);
10949
Matt Carlson303fc922009-11-02 14:27:34 +000010950 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
10951 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070010952 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010953 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070010954
10955 udelay(10);
10956
Matt Carlson898a56f2009-08-28 14:02:40 +000010957 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
10958 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010959 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070010960 (rx_idx == (rx_start_idx + num_pkts)))
10961 break;
10962 }
10963
Alexander Duyckf4188d82009-12-02 16:48:38 +000010964 pci_unmap_single(tp->pdev, map, tx_len, PCI_DMA_TODEVICE);
Michael Chanc76949a2005-05-29 14:58:59 -070010965 dev_kfree_skb(skb);
10966
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010967 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070010968 goto out;
10969
10970 if (rx_idx != rx_start_idx + num_pkts)
10971 goto out;
10972
Matt Carlson72334482009-08-28 14:03:01 +000010973 desc = &rnapi->rx_rcb[rx_start_idx];
Michael Chanc76949a2005-05-29 14:58:59 -070010974 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
10975 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
10976 if (opaque_key != RXD_OPAQUE_RING_STD)
10977 goto out;
10978
10979 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
10980 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
10981 goto out;
10982
10983 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) - 4;
10984 if (rx_len != tx_len)
10985 goto out;
10986
Matt Carlson21f581a2009-08-28 14:00:25 +000010987 rx_skb = tpr->rx_std_buffers[desc_idx].skb;
Michael Chanc76949a2005-05-29 14:58:59 -070010988
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +000010989 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx], mapping);
Michael Chanc76949a2005-05-29 14:58:59 -070010990 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len, PCI_DMA_FROMDEVICE);
10991
10992 for (i = 14; i < tx_len; i++) {
10993 if (*(rx_skb->data + i) != (u8) (i & 0xff))
10994 goto out;
10995 }
10996 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010997
Michael Chanc76949a2005-05-29 14:58:59 -070010998 /* tg3_free_rings will unmap and free the rx_skb */
10999out:
11000 return err;
11001}
11002
Michael Chan9f40dea2005-09-05 17:53:06 -070011003#define TG3_MAC_LOOPBACK_FAILED 1
11004#define TG3_PHY_LOOPBACK_FAILED 2
11005#define TG3_LOOPBACK_FAILED (TG3_MAC_LOOPBACK_FAILED | \
11006 TG3_PHY_LOOPBACK_FAILED)
11007
11008static int tg3_test_loopback(struct tg3 *tp)
11009{
11010 int err = 0;
Matt Carlson9936bcf2007-10-10 18:03:07 -070011011 u32 cpmuctrl = 0;
Michael Chan9f40dea2005-09-05 17:53:06 -070011012
11013 if (!netif_running(tp->dev))
11014 return TG3_LOOPBACK_FAILED;
11015
Michael Chanb9ec6c12006-07-25 16:37:27 -070011016 err = tg3_reset_hw(tp, 1);
11017 if (err)
11018 return TG3_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070011019
Matt Carlson6833c042008-11-21 17:18:59 -080011020 /* Turn off gphy autopowerdown. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011021 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -080011022 tg3_phy_toggle_apd(tp, false);
11023
Matt Carlson321d32a2008-11-21 17:22:19 -080011024 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070011025 int i;
11026 u32 status;
11027
11028 tw32(TG3_CPMU_MUTEX_REQ, CPMU_MUTEX_REQ_DRIVER);
11029
11030 /* Wait for up to 40 microseconds to acquire lock. */
11031 for (i = 0; i < 4; i++) {
11032 status = tr32(TG3_CPMU_MUTEX_GNT);
11033 if (status == CPMU_MUTEX_GNT_DRIVER)
11034 break;
11035 udelay(10);
11036 }
11037
11038 if (status != CPMU_MUTEX_GNT_DRIVER)
11039 return TG3_LOOPBACK_FAILED;
11040
Matt Carlsonb2a5c192008-04-03 21:44:44 -070011041 /* Turn off link-based power management. */
Matt Carlsone8750932007-11-12 21:11:51 -080011042 cpmuctrl = tr32(TG3_CPMU_CTRL);
Matt Carlson109115e2008-05-02 16:48:59 -070011043 tw32(TG3_CPMU_CTRL,
11044 cpmuctrl & ~(CPMU_CTRL_LINK_SPEED_MODE |
11045 CPMU_CTRL_LINK_AWARE_MODE));
Matt Carlson9936bcf2007-10-10 18:03:07 -070011046 }
11047
Michael Chan9f40dea2005-09-05 17:53:06 -070011048 if (tg3_run_loopback(tp, TG3_MAC_LOOPBACK))
11049 err |= TG3_MAC_LOOPBACK_FAILED;
Matt Carlson9936bcf2007-10-10 18:03:07 -070011050
Matt Carlson321d32a2008-11-21 17:22:19 -080011051 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070011052 tw32(TG3_CPMU_CTRL, cpmuctrl);
11053
11054 /* Release the mutex */
11055 tw32(TG3_CPMU_MUTEX_GNT, CPMU_MUTEX_GNT_DRIVER);
11056 }
11057
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011058 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Matt Carlsondd477002008-05-25 23:45:58 -070011059 !(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)) {
Michael Chan9f40dea2005-09-05 17:53:06 -070011060 if (tg3_run_loopback(tp, TG3_PHY_LOOPBACK))
11061 err |= TG3_PHY_LOOPBACK_FAILED;
11062 }
11063
Matt Carlson6833c042008-11-21 17:18:59 -080011064 /* Re-enable gphy autopowerdown. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011065 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -080011066 tg3_phy_toggle_apd(tp, true);
11067
Michael Chan9f40dea2005-09-05 17:53:06 -070011068 return err;
11069}
11070
Michael Chan4cafd3f2005-05-29 14:56:34 -070011071static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
11072 u64 *data)
11073{
Michael Chan566f86a2005-05-29 14:56:58 -070011074 struct tg3 *tp = netdev_priv(dev);
11075
Matt Carlson80096062010-08-02 11:26:06 +000011076 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080011077 tg3_set_power_state(tp, PCI_D0);
11078
Michael Chan566f86a2005-05-29 14:56:58 -070011079 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
11080
11081 if (tg3_test_nvram(tp) != 0) {
11082 etest->flags |= ETH_TEST_FL_FAILED;
11083 data[0] = 1;
11084 }
Michael Chanca430072005-05-29 14:57:23 -070011085 if (tg3_test_link(tp) != 0) {
11086 etest->flags |= ETH_TEST_FL_FAILED;
11087 data[1] = 1;
11088 }
Michael Chana71116d2005-05-29 14:58:11 -070011089 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011090 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070011091
Michael Chanbbe832c2005-06-24 20:20:04 -070011092 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011093 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070011094 tg3_netif_stop(tp);
11095 irq_sync = 1;
11096 }
11097
11098 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070011099
11100 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080011101 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011102 tg3_halt_cpu(tp, RX_CPU_BASE);
11103 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
11104 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080011105 if (!err)
11106 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011107
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011108 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad2006-03-20 22:27:35 -080011109 tg3_phy_reset(tp);
11110
Michael Chana71116d2005-05-29 14:58:11 -070011111 if (tg3_test_registers(tp) != 0) {
11112 etest->flags |= ETH_TEST_FL_FAILED;
11113 data[2] = 1;
11114 }
Michael Chan7942e1d2005-05-29 14:58:36 -070011115 if (tg3_test_memory(tp) != 0) {
11116 etest->flags |= ETH_TEST_FL_FAILED;
11117 data[3] = 1;
11118 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011119 if ((data[4] = tg3_test_loopback(tp)) != 0)
Michael Chanc76949a2005-05-29 14:58:59 -070011120 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070011121
David S. Millerf47c11e2005-06-24 20:18:35 -070011122 tg3_full_unlock(tp);
11123
Michael Chand4bc3922005-05-29 14:59:20 -070011124 if (tg3_test_interrupt(tp) != 0) {
11125 etest->flags |= ETH_TEST_FL_FAILED;
11126 data[5] = 1;
11127 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011128
11129 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070011130
Michael Chana71116d2005-05-29 14:58:11 -070011131 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
11132 if (netif_running(dev)) {
11133 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011134 err2 = tg3_restart_hw(tp, 1);
11135 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070011136 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011137 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011138
11139 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011140
11141 if (irq_sync && !err2)
11142 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011143 }
Matt Carlson80096062010-08-02 11:26:06 +000011144 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080011145 tg3_set_power_state(tp, PCI_D3hot);
11146
Michael Chan4cafd3f2005-05-29 14:56:34 -070011147}
11148
Linus Torvalds1da177e2005-04-16 15:20:36 -070011149static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
11150{
11151 struct mii_ioctl_data *data = if_mii(ifr);
11152 struct tg3 *tp = netdev_priv(dev);
11153 int err;
11154
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011155 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011156 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011157 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011158 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011159 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000011160 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011161 }
11162
Matt Carlson33f401a2010-04-05 10:19:27 +000011163 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011164 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000011165 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011166
11167 /* fallthru */
11168 case SIOCGMIIREG: {
11169 u32 mii_regval;
11170
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011171 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011172 break; /* We have no PHY */
11173
Matt Carlson80096062010-08-02 11:26:06 +000011174 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080011175 return -EAGAIN;
11176
David S. Millerf47c11e2005-06-24 20:18:35 -070011177 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011178 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070011179 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011180
11181 data->val_out = mii_regval;
11182
11183 return err;
11184 }
11185
11186 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011187 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011188 break; /* We have no PHY */
11189
Matt Carlson80096062010-08-02 11:26:06 +000011190 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080011191 return -EAGAIN;
11192
David S. Millerf47c11e2005-06-24 20:18:35 -070011193 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011194 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070011195 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011196
11197 return err;
11198
11199 default:
11200 /* do nothing */
11201 break;
11202 }
11203 return -EOPNOTSUPP;
11204}
11205
11206#if TG3_VLAN_TAG_USED
11207static void tg3_vlan_rx_register(struct net_device *dev, struct vlan_group *grp)
11208{
11209 struct tg3 *tp = netdev_priv(dev);
11210
Matt Carlson844b3ee2009-02-25 14:23:56 +000011211 if (!netif_running(dev)) {
11212 tp->vlgrp = grp;
11213 return;
11214 }
11215
11216 tg3_netif_stop(tp);
Michael Chan29315e82006-06-29 20:12:30 -070011217
David S. Millerf47c11e2005-06-24 20:18:35 -070011218 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011219
11220 tp->vlgrp = grp;
11221
11222 /* Update RX_MODE_KEEP_VLAN_TAG bit in RX_MODE register. */
11223 __tg3_set_rx_mode(dev);
11224
Matt Carlson844b3ee2009-02-25 14:23:56 +000011225 tg3_netif_start(tp);
Michael Chan46966542007-07-11 19:47:19 -070011226
11227 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011228}
Linus Torvalds1da177e2005-04-16 15:20:36 -070011229#endif
11230
David S. Miller15f98502005-05-18 22:49:26 -070011231static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11232{
11233 struct tg3 *tp = netdev_priv(dev);
11234
11235 memcpy(ec, &tp->coal, sizeof(*ec));
11236 return 0;
11237}
11238
Michael Chand244c892005-07-05 14:42:33 -070011239static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11240{
11241 struct tg3 *tp = netdev_priv(dev);
11242 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
11243 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
11244
11245 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
11246 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
11247 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
11248 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
11249 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
11250 }
11251
11252 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
11253 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
11254 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
11255 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
11256 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
11257 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
11258 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
11259 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
11260 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
11261 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
11262 return -EINVAL;
11263
11264 /* No rx interrupts will be generated if both are zero */
11265 if ((ec->rx_coalesce_usecs == 0) &&
11266 (ec->rx_max_coalesced_frames == 0))
11267 return -EINVAL;
11268
11269 /* No tx interrupts will be generated if both are zero */
11270 if ((ec->tx_coalesce_usecs == 0) &&
11271 (ec->tx_max_coalesced_frames == 0))
11272 return -EINVAL;
11273
11274 /* Only copy relevant parameters, ignore all others. */
11275 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
11276 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
11277 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
11278 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
11279 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
11280 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
11281 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
11282 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
11283 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
11284
11285 if (netif_running(dev)) {
11286 tg3_full_lock(tp, 0);
11287 __tg3_set_coalesce(tp, &tp->coal);
11288 tg3_full_unlock(tp);
11289 }
11290 return 0;
11291}
11292
Jeff Garzik7282d492006-09-13 14:30:00 -040011293static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011294 .get_settings = tg3_get_settings,
11295 .set_settings = tg3_set_settings,
11296 .get_drvinfo = tg3_get_drvinfo,
11297 .get_regs_len = tg3_get_regs_len,
11298 .get_regs = tg3_get_regs,
11299 .get_wol = tg3_get_wol,
11300 .set_wol = tg3_set_wol,
11301 .get_msglevel = tg3_get_msglevel,
11302 .set_msglevel = tg3_set_msglevel,
11303 .nway_reset = tg3_nway_reset,
11304 .get_link = ethtool_op_get_link,
11305 .get_eeprom_len = tg3_get_eeprom_len,
11306 .get_eeprom = tg3_get_eeprom,
11307 .set_eeprom = tg3_set_eeprom,
11308 .get_ringparam = tg3_get_ringparam,
11309 .set_ringparam = tg3_set_ringparam,
11310 .get_pauseparam = tg3_get_pauseparam,
11311 .set_pauseparam = tg3_set_pauseparam,
11312 .get_rx_csum = tg3_get_rx_csum,
11313 .set_rx_csum = tg3_set_rx_csum,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011314 .set_tx_csum = tg3_set_tx_csum,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011315 .set_sg = ethtool_op_set_sg,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011316 .set_tso = tg3_set_tso,
Michael Chan4cafd3f2005-05-29 14:56:34 -070011317 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011318 .get_strings = tg3_get_strings,
Michael Chan4009a932005-09-05 17:52:54 -070011319 .phys_id = tg3_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011320 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070011321 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070011322 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070011323 .get_sset_count = tg3_get_sset_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011324};
11325
11326static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
11327{
Michael Chan1b277772006-03-20 22:27:48 -080011328 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011329
11330 tp->nvram_size = EEPROM_CHIP_SIZE;
11331
Matt Carlsone4f34112009-02-25 14:25:00 +000011332 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011333 return;
11334
Michael Chanb16250e2006-09-27 16:10:14 -070011335 if ((magic != TG3_EEPROM_MAGIC) &&
11336 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
11337 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011338 return;
11339
11340 /*
11341 * Size the chip by reading offsets at increasing powers of two.
11342 * When we encounter our validation signature, we know the addressing
11343 * has wrapped around, and thus have our chip size.
11344 */
Michael Chan1b277772006-03-20 22:27:48 -080011345 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011346
11347 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000011348 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011349 return;
11350
Michael Chan18201802006-03-20 22:29:15 -080011351 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011352 break;
11353
11354 cursize <<= 1;
11355 }
11356
11357 tp->nvram_size = cursize;
11358}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011359
Linus Torvalds1da177e2005-04-16 15:20:36 -070011360static void __devinit tg3_get_nvram_size(struct tg3 *tp)
11361{
11362 u32 val;
11363
Matt Carlsondf259d82009-04-20 06:57:14 +000011364 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
11365 tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080011366 return;
11367
11368 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080011369 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080011370 tg3_get_eeprom_size(tp);
11371 return;
11372 }
11373
Matt Carlson6d348f22009-02-25 14:25:52 +000011374 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011375 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000011376 /* This is confusing. We want to operate on the
11377 * 16-bit value at offset 0xf2. The tg3_nvram_read()
11378 * call will read from NVRAM and byteswap the data
11379 * according to the byteswapping settings for all
11380 * other register accesses. This ensures the data we
11381 * want will always reside in the lower 16-bits.
11382 * However, the data in NVRAM is in LE format, which
11383 * means the data from the NVRAM read will always be
11384 * opposite the endianness of the CPU. The 16-bit
11385 * byteswap then brings the data to CPU endianness.
11386 */
11387 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011388 return;
11389 }
11390 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070011391 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011392}
11393
11394static void __devinit tg3_get_nvram_info(struct tg3 *tp)
11395{
11396 u32 nvcfg1;
11397
11398 nvcfg1 = tr32(NVRAM_CFG1);
11399 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
11400 tp->tg3_flags2 |= TG3_FLG2_FLASH;
Matt Carlson8590a602009-08-28 12:29:16 +000011401 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011402 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11403 tw32(NVRAM_CFG1, nvcfg1);
11404 }
11405
Michael Chan4c987482005-09-05 17:52:38 -070011406 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750) ||
Michael Chana4e2b342005-10-26 15:46:52 -070011407 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011408 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011409 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
11410 tp->nvram_jedecnum = JEDEC_ATMEL;
11411 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
11412 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11413 break;
11414 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
11415 tp->nvram_jedecnum = JEDEC_ATMEL;
11416 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
11417 break;
11418 case FLASH_VENDOR_ATMEL_EEPROM:
11419 tp->nvram_jedecnum = JEDEC_ATMEL;
11420 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11421 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11422 break;
11423 case FLASH_VENDOR_ST:
11424 tp->nvram_jedecnum = JEDEC_ST;
11425 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
11426 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11427 break;
11428 case FLASH_VENDOR_SAIFUN:
11429 tp->nvram_jedecnum = JEDEC_SAIFUN;
11430 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
11431 break;
11432 case FLASH_VENDOR_SST_SMALL:
11433 case FLASH_VENDOR_SST_LARGE:
11434 tp->nvram_jedecnum = JEDEC_SST;
11435 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
11436 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011437 }
Matt Carlson8590a602009-08-28 12:29:16 +000011438 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011439 tp->nvram_jedecnum = JEDEC_ATMEL;
11440 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
11441 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11442 }
11443}
11444
Matt Carlsona1b950d2009-09-01 13:20:17 +000011445static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
11446{
11447 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
11448 case FLASH_5752PAGE_SIZE_256:
11449 tp->nvram_pagesize = 256;
11450 break;
11451 case FLASH_5752PAGE_SIZE_512:
11452 tp->nvram_pagesize = 512;
11453 break;
11454 case FLASH_5752PAGE_SIZE_1K:
11455 tp->nvram_pagesize = 1024;
11456 break;
11457 case FLASH_5752PAGE_SIZE_2K:
11458 tp->nvram_pagesize = 2048;
11459 break;
11460 case FLASH_5752PAGE_SIZE_4K:
11461 tp->nvram_pagesize = 4096;
11462 break;
11463 case FLASH_5752PAGE_SIZE_264:
11464 tp->nvram_pagesize = 264;
11465 break;
11466 case FLASH_5752PAGE_SIZE_528:
11467 tp->nvram_pagesize = 528;
11468 break;
11469 }
11470}
11471
Michael Chan361b4ac2005-04-21 17:11:21 -070011472static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
11473{
11474 u32 nvcfg1;
11475
11476 nvcfg1 = tr32(NVRAM_CFG1);
11477
Michael Chane6af3012005-04-21 17:12:05 -070011478 /* NVRAM protection for TPM */
11479 if (nvcfg1 & (1 << 27))
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011480 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Michael Chane6af3012005-04-21 17:12:05 -070011481
Michael Chan361b4ac2005-04-21 17:11:21 -070011482 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011483 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
11484 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
11485 tp->nvram_jedecnum = JEDEC_ATMEL;
11486 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11487 break;
11488 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11489 tp->nvram_jedecnum = JEDEC_ATMEL;
11490 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11491 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11492 break;
11493 case FLASH_5752VENDOR_ST_M45PE10:
11494 case FLASH_5752VENDOR_ST_M45PE20:
11495 case FLASH_5752VENDOR_ST_M45PE40:
11496 tp->nvram_jedecnum = JEDEC_ST;
11497 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11498 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11499 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070011500 }
11501
11502 if (tp->tg3_flags2 & TG3_FLG2_FLASH) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000011503 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000011504 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070011505 /* For eeprom, set pagesize to maximum eeprom size */
11506 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11507
11508 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11509 tw32(NVRAM_CFG1, nvcfg1);
11510 }
11511}
11512
Michael Chand3c7b882006-03-23 01:28:25 -080011513static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
11514{
Matt Carlson989a9d22007-05-05 11:51:05 -070011515 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080011516
11517 nvcfg1 = tr32(NVRAM_CFG1);
11518
11519 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070011520 if (nvcfg1 & (1 << 27)) {
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011521 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Matt Carlson989a9d22007-05-05 11:51:05 -070011522 protect = 1;
11523 }
Michael Chand3c7b882006-03-23 01:28:25 -080011524
Matt Carlson989a9d22007-05-05 11:51:05 -070011525 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11526 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011527 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11528 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11529 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11530 case FLASH_5755VENDOR_ATMEL_FLASH_5:
11531 tp->nvram_jedecnum = JEDEC_ATMEL;
11532 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11533 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11534 tp->nvram_pagesize = 264;
11535 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
11536 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
11537 tp->nvram_size = (protect ? 0x3e200 :
11538 TG3_NVRAM_SIZE_512KB);
11539 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
11540 tp->nvram_size = (protect ? 0x1f200 :
11541 TG3_NVRAM_SIZE_256KB);
11542 else
11543 tp->nvram_size = (protect ? 0x1f200 :
11544 TG3_NVRAM_SIZE_128KB);
11545 break;
11546 case FLASH_5752VENDOR_ST_M45PE10:
11547 case FLASH_5752VENDOR_ST_M45PE20:
11548 case FLASH_5752VENDOR_ST_M45PE40:
11549 tp->nvram_jedecnum = JEDEC_ST;
11550 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11551 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11552 tp->nvram_pagesize = 256;
11553 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
11554 tp->nvram_size = (protect ?
11555 TG3_NVRAM_SIZE_64KB :
11556 TG3_NVRAM_SIZE_128KB);
11557 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
11558 tp->nvram_size = (protect ?
11559 TG3_NVRAM_SIZE_64KB :
11560 TG3_NVRAM_SIZE_256KB);
11561 else
11562 tp->nvram_size = (protect ?
11563 TG3_NVRAM_SIZE_128KB :
11564 TG3_NVRAM_SIZE_512KB);
11565 break;
Michael Chand3c7b882006-03-23 01:28:25 -080011566 }
11567}
11568
Michael Chan1b277772006-03-20 22:27:48 -080011569static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
11570{
11571 u32 nvcfg1;
11572
11573 nvcfg1 = tr32(NVRAM_CFG1);
11574
11575 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011576 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
11577 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11578 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
11579 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11580 tp->nvram_jedecnum = JEDEC_ATMEL;
11581 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11582 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080011583
Matt Carlson8590a602009-08-28 12:29:16 +000011584 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11585 tw32(NVRAM_CFG1, nvcfg1);
11586 break;
11587 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11588 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11589 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11590 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11591 tp->nvram_jedecnum = JEDEC_ATMEL;
11592 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11593 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11594 tp->nvram_pagesize = 264;
11595 break;
11596 case FLASH_5752VENDOR_ST_M45PE10:
11597 case FLASH_5752VENDOR_ST_M45PE20:
11598 case FLASH_5752VENDOR_ST_M45PE40:
11599 tp->nvram_jedecnum = JEDEC_ST;
11600 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11601 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11602 tp->nvram_pagesize = 256;
11603 break;
Michael Chan1b277772006-03-20 22:27:48 -080011604 }
11605}
11606
Matt Carlson6b91fa02007-10-10 18:01:09 -070011607static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
11608{
11609 u32 nvcfg1, protect = 0;
11610
11611 nvcfg1 = tr32(NVRAM_CFG1);
11612
11613 /* NVRAM protection for TPM */
11614 if (nvcfg1 & (1 << 27)) {
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011615 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011616 protect = 1;
11617 }
11618
11619 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11620 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011621 case FLASH_5761VENDOR_ATMEL_ADB021D:
11622 case FLASH_5761VENDOR_ATMEL_ADB041D:
11623 case FLASH_5761VENDOR_ATMEL_ADB081D:
11624 case FLASH_5761VENDOR_ATMEL_ADB161D:
11625 case FLASH_5761VENDOR_ATMEL_MDB021D:
11626 case FLASH_5761VENDOR_ATMEL_MDB041D:
11627 case FLASH_5761VENDOR_ATMEL_MDB081D:
11628 case FLASH_5761VENDOR_ATMEL_MDB161D:
11629 tp->nvram_jedecnum = JEDEC_ATMEL;
11630 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11631 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11632 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
11633 tp->nvram_pagesize = 256;
11634 break;
11635 case FLASH_5761VENDOR_ST_A_M45PE20:
11636 case FLASH_5761VENDOR_ST_A_M45PE40:
11637 case FLASH_5761VENDOR_ST_A_M45PE80:
11638 case FLASH_5761VENDOR_ST_A_M45PE16:
11639 case FLASH_5761VENDOR_ST_M_M45PE20:
11640 case FLASH_5761VENDOR_ST_M_M45PE40:
11641 case FLASH_5761VENDOR_ST_M_M45PE80:
11642 case FLASH_5761VENDOR_ST_M_M45PE16:
11643 tp->nvram_jedecnum = JEDEC_ST;
11644 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11645 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11646 tp->nvram_pagesize = 256;
11647 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011648 }
11649
11650 if (protect) {
11651 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
11652 } else {
11653 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011654 case FLASH_5761VENDOR_ATMEL_ADB161D:
11655 case FLASH_5761VENDOR_ATMEL_MDB161D:
11656 case FLASH_5761VENDOR_ST_A_M45PE16:
11657 case FLASH_5761VENDOR_ST_M_M45PE16:
11658 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
11659 break;
11660 case FLASH_5761VENDOR_ATMEL_ADB081D:
11661 case FLASH_5761VENDOR_ATMEL_MDB081D:
11662 case FLASH_5761VENDOR_ST_A_M45PE80:
11663 case FLASH_5761VENDOR_ST_M_M45PE80:
11664 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
11665 break;
11666 case FLASH_5761VENDOR_ATMEL_ADB041D:
11667 case FLASH_5761VENDOR_ATMEL_MDB041D:
11668 case FLASH_5761VENDOR_ST_A_M45PE40:
11669 case FLASH_5761VENDOR_ST_M_M45PE40:
11670 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11671 break;
11672 case FLASH_5761VENDOR_ATMEL_ADB021D:
11673 case FLASH_5761VENDOR_ATMEL_MDB021D:
11674 case FLASH_5761VENDOR_ST_A_M45PE20:
11675 case FLASH_5761VENDOR_ST_M_M45PE20:
11676 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11677 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011678 }
11679 }
11680}
11681
Michael Chanb5d37722006-09-27 16:06:21 -070011682static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
11683{
11684 tp->nvram_jedecnum = JEDEC_ATMEL;
11685 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11686 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11687}
11688
Matt Carlson321d32a2008-11-21 17:22:19 -080011689static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
11690{
11691 u32 nvcfg1;
11692
11693 nvcfg1 = tr32(NVRAM_CFG1);
11694
11695 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11696 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11697 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11698 tp->nvram_jedecnum = JEDEC_ATMEL;
11699 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11700 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11701
11702 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11703 tw32(NVRAM_CFG1, nvcfg1);
11704 return;
11705 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11706 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11707 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11708 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11709 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11710 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11711 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
11712 tp->nvram_jedecnum = JEDEC_ATMEL;
11713 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11714 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11715
11716 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11717 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11718 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11719 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11720 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11721 break;
11722 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11723 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11724 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11725 break;
11726 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11727 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
11728 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11729 break;
11730 }
11731 break;
11732 case FLASH_5752VENDOR_ST_M45PE10:
11733 case FLASH_5752VENDOR_ST_M45PE20:
11734 case FLASH_5752VENDOR_ST_M45PE40:
11735 tp->nvram_jedecnum = JEDEC_ST;
11736 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11737 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11738
11739 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11740 case FLASH_5752VENDOR_ST_M45PE10:
11741 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11742 break;
11743 case FLASH_5752VENDOR_ST_M45PE20:
11744 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11745 break;
11746 case FLASH_5752VENDOR_ST_M45PE40:
11747 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11748 break;
11749 }
11750 break;
11751 default:
Matt Carlsondf259d82009-04-20 06:57:14 +000011752 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
Matt Carlson321d32a2008-11-21 17:22:19 -080011753 return;
11754 }
11755
Matt Carlsona1b950d2009-09-01 13:20:17 +000011756 tg3_nvram_get_pagesize(tp, nvcfg1);
11757 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Matt Carlson321d32a2008-11-21 17:22:19 -080011758 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011759}
11760
11761
11762static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
11763{
11764 u32 nvcfg1;
11765
11766 nvcfg1 = tr32(NVRAM_CFG1);
11767
11768 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11769 case FLASH_5717VENDOR_ATMEL_EEPROM:
11770 case FLASH_5717VENDOR_MICRO_EEPROM:
11771 tp->nvram_jedecnum = JEDEC_ATMEL;
11772 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11773 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11774
11775 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11776 tw32(NVRAM_CFG1, nvcfg1);
11777 return;
11778 case FLASH_5717VENDOR_ATMEL_MDB011D:
11779 case FLASH_5717VENDOR_ATMEL_ADB011B:
11780 case FLASH_5717VENDOR_ATMEL_ADB011D:
11781 case FLASH_5717VENDOR_ATMEL_MDB021D:
11782 case FLASH_5717VENDOR_ATMEL_ADB021B:
11783 case FLASH_5717VENDOR_ATMEL_ADB021D:
11784 case FLASH_5717VENDOR_ATMEL_45USPT:
11785 tp->nvram_jedecnum = JEDEC_ATMEL;
11786 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11787 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11788
11789 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11790 case FLASH_5717VENDOR_ATMEL_MDB021D:
11791 case FLASH_5717VENDOR_ATMEL_ADB021B:
11792 case FLASH_5717VENDOR_ATMEL_ADB021D:
11793 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11794 break;
11795 default:
11796 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11797 break;
11798 }
Matt Carlson321d32a2008-11-21 17:22:19 -080011799 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011800 case FLASH_5717VENDOR_ST_M_M25PE10:
11801 case FLASH_5717VENDOR_ST_A_M25PE10:
11802 case FLASH_5717VENDOR_ST_M_M45PE10:
11803 case FLASH_5717VENDOR_ST_A_M45PE10:
11804 case FLASH_5717VENDOR_ST_M_M25PE20:
11805 case FLASH_5717VENDOR_ST_A_M25PE20:
11806 case FLASH_5717VENDOR_ST_M_M45PE20:
11807 case FLASH_5717VENDOR_ST_A_M45PE20:
11808 case FLASH_5717VENDOR_ST_25USPT:
11809 case FLASH_5717VENDOR_ST_45USPT:
11810 tp->nvram_jedecnum = JEDEC_ST;
11811 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11812 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11813
11814 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11815 case FLASH_5717VENDOR_ST_M_M25PE20:
11816 case FLASH_5717VENDOR_ST_A_M25PE20:
11817 case FLASH_5717VENDOR_ST_M_M45PE20:
11818 case FLASH_5717VENDOR_ST_A_M45PE20:
11819 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11820 break;
11821 default:
11822 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11823 break;
11824 }
Matt Carlson321d32a2008-11-21 17:22:19 -080011825 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011826 default:
11827 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
11828 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080011829 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000011830
11831 tg3_nvram_get_pagesize(tp, nvcfg1);
11832 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
11833 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
Matt Carlson321d32a2008-11-21 17:22:19 -080011834}
11835
Linus Torvalds1da177e2005-04-16 15:20:36 -070011836/* Chips other than 5700/5701 use the NVRAM for fetching info. */
11837static void __devinit tg3_nvram_init(struct tg3 *tp)
11838{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011839 tw32_f(GRC_EEPROM_ADDR,
11840 (EEPROM_ADDR_FSM_RESET |
11841 (EEPROM_DEFAULT_CLOCK_PERIOD <<
11842 EEPROM_ADDR_CLKPERD_SHIFT)));
11843
Michael Chan9d57f012006-12-07 00:23:25 -080011844 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011845
11846 /* Enable seeprom accesses. */
11847 tw32_f(GRC_LOCAL_CTRL,
11848 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
11849 udelay(100);
11850
11851 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
11852 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
11853 tp->tg3_flags |= TG3_FLAG_NVRAM;
11854
Michael Chanec41c7d2006-01-17 02:40:55 -080011855 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000011856 netdev_warn(tp->dev,
11857 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000011858 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080011859 return;
11860 }
Michael Chane6af3012005-04-21 17:12:05 -070011861 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011862
Matt Carlson989a9d22007-05-05 11:51:05 -070011863 tp->nvram_size = 0;
11864
Michael Chan361b4ac2005-04-21 17:11:21 -070011865 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
11866 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080011867 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
11868 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070011869 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070011870 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
11871 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080011872 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070011873 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
11874 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070011875 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
11876 tg3_get_5906_nvram_info(tp);
Matt Carlsonb703df62009-12-03 08:36:21 +000011877 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
11878 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson321d32a2008-11-21 17:22:19 -080011879 tg3_get_57780_nvram_info(tp);
Matt Carlsona50d0792010-06-05 17:24:37 +000011880 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
11881 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000011882 tg3_get_5717_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070011883 else
11884 tg3_get_nvram_info(tp);
11885
Matt Carlson989a9d22007-05-05 11:51:05 -070011886 if (tp->nvram_size == 0)
11887 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011888
Michael Chane6af3012005-04-21 17:12:05 -070011889 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080011890 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011891
11892 } else {
11893 tp->tg3_flags &= ~(TG3_FLAG_NVRAM | TG3_FLAG_NVRAM_BUFFERED);
11894
11895 tg3_get_eeprom_size(tp);
11896 }
11897}
11898
Linus Torvalds1da177e2005-04-16 15:20:36 -070011899static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
11900 u32 offset, u32 len, u8 *buf)
11901{
11902 int i, j, rc = 0;
11903 u32 val;
11904
11905 for (i = 0; i < len; i += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080011906 u32 addr;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011907 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011908
11909 addr = offset + i;
11910
11911 memcpy(&data, buf + i, 4);
11912
Matt Carlson62cedd12009-04-20 14:52:29 -070011913 /*
11914 * The SEEPROM interface expects the data to always be opposite
11915 * the native endian format. We accomplish this by reversing
11916 * all the operations that would have been performed on the
11917 * data from a call to tg3_nvram_read_be32().
11918 */
11919 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011920
11921 val = tr32(GRC_EEPROM_ADDR);
11922 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
11923
11924 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
11925 EEPROM_ADDR_READ);
11926 tw32(GRC_EEPROM_ADDR, val |
11927 (0 << EEPROM_ADDR_DEVID_SHIFT) |
11928 (addr & EEPROM_ADDR_ADDR_MASK) |
11929 EEPROM_ADDR_START |
11930 EEPROM_ADDR_WRITE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011931
Michael Chan9d57f012006-12-07 00:23:25 -080011932 for (j = 0; j < 1000; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011933 val = tr32(GRC_EEPROM_ADDR);
11934
11935 if (val & EEPROM_ADDR_COMPLETE)
11936 break;
Michael Chan9d57f012006-12-07 00:23:25 -080011937 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011938 }
11939 if (!(val & EEPROM_ADDR_COMPLETE)) {
11940 rc = -EBUSY;
11941 break;
11942 }
11943 }
11944
11945 return rc;
11946}
11947
11948/* offset and length are dword aligned */
11949static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
11950 u8 *buf)
11951{
11952 int ret = 0;
11953 u32 pagesize = tp->nvram_pagesize;
11954 u32 pagemask = pagesize - 1;
11955 u32 nvram_cmd;
11956 u8 *tmp;
11957
11958 tmp = kmalloc(pagesize, GFP_KERNEL);
11959 if (tmp == NULL)
11960 return -ENOMEM;
11961
11962 while (len) {
11963 int j;
Michael Chane6af3012005-04-21 17:12:05 -070011964 u32 phy_addr, page_off, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011965
11966 phy_addr = offset & ~pagemask;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011967
Linus Torvalds1da177e2005-04-16 15:20:36 -070011968 for (j = 0; j < pagesize; j += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000011969 ret = tg3_nvram_read_be32(tp, phy_addr + j,
11970 (__be32 *) (tmp + j));
11971 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011972 break;
11973 }
11974 if (ret)
11975 break;
11976
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011977 page_off = offset & pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011978 size = pagesize;
11979 if (len < size)
11980 size = len;
11981
11982 len -= size;
11983
11984 memcpy(tmp + page_off, buf, size);
11985
11986 offset = offset + (pagesize - page_off);
11987
Michael Chane6af3012005-04-21 17:12:05 -070011988 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011989
11990 /*
11991 * Before we can erase the flash page, we need
11992 * to issue a special "write enable" command.
11993 */
11994 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
11995
11996 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
11997 break;
11998
11999 /* Erase the target page */
12000 tw32(NVRAM_ADDR, phy_addr);
12001
12002 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
12003 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
12004
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012005 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012006 break;
12007
12008 /* Issue another write enable to start the write. */
12009 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12010
12011 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12012 break;
12013
12014 for (j = 0; j < pagesize; j += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012015 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012016
Al Virob9fc7dc2007-12-17 22:59:57 -080012017 data = *((__be32 *) (tmp + j));
Matt Carlsona9dc5292009-02-25 14:25:30 +000012018
Al Virob9fc7dc2007-12-17 22:59:57 -080012019 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012020
12021 tw32(NVRAM_ADDR, phy_addr + j);
12022
12023 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
12024 NVRAM_CMD_WR;
12025
12026 if (j == 0)
12027 nvram_cmd |= NVRAM_CMD_FIRST;
12028 else if (j == (pagesize - 4))
12029 nvram_cmd |= NVRAM_CMD_LAST;
12030
12031 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12032 break;
12033 }
12034 if (ret)
12035 break;
12036 }
12037
12038 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12039 tg3_nvram_exec_cmd(tp, nvram_cmd);
12040
12041 kfree(tmp);
12042
12043 return ret;
12044}
12045
12046/* offset and length are dword aligned */
12047static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
12048 u8 *buf)
12049{
12050 int i, ret = 0;
12051
12052 for (i = 0; i < len; i += 4, offset += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012053 u32 page_off, phy_addr, nvram_cmd;
12054 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012055
12056 memcpy(&data, buf + i, 4);
Al Virob9fc7dc2007-12-17 22:59:57 -080012057 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012058
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012059 page_off = offset % tp->nvram_pagesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012060
Michael Chan18201802006-03-20 22:29:15 -080012061 phy_addr = tg3_nvram_phys_addr(tp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012062
12063 tw32(NVRAM_ADDR, phy_addr);
12064
12065 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
12066
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012067 if (page_off == 0 || i == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012068 nvram_cmd |= NVRAM_CMD_FIRST;
Michael Chanf6d9a252006-04-29 19:00:24 -070012069 if (page_off == (tp->nvram_pagesize - 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012070 nvram_cmd |= NVRAM_CMD_LAST;
12071
12072 if (i == (len - 4))
12073 nvram_cmd |= NVRAM_CMD_LAST;
12074
Matt Carlson321d32a2008-11-21 17:22:19 -080012075 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
12076 !(tp->tg3_flags3 & TG3_FLG3_5755_PLUS) &&
Michael Chan4c987482005-09-05 17:52:38 -070012077 (tp->nvram_jedecnum == JEDEC_ST) &&
12078 (nvram_cmd & NVRAM_CMD_FIRST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012079
12080 if ((ret = tg3_nvram_exec_cmd(tp,
12081 NVRAM_CMD_WREN | NVRAM_CMD_GO |
12082 NVRAM_CMD_DONE)))
12083
12084 break;
12085 }
12086 if (!(tp->tg3_flags2 & TG3_FLG2_FLASH)) {
12087 /* We always do complete word writes to eeprom. */
12088 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
12089 }
12090
12091 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12092 break;
12093 }
12094 return ret;
12095}
12096
12097/* offset and length are dword aligned */
12098static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
12099{
12100 int ret;
12101
Linus Torvalds1da177e2005-04-16 15:20:36 -070012102 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT) {
Michael Chan314fba32005-04-21 17:07:04 -070012103 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
12104 ~GRC_LCLCTRL_GPIO_OUTPUT1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012105 udelay(40);
12106 }
12107
12108 if (!(tp->tg3_flags & TG3_FLAG_NVRAM)) {
12109 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
Matt Carlson859a5882010-04-05 10:19:28 +000012110 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012111 u32 grc_mode;
12112
Michael Chanec41c7d2006-01-17 02:40:55 -080012113 ret = tg3_nvram_lock(tp);
12114 if (ret)
12115 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012116
Michael Chane6af3012005-04-21 17:12:05 -070012117 tg3_enable_nvram_access(tp);
12118 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Matt Carlsonf66a29b2009-11-13 13:03:36 +000012119 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012120 tw32(NVRAM_WRITE1, 0x406);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012121
12122 grc_mode = tr32(GRC_MODE);
12123 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
12124
12125 if ((tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) ||
12126 !(tp->tg3_flags2 & TG3_FLG2_FLASH)) {
12127
12128 ret = tg3_nvram_write_block_buffered(tp, offset, len,
12129 buf);
Matt Carlson859a5882010-04-05 10:19:28 +000012130 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012131 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
12132 buf);
12133 }
12134
12135 grc_mode = tr32(GRC_MODE);
12136 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
12137
Michael Chane6af3012005-04-21 17:12:05 -070012138 tg3_disable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012139 tg3_nvram_unlock(tp);
12140 }
12141
12142 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT) {
Michael Chan314fba32005-04-21 17:07:04 -070012143 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012144 udelay(40);
12145 }
12146
12147 return ret;
12148}
12149
12150struct subsys_tbl_ent {
12151 u16 subsys_vendor, subsys_devid;
12152 u32 phy_id;
12153};
12154
Matt Carlson24daf2b2010-02-17 15:17:02 +000012155static struct subsys_tbl_ent subsys_id_to_phy_id[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012156 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012157 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012158 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012159 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012160 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012161 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012162 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012163 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12164 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
12165 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012166 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012167 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012168 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012169 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12170 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
12171 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012172 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012173 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012174 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012175 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012176 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012177 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012178 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012179
12180 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012181 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012182 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012183 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012184 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012185 { TG3PCI_SUBVENDOR_ID_3COM,
12186 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
12187 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012188 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012189 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012190 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012191
12192 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012193 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012194 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012195 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012196 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012197 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012198 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012199 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012200 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012201
12202 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012203 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012204 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012205 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012206 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012207 { TG3PCI_SUBVENDOR_ID_COMPAQ,
12208 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
12209 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012210 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012211 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012212 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012213
12214 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012215 { TG3PCI_SUBVENDOR_ID_IBM,
12216 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012217};
12218
Matt Carlson24daf2b2010-02-17 15:17:02 +000012219static struct subsys_tbl_ent * __devinit tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012220{
12221 int i;
12222
12223 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
12224 if ((subsys_id_to_phy_id[i].subsys_vendor ==
12225 tp->pdev->subsystem_vendor) &&
12226 (subsys_id_to_phy_id[i].subsys_devid ==
12227 tp->pdev->subsystem_device))
12228 return &subsys_id_to_phy_id[i];
12229 }
12230 return NULL;
12231}
12232
Michael Chan7d0c41e2005-04-21 17:06:20 -070012233static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012234{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012235 u32 val;
Michael Chancaf636c72006-03-22 01:05:31 -080012236 u16 pmcsr;
12237
12238 /* On some early chips the SRAM cannot be accessed in D3hot state,
12239 * so need make sure we're in D0.
12240 */
12241 pci_read_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, &pmcsr);
12242 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
12243 pci_write_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, pmcsr);
12244 msleep(1);
Michael Chan7d0c41e2005-04-21 17:06:20 -070012245
12246 /* Make sure register accesses (indirect or otherwise)
12247 * will function correctly.
12248 */
12249 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
12250 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012251
David S. Millerf49639e2006-06-09 11:58:36 -070012252 /* The memory arbiter has to be enabled in order for SRAM accesses
12253 * to succeed. Normally on powerup the tg3 chip firmware will make
12254 * sure it is enabled, but other entities such as system netboot
12255 * code might disable it.
12256 */
12257 val = tr32(MEMARB_MODE);
12258 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
12259
Matt Carlson79eb6902010-02-17 15:17:03 +000012260 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012261 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12262
Gary Zambranoa85feb82007-05-05 11:52:19 -070012263 /* Assume an onboard device and WOL capable by default. */
12264 tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT | TG3_FLAG_WOL_CAP;
David S. Miller72b845e2006-03-14 14:11:48 -080012265
Michael Chanb5d37722006-09-27 16:06:21 -070012266 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080012267 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Michael Chanb5d37722006-09-27 16:06:21 -070012268 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080012269 tp->tg3_flags2 |= TG3_FLG2_IS_NIC;
12270 }
Matt Carlson0527ba32007-10-10 18:03:30 -070012271 val = tr32(VCPU_CFGSHDW);
12272 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Matt Carlson8ed5d972007-05-07 00:25:49 -070012273 tp->tg3_flags |= TG3_FLAG_ASPM_WORKAROUND;
Matt Carlson0527ba32007-10-10 18:03:30 -070012274 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Matt Carlson20232762008-12-21 20:18:56 -080012275 (val & VCPU_CFGSHDW_WOL_MAGPKT))
Matt Carlson0527ba32007-10-10 18:03:30 -070012276 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012277 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070012278 }
12279
Linus Torvalds1da177e2005-04-16 15:20:36 -070012280 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
12281 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
12282 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070012283 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012284 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012285
12286 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
12287 tp->nic_sram_data_cfg = nic_cfg;
12288
12289 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
12290 ver >>= NIC_SRAM_DATA_VER_SHIFT;
12291 if ((GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700) &&
12292 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) &&
12293 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703) &&
12294 (ver > 0) && (ver < 0x100))
12295 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
12296
Matt Carlsona9daf362008-05-25 23:49:44 -070012297 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
12298 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
12299
Linus Torvalds1da177e2005-04-16 15:20:36 -070012300 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
12301 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
12302 eeprom_phy_serdes = 1;
12303
12304 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
12305 if (nic_phy_id != 0) {
12306 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
12307 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
12308
12309 eeprom_phy_id = (id1 >> 16) << 10;
12310 eeprom_phy_id |= (id2 & 0xfc00) << 16;
12311 eeprom_phy_id |= (id2 & 0x03ff) << 0;
12312 } else
12313 eeprom_phy_id = 0;
12314
Michael Chan7d0c41e2005-04-21 17:06:20 -070012315 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070012316 if (eeprom_phy_serdes) {
Matt Carlsona50d0792010-06-05 17:24:37 +000012317 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012318 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000012319 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012320 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070012321 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070012322
John W. Linvillecbf46852005-04-21 17:01:29 -070012323 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012324 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
12325 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070012326 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070012327 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
12328
12329 switch (led_cfg) {
12330 default:
12331 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
12332 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12333 break;
12334
12335 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
12336 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12337 break;
12338
12339 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
12340 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070012341
12342 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
12343 * read on some older 5700/5701 bootcode.
12344 */
12345 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
12346 ASIC_REV_5700 ||
12347 GET_ASIC_REV(tp->pci_chip_rev_id) ==
12348 ASIC_REV_5701)
12349 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12350
Linus Torvalds1da177e2005-04-16 15:20:36 -070012351 break;
12352
12353 case SHASTA_EXT_LED_SHARED:
12354 tp->led_ctrl = LED_CTRL_MODE_SHARED;
12355 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
12356 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
12357 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12358 LED_CTRL_MODE_PHY_2);
12359 break;
12360
12361 case SHASTA_EXT_LED_MAC:
12362 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
12363 break;
12364
12365 case SHASTA_EXT_LED_COMBO:
12366 tp->led_ctrl = LED_CTRL_MODE_COMBO;
12367 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
12368 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12369 LED_CTRL_MODE_PHY_2);
12370 break;
12371
Stephen Hemminger855e1112008-04-16 16:37:28 -070012372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012373
12374 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
12375 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
12376 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
12377 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12378
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012379 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
12380 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080012381
Michael Chan9d26e212006-12-07 00:21:14 -080012382 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012383 tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080012384 if ((tp->pdev->subsystem_vendor ==
12385 PCI_VENDOR_ID_ARIMA) &&
12386 (tp->pdev->subsystem_device == 0x205a ||
12387 tp->pdev->subsystem_device == 0x2063))
12388 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
12389 } else {
David S. Millerf49639e2006-06-09 11:58:36 -070012390 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080012391 tp->tg3_flags2 |= TG3_FLG2_IS_NIC;
12392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012393
12394 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
12395 tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
John W. Linvillecbf46852005-04-21 17:01:29 -070012396 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012397 tp->tg3_flags2 |= TG3_FLG2_ASF_NEW_HANDSHAKE;
12398 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012399
12400 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
12401 (tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
Matt Carlson0d3031d2007-10-10 18:02:43 -070012402 tp->tg3_flags3 |= TG3_FLG3_ENABLE_APE;
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012403
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012404 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070012405 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
12406 tp->tg3_flags &= ~TG3_FLAG_WOL_CAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012407
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070012408 if ((tp->tg3_flags & TG3_FLAG_WOL_CAP) &&
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012409 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE))
Matt Carlson0527ba32007-10-10 18:03:30 -070012410 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
12411
Linus Torvalds1da177e2005-04-16 15:20:36 -070012412 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012413 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012414
12415 /* serdes signal pre-emphasis in register 0x590 set by */
12416 /* bootcode if bit 18 is set */
12417 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012418 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070012419
Matt Carlson321d32a2008-11-21 17:22:19 -080012420 if (((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
12421 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080012422 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012423 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080012424
Matt Carlson8c69b1e2010-08-02 11:26:00 +000012425 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
12426 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
12427 !(tp->tg3_flags3 & TG3_FLG3_5717_PLUS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070012428 u32 cfg3;
12429
12430 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
12431 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
12432 tp->tg3_flags |= TG3_FLAG_ASPM_WORKAROUND;
12433 }
Matt Carlsona9daf362008-05-25 23:49:44 -070012434
Matt Carlson14417062010-02-17 15:16:59 +000012435 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
12436 tp->tg3_flags3 |= TG3_FLG3_RGMII_INBAND_DISABLE;
Matt Carlsona9daf362008-05-25 23:49:44 -070012437 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
12438 tp->tg3_flags3 |= TG3_FLG3_RGMII_EXT_IBND_RX_EN;
12439 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
12440 tp->tg3_flags3 |= TG3_FLG3_RGMII_EXT_IBND_TX_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012441 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012442done:
12443 device_init_wakeup(&tp->pdev->dev, tp->tg3_flags & TG3_FLAG_WOL_CAP);
12444 device_set_wakeup_enable(&tp->pdev->dev,
12445 tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
Michael Chan7d0c41e2005-04-21 17:06:20 -070012446}
12447
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012448static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
12449{
12450 int i;
12451 u32 val;
12452
12453 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
12454 tw32(OTP_CTRL, cmd);
12455
12456 /* Wait for up to 1 ms for command to execute. */
12457 for (i = 0; i < 100; i++) {
12458 val = tr32(OTP_STATUS);
12459 if (val & OTP_STATUS_CMD_DONE)
12460 break;
12461 udelay(10);
12462 }
12463
12464 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
12465}
12466
12467/* Read the gphy configuration from the OTP region of the chip. The gphy
12468 * configuration is a 32-bit value that straddles the alignment boundary.
12469 * We do two 32-bit reads and then shift and merge the results.
12470 */
12471static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
12472{
12473 u32 bhalf_otp, thalf_otp;
12474
12475 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
12476
12477 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
12478 return 0;
12479
12480 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
12481
12482 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12483 return 0;
12484
12485 thalf_otp = tr32(OTP_READ_DATA);
12486
12487 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
12488
12489 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12490 return 0;
12491
12492 bhalf_otp = tr32(OTP_READ_DATA);
12493
12494 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
12495}
12496
Michael Chan7d0c41e2005-04-21 17:06:20 -070012497static int __devinit tg3_phy_probe(struct tg3 *tp)
12498{
12499 u32 hw_phy_id_1, hw_phy_id_2;
12500 u32 hw_phy_id, hw_phy_id_masked;
12501 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012502
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012503 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)
12504 return tg3_phy_init(tp);
12505
Linus Torvalds1da177e2005-04-16 15:20:36 -070012506 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010012507 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012508 */
12509 err = 0;
Matt Carlson0d3031d2007-10-10 18:02:43 -070012510 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
12511 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000012512 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012513 } else {
12514 /* Now read the physical PHY_ID from the chip and verify
12515 * that it is sane. If it doesn't look good, we fall back
12516 * to either the hard-coded table based PHY_ID and failing
12517 * that the value found in the eeprom area.
12518 */
12519 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
12520 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
12521
12522 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
12523 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
12524 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
12525
Matt Carlson79eb6902010-02-17 15:17:03 +000012526 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012527 }
12528
Matt Carlson79eb6902010-02-17 15:17:03 +000012529 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012530 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000012531 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012532 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070012533 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012534 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012535 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000012536 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070012537 /* Do nothing, phy ID already set up in
12538 * tg3_get_eeprom_hw_cfg().
12539 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070012540 } else {
12541 struct subsys_tbl_ent *p;
12542
12543 /* No eeprom signature? Try the hardcoded
12544 * subsys device table.
12545 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012546 p = tg3_lookup_by_subsys(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012547 if (!p)
12548 return -ENODEV;
12549
12550 tp->phy_id = p->phy_id;
12551 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000012552 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012553 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012554 }
12555 }
12556
Matt Carlson52b02d02010-10-14 10:37:41 +000012557 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
12558 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12559 tp->pci_chip_rev_id != CHIPREV_ID_57765_A0))
12560 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
12561
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012562 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Matt Carlson0d3031d2007-10-10 18:02:43 -070012563 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070012564 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chan3600d912006-12-07 00:21:48 -080012565 u32 bmsr, adv_reg, tg3_ctrl, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012566
12567 tg3_readphy(tp, MII_BMSR, &bmsr);
12568 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
12569 (bmsr & BMSR_LSTATUS))
12570 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012571
Linus Torvalds1da177e2005-04-16 15:20:36 -070012572 err = tg3_phy_reset(tp);
12573 if (err)
12574 return err;
12575
12576 adv_reg = (ADVERTISE_10HALF | ADVERTISE_10FULL |
12577 ADVERTISE_100HALF | ADVERTISE_100FULL |
12578 ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
12579 tg3_ctrl = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012580 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012581 tg3_ctrl = (MII_TG3_CTRL_ADV_1000_HALF |
12582 MII_TG3_CTRL_ADV_1000_FULL);
12583 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
12584 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
12585 tg3_ctrl |= (MII_TG3_CTRL_AS_MASTER |
12586 MII_TG3_CTRL_ENABLE_AS_MASTER);
12587 }
12588
Michael Chan3600d912006-12-07 00:21:48 -080012589 mask = (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
12590 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
12591 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full);
12592 if (!tg3_copper_is_advertising_all(tp, mask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012593 tg3_writephy(tp, MII_ADVERTISE, adv_reg);
12594
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012595 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012596 tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
12597
12598 tg3_writephy(tp, MII_BMCR,
12599 BMCR_ANENABLE | BMCR_ANRESTART);
12600 }
12601 tg3_phy_set_wirespeed(tp);
12602
12603 tg3_writephy(tp, MII_ADVERTISE, adv_reg);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012604 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012605 tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
12606 }
12607
12608skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000012609 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012610 err = tg3_init_5401phy_dsp(tp);
12611 if (err)
12612 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012613
Linus Torvalds1da177e2005-04-16 15:20:36 -070012614 err = tg3_init_5401phy_dsp(tp);
12615 }
12616
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012617 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012618 tp->link_config.advertising =
12619 (ADVERTISED_1000baseT_Half |
12620 ADVERTISED_1000baseT_Full |
12621 ADVERTISED_Autoneg |
12622 ADVERTISED_FIBRE);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012623 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012624 tp->link_config.advertising &=
12625 ~(ADVERTISED_1000baseT_Half |
12626 ADVERTISED_1000baseT_Full);
12627
12628 return err;
12629}
12630
Matt Carlson184b8902010-04-05 10:19:25 +000012631static void __devinit tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012632{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000012633 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000012634 unsigned int block_end, rosize, len;
Matt Carlson184b8902010-04-05 10:19:25 +000012635 int j, i = 0;
Michael Chan1b277772006-03-20 22:27:48 -080012636 u32 magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012637
Matt Carlsondf259d82009-04-20 06:57:14 +000012638 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
12639 tg3_nvram_read(tp, 0x0, &magic))
Matt Carlsona4a8bb12010-09-15 09:00:00 +000012640 goto out_no_vpd;
12641
12642 vpd_data = kmalloc(TG3_NVM_VPD_LEN, GFP_KERNEL);
12643 if (!vpd_data)
12644 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012645
Michael Chan18201802006-03-20 22:29:15 -080012646 if (magic == TG3_EEPROM_MAGIC) {
Matt Carlson141518c2009-12-03 08:36:22 +000012647 for (i = 0; i < TG3_NVM_VPD_LEN; i += 4) {
Michael Chan1b277772006-03-20 22:27:48 -080012648 u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012649
Matt Carlson6d348f22009-02-25 14:25:52 +000012650 /* The data is in little-endian format in NVRAM.
12651 * Use the big-endian read routines to preserve
12652 * the byte order as it exists in NVRAM.
12653 */
Matt Carlson141518c2009-12-03 08:36:22 +000012654 if (tg3_nvram_read_be32(tp, TG3_NVM_VPD_OFF + i, &tmp))
Michael Chan1b277772006-03-20 22:27:48 -080012655 goto out_not_found;
12656
Matt Carlson6d348f22009-02-25 14:25:52 +000012657 memcpy(&vpd_data[i], &tmp, sizeof(tmp));
Michael Chan1b277772006-03-20 22:27:48 -080012658 }
12659 } else {
Matt Carlson94c982b2009-12-03 08:36:23 +000012660 ssize_t cnt;
Matt Carlson4181b2c2010-02-26 14:04:45 +000012661 unsigned int pos = 0;
Michael Chan1b277772006-03-20 22:27:48 -080012662
Matt Carlson94c982b2009-12-03 08:36:23 +000012663 for (; pos < TG3_NVM_VPD_LEN && i < 3; i++, pos += cnt) {
12664 cnt = pci_read_vpd(tp->pdev, pos,
12665 TG3_NVM_VPD_LEN - pos,
12666 &vpd_data[pos]);
12667 if (cnt == -ETIMEDOUT || -EINTR)
12668 cnt = 0;
12669 else if (cnt < 0)
David S. Millerf49639e2006-06-09 11:58:36 -070012670 goto out_not_found;
Michael Chan1b277772006-03-20 22:27:48 -080012671 }
Matt Carlson94c982b2009-12-03 08:36:23 +000012672 if (pos != TG3_NVM_VPD_LEN)
12673 goto out_not_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012674 }
12675
Matt Carlson4181b2c2010-02-26 14:04:45 +000012676 i = pci_vpd_find_tag(vpd_data, 0, TG3_NVM_VPD_LEN,
12677 PCI_VPD_LRDT_RO_DATA);
12678 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012679 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000012680
12681 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
12682 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
12683 i += PCI_VPD_LRDT_TAG_SIZE;
12684
12685 if (block_end > TG3_NVM_VPD_LEN)
12686 goto out_not_found;
12687
Matt Carlson184b8902010-04-05 10:19:25 +000012688 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
12689 PCI_VPD_RO_KEYWORD_MFR_ID);
12690 if (j > 0) {
12691 len = pci_vpd_info_field_size(&vpd_data[j]);
12692
12693 j += PCI_VPD_INFO_FLD_HDR_SIZE;
12694 if (j + len > block_end || len != 4 ||
12695 memcmp(&vpd_data[j], "1028", 4))
12696 goto partno;
12697
12698 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
12699 PCI_VPD_RO_KEYWORD_VENDOR0);
12700 if (j < 0)
12701 goto partno;
12702
12703 len = pci_vpd_info_field_size(&vpd_data[j]);
12704
12705 j += PCI_VPD_INFO_FLD_HDR_SIZE;
12706 if (j + len > block_end)
12707 goto partno;
12708
12709 memcpy(tp->fw_ver, &vpd_data[j], len);
12710 strncat(tp->fw_ver, " bc ", TG3_NVM_VPD_LEN - len - 1);
12711 }
12712
12713partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000012714 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
12715 PCI_VPD_RO_KEYWORD_PARTNO);
12716 if (i < 0)
12717 goto out_not_found;
12718
12719 len = pci_vpd_info_field_size(&vpd_data[i]);
12720
12721 i += PCI_VPD_INFO_FLD_HDR_SIZE;
12722 if (len > TG3_BPN_SIZE ||
12723 (len + i) > TG3_NVM_VPD_LEN)
12724 goto out_not_found;
12725
12726 memcpy(tp->board_part_number, &vpd_data[i], len);
12727
Linus Torvalds1da177e2005-04-16 15:20:36 -070012728out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000012729 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000012730 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000012731 return;
12732
12733out_no_vpd:
Matt Carlson37a949c2010-09-30 10:34:33 +000012734 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
12735 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717)
12736 strcpy(tp->board_part_number, "BCM5717");
12737 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
12738 strcpy(tp->board_part_number, "BCM5718");
12739 else
12740 goto nomatch;
12741 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
12742 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
12743 strcpy(tp->board_part_number, "BCM57780");
12744 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
12745 strcpy(tp->board_part_number, "BCM57760");
12746 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
12747 strcpy(tp->board_part_number, "BCM57790");
12748 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
12749 strcpy(tp->board_part_number, "BCM57788");
12750 else
12751 goto nomatch;
12752 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
12753 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
12754 strcpy(tp->board_part_number, "BCM57761");
12755 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
12756 strcpy(tp->board_part_number, "BCM57765");
12757 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
12758 strcpy(tp->board_part_number, "BCM57781");
12759 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
12760 strcpy(tp->board_part_number, "BCM57785");
12761 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
12762 strcpy(tp->board_part_number, "BCM57791");
12763 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
12764 strcpy(tp->board_part_number, "BCM57795");
12765 else
12766 goto nomatch;
12767 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070012768 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000012769 } else {
12770nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070012771 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000012772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012773}
12774
Matt Carlson9c8a6202007-10-21 16:16:08 -070012775static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
12776{
12777 u32 val;
12778
Matt Carlsone4f34112009-02-25 14:25:00 +000012779 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070012780 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000012781 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070012782 val != 0)
12783 return 0;
12784
12785 return 1;
12786}
12787
Matt Carlsonacd9c112009-02-25 14:26:33 +000012788static void __devinit tg3_read_bc_ver(struct tg3 *tp)
12789{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012790 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000012791 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012792 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000012793
12794 if (tg3_nvram_read(tp, 0xc, &offset) ||
12795 tg3_nvram_read(tp, 0x4, &start))
12796 return;
12797
12798 offset = tg3_nvram_logical_addr(tp, offset);
12799
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012800 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000012801 return;
12802
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012803 if ((val & 0xfc000000) == 0x0c000000) {
12804 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000012805 return;
12806
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012807 if (val == 0)
12808 newver = true;
12809 }
12810
Matt Carlson75f99362010-04-05 10:19:24 +000012811 dst_off = strlen(tp->fw_ver);
12812
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012813 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000012814 if (TG3_VER_SIZE - dst_off < 16 ||
12815 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012816 return;
12817
12818 offset = offset + ver_offset - start;
12819 for (i = 0; i < 16; i += 4) {
12820 __be32 v;
12821 if (tg3_nvram_read_be32(tp, offset + i, &v))
12822 return;
12823
Matt Carlson75f99362010-04-05 10:19:24 +000012824 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012825 }
12826 } else {
12827 u32 major, minor;
12828
12829 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
12830 return;
12831
12832 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
12833 TG3_NVM_BCVER_MAJSFT;
12834 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000012835 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
12836 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000012837 }
12838}
12839
Matt Carlsona6f6cb12009-02-25 14:27:43 +000012840static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
12841{
12842 u32 val, major, minor;
12843
12844 /* Use native endian representation */
12845 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
12846 return;
12847
12848 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
12849 TG3_NVM_HWSB_CFG1_MAJSFT;
12850 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
12851 TG3_NVM_HWSB_CFG1_MINSFT;
12852
12853 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
12854}
12855
Matt Carlsondfe00d72008-11-21 17:19:41 -080012856static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
12857{
12858 u32 offset, major, minor, build;
12859
Matt Carlson75f99362010-04-05 10:19:24 +000012860 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080012861
12862 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
12863 return;
12864
12865 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
12866 case TG3_EEPROM_SB_REVISION_0:
12867 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
12868 break;
12869 case TG3_EEPROM_SB_REVISION_2:
12870 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
12871 break;
12872 case TG3_EEPROM_SB_REVISION_3:
12873 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
12874 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000012875 case TG3_EEPROM_SB_REVISION_4:
12876 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
12877 break;
12878 case TG3_EEPROM_SB_REVISION_5:
12879 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
12880 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000012881 case TG3_EEPROM_SB_REVISION_6:
12882 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
12883 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080012884 default:
12885 return;
12886 }
12887
Matt Carlsone4f34112009-02-25 14:25:00 +000012888 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080012889 return;
12890
12891 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
12892 TG3_EEPROM_SB_EDH_BLD_SHFT;
12893 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
12894 TG3_EEPROM_SB_EDH_MAJ_SHFT;
12895 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
12896
12897 if (minor > 99 || build > 26)
12898 return;
12899
Matt Carlson75f99362010-04-05 10:19:24 +000012900 offset = strlen(tp->fw_ver);
12901 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
12902 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080012903
12904 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000012905 offset = strlen(tp->fw_ver);
12906 if (offset < TG3_VER_SIZE - 1)
12907 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080012908 }
12909}
12910
Matt Carlsonacd9c112009-02-25 14:26:33 +000012911static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080012912{
12913 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000012914 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070012915
12916 for (offset = TG3_NVM_DIR_START;
12917 offset < TG3_NVM_DIR_END;
12918 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000012919 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012920 return;
12921
12922 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
12923 break;
12924 }
12925
12926 if (offset == TG3_NVM_DIR_END)
12927 return;
12928
12929 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
12930 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000012931 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012932 return;
12933
Matt Carlsone4f34112009-02-25 14:25:00 +000012934 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070012935 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000012936 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012937 return;
12938
12939 offset += val - start;
12940
Matt Carlsonacd9c112009-02-25 14:26:33 +000012941 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012942
Matt Carlsonacd9c112009-02-25 14:26:33 +000012943 tp->fw_ver[vlen++] = ',';
12944 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070012945
12946 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012947 __be32 v;
12948 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012949 return;
12950
Al Virob9fc7dc2007-12-17 22:59:57 -080012951 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012952
Matt Carlsonacd9c112009-02-25 14:26:33 +000012953 if (vlen > TG3_VER_SIZE - sizeof(v)) {
12954 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012955 break;
12956 }
12957
Matt Carlsonacd9c112009-02-25 14:26:33 +000012958 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
12959 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012960 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000012961}
12962
Matt Carlson7fd76442009-02-25 14:27:20 +000012963static void __devinit tg3_read_dash_ver(struct tg3 *tp)
12964{
12965 int vlen;
12966 u32 apedata;
Matt Carlsonecc79642010-08-02 11:26:01 +000012967 char *fwtype;
Matt Carlson7fd76442009-02-25 14:27:20 +000012968
12969 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) ||
12970 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
12971 return;
12972
12973 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
12974 if (apedata != APE_SEG_SIG_MAGIC)
12975 return;
12976
12977 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
12978 if (!(apedata & APE_FW_STATUS_READY))
12979 return;
12980
12981 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
12982
Matt Carlsondc6d0742010-09-15 08:59:55 +000012983 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI) {
12984 tp->tg3_flags3 |= TG3_FLG3_APE_HAS_NCSI;
Matt Carlsonecc79642010-08-02 11:26:01 +000012985 fwtype = "NCSI";
Matt Carlsondc6d0742010-09-15 08:59:55 +000012986 } else {
Matt Carlsonecc79642010-08-02 11:26:01 +000012987 fwtype = "DASH";
Matt Carlsondc6d0742010-09-15 08:59:55 +000012988 }
Matt Carlsonecc79642010-08-02 11:26:01 +000012989
Matt Carlson7fd76442009-02-25 14:27:20 +000012990 vlen = strlen(tp->fw_ver);
12991
Matt Carlsonecc79642010-08-02 11:26:01 +000012992 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
12993 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000012994 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
12995 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
12996 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
12997 (apedata & APE_FW_VERSION_BLDMSK));
12998}
12999
Matt Carlsonacd9c112009-02-25 14:26:33 +000013000static void __devinit tg3_read_fw_ver(struct tg3 *tp)
13001{
13002 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000013003 bool vpd_vers = false;
13004
13005 if (tp->fw_ver[0] != 0)
13006 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013007
Matt Carlsondf259d82009-04-20 06:57:14 +000013008 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) {
Matt Carlson75f99362010-04-05 10:19:24 +000013009 strcat(tp->fw_ver, "sb");
Matt Carlsondf259d82009-04-20 06:57:14 +000013010 return;
13011 }
13012
Matt Carlsonacd9c112009-02-25 14:26:33 +000013013 if (tg3_nvram_read(tp, 0, &val))
13014 return;
13015
13016 if (val == TG3_EEPROM_MAGIC)
13017 tg3_read_bc_ver(tp);
13018 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
13019 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013020 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
13021 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013022 else
13023 return;
13024
13025 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
Matt Carlson75f99362010-04-05 10:19:24 +000013026 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) || vpd_vers)
13027 goto done;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013028
13029 tg3_read_mgmtfw_ver(tp);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013030
Matt Carlson75f99362010-04-05 10:19:24 +000013031done:
Matt Carlson9c8a6202007-10-21 16:16:08 -070013032 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080013033}
13034
Michael Chan7544b092007-05-05 13:08:32 -070013035static struct pci_dev * __devinit tg3_find_peer(struct tg3 *);
13036
Eric Dumazet7fe876a2010-07-08 06:14:55 +000013037static void inline vlan_features_add(struct net_device *dev, unsigned long flags)
13038{
13039#if TG3_VLAN_TAG_USED
13040 dev->vlan_features |= flags;
13041#endif
13042}
13043
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013044static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
13045{
13046 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13047 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
13048 return 4096;
13049 else if ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) &&
13050 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
13051 return 1024;
13052 else
13053 return 512;
13054}
13055
Linus Torvalds1da177e2005-04-16 15:20:36 -070013056static int __devinit tg3_get_invariants(struct tg3 *tp)
13057{
13058 static struct pci_device_id write_reorder_chipsets[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013059 { PCI_DEVICE(PCI_VENDOR_ID_AMD,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000013060 PCI_DEVICE_ID_AMD_FE_GATE_700C) },
John W. Linvillec165b002006-07-08 13:28:53 -070013061 { PCI_DEVICE(PCI_VENDOR_ID_AMD,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000013062 PCI_DEVICE_ID_AMD_8131_BRIDGE) },
Michael Chan399de502005-10-03 14:02:39 -070013063 { PCI_DEVICE(PCI_VENDOR_ID_VIA,
13064 PCI_DEVICE_ID_VIA_8385_0) },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013065 { },
13066 };
13067 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013068 u32 pci_state_reg, grc_misc_cfg;
13069 u32 val;
13070 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013071 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013072
Linus Torvalds1da177e2005-04-16 15:20:36 -070013073 /* Force memory write invalidate off. If we leave it on,
13074 * then on 5700_BX chips we have to enable a workaround.
13075 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
13076 * to match the cacheline size. The Broadcom driver have this
13077 * workaround but turns MWI off all the times so never uses
13078 * it. This seems to suggest that the workaround is insufficient.
13079 */
13080 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13081 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
13082 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13083
13084 /* It is absolutely critical that TG3PCI_MISC_HOST_CTRL
13085 * has the register indirect write enable bit set before
13086 * we try to access any of the MMIO registers. It is also
13087 * critical that the PCI-X hw workaround situation is decided
13088 * before that as well.
13089 */
13090 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13091 &misc_ctrl_reg);
13092
13093 tp->pci_chip_rev_id = (misc_ctrl_reg >>
13094 MISC_HOST_CTRL_CHIPREV_SHIFT);
Matt Carlson795d01c2007-10-07 23:28:17 -070013095 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
13096 u32 prod_id_asic_rev;
13097
Matt Carlson5001e2f2009-11-13 13:03:51 +000013098 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
13099 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
Matt Carlsona50d0792010-06-05 17:24:37 +000013100 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013101 pci_read_config_dword(tp->pdev,
13102 TG3PCI_GEN2_PRODID_ASICREV,
13103 &prod_id_asic_rev);
Matt Carlsonb703df62009-12-03 08:36:21 +000013104 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
13105 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
13106 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
13107 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
13108 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
13109 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13110 pci_read_config_dword(tp->pdev,
13111 TG3PCI_GEN15_PRODID_ASICREV,
13112 &prod_id_asic_rev);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013113 else
13114 pci_read_config_dword(tp->pdev, TG3PCI_PRODID_ASICREV,
13115 &prod_id_asic_rev);
13116
Matt Carlson321d32a2008-11-21 17:22:19 -080013117 tp->pci_chip_rev_id = prod_id_asic_rev;
Matt Carlson795d01c2007-10-07 23:28:17 -070013118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013119
Michael Chanff645be2005-04-21 17:09:53 -070013120 /* Wrong chip ID in 5752 A0. This code can be removed later
13121 * as A0 is not in production.
13122 */
13123 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
13124 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
13125
Michael Chan68929142005-08-09 20:17:14 -070013126 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
13127 * we need to disable memory and use config. cycles
13128 * only to access all registers. The 5702/03 chips
13129 * can mistakenly decode the special cycles from the
13130 * ICH chipsets as memory write cycles, causing corruption
13131 * of register and memory space. Only certain ICH bridges
13132 * will drive special cycles with non-zero data during the
13133 * address phase which can fall within the 5703's address
13134 * range. This is not an ICH bug as the PCI spec allows
13135 * non-zero address during special cycles. However, only
13136 * these ICH bridges are known to drive non-zero addresses
13137 * during special cycles.
13138 *
13139 * Since special cycles do not cross PCI bridges, we only
13140 * enable this workaround if the 5703 is on the secondary
13141 * bus of these ICH bridges.
13142 */
13143 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
13144 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
13145 static struct tg3_dev_id {
13146 u32 vendor;
13147 u32 device;
13148 u32 rev;
13149 } ich_chipsets[] = {
13150 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
13151 PCI_ANY_ID },
13152 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
13153 PCI_ANY_ID },
13154 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
13155 0xa },
13156 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
13157 PCI_ANY_ID },
13158 { },
13159 };
13160 struct tg3_dev_id *pci_id = &ich_chipsets[0];
13161 struct pci_dev *bridge = NULL;
13162
13163 while (pci_id->vendor != 0) {
13164 bridge = pci_get_device(pci_id->vendor, pci_id->device,
13165 bridge);
13166 if (!bridge) {
13167 pci_id++;
13168 continue;
13169 }
13170 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070013171 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070013172 continue;
13173 }
13174 if (bridge->subordinate &&
13175 (bridge->subordinate->number ==
13176 tp->pdev->bus->number)) {
13177
13178 tp->tg3_flags2 |= TG3_FLG2_ICH_WORKAROUND;
13179 pci_dev_put(bridge);
13180 break;
13181 }
13182 }
13183 }
13184
Matt Carlson41588ba2008-04-19 18:12:33 -070013185 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
13186 static struct tg3_dev_id {
13187 u32 vendor;
13188 u32 device;
13189 } bridge_chipsets[] = {
13190 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
13191 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
13192 { },
13193 };
13194 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
13195 struct pci_dev *bridge = NULL;
13196
13197 while (pci_id->vendor != 0) {
13198 bridge = pci_get_device(pci_id->vendor,
13199 pci_id->device,
13200 bridge);
13201 if (!bridge) {
13202 pci_id++;
13203 continue;
13204 }
13205 if (bridge->subordinate &&
13206 (bridge->subordinate->number <=
13207 tp->pdev->bus->number) &&
13208 (bridge->subordinate->subordinate >=
13209 tp->pdev->bus->number)) {
13210 tp->tg3_flags3 |= TG3_FLG3_5701_DMA_BUG;
13211 pci_dev_put(bridge);
13212 break;
13213 }
13214 }
13215 }
13216
Michael Chan4a29cc22006-03-19 13:21:12 -080013217 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
13218 * DMA addresses > 40-bit. This bridge may have other additional
13219 * 57xx devices behind it in some 4-port NIC designs for example.
13220 * Any tg3 device found behind the bridge will also need the 40-bit
13221 * DMA workaround.
13222 */
Michael Chana4e2b342005-10-26 15:46:52 -070013223 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
13224 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
13225 tp->tg3_flags2 |= TG3_FLG2_5780_CLASS;
Michael Chan4a29cc22006-03-19 13:21:12 -080013226 tp->tg3_flags |= TG3_FLAG_40BIT_DMA_BUG;
Michael Chan4cf78e42005-07-25 12:29:19 -070013227 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a5882010-04-05 10:19:28 +000013228 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080013229 struct pci_dev *bridge = NULL;
13230
13231 do {
13232 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
13233 PCI_DEVICE_ID_SERVERWORKS_EPB,
13234 bridge);
13235 if (bridge && bridge->subordinate &&
13236 (bridge->subordinate->number <=
13237 tp->pdev->bus->number) &&
13238 (bridge->subordinate->subordinate >=
13239 tp->pdev->bus->number)) {
13240 tp->tg3_flags |= TG3_FLAG_40BIT_DMA_BUG;
13241 pci_dev_put(bridge);
13242 break;
13243 }
13244 } while (bridge);
13245 }
Michael Chan4cf78e42005-07-25 12:29:19 -070013246
Linus Torvalds1da177e2005-04-16 15:20:36 -070013247 /* Initialize misc host control in PCI block. */
13248 tp->misc_host_ctrl |= (misc_ctrl_reg &
13249 MISC_HOST_CTRL_CHIPREV);
13250 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13251 tp->misc_host_ctrl);
13252
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013253 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
13254 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
13255 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Michael Chan7544b092007-05-05 13:08:32 -070013256 tp->pdev_peer = tg3_find_peer(tp);
13257
Matt Carlsonc885e822010-08-02 11:25:57 +000013258 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13259 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13260 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
13261 tp->tg3_flags3 |= TG3_FLG3_5717_PLUS;
13262
Matt Carlson321d32a2008-11-21 17:22:19 -080013263 /* Intentionally exclude ASIC_REV_5906 */
13264 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Michael Chand9ab5ad2006-03-20 22:27:35 -080013265 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013266 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013267 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013268 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013269 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlsonc885e822010-08-02 11:25:57 +000013270 (tp->tg3_flags3 & TG3_FLG3_5717_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080013271 tp->tg3_flags3 |= TG3_FLG3_5755_PLUS;
13272
13273 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13274 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Michael Chanb5d37722006-09-27 16:06:21 -070013275 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013276 (tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
Michael Chana4e2b342005-10-26 15:46:52 -070013277 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
John W. Linville6708e5c2005-04-21 17:00:52 -070013278 tp->tg3_flags2 |= TG3_FLG2_5750_PLUS;
13279
John W. Linville1b440c562005-04-21 17:03:18 -070013280 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) ||
13281 (tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
13282 tp->tg3_flags2 |= TG3_FLG2_5705_PLUS;
13283
Matt Carlson027455a2008-12-21 20:19:30 -080013284 /* 5700 B0 chips do not support checksumming correctly due
13285 * to hardware bugs.
13286 */
13287 if (tp->pci_chip_rev_id == CHIPREV_ID_5700_B0)
13288 tp->tg3_flags |= TG3_FLAG_BROKEN_CHECKSUMS;
13289 else {
Eric Dumazet7fe876a2010-07-08 06:14:55 +000013290 unsigned long features = NETIF_F_IP_CSUM | NETIF_F_SG | NETIF_F_GRO;
13291
Matt Carlson027455a2008-12-21 20:19:30 -080013292 tp->tg3_flags |= TG3_FLAG_RX_CHECKSUMS;
Matt Carlson027455a2008-12-21 20:19:30 -080013293 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Eric Dumazet7fe876a2010-07-08 06:14:55 +000013294 features |= NETIF_F_IPV6_CSUM;
13295 tp->dev->features |= features;
13296 vlan_features_add(tp->dev, features);
Matt Carlson027455a2008-12-21 20:19:30 -080013297 }
13298
Matt Carlson507399f2009-11-13 13:03:37 +000013299 /* Determine TSO capabilities */
Matt Carlsonc885e822010-08-02 11:25:57 +000013300 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)
Matt Carlsone849cdc2009-11-13 13:03:38 +000013301 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_3;
13302 else if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
13303 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlson507399f2009-11-13 13:03:37 +000013304 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_2;
13305 else if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
13306 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_1 | TG3_FLG2_TSO_BUG;
13307 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
13308 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
13309 tp->tg3_flags2 &= ~TG3_FLG2_TSO_BUG;
13310 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13311 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13312 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
13313 tp->tg3_flags2 |= TG3_FLG2_TSO_BUG;
13314 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
13315 tp->fw_needed = FIRMWARE_TG3TSO5;
13316 else
13317 tp->fw_needed = FIRMWARE_TG3TSO;
13318 }
13319
13320 tp->irq_max = 1;
13321
Michael Chan5a6f3072006-03-20 22:28:05 -080013322 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
Michael Chan7544b092007-05-05 13:08:32 -070013323 tp->tg3_flags |= TG3_FLAG_SUPPORT_MSI;
13324 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
13325 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
13326 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
13327 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
13328 tp->pdev_peer == tp->pdev))
13329 tp->tg3_flags &= ~TG3_FLAG_SUPPORT_MSI;
13330
Matt Carlson321d32a2008-11-21 17:22:19 -080013331 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070013332 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanfcfa0a32006-03-20 22:28:41 -080013333 tp->tg3_flags2 |= TG3_FLG2_1SHOT_MSI;
Michael Chan52c0fd82006-06-29 20:15:54 -070013334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013335
Matt Carlsonc885e822010-08-02 11:25:57 +000013336 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
Matt Carlson507399f2009-11-13 13:03:37 +000013337 tp->tg3_flags |= TG3_FLAG_SUPPORT_MSIX;
13338 tp->irq_max = TG3_IRQ_MAX_VECS;
13339 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013340 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000013341
Matt Carlson615774f2009-11-13 13:03:39 +000013342 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsona50d0792010-06-05 17:24:37 +000013343 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlson615774f2009-11-13 13:03:39 +000013344 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
13345 tp->tg3_flags3 |= TG3_FLG3_SHORT_DMA_BUG;
13346 else if (!(tp->tg3_flags3 & TG3_FLG3_5755_PLUS)) {
13347 tp->tg3_flags3 |= TG3_FLG3_4G_DMA_BNDRY_BUG;
13348 tp->tg3_flags3 |= TG3_FLG3_40BIT_DMA_LIMIT_BUG;
Matt Carlson0e1406d2009-11-02 12:33:33 +000013349 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013350
Matt Carlsonc885e822010-08-02 11:25:57 +000013351 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)
Matt Carlsonb703df62009-12-03 08:36:21 +000013352 tp->tg3_flags3 |= TG3_FLG3_USE_JUMBO_BDFLAG;
13353
Matt Carlsonf51f3562008-05-25 23:45:08 -070013354 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
Matt Carlsonc6cdf432010-04-05 10:19:26 +000013355 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
13356 (tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG))
Matt Carlson8f666b02009-08-28 13:58:24 +000013357 tp->tg3_flags |= TG3_FLAG_JUMBO_CAPABLE;
Michael Chan0f893dc2005-07-25 12:30:38 -070013358
Matt Carlson52f44902008-11-21 17:17:04 -080013359 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
13360 &pci_state_reg);
13361
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013362 tp->pcie_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_EXP);
13363 if (tp->pcie_cap != 0) {
13364 u16 lnkctl;
13365
Linus Torvalds1da177e2005-04-16 15:20:36 -070013366 tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS;
Matt Carlson5f5c51e2007-11-12 21:19:37 -080013367
Matt Carlsoncf790032010-11-24 08:31:48 +000013368 tp->pcie_readrq = 4096;
13369 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) {
13370 u16 word;
13371
13372 pci_read_config_word(tp->pdev,
13373 tp->pcie_cap + PCI_EXP_LNKSTA,
13374 &word);
13375 switch (word & PCI_EXP_LNKSTA_CLS) {
13376 case PCI_EXP_LNKSTA_CLS_2_5GB:
13377 word &= PCI_EXP_LNKSTA_NLW;
13378 word >>= PCI_EXP_LNKSTA_NLW_SHIFT;
13379 switch (word) {
13380 case 2:
13381 tp->pcie_readrq = 2048;
13382 break;
13383 case 4:
13384 tp->pcie_readrq = 1024;
13385 break;
13386 }
13387 break;
13388
13389 case PCI_EXP_LNKSTA_CLS_5_0GB:
13390 word &= PCI_EXP_LNKSTA_NLW;
13391 word >>= PCI_EXP_LNKSTA_NLW_SHIFT;
13392 switch (word) {
13393 case 1:
13394 tp->pcie_readrq = 2048;
13395 break;
13396 case 2:
13397 tp->pcie_readrq = 1024;
13398 break;
13399 case 4:
13400 tp->pcie_readrq = 512;
13401 break;
13402 }
13403 }
13404 }
13405
13406 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080013407
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013408 pci_read_config_word(tp->pdev,
13409 tp->pcie_cap + PCI_EXP_LNKCTL,
13410 &lnkctl);
13411 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
13412 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanc7835a72006-11-15 21:14:42 -080013413 tp->tg3_flags2 &= ~TG3_FLG2_HW_TSO_2;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013414 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013415 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000013416 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
13417 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013418 tp->tg3_flags3 |= TG3_FLG3_CLKREQ_BUG;
Matt Carlson614b05902010-01-20 16:58:02 +000013419 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
13420 tp->tg3_flags3 |= TG3_FLG3_L1PLLPD_EN;
Michael Chanc7835a72006-11-15 21:14:42 -080013421 }
Matt Carlson52f44902008-11-21 17:17:04 -080013422 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Matt Carlsonfcb389d2008-11-03 16:55:44 -080013423 tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS;
Matt Carlson52f44902008-11-21 17:17:04 -080013424 } else if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
13425 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
13426 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
13427 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000013428 dev_err(&tp->pdev->dev,
13429 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080013430 return -EIO;
13431 }
13432
13433 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
13434 tp->tg3_flags |= TG3_FLAG_PCIX_MODE;
13435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013436
Michael Chan399de502005-10-03 14:02:39 -070013437 /* If we have an AMD 762 or VIA K8T800 chipset, write
13438 * reordering to the mailbox registers done by the host
13439 * controller can cause major troubles. We read back from
13440 * every mailbox register write to force the writes to be
13441 * posted to the chip in order.
13442 */
13443 if (pci_dev_present(write_reorder_chipsets) &&
13444 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
13445 tp->tg3_flags |= TG3_FLAG_MBOX_WRITE_REORDER;
13446
Matt Carlson69fc4052008-12-21 20:19:57 -080013447 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
13448 &tp->pci_cacheline_sz);
13449 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13450 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013451 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
13452 tp->pci_lat_timer < 64) {
13453 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080013454 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13455 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013456 }
13457
Matt Carlson52f44902008-11-21 17:17:04 -080013458 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
13459 /* 5700 BX chips need to have their TX producer index
13460 * mailboxes written twice to workaround a bug.
13461 */
13462 tp->tg3_flags |= TG3_FLAG_TXD_MBOX_HWBUG;
Matt Carlson9974a352007-10-07 23:27:28 -070013463
Matt Carlson52f44902008-11-21 17:17:04 -080013464 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013465 *
13466 * The workaround is to use indirect register accesses
13467 * for all chip writes not to mailbox registers.
13468 */
Matt Carlson52f44902008-11-21 17:17:04 -080013469 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013470 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013471
13472 tp->tg3_flags |= TG3_FLAG_PCIX_TARGET_HWBUG;
13473
13474 /* The chip can have it's power management PCI config
13475 * space registers clobbered due to this bug.
13476 * So explicitly force the chip into D0 here.
13477 */
Matt Carlson9974a352007-10-07 23:27:28 -070013478 pci_read_config_dword(tp->pdev,
13479 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013480 &pm_reg);
13481 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
13482 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070013483 pci_write_config_dword(tp->pdev,
13484 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013485 pm_reg);
13486
13487 /* Also, force SERR#/PERR# in PCI command. */
13488 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13489 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
13490 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13491 }
13492 }
13493
Linus Torvalds1da177e2005-04-16 15:20:36 -070013494 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
13495 tp->tg3_flags |= TG3_FLAG_PCI_HIGH_SPEED;
13496 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
13497 tp->tg3_flags |= TG3_FLAG_PCI_32BIT;
13498
13499 /* Chip-specific fixup from Broadcom driver */
13500 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
13501 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
13502 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
13503 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
13504 }
13505
Michael Chan1ee582d2005-08-09 20:16:46 -070013506 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070013507 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013508 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070013509 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070013510 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013511 tp->write32_tx_mbox = tg3_write32;
13512 tp->write32_rx_mbox = tg3_write32;
13513
13514 /* Various workaround register access methods */
13515 if (tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG)
13516 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013517 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
13518 ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
13519 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
13520 /*
13521 * Back to back register writes can cause problems on these
13522 * chips, the workaround is to read back all reg writes
13523 * except those to mailbox regs.
13524 *
13525 * See tg3_write_indirect_reg32().
13526 */
Michael Chan1ee582d2005-08-09 20:16:46 -070013527 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013528 }
13529
Michael Chan1ee582d2005-08-09 20:16:46 -070013530 if ((tp->tg3_flags & TG3_FLAG_TXD_MBOX_HWBUG) ||
13531 (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)) {
13532 tp->write32_tx_mbox = tg3_write32_tx_mbox;
13533 if (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)
13534 tp->write32_rx_mbox = tg3_write_flush_reg32;
13535 }
Michael Chan20094932005-08-09 20:16:32 -070013536
Michael Chan68929142005-08-09 20:17:14 -070013537 if (tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND) {
13538 tp->read32 = tg3_read_indirect_reg32;
13539 tp->write32 = tg3_write_indirect_reg32;
13540 tp->read32_mbox = tg3_read_indirect_mbox;
13541 tp->write32_mbox = tg3_write_indirect_mbox;
13542 tp->write32_tx_mbox = tg3_write_indirect_mbox;
13543 tp->write32_rx_mbox = tg3_write_indirect_mbox;
13544
13545 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070013546 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070013547
13548 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13549 pci_cmd &= ~PCI_COMMAND_MEMORY;
13550 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13551 }
Michael Chanb5d37722006-09-27 16:06:21 -070013552 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
13553 tp->read32_mbox = tg3_read32_mbox_5906;
13554 tp->write32_mbox = tg3_write32_mbox_5906;
13555 tp->write32_tx_mbox = tg3_write32_mbox_5906;
13556 tp->write32_rx_mbox = tg3_write32_mbox_5906;
13557 }
Michael Chan68929142005-08-09 20:17:14 -070013558
Michael Chanbbadf502006-04-06 21:46:34 -070013559 if (tp->write32 == tg3_write_indirect_reg32 ||
13560 ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) &&
13561 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070013562 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Michael Chanbbadf502006-04-06 21:46:34 -070013563 tp->tg3_flags |= TG3_FLAG_SRAM_USE_CONFIG;
13564
Michael Chan7d0c41e2005-04-21 17:06:20 -070013565 /* Get eeprom hw config before calling tg3_set_power_state().
Michael Chan9d26e212006-12-07 00:21:14 -080013566 * In particular, the TG3_FLG2_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070013567 * determined before calling tg3_set_power_state() so that
13568 * we know whether or not to switch out of Vaux power.
13569 * When the flag is set, it means that GPIO1 is used for eeprom
13570 * write protect and also implies that it is a LOM where GPIOs
13571 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013572 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070013573 tg3_get_eeprom_hw_cfg(tp);
13574
Matt Carlson0d3031d2007-10-10 18:02:43 -070013575 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
13576 /* Allow reads and writes to the
13577 * APE register and memory space.
13578 */
13579 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc2010-06-05 17:24:30 +000013580 PCISTATE_ALLOW_APE_SHMEM_WR |
13581 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070013582 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
13583 pci_state_reg);
13584 }
13585
Matt Carlson9936bcf2007-10-10 18:03:07 -070013586 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013587 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013588 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013589 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlsonc885e822010-08-02 11:25:57 +000013590 (tp->tg3_flags3 & TG3_FLG3_5717_PLUS))
Matt Carlsond30cdd22007-10-07 23:28:35 -070013591 tp->tg3_flags |= TG3_FLAG_CPMU_PRESENT;
13592
Michael Chan314fba32005-04-21 17:07:04 -070013593 /* Set up tp->grc_local_ctrl before calling tg3_set_power_state().
13594 * GPIO1 driven high will bring 5700's external PHY out of reset.
13595 * It is also used as eeprom write protect on LOMs.
13596 */
13597 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
13598 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
13599 (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT))
13600 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
13601 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070013602 /* Unused GPIO3 must be driven as output on 5752 because there
13603 * are no pull-up resistors on unused GPIO pins.
13604 */
13605 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
13606 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070013607
Matt Carlson321d32a2008-11-21 17:22:19 -080013608 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsoncb4ed1f2010-01-20 16:58:09 +000013609 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
13610 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Michael Chanaf36e6b2006-03-23 01:28:06 -080013611 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
13612
Matt Carlson8d519ab2009-04-20 06:58:01 +000013613 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
13614 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070013615 /* Turn off the debug UART. */
13616 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
13617 if (tp->tg3_flags2 & TG3_FLG2_IS_NIC)
13618 /* Keep VMain power. */
13619 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
13620 GRC_LCLCTRL_GPIO_OUTPUT0;
13621 }
13622
Linus Torvalds1da177e2005-04-16 15:20:36 -070013623 /* Force the chip into D0. */
Michael Chanbc1c7562006-03-20 17:48:03 -080013624 err = tg3_set_power_state(tp, PCI_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013625 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000013626 dev_err(&tp->pdev->dev, "Transition to D0 failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070013627 return err;
13628 }
13629
Linus Torvalds1da177e2005-04-16 15:20:36 -070013630 /* Derive initial jumbo mode from MTU assigned in
13631 * ether_setup() via the alloc_etherdev() call
13632 */
Michael Chan0f893dc2005-07-25 12:30:38 -070013633 if (tp->dev->mtu > ETH_DATA_LEN &&
Michael Chana4e2b342005-10-26 15:46:52 -070013634 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Michael Chan0f893dc2005-07-25 12:30:38 -070013635 tp->tg3_flags |= TG3_FLAG_JUMBO_RING_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013636
13637 /* Determine WakeOnLan speed to use. */
13638 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13639 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
13640 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
13641 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
13642 tp->tg3_flags &= ~(TG3_FLAG_WOL_SPEED_100MB);
13643 } else {
13644 tp->tg3_flags |= TG3_FLAG_WOL_SPEED_100MB;
13645 }
13646
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013647 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013648 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013649
Linus Torvalds1da177e2005-04-16 15:20:36 -070013650 /* A few boards don't want Ethernet@WireSpeed phy feature */
13651 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
13652 ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
13653 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070013654 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013655 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
13656 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
13657 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013658
13659 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
13660 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013661 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013662 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013663 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013664
Matt Carlson321d32a2008-11-21 17:22:19 -080013665 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013666 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080013667 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013668 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
Matt Carlsonc885e822010-08-02 11:25:57 +000013669 !(tp->tg3_flags3 & TG3_FLG3_5717_PLUS)) {
Michael Chanc424cb22006-04-29 18:56:34 -070013670 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013671 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013672 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
13673 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080013674 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
13675 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013676 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080013677 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013678 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080013679 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013680 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070013681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013682
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013683 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
13684 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
13685 tp->phy_otp = tg3_read_otp_phycfg(tp);
13686 if (tp->phy_otp == 0)
13687 tp->phy_otp = TG3_OTP_DEFAULT;
13688 }
13689
Matt Carlsonf51f3562008-05-25 23:45:08 -070013690 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT)
Matt Carlson8ef21422008-05-02 16:47:53 -070013691 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
13692 else
13693 tp->mi_mode = MAC_MI_MODE_BASE;
13694
Linus Torvalds1da177e2005-04-16 15:20:36 -070013695 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013696 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
13697 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
13698 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
13699
Matt Carlson321d32a2008-11-21 17:22:19 -080013700 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
13701 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson57e69832008-05-25 23:48:31 -070013702 tp->tg3_flags3 |= TG3_FLG3_USE_PHYLIB;
13703
Matt Carlson158d7ab2008-05-29 01:37:54 -070013704 err = tg3_mdio_init(tp);
13705 if (err)
13706 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013707
13708 /* Initialize data/descriptor byte/word swapping. */
13709 val = tr32(GRC_MODE);
13710 val &= GRC_MODE_HOST_STACKUP;
13711 tw32(GRC_MODE, val | tp->grc_mode);
13712
13713 tg3_switch_clocks(tp);
13714
13715 /* Clear this out for sanity. */
13716 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
13717
13718 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
13719 &pci_state_reg);
13720 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
13721 (tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG) == 0) {
13722 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
13723
13724 if (chiprevid == CHIPREV_ID_5701_A0 ||
13725 chiprevid == CHIPREV_ID_5701_B0 ||
13726 chiprevid == CHIPREV_ID_5701_B2 ||
13727 chiprevid == CHIPREV_ID_5701_B5) {
13728 void __iomem *sram_base;
13729
13730 /* Write some dummy words into the SRAM status block
13731 * area, see if it reads back correctly. If the return
13732 * value is bad, force enable the PCIX workaround.
13733 */
13734 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
13735
13736 writel(0x00000000, sram_base);
13737 writel(0x00000000, sram_base + 4);
13738 writel(0xffffffff, sram_base + 4);
13739 if (readl(sram_base) != 0x00000000)
13740 tp->tg3_flags |= TG3_FLAG_PCIX_TARGET_HWBUG;
13741 }
13742 }
13743
13744 udelay(50);
13745 tg3_nvram_init(tp);
13746
13747 grc_misc_cfg = tr32(GRC_MISC_CFG);
13748 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
13749
Linus Torvalds1da177e2005-04-16 15:20:36 -070013750 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
13751 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
13752 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
13753 tp->tg3_flags2 |= TG3_FLG2_IS_5788;
13754
David S. Millerfac9b832005-05-18 22:46:34 -070013755 if (!(tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
13756 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700))
13757 tp->tg3_flags |= TG3_FLAG_TAGGED_STATUS;
13758 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) {
13759 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
13760 HOSTCC_MODE_CLRTICK_TXBD);
13761
13762 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
13763 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13764 tp->misc_host_ctrl);
13765 }
13766
Matt Carlson3bda1252008-08-15 14:08:22 -070013767 /* Preserve the APE MAC_MODE bits */
13768 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
Matt Carlsond2394e6b2010-11-24 08:31:47 +000013769 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070013770 else
13771 tp->mac_mode = TG3_DEF_MAC_MODE;
13772
Linus Torvalds1da177e2005-04-16 15:20:36 -070013773 /* these are limited to 10/100 only */
13774 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
13775 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
13776 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
13777 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
13778 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
13779 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
13780 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
13781 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
13782 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
Michael Chan676917d2006-12-07 00:20:22 -080013783 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
13784 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013785 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
Matt Carlsond1101142010-02-17 15:16:55 +000013786 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
13787 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013788 (tp->phy_flags & TG3_PHYFLG_IS_FET))
13789 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013790
13791 err = tg3_phy_probe(tp);
13792 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000013793 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013794 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013795 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013796 }
13797
Matt Carlson184b8902010-04-05 10:19:25 +000013798 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080013799 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013800
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013801 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
13802 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013803 } else {
13804 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013805 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013806 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013807 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013808 }
13809
13810 /* 5700 {AX,BX} chips have a broken status block link
13811 * change bit implementation, so we must use the
13812 * status register in those cases.
13813 */
13814 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
13815 tp->tg3_flags |= TG3_FLAG_USE_LINKCHG_REG;
13816 else
13817 tp->tg3_flags &= ~TG3_FLAG_USE_LINKCHG_REG;
13818
13819 /* The led_ctrl is set during tg3_phy_probe, here we might
13820 * have to force the link status polling mechanism based
13821 * upon subsystem IDs.
13822 */
13823 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070013824 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013825 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
13826 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
13827 tp->tg3_flags |= TG3_FLAG_USE_LINKCHG_REG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013828 }
13829
13830 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013831 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013832 tp->tg3_flags |= TG3_FLAG_POLL_SERDES;
13833 else
13834 tp->tg3_flags &= ~TG3_FLAG_POLL_SERDES;
13835
Matt Carlson9dc7a112010-04-12 06:58:28 +000013836 tp->rx_offset = NET_IP_ALIGN + TG3_RX_HEADROOM;
Matt Carlsond2757fc2010-04-12 06:58:27 +000013837 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013838 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsond2757fc2010-04-12 06:58:27 +000013839 (tp->tg3_flags & TG3_FLAG_PCIX_MODE) != 0) {
Matt Carlson9dc7a112010-04-12 06:58:28 +000013840 tp->rx_offset -= NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000013841#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000013842 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000013843#endif
13844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013845
Matt Carlson2c49a442010-09-30 10:34:35 +000013846 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
13847 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013848 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
13849
Matt Carlson2c49a442010-09-30 10:34:35 +000013850 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070013851
13852 /* Increment the rx prod index on the rx std ring by at most
13853 * 8 for these chips to workaround hw errata.
13854 */
13855 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13856 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
13857 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
13858 tp->rx_std_max_post = 8;
13859
Matt Carlson8ed5d972007-05-07 00:25:49 -070013860 if (tp->tg3_flags & TG3_FLAG_ASPM_WORKAROUND)
13861 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
13862 PCIE_PWR_MGMT_L1_THRESH_MSK;
13863
Linus Torvalds1da177e2005-04-16 15:20:36 -070013864 return err;
13865}
13866
David S. Miller49b6e95f2007-03-29 01:38:42 -070013867#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070013868static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
13869{
13870 struct net_device *dev = tp->dev;
13871 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070013872 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070013873 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070013874 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013875
David S. Miller49b6e95f2007-03-29 01:38:42 -070013876 addr = of_get_property(dp, "local-mac-address", &len);
13877 if (addr && len == 6) {
13878 memcpy(dev->dev_addr, addr, 6);
13879 memcpy(dev->perm_addr, dev->dev_addr, 6);
13880 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013881 }
13882 return -ENODEV;
13883}
13884
13885static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
13886{
13887 struct net_device *dev = tp->dev;
13888
13889 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070013890 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013891 return 0;
13892}
13893#endif
13894
13895static int __devinit tg3_get_device_address(struct tg3 *tp)
13896{
13897 struct net_device *dev = tp->dev;
13898 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080013899 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013900
David S. Miller49b6e95f2007-03-29 01:38:42 -070013901#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070013902 if (!tg3_get_macaddr_sparc(tp))
13903 return 0;
13904#endif
13905
13906 mac_offset = 0x7c;
David S. Millerf49639e2006-06-09 11:58:36 -070013907 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) ||
Michael Chana4e2b342005-10-26 15:46:52 -070013908 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013909 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
13910 mac_offset = 0xcc;
13911 if (tg3_nvram_lock(tp))
13912 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
13913 else
13914 tg3_nvram_unlock(tp);
Matt Carlsona50d0792010-06-05 17:24:37 +000013915 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13916 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) {
13917 if (PCI_FUNC(tp->pdev->devfn) & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000013918 mac_offset = 0xcc;
Matt Carlsona50d0792010-06-05 17:24:37 +000013919 if (PCI_FUNC(tp->pdev->devfn) > 1)
13920 mac_offset += 0x18c;
Matt Carlsona1b950d2009-09-01 13:20:17 +000013921 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070013922 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013923
13924 /* First try to get it from MAC address mailbox. */
13925 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
13926 if ((hi >> 16) == 0x484b) {
13927 dev->dev_addr[0] = (hi >> 8) & 0xff;
13928 dev->dev_addr[1] = (hi >> 0) & 0xff;
13929
13930 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
13931 dev->dev_addr[2] = (lo >> 24) & 0xff;
13932 dev->dev_addr[3] = (lo >> 16) & 0xff;
13933 dev->dev_addr[4] = (lo >> 8) & 0xff;
13934 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013935
Michael Chan008652b2006-03-27 23:14:53 -080013936 /* Some old bootcode may report a 0 MAC address in SRAM */
13937 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
13938 }
13939 if (!addr_ok) {
13940 /* Next, try NVRAM. */
Matt Carlsondf259d82009-04-20 06:57:14 +000013941 if (!(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) &&
13942 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000013943 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070013944 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
13945 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080013946 }
13947 /* Finally just fetch it out of the MAC control regs. */
13948 else {
13949 hi = tr32(MAC_ADDR_0_HIGH);
13950 lo = tr32(MAC_ADDR_0_LOW);
13951
13952 dev->dev_addr[5] = lo & 0xff;
13953 dev->dev_addr[4] = (lo >> 8) & 0xff;
13954 dev->dev_addr[3] = (lo >> 16) & 0xff;
13955 dev->dev_addr[2] = (lo >> 24) & 0xff;
13956 dev->dev_addr[1] = hi & 0xff;
13957 dev->dev_addr[0] = (hi >> 8) & 0xff;
13958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013959 }
13960
13961 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070013962#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070013963 if (!tg3_get_default_macaddr_sparc(tp))
13964 return 0;
13965#endif
13966 return -EINVAL;
13967 }
John W. Linville2ff43692005-09-12 14:44:20 -070013968 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013969 return 0;
13970}
13971
David S. Miller59e6b432005-05-18 22:50:10 -070013972#define BOUNDARY_SINGLE_CACHELINE 1
13973#define BOUNDARY_MULTI_CACHELINE 2
13974
13975static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
13976{
13977 int cacheline_size;
13978 u8 byte;
13979 int goal;
13980
13981 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
13982 if (byte == 0)
13983 cacheline_size = 1024;
13984 else
13985 cacheline_size = (int) byte * 4;
13986
13987 /* On 5703 and later chips, the boundary bits have no
13988 * effect.
13989 */
13990 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13991 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13992 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
13993 goto out;
13994
13995#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
13996 goal = BOUNDARY_MULTI_CACHELINE;
13997#else
13998#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
13999 goal = BOUNDARY_SINGLE_CACHELINE;
14000#else
14001 goal = 0;
14002#endif
14003#endif
14004
Matt Carlsonc885e822010-08-02 11:25:57 +000014005 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014006 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
14007 goto out;
14008 }
14009
David S. Miller59e6b432005-05-18 22:50:10 -070014010 if (!goal)
14011 goto out;
14012
14013 /* PCI controllers on most RISC systems tend to disconnect
14014 * when a device tries to burst across a cache-line boundary.
14015 * Therefore, letting tg3 do so just wastes PCI bandwidth.
14016 *
14017 * Unfortunately, for PCI-E there are only limited
14018 * write-side controls for this, and thus for reads
14019 * we will still get the disconnects. We'll also waste
14020 * these PCI cycles for both read and write for chips
14021 * other than 5700 and 5701 which do not implement the
14022 * boundary bits.
14023 */
14024 if ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) &&
14025 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)) {
14026 switch (cacheline_size) {
14027 case 16:
14028 case 32:
14029 case 64:
14030 case 128:
14031 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14032 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
14033 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
14034 } else {
14035 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14036 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14037 }
14038 break;
14039
14040 case 256:
14041 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
14042 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
14043 break;
14044
14045 default:
14046 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14047 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14048 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014049 }
David S. Miller59e6b432005-05-18 22:50:10 -070014050 } else if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
14051 switch (cacheline_size) {
14052 case 16:
14053 case 32:
14054 case 64:
14055 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14056 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14057 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
14058 break;
14059 }
14060 /* fallthrough */
14061 case 128:
14062 default:
14063 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14064 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
14065 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014066 }
David S. Miller59e6b432005-05-18 22:50:10 -070014067 } else {
14068 switch (cacheline_size) {
14069 case 16:
14070 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14071 val |= (DMA_RWCTRL_READ_BNDRY_16 |
14072 DMA_RWCTRL_WRITE_BNDRY_16);
14073 break;
14074 }
14075 /* fallthrough */
14076 case 32:
14077 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14078 val |= (DMA_RWCTRL_READ_BNDRY_32 |
14079 DMA_RWCTRL_WRITE_BNDRY_32);
14080 break;
14081 }
14082 /* fallthrough */
14083 case 64:
14084 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14085 val |= (DMA_RWCTRL_READ_BNDRY_64 |
14086 DMA_RWCTRL_WRITE_BNDRY_64);
14087 break;
14088 }
14089 /* fallthrough */
14090 case 128:
14091 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14092 val |= (DMA_RWCTRL_READ_BNDRY_128 |
14093 DMA_RWCTRL_WRITE_BNDRY_128);
14094 break;
14095 }
14096 /* fallthrough */
14097 case 256:
14098 val |= (DMA_RWCTRL_READ_BNDRY_256 |
14099 DMA_RWCTRL_WRITE_BNDRY_256);
14100 break;
14101 case 512:
14102 val |= (DMA_RWCTRL_READ_BNDRY_512 |
14103 DMA_RWCTRL_WRITE_BNDRY_512);
14104 break;
14105 case 1024:
14106 default:
14107 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
14108 DMA_RWCTRL_WRITE_BNDRY_1024);
14109 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014110 }
David S. Miller59e6b432005-05-18 22:50:10 -070014111 }
14112
14113out:
14114 return val;
14115}
14116
Linus Torvalds1da177e2005-04-16 15:20:36 -070014117static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
14118{
14119 struct tg3_internal_buffer_desc test_desc;
14120 u32 sram_dma_descs;
14121 int i, ret;
14122
14123 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
14124
14125 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
14126 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
14127 tw32(RDMAC_STATUS, 0);
14128 tw32(WDMAC_STATUS, 0);
14129
14130 tw32(BUFMGR_MODE, 0);
14131 tw32(FTQ_RESET, 0);
14132
14133 test_desc.addr_hi = ((u64) buf_dma) >> 32;
14134 test_desc.addr_lo = buf_dma & 0xffffffff;
14135 test_desc.nic_mbuf = 0x00002100;
14136 test_desc.len = size;
14137
14138 /*
14139 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
14140 * the *second* time the tg3 driver was getting loaded after an
14141 * initial scan.
14142 *
14143 * Broadcom tells me:
14144 * ...the DMA engine is connected to the GRC block and a DMA
14145 * reset may affect the GRC block in some unpredictable way...
14146 * The behavior of resets to individual blocks has not been tested.
14147 *
14148 * Broadcom noted the GRC reset will also reset all sub-components.
14149 */
14150 if (to_device) {
14151 test_desc.cqid_sqid = (13 << 8) | 2;
14152
14153 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
14154 udelay(40);
14155 } else {
14156 test_desc.cqid_sqid = (16 << 8) | 7;
14157
14158 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
14159 udelay(40);
14160 }
14161 test_desc.flags = 0x00000005;
14162
14163 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
14164 u32 val;
14165
14166 val = *(((u32 *)&test_desc) + i);
14167 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
14168 sram_dma_descs + (i * sizeof(u32)));
14169 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
14170 }
14171 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
14172
Matt Carlson859a5882010-04-05 10:19:28 +000014173 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014174 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a5882010-04-05 10:19:28 +000014175 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070014176 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014177
14178 ret = -ENODEV;
14179 for (i = 0; i < 40; i++) {
14180 u32 val;
14181
14182 if (to_device)
14183 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
14184 else
14185 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
14186 if ((val & 0xffff) == sram_dma_descs) {
14187 ret = 0;
14188 break;
14189 }
14190
14191 udelay(100);
14192 }
14193
14194 return ret;
14195}
14196
David S. Millerded73402005-05-23 13:59:47 -070014197#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070014198
14199static int __devinit tg3_test_dma(struct tg3 *tp)
14200{
14201 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070014202 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014203 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014204
14205 buf = pci_alloc_consistent(tp->pdev, TEST_BUFFER_SIZE, &buf_dma);
14206 if (!buf) {
14207 ret = -ENOMEM;
14208 goto out_nofree;
14209 }
14210
14211 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
14212 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
14213
David S. Miller59e6b432005-05-18 22:50:10 -070014214 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014215
Matt Carlsonc885e822010-08-02 11:25:57 +000014216 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014217 goto out;
14218
Linus Torvalds1da177e2005-04-16 15:20:36 -070014219 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
14220 /* DMA read watermark not used on PCIE */
14221 tp->dma_rwctrl |= 0x00180000;
14222 } else if (!(tp->tg3_flags & TG3_FLAG_PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070014223 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
14224 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014225 tp->dma_rwctrl |= 0x003f0000;
14226 else
14227 tp->dma_rwctrl |= 0x003f000f;
14228 } else {
14229 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14230 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
14231 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080014232 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014233
Michael Chan4a29cc22006-03-19 13:21:12 -080014234 /* If the 5704 is behind the EPB bridge, we can
14235 * do the less restrictive ONE_DMA workaround for
14236 * better performance.
14237 */
14238 if ((tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG) &&
14239 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14240 tp->dma_rwctrl |= 0x8000;
14241 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014242 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
14243
Michael Chan49afdeb2007-02-13 12:17:03 -080014244 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
14245 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070014246 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080014247 tp->dma_rwctrl |=
14248 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
14249 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
14250 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070014251 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
14252 /* 5780 always in PCIX mode */
14253 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070014254 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
14255 /* 5714 always in PCIX mode */
14256 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014257 } else {
14258 tp->dma_rwctrl |= 0x001b000f;
14259 }
14260 }
14261
14262 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14263 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14264 tp->dma_rwctrl &= 0xfffffff0;
14265
14266 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14267 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
14268 /* Remove this if it causes problems for some boards. */
14269 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
14270
14271 /* On 5700/5701 chips, we need to set this bit.
14272 * Otherwise the chip will issue cacheline transactions
14273 * to streamable DMA memory with not all the byte
14274 * enables turned on. This is an error on several
14275 * RISC PCI controllers, in particular sparc64.
14276 *
14277 * On 5703/5704 chips, this bit has been reassigned
14278 * a different meaning. In particular, it is used
14279 * on those chips to enable a PCI-X workaround.
14280 */
14281 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
14282 }
14283
14284 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14285
14286#if 0
14287 /* Unneeded, already done by tg3_get_invariants. */
14288 tg3_switch_clocks(tp);
14289#endif
14290
Linus Torvalds1da177e2005-04-16 15:20:36 -070014291 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14292 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
14293 goto out;
14294
David S. Miller59e6b432005-05-18 22:50:10 -070014295 /* It is best to perform DMA test with maximum write burst size
14296 * to expose the 5700/5701 write DMA bug.
14297 */
14298 saved_dma_rwctrl = tp->dma_rwctrl;
14299 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14300 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14301
Linus Torvalds1da177e2005-04-16 15:20:36 -070014302 while (1) {
14303 u32 *p = buf, i;
14304
14305 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
14306 p[i] = i;
14307
14308 /* Send the buffer to the chip. */
14309 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
14310 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000014311 dev_err(&tp->pdev->dev,
14312 "%s: Buffer write failed. err = %d\n",
14313 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014314 break;
14315 }
14316
14317#if 0
14318 /* validate data reached card RAM correctly. */
14319 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14320 u32 val;
14321 tg3_read_mem(tp, 0x2100 + (i*4), &val);
14322 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000014323 dev_err(&tp->pdev->dev,
14324 "%s: Buffer corrupted on device! "
14325 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014326 /* ret = -ENODEV here? */
14327 }
14328 p[i] = 0;
14329 }
14330#endif
14331 /* Now read it back. */
14332 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
14333 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000014334 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
14335 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014336 break;
14337 }
14338
14339 /* Verify it. */
14340 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14341 if (p[i] == i)
14342 continue;
14343
David S. Miller59e6b432005-05-18 22:50:10 -070014344 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14345 DMA_RWCTRL_WRITE_BNDRY_16) {
14346 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014347 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
14348 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14349 break;
14350 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000014351 dev_err(&tp->pdev->dev,
14352 "%s: Buffer corrupted on read back! "
14353 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014354 ret = -ENODEV;
14355 goto out;
14356 }
14357 }
14358
14359 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
14360 /* Success. */
14361 ret = 0;
14362 break;
14363 }
14364 }
David S. Miller59e6b432005-05-18 22:50:10 -070014365 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14366 DMA_RWCTRL_WRITE_BNDRY_16) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014367 static struct pci_device_id dma_wait_state_chipsets[] = {
14368 { PCI_DEVICE(PCI_VENDOR_ID_APPLE,
14369 PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
14370 { },
14371 };
14372
David S. Miller59e6b432005-05-18 22:50:10 -070014373 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070014374 * now look for chipsets that are known to expose the
14375 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070014376 */
Michael Chan6d1cfba2005-06-08 14:13:14 -070014377 if (pci_dev_present(dma_wait_state_chipsets)) {
14378 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14379 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a5882010-04-05 10:19:28 +000014380 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014381 /* Safe to use the calculated DMA boundary. */
14382 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a5882010-04-05 10:19:28 +000014383 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070014384
David S. Miller59e6b432005-05-18 22:50:10 -070014385 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014387
14388out:
14389 pci_free_consistent(tp->pdev, TEST_BUFFER_SIZE, buf, buf_dma);
14390out_nofree:
14391 return ret;
14392}
14393
14394static void __devinit tg3_init_link_config(struct tg3 *tp)
14395{
14396 tp->link_config.advertising =
14397 (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
14398 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
14399 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full |
14400 ADVERTISED_Autoneg | ADVERTISED_MII);
14401 tp->link_config.speed = SPEED_INVALID;
14402 tp->link_config.duplex = DUPLEX_INVALID;
14403 tp->link_config.autoneg = AUTONEG_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014404 tp->link_config.active_speed = SPEED_INVALID;
14405 tp->link_config.active_duplex = DUPLEX_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014406 tp->link_config.orig_speed = SPEED_INVALID;
14407 tp->link_config.orig_duplex = DUPLEX_INVALID;
14408 tp->link_config.orig_autoneg = AUTONEG_INVALID;
14409}
14410
14411static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
14412{
Matt Carlsonc885e822010-08-02 11:25:57 +000014413 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
Matt Carlson666bc832010-01-20 16:58:03 +000014414 tp->bufmgr_config.mbuf_read_dma_low_water =
14415 DEFAULT_MB_RDMA_LOW_WATER_5705;
14416 tp->bufmgr_config.mbuf_mac_rx_low_water =
14417 DEFAULT_MB_MACRX_LOW_WATER_57765;
14418 tp->bufmgr_config.mbuf_high_water =
14419 DEFAULT_MB_HIGH_WATER_57765;
14420
14421 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14422 DEFAULT_MB_RDMA_LOW_WATER_5705;
14423 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14424 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
14425 tp->bufmgr_config.mbuf_high_water_jumbo =
14426 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
14427 } else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
Michael Chanfdfec172005-07-25 12:31:48 -070014428 tp->bufmgr_config.mbuf_read_dma_low_water =
14429 DEFAULT_MB_RDMA_LOW_WATER_5705;
14430 tp->bufmgr_config.mbuf_mac_rx_low_water =
14431 DEFAULT_MB_MACRX_LOW_WATER_5705;
14432 tp->bufmgr_config.mbuf_high_water =
14433 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070014434 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
14435 tp->bufmgr_config.mbuf_mac_rx_low_water =
14436 DEFAULT_MB_MACRX_LOW_WATER_5906;
14437 tp->bufmgr_config.mbuf_high_water =
14438 DEFAULT_MB_HIGH_WATER_5906;
14439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014440
Michael Chanfdfec172005-07-25 12:31:48 -070014441 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14442 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
14443 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14444 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
14445 tp->bufmgr_config.mbuf_high_water_jumbo =
14446 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
14447 } else {
14448 tp->bufmgr_config.mbuf_read_dma_low_water =
14449 DEFAULT_MB_RDMA_LOW_WATER;
14450 tp->bufmgr_config.mbuf_mac_rx_low_water =
14451 DEFAULT_MB_MACRX_LOW_WATER;
14452 tp->bufmgr_config.mbuf_high_water =
14453 DEFAULT_MB_HIGH_WATER;
14454
14455 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14456 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
14457 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14458 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
14459 tp->bufmgr_config.mbuf_high_water_jumbo =
14460 DEFAULT_MB_HIGH_WATER_JUMBO;
14461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014462
14463 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
14464 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
14465}
14466
14467static char * __devinit tg3_phy_string(struct tg3 *tp)
14468{
Matt Carlson79eb6902010-02-17 15:17:03 +000014469 switch (tp->phy_id & TG3_PHY_ID_MASK) {
14470 case TG3_PHY_ID_BCM5400: return "5400";
14471 case TG3_PHY_ID_BCM5401: return "5401";
14472 case TG3_PHY_ID_BCM5411: return "5411";
14473 case TG3_PHY_ID_BCM5701: return "5701";
14474 case TG3_PHY_ID_BCM5703: return "5703";
14475 case TG3_PHY_ID_BCM5704: return "5704";
14476 case TG3_PHY_ID_BCM5705: return "5705";
14477 case TG3_PHY_ID_BCM5750: return "5750";
14478 case TG3_PHY_ID_BCM5752: return "5752";
14479 case TG3_PHY_ID_BCM5714: return "5714";
14480 case TG3_PHY_ID_BCM5780: return "5780";
14481 case TG3_PHY_ID_BCM5755: return "5755";
14482 case TG3_PHY_ID_BCM5787: return "5787";
14483 case TG3_PHY_ID_BCM5784: return "5784";
14484 case TG3_PHY_ID_BCM5756: return "5722/5756";
14485 case TG3_PHY_ID_BCM5906: return "5906";
14486 case TG3_PHY_ID_BCM5761: return "5761";
14487 case TG3_PHY_ID_BCM5718C: return "5718C";
14488 case TG3_PHY_ID_BCM5718S: return "5718S";
14489 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000014490 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson79eb6902010-02-17 15:17:03 +000014491 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070014492 case 0: return "serdes";
14493 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070014494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014495}
14496
Michael Chanf9804dd2005-09-27 12:13:10 -070014497static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
14498{
14499 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
14500 strcpy(str, "PCI Express");
14501 return str;
14502 } else if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
14503 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
14504
14505 strcpy(str, "PCIX:");
14506
14507 if ((clock_ctrl == 7) ||
14508 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
14509 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
14510 strcat(str, "133MHz");
14511 else if (clock_ctrl == 0)
14512 strcat(str, "33MHz");
14513 else if (clock_ctrl == 2)
14514 strcat(str, "50MHz");
14515 else if (clock_ctrl == 4)
14516 strcat(str, "66MHz");
14517 else if (clock_ctrl == 6)
14518 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070014519 } else {
14520 strcpy(str, "PCI:");
14521 if (tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED)
14522 strcat(str, "66MHz");
14523 else
14524 strcat(str, "33MHz");
14525 }
14526 if (tp->tg3_flags & TG3_FLAG_PCI_32BIT)
14527 strcat(str, ":32-bit");
14528 else
14529 strcat(str, ":64-bit");
14530 return str;
14531}
14532
Michael Chan8c2dc7e2005-12-19 16:26:02 -080014533static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014534{
14535 struct pci_dev *peer;
14536 unsigned int func, devnr = tp->pdev->devfn & ~7;
14537
14538 for (func = 0; func < 8; func++) {
14539 peer = pci_get_slot(tp->pdev->bus, devnr | func);
14540 if (peer && peer != tp->pdev)
14541 break;
14542 pci_dev_put(peer);
14543 }
Michael Chan16fe9d72005-12-13 21:09:54 -080014544 /* 5704 can be configured in single-port mode, set peer to
14545 * tp->pdev in that case.
14546 */
14547 if (!peer) {
14548 peer = tp->pdev;
14549 return peer;
14550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014551
14552 /*
14553 * We don't need to keep the refcount elevated; there's no way
14554 * to remove one half of this device without removing the other
14555 */
14556 pci_dev_put(peer);
14557
14558 return peer;
14559}
14560
David S. Miller15f98502005-05-18 22:49:26 -070014561static void __devinit tg3_init_coal(struct tg3 *tp)
14562{
14563 struct ethtool_coalesce *ec = &tp->coal;
14564
14565 memset(ec, 0, sizeof(*ec));
14566 ec->cmd = ETHTOOL_GCOALESCE;
14567 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
14568 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
14569 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
14570 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
14571 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
14572 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
14573 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
14574 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
14575 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
14576
14577 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
14578 HOSTCC_MODE_CLRTICK_TXBD)) {
14579 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
14580 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
14581 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
14582 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
14583 }
Michael Chand244c892005-07-05 14:42:33 -070014584
14585 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
14586 ec->rx_coalesce_usecs_irq = 0;
14587 ec->tx_coalesce_usecs_irq = 0;
14588 ec->stats_block_coalesce_usecs = 0;
14589 }
David S. Miller15f98502005-05-18 22:49:26 -070014590}
14591
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080014592static const struct net_device_ops tg3_netdev_ops = {
14593 .ndo_open = tg3_open,
14594 .ndo_stop = tg3_close,
Stephen Hemminger00829822008-11-20 20:14:53 -080014595 .ndo_start_xmit = tg3_start_xmit,
Eric Dumazet511d2222010-07-07 20:44:24 +000014596 .ndo_get_stats64 = tg3_get_stats64,
Stephen Hemminger00829822008-11-20 20:14:53 -080014597 .ndo_validate_addr = eth_validate_addr,
14598 .ndo_set_multicast_list = tg3_set_rx_mode,
14599 .ndo_set_mac_address = tg3_set_mac_addr,
14600 .ndo_do_ioctl = tg3_ioctl,
14601 .ndo_tx_timeout = tg3_tx_timeout,
14602 .ndo_change_mtu = tg3_change_mtu,
14603#if TG3_VLAN_TAG_USED
14604 .ndo_vlan_rx_register = tg3_vlan_rx_register,
14605#endif
14606#ifdef CONFIG_NET_POLL_CONTROLLER
14607 .ndo_poll_controller = tg3_poll_controller,
14608#endif
14609};
14610
14611static const struct net_device_ops tg3_netdev_ops_dma_bug = {
14612 .ndo_open = tg3_open,
14613 .ndo_stop = tg3_close,
14614 .ndo_start_xmit = tg3_start_xmit_dma_bug,
Eric Dumazet511d2222010-07-07 20:44:24 +000014615 .ndo_get_stats64 = tg3_get_stats64,
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080014616 .ndo_validate_addr = eth_validate_addr,
14617 .ndo_set_multicast_list = tg3_set_rx_mode,
14618 .ndo_set_mac_address = tg3_set_mac_addr,
14619 .ndo_do_ioctl = tg3_ioctl,
14620 .ndo_tx_timeout = tg3_tx_timeout,
14621 .ndo_change_mtu = tg3_change_mtu,
14622#if TG3_VLAN_TAG_USED
14623 .ndo_vlan_rx_register = tg3_vlan_rx_register,
14624#endif
14625#ifdef CONFIG_NET_POLL_CONTROLLER
14626 .ndo_poll_controller = tg3_poll_controller,
14627#endif
14628};
14629
Linus Torvalds1da177e2005-04-16 15:20:36 -070014630static int __devinit tg3_init_one(struct pci_dev *pdev,
14631 const struct pci_device_id *ent)
14632{
Linus Torvalds1da177e2005-04-16 15:20:36 -070014633 struct net_device *dev;
14634 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000014635 int i, err, pm_cap;
14636 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070014637 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080014638 u64 dma_mask, persist_dma_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014639
Joe Perches05dbe002010-02-17 19:44:19 +000014640 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014641
14642 err = pci_enable_device(pdev);
14643 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014644 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014645 return err;
14646 }
14647
Linus Torvalds1da177e2005-04-16 15:20:36 -070014648 err = pci_request_regions(pdev, DRV_MODULE_NAME);
14649 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014650 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014651 goto err_out_disable_pdev;
14652 }
14653
14654 pci_set_master(pdev);
14655
14656 /* Find power-management capability. */
14657 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
14658 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000014659 dev_err(&pdev->dev,
14660 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014661 err = -EIO;
14662 goto err_out_free_res;
14663 }
14664
Matt Carlsonfe5f5782009-09-01 13:09:39 +000014665 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014666 if (!dev) {
Matt Carlson2445e462010-04-05 10:19:21 +000014667 dev_err(&pdev->dev, "Etherdev alloc failed, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014668 err = -ENOMEM;
14669 goto err_out_free_res;
14670 }
14671
Linus Torvalds1da177e2005-04-16 15:20:36 -070014672 SET_NETDEV_DEV(dev, &pdev->dev);
14673
Linus Torvalds1da177e2005-04-16 15:20:36 -070014674#if TG3_VLAN_TAG_USED
14675 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014676#endif
14677
14678 tp = netdev_priv(dev);
14679 tp->pdev = pdev;
14680 tp->dev = dev;
14681 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014682 tp->rx_mode = TG3_DEF_RX_MODE;
14683 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070014684
Linus Torvalds1da177e2005-04-16 15:20:36 -070014685 if (tg3_debug > 0)
14686 tp->msg_enable = tg3_debug;
14687 else
14688 tp->msg_enable = TG3_DEF_MSG_ENABLE;
14689
14690 /* The word/byte swap controls here control register access byte
14691 * swapping. DMA data byte swapping is controlled in the GRC_MODE
14692 * setting below.
14693 */
14694 tp->misc_host_ctrl =
14695 MISC_HOST_CTRL_MASK_PCI_INT |
14696 MISC_HOST_CTRL_WORD_SWAP |
14697 MISC_HOST_CTRL_INDIR_ACCESS |
14698 MISC_HOST_CTRL_PCISTATE_RW;
14699
14700 /* The NONFRM (non-frame) byte/word swap controls take effect
14701 * on descriptor entries, anything which isn't packet data.
14702 *
14703 * The StrongARM chips on the board (one for tx, one for rx)
14704 * are running in big-endian mode.
14705 */
14706 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
14707 GRC_MODE_WSWAP_NONFRM_DATA);
14708#ifdef __BIG_ENDIAN
14709 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
14710#endif
14711 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014712 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000014713 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014714
Matt Carlsond5fe4882008-11-21 17:20:32 -080014715 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010014716 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014717 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014718 err = -ENOMEM;
14719 goto err_out_free_dev;
14720 }
14721
14722 tg3_init_link_config(tp);
14723
Linus Torvalds1da177e2005-04-16 15:20:36 -070014724 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
14725 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014726
Linus Torvalds1da177e2005-04-16 15:20:36 -070014727 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014728 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014729 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014730
14731 err = tg3_get_invariants(tp);
14732 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014733 dev_err(&pdev->dev,
14734 "Problem fetching invariants of chip, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014735 goto err_out_iounmap;
14736 }
14737
Matt Carlson615774f2009-11-13 13:03:39 +000014738 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) &&
Matt Carlson2e9f7a72010-09-15 08:59:56 +000014739 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
Matt Carlsona50d0792010-06-05 17:24:37 +000014740 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5719)
Stephen Hemminger00829822008-11-20 20:14:53 -080014741 dev->netdev_ops = &tg3_netdev_ops;
14742 else
14743 dev->netdev_ops = &tg3_netdev_ops_dma_bug;
14744
14745
Michael Chan4a29cc22006-03-19 13:21:12 -080014746 /* The EPB bridge inside 5714, 5715, and 5780 and any
14747 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080014748 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
14749 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
14750 * do DMA address check in tg3_start_xmit().
14751 */
Michael Chan4a29cc22006-03-19 13:21:12 -080014752 if (tp->tg3_flags2 & TG3_FLG2_IS_5788)
Yang Hongyang284901a2009-04-06 19:01:15 -070014753 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Michael Chan4a29cc22006-03-19 13:21:12 -080014754 else if (tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070014755 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080014756#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070014757 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080014758#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080014759 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070014760 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080014761
14762 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070014763 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080014764 err = pci_set_dma_mask(pdev, dma_mask);
14765 if (!err) {
14766 dev->features |= NETIF_F_HIGHDMA;
14767 err = pci_set_consistent_dma_mask(pdev,
14768 persist_dma_mask);
14769 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014770 dev_err(&pdev->dev, "Unable to obtain 64 bit "
14771 "DMA for consistent allocations\n");
Michael Chan72f2afb2006-03-06 19:28:35 -080014772 goto err_out_iounmap;
14773 }
14774 }
14775 }
Yang Hongyang284901a2009-04-06 19:01:15 -070014776 if (err || dma_mask == DMA_BIT_MASK(32)) {
14777 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080014778 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014779 dev_err(&pdev->dev,
14780 "No usable DMA configuration, aborting\n");
Michael Chan72f2afb2006-03-06 19:28:35 -080014781 goto err_out_iounmap;
14782 }
14783 }
14784
Michael Chanfdfec172005-07-25 12:31:48 -070014785 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014786
Matt Carlson507399f2009-11-13 13:03:37 +000014787 /* Selectively allow TSO based on operating conditions */
14788 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO) ||
14789 (tp->fw_needed && !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)))
14790 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
14791 else {
14792 tp->tg3_flags2 &= ~(TG3_FLG2_TSO_CAPABLE | TG3_FLG2_TSO_BUG);
14793 tp->fw_needed = NULL;
14794 }
14795
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014796 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
Matt Carlson9e9fd122009-01-19 16:57:45 -080014797 tp->fw_needed = FIRMWARE_TG3;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014798
Michael Chan4e3a7aa2006-03-20 17:47:44 -080014799 /* TSO is on by default on chips that support hardware TSO.
14800 * Firmware TSO on older chips gives lower performance, so it
14801 * is off by default, but can be enabled using ethtool.
14802 */
Matt Carlsone849cdc2009-11-13 13:03:38 +000014803 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO) &&
Eric Dumazet7fe876a2010-07-08 06:14:55 +000014804 (dev->features & NETIF_F_IP_CSUM)) {
Matt Carlsone849cdc2009-11-13 13:03:38 +000014805 dev->features |= NETIF_F_TSO;
Eric Dumazet7fe876a2010-07-08 06:14:55 +000014806 vlan_features_add(dev, NETIF_F_TSO);
14807 }
Matt Carlsone849cdc2009-11-13 13:03:38 +000014808 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_2) ||
14809 (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3)) {
Eric Dumazet7fe876a2010-07-08 06:14:55 +000014810 if (dev->features & NETIF_F_IPV6_CSUM) {
Michael Chanb0026622006-07-03 19:42:14 -070014811 dev->features |= NETIF_F_TSO6;
Eric Dumazet7fe876a2010-07-08 06:14:55 +000014812 vlan_features_add(dev, NETIF_F_TSO6);
14813 }
Matt Carlsone849cdc2009-11-13 13:03:38 +000014814 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
14815 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070014816 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
14817 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014818 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Eric Dumazet7fe876a2010-07-08 06:14:55 +000014819 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070014820 dev->features |= NETIF_F_TSO_ECN;
Eric Dumazet7fe876a2010-07-08 06:14:55 +000014821 vlan_features_add(dev, NETIF_F_TSO_ECN);
14822 }
Michael Chanb0026622006-07-03 19:42:14 -070014823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014824
Linus Torvalds1da177e2005-04-16 15:20:36 -070014825 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
14826 !(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) &&
14827 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
14828 tp->tg3_flags2 |= TG3_FLG2_MAX_RXPEND_64;
14829 tp->rx_pending = 63;
14830 }
14831
Linus Torvalds1da177e2005-04-16 15:20:36 -070014832 err = tg3_get_device_address(tp);
14833 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014834 dev_err(&pdev->dev,
14835 "Could not obtain valid ethernet address, aborting\n");
Matt Carlson026a6c22009-12-03 08:36:24 +000014836 goto err_out_iounmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014837 }
14838
Matt Carlson0d3031d2007-10-10 18:02:43 -070014839 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
Matt Carlson63532392008-11-03 16:49:57 -080014840 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
Al Viro79ea13c2008-01-24 02:06:46 -080014841 if (!tp->aperegs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014842 dev_err(&pdev->dev,
14843 "Cannot map APE registers, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070014844 err = -ENOMEM;
Matt Carlson026a6c22009-12-03 08:36:24 +000014845 goto err_out_iounmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070014846 }
14847
14848 tg3_ape_lock_init(tp);
Matt Carlson7fd76442009-02-25 14:27:20 +000014849
14850 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF)
14851 tg3_read_dash_ver(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070014852 }
14853
Matt Carlsonc88864d2007-11-12 21:07:01 -080014854 /*
14855 * Reset chip in case UNDI or EFI driver did not shutdown
14856 * DMA self test will enable WDMAC and we'll see (spurious)
14857 * pending DMA on the PCI bus at that point.
14858 */
14859 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
14860 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
14861 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
14862 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
14863 }
14864
14865 err = tg3_test_dma(tp);
14866 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014867 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080014868 goto err_out_apeunmap;
14869 }
14870
Matt Carlsonc88864d2007-11-12 21:07:01 -080014871 /* flow control autonegotiation is default behavior */
14872 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
Steve Glendinninge18ce342008-12-16 02:00:00 -080014873 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlsonc88864d2007-11-12 21:07:01 -080014874
Matt Carlson78f90dc2009-11-13 13:03:42 +000014875 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
14876 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
14877 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000014878 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000014879 struct tg3_napi *tnapi = &tp->napi[i];
14880
14881 tnapi->tp = tp;
14882 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
14883
14884 tnapi->int_mbox = intmbx;
14885 if (i < 4)
14886 intmbx += 0x8;
14887 else
14888 intmbx += 0x4;
14889
14890 tnapi->consmbox = rcvmbx;
14891 tnapi->prodmbox = sndmbx;
14892
Matt Carlson66cfd1b2010-09-30 10:34:30 +000014893 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000014894 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000014895 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000014896 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000014897
14898 if (!(tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX))
14899 break;
14900
14901 /*
14902 * If we support MSIX, we'll be using RSS. If we're using
14903 * RSS, the first vector only handles link interrupts and the
14904 * remaining vectors handle rx and tx interrupts. Reuse the
14905 * mailbox values for the next iteration. The values we setup
14906 * above are still useful for the single vectored mode.
14907 */
14908 if (!i)
14909 continue;
14910
14911 rcvmbx += 0x8;
14912
14913 if (sndmbx & 0x4)
14914 sndmbx -= 0x4;
14915 else
14916 sndmbx += 0xc;
14917 }
14918
Matt Carlsonc88864d2007-11-12 21:07:01 -080014919 tg3_init_coal(tp);
14920
Michael Chanc49a1562006-12-17 17:07:29 -080014921 pci_set_drvdata(pdev, dev);
14922
Linus Torvalds1da177e2005-04-16 15:20:36 -070014923 err = register_netdev(dev);
14924 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014925 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070014926 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014927 }
14928
Joe Perches05dbe002010-02-17 19:44:19 +000014929 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
14930 tp->board_part_number,
14931 tp->pci_chip_rev_id,
14932 tg3_bus_string(tp, str),
14933 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014934
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014935 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000014936 struct phy_device *phydev;
14937 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000014938 netdev_info(dev,
14939 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000014940 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014941 } else {
14942 char *ethtype;
14943
14944 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
14945 ethtype = "10/100Base-TX";
14946 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
14947 ethtype = "1000Base-SX";
14948 else
14949 ethtype = "10/100/1000Base-T";
14950
Matt Carlson5129c3a2010-04-05 10:19:23 +000014951 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014952 "(WireSpeed[%d])\n", tg3_phy_string(tp), ethtype,
14953 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0);
14954 }
Matt Carlsondf59c942008-11-03 16:52:56 -080014955
Joe Perches05dbe002010-02-17 19:44:19 +000014956 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
14957 (tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) != 0,
14958 (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014959 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches05dbe002010-02-17 19:44:19 +000014960 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0,
14961 (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) != 0);
14962 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
14963 tp->dma_rwctrl,
14964 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
14965 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014966
14967 return 0;
14968
Matt Carlson0d3031d2007-10-10 18:02:43 -070014969err_out_apeunmap:
14970 if (tp->aperegs) {
14971 iounmap(tp->aperegs);
14972 tp->aperegs = NULL;
14973 }
14974
Linus Torvalds1da177e2005-04-16 15:20:36 -070014975err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070014976 if (tp->regs) {
14977 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070014978 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070014979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014980
14981err_out_free_dev:
14982 free_netdev(dev);
14983
14984err_out_free_res:
14985 pci_release_regions(pdev);
14986
14987err_out_disable_pdev:
14988 pci_disable_device(pdev);
14989 pci_set_drvdata(pdev, NULL);
14990 return err;
14991}
14992
14993static void __devexit tg3_remove_one(struct pci_dev *pdev)
14994{
14995 struct net_device *dev = pci_get_drvdata(pdev);
14996
14997 if (dev) {
14998 struct tg3 *tp = netdev_priv(dev);
14999
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080015000 if (tp->fw)
15001 release_firmware(tp->fw);
15002
Michael Chan7faa0062006-02-02 17:29:28 -080015003 flush_scheduled_work();
Matt Carlson158d7ab2008-05-29 01:37:54 -070015004
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015005 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
15006 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015007 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015008 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070015009
Linus Torvalds1da177e2005-04-16 15:20:36 -070015010 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015011 if (tp->aperegs) {
15012 iounmap(tp->aperegs);
15013 tp->aperegs = NULL;
15014 }
Michael Chan68929142005-08-09 20:17:14 -070015015 if (tp->regs) {
15016 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015017 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015019 free_netdev(dev);
15020 pci_release_regions(pdev);
15021 pci_disable_device(pdev);
15022 pci_set_drvdata(pdev, NULL);
15023 }
15024}
15025
15026static int tg3_suspend(struct pci_dev *pdev, pm_message_t state)
15027{
15028 struct net_device *dev = pci_get_drvdata(pdev);
15029 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070015030 pci_power_t target_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015031 int err;
15032
Michael Chan3e0c95f2007-08-03 20:56:54 -070015033 /* PCI register 4 needs to be saved whether netif_running() or not.
15034 * MSI address and data need to be saved if using MSI and
15035 * netif_running().
15036 */
15037 pci_save_state(pdev);
15038
Linus Torvalds1da177e2005-04-16 15:20:36 -070015039 if (!netif_running(dev))
15040 return 0;
15041
Michael Chan7faa0062006-02-02 17:29:28 -080015042 flush_scheduled_work();
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015043 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015044 tg3_netif_stop(tp);
15045
15046 del_timer_sync(&tp->timer);
15047
David S. Millerf47c11e2005-06-24 20:18:35 -070015048 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015049 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070015050 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015051
15052 netif_device_detach(dev);
15053
David S. Millerf47c11e2005-06-24 20:18:35 -070015054 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070015055 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan6a9eba12005-12-13 21:08:58 -080015056 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
David S. Millerf47c11e2005-06-24 20:18:35 -070015057 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015058
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070015059 target_state = pdev->pm_cap ? pci_target_state(pdev) : PCI_D3hot;
15060
15061 err = tg3_set_power_state(tp, target_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015062 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015063 int err2;
15064
David S. Millerf47c11e2005-06-24 20:18:35 -070015065 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015066
Michael Chan6a9eba12005-12-13 21:08:58 -080015067 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015068 err2 = tg3_restart_hw(tp, 1);
15069 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070015070 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015071
15072 tp->timer.expires = jiffies + tp->timer_offset;
15073 add_timer(&tp->timer);
15074
15075 netif_device_attach(dev);
15076 tg3_netif_start(tp);
15077
Michael Chanb9ec6c12006-07-25 16:37:27 -070015078out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015079 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015080
15081 if (!err2)
15082 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015083 }
15084
15085 return err;
15086}
15087
15088static int tg3_resume(struct pci_dev *pdev)
15089{
15090 struct net_device *dev = pci_get_drvdata(pdev);
15091 struct tg3 *tp = netdev_priv(dev);
15092 int err;
15093
Michael Chan3e0c95f2007-08-03 20:56:54 -070015094 pci_restore_state(tp->pdev);
15095
Linus Torvalds1da177e2005-04-16 15:20:36 -070015096 if (!netif_running(dev))
15097 return 0;
15098
Michael Chanbc1c7562006-03-20 17:48:03 -080015099 err = tg3_set_power_state(tp, PCI_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015100 if (err)
15101 return err;
15102
15103 netif_device_attach(dev);
15104
David S. Millerf47c11e2005-06-24 20:18:35 -070015105 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015106
Michael Chan6a9eba12005-12-13 21:08:58 -080015107 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Michael Chanb9ec6c12006-07-25 16:37:27 -070015108 err = tg3_restart_hw(tp, 1);
15109 if (err)
15110 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015111
15112 tp->timer.expires = jiffies + tp->timer_offset;
15113 add_timer(&tp->timer);
15114
Linus Torvalds1da177e2005-04-16 15:20:36 -070015115 tg3_netif_start(tp);
15116
Michael Chanb9ec6c12006-07-25 16:37:27 -070015117out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015118 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015119
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015120 if (!err)
15121 tg3_phy_start(tp);
15122
Michael Chanb9ec6c12006-07-25 16:37:27 -070015123 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015124}
15125
15126static struct pci_driver tg3_driver = {
15127 .name = DRV_MODULE_NAME,
15128 .id_table = tg3_pci_tbl,
15129 .probe = tg3_init_one,
15130 .remove = __devexit_p(tg3_remove_one),
15131 .suspend = tg3_suspend,
15132 .resume = tg3_resume
15133};
15134
15135static int __init tg3_init(void)
15136{
Jeff Garzik29917622006-08-19 17:48:59 -040015137 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015138}
15139
15140static void __exit tg3_cleanup(void)
15141{
15142 pci_unregister_driver(&tg3_driver);
15143}
15144
15145module_init(tg3_init);
15146module_exit(tg3_cleanup);