blob: 84530d9f1f4bd527d0a2d35598374e8583f4c1b1 [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 Carlsonc7ebfda2010-09-30 10:34:37 +000072#define TG3_MIN_NUM 114
Matt Carlson6867c842010-07-11 09:31:44 +000073#define DRV_MODULE_VERSION \
74 __stringify(TG3_MAJ_NUM) "." __stringify(TG3_MIN_NUM)
Matt Carlsonc7ebfda2010-09-30 10:34:37 +000075#define DRV_MODULE_RELDATE "September 30, 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 Carlsonf92d9dc12010-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 Carlsonf92d9dc12010-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 Carlsonf92d9dc12010-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 Carlsonf92d9dc12010-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 Carlsonf92d9dc12010-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 Carlsonf92d9dc12010-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 Carlsonf92d9dc12010-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 Carlsonf92d9dc12010-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 Carlsonf92d9dc12010-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 Buytenhek298cf9be2008-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 Buytenhek298cf9be2008-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 Buytenhek298cf9be2008-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 Buytenhek298cf9be2008-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 Buytenhek298cf9be2008-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 Buytenhek298cf9be2008-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 Carlsoncdd4e09d2009-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 Buytenhek298cf9be2008-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 Carlson800960682010-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 Carlson859a588792010-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 Carlson800960682010-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 Carlson800960682010-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 Carlson800960682010-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 Carlson3bda1252008-08-15 14:08:22 -07002731 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
2732 mac_mode |= tp->mac_mode &
2733 (MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN);
2734 if (mac_mode & MAC_MODE_APE_TX_EN)
2735 mac_mode |= MAC_MODE_TDE_ENABLE;
2736 }
2737
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 tw32_f(MAC_MODE, mac_mode);
2739 udelay(100);
2740
2741 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
2742 udelay(10);
2743 }
2744
2745 if (!(tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB) &&
2746 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2747 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
2748 u32 base_val;
2749
2750 base_val = tp->pci_clock_ctrl;
2751 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
2752 CLOCK_CTRL_TXCLK_DISABLE);
2753
Michael Chanb401e9e2005-12-19 16:27:04 -08002754 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
2755 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Michael Chand7b0a852007-02-13 12:17:38 -08002756 } else if ((tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
Matt Carlson795d01c2007-10-07 23:28:17 -07002757 (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
Michael Chand7b0a852007-02-13 12:17:38 -08002758 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)) {
Michael Chan4cf78e42005-07-25 12:29:19 -07002759 /* do nothing */
Michael Chan85e94ce2005-04-21 17:05:28 -07002760 } else if (!((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF))) {
2762 u32 newbits1, newbits2;
2763
2764 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2765 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2766 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
2767 CLOCK_CTRL_TXCLK_DISABLE |
2768 CLOCK_CTRL_ALTCLK);
2769 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2770 } else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
2771 newbits1 = CLOCK_CTRL_625_CORE;
2772 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
2773 } else {
2774 newbits1 = CLOCK_CTRL_ALTCLK;
2775 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2776 }
2777
Michael Chanb401e9e2005-12-19 16:27:04 -08002778 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
2779 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780
Michael Chanb401e9e2005-12-19 16:27:04 -08002781 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
2782 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783
2784 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
2785 u32 newbits3;
2786
2787 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2788 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2789 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
2790 CLOCK_CTRL_TXCLK_DISABLE |
2791 CLOCK_CTRL_44MHZ_CORE);
2792 } else {
2793 newbits3 = CLOCK_CTRL_44MHZ_CORE;
2794 }
2795
Michael Chanb401e9e2005-12-19 16:27:04 -08002796 tw32_wait_f(TG3PCI_CLOCK_CTRL,
2797 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 }
2799 }
2800
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002801 if (!(device_should_wake) &&
Matt Carlson22435842008-11-21 17:21:13 -08002802 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08002803 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08002804
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 tg3_frob_aux_power(tp);
2806
2807 /* Workaround for unstable PLL clock */
2808 if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
2809 (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
2810 u32 val = tr32(0x7d00);
2811
2812 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
2813 tw32(0x7d00, val);
Michael Chan6921d202005-12-13 21:15:53 -08002814 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08002815 int err;
2816
2817 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08002819 if (!err)
2820 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08002821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 }
2823
Michael Chanbbadf502006-04-06 21:46:34 -07002824 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
2825
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002826 if (device_should_wake)
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07002827 pci_enable_wake(tp->pdev, state, true);
2828
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 /* Finally, set the new power state. */
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07002830 pci_set_power_state(tp->pdev, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 return 0;
2833}
2834
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
2836{
2837 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
2838 case MII_TG3_AUX_STAT_10HALF:
2839 *speed = SPEED_10;
2840 *duplex = DUPLEX_HALF;
2841 break;
2842
2843 case MII_TG3_AUX_STAT_10FULL:
2844 *speed = SPEED_10;
2845 *duplex = DUPLEX_FULL;
2846 break;
2847
2848 case MII_TG3_AUX_STAT_100HALF:
2849 *speed = SPEED_100;
2850 *duplex = DUPLEX_HALF;
2851 break;
2852
2853 case MII_TG3_AUX_STAT_100FULL:
2854 *speed = SPEED_100;
2855 *duplex = DUPLEX_FULL;
2856 break;
2857
2858 case MII_TG3_AUX_STAT_1000HALF:
2859 *speed = SPEED_1000;
2860 *duplex = DUPLEX_HALF;
2861 break;
2862
2863 case MII_TG3_AUX_STAT_1000FULL:
2864 *speed = SPEED_1000;
2865 *duplex = DUPLEX_FULL;
2866 break;
2867
2868 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002869 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07002870 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
2871 SPEED_10;
2872 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
2873 DUPLEX_HALF;
2874 break;
2875 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 *speed = SPEED_INVALID;
2877 *duplex = DUPLEX_INVALID;
2878 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880}
2881
2882static void tg3_phy_copper_begin(struct tg3 *tp)
2883{
2884 u32 new_adv;
2885 int i;
2886
Matt Carlson800960682010-08-02 11:26:06 +00002887 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 /* Entering low power mode. Disable gigabit and
2889 * 100baseT advertisements.
2890 */
2891 tg3_writephy(tp, MII_TG3_CTRL, 0);
2892
2893 new_adv = (ADVERTISE_10HALF | ADVERTISE_10FULL |
2894 ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
2895 if (tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB)
2896 new_adv |= (ADVERTISE_100HALF | ADVERTISE_100FULL);
2897
2898 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2899 } else if (tp->link_config.speed == SPEED_INVALID) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002900 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 tp->link_config.advertising &=
2902 ~(ADVERTISED_1000baseT_Half |
2903 ADVERTISED_1000baseT_Full);
2904
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002905 new_adv = ADVERTISE_CSMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 if (tp->link_config.advertising & ADVERTISED_10baseT_Half)
2907 new_adv |= ADVERTISE_10HALF;
2908 if (tp->link_config.advertising & ADVERTISED_10baseT_Full)
2909 new_adv |= ADVERTISE_10FULL;
2910 if (tp->link_config.advertising & ADVERTISED_100baseT_Half)
2911 new_adv |= ADVERTISE_100HALF;
2912 if (tp->link_config.advertising & ADVERTISED_100baseT_Full)
2913 new_adv |= ADVERTISE_100FULL;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002914
2915 new_adv |= tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2916
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2918
2919 if (tp->link_config.advertising &
2920 (ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full)) {
2921 new_adv = 0;
2922 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
2923 new_adv |= MII_TG3_CTRL_ADV_1000_HALF;
2924 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
2925 new_adv |= MII_TG3_CTRL_ADV_1000_FULL;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002926 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2928 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0))
2929 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2930 MII_TG3_CTRL_ENABLE_AS_MASTER);
2931 tg3_writephy(tp, MII_TG3_CTRL, new_adv);
2932 } else {
2933 tg3_writephy(tp, MII_TG3_CTRL, 0);
2934 }
2935 } else {
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002936 new_adv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2937 new_adv |= ADVERTISE_CSMA;
2938
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 /* Asking for a specific link mode. */
2940 if (tp->link_config.speed == SPEED_1000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2942
2943 if (tp->link_config.duplex == DUPLEX_FULL)
2944 new_adv = MII_TG3_CTRL_ADV_1000_FULL;
2945 else
2946 new_adv = MII_TG3_CTRL_ADV_1000_HALF;
2947 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2948 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
2949 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2950 MII_TG3_CTRL_ENABLE_AS_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 if (tp->link_config.speed == SPEED_100) {
2953 if (tp->link_config.duplex == DUPLEX_FULL)
2954 new_adv |= ADVERTISE_100FULL;
2955 else
2956 new_adv |= ADVERTISE_100HALF;
2957 } else {
2958 if (tp->link_config.duplex == DUPLEX_FULL)
2959 new_adv |= ADVERTISE_10FULL;
2960 else
2961 new_adv |= ADVERTISE_10HALF;
2962 }
2963 tg3_writephy(tp, MII_ADVERTISE, new_adv);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002964
2965 new_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002967
2968 tg3_writephy(tp, MII_TG3_CTRL, new_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 }
2970
Matt Carlson52b02d02010-10-14 10:37:41 +00002971 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
2972 u32 val = 0;
2973
2974 tw32(TG3_CPMU_EEE_MODE,
2975 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
2976
2977 /* Enable SM_DSP clock and tx 6dB coding. */
2978 val = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
2979 MII_TG3_AUXCTL_ACTL_SMDSP_ENA |
2980 MII_TG3_AUXCTL_ACTL_TX_6DB;
2981 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
2982
2983 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2984 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) &&
2985 !tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
2986 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2,
2987 val | MII_TG3_DSP_CH34TP2_HIBW01);
2988
2989 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
2990 /* Advertise 100-BaseTX EEE ability */
2991 if (tp->link_config.advertising &
2992 (ADVERTISED_100baseT_Half |
2993 ADVERTISED_100baseT_Full))
2994 val |= TG3_CL45_D7_EEEADV_CAP_100TX;
2995 /* Advertise 1000-BaseT EEE ability */
2996 if (tp->link_config.advertising &
2997 (ADVERTISED_1000baseT_Half |
2998 ADVERTISED_1000baseT_Full))
2999 val |= TG3_CL45_D7_EEEADV_CAP_1000T;
3000 }
3001 tg3_phy_cl45_write(tp, 0x7, TG3_CL45_D7_EEEADV_CAP, val);
3002
3003 /* Turn off SM_DSP clock. */
3004 val = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
3005 MII_TG3_AUXCTL_ACTL_TX_6DB;
3006 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
3007 }
3008
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 if (tp->link_config.autoneg == AUTONEG_DISABLE &&
3010 tp->link_config.speed != SPEED_INVALID) {
3011 u32 bmcr, orig_bmcr;
3012
3013 tp->link_config.active_speed = tp->link_config.speed;
3014 tp->link_config.active_duplex = tp->link_config.duplex;
3015
3016 bmcr = 0;
3017 switch (tp->link_config.speed) {
3018 default:
3019 case SPEED_10:
3020 break;
3021
3022 case SPEED_100:
3023 bmcr |= BMCR_SPEED100;
3024 break;
3025
3026 case SPEED_1000:
3027 bmcr |= TG3_BMCR_SPEED1000;
3028 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003029 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030
3031 if (tp->link_config.duplex == DUPLEX_FULL)
3032 bmcr |= BMCR_FULLDPLX;
3033
3034 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
3035 (bmcr != orig_bmcr)) {
3036 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
3037 for (i = 0; i < 1500; i++) {
3038 u32 tmp;
3039
3040 udelay(10);
3041 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
3042 tg3_readphy(tp, MII_BMSR, &tmp))
3043 continue;
3044 if (!(tmp & BMSR_LSTATUS)) {
3045 udelay(40);
3046 break;
3047 }
3048 }
3049 tg3_writephy(tp, MII_BMCR, bmcr);
3050 udelay(40);
3051 }
3052 } else {
3053 tg3_writephy(tp, MII_BMCR,
3054 BMCR_ANENABLE | BMCR_ANRESTART);
3055 }
3056}
3057
3058static int tg3_init_5401phy_dsp(struct tg3 *tp)
3059{
3060 int err;
3061
3062 /* Turn off tap power management. */
3063 /* Set Extended packet length bit */
3064 err = tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4c20);
3065
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00003066 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
3067 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
3068 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
3069 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
3070 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071
3072 udelay(40);
3073
3074 return err;
3075}
3076
Michael Chan3600d912006-12-07 00:21:48 -08003077static int tg3_copper_is_advertising_all(struct tg3 *tp, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078{
Michael Chan3600d912006-12-07 00:21:48 -08003079 u32 adv_reg, all_mask = 0;
3080
3081 if (mask & ADVERTISED_10baseT_Half)
3082 all_mask |= ADVERTISE_10HALF;
3083 if (mask & ADVERTISED_10baseT_Full)
3084 all_mask |= ADVERTISE_10FULL;
3085 if (mask & ADVERTISED_100baseT_Half)
3086 all_mask |= ADVERTISE_100HALF;
3087 if (mask & ADVERTISED_100baseT_Full)
3088 all_mask |= ADVERTISE_100FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089
3090 if (tg3_readphy(tp, MII_ADVERTISE, &adv_reg))
3091 return 0;
3092
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 if ((adv_reg & all_mask) != all_mask)
3094 return 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003095 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 u32 tg3_ctrl;
3097
Michael Chan3600d912006-12-07 00:21:48 -08003098 all_mask = 0;
3099 if (mask & ADVERTISED_1000baseT_Half)
3100 all_mask |= ADVERTISE_1000HALF;
3101 if (mask & ADVERTISED_1000baseT_Full)
3102 all_mask |= ADVERTISE_1000FULL;
3103
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 if (tg3_readphy(tp, MII_TG3_CTRL, &tg3_ctrl))
3105 return 0;
3106
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 if ((tg3_ctrl & all_mask) != all_mask)
3108 return 0;
3109 }
3110 return 1;
3111}
3112
Matt Carlsonef167e22007-12-20 20:10:01 -08003113static int tg3_adv_1000T_flowctrl_ok(struct tg3 *tp, u32 *lcladv, u32 *rmtadv)
3114{
3115 u32 curadv, reqadv;
3116
3117 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
3118 return 1;
3119
3120 curadv = *lcladv & (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
3121 reqadv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
3122
3123 if (tp->link_config.active_duplex == DUPLEX_FULL) {
3124 if (curadv != reqadv)
3125 return 0;
3126
3127 if (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG)
3128 tg3_readphy(tp, MII_LPA, rmtadv);
3129 } else {
3130 /* Reprogram the advertisement register, even if it
3131 * does not affect the current link. If the link
3132 * gets renegotiated in the future, we can save an
3133 * additional renegotiation cycle by advertising
3134 * it correctly in the first place.
3135 */
3136 if (curadv != reqadv) {
3137 *lcladv &= ~(ADVERTISE_PAUSE_CAP |
3138 ADVERTISE_PAUSE_ASYM);
3139 tg3_writephy(tp, MII_ADVERTISE, *lcladv | reqadv);
3140 }
3141 }
3142
3143 return 1;
3144}
3145
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
3147{
3148 int current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003149 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08003150 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 u16 current_speed;
3152 u8 current_duplex;
3153 int i, err;
3154
3155 tw32(MAC_EVENT, 0);
3156
3157 tw32_f(MAC_STATUS,
3158 (MAC_STATUS_SYNC_CHANGED |
3159 MAC_STATUS_CFG_CHANGED |
3160 MAC_STATUS_MI_COMPLETION |
3161 MAC_STATUS_LNKSTATE_CHANGED));
3162 udelay(40);
3163
Matt Carlson8ef21422008-05-02 16:47:53 -07003164 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
3165 tw32_f(MAC_MI_MODE,
3166 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
3167 udelay(80);
3168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169
3170 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x02);
3171
3172 /* Some third-party PHYs need to be reset on link going
3173 * down.
3174 */
3175 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3176 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
3177 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
3178 netif_carrier_ok(tp->dev)) {
3179 tg3_readphy(tp, MII_BMSR, &bmsr);
3180 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3181 !(bmsr & BMSR_LSTATUS))
3182 force_reset = 1;
3183 }
3184 if (force_reset)
3185 tg3_phy_reset(tp);
3186
Matt Carlson79eb6902010-02-17 15:17:03 +00003187 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 tg3_readphy(tp, MII_BMSR, &bmsr);
3189 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
3190 !(tp->tg3_flags & TG3_FLAG_INIT_COMPLETE))
3191 bmsr = 0;
3192
3193 if (!(bmsr & BMSR_LSTATUS)) {
3194 err = tg3_init_5401phy_dsp(tp);
3195 if (err)
3196 return err;
3197
3198 tg3_readphy(tp, MII_BMSR, &bmsr);
3199 for (i = 0; i < 1000; i++) {
3200 udelay(10);
3201 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3202 (bmsr & BMSR_LSTATUS)) {
3203 udelay(40);
3204 break;
3205 }
3206 }
3207
Matt Carlson79eb6902010-02-17 15:17:03 +00003208 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
3209 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 !(bmsr & BMSR_LSTATUS) &&
3211 tp->link_config.active_speed == SPEED_1000) {
3212 err = tg3_phy_reset(tp);
3213 if (!err)
3214 err = tg3_init_5401phy_dsp(tp);
3215 if (err)
3216 return err;
3217 }
3218 }
3219 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3220 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
3221 /* 5701 {A0,B0} CRC bug workaround */
3222 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00003223 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
3224 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
3225 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 }
3227
3228 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003229 tg3_readphy(tp, MII_TG3_ISTAT, &val);
3230 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003232 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003234 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 tg3_writephy(tp, MII_TG3_IMASK, ~0);
3236
3237 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3238 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3239 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
3240 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3241 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
3242 else
3243 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
3244 }
3245
3246 current_link_up = 0;
3247 current_speed = SPEED_INVALID;
3248 current_duplex = DUPLEX_INVALID;
3249
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003250 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4007);
3252 tg3_readphy(tp, MII_TG3_AUX_CTRL, &val);
3253 if (!(val & (1 << 10))) {
3254 val |= (1 << 10);
3255 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
3256 goto relink;
3257 }
3258 }
3259
3260 bmsr = 0;
3261 for (i = 0; i < 100; i++) {
3262 tg3_readphy(tp, MII_BMSR, &bmsr);
3263 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3264 (bmsr & BMSR_LSTATUS))
3265 break;
3266 udelay(40);
3267 }
3268
3269 if (bmsr & BMSR_LSTATUS) {
3270 u32 aux_stat, bmcr;
3271
3272 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
3273 for (i = 0; i < 2000; i++) {
3274 udelay(10);
3275 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
3276 aux_stat)
3277 break;
3278 }
3279
3280 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
3281 &current_speed,
3282 &current_duplex);
3283
3284 bmcr = 0;
3285 for (i = 0; i < 200; i++) {
3286 tg3_readphy(tp, MII_BMCR, &bmcr);
3287 if (tg3_readphy(tp, MII_BMCR, &bmcr))
3288 continue;
3289 if (bmcr && bmcr != 0x7fff)
3290 break;
3291 udelay(10);
3292 }
3293
Matt Carlsonef167e22007-12-20 20:10:01 -08003294 lcl_adv = 0;
3295 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296
Matt Carlsonef167e22007-12-20 20:10:01 -08003297 tp->link_config.active_speed = current_speed;
3298 tp->link_config.active_duplex = current_duplex;
3299
3300 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3301 if ((bmcr & BMCR_ANENABLE) &&
3302 tg3_copper_is_advertising_all(tp,
3303 tp->link_config.advertising)) {
3304 if (tg3_adv_1000T_flowctrl_ok(tp, &lcl_adv,
3305 &rmt_adv))
3306 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 }
3308 } else {
3309 if (!(bmcr & BMCR_ANENABLE) &&
3310 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08003311 tp->link_config.duplex == current_duplex &&
3312 tp->link_config.flowctrl ==
3313 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 }
3316 }
3317
Matt Carlsonef167e22007-12-20 20:10:01 -08003318 if (current_link_up == 1 &&
3319 tp->link_config.active_duplex == DUPLEX_FULL)
3320 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 }
3322
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323relink:
Matt Carlson800960682010-08-02 11:26:06 +00003324 if (current_link_up == 0 || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 tg3_phy_copper_begin(tp);
3326
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003327 tg3_readphy(tp, MII_BMSR, &bmsr);
3328 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3329 (bmsr & BMSR_LSTATUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 current_link_up = 1;
3331 }
3332
3333 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
3334 if (current_link_up == 1) {
3335 if (tp->link_config.active_speed == SPEED_100 ||
3336 tp->link_config.active_speed == SPEED_10)
3337 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3338 else
3339 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003340 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00003341 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3342 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
3344
3345 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
3346 if (tp->link_config.active_duplex == DUPLEX_HALF)
3347 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
3348
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003350 if (current_link_up == 1 &&
3351 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003353 else
3354 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 }
3356
3357 /* ??? Without this setting Netgear GA302T PHY does not
3358 * ??? send/receive packets...
3359 */
Matt Carlson79eb6902010-02-17 15:17:03 +00003360 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
3362 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
3363 tw32_f(MAC_MI_MODE, tp->mi_mode);
3364 udelay(80);
3365 }
3366
3367 tw32_f(MAC_MODE, tp->mac_mode);
3368 udelay(40);
3369
Matt Carlson52b02d02010-10-14 10:37:41 +00003370 tg3_phy_eee_adjust(tp, current_link_up);
3371
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 if (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) {
3373 /* Polled via timer. */
3374 tw32_f(MAC_EVENT, 0);
3375 } else {
3376 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
3377 }
3378 udelay(40);
3379
3380 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
3381 current_link_up == 1 &&
3382 tp->link_config.active_speed == SPEED_1000 &&
3383 ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) ||
3384 (tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED))) {
3385 udelay(120);
3386 tw32_f(MAC_STATUS,
3387 (MAC_STATUS_SYNC_CHANGED |
3388 MAC_STATUS_CFG_CHANGED));
3389 udelay(40);
3390 tg3_write_mem(tp,
3391 NIC_SRAM_FIRMWARE_MBOX,
3392 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
3393 }
3394
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003395 /* Prevent send BD corruption. */
3396 if (tp->tg3_flags3 & TG3_FLG3_CLKREQ_BUG) {
3397 u16 oldlnkctl, newlnkctl;
3398
3399 pci_read_config_word(tp->pdev,
3400 tp->pcie_cap + PCI_EXP_LNKCTL,
3401 &oldlnkctl);
3402 if (tp->link_config.active_speed == SPEED_100 ||
3403 tp->link_config.active_speed == SPEED_10)
3404 newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
3405 else
3406 newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
3407 if (newlnkctl != oldlnkctl)
3408 pci_write_config_word(tp->pdev,
3409 tp->pcie_cap + PCI_EXP_LNKCTL,
3410 newlnkctl);
3411 }
3412
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 if (current_link_up != netif_carrier_ok(tp->dev)) {
3414 if (current_link_up)
3415 netif_carrier_on(tp->dev);
3416 else
3417 netif_carrier_off(tp->dev);
3418 tg3_link_report(tp);
3419 }
3420
3421 return 0;
3422}
3423
3424struct tg3_fiber_aneginfo {
3425 int state;
3426#define ANEG_STATE_UNKNOWN 0
3427#define ANEG_STATE_AN_ENABLE 1
3428#define ANEG_STATE_RESTART_INIT 2
3429#define ANEG_STATE_RESTART 3
3430#define ANEG_STATE_DISABLE_LINK_OK 4
3431#define ANEG_STATE_ABILITY_DETECT_INIT 5
3432#define ANEG_STATE_ABILITY_DETECT 6
3433#define ANEG_STATE_ACK_DETECT_INIT 7
3434#define ANEG_STATE_ACK_DETECT 8
3435#define ANEG_STATE_COMPLETE_ACK_INIT 9
3436#define ANEG_STATE_COMPLETE_ACK 10
3437#define ANEG_STATE_IDLE_DETECT_INIT 11
3438#define ANEG_STATE_IDLE_DETECT 12
3439#define ANEG_STATE_LINK_OK 13
3440#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
3441#define ANEG_STATE_NEXT_PAGE_WAIT 15
3442
3443 u32 flags;
3444#define MR_AN_ENABLE 0x00000001
3445#define MR_RESTART_AN 0x00000002
3446#define MR_AN_COMPLETE 0x00000004
3447#define MR_PAGE_RX 0x00000008
3448#define MR_NP_LOADED 0x00000010
3449#define MR_TOGGLE_TX 0x00000020
3450#define MR_LP_ADV_FULL_DUPLEX 0x00000040
3451#define MR_LP_ADV_HALF_DUPLEX 0x00000080
3452#define MR_LP_ADV_SYM_PAUSE 0x00000100
3453#define MR_LP_ADV_ASYM_PAUSE 0x00000200
3454#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
3455#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
3456#define MR_LP_ADV_NEXT_PAGE 0x00001000
3457#define MR_TOGGLE_RX 0x00002000
3458#define MR_NP_RX 0x00004000
3459
3460#define MR_LINK_OK 0x80000000
3461
3462 unsigned long link_time, cur_time;
3463
3464 u32 ability_match_cfg;
3465 int ability_match_count;
3466
3467 char ability_match, idle_match, ack_match;
3468
3469 u32 txconfig, rxconfig;
3470#define ANEG_CFG_NP 0x00000080
3471#define ANEG_CFG_ACK 0x00000040
3472#define ANEG_CFG_RF2 0x00000020
3473#define ANEG_CFG_RF1 0x00000010
3474#define ANEG_CFG_PS2 0x00000001
3475#define ANEG_CFG_PS1 0x00008000
3476#define ANEG_CFG_HD 0x00004000
3477#define ANEG_CFG_FD 0x00002000
3478#define ANEG_CFG_INVAL 0x00001f06
3479
3480};
3481#define ANEG_OK 0
3482#define ANEG_DONE 1
3483#define ANEG_TIMER_ENAB 2
3484#define ANEG_FAILED -1
3485
3486#define ANEG_STATE_SETTLE_TIME 10000
3487
3488static int tg3_fiber_aneg_smachine(struct tg3 *tp,
3489 struct tg3_fiber_aneginfo *ap)
3490{
Matt Carlson5be73b42007-12-20 20:09:29 -08003491 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 unsigned long delta;
3493 u32 rx_cfg_reg;
3494 int ret;
3495
3496 if (ap->state == ANEG_STATE_UNKNOWN) {
3497 ap->rxconfig = 0;
3498 ap->link_time = 0;
3499 ap->cur_time = 0;
3500 ap->ability_match_cfg = 0;
3501 ap->ability_match_count = 0;
3502 ap->ability_match = 0;
3503 ap->idle_match = 0;
3504 ap->ack_match = 0;
3505 }
3506 ap->cur_time++;
3507
3508 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
3509 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
3510
3511 if (rx_cfg_reg != ap->ability_match_cfg) {
3512 ap->ability_match_cfg = rx_cfg_reg;
3513 ap->ability_match = 0;
3514 ap->ability_match_count = 0;
3515 } else {
3516 if (++ap->ability_match_count > 1) {
3517 ap->ability_match = 1;
3518 ap->ability_match_cfg = rx_cfg_reg;
3519 }
3520 }
3521 if (rx_cfg_reg & ANEG_CFG_ACK)
3522 ap->ack_match = 1;
3523 else
3524 ap->ack_match = 0;
3525
3526 ap->idle_match = 0;
3527 } else {
3528 ap->idle_match = 1;
3529 ap->ability_match_cfg = 0;
3530 ap->ability_match_count = 0;
3531 ap->ability_match = 0;
3532 ap->ack_match = 0;
3533
3534 rx_cfg_reg = 0;
3535 }
3536
3537 ap->rxconfig = rx_cfg_reg;
3538 ret = ANEG_OK;
3539
Matt Carlson33f401a2010-04-05 10:19:27 +00003540 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 case ANEG_STATE_UNKNOWN:
3542 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
3543 ap->state = ANEG_STATE_AN_ENABLE;
3544
3545 /* fallthru */
3546 case ANEG_STATE_AN_ENABLE:
3547 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
3548 if (ap->flags & MR_AN_ENABLE) {
3549 ap->link_time = 0;
3550 ap->cur_time = 0;
3551 ap->ability_match_cfg = 0;
3552 ap->ability_match_count = 0;
3553 ap->ability_match = 0;
3554 ap->idle_match = 0;
3555 ap->ack_match = 0;
3556
3557 ap->state = ANEG_STATE_RESTART_INIT;
3558 } else {
3559 ap->state = ANEG_STATE_DISABLE_LINK_OK;
3560 }
3561 break;
3562
3563 case ANEG_STATE_RESTART_INIT:
3564 ap->link_time = ap->cur_time;
3565 ap->flags &= ~(MR_NP_LOADED);
3566 ap->txconfig = 0;
3567 tw32(MAC_TX_AUTO_NEG, 0);
3568 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3569 tw32_f(MAC_MODE, tp->mac_mode);
3570 udelay(40);
3571
3572 ret = ANEG_TIMER_ENAB;
3573 ap->state = ANEG_STATE_RESTART;
3574
3575 /* fallthru */
3576 case ANEG_STATE_RESTART:
3577 delta = ap->cur_time - ap->link_time;
Matt Carlson859a588792010-04-05 10:19:28 +00003578 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a588792010-04-05 10:19:28 +00003580 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 break;
3583
3584 case ANEG_STATE_DISABLE_LINK_OK:
3585 ret = ANEG_DONE;
3586 break;
3587
3588 case ANEG_STATE_ABILITY_DETECT_INIT:
3589 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08003590 ap->txconfig = ANEG_CFG_FD;
3591 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3592 if (flowctrl & ADVERTISE_1000XPAUSE)
3593 ap->txconfig |= ANEG_CFG_PS1;
3594 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3595 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3597 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3598 tw32_f(MAC_MODE, tp->mac_mode);
3599 udelay(40);
3600
3601 ap->state = ANEG_STATE_ABILITY_DETECT;
3602 break;
3603
3604 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a588792010-04-05 10:19:28 +00003605 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 break;
3608
3609 case ANEG_STATE_ACK_DETECT_INIT:
3610 ap->txconfig |= ANEG_CFG_ACK;
3611 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3612 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3613 tw32_f(MAC_MODE, tp->mac_mode);
3614 udelay(40);
3615
3616 ap->state = ANEG_STATE_ACK_DETECT;
3617
3618 /* fallthru */
3619 case ANEG_STATE_ACK_DETECT:
3620 if (ap->ack_match != 0) {
3621 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
3622 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
3623 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
3624 } else {
3625 ap->state = ANEG_STATE_AN_ENABLE;
3626 }
3627 } else if (ap->ability_match != 0 &&
3628 ap->rxconfig == 0) {
3629 ap->state = ANEG_STATE_AN_ENABLE;
3630 }
3631 break;
3632
3633 case ANEG_STATE_COMPLETE_ACK_INIT:
3634 if (ap->rxconfig & ANEG_CFG_INVAL) {
3635 ret = ANEG_FAILED;
3636 break;
3637 }
3638 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
3639 MR_LP_ADV_HALF_DUPLEX |
3640 MR_LP_ADV_SYM_PAUSE |
3641 MR_LP_ADV_ASYM_PAUSE |
3642 MR_LP_ADV_REMOTE_FAULT1 |
3643 MR_LP_ADV_REMOTE_FAULT2 |
3644 MR_LP_ADV_NEXT_PAGE |
3645 MR_TOGGLE_RX |
3646 MR_NP_RX);
3647 if (ap->rxconfig & ANEG_CFG_FD)
3648 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
3649 if (ap->rxconfig & ANEG_CFG_HD)
3650 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
3651 if (ap->rxconfig & ANEG_CFG_PS1)
3652 ap->flags |= MR_LP_ADV_SYM_PAUSE;
3653 if (ap->rxconfig & ANEG_CFG_PS2)
3654 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
3655 if (ap->rxconfig & ANEG_CFG_RF1)
3656 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
3657 if (ap->rxconfig & ANEG_CFG_RF2)
3658 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
3659 if (ap->rxconfig & ANEG_CFG_NP)
3660 ap->flags |= MR_LP_ADV_NEXT_PAGE;
3661
3662 ap->link_time = ap->cur_time;
3663
3664 ap->flags ^= (MR_TOGGLE_TX);
3665 if (ap->rxconfig & 0x0008)
3666 ap->flags |= MR_TOGGLE_RX;
3667 if (ap->rxconfig & ANEG_CFG_NP)
3668 ap->flags |= MR_NP_RX;
3669 ap->flags |= MR_PAGE_RX;
3670
3671 ap->state = ANEG_STATE_COMPLETE_ACK;
3672 ret = ANEG_TIMER_ENAB;
3673 break;
3674
3675 case ANEG_STATE_COMPLETE_ACK:
3676 if (ap->ability_match != 0 &&
3677 ap->rxconfig == 0) {
3678 ap->state = ANEG_STATE_AN_ENABLE;
3679 break;
3680 }
3681 delta = ap->cur_time - ap->link_time;
3682 if (delta > ANEG_STATE_SETTLE_TIME) {
3683 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
3684 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3685 } else {
3686 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
3687 !(ap->flags & MR_NP_RX)) {
3688 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3689 } else {
3690 ret = ANEG_FAILED;
3691 }
3692 }
3693 }
3694 break;
3695
3696 case ANEG_STATE_IDLE_DETECT_INIT:
3697 ap->link_time = ap->cur_time;
3698 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3699 tw32_f(MAC_MODE, tp->mac_mode);
3700 udelay(40);
3701
3702 ap->state = ANEG_STATE_IDLE_DETECT;
3703 ret = ANEG_TIMER_ENAB;
3704 break;
3705
3706 case ANEG_STATE_IDLE_DETECT:
3707 if (ap->ability_match != 0 &&
3708 ap->rxconfig == 0) {
3709 ap->state = ANEG_STATE_AN_ENABLE;
3710 break;
3711 }
3712 delta = ap->cur_time - ap->link_time;
3713 if (delta > ANEG_STATE_SETTLE_TIME) {
3714 /* XXX another gem from the Broadcom driver :( */
3715 ap->state = ANEG_STATE_LINK_OK;
3716 }
3717 break;
3718
3719 case ANEG_STATE_LINK_OK:
3720 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
3721 ret = ANEG_DONE;
3722 break;
3723
3724 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
3725 /* ??? unimplemented */
3726 break;
3727
3728 case ANEG_STATE_NEXT_PAGE_WAIT:
3729 /* ??? unimplemented */
3730 break;
3731
3732 default:
3733 ret = ANEG_FAILED;
3734 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736
3737 return ret;
3738}
3739
Matt Carlson5be73b42007-12-20 20:09:29 -08003740static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741{
3742 int res = 0;
3743 struct tg3_fiber_aneginfo aninfo;
3744 int status = ANEG_FAILED;
3745 unsigned int tick;
3746 u32 tmp;
3747
3748 tw32_f(MAC_TX_AUTO_NEG, 0);
3749
3750 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
3751 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
3752 udelay(40);
3753
3754 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
3755 udelay(40);
3756
3757 memset(&aninfo, 0, sizeof(aninfo));
3758 aninfo.flags |= MR_AN_ENABLE;
3759 aninfo.state = ANEG_STATE_UNKNOWN;
3760 aninfo.cur_time = 0;
3761 tick = 0;
3762 while (++tick < 195000) {
3763 status = tg3_fiber_aneg_smachine(tp, &aninfo);
3764 if (status == ANEG_DONE || status == ANEG_FAILED)
3765 break;
3766
3767 udelay(1);
3768 }
3769
3770 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3771 tw32_f(MAC_MODE, tp->mac_mode);
3772 udelay(40);
3773
Matt Carlson5be73b42007-12-20 20:09:29 -08003774 *txflags = aninfo.txconfig;
3775 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776
3777 if (status == ANEG_DONE &&
3778 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
3779 MR_LP_ADV_FULL_DUPLEX)))
3780 res = 1;
3781
3782 return res;
3783}
3784
3785static void tg3_init_bcm8002(struct tg3 *tp)
3786{
3787 u32 mac_status = tr32(MAC_STATUS);
3788 int i;
3789
3790 /* Reset when initting first time or we have a link. */
3791 if ((tp->tg3_flags & TG3_FLAG_INIT_COMPLETE) &&
3792 !(mac_status & MAC_STATUS_PCS_SYNCED))
3793 return;
3794
3795 /* Set PLL lock range. */
3796 tg3_writephy(tp, 0x16, 0x8007);
3797
3798 /* SW reset */
3799 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
3800
3801 /* Wait for reset to complete. */
3802 /* XXX schedule_timeout() ... */
3803 for (i = 0; i < 500; i++)
3804 udelay(10);
3805
3806 /* Config mode; select PMA/Ch 1 regs. */
3807 tg3_writephy(tp, 0x10, 0x8411);
3808
3809 /* Enable auto-lock and comdet, select txclk for tx. */
3810 tg3_writephy(tp, 0x11, 0x0a10);
3811
3812 tg3_writephy(tp, 0x18, 0x00a0);
3813 tg3_writephy(tp, 0x16, 0x41ff);
3814
3815 /* Assert and deassert POR. */
3816 tg3_writephy(tp, 0x13, 0x0400);
3817 udelay(40);
3818 tg3_writephy(tp, 0x13, 0x0000);
3819
3820 tg3_writephy(tp, 0x11, 0x0a50);
3821 udelay(40);
3822 tg3_writephy(tp, 0x11, 0x0a10);
3823
3824 /* Wait for signal to stabilize */
3825 /* XXX schedule_timeout() ... */
3826 for (i = 0; i < 15000; i++)
3827 udelay(10);
3828
3829 /* Deselect the channel register so we can read the PHYID
3830 * later.
3831 */
3832 tg3_writephy(tp, 0x10, 0x8011);
3833}
3834
3835static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
3836{
Matt Carlson82cd3d12007-12-20 20:09:00 -08003837 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 u32 sg_dig_ctrl, sg_dig_status;
3839 u32 serdes_cfg, expected_sg_dig_ctrl;
3840 int workaround, port_a;
3841 int current_link_up;
3842
3843 serdes_cfg = 0;
3844 expected_sg_dig_ctrl = 0;
3845 workaround = 0;
3846 port_a = 1;
3847 current_link_up = 0;
3848
3849 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
3850 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
3851 workaround = 1;
3852 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
3853 port_a = 0;
3854
3855 /* preserve bits 0-11,13,14 for signal pre-emphasis */
3856 /* preserve bits 20-23 for voltage regulator */
3857 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
3858 }
3859
3860 sg_dig_ctrl = tr32(SG_DIG_CTRL);
3861
3862 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003863 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864 if (workaround) {
3865 u32 val = serdes_cfg;
3866
3867 if (port_a)
3868 val |= 0xc010000;
3869 else
3870 val |= 0x4010000;
3871 tw32_f(MAC_SERDES_CFG, val);
3872 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003873
3874 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875 }
3876 if (mac_status & MAC_STATUS_PCS_SYNCED) {
3877 tg3_setup_flow_control(tp, 0, 0);
3878 current_link_up = 1;
3879 }
3880 goto out;
3881 }
3882
3883 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003884 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885
Matt Carlson82cd3d12007-12-20 20:09:00 -08003886 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3887 if (flowctrl & ADVERTISE_1000XPAUSE)
3888 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
3889 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3890 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891
3892 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003893 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07003894 tp->serdes_counter &&
3895 ((mac_status & (MAC_STATUS_PCS_SYNCED |
3896 MAC_STATUS_RCVD_CFG)) ==
3897 MAC_STATUS_PCS_SYNCED)) {
3898 tp->serdes_counter--;
3899 current_link_up = 1;
3900 goto out;
3901 }
3902restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903 if (workaround)
3904 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003905 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906 udelay(5);
3907 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
3908
Michael Chan3d3ebe72006-09-27 15:59:15 -07003909 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003910 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
3912 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003913 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 mac_status = tr32(MAC_STATUS);
3915
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003916 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08003918 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919
Matt Carlson82cd3d12007-12-20 20:09:00 -08003920 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
3921 local_adv |= ADVERTISE_1000XPAUSE;
3922 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
3923 local_adv |= ADVERTISE_1000XPSE_ASYM;
3924
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003925 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003926 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003927 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003928 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929
3930 tg3_setup_flow_control(tp, local_adv, remote_adv);
3931 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003932 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003933 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003934 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003935 if (tp->serdes_counter)
3936 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 else {
3938 if (workaround) {
3939 u32 val = serdes_cfg;
3940
3941 if (port_a)
3942 val |= 0xc010000;
3943 else
3944 val |= 0x4010000;
3945
3946 tw32_f(MAC_SERDES_CFG, val);
3947 }
3948
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003949 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 udelay(40);
3951
3952 /* Link parallel detection - link is up */
3953 /* only if we have PCS_SYNC and not */
3954 /* receiving config code words */
3955 mac_status = tr32(MAC_STATUS);
3956 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
3957 !(mac_status & MAC_STATUS_RCVD_CFG)) {
3958 tg3_setup_flow_control(tp, 0, 0);
3959 current_link_up = 1;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003960 tp->phy_flags |=
3961 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003962 tp->serdes_counter =
3963 SERDES_PARALLEL_DET_TIMEOUT;
3964 } else
3965 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 }
3967 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07003968 } else {
3969 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003970 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 }
3972
3973out:
3974 return current_link_up;
3975}
3976
3977static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
3978{
3979 int current_link_up = 0;
3980
Michael Chan5cf64b8a2007-05-05 12:11:21 -07003981 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983
3984 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08003985 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003987
Matt Carlson5be73b42007-12-20 20:09:29 -08003988 if (fiber_autoneg(tp, &txflags, &rxflags)) {
3989 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990
Matt Carlson5be73b42007-12-20 20:09:29 -08003991 if (txflags & ANEG_CFG_PS1)
3992 local_adv |= ADVERTISE_1000XPAUSE;
3993 if (txflags & ANEG_CFG_PS2)
3994 local_adv |= ADVERTISE_1000XPSE_ASYM;
3995
3996 if (rxflags & MR_LP_ADV_SYM_PAUSE)
3997 remote_adv |= LPA_1000XPAUSE;
3998 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
3999 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000
4001 tg3_setup_flow_control(tp, local_adv, remote_adv);
4002
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 current_link_up = 1;
4004 }
4005 for (i = 0; i < 30; i++) {
4006 udelay(20);
4007 tw32_f(MAC_STATUS,
4008 (MAC_STATUS_SYNC_CHANGED |
4009 MAC_STATUS_CFG_CHANGED));
4010 udelay(40);
4011 if ((tr32(MAC_STATUS) &
4012 (MAC_STATUS_SYNC_CHANGED |
4013 MAC_STATUS_CFG_CHANGED)) == 0)
4014 break;
4015 }
4016
4017 mac_status = tr32(MAC_STATUS);
4018 if (current_link_up == 0 &&
4019 (mac_status & MAC_STATUS_PCS_SYNCED) &&
4020 !(mac_status & MAC_STATUS_RCVD_CFG))
4021 current_link_up = 1;
4022 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08004023 tg3_setup_flow_control(tp, 0, 0);
4024
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 /* Forcing 1000FD link up. */
4026 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027
4028 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
4029 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004030
4031 tw32_f(MAC_MODE, tp->mac_mode);
4032 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033 }
4034
4035out:
4036 return current_link_up;
4037}
4038
4039static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
4040{
4041 u32 orig_pause_cfg;
4042 u16 orig_active_speed;
4043 u8 orig_active_duplex;
4044 u32 mac_status;
4045 int current_link_up;
4046 int i;
4047
Matt Carlson8d018622007-12-20 20:05:44 -08004048 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 orig_active_speed = tp->link_config.active_speed;
4050 orig_active_duplex = tp->link_config.active_duplex;
4051
4052 if (!(tp->tg3_flags2 & TG3_FLG2_HW_AUTONEG) &&
4053 netif_carrier_ok(tp->dev) &&
4054 (tp->tg3_flags & TG3_FLAG_INIT_COMPLETE)) {
4055 mac_status = tr32(MAC_STATUS);
4056 mac_status &= (MAC_STATUS_PCS_SYNCED |
4057 MAC_STATUS_SIGNAL_DET |
4058 MAC_STATUS_CFG_CHANGED |
4059 MAC_STATUS_RCVD_CFG);
4060 if (mac_status == (MAC_STATUS_PCS_SYNCED |
4061 MAC_STATUS_SIGNAL_DET)) {
4062 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4063 MAC_STATUS_CFG_CHANGED));
4064 return 0;
4065 }
4066 }
4067
4068 tw32_f(MAC_TX_AUTO_NEG, 0);
4069
4070 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
4071 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
4072 tw32_f(MAC_MODE, tp->mac_mode);
4073 udelay(40);
4074
Matt Carlson79eb6902010-02-17 15:17:03 +00004075 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 tg3_init_bcm8002(tp);
4077
4078 /* Enable link change event even when serdes polling. */
4079 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4080 udelay(40);
4081
4082 current_link_up = 0;
4083 mac_status = tr32(MAC_STATUS);
4084
4085 if (tp->tg3_flags2 & TG3_FLG2_HW_AUTONEG)
4086 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
4087 else
4088 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
4089
Matt Carlson898a56f2009-08-28 14:02:40 +00004090 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00004092 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093
4094 for (i = 0; i < 100; i++) {
4095 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4096 MAC_STATUS_CFG_CHANGED));
4097 udelay(5);
4098 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07004099 MAC_STATUS_CFG_CHANGED |
4100 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 break;
4102 }
4103
4104 mac_status = tr32(MAC_STATUS);
4105 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
4106 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004107 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
4108 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109 tw32_f(MAC_MODE, (tp->mac_mode |
4110 MAC_MODE_SEND_CONFIGS));
4111 udelay(1);
4112 tw32_f(MAC_MODE, tp->mac_mode);
4113 }
4114 }
4115
4116 if (current_link_up == 1) {
4117 tp->link_config.active_speed = SPEED_1000;
4118 tp->link_config.active_duplex = DUPLEX_FULL;
4119 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4120 LED_CTRL_LNKLED_OVERRIDE |
4121 LED_CTRL_1000MBPS_ON));
4122 } else {
4123 tp->link_config.active_speed = SPEED_INVALID;
4124 tp->link_config.active_duplex = DUPLEX_INVALID;
4125 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4126 LED_CTRL_LNKLED_OVERRIDE |
4127 LED_CTRL_TRAFFIC_OVERRIDE));
4128 }
4129
4130 if (current_link_up != netif_carrier_ok(tp->dev)) {
4131 if (current_link_up)
4132 netif_carrier_on(tp->dev);
4133 else
4134 netif_carrier_off(tp->dev);
4135 tg3_link_report(tp);
4136 } else {
Matt Carlson8d018622007-12-20 20:05:44 -08004137 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138 if (orig_pause_cfg != now_pause_cfg ||
4139 orig_active_speed != tp->link_config.active_speed ||
4140 orig_active_duplex != tp->link_config.active_duplex)
4141 tg3_link_report(tp);
4142 }
4143
4144 return 0;
4145}
4146
Michael Chan747e8f82005-07-25 12:33:22 -07004147static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
4148{
4149 int current_link_up, err = 0;
4150 u32 bmsr, bmcr;
4151 u16 current_speed;
4152 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08004153 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07004154
4155 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4156 tw32_f(MAC_MODE, tp->mac_mode);
4157 udelay(40);
4158
4159 tw32(MAC_EVENT, 0);
4160
4161 tw32_f(MAC_STATUS,
4162 (MAC_STATUS_SYNC_CHANGED |
4163 MAC_STATUS_CFG_CHANGED |
4164 MAC_STATUS_MI_COMPLETION |
4165 MAC_STATUS_LNKSTATE_CHANGED));
4166 udelay(40);
4167
4168 if (force_reset)
4169 tg3_phy_reset(tp);
4170
4171 current_link_up = 0;
4172 current_speed = SPEED_INVALID;
4173 current_duplex = DUPLEX_INVALID;
4174
4175 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4176 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004177 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
4178 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4179 bmsr |= BMSR_LSTATUS;
4180 else
4181 bmsr &= ~BMSR_LSTATUS;
4182 }
Michael Chan747e8f82005-07-25 12:33:22 -07004183
4184 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
4185
4186 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004187 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004188 /* do nothing, just check for link up at the end */
4189 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4190 u32 adv, new_adv;
4191
4192 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4193 new_adv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
4194 ADVERTISE_1000XPAUSE |
4195 ADVERTISE_1000XPSE_ASYM |
4196 ADVERTISE_SLCT);
4197
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004198 new_adv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Michael Chan747e8f82005-07-25 12:33:22 -07004199
4200 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
4201 new_adv |= ADVERTISE_1000XHALF;
4202 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
4203 new_adv |= ADVERTISE_1000XFULL;
4204
4205 if ((new_adv != adv) || !(bmcr & BMCR_ANENABLE)) {
4206 tg3_writephy(tp, MII_ADVERTISE, new_adv);
4207 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
4208 tg3_writephy(tp, MII_BMCR, bmcr);
4209
4210 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07004211 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004212 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004213
4214 return err;
4215 }
4216 } else {
4217 u32 new_bmcr;
4218
4219 bmcr &= ~BMCR_SPEED1000;
4220 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
4221
4222 if (tp->link_config.duplex == DUPLEX_FULL)
4223 new_bmcr |= BMCR_FULLDPLX;
4224
4225 if (new_bmcr != bmcr) {
4226 /* BMCR_SPEED1000 is a reserved bit that needs
4227 * to be set on write.
4228 */
4229 new_bmcr |= BMCR_SPEED1000;
4230
4231 /* Force a linkdown */
4232 if (netif_carrier_ok(tp->dev)) {
4233 u32 adv;
4234
4235 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4236 adv &= ~(ADVERTISE_1000XFULL |
4237 ADVERTISE_1000XHALF |
4238 ADVERTISE_SLCT);
4239 tg3_writephy(tp, MII_ADVERTISE, adv);
4240 tg3_writephy(tp, MII_BMCR, bmcr |
4241 BMCR_ANRESTART |
4242 BMCR_ANENABLE);
4243 udelay(10);
4244 netif_carrier_off(tp->dev);
4245 }
4246 tg3_writephy(tp, MII_BMCR, new_bmcr);
4247 bmcr = new_bmcr;
4248 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4249 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004250 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
4251 ASIC_REV_5714) {
4252 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4253 bmsr |= BMSR_LSTATUS;
4254 else
4255 bmsr &= ~BMSR_LSTATUS;
4256 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004257 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004258 }
4259 }
4260
4261 if (bmsr & BMSR_LSTATUS) {
4262 current_speed = SPEED_1000;
4263 current_link_up = 1;
4264 if (bmcr & BMCR_FULLDPLX)
4265 current_duplex = DUPLEX_FULL;
4266 else
4267 current_duplex = DUPLEX_HALF;
4268
Matt Carlsonef167e22007-12-20 20:10:01 -08004269 local_adv = 0;
4270 remote_adv = 0;
4271
Michael Chan747e8f82005-07-25 12:33:22 -07004272 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08004273 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07004274
4275 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
4276 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
4277 common = local_adv & remote_adv;
4278 if (common & (ADVERTISE_1000XHALF |
4279 ADVERTISE_1000XFULL)) {
4280 if (common & ADVERTISE_1000XFULL)
4281 current_duplex = DUPLEX_FULL;
4282 else
4283 current_duplex = DUPLEX_HALF;
Matt Carlson57d8b882010-06-05 17:24:35 +00004284 } else if (!(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
4285 /* Link is up via parallel detect */
Matt Carlson859a588792010-04-05 10:19:28 +00004286 } else {
Michael Chan747e8f82005-07-25 12:33:22 -07004287 current_link_up = 0;
Matt Carlson859a588792010-04-05 10:19:28 +00004288 }
Michael Chan747e8f82005-07-25 12:33:22 -07004289 }
4290 }
4291
Matt Carlsonef167e22007-12-20 20:10:01 -08004292 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
4293 tg3_setup_flow_control(tp, local_adv, remote_adv);
4294
Michael Chan747e8f82005-07-25 12:33:22 -07004295 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4296 if (tp->link_config.active_duplex == DUPLEX_HALF)
4297 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4298
4299 tw32_f(MAC_MODE, tp->mac_mode);
4300 udelay(40);
4301
4302 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4303
4304 tp->link_config.active_speed = current_speed;
4305 tp->link_config.active_duplex = current_duplex;
4306
4307 if (current_link_up != netif_carrier_ok(tp->dev)) {
4308 if (current_link_up)
4309 netif_carrier_on(tp->dev);
4310 else {
4311 netif_carrier_off(tp->dev);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004312 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004313 }
4314 tg3_link_report(tp);
4315 }
4316 return err;
4317}
4318
4319static void tg3_serdes_parallel_detect(struct tg3 *tp)
4320{
Michael Chan3d3ebe72006-09-27 15:59:15 -07004321 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07004322 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07004323 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07004324 return;
4325 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00004326
Michael Chan747e8f82005-07-25 12:33:22 -07004327 if (!netif_carrier_ok(tp->dev) &&
4328 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
4329 u32 bmcr;
4330
4331 tg3_readphy(tp, MII_BMCR, &bmcr);
4332 if (bmcr & BMCR_ANENABLE) {
4333 u32 phy1, phy2;
4334
4335 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004336 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
4337 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07004338
4339 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004340 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
4341 MII_TG3_DSP_EXP1_INT_STAT);
4342 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
4343 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07004344
4345 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
4346 /* We have signal detect and not receiving
4347 * config code words, link is up by parallel
4348 * detection.
4349 */
4350
4351 bmcr &= ~BMCR_ANENABLE;
4352 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
4353 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004354 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004355 }
4356 }
Matt Carlson859a588792010-04-05 10:19:28 +00004357 } else if (netif_carrier_ok(tp->dev) &&
4358 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004359 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004360 u32 phy2;
4361
4362 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004363 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
4364 MII_TG3_DSP_EXP1_INT_STAT);
4365 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07004366 if (phy2 & 0x20) {
4367 u32 bmcr;
4368
4369 /* Config code words received, turn on autoneg. */
4370 tg3_readphy(tp, MII_BMCR, &bmcr);
4371 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
4372
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004373 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004374
4375 }
4376 }
4377}
4378
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379static int tg3_setup_phy(struct tg3 *tp, int force_reset)
4380{
4381 int err;
4382
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004383 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004385 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07004386 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a588792010-04-05 10:19:28 +00004387 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389
Matt Carlsonbcb37f62008-11-03 16:52:09 -08004390 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08004391 u32 val, scale;
4392
4393 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
4394 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
4395 scale = 65;
4396 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
4397 scale = 6;
4398 else
4399 scale = 12;
4400
4401 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
4402 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
4403 tw32(GRC_MISC_CFG, val);
4404 }
4405
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 if (tp->link_config.active_speed == SPEED_1000 &&
4407 tp->link_config.active_duplex == DUPLEX_HALF)
4408 tw32(MAC_TX_LENGTHS,
4409 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4410 (6 << TX_LENGTHS_IPG_SHIFT) |
4411 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
4412 else
4413 tw32(MAC_TX_LENGTHS,
4414 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4415 (6 << TX_LENGTHS_IPG_SHIFT) |
4416 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
4417
4418 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
4419 if (netif_carrier_ok(tp->dev)) {
4420 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07004421 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422 } else {
4423 tw32(HOSTCC_STAT_COAL_TICKS, 0);
4424 }
4425 }
4426
Matt Carlson8ed5d972007-05-07 00:25:49 -07004427 if (tp->tg3_flags & TG3_FLAG_ASPM_WORKAROUND) {
4428 u32 val = tr32(PCIE_PWR_MGMT_THRESH);
4429 if (!netif_carrier_ok(tp->dev))
4430 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
4431 tp->pwrmgmt_thresh;
4432 else
4433 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
4434 tw32(PCIE_PWR_MGMT_THRESH, val);
4435 }
4436
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 return err;
4438}
4439
Matt Carlson66cfd1b2010-09-30 10:34:30 +00004440static inline int tg3_irq_sync(struct tg3 *tp)
4441{
4442 return tp->irq_sync;
4443}
4444
Michael Chandf3e6542006-05-26 17:48:07 -07004445/* This is called whenever we suspect that the system chipset is re-
4446 * ordering the sequence of MMIO to the tx send mailbox. The symptom
4447 * is bogus tx completions. We try to recover by setting the
4448 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
4449 * in the workqueue.
4450 */
4451static void tg3_tx_recover(struct tg3 *tp)
4452{
4453 BUG_ON((tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER) ||
4454 tp->write32_tx_mbox == tg3_write_indirect_mbox);
4455
Matt Carlson5129c3a2010-04-05 10:19:23 +00004456 netdev_warn(tp->dev,
4457 "The system may be re-ordering memory-mapped I/O "
4458 "cycles to the network device, attempting to recover. "
4459 "Please report the problem to the driver maintainer "
4460 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07004461
4462 spin_lock(&tp->lock);
Michael Chandf3e6542006-05-26 17:48:07 -07004463 tp->tg3_flags |= TG3_FLAG_TX_RECOVERY_PENDING;
Michael Chandf3e6542006-05-26 17:48:07 -07004464 spin_unlock(&tp->lock);
4465}
4466
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004467static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07004468{
Matt Carlsonf65aac12010-08-02 11:26:03 +00004469 /* Tell compiler to fetch tx indices from memory. */
4470 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004471 return tnapi->tx_pending -
4472 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07004473}
4474
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475/* Tigon3 never reports partial packet sends. So we do not
4476 * need special logic to handle SKBs that have not had all
4477 * of their frags sent yet, like SunGEM does.
4478 */
Matt Carlson17375d22009-08-28 14:02:18 +00004479static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480{
Matt Carlson17375d22009-08-28 14:02:18 +00004481 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00004482 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004483 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004484 struct netdev_queue *txq;
4485 int index = tnapi - tp->napi;
4486
Matt Carlson19cfaec2009-12-03 08:36:20 +00004487 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004488 index--;
4489
4490 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491
4492 while (sw_idx != hw_idx) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00004493 struct ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07004495 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496
Michael Chandf3e6542006-05-26 17:48:07 -07004497 if (unlikely(skb == NULL)) {
4498 tg3_tx_recover(tp);
4499 return;
4500 }
4501
Alexander Duyckf4188d82009-12-02 16:48:38 +00004502 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004503 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00004504 skb_headlen(skb),
4505 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506
4507 ri->skb = NULL;
4508
4509 sw_idx = NEXT_TX(sw_idx);
4510
4511 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004512 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07004513 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
4514 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00004515
4516 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004517 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00004518 skb_shinfo(skb)->frags[i].size,
4519 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520 sw_idx = NEXT_TX(sw_idx);
4521 }
4522
David S. Millerf47c11e2005-06-24 20:18:35 -07004523 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07004524
4525 if (unlikely(tx_bug)) {
4526 tg3_tx_recover(tp);
4527 return;
4528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 }
4530
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004531 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532
Michael Chan1b2a7202006-08-07 21:46:02 -07004533 /* Need to make the tx_cons update visible to tg3_start_xmit()
4534 * before checking for netif_queue_stopped(). Without the
4535 * memory barrier, there is a small possibility that tg3_start_xmit()
4536 * will miss it and cause the queue to be stopped forever.
4537 */
4538 smp_mb();
4539
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004540 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004541 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004542 __netif_tx_lock(txq, smp_processor_id());
4543 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004544 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004545 netif_tx_wake_queue(txq);
4546 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07004547 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548}
4549
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004550static void tg3_rx_skb_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
4551{
4552 if (!ri->skb)
4553 return;
4554
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004555 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004556 map_sz, PCI_DMA_FROMDEVICE);
4557 dev_kfree_skb_any(ri->skb);
4558 ri->skb = NULL;
4559}
4560
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561/* Returns size of skb allocated or < 0 on error.
4562 *
4563 * We only need to fill in the address because the other members
4564 * of the RX descriptor are invariant, see tg3_init_rings.
4565 *
4566 * Note the purposeful assymetry of cpu vs. chip accesses. For
4567 * posting buffers we only dirty the first cache line of the RX
4568 * descriptor (containing the address). Whereas for the RX status
4569 * buffers the cpu only reads the last cacheline of the RX descriptor
4570 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
4571 */
Matt Carlson86b21e52009-11-13 13:03:45 +00004572static int tg3_alloc_rx_skb(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Matt Carlsona3896162009-11-13 13:03:44 +00004573 u32 opaque_key, u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574{
4575 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00004576 struct ring_info *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 struct sk_buff *skb;
4578 dma_addr_t mapping;
4579 int skb_size, dest_idx;
4580
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 switch (opaque_key) {
4582 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00004583 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00004584 desc = &tpr->rx_std[dest_idx];
4585 map = &tpr->rx_std_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004586 skb_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587 break;
4588
4589 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00004590 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00004591 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00004592 map = &tpr->rx_jmb_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004593 skb_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594 break;
4595
4596 default:
4597 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004598 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599
4600 /* Do not overwrite any of the map or rp information
4601 * until we are sure we can commit to a new buffer.
4602 *
4603 * Callers depend upon this behavior and assume that
4604 * we leave everything unchanged if we fail.
4605 */
Matt Carlson287be122009-08-28 13:58:46 +00004606 skb = netdev_alloc_skb(tp->dev, skb_size + tp->rx_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607 if (skb == NULL)
4608 return -ENOMEM;
4609
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610 skb_reserve(skb, tp->rx_offset);
4611
Matt Carlson287be122009-08-28 13:58:46 +00004612 mapping = pci_map_single(tp->pdev, skb->data, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 PCI_DMA_FROMDEVICE);
Matt Carlsona21771d2009-11-02 14:25:31 +00004614 if (pci_dma_mapping_error(tp->pdev, mapping)) {
4615 dev_kfree_skb(skb);
4616 return -EIO;
4617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618
4619 map->skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004620 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 desc->addr_hi = ((u64)mapping >> 32);
4623 desc->addr_lo = ((u64)mapping & 0xffffffff);
4624
4625 return skb_size;
4626}
4627
4628/* We only need to move over in the address because the other
4629 * members of the RX descriptor are invariant. See notes above
4630 * tg3_alloc_rx_skb for full details.
4631 */
Matt Carlsona3896162009-11-13 13:03:44 +00004632static void tg3_recycle_rx(struct tg3_napi *tnapi,
4633 struct tg3_rx_prodring_set *dpr,
4634 u32 opaque_key, int src_idx,
4635 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636{
Matt Carlson17375d22009-08-28 14:02:18 +00004637 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
4639 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00004640 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00004641 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642
4643 switch (opaque_key) {
4644 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00004645 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00004646 dest_desc = &dpr->rx_std[dest_idx];
4647 dest_map = &dpr->rx_std_buffers[dest_idx];
4648 src_desc = &spr->rx_std[src_idx];
4649 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650 break;
4651
4652 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00004653 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00004654 dest_desc = &dpr->rx_jmb[dest_idx].std;
4655 dest_map = &dpr->rx_jmb_buffers[dest_idx];
4656 src_desc = &spr->rx_jmb[src_idx].std;
4657 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658 break;
4659
4660 default:
4661 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663
4664 dest_map->skb = src_map->skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004665 dma_unmap_addr_set(dest_map, mapping,
4666 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667 dest_desc->addr_hi = src_desc->addr_hi;
4668 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00004669
4670 /* Ensure that the update to the skb happens after the physical
4671 * addresses have been transferred to the new BD location.
4672 */
4673 smp_wmb();
4674
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675 src_map->skb = NULL;
4676}
4677
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678/* The RX ring scheme is composed of multiple rings which post fresh
4679 * buffers to the chip, and one special ring the chip uses to report
4680 * status back to the host.
4681 *
4682 * The special ring reports the status of received packets to the
4683 * host. The chip does not write into the original descriptor the
4684 * RX buffer was obtained from. The chip simply takes the original
4685 * descriptor as provided by the host, updates the status and length
4686 * field, then writes this into the next status ring entry.
4687 *
4688 * Each ring the host uses to post buffers to the chip is described
4689 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
4690 * it is first placed into the on-chip ram. When the packet's length
4691 * is known, it walks down the TG3_BDINFO entries to select the ring.
4692 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
4693 * which is within the range of the new packet's length is chosen.
4694 *
4695 * The "separate ring for rx status" scheme may sound queer, but it makes
4696 * sense from a cache coherency perspective. If only the host writes
4697 * to the buffer post rings, and only the chip writes to the rx status
4698 * rings, then cache lines never move beyond shared-modified state.
4699 * If both the host and chip were to write into the same ring, cache line
4700 * eviction could occur since both entities want it in an exclusive state.
4701 */
Matt Carlson17375d22009-08-28 14:02:18 +00004702static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703{
Matt Carlson17375d22009-08-28 14:02:18 +00004704 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07004705 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00004706 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00004707 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07004708 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00004710 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004712 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713 /*
4714 * We need to order the read of hw_idx and the read of
4715 * the opaque cookie.
4716 */
4717 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718 work_mask = 0;
4719 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00004720 std_prod_idx = tpr->rx_std_prod_idx;
4721 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00004723 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00004724 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725 unsigned int len;
4726 struct sk_buff *skb;
4727 dma_addr_t dma_addr;
4728 u32 opaque_key, desc_idx, *post_ptr;
Matt Carlson9dc7a112010-04-12 06:58:28 +00004729 bool hw_vlan __maybe_unused = false;
4730 u16 vtag __maybe_unused = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731
4732 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
4733 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
4734 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00004735 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004736 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00004737 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00004738 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07004739 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00004741 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004742 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00004743 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00004744 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00004745 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747
4748 work_mask |= opaque_key;
4749
4750 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
4751 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
4752 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00004753 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754 desc_idx, *post_ptr);
4755 drop_it_no_recycle:
4756 /* Other statistics kept track of by card. */
4757 tp->net_stats.rx_dropped++;
4758 goto next_pkt;
4759 }
4760
Matt Carlsonad829262008-11-21 17:16:16 -08004761 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
4762 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763
Matt Carlsond2757fc2010-04-12 06:58:27 +00004764 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765 int skb_size;
4766
Matt Carlson86b21e52009-11-13 13:03:45 +00004767 skb_size = tg3_alloc_rx_skb(tp, tpr, opaque_key,
Matt Carlsonafc081f2009-11-13 13:03:43 +00004768 *post_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769 if (skb_size < 0)
4770 goto drop_it;
4771
Matt Carlson287be122009-08-28 13:58:46 +00004772 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773 PCI_DMA_FROMDEVICE);
4774
Matt Carlson61e800c2010-02-17 15:16:54 +00004775 /* Ensure that the update to the skb happens
4776 * after the usage of the old DMA mapping.
4777 */
4778 smp_wmb();
4779
4780 ri->skb = NULL;
4781
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 skb_put(skb, len);
4783 } else {
4784 struct sk_buff *copy_skb;
4785
Matt Carlsona3896162009-11-13 13:03:44 +00004786 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787 desc_idx, *post_ptr);
4788
Matt Carlson9dc7a112010-04-12 06:58:28 +00004789 copy_skb = netdev_alloc_skb(tp->dev, len + VLAN_HLEN +
4790 TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 if (copy_skb == NULL)
4792 goto drop_it_no_recycle;
4793
Matt Carlson9dc7a112010-04-12 06:58:28 +00004794 skb_reserve(copy_skb, TG3_RAW_IP_ALIGN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795 skb_put(copy_skb, len);
4796 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03004797 skb_copy_from_linear_data(skb, copy_skb->data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
4799
4800 /* We'll reuse the original ring buffer. */
4801 skb = copy_skb;
4802 }
4803
4804 if ((tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) &&
4805 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
4806 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
4807 >> RXD_TCPCSUM_SHIFT) == 0xffff))
4808 skb->ip_summed = CHECKSUM_UNNECESSARY;
4809 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07004810 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811
4812 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00004813
4814 if (len > (tp->dev->mtu + ETH_HLEN) &&
4815 skb->protocol != htons(ETH_P_8021Q)) {
4816 dev_kfree_skb(skb);
4817 goto next_pkt;
4818 }
4819
Matt Carlson9dc7a112010-04-12 06:58:28 +00004820 if (desc->type_flags & RXD_FLAG_VLAN &&
4821 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG)) {
4822 vtag = desc->err_vlan & RXD_VLAN_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823#if TG3_VLAN_TAG_USED
Matt Carlson9dc7a112010-04-12 06:58:28 +00004824 if (tp->vlgrp)
4825 hw_vlan = true;
4826 else
4827#endif
4828 {
4829 struct vlan_ethhdr *ve = (struct vlan_ethhdr *)
4830 __skb_push(skb, VLAN_HLEN);
4831
4832 memmove(ve, skb->data + VLAN_HLEN,
4833 ETH_ALEN * 2);
4834 ve->h_vlan_proto = htons(ETH_P_8021Q);
4835 ve->h_vlan_TCI = htons(vtag);
4836 }
4837 }
4838
4839#if TG3_VLAN_TAG_USED
4840 if (hw_vlan)
4841 vlan_gro_receive(&tnapi->napi, tp->vlgrp, vtag, skb);
4842 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843#endif
Matt Carlson17375d22009-08-28 14:02:18 +00004844 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 received++;
4847 budget--;
4848
4849next_pkt:
4850 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07004851
4852 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00004853 tpr->rx_std_prod_idx = std_prod_idx &
4854 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00004855 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
4856 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07004857 work_mask &= ~RXD_OPAQUE_RING_STD;
4858 rx_std_posted = 0;
4859 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07004861 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00004862 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07004863
4864 /* Refresh hw_idx to see if there is new work */
4865 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004866 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07004867 rmb();
4868 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869 }
4870
4871 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00004872 tnapi->rx_rcb_ptr = sw_idx;
4873 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874
4875 /* Refill RX ring(s). */
Matt Carlsone4af1af2010-02-12 14:47:05 +00004876 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004877 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00004878 tpr->rx_std_prod_idx = std_prod_idx &
4879 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004880 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
4881 tpr->rx_std_prod_idx);
4882 }
4883 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00004884 tpr->rx_jmb_prod_idx = jmb_prod_idx &
4885 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004886 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
4887 tpr->rx_jmb_prod_idx);
4888 }
4889 mmiowb();
4890 } else if (work_mask) {
4891 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
4892 * updated before the producer indices can be updated.
4893 */
4894 smp_wmb();
4895
Matt Carlson2c49a442010-09-30 10:34:35 +00004896 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
4897 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004898
Matt Carlsone4af1af2010-02-12 14:47:05 +00004899 if (tnapi != &tp->napi[1])
4900 napi_schedule(&tp->napi[1].napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902
4903 return received;
4904}
4905
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004906static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908 /* handle link change and other phy events */
4909 if (!(tp->tg3_flags &
4910 (TG3_FLAG_USE_LINKCHG_REG |
4911 TG3_FLAG_POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004912 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
4913
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 if (sblk->status & SD_STATUS_LINK_CHG) {
4915 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004916 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07004917 spin_lock(&tp->lock);
Matt Carlsondd477002008-05-25 23:45:58 -07004918 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
4919 tw32_f(MAC_STATUS,
4920 (MAC_STATUS_SYNC_CHANGED |
4921 MAC_STATUS_CFG_CHANGED |
4922 MAC_STATUS_MI_COMPLETION |
4923 MAC_STATUS_LNKSTATE_CHANGED));
4924 udelay(40);
4925 } else
4926 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07004927 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928 }
4929 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004930}
4931
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004932static int tg3_rx_prodring_xfer(struct tg3 *tp,
4933 struct tg3_rx_prodring_set *dpr,
4934 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004935{
4936 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004937 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004938
4939 while (1) {
4940 src_prod_idx = spr->rx_std_prod_idx;
4941
4942 /* Make sure updates to the rx_std_buffers[] entries and the
4943 * standard producer index are seen in the correct order.
4944 */
4945 smp_rmb();
4946
4947 if (spr->rx_std_cons_idx == src_prod_idx)
4948 break;
4949
4950 if (spr->rx_std_cons_idx < src_prod_idx)
4951 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
4952 else
Matt Carlson2c49a442010-09-30 10:34:35 +00004953 cpycnt = tp->rx_std_ring_mask + 1 -
4954 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004955
Matt Carlson2c49a442010-09-30 10:34:35 +00004956 cpycnt = min(cpycnt,
4957 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004958
4959 si = spr->rx_std_cons_idx;
4960 di = dpr->rx_std_prod_idx;
4961
Matt Carlsone92967b2010-02-12 14:47:06 +00004962 for (i = di; i < di + cpycnt; i++) {
4963 if (dpr->rx_std_buffers[i].skb) {
4964 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004965 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00004966 break;
4967 }
4968 }
4969
4970 if (!cpycnt)
4971 break;
4972
4973 /* Ensure that updates to the rx_std_buffers ring and the
4974 * shadowed hardware producer ring from tg3_recycle_skb() are
4975 * ordered correctly WRT the skb check above.
4976 */
4977 smp_rmb();
4978
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004979 memcpy(&dpr->rx_std_buffers[di],
4980 &spr->rx_std_buffers[si],
4981 cpycnt * sizeof(struct ring_info));
4982
4983 for (i = 0; i < cpycnt; i++, di++, si++) {
4984 struct tg3_rx_buffer_desc *sbd, *dbd;
4985 sbd = &spr->rx_std[si];
4986 dbd = &dpr->rx_std[di];
4987 dbd->addr_hi = sbd->addr_hi;
4988 dbd->addr_lo = sbd->addr_lo;
4989 }
4990
Matt Carlson2c49a442010-09-30 10:34:35 +00004991 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
4992 tp->rx_std_ring_mask;
4993 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
4994 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004995 }
4996
4997 while (1) {
4998 src_prod_idx = spr->rx_jmb_prod_idx;
4999
5000 /* Make sure updates to the rx_jmb_buffers[] entries and
5001 * the jumbo producer index are seen in the correct order.
5002 */
5003 smp_rmb();
5004
5005 if (spr->rx_jmb_cons_idx == src_prod_idx)
5006 break;
5007
5008 if (spr->rx_jmb_cons_idx < src_prod_idx)
5009 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
5010 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005011 cpycnt = tp->rx_jmb_ring_mask + 1 -
5012 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005013
5014 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00005015 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005016
5017 si = spr->rx_jmb_cons_idx;
5018 di = dpr->rx_jmb_prod_idx;
5019
Matt Carlsone92967b2010-02-12 14:47:06 +00005020 for (i = di; i < di + cpycnt; i++) {
5021 if (dpr->rx_jmb_buffers[i].skb) {
5022 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005023 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005024 break;
5025 }
5026 }
5027
5028 if (!cpycnt)
5029 break;
5030
5031 /* Ensure that updates to the rx_jmb_buffers ring and the
5032 * shadowed hardware producer ring from tg3_recycle_skb() are
5033 * ordered correctly WRT the skb check above.
5034 */
5035 smp_rmb();
5036
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005037 memcpy(&dpr->rx_jmb_buffers[di],
5038 &spr->rx_jmb_buffers[si],
5039 cpycnt * sizeof(struct ring_info));
5040
5041 for (i = 0; i < cpycnt; i++, di++, si++) {
5042 struct tg3_rx_buffer_desc *sbd, *dbd;
5043 sbd = &spr->rx_jmb[si].std;
5044 dbd = &dpr->rx_jmb[di].std;
5045 dbd->addr_hi = sbd->addr_hi;
5046 dbd->addr_lo = sbd->addr_lo;
5047 }
5048
Matt Carlson2c49a442010-09-30 10:34:35 +00005049 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
5050 tp->rx_jmb_ring_mask;
5051 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
5052 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005053 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005054
5055 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005056}
5057
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005058static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
5059{
5060 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061
5062 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005063 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00005064 tg3_tx(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07005065 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
Michael Chan4fd7ab52007-10-12 01:39:50 -07005066 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067 }
5068
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 /* run RX thread, within the bounds set by NAPI.
5070 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005071 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005073 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00005074 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005076 if ((tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005077 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005078 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00005079 u32 std_prod_idx = dpr->rx_std_prod_idx;
5080 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005081
Matt Carlsone4af1af2010-02-12 14:47:05 +00005082 for (i = 1; i < tp->irq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005083 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00005084 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005085
5086 wmb();
5087
Matt Carlsone4af1af2010-02-12 14:47:05 +00005088 if (std_prod_idx != dpr->rx_std_prod_idx)
5089 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5090 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005091
Matt Carlsone4af1af2010-02-12 14:47:05 +00005092 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
5093 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5094 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005095
5096 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005097
5098 if (err)
5099 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005100 }
5101
David S. Miller6f535762007-10-11 18:08:29 -07005102 return work_done;
5103}
David S. Millerf7383c22005-05-18 22:50:53 -07005104
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005105static int tg3_poll_msix(struct napi_struct *napi, int budget)
5106{
5107 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5108 struct tg3 *tp = tnapi->tp;
5109 int work_done = 0;
5110 struct tg3_hw_status *sblk = tnapi->hw_status;
5111
5112 while (1) {
5113 work_done = tg3_poll_work(tnapi, work_done, budget);
5114
5115 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
5116 goto tx_recovery;
5117
5118 if (unlikely(work_done >= budget))
5119 break;
5120
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005121 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005122 * to tell the hw how much work has been processed,
5123 * so we must read it before checking for more work.
5124 */
5125 tnapi->last_tag = sblk->status_tag;
5126 tnapi->last_irq_tag = tnapi->last_tag;
5127 rmb();
5128
5129 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00005130 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
5131 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005132 napi_complete(napi);
5133 /* Reenable interrupts. */
5134 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
5135 mmiowb();
5136 break;
5137 }
5138 }
5139
5140 return work_done;
5141
5142tx_recovery:
5143 /* work_done is guaranteed to be less than budget. */
5144 napi_complete(napi);
5145 schedule_work(&tp->reset_task);
5146 return work_done;
5147}
5148
David S. Miller6f535762007-10-11 18:08:29 -07005149static int tg3_poll(struct napi_struct *napi, int budget)
5150{
Matt Carlson8ef04422009-08-28 14:01:37 +00005151 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5152 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07005153 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00005154 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07005155
5156 while (1) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005157 tg3_poll_link(tp);
5158
Matt Carlson17375d22009-08-28 14:02:18 +00005159 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07005160
5161 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
5162 goto tx_recovery;
5163
5164 if (unlikely(work_done >= budget))
5165 break;
5166
Michael Chan4fd7ab52007-10-12 01:39:50 -07005167 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) {
Matt Carlson17375d22009-08-28 14:02:18 +00005168 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07005169 * to tell the hw how much work has been processed,
5170 * so we must read it before checking for more work.
5171 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005172 tnapi->last_tag = sblk->status_tag;
5173 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07005174 rmb();
5175 } else
5176 sblk->status &= ~SD_STATUS_UPDATED;
5177
Matt Carlson17375d22009-08-28 14:02:18 +00005178 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005179 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00005180 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07005181 break;
5182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183 }
5184
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005185 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07005186
5187tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07005188 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08005189 napi_complete(napi);
David S. Miller6f535762007-10-11 18:08:29 -07005190 schedule_work(&tp->reset_task);
Michael Chan4fd7ab52007-10-12 01:39:50 -07005191 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192}
5193
Matt Carlson66cfd1b2010-09-30 10:34:30 +00005194static void tg3_napi_disable(struct tg3 *tp)
5195{
5196 int i;
5197
5198 for (i = tp->irq_cnt - 1; i >= 0; i--)
5199 napi_disable(&tp->napi[i].napi);
5200}
5201
5202static void tg3_napi_enable(struct tg3 *tp)
5203{
5204 int i;
5205
5206 for (i = 0; i < tp->irq_cnt; i++)
5207 napi_enable(&tp->napi[i].napi);
5208}
5209
5210static void tg3_napi_init(struct tg3 *tp)
5211{
5212 int i;
5213
5214 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
5215 for (i = 1; i < tp->irq_cnt; i++)
5216 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
5217}
5218
5219static void tg3_napi_fini(struct tg3 *tp)
5220{
5221 int i;
5222
5223 for (i = 0; i < tp->irq_cnt; i++)
5224 netif_napi_del(&tp->napi[i].napi);
5225}
5226
5227static inline void tg3_netif_stop(struct tg3 *tp)
5228{
5229 tp->dev->trans_start = jiffies; /* prevent tx timeout */
5230 tg3_napi_disable(tp);
5231 netif_tx_disable(tp->dev);
5232}
5233
5234static inline void tg3_netif_start(struct tg3 *tp)
5235{
5236 /* NOTE: unconditional netif_tx_wake_all_queues is only
5237 * appropriate so long as all callers are assured to
5238 * have free tx slots (such as after tg3_init_hw)
5239 */
5240 netif_tx_wake_all_queues(tp->dev);
5241
5242 tg3_napi_enable(tp);
5243 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
5244 tg3_enable_ints(tp);
5245}
5246
David S. Millerf47c11e2005-06-24 20:18:35 -07005247static void tg3_irq_quiesce(struct tg3 *tp)
5248{
Matt Carlson4f125f42009-09-01 12:55:02 +00005249 int i;
5250
David S. Millerf47c11e2005-06-24 20:18:35 -07005251 BUG_ON(tp->irq_sync);
5252
5253 tp->irq_sync = 1;
5254 smp_mb();
5255
Matt Carlson4f125f42009-09-01 12:55:02 +00005256 for (i = 0; i < tp->irq_cnt; i++)
5257 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07005258}
5259
David S. Millerf47c11e2005-06-24 20:18:35 -07005260/* Fully shutdown all tg3 driver activity elsewhere in the system.
5261 * If irq_sync is non-zero, then the IRQ handler must be synchronized
5262 * with as well. Most of the time, this is not necessary except when
5263 * shutting down the device.
5264 */
5265static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
5266{
Michael Chan46966542007-07-11 19:47:19 -07005267 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07005268 if (irq_sync)
5269 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07005270}
5271
5272static inline void tg3_full_unlock(struct tg3 *tp)
5273{
David S. Millerf47c11e2005-06-24 20:18:35 -07005274 spin_unlock_bh(&tp->lock);
5275}
5276
Michael Chanfcfa0a32006-03-20 22:28:41 -08005277/* One-shot MSI handler - Chip automatically disables interrupt
5278 * after sending MSI so driver doesn't have to do it.
5279 */
David Howells7d12e782006-10-05 14:55:46 +01005280static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08005281{
Matt Carlson09943a12009-08-28 14:01:57 +00005282 struct tg3_napi *tnapi = dev_id;
5283 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08005284
Matt Carlson898a56f2009-08-28 14:02:40 +00005285 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005286 if (tnapi->rx_rcb)
5287 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005288
5289 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005290 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005291
5292 return IRQ_HANDLED;
5293}
5294
Michael Chan88b06bc22005-04-21 17:13:25 -07005295/* MSI ISR - No need to check for interrupt sharing and no need to
5296 * flush status block and interrupt mailbox. PCI ordering rules
5297 * guarantee that MSI will arrive after the status block.
5298 */
David Howells7d12e782006-10-05 14:55:46 +01005299static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07005300{
Matt Carlson09943a12009-08-28 14:01:57 +00005301 struct tg3_napi *tnapi = dev_id;
5302 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07005303
Matt Carlson898a56f2009-08-28 14:02:40 +00005304 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005305 if (tnapi->rx_rcb)
5306 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07005307 /*
David S. Millerfac9b832005-05-18 22:46:34 -07005308 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07005309 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07005310 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07005311 * NIC to stop sending us irqs, engaging "in-intr-handler"
5312 * event coalescing.
5313 */
5314 tw32_mailbox(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07005315 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005316 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07005317
Michael Chan88b06bc22005-04-21 17:13:25 -07005318 return IRQ_RETVAL(1);
5319}
5320
David Howells7d12e782006-10-05 14:55:46 +01005321static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322{
Matt Carlson09943a12009-08-28 14:01:57 +00005323 struct tg3_napi *tnapi = dev_id;
5324 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005325 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326 unsigned int handled = 1;
5327
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328 /* In INTx mode, it is possible for the interrupt to arrive at
5329 * the CPU before the status block posted prior to the interrupt.
5330 * Reading the PCI State register will confirm whether the
5331 * interrupt is ours and will flush the status block.
5332 */
Michael Chand18edcb2007-03-24 20:57:11 -07005333 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
5334 if ((tp->tg3_flags & TG3_FLAG_CHIP_RESETTING) ||
5335 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5336 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005337 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07005338 }
Michael Chand18edcb2007-03-24 20:57:11 -07005339 }
5340
5341 /*
5342 * Writing any value to intr-mbox-0 clears PCI INTA# and
5343 * chip-internal interrupt pending events.
5344 * Writing non-zero to intr-mbox-0 additional tells the
5345 * NIC to stop sending us irqs, engaging "in-intr-handler"
5346 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005347 *
5348 * Flush the mailbox to de-assert the IRQ immediately to prevent
5349 * spurious interrupts. The flush impacts performance but
5350 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005351 */
Michael Chanc04cb342007-05-07 00:26:15 -07005352 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07005353 if (tg3_irq_sync(tp))
5354 goto out;
5355 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00005356 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00005357 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00005358 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07005359 } else {
5360 /* No work, shared interrupt perhaps? re-enable
5361 * interrupts, and flush that PCI write
5362 */
5363 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
5364 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07005365 }
David S. Millerf47c11e2005-06-24 20:18:35 -07005366out:
David S. Millerfac9b832005-05-18 22:46:34 -07005367 return IRQ_RETVAL(handled);
5368}
5369
David Howells7d12e782006-10-05 14:55:46 +01005370static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07005371{
Matt Carlson09943a12009-08-28 14:01:57 +00005372 struct tg3_napi *tnapi = dev_id;
5373 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005374 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07005375 unsigned int handled = 1;
5376
David S. Millerfac9b832005-05-18 22:46:34 -07005377 /* In INTx mode, it is possible for the interrupt to arrive at
5378 * the CPU before the status block posted prior to the interrupt.
5379 * Reading the PCI State register will confirm whether the
5380 * interrupt is ours and will flush the status block.
5381 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005382 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Michael Chand18edcb2007-03-24 20:57:11 -07005383 if ((tp->tg3_flags & TG3_FLAG_CHIP_RESETTING) ||
5384 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5385 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005386 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387 }
Michael Chand18edcb2007-03-24 20:57:11 -07005388 }
5389
5390 /*
5391 * writing any value to intr-mbox-0 clears PCI INTA# and
5392 * chip-internal interrupt pending events.
5393 * writing non-zero to intr-mbox-0 additional tells the
5394 * NIC to stop sending us irqs, engaging "in-intr-handler"
5395 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005396 *
5397 * Flush the mailbox to de-assert the IRQ immediately to prevent
5398 * spurious interrupts. The flush impacts performance but
5399 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005400 */
Michael Chanc04cb342007-05-07 00:26:15 -07005401 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00005402
5403 /*
5404 * In a shared interrupt configuration, sometimes other devices'
5405 * interrupts will scream. We record the current status tag here
5406 * so that the above check can report that the screaming interrupts
5407 * are unhandled. Eventually they will be silenced.
5408 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005409 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00005410
Michael Chand18edcb2007-03-24 20:57:11 -07005411 if (tg3_irq_sync(tp))
5412 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00005413
Matt Carlson72334482009-08-28 14:03:01 +00005414 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00005415
Matt Carlson09943a12009-08-28 14:01:57 +00005416 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00005417
David S. Millerf47c11e2005-06-24 20:18:35 -07005418out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419 return IRQ_RETVAL(handled);
5420}
5421
Michael Chan79381092005-04-21 17:13:59 -07005422/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01005423static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07005424{
Matt Carlson09943a12009-08-28 14:01:57 +00005425 struct tg3_napi *tnapi = dev_id;
5426 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005427 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07005428
Michael Chanf9804dd2005-09-27 12:13:10 -07005429 if ((sblk->status & SD_STATUS_UPDATED) ||
5430 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07005431 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07005432 return IRQ_RETVAL(1);
5433 }
5434 return IRQ_RETVAL(0);
5435}
5436
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07005437static int tg3_init_hw(struct tg3 *, int);
Michael Chan944d9802005-05-29 14:57:48 -07005438static int tg3_halt(struct tg3 *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439
Michael Chanb9ec6c12006-07-25 16:37:27 -07005440/* Restart hardware after configuration changes, self-test, etc.
5441 * Invoked with tp->lock held.
5442 */
5443static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
Eric Dumazet78c61462008-04-24 23:33:06 -07005444 __releases(tp->lock)
5445 __acquires(tp->lock)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005446{
5447 int err;
5448
5449 err = tg3_init_hw(tp, reset_phy);
5450 if (err) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00005451 netdev_err(tp->dev,
5452 "Failed to re-initialize device, aborting\n");
Michael Chanb9ec6c12006-07-25 16:37:27 -07005453 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
5454 tg3_full_unlock(tp);
5455 del_timer_sync(&tp->timer);
5456 tp->irq_sync = 0;
Matt Carlsonfed97812009-09-01 13:10:19 +00005457 tg3_napi_enable(tp);
Michael Chanb9ec6c12006-07-25 16:37:27 -07005458 dev_close(tp->dev);
5459 tg3_full_lock(tp, 0);
5460 }
5461 return err;
5462}
5463
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464#ifdef CONFIG_NET_POLL_CONTROLLER
5465static void tg3_poll_controller(struct net_device *dev)
5466{
Matt Carlson4f125f42009-09-01 12:55:02 +00005467 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07005468 struct tg3 *tp = netdev_priv(dev);
5469
Matt Carlson4f125f42009-09-01 12:55:02 +00005470 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00005471 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472}
5473#endif
5474
David Howellsc4028952006-11-22 14:57:56 +00005475static void tg3_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476{
David Howellsc4028952006-11-22 14:57:56 +00005477 struct tg3 *tp = container_of(work, struct tg3, reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005478 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479 unsigned int restart_timer;
5480
Michael Chan7faa0062006-02-02 17:29:28 -08005481 tg3_full_lock(tp, 0);
Michael Chan7faa0062006-02-02 17:29:28 -08005482
5483 if (!netif_running(tp->dev)) {
Michael Chan7faa0062006-02-02 17:29:28 -08005484 tg3_full_unlock(tp);
5485 return;
5486 }
5487
5488 tg3_full_unlock(tp);
5489
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005490 tg3_phy_stop(tp);
5491
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492 tg3_netif_stop(tp);
5493
David S. Millerf47c11e2005-06-24 20:18:35 -07005494 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495
5496 restart_timer = tp->tg3_flags2 & TG3_FLG2_RESTART_TIMER;
5497 tp->tg3_flags2 &= ~TG3_FLG2_RESTART_TIMER;
5498
Michael Chandf3e6542006-05-26 17:48:07 -07005499 if (tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING) {
5500 tp->write32_tx_mbox = tg3_write32_tx_mbox;
5501 tp->write32_rx_mbox = tg3_write_flush_reg32;
5502 tp->tg3_flags |= TG3_FLAG_MBOX_WRITE_REORDER;
5503 tp->tg3_flags &= ~TG3_FLAG_TX_RECOVERY_PENDING;
5504 }
5505
Michael Chan944d9802005-05-29 14:57:48 -07005506 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005507 err = tg3_init_hw(tp, 1);
5508 if (err)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005509 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510
5511 tg3_netif_start(tp);
5512
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513 if (restart_timer)
5514 mod_timer(&tp->timer, jiffies + 1);
Michael Chan7faa0062006-02-02 17:29:28 -08005515
Michael Chanb9ec6c12006-07-25 16:37:27 -07005516out:
Michael Chan7faa0062006-02-02 17:29:28 -08005517 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005518
5519 if (!err)
5520 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521}
5522
Michael Chanb0408752007-02-13 12:18:30 -08005523static void tg3_dump_short_state(struct tg3 *tp)
5524{
Joe Perches05dbe002010-02-17 19:44:19 +00005525 netdev_err(tp->dev, "DEBUG: MAC_TX_STATUS[%08x] MAC_RX_STATUS[%08x]\n",
5526 tr32(MAC_TX_STATUS), tr32(MAC_RX_STATUS));
5527 netdev_err(tp->dev, "DEBUG: RDMAC_STATUS[%08x] WDMAC_STATUS[%08x]\n",
5528 tr32(RDMAC_STATUS), tr32(WDMAC_STATUS));
Michael Chanb0408752007-02-13 12:18:30 -08005529}
5530
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531static void tg3_tx_timeout(struct net_device *dev)
5532{
5533 struct tg3 *tp = netdev_priv(dev);
5534
Michael Chanb0408752007-02-13 12:18:30 -08005535 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00005536 netdev_err(dev, "transmit timed out, resetting\n");
Michael Chanb0408752007-02-13 12:18:30 -08005537 tg3_dump_short_state(tp);
5538 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539
5540 schedule_work(&tp->reset_task);
5541}
5542
Michael Chanc58ec932005-09-17 00:46:27 -07005543/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
5544static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
5545{
5546 u32 base = (u32) mapping & 0xffffffff;
5547
Eric Dumazet807540b2010-09-23 05:40:09 +00005548 return (base > 0xffffdcc0) && (base + len + 8 < base);
Michael Chanc58ec932005-09-17 00:46:27 -07005549}
5550
Michael Chan72f2afb2006-03-06 19:28:35 -08005551/* Test for DMA addresses > 40-bit */
5552static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
5553 int len)
5554{
5555#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Michael Chan6728a8e2006-03-27 23:16:49 -08005556 if (tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG)
Eric Dumazet807540b2010-09-23 05:40:09 +00005557 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08005558 return 0;
5559#else
5560 return 0;
5561#endif
5562}
5563
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005564static void tg3_set_txd(struct tg3_napi *, int, dma_addr_t, int, u32, u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565
Michael Chan72f2afb2006-03-06 19:28:35 -08005566/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00005567static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
5568 struct sk_buff *skb, u32 last_plus_one,
5569 u32 *start, u32 base_flags, u32 mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570{
Matt Carlson24f4efd2009-11-13 13:03:35 +00005571 struct tg3 *tp = tnapi->tp;
Matt Carlson41588ba2008-04-19 18:12:33 -07005572 struct sk_buff *new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07005573 dma_addr_t new_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574 u32 entry = *start;
Michael Chanc58ec932005-09-17 00:46:27 -07005575 int i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576
Matt Carlson41588ba2008-04-19 18:12:33 -07005577 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
5578 new_skb = skb_copy(skb, GFP_ATOMIC);
5579 else {
5580 int more_headroom = 4 - ((unsigned long)skb->data & 3);
5581
5582 new_skb = skb_copy_expand(skb,
5583 skb_headroom(skb) + more_headroom,
5584 skb_tailroom(skb), GFP_ATOMIC);
5585 }
5586
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07005588 ret = -1;
5589 } else {
5590 /* New SKB is guaranteed to be linear. */
5591 entry = *start;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005592 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
5593 PCI_DMA_TODEVICE);
5594 /* Make sure the mapping succeeded */
5595 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
5596 ret = -1;
5597 dev_kfree_skb(new_skb);
5598 new_skb = NULL;
David S. Miller90079ce2008-09-11 04:52:51 -07005599
Michael Chanc58ec932005-09-17 00:46:27 -07005600 /* Make sure new skb does not cross any 4G boundaries.
5601 * Drop the packet if it does.
5602 */
Alexander Duyckf4188d82009-12-02 16:48:38 +00005603 } else if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
5604 tg3_4g_overflow_test(new_addr, new_skb->len)) {
5605 pci_unmap_single(tp->pdev, new_addr, new_skb->len,
5606 PCI_DMA_TODEVICE);
Michael Chanc58ec932005-09-17 00:46:27 -07005607 ret = -1;
5608 dev_kfree_skb(new_skb);
5609 new_skb = NULL;
5610 } else {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005611 tg3_set_txd(tnapi, entry, new_addr, new_skb->len,
Michael Chanc58ec932005-09-17 00:46:27 -07005612 base_flags, 1 | (mss << 1));
5613 *start = NEXT_TX(entry);
5614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615 }
5616
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617 /* Now clean up the sw ring entries. */
5618 i = 0;
5619 while (entry != last_plus_one) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00005620 int len;
5621
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005622 if (i == 0)
Alexander Duyckf4188d82009-12-02 16:48:38 +00005623 len = skb_headlen(skb);
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005624 else
Alexander Duyckf4188d82009-12-02 16:48:38 +00005625 len = skb_shinfo(skb)->frags[i-1].size;
5626
5627 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005628 dma_unmap_addr(&tnapi->tx_buffers[entry],
Alexander Duyckf4188d82009-12-02 16:48:38 +00005629 mapping),
5630 len, PCI_DMA_TODEVICE);
5631 if (i == 0) {
5632 tnapi->tx_buffers[entry].skb = new_skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005633 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00005634 new_addr);
5635 } else {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005636 tnapi->tx_buffers[entry].skb = NULL;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638 entry = NEXT_TX(entry);
5639 i++;
5640 }
5641
5642 dev_kfree_skb(skb);
5643
Michael Chanc58ec932005-09-17 00:46:27 -07005644 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645}
5646
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005647static void tg3_set_txd(struct tg3_napi *tnapi, int entry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648 dma_addr_t mapping, int len, u32 flags,
5649 u32 mss_and_is_end)
5650{
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005651 struct tg3_tx_buffer_desc *txd = &tnapi->tx_ring[entry];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652 int is_end = (mss_and_is_end & 0x1);
5653 u32 mss = (mss_and_is_end >> 1);
5654 u32 vlan_tag = 0;
5655
5656 if (is_end)
5657 flags |= TXD_FLAG_END;
5658 if (flags & TXD_FLAG_VLAN) {
5659 vlan_tag = flags >> 16;
5660 flags &= 0xffff;
5661 }
5662 vlan_tag |= (mss << TXD_MSS_SHIFT);
5663
5664 txd->addr_hi = ((u64) mapping >> 32);
5665 txd->addr_lo = ((u64) mapping & 0xffffffff);
5666 txd->len_flags = (len << TXD_LEN_SHIFT) | flags;
5667 txd->vlan_tag = vlan_tag << TXD_VLAN_TAG_SHIFT;
5668}
5669
Michael Chan5a6f3072006-03-20 22:28:05 -08005670/* hard_start_xmit for devices that don't have any bugs and
Matt Carlsone849cdc2009-11-13 13:03:38 +00005671 * support TG3_FLG2_HW_TSO_2 and TG3_FLG2_HW_TSO_3 only.
Michael Chan5a6f3072006-03-20 22:28:05 -08005672 */
Stephen Hemminger613573252009-08-31 19:50:58 +00005673static netdev_tx_t tg3_start_xmit(struct sk_buff *skb,
5674 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675{
5676 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677 u32 len, entry, base_flags, mss;
David S. Miller90079ce2008-09-11 04:52:51 -07005678 dma_addr_t mapping;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005679 struct tg3_napi *tnapi;
5680 struct netdev_queue *txq;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005681 unsigned int i, last;
5682
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005683 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
5684 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Matt Carlson19cfaec2009-12-03 08:36:20 +00005685 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005686 tnapi++;
Michael Chan5a6f3072006-03-20 22:28:05 -08005687
Michael Chan00b70502006-06-17 21:58:45 -07005688 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005689 * and TX reclaim runs via tp->napi.poll inside of a software
Michael Chan5a6f3072006-03-20 22:28:05 -08005690 * interrupt. Furthermore, IRQ processing runs lockless so we have
5691 * no IRQ context deadlocks to worry about either. Rejoice!
5692 */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005693 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005694 if (!netif_tx_queue_stopped(txq)) {
5695 netif_tx_stop_queue(txq);
Michael Chan5a6f3072006-03-20 22:28:05 -08005696
5697 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00005698 netdev_err(dev,
5699 "BUG! Tx Ring full when queue awake!\n");
Michael Chan5a6f3072006-03-20 22:28:05 -08005700 }
Michael Chan5a6f3072006-03-20 22:28:05 -08005701 return NETDEV_TX_BUSY;
5702 }
5703
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005704 entry = tnapi->tx_prod;
Michael Chan5a6f3072006-03-20 22:28:05 -08005705 base_flags = 0;
Matt Carlsonbe98da62010-07-11 09:31:46 +00005706 mss = skb_shinfo(skb)->gso_size;
5707 if (mss) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005708 int tcp_opt_len, ip_tcp_len;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005709 u32 hdrlen;
Michael Chan5a6f3072006-03-20 22:28:05 -08005710
5711 if (skb_header_cloned(skb) &&
5712 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
5713 dev_kfree_skb(skb);
5714 goto out_unlock;
5715 }
5716
Matt Carlson02e96082010-09-15 08:59:59 +00005717 if (skb_is_gso_v6(skb)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005718 hdrlen = skb_headlen(skb) - ETH_HLEN;
Matt Carlson02e96082010-09-15 08:59:59 +00005719 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005720 struct iphdr *iph = ip_hdr(skb);
5721
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07005722 tcp_opt_len = tcp_optlen(skb);
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -03005723 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
Michael Chanb0026622006-07-03 19:42:14 -07005724
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005725 iph->check = 0;
5726 iph->tot_len = htons(mss + ip_tcp_len + tcp_opt_len);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005727 hdrlen = ip_tcp_len + tcp_opt_len;
Michael Chanb0026622006-07-03 19:42:14 -07005728 }
Michael Chan5a6f3072006-03-20 22:28:05 -08005729
Matt Carlsone849cdc2009-11-13 13:03:38 +00005730 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005731 mss |= (hdrlen & 0xc) << 12;
5732 if (hdrlen & 0x10)
5733 base_flags |= 0x00000010;
5734 base_flags |= (hdrlen & 0x3e0) << 5;
5735 } else
5736 mss |= hdrlen << 9;
5737
Michael Chan5a6f3072006-03-20 22:28:05 -08005738 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
5739 TXD_FLAG_CPU_POST_DMA);
5740
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005741 tcp_hdr(skb)->check = 0;
Michael Chan5a6f3072006-03-20 22:28:05 -08005742
Matt Carlson859a588792010-04-05 10:19:28 +00005743 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005744 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson859a588792010-04-05 10:19:28 +00005745 }
5746
Michael Chan5a6f3072006-03-20 22:28:05 -08005747#if TG3_VLAN_TAG_USED
5748 if (tp->vlgrp != NULL && vlan_tx_tag_present(skb))
5749 base_flags |= (TXD_FLAG_VLAN |
5750 (vlan_tx_tag_get(skb) << 16));
5751#endif
5752
Alexander Duyckf4188d82009-12-02 16:48:38 +00005753 len = skb_headlen(skb);
5754
5755 /* Queue skb data, a.k.a. the main skb fragment. */
5756 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
5757 if (pci_dma_mapping_error(tp->pdev, mapping)) {
David S. Miller90079ce2008-09-11 04:52:51 -07005758 dev_kfree_skb(skb);
5759 goto out_unlock;
5760 }
5761
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005762 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005763 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005764
Matt Carlsonb703df62009-12-03 08:36:21 +00005765 if ((tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005766 !mss && skb->len > ETH_DATA_LEN)
5767 base_flags |= TXD_FLAG_JMB_PKT;
5768
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005769 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
Michael Chan5a6f3072006-03-20 22:28:05 -08005770 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
5771
5772 entry = NEXT_TX(entry);
5773
5774 /* Now loop through additional data fragments, and queue them. */
5775 if (skb_shinfo(skb)->nr_frags > 0) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005776 last = skb_shinfo(skb)->nr_frags - 1;
5777 for (i = 0; i <= last; i++) {
5778 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5779
5780 len = frag->size;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005781 mapping = pci_map_page(tp->pdev,
5782 frag->page,
5783 frag->page_offset,
5784 len, PCI_DMA_TODEVICE);
5785 if (pci_dma_mapping_error(tp->pdev, mapping))
5786 goto dma_error;
5787
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005788 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005789 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00005790 mapping);
Michael Chan5a6f3072006-03-20 22:28:05 -08005791
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005792 tg3_set_txd(tnapi, entry, mapping, len,
Michael Chan5a6f3072006-03-20 22:28:05 -08005793 base_flags, (i == last) | (mss << 1));
5794
5795 entry = NEXT_TX(entry);
5796 }
5797 }
5798
5799 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005800 tw32_tx_mbox(tnapi->prodmbox, entry);
Michael Chan5a6f3072006-03-20 22:28:05 -08005801
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005802 tnapi->tx_prod = entry;
5803 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005804 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00005805
5806 /* netif_tx_stop_queue() must be done before checking
5807 * checking tx index in tg3_tx_avail() below, because in
5808 * tg3_tx(), we update tx index before checking for
5809 * netif_tx_queue_stopped().
5810 */
5811 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005812 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005813 netif_tx_wake_queue(txq);
Michael Chan5a6f3072006-03-20 22:28:05 -08005814 }
5815
5816out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00005817 mmiowb();
Michael Chan5a6f3072006-03-20 22:28:05 -08005818
5819 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005820
5821dma_error:
5822 last = i;
5823 entry = tnapi->tx_prod;
5824 tnapi->tx_buffers[entry].skb = NULL;
5825 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005826 dma_unmap_addr(&tnapi->tx_buffers[entry], mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005827 skb_headlen(skb),
5828 PCI_DMA_TODEVICE);
5829 for (i = 0; i <= last; i++) {
5830 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5831 entry = NEXT_TX(entry);
5832
5833 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005834 dma_unmap_addr(&tnapi->tx_buffers[entry],
Alexander Duyckf4188d82009-12-02 16:48:38 +00005835 mapping),
5836 frag->size, PCI_DMA_TODEVICE);
5837 }
5838
5839 dev_kfree_skb(skb);
5840 return NETDEV_TX_OK;
Michael Chan5a6f3072006-03-20 22:28:05 -08005841}
5842
Stephen Hemminger613573252009-08-31 19:50:58 +00005843static netdev_tx_t tg3_start_xmit_dma_bug(struct sk_buff *,
5844 struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07005845
5846/* Use GSO to workaround a rare TSO bug that may be triggered when the
5847 * TSO header is greater than 80 bytes.
5848 */
5849static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
5850{
5851 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005852 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07005853
5854 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005855 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07005856 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00005857
5858 /* netif_tx_stop_queue() must be done before checking
5859 * checking tx index in tg3_tx_avail() below, because in
5860 * tg3_tx(), we update tx index before checking for
5861 * netif_tx_queue_stopped().
5862 */
5863 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005864 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08005865 return NETDEV_TX_BUSY;
5866
5867 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07005868 }
5869
5870 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07005871 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07005872 goto tg3_tso_bug_end;
5873
5874 do {
5875 nskb = segs;
5876 segs = segs->next;
5877 nskb->next = NULL;
5878 tg3_start_xmit_dma_bug(nskb, tp->dev);
5879 } while (segs);
5880
5881tg3_tso_bug_end:
5882 dev_kfree_skb(skb);
5883
5884 return NETDEV_TX_OK;
5885}
Michael Chan52c0fd82006-06-29 20:15:54 -07005886
Michael Chan5a6f3072006-03-20 22:28:05 -08005887/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
5888 * support TG3_FLG2_HW_TSO_1 or firmware TSO only.
5889 */
Stephen Hemminger613573252009-08-31 19:50:58 +00005890static netdev_tx_t tg3_start_xmit_dma_bug(struct sk_buff *skb,
5891 struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08005892{
5893 struct tg3 *tp = netdev_priv(dev);
Michael Chan5a6f3072006-03-20 22:28:05 -08005894 u32 len, entry, base_flags, mss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005895 int would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07005896 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00005897 struct tg3_napi *tnapi;
5898 struct netdev_queue *txq;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005899 unsigned int i, last;
5900
Matt Carlson24f4efd2009-11-13 13:03:35 +00005901 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
5902 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Matt Carlson19cfaec2009-12-03 08:36:20 +00005903 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlson24f4efd2009-11-13 13:03:35 +00005904 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905
Michael Chan00b70502006-06-17 21:58:45 -07005906 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005907 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07005908 * interrupt. Furthermore, IRQ processing runs lockless so we have
5909 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07005910 */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005911 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00005912 if (!netif_tx_queue_stopped(txq)) {
5913 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08005914
5915 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00005916 netdev_err(dev,
5917 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08005918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005919 return NETDEV_TX_BUSY;
5920 }
5921
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005922 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07005924 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005925 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00005926
Matt Carlsonbe98da62010-07-11 09:31:46 +00005927 mss = skb_shinfo(skb)->gso_size;
5928 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005929 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00005930 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005931
5932 if (skb_header_cloned(skb) &&
5933 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
5934 dev_kfree_skb(skb);
5935 goto out_unlock;
5936 }
5937
Matt Carlson34195c32010-07-11 09:31:42 +00005938 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07005939 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005940
Matt Carlson02e96082010-09-15 08:59:59 +00005941 if (skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00005942 hdr_len = skb_headlen(skb) - ETH_HLEN;
5943 } else {
5944 u32 ip_tcp_len;
5945
5946 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
5947 hdr_len = ip_tcp_len + tcp_opt_len;
5948
5949 iph->check = 0;
5950 iph->tot_len = htons(mss + hdr_len);
5951 }
5952
Michael Chan52c0fd82006-06-29 20:15:54 -07005953 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Michael Chan7f62ad52007-02-20 23:25:40 -08005954 (tp->tg3_flags2 & TG3_FLG2_TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00005955 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07005956
Linus Torvalds1da177e2005-04-16 15:20:36 -07005957 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
5958 TXD_FLAG_CPU_POST_DMA);
5959
Linus Torvalds1da177e2005-04-16 15:20:36 -07005960 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005961 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005962 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005963 } else
5964 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
5965 iph->daddr, 0,
5966 IPPROTO_TCP,
5967 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005968
Matt Carlson615774f2009-11-13 13:03:39 +00005969 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) {
5970 mss |= (hdr_len & 0xc) << 12;
5971 if (hdr_len & 0x10)
5972 base_flags |= 0x00000010;
5973 base_flags |= (hdr_len & 0x3e0) << 5;
5974 } else if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_2)
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005975 mss |= hdr_len << 9;
5976 else if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_1) ||
5977 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005978 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005979 int tsflags;
5980
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005981 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982 mss |= (tsflags << 11);
5983 }
5984 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005985 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986 int tsflags;
5987
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005988 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005989 base_flags |= tsflags << 12;
5990 }
5991 }
5992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993#if TG3_VLAN_TAG_USED
5994 if (tp->vlgrp != NULL && vlan_tx_tag_present(skb))
5995 base_flags |= (TXD_FLAG_VLAN |
5996 (vlan_tx_tag_get(skb) << 16));
5997#endif
5998
Matt Carlsonb703df62009-12-03 08:36:21 +00005999 if ((tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) &&
Matt Carlson615774f2009-11-13 13:03:39 +00006000 !mss && skb->len > ETH_DATA_LEN)
6001 base_flags |= TXD_FLAG_JMB_PKT;
6002
Alexander Duyckf4188d82009-12-02 16:48:38 +00006003 len = skb_headlen(skb);
6004
6005 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
6006 if (pci_dma_mapping_error(tp->pdev, mapping)) {
David S. Miller90079ce2008-09-11 04:52:51 -07006007 dev_kfree_skb(skb);
6008 goto out_unlock;
6009 }
6010
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006011 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006012 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006013
6014 would_hit_hwbug = 0;
6015
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006016 if ((tp->tg3_flags3 & TG3_FLG3_SHORT_DMA_BUG) && len <= 8)
6017 would_hit_hwbug = 1;
6018
Matt Carlson0e1406d2009-11-02 12:33:33 +00006019 if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
6020 tg3_4g_overflow_test(mapping, len))
Matt Carlson41588ba2008-04-19 18:12:33 -07006021 would_hit_hwbug = 1;
Matt Carlson0e1406d2009-11-02 12:33:33 +00006022
6023 if ((tp->tg3_flags3 & TG3_FLG3_40BIT_DMA_LIMIT_BUG) &&
6024 tg3_40bit_overflow_test(tp, mapping, len))
6025 would_hit_hwbug = 1;
6026
6027 if (tp->tg3_flags3 & TG3_FLG3_5701_DMA_BUG)
Michael Chanc58ec932005-09-17 00:46:27 -07006028 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006029
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006030 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006031 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
6032
6033 entry = NEXT_TX(entry);
6034
6035 /* Now loop through additional data fragments, and queue them. */
6036 if (skb_shinfo(skb)->nr_frags > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006037 last = skb_shinfo(skb)->nr_frags - 1;
6038 for (i = 0; i <= last; i++) {
6039 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6040
6041 len = frag->size;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006042 mapping = pci_map_page(tp->pdev,
6043 frag->page,
6044 frag->page_offset,
6045 len, PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006046
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006047 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006048 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00006049 mapping);
6050 if (pci_dma_mapping_error(tp->pdev, mapping))
6051 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006052
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006053 if ((tp->tg3_flags3 & TG3_FLG3_SHORT_DMA_BUG) &&
6054 len <= 8)
6055 would_hit_hwbug = 1;
6056
Matt Carlson0e1406d2009-11-02 12:33:33 +00006057 if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
6058 tg3_4g_overflow_test(mapping, len))
Michael Chanc58ec932005-09-17 00:46:27 -07006059 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006060
Matt Carlson0e1406d2009-11-02 12:33:33 +00006061 if ((tp->tg3_flags3 & TG3_FLG3_40BIT_DMA_LIMIT_BUG) &&
6062 tg3_40bit_overflow_test(tp, mapping, len))
Michael Chan72f2afb2006-03-06 19:28:35 -08006063 would_hit_hwbug = 1;
6064
Linus Torvalds1da177e2005-04-16 15:20:36 -07006065 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006066 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067 base_flags, (i == last)|(mss << 1));
6068 else
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006069 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070 base_flags, (i == last));
6071
6072 entry = NEXT_TX(entry);
6073 }
6074 }
6075
6076 if (would_hit_hwbug) {
6077 u32 last_plus_one = entry;
6078 u32 start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079
Michael Chanc58ec932005-09-17 00:46:27 -07006080 start = entry - 1 - skb_shinfo(skb)->nr_frags;
6081 start &= (TG3_TX_RING_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082
6083 /* If the workaround fails due to memory/mapping
6084 * failure, silently drop this packet.
6085 */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006086 if (tigon3_dma_hwbug_workaround(tnapi, skb, last_plus_one,
Michael Chanc58ec932005-09-17 00:46:27 -07006087 &start, base_flags, mss))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006088 goto out_unlock;
6089
6090 entry = start;
6091 }
6092
6093 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006094 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006096 tnapi->tx_prod = entry;
6097 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006098 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006099
6100 /* netif_tx_stop_queue() must be done before checking
6101 * checking tx index in tg3_tx_avail() below, because in
6102 * tg3_tx(), we update tx index before checking for
6103 * netif_tx_queue_stopped().
6104 */
6105 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006106 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006107 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07006108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006109
6110out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00006111 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006112
6113 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006114
6115dma_error:
6116 last = i;
6117 entry = tnapi->tx_prod;
6118 tnapi->tx_buffers[entry].skb = NULL;
6119 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006120 dma_unmap_addr(&tnapi->tx_buffers[entry], mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006121 skb_headlen(skb),
6122 PCI_DMA_TODEVICE);
6123 for (i = 0; i <= last; i++) {
6124 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6125 entry = NEXT_TX(entry);
6126
6127 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006128 dma_unmap_addr(&tnapi->tx_buffers[entry],
Alexander Duyckf4188d82009-12-02 16:48:38 +00006129 mapping),
6130 frag->size, PCI_DMA_TODEVICE);
6131 }
6132
6133 dev_kfree_skb(skb);
6134 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006135}
6136
6137static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
6138 int new_mtu)
6139{
6140 dev->mtu = new_mtu;
6141
Michael Chanef7f5ec2005-07-25 12:32:25 -07006142 if (new_mtu > ETH_DATA_LEN) {
Michael Chana4e2b342005-10-26 15:46:52 -07006143 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
Michael Chanef7f5ec2005-07-25 12:32:25 -07006144 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
6145 ethtool_op_set_tso(dev, 0);
Matt Carlson859a588792010-04-05 10:19:28 +00006146 } else {
Michael Chanef7f5ec2005-07-25 12:32:25 -07006147 tp->tg3_flags |= TG3_FLAG_JUMBO_RING_ENABLE;
Matt Carlson859a588792010-04-05 10:19:28 +00006148 }
Michael Chanef7f5ec2005-07-25 12:32:25 -07006149 } else {
Michael Chana4e2b342005-10-26 15:46:52 -07006150 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
Michael Chanef7f5ec2005-07-25 12:32:25 -07006151 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
Michael Chan0f893dc2005-07-25 12:30:38 -07006152 tp->tg3_flags &= ~TG3_FLAG_JUMBO_RING_ENABLE;
Michael Chanef7f5ec2005-07-25 12:32:25 -07006153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006154}
6155
6156static int tg3_change_mtu(struct net_device *dev, int new_mtu)
6157{
6158 struct tg3 *tp = netdev_priv(dev);
Michael Chanb9ec6c12006-07-25 16:37:27 -07006159 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006160
6161 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
6162 return -EINVAL;
6163
6164 if (!netif_running(dev)) {
6165 /* We'll just catch it later when the
6166 * device is up'd.
6167 */
6168 tg3_set_mtu(dev, tp, new_mtu);
6169 return 0;
6170 }
6171
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006172 tg3_phy_stop(tp);
6173
Linus Torvalds1da177e2005-04-16 15:20:36 -07006174 tg3_netif_stop(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07006175
6176 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006177
Michael Chan944d9802005-05-29 14:57:48 -07006178 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006179
6180 tg3_set_mtu(dev, tp, new_mtu);
6181
Michael Chanb9ec6c12006-07-25 16:37:27 -07006182 err = tg3_restart_hw(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006183
Michael Chanb9ec6c12006-07-25 16:37:27 -07006184 if (!err)
6185 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006186
David S. Millerf47c11e2005-06-24 20:18:35 -07006187 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006188
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006189 if (!err)
6190 tg3_phy_start(tp);
6191
Michael Chanb9ec6c12006-07-25 16:37:27 -07006192 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006193}
6194
Matt Carlson21f581a2009-08-28 14:00:25 +00006195static void tg3_rx_prodring_free(struct tg3 *tp,
6196 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006197{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006198 int i;
6199
Matt Carlson8fea32b2010-09-15 08:59:58 +00006200 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006201 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00006202 i = (i + 1) & tp->rx_std_ring_mask)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006203 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6204 tp->rx_pkt_map_sz);
6205
6206 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
6207 for (i = tpr->rx_jmb_cons_idx;
6208 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00006209 i = (i + 1) & tp->rx_jmb_ring_mask) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006210 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6211 TG3_RX_JMB_MAP_SZ);
6212 }
6213 }
6214
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006215 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217
Matt Carlson2c49a442010-09-30 10:34:35 +00006218 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006219 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6220 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006221
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006222 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006223 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006224 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6225 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006226 }
6227}
6228
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006229/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006230 *
6231 * The chip has been shut down and the driver detached from
6232 * the networking, so no interrupts or new tx packets will
6233 * end up in the driver. tp->{tx,}lock are held and thus
6234 * we may not sleep.
6235 */
Matt Carlson21f581a2009-08-28 14:00:25 +00006236static int tg3_rx_prodring_alloc(struct tg3 *tp,
6237 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006238{
Matt Carlson287be122009-08-28 13:58:46 +00006239 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006240
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006241 tpr->rx_std_cons_idx = 0;
6242 tpr->rx_std_prod_idx = 0;
6243 tpr->rx_jmb_cons_idx = 0;
6244 tpr->rx_jmb_prod_idx = 0;
6245
Matt Carlson8fea32b2010-09-15 08:59:58 +00006246 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006247 memset(&tpr->rx_std_buffers[0], 0,
6248 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006249 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE)
6250 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00006251 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006252 goto done;
6253 }
6254
Linus Torvalds1da177e2005-04-16 15:20:36 -07006255 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00006256 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006257
Matt Carlson287be122009-08-28 13:58:46 +00006258 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Michael Chana4e2b342005-10-26 15:46:52 -07006259 if ((tp->tg3_flags2 & TG3_FLG2_5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00006260 tp->dev->mtu > ETH_DATA_LEN)
6261 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
6262 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07006263
Linus Torvalds1da177e2005-04-16 15:20:36 -07006264 /* Initialize invariants of the rings, we only set this
6265 * stuff once. This works because the card does not
6266 * write into the rx buffer posting rings.
6267 */
Matt Carlson2c49a442010-09-30 10:34:35 +00006268 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006269 struct tg3_rx_buffer_desc *rxd;
6270
Matt Carlson21f581a2009-08-28 14:00:25 +00006271 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00006272 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006273 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
6274 rxd->opaque = (RXD_OPAQUE_RING_STD |
6275 (i << RXD_OPAQUE_INDEX_SHIFT));
6276 }
6277
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006278 /* Now allocate fresh SKBs for each rx ring. */
6279 for (i = 0; i < tp->rx_pending; i++) {
Matt Carlson86b21e52009-11-13 13:03:45 +00006280 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_STD, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006281 netdev_warn(tp->dev,
6282 "Using a smaller RX standard ring. Only "
6283 "%d out of %d buffers were allocated "
6284 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006285 if (i == 0)
6286 goto initfail;
6287 tp->rx_pending = i;
6288 break;
6289 }
6290 }
6291
6292 if (!(tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE))
6293 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
6364 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006365 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00006366 GFP_KERNEL);
6367 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006368 goto err_out;
6369
Matt Carlson21f581a2009-08-28 14:00:25 +00006370 tpr->rx_jmb = pci_alloc_consistent(tp->pdev,
Matt Carlson2c49a442010-09-30 10:34:35 +00006371 TG3_RX_JMB_RING_BYTES(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00006372 &tpr->rx_jmb_mapping);
6373 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006374 goto err_out;
6375 }
6376
6377 return 0;
6378
6379err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00006380 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006381 return -ENOMEM;
6382}
6383
6384/* Free up pending packets in all rx/tx rings.
6385 *
6386 * The chip has been shut down and the driver detached from
6387 * the networking, so no interrupts or new tx packets will
6388 * end up in the driver. tp->{tx,}lock is not held and we are not
6389 * in an interrupt context and thus may sleep.
6390 */
6391static void tg3_free_rings(struct tg3 *tp)
6392{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006393 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006394
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006395 for (j = 0; j < tp->irq_cnt; j++) {
6396 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006397
Matt Carlson8fea32b2010-09-15 08:59:58 +00006398 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00006399
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006400 if (!tnapi->tx_buffers)
6401 continue;
6402
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006403 for (i = 0; i < TG3_TX_RING_SIZE; ) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00006404 struct ring_info *txp;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006405 struct sk_buff *skb;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006406 unsigned int k;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006407
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006408 txp = &tnapi->tx_buffers[i];
6409 skb = txp->skb;
6410
6411 if (skb == NULL) {
6412 i++;
6413 continue;
6414 }
6415
Alexander Duyckf4188d82009-12-02 16:48:38 +00006416 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006417 dma_unmap_addr(txp, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006418 skb_headlen(skb),
6419 PCI_DMA_TODEVICE);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006420 txp->skb = NULL;
6421
Alexander Duyckf4188d82009-12-02 16:48:38 +00006422 i++;
6423
6424 for (k = 0; k < skb_shinfo(skb)->nr_frags; k++) {
6425 txp = &tnapi->tx_buffers[i & (TG3_TX_RING_SIZE - 1)];
6426 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006427 dma_unmap_addr(txp, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006428 skb_shinfo(skb)->frags[k].size,
6429 PCI_DMA_TODEVICE);
6430 i++;
6431 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006432
6433 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006434 }
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006435 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006436}
6437
6438/* Initialize tx/rx rings for packet processing.
6439 *
6440 * The chip has been shut down and the driver detached from
6441 * the networking, so no interrupts or new tx packets will
6442 * end up in the driver. tp->{tx,}lock are held and thus
6443 * we may not sleep.
6444 */
6445static int tg3_init_rings(struct tg3 *tp)
6446{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006447 int i;
Matt Carlson72334482009-08-28 14:03:01 +00006448
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006449 /* Free up all the SKBs. */
6450 tg3_free_rings(tp);
6451
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006452 for (i = 0; i < tp->irq_cnt; i++) {
6453 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006454
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006455 tnapi->last_tag = 0;
6456 tnapi->last_irq_tag = 0;
6457 tnapi->hw_status->status = 0;
6458 tnapi->hw_status->status_tag = 0;
6459 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6460
6461 tnapi->tx_prod = 0;
6462 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006463 if (tnapi->tx_ring)
6464 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006465
6466 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006467 if (tnapi->rx_rcb)
6468 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006469
Matt Carlson8fea32b2010-09-15 08:59:58 +00006470 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00006471 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006472 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006473 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006474 }
Matt Carlson72334482009-08-28 14:03:01 +00006475
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006476 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006477}
6478
6479/*
6480 * Must not be invoked with interrupt sources disabled and
6481 * the hardware shutdown down.
6482 */
6483static void tg3_free_consistent(struct tg3 *tp)
6484{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006485 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006486
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006487 for (i = 0; i < tp->irq_cnt; i++) {
6488 struct tg3_napi *tnapi = &tp->napi[i];
6489
6490 if (tnapi->tx_ring) {
6491 pci_free_consistent(tp->pdev, TG3_TX_RING_BYTES,
6492 tnapi->tx_ring, tnapi->tx_desc_mapping);
6493 tnapi->tx_ring = NULL;
6494 }
6495
6496 kfree(tnapi->tx_buffers);
6497 tnapi->tx_buffers = NULL;
6498
6499 if (tnapi->rx_rcb) {
6500 pci_free_consistent(tp->pdev, TG3_RX_RCB_RING_BYTES(tp),
6501 tnapi->rx_rcb,
6502 tnapi->rx_rcb_mapping);
6503 tnapi->rx_rcb = NULL;
6504 }
6505
Matt Carlson8fea32b2010-09-15 08:59:58 +00006506 tg3_rx_prodring_fini(tp, &tnapi->prodring);
6507
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006508 if (tnapi->hw_status) {
6509 pci_free_consistent(tp->pdev, TG3_HW_STATUS_SIZE,
6510 tnapi->hw_status,
6511 tnapi->status_mapping);
6512 tnapi->hw_status = NULL;
6513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006514 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006515
Linus Torvalds1da177e2005-04-16 15:20:36 -07006516 if (tp->hw_stats) {
6517 pci_free_consistent(tp->pdev, sizeof(struct tg3_hw_stats),
6518 tp->hw_stats, tp->stats_mapping);
6519 tp->hw_stats = NULL;
6520 }
6521}
6522
6523/*
6524 * Must not be invoked with interrupt sources disabled and
6525 * the hardware shutdown down. Can sleep.
6526 */
6527static int tg3_alloc_consistent(struct tg3 *tp)
6528{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006529 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006530
Linus Torvalds1da177e2005-04-16 15:20:36 -07006531 tp->hw_stats = pci_alloc_consistent(tp->pdev,
6532 sizeof(struct tg3_hw_stats),
6533 &tp->stats_mapping);
6534 if (!tp->hw_stats)
6535 goto err_out;
6536
Linus Torvalds1da177e2005-04-16 15:20:36 -07006537 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6538
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006539 for (i = 0; i < tp->irq_cnt; i++) {
6540 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006541 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006542
6543 tnapi->hw_status = pci_alloc_consistent(tp->pdev,
6544 TG3_HW_STATUS_SIZE,
6545 &tnapi->status_mapping);
6546 if (!tnapi->hw_status)
6547 goto err_out;
6548
6549 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006550 sblk = tnapi->hw_status;
6551
Matt Carlson8fea32b2010-09-15 08:59:58 +00006552 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
6553 goto err_out;
6554
Matt Carlson19cfaec2009-12-03 08:36:20 +00006555 /* If multivector TSS is enabled, vector 0 does not handle
6556 * tx interrupts. Don't allocate any resources for it.
6557 */
6558 if ((!i && !(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)) ||
6559 (i && (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS))) {
6560 tnapi->tx_buffers = kzalloc(sizeof(struct ring_info) *
6561 TG3_TX_RING_SIZE,
6562 GFP_KERNEL);
6563 if (!tnapi->tx_buffers)
6564 goto err_out;
6565
6566 tnapi->tx_ring = pci_alloc_consistent(tp->pdev,
6567 TG3_TX_RING_BYTES,
6568 &tnapi->tx_desc_mapping);
6569 if (!tnapi->tx_ring)
6570 goto err_out;
6571 }
6572
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006573 /*
6574 * When RSS is enabled, the status block format changes
6575 * slightly. The "rx_jumbo_consumer", "reserved",
6576 * and "rx_mini_consumer" members get mapped to the
6577 * other three rx return ring producer indexes.
6578 */
6579 switch (i) {
6580 default:
6581 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
6582 break;
6583 case 2:
6584 tnapi->rx_rcb_prod_idx = &sblk->rx_jumbo_consumer;
6585 break;
6586 case 3:
6587 tnapi->rx_rcb_prod_idx = &sblk->reserved;
6588 break;
6589 case 4:
6590 tnapi->rx_rcb_prod_idx = &sblk->rx_mini_consumer;
6591 break;
6592 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006593
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006594 /*
6595 * If multivector RSS is enabled, vector 0 does not handle
6596 * rx or tx interrupts. Don't allocate any resources for it.
6597 */
6598 if (!i && (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS))
6599 continue;
6600
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006601 tnapi->rx_rcb = pci_alloc_consistent(tp->pdev,
6602 TG3_RX_RCB_RING_BYTES(tp),
6603 &tnapi->rx_rcb_mapping);
6604 if (!tnapi->rx_rcb)
6605 goto err_out;
6606
6607 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006608 }
6609
Linus Torvalds1da177e2005-04-16 15:20:36 -07006610 return 0;
6611
6612err_out:
6613 tg3_free_consistent(tp);
6614 return -ENOMEM;
6615}
6616
6617#define MAX_WAIT_CNT 1000
6618
6619/* To stop a block, clear the enable bit and poll till it
6620 * clears. tp->lock is held.
6621 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006622static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006623{
6624 unsigned int i;
6625 u32 val;
6626
6627 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
6628 switch (ofs) {
6629 case RCVLSC_MODE:
6630 case DMAC_MODE:
6631 case MBFREE_MODE:
6632 case BUFMGR_MODE:
6633 case MEMARB_MODE:
6634 /* We can't enable/disable these bits of the
6635 * 5705/5750, just say success.
6636 */
6637 return 0;
6638
6639 default:
6640 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006642 }
6643
6644 val = tr32(ofs);
6645 val &= ~enable_bit;
6646 tw32_f(ofs, val);
6647
6648 for (i = 0; i < MAX_WAIT_CNT; i++) {
6649 udelay(100);
6650 val = tr32(ofs);
6651 if ((val & enable_bit) == 0)
6652 break;
6653 }
6654
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006655 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00006656 dev_err(&tp->pdev->dev,
6657 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
6658 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006659 return -ENODEV;
6660 }
6661
6662 return 0;
6663}
6664
6665/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006666static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006667{
6668 int i, err;
6669
6670 tg3_disable_ints(tp);
6671
6672 tp->rx_mode &= ~RX_MODE_ENABLE;
6673 tw32_f(MAC_RX_MODE, tp->rx_mode);
6674 udelay(10);
6675
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006676 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
6677 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
6678 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
6679 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
6680 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
6681 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006682
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006683 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
6684 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
6685 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
6686 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
6687 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
6688 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
6689 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006690
6691 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
6692 tw32_f(MAC_MODE, tp->mac_mode);
6693 udelay(40);
6694
6695 tp->tx_mode &= ~TX_MODE_ENABLE;
6696 tw32_f(MAC_TX_MODE, tp->tx_mode);
6697
6698 for (i = 0; i < MAX_WAIT_CNT; i++) {
6699 udelay(100);
6700 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
6701 break;
6702 }
6703 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00006704 dev_err(&tp->pdev->dev,
6705 "%s timed out, TX_MODE_ENABLE will not clear "
6706 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07006707 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006708 }
6709
Michael Chane6de8ad2005-05-05 14:42:41 -07006710 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006711 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
6712 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006713
6714 tw32(FTQ_RESET, 0xffffffff);
6715 tw32(FTQ_RESET, 0x00000000);
6716
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006717 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
6718 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006719
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006720 for (i = 0; i < tp->irq_cnt; i++) {
6721 struct tg3_napi *tnapi = &tp->napi[i];
6722 if (tnapi->hw_status)
6723 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006725 if (tp->hw_stats)
6726 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6727
Linus Torvalds1da177e2005-04-16 15:20:36 -07006728 return err;
6729}
6730
Matt Carlson0d3031d2007-10-10 18:02:43 -07006731static void tg3_ape_send_event(struct tg3 *tp, u32 event)
6732{
6733 int i;
6734 u32 apedata;
6735
Matt Carlsondc6d0742010-09-15 08:59:55 +00006736 /* NCSI does not support APE events */
6737 if (tp->tg3_flags3 & TG3_FLG3_APE_HAS_NCSI)
6738 return;
6739
Matt Carlson0d3031d2007-10-10 18:02:43 -07006740 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
6741 if (apedata != APE_SEG_SIG_MAGIC)
6742 return;
6743
6744 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
Matt Carlson731fd792008-08-15 14:07:51 -07006745 if (!(apedata & APE_FW_STATUS_READY))
Matt Carlson0d3031d2007-10-10 18:02:43 -07006746 return;
6747
6748 /* Wait for up to 1 millisecond for APE to service previous event. */
6749 for (i = 0; i < 10; i++) {
6750 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
6751 return;
6752
6753 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
6754
6755 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6756 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
6757 event | APE_EVENT_STATUS_EVENT_PENDING);
6758
6759 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
6760
6761 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6762 break;
6763
6764 udelay(100);
6765 }
6766
6767 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6768 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
6769}
6770
6771static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
6772{
6773 u32 event;
6774 u32 apedata;
6775
6776 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
6777 return;
6778
6779 switch (kind) {
Matt Carlson33f401a2010-04-05 10:19:27 +00006780 case RESET_KIND_INIT:
6781 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
6782 APE_HOST_SEG_SIG_MAGIC);
6783 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
6784 APE_HOST_SEG_LEN_MAGIC);
6785 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
6786 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
6787 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
Matt Carlson6867c842010-07-11 09:31:44 +00006788 APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
Matt Carlson33f401a2010-04-05 10:19:27 +00006789 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
6790 APE_HOST_BEHAV_NO_PHYLOCK);
Matt Carlsondc6d0742010-09-15 08:59:55 +00006791 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
6792 TG3_APE_HOST_DRVR_STATE_START);
Matt Carlson0d3031d2007-10-10 18:02:43 -07006793
Matt Carlson33f401a2010-04-05 10:19:27 +00006794 event = APE_EVENT_STATUS_STATE_START;
6795 break;
6796 case RESET_KIND_SHUTDOWN:
6797 /* With the interface we are currently using,
6798 * APE does not track driver state. Wiping
6799 * out the HOST SEGMENT SIGNATURE forces
6800 * the APE to assume OS absent status.
6801 */
6802 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
Matt Carlsonb2aee152008-11-03 16:51:11 -08006803
Matt Carlsondc6d0742010-09-15 08:59:55 +00006804 if (device_may_wakeup(&tp->pdev->dev) &&
6805 (tp->tg3_flags & TG3_FLAG_WOL_ENABLE)) {
6806 tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
6807 TG3_APE_HOST_WOL_SPEED_AUTO);
6808 apedata = TG3_APE_HOST_DRVR_STATE_WOL;
6809 } else
6810 apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
6811
6812 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
6813
Matt Carlson33f401a2010-04-05 10:19:27 +00006814 event = APE_EVENT_STATUS_STATE_UNLOAD;
6815 break;
6816 case RESET_KIND_SUSPEND:
6817 event = APE_EVENT_STATUS_STATE_SUSPEND;
6818 break;
6819 default:
6820 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -07006821 }
6822
6823 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
6824
6825 tg3_ape_send_event(tp, event);
6826}
6827
Michael Chane6af3012005-04-21 17:12:05 -07006828/* tp->lock is held. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006829static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
6830{
David S. Millerf49639e2006-06-09 11:58:36 -07006831 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
6832 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006833
6834 if (tp->tg3_flags2 & TG3_FLG2_ASF_NEW_HANDSHAKE) {
6835 switch (kind) {
6836 case RESET_KIND_INIT:
6837 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6838 DRV_STATE_START);
6839 break;
6840
6841 case RESET_KIND_SHUTDOWN:
6842 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6843 DRV_STATE_UNLOAD);
6844 break;
6845
6846 case RESET_KIND_SUSPEND:
6847 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6848 DRV_STATE_SUSPEND);
6849 break;
6850
6851 default:
6852 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006854 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07006855
6856 if (kind == RESET_KIND_INIT ||
6857 kind == RESET_KIND_SUSPEND)
6858 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006859}
6860
6861/* tp->lock is held. */
6862static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
6863{
6864 if (tp->tg3_flags2 & TG3_FLG2_ASF_NEW_HANDSHAKE) {
6865 switch (kind) {
6866 case RESET_KIND_INIT:
6867 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6868 DRV_STATE_START_DONE);
6869 break;
6870
6871 case RESET_KIND_SHUTDOWN:
6872 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6873 DRV_STATE_UNLOAD_DONE);
6874 break;
6875
6876 default:
6877 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006878 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006879 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07006880
6881 if (kind == RESET_KIND_SHUTDOWN)
6882 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006883}
6884
6885/* tp->lock is held. */
6886static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
6887{
6888 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) {
6889 switch (kind) {
6890 case RESET_KIND_INIT:
6891 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6892 DRV_STATE_START);
6893 break;
6894
6895 case RESET_KIND_SHUTDOWN:
6896 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6897 DRV_STATE_UNLOAD);
6898 break;
6899
6900 case RESET_KIND_SUSPEND:
6901 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6902 DRV_STATE_SUSPEND);
6903 break;
6904
6905 default:
6906 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006908 }
6909}
6910
Michael Chan7a6f4362006-09-27 16:03:31 -07006911static int tg3_poll_fw(struct tg3 *tp)
6912{
6913 int i;
6914 u32 val;
6915
Michael Chanb5d37722006-09-27 16:06:21 -07006916 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Gary Zambrano0ccead12006-11-14 16:34:00 -08006917 /* Wait up to 20ms for init done. */
6918 for (i = 0; i < 200; i++) {
Michael Chanb5d37722006-09-27 16:06:21 -07006919 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
6920 return 0;
Gary Zambrano0ccead12006-11-14 16:34:00 -08006921 udelay(100);
Michael Chanb5d37722006-09-27 16:06:21 -07006922 }
6923 return -ENODEV;
6924 }
6925
Michael Chan7a6f4362006-09-27 16:03:31 -07006926 /* Wait for firmware initialization to complete. */
6927 for (i = 0; i < 100000; i++) {
6928 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
6929 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
6930 break;
6931 udelay(10);
6932 }
6933
6934 /* Chip might not be fitted with firmware. Some Sun onboard
6935 * parts are configured like that. So don't signal the timeout
6936 * of the above loop as an error, but do report the lack of
6937 * running firmware once.
6938 */
6939 if (i >= 100000 &&
6940 !(tp->tg3_flags2 & TG3_FLG2_NO_FWARE_REPORTED)) {
6941 tp->tg3_flags2 |= TG3_FLG2_NO_FWARE_REPORTED;
6942
Joe Perches05dbe002010-02-17 19:44:19 +00006943 netdev_info(tp->dev, "No firmware running\n");
Michael Chan7a6f4362006-09-27 16:03:31 -07006944 }
6945
Matt Carlson6b10c162010-02-12 14:47:08 +00006946 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
6947 /* The 57765 A0 needs a little more
6948 * time to do some important work.
6949 */
6950 mdelay(10);
6951 }
6952
Michael Chan7a6f4362006-09-27 16:03:31 -07006953 return 0;
6954}
6955
Michael Chanee6a99b2007-07-18 21:49:10 -07006956/* Save PCI command register before chip reset */
6957static void tg3_save_pci_state(struct tg3 *tp)
6958{
Matt Carlson8a6eac92007-10-21 16:17:55 -07006959 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07006960}
6961
6962/* Restore PCI state after chip reset */
6963static void tg3_restore_pci_state(struct tg3 *tp)
6964{
6965 u32 val;
6966
6967 /* Re-enable indirect register accesses. */
6968 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
6969 tp->misc_host_ctrl);
6970
6971 /* Set MAX PCI retry to zero. */
6972 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
6973 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
6974 (tp->tg3_flags & TG3_FLAG_PCIX_MODE))
6975 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07006976 /* Allow reads and writes to the APE register and memory space. */
6977 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
6978 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00006979 PCISTATE_ALLOW_APE_SHMEM_WR |
6980 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07006981 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
6982
Matt Carlson8a6eac92007-10-21 16:17:55 -07006983 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07006984
Matt Carlsonfcb389d2008-11-03 16:55:44 -08006985 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785) {
6986 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)
6987 pcie_set_readrq(tp->pdev, 4096);
6988 else {
6989 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
6990 tp->pci_cacheline_sz);
6991 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
6992 tp->pci_lat_timer);
6993 }
Michael Chan114342f2007-10-15 02:12:26 -07006994 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08006995
Michael Chanee6a99b2007-07-18 21:49:10 -07006996 /* Make sure PCI-X relaxed ordering bit is clear. */
Matt Carlson52f44902008-11-21 17:17:04 -08006997 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
Matt Carlson9974a352007-10-07 23:27:28 -07006998 u16 pcix_cmd;
6999
7000 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7001 &pcix_cmd);
7002 pcix_cmd &= ~PCI_X_CMD_ERO;
7003 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7004 pcix_cmd);
7005 }
Michael Chanee6a99b2007-07-18 21:49:10 -07007006
7007 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007008
7009 /* Chip reset on 5780 will reset MSI enable bit,
7010 * so need to restore it.
7011 */
7012 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
7013 u16 ctrl;
7014
7015 pci_read_config_word(tp->pdev,
7016 tp->msi_cap + PCI_MSI_FLAGS,
7017 &ctrl);
7018 pci_write_config_word(tp->pdev,
7019 tp->msi_cap + PCI_MSI_FLAGS,
7020 ctrl | PCI_MSI_FLAGS_ENABLE);
7021 val = tr32(MSGINT_MODE);
7022 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
7023 }
7024 }
7025}
7026
Linus Torvalds1da177e2005-04-16 15:20:36 -07007027static void tg3_stop_fw(struct tg3 *);
7028
7029/* tp->lock is held. */
7030static int tg3_chip_reset(struct tg3 *tp)
7031{
7032 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07007033 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00007034 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007035
David S. Millerf49639e2006-06-09 11:58:36 -07007036 tg3_nvram_lock(tp);
7037
Matt Carlson77b483f2008-08-15 14:07:24 -07007038 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
7039
David S. Millerf49639e2006-06-09 11:58:36 -07007040 /* No matching tg3_nvram_unlock() after this because
7041 * chip reset below will undo the nvram lock.
7042 */
7043 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007044
Michael Chanee6a99b2007-07-18 21:49:10 -07007045 /* GRC_MISC_CFG core clock reset will clear the memory
7046 * enable bit in PCI register 4 and the MSI enable bit
7047 * on some chips, so we save relevant registers here.
7048 */
7049 tg3_save_pci_state(tp);
7050
Michael Chand9ab5ad2006-03-20 22:27:35 -08007051 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08007052 (tp->tg3_flags3 & TG3_FLG3_5755_PLUS))
Michael Chand9ab5ad2006-03-20 22:27:35 -08007053 tw32(GRC_FASTBOOT_PC, 0);
7054
Linus Torvalds1da177e2005-04-16 15:20:36 -07007055 /*
7056 * We must avoid the readl() that normally takes place.
7057 * It locks machines, causes machine checks, and other
7058 * fun things. So, temporarily disable the 5701
7059 * hardware workaround, while we do the reset.
7060 */
Michael Chan1ee582d2005-08-09 20:16:46 -07007061 write_op = tp->write32;
7062 if (write_op == tg3_write_flush_reg32)
7063 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007064
Michael Chand18edcb2007-03-24 20:57:11 -07007065 /* Prevent the irq handler from reading or writing PCI registers
7066 * during chip reset when the memory enable bit in the PCI command
7067 * register may be cleared. The chip does not generate interrupt
7068 * at this time, but the irq handler may still be called due to irq
7069 * sharing or irqpoll.
7070 */
7071 tp->tg3_flags |= TG3_FLAG_CHIP_RESETTING;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007072 for (i = 0; i < tp->irq_cnt; i++) {
7073 struct tg3_napi *tnapi = &tp->napi[i];
7074 if (tnapi->hw_status) {
7075 tnapi->hw_status->status = 0;
7076 tnapi->hw_status->status_tag = 0;
7077 }
7078 tnapi->last_tag = 0;
7079 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07007080 }
Michael Chand18edcb2007-03-24 20:57:11 -07007081 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00007082
7083 for (i = 0; i < tp->irq_cnt; i++)
7084 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07007085
Matt Carlson255ca312009-08-25 10:07:27 +00007086 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7087 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7088 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
7089 }
7090
Linus Torvalds1da177e2005-04-16 15:20:36 -07007091 /* do the reset */
7092 val = GRC_MISC_CFG_CORECLK_RESET;
7093
7094 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
Matt Carlson88075d92010-08-02 11:25:58 +00007095 /* Force PCIe 1.0a mode */
7096 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
7097 !(tp->tg3_flags3 & TG3_FLG3_5717_PLUS) &&
7098 tr32(TG3_PCIE_PHY_TSTCTL) ==
7099 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
7100 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
7101
Linus Torvalds1da177e2005-04-16 15:20:36 -07007102 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
7103 tw32(GRC_MISC_CFG, (1 << 29));
7104 val |= (1 << 29);
7105 }
7106 }
7107
Michael Chanb5d37722006-09-27 16:06:21 -07007108 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7109 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
7110 tw32(GRC_VCPU_EXT_CTRL,
7111 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
7112 }
7113
Matt Carlsonf37500d2010-08-02 11:25:59 +00007114 /* Manage gphy power for all CPMU absent PCIe devices. */
7115 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
7116 !(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007117 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00007118
Linus Torvalds1da177e2005-04-16 15:20:36 -07007119 tw32(GRC_MISC_CFG, val);
7120
Michael Chan1ee582d2005-08-09 20:16:46 -07007121 /* restore 5701 hardware bug workaround write method */
7122 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007123
7124 /* Unfortunately, we have to delay before the PCI read back.
7125 * Some 575X chips even will not respond to a PCI cfg access
7126 * when the reset command is given to the chip.
7127 *
7128 * How do these hardware designers expect things to work
7129 * properly if the PCI write is posted for a long period
7130 * of time? It is always necessary to have some method by
7131 * which a register read back can occur to push the write
7132 * out which does the reset.
7133 *
7134 * For most tg3 variants the trick below was working.
7135 * Ho hum...
7136 */
7137 udelay(120);
7138
7139 /* Flush PCI posted writes. The normal MMIO registers
7140 * are inaccessible at this time so this is the only
7141 * way to make this reliably (actually, this is no longer
7142 * the case, see above). I tried to use indirect
7143 * register read/write but this upset some 5701 variants.
7144 */
7145 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
7146
7147 udelay(120);
7148
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007149 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) && tp->pcie_cap) {
Matt Carlsone7126992009-08-25 10:08:16 +00007150 u16 val16;
7151
Linus Torvalds1da177e2005-04-16 15:20:36 -07007152 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
7153 int i;
7154 u32 cfg_val;
7155
7156 /* Wait for link training to complete. */
7157 for (i = 0; i < 5000; i++)
7158 udelay(100);
7159
7160 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
7161 pci_write_config_dword(tp->pdev, 0xc4,
7162 cfg_val | (1 << 15));
7163 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007164
Matt Carlsone7126992009-08-25 10:08:16 +00007165 /* Clear the "no snoop" and "relaxed ordering" bits. */
7166 pci_read_config_word(tp->pdev,
7167 tp->pcie_cap + PCI_EXP_DEVCTL,
7168 &val16);
7169 val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
7170 PCI_EXP_DEVCTL_NOSNOOP_EN);
7171 /*
7172 * Older PCIe devices only support the 128 byte
7173 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007174 */
Matt Carlson6de34cb2010-08-02 11:25:55 +00007175 if (!(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT))
Matt Carlsone7126992009-08-25 10:08:16 +00007176 val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007177 pci_write_config_word(tp->pdev,
7178 tp->pcie_cap + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007179 val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007180
7181 pcie_set_readrq(tp->pdev, 4096);
7182
7183 /* Clear error status */
7184 pci_write_config_word(tp->pdev,
7185 tp->pcie_cap + PCI_EXP_DEVSTA,
7186 PCI_EXP_DEVSTA_CED |
7187 PCI_EXP_DEVSTA_NFED |
7188 PCI_EXP_DEVSTA_FED |
7189 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007190 }
7191
Michael Chanee6a99b2007-07-18 21:49:10 -07007192 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007193
Michael Chand18edcb2007-03-24 20:57:11 -07007194 tp->tg3_flags &= ~TG3_FLAG_CHIP_RESETTING;
7195
Michael Chanee6a99b2007-07-18 21:49:10 -07007196 val = 0;
7197 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
Michael Chan4cf78e42005-07-25 12:29:19 -07007198 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07007199 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007200
7201 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
7202 tg3_stop_fw(tp);
7203 tw32(0x5000, 0x400);
7204 }
7205
7206 tw32(GRC_MODE, tp->grc_mode);
7207
7208 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007209 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007210
7211 tw32(0xc4, val | (1 << 15));
7212 }
7213
7214 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
7215 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
7216 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
7217 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
7218 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
7219 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7220 }
7221
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007222 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007223 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
7224 tw32_f(MAC_MODE, tp->mac_mode);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007225 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Michael Chan747e8f82005-07-25 12:33:22 -07007226 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
7227 tw32_f(MAC_MODE, tp->mac_mode);
Matt Carlson3bda1252008-08-15 14:08:22 -07007228 } else if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
7229 tp->mac_mode &= (MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN);
7230 if (tp->mac_mode & MAC_MODE_APE_TX_EN)
7231 tp->mac_mode |= MAC_MODE_TDE_ENABLE;
7232 tw32_f(MAC_MODE, tp->mac_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007233 } else
7234 tw32_f(MAC_MODE, 0);
7235 udelay(40);
7236
Matt Carlson77b483f2008-08-15 14:07:24 -07007237 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
7238
Michael Chan7a6f4362006-09-27 16:03:31 -07007239 err = tg3_poll_fw(tp);
7240 if (err)
7241 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007242
Matt Carlson0a9140c2009-08-28 12:27:50 +00007243 tg3_mdio_start(tp);
7244
Linus Torvalds1da177e2005-04-16 15:20:36 -07007245 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007246 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
7247 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonc885e822010-08-02 11:25:57 +00007248 !(tp->tg3_flags3 & TG3_FLG3_5717_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007249 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007250
7251 tw32(0x7c00, val | (1 << 25));
7252 }
7253
7254 /* Reprobe ASF enable state. */
7255 tp->tg3_flags &= ~TG3_FLAG_ENABLE_ASF;
7256 tp->tg3_flags2 &= ~TG3_FLG2_ASF_NEW_HANDSHAKE;
7257 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
7258 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
7259 u32 nic_cfg;
7260
7261 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
7262 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
7263 tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
Matt Carlson4ba526c2008-08-15 14:10:04 -07007264 tp->last_event_jiffies = jiffies;
John W. Linvillecbf46852005-04-21 17:01:29 -07007265 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007266 tp->tg3_flags2 |= TG3_FLG2_ASF_NEW_HANDSHAKE;
7267 }
7268 }
7269
7270 return 0;
7271}
7272
7273/* tp->lock is held. */
7274static void tg3_stop_fw(struct tg3 *tp)
7275{
Matt Carlson0d3031d2007-10-10 18:02:43 -07007276 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
7277 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07007278 /* Wait for RX cpu to ACK the previous event. */
7279 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007280
7281 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007282
7283 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007284
Matt Carlson7c5026a2008-05-02 16:49:29 -07007285 /* Wait for RX cpu to ACK this event. */
7286 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007287 }
7288}
7289
7290/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07007291static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007292{
7293 int err;
7294
7295 tg3_stop_fw(tp);
7296
Michael Chan944d9802005-05-29 14:57:48 -07007297 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007298
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007299 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007300 err = tg3_chip_reset(tp);
7301
Matt Carlsondaba2a62009-04-20 06:58:52 +00007302 __tg3_set_mac_addr(tp, 0);
7303
Michael Chan944d9802005-05-29 14:57:48 -07007304 tg3_write_sig_legacy(tp, kind);
7305 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007306
7307 if (err)
7308 return err;
7309
7310 return 0;
7311}
7312
Linus Torvalds1da177e2005-04-16 15:20:36 -07007313#define RX_CPU_SCRATCH_BASE 0x30000
7314#define RX_CPU_SCRATCH_SIZE 0x04000
7315#define TX_CPU_SCRATCH_BASE 0x34000
7316#define TX_CPU_SCRATCH_SIZE 0x04000
7317
7318/* tp->lock is held. */
7319static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
7320{
7321 int i;
7322
Eric Sesterhenn5d9428d2006-04-02 13:52:48 +02007323 BUG_ON(offset == TX_CPU_BASE &&
7324 (tp->tg3_flags2 & TG3_FLG2_5705_PLUS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007325
Michael Chanb5d37722006-09-27 16:06:21 -07007326 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7327 u32 val = tr32(GRC_VCPU_EXT_CTRL);
7328
7329 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
7330 return 0;
7331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007332 if (offset == RX_CPU_BASE) {
7333 for (i = 0; i < 10000; i++) {
7334 tw32(offset + CPU_STATE, 0xffffffff);
7335 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7336 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7337 break;
7338 }
7339
7340 tw32(offset + CPU_STATE, 0xffffffff);
7341 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
7342 udelay(10);
7343 } else {
7344 for (i = 0; i < 10000; i++) {
7345 tw32(offset + CPU_STATE, 0xffffffff);
7346 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7347 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7348 break;
7349 }
7350 }
7351
7352 if (i >= 10000) {
Joe Perches05dbe002010-02-17 19:44:19 +00007353 netdev_err(tp->dev, "%s timed out, %s CPU\n",
7354 __func__, offset == RX_CPU_BASE ? "RX" : "TX");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007355 return -ENODEV;
7356 }
Michael Chanec41c7d2006-01-17 02:40:55 -08007357
7358 /* Clear firmware's nvram arbitration. */
7359 if (tp->tg3_flags & TG3_FLAG_NVRAM)
7360 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007361 return 0;
7362}
7363
7364struct fw_info {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007365 unsigned int fw_base;
7366 unsigned int fw_len;
7367 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007368};
7369
7370/* tp->lock is held. */
7371static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base, u32 cpu_scratch_base,
7372 int cpu_scratch_size, struct fw_info *info)
7373{
Michael Chanec41c7d2006-01-17 02:40:55 -08007374 int err, lock_err, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007375 void (*write_op)(struct tg3 *, u32, u32);
7376
7377 if (cpu_base == TX_CPU_BASE &&
7378 (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007379 netdev_err(tp->dev,
7380 "%s: Trying to load TX cpu firmware which is 5705\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007381 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007382 return -EINVAL;
7383 }
7384
7385 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
7386 write_op = tg3_write_mem;
7387 else
7388 write_op = tg3_write_indirect_reg32;
7389
Michael Chan1b628152005-05-29 14:59:49 -07007390 /* It is possible that bootcode is still loading at this point.
7391 * Get the nvram lock first before halting the cpu.
7392 */
Michael Chanec41c7d2006-01-17 02:40:55 -08007393 lock_err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007394 err = tg3_halt_cpu(tp, cpu_base);
Michael Chanec41c7d2006-01-17 02:40:55 -08007395 if (!lock_err)
7396 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007397 if (err)
7398 goto out;
7399
7400 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
7401 write_op(tp, cpu_scratch_base + i, 0);
7402 tw32(cpu_base + CPU_STATE, 0xffffffff);
7403 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007404 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007405 write_op(tp, (cpu_scratch_base +
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007406 (info->fw_base & 0xffff) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07007407 (i * sizeof(u32))),
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007408 be32_to_cpu(info->fw_data[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007409
7410 err = 0;
7411
7412out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007413 return err;
7414}
7415
7416/* tp->lock is held. */
7417static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
7418{
7419 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007420 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007421 int err, i;
7422
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007423 fw_data = (void *)tp->fw->data;
7424
7425 /* Firmware blob starts with version numbers, followed by
7426 start address and length. We are setting complete length.
7427 length = end_address_of_bss - start_address_of_text.
7428 Remainder is the blob to be loaded contiguously
7429 from start address. */
7430
7431 info.fw_base = be32_to_cpu(fw_data[1]);
7432 info.fw_len = tp->fw->size - 12;
7433 info.fw_data = &fw_data[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007434
7435 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
7436 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
7437 &info);
7438 if (err)
7439 return err;
7440
7441 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
7442 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
7443 &info);
7444 if (err)
7445 return err;
7446
7447 /* Now startup only the RX cpu. */
7448 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007449 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007450
7451 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007452 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007453 break;
7454 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7455 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007456 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007457 udelay(1000);
7458 }
7459 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007460 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
7461 "should be %08x\n", __func__,
Joe Perches05dbe002010-02-17 19:44:19 +00007462 tr32(RX_CPU_BASE + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007463 return -ENODEV;
7464 }
7465 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7466 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
7467
7468 return 0;
7469}
7470
Linus Torvalds1da177e2005-04-16 15:20:36 -07007471/* 5705 needs a special version of the TSO firmware. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007472
7473/* tp->lock is held. */
7474static int tg3_load_tso_firmware(struct tg3 *tp)
7475{
7476 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007477 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007478 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
7479 int err, i;
7480
7481 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
7482 return 0;
7483
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007484 fw_data = (void *)tp->fw->data;
7485
7486 /* Firmware blob starts with version numbers, followed by
7487 start address and length. We are setting complete length.
7488 length = end_address_of_bss - start_address_of_text.
7489 Remainder is the blob to be loaded contiguously
7490 from start address. */
7491
7492 info.fw_base = be32_to_cpu(fw_data[1]);
7493 cpu_scratch_size = tp->fw_len;
7494 info.fw_len = tp->fw->size - 12;
7495 info.fw_data = &fw_data[3];
7496
Linus Torvalds1da177e2005-04-16 15:20:36 -07007497 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007498 cpu_base = RX_CPU_BASE;
7499 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007500 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007501 cpu_base = TX_CPU_BASE;
7502 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
7503 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
7504 }
7505
7506 err = tg3_load_firmware_cpu(tp, cpu_base,
7507 cpu_scratch_base, cpu_scratch_size,
7508 &info);
7509 if (err)
7510 return err;
7511
7512 /* Now startup the cpu. */
7513 tw32(cpu_base + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007514 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007515
7516 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007517 if (tr32(cpu_base + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007518 break;
7519 tw32(cpu_base + CPU_STATE, 0xffffffff);
7520 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007521 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007522 udelay(1000);
7523 }
7524 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007525 netdev_err(tp->dev,
7526 "%s fails to set CPU PC, is %08x should be %08x\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007527 __func__, tr32(cpu_base + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007528 return -ENODEV;
7529 }
7530 tw32(cpu_base + CPU_STATE, 0xffffffff);
7531 tw32_f(cpu_base + CPU_MODE, 0x00000000);
7532 return 0;
7533}
7534
Linus Torvalds1da177e2005-04-16 15:20:36 -07007535
Linus Torvalds1da177e2005-04-16 15:20:36 -07007536static int tg3_set_mac_addr(struct net_device *dev, void *p)
7537{
7538 struct tg3 *tp = netdev_priv(dev);
7539 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07007540 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007541
Michael Chanf9804dd2005-09-27 12:13:10 -07007542 if (!is_valid_ether_addr(addr->sa_data))
7543 return -EINVAL;
7544
Linus Torvalds1da177e2005-04-16 15:20:36 -07007545 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7546
Michael Chane75f7c92006-03-20 21:33:26 -08007547 if (!netif_running(dev))
7548 return 0;
7549
Michael Chan58712ef2006-04-29 18:58:01 -07007550 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) {
Michael Chan986e0ae2007-05-05 12:10:20 -07007551 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07007552
Michael Chan986e0ae2007-05-05 12:10:20 -07007553 addr0_high = tr32(MAC_ADDR_0_HIGH);
7554 addr0_low = tr32(MAC_ADDR_0_LOW);
7555 addr1_high = tr32(MAC_ADDR_1_HIGH);
7556 addr1_low = tr32(MAC_ADDR_1_LOW);
7557
7558 /* Skip MAC addr 1 if ASF is using it. */
7559 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
7560 !(addr1_high == 0 && addr1_low == 0))
7561 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07007562 }
Michael Chan986e0ae2007-05-05 12:10:20 -07007563 spin_lock_bh(&tp->lock);
7564 __tg3_set_mac_addr(tp, skip_mac_1);
7565 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007566
Michael Chanb9ec6c12006-07-25 16:37:27 -07007567 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007568}
7569
7570/* tp->lock is held. */
7571static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
7572 dma_addr_t mapping, u32 maxlen_flags,
7573 u32 nic_addr)
7574{
7575 tg3_write_mem(tp,
7576 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
7577 ((u64) mapping >> 32));
7578 tg3_write_mem(tp,
7579 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
7580 ((u64) mapping & 0xffffffff));
7581 tg3_write_mem(tp,
7582 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
7583 maxlen_flags);
7584
7585 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7586 tg3_write_mem(tp,
7587 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
7588 nic_addr);
7589}
7590
7591static void __tg3_set_rx_mode(struct net_device *);
Michael Chand244c892005-07-05 14:42:33 -07007592static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07007593{
Matt Carlsonb6080e12009-09-01 13:12:00 +00007594 int i;
7595
Matt Carlson19cfaec2009-12-03 08:36:20 +00007596 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007597 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
7598 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
7599 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007600 } else {
7601 tw32(HOSTCC_TXCOL_TICKS, 0);
7602 tw32(HOSTCC_TXMAX_FRAMES, 0);
7603 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007604 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007605
Matt Carlson20d73752010-07-11 09:31:41 +00007606 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00007607 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
7608 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
7609 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
7610 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007611 tw32(HOSTCC_RXCOL_TICKS, 0);
7612 tw32(HOSTCC_RXMAX_FRAMES, 0);
7613 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07007614 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007615
David S. Miller15f98502005-05-18 22:49:26 -07007616 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7617 u32 val = ec->stats_block_coalesce_usecs;
7618
Matt Carlsonb6080e12009-09-01 13:12:00 +00007619 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
7620 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
7621
David S. Miller15f98502005-05-18 22:49:26 -07007622 if (!netif_carrier_ok(tp->dev))
7623 val = 0;
7624
7625 tw32(HOSTCC_STAT_COAL_TICKS, val);
7626 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007627
7628 for (i = 0; i < tp->irq_cnt - 1; i++) {
7629 u32 reg;
7630
7631 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
7632 tw32(reg, ec->rx_coalesce_usecs);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007633 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
7634 tw32(reg, ec->rx_max_coalesced_frames);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007635 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
7636 tw32(reg, ec->rx_max_coalesced_frames_irq);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007637
7638 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS) {
7639 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
7640 tw32(reg, ec->tx_coalesce_usecs);
7641 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
7642 tw32(reg, ec->tx_max_coalesced_frames);
7643 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
7644 tw32(reg, ec->tx_max_coalesced_frames_irq);
7645 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007646 }
7647
7648 for (; i < tp->irq_max - 1; i++) {
7649 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007650 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007651 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007652
7653 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS) {
7654 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
7655 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
7656 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
7657 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007658 }
David S. Miller15f98502005-05-18 22:49:26 -07007659}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007660
7661/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00007662static void tg3_rings_reset(struct tg3 *tp)
7663{
7664 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007665 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007666 struct tg3_napi *tnapi = &tp->napi[0];
7667
7668 /* Disable all transmit rings but the first. */
7669 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7670 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlson3d377282010-10-14 10:37:39 +00007671 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
7672 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
7673 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
Matt Carlsonb703df62009-12-03 08:36:21 +00007674 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
7675 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007676 else
7677 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7678
7679 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7680 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
7681 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
7682 BDINFO_FLAGS_DISABLED);
7683
7684
7685 /* Disable all receive return rings but the first. */
Matt Carlsona50d0792010-06-05 17:24:37 +00007686 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
7687 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007688 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
7689 else if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00007690 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00007691 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
7692 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson2d31eca2009-09-01 12:53:31 +00007693 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
7694 else
7695 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7696
7697 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7698 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
7699 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
7700 BDINFO_FLAGS_DISABLED);
7701
7702 /* Disable interrupts */
7703 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
7704
7705 /* Zero mailbox registers. */
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007706 if (tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00007707 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007708 tp->napi[i].tx_prod = 0;
7709 tp->napi[i].tx_cons = 0;
Matt Carlsonc2353a32010-01-20 16:58:08 +00007710 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
7711 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007712 tw32_rx_mbox(tp->napi[i].consmbox, 0);
7713 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
7714 }
Matt Carlsonc2353a32010-01-20 16:58:08 +00007715 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS))
7716 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007717 } else {
7718 tp->napi[0].tx_prod = 0;
7719 tp->napi[0].tx_cons = 0;
7720 tw32_mailbox(tp->napi[0].prodmbox, 0);
7721 tw32_rx_mbox(tp->napi[0].consmbox, 0);
7722 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007723
7724 /* Make sure the NIC-based send BD rings are disabled. */
7725 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7726 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
7727 for (i = 0; i < 16; i++)
7728 tw32_tx_mbox(mbox + i * 8, 0);
7729 }
7730
7731 txrcb = NIC_SRAM_SEND_RCB;
7732 rxrcb = NIC_SRAM_RCV_RET_RCB;
7733
7734 /* Clear status block in ram. */
7735 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7736
7737 /* Set status block DMA address */
7738 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
7739 ((u64) tnapi->status_mapping >> 32));
7740 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
7741 ((u64) tnapi->status_mapping & 0xffffffff));
7742
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007743 if (tnapi->tx_ring) {
7744 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7745 (TG3_TX_RING_SIZE <<
7746 BDINFO_FLAGS_MAXLEN_SHIFT),
7747 NIC_SRAM_TX_BUFFER_DESC);
7748 txrcb += TG3_BDINFO_SIZE;
7749 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007750
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007751 if (tnapi->rx_rcb) {
7752 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00007753 (tp->rx_ret_ring_mask + 1) <<
7754 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007755 rxrcb += TG3_BDINFO_SIZE;
7756 }
7757
7758 stblk = HOSTCC_STATBLCK_RING1;
7759
7760 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
7761 u64 mapping = (u64)tnapi->status_mapping;
7762 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
7763 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
7764
7765 /* Clear status block in ram. */
7766 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7767
Matt Carlson19cfaec2009-12-03 08:36:20 +00007768 if (tnapi->tx_ring) {
7769 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7770 (TG3_TX_RING_SIZE <<
7771 BDINFO_FLAGS_MAXLEN_SHIFT),
7772 NIC_SRAM_TX_BUFFER_DESC);
7773 txrcb += TG3_BDINFO_SIZE;
7774 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007775
7776 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00007777 ((tp->rx_ret_ring_mask + 1) <<
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007778 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
7779
7780 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007781 rxrcb += TG3_BDINFO_SIZE;
7782 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007783}
7784
7785/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07007786static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007787{
7788 u32 val, rdmac_mode;
7789 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00007790 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007791
7792 tg3_disable_ints(tp);
7793
7794 tg3_stop_fw(tp);
7795
7796 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
7797
Matt Carlson859a588792010-04-05 10:19:28 +00007798 if (tp->tg3_flags & TG3_FLAG_INIT_COMPLETE)
Michael Chane6de8ad2005-05-05 14:42:41 -07007799 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007800
Matt Carlson603f1172010-02-12 14:47:10 +00007801 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08007802 tg3_phy_reset(tp);
7803
Linus Torvalds1da177e2005-04-16 15:20:36 -07007804 err = tg3_chip_reset(tp);
7805 if (err)
7806 return err;
7807
7808 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
7809
Matt Carlsonbcb37f62008-11-03 16:52:09 -08007810 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07007811 val = tr32(TG3_CPMU_CTRL);
7812 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
7813 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08007814
7815 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
7816 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
7817 val |= CPMU_LSPD_10MB_MACCLK_6_25;
7818 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
7819
7820 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
7821 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
7822 val |= CPMU_LNK_AWARE_MACCLK_6_25;
7823 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
7824
7825 val = tr32(TG3_CPMU_HST_ACC);
7826 val &= ~CPMU_HST_ACC_MACCLK_MASK;
7827 val |= CPMU_HST_ACC_MACCLK_6_25;
7828 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07007829 }
7830
Matt Carlson33466d92009-04-20 06:57:41 +00007831 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7832 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
7833 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
7834 PCIE_PWR_MGMT_L1_THRESH_4MS;
7835 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00007836
7837 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
7838 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
7839
7840 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00007841
Matt Carlsonf40386c2009-11-02 14:24:02 +00007842 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7843 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00007844 }
7845
Matt Carlson614b0592010-01-20 16:58:02 +00007846 if (tp->tg3_flags3 & TG3_FLG3_L1PLLPD_EN) {
7847 u32 grc_mode = tr32(GRC_MODE);
7848
7849 /* Access the lower 1K of PL PCIE block registers. */
7850 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
7851 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
7852
7853 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
7854 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
7855 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
7856
7857 tw32(GRC_MODE, grc_mode);
7858 }
7859
Matt Carlsoncea46462010-04-12 06:58:24 +00007860 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
7861 u32 grc_mode = tr32(GRC_MODE);
7862
7863 /* Access the lower 1K of PL PCIE block registers. */
7864 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
7865 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
7866
7867 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5);
7868 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
7869 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
7870
7871 tw32(GRC_MODE, grc_mode);
Matt Carlsona977dbe2010-04-12 06:58:26 +00007872
7873 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
7874 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
7875 val |= CPMU_LSPD_10MB_MACCLK_6_25;
7876 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00007877 }
7878
Matt Carlson52b02d02010-10-14 10:37:41 +00007879 /* Enable MAC control of LPI */
7880 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
7881 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL,
7882 TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
7883 TG3_CPMU_EEE_LNKIDL_UART_IDL);
7884
7885 tw32_f(TG3_CPMU_EEE_CTRL,
7886 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
7887
7888 tw32_f(TG3_CPMU_EEE_MODE,
7889 TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
7890 TG3_CPMU_EEEMD_LPI_IN_TX |
7891 TG3_CPMU_EEEMD_LPI_IN_RX |
7892 TG3_CPMU_EEEMD_EEE_ENABLE);
7893 }
7894
Linus Torvalds1da177e2005-04-16 15:20:36 -07007895 /* This works around an issue with Athlon chipsets on
7896 * B3 tigon3 silicon. This bit has no effect on any
7897 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07007898 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007899 */
Matt Carlson795d01c2007-10-07 23:28:17 -07007900 if (!(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT)) {
7901 if (!(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
7902 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
7903 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7904 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007905
7906 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
7907 (tp->tg3_flags & TG3_FLAG_PCIX_MODE)) {
7908 val = tr32(TG3PCI_PCISTATE);
7909 val |= PCISTATE_RETRY_SAME_DMA;
7910 tw32(TG3PCI_PCISTATE, val);
7911 }
7912
Matt Carlson0d3031d2007-10-10 18:02:43 -07007913 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
7914 /* Allow reads and writes to the
7915 * APE register and memory space.
7916 */
7917 val = tr32(TG3PCI_PCISTATE);
7918 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00007919 PCISTATE_ALLOW_APE_SHMEM_WR |
7920 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07007921 tw32(TG3PCI_PCISTATE, val);
7922 }
7923
Linus Torvalds1da177e2005-04-16 15:20:36 -07007924 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
7925 /* Enable some hw fixes. */
7926 val = tr32(TG3PCI_MSI_DATA);
7927 val |= (1 << 26) | (1 << 28) | (1 << 29);
7928 tw32(TG3PCI_MSI_DATA, val);
7929 }
7930
7931 /* Descriptor ring init may make accesses to the
7932 * NIC SRAM area to setup the TX descriptors, so we
7933 * can only do this after the hardware has been
7934 * successfully reset.
7935 */
Michael Chan32d8c572006-07-25 16:38:29 -07007936 err = tg3_init_rings(tp);
7937 if (err)
7938 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007939
Matt Carlsonc885e822010-08-02 11:25:57 +00007940 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00007941 val = tr32(TG3PCI_DMA_RW_CTRL) &
7942 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Matt Carlson1a319022010-04-12 06:58:25 +00007943 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
7944 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00007945 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
7946 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
7947 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07007948 /* This value is determined during the probe time DMA
7949 * engine test, tg3_test_dma.
7950 */
7951 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
7952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007953
7954 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
7955 GRC_MODE_4X_NIC_SEND_RINGS |
7956 GRC_MODE_NO_TX_PHDR_CSUM |
7957 GRC_MODE_NO_RX_PHDR_CSUM);
7958 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07007959
7960 /* Pseudo-header checksum is done by hardware logic and not
7961 * the offload processers, so make the chip do the pseudo-
7962 * header checksums on receive. For transmit it is more
7963 * convenient to do the pseudo-header checksum in software
7964 * as Linux does that on transmit for us in all cases.
7965 */
7966 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007967
7968 tw32(GRC_MODE,
7969 tp->grc_mode |
7970 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
7971
7972 /* Setup the timer prescalar register. Clock is always 66Mhz. */
7973 val = tr32(GRC_MISC_CFG);
7974 val &= ~0xff;
7975 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
7976 tw32(GRC_MISC_CFG, val);
7977
7978 /* Initialize MBUF/DESC pool. */
John W. Linvillecbf46852005-04-21 17:01:29 -07007979 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007980 /* Do nothing. */
7981 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
7982 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
7983 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
7984 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
7985 else
7986 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
7987 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
7988 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Matt Carlson859a588792010-04-05 10:19:28 +00007989 } else if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007990 int fw_len;
7991
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007992 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007993 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
7994 tw32(BUFMGR_MB_POOL_ADDR,
7995 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
7996 tw32(BUFMGR_MB_POOL_SIZE,
7997 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
7998 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007999
Michael Chan0f893dc2005-07-25 12:30:38 -07008000 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008001 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8002 tp->bufmgr_config.mbuf_read_dma_low_water);
8003 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8004 tp->bufmgr_config.mbuf_mac_rx_low_water);
8005 tw32(BUFMGR_MB_HIGH_WATER,
8006 tp->bufmgr_config.mbuf_high_water);
8007 } else {
8008 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8009 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
8010 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8011 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
8012 tw32(BUFMGR_MB_HIGH_WATER,
8013 tp->bufmgr_config.mbuf_high_water_jumbo);
8014 }
8015 tw32(BUFMGR_DMA_LOW_WATER,
8016 tp->bufmgr_config.dma_low_water);
8017 tw32(BUFMGR_DMA_HIGH_WATER,
8018 tp->bufmgr_config.dma_high_water);
8019
Matt Carlsond309a462010-09-30 10:34:31 +00008020 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
8021 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
8022 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
8023 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008024 for (i = 0; i < 2000; i++) {
8025 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
8026 break;
8027 udelay(10);
8028 }
8029 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00008030 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008031 return -ENODEV;
8032 }
8033
8034 /* Setup replenish threshold. */
Michael Chanf92905d2006-06-29 20:14:29 -07008035 val = tp->rx_pending / 8;
8036 if (val == 0)
8037 val = 1;
8038 else if (val > tp->rx_std_max_post)
8039 val = tp->rx_std_max_post;
Michael Chanb5d37722006-09-27 16:06:21 -07008040 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
8041 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
8042 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
8043
8044 if (val > (TG3_RX_INTERNAL_RING_SZ_5906 / 2))
8045 val = TG3_RX_INTERNAL_RING_SZ_5906 / 2;
8046 }
Michael Chanf92905d2006-06-29 20:14:29 -07008047
8048 tw32(RCVBDI_STD_THRESH, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008049
8050 /* Initialize TG3_BDINFO's at:
8051 * RCVDBDI_STD_BD: standard eth size rx ring
8052 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
8053 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
8054 *
8055 * like so:
8056 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
8057 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
8058 * ring attribute flags
8059 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
8060 *
8061 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
8062 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
8063 *
8064 * The size of each ring is fixed in the firmware, but the location is
8065 * configurable.
8066 */
8067 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008068 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008069 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008070 ((u64) tpr->rx_std_mapping & 0xffffffff));
Matt Carlsona50d0792010-06-05 17:24:37 +00008071 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
8072 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5719)
Matt Carlson87668d32009-11-13 13:03:34 +00008073 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
8074 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008075
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008076 /* Disable the mini ring */
8077 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008078 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
8079 BDINFO_FLAGS_DISABLED);
8080
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008081 /* Program the jumbo buffer descriptor ring control
8082 * blocks on those devices that have them.
8083 */
Matt Carlson8f666b02009-08-28 13:58:24 +00008084 if ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) &&
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008085 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008086 /* Setup replenish threshold. */
8087 tw32(RCVBDI_JUMBO_THRESH, tp->rx_jumbo_pending / 8);
8088
Michael Chan0f893dc2005-07-25 12:30:38 -07008089 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008090 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008091 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008092 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008093 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008094 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlson79ed5ac2009-08-28 14:00:55 +00008095 (RX_JUMBO_MAX_SIZE << BDINFO_FLAGS_MAXLEN_SHIFT) |
8096 BDINFO_FLAGS_USE_EXT_RECV);
Matt Carlsona50d0792010-06-05 17:24:37 +00008097 if (!(tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) ||
8098 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson87668d32009-11-13 13:03:34 +00008099 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
8100 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008101 } else {
8102 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
8103 BDINFO_FLAGS_DISABLED);
8104 }
8105
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008106 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
8107 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8108 val = RX_STD_MAX_SIZE_5705;
8109 else
8110 val = RX_STD_MAX_SIZE_5717;
8111 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
8112 val |= (TG3_RX_STD_DMA_SZ << 2);
8113 } else
Matt Carlson04380d42010-04-12 06:58:29 +00008114 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008115 } else
8116 val = RX_STD_MAX_SIZE_5705 << BDINFO_FLAGS_MAXLEN_SHIFT;
8117
8118 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008119
Matt Carlson411da642009-11-13 13:03:46 +00008120 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e62009-11-13 13:03:49 +00008121 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008122
Matt Carlson411da642009-11-13 13:03:46 +00008123 tpr->rx_jmb_prod_idx = (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) ?
Matt Carlson21f581a2009-08-28 14:00:25 +00008124 tp->rx_jumbo_pending : 0;
Matt Carlson66711e62009-11-13 13:03:49 +00008125 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008126
Matt Carlsonc885e822010-08-02 11:25:57 +00008127 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008128 tw32(STD_REPLENISH_LWM, 32);
8129 tw32(JMB_REPLENISH_LWM, 16);
8130 }
8131
Matt Carlson2d31eca2009-09-01 12:53:31 +00008132 tg3_rings_reset(tp);
8133
Linus Torvalds1da177e2005-04-16 15:20:36 -07008134 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07008135 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008136
8137 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00008138 tw32(MAC_RX_MTU_SIZE,
8139 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008140
8141 /* The slot time is changed by tg3_setup_phy if we
8142 * run at gigabit with half duplex.
8143 */
8144 tw32(MAC_TX_LENGTHS,
8145 (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
8146 (6 << TX_LENGTHS_IPG_SHIFT) |
8147 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
8148
8149 /* Receive rules. */
8150 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
8151 tw32(RCVLPC_CONFIG, 0x0181);
8152
8153 /* Calculate RDMAC_MODE setting early, we need it to determine
8154 * the RCVLPC_STATE_ENABLE mask.
8155 */
8156 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
8157 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
8158 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
8159 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
8160 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07008161
Matt Carlsona50d0792010-06-05 17:24:37 +00008162 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
8163 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlson0339e4e2010-02-12 14:47:09 +00008164 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
8165
Matt Carlson57e69832008-05-25 23:48:31 -07008166 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08008167 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8168 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07008169 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
8170 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
8171 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
8172
Michael Chan85e94ce2005-04-21 17:05:28 -07008173 /* If statement applies to 5705 and 5750 PCI devices only */
8174 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8175 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) ||
8176 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008177 if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07008178 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008179 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
8180 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
8181 !(tp->tg3_flags2 & TG3_FLG2_IS_5788)) {
8182 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8183 }
8184 }
8185
Michael Chan85e94ce2005-04-21 17:05:28 -07008186 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)
8187 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8188
Linus Torvalds1da177e2005-04-16 15:20:36 -07008189 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
Matt Carlson027455a2008-12-21 20:19:30 -08008190 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
8191
Matt Carlsone849cdc2009-11-13 13:03:38 +00008192 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
8193 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson027455a2008-12-21 20:19:30 -08008194 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
8195 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008196
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008197 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
8198 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
8199 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8200 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
8201 (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)) {
8202 val = tr32(TG3_RDMA_RSRVCTRL_REG);
8203 tw32(TG3_RDMA_RSRVCTRL_REG,
8204 val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
8205 }
8206
Matt Carlsond309a462010-09-30 10:34:31 +00008207 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) {
8208 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
8209 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val |
8210 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
8211 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
8212 }
8213
Linus Torvalds1da177e2005-04-16 15:20:36 -07008214 /* Receive/send statistics. */
Michael Chan16613942006-06-29 20:15:13 -07008215 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
8216 val = tr32(RCVLPC_STATS_ENABLE);
8217 val &= ~RCVLPC_STATSENAB_DACK_FIX;
8218 tw32(RCVLPC_STATS_ENABLE, val);
8219 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
8220 (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008221 val = tr32(RCVLPC_STATS_ENABLE);
8222 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
8223 tw32(RCVLPC_STATS_ENABLE, val);
8224 } else {
8225 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
8226 }
8227 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
8228 tw32(SNDDATAI_STATSENAB, 0xffffff);
8229 tw32(SNDDATAI_STATSCTRL,
8230 (SNDDATAI_SCTRL_ENABLE |
8231 SNDDATAI_SCTRL_FASTUPD));
8232
8233 /* Setup host coalescing engine. */
8234 tw32(HOSTCC_MODE, 0);
8235 for (i = 0; i < 2000; i++) {
8236 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
8237 break;
8238 udelay(10);
8239 }
8240
Michael Chand244c892005-07-05 14:42:33 -07008241 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008242
Linus Torvalds1da177e2005-04-16 15:20:36 -07008243 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
8244 /* Status/statistics block address. See tg3_timer,
8245 * the tg3_periodic_fetch_stats call there, and
8246 * tg3_get_stats to see how this works for 5705/5750 chips.
8247 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008248 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8249 ((u64) tp->stats_mapping >> 32));
8250 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8251 ((u64) tp->stats_mapping & 0xffffffff));
8252 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008253
Linus Torvalds1da177e2005-04-16 15:20:36 -07008254 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008255
8256 /* Clear statistics and status block memory areas */
8257 for (i = NIC_SRAM_STATS_BLK;
8258 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
8259 i += sizeof(u32)) {
8260 tg3_write_mem(tp, i, 0);
8261 udelay(40);
8262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008263 }
8264
8265 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
8266
8267 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
8268 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
8269 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
8270 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
8271
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008272 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
8273 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -07008274 /* reset to prevent losing 1st rx packet intermittently */
8275 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8276 udelay(10);
8277 }
8278
Matt Carlson3bda1252008-08-15 14:08:22 -07008279 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
8280 tp->mac_mode &= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
8281 else
8282 tp->mac_mode = 0;
8283 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Linus Torvalds1da177e2005-04-16 15:20:36 -07008284 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE | MAC_MODE_FHDE_ENABLE;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07008285 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008286 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07008287 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
8288 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008289 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
8290 udelay(40);
8291
Michael Chan314fba32005-04-21 17:07:04 -07008292 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Michael Chan9d26e212006-12-07 00:21:14 -08008293 * If TG3_FLG2_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07008294 * register to preserve the GPIO settings for LOMs. The GPIOs,
8295 * whether used as inputs or outputs, are set by boot code after
8296 * reset.
8297 */
Michael Chan9d26e212006-12-07 00:21:14 -08008298 if (!(tp->tg3_flags2 & TG3_FLG2_IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07008299 u32 gpio_mask;
8300
Michael Chan9d26e212006-12-07 00:21:14 -08008301 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
8302 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
8303 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07008304
8305 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
8306 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
8307 GRC_LCLCTRL_GPIO_OUTPUT3;
8308
Michael Chanaf36e6b2006-03-23 01:28:06 -08008309 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
8310 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
8311
Gary Zambranoaaf84462007-05-05 11:51:45 -07008312 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07008313 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
8314
8315 /* GPIO1 must be driven high for eeprom write protect */
Michael Chan9d26e212006-12-07 00:21:14 -08008316 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT)
8317 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
8318 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07008319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008320 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8321 udelay(100);
8322
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008323 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX) {
8324 val = tr32(MSGINT_MODE);
8325 val |= MSGINT_MODE_MULTIVEC_EN | MSGINT_MODE_ENABLE;
8326 tw32(MSGINT_MODE, val);
8327 }
8328
Linus Torvalds1da177e2005-04-16 15:20:36 -07008329 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
8330 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
8331 udelay(40);
8332 }
8333
8334 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
8335 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
8336 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
8337 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
8338 WDMAC_MODE_LNGREAD_ENAB);
8339
Michael Chan85e94ce2005-04-21 17:05:28 -07008340 /* If statement applies to 5705 and 5750 PCI devices only */
8341 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8342 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) ||
8343 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750) {
Matt Carlson29ea0952009-08-25 10:07:54 +00008344 if ((tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008345 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
8346 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
8347 /* nothing */
8348 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
8349 !(tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
8350 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)) {
8351 val |= WDMAC_MODE_RX_ACCEL;
8352 }
8353 }
8354
Michael Chand9ab5ad2006-03-20 22:27:35 -08008355 /* Enable host coalescing bug fix */
Matt Carlson321d32a2008-11-21 17:22:19 -08008356 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Matt Carlsonf51f3562008-05-25 23:45:08 -07008357 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad2006-03-20 22:27:35 -08008358
Matt Carlson788a0352009-11-02 14:26:03 +00008359 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
8360 val |= WDMAC_MODE_BURST_ALL_DATA;
8361
Linus Torvalds1da177e2005-04-16 15:20:36 -07008362 tw32_f(WDMAC_MODE, val);
8363 udelay(40);
8364
Matt Carlson9974a352007-10-07 23:27:28 -07008365 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
8366 u16 pcix_cmd;
8367
8368 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8369 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008370 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07008371 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
8372 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008373 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07008374 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
8375 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008376 }
Matt Carlson9974a352007-10-07 23:27:28 -07008377 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8378 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008379 }
8380
8381 tw32_f(RDMAC_MODE, rdmac_mode);
8382 udelay(40);
8383
8384 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
8385 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
8386 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07008387
8388 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
8389 tw32(SNDDATAC_MODE,
8390 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
8391 else
8392 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
8393
Linus Torvalds1da177e2005-04-16 15:20:36 -07008394 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
8395 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008396 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
8397 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
8398 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
8399 val |= RCVDBDI_MODE_LRG_RING_SZ;
8400 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008401 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008402 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
8403 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008404 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Matt Carlson19cfaec2009-12-03 08:36:20 +00008405 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008406 val |= SNDBDI_MODE_MULTI_TXQ_EN;
8407 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008408 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
8409
8410 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
8411 err = tg3_load_5701_a0_firmware_fix(tp);
8412 if (err)
8413 return err;
8414 }
8415
Linus Torvalds1da177e2005-04-16 15:20:36 -07008416 if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) {
8417 err = tg3_load_tso_firmware(tp);
8418 if (err)
8419 return err;
8420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008421
8422 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonb1d05212010-06-05 17:24:31 +00008423 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
8424 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
8425 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008426 tw32_f(MAC_TX_MODE, tp->tx_mode);
8427 udelay(100);
8428
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008429 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS) {
8430 u32 reg = MAC_RSS_INDIR_TBL_0;
8431 u8 *ent = (u8 *)&val;
8432
8433 /* Setup the indirection table */
8434 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
8435 int idx = i % sizeof(val);
8436
Matt Carlson5efeeea2010-07-11 09:31:40 +00008437 ent[idx] = i % (tp->irq_cnt - 1);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008438 if (idx == sizeof(val) - 1) {
8439 tw32(reg, val);
8440 reg += 4;
8441 }
8442 }
8443
8444 /* Setup the "secret" hash key. */
8445 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
8446 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
8447 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
8448 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
8449 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
8450 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
8451 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
8452 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
8453 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
8454 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
8455 }
8456
Linus Torvalds1da177e2005-04-16 15:20:36 -07008457 tp->rx_mode = RX_MODE_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08008458 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Michael Chanaf36e6b2006-03-23 01:28:06 -08008459 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
8460
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008461 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)
8462 tp->rx_mode |= RX_MODE_RSS_ENABLE |
8463 RX_MODE_RSS_ITBL_HASH_BITS_7 |
8464 RX_MODE_RSS_IPV6_HASH_EN |
8465 RX_MODE_RSS_TCP_IPV6_HASH_EN |
8466 RX_MODE_RSS_IPV4_HASH_EN |
8467 RX_MODE_RSS_TCP_IPV4_HASH_EN;
8468
Linus Torvalds1da177e2005-04-16 15:20:36 -07008469 tw32_f(MAC_RX_MODE, tp->rx_mode);
8470 udelay(10);
8471
Linus Torvalds1da177e2005-04-16 15:20:36 -07008472 tw32(MAC_LED_CTRL, tp->led_ctrl);
8473
8474 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008475 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008476 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8477 udelay(10);
8478 }
8479 tw32_f(MAC_RX_MODE, tp->rx_mode);
8480 udelay(10);
8481
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008482 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008483 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008484 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008485 /* Set drive transmission level to 1.2V */
8486 /* only if the signal pre-emphasis bit is not set */
8487 val = tr32(MAC_SERDES_CFG);
8488 val &= 0xfffff000;
8489 val |= 0x880;
8490 tw32(MAC_SERDES_CFG, val);
8491 }
8492 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
8493 tw32(MAC_SERDES_CFG, 0x616000);
8494 }
8495
8496 /* Prevent chip from dropping frames when flow control
8497 * is enabled.
8498 */
Matt Carlson666bc832010-01-20 16:58:03 +00008499 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8500 val = 1;
8501 else
8502 val = 2;
8503 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008504
8505 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008506 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008507 /* Use hardware link auto-negotiation */
8508 tp->tg3_flags2 |= TG3_FLG2_HW_AUTONEG;
8509 }
8510
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008511 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Michael Chand4d2c552006-03-20 17:47:20 -08008512 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)) {
8513 u32 tmp;
8514
8515 tmp = tr32(SERDES_RX_CTRL);
8516 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
8517 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
8518 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
8519 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8520 }
8521
Matt Carlsondd477002008-05-25 23:45:58 -07008522 if (!(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)) {
Matt Carlson800960682010-08-02 11:26:06 +00008523 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
8524 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07008525 tp->link_config.speed = tp->link_config.orig_speed;
8526 tp->link_config.duplex = tp->link_config.orig_duplex;
8527 tp->link_config.autoneg = tp->link_config.orig_autoneg;
8528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008529
Matt Carlsondd477002008-05-25 23:45:58 -07008530 err = tg3_setup_phy(tp, 0);
8531 if (err)
8532 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008533
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008534 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
8535 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07008536 u32 tmp;
8537
8538 /* Clear CRC stats. */
8539 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
8540 tg3_writephy(tp, MII_TG3_TEST1,
8541 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00008542 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -07008543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008544 }
8545 }
8546
8547 __tg3_set_rx_mode(tp->dev);
8548
8549 /* Initialize receive rules. */
8550 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
8551 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
8552 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
8553 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
8554
Michael Chan4cf78e42005-07-25 12:29:19 -07008555 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Michael Chana4e2b342005-10-26 15:46:52 -07008556 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008557 limit = 8;
8558 else
8559 limit = 16;
8560 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF)
8561 limit -= 4;
8562 switch (limit) {
8563 case 16:
8564 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
8565 case 15:
8566 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
8567 case 14:
8568 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
8569 case 13:
8570 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
8571 case 12:
8572 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
8573 case 11:
8574 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
8575 case 10:
8576 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
8577 case 9:
8578 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
8579 case 8:
8580 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
8581 case 7:
8582 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
8583 case 6:
8584 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
8585 case 5:
8586 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
8587 case 4:
8588 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
8589 case 3:
8590 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
8591 case 2:
8592 case 1:
8593
8594 default:
8595 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07008596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008597
Matt Carlson9ce768e2007-10-11 19:49:11 -07008598 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
8599 /* Write our heartbeat update interval to APE. */
8600 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
8601 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07008602
Linus Torvalds1da177e2005-04-16 15:20:36 -07008603 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
8604
Linus Torvalds1da177e2005-04-16 15:20:36 -07008605 return 0;
8606}
8607
8608/* Called at device open time to get the chip ready for
8609 * packet processing. Invoked with tp->lock held.
8610 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008611static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008612{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008613 tg3_switch_clocks(tp);
8614
8615 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
8616
Matt Carlson2f751b62008-08-04 23:17:34 -07008617 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008618}
8619
8620#define TG3_STAT_ADD32(PSTAT, REG) \
8621do { u32 __val = tr32(REG); \
8622 (PSTAT)->low += __val; \
8623 if ((PSTAT)->low < __val) \
8624 (PSTAT)->high += 1; \
8625} while (0)
8626
8627static void tg3_periodic_fetch_stats(struct tg3 *tp)
8628{
8629 struct tg3_hw_stats *sp = tp->hw_stats;
8630
8631 if (!netif_carrier_ok(tp->dev))
8632 return;
8633
8634 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
8635 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
8636 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
8637 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
8638 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
8639 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
8640 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
8641 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
8642 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
8643 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
8644 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
8645 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
8646 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
8647
8648 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
8649 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
8650 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
8651 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
8652 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
8653 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
8654 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
8655 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
8656 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
8657 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
8658 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
8659 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
8660 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
8661 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07008662
8663 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
8664 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
8665 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008666}
8667
8668static void tg3_timer(unsigned long __opaque)
8669{
8670 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008671
Michael Chanf475f162006-03-27 23:20:14 -08008672 if (tp->irq_sync)
8673 goto restart_timer;
8674
David S. Millerf47c11e2005-06-24 20:18:35 -07008675 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008676
David S. Millerfac9b832005-05-18 22:46:34 -07008677 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)) {
8678 /* All of this garbage is because when using non-tagged
8679 * IRQ status the mailbox/status_block protocol the chip
8680 * uses with the cpu is race prone.
8681 */
Matt Carlson898a56f2009-08-28 14:02:40 +00008682 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -07008683 tw32(GRC_LOCAL_CTRL,
8684 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
8685 } else {
8686 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00008687 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -07008688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008689
David S. Millerfac9b832005-05-18 22:46:34 -07008690 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
8691 tp->tg3_flags2 |= TG3_FLG2_RESTART_TIMER;
David S. Millerf47c11e2005-06-24 20:18:35 -07008692 spin_unlock(&tp->lock);
David S. Millerfac9b832005-05-18 22:46:34 -07008693 schedule_work(&tp->reset_task);
8694 return;
8695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008696 }
8697
Linus Torvalds1da177e2005-04-16 15:20:36 -07008698 /* This part only runs once per second. */
8699 if (!--tp->timer_counter) {
David S. Millerfac9b832005-05-18 22:46:34 -07008700 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
8701 tg3_periodic_fetch_stats(tp);
8702
Matt Carlson52b02d02010-10-14 10:37:41 +00008703 if (tp->setlpicnt && !--tp->setlpicnt) {
8704 u32 val = tr32(TG3_CPMU_EEE_MODE);
8705 tw32(TG3_CPMU_EEE_MODE,
8706 val | TG3_CPMU_EEEMD_LPI_ENABLE);
8707 }
8708
Linus Torvalds1da177e2005-04-16 15:20:36 -07008709 if (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) {
8710 u32 mac_stat;
8711 int phy_event;
8712
8713 mac_stat = tr32(MAC_STATUS);
8714
8715 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008716 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008717 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
8718 phy_event = 1;
8719 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
8720 phy_event = 1;
8721
8722 if (phy_event)
8723 tg3_setup_phy(tp, 0);
8724 } else if (tp->tg3_flags & TG3_FLAG_POLL_SERDES) {
8725 u32 mac_stat = tr32(MAC_STATUS);
8726 int need_setup = 0;
8727
8728 if (netif_carrier_ok(tp->dev) &&
8729 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
8730 need_setup = 1;
8731 }
Matt Carlsonbe98da62010-07-11 09:31:46 +00008732 if (!netif_carrier_ok(tp->dev) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008733 (mac_stat & (MAC_STATUS_PCS_SYNCED |
8734 MAC_STATUS_SIGNAL_DET))) {
8735 need_setup = 1;
8736 }
8737 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07008738 if (!tp->serdes_counter) {
8739 tw32_f(MAC_MODE,
8740 (tp->mac_mode &
8741 ~MAC_MODE_PORT_MODE_MASK));
8742 udelay(40);
8743 tw32_f(MAC_MODE, tp->mac_mode);
8744 udelay(40);
8745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008746 tg3_setup_phy(tp, 0);
8747 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008748 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Matt Carlson2138c002010-07-11 09:31:43 +00008749 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -07008750 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +00008751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008752
8753 tp->timer_counter = tp->timer_multiplier;
8754 }
8755
Michael Chan130b8e42006-09-27 16:00:40 -07008756 /* Heartbeat is only sent once every 2 seconds.
8757 *
8758 * The heartbeat is to tell the ASF firmware that the host
8759 * driver is still alive. In the event that the OS crashes,
8760 * ASF needs to reset the hardware to free up the FIFO space
8761 * that may be filled with rx packets destined for the host.
8762 * If the FIFO is full, ASF will no longer function properly.
8763 *
8764 * Unintended resets have been reported on real time kernels
8765 * where the timer doesn't run on time. Netpoll will also have
8766 * same problem.
8767 *
8768 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
8769 * to check the ring condition when the heartbeat is expiring
8770 * before doing the reset. This will prevent most unintended
8771 * resets.
8772 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008773 if (!--tp->asf_counter) {
Matt Carlsonbc7959b2008-08-15 14:08:55 -07008774 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
8775 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07008776 tg3_wait_for_event_ack(tp);
8777
Michael Chanbbadf502006-04-06 21:46:34 -07008778 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07008779 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07008780 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +00008781 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
8782 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -07008783
8784 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008785 }
8786 tp->asf_counter = tp->asf_multiplier;
8787 }
8788
David S. Millerf47c11e2005-06-24 20:18:35 -07008789 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008790
Michael Chanf475f162006-03-27 23:20:14 -08008791restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07008792 tp->timer.expires = jiffies + tp->timer_offset;
8793 add_timer(&tp->timer);
8794}
8795
Matt Carlson4f125f42009-09-01 12:55:02 +00008796static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -08008797{
David Howells7d12e782006-10-05 14:55:46 +01008798 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008799 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +00008800 char *name;
8801 struct tg3_napi *tnapi = &tp->napi[irq_num];
8802
8803 if (tp->irq_cnt == 1)
8804 name = tp->dev->name;
8805 else {
8806 name = &tnapi->irq_lbl[0];
8807 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
8808 name[IFNAMSIZ-1] = 0;
8809 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08008810
Matt Carlson679563f2009-09-01 12:55:46 +00008811 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI_OR_MSIX) {
Michael Chanfcfa0a32006-03-20 22:28:41 -08008812 fn = tg3_msi;
8813 if (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)
8814 fn = tg3_msi_1shot;
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07008815 flags = IRQF_SAMPLE_RANDOM;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008816 } else {
8817 fn = tg3_interrupt;
8818 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)
8819 fn = tg3_interrupt_tagged;
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07008820 flags = IRQF_SHARED | IRQF_SAMPLE_RANDOM;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008821 }
Matt Carlson4f125f42009-09-01 12:55:02 +00008822
8823 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08008824}
8825
Michael Chan79381092005-04-21 17:13:59 -07008826static int tg3_test_interrupt(struct tg3 *tp)
8827{
Matt Carlson09943a12009-08-28 14:01:57 +00008828 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -07008829 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -07008830 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008831 u32 val;
Michael Chan79381092005-04-21 17:13:59 -07008832
Michael Chand4bc3922005-05-29 14:59:20 -07008833 if (!netif_running(dev))
8834 return -ENODEV;
8835
Michael Chan79381092005-04-21 17:13:59 -07008836 tg3_disable_ints(tp);
8837
Matt Carlson4f125f42009-09-01 12:55:02 +00008838 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07008839
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008840 /*
8841 * Turn off MSI one shot mode. Otherwise this test has no
8842 * observable way to know whether the interrupt was delivered.
8843 */
Matt Carlsonc885e822010-08-02 11:25:57 +00008844 if ((tp->tg3_flags3 & TG3_FLG3_5717_PLUS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008845 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
8846 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
8847 tw32(MSGINT_MODE, val);
8848 }
8849
Matt Carlson4f125f42009-09-01 12:55:02 +00008850 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Matt Carlson09943a12009-08-28 14:01:57 +00008851 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07008852 if (err)
8853 return err;
8854
Matt Carlson898a56f2009-08-28 14:02:40 +00008855 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -07008856 tg3_enable_ints(tp);
8857
8858 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00008859 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -07008860
8861 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -07008862 u32 int_mbox, misc_host_ctrl;
8863
Matt Carlson898a56f2009-08-28 14:02:40 +00008864 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -07008865 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
8866
8867 if ((int_mbox != 0) ||
8868 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
8869 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -07008870 break;
Michael Chanb16250e2006-09-27 16:10:14 -07008871 }
8872
Michael Chan79381092005-04-21 17:13:59 -07008873 msleep(10);
8874 }
8875
8876 tg3_disable_ints(tp);
8877
Matt Carlson4f125f42009-09-01 12:55:02 +00008878 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008879
Matt Carlson4f125f42009-09-01 12:55:02 +00008880 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07008881
8882 if (err)
8883 return err;
8884
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008885 if (intr_ok) {
8886 /* Reenable MSI one shot mode. */
Matt Carlsonc885e822010-08-02 11:25:57 +00008887 if ((tp->tg3_flags3 & TG3_FLG3_5717_PLUS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008888 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
8889 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
8890 tw32(MSGINT_MODE, val);
8891 }
Michael Chan79381092005-04-21 17:13:59 -07008892 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008893 }
Michael Chan79381092005-04-21 17:13:59 -07008894
8895 return -EIO;
8896}
8897
8898/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
8899 * successfully restored
8900 */
8901static int tg3_test_msi(struct tg3 *tp)
8902{
Michael Chan79381092005-04-21 17:13:59 -07008903 int err;
8904 u16 pci_cmd;
8905
8906 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSI))
8907 return 0;
8908
8909 /* Turn off SERR reporting in case MSI terminates with Master
8910 * Abort.
8911 */
8912 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
8913 pci_write_config_word(tp->pdev, PCI_COMMAND,
8914 pci_cmd & ~PCI_COMMAND_SERR);
8915
8916 err = tg3_test_interrupt(tp);
8917
8918 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
8919
8920 if (!err)
8921 return 0;
8922
8923 /* other failures */
8924 if (err != -EIO)
8925 return err;
8926
8927 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +00008928 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
8929 "to INTx mode. Please report this failure to the PCI "
8930 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -07008931
Matt Carlson4f125f42009-09-01 12:55:02 +00008932 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +00008933
Michael Chan79381092005-04-21 17:13:59 -07008934 pci_disable_msi(tp->pdev);
8935
8936 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI;
Andre Detschdc8bf1b2010-04-26 07:27:07 +00008937 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -07008938
Matt Carlson4f125f42009-09-01 12:55:02 +00008939 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07008940 if (err)
8941 return err;
8942
8943 /* Need to reset the chip because the MSI cycle may have terminated
8944 * with Master Abort.
8945 */
David S. Millerf47c11e2005-06-24 20:18:35 -07008946 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07008947
Michael Chan944d9802005-05-29 14:57:48 -07008948 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008949 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07008950
David S. Millerf47c11e2005-06-24 20:18:35 -07008951 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07008952
8953 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +00008954 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -07008955
8956 return err;
8957}
8958
Matt Carlson9e9fd122009-01-19 16:57:45 -08008959static int tg3_request_firmware(struct tg3 *tp)
8960{
8961 const __be32 *fw_data;
8962
8963 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00008964 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
8965 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08008966 return -ENOENT;
8967 }
8968
8969 fw_data = (void *)tp->fw->data;
8970
8971 /* Firmware blob starts with version numbers, followed by
8972 * start address and _full_ length including BSS sections
8973 * (which must be longer than the actual data, of course
8974 */
8975
8976 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
8977 if (tp->fw_len < (tp->fw->size - 12)) {
Joe Perches05dbe002010-02-17 19:44:19 +00008978 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
8979 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08008980 release_firmware(tp->fw);
8981 tp->fw = NULL;
8982 return -EINVAL;
8983 }
8984
8985 /* We no longer need firmware; we have it. */
8986 tp->fw_needed = NULL;
8987 return 0;
8988}
8989
Matt Carlson679563f2009-09-01 12:55:46 +00008990static bool tg3_enable_msix(struct tg3 *tp)
8991{
8992 int i, rc, cpus = num_online_cpus();
8993 struct msix_entry msix_ent[tp->irq_max];
8994
8995 if (cpus == 1)
8996 /* Just fallback to the simpler MSI mode. */
8997 return false;
8998
8999 /*
9000 * We want as many rx rings enabled as there are cpus.
9001 * The first MSIX vector only deals with link interrupts, etc,
9002 * so we add one to the number of vectors we are requesting.
9003 */
9004 tp->irq_cnt = min_t(unsigned, cpus + 1, tp->irq_max);
9005
9006 for (i = 0; i < tp->irq_max; i++) {
9007 msix_ent[i].entry = i;
9008 msix_ent[i].vector = 0;
9009 }
9010
9011 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +00009012 if (rc < 0) {
9013 return false;
9014 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +00009015 if (pci_enable_msix(tp->pdev, msix_ent, rc))
9016 return false;
Joe Perches05dbe002010-02-17 19:44:19 +00009017 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
9018 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +00009019 tp->irq_cnt = rc;
9020 }
9021
9022 for (i = 0; i < tp->irq_max; i++)
9023 tp->napi[i].irq_vec = msix_ent[i].vector;
9024
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009025 netif_set_real_num_tx_queues(tp->dev, 1);
9026 rc = tp->irq_cnt > 1 ? tp->irq_cnt - 1 : 1;
9027 if (netif_set_real_num_rx_queues(tp->dev, rc)) {
9028 pci_disable_msix(tp->pdev);
9029 return false;
9030 }
Matt Carlsonf0392d22010-09-15 08:59:54 +00009031 if (tp->irq_cnt > 1)
Matt Carlson2430b032010-06-05 17:24:34 +00009032 tp->tg3_flags3 |= TG3_FLG3_ENABLE_RSS;
9033
Matt Carlson679563f2009-09-01 12:55:46 +00009034 return true;
9035}
9036
Matt Carlson07b01732009-08-28 14:01:15 +00009037static void tg3_ints_init(struct tg3 *tp)
9038{
Matt Carlson679563f2009-09-01 12:55:46 +00009039 if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSI_OR_MSIX) &&
9040 !(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +00009041 /* All MSI supporting chips should support tagged
9042 * status. Assert that this is the case.
9043 */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009044 netdev_warn(tp->dev,
9045 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +00009046 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +00009047 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009048
Matt Carlson679563f2009-09-01 12:55:46 +00009049 if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX) && tg3_enable_msix(tp))
9050 tp->tg3_flags2 |= TG3_FLG2_USING_MSIX;
9051 else if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSI) &&
9052 pci_enable_msi(tp->pdev) == 0)
9053 tp->tg3_flags2 |= TG3_FLG2_USING_MSI;
9054
9055 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI_OR_MSIX) {
9056 u32 msi_mode = tr32(MSGINT_MODE);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009057 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
9058 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson679563f2009-09-01 12:55:46 +00009059 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
9060 }
9061defcfg:
9062 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSIX)) {
9063 tp->irq_cnt = 1;
9064 tp->napi[0].irq_vec = tp->pdev->irq;
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009065 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -07009066 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +00009067 }
Matt Carlson07b01732009-08-28 14:01:15 +00009068}
9069
9070static void tg3_ints_fini(struct tg3 *tp)
9071{
Matt Carlson679563f2009-09-01 12:55:46 +00009072 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
9073 pci_disable_msix(tp->pdev);
9074 else if (tp->tg3_flags2 & TG3_FLG2_USING_MSI)
9075 pci_disable_msi(tp->pdev);
9076 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI_OR_MSIX;
Matt Carlson774ee752010-08-02 11:25:56 +00009077 tp->tg3_flags3 &= ~(TG3_FLG3_ENABLE_RSS | TG3_FLG3_ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +00009078}
9079
Linus Torvalds1da177e2005-04-16 15:20:36 -07009080static int tg3_open(struct net_device *dev)
9081{
9082 struct tg3 *tp = netdev_priv(dev);
Matt Carlson4f125f42009-09-01 12:55:02 +00009083 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009084
Matt Carlson9e9fd122009-01-19 16:57:45 -08009085 if (tp->fw_needed) {
9086 err = tg3_request_firmware(tp);
9087 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
9088 if (err)
9089 return err;
9090 } else if (err) {
Joe Perches05dbe002010-02-17 19:44:19 +00009091 netdev_warn(tp->dev, "TSO capability disabled\n");
Matt Carlson9e9fd122009-01-19 16:57:45 -08009092 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
9093 } else if (!(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009094 netdev_notice(tp->dev, "TSO capability restored\n");
Matt Carlson9e9fd122009-01-19 16:57:45 -08009095 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
9096 }
9097 }
9098
Michael Chanc49a1562006-12-17 17:07:29 -08009099 netif_carrier_off(tp->dev);
9100
Michael Chanbc1c7562006-03-20 17:48:03 -08009101 err = tg3_set_power_state(tp, PCI_D0);
Matt Carlson2f751b62008-08-04 23:17:34 -07009102 if (err)
Michael Chanbc1c7562006-03-20 17:48:03 -08009103 return err;
Matt Carlson2f751b62008-08-04 23:17:34 -07009104
9105 tg3_full_lock(tp, 0);
Michael Chanbc1c7562006-03-20 17:48:03 -08009106
Linus Torvalds1da177e2005-04-16 15:20:36 -07009107 tg3_disable_ints(tp);
9108 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
9109
David S. Millerf47c11e2005-06-24 20:18:35 -07009110 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009111
Matt Carlson679563f2009-09-01 12:55:46 +00009112 /*
9113 * Setup interrupts first so we know how
9114 * many NAPI resources to allocate
9115 */
9116 tg3_ints_init(tp);
9117
Linus Torvalds1da177e2005-04-16 15:20:36 -07009118 /* The placement of this call is tied
9119 * to the setup and use of Host TX descriptors.
9120 */
9121 err = tg3_alloc_consistent(tp);
9122 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009123 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009124
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009125 tg3_napi_init(tp);
9126
Matt Carlsonfed97812009-09-01 13:10:19 +00009127 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07009128
Matt Carlson4f125f42009-09-01 12:55:02 +00009129 for (i = 0; i < tp->irq_cnt; i++) {
9130 struct tg3_napi *tnapi = &tp->napi[i];
9131 err = tg3_request_irq(tp, i);
9132 if (err) {
9133 for (i--; i >= 0; i--)
9134 free_irq(tnapi->irq_vec, tnapi);
9135 break;
9136 }
9137 }
Matt Carlson07b01732009-08-28 14:01:15 +00009138
9139 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009140 goto err_out2;
Matt Carlson07b01732009-08-28 14:01:15 +00009141
David S. Millerf47c11e2005-06-24 20:18:35 -07009142 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009143
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009144 err = tg3_init_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009145 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -07009146 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009147 tg3_free_rings(tp);
9148 } else {
David S. Millerfac9b832005-05-18 22:46:34 -07009149 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)
9150 tp->timer_offset = HZ;
9151 else
9152 tp->timer_offset = HZ / 10;
9153
9154 BUG_ON(tp->timer_offset > HZ);
9155 tp->timer_counter = tp->timer_multiplier =
9156 (HZ / tp->timer_offset);
9157 tp->asf_counter = tp->asf_multiplier =
Michael Chan28fbef72005-10-26 15:48:35 -07009158 ((HZ / tp->timer_offset) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009159
9160 init_timer(&tp->timer);
9161 tp->timer.expires = jiffies + tp->timer_offset;
9162 tp->timer.data = (unsigned long) tp;
9163 tp->timer.function = tg3_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009164 }
9165
David S. Millerf47c11e2005-06-24 20:18:35 -07009166 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009167
Matt Carlson07b01732009-08-28 14:01:15 +00009168 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009169 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009170
Michael Chan79381092005-04-21 17:13:59 -07009171 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
9172 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -07009173
Michael Chan79381092005-04-21 17:13:59 -07009174 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -07009175 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -07009176 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -07009177 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07009178 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009179
Matt Carlson679563f2009-09-01 12:55:46 +00009180 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -07009181 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009182
Matt Carlsonc885e822010-08-02 11:25:57 +00009183 if (!(tp->tg3_flags3 & TG3_FLG3_5717_PLUS) &&
9184 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009185 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009186
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009187 tw32(PCIE_TRANSACTION_CFG,
9188 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009189 }
Michael Chan79381092005-04-21 17:13:59 -07009190 }
9191
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009192 tg3_phy_start(tp);
9193
David S. Millerf47c11e2005-06-24 20:18:35 -07009194 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009195
Michael Chan79381092005-04-21 17:13:59 -07009196 add_timer(&tp->timer);
9197 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009198 tg3_enable_ints(tp);
9199
David S. Millerf47c11e2005-06-24 20:18:35 -07009200 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009201
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009202 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009203
9204 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +00009205
Matt Carlson679563f2009-09-01 12:55:46 +00009206err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +00009207 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9208 struct tg3_napi *tnapi = &tp->napi[i];
9209 free_irq(tnapi->irq_vec, tnapi);
9210 }
Matt Carlson07b01732009-08-28 14:01:15 +00009211
Matt Carlson679563f2009-09-01 12:55:46 +00009212err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +00009213 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009214 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00009215 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +00009216
9217err_out1:
9218 tg3_ints_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00009219 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009220}
9221
Eric Dumazet511d2222010-07-07 20:44:24 +00009222static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *,
9223 struct rtnl_link_stats64 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009224static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *);
9225
9226static int tg3_close(struct net_device *dev)
9227{
Matt Carlson4f125f42009-09-01 12:55:02 +00009228 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009229 struct tg3 *tp = netdev_priv(dev);
9230
Matt Carlsonfed97812009-09-01 13:10:19 +00009231 tg3_napi_disable(tp);
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07009232 cancel_work_sync(&tp->reset_task);
Michael Chan7faa0062006-02-02 17:29:28 -08009233
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009234 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009235
9236 del_timer_sync(&tp->timer);
9237
Matt Carlson24bb4fb2009-10-05 17:55:29 +00009238 tg3_phy_stop(tp);
9239
David S. Millerf47c11e2005-06-24 20:18:35 -07009240 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009241
9242 tg3_disable_ints(tp);
9243
Michael Chan944d9802005-05-29 14:57:48 -07009244 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009245 tg3_free_rings(tp);
Michael Chan5cf64b8a2007-05-05 12:11:21 -07009246 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009247
David S. Millerf47c11e2005-06-24 20:18:35 -07009248 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009249
Matt Carlson4f125f42009-09-01 12:55:02 +00009250 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9251 struct tg3_napi *tnapi = &tp->napi[i];
9252 free_irq(tnapi->irq_vec, tnapi);
9253 }
Matt Carlson07b01732009-08-28 14:01:15 +00009254
9255 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009256
Eric Dumazet511d2222010-07-07 20:44:24 +00009257 tg3_get_stats64(tp->dev, &tp->net_stats_prev);
9258
Linus Torvalds1da177e2005-04-16 15:20:36 -07009259 memcpy(&tp->estats_prev, tg3_get_estats(tp),
9260 sizeof(tp->estats_prev));
9261
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009262 tg3_napi_fini(tp);
9263
Linus Torvalds1da177e2005-04-16 15:20:36 -07009264 tg3_free_consistent(tp);
9265
Michael Chanbc1c7562006-03-20 17:48:03 -08009266 tg3_set_power_state(tp, PCI_D3hot);
9267
9268 netif_carrier_off(tp->dev);
9269
Linus Torvalds1da177e2005-04-16 15:20:36 -07009270 return 0;
9271}
9272
Eric Dumazet511d2222010-07-07 20:44:24 +00009273static inline u64 get_stat64(tg3_stat64_t *val)
Stefan Buehler816f8b82008-08-15 14:10:54 -07009274{
9275 return ((u64)val->high << 32) | ((u64)val->low);
9276}
9277
Eric Dumazet511d2222010-07-07 20:44:24 +00009278static u64 calc_crc_errors(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009279{
9280 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9281
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009282 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009283 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
9284 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009285 u32 val;
9286
David S. Millerf47c11e2005-06-24 20:18:35 -07009287 spin_lock_bh(&tp->lock);
Michael Chan569a5df2007-02-13 12:18:15 -08009288 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
9289 tg3_writephy(tp, MII_TG3_TEST1,
9290 val | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009291 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009292 } else
9293 val = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07009294 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009295
9296 tp->phy_crc_errors += val;
9297
9298 return tp->phy_crc_errors;
9299 }
9300
9301 return get_stat64(&hw_stats->rx_fcs_errors);
9302}
9303
9304#define ESTAT_ADD(member) \
9305 estats->member = old_estats->member + \
Eric Dumazet511d2222010-07-07 20:44:24 +00009306 get_stat64(&hw_stats->member)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009307
9308static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *tp)
9309{
9310 struct tg3_ethtool_stats *estats = &tp->estats;
9311 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
9312 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9313
9314 if (!hw_stats)
9315 return old_estats;
9316
9317 ESTAT_ADD(rx_octets);
9318 ESTAT_ADD(rx_fragments);
9319 ESTAT_ADD(rx_ucast_packets);
9320 ESTAT_ADD(rx_mcast_packets);
9321 ESTAT_ADD(rx_bcast_packets);
9322 ESTAT_ADD(rx_fcs_errors);
9323 ESTAT_ADD(rx_align_errors);
9324 ESTAT_ADD(rx_xon_pause_rcvd);
9325 ESTAT_ADD(rx_xoff_pause_rcvd);
9326 ESTAT_ADD(rx_mac_ctrl_rcvd);
9327 ESTAT_ADD(rx_xoff_entered);
9328 ESTAT_ADD(rx_frame_too_long_errors);
9329 ESTAT_ADD(rx_jabbers);
9330 ESTAT_ADD(rx_undersize_packets);
9331 ESTAT_ADD(rx_in_length_errors);
9332 ESTAT_ADD(rx_out_length_errors);
9333 ESTAT_ADD(rx_64_or_less_octet_packets);
9334 ESTAT_ADD(rx_65_to_127_octet_packets);
9335 ESTAT_ADD(rx_128_to_255_octet_packets);
9336 ESTAT_ADD(rx_256_to_511_octet_packets);
9337 ESTAT_ADD(rx_512_to_1023_octet_packets);
9338 ESTAT_ADD(rx_1024_to_1522_octet_packets);
9339 ESTAT_ADD(rx_1523_to_2047_octet_packets);
9340 ESTAT_ADD(rx_2048_to_4095_octet_packets);
9341 ESTAT_ADD(rx_4096_to_8191_octet_packets);
9342 ESTAT_ADD(rx_8192_to_9022_octet_packets);
9343
9344 ESTAT_ADD(tx_octets);
9345 ESTAT_ADD(tx_collisions);
9346 ESTAT_ADD(tx_xon_sent);
9347 ESTAT_ADD(tx_xoff_sent);
9348 ESTAT_ADD(tx_flow_control);
9349 ESTAT_ADD(tx_mac_errors);
9350 ESTAT_ADD(tx_single_collisions);
9351 ESTAT_ADD(tx_mult_collisions);
9352 ESTAT_ADD(tx_deferred);
9353 ESTAT_ADD(tx_excessive_collisions);
9354 ESTAT_ADD(tx_late_collisions);
9355 ESTAT_ADD(tx_collide_2times);
9356 ESTAT_ADD(tx_collide_3times);
9357 ESTAT_ADD(tx_collide_4times);
9358 ESTAT_ADD(tx_collide_5times);
9359 ESTAT_ADD(tx_collide_6times);
9360 ESTAT_ADD(tx_collide_7times);
9361 ESTAT_ADD(tx_collide_8times);
9362 ESTAT_ADD(tx_collide_9times);
9363 ESTAT_ADD(tx_collide_10times);
9364 ESTAT_ADD(tx_collide_11times);
9365 ESTAT_ADD(tx_collide_12times);
9366 ESTAT_ADD(tx_collide_13times);
9367 ESTAT_ADD(tx_collide_14times);
9368 ESTAT_ADD(tx_collide_15times);
9369 ESTAT_ADD(tx_ucast_packets);
9370 ESTAT_ADD(tx_mcast_packets);
9371 ESTAT_ADD(tx_bcast_packets);
9372 ESTAT_ADD(tx_carrier_sense_errors);
9373 ESTAT_ADD(tx_discards);
9374 ESTAT_ADD(tx_errors);
9375
9376 ESTAT_ADD(dma_writeq_full);
9377 ESTAT_ADD(dma_write_prioq_full);
9378 ESTAT_ADD(rxbds_empty);
9379 ESTAT_ADD(rx_discards);
9380 ESTAT_ADD(rx_errors);
9381 ESTAT_ADD(rx_threshold_hit);
9382
9383 ESTAT_ADD(dma_readq_full);
9384 ESTAT_ADD(dma_read_prioq_full);
9385 ESTAT_ADD(tx_comp_queue_full);
9386
9387 ESTAT_ADD(ring_set_send_prod_index);
9388 ESTAT_ADD(ring_status_update);
9389 ESTAT_ADD(nic_irqs);
9390 ESTAT_ADD(nic_avoided_irqs);
9391 ESTAT_ADD(nic_tx_threshold_hit);
9392
9393 return estats;
9394}
9395
Eric Dumazet511d2222010-07-07 20:44:24 +00009396static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
9397 struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009398{
9399 struct tg3 *tp = netdev_priv(dev);
Eric Dumazet511d2222010-07-07 20:44:24 +00009400 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009401 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9402
9403 if (!hw_stats)
9404 return old_stats;
9405
9406 stats->rx_packets = old_stats->rx_packets +
9407 get_stat64(&hw_stats->rx_ucast_packets) +
9408 get_stat64(&hw_stats->rx_mcast_packets) +
9409 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009410
Linus Torvalds1da177e2005-04-16 15:20:36 -07009411 stats->tx_packets = old_stats->tx_packets +
9412 get_stat64(&hw_stats->tx_ucast_packets) +
9413 get_stat64(&hw_stats->tx_mcast_packets) +
9414 get_stat64(&hw_stats->tx_bcast_packets);
9415
9416 stats->rx_bytes = old_stats->rx_bytes +
9417 get_stat64(&hw_stats->rx_octets);
9418 stats->tx_bytes = old_stats->tx_bytes +
9419 get_stat64(&hw_stats->tx_octets);
9420
9421 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -07009422 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009423 stats->tx_errors = old_stats->tx_errors +
9424 get_stat64(&hw_stats->tx_errors) +
9425 get_stat64(&hw_stats->tx_mac_errors) +
9426 get_stat64(&hw_stats->tx_carrier_sense_errors) +
9427 get_stat64(&hw_stats->tx_discards);
9428
9429 stats->multicast = old_stats->multicast +
9430 get_stat64(&hw_stats->rx_mcast_packets);
9431 stats->collisions = old_stats->collisions +
9432 get_stat64(&hw_stats->tx_collisions);
9433
9434 stats->rx_length_errors = old_stats->rx_length_errors +
9435 get_stat64(&hw_stats->rx_frame_too_long_errors) +
9436 get_stat64(&hw_stats->rx_undersize_packets);
9437
9438 stats->rx_over_errors = old_stats->rx_over_errors +
9439 get_stat64(&hw_stats->rxbds_empty);
9440 stats->rx_frame_errors = old_stats->rx_frame_errors +
9441 get_stat64(&hw_stats->rx_align_errors);
9442 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
9443 get_stat64(&hw_stats->tx_discards);
9444 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
9445 get_stat64(&hw_stats->tx_carrier_sense_errors);
9446
9447 stats->rx_crc_errors = old_stats->rx_crc_errors +
9448 calc_crc_errors(tp);
9449
John W. Linville4f63b872005-09-12 14:43:18 -07009450 stats->rx_missed_errors = old_stats->rx_missed_errors +
9451 get_stat64(&hw_stats->rx_discards);
9452
Linus Torvalds1da177e2005-04-16 15:20:36 -07009453 return stats;
9454}
9455
9456static inline u32 calc_crc(unsigned char *buf, int len)
9457{
9458 u32 reg;
9459 u32 tmp;
9460 int j, k;
9461
9462 reg = 0xffffffff;
9463
9464 for (j = 0; j < len; j++) {
9465 reg ^= buf[j];
9466
9467 for (k = 0; k < 8; k++) {
9468 tmp = reg & 0x01;
9469
9470 reg >>= 1;
9471
Matt Carlson859a588792010-04-05 10:19:28 +00009472 if (tmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009473 reg ^= 0xedb88320;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009474 }
9475 }
9476
9477 return ~reg;
9478}
9479
9480static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
9481{
9482 /* accept or reject all multicast frames */
9483 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
9484 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
9485 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
9486 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
9487}
9488
9489static void __tg3_set_rx_mode(struct net_device *dev)
9490{
9491 struct tg3 *tp = netdev_priv(dev);
9492 u32 rx_mode;
9493
9494 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
9495 RX_MODE_KEEP_VLAN_TAG);
9496
9497 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
9498 * flag clear.
9499 */
9500#if TG3_VLAN_TAG_USED
9501 if (!tp->vlgrp &&
9502 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
9503 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9504#else
9505 /* By definition, VLAN is disabled always in this
9506 * case.
9507 */
9508 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
9509 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9510#endif
9511
9512 if (dev->flags & IFF_PROMISC) {
9513 /* Promiscuous mode. */
9514 rx_mode |= RX_MODE_PROMISC;
9515 } else if (dev->flags & IFF_ALLMULTI) {
9516 /* Accept all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +00009517 tg3_set_multi(tp, 1);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00009518 } else if (netdev_mc_empty(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009519 /* Reject all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +00009520 tg3_set_multi(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009521 } else {
9522 /* Accept one or more multicast(s). */
Jiri Pirko22bedad32010-04-01 21:22:57 +00009523 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009524 u32 mc_filter[4] = { 0, };
9525 u32 regidx;
9526 u32 bit;
9527 u32 crc;
9528
Jiri Pirko22bedad32010-04-01 21:22:57 +00009529 netdev_for_each_mc_addr(ha, dev) {
9530 crc = calc_crc(ha->addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009531 bit = ~crc & 0x7f;
9532 regidx = (bit & 0x60) >> 5;
9533 bit &= 0x1f;
9534 mc_filter[regidx] |= (1 << bit);
9535 }
9536
9537 tw32(MAC_HASH_REG_0, mc_filter[0]);
9538 tw32(MAC_HASH_REG_1, mc_filter[1]);
9539 tw32(MAC_HASH_REG_2, mc_filter[2]);
9540 tw32(MAC_HASH_REG_3, mc_filter[3]);
9541 }
9542
9543 if (rx_mode != tp->rx_mode) {
9544 tp->rx_mode = rx_mode;
9545 tw32_f(MAC_RX_MODE, rx_mode);
9546 udelay(10);
9547 }
9548}
9549
9550static void tg3_set_rx_mode(struct net_device *dev)
9551{
9552 struct tg3 *tp = netdev_priv(dev);
9553
Michael Chane75f7c92006-03-20 21:33:26 -08009554 if (!netif_running(dev))
9555 return;
9556
David S. Millerf47c11e2005-06-24 20:18:35 -07009557 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009558 __tg3_set_rx_mode(dev);
David S. Millerf47c11e2005-06-24 20:18:35 -07009559 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009560}
9561
9562#define TG3_REGDUMP_LEN (32 * 1024)
9563
9564static int tg3_get_regs_len(struct net_device *dev)
9565{
9566 return TG3_REGDUMP_LEN;
9567}
9568
9569static void tg3_get_regs(struct net_device *dev,
9570 struct ethtool_regs *regs, void *_p)
9571{
9572 u32 *p = _p;
9573 struct tg3 *tp = netdev_priv(dev);
9574 u8 *orig_p = _p;
9575 int i;
9576
9577 regs->version = 0;
9578
9579 memset(p, 0, TG3_REGDUMP_LEN);
9580
Matt Carlson800960682010-08-02 11:26:06 +00009581 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -08009582 return;
9583
David S. Millerf47c11e2005-06-24 20:18:35 -07009584 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009585
9586#define __GET_REG32(reg) (*(p)++ = tr32(reg))
Matt Carlsonbe98da62010-07-11 09:31:46 +00009587#define GET_REG32_LOOP(base, len) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07009588do { p = (u32 *)(orig_p + (base)); \
9589 for (i = 0; i < len; i += 4) \
9590 __GET_REG32((base) + i); \
9591} while (0)
9592#define GET_REG32_1(reg) \
9593do { p = (u32 *)(orig_p + (reg)); \
9594 __GET_REG32((reg)); \
9595} while (0)
9596
9597 GET_REG32_LOOP(TG3PCI_VENDOR, 0xb0);
9598 GET_REG32_LOOP(MAILBOX_INTERRUPT_0, 0x200);
9599 GET_REG32_LOOP(MAC_MODE, 0x4f0);
9600 GET_REG32_LOOP(SNDDATAI_MODE, 0xe0);
9601 GET_REG32_1(SNDDATAC_MODE);
9602 GET_REG32_LOOP(SNDBDS_MODE, 0x80);
9603 GET_REG32_LOOP(SNDBDI_MODE, 0x48);
9604 GET_REG32_1(SNDBDC_MODE);
9605 GET_REG32_LOOP(RCVLPC_MODE, 0x20);
9606 GET_REG32_LOOP(RCVLPC_SELLST_BASE, 0x15c);
9607 GET_REG32_LOOP(RCVDBDI_MODE, 0x0c);
9608 GET_REG32_LOOP(RCVDBDI_JUMBO_BD, 0x3c);
9609 GET_REG32_LOOP(RCVDBDI_BD_PROD_IDX_0, 0x44);
9610 GET_REG32_1(RCVDCC_MODE);
9611 GET_REG32_LOOP(RCVBDI_MODE, 0x20);
9612 GET_REG32_LOOP(RCVCC_MODE, 0x14);
9613 GET_REG32_LOOP(RCVLSC_MODE, 0x08);
9614 GET_REG32_1(MBFREE_MODE);
9615 GET_REG32_LOOP(HOSTCC_MODE, 0x100);
9616 GET_REG32_LOOP(MEMARB_MODE, 0x10);
9617 GET_REG32_LOOP(BUFMGR_MODE, 0x58);
9618 GET_REG32_LOOP(RDMAC_MODE, 0x08);
9619 GET_REG32_LOOP(WDMAC_MODE, 0x08);
Chris Elmquist091465d2005-12-20 13:25:19 -08009620 GET_REG32_1(RX_CPU_MODE);
9621 GET_REG32_1(RX_CPU_STATE);
9622 GET_REG32_1(RX_CPU_PGMCTR);
9623 GET_REG32_1(RX_CPU_HWBKPT);
9624 GET_REG32_1(TX_CPU_MODE);
9625 GET_REG32_1(TX_CPU_STATE);
9626 GET_REG32_1(TX_CPU_PGMCTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009627 GET_REG32_LOOP(GRCMBOX_INTERRUPT_0, 0x110);
9628 GET_REG32_LOOP(FTQ_RESET, 0x120);
9629 GET_REG32_LOOP(MSGINT_MODE, 0x0c);
9630 GET_REG32_1(DMAC_MODE);
9631 GET_REG32_LOOP(GRC_MODE, 0x4c);
9632 if (tp->tg3_flags & TG3_FLAG_NVRAM)
9633 GET_REG32_LOOP(NVRAM_CMD, 0x24);
9634
9635#undef __GET_REG32
9636#undef GET_REG32_LOOP
9637#undef GET_REG32_1
9638
David S. Millerf47c11e2005-06-24 20:18:35 -07009639 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009640}
9641
9642static int tg3_get_eeprom_len(struct net_device *dev)
9643{
9644 struct tg3 *tp = netdev_priv(dev);
9645
9646 return tp->nvram_size;
9647}
9648
Linus Torvalds1da177e2005-04-16 15:20:36 -07009649static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9650{
9651 struct tg3 *tp = netdev_priv(dev);
9652 int ret;
9653 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -08009654 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009655 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009656
Matt Carlsondf259d82009-04-20 06:57:14 +00009657 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM)
9658 return -EINVAL;
9659
Matt Carlson800960682010-08-02 11:26:06 +00009660 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -08009661 return -EAGAIN;
9662
Linus Torvalds1da177e2005-04-16 15:20:36 -07009663 offset = eeprom->offset;
9664 len = eeprom->len;
9665 eeprom->len = 0;
9666
9667 eeprom->magic = TG3_EEPROM_MAGIC;
9668
9669 if (offset & 3) {
9670 /* adjustments to start on required 4 byte boundary */
9671 b_offset = offset & 3;
9672 b_count = 4 - b_offset;
9673 if (b_count > len) {
9674 /* i.e. offset=1 len=2 */
9675 b_count = len;
9676 }
Matt Carlsona9dc5292009-02-25 14:25:30 +00009677 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009678 if (ret)
9679 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +00009680 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009681 len -= b_count;
9682 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00009683 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009684 }
9685
9686 /* read bytes upto the last 4 byte boundary */
9687 pd = &data[eeprom->len];
9688 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +00009689 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009690 if (ret) {
9691 eeprom->len += i;
9692 return ret;
9693 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009694 memcpy(pd + i, &val, 4);
9695 }
9696 eeprom->len += i;
9697
9698 if (len & 3) {
9699 /* read last bytes not ending on 4 byte boundary */
9700 pd = &data[eeprom->len];
9701 b_count = len & 3;
9702 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009703 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009704 if (ret)
9705 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009706 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009707 eeprom->len += b_count;
9708 }
9709 return 0;
9710}
9711
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009712static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009713
9714static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9715{
9716 struct tg3 *tp = netdev_priv(dev);
9717 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009718 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009719 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009720 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009721
Matt Carlson800960682010-08-02 11:26:06 +00009722 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -08009723 return -EAGAIN;
9724
Matt Carlsondf259d82009-04-20 06:57:14 +00009725 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
9726 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009727 return -EINVAL;
9728
9729 offset = eeprom->offset;
9730 len = eeprom->len;
9731
9732 if ((b_offset = (offset & 3))) {
9733 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +00009734 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009735 if (ret)
9736 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009737 len += b_offset;
9738 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -07009739 if (len < 4)
9740 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009741 }
9742
9743 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -07009744 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009745 /* adjustments to end on required 4 byte boundary */
9746 odd_len = 1;
9747 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009748 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009749 if (ret)
9750 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009751 }
9752
9753 buf = data;
9754 if (b_offset || odd_len) {
9755 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009756 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009757 return -ENOMEM;
9758 if (b_offset)
9759 memcpy(buf, &start, 4);
9760 if (odd_len)
9761 memcpy(buf+len-4, &end, 4);
9762 memcpy(buf + b_offset, data, eeprom->len);
9763 }
9764
9765 ret = tg3_nvram_write_block(tp, offset, len, buf);
9766
9767 if (buf != data)
9768 kfree(buf);
9769
9770 return ret;
9771}
9772
9773static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9774{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009775 struct tg3 *tp = netdev_priv(dev);
9776
9777 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009778 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009779 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009780 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009781 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
9782 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009783 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009784
Linus Torvalds1da177e2005-04-16 15:20:36 -07009785 cmd->supported = (SUPPORTED_Autoneg);
9786
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009787 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009788 cmd->supported |= (SUPPORTED_1000baseT_Half |
9789 SUPPORTED_1000baseT_Full);
9790
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009791 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009792 cmd->supported |= (SUPPORTED_100baseT_Half |
9793 SUPPORTED_100baseT_Full |
9794 SUPPORTED_10baseT_Half |
9795 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -08009796 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -07009797 cmd->port = PORT_TP;
9798 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009799 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -07009800 cmd->port = PORT_FIBRE;
9801 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009802
Linus Torvalds1da177e2005-04-16 15:20:36 -07009803 cmd->advertising = tp->link_config.advertising;
9804 if (netif_running(dev)) {
9805 cmd->speed = tp->link_config.active_speed;
9806 cmd->duplex = tp->link_config.active_duplex;
9807 }
Matt Carlson882e9792009-09-01 13:21:36 +00009808 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +00009809 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009810 cmd->autoneg = tp->link_config.autoneg;
9811 cmd->maxtxpkt = 0;
9812 cmd->maxrxpkt = 0;
9813 return 0;
9814}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009815
Linus Torvalds1da177e2005-04-16 15:20:36 -07009816static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9817{
9818 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009819
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009820 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009821 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009822 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009823 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009824 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
9825 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009826 }
9827
Matt Carlson7e5856b2009-02-25 14:23:01 +00009828 if (cmd->autoneg != AUTONEG_ENABLE &&
9829 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -07009830 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +00009831
9832 if (cmd->autoneg == AUTONEG_DISABLE &&
9833 cmd->duplex != DUPLEX_FULL &&
9834 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -07009835 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009836
Matt Carlson7e5856b2009-02-25 14:23:01 +00009837 if (cmd->autoneg == AUTONEG_ENABLE) {
9838 u32 mask = ADVERTISED_Autoneg |
9839 ADVERTISED_Pause |
9840 ADVERTISED_Asym_Pause;
9841
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009842 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +00009843 mask |= ADVERTISED_1000baseT_Half |
9844 ADVERTISED_1000baseT_Full;
9845
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009846 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +00009847 mask |= ADVERTISED_100baseT_Half |
9848 ADVERTISED_100baseT_Full |
9849 ADVERTISED_10baseT_Half |
9850 ADVERTISED_10baseT_Full |
9851 ADVERTISED_TP;
9852 else
9853 mask |= ADVERTISED_FIBRE;
9854
9855 if (cmd->advertising & ~mask)
9856 return -EINVAL;
9857
9858 mask &= (ADVERTISED_1000baseT_Half |
9859 ADVERTISED_1000baseT_Full |
9860 ADVERTISED_100baseT_Half |
9861 ADVERTISED_100baseT_Full |
9862 ADVERTISED_10baseT_Half |
9863 ADVERTISED_10baseT_Full);
9864
9865 cmd->advertising &= mask;
9866 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009867 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
Matt Carlson7e5856b2009-02-25 14:23:01 +00009868 if (cmd->speed != SPEED_1000)
9869 return -EINVAL;
9870
9871 if (cmd->duplex != DUPLEX_FULL)
9872 return -EINVAL;
9873 } else {
9874 if (cmd->speed != SPEED_100 &&
9875 cmd->speed != SPEED_10)
9876 return -EINVAL;
9877 }
9878 }
9879
David S. Millerf47c11e2005-06-24 20:18:35 -07009880 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009881
9882 tp->link_config.autoneg = cmd->autoneg;
9883 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -07009884 tp->link_config.advertising = (cmd->advertising |
9885 ADVERTISED_Autoneg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009886 tp->link_config.speed = SPEED_INVALID;
9887 tp->link_config.duplex = DUPLEX_INVALID;
9888 } else {
9889 tp->link_config.advertising = 0;
9890 tp->link_config.speed = cmd->speed;
9891 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009892 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009893
Michael Chan24fcad62006-12-17 17:06:46 -08009894 tp->link_config.orig_speed = tp->link_config.speed;
9895 tp->link_config.orig_duplex = tp->link_config.duplex;
9896 tp->link_config.orig_autoneg = tp->link_config.autoneg;
9897
Linus Torvalds1da177e2005-04-16 15:20:36 -07009898 if (netif_running(dev))
9899 tg3_setup_phy(tp, 1);
9900
David S. Millerf47c11e2005-06-24 20:18:35 -07009901 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009902
Linus Torvalds1da177e2005-04-16 15:20:36 -07009903 return 0;
9904}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009905
Linus Torvalds1da177e2005-04-16 15:20:36 -07009906static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
9907{
9908 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009909
Linus Torvalds1da177e2005-04-16 15:20:36 -07009910 strcpy(info->driver, DRV_MODULE_NAME);
9911 strcpy(info->version, DRV_MODULE_VERSION);
Michael Chanc4e65752006-03-20 22:29:32 -08009912 strcpy(info->fw_version, tp->fw_ver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009913 strcpy(info->bus_info, pci_name(tp->pdev));
9914}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009915
Linus Torvalds1da177e2005-04-16 15:20:36 -07009916static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
9917{
9918 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009919
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009920 if ((tp->tg3_flags & TG3_FLAG_WOL_CAP) &&
9921 device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -07009922 wol->supported = WAKE_MAGIC;
9923 else
9924 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009925 wol->wolopts = 0;
Matt Carlson05ac4cb2008-11-03 16:53:46 -08009926 if ((tp->tg3_flags & TG3_FLAG_WOL_ENABLE) &&
9927 device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009928 wol->wolopts = WAKE_MAGIC;
9929 memset(&wol->sopass, 0, sizeof(wol->sopass));
9930}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009931
Linus Torvalds1da177e2005-04-16 15:20:36 -07009932static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
9933{
9934 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009935 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009936
Linus Torvalds1da177e2005-04-16 15:20:36 -07009937 if (wol->wolopts & ~WAKE_MAGIC)
9938 return -EINVAL;
9939 if ((wol->wolopts & WAKE_MAGIC) &&
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009940 !((tp->tg3_flags & TG3_FLAG_WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009941 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009942
David S. Millerf47c11e2005-06-24 20:18:35 -07009943 spin_lock_bh(&tp->lock);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009944 if (wol->wolopts & WAKE_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009945 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009946 device_set_wakeup_enable(dp, true);
9947 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009948 tp->tg3_flags &= ~TG3_FLAG_WOL_ENABLE;
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009949 device_set_wakeup_enable(dp, false);
9950 }
David S. Millerf47c11e2005-06-24 20:18:35 -07009951 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009952
Linus Torvalds1da177e2005-04-16 15:20:36 -07009953 return 0;
9954}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009955
Linus Torvalds1da177e2005-04-16 15:20:36 -07009956static u32 tg3_get_msglevel(struct net_device *dev)
9957{
9958 struct tg3 *tp = netdev_priv(dev);
9959 return tp->msg_enable;
9960}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009961
Linus Torvalds1da177e2005-04-16 15:20:36 -07009962static void tg3_set_msglevel(struct net_device *dev, u32 value)
9963{
9964 struct tg3 *tp = netdev_priv(dev);
9965 tp->msg_enable = value;
9966}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009967
Linus Torvalds1da177e2005-04-16 15:20:36 -07009968static int tg3_set_tso(struct net_device *dev, u32 value)
9969{
9970 struct tg3 *tp = netdev_priv(dev);
9971
9972 if (!(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
9973 if (value)
9974 return -EINVAL;
9975 return 0;
9976 }
Matt Carlson027455a2008-12-21 20:19:30 -08009977 if ((dev->features & NETIF_F_IPV6_CSUM) &&
Matt Carlsone849cdc2009-11-13 13:03:38 +00009978 ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_2) ||
9979 (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3))) {
Matt Carlson9936bcf2007-10-10 18:03:07 -07009980 if (value) {
Michael Chanb0026622006-07-03 19:42:14 -07009981 dev->features |= NETIF_F_TSO6;
Matt Carlsone849cdc2009-11-13 13:03:38 +00009982 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
9983 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -07009984 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
9985 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Matt Carlson321d32a2008-11-21 17:22:19 -08009986 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsone849cdc2009-11-13 13:03:38 +00009987 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson9936bcf2007-10-10 18:03:07 -07009988 dev->features |= NETIF_F_TSO_ECN;
9989 } else
9990 dev->features &= ~(NETIF_F_TSO6 | NETIF_F_TSO_ECN);
Michael Chanb0026622006-07-03 19:42:14 -07009991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009992 return ethtool_op_set_tso(dev, value);
9993}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009994
Linus Torvalds1da177e2005-04-16 15:20:36 -07009995static int tg3_nway_reset(struct net_device *dev)
9996{
9997 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009998 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009999
Linus Torvalds1da177e2005-04-16 15:20:36 -070010000 if (!netif_running(dev))
10001 return -EAGAIN;
10002
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010003 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070010004 return -EINVAL;
10005
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010006 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010007 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010008 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010009 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010010 } else {
10011 u32 bmcr;
10012
10013 spin_lock_bh(&tp->lock);
10014 r = -EINVAL;
10015 tg3_readphy(tp, MII_BMCR, &bmcr);
10016 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
10017 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010018 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010019 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
10020 BMCR_ANENABLE);
10021 r = 0;
10022 }
10023 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010024 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010025
Linus Torvalds1da177e2005-04-16 15:20:36 -070010026 return r;
10027}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010028
Linus Torvalds1da177e2005-04-16 15:20:36 -070010029static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10030{
10031 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010032
Matt Carlson2c49a442010-09-30 10:34:35 +000010033 ering->rx_max_pending = tp->rx_std_ring_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010034 ering->rx_mini_max_pending = 0;
Michael Chan4f81c322006-03-20 21:33:42 -080010035 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE)
Matt Carlson2c49a442010-09-30 10:34:35 +000010036 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080010037 else
10038 ering->rx_jumbo_max_pending = 0;
10039
10040 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010041
10042 ering->rx_pending = tp->rx_pending;
10043 ering->rx_mini_pending = 0;
Michael Chan4f81c322006-03-20 21:33:42 -080010044 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE)
10045 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
10046 else
10047 ering->rx_jumbo_pending = 0;
10048
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010049 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010050}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010051
Linus Torvalds1da177e2005-04-16 15:20:36 -070010052static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10053{
10054 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000010055 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010056
Matt Carlson2c49a442010-09-30 10:34:35 +000010057 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
10058 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070010059 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
10060 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Michael Chan7f62ad52007-02-20 23:25:40 -080010061 ((tp->tg3_flags2 & TG3_FLG2_TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070010062 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010063 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010064
Michael Chanbbe832c2005-06-24 20:20:04 -070010065 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010066 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010067 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010068 irq_sync = 1;
10069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010070
Michael Chanbbe832c2005-06-24 20:20:04 -070010071 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010072
Linus Torvalds1da177e2005-04-16 15:20:36 -070010073 tp->rx_pending = ering->rx_pending;
10074
10075 if ((tp->tg3_flags2 & TG3_FLG2_MAX_RXPEND_64) &&
10076 tp->rx_pending > 63)
10077 tp->rx_pending = 63;
10078 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000010079
Matt Carlson6fd45cb2010-09-15 08:59:57 +000010080 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000010081 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010082
10083 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070010084 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -070010085 err = tg3_restart_hw(tp, 1);
10086 if (!err)
10087 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010088 }
10089
David S. Millerf47c11e2005-06-24 20:18:35 -070010090 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010091
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010092 if (irq_sync && !err)
10093 tg3_phy_start(tp);
10094
Michael Chanb9ec6c12006-07-25 16:37:27 -070010095 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010096}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010097
Linus Torvalds1da177e2005-04-16 15:20:36 -070010098static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10099{
10100 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010101
Linus Torvalds1da177e2005-04-16 15:20:36 -070010102 epause->autoneg = (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG) != 0;
Matt Carlson8d018622007-12-20 20:05:44 -080010103
Steve Glendinninge18ce342008-12-16 02:00:00 -080010104 if (tp->link_config.active_flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080010105 epause->rx_pause = 1;
10106 else
10107 epause->rx_pause = 0;
10108
Steve Glendinninge18ce342008-12-16 02:00:00 -080010109 if (tp->link_config.active_flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080010110 epause->tx_pause = 1;
10111 else
10112 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010113}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010114
Linus Torvalds1da177e2005-04-16 15:20:36 -070010115static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10116{
10117 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010118 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010119
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010120 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson27121682010-02-17 15:16:57 +000010121 u32 newadv;
10122 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010123
Matt Carlson27121682010-02-17 15:16:57 +000010124 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010125
Matt Carlson27121682010-02-17 15:16:57 +000010126 if (!(phydev->supported & SUPPORTED_Pause) ||
10127 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000010128 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000010129 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010130
Matt Carlson27121682010-02-17 15:16:57 +000010131 tp->link_config.flowctrl = 0;
10132 if (epause->rx_pause) {
10133 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010134
Matt Carlson27121682010-02-17 15:16:57 +000010135 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080010136 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000010137 newadv = ADVERTISED_Pause;
10138 } else
10139 newadv = ADVERTISED_Pause |
10140 ADVERTISED_Asym_Pause;
10141 } else if (epause->tx_pause) {
10142 tp->link_config.flowctrl |= FLOW_CTRL_TX;
10143 newadv = ADVERTISED_Asym_Pause;
10144 } else
10145 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010146
Matt Carlson27121682010-02-17 15:16:57 +000010147 if (epause->autoneg)
10148 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
10149 else
10150 tp->tg3_flags &= ~TG3_FLAG_PAUSE_AUTONEG;
10151
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010152 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000010153 u32 oldadv = phydev->advertising &
10154 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
10155 if (oldadv != newadv) {
10156 phydev->advertising &=
10157 ~(ADVERTISED_Pause |
10158 ADVERTISED_Asym_Pause);
10159 phydev->advertising |= newadv;
10160 if (phydev->autoneg) {
10161 /*
10162 * Always renegotiate the link to
10163 * inform our link partner of our
10164 * flow control settings, even if the
10165 * flow control is forced. Let
10166 * tg3_adjust_link() do the final
10167 * flow control setup.
10168 */
10169 return phy_start_aneg(phydev);
10170 }
10171 }
10172
10173 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010174 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000010175 } else {
10176 tp->link_config.orig_advertising &=
10177 ~(ADVERTISED_Pause |
10178 ADVERTISED_Asym_Pause);
10179 tp->link_config.orig_advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010180 }
10181 } else {
10182 int irq_sync = 0;
10183
10184 if (netif_running(dev)) {
10185 tg3_netif_stop(tp);
10186 irq_sync = 1;
10187 }
10188
10189 tg3_full_lock(tp, irq_sync);
10190
10191 if (epause->autoneg)
10192 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
10193 else
10194 tp->tg3_flags &= ~TG3_FLAG_PAUSE_AUTONEG;
10195 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010196 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010197 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010198 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010199 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010200 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010201 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010202 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010203
10204 if (netif_running(dev)) {
10205 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10206 err = tg3_restart_hw(tp, 1);
10207 if (!err)
10208 tg3_netif_start(tp);
10209 }
10210
10211 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010213
Michael Chanb9ec6c12006-07-25 16:37:27 -070010214 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010215}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010216
Linus Torvalds1da177e2005-04-16 15:20:36 -070010217static u32 tg3_get_rx_csum(struct net_device *dev)
10218{
10219 struct tg3 *tp = netdev_priv(dev);
10220 return (tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) != 0;
10221}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010222
Linus Torvalds1da177e2005-04-16 15:20:36 -070010223static int tg3_set_rx_csum(struct net_device *dev, u32 data)
10224{
10225 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010226
Linus Torvalds1da177e2005-04-16 15:20:36 -070010227 if (tp->tg3_flags & TG3_FLAG_BROKEN_CHECKSUMS) {
10228 if (data != 0)
10229 return -EINVAL;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010230 return 0;
10231 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010232
David S. Millerf47c11e2005-06-24 20:18:35 -070010233 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010234 if (data)
10235 tp->tg3_flags |= TG3_FLAG_RX_CHECKSUMS;
10236 else
10237 tp->tg3_flags &= ~TG3_FLAG_RX_CHECKSUMS;
David S. Millerf47c11e2005-06-24 20:18:35 -070010238 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010239
Linus Torvalds1da177e2005-04-16 15:20:36 -070010240 return 0;
10241}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010242
Linus Torvalds1da177e2005-04-16 15:20:36 -070010243static int tg3_set_tx_csum(struct net_device *dev, u32 data)
10244{
10245 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010246
Linus Torvalds1da177e2005-04-16 15:20:36 -070010247 if (tp->tg3_flags & TG3_FLAG_BROKEN_CHECKSUMS) {
10248 if (data != 0)
10249 return -EINVAL;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010250 return 0;
10251 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010252
Matt Carlson321d32a2008-11-21 17:22:19 -080010253 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Michael Chan6460d942007-07-14 19:07:52 -070010254 ethtool_op_set_tx_ipv6_csum(dev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010255 else
Michael Chan9c27dbd2006-03-20 22:28:27 -080010256 ethtool_op_set_tx_csum(dev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010257
10258 return 0;
10259}
10260
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010261static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010262{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010263 switch (sset) {
10264 case ETH_SS_TEST:
10265 return TG3_NUM_TEST;
10266 case ETH_SS_STATS:
10267 return TG3_NUM_STATS;
10268 default:
10269 return -EOPNOTSUPP;
10270 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070010271}
10272
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010273static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010274{
10275 switch (stringset) {
10276 case ETH_SS_STATS:
10277 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
10278 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070010279 case ETH_SS_TEST:
10280 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
10281 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010282 default:
10283 WARN_ON(1); /* we need a WARN() */
10284 break;
10285 }
10286}
10287
Michael Chan4009a932005-09-05 17:52:54 -070010288static int tg3_phys_id(struct net_device *dev, u32 data)
10289{
10290 struct tg3 *tp = netdev_priv(dev);
10291 int i;
10292
10293 if (!netif_running(tp->dev))
10294 return -EAGAIN;
10295
10296 if (data == 0)
Stephen Hemminger759afc32008-02-23 19:51:59 -080010297 data = UINT_MAX / 2;
Michael Chan4009a932005-09-05 17:52:54 -070010298
10299 for (i = 0; i < (data * 2); i++) {
10300 if ((i % 2) == 0)
10301 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10302 LED_CTRL_1000MBPS_ON |
10303 LED_CTRL_100MBPS_ON |
10304 LED_CTRL_10MBPS_ON |
10305 LED_CTRL_TRAFFIC_OVERRIDE |
10306 LED_CTRL_TRAFFIC_BLINK |
10307 LED_CTRL_TRAFFIC_LED);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010308
Michael Chan4009a932005-09-05 17:52:54 -070010309 else
10310 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10311 LED_CTRL_TRAFFIC_OVERRIDE);
10312
10313 if (msleep_interruptible(500))
10314 break;
10315 }
10316 tw32(MAC_LED_CTRL, tp->led_ctrl);
10317 return 0;
10318}
10319
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010320static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010321 struct ethtool_stats *estats, u64 *tmp_stats)
10322{
10323 struct tg3 *tp = netdev_priv(dev);
10324 memcpy(tmp_stats, tg3_get_estats(tp), sizeof(tp->estats));
10325}
10326
Michael Chan566f86a2005-05-29 14:56:58 -070010327#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080010328#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
10329#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
10330#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Michael Chanb16250e2006-09-27 16:10:14 -070010331#define NVRAM_SELFBOOT_HW_SIZE 0x20
10332#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070010333
10334static int tg3_test_nvram(struct tg3 *tp)
10335{
Al Virob9fc7dc2007-12-17 22:59:57 -080010336 u32 csum, magic;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010337 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010338 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070010339
Matt Carlsondf259d82009-04-20 06:57:14 +000010340 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM)
10341 return 0;
10342
Matt Carlsone4f34112009-02-25 14:25:00 +000010343 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080010344 return -EIO;
10345
Michael Chan1b277772006-03-20 22:27:48 -080010346 if (magic == TG3_EEPROM_MAGIC)
10347 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070010348 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080010349 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
10350 TG3_EEPROM_SB_FORMAT_1) {
10351 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
10352 case TG3_EEPROM_SB_REVISION_0:
10353 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
10354 break;
10355 case TG3_EEPROM_SB_REVISION_2:
10356 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
10357 break;
10358 case TG3_EEPROM_SB_REVISION_3:
10359 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
10360 break;
10361 default:
10362 return 0;
10363 }
10364 } else
Michael Chan1b277772006-03-20 22:27:48 -080010365 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070010366 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
10367 size = NVRAM_SELFBOOT_HW_SIZE;
10368 else
Michael Chan1b277772006-03-20 22:27:48 -080010369 return -EIO;
10370
10371 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070010372 if (buf == NULL)
10373 return -ENOMEM;
10374
Michael Chan1b277772006-03-20 22:27:48 -080010375 err = -EIO;
10376 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010377 err = tg3_nvram_read_be32(tp, i, &buf[j]);
10378 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070010379 break;
Michael Chan566f86a2005-05-29 14:56:58 -070010380 }
Michael Chan1b277772006-03-20 22:27:48 -080010381 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070010382 goto out;
10383
Michael Chan1b277772006-03-20 22:27:48 -080010384 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010385 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080010386 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010387 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080010388 u8 *buf8 = (u8 *) buf, csum8 = 0;
10389
Al Virob9fc7dc2007-12-17 22:59:57 -080010390 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080010391 TG3_EEPROM_SB_REVISION_2) {
10392 /* For rev 2, the csum doesn't include the MBA. */
10393 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
10394 csum8 += buf8[i];
10395 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
10396 csum8 += buf8[i];
10397 } else {
10398 for (i = 0; i < size; i++)
10399 csum8 += buf8[i];
10400 }
Michael Chan1b277772006-03-20 22:27:48 -080010401
Adrian Bunkad96b482006-04-05 22:21:04 -070010402 if (csum8 == 0) {
10403 err = 0;
10404 goto out;
10405 }
10406
10407 err = -EIO;
10408 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080010409 }
Michael Chan566f86a2005-05-29 14:56:58 -070010410
Al Virob9fc7dc2007-12-17 22:59:57 -080010411 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010412 TG3_EEPROM_MAGIC_HW) {
10413 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000010414 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070010415 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070010416
10417 /* Separate the parity bits and the data bytes. */
10418 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
10419 if ((i == 0) || (i == 8)) {
10420 int l;
10421 u8 msk;
10422
10423 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
10424 parity[k++] = buf8[i] & msk;
10425 i++;
Matt Carlson859a588792010-04-05 10:19:28 +000010426 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070010427 int l;
10428 u8 msk;
10429
10430 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
10431 parity[k++] = buf8[i] & msk;
10432 i++;
10433
10434 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
10435 parity[k++] = buf8[i] & msk;
10436 i++;
10437 }
10438 data[j++] = buf8[i];
10439 }
10440
10441 err = -EIO;
10442 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
10443 u8 hw8 = hweight8(data[i]);
10444
10445 if ((hw8 & 0x1) && parity[i])
10446 goto out;
10447 else if (!(hw8 & 0x1) && !parity[i])
10448 goto out;
10449 }
10450 err = 0;
10451 goto out;
10452 }
10453
Michael Chan566f86a2005-05-29 14:56:58 -070010454 /* Bootstrap checksum at offset 0x10 */
10455 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlsona9dc5292009-02-25 14:25:30 +000010456 if (csum != be32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070010457 goto out;
10458
10459 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
10460 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlsona9dc5292009-02-25 14:25:30 +000010461 if (csum != be32_to_cpu(buf[0xfc/4]))
10462 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070010463
10464 err = 0;
10465
10466out:
10467 kfree(buf);
10468 return err;
10469}
10470
Michael Chanca430072005-05-29 14:57:23 -070010471#define TG3_SERDES_TIMEOUT_SEC 2
10472#define TG3_COPPER_TIMEOUT_SEC 6
10473
10474static int tg3_test_link(struct tg3 *tp)
10475{
10476 int i, max;
10477
10478 if (!netif_running(tp->dev))
10479 return -ENODEV;
10480
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010481 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070010482 max = TG3_SERDES_TIMEOUT_SEC;
10483 else
10484 max = TG3_COPPER_TIMEOUT_SEC;
10485
10486 for (i = 0; i < max; i++) {
10487 if (netif_carrier_ok(tp->dev))
10488 return 0;
10489
10490 if (msleep_interruptible(1000))
10491 break;
10492 }
10493
10494 return -EIO;
10495}
10496
Michael Chana71116d2005-05-29 14:58:11 -070010497/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080010498static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070010499{
Michael Chanb16250e2006-09-27 16:10:14 -070010500 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070010501 u32 offset, read_mask, write_mask, val, save_val, read_val;
10502 static struct {
10503 u16 offset;
10504 u16 flags;
10505#define TG3_FL_5705 0x1
10506#define TG3_FL_NOT_5705 0x2
10507#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070010508#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070010509 u32 read_mask;
10510 u32 write_mask;
10511 } reg_tbl[] = {
10512 /* MAC Control Registers */
10513 { MAC_MODE, TG3_FL_NOT_5705,
10514 0x00000000, 0x00ef6f8c },
10515 { MAC_MODE, TG3_FL_5705,
10516 0x00000000, 0x01ef6b8c },
10517 { MAC_STATUS, TG3_FL_NOT_5705,
10518 0x03800107, 0x00000000 },
10519 { MAC_STATUS, TG3_FL_5705,
10520 0x03800100, 0x00000000 },
10521 { MAC_ADDR_0_HIGH, 0x0000,
10522 0x00000000, 0x0000ffff },
10523 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010524 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070010525 { MAC_RX_MTU_SIZE, 0x0000,
10526 0x00000000, 0x0000ffff },
10527 { MAC_TX_MODE, 0x0000,
10528 0x00000000, 0x00000070 },
10529 { MAC_TX_LENGTHS, 0x0000,
10530 0x00000000, 0x00003fff },
10531 { MAC_RX_MODE, TG3_FL_NOT_5705,
10532 0x00000000, 0x000007fc },
10533 { MAC_RX_MODE, TG3_FL_5705,
10534 0x00000000, 0x000007dc },
10535 { MAC_HASH_REG_0, 0x0000,
10536 0x00000000, 0xffffffff },
10537 { MAC_HASH_REG_1, 0x0000,
10538 0x00000000, 0xffffffff },
10539 { MAC_HASH_REG_2, 0x0000,
10540 0x00000000, 0xffffffff },
10541 { MAC_HASH_REG_3, 0x0000,
10542 0x00000000, 0xffffffff },
10543
10544 /* Receive Data and Receive BD Initiator Control Registers. */
10545 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
10546 0x00000000, 0xffffffff },
10547 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
10548 0x00000000, 0xffffffff },
10549 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
10550 0x00000000, 0x00000003 },
10551 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
10552 0x00000000, 0xffffffff },
10553 { RCVDBDI_STD_BD+0, 0x0000,
10554 0x00000000, 0xffffffff },
10555 { RCVDBDI_STD_BD+4, 0x0000,
10556 0x00000000, 0xffffffff },
10557 { RCVDBDI_STD_BD+8, 0x0000,
10558 0x00000000, 0xffff0002 },
10559 { RCVDBDI_STD_BD+0xc, 0x0000,
10560 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010561
Michael Chana71116d2005-05-29 14:58:11 -070010562 /* Receive BD Initiator Control Registers. */
10563 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
10564 0x00000000, 0xffffffff },
10565 { RCVBDI_STD_THRESH, TG3_FL_5705,
10566 0x00000000, 0x000003ff },
10567 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
10568 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010569
Michael Chana71116d2005-05-29 14:58:11 -070010570 /* Host Coalescing Control Registers. */
10571 { HOSTCC_MODE, TG3_FL_NOT_5705,
10572 0x00000000, 0x00000004 },
10573 { HOSTCC_MODE, TG3_FL_5705,
10574 0x00000000, 0x000000f6 },
10575 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
10576 0x00000000, 0xffffffff },
10577 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
10578 0x00000000, 0x000003ff },
10579 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
10580 0x00000000, 0xffffffff },
10581 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
10582 0x00000000, 0x000003ff },
10583 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
10584 0x00000000, 0xffffffff },
10585 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10586 0x00000000, 0x000000ff },
10587 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
10588 0x00000000, 0xffffffff },
10589 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10590 0x00000000, 0x000000ff },
10591 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
10592 0x00000000, 0xffffffff },
10593 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
10594 0x00000000, 0xffffffff },
10595 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10596 0x00000000, 0xffffffff },
10597 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10598 0x00000000, 0x000000ff },
10599 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10600 0x00000000, 0xffffffff },
10601 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10602 0x00000000, 0x000000ff },
10603 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
10604 0x00000000, 0xffffffff },
10605 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
10606 0x00000000, 0xffffffff },
10607 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
10608 0x00000000, 0xffffffff },
10609 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
10610 0x00000000, 0xffffffff },
10611 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
10612 0x00000000, 0xffffffff },
10613 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
10614 0xffffffff, 0x00000000 },
10615 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
10616 0xffffffff, 0x00000000 },
10617
10618 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070010619 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010620 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070010621 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010622 0x00000000, 0x007fffff },
10623 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
10624 0x00000000, 0x0000003f },
10625 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
10626 0x00000000, 0x000001ff },
10627 { BUFMGR_MB_HIGH_WATER, 0x0000,
10628 0x00000000, 0x000001ff },
10629 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
10630 0xffffffff, 0x00000000 },
10631 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
10632 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010633
Michael Chana71116d2005-05-29 14:58:11 -070010634 /* Mailbox Registers */
10635 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
10636 0x00000000, 0x000001ff },
10637 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
10638 0x00000000, 0x000001ff },
10639 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
10640 0x00000000, 0x000007ff },
10641 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
10642 0x00000000, 0x000001ff },
10643
10644 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
10645 };
10646
Michael Chanb16250e2006-09-27 16:10:14 -070010647 is_5705 = is_5750 = 0;
10648 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
Michael Chana71116d2005-05-29 14:58:11 -070010649 is_5705 = 1;
Michael Chanb16250e2006-09-27 16:10:14 -070010650 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
10651 is_5750 = 1;
10652 }
Michael Chana71116d2005-05-29 14:58:11 -070010653
10654 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
10655 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
10656 continue;
10657
10658 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
10659 continue;
10660
10661 if ((tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
10662 (reg_tbl[i].flags & TG3_FL_NOT_5788))
10663 continue;
10664
Michael Chanb16250e2006-09-27 16:10:14 -070010665 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
10666 continue;
10667
Michael Chana71116d2005-05-29 14:58:11 -070010668 offset = (u32) reg_tbl[i].offset;
10669 read_mask = reg_tbl[i].read_mask;
10670 write_mask = reg_tbl[i].write_mask;
10671
10672 /* Save the original register content */
10673 save_val = tr32(offset);
10674
10675 /* Determine the read-only value. */
10676 read_val = save_val & read_mask;
10677
10678 /* Write zero to the register, then make sure the read-only bits
10679 * are not changed and the read/write bits are all zeros.
10680 */
10681 tw32(offset, 0);
10682
10683 val = tr32(offset);
10684
10685 /* Test the read-only and read/write bits. */
10686 if (((val & read_mask) != read_val) || (val & write_mask))
10687 goto out;
10688
10689 /* Write ones to all the bits defined by RdMask and WrMask, then
10690 * make sure the read-only bits are not changed and the
10691 * read/write bits are all ones.
10692 */
10693 tw32(offset, read_mask | write_mask);
10694
10695 val = tr32(offset);
10696
10697 /* Test the read-only bits. */
10698 if ((val & read_mask) != read_val)
10699 goto out;
10700
10701 /* Test the read/write bits. */
10702 if ((val & write_mask) != write_mask)
10703 goto out;
10704
10705 tw32(offset, save_val);
10706 }
10707
10708 return 0;
10709
10710out:
Michael Chan9f88f292006-12-07 00:22:54 -080010711 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000010712 netdev_err(tp->dev,
10713 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070010714 tw32(offset, save_val);
10715 return -EIO;
10716}
10717
Michael Chan7942e1d2005-05-29 14:58:36 -070010718static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
10719{
Arjan van de Venf71e1302006-03-03 21:33:57 -050010720 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070010721 int i;
10722 u32 j;
10723
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020010724 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070010725 for (j = 0; j < len; j += 4) {
10726 u32 val;
10727
10728 tg3_write_mem(tp, offset + j, test_pattern[i]);
10729 tg3_read_mem(tp, offset + j, &val);
10730 if (val != test_pattern[i])
10731 return -EIO;
10732 }
10733 }
10734 return 0;
10735}
10736
10737static int tg3_test_memory(struct tg3 *tp)
10738{
10739 static struct mem_entry {
10740 u32 offset;
10741 u32 len;
10742 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080010743 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070010744 { 0x00002000, 0x1c000},
10745 { 0xffffffff, 0x00000}
10746 }, mem_tbl_5705[] = {
10747 { 0x00000100, 0x0000c},
10748 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070010749 { 0x00004000, 0x00800},
10750 { 0x00006000, 0x01000},
10751 { 0x00008000, 0x02000},
10752 { 0x00010000, 0x0e000},
10753 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080010754 }, mem_tbl_5755[] = {
10755 { 0x00000200, 0x00008},
10756 { 0x00004000, 0x00800},
10757 { 0x00006000, 0x00800},
10758 { 0x00008000, 0x02000},
10759 { 0x00010000, 0x0c000},
10760 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070010761 }, mem_tbl_5906[] = {
10762 { 0x00000200, 0x00008},
10763 { 0x00004000, 0x00400},
10764 { 0x00006000, 0x00400},
10765 { 0x00008000, 0x01000},
10766 { 0x00010000, 0x01000},
10767 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000010768 }, mem_tbl_5717[] = {
10769 { 0x00000200, 0x00008},
10770 { 0x00010000, 0x0a000},
10771 { 0x00020000, 0x13c00},
10772 { 0xffffffff, 0x00000}
10773 }, mem_tbl_57765[] = {
10774 { 0x00000200, 0x00008},
10775 { 0x00004000, 0x00800},
10776 { 0x00006000, 0x09800},
10777 { 0x00010000, 0x0a000},
10778 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070010779 };
10780 struct mem_entry *mem_tbl;
10781 int err = 0;
10782 int i;
10783
Matt Carlsona50d0792010-06-05 17:24:37 +000010784 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
10785 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlson8b5a6c42010-01-20 16:58:06 +000010786 mem_tbl = mem_tbl_5717;
10787 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
10788 mem_tbl = mem_tbl_57765;
10789 else if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Matt Carlson321d32a2008-11-21 17:22:19 -080010790 mem_tbl = mem_tbl_5755;
10791 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
10792 mem_tbl = mem_tbl_5906;
10793 else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
10794 mem_tbl = mem_tbl_5705;
10795 else
Michael Chan7942e1d2005-05-29 14:58:36 -070010796 mem_tbl = mem_tbl_570x;
10797
10798 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000010799 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
10800 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070010801 break;
10802 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010803
Michael Chan7942e1d2005-05-29 14:58:36 -070010804 return err;
10805}
10806
Michael Chan9f40dea2005-09-05 17:53:06 -070010807#define TG3_MAC_LOOPBACK 0
10808#define TG3_PHY_LOOPBACK 1
10809
10810static int tg3_run_loopback(struct tg3 *tp, int loopback_mode)
Michael Chanc76949a2005-05-29 14:58:59 -070010811{
Michael Chan9f40dea2005-09-05 17:53:06 -070010812 u32 mac_mode, rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010813 u32 desc_idx, coal_now;
Michael Chanc76949a2005-05-29 14:58:59 -070010814 struct sk_buff *skb, *rx_skb;
10815 u8 *tx_data;
10816 dma_addr_t map;
10817 int num_pkts, tx_len, rx_len, i, err;
10818 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000010819 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000010820 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070010821
Matt Carlsonc8873402010-02-12 14:47:11 +000010822 tnapi = &tp->napi[0];
10823 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000010824 if (tp->irq_cnt > 1) {
Matt Carlson1da85aa2010-09-30 10:34:34 +000010825 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)
10826 rnapi = &tp->napi[1];
Matt Carlsonc8873402010-02-12 14:47:11 +000010827 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
10828 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000010829 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010830 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000010831
Michael Chan9f40dea2005-09-05 17:53:06 -070010832 if (loopback_mode == TG3_MAC_LOOPBACK) {
Michael Chanc94e3942005-09-27 12:12:42 -070010833 /* HW errata - mac loopback fails in some cases on 5780.
10834 * Normal traffic and PHY loopback are not affected by
10835 * errata.
10836 */
10837 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780)
10838 return 0;
10839
Michael Chan9f40dea2005-09-05 17:53:06 -070010840 mac_mode = (tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK) |
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010841 MAC_MODE_PORT_INT_LPBACK;
10842 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
10843 mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010844 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
Michael Chan3f7045c2006-09-27 16:02:29 -070010845 mac_mode |= MAC_MODE_PORT_MODE_MII;
10846 else
10847 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chan9f40dea2005-09-05 17:53:06 -070010848 tw32(MAC_MODE, mac_mode);
10849 } else if (loopback_mode == TG3_PHY_LOOPBACK) {
Michael Chan3f7045c2006-09-27 16:02:29 -070010850 u32 val;
10851
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010852 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +000010853 tg3_phy_fet_toggle_apd(tp, false);
Michael Chan5d64ad32006-12-07 00:19:40 -080010854 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED100;
10855 } else
10856 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED1000;
Michael Chan3f7045c2006-09-27 16:02:29 -070010857
Matt Carlson9ef8ca92007-07-11 19:48:29 -070010858 tg3_phy_toggle_automdix(tp, 0);
10859
Michael Chan3f7045c2006-09-27 16:02:29 -070010860 tg3_writephy(tp, MII_BMCR, val);
Michael Chanc94e3942005-09-27 12:12:42 -070010861 udelay(40);
Michael Chan5d64ad32006-12-07 00:19:40 -080010862
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010863 mac_mode = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010864 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson1061b7c2010-02-12 14:47:12 +000010865 tg3_writephy(tp, MII_TG3_FET_PTEST,
10866 MII_TG3_FET_PTEST_FRC_TX_LINK |
10867 MII_TG3_FET_PTEST_FRC_TX_LOCK);
10868 /* The write needs to be flushed for the AC131 */
10869 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
10870 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
Michael Chan5d64ad32006-12-07 00:19:40 -080010871 mac_mode |= MAC_MODE_PORT_MODE_MII;
10872 } else
10873 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chanb16250e2006-09-27 16:10:14 -070010874
Michael Chanc94e3942005-09-27 12:12:42 -070010875 /* reset to prevent losing 1st rx packet intermittently */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010876 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Michael Chanc94e3942005-09-27 12:12:42 -070010877 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
10878 udelay(10);
10879 tw32_f(MAC_RX_MODE, tp->rx_mode);
10880 }
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010881 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlson79eb6902010-02-17 15:17:03 +000010882 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
10883 if (masked_phy_id == TG3_PHY_ID_BCM5401)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010884 mac_mode &= ~MAC_MODE_LINK_POLARITY;
Matt Carlson79eb6902010-02-17 15:17:03 +000010885 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010886 mac_mode |= MAC_MODE_LINK_POLARITY;
Michael Chanff18ff02006-03-27 23:17:27 -080010887 tg3_writephy(tp, MII_TG3_EXT_CTRL,
10888 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
10889 }
Michael Chan9f40dea2005-09-05 17:53:06 -070010890 tw32(MAC_MODE, mac_mode);
Matt Carlson859a588792010-04-05 10:19:28 +000010891 } else {
Michael Chan9f40dea2005-09-05 17:53:06 -070010892 return -EINVAL;
Matt Carlson859a588792010-04-05 10:19:28 +000010893 }
Michael Chanc76949a2005-05-29 14:58:59 -070010894
10895 err = -EIO;
10896
Michael Chanc76949a2005-05-29 14:58:59 -070010897 tx_len = 1514;
David S. Millera20e9c62006-07-31 22:38:16 -070010898 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070010899 if (!skb)
10900 return -ENOMEM;
10901
Michael Chanc76949a2005-05-29 14:58:59 -070010902 tx_data = skb_put(skb, tx_len);
10903 memcpy(tx_data, tp->dev->dev_addr, 6);
10904 memset(tx_data + 6, 0x0, 8);
10905
10906 tw32(MAC_RX_MTU_SIZE, tx_len + 4);
10907
10908 for (i = 14; i < tx_len; i++)
10909 tx_data[i] = (u8) (i & 0xff);
10910
Alexander Duyckf4188d82009-12-02 16:48:38 +000010911 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
10912 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000010913 dev_kfree_skb(skb);
10914 return -EIO;
10915 }
Michael Chanc76949a2005-05-29 14:58:59 -070010916
10917 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010918 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070010919
10920 udelay(10);
10921
Matt Carlson898a56f2009-08-28 14:02:40 +000010922 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070010923
Michael Chanc76949a2005-05-29 14:58:59 -070010924 num_pkts = 0;
10925
Alexander Duyckf4188d82009-12-02 16:48:38 +000010926 tg3_set_txd(tnapi, tnapi->tx_prod, map, tx_len, 0, 1);
Michael Chanc76949a2005-05-29 14:58:59 -070010927
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010928 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070010929 num_pkts++;
10930
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010931 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
10932 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070010933
10934 udelay(10);
10935
Matt Carlson303fc922009-11-02 14:27:34 +000010936 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
10937 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070010938 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010939 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070010940
10941 udelay(10);
10942
Matt Carlson898a56f2009-08-28 14:02:40 +000010943 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
10944 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010945 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070010946 (rx_idx == (rx_start_idx + num_pkts)))
10947 break;
10948 }
10949
Alexander Duyckf4188d82009-12-02 16:48:38 +000010950 pci_unmap_single(tp->pdev, map, tx_len, PCI_DMA_TODEVICE);
Michael Chanc76949a2005-05-29 14:58:59 -070010951 dev_kfree_skb(skb);
10952
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010953 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070010954 goto out;
10955
10956 if (rx_idx != rx_start_idx + num_pkts)
10957 goto out;
10958
Matt Carlson72334482009-08-28 14:03:01 +000010959 desc = &rnapi->rx_rcb[rx_start_idx];
Michael Chanc76949a2005-05-29 14:58:59 -070010960 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
10961 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
10962 if (opaque_key != RXD_OPAQUE_RING_STD)
10963 goto out;
10964
10965 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
10966 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
10967 goto out;
10968
10969 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) - 4;
10970 if (rx_len != tx_len)
10971 goto out;
10972
Matt Carlson21f581a2009-08-28 14:00:25 +000010973 rx_skb = tpr->rx_std_buffers[desc_idx].skb;
Michael Chanc76949a2005-05-29 14:58:59 -070010974
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +000010975 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx], mapping);
Michael Chanc76949a2005-05-29 14:58:59 -070010976 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len, PCI_DMA_FROMDEVICE);
10977
10978 for (i = 14; i < tx_len; i++) {
10979 if (*(rx_skb->data + i) != (u8) (i & 0xff))
10980 goto out;
10981 }
10982 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010983
Michael Chanc76949a2005-05-29 14:58:59 -070010984 /* tg3_free_rings will unmap and free the rx_skb */
10985out:
10986 return err;
10987}
10988
Michael Chan9f40dea2005-09-05 17:53:06 -070010989#define TG3_MAC_LOOPBACK_FAILED 1
10990#define TG3_PHY_LOOPBACK_FAILED 2
10991#define TG3_LOOPBACK_FAILED (TG3_MAC_LOOPBACK_FAILED | \
10992 TG3_PHY_LOOPBACK_FAILED)
10993
10994static int tg3_test_loopback(struct tg3 *tp)
10995{
10996 int err = 0;
Matt Carlson9936bcf2007-10-10 18:03:07 -070010997 u32 cpmuctrl = 0;
Michael Chan9f40dea2005-09-05 17:53:06 -070010998
10999 if (!netif_running(tp->dev))
11000 return TG3_LOOPBACK_FAILED;
11001
Michael Chanb9ec6c12006-07-25 16:37:27 -070011002 err = tg3_reset_hw(tp, 1);
11003 if (err)
11004 return TG3_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070011005
Matt Carlson6833c042008-11-21 17:18:59 -080011006 /* Turn off gphy autopowerdown. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011007 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -080011008 tg3_phy_toggle_apd(tp, false);
11009
Matt Carlson321d32a2008-11-21 17:22:19 -080011010 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070011011 int i;
11012 u32 status;
11013
11014 tw32(TG3_CPMU_MUTEX_REQ, CPMU_MUTEX_REQ_DRIVER);
11015
11016 /* Wait for up to 40 microseconds to acquire lock. */
11017 for (i = 0; i < 4; i++) {
11018 status = tr32(TG3_CPMU_MUTEX_GNT);
11019 if (status == CPMU_MUTEX_GNT_DRIVER)
11020 break;
11021 udelay(10);
11022 }
11023
11024 if (status != CPMU_MUTEX_GNT_DRIVER)
11025 return TG3_LOOPBACK_FAILED;
11026
Matt Carlsonb2a5c192008-04-03 21:44:44 -070011027 /* Turn off link-based power management. */
Matt Carlsone8750932007-11-12 21:11:51 -080011028 cpmuctrl = tr32(TG3_CPMU_CTRL);
Matt Carlson109115e2008-05-02 16:48:59 -070011029 tw32(TG3_CPMU_CTRL,
11030 cpmuctrl & ~(CPMU_CTRL_LINK_SPEED_MODE |
11031 CPMU_CTRL_LINK_AWARE_MODE));
Matt Carlson9936bcf2007-10-10 18:03:07 -070011032 }
11033
Michael Chan9f40dea2005-09-05 17:53:06 -070011034 if (tg3_run_loopback(tp, TG3_MAC_LOOPBACK))
11035 err |= TG3_MAC_LOOPBACK_FAILED;
Matt Carlson9936bcf2007-10-10 18:03:07 -070011036
Matt Carlson321d32a2008-11-21 17:22:19 -080011037 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070011038 tw32(TG3_CPMU_CTRL, cpmuctrl);
11039
11040 /* Release the mutex */
11041 tw32(TG3_CPMU_MUTEX_GNT, CPMU_MUTEX_GNT_DRIVER);
11042 }
11043
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011044 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Matt Carlsondd477002008-05-25 23:45:58 -070011045 !(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)) {
Michael Chan9f40dea2005-09-05 17:53:06 -070011046 if (tg3_run_loopback(tp, TG3_PHY_LOOPBACK))
11047 err |= TG3_PHY_LOOPBACK_FAILED;
11048 }
11049
Matt Carlson6833c042008-11-21 17:18:59 -080011050 /* Re-enable gphy autopowerdown. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011051 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -080011052 tg3_phy_toggle_apd(tp, true);
11053
Michael Chan9f40dea2005-09-05 17:53:06 -070011054 return err;
11055}
11056
Michael Chan4cafd3f2005-05-29 14:56:34 -070011057static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
11058 u64 *data)
11059{
Michael Chan566f86a2005-05-29 14:56:58 -070011060 struct tg3 *tp = netdev_priv(dev);
11061
Matt Carlson800960682010-08-02 11:26:06 +000011062 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080011063 tg3_set_power_state(tp, PCI_D0);
11064
Michael Chan566f86a2005-05-29 14:56:58 -070011065 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
11066
11067 if (tg3_test_nvram(tp) != 0) {
11068 etest->flags |= ETH_TEST_FL_FAILED;
11069 data[0] = 1;
11070 }
Michael Chanca430072005-05-29 14:57:23 -070011071 if (tg3_test_link(tp) != 0) {
11072 etest->flags |= ETH_TEST_FL_FAILED;
11073 data[1] = 1;
11074 }
Michael Chana71116d2005-05-29 14:58:11 -070011075 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011076 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070011077
Michael Chanbbe832c2005-06-24 20:20:04 -070011078 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011079 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070011080 tg3_netif_stop(tp);
11081 irq_sync = 1;
11082 }
11083
11084 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070011085
11086 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080011087 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011088 tg3_halt_cpu(tp, RX_CPU_BASE);
11089 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
11090 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080011091 if (!err)
11092 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011093
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011094 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad2006-03-20 22:27:35 -080011095 tg3_phy_reset(tp);
11096
Michael Chana71116d2005-05-29 14:58:11 -070011097 if (tg3_test_registers(tp) != 0) {
11098 etest->flags |= ETH_TEST_FL_FAILED;
11099 data[2] = 1;
11100 }
Michael Chan7942e1d2005-05-29 14:58:36 -070011101 if (tg3_test_memory(tp) != 0) {
11102 etest->flags |= ETH_TEST_FL_FAILED;
11103 data[3] = 1;
11104 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011105 if ((data[4] = tg3_test_loopback(tp)) != 0)
Michael Chanc76949a2005-05-29 14:58:59 -070011106 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070011107
David S. Millerf47c11e2005-06-24 20:18:35 -070011108 tg3_full_unlock(tp);
11109
Michael Chand4bc3922005-05-29 14:59:20 -070011110 if (tg3_test_interrupt(tp) != 0) {
11111 etest->flags |= ETH_TEST_FL_FAILED;
11112 data[5] = 1;
11113 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011114
11115 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070011116
Michael Chana71116d2005-05-29 14:58:11 -070011117 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
11118 if (netif_running(dev)) {
11119 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011120 err2 = tg3_restart_hw(tp, 1);
11121 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070011122 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011123 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011124
11125 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011126
11127 if (irq_sync && !err2)
11128 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011129 }
Matt Carlson800960682010-08-02 11:26:06 +000011130 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080011131 tg3_set_power_state(tp, PCI_D3hot);
11132
Michael Chan4cafd3f2005-05-29 14:56:34 -070011133}
11134
Linus Torvalds1da177e2005-04-16 15:20:36 -070011135static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
11136{
11137 struct mii_ioctl_data *data = if_mii(ifr);
11138 struct tg3 *tp = netdev_priv(dev);
11139 int err;
11140
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011141 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011142 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011143 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011144 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011145 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000011146 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011147 }
11148
Matt Carlson33f401a2010-04-05 10:19:27 +000011149 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011150 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000011151 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011152
11153 /* fallthru */
11154 case SIOCGMIIREG: {
11155 u32 mii_regval;
11156
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011157 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011158 break; /* We have no PHY */
11159
Matt Carlson800960682010-08-02 11:26:06 +000011160 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080011161 return -EAGAIN;
11162
David S. Millerf47c11e2005-06-24 20:18:35 -070011163 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011164 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070011165 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011166
11167 data->val_out = mii_regval;
11168
11169 return err;
11170 }
11171
11172 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011173 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011174 break; /* We have no PHY */
11175
Matt Carlson800960682010-08-02 11:26:06 +000011176 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080011177 return -EAGAIN;
11178
David S. Millerf47c11e2005-06-24 20:18:35 -070011179 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011180 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070011181 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011182
11183 return err;
11184
11185 default:
11186 /* do nothing */
11187 break;
11188 }
11189 return -EOPNOTSUPP;
11190}
11191
11192#if TG3_VLAN_TAG_USED
11193static void tg3_vlan_rx_register(struct net_device *dev, struct vlan_group *grp)
11194{
11195 struct tg3 *tp = netdev_priv(dev);
11196
Matt Carlson844b3ee2009-02-25 14:23:56 +000011197 if (!netif_running(dev)) {
11198 tp->vlgrp = grp;
11199 return;
11200 }
11201
11202 tg3_netif_stop(tp);
Michael Chan29315e82006-06-29 20:12:30 -070011203
David S. Millerf47c11e2005-06-24 20:18:35 -070011204 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011205
11206 tp->vlgrp = grp;
11207
11208 /* Update RX_MODE_KEEP_VLAN_TAG bit in RX_MODE register. */
11209 __tg3_set_rx_mode(dev);
11210
Matt Carlson844b3ee2009-02-25 14:23:56 +000011211 tg3_netif_start(tp);
Michael Chan46966542007-07-11 19:47:19 -070011212
11213 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011214}
Linus Torvalds1da177e2005-04-16 15:20:36 -070011215#endif
11216
David S. Miller15f98502005-05-18 22:49:26 -070011217static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11218{
11219 struct tg3 *tp = netdev_priv(dev);
11220
11221 memcpy(ec, &tp->coal, sizeof(*ec));
11222 return 0;
11223}
11224
Michael Chand244c892005-07-05 14:42:33 -070011225static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11226{
11227 struct tg3 *tp = netdev_priv(dev);
11228 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
11229 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
11230
11231 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
11232 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
11233 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
11234 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
11235 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
11236 }
11237
11238 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
11239 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
11240 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
11241 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
11242 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
11243 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
11244 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
11245 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
11246 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
11247 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
11248 return -EINVAL;
11249
11250 /* No rx interrupts will be generated if both are zero */
11251 if ((ec->rx_coalesce_usecs == 0) &&
11252 (ec->rx_max_coalesced_frames == 0))
11253 return -EINVAL;
11254
11255 /* No tx interrupts will be generated if both are zero */
11256 if ((ec->tx_coalesce_usecs == 0) &&
11257 (ec->tx_max_coalesced_frames == 0))
11258 return -EINVAL;
11259
11260 /* Only copy relevant parameters, ignore all others. */
11261 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
11262 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
11263 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
11264 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
11265 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
11266 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
11267 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
11268 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
11269 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
11270
11271 if (netif_running(dev)) {
11272 tg3_full_lock(tp, 0);
11273 __tg3_set_coalesce(tp, &tp->coal);
11274 tg3_full_unlock(tp);
11275 }
11276 return 0;
11277}
11278
Jeff Garzik7282d492006-09-13 14:30:00 -040011279static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011280 .get_settings = tg3_get_settings,
11281 .set_settings = tg3_set_settings,
11282 .get_drvinfo = tg3_get_drvinfo,
11283 .get_regs_len = tg3_get_regs_len,
11284 .get_regs = tg3_get_regs,
11285 .get_wol = tg3_get_wol,
11286 .set_wol = tg3_set_wol,
11287 .get_msglevel = tg3_get_msglevel,
11288 .set_msglevel = tg3_set_msglevel,
11289 .nway_reset = tg3_nway_reset,
11290 .get_link = ethtool_op_get_link,
11291 .get_eeprom_len = tg3_get_eeprom_len,
11292 .get_eeprom = tg3_get_eeprom,
11293 .set_eeprom = tg3_set_eeprom,
11294 .get_ringparam = tg3_get_ringparam,
11295 .set_ringparam = tg3_set_ringparam,
11296 .get_pauseparam = tg3_get_pauseparam,
11297 .set_pauseparam = tg3_set_pauseparam,
11298 .get_rx_csum = tg3_get_rx_csum,
11299 .set_rx_csum = tg3_set_rx_csum,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011300 .set_tx_csum = tg3_set_tx_csum,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011301 .set_sg = ethtool_op_set_sg,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011302 .set_tso = tg3_set_tso,
Michael Chan4cafd3f2005-05-29 14:56:34 -070011303 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011304 .get_strings = tg3_get_strings,
Michael Chan4009a932005-09-05 17:52:54 -070011305 .phys_id = tg3_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011306 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070011307 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070011308 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070011309 .get_sset_count = tg3_get_sset_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011310};
11311
11312static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
11313{
Michael Chan1b277772006-03-20 22:27:48 -080011314 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011315
11316 tp->nvram_size = EEPROM_CHIP_SIZE;
11317
Matt Carlsone4f34112009-02-25 14:25:00 +000011318 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011319 return;
11320
Michael Chanb16250e2006-09-27 16:10:14 -070011321 if ((magic != TG3_EEPROM_MAGIC) &&
11322 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
11323 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011324 return;
11325
11326 /*
11327 * Size the chip by reading offsets at increasing powers of two.
11328 * When we encounter our validation signature, we know the addressing
11329 * has wrapped around, and thus have our chip size.
11330 */
Michael Chan1b277772006-03-20 22:27:48 -080011331 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011332
11333 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000011334 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011335 return;
11336
Michael Chan18201802006-03-20 22:29:15 -080011337 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011338 break;
11339
11340 cursize <<= 1;
11341 }
11342
11343 tp->nvram_size = cursize;
11344}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011345
Linus Torvalds1da177e2005-04-16 15:20:36 -070011346static void __devinit tg3_get_nvram_size(struct tg3 *tp)
11347{
11348 u32 val;
11349
Matt Carlsondf259d82009-04-20 06:57:14 +000011350 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
11351 tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080011352 return;
11353
11354 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080011355 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080011356 tg3_get_eeprom_size(tp);
11357 return;
11358 }
11359
Matt Carlson6d348f22009-02-25 14:25:52 +000011360 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011361 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000011362 /* This is confusing. We want to operate on the
11363 * 16-bit value at offset 0xf2. The tg3_nvram_read()
11364 * call will read from NVRAM and byteswap the data
11365 * according to the byteswapping settings for all
11366 * other register accesses. This ensures the data we
11367 * want will always reside in the lower 16-bits.
11368 * However, the data in NVRAM is in LE format, which
11369 * means the data from the NVRAM read will always be
11370 * opposite the endianness of the CPU. The 16-bit
11371 * byteswap then brings the data to CPU endianness.
11372 */
11373 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011374 return;
11375 }
11376 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070011377 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011378}
11379
11380static void __devinit tg3_get_nvram_info(struct tg3 *tp)
11381{
11382 u32 nvcfg1;
11383
11384 nvcfg1 = tr32(NVRAM_CFG1);
11385 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
11386 tp->tg3_flags2 |= TG3_FLG2_FLASH;
Matt Carlson8590a602009-08-28 12:29:16 +000011387 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011388 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11389 tw32(NVRAM_CFG1, nvcfg1);
11390 }
11391
Michael Chan4c987482005-09-05 17:52:38 -070011392 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750) ||
Michael Chana4e2b342005-10-26 15:46:52 -070011393 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011394 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011395 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
11396 tp->nvram_jedecnum = JEDEC_ATMEL;
11397 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
11398 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11399 break;
11400 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
11401 tp->nvram_jedecnum = JEDEC_ATMEL;
11402 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
11403 break;
11404 case FLASH_VENDOR_ATMEL_EEPROM:
11405 tp->nvram_jedecnum = JEDEC_ATMEL;
11406 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11407 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11408 break;
11409 case FLASH_VENDOR_ST:
11410 tp->nvram_jedecnum = JEDEC_ST;
11411 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
11412 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11413 break;
11414 case FLASH_VENDOR_SAIFUN:
11415 tp->nvram_jedecnum = JEDEC_SAIFUN;
11416 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
11417 break;
11418 case FLASH_VENDOR_SST_SMALL:
11419 case FLASH_VENDOR_SST_LARGE:
11420 tp->nvram_jedecnum = JEDEC_SST;
11421 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
11422 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011423 }
Matt Carlson8590a602009-08-28 12:29:16 +000011424 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011425 tp->nvram_jedecnum = JEDEC_ATMEL;
11426 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
11427 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11428 }
11429}
11430
Matt Carlsona1b950d2009-09-01 13:20:17 +000011431static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
11432{
11433 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
11434 case FLASH_5752PAGE_SIZE_256:
11435 tp->nvram_pagesize = 256;
11436 break;
11437 case FLASH_5752PAGE_SIZE_512:
11438 tp->nvram_pagesize = 512;
11439 break;
11440 case FLASH_5752PAGE_SIZE_1K:
11441 tp->nvram_pagesize = 1024;
11442 break;
11443 case FLASH_5752PAGE_SIZE_2K:
11444 tp->nvram_pagesize = 2048;
11445 break;
11446 case FLASH_5752PAGE_SIZE_4K:
11447 tp->nvram_pagesize = 4096;
11448 break;
11449 case FLASH_5752PAGE_SIZE_264:
11450 tp->nvram_pagesize = 264;
11451 break;
11452 case FLASH_5752PAGE_SIZE_528:
11453 tp->nvram_pagesize = 528;
11454 break;
11455 }
11456}
11457
Michael Chan361b4ac2005-04-21 17:11:21 -070011458static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
11459{
11460 u32 nvcfg1;
11461
11462 nvcfg1 = tr32(NVRAM_CFG1);
11463
Michael Chane6af3012005-04-21 17:12:05 -070011464 /* NVRAM protection for TPM */
11465 if (nvcfg1 & (1 << 27))
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011466 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Michael Chane6af3012005-04-21 17:12:05 -070011467
Michael Chan361b4ac2005-04-21 17:11:21 -070011468 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011469 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
11470 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
11471 tp->nvram_jedecnum = JEDEC_ATMEL;
11472 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11473 break;
11474 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11475 tp->nvram_jedecnum = JEDEC_ATMEL;
11476 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11477 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11478 break;
11479 case FLASH_5752VENDOR_ST_M45PE10:
11480 case FLASH_5752VENDOR_ST_M45PE20:
11481 case FLASH_5752VENDOR_ST_M45PE40:
11482 tp->nvram_jedecnum = JEDEC_ST;
11483 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11484 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11485 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070011486 }
11487
11488 if (tp->tg3_flags2 & TG3_FLG2_FLASH) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000011489 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000011490 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070011491 /* For eeprom, set pagesize to maximum eeprom size */
11492 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11493
11494 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11495 tw32(NVRAM_CFG1, nvcfg1);
11496 }
11497}
11498
Michael Chand3c7b882006-03-23 01:28:25 -080011499static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
11500{
Matt Carlson989a9d22007-05-05 11:51:05 -070011501 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080011502
11503 nvcfg1 = tr32(NVRAM_CFG1);
11504
11505 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070011506 if (nvcfg1 & (1 << 27)) {
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011507 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Matt Carlson989a9d22007-05-05 11:51:05 -070011508 protect = 1;
11509 }
Michael Chand3c7b882006-03-23 01:28:25 -080011510
Matt Carlson989a9d22007-05-05 11:51:05 -070011511 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11512 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011513 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11514 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11515 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11516 case FLASH_5755VENDOR_ATMEL_FLASH_5:
11517 tp->nvram_jedecnum = JEDEC_ATMEL;
11518 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11519 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11520 tp->nvram_pagesize = 264;
11521 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
11522 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
11523 tp->nvram_size = (protect ? 0x3e200 :
11524 TG3_NVRAM_SIZE_512KB);
11525 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
11526 tp->nvram_size = (protect ? 0x1f200 :
11527 TG3_NVRAM_SIZE_256KB);
11528 else
11529 tp->nvram_size = (protect ? 0x1f200 :
11530 TG3_NVRAM_SIZE_128KB);
11531 break;
11532 case FLASH_5752VENDOR_ST_M45PE10:
11533 case FLASH_5752VENDOR_ST_M45PE20:
11534 case FLASH_5752VENDOR_ST_M45PE40:
11535 tp->nvram_jedecnum = JEDEC_ST;
11536 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11537 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11538 tp->nvram_pagesize = 256;
11539 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
11540 tp->nvram_size = (protect ?
11541 TG3_NVRAM_SIZE_64KB :
11542 TG3_NVRAM_SIZE_128KB);
11543 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
11544 tp->nvram_size = (protect ?
11545 TG3_NVRAM_SIZE_64KB :
11546 TG3_NVRAM_SIZE_256KB);
11547 else
11548 tp->nvram_size = (protect ?
11549 TG3_NVRAM_SIZE_128KB :
11550 TG3_NVRAM_SIZE_512KB);
11551 break;
Michael Chand3c7b882006-03-23 01:28:25 -080011552 }
11553}
11554
Michael Chan1b277772006-03-20 22:27:48 -080011555static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
11556{
11557 u32 nvcfg1;
11558
11559 nvcfg1 = tr32(NVRAM_CFG1);
11560
11561 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011562 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
11563 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11564 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
11565 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11566 tp->nvram_jedecnum = JEDEC_ATMEL;
11567 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11568 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080011569
Matt Carlson8590a602009-08-28 12:29:16 +000011570 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11571 tw32(NVRAM_CFG1, nvcfg1);
11572 break;
11573 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11574 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11575 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11576 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11577 tp->nvram_jedecnum = JEDEC_ATMEL;
11578 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11579 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11580 tp->nvram_pagesize = 264;
11581 break;
11582 case FLASH_5752VENDOR_ST_M45PE10:
11583 case FLASH_5752VENDOR_ST_M45PE20:
11584 case FLASH_5752VENDOR_ST_M45PE40:
11585 tp->nvram_jedecnum = JEDEC_ST;
11586 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11587 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11588 tp->nvram_pagesize = 256;
11589 break;
Michael Chan1b277772006-03-20 22:27:48 -080011590 }
11591}
11592
Matt Carlson6b91fa02007-10-10 18:01:09 -070011593static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
11594{
11595 u32 nvcfg1, protect = 0;
11596
11597 nvcfg1 = tr32(NVRAM_CFG1);
11598
11599 /* NVRAM protection for TPM */
11600 if (nvcfg1 & (1 << 27)) {
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011601 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011602 protect = 1;
11603 }
11604
11605 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11606 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011607 case FLASH_5761VENDOR_ATMEL_ADB021D:
11608 case FLASH_5761VENDOR_ATMEL_ADB041D:
11609 case FLASH_5761VENDOR_ATMEL_ADB081D:
11610 case FLASH_5761VENDOR_ATMEL_ADB161D:
11611 case FLASH_5761VENDOR_ATMEL_MDB021D:
11612 case FLASH_5761VENDOR_ATMEL_MDB041D:
11613 case FLASH_5761VENDOR_ATMEL_MDB081D:
11614 case FLASH_5761VENDOR_ATMEL_MDB161D:
11615 tp->nvram_jedecnum = JEDEC_ATMEL;
11616 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11617 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11618 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
11619 tp->nvram_pagesize = 256;
11620 break;
11621 case FLASH_5761VENDOR_ST_A_M45PE20:
11622 case FLASH_5761VENDOR_ST_A_M45PE40:
11623 case FLASH_5761VENDOR_ST_A_M45PE80:
11624 case FLASH_5761VENDOR_ST_A_M45PE16:
11625 case FLASH_5761VENDOR_ST_M_M45PE20:
11626 case FLASH_5761VENDOR_ST_M_M45PE40:
11627 case FLASH_5761VENDOR_ST_M_M45PE80:
11628 case FLASH_5761VENDOR_ST_M_M45PE16:
11629 tp->nvram_jedecnum = JEDEC_ST;
11630 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11631 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11632 tp->nvram_pagesize = 256;
11633 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011634 }
11635
11636 if (protect) {
11637 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
11638 } else {
11639 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011640 case FLASH_5761VENDOR_ATMEL_ADB161D:
11641 case FLASH_5761VENDOR_ATMEL_MDB161D:
11642 case FLASH_5761VENDOR_ST_A_M45PE16:
11643 case FLASH_5761VENDOR_ST_M_M45PE16:
11644 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
11645 break;
11646 case FLASH_5761VENDOR_ATMEL_ADB081D:
11647 case FLASH_5761VENDOR_ATMEL_MDB081D:
11648 case FLASH_5761VENDOR_ST_A_M45PE80:
11649 case FLASH_5761VENDOR_ST_M_M45PE80:
11650 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
11651 break;
11652 case FLASH_5761VENDOR_ATMEL_ADB041D:
11653 case FLASH_5761VENDOR_ATMEL_MDB041D:
11654 case FLASH_5761VENDOR_ST_A_M45PE40:
11655 case FLASH_5761VENDOR_ST_M_M45PE40:
11656 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11657 break;
11658 case FLASH_5761VENDOR_ATMEL_ADB021D:
11659 case FLASH_5761VENDOR_ATMEL_MDB021D:
11660 case FLASH_5761VENDOR_ST_A_M45PE20:
11661 case FLASH_5761VENDOR_ST_M_M45PE20:
11662 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11663 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011664 }
11665 }
11666}
11667
Michael Chanb5d37722006-09-27 16:06:21 -070011668static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
11669{
11670 tp->nvram_jedecnum = JEDEC_ATMEL;
11671 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11672 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11673}
11674
Matt Carlson321d32a2008-11-21 17:22:19 -080011675static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
11676{
11677 u32 nvcfg1;
11678
11679 nvcfg1 = tr32(NVRAM_CFG1);
11680
11681 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11682 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11683 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11684 tp->nvram_jedecnum = JEDEC_ATMEL;
11685 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11686 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11687
11688 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11689 tw32(NVRAM_CFG1, nvcfg1);
11690 return;
11691 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11692 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11693 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11694 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11695 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11696 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11697 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
11698 tp->nvram_jedecnum = JEDEC_ATMEL;
11699 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11700 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11701
11702 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11703 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11704 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11705 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11706 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11707 break;
11708 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11709 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11710 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11711 break;
11712 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11713 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
11714 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11715 break;
11716 }
11717 break;
11718 case FLASH_5752VENDOR_ST_M45PE10:
11719 case FLASH_5752VENDOR_ST_M45PE20:
11720 case FLASH_5752VENDOR_ST_M45PE40:
11721 tp->nvram_jedecnum = JEDEC_ST;
11722 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11723 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11724
11725 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11726 case FLASH_5752VENDOR_ST_M45PE10:
11727 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11728 break;
11729 case FLASH_5752VENDOR_ST_M45PE20:
11730 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11731 break;
11732 case FLASH_5752VENDOR_ST_M45PE40:
11733 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11734 break;
11735 }
11736 break;
11737 default:
Matt Carlsondf259d82009-04-20 06:57:14 +000011738 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
Matt Carlson321d32a2008-11-21 17:22:19 -080011739 return;
11740 }
11741
Matt Carlsona1b950d2009-09-01 13:20:17 +000011742 tg3_nvram_get_pagesize(tp, nvcfg1);
11743 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Matt Carlson321d32a2008-11-21 17:22:19 -080011744 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011745}
11746
11747
11748static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
11749{
11750 u32 nvcfg1;
11751
11752 nvcfg1 = tr32(NVRAM_CFG1);
11753
11754 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11755 case FLASH_5717VENDOR_ATMEL_EEPROM:
11756 case FLASH_5717VENDOR_MICRO_EEPROM:
11757 tp->nvram_jedecnum = JEDEC_ATMEL;
11758 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11759 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11760
11761 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11762 tw32(NVRAM_CFG1, nvcfg1);
11763 return;
11764 case FLASH_5717VENDOR_ATMEL_MDB011D:
11765 case FLASH_5717VENDOR_ATMEL_ADB011B:
11766 case FLASH_5717VENDOR_ATMEL_ADB011D:
11767 case FLASH_5717VENDOR_ATMEL_MDB021D:
11768 case FLASH_5717VENDOR_ATMEL_ADB021B:
11769 case FLASH_5717VENDOR_ATMEL_ADB021D:
11770 case FLASH_5717VENDOR_ATMEL_45USPT:
11771 tp->nvram_jedecnum = JEDEC_ATMEL;
11772 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11773 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11774
11775 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11776 case FLASH_5717VENDOR_ATMEL_MDB021D:
11777 case FLASH_5717VENDOR_ATMEL_ADB021B:
11778 case FLASH_5717VENDOR_ATMEL_ADB021D:
11779 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11780 break;
11781 default:
11782 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11783 break;
11784 }
Matt Carlson321d32a2008-11-21 17:22:19 -080011785 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011786 case FLASH_5717VENDOR_ST_M_M25PE10:
11787 case FLASH_5717VENDOR_ST_A_M25PE10:
11788 case FLASH_5717VENDOR_ST_M_M45PE10:
11789 case FLASH_5717VENDOR_ST_A_M45PE10:
11790 case FLASH_5717VENDOR_ST_M_M25PE20:
11791 case FLASH_5717VENDOR_ST_A_M25PE20:
11792 case FLASH_5717VENDOR_ST_M_M45PE20:
11793 case FLASH_5717VENDOR_ST_A_M45PE20:
11794 case FLASH_5717VENDOR_ST_25USPT:
11795 case FLASH_5717VENDOR_ST_45USPT:
11796 tp->nvram_jedecnum = JEDEC_ST;
11797 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11798 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11799
11800 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11801 case FLASH_5717VENDOR_ST_M_M25PE20:
11802 case FLASH_5717VENDOR_ST_A_M25PE20:
11803 case FLASH_5717VENDOR_ST_M_M45PE20:
11804 case FLASH_5717VENDOR_ST_A_M45PE20:
11805 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11806 break;
11807 default:
11808 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11809 break;
11810 }
Matt Carlson321d32a2008-11-21 17:22:19 -080011811 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011812 default:
11813 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
11814 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080011815 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000011816
11817 tg3_nvram_get_pagesize(tp, nvcfg1);
11818 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
11819 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
Matt Carlson321d32a2008-11-21 17:22:19 -080011820}
11821
Linus Torvalds1da177e2005-04-16 15:20:36 -070011822/* Chips other than 5700/5701 use the NVRAM for fetching info. */
11823static void __devinit tg3_nvram_init(struct tg3 *tp)
11824{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011825 tw32_f(GRC_EEPROM_ADDR,
11826 (EEPROM_ADDR_FSM_RESET |
11827 (EEPROM_DEFAULT_CLOCK_PERIOD <<
11828 EEPROM_ADDR_CLKPERD_SHIFT)));
11829
Michael Chan9d57f012006-12-07 00:23:25 -080011830 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011831
11832 /* Enable seeprom accesses. */
11833 tw32_f(GRC_LOCAL_CTRL,
11834 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
11835 udelay(100);
11836
11837 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
11838 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
11839 tp->tg3_flags |= TG3_FLAG_NVRAM;
11840
Michael Chanec41c7d2006-01-17 02:40:55 -080011841 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000011842 netdev_warn(tp->dev,
11843 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000011844 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080011845 return;
11846 }
Michael Chane6af3012005-04-21 17:12:05 -070011847 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011848
Matt Carlson989a9d22007-05-05 11:51:05 -070011849 tp->nvram_size = 0;
11850
Michael Chan361b4ac2005-04-21 17:11:21 -070011851 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
11852 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080011853 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
11854 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070011855 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070011856 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
11857 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080011858 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070011859 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
11860 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070011861 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
11862 tg3_get_5906_nvram_info(tp);
Matt Carlsonb703df62009-12-03 08:36:21 +000011863 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
11864 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson321d32a2008-11-21 17:22:19 -080011865 tg3_get_57780_nvram_info(tp);
Matt Carlsona50d0792010-06-05 17:24:37 +000011866 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
11867 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000011868 tg3_get_5717_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070011869 else
11870 tg3_get_nvram_info(tp);
11871
Matt Carlson989a9d22007-05-05 11:51:05 -070011872 if (tp->nvram_size == 0)
11873 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011874
Michael Chane6af3012005-04-21 17:12:05 -070011875 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080011876 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011877
11878 } else {
11879 tp->tg3_flags &= ~(TG3_FLAG_NVRAM | TG3_FLAG_NVRAM_BUFFERED);
11880
11881 tg3_get_eeprom_size(tp);
11882 }
11883}
11884
Linus Torvalds1da177e2005-04-16 15:20:36 -070011885static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
11886 u32 offset, u32 len, u8 *buf)
11887{
11888 int i, j, rc = 0;
11889 u32 val;
11890
11891 for (i = 0; i < len; i += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080011892 u32 addr;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011893 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011894
11895 addr = offset + i;
11896
11897 memcpy(&data, buf + i, 4);
11898
Matt Carlson62cedd12009-04-20 14:52:29 -070011899 /*
11900 * The SEEPROM interface expects the data to always be opposite
11901 * the native endian format. We accomplish this by reversing
11902 * all the operations that would have been performed on the
11903 * data from a call to tg3_nvram_read_be32().
11904 */
11905 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011906
11907 val = tr32(GRC_EEPROM_ADDR);
11908 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
11909
11910 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
11911 EEPROM_ADDR_READ);
11912 tw32(GRC_EEPROM_ADDR, val |
11913 (0 << EEPROM_ADDR_DEVID_SHIFT) |
11914 (addr & EEPROM_ADDR_ADDR_MASK) |
11915 EEPROM_ADDR_START |
11916 EEPROM_ADDR_WRITE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011917
Michael Chan9d57f012006-12-07 00:23:25 -080011918 for (j = 0; j < 1000; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011919 val = tr32(GRC_EEPROM_ADDR);
11920
11921 if (val & EEPROM_ADDR_COMPLETE)
11922 break;
Michael Chan9d57f012006-12-07 00:23:25 -080011923 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011924 }
11925 if (!(val & EEPROM_ADDR_COMPLETE)) {
11926 rc = -EBUSY;
11927 break;
11928 }
11929 }
11930
11931 return rc;
11932}
11933
11934/* offset and length are dword aligned */
11935static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
11936 u8 *buf)
11937{
11938 int ret = 0;
11939 u32 pagesize = tp->nvram_pagesize;
11940 u32 pagemask = pagesize - 1;
11941 u32 nvram_cmd;
11942 u8 *tmp;
11943
11944 tmp = kmalloc(pagesize, GFP_KERNEL);
11945 if (tmp == NULL)
11946 return -ENOMEM;
11947
11948 while (len) {
11949 int j;
Michael Chane6af3012005-04-21 17:12:05 -070011950 u32 phy_addr, page_off, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011951
11952 phy_addr = offset & ~pagemask;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011953
Linus Torvalds1da177e2005-04-16 15:20:36 -070011954 for (j = 0; j < pagesize; j += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000011955 ret = tg3_nvram_read_be32(tp, phy_addr + j,
11956 (__be32 *) (tmp + j));
11957 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011958 break;
11959 }
11960 if (ret)
11961 break;
11962
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011963 page_off = offset & pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011964 size = pagesize;
11965 if (len < size)
11966 size = len;
11967
11968 len -= size;
11969
11970 memcpy(tmp + page_off, buf, size);
11971
11972 offset = offset + (pagesize - page_off);
11973
Michael Chane6af3012005-04-21 17:12:05 -070011974 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011975
11976 /*
11977 * Before we can erase the flash page, we need
11978 * to issue a special "write enable" command.
11979 */
11980 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
11981
11982 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
11983 break;
11984
11985 /* Erase the target page */
11986 tw32(NVRAM_ADDR, phy_addr);
11987
11988 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
11989 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
11990
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011991 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011992 break;
11993
11994 /* Issue another write enable to start the write. */
11995 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
11996
11997 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
11998 break;
11999
12000 for (j = 0; j < pagesize; j += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012001 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012002
Al Virob9fc7dc2007-12-17 22:59:57 -080012003 data = *((__be32 *) (tmp + j));
Matt Carlsona9dc5292009-02-25 14:25:30 +000012004
Al Virob9fc7dc2007-12-17 22:59:57 -080012005 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012006
12007 tw32(NVRAM_ADDR, phy_addr + j);
12008
12009 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
12010 NVRAM_CMD_WR;
12011
12012 if (j == 0)
12013 nvram_cmd |= NVRAM_CMD_FIRST;
12014 else if (j == (pagesize - 4))
12015 nvram_cmd |= NVRAM_CMD_LAST;
12016
12017 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12018 break;
12019 }
12020 if (ret)
12021 break;
12022 }
12023
12024 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12025 tg3_nvram_exec_cmd(tp, nvram_cmd);
12026
12027 kfree(tmp);
12028
12029 return ret;
12030}
12031
12032/* offset and length are dword aligned */
12033static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
12034 u8 *buf)
12035{
12036 int i, ret = 0;
12037
12038 for (i = 0; i < len; i += 4, offset += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012039 u32 page_off, phy_addr, nvram_cmd;
12040 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012041
12042 memcpy(&data, buf + i, 4);
Al Virob9fc7dc2007-12-17 22:59:57 -080012043 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012044
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012045 page_off = offset % tp->nvram_pagesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012046
Michael Chan18201802006-03-20 22:29:15 -080012047 phy_addr = tg3_nvram_phys_addr(tp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012048
12049 tw32(NVRAM_ADDR, phy_addr);
12050
12051 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
12052
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012053 if (page_off == 0 || i == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012054 nvram_cmd |= NVRAM_CMD_FIRST;
Michael Chanf6d9a252006-04-29 19:00:24 -070012055 if (page_off == (tp->nvram_pagesize - 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012056 nvram_cmd |= NVRAM_CMD_LAST;
12057
12058 if (i == (len - 4))
12059 nvram_cmd |= NVRAM_CMD_LAST;
12060
Matt Carlson321d32a2008-11-21 17:22:19 -080012061 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
12062 !(tp->tg3_flags3 & TG3_FLG3_5755_PLUS) &&
Michael Chan4c987482005-09-05 17:52:38 -070012063 (tp->nvram_jedecnum == JEDEC_ST) &&
12064 (nvram_cmd & NVRAM_CMD_FIRST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012065
12066 if ((ret = tg3_nvram_exec_cmd(tp,
12067 NVRAM_CMD_WREN | NVRAM_CMD_GO |
12068 NVRAM_CMD_DONE)))
12069
12070 break;
12071 }
12072 if (!(tp->tg3_flags2 & TG3_FLG2_FLASH)) {
12073 /* We always do complete word writes to eeprom. */
12074 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
12075 }
12076
12077 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12078 break;
12079 }
12080 return ret;
12081}
12082
12083/* offset and length are dword aligned */
12084static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
12085{
12086 int ret;
12087
Linus Torvalds1da177e2005-04-16 15:20:36 -070012088 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT) {
Michael Chan314fba32005-04-21 17:07:04 -070012089 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
12090 ~GRC_LCLCTRL_GPIO_OUTPUT1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012091 udelay(40);
12092 }
12093
12094 if (!(tp->tg3_flags & TG3_FLAG_NVRAM)) {
12095 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
Matt Carlson859a588792010-04-05 10:19:28 +000012096 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012097 u32 grc_mode;
12098
Michael Chanec41c7d2006-01-17 02:40:55 -080012099 ret = tg3_nvram_lock(tp);
12100 if (ret)
12101 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012102
Michael Chane6af3012005-04-21 17:12:05 -070012103 tg3_enable_nvram_access(tp);
12104 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Matt Carlsonf66a29b2009-11-13 13:03:36 +000012105 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012106 tw32(NVRAM_WRITE1, 0x406);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012107
12108 grc_mode = tr32(GRC_MODE);
12109 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
12110
12111 if ((tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) ||
12112 !(tp->tg3_flags2 & TG3_FLG2_FLASH)) {
12113
12114 ret = tg3_nvram_write_block_buffered(tp, offset, len,
12115 buf);
Matt Carlson859a588792010-04-05 10:19:28 +000012116 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012117 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
12118 buf);
12119 }
12120
12121 grc_mode = tr32(GRC_MODE);
12122 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
12123
Michael Chane6af3012005-04-21 17:12:05 -070012124 tg3_disable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012125 tg3_nvram_unlock(tp);
12126 }
12127
12128 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT) {
Michael Chan314fba32005-04-21 17:07:04 -070012129 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012130 udelay(40);
12131 }
12132
12133 return ret;
12134}
12135
12136struct subsys_tbl_ent {
12137 u16 subsys_vendor, subsys_devid;
12138 u32 phy_id;
12139};
12140
Matt Carlson24daf2b2010-02-17 15:17:02 +000012141static struct subsys_tbl_ent subsys_id_to_phy_id[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012142 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012143 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012144 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012145 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012146 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012147 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012148 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012149 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12150 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
12151 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012152 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012153 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012154 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012155 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12156 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
12157 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012158 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012159 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012160 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, 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_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012163 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012164 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012165
12166 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012167 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012168 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012169 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012170 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012171 { TG3PCI_SUBVENDOR_ID_3COM,
12172 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
12173 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012174 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012175 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012176 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012177
12178 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012179 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012180 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012181 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012182 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012183 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012184 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012185 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012186 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012187
12188 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012189 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012190 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012191 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012192 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012193 { TG3PCI_SUBVENDOR_ID_COMPAQ,
12194 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
12195 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012196 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012197 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012198 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012199
12200 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012201 { TG3PCI_SUBVENDOR_ID_IBM,
12202 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012203};
12204
Matt Carlson24daf2b2010-02-17 15:17:02 +000012205static struct subsys_tbl_ent * __devinit tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012206{
12207 int i;
12208
12209 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
12210 if ((subsys_id_to_phy_id[i].subsys_vendor ==
12211 tp->pdev->subsystem_vendor) &&
12212 (subsys_id_to_phy_id[i].subsys_devid ==
12213 tp->pdev->subsystem_device))
12214 return &subsys_id_to_phy_id[i];
12215 }
12216 return NULL;
12217}
12218
Michael Chan7d0c41e2005-04-21 17:06:20 -070012219static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012220{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012221 u32 val;
Michael Chancaf636c72006-03-22 01:05:31 -080012222 u16 pmcsr;
12223
12224 /* On some early chips the SRAM cannot be accessed in D3hot state,
12225 * so need make sure we're in D0.
12226 */
12227 pci_read_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, &pmcsr);
12228 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
12229 pci_write_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, pmcsr);
12230 msleep(1);
Michael Chan7d0c41e2005-04-21 17:06:20 -070012231
12232 /* Make sure register accesses (indirect or otherwise)
12233 * will function correctly.
12234 */
12235 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
12236 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012237
David S. Millerf49639e2006-06-09 11:58:36 -070012238 /* The memory arbiter has to be enabled in order for SRAM accesses
12239 * to succeed. Normally on powerup the tg3 chip firmware will make
12240 * sure it is enabled, but other entities such as system netboot
12241 * code might disable it.
12242 */
12243 val = tr32(MEMARB_MODE);
12244 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
12245
Matt Carlson79eb6902010-02-17 15:17:03 +000012246 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012247 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12248
Gary Zambranoa85feb82007-05-05 11:52:19 -070012249 /* Assume an onboard device and WOL capable by default. */
12250 tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT | TG3_FLAG_WOL_CAP;
David S. Miller72b845e2006-03-14 14:11:48 -080012251
Michael Chanb5d37722006-09-27 16:06:21 -070012252 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080012253 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Michael Chanb5d37722006-09-27 16:06:21 -070012254 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080012255 tp->tg3_flags2 |= TG3_FLG2_IS_NIC;
12256 }
Matt Carlson0527ba32007-10-10 18:03:30 -070012257 val = tr32(VCPU_CFGSHDW);
12258 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Matt Carlson8ed5d972007-05-07 00:25:49 -070012259 tp->tg3_flags |= TG3_FLAG_ASPM_WORKAROUND;
Matt Carlson0527ba32007-10-10 18:03:30 -070012260 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Matt Carlson20232762008-12-21 20:18:56 -080012261 (val & VCPU_CFGSHDW_WOL_MAGPKT))
Matt Carlson0527ba32007-10-10 18:03:30 -070012262 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012263 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070012264 }
12265
Linus Torvalds1da177e2005-04-16 15:20:36 -070012266 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
12267 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
12268 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070012269 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012270 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012271
12272 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
12273 tp->nic_sram_data_cfg = nic_cfg;
12274
12275 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
12276 ver >>= NIC_SRAM_DATA_VER_SHIFT;
12277 if ((GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700) &&
12278 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) &&
12279 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703) &&
12280 (ver > 0) && (ver < 0x100))
12281 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
12282
Matt Carlsona9daf362008-05-25 23:49:44 -070012283 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
12284 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
12285
Linus Torvalds1da177e2005-04-16 15:20:36 -070012286 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
12287 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
12288 eeprom_phy_serdes = 1;
12289
12290 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
12291 if (nic_phy_id != 0) {
12292 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
12293 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
12294
12295 eeprom_phy_id = (id1 >> 16) << 10;
12296 eeprom_phy_id |= (id2 & 0xfc00) << 16;
12297 eeprom_phy_id |= (id2 & 0x03ff) << 0;
12298 } else
12299 eeprom_phy_id = 0;
12300
Michael Chan7d0c41e2005-04-21 17:06:20 -070012301 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070012302 if (eeprom_phy_serdes) {
Matt Carlsona50d0792010-06-05 17:24:37 +000012303 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012304 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000012305 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012306 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070012307 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070012308
John W. Linvillecbf46852005-04-21 17:01:29 -070012309 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012310 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
12311 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070012312 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070012313 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
12314
12315 switch (led_cfg) {
12316 default:
12317 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
12318 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12319 break;
12320
12321 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
12322 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12323 break;
12324
12325 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
12326 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070012327
12328 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
12329 * read on some older 5700/5701 bootcode.
12330 */
12331 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
12332 ASIC_REV_5700 ||
12333 GET_ASIC_REV(tp->pci_chip_rev_id) ==
12334 ASIC_REV_5701)
12335 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12336
Linus Torvalds1da177e2005-04-16 15:20:36 -070012337 break;
12338
12339 case SHASTA_EXT_LED_SHARED:
12340 tp->led_ctrl = LED_CTRL_MODE_SHARED;
12341 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
12342 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
12343 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12344 LED_CTRL_MODE_PHY_2);
12345 break;
12346
12347 case SHASTA_EXT_LED_MAC:
12348 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
12349 break;
12350
12351 case SHASTA_EXT_LED_COMBO:
12352 tp->led_ctrl = LED_CTRL_MODE_COMBO;
12353 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
12354 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12355 LED_CTRL_MODE_PHY_2);
12356 break;
12357
Stephen Hemminger855e1112008-04-16 16:37:28 -070012358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012359
12360 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
12361 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
12362 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
12363 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12364
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012365 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
12366 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080012367
Michael Chan9d26e212006-12-07 00:21:14 -080012368 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012369 tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080012370 if ((tp->pdev->subsystem_vendor ==
12371 PCI_VENDOR_ID_ARIMA) &&
12372 (tp->pdev->subsystem_device == 0x205a ||
12373 tp->pdev->subsystem_device == 0x2063))
12374 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
12375 } else {
David S. Millerf49639e2006-06-09 11:58:36 -070012376 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080012377 tp->tg3_flags2 |= TG3_FLG2_IS_NIC;
12378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012379
12380 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
12381 tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
John W. Linvillecbf46852005-04-21 17:01:29 -070012382 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012383 tp->tg3_flags2 |= TG3_FLG2_ASF_NEW_HANDSHAKE;
12384 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012385
12386 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
12387 (tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
Matt Carlson0d3031d2007-10-10 18:02:43 -070012388 tp->tg3_flags3 |= TG3_FLG3_ENABLE_APE;
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012389
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012390 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070012391 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
12392 tp->tg3_flags &= ~TG3_FLAG_WOL_CAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012393
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070012394 if ((tp->tg3_flags & TG3_FLAG_WOL_CAP) &&
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012395 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE))
Matt Carlson0527ba32007-10-10 18:03:30 -070012396 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
12397
Linus Torvalds1da177e2005-04-16 15:20:36 -070012398 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012399 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012400
12401 /* serdes signal pre-emphasis in register 0x590 set by */
12402 /* bootcode if bit 18 is set */
12403 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012404 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070012405
Matt Carlson321d32a2008-11-21 17:22:19 -080012406 if (((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
12407 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080012408 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012409 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080012410
Matt Carlson8c69b1e2010-08-02 11:26:00 +000012411 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
12412 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
12413 !(tp->tg3_flags3 & TG3_FLG3_5717_PLUS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070012414 u32 cfg3;
12415
12416 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
12417 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
12418 tp->tg3_flags |= TG3_FLAG_ASPM_WORKAROUND;
12419 }
Matt Carlsona9daf362008-05-25 23:49:44 -070012420
Matt Carlson14417062010-02-17 15:16:59 +000012421 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
12422 tp->tg3_flags3 |= TG3_FLG3_RGMII_INBAND_DISABLE;
Matt Carlsona9daf362008-05-25 23:49:44 -070012423 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
12424 tp->tg3_flags3 |= TG3_FLG3_RGMII_EXT_IBND_RX_EN;
12425 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
12426 tp->tg3_flags3 |= TG3_FLG3_RGMII_EXT_IBND_TX_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012427 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012428done:
12429 device_init_wakeup(&tp->pdev->dev, tp->tg3_flags & TG3_FLAG_WOL_CAP);
12430 device_set_wakeup_enable(&tp->pdev->dev,
12431 tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
Michael Chan7d0c41e2005-04-21 17:06:20 -070012432}
12433
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012434static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
12435{
12436 int i;
12437 u32 val;
12438
12439 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
12440 tw32(OTP_CTRL, cmd);
12441
12442 /* Wait for up to 1 ms for command to execute. */
12443 for (i = 0; i < 100; i++) {
12444 val = tr32(OTP_STATUS);
12445 if (val & OTP_STATUS_CMD_DONE)
12446 break;
12447 udelay(10);
12448 }
12449
12450 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
12451}
12452
12453/* Read the gphy configuration from the OTP region of the chip. The gphy
12454 * configuration is a 32-bit value that straddles the alignment boundary.
12455 * We do two 32-bit reads and then shift and merge the results.
12456 */
12457static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
12458{
12459 u32 bhalf_otp, thalf_otp;
12460
12461 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
12462
12463 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
12464 return 0;
12465
12466 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
12467
12468 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12469 return 0;
12470
12471 thalf_otp = tr32(OTP_READ_DATA);
12472
12473 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
12474
12475 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12476 return 0;
12477
12478 bhalf_otp = tr32(OTP_READ_DATA);
12479
12480 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
12481}
12482
Michael Chan7d0c41e2005-04-21 17:06:20 -070012483static int __devinit tg3_phy_probe(struct tg3 *tp)
12484{
12485 u32 hw_phy_id_1, hw_phy_id_2;
12486 u32 hw_phy_id, hw_phy_id_masked;
12487 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012488
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012489 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)
12490 return tg3_phy_init(tp);
12491
Linus Torvalds1da177e2005-04-16 15:20:36 -070012492 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010012493 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012494 */
12495 err = 0;
Matt Carlson0d3031d2007-10-10 18:02:43 -070012496 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
12497 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000012498 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012499 } else {
12500 /* Now read the physical PHY_ID from the chip and verify
12501 * that it is sane. If it doesn't look good, we fall back
12502 * to either the hard-coded table based PHY_ID and failing
12503 * that the value found in the eeprom area.
12504 */
12505 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
12506 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
12507
12508 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
12509 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
12510 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
12511
Matt Carlson79eb6902010-02-17 15:17:03 +000012512 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012513 }
12514
Matt Carlson79eb6902010-02-17 15:17:03 +000012515 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012516 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000012517 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012518 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070012519 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012520 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012521 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000012522 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070012523 /* Do nothing, phy ID already set up in
12524 * tg3_get_eeprom_hw_cfg().
12525 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070012526 } else {
12527 struct subsys_tbl_ent *p;
12528
12529 /* No eeprom signature? Try the hardcoded
12530 * subsys device table.
12531 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012532 p = tg3_lookup_by_subsys(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012533 if (!p)
12534 return -ENODEV;
12535
12536 tp->phy_id = p->phy_id;
12537 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000012538 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012539 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012540 }
12541 }
12542
Matt Carlson52b02d02010-10-14 10:37:41 +000012543 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
12544 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12545 tp->pci_chip_rev_id != CHIPREV_ID_57765_A0))
12546 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
12547
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012548 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Matt Carlson0d3031d2007-10-10 18:02:43 -070012549 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070012550 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chan3600d912006-12-07 00:21:48 -080012551 u32 bmsr, adv_reg, tg3_ctrl, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012552
12553 tg3_readphy(tp, MII_BMSR, &bmsr);
12554 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
12555 (bmsr & BMSR_LSTATUS))
12556 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012557
Linus Torvalds1da177e2005-04-16 15:20:36 -070012558 err = tg3_phy_reset(tp);
12559 if (err)
12560 return err;
12561
12562 adv_reg = (ADVERTISE_10HALF | ADVERTISE_10FULL |
12563 ADVERTISE_100HALF | ADVERTISE_100FULL |
12564 ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
12565 tg3_ctrl = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012566 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012567 tg3_ctrl = (MII_TG3_CTRL_ADV_1000_HALF |
12568 MII_TG3_CTRL_ADV_1000_FULL);
12569 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
12570 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
12571 tg3_ctrl |= (MII_TG3_CTRL_AS_MASTER |
12572 MII_TG3_CTRL_ENABLE_AS_MASTER);
12573 }
12574
Michael Chan3600d912006-12-07 00:21:48 -080012575 mask = (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
12576 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
12577 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full);
12578 if (!tg3_copper_is_advertising_all(tp, mask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012579 tg3_writephy(tp, MII_ADVERTISE, adv_reg);
12580
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012581 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012582 tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
12583
12584 tg3_writephy(tp, MII_BMCR,
12585 BMCR_ANENABLE | BMCR_ANRESTART);
12586 }
12587 tg3_phy_set_wirespeed(tp);
12588
12589 tg3_writephy(tp, MII_ADVERTISE, adv_reg);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012590 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012591 tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
12592 }
12593
12594skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000012595 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012596 err = tg3_init_5401phy_dsp(tp);
12597 if (err)
12598 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012599
Linus Torvalds1da177e2005-04-16 15:20:36 -070012600 err = tg3_init_5401phy_dsp(tp);
12601 }
12602
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012603 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012604 tp->link_config.advertising =
12605 (ADVERTISED_1000baseT_Half |
12606 ADVERTISED_1000baseT_Full |
12607 ADVERTISED_Autoneg |
12608 ADVERTISED_FIBRE);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012609 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012610 tp->link_config.advertising &=
12611 ~(ADVERTISED_1000baseT_Half |
12612 ADVERTISED_1000baseT_Full);
12613
12614 return err;
12615}
12616
Matt Carlson184b8902010-04-05 10:19:25 +000012617static void __devinit tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012618{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000012619 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000012620 unsigned int block_end, rosize, len;
Matt Carlson184b8902010-04-05 10:19:25 +000012621 int j, i = 0;
Michael Chan1b277772006-03-20 22:27:48 -080012622 u32 magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012623
Matt Carlsondf259d82009-04-20 06:57:14 +000012624 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
12625 tg3_nvram_read(tp, 0x0, &magic))
Matt Carlsona4a8bb12010-09-15 09:00:00 +000012626 goto out_no_vpd;
12627
12628 vpd_data = kmalloc(TG3_NVM_VPD_LEN, GFP_KERNEL);
12629 if (!vpd_data)
12630 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012631
Michael Chan18201802006-03-20 22:29:15 -080012632 if (magic == TG3_EEPROM_MAGIC) {
Matt Carlson141518c2009-12-03 08:36:22 +000012633 for (i = 0; i < TG3_NVM_VPD_LEN; i += 4) {
Michael Chan1b277772006-03-20 22:27:48 -080012634 u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012635
Matt Carlson6d348f22009-02-25 14:25:52 +000012636 /* The data is in little-endian format in NVRAM.
12637 * Use the big-endian read routines to preserve
12638 * the byte order as it exists in NVRAM.
12639 */
Matt Carlson141518c2009-12-03 08:36:22 +000012640 if (tg3_nvram_read_be32(tp, TG3_NVM_VPD_OFF + i, &tmp))
Michael Chan1b277772006-03-20 22:27:48 -080012641 goto out_not_found;
12642
Matt Carlson6d348f22009-02-25 14:25:52 +000012643 memcpy(&vpd_data[i], &tmp, sizeof(tmp));
Michael Chan1b277772006-03-20 22:27:48 -080012644 }
12645 } else {
Matt Carlson94c982b2009-12-03 08:36:23 +000012646 ssize_t cnt;
Matt Carlson4181b2c2010-02-26 14:04:45 +000012647 unsigned int pos = 0;
Michael Chan1b277772006-03-20 22:27:48 -080012648
Matt Carlson94c982b2009-12-03 08:36:23 +000012649 for (; pos < TG3_NVM_VPD_LEN && i < 3; i++, pos += cnt) {
12650 cnt = pci_read_vpd(tp->pdev, pos,
12651 TG3_NVM_VPD_LEN - pos,
12652 &vpd_data[pos]);
12653 if (cnt == -ETIMEDOUT || -EINTR)
12654 cnt = 0;
12655 else if (cnt < 0)
David S. Millerf49639e2006-06-09 11:58:36 -070012656 goto out_not_found;
Michael Chan1b277772006-03-20 22:27:48 -080012657 }
Matt Carlson94c982b2009-12-03 08:36:23 +000012658 if (pos != TG3_NVM_VPD_LEN)
12659 goto out_not_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012660 }
12661
Matt Carlson4181b2c2010-02-26 14:04:45 +000012662 i = pci_vpd_find_tag(vpd_data, 0, TG3_NVM_VPD_LEN,
12663 PCI_VPD_LRDT_RO_DATA);
12664 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012665 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000012666
12667 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
12668 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
12669 i += PCI_VPD_LRDT_TAG_SIZE;
12670
12671 if (block_end > TG3_NVM_VPD_LEN)
12672 goto out_not_found;
12673
Matt Carlson184b8902010-04-05 10:19:25 +000012674 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
12675 PCI_VPD_RO_KEYWORD_MFR_ID);
12676 if (j > 0) {
12677 len = pci_vpd_info_field_size(&vpd_data[j]);
12678
12679 j += PCI_VPD_INFO_FLD_HDR_SIZE;
12680 if (j + len > block_end || len != 4 ||
12681 memcmp(&vpd_data[j], "1028", 4))
12682 goto partno;
12683
12684 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
12685 PCI_VPD_RO_KEYWORD_VENDOR0);
12686 if (j < 0)
12687 goto partno;
12688
12689 len = pci_vpd_info_field_size(&vpd_data[j]);
12690
12691 j += PCI_VPD_INFO_FLD_HDR_SIZE;
12692 if (j + len > block_end)
12693 goto partno;
12694
12695 memcpy(tp->fw_ver, &vpd_data[j], len);
12696 strncat(tp->fw_ver, " bc ", TG3_NVM_VPD_LEN - len - 1);
12697 }
12698
12699partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000012700 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
12701 PCI_VPD_RO_KEYWORD_PARTNO);
12702 if (i < 0)
12703 goto out_not_found;
12704
12705 len = pci_vpd_info_field_size(&vpd_data[i]);
12706
12707 i += PCI_VPD_INFO_FLD_HDR_SIZE;
12708 if (len > TG3_BPN_SIZE ||
12709 (len + i) > TG3_NVM_VPD_LEN)
12710 goto out_not_found;
12711
12712 memcpy(tp->board_part_number, &vpd_data[i], len);
12713
Linus Torvalds1da177e2005-04-16 15:20:36 -070012714out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000012715 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000012716 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000012717 return;
12718
12719out_no_vpd:
Matt Carlson37a949c2010-09-30 10:34:33 +000012720 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
12721 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717)
12722 strcpy(tp->board_part_number, "BCM5717");
12723 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
12724 strcpy(tp->board_part_number, "BCM5718");
12725 else
12726 goto nomatch;
12727 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
12728 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
12729 strcpy(tp->board_part_number, "BCM57780");
12730 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
12731 strcpy(tp->board_part_number, "BCM57760");
12732 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
12733 strcpy(tp->board_part_number, "BCM57790");
12734 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
12735 strcpy(tp->board_part_number, "BCM57788");
12736 else
12737 goto nomatch;
12738 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
12739 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
12740 strcpy(tp->board_part_number, "BCM57761");
12741 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
12742 strcpy(tp->board_part_number, "BCM57765");
12743 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
12744 strcpy(tp->board_part_number, "BCM57781");
12745 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
12746 strcpy(tp->board_part_number, "BCM57785");
12747 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
12748 strcpy(tp->board_part_number, "BCM57791");
12749 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
12750 strcpy(tp->board_part_number, "BCM57795");
12751 else
12752 goto nomatch;
12753 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070012754 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000012755 } else {
12756nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070012757 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000012758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012759}
12760
Matt Carlson9c8a6202007-10-21 16:16:08 -070012761static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
12762{
12763 u32 val;
12764
Matt Carlsone4f34112009-02-25 14:25:00 +000012765 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070012766 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000012767 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070012768 val != 0)
12769 return 0;
12770
12771 return 1;
12772}
12773
Matt Carlsonacd9c112009-02-25 14:26:33 +000012774static void __devinit tg3_read_bc_ver(struct tg3 *tp)
12775{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012776 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000012777 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012778 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000012779
12780 if (tg3_nvram_read(tp, 0xc, &offset) ||
12781 tg3_nvram_read(tp, 0x4, &start))
12782 return;
12783
12784 offset = tg3_nvram_logical_addr(tp, offset);
12785
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012786 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000012787 return;
12788
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012789 if ((val & 0xfc000000) == 0x0c000000) {
12790 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000012791 return;
12792
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012793 if (val == 0)
12794 newver = true;
12795 }
12796
Matt Carlson75f99362010-04-05 10:19:24 +000012797 dst_off = strlen(tp->fw_ver);
12798
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012799 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000012800 if (TG3_VER_SIZE - dst_off < 16 ||
12801 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012802 return;
12803
12804 offset = offset + ver_offset - start;
12805 for (i = 0; i < 16; i += 4) {
12806 __be32 v;
12807 if (tg3_nvram_read_be32(tp, offset + i, &v))
12808 return;
12809
Matt Carlson75f99362010-04-05 10:19:24 +000012810 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012811 }
12812 } else {
12813 u32 major, minor;
12814
12815 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
12816 return;
12817
12818 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
12819 TG3_NVM_BCVER_MAJSFT;
12820 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000012821 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
12822 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000012823 }
12824}
12825
Matt Carlsona6f6cb12009-02-25 14:27:43 +000012826static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
12827{
12828 u32 val, major, minor;
12829
12830 /* Use native endian representation */
12831 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
12832 return;
12833
12834 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
12835 TG3_NVM_HWSB_CFG1_MAJSFT;
12836 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
12837 TG3_NVM_HWSB_CFG1_MINSFT;
12838
12839 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
12840}
12841
Matt Carlsondfe00d72008-11-21 17:19:41 -080012842static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
12843{
12844 u32 offset, major, minor, build;
12845
Matt Carlson75f99362010-04-05 10:19:24 +000012846 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080012847
12848 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
12849 return;
12850
12851 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
12852 case TG3_EEPROM_SB_REVISION_0:
12853 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
12854 break;
12855 case TG3_EEPROM_SB_REVISION_2:
12856 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
12857 break;
12858 case TG3_EEPROM_SB_REVISION_3:
12859 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
12860 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000012861 case TG3_EEPROM_SB_REVISION_4:
12862 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
12863 break;
12864 case TG3_EEPROM_SB_REVISION_5:
12865 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
12866 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000012867 case TG3_EEPROM_SB_REVISION_6:
12868 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
12869 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080012870 default:
12871 return;
12872 }
12873
Matt Carlsone4f34112009-02-25 14:25:00 +000012874 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080012875 return;
12876
12877 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
12878 TG3_EEPROM_SB_EDH_BLD_SHFT;
12879 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
12880 TG3_EEPROM_SB_EDH_MAJ_SHFT;
12881 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
12882
12883 if (minor > 99 || build > 26)
12884 return;
12885
Matt Carlson75f99362010-04-05 10:19:24 +000012886 offset = strlen(tp->fw_ver);
12887 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
12888 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080012889
12890 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000012891 offset = strlen(tp->fw_ver);
12892 if (offset < TG3_VER_SIZE - 1)
12893 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080012894 }
12895}
12896
Matt Carlsonacd9c112009-02-25 14:26:33 +000012897static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080012898{
12899 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000012900 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070012901
12902 for (offset = TG3_NVM_DIR_START;
12903 offset < TG3_NVM_DIR_END;
12904 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000012905 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012906 return;
12907
12908 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
12909 break;
12910 }
12911
12912 if (offset == TG3_NVM_DIR_END)
12913 return;
12914
12915 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
12916 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000012917 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012918 return;
12919
Matt Carlsone4f34112009-02-25 14:25:00 +000012920 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070012921 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000012922 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012923 return;
12924
12925 offset += val - start;
12926
Matt Carlsonacd9c112009-02-25 14:26:33 +000012927 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012928
Matt Carlsonacd9c112009-02-25 14:26:33 +000012929 tp->fw_ver[vlen++] = ',';
12930 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070012931
12932 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012933 __be32 v;
12934 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012935 return;
12936
Al Virob9fc7dc2007-12-17 22:59:57 -080012937 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012938
Matt Carlsonacd9c112009-02-25 14:26:33 +000012939 if (vlen > TG3_VER_SIZE - sizeof(v)) {
12940 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012941 break;
12942 }
12943
Matt Carlsonacd9c112009-02-25 14:26:33 +000012944 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
12945 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012946 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000012947}
12948
Matt Carlson7fd76442009-02-25 14:27:20 +000012949static void __devinit tg3_read_dash_ver(struct tg3 *tp)
12950{
12951 int vlen;
12952 u32 apedata;
Matt Carlsonecc79642010-08-02 11:26:01 +000012953 char *fwtype;
Matt Carlson7fd76442009-02-25 14:27:20 +000012954
12955 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) ||
12956 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
12957 return;
12958
12959 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
12960 if (apedata != APE_SEG_SIG_MAGIC)
12961 return;
12962
12963 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
12964 if (!(apedata & APE_FW_STATUS_READY))
12965 return;
12966
12967 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
12968
Matt Carlsondc6d0742010-09-15 08:59:55 +000012969 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI) {
12970 tp->tg3_flags3 |= TG3_FLG3_APE_HAS_NCSI;
Matt Carlsonecc79642010-08-02 11:26:01 +000012971 fwtype = "NCSI";
Matt Carlsondc6d0742010-09-15 08:59:55 +000012972 } else {
Matt Carlsonecc79642010-08-02 11:26:01 +000012973 fwtype = "DASH";
Matt Carlsondc6d0742010-09-15 08:59:55 +000012974 }
Matt Carlsonecc79642010-08-02 11:26:01 +000012975
Matt Carlson7fd76442009-02-25 14:27:20 +000012976 vlen = strlen(tp->fw_ver);
12977
Matt Carlsonecc79642010-08-02 11:26:01 +000012978 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
12979 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000012980 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
12981 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
12982 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
12983 (apedata & APE_FW_VERSION_BLDMSK));
12984}
12985
Matt Carlsonacd9c112009-02-25 14:26:33 +000012986static void __devinit tg3_read_fw_ver(struct tg3 *tp)
12987{
12988 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000012989 bool vpd_vers = false;
12990
12991 if (tp->fw_ver[0] != 0)
12992 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000012993
Matt Carlsondf259d82009-04-20 06:57:14 +000012994 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) {
Matt Carlson75f99362010-04-05 10:19:24 +000012995 strcat(tp->fw_ver, "sb");
Matt Carlsondf259d82009-04-20 06:57:14 +000012996 return;
12997 }
12998
Matt Carlsonacd9c112009-02-25 14:26:33 +000012999 if (tg3_nvram_read(tp, 0, &val))
13000 return;
13001
13002 if (val == TG3_EEPROM_MAGIC)
13003 tg3_read_bc_ver(tp);
13004 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
13005 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013006 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
13007 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013008 else
13009 return;
13010
13011 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
Matt Carlson75f99362010-04-05 10:19:24 +000013012 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) || vpd_vers)
13013 goto done;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013014
13015 tg3_read_mgmtfw_ver(tp);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013016
Matt Carlson75f99362010-04-05 10:19:24 +000013017done:
Matt Carlson9c8a6202007-10-21 16:16:08 -070013018 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080013019}
13020
Michael Chan7544b092007-05-05 13:08:32 -070013021static struct pci_dev * __devinit tg3_find_peer(struct tg3 *);
13022
Eric Dumazet7fe876a2010-07-08 06:14:55 +000013023static void inline vlan_features_add(struct net_device *dev, unsigned long flags)
13024{
13025#if TG3_VLAN_TAG_USED
13026 dev->vlan_features |= flags;
13027#endif
13028}
13029
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013030static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
13031{
13032 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13033 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
13034 return 4096;
13035 else if ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) &&
13036 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
13037 return 1024;
13038 else
13039 return 512;
13040}
13041
Linus Torvalds1da177e2005-04-16 15:20:36 -070013042static int __devinit tg3_get_invariants(struct tg3 *tp)
13043{
13044 static struct pci_device_id write_reorder_chipsets[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013045 { PCI_DEVICE(PCI_VENDOR_ID_AMD,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000013046 PCI_DEVICE_ID_AMD_FE_GATE_700C) },
John W. Linvillec165b002006-07-08 13:28:53 -070013047 { PCI_DEVICE(PCI_VENDOR_ID_AMD,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000013048 PCI_DEVICE_ID_AMD_8131_BRIDGE) },
Michael Chan399de502005-10-03 14:02:39 -070013049 { PCI_DEVICE(PCI_VENDOR_ID_VIA,
13050 PCI_DEVICE_ID_VIA_8385_0) },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013051 { },
13052 };
13053 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013054 u32 pci_state_reg, grc_misc_cfg;
13055 u32 val;
13056 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013057 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013058
Linus Torvalds1da177e2005-04-16 15:20:36 -070013059 /* Force memory write invalidate off. If we leave it on,
13060 * then on 5700_BX chips we have to enable a workaround.
13061 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
13062 * to match the cacheline size. The Broadcom driver have this
13063 * workaround but turns MWI off all the times so never uses
13064 * it. This seems to suggest that the workaround is insufficient.
13065 */
13066 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13067 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
13068 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13069
13070 /* It is absolutely critical that TG3PCI_MISC_HOST_CTRL
13071 * has the register indirect write enable bit set before
13072 * we try to access any of the MMIO registers. It is also
13073 * critical that the PCI-X hw workaround situation is decided
13074 * before that as well.
13075 */
13076 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13077 &misc_ctrl_reg);
13078
13079 tp->pci_chip_rev_id = (misc_ctrl_reg >>
13080 MISC_HOST_CTRL_CHIPREV_SHIFT);
Matt Carlson795d01c2007-10-07 23:28:17 -070013081 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
13082 u32 prod_id_asic_rev;
13083
Matt Carlson5001e2f2009-11-13 13:03:51 +000013084 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
13085 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
Matt Carlsona50d0792010-06-05 17:24:37 +000013086 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013087 pci_read_config_dword(tp->pdev,
13088 TG3PCI_GEN2_PRODID_ASICREV,
13089 &prod_id_asic_rev);
Matt Carlsonb703df62009-12-03 08:36:21 +000013090 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
13091 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
13092 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
13093 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
13094 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
13095 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13096 pci_read_config_dword(tp->pdev,
13097 TG3PCI_GEN15_PRODID_ASICREV,
13098 &prod_id_asic_rev);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013099 else
13100 pci_read_config_dword(tp->pdev, TG3PCI_PRODID_ASICREV,
13101 &prod_id_asic_rev);
13102
Matt Carlson321d32a2008-11-21 17:22:19 -080013103 tp->pci_chip_rev_id = prod_id_asic_rev;
Matt Carlson795d01c2007-10-07 23:28:17 -070013104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013105
Michael Chanff645be2005-04-21 17:09:53 -070013106 /* Wrong chip ID in 5752 A0. This code can be removed later
13107 * as A0 is not in production.
13108 */
13109 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
13110 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
13111
Michael Chan68929142005-08-09 20:17:14 -070013112 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
13113 * we need to disable memory and use config. cycles
13114 * only to access all registers. The 5702/03 chips
13115 * can mistakenly decode the special cycles from the
13116 * ICH chipsets as memory write cycles, causing corruption
13117 * of register and memory space. Only certain ICH bridges
13118 * will drive special cycles with non-zero data during the
13119 * address phase which can fall within the 5703's address
13120 * range. This is not an ICH bug as the PCI spec allows
13121 * non-zero address during special cycles. However, only
13122 * these ICH bridges are known to drive non-zero addresses
13123 * during special cycles.
13124 *
13125 * Since special cycles do not cross PCI bridges, we only
13126 * enable this workaround if the 5703 is on the secondary
13127 * bus of these ICH bridges.
13128 */
13129 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
13130 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
13131 static struct tg3_dev_id {
13132 u32 vendor;
13133 u32 device;
13134 u32 rev;
13135 } ich_chipsets[] = {
13136 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
13137 PCI_ANY_ID },
13138 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
13139 PCI_ANY_ID },
13140 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
13141 0xa },
13142 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
13143 PCI_ANY_ID },
13144 { },
13145 };
13146 struct tg3_dev_id *pci_id = &ich_chipsets[0];
13147 struct pci_dev *bridge = NULL;
13148
13149 while (pci_id->vendor != 0) {
13150 bridge = pci_get_device(pci_id->vendor, pci_id->device,
13151 bridge);
13152 if (!bridge) {
13153 pci_id++;
13154 continue;
13155 }
13156 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070013157 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070013158 continue;
13159 }
13160 if (bridge->subordinate &&
13161 (bridge->subordinate->number ==
13162 tp->pdev->bus->number)) {
13163
13164 tp->tg3_flags2 |= TG3_FLG2_ICH_WORKAROUND;
13165 pci_dev_put(bridge);
13166 break;
13167 }
13168 }
13169 }
13170
Matt Carlson41588ba2008-04-19 18:12:33 -070013171 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
13172 static struct tg3_dev_id {
13173 u32 vendor;
13174 u32 device;
13175 } bridge_chipsets[] = {
13176 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
13177 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
13178 { },
13179 };
13180 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
13181 struct pci_dev *bridge = NULL;
13182
13183 while (pci_id->vendor != 0) {
13184 bridge = pci_get_device(pci_id->vendor,
13185 pci_id->device,
13186 bridge);
13187 if (!bridge) {
13188 pci_id++;
13189 continue;
13190 }
13191 if (bridge->subordinate &&
13192 (bridge->subordinate->number <=
13193 tp->pdev->bus->number) &&
13194 (bridge->subordinate->subordinate >=
13195 tp->pdev->bus->number)) {
13196 tp->tg3_flags3 |= TG3_FLG3_5701_DMA_BUG;
13197 pci_dev_put(bridge);
13198 break;
13199 }
13200 }
13201 }
13202
Michael Chan4a29cc22006-03-19 13:21:12 -080013203 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
13204 * DMA addresses > 40-bit. This bridge may have other additional
13205 * 57xx devices behind it in some 4-port NIC designs for example.
13206 * Any tg3 device found behind the bridge will also need the 40-bit
13207 * DMA workaround.
13208 */
Michael Chana4e2b342005-10-26 15:46:52 -070013209 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
13210 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
13211 tp->tg3_flags2 |= TG3_FLG2_5780_CLASS;
Michael Chan4a29cc22006-03-19 13:21:12 -080013212 tp->tg3_flags |= TG3_FLAG_40BIT_DMA_BUG;
Michael Chan4cf78e42005-07-25 12:29:19 -070013213 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a588792010-04-05 10:19:28 +000013214 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080013215 struct pci_dev *bridge = NULL;
13216
13217 do {
13218 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
13219 PCI_DEVICE_ID_SERVERWORKS_EPB,
13220 bridge);
13221 if (bridge && bridge->subordinate &&
13222 (bridge->subordinate->number <=
13223 tp->pdev->bus->number) &&
13224 (bridge->subordinate->subordinate >=
13225 tp->pdev->bus->number)) {
13226 tp->tg3_flags |= TG3_FLAG_40BIT_DMA_BUG;
13227 pci_dev_put(bridge);
13228 break;
13229 }
13230 } while (bridge);
13231 }
Michael Chan4cf78e42005-07-25 12:29:19 -070013232
Linus Torvalds1da177e2005-04-16 15:20:36 -070013233 /* Initialize misc host control in PCI block. */
13234 tp->misc_host_ctrl |= (misc_ctrl_reg &
13235 MISC_HOST_CTRL_CHIPREV);
13236 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13237 tp->misc_host_ctrl);
13238
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013239 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
13240 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
13241 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Michael Chan7544b092007-05-05 13:08:32 -070013242 tp->pdev_peer = tg3_find_peer(tp);
13243
Matt Carlsonc885e822010-08-02 11:25:57 +000013244 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13245 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13246 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
13247 tp->tg3_flags3 |= TG3_FLG3_5717_PLUS;
13248
Matt Carlson321d32a2008-11-21 17:22:19 -080013249 /* Intentionally exclude ASIC_REV_5906 */
13250 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Michael Chand9ab5ad2006-03-20 22:27:35 -080013251 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013252 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013253 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013254 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013255 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlsonc885e822010-08-02 11:25:57 +000013256 (tp->tg3_flags3 & TG3_FLG3_5717_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080013257 tp->tg3_flags3 |= TG3_FLG3_5755_PLUS;
13258
13259 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13260 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Michael Chanb5d37722006-09-27 16:06:21 -070013261 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013262 (tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
Michael Chana4e2b342005-10-26 15:46:52 -070013263 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
John W. Linville6708e5c2005-04-21 17:00:52 -070013264 tp->tg3_flags2 |= TG3_FLG2_5750_PLUS;
13265
John W. Linville1b440c562005-04-21 17:03:18 -070013266 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) ||
13267 (tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
13268 tp->tg3_flags2 |= TG3_FLG2_5705_PLUS;
13269
Matt Carlson027455a2008-12-21 20:19:30 -080013270 /* 5700 B0 chips do not support checksumming correctly due
13271 * to hardware bugs.
13272 */
13273 if (tp->pci_chip_rev_id == CHIPREV_ID_5700_B0)
13274 tp->tg3_flags |= TG3_FLAG_BROKEN_CHECKSUMS;
13275 else {
Eric Dumazet7fe876a2010-07-08 06:14:55 +000013276 unsigned long features = NETIF_F_IP_CSUM | NETIF_F_SG | NETIF_F_GRO;
13277
Matt Carlson027455a2008-12-21 20:19:30 -080013278 tp->tg3_flags |= TG3_FLAG_RX_CHECKSUMS;
Matt Carlson027455a2008-12-21 20:19:30 -080013279 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Eric Dumazet7fe876a2010-07-08 06:14:55 +000013280 features |= NETIF_F_IPV6_CSUM;
13281 tp->dev->features |= features;
13282 vlan_features_add(tp->dev, features);
Matt Carlson027455a2008-12-21 20:19:30 -080013283 }
13284
Matt Carlson507399f2009-11-13 13:03:37 +000013285 /* Determine TSO capabilities */
Matt Carlsonc885e822010-08-02 11:25:57 +000013286 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)
Matt Carlsone849cdc2009-11-13 13:03:38 +000013287 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_3;
13288 else if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
13289 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlson507399f2009-11-13 13:03:37 +000013290 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_2;
13291 else if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
13292 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_1 | TG3_FLG2_TSO_BUG;
13293 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
13294 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
13295 tp->tg3_flags2 &= ~TG3_FLG2_TSO_BUG;
13296 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13297 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13298 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
13299 tp->tg3_flags2 |= TG3_FLG2_TSO_BUG;
13300 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
13301 tp->fw_needed = FIRMWARE_TG3TSO5;
13302 else
13303 tp->fw_needed = FIRMWARE_TG3TSO;
13304 }
13305
13306 tp->irq_max = 1;
13307
Michael Chan5a6f3072006-03-20 22:28:05 -080013308 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
Michael Chan7544b092007-05-05 13:08:32 -070013309 tp->tg3_flags |= TG3_FLAG_SUPPORT_MSI;
13310 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
13311 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
13312 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
13313 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
13314 tp->pdev_peer == tp->pdev))
13315 tp->tg3_flags &= ~TG3_FLAG_SUPPORT_MSI;
13316
Matt Carlson321d32a2008-11-21 17:22:19 -080013317 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070013318 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanfcfa0a32006-03-20 22:28:41 -080013319 tp->tg3_flags2 |= TG3_FLG2_1SHOT_MSI;
Michael Chan52c0fd82006-06-29 20:15:54 -070013320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013321
Matt Carlsonc885e822010-08-02 11:25:57 +000013322 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
Matt Carlson507399f2009-11-13 13:03:37 +000013323 tp->tg3_flags |= TG3_FLAG_SUPPORT_MSIX;
13324 tp->irq_max = TG3_IRQ_MAX_VECS;
13325 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013326 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000013327
Matt Carlson615774f2009-11-13 13:03:39 +000013328 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsona50d0792010-06-05 17:24:37 +000013329 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlson615774f2009-11-13 13:03:39 +000013330 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
13331 tp->tg3_flags3 |= TG3_FLG3_SHORT_DMA_BUG;
13332 else if (!(tp->tg3_flags3 & TG3_FLG3_5755_PLUS)) {
13333 tp->tg3_flags3 |= TG3_FLG3_4G_DMA_BNDRY_BUG;
13334 tp->tg3_flags3 |= TG3_FLG3_40BIT_DMA_LIMIT_BUG;
Matt Carlson0e1406d2009-11-02 12:33:33 +000013335 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013336
Matt Carlsonc885e822010-08-02 11:25:57 +000013337 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)
Matt Carlsonb703df62009-12-03 08:36:21 +000013338 tp->tg3_flags3 |= TG3_FLG3_USE_JUMBO_BDFLAG;
13339
Matt Carlsonf51f3562008-05-25 23:45:08 -070013340 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
Matt Carlsonc6cdf432010-04-05 10:19:26 +000013341 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
13342 (tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG))
Matt Carlson8f666b02009-08-28 13:58:24 +000013343 tp->tg3_flags |= TG3_FLAG_JUMBO_CAPABLE;
Michael Chan0f893dc2005-07-25 12:30:38 -070013344
Matt Carlson52f44902008-11-21 17:17:04 -080013345 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
13346 &pci_state_reg);
13347
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013348 tp->pcie_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_EXP);
13349 if (tp->pcie_cap != 0) {
13350 u16 lnkctl;
13351
Linus Torvalds1da177e2005-04-16 15:20:36 -070013352 tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS;
Matt Carlson5f5c51e2007-11-12 21:19:37 -080013353
13354 pcie_set_readrq(tp->pdev, 4096);
13355
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013356 pci_read_config_word(tp->pdev,
13357 tp->pcie_cap + PCI_EXP_LNKCTL,
13358 &lnkctl);
13359 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
13360 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanc7835a72006-11-15 21:14:42 -080013361 tp->tg3_flags2 &= ~TG3_FLG2_HW_TSO_2;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013362 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013363 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000013364 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
13365 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013366 tp->tg3_flags3 |= TG3_FLG3_CLKREQ_BUG;
Matt Carlson614b0592010-01-20 16:58:02 +000013367 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
13368 tp->tg3_flags3 |= TG3_FLG3_L1PLLPD_EN;
Michael Chanc7835a72006-11-15 21:14:42 -080013369 }
Matt Carlson52f44902008-11-21 17:17:04 -080013370 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Matt Carlsonfcb389d2008-11-03 16:55:44 -080013371 tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS;
Matt Carlson52f44902008-11-21 17:17:04 -080013372 } else if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
13373 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
13374 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
13375 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000013376 dev_err(&tp->pdev->dev,
13377 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080013378 return -EIO;
13379 }
13380
13381 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
13382 tp->tg3_flags |= TG3_FLAG_PCIX_MODE;
13383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013384
Michael Chan399de502005-10-03 14:02:39 -070013385 /* If we have an AMD 762 or VIA K8T800 chipset, write
13386 * reordering to the mailbox registers done by the host
13387 * controller can cause major troubles. We read back from
13388 * every mailbox register write to force the writes to be
13389 * posted to the chip in order.
13390 */
13391 if (pci_dev_present(write_reorder_chipsets) &&
13392 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
13393 tp->tg3_flags |= TG3_FLAG_MBOX_WRITE_REORDER;
13394
Matt Carlson69fc4052008-12-21 20:19:57 -080013395 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
13396 &tp->pci_cacheline_sz);
13397 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13398 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013399 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
13400 tp->pci_lat_timer < 64) {
13401 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080013402 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13403 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013404 }
13405
Matt Carlson52f44902008-11-21 17:17:04 -080013406 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
13407 /* 5700 BX chips need to have their TX producer index
13408 * mailboxes written twice to workaround a bug.
13409 */
13410 tp->tg3_flags |= TG3_FLAG_TXD_MBOX_HWBUG;
Matt Carlson9974a352007-10-07 23:27:28 -070013411
Matt Carlson52f44902008-11-21 17:17:04 -080013412 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013413 *
13414 * The workaround is to use indirect register accesses
13415 * for all chip writes not to mailbox registers.
13416 */
Matt Carlson52f44902008-11-21 17:17:04 -080013417 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013418 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013419
13420 tp->tg3_flags |= TG3_FLAG_PCIX_TARGET_HWBUG;
13421
13422 /* The chip can have it's power management PCI config
13423 * space registers clobbered due to this bug.
13424 * So explicitly force the chip into D0 here.
13425 */
Matt Carlson9974a352007-10-07 23:27:28 -070013426 pci_read_config_dword(tp->pdev,
13427 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013428 &pm_reg);
13429 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
13430 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070013431 pci_write_config_dword(tp->pdev,
13432 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013433 pm_reg);
13434
13435 /* Also, force SERR#/PERR# in PCI command. */
13436 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13437 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
13438 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13439 }
13440 }
13441
Linus Torvalds1da177e2005-04-16 15:20:36 -070013442 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
13443 tp->tg3_flags |= TG3_FLAG_PCI_HIGH_SPEED;
13444 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
13445 tp->tg3_flags |= TG3_FLAG_PCI_32BIT;
13446
13447 /* Chip-specific fixup from Broadcom driver */
13448 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
13449 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
13450 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
13451 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
13452 }
13453
Michael Chan1ee582d2005-08-09 20:16:46 -070013454 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070013455 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013456 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070013457 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070013458 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013459 tp->write32_tx_mbox = tg3_write32;
13460 tp->write32_rx_mbox = tg3_write32;
13461
13462 /* Various workaround register access methods */
13463 if (tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG)
13464 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013465 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
13466 ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
13467 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
13468 /*
13469 * Back to back register writes can cause problems on these
13470 * chips, the workaround is to read back all reg writes
13471 * except those to mailbox regs.
13472 *
13473 * See tg3_write_indirect_reg32().
13474 */
Michael Chan1ee582d2005-08-09 20:16:46 -070013475 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013476 }
13477
Michael Chan1ee582d2005-08-09 20:16:46 -070013478 if ((tp->tg3_flags & TG3_FLAG_TXD_MBOX_HWBUG) ||
13479 (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)) {
13480 tp->write32_tx_mbox = tg3_write32_tx_mbox;
13481 if (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)
13482 tp->write32_rx_mbox = tg3_write_flush_reg32;
13483 }
Michael Chan20094932005-08-09 20:16:32 -070013484
Michael Chan68929142005-08-09 20:17:14 -070013485 if (tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND) {
13486 tp->read32 = tg3_read_indirect_reg32;
13487 tp->write32 = tg3_write_indirect_reg32;
13488 tp->read32_mbox = tg3_read_indirect_mbox;
13489 tp->write32_mbox = tg3_write_indirect_mbox;
13490 tp->write32_tx_mbox = tg3_write_indirect_mbox;
13491 tp->write32_rx_mbox = tg3_write_indirect_mbox;
13492
13493 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070013494 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070013495
13496 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13497 pci_cmd &= ~PCI_COMMAND_MEMORY;
13498 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13499 }
Michael Chanb5d37722006-09-27 16:06:21 -070013500 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
13501 tp->read32_mbox = tg3_read32_mbox_5906;
13502 tp->write32_mbox = tg3_write32_mbox_5906;
13503 tp->write32_tx_mbox = tg3_write32_mbox_5906;
13504 tp->write32_rx_mbox = tg3_write32_mbox_5906;
13505 }
Michael Chan68929142005-08-09 20:17:14 -070013506
Michael Chanbbadf502006-04-06 21:46:34 -070013507 if (tp->write32 == tg3_write_indirect_reg32 ||
13508 ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) &&
13509 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070013510 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Michael Chanbbadf502006-04-06 21:46:34 -070013511 tp->tg3_flags |= TG3_FLAG_SRAM_USE_CONFIG;
13512
Michael Chan7d0c41e2005-04-21 17:06:20 -070013513 /* Get eeprom hw config before calling tg3_set_power_state().
Michael Chan9d26e212006-12-07 00:21:14 -080013514 * In particular, the TG3_FLG2_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070013515 * determined before calling tg3_set_power_state() so that
13516 * we know whether or not to switch out of Vaux power.
13517 * When the flag is set, it means that GPIO1 is used for eeprom
13518 * write protect and also implies that it is a LOM where GPIOs
13519 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013520 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070013521 tg3_get_eeprom_hw_cfg(tp);
13522
Matt Carlson0d3031d2007-10-10 18:02:43 -070013523 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
13524 /* Allow reads and writes to the
13525 * APE register and memory space.
13526 */
13527 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +000013528 PCISTATE_ALLOW_APE_SHMEM_WR |
13529 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070013530 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
13531 pci_state_reg);
13532 }
13533
Matt Carlson9936bcf2007-10-10 18:03:07 -070013534 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013535 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013536 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013537 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlsonc885e822010-08-02 11:25:57 +000013538 (tp->tg3_flags3 & TG3_FLG3_5717_PLUS))
Matt Carlsond30cdd22007-10-07 23:28:35 -070013539 tp->tg3_flags |= TG3_FLAG_CPMU_PRESENT;
13540
Michael Chan314fba32005-04-21 17:07:04 -070013541 /* Set up tp->grc_local_ctrl before calling tg3_set_power_state().
13542 * GPIO1 driven high will bring 5700's external PHY out of reset.
13543 * It is also used as eeprom write protect on LOMs.
13544 */
13545 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
13546 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
13547 (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT))
13548 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
13549 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070013550 /* Unused GPIO3 must be driven as output on 5752 because there
13551 * are no pull-up resistors on unused GPIO pins.
13552 */
13553 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
13554 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070013555
Matt Carlson321d32a2008-11-21 17:22:19 -080013556 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsoncb4ed1f2010-01-20 16:58:09 +000013557 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
13558 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Michael Chanaf36e6b2006-03-23 01:28:06 -080013559 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
13560
Matt Carlson8d519ab2009-04-20 06:58:01 +000013561 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
13562 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070013563 /* Turn off the debug UART. */
13564 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
13565 if (tp->tg3_flags2 & TG3_FLG2_IS_NIC)
13566 /* Keep VMain power. */
13567 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
13568 GRC_LCLCTRL_GPIO_OUTPUT0;
13569 }
13570
Linus Torvalds1da177e2005-04-16 15:20:36 -070013571 /* Force the chip into D0. */
Michael Chanbc1c7562006-03-20 17:48:03 -080013572 err = tg3_set_power_state(tp, PCI_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013573 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000013574 dev_err(&tp->pdev->dev, "Transition to D0 failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070013575 return err;
13576 }
13577
Linus Torvalds1da177e2005-04-16 15:20:36 -070013578 /* Derive initial jumbo mode from MTU assigned in
13579 * ether_setup() via the alloc_etherdev() call
13580 */
Michael Chan0f893dc2005-07-25 12:30:38 -070013581 if (tp->dev->mtu > ETH_DATA_LEN &&
Michael Chana4e2b342005-10-26 15:46:52 -070013582 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Michael Chan0f893dc2005-07-25 12:30:38 -070013583 tp->tg3_flags |= TG3_FLAG_JUMBO_RING_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013584
13585 /* Determine WakeOnLan speed to use. */
13586 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13587 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
13588 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
13589 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
13590 tp->tg3_flags &= ~(TG3_FLAG_WOL_SPEED_100MB);
13591 } else {
13592 tp->tg3_flags |= TG3_FLAG_WOL_SPEED_100MB;
13593 }
13594
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013595 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013596 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013597
Linus Torvalds1da177e2005-04-16 15:20:36 -070013598 /* A few boards don't want Ethernet@WireSpeed phy feature */
13599 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
13600 ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
13601 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070013602 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013603 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
13604 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
13605 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013606
13607 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
13608 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013609 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013610 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013611 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013612
Matt Carlson321d32a2008-11-21 17:22:19 -080013613 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013614 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080013615 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013616 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
Matt Carlsonc885e822010-08-02 11:25:57 +000013617 !(tp->tg3_flags3 & TG3_FLG3_5717_PLUS)) {
Michael Chanc424cb22006-04-29 18:56:34 -070013618 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013619 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013620 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
13621 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080013622 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
13623 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013624 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080013625 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013626 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080013627 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013628 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070013629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013630
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013631 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
13632 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
13633 tp->phy_otp = tg3_read_otp_phycfg(tp);
13634 if (tp->phy_otp == 0)
13635 tp->phy_otp = TG3_OTP_DEFAULT;
13636 }
13637
Matt Carlsonf51f3562008-05-25 23:45:08 -070013638 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT)
Matt Carlson8ef21422008-05-02 16:47:53 -070013639 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
13640 else
13641 tp->mi_mode = MAC_MI_MODE_BASE;
13642
Linus Torvalds1da177e2005-04-16 15:20:36 -070013643 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013644 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
13645 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
13646 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
13647
Matt Carlson321d32a2008-11-21 17:22:19 -080013648 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
13649 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson57e69832008-05-25 23:48:31 -070013650 tp->tg3_flags3 |= TG3_FLG3_USE_PHYLIB;
13651
Matt Carlson158d7ab2008-05-29 01:37:54 -070013652 err = tg3_mdio_init(tp);
13653 if (err)
13654 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013655
13656 /* Initialize data/descriptor byte/word swapping. */
13657 val = tr32(GRC_MODE);
13658 val &= GRC_MODE_HOST_STACKUP;
13659 tw32(GRC_MODE, val | tp->grc_mode);
13660
13661 tg3_switch_clocks(tp);
13662
13663 /* Clear this out for sanity. */
13664 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
13665
13666 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
13667 &pci_state_reg);
13668 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
13669 (tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG) == 0) {
13670 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
13671
13672 if (chiprevid == CHIPREV_ID_5701_A0 ||
13673 chiprevid == CHIPREV_ID_5701_B0 ||
13674 chiprevid == CHIPREV_ID_5701_B2 ||
13675 chiprevid == CHIPREV_ID_5701_B5) {
13676 void __iomem *sram_base;
13677
13678 /* Write some dummy words into the SRAM status block
13679 * area, see if it reads back correctly. If the return
13680 * value is bad, force enable the PCIX workaround.
13681 */
13682 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
13683
13684 writel(0x00000000, sram_base);
13685 writel(0x00000000, sram_base + 4);
13686 writel(0xffffffff, sram_base + 4);
13687 if (readl(sram_base) != 0x00000000)
13688 tp->tg3_flags |= TG3_FLAG_PCIX_TARGET_HWBUG;
13689 }
13690 }
13691
13692 udelay(50);
13693 tg3_nvram_init(tp);
13694
13695 grc_misc_cfg = tr32(GRC_MISC_CFG);
13696 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
13697
Linus Torvalds1da177e2005-04-16 15:20:36 -070013698 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
13699 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
13700 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
13701 tp->tg3_flags2 |= TG3_FLG2_IS_5788;
13702
David S. Millerfac9b832005-05-18 22:46:34 -070013703 if (!(tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
13704 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700))
13705 tp->tg3_flags |= TG3_FLAG_TAGGED_STATUS;
13706 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) {
13707 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
13708 HOSTCC_MODE_CLRTICK_TXBD);
13709
13710 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
13711 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13712 tp->misc_host_ctrl);
13713 }
13714
Matt Carlson3bda1252008-08-15 14:08:22 -070013715 /* Preserve the APE MAC_MODE bits */
13716 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
13717 tp->mac_mode = tr32(MAC_MODE) |
13718 MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
13719 else
13720 tp->mac_mode = TG3_DEF_MAC_MODE;
13721
Linus Torvalds1da177e2005-04-16 15:20:36 -070013722 /* these are limited to 10/100 only */
13723 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
13724 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
13725 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
13726 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
13727 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
13728 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
13729 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
13730 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
13731 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
Michael Chan676917d2006-12-07 00:20:22 -080013732 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
13733 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013734 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
Matt Carlsond1101142010-02-17 15:16:55 +000013735 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
13736 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013737 (tp->phy_flags & TG3_PHYFLG_IS_FET))
13738 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013739
13740 err = tg3_phy_probe(tp);
13741 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000013742 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013743 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013744 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013745 }
13746
Matt Carlson184b8902010-04-05 10:19:25 +000013747 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080013748 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013749
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013750 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
13751 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013752 } else {
13753 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013754 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013755 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013756 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013757 }
13758
13759 /* 5700 {AX,BX} chips have a broken status block link
13760 * change bit implementation, so we must use the
13761 * status register in those cases.
13762 */
13763 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
13764 tp->tg3_flags |= TG3_FLAG_USE_LINKCHG_REG;
13765 else
13766 tp->tg3_flags &= ~TG3_FLAG_USE_LINKCHG_REG;
13767
13768 /* The led_ctrl is set during tg3_phy_probe, here we might
13769 * have to force the link status polling mechanism based
13770 * upon subsystem IDs.
13771 */
13772 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070013773 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013774 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
13775 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
13776 tp->tg3_flags |= TG3_FLAG_USE_LINKCHG_REG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013777 }
13778
13779 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013780 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013781 tp->tg3_flags |= TG3_FLAG_POLL_SERDES;
13782 else
13783 tp->tg3_flags &= ~TG3_FLAG_POLL_SERDES;
13784
Matt Carlson9dc7a112010-04-12 06:58:28 +000013785 tp->rx_offset = NET_IP_ALIGN + TG3_RX_HEADROOM;
Matt Carlsond2757fc2010-04-12 06:58:27 +000013786 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013787 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsond2757fc2010-04-12 06:58:27 +000013788 (tp->tg3_flags & TG3_FLAG_PCIX_MODE) != 0) {
Matt Carlson9dc7a112010-04-12 06:58:28 +000013789 tp->rx_offset -= NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000013790#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000013791 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000013792#endif
13793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013794
Matt Carlson2c49a442010-09-30 10:34:35 +000013795 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
13796 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013797 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
13798
Matt Carlson2c49a442010-09-30 10:34:35 +000013799 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070013800
13801 /* Increment the rx prod index on the rx std ring by at most
13802 * 8 for these chips to workaround hw errata.
13803 */
13804 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13805 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
13806 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
13807 tp->rx_std_max_post = 8;
13808
Matt Carlson8ed5d972007-05-07 00:25:49 -070013809 if (tp->tg3_flags & TG3_FLAG_ASPM_WORKAROUND)
13810 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
13811 PCIE_PWR_MGMT_L1_THRESH_MSK;
13812
Linus Torvalds1da177e2005-04-16 15:20:36 -070013813 return err;
13814}
13815
David S. Miller49b6e95f2007-03-29 01:38:42 -070013816#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070013817static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
13818{
13819 struct net_device *dev = tp->dev;
13820 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070013821 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070013822 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070013823 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013824
David S. Miller49b6e95f2007-03-29 01:38:42 -070013825 addr = of_get_property(dp, "local-mac-address", &len);
13826 if (addr && len == 6) {
13827 memcpy(dev->dev_addr, addr, 6);
13828 memcpy(dev->perm_addr, dev->dev_addr, 6);
13829 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013830 }
13831 return -ENODEV;
13832}
13833
13834static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
13835{
13836 struct net_device *dev = tp->dev;
13837
13838 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070013839 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013840 return 0;
13841}
13842#endif
13843
13844static int __devinit tg3_get_device_address(struct tg3 *tp)
13845{
13846 struct net_device *dev = tp->dev;
13847 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080013848 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013849
David S. Miller49b6e95f2007-03-29 01:38:42 -070013850#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070013851 if (!tg3_get_macaddr_sparc(tp))
13852 return 0;
13853#endif
13854
13855 mac_offset = 0x7c;
David S. Millerf49639e2006-06-09 11:58:36 -070013856 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) ||
Michael Chana4e2b342005-10-26 15:46:52 -070013857 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013858 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
13859 mac_offset = 0xcc;
13860 if (tg3_nvram_lock(tp))
13861 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
13862 else
13863 tg3_nvram_unlock(tp);
Matt Carlsona50d0792010-06-05 17:24:37 +000013864 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13865 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) {
13866 if (PCI_FUNC(tp->pdev->devfn) & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000013867 mac_offset = 0xcc;
Matt Carlsona50d0792010-06-05 17:24:37 +000013868 if (PCI_FUNC(tp->pdev->devfn) > 1)
13869 mac_offset += 0x18c;
Matt Carlsona1b950d2009-09-01 13:20:17 +000013870 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070013871 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013872
13873 /* First try to get it from MAC address mailbox. */
13874 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
13875 if ((hi >> 16) == 0x484b) {
13876 dev->dev_addr[0] = (hi >> 8) & 0xff;
13877 dev->dev_addr[1] = (hi >> 0) & 0xff;
13878
13879 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
13880 dev->dev_addr[2] = (lo >> 24) & 0xff;
13881 dev->dev_addr[3] = (lo >> 16) & 0xff;
13882 dev->dev_addr[4] = (lo >> 8) & 0xff;
13883 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013884
Michael Chan008652b2006-03-27 23:14:53 -080013885 /* Some old bootcode may report a 0 MAC address in SRAM */
13886 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
13887 }
13888 if (!addr_ok) {
13889 /* Next, try NVRAM. */
Matt Carlsondf259d82009-04-20 06:57:14 +000013890 if (!(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) &&
13891 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000013892 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070013893 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
13894 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080013895 }
13896 /* Finally just fetch it out of the MAC control regs. */
13897 else {
13898 hi = tr32(MAC_ADDR_0_HIGH);
13899 lo = tr32(MAC_ADDR_0_LOW);
13900
13901 dev->dev_addr[5] = lo & 0xff;
13902 dev->dev_addr[4] = (lo >> 8) & 0xff;
13903 dev->dev_addr[3] = (lo >> 16) & 0xff;
13904 dev->dev_addr[2] = (lo >> 24) & 0xff;
13905 dev->dev_addr[1] = hi & 0xff;
13906 dev->dev_addr[0] = (hi >> 8) & 0xff;
13907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013908 }
13909
13910 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070013911#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070013912 if (!tg3_get_default_macaddr_sparc(tp))
13913 return 0;
13914#endif
13915 return -EINVAL;
13916 }
John W. Linville2ff43692005-09-12 14:44:20 -070013917 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013918 return 0;
13919}
13920
David S. Miller59e6b432005-05-18 22:50:10 -070013921#define BOUNDARY_SINGLE_CACHELINE 1
13922#define BOUNDARY_MULTI_CACHELINE 2
13923
13924static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
13925{
13926 int cacheline_size;
13927 u8 byte;
13928 int goal;
13929
13930 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
13931 if (byte == 0)
13932 cacheline_size = 1024;
13933 else
13934 cacheline_size = (int) byte * 4;
13935
13936 /* On 5703 and later chips, the boundary bits have no
13937 * effect.
13938 */
13939 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13940 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13941 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
13942 goto out;
13943
13944#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
13945 goal = BOUNDARY_MULTI_CACHELINE;
13946#else
13947#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
13948 goal = BOUNDARY_SINGLE_CACHELINE;
13949#else
13950 goal = 0;
13951#endif
13952#endif
13953
Matt Carlsonc885e822010-08-02 11:25:57 +000013954 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000013955 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
13956 goto out;
13957 }
13958
David S. Miller59e6b432005-05-18 22:50:10 -070013959 if (!goal)
13960 goto out;
13961
13962 /* PCI controllers on most RISC systems tend to disconnect
13963 * when a device tries to burst across a cache-line boundary.
13964 * Therefore, letting tg3 do so just wastes PCI bandwidth.
13965 *
13966 * Unfortunately, for PCI-E there are only limited
13967 * write-side controls for this, and thus for reads
13968 * we will still get the disconnects. We'll also waste
13969 * these PCI cycles for both read and write for chips
13970 * other than 5700 and 5701 which do not implement the
13971 * boundary bits.
13972 */
13973 if ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) &&
13974 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)) {
13975 switch (cacheline_size) {
13976 case 16:
13977 case 32:
13978 case 64:
13979 case 128:
13980 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13981 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
13982 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
13983 } else {
13984 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
13985 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
13986 }
13987 break;
13988
13989 case 256:
13990 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
13991 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
13992 break;
13993
13994 default:
13995 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
13996 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
13997 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070013998 }
David S. Miller59e6b432005-05-18 22:50:10 -070013999 } else if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
14000 switch (cacheline_size) {
14001 case 16:
14002 case 32:
14003 case 64:
14004 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14005 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14006 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
14007 break;
14008 }
14009 /* fallthrough */
14010 case 128:
14011 default:
14012 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14013 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
14014 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014015 }
David S. Miller59e6b432005-05-18 22:50:10 -070014016 } else {
14017 switch (cacheline_size) {
14018 case 16:
14019 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14020 val |= (DMA_RWCTRL_READ_BNDRY_16 |
14021 DMA_RWCTRL_WRITE_BNDRY_16);
14022 break;
14023 }
14024 /* fallthrough */
14025 case 32:
14026 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14027 val |= (DMA_RWCTRL_READ_BNDRY_32 |
14028 DMA_RWCTRL_WRITE_BNDRY_32);
14029 break;
14030 }
14031 /* fallthrough */
14032 case 64:
14033 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14034 val |= (DMA_RWCTRL_READ_BNDRY_64 |
14035 DMA_RWCTRL_WRITE_BNDRY_64);
14036 break;
14037 }
14038 /* fallthrough */
14039 case 128:
14040 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14041 val |= (DMA_RWCTRL_READ_BNDRY_128 |
14042 DMA_RWCTRL_WRITE_BNDRY_128);
14043 break;
14044 }
14045 /* fallthrough */
14046 case 256:
14047 val |= (DMA_RWCTRL_READ_BNDRY_256 |
14048 DMA_RWCTRL_WRITE_BNDRY_256);
14049 break;
14050 case 512:
14051 val |= (DMA_RWCTRL_READ_BNDRY_512 |
14052 DMA_RWCTRL_WRITE_BNDRY_512);
14053 break;
14054 case 1024:
14055 default:
14056 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
14057 DMA_RWCTRL_WRITE_BNDRY_1024);
14058 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014059 }
David S. Miller59e6b432005-05-18 22:50:10 -070014060 }
14061
14062out:
14063 return val;
14064}
14065
Linus Torvalds1da177e2005-04-16 15:20:36 -070014066static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
14067{
14068 struct tg3_internal_buffer_desc test_desc;
14069 u32 sram_dma_descs;
14070 int i, ret;
14071
14072 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
14073
14074 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
14075 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
14076 tw32(RDMAC_STATUS, 0);
14077 tw32(WDMAC_STATUS, 0);
14078
14079 tw32(BUFMGR_MODE, 0);
14080 tw32(FTQ_RESET, 0);
14081
14082 test_desc.addr_hi = ((u64) buf_dma) >> 32;
14083 test_desc.addr_lo = buf_dma & 0xffffffff;
14084 test_desc.nic_mbuf = 0x00002100;
14085 test_desc.len = size;
14086
14087 /*
14088 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
14089 * the *second* time the tg3 driver was getting loaded after an
14090 * initial scan.
14091 *
14092 * Broadcom tells me:
14093 * ...the DMA engine is connected to the GRC block and a DMA
14094 * reset may affect the GRC block in some unpredictable way...
14095 * The behavior of resets to individual blocks has not been tested.
14096 *
14097 * Broadcom noted the GRC reset will also reset all sub-components.
14098 */
14099 if (to_device) {
14100 test_desc.cqid_sqid = (13 << 8) | 2;
14101
14102 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
14103 udelay(40);
14104 } else {
14105 test_desc.cqid_sqid = (16 << 8) | 7;
14106
14107 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
14108 udelay(40);
14109 }
14110 test_desc.flags = 0x00000005;
14111
14112 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
14113 u32 val;
14114
14115 val = *(((u32 *)&test_desc) + i);
14116 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
14117 sram_dma_descs + (i * sizeof(u32)));
14118 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
14119 }
14120 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
14121
Matt Carlson859a588792010-04-05 10:19:28 +000014122 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014123 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a588792010-04-05 10:19:28 +000014124 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070014125 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014126
14127 ret = -ENODEV;
14128 for (i = 0; i < 40; i++) {
14129 u32 val;
14130
14131 if (to_device)
14132 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
14133 else
14134 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
14135 if ((val & 0xffff) == sram_dma_descs) {
14136 ret = 0;
14137 break;
14138 }
14139
14140 udelay(100);
14141 }
14142
14143 return ret;
14144}
14145
David S. Millerded73402005-05-23 13:59:47 -070014146#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070014147
14148static int __devinit tg3_test_dma(struct tg3 *tp)
14149{
14150 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070014151 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014152 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014153
14154 buf = pci_alloc_consistent(tp->pdev, TEST_BUFFER_SIZE, &buf_dma);
14155 if (!buf) {
14156 ret = -ENOMEM;
14157 goto out_nofree;
14158 }
14159
14160 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
14161 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
14162
David S. Miller59e6b432005-05-18 22:50:10 -070014163 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014164
Matt Carlsonc885e822010-08-02 11:25:57 +000014165 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014166 goto out;
14167
Linus Torvalds1da177e2005-04-16 15:20:36 -070014168 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
14169 /* DMA read watermark not used on PCIE */
14170 tp->dma_rwctrl |= 0x00180000;
14171 } else if (!(tp->tg3_flags & TG3_FLAG_PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070014172 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
14173 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014174 tp->dma_rwctrl |= 0x003f0000;
14175 else
14176 tp->dma_rwctrl |= 0x003f000f;
14177 } else {
14178 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14179 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
14180 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080014181 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014182
Michael Chan4a29cc22006-03-19 13:21:12 -080014183 /* If the 5704 is behind the EPB bridge, we can
14184 * do the less restrictive ONE_DMA workaround for
14185 * better performance.
14186 */
14187 if ((tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG) &&
14188 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14189 tp->dma_rwctrl |= 0x8000;
14190 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014191 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
14192
Michael Chan49afdeb2007-02-13 12:17:03 -080014193 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
14194 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070014195 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080014196 tp->dma_rwctrl |=
14197 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
14198 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
14199 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070014200 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
14201 /* 5780 always in PCIX mode */
14202 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070014203 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
14204 /* 5714 always in PCIX mode */
14205 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014206 } else {
14207 tp->dma_rwctrl |= 0x001b000f;
14208 }
14209 }
14210
14211 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14212 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14213 tp->dma_rwctrl &= 0xfffffff0;
14214
14215 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14216 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
14217 /* Remove this if it causes problems for some boards. */
14218 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
14219
14220 /* On 5700/5701 chips, we need to set this bit.
14221 * Otherwise the chip will issue cacheline transactions
14222 * to streamable DMA memory with not all the byte
14223 * enables turned on. This is an error on several
14224 * RISC PCI controllers, in particular sparc64.
14225 *
14226 * On 5703/5704 chips, this bit has been reassigned
14227 * a different meaning. In particular, it is used
14228 * on those chips to enable a PCI-X workaround.
14229 */
14230 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
14231 }
14232
14233 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14234
14235#if 0
14236 /* Unneeded, already done by tg3_get_invariants. */
14237 tg3_switch_clocks(tp);
14238#endif
14239
Linus Torvalds1da177e2005-04-16 15:20:36 -070014240 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14241 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
14242 goto out;
14243
David S. Miller59e6b432005-05-18 22:50:10 -070014244 /* It is best to perform DMA test with maximum write burst size
14245 * to expose the 5700/5701 write DMA bug.
14246 */
14247 saved_dma_rwctrl = tp->dma_rwctrl;
14248 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14249 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14250
Linus Torvalds1da177e2005-04-16 15:20:36 -070014251 while (1) {
14252 u32 *p = buf, i;
14253
14254 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
14255 p[i] = i;
14256
14257 /* Send the buffer to the chip. */
14258 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
14259 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000014260 dev_err(&tp->pdev->dev,
14261 "%s: Buffer write failed. err = %d\n",
14262 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014263 break;
14264 }
14265
14266#if 0
14267 /* validate data reached card RAM correctly. */
14268 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14269 u32 val;
14270 tg3_read_mem(tp, 0x2100 + (i*4), &val);
14271 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000014272 dev_err(&tp->pdev->dev,
14273 "%s: Buffer corrupted on device! "
14274 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014275 /* ret = -ENODEV here? */
14276 }
14277 p[i] = 0;
14278 }
14279#endif
14280 /* Now read it back. */
14281 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
14282 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000014283 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
14284 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014285 break;
14286 }
14287
14288 /* Verify it. */
14289 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14290 if (p[i] == i)
14291 continue;
14292
David S. Miller59e6b432005-05-18 22:50:10 -070014293 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14294 DMA_RWCTRL_WRITE_BNDRY_16) {
14295 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014296 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
14297 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14298 break;
14299 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000014300 dev_err(&tp->pdev->dev,
14301 "%s: Buffer corrupted on read back! "
14302 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014303 ret = -ENODEV;
14304 goto out;
14305 }
14306 }
14307
14308 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
14309 /* Success. */
14310 ret = 0;
14311 break;
14312 }
14313 }
David S. Miller59e6b432005-05-18 22:50:10 -070014314 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14315 DMA_RWCTRL_WRITE_BNDRY_16) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014316 static struct pci_device_id dma_wait_state_chipsets[] = {
14317 { PCI_DEVICE(PCI_VENDOR_ID_APPLE,
14318 PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
14319 { },
14320 };
14321
David S. Miller59e6b432005-05-18 22:50:10 -070014322 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070014323 * now look for chipsets that are known to expose the
14324 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070014325 */
Michael Chan6d1cfba2005-06-08 14:13:14 -070014326 if (pci_dev_present(dma_wait_state_chipsets)) {
14327 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14328 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a588792010-04-05 10:19:28 +000014329 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014330 /* Safe to use the calculated DMA boundary. */
14331 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a588792010-04-05 10:19:28 +000014332 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070014333
David S. Miller59e6b432005-05-18 22:50:10 -070014334 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014336
14337out:
14338 pci_free_consistent(tp->pdev, TEST_BUFFER_SIZE, buf, buf_dma);
14339out_nofree:
14340 return ret;
14341}
14342
14343static void __devinit tg3_init_link_config(struct tg3 *tp)
14344{
14345 tp->link_config.advertising =
14346 (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
14347 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
14348 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full |
14349 ADVERTISED_Autoneg | ADVERTISED_MII);
14350 tp->link_config.speed = SPEED_INVALID;
14351 tp->link_config.duplex = DUPLEX_INVALID;
14352 tp->link_config.autoneg = AUTONEG_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014353 tp->link_config.active_speed = SPEED_INVALID;
14354 tp->link_config.active_duplex = DUPLEX_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014355 tp->link_config.orig_speed = SPEED_INVALID;
14356 tp->link_config.orig_duplex = DUPLEX_INVALID;
14357 tp->link_config.orig_autoneg = AUTONEG_INVALID;
14358}
14359
14360static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
14361{
Matt Carlsonc885e822010-08-02 11:25:57 +000014362 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
Matt Carlson666bc832010-01-20 16:58:03 +000014363 tp->bufmgr_config.mbuf_read_dma_low_water =
14364 DEFAULT_MB_RDMA_LOW_WATER_5705;
14365 tp->bufmgr_config.mbuf_mac_rx_low_water =
14366 DEFAULT_MB_MACRX_LOW_WATER_57765;
14367 tp->bufmgr_config.mbuf_high_water =
14368 DEFAULT_MB_HIGH_WATER_57765;
14369
14370 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14371 DEFAULT_MB_RDMA_LOW_WATER_5705;
14372 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14373 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
14374 tp->bufmgr_config.mbuf_high_water_jumbo =
14375 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
14376 } else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
Michael Chanfdfec1722005-07-25 12:31:48 -070014377 tp->bufmgr_config.mbuf_read_dma_low_water =
14378 DEFAULT_MB_RDMA_LOW_WATER_5705;
14379 tp->bufmgr_config.mbuf_mac_rx_low_water =
14380 DEFAULT_MB_MACRX_LOW_WATER_5705;
14381 tp->bufmgr_config.mbuf_high_water =
14382 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070014383 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
14384 tp->bufmgr_config.mbuf_mac_rx_low_water =
14385 DEFAULT_MB_MACRX_LOW_WATER_5906;
14386 tp->bufmgr_config.mbuf_high_water =
14387 DEFAULT_MB_HIGH_WATER_5906;
14388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014389
Michael Chanfdfec1722005-07-25 12:31:48 -070014390 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14391 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
14392 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14393 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
14394 tp->bufmgr_config.mbuf_high_water_jumbo =
14395 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
14396 } else {
14397 tp->bufmgr_config.mbuf_read_dma_low_water =
14398 DEFAULT_MB_RDMA_LOW_WATER;
14399 tp->bufmgr_config.mbuf_mac_rx_low_water =
14400 DEFAULT_MB_MACRX_LOW_WATER;
14401 tp->bufmgr_config.mbuf_high_water =
14402 DEFAULT_MB_HIGH_WATER;
14403
14404 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14405 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
14406 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14407 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
14408 tp->bufmgr_config.mbuf_high_water_jumbo =
14409 DEFAULT_MB_HIGH_WATER_JUMBO;
14410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014411
14412 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
14413 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
14414}
14415
14416static char * __devinit tg3_phy_string(struct tg3 *tp)
14417{
Matt Carlson79eb6902010-02-17 15:17:03 +000014418 switch (tp->phy_id & TG3_PHY_ID_MASK) {
14419 case TG3_PHY_ID_BCM5400: return "5400";
14420 case TG3_PHY_ID_BCM5401: return "5401";
14421 case TG3_PHY_ID_BCM5411: return "5411";
14422 case TG3_PHY_ID_BCM5701: return "5701";
14423 case TG3_PHY_ID_BCM5703: return "5703";
14424 case TG3_PHY_ID_BCM5704: return "5704";
14425 case TG3_PHY_ID_BCM5705: return "5705";
14426 case TG3_PHY_ID_BCM5750: return "5750";
14427 case TG3_PHY_ID_BCM5752: return "5752";
14428 case TG3_PHY_ID_BCM5714: return "5714";
14429 case TG3_PHY_ID_BCM5780: return "5780";
14430 case TG3_PHY_ID_BCM5755: return "5755";
14431 case TG3_PHY_ID_BCM5787: return "5787";
14432 case TG3_PHY_ID_BCM5784: return "5784";
14433 case TG3_PHY_ID_BCM5756: return "5722/5756";
14434 case TG3_PHY_ID_BCM5906: return "5906";
14435 case TG3_PHY_ID_BCM5761: return "5761";
14436 case TG3_PHY_ID_BCM5718C: return "5718C";
14437 case TG3_PHY_ID_BCM5718S: return "5718S";
14438 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000014439 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson79eb6902010-02-17 15:17:03 +000014440 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070014441 case 0: return "serdes";
14442 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070014443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014444}
14445
Michael Chanf9804dd2005-09-27 12:13:10 -070014446static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
14447{
14448 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
14449 strcpy(str, "PCI Express");
14450 return str;
14451 } else if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
14452 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
14453
14454 strcpy(str, "PCIX:");
14455
14456 if ((clock_ctrl == 7) ||
14457 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
14458 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
14459 strcat(str, "133MHz");
14460 else if (clock_ctrl == 0)
14461 strcat(str, "33MHz");
14462 else if (clock_ctrl == 2)
14463 strcat(str, "50MHz");
14464 else if (clock_ctrl == 4)
14465 strcat(str, "66MHz");
14466 else if (clock_ctrl == 6)
14467 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070014468 } else {
14469 strcpy(str, "PCI:");
14470 if (tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED)
14471 strcat(str, "66MHz");
14472 else
14473 strcat(str, "33MHz");
14474 }
14475 if (tp->tg3_flags & TG3_FLAG_PCI_32BIT)
14476 strcat(str, ":32-bit");
14477 else
14478 strcat(str, ":64-bit");
14479 return str;
14480}
14481
Michael Chan8c2dc7e2005-12-19 16:26:02 -080014482static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014483{
14484 struct pci_dev *peer;
14485 unsigned int func, devnr = tp->pdev->devfn & ~7;
14486
14487 for (func = 0; func < 8; func++) {
14488 peer = pci_get_slot(tp->pdev->bus, devnr | func);
14489 if (peer && peer != tp->pdev)
14490 break;
14491 pci_dev_put(peer);
14492 }
Michael Chan16fe9d72005-12-13 21:09:54 -080014493 /* 5704 can be configured in single-port mode, set peer to
14494 * tp->pdev in that case.
14495 */
14496 if (!peer) {
14497 peer = tp->pdev;
14498 return peer;
14499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014500
14501 /*
14502 * We don't need to keep the refcount elevated; there's no way
14503 * to remove one half of this device without removing the other
14504 */
14505 pci_dev_put(peer);
14506
14507 return peer;
14508}
14509
David S. Miller15f98502005-05-18 22:49:26 -070014510static void __devinit tg3_init_coal(struct tg3 *tp)
14511{
14512 struct ethtool_coalesce *ec = &tp->coal;
14513
14514 memset(ec, 0, sizeof(*ec));
14515 ec->cmd = ETHTOOL_GCOALESCE;
14516 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
14517 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
14518 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
14519 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
14520 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
14521 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
14522 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
14523 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
14524 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
14525
14526 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
14527 HOSTCC_MODE_CLRTICK_TXBD)) {
14528 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
14529 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
14530 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
14531 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
14532 }
Michael Chand244c892005-07-05 14:42:33 -070014533
14534 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
14535 ec->rx_coalesce_usecs_irq = 0;
14536 ec->tx_coalesce_usecs_irq = 0;
14537 ec->stats_block_coalesce_usecs = 0;
14538 }
David S. Miller15f98502005-05-18 22:49:26 -070014539}
14540
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080014541static const struct net_device_ops tg3_netdev_ops = {
14542 .ndo_open = tg3_open,
14543 .ndo_stop = tg3_close,
Stephen Hemminger00829822008-11-20 20:14:53 -080014544 .ndo_start_xmit = tg3_start_xmit,
Eric Dumazet511d2222010-07-07 20:44:24 +000014545 .ndo_get_stats64 = tg3_get_stats64,
Stephen Hemminger00829822008-11-20 20:14:53 -080014546 .ndo_validate_addr = eth_validate_addr,
14547 .ndo_set_multicast_list = tg3_set_rx_mode,
14548 .ndo_set_mac_address = tg3_set_mac_addr,
14549 .ndo_do_ioctl = tg3_ioctl,
14550 .ndo_tx_timeout = tg3_tx_timeout,
14551 .ndo_change_mtu = tg3_change_mtu,
14552#if TG3_VLAN_TAG_USED
14553 .ndo_vlan_rx_register = tg3_vlan_rx_register,
14554#endif
14555#ifdef CONFIG_NET_POLL_CONTROLLER
14556 .ndo_poll_controller = tg3_poll_controller,
14557#endif
14558};
14559
14560static const struct net_device_ops tg3_netdev_ops_dma_bug = {
14561 .ndo_open = tg3_open,
14562 .ndo_stop = tg3_close,
14563 .ndo_start_xmit = tg3_start_xmit_dma_bug,
Eric Dumazet511d2222010-07-07 20:44:24 +000014564 .ndo_get_stats64 = tg3_get_stats64,
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080014565 .ndo_validate_addr = eth_validate_addr,
14566 .ndo_set_multicast_list = tg3_set_rx_mode,
14567 .ndo_set_mac_address = tg3_set_mac_addr,
14568 .ndo_do_ioctl = tg3_ioctl,
14569 .ndo_tx_timeout = tg3_tx_timeout,
14570 .ndo_change_mtu = tg3_change_mtu,
14571#if TG3_VLAN_TAG_USED
14572 .ndo_vlan_rx_register = tg3_vlan_rx_register,
14573#endif
14574#ifdef CONFIG_NET_POLL_CONTROLLER
14575 .ndo_poll_controller = tg3_poll_controller,
14576#endif
14577};
14578
Linus Torvalds1da177e2005-04-16 15:20:36 -070014579static int __devinit tg3_init_one(struct pci_dev *pdev,
14580 const struct pci_device_id *ent)
14581{
Linus Torvalds1da177e2005-04-16 15:20:36 -070014582 struct net_device *dev;
14583 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000014584 int i, err, pm_cap;
14585 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070014586 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080014587 u64 dma_mask, persist_dma_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014588
Joe Perches05dbe002010-02-17 19:44:19 +000014589 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014590
14591 err = pci_enable_device(pdev);
14592 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014593 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014594 return err;
14595 }
14596
Linus Torvalds1da177e2005-04-16 15:20:36 -070014597 err = pci_request_regions(pdev, DRV_MODULE_NAME);
14598 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014599 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014600 goto err_out_disable_pdev;
14601 }
14602
14603 pci_set_master(pdev);
14604
14605 /* Find power-management capability. */
14606 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
14607 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000014608 dev_err(&pdev->dev,
14609 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014610 err = -EIO;
14611 goto err_out_free_res;
14612 }
14613
Matt Carlsonfe5f5782009-09-01 13:09:39 +000014614 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014615 if (!dev) {
Matt Carlson2445e462010-04-05 10:19:21 +000014616 dev_err(&pdev->dev, "Etherdev alloc failed, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014617 err = -ENOMEM;
14618 goto err_out_free_res;
14619 }
14620
Linus Torvalds1da177e2005-04-16 15:20:36 -070014621 SET_NETDEV_DEV(dev, &pdev->dev);
14622
Linus Torvalds1da177e2005-04-16 15:20:36 -070014623#if TG3_VLAN_TAG_USED
14624 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014625#endif
14626
14627 tp = netdev_priv(dev);
14628 tp->pdev = pdev;
14629 tp->dev = dev;
14630 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014631 tp->rx_mode = TG3_DEF_RX_MODE;
14632 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070014633
Linus Torvalds1da177e2005-04-16 15:20:36 -070014634 if (tg3_debug > 0)
14635 tp->msg_enable = tg3_debug;
14636 else
14637 tp->msg_enable = TG3_DEF_MSG_ENABLE;
14638
14639 /* The word/byte swap controls here control register access byte
14640 * swapping. DMA data byte swapping is controlled in the GRC_MODE
14641 * setting below.
14642 */
14643 tp->misc_host_ctrl =
14644 MISC_HOST_CTRL_MASK_PCI_INT |
14645 MISC_HOST_CTRL_WORD_SWAP |
14646 MISC_HOST_CTRL_INDIR_ACCESS |
14647 MISC_HOST_CTRL_PCISTATE_RW;
14648
14649 /* The NONFRM (non-frame) byte/word swap controls take effect
14650 * on descriptor entries, anything which isn't packet data.
14651 *
14652 * The StrongARM chips on the board (one for tx, one for rx)
14653 * are running in big-endian mode.
14654 */
14655 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
14656 GRC_MODE_WSWAP_NONFRM_DATA);
14657#ifdef __BIG_ENDIAN
14658 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
14659#endif
14660 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014661 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000014662 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014663
Matt Carlsond5fe4882008-11-21 17:20:32 -080014664 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010014665 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014666 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014667 err = -ENOMEM;
14668 goto err_out_free_dev;
14669 }
14670
14671 tg3_init_link_config(tp);
14672
Linus Torvalds1da177e2005-04-16 15:20:36 -070014673 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
14674 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014675
Linus Torvalds1da177e2005-04-16 15:20:36 -070014676 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014677 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014678 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014679
14680 err = tg3_get_invariants(tp);
14681 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014682 dev_err(&pdev->dev,
14683 "Problem fetching invariants of chip, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014684 goto err_out_iounmap;
14685 }
14686
Matt Carlson615774f2009-11-13 13:03:39 +000014687 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) &&
Matt Carlson2e9f7a72010-09-15 08:59:56 +000014688 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
Matt Carlsona50d0792010-06-05 17:24:37 +000014689 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5719)
Stephen Hemminger00829822008-11-20 20:14:53 -080014690 dev->netdev_ops = &tg3_netdev_ops;
14691 else
14692 dev->netdev_ops = &tg3_netdev_ops_dma_bug;
14693
14694
Michael Chan4a29cc22006-03-19 13:21:12 -080014695 /* The EPB bridge inside 5714, 5715, and 5780 and any
14696 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080014697 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
14698 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
14699 * do DMA address check in tg3_start_xmit().
14700 */
Michael Chan4a29cc22006-03-19 13:21:12 -080014701 if (tp->tg3_flags2 & TG3_FLG2_IS_5788)
Yang Hongyang284901a2009-04-06 19:01:15 -070014702 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Michael Chan4a29cc22006-03-19 13:21:12 -080014703 else if (tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070014704 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080014705#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070014706 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080014707#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080014708 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070014709 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080014710
14711 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070014712 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080014713 err = pci_set_dma_mask(pdev, dma_mask);
14714 if (!err) {
14715 dev->features |= NETIF_F_HIGHDMA;
14716 err = pci_set_consistent_dma_mask(pdev,
14717 persist_dma_mask);
14718 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014719 dev_err(&pdev->dev, "Unable to obtain 64 bit "
14720 "DMA for consistent allocations\n");
Michael Chan72f2afb2006-03-06 19:28:35 -080014721 goto err_out_iounmap;
14722 }
14723 }
14724 }
Yang Hongyang284901a2009-04-06 19:01:15 -070014725 if (err || dma_mask == DMA_BIT_MASK(32)) {
14726 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080014727 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014728 dev_err(&pdev->dev,
14729 "No usable DMA configuration, aborting\n");
Michael Chan72f2afb2006-03-06 19:28:35 -080014730 goto err_out_iounmap;
14731 }
14732 }
14733
Michael Chanfdfec1722005-07-25 12:31:48 -070014734 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014735
Matt Carlson507399f2009-11-13 13:03:37 +000014736 /* Selectively allow TSO based on operating conditions */
14737 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO) ||
14738 (tp->fw_needed && !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)))
14739 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
14740 else {
14741 tp->tg3_flags2 &= ~(TG3_FLG2_TSO_CAPABLE | TG3_FLG2_TSO_BUG);
14742 tp->fw_needed = NULL;
14743 }
14744
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014745 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
Matt Carlson9e9fd122009-01-19 16:57:45 -080014746 tp->fw_needed = FIRMWARE_TG3;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014747
Michael Chan4e3a7aa2006-03-20 17:47:44 -080014748 /* TSO is on by default on chips that support hardware TSO.
14749 * Firmware TSO on older chips gives lower performance, so it
14750 * is off by default, but can be enabled using ethtool.
14751 */
Matt Carlsone849cdc2009-11-13 13:03:38 +000014752 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO) &&
Eric Dumazet7fe876a2010-07-08 06:14:55 +000014753 (dev->features & NETIF_F_IP_CSUM)) {
Matt Carlsone849cdc2009-11-13 13:03:38 +000014754 dev->features |= NETIF_F_TSO;
Eric Dumazet7fe876a2010-07-08 06:14:55 +000014755 vlan_features_add(dev, NETIF_F_TSO);
14756 }
Matt Carlsone849cdc2009-11-13 13:03:38 +000014757 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_2) ||
14758 (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3)) {
Eric Dumazet7fe876a2010-07-08 06:14:55 +000014759 if (dev->features & NETIF_F_IPV6_CSUM) {
Michael Chanb0026622006-07-03 19:42:14 -070014760 dev->features |= NETIF_F_TSO6;
Eric Dumazet7fe876a2010-07-08 06:14:55 +000014761 vlan_features_add(dev, NETIF_F_TSO6);
14762 }
Matt Carlsone849cdc2009-11-13 13:03:38 +000014763 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
14764 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070014765 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
14766 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014767 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Eric Dumazet7fe876a2010-07-08 06:14:55 +000014768 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070014769 dev->features |= NETIF_F_TSO_ECN;
Eric Dumazet7fe876a2010-07-08 06:14:55 +000014770 vlan_features_add(dev, NETIF_F_TSO_ECN);
14771 }
Michael Chanb0026622006-07-03 19:42:14 -070014772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014773
Linus Torvalds1da177e2005-04-16 15:20:36 -070014774 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
14775 !(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) &&
14776 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
14777 tp->tg3_flags2 |= TG3_FLG2_MAX_RXPEND_64;
14778 tp->rx_pending = 63;
14779 }
14780
Linus Torvalds1da177e2005-04-16 15:20:36 -070014781 err = tg3_get_device_address(tp);
14782 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014783 dev_err(&pdev->dev,
14784 "Could not obtain valid ethernet address, aborting\n");
Matt Carlson026a6c22009-12-03 08:36:24 +000014785 goto err_out_iounmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014786 }
14787
Matt Carlson0d3031d2007-10-10 18:02:43 -070014788 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
Matt Carlson63532392008-11-03 16:49:57 -080014789 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
Al Viro79ea13c2008-01-24 02:06:46 -080014790 if (!tp->aperegs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014791 dev_err(&pdev->dev,
14792 "Cannot map APE registers, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070014793 err = -ENOMEM;
Matt Carlson026a6c22009-12-03 08:36:24 +000014794 goto err_out_iounmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070014795 }
14796
14797 tg3_ape_lock_init(tp);
Matt Carlson7fd76442009-02-25 14:27:20 +000014798
14799 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF)
14800 tg3_read_dash_ver(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070014801 }
14802
Matt Carlsonc88864d2007-11-12 21:07:01 -080014803 /*
14804 * Reset chip in case UNDI or EFI driver did not shutdown
14805 * DMA self test will enable WDMAC and we'll see (spurious)
14806 * pending DMA on the PCI bus at that point.
14807 */
14808 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
14809 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
14810 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
14811 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
14812 }
14813
14814 err = tg3_test_dma(tp);
14815 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014816 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080014817 goto err_out_apeunmap;
14818 }
14819
Matt Carlsonc88864d2007-11-12 21:07:01 -080014820 /* flow control autonegotiation is default behavior */
14821 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
Steve Glendinninge18ce342008-12-16 02:00:00 -080014822 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlsonc88864d2007-11-12 21:07:01 -080014823
Matt Carlson78f90dc2009-11-13 13:03:42 +000014824 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
14825 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
14826 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000014827 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000014828 struct tg3_napi *tnapi = &tp->napi[i];
14829
14830 tnapi->tp = tp;
14831 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
14832
14833 tnapi->int_mbox = intmbx;
14834 if (i < 4)
14835 intmbx += 0x8;
14836 else
14837 intmbx += 0x4;
14838
14839 tnapi->consmbox = rcvmbx;
14840 tnapi->prodmbox = sndmbx;
14841
Matt Carlson66cfd1b2010-09-30 10:34:30 +000014842 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000014843 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000014844 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000014845 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000014846
14847 if (!(tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX))
14848 break;
14849
14850 /*
14851 * If we support MSIX, we'll be using RSS. If we're using
14852 * RSS, the first vector only handles link interrupts and the
14853 * remaining vectors handle rx and tx interrupts. Reuse the
14854 * mailbox values for the next iteration. The values we setup
14855 * above are still useful for the single vectored mode.
14856 */
14857 if (!i)
14858 continue;
14859
14860 rcvmbx += 0x8;
14861
14862 if (sndmbx & 0x4)
14863 sndmbx -= 0x4;
14864 else
14865 sndmbx += 0xc;
14866 }
14867
Matt Carlsonc88864d2007-11-12 21:07:01 -080014868 tg3_init_coal(tp);
14869
Michael Chanc49a1562006-12-17 17:07:29 -080014870 pci_set_drvdata(pdev, dev);
14871
Linus Torvalds1da177e2005-04-16 15:20:36 -070014872 err = register_netdev(dev);
14873 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014874 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070014875 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014876 }
14877
Joe Perches05dbe002010-02-17 19:44:19 +000014878 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
14879 tp->board_part_number,
14880 tp->pci_chip_rev_id,
14881 tg3_bus_string(tp, str),
14882 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014883
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014884 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000014885 struct phy_device *phydev;
14886 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000014887 netdev_info(dev,
14888 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000014889 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014890 } else {
14891 char *ethtype;
14892
14893 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
14894 ethtype = "10/100Base-TX";
14895 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
14896 ethtype = "1000Base-SX";
14897 else
14898 ethtype = "10/100/1000Base-T";
14899
Matt Carlson5129c3a2010-04-05 10:19:23 +000014900 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014901 "(WireSpeed[%d])\n", tg3_phy_string(tp), ethtype,
14902 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0);
14903 }
Matt Carlsondf59c942008-11-03 16:52:56 -080014904
Joe Perches05dbe002010-02-17 19:44:19 +000014905 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
14906 (tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) != 0,
14907 (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014908 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches05dbe002010-02-17 19:44:19 +000014909 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0,
14910 (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) != 0);
14911 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
14912 tp->dma_rwctrl,
14913 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
14914 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014915
14916 return 0;
14917
Matt Carlson0d3031d2007-10-10 18:02:43 -070014918err_out_apeunmap:
14919 if (tp->aperegs) {
14920 iounmap(tp->aperegs);
14921 tp->aperegs = NULL;
14922 }
14923
Linus Torvalds1da177e2005-04-16 15:20:36 -070014924err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070014925 if (tp->regs) {
14926 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070014927 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070014928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014929
14930err_out_free_dev:
14931 free_netdev(dev);
14932
14933err_out_free_res:
14934 pci_release_regions(pdev);
14935
14936err_out_disable_pdev:
14937 pci_disable_device(pdev);
14938 pci_set_drvdata(pdev, NULL);
14939 return err;
14940}
14941
14942static void __devexit tg3_remove_one(struct pci_dev *pdev)
14943{
14944 struct net_device *dev = pci_get_drvdata(pdev);
14945
14946 if (dev) {
14947 struct tg3 *tp = netdev_priv(dev);
14948
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014949 if (tp->fw)
14950 release_firmware(tp->fw);
14951
Michael Chan7faa0062006-02-02 17:29:28 -080014952 flush_scheduled_work();
Matt Carlson158d7ab2008-05-29 01:37:54 -070014953
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014954 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
14955 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070014956 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014957 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070014958
Linus Torvalds1da177e2005-04-16 15:20:36 -070014959 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070014960 if (tp->aperegs) {
14961 iounmap(tp->aperegs);
14962 tp->aperegs = NULL;
14963 }
Michael Chan68929142005-08-09 20:17:14 -070014964 if (tp->regs) {
14965 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070014966 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070014967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014968 free_netdev(dev);
14969 pci_release_regions(pdev);
14970 pci_disable_device(pdev);
14971 pci_set_drvdata(pdev, NULL);
14972 }
14973}
14974
14975static int tg3_suspend(struct pci_dev *pdev, pm_message_t state)
14976{
14977 struct net_device *dev = pci_get_drvdata(pdev);
14978 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070014979 pci_power_t target_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014980 int err;
14981
Michael Chan3e0c95f2007-08-03 20:56:54 -070014982 /* PCI register 4 needs to be saved whether netif_running() or not.
14983 * MSI address and data need to be saved if using MSI and
14984 * netif_running().
14985 */
14986 pci_save_state(pdev);
14987
Linus Torvalds1da177e2005-04-16 15:20:36 -070014988 if (!netif_running(dev))
14989 return 0;
14990
Michael Chan7faa0062006-02-02 17:29:28 -080014991 flush_scheduled_work();
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014992 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014993 tg3_netif_stop(tp);
14994
14995 del_timer_sync(&tp->timer);
14996
David S. Millerf47c11e2005-06-24 20:18:35 -070014997 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014998 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070014999 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015000
15001 netif_device_detach(dev);
15002
David S. Millerf47c11e2005-06-24 20:18:35 -070015003 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070015004 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan6a9eba12005-12-13 21:08:58 -080015005 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
David S. Millerf47c11e2005-06-24 20:18:35 -070015006 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015007
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070015008 target_state = pdev->pm_cap ? pci_target_state(pdev) : PCI_D3hot;
15009
15010 err = tg3_set_power_state(tp, target_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015011 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015012 int err2;
15013
David S. Millerf47c11e2005-06-24 20:18:35 -070015014 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015015
Michael Chan6a9eba12005-12-13 21:08:58 -080015016 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015017 err2 = tg3_restart_hw(tp, 1);
15018 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070015019 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015020
15021 tp->timer.expires = jiffies + tp->timer_offset;
15022 add_timer(&tp->timer);
15023
15024 netif_device_attach(dev);
15025 tg3_netif_start(tp);
15026
Michael Chanb9ec6c12006-07-25 16:37:27 -070015027out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015028 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015029
15030 if (!err2)
15031 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015032 }
15033
15034 return err;
15035}
15036
15037static int tg3_resume(struct pci_dev *pdev)
15038{
15039 struct net_device *dev = pci_get_drvdata(pdev);
15040 struct tg3 *tp = netdev_priv(dev);
15041 int err;
15042
Michael Chan3e0c95f2007-08-03 20:56:54 -070015043 pci_restore_state(tp->pdev);
15044
Linus Torvalds1da177e2005-04-16 15:20:36 -070015045 if (!netif_running(dev))
15046 return 0;
15047
Michael Chanbc1c7562006-03-20 17:48:03 -080015048 err = tg3_set_power_state(tp, PCI_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015049 if (err)
15050 return err;
15051
15052 netif_device_attach(dev);
15053
David S. Millerf47c11e2005-06-24 20:18:35 -070015054 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015055
Michael Chan6a9eba12005-12-13 21:08:58 -080015056 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Michael Chanb9ec6c12006-07-25 16:37:27 -070015057 err = tg3_restart_hw(tp, 1);
15058 if (err)
15059 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015060
15061 tp->timer.expires = jiffies + tp->timer_offset;
15062 add_timer(&tp->timer);
15063
Linus Torvalds1da177e2005-04-16 15:20:36 -070015064 tg3_netif_start(tp);
15065
Michael Chanb9ec6c12006-07-25 16:37:27 -070015066out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015067 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015068
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015069 if (!err)
15070 tg3_phy_start(tp);
15071
Michael Chanb9ec6c12006-07-25 16:37:27 -070015072 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015073}
15074
15075static struct pci_driver tg3_driver = {
15076 .name = DRV_MODULE_NAME,
15077 .id_table = tg3_pci_tbl,
15078 .probe = tg3_init_one,
15079 .remove = __devexit_p(tg3_remove_one),
15080 .suspend = tg3_suspend,
15081 .resume = tg3_resume
15082};
15083
15084static int __init tg3_init(void)
15085{
Jeff Garzik29917622006-08-19 17:48:59 -040015086 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015087}
15088
15089static void __exit tg3_cleanup(void)
15090{
15091 pci_unregister_driver(&tg3_driver);
15092}
15093
15094module_init(tg3_init);
15095module_exit(tg3_cleanup);