blob: 8ffa5afd41410baa1cf462b87af9fe76ca50d11d [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 Carlsonb86fb2c2011-01-25 15:58:57 +00007 * Copyright (C) 2005-2011 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>
Matt Carlson3110f5f52010-12-06 08:28:50 +000035#include <linux/mdio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/mii.h>
Matt Carlson158d7ab2008-05-29 01:37:54 -070037#include <linux/phy.h>
Matt Carlsona9daf362008-05-25 23:49:44 -070038#include <linux/brcmphy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/if_vlan.h>
40#include <linux/ip.h>
41#include <linux/tcp.h>
42#include <linux/workqueue.h>
Michael Chan61487482005-09-05 17:53:19 -070043#include <linux/prefetch.h>
Tobias Klauserf9a5f7d2005-10-29 15:09:26 +020044#include <linux/dma-mapping.h>
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080045#include <linux/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#include <net/checksum.h>
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030048#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50#include <asm/system.h>
Javier Martinez Canillas27fd9de2011-03-26 16:42:31 +000051#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <asm/byteorder.h>
Javier Martinez Canillas27fd9de2011-03-26 16:42:31 +000053#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
David S. Miller49b6e95f2007-03-29 01:38:42 -070055#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <asm/idprom.h>
David S. Miller49b6e95f2007-03-29 01:38:42 -070057#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#endif
59
Matt Carlson63532392008-11-03 16:49:57 -080060#define BAR_0 0
61#define BAR_2 2
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include "tg3.h"
64
65#define DRV_MODULE_NAME "tg3"
Matt Carlson6867c842010-07-11 09:31:44 +000066#define TG3_MAJ_NUM 3
Matt Carlsonb86fb2c2011-01-25 15:58:57 +000067#define TG3_MIN_NUM 117
Matt Carlson6867c842010-07-11 09:31:44 +000068#define DRV_MODULE_VERSION \
69 __stringify(TG3_MAJ_NUM) "." __stringify(TG3_MIN_NUM)
Matt Carlsonb86fb2c2011-01-25 15:58:57 +000070#define DRV_MODULE_RELDATE "January 25, 2011"
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72#define TG3_DEF_MAC_MODE 0
73#define TG3_DEF_RX_MODE 0
74#define TG3_DEF_TX_MODE 0
75#define TG3_DEF_MSG_ENABLE \
76 (NETIF_MSG_DRV | \
77 NETIF_MSG_PROBE | \
78 NETIF_MSG_LINK | \
79 NETIF_MSG_TIMER | \
80 NETIF_MSG_IFDOWN | \
81 NETIF_MSG_IFUP | \
82 NETIF_MSG_RX_ERR | \
83 NETIF_MSG_TX_ERR)
84
85/* length of time before we decide the hardware is borked,
86 * and dev->tx_timeout() should be called to fix the problem
87 */
88#define TG3_TX_TIMEOUT (5 * HZ)
89
90/* hardware minimum and maximum for a single frame's data payload */
91#define TG3_MIN_MTU 60
92#define TG3_MAX_MTU(tp) \
Matt Carlson8f666b02009-08-28 13:58:24 +000093 ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) ? 9000 : 1500)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95/* These numbers seem to be hard coded in the NIC firmware somehow.
96 * You can't change the ring sizes, but you can change where you place
97 * them in the NIC onboard memory.
98 */
Matt Carlson7cb32cf2010-09-30 10:34:36 +000099#define TG3_RX_STD_RING_SIZE(tp) \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000100 ((tp->tg3_flags3 & TG3_FLG3_LRG_PROD_RING_CAP) ? \
101 TG3_RX_STD_MAX_SIZE_5717 : TG3_RX_STD_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102#define TG3_DEF_RX_RING_PENDING 200
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000103#define TG3_RX_JMB_RING_SIZE(tp) \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000104 ((tp->tg3_flags3 & TG3_FLG3_LRG_PROD_RING_CAP) ? \
105 TG3_RX_JMB_MAX_SIZE_5717 : TG3_RX_JMB_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106#define TG3_DEF_RX_JUMBO_RING_PENDING 100
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000107#define TG3_RSS_INDIR_TBL_SIZE 128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109/* Do not place this n-ring entries value into the tp struct itself,
110 * we really want to expose these constants to GCC so that modulo et
111 * al. operations are done with shifts and masks instead of with
112 * hw multiply/modulo instructions. Another solution would be to
113 * replace things like '% foo' with '& (foo - 1)'.
114 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116#define TG3_TX_RING_SIZE 512
117#define TG3_DEF_TX_RING_PENDING (TG3_TX_RING_SIZE - 1)
118
Matt Carlson2c49a442010-09-30 10:34:35 +0000119#define TG3_RX_STD_RING_BYTES(tp) \
120 (sizeof(struct tg3_rx_buffer_desc) * TG3_RX_STD_RING_SIZE(tp))
121#define TG3_RX_JMB_RING_BYTES(tp) \
122 (sizeof(struct tg3_ext_rx_buffer_desc) * TG3_RX_JMB_RING_SIZE(tp))
123#define TG3_RX_RCB_RING_BYTES(tp) \
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000124 (sizeof(struct tg3_rx_buffer_desc) * (tp->rx_ret_ring_mask + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125#define TG3_TX_RING_BYTES (sizeof(struct tg3_tx_buffer_desc) * \
126 TG3_TX_RING_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127#define NEXT_TX(N) (((N) + 1) & (TG3_TX_RING_SIZE - 1))
128
Matt Carlson287be122009-08-28 13:58:46 +0000129#define TG3_DMA_BYTE_ENAB 64
130
131#define TG3_RX_STD_DMA_SZ 1536
132#define TG3_RX_JMB_DMA_SZ 9046
133
134#define TG3_RX_DMA_TO_MAP_SZ(x) ((x) + TG3_DMA_BYTE_ENAB)
135
136#define TG3_RX_STD_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_STD_DMA_SZ)
137#define TG3_RX_JMB_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_JMB_DMA_SZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Matt Carlson2c49a442010-09-30 10:34:35 +0000139#define TG3_RX_STD_BUFF_RING_SIZE(tp) \
140 (sizeof(struct ring_info) * TG3_RX_STD_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000141
Matt Carlson2c49a442010-09-30 10:34:35 +0000142#define TG3_RX_JMB_BUFF_RING_SIZE(tp) \
143 (sizeof(struct ring_info) * TG3_RX_JMB_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000144
Matt Carlsond2757fc2010-04-12 06:58:27 +0000145/* Due to a hardware bug, the 5701 can only DMA to memory addresses
146 * that are at least dword aligned when used in PCIX mode. The driver
147 * works around this bug by double copying the packet. This workaround
148 * is built into the normal double copy length check for efficiency.
149 *
150 * However, the double copy is only necessary on those architectures
151 * where unaligned memory accesses are inefficient. For those architectures
152 * where unaligned memory accesses incur little penalty, we can reintegrate
153 * the 5701 in the normal rx path. Doing so saves a device structure
154 * dereference by hardcoding the double copy threshold in place.
155 */
156#define TG3_RX_COPY_THRESHOLD 256
157#if NET_IP_ALIGN == 0 || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
158 #define TG3_RX_COPY_THRESH(tp) TG3_RX_COPY_THRESHOLD
159#else
160 #define TG3_RX_COPY_THRESH(tp) ((tp)->rx_copy_thresh)
161#endif
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163/* minimum number of free TX descriptors required to wake up TX process */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000164#define TG3_TX_WAKEUP_THRESH(tnapi) ((tnapi)->tx_pending / 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Matt Carlsonad829262008-11-21 17:16:16 -0800166#define TG3_RAW_IP_ALIGN 2
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168/* number of ETHTOOL_GSTATS u64's */
169#define TG3_NUM_STATS (sizeof(struct tg3_ethtool_stats)/sizeof(u64))
170
Michael Chan4cafd3f2005-05-29 14:56:34 -0700171#define TG3_NUM_TEST 6
172
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000173#define TG3_FW_UPDATE_TIMEOUT_SEC 5
174
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800175#define FIRMWARE_TG3 "tigon/tg3.bin"
176#define FIRMWARE_TG3TSO "tigon/tg3_tso.bin"
177#define FIRMWARE_TG3TSO5 "tigon/tg3_tso5.bin"
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179static char version[] __devinitdata =
Joe Perches05dbe002010-02-17 19:44:19 +0000180 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182MODULE_AUTHOR("David S. Miller (davem@redhat.com) and Jeff Garzik (jgarzik@pobox.com)");
183MODULE_DESCRIPTION("Broadcom Tigon3 ethernet driver");
184MODULE_LICENSE("GPL");
185MODULE_VERSION(DRV_MODULE_VERSION);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800186MODULE_FIRMWARE(FIRMWARE_TG3);
187MODULE_FIRMWARE(FIRMWARE_TG3TSO);
188MODULE_FIRMWARE(FIRMWARE_TG3TSO5);
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190static int tg3_debug = -1; /* -1 == use TG3_DEF_MSG_ENABLE as value */
191module_param(tg3_debug, int, 0);
192MODULE_PARM_DESC(tg3_debug, "Tigon3 bitmapped debugging message enable value");
193
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000194static DEFINE_PCI_DEVICE_TABLE(tg3_pci_tbl) = {
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700195 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5700)},
196 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5701)},
197 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702)},
198 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703)},
199 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704)},
200 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702FE)},
201 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705)},
202 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705_2)},
203 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M)},
204 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M_2)},
205 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702X)},
206 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703X)},
207 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S)},
208 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702A3)},
209 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703A3)},
210 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5782)},
211 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5788)},
212 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5789)},
213 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901)},
214 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901_2)},
215 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S_2)},
216 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705F)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700217 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5721)},
Michael Chan126a3362006-09-27 16:03:07 -0700218 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5722)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700219 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700220 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751M)},
221 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751F)},
222 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752)},
223 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752M)},
224 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753)},
225 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753M)},
226 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753F)},
227 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754)},
228 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754M)},
229 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755)},
230 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755M)},
Michael Chan126a3362006-09-27 16:03:07 -0700231 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5756)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700232 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5786)},
233 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787)},
234 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787M)},
Michael Chan676917d2006-12-07 00:20:22 -0800235 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787F)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700236 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714)},
237 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714S)},
238 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715)},
239 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715S)},
240 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780)},
241 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780S)},
242 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5781)},
Michael Chanb5d37722006-09-27 16:06:21 -0700243 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906)},
244 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906M)},
Matt Carlsond30cdd22007-10-07 23:28:35 -0700245 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5784)},
246 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5764)},
Matt Carlson6c7af272007-10-21 16:12:02 -0700247 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5723)},
Matt Carlson9936bcf2007-10-10 18:03:07 -0700248 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761)},
249 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761E)},
Matt Carlsonc88e6682008-11-03 16:49:18 -0800250 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761S)},
251 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761SE)},
Matt Carlson2befdce2009-08-28 12:28:45 +0000252 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_G)},
253 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_F)},
Matt Carlson321d32a2008-11-21 17:22:19 -0800254 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780)},
255 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57760)},
256 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57790)},
Matt Carlson5e7ccf22009-08-25 10:08:42 +0000257 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57788)},
Matt Carlson5001e2f2009-11-13 13:03:51 +0000258 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717)},
259 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5718)},
Matt Carlsonb0f75222010-01-20 16:58:11 +0000260 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57781)},
261 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57785)},
262 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57761)},
263 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57765)},
264 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57791)},
265 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57795)},
Matt Carlson302b5002010-06-05 17:24:38 +0000266 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5719)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700267 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9DXX)},
268 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9MXX)},
269 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1000)},
270 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1001)},
271 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1003)},
272 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC9100)},
273 {PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_TIGON3)},
274 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275};
276
277MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
278
Andreas Mohr50da8592006-08-14 23:54:30 -0700279static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 const char string[ETH_GSTRING_LEN];
281} ethtool_stats_keys[TG3_NUM_STATS] = {
282 { "rx_octets" },
283 { "rx_fragments" },
284 { "rx_ucast_packets" },
285 { "rx_mcast_packets" },
286 { "rx_bcast_packets" },
287 { "rx_fcs_errors" },
288 { "rx_align_errors" },
289 { "rx_xon_pause_rcvd" },
290 { "rx_xoff_pause_rcvd" },
291 { "rx_mac_ctrl_rcvd" },
292 { "rx_xoff_entered" },
293 { "rx_frame_too_long_errors" },
294 { "rx_jabbers" },
295 { "rx_undersize_packets" },
296 { "rx_in_length_errors" },
297 { "rx_out_length_errors" },
298 { "rx_64_or_less_octet_packets" },
299 { "rx_65_to_127_octet_packets" },
300 { "rx_128_to_255_octet_packets" },
301 { "rx_256_to_511_octet_packets" },
302 { "rx_512_to_1023_octet_packets" },
303 { "rx_1024_to_1522_octet_packets" },
304 { "rx_1523_to_2047_octet_packets" },
305 { "rx_2048_to_4095_octet_packets" },
306 { "rx_4096_to_8191_octet_packets" },
307 { "rx_8192_to_9022_octet_packets" },
308
309 { "tx_octets" },
310 { "tx_collisions" },
311
312 { "tx_xon_sent" },
313 { "tx_xoff_sent" },
314 { "tx_flow_control" },
315 { "tx_mac_errors" },
316 { "tx_single_collisions" },
317 { "tx_mult_collisions" },
318 { "tx_deferred" },
319 { "tx_excessive_collisions" },
320 { "tx_late_collisions" },
321 { "tx_collide_2times" },
322 { "tx_collide_3times" },
323 { "tx_collide_4times" },
324 { "tx_collide_5times" },
325 { "tx_collide_6times" },
326 { "tx_collide_7times" },
327 { "tx_collide_8times" },
328 { "tx_collide_9times" },
329 { "tx_collide_10times" },
330 { "tx_collide_11times" },
331 { "tx_collide_12times" },
332 { "tx_collide_13times" },
333 { "tx_collide_14times" },
334 { "tx_collide_15times" },
335 { "tx_ucast_packets" },
336 { "tx_mcast_packets" },
337 { "tx_bcast_packets" },
338 { "tx_carrier_sense_errors" },
339 { "tx_discards" },
340 { "tx_errors" },
341
342 { "dma_writeq_full" },
343 { "dma_write_prioq_full" },
344 { "rxbds_empty" },
345 { "rx_discards" },
346 { "rx_errors" },
347 { "rx_threshold_hit" },
348
349 { "dma_readq_full" },
350 { "dma_read_prioq_full" },
351 { "tx_comp_queue_full" },
352
353 { "ring_set_send_prod_index" },
354 { "ring_status_update" },
355 { "nic_irqs" },
356 { "nic_avoided_irqs" },
357 { "nic_tx_threshold_hit" }
358};
359
Andreas Mohr50da8592006-08-14 23:54:30 -0700360static const struct {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700361 const char string[ETH_GSTRING_LEN];
362} ethtool_test_keys[TG3_NUM_TEST] = {
363 { "nvram test (online) " },
364 { "link test (online) " },
365 { "register test (offline)" },
366 { "memory test (offline)" },
367 { "loopback test (offline)" },
368 { "interrupt test (offline)" },
369};
370
Michael Chanb401e9e2005-12-19 16:27:04 -0800371static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
372{
373 writel(val, tp->regs + off);
374}
375
376static u32 tg3_read32(struct tg3 *tp, u32 off)
377{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000378 return readl(tp->regs + off);
Michael Chanb401e9e2005-12-19 16:27:04 -0800379}
380
Matt Carlson0d3031d2007-10-10 18:02:43 -0700381static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
382{
383 writel(val, tp->aperegs + off);
384}
385
386static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
387{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000388 return readl(tp->aperegs + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700389}
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
392{
Michael Chan68929142005-08-09 20:17:14 -0700393 unsigned long flags;
394
395 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700396 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
397 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
Michael Chan68929142005-08-09 20:17:14 -0700398 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700399}
400
401static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
402{
403 writel(val, tp->regs + off);
404 readl(tp->regs + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
Michael Chan68929142005-08-09 20:17:14 -0700407static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
408{
409 unsigned long flags;
410 u32 val;
411
412 spin_lock_irqsave(&tp->indirect_lock, flags);
413 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
414 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
415 spin_unlock_irqrestore(&tp->indirect_lock, flags);
416 return val;
417}
418
419static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
420{
421 unsigned long flags;
422
423 if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
424 pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
425 TG3_64BIT_REG_LOW, val);
426 return;
427 }
Matt Carlson66711e62009-11-13 13:03:49 +0000428 if (off == TG3_RX_STD_PROD_IDX_REG) {
Michael Chan68929142005-08-09 20:17:14 -0700429 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
430 TG3_64BIT_REG_LOW, val);
431 return;
432 }
433
434 spin_lock_irqsave(&tp->indirect_lock, flags);
435 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
436 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
437 spin_unlock_irqrestore(&tp->indirect_lock, flags);
438
439 /* In indirect mode when disabling interrupts, we also need
440 * to clear the interrupt bit in the GRC local ctrl register.
441 */
442 if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
443 (val == 0x1)) {
444 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
445 tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
446 }
447}
448
449static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
450{
451 unsigned long flags;
452 u32 val;
453
454 spin_lock_irqsave(&tp->indirect_lock, flags);
455 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
456 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
457 spin_unlock_irqrestore(&tp->indirect_lock, flags);
458 return val;
459}
460
Michael Chanb401e9e2005-12-19 16:27:04 -0800461/* usec_wait specifies the wait time in usec when writing to certain registers
462 * where it is unsafe to read back the register without some delay.
463 * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
464 * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
465 */
466static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
Michael Chanb401e9e2005-12-19 16:27:04 -0800468 if ((tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG) ||
469 (tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND))
470 /* Non-posted methods */
471 tp->write32(tp, off, val);
472 else {
473 /* Posted method */
474 tg3_write32(tp, off, val);
475 if (usec_wait)
476 udelay(usec_wait);
477 tp->read32(tp, off);
478 }
479 /* Wait again after the read for the posted method to guarantee that
480 * the wait time is met.
481 */
482 if (usec_wait)
483 udelay(usec_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
485
Michael Chan09ee9292005-08-09 20:17:00 -0700486static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
487{
488 tp->write32_mbox(tp, off, val);
Michael Chan68929142005-08-09 20:17:14 -0700489 if (!(tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER) &&
490 !(tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND))
491 tp->read32_mbox(tp, off);
Michael Chan09ee9292005-08-09 20:17:00 -0700492}
493
Michael Chan20094932005-08-09 20:16:32 -0700494static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
496 void __iomem *mbox = tp->regs + off;
497 writel(val, mbox);
498 if (tp->tg3_flags & TG3_FLAG_TXD_MBOX_HWBUG)
499 writel(val, mbox);
500 if (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)
501 readl(mbox);
502}
503
Michael Chanb5d37722006-09-27 16:06:21 -0700504static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
505{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000506 return readl(tp->regs + off + GRCMBOX_BASE);
Michael Chanb5d37722006-09-27 16:06:21 -0700507}
508
509static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
510{
511 writel(val, tp->regs + off + GRCMBOX_BASE);
512}
513
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000514#define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700515#define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000516#define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
517#define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
518#define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
Michael Chan20094932005-08-09 20:16:32 -0700519
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000520#define tw32(reg, val) tp->write32(tp, reg, val)
521#define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
522#define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
523#define tr32(reg) tp->read32(tp, reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
526{
Michael Chan68929142005-08-09 20:17:14 -0700527 unsigned long flags;
528
Michael Chanb5d37722006-09-27 16:06:21 -0700529 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) &&
530 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
531 return;
532
Michael Chan68929142005-08-09 20:17:14 -0700533 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chanbbadf502006-04-06 21:46:34 -0700534 if (tp->tg3_flags & TG3_FLAG_SRAM_USE_CONFIG) {
535 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
536 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Michael Chanbbadf502006-04-06 21:46:34 -0700538 /* Always leave this as zero. */
539 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
540 } else {
541 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
542 tw32_f(TG3PCI_MEM_WIN_DATA, val);
543
544 /* Always leave this as zero. */
545 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
546 }
Michael Chan68929142005-08-09 20:17:14 -0700547 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548}
549
550static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
551{
Michael Chan68929142005-08-09 20:17:14 -0700552 unsigned long flags;
553
Michael Chanb5d37722006-09-27 16:06:21 -0700554 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) &&
555 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
556 *val = 0;
557 return;
558 }
559
Michael Chan68929142005-08-09 20:17:14 -0700560 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chanbbadf502006-04-06 21:46:34 -0700561 if (tp->tg3_flags & TG3_FLAG_SRAM_USE_CONFIG) {
562 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
563 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Michael Chanbbadf502006-04-06 21:46:34 -0700565 /* Always leave this as zero. */
566 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
567 } else {
568 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
569 *val = tr32(TG3PCI_MEM_WIN_DATA);
570
571 /* Always leave this as zero. */
572 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
573 }
Michael Chan68929142005-08-09 20:17:14 -0700574 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575}
576
Matt Carlson0d3031d2007-10-10 18:02:43 -0700577static void tg3_ape_lock_init(struct tg3 *tp)
578{
579 int i;
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000580 u32 regbase;
581
582 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
583 regbase = TG3_APE_LOCK_GRANT;
584 else
585 regbase = TG3_APE_PER_LOCK_GRANT;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700586
587 /* Make sure the driver hasn't any stale locks. */
588 for (i = 0; i < 8; i++)
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000589 tg3_ape_write32(tp, regbase + 4 * i, APE_LOCK_GRANT_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700590}
591
592static int tg3_ape_lock(struct tg3 *tp, int locknum)
593{
594 int i, off;
595 int ret = 0;
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000596 u32 status, req, gnt;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700597
598 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
599 return 0;
600
601 switch (locknum) {
Matt Carlson33f401a2010-04-05 10:19:27 +0000602 case TG3_APE_LOCK_GRC:
603 case TG3_APE_LOCK_MEM:
604 break;
605 default:
606 return -EINVAL;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700607 }
608
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000609 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
610 req = TG3_APE_LOCK_REQ;
611 gnt = TG3_APE_LOCK_GRANT;
612 } else {
613 req = TG3_APE_PER_LOCK_REQ;
614 gnt = TG3_APE_PER_LOCK_GRANT;
615 }
616
Matt Carlson0d3031d2007-10-10 18:02:43 -0700617 off = 4 * locknum;
618
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000619 tg3_ape_write32(tp, req + off, APE_LOCK_REQ_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700620
621 /* Wait for up to 1 millisecond to acquire lock. */
622 for (i = 0; i < 100; i++) {
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000623 status = tg3_ape_read32(tp, gnt + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700624 if (status == APE_LOCK_GRANT_DRIVER)
625 break;
626 udelay(10);
627 }
628
629 if (status != APE_LOCK_GRANT_DRIVER) {
630 /* Revoke the lock request. */
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000631 tg3_ape_write32(tp, gnt + off,
Matt Carlson0d3031d2007-10-10 18:02:43 -0700632 APE_LOCK_GRANT_DRIVER);
633
634 ret = -EBUSY;
635 }
636
637 return ret;
638}
639
640static void tg3_ape_unlock(struct tg3 *tp, int locknum)
641{
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000642 u32 gnt;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700643
644 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
645 return;
646
647 switch (locknum) {
Matt Carlson33f401a2010-04-05 10:19:27 +0000648 case TG3_APE_LOCK_GRC:
649 case TG3_APE_LOCK_MEM:
650 break;
651 default:
652 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700653 }
654
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000655 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
656 gnt = TG3_APE_LOCK_GRANT;
657 else
658 gnt = TG3_APE_PER_LOCK_GRANT;
659
660 tg3_ape_write32(tp, gnt + 4 * locknum, APE_LOCK_GRANT_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700661}
662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663static void tg3_disable_ints(struct tg3 *tp)
664{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000665 int i;
666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 tw32(TG3PCI_MISC_HOST_CTRL,
668 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000669 for (i = 0; i < tp->irq_max; i++)
670 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671}
672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673static void tg3_enable_ints(struct tg3 *tp)
674{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000675 int i;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000676
Michael Chanbbe832c2005-06-24 20:20:04 -0700677 tp->irq_sync = 0;
678 wmb();
679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 tw32(TG3PCI_MISC_HOST_CTRL,
681 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000682
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000683 tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000684 for (i = 0; i < tp->irq_cnt; i++) {
685 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000686
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000687 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
688 if (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)
689 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
690
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000691 tp->coal_now |= tnapi->coal_now;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000692 }
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000693
694 /* Force an initial interrupt */
695 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) &&
696 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
697 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
698 else
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000699 tw32(HOSTCC_MODE, tp->coal_now);
700
701 tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702}
703
Matt Carlson17375d22009-08-28 14:02:18 +0000704static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
Michael Chan04237dd2005-04-25 15:17:17 -0700705{
Matt Carlson17375d22009-08-28 14:02:18 +0000706 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +0000707 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan04237dd2005-04-25 15:17:17 -0700708 unsigned int work_exists = 0;
709
710 /* check for phy events */
711 if (!(tp->tg3_flags &
712 (TG3_FLAG_USE_LINKCHG_REG |
713 TG3_FLAG_POLL_SERDES))) {
714 if (sblk->status & SD_STATUS_LINK_CHG)
715 work_exists = 1;
716 }
717 /* check for RX/TX work to do */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000718 if (sblk->idx[0].tx_consumer != tnapi->tx_cons ||
Matt Carlson8d9d7cf2009-09-01 13:19:05 +0000719 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -0700720 work_exists = 1;
721
722 return work_exists;
723}
724
Matt Carlson17375d22009-08-28 14:02:18 +0000725/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -0700726 * similar to tg3_enable_ints, but it accurately determines whether there
727 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400728 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 */
Matt Carlson17375d22009-08-28 14:02:18 +0000730static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
Matt Carlson17375d22009-08-28 14:02:18 +0000732 struct tg3 *tp = tnapi->tp;
733
Matt Carlson898a56f2009-08-28 14:02:40 +0000734 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 mmiowb();
736
David S. Millerfac9b832005-05-18 22:46:34 -0700737 /* When doing tagged status, this work check is unnecessary.
738 * The last_tag we write above tells the chip which piece of
739 * work we've completed.
740 */
741 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) &&
Matt Carlson17375d22009-08-28 14:02:18 +0000742 tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -0700743 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +0000744 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745}
746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747static void tg3_switch_clocks(struct tg3 *tp)
748{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000749 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 u32 orig_clock_ctrl;
751
Matt Carlson795d01c2007-10-07 23:28:17 -0700752 if ((tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
753 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -0700754 return;
755
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000756 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 orig_clock_ctrl = clock_ctrl;
759 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
760 CLOCK_CTRL_CLKRUN_OENABLE |
761 0x1f);
762 tp->pci_clock_ctrl = clock_ctrl;
763
764 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
765 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800766 tw32_wait_f(TG3PCI_CLOCK_CTRL,
767 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 }
769 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800770 tw32_wait_f(TG3PCI_CLOCK_CTRL,
771 clock_ctrl |
772 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
773 40);
774 tw32_wait_f(TG3PCI_CLOCK_CTRL,
775 clock_ctrl | (CLOCK_CTRL_ALTCLK),
776 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 }
Michael Chanb401e9e2005-12-19 16:27:04 -0800778 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779}
780
781#define PHY_BUSY_LOOPS 5000
782
783static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
784{
785 u32 frame_val;
786 unsigned int loops;
787 int ret;
788
789 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
790 tw32_f(MAC_MI_MODE,
791 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
792 udelay(80);
793 }
794
795 *val = 0x0;
796
Matt Carlson882e9792009-09-01 13:21:36 +0000797 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 MI_COM_PHY_ADDR_MASK);
799 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
800 MI_COM_REG_ADDR_MASK);
801 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 tw32_f(MAC_MI_COM, frame_val);
804
805 loops = PHY_BUSY_LOOPS;
806 while (loops != 0) {
807 udelay(10);
808 frame_val = tr32(MAC_MI_COM);
809
810 if ((frame_val & MI_COM_BUSY) == 0) {
811 udelay(5);
812 frame_val = tr32(MAC_MI_COM);
813 break;
814 }
815 loops -= 1;
816 }
817
818 ret = -EBUSY;
819 if (loops != 0) {
820 *val = frame_val & MI_COM_DATA_MASK;
821 ret = 0;
822 }
823
824 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
825 tw32_f(MAC_MI_MODE, tp->mi_mode);
826 udelay(80);
827 }
828
829 return ret;
830}
831
832static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
833{
834 u32 frame_val;
835 unsigned int loops;
836 int ret;
837
Matt Carlsonf07e9af2010-08-02 11:26:07 +0000838 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Michael Chanb5d37722006-09-27 16:06:21 -0700839 (reg == MII_TG3_CTRL || reg == MII_TG3_AUX_CTRL))
840 return 0;
841
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
843 tw32_f(MAC_MI_MODE,
844 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
845 udelay(80);
846 }
847
Matt Carlson882e9792009-09-01 13:21:36 +0000848 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 MI_COM_PHY_ADDR_MASK);
850 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
851 MI_COM_REG_ADDR_MASK);
852 frame_val |= (val & MI_COM_DATA_MASK);
853 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 tw32_f(MAC_MI_COM, frame_val);
856
857 loops = PHY_BUSY_LOOPS;
858 while (loops != 0) {
859 udelay(10);
860 frame_val = tr32(MAC_MI_COM);
861 if ((frame_val & MI_COM_BUSY) == 0) {
862 udelay(5);
863 frame_val = tr32(MAC_MI_COM);
864 break;
865 }
866 loops -= 1;
867 }
868
869 ret = -EBUSY;
870 if (loops != 0)
871 ret = 0;
872
873 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
874 tw32_f(MAC_MI_MODE, tp->mi_mode);
875 udelay(80);
876 }
877
878 return ret;
879}
880
Matt Carlson95e28692008-05-25 23:44:14 -0700881static int tg3_bmcr_reset(struct tg3 *tp)
882{
883 u32 phy_control;
884 int limit, err;
885
886 /* OK, reset it, and poll the BMCR_RESET bit until it
887 * clears or we time out.
888 */
889 phy_control = BMCR_RESET;
890 err = tg3_writephy(tp, MII_BMCR, phy_control);
891 if (err != 0)
892 return -EBUSY;
893
894 limit = 5000;
895 while (limit--) {
896 err = tg3_readphy(tp, MII_BMCR, &phy_control);
897 if (err != 0)
898 return -EBUSY;
899
900 if ((phy_control & BMCR_RESET) == 0) {
901 udelay(40);
902 break;
903 }
904 udelay(10);
905 }
Roel Kluind4675b52009-02-12 16:33:27 -0800906 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -0700907 return -EBUSY;
908
909 return 0;
910}
911
Matt Carlson158d7ab2008-05-29 01:37:54 -0700912static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
913{
Francois Romieu3d165432009-01-19 16:56:50 -0800914 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700915 u32 val;
916
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000917 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -0700918
919 if (tg3_readphy(tp, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000920 val = -EIO;
921
922 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -0700923
924 return val;
925}
926
927static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
928{
Francois Romieu3d165432009-01-19 16:56:50 -0800929 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000930 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700931
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000932 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -0700933
934 if (tg3_writephy(tp, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000935 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700936
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000937 spin_unlock_bh(&tp->lock);
938
939 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700940}
941
942static int tg3_mdio_reset(struct mii_bus *bp)
943{
944 return 0;
945}
946
Matt Carlson9c61d6b2008-11-03 16:54:56 -0800947static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -0700948{
949 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800950 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -0700951
Matt Carlson3f0e3ad2009-11-02 14:24:36 +0000952 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800953 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +0000954 case PHY_ID_BCM50610:
955 case PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800956 val = MAC_PHYCFG2_50610_LED_MODES;
957 break;
Matt Carlson6a443a02010-02-17 15:17:04 +0000958 case PHY_ID_BCMAC131:
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800959 val = MAC_PHYCFG2_AC131_LED_MODES;
960 break;
Matt Carlson6a443a02010-02-17 15:17:04 +0000961 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800962 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
963 break;
Matt Carlson6a443a02010-02-17 15:17:04 +0000964 case PHY_ID_RTL8201E:
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800965 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
966 break;
967 default:
Matt Carlsona9daf362008-05-25 23:49:44 -0700968 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800969 }
970
971 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
972 tw32(MAC_PHYCFG2, val);
973
974 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +0000975 val &= ~(MAC_PHYCFG1_RGMII_INT |
976 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
977 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800978 tw32(MAC_PHYCFG1, val);
979
980 return;
981 }
982
Matt Carlson14417062010-02-17 15:16:59 +0000983 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE))
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800984 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
985 MAC_PHYCFG2_FMODE_MASK_MASK |
986 MAC_PHYCFG2_GMODE_MASK_MASK |
987 MAC_PHYCFG2_ACT_MASK_MASK |
988 MAC_PHYCFG2_QUAL_MASK_MASK |
989 MAC_PHYCFG2_INBAND_ENABLE;
990
991 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -0700992
Matt Carlsonbb85fbb2009-08-25 10:09:07 +0000993 val = tr32(MAC_PHYCFG1);
994 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
995 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
Matt Carlson14417062010-02-17 15:16:59 +0000996 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE)) {
Matt Carlsona9daf362008-05-25 23:49:44 -0700997 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
998 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
999 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
1000 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1001 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001002 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1003 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1004 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001005
Matt Carlsona9daf362008-05-25 23:49:44 -07001006 val = tr32(MAC_EXT_RGMII_MODE);
1007 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1008 MAC_RGMII_MODE_RX_QUALITY |
1009 MAC_RGMII_MODE_RX_ACTIVITY |
1010 MAC_RGMII_MODE_RX_ENG_DET |
1011 MAC_RGMII_MODE_TX_ENABLE |
1012 MAC_RGMII_MODE_TX_LOWPWR |
1013 MAC_RGMII_MODE_TX_RESET);
Matt Carlson14417062010-02-17 15:16:59 +00001014 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE)) {
Matt Carlsona9daf362008-05-25 23:49:44 -07001015 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
1016 val |= MAC_RGMII_MODE_RX_INT_B |
1017 MAC_RGMII_MODE_RX_QUALITY |
1018 MAC_RGMII_MODE_RX_ACTIVITY |
1019 MAC_RGMII_MODE_RX_ENG_DET;
1020 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
1021 val |= MAC_RGMII_MODE_TX_ENABLE |
1022 MAC_RGMII_MODE_TX_LOWPWR |
1023 MAC_RGMII_MODE_TX_RESET;
1024 }
1025 tw32(MAC_EXT_RGMII_MODE, val);
1026}
1027
Matt Carlson158d7ab2008-05-29 01:37:54 -07001028static void tg3_mdio_start(struct tg3 *tp)
1029{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001030 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1031 tw32_f(MAC_MI_MODE, tp->mi_mode);
1032 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001033
Matt Carlson9ea48182010-02-17 15:17:01 +00001034 if ((tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED) &&
1035 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1036 tg3_mdio_config_5785(tp);
1037}
1038
1039static int tg3_mdio_init(struct tg3 *tp)
1040{
1041 int i;
1042 u32 reg;
1043 struct phy_device *phydev;
1044
Matt Carlsona50d0792010-06-05 17:24:37 +00001045 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
1046 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) {
Matt Carlson9c7df912010-06-05 17:24:36 +00001047 u32 is_serdes;
Matt Carlson882e9792009-09-01 13:21:36 +00001048
Matt Carlson9c7df912010-06-05 17:24:36 +00001049 tp->phy_addr = PCI_FUNC(tp->pdev->devfn) + 1;
Matt Carlson882e9792009-09-01 13:21:36 +00001050
Matt Carlsond1ec96a2010-01-12 10:11:38 +00001051 if (tp->pci_chip_rev_id != CHIPREV_ID_5717_A0)
1052 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1053 else
1054 is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1055 TG3_CPMU_PHY_STRAP_IS_SERDES;
Matt Carlson882e9792009-09-01 13:21:36 +00001056 if (is_serdes)
1057 tp->phy_addr += 7;
1058 } else
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001059 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001060
Matt Carlson158d7ab2008-05-29 01:37:54 -07001061 tg3_mdio_start(tp);
1062
1063 if (!(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) ||
1064 (tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED))
1065 return 0;
1066
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001067 tp->mdio_bus = mdiobus_alloc();
1068 if (tp->mdio_bus == NULL)
1069 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001070
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001071 tp->mdio_bus->name = "tg3 mdio bus";
1072 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001073 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001074 tp->mdio_bus->priv = tp;
1075 tp->mdio_bus->parent = &tp->pdev->dev;
1076 tp->mdio_bus->read = &tg3_mdio_read;
1077 tp->mdio_bus->write = &tg3_mdio_write;
1078 tp->mdio_bus->reset = &tg3_mdio_reset;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001079 tp->mdio_bus->phy_mask = ~(1 << TG3_PHY_MII_ADDR);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001080 tp->mdio_bus->irq = &tp->mdio_irq[0];
Matt Carlson158d7ab2008-05-29 01:37:54 -07001081
1082 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001083 tp->mdio_bus->irq[i] = PHY_POLL;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001084
1085 /* The bus registration will look for all the PHYs on the mdio bus.
1086 * Unfortunately, it does not ensure the PHY is powered up before
1087 * accessing the PHY ID registers. A chip reset is the
1088 * quickest way to bring the device back to an operational state..
1089 */
1090 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1091 tg3_bmcr_reset(tp);
1092
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001093 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001094 if (i) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001095 dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001096 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001097 return i;
1098 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001099
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001100 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsona9daf362008-05-25 23:49:44 -07001101
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001102 if (!phydev || !phydev->drv) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001103 dev_warn(&tp->pdev->dev, "No PHY devices\n");
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001104 mdiobus_unregister(tp->mdio_bus);
1105 mdiobus_free(tp->mdio_bus);
1106 return -ENODEV;
1107 }
1108
1109 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001110 case PHY_ID_BCM57780:
Matt Carlson321d32a2008-11-21 17:22:19 -08001111 phydev->interface = PHY_INTERFACE_MODE_GMII;
Matt Carlsonc704dc22009-11-02 14:32:12 +00001112 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08001113 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001114 case PHY_ID_BCM50610:
1115 case PHY_ID_BCM50610M:
Matt Carlson32e5a8d2009-11-02 14:31:39 +00001116 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001117 PHY_BRCM_RX_REFCLK_UNUSED |
Matt Carlson52fae082009-11-02 14:32:38 +00001118 PHY_BRCM_DIS_TXCRXC_NOENRGY |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001119 PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson14417062010-02-17 15:16:59 +00001120 if (tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE)
Matt Carlsona9daf362008-05-25 23:49:44 -07001121 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
1122 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
1123 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
1124 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
1125 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001126 /* fallthru */
Matt Carlson6a443a02010-02-17 15:17:04 +00001127 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001128 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001129 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001130 case PHY_ID_RTL8201E:
1131 case PHY_ID_BCMAC131:
Matt Carlsona9daf362008-05-25 23:49:44 -07001132 phydev->interface = PHY_INTERFACE_MODE_MII;
Matt Carlsoncdd4e092009-11-02 14:31:11 +00001133 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001134 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlsona9daf362008-05-25 23:49:44 -07001135 break;
1136 }
1137
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001138 tp->tg3_flags3 |= TG3_FLG3_MDIOBUS_INITED;
1139
1140 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1141 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001142
1143 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001144}
1145
1146static void tg3_mdio_fini(struct tg3 *tp)
1147{
1148 if (tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED) {
1149 tp->tg3_flags3 &= ~TG3_FLG3_MDIOBUS_INITED;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001150 mdiobus_unregister(tp->mdio_bus);
1151 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001152 }
1153}
1154
Matt Carlsonddfc87b2010-10-14 10:37:40 +00001155static int tg3_phy_cl45_write(struct tg3 *tp, u32 devad, u32 addr, u32 val)
1156{
1157 int err;
1158
1159 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1160 if (err)
1161 goto done;
1162
1163 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1164 if (err)
1165 goto done;
1166
1167 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1168 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1169 if (err)
1170 goto done;
1171
1172 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, val);
1173
1174done:
1175 return err;
1176}
1177
1178static int tg3_phy_cl45_read(struct tg3 *tp, u32 devad, u32 addr, u32 *val)
1179{
1180 int err;
1181
1182 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1183 if (err)
1184 goto done;
1185
1186 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1187 if (err)
1188 goto done;
1189
1190 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1191 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1192 if (err)
1193 goto done;
1194
1195 err = tg3_readphy(tp, MII_TG3_MMD_ADDRESS, val);
1196
1197done:
1198 return err;
1199}
1200
Matt Carlson95e28692008-05-25 23:44:14 -07001201/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001202static inline void tg3_generate_fw_event(struct tg3 *tp)
1203{
1204 u32 val;
1205
1206 val = tr32(GRC_RX_CPU_EVENT);
1207 val |= GRC_RX_CPU_DRIVER_EVENT;
1208 tw32_f(GRC_RX_CPU_EVENT, val);
1209
1210 tp->last_event_jiffies = jiffies;
1211}
1212
1213#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1214
1215/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001216static void tg3_wait_for_event_ack(struct tg3 *tp)
1217{
1218 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001219 unsigned int delay_cnt;
1220 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001221
Matt Carlson4ba526c2008-08-15 14:10:04 -07001222 /* If enough time has passed, no wait is necessary. */
1223 time_remain = (long)(tp->last_event_jiffies + 1 +
1224 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1225 (long)jiffies;
1226 if (time_remain < 0)
1227 return;
1228
1229 /* Check if we can shorten the wait time. */
1230 delay_cnt = jiffies_to_usecs(time_remain);
1231 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1232 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1233 delay_cnt = (delay_cnt >> 3) + 1;
1234
1235 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001236 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1237 break;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001238 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001239 }
1240}
1241
1242/* tp->lock is held. */
1243static void tg3_ump_link_report(struct tg3 *tp)
1244{
1245 u32 reg;
1246 u32 val;
1247
1248 if (!(tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
1249 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
1250 return;
1251
1252 tg3_wait_for_event_ack(tp);
1253
1254 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1255
1256 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1257
1258 val = 0;
1259 if (!tg3_readphy(tp, MII_BMCR, &reg))
1260 val = reg << 16;
1261 if (!tg3_readphy(tp, MII_BMSR, &reg))
1262 val |= (reg & 0xffff);
1263 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX, val);
1264
1265 val = 0;
1266 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1267 val = reg << 16;
1268 if (!tg3_readphy(tp, MII_LPA, &reg))
1269 val |= (reg & 0xffff);
1270 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 4, val);
1271
1272 val = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001273 if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
Matt Carlson95e28692008-05-25 23:44:14 -07001274 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1275 val = reg << 16;
1276 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1277 val |= (reg & 0xffff);
1278 }
1279 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 8, val);
1280
1281 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1282 val = reg << 16;
1283 else
1284 val = 0;
1285 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 12, val);
1286
Matt Carlson4ba526c2008-08-15 14:10:04 -07001287 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001288}
1289
1290static void tg3_link_report(struct tg3 *tp)
1291{
1292 if (!netif_carrier_ok(tp->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001293 netif_info(tp, link, tp->dev, "Link is down\n");
Matt Carlson95e28692008-05-25 23:44:14 -07001294 tg3_ump_link_report(tp);
1295 } else if (netif_msg_link(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001296 netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
1297 (tp->link_config.active_speed == SPEED_1000 ?
1298 1000 :
1299 (tp->link_config.active_speed == SPEED_100 ?
1300 100 : 10)),
1301 (tp->link_config.active_duplex == DUPLEX_FULL ?
1302 "full" : "half"));
Matt Carlson95e28692008-05-25 23:44:14 -07001303
Joe Perches05dbe002010-02-17 19:44:19 +00001304 netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
1305 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
1306 "on" : "off",
1307 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
1308 "on" : "off");
Matt Carlson95e28692008-05-25 23:44:14 -07001309 tg3_ump_link_report(tp);
1310 }
1311}
1312
1313static u16 tg3_advert_flowctrl_1000T(u8 flow_ctrl)
1314{
1315 u16 miireg;
1316
Steve Glendinninge18ce342008-12-16 02:00:00 -08001317 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001318 miireg = ADVERTISE_PAUSE_CAP;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001319 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001320 miireg = ADVERTISE_PAUSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001321 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001322 miireg = ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1323 else
1324 miireg = 0;
1325
1326 return miireg;
1327}
1328
1329static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1330{
1331 u16 miireg;
1332
Steve Glendinninge18ce342008-12-16 02:00:00 -08001333 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001334 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001335 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001336 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001337 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001338 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1339 else
1340 miireg = 0;
1341
1342 return miireg;
1343}
1344
Matt Carlson95e28692008-05-25 23:44:14 -07001345static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1346{
1347 u8 cap = 0;
1348
1349 if (lcladv & ADVERTISE_1000XPAUSE) {
1350 if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1351 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001352 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001353 else if (rmtadv & LPA_1000XPAUSE_ASYM)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001354 cap = FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001355 } else {
1356 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001357 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001358 }
1359 } else if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1360 if ((rmtadv & LPA_1000XPAUSE) && (rmtadv & LPA_1000XPAUSE_ASYM))
Steve Glendinninge18ce342008-12-16 02:00:00 -08001361 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001362 }
1363
1364 return cap;
1365}
1366
Matt Carlsonf51f3562008-05-25 23:45:08 -07001367static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001368{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001369 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001370 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001371 u32 old_rx_mode = tp->rx_mode;
1372 u32 old_tx_mode = tp->tx_mode;
1373
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001374 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001375 autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001376 else
1377 autoneg = tp->link_config.autoneg;
1378
1379 if (autoneg == AUTONEG_ENABLE &&
Matt Carlson95e28692008-05-25 23:44:14 -07001380 (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001381 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001382 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001383 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001384 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001385 } else
1386 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001387
Matt Carlsonf51f3562008-05-25 23:45:08 -07001388 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001389
Steve Glendinninge18ce342008-12-16 02:00:00 -08001390 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001391 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1392 else
1393 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1394
Matt Carlsonf51f3562008-05-25 23:45:08 -07001395 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001396 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001397
Steve Glendinninge18ce342008-12-16 02:00:00 -08001398 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001399 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1400 else
1401 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1402
Matt Carlsonf51f3562008-05-25 23:45:08 -07001403 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001404 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001405}
1406
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001407static void tg3_adjust_link(struct net_device *dev)
1408{
1409 u8 oldflowctrl, linkmesg = 0;
1410 u32 mac_mode, lcl_adv, rmt_adv;
1411 struct tg3 *tp = netdev_priv(dev);
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001412 struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001413
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001414 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001415
1416 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
1417 MAC_MODE_HALF_DUPLEX);
1418
1419 oldflowctrl = tp->link_config.active_flowctrl;
1420
1421 if (phydev->link) {
1422 lcl_adv = 0;
1423 rmt_adv = 0;
1424
1425 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
1426 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001427 else if (phydev->speed == SPEED_1000 ||
1428 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001429 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001430 else
1431 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001432
1433 if (phydev->duplex == DUPLEX_HALF)
1434 mac_mode |= MAC_MODE_HALF_DUPLEX;
1435 else {
1436 lcl_adv = tg3_advert_flowctrl_1000T(
1437 tp->link_config.flowctrl);
1438
1439 if (phydev->pause)
1440 rmt_adv = LPA_PAUSE_CAP;
1441 if (phydev->asym_pause)
1442 rmt_adv |= LPA_PAUSE_ASYM;
1443 }
1444
1445 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
1446 } else
1447 mac_mode |= MAC_MODE_PORT_MODE_GMII;
1448
1449 if (mac_mode != tp->mac_mode) {
1450 tp->mac_mode = mac_mode;
1451 tw32_f(MAC_MODE, tp->mac_mode);
1452 udelay(40);
1453 }
1454
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001455 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
1456 if (phydev->speed == SPEED_10)
1457 tw32(MAC_MI_STAT,
1458 MAC_MI_STAT_10MBPS_MODE |
1459 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1460 else
1461 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1462 }
1463
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001464 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
1465 tw32(MAC_TX_LENGTHS,
1466 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1467 (6 << TX_LENGTHS_IPG_SHIFT) |
1468 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
1469 else
1470 tw32(MAC_TX_LENGTHS,
1471 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1472 (6 << TX_LENGTHS_IPG_SHIFT) |
1473 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
1474
1475 if ((phydev->link && tp->link_config.active_speed == SPEED_INVALID) ||
1476 (!phydev->link && tp->link_config.active_speed != SPEED_INVALID) ||
1477 phydev->speed != tp->link_config.active_speed ||
1478 phydev->duplex != tp->link_config.active_duplex ||
1479 oldflowctrl != tp->link_config.active_flowctrl)
Matt Carlsonc6cdf432010-04-05 10:19:26 +00001480 linkmesg = 1;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001481
1482 tp->link_config.active_speed = phydev->speed;
1483 tp->link_config.active_duplex = phydev->duplex;
1484
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001485 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001486
1487 if (linkmesg)
1488 tg3_link_report(tp);
1489}
1490
1491static int tg3_phy_init(struct tg3 *tp)
1492{
1493 struct phy_device *phydev;
1494
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001495 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001496 return 0;
1497
1498 /* Bring the PHY back to a known state. */
1499 tg3_bmcr_reset(tp);
1500
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001501 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001502
1503 /* Attach the MAC to the PHY. */
Kay Sieversfb28ad32008-11-10 13:55:14 -08001504 phydev = phy_connect(tp->dev, dev_name(&phydev->dev), tg3_adjust_link,
Matt Carlsona9daf362008-05-25 23:49:44 -07001505 phydev->dev_flags, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001506 if (IS_ERR(phydev)) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001507 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001508 return PTR_ERR(phydev);
1509 }
1510
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001511 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001512 switch (phydev->interface) {
1513 case PHY_INTERFACE_MODE_GMII:
1514 case PHY_INTERFACE_MODE_RGMII:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001515 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Matt Carlson321d32a2008-11-21 17:22:19 -08001516 phydev->supported &= (PHY_GBIT_FEATURES |
1517 SUPPORTED_Pause |
1518 SUPPORTED_Asym_Pause);
1519 break;
1520 }
1521 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001522 case PHY_INTERFACE_MODE_MII:
1523 phydev->supported &= (PHY_BASIC_FEATURES |
1524 SUPPORTED_Pause |
1525 SUPPORTED_Asym_Pause);
1526 break;
1527 default:
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001528 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001529 return -EINVAL;
1530 }
1531
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001532 tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001533
1534 phydev->advertising = phydev->supported;
1535
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001536 return 0;
1537}
1538
1539static void tg3_phy_start(struct tg3 *tp)
1540{
1541 struct phy_device *phydev;
1542
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001543 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001544 return;
1545
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001546 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001547
Matt Carlson80096062010-08-02 11:26:06 +00001548 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
1549 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001550 phydev->speed = tp->link_config.orig_speed;
1551 phydev->duplex = tp->link_config.orig_duplex;
1552 phydev->autoneg = tp->link_config.orig_autoneg;
1553 phydev->advertising = tp->link_config.orig_advertising;
1554 }
1555
1556 phy_start(phydev);
1557
1558 phy_start_aneg(phydev);
1559}
1560
1561static void tg3_phy_stop(struct tg3 *tp)
1562{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001563 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001564 return;
1565
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001566 phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001567}
1568
1569static void tg3_phy_fini(struct tg3 *tp)
1570{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001571 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001572 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001573 tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001574 }
1575}
1576
Matt Carlson52b02d02010-10-14 10:37:41 +00001577static int tg3_phydsp_read(struct tg3 *tp, u32 reg, u32 *val)
1578{
1579 int err;
1580
1581 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1582 if (!err)
1583 err = tg3_readphy(tp, MII_TG3_DSP_RW_PORT, val);
1584
1585 return err;
1586}
1587
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00001588static int tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001589{
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00001590 int err;
1591
1592 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1593 if (!err)
1594 err = tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
1595
1596 return err;
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001597}
1598
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001599static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
1600{
1601 u32 phytest;
1602
1603 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
1604 u32 phy;
1605
1606 tg3_writephy(tp, MII_TG3_FET_TEST,
1607 phytest | MII_TG3_FET_SHADOW_EN);
1608 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
1609 if (enable)
1610 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
1611 else
1612 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
1613 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
1614 }
1615 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
1616 }
1617}
1618
Matt Carlson6833c042008-11-21 17:18:59 -08001619static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
1620{
1621 u32 reg;
1622
Matt Carlsonecf14102010-01-20 16:58:05 +00001623 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
Matt Carlsona50d0792010-06-05 17:24:37 +00001624 ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
1625 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001626 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08001627 return;
1628
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001629 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001630 tg3_phy_fet_toggle_apd(tp, enable);
1631 return;
1632 }
1633
Matt Carlson6833c042008-11-21 17:18:59 -08001634 reg = MII_TG3_MISC_SHDW_WREN |
1635 MII_TG3_MISC_SHDW_SCR5_SEL |
1636 MII_TG3_MISC_SHDW_SCR5_LPED |
1637 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
1638 MII_TG3_MISC_SHDW_SCR5_SDTL |
1639 MII_TG3_MISC_SHDW_SCR5_C125OE;
1640 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 || !enable)
1641 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
1642
1643 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1644
1645
1646 reg = MII_TG3_MISC_SHDW_WREN |
1647 MII_TG3_MISC_SHDW_APD_SEL |
1648 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
1649 if (enable)
1650 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
1651
1652 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1653}
1654
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001655static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
1656{
1657 u32 phy;
1658
1659 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001660 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001661 return;
1662
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001663 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001664 u32 ephy;
1665
Matt Carlson535ef6e2009-08-25 10:09:36 +00001666 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
1667 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
1668
1669 tg3_writephy(tp, MII_TG3_FET_TEST,
1670 ephy | MII_TG3_FET_SHADOW_EN);
1671 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001672 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00001673 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001674 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00001675 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
1676 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001677 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00001678 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001679 }
1680 } else {
1681 phy = MII_TG3_AUXCTL_MISC_RDSEL_MISC |
1682 MII_TG3_AUXCTL_SHDWSEL_MISC;
1683 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, phy) &&
1684 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &phy)) {
1685 if (enable)
1686 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1687 else
1688 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1689 phy |= MII_TG3_AUXCTL_MISC_WREN;
1690 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1691 }
1692 }
1693}
1694
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695static void tg3_phy_set_wirespeed(struct tg3 *tp)
1696{
1697 u32 val;
1698
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001699 if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 return;
1701
1702 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x7007) &&
1703 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &val))
1704 tg3_writephy(tp, MII_TG3_AUX_CTRL,
1705 (val | (1 << 15) | (1 << 4)));
1706}
1707
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001708static void tg3_phy_apply_otp(struct tg3 *tp)
1709{
1710 u32 otp, phy;
1711
1712 if (!tp->phy_otp)
1713 return;
1714
1715 otp = tp->phy_otp;
1716
1717 /* Enable SM_DSP clock and tx 6dB coding. */
1718 phy = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1719 MII_TG3_AUXCTL_ACTL_SMDSP_ENA |
1720 MII_TG3_AUXCTL_ACTL_TX_6DB;
1721 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1722
1723 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
1724 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
1725 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
1726
1727 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
1728 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
1729 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
1730
1731 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
1732 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
1733 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
1734
1735 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
1736 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
1737
1738 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
1739 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
1740
1741 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
1742 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
1743 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
1744
1745 /* Turn off SM_DSP clock. */
1746 phy = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1747 MII_TG3_AUXCTL_ACTL_TX_6DB;
1748 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1749}
1750
Matt Carlson52b02d02010-10-14 10:37:41 +00001751static void tg3_phy_eee_adjust(struct tg3 *tp, u32 current_link_up)
1752{
1753 u32 val;
1754
1755 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
1756 return;
1757
1758 tp->setlpicnt = 0;
1759
1760 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
1761 current_link_up == 1 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +00001762 tp->link_config.active_duplex == DUPLEX_FULL &&
1763 (tp->link_config.active_speed == SPEED_100 ||
1764 tp->link_config.active_speed == SPEED_1000)) {
Matt Carlson52b02d02010-10-14 10:37:41 +00001765 u32 eeectl;
1766
1767 if (tp->link_config.active_speed == SPEED_1000)
1768 eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
1769 else
1770 eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
1771
1772 tw32(TG3_CPMU_EEE_CTRL, eeectl);
1773
Matt Carlson3110f5f52010-12-06 08:28:50 +00001774 tg3_phy_cl45_read(tp, MDIO_MMD_AN,
1775 TG3_CL45_D7_EEERES_STAT, &val);
Matt Carlson52b02d02010-10-14 10:37:41 +00001776
Matt Carlson21a00ab2011-01-25 15:58:55 +00001777 switch (val) {
1778 case TG3_CL45_D7_EEERES_STAT_LP_1000T:
1779 switch (GET_ASIC_REV(tp->pci_chip_rev_id)) {
1780 case ASIC_REV_5717:
1781 case ASIC_REV_5719:
1782 case ASIC_REV_57765:
1783 /* Enable SM_DSP clock and tx 6dB coding. */
1784 val = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1785 MII_TG3_AUXCTL_ACTL_SMDSP_ENA |
1786 MII_TG3_AUXCTL_ACTL_TX_6DB;
1787 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
1788
1789 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0000);
1790
1791 /* Turn off SM_DSP clock. */
1792 val = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1793 MII_TG3_AUXCTL_ACTL_TX_6DB;
1794 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
1795 }
1796 /* Fallthrough */
1797 case TG3_CL45_D7_EEERES_STAT_LP_100TX:
Matt Carlson52b02d02010-10-14 10:37:41 +00001798 tp->setlpicnt = 2;
Matt Carlson21a00ab2011-01-25 15:58:55 +00001799 }
Matt Carlson52b02d02010-10-14 10:37:41 +00001800 }
1801
1802 if (!tp->setlpicnt) {
1803 val = tr32(TG3_CPMU_EEE_MODE);
1804 tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
1805 }
1806}
1807
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808static int tg3_wait_macro_done(struct tg3 *tp)
1809{
1810 int limit = 100;
1811
1812 while (limit--) {
1813 u32 tmp32;
1814
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001815 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 if ((tmp32 & 0x1000) == 0)
1817 break;
1818 }
1819 }
Roel Kluind4675b52009-02-12 16:33:27 -08001820 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 return -EBUSY;
1822
1823 return 0;
1824}
1825
1826static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
1827{
1828 static const u32 test_pat[4][6] = {
1829 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
1830 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
1831 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
1832 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
1833 };
1834 int chan;
1835
1836 for (chan = 0; chan < 4; chan++) {
1837 int i;
1838
1839 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1840 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001841 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
1843 for (i = 0; i < 6; i++)
1844 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
1845 test_pat[chan][i]);
1846
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001847 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 if (tg3_wait_macro_done(tp)) {
1849 *resetp = 1;
1850 return -EBUSY;
1851 }
1852
1853 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1854 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001855 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 if (tg3_wait_macro_done(tp)) {
1857 *resetp = 1;
1858 return -EBUSY;
1859 }
1860
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001861 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 if (tg3_wait_macro_done(tp)) {
1863 *resetp = 1;
1864 return -EBUSY;
1865 }
1866
1867 for (i = 0; i < 6; i += 2) {
1868 u32 low, high;
1869
1870 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
1871 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
1872 tg3_wait_macro_done(tp)) {
1873 *resetp = 1;
1874 return -EBUSY;
1875 }
1876 low &= 0x7fff;
1877 high &= 0x000f;
1878 if (low != test_pat[chan][i] ||
1879 high != test_pat[chan][i+1]) {
1880 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
1881 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
1882 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
1883
1884 return -EBUSY;
1885 }
1886 }
1887 }
1888
1889 return 0;
1890}
1891
1892static int tg3_phy_reset_chanpat(struct tg3 *tp)
1893{
1894 int chan;
1895
1896 for (chan = 0; chan < 4; chan++) {
1897 int i;
1898
1899 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1900 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001901 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 for (i = 0; i < 6; i++)
1903 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001904 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 if (tg3_wait_macro_done(tp))
1906 return -EBUSY;
1907 }
1908
1909 return 0;
1910}
1911
1912static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
1913{
1914 u32 reg32, phy9_orig;
1915 int retries, do_phy_reset, err;
1916
1917 retries = 10;
1918 do_phy_reset = 1;
1919 do {
1920 if (do_phy_reset) {
1921 err = tg3_bmcr_reset(tp);
1922 if (err)
1923 return err;
1924 do_phy_reset = 0;
1925 }
1926
1927 /* Disable transmitter and interrupt. */
1928 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
1929 continue;
1930
1931 reg32 |= 0x3000;
1932 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
1933
1934 /* Set full-duplex, 1000 mbps. */
1935 tg3_writephy(tp, MII_BMCR,
1936 BMCR_FULLDPLX | TG3_BMCR_SPEED1000);
1937
1938 /* Set to master mode. */
1939 if (tg3_readphy(tp, MII_TG3_CTRL, &phy9_orig))
1940 continue;
1941
1942 tg3_writephy(tp, MII_TG3_CTRL,
1943 (MII_TG3_CTRL_AS_MASTER |
1944 MII_TG3_CTRL_ENABLE_AS_MASTER));
1945
1946 /* Enable SM_DSP_CLOCK and 6dB. */
1947 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
1948
1949 /* Block the PHY control access. */
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00001950 tg3_phydsp_write(tp, 0x8005, 0x0800);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
1952 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
1953 if (!err)
1954 break;
1955 } while (--retries);
1956
1957 err = tg3_phy_reset_chanpat(tp);
1958 if (err)
1959 return err;
1960
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00001961 tg3_phydsp_write(tp, 0x8005, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
1963 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001964 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
1966 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
1967 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
1968 /* Set Extended packet length bit for jumbo frames */
1969 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4400);
Matt Carlson859a5882010-04-05 10:19:28 +00001970 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
1972 }
1973
1974 tg3_writephy(tp, MII_TG3_CTRL, phy9_orig);
1975
1976 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
1977 reg32 &= ~0x3000;
1978 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
1979 } else if (!err)
1980 err = -EBUSY;
1981
1982 return err;
1983}
1984
1985/* This will reset the tigon3 PHY if there is no valid
1986 * link unless the FORCE argument is non-zero.
1987 */
1988static int tg3_phy_reset(struct tg3 *tp)
1989{
Matt Carlsonf833c4c2010-09-15 09:00:01 +00001990 u32 val, cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 int err;
1992
Michael Chan60189dd2006-12-17 17:08:07 -08001993 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08001994 val = tr32(GRC_MISC_CFG);
1995 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
1996 udelay(40);
1997 }
Matt Carlsonf833c4c2010-09-15 09:00:01 +00001998 err = tg3_readphy(tp, MII_BMSR, &val);
1999 err |= tg3_readphy(tp, MII_BMSR, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 if (err != 0)
2001 return -EBUSY;
2002
Michael Chanc8e1e822006-04-29 18:55:17 -07002003 if (netif_running(tp->dev) && netif_carrier_ok(tp->dev)) {
2004 netif_carrier_off(tp->dev);
2005 tg3_link_report(tp);
2006 }
2007
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2009 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2010 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
2011 err = tg3_phy_reset_5703_4_5(tp);
2012 if (err)
2013 return err;
2014 goto out;
2015 }
2016
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002017 cpmuctrl = 0;
2018 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
2019 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
2020 cpmuctrl = tr32(TG3_CPMU_CTRL);
2021 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
2022 tw32(TG3_CPMU_CTRL,
2023 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
2024 }
2025
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 err = tg3_bmcr_reset(tp);
2027 if (err)
2028 return err;
2029
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002030 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002031 val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
2032 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002033
2034 tw32(TG3_CPMU_CTRL, cpmuctrl);
2035 }
2036
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002037 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2038 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002039 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2040 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
2041 CPMU_LSPD_1000MB_MACCLK_12_5) {
2042 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2043 udelay(40);
2044 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2045 }
2046 }
2047
Matt Carlsona50d0792010-06-05 17:24:37 +00002048 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2049 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002050 (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
Matt Carlsonecf14102010-01-20 16:58:05 +00002051 return 0;
2052
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002053 tg3_phy_apply_otp(tp);
2054
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002055 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -08002056 tg3_phy_toggle_apd(tp, true);
2057 else
2058 tg3_phy_toggle_apd(tp, false);
2059
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060out:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002061 if (tp->phy_flags & TG3_PHYFLG_ADC_BUG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002063 tg3_phydsp_write(tp, 0x201f, 0x2aaa);
2064 tg3_phydsp_write(tp, 0x000a, 0x0323);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
2066 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002067 if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002068 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2069 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002071 if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002073 tg3_phydsp_write(tp, 0x000a, 0x310b);
2074 tg3_phydsp_write(tp, 0x201f, 0x9506);
2075 tg3_phydsp_write(tp, 0x401f, 0x14e2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002077 } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
Michael Chanc424cb22006-04-29 18:56:34 -07002078 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
2079 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002080 if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
Michael Chanc1d2a192007-01-08 19:57:20 -08002081 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2082 tg3_writephy(tp, MII_TG3_TEST1,
2083 MII_TG3_TEST1_TRIM_EN | 0x4);
2084 } else
2085 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
Michael Chanc424cb22006-04-29 18:56:34 -07002086 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
2087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 /* Set Extended packet length bit (bit 14) on all chips that */
2089 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002090 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 /* Cannot do read-modify-write on 5401 */
2092 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4c20);
Matt Carlson8f666b02009-08-28 13:58:24 +00002093 } else if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 /* Set bit 14 with read-modify-write to preserve other bits */
2095 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0007) &&
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002096 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &val))
2097 tg3_writephy(tp, MII_TG3_AUX_CTRL, val | 0x4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 }
2099
2100 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2101 * jumbo frames transmission.
2102 */
Matt Carlson8f666b02009-08-28 13:58:24 +00002103 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002104 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002105 tg3_writephy(tp, MII_TG3_EXT_CTRL,
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002106 val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 }
2108
Michael Chan715116a2006-09-27 16:09:25 -07002109 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002110 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002111 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002112 }
2113
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002114 tg3_phy_toggle_automdix(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 tg3_phy_set_wirespeed(tp);
2116 return 0;
2117}
2118
2119static void tg3_frob_aux_power(struct tg3 *tp)
2120{
Matt Carlson683644b2011-03-09 16:58:23 +00002121 bool need_vaux = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
Matt Carlson334355a2010-01-20 16:58:10 +00002123 /* The GPIOs do something completely different on 57765. */
2124 if ((tp->tg3_flags2 & TG3_FLG2_IS_NIC) == 0 ||
Matt Carlsona50d0792010-06-05 17:24:37 +00002125 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlson334355a2010-01-20 16:58:10 +00002126 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 return;
2128
Matt Carlson683644b2011-03-09 16:58:23 +00002129 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2130 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
2131 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) &&
2132 tp->pdev_peer != tp->pdev) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002133 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002135 dev_peer = pci_get_drvdata(tp->pdev_peer);
Matt Carlson683644b2011-03-09 16:58:23 +00002136
Michael Chanbc1c7562006-03-20 17:48:03 -08002137 /* remove_one() may have been run on the peer. */
Matt Carlson683644b2011-03-09 16:58:23 +00002138 if (dev_peer) {
2139 struct tg3 *tp_peer = netdev_priv(dev_peer);
2140
2141 if (tp_peer->tg3_flags & TG3_FLAG_INIT_COMPLETE)
2142 return;
2143
2144 if ((tp_peer->tg3_flags & TG3_FLAG_WOL_ENABLE) ||
2145 (tp_peer->tg3_flags & TG3_FLAG_ENABLE_ASF))
2146 need_vaux = true;
2147 }
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
Matt Carlson683644b2011-03-09 16:58:23 +00002150 if ((tp->tg3_flags & TG3_FLAG_WOL_ENABLE) ||
2151 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
2152 need_vaux = true;
2153
2154 if (need_vaux) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2156 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Michael Chanb401e9e2005-12-19 16:27:04 -08002157 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2158 (GRC_LCLCTRL_GPIO_OE0 |
2159 GRC_LCLCTRL_GPIO_OE1 |
2160 GRC_LCLCTRL_GPIO_OE2 |
2161 GRC_LCLCTRL_GPIO_OUTPUT0 |
2162 GRC_LCLCTRL_GPIO_OUTPUT1),
2163 100);
Matt Carlson8d519ab2009-04-20 06:58:01 +00002164 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2165 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -07002166 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2167 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2168 GRC_LCLCTRL_GPIO_OE1 |
2169 GRC_LCLCTRL_GPIO_OE2 |
2170 GRC_LCLCTRL_GPIO_OUTPUT0 |
2171 GRC_LCLCTRL_GPIO_OUTPUT1 |
2172 tp->grc_local_ctrl;
2173 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2174
2175 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2176 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2177
2178 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2179 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 } else {
2181 u32 no_gpio2;
Michael Chandc56b7d2005-12-19 16:26:28 -08002182 u32 grc_local_ctrl = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
Michael Chandc56b7d2005-12-19 16:26:28 -08002184 /* Workaround to prevent overdrawing Amps. */
2185 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2186 ASIC_REV_5714) {
2187 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chanb401e9e2005-12-19 16:27:04 -08002188 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2189 grc_local_ctrl, 100);
Michael Chandc56b7d2005-12-19 16:26:28 -08002190 }
2191
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 /* On 5753 and variants, GPIO2 cannot be used. */
2193 no_gpio2 = tp->nic_sram_data_cfg &
2194 NIC_SRAM_DATA_CFG_NO_GPIO2;
2195
Michael Chandc56b7d2005-12-19 16:26:28 -08002196 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 GRC_LCLCTRL_GPIO_OE1 |
2198 GRC_LCLCTRL_GPIO_OE2 |
2199 GRC_LCLCTRL_GPIO_OUTPUT1 |
2200 GRC_LCLCTRL_GPIO_OUTPUT2;
2201 if (no_gpio2) {
2202 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2203 GRC_LCLCTRL_GPIO_OUTPUT2);
2204 }
Michael Chanb401e9e2005-12-19 16:27:04 -08002205 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2206 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
2208 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2209
Michael Chanb401e9e2005-12-19 16:27:04 -08002210 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2211 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
2213 if (!no_gpio2) {
2214 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chanb401e9e2005-12-19 16:27:04 -08002215 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2216 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 }
2218 }
2219 } else {
2220 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
2221 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
Michael Chanb401e9e2005-12-19 16:27:04 -08002222 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2223 (GRC_LCLCTRL_GPIO_OE1 |
2224 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
Michael Chanb401e9e2005-12-19 16:27:04 -08002226 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2227 GRC_LCLCTRL_GPIO_OE1, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
Michael Chanb401e9e2005-12-19 16:27:04 -08002229 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2230 (GRC_LCLCTRL_GPIO_OE1 |
2231 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 }
2233 }
2234}
2235
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002236static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2237{
2238 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2239 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00002240 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002241 if (speed != SPEED_10)
2242 return 1;
2243 } else if (speed == SPEED_10)
2244 return 1;
2245
2246 return 0;
2247}
2248
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249static int tg3_setup_phy(struct tg3 *, int);
2250
2251#define RESET_KIND_SHUTDOWN 0
2252#define RESET_KIND_INIT 1
2253#define RESET_KIND_SUSPEND 2
2254
2255static void tg3_write_sig_post_reset(struct tg3 *, int);
2256static int tg3_halt_cpu(struct tg3 *, u32);
2257
Matt Carlson0a459aa2008-11-03 16:54:15 -08002258static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002259{
Matt Carlsonce057f02007-11-12 21:08:03 -08002260 u32 val;
2261
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002262 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Michael Chan51297242007-02-13 12:17:57 -08002263 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2264 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2265 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2266
2267 sg_dig_ctrl |=
2268 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2269 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2270 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2271 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002272 return;
Michael Chan51297242007-02-13 12:17:57 -08002273 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002274
Michael Chan60189dd2006-12-17 17:08:07 -08002275 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002276 tg3_bmcr_reset(tp);
2277 val = tr32(GRC_MISC_CFG);
2278 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2279 udelay(40);
2280 return;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002281 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson0e5f7842009-11-02 14:26:38 +00002282 u32 phytest;
2283 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2284 u32 phy;
2285
2286 tg3_writephy(tp, MII_ADVERTISE, 0);
2287 tg3_writephy(tp, MII_BMCR,
2288 BMCR_ANENABLE | BMCR_ANRESTART);
2289
2290 tg3_writephy(tp, MII_TG3_FET_TEST,
2291 phytest | MII_TG3_FET_SHADOW_EN);
2292 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
2293 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
2294 tg3_writephy(tp,
2295 MII_TG3_FET_SHDW_AUXMODE4,
2296 phy);
2297 }
2298 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2299 }
2300 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002301 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07002302 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2303 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002304
2305 tg3_writephy(tp, MII_TG3_AUX_CTRL,
2306 MII_TG3_AUXCTL_SHDWSEL_PWRCTL |
2307 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2308 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2309 MII_TG3_AUXCTL_PCTL_VREG_11V);
Michael Chan715116a2006-09-27 16:09:25 -07002310 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002311
Michael Chan15c3b692006-03-22 01:06:52 -08002312 /* The PHY should not be powered down on some chips because
2313 * of bugs.
2314 */
2315 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2316 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2317 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002318 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Michael Chan15c3b692006-03-22 01:06:52 -08002319 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08002320
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002321 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2322 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002323 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2324 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2325 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2326 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2327 }
2328
Michael Chan15c3b692006-03-22 01:06:52 -08002329 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2330}
2331
Matt Carlson3f007892008-11-03 16:51:36 -08002332/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002333static int tg3_nvram_lock(struct tg3 *tp)
2334{
2335 if (tp->tg3_flags & TG3_FLAG_NVRAM) {
2336 int i;
2337
2338 if (tp->nvram_lock_cnt == 0) {
2339 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2340 for (i = 0; i < 8000; i++) {
2341 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2342 break;
2343 udelay(20);
2344 }
2345 if (i == 8000) {
2346 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2347 return -ENODEV;
2348 }
2349 }
2350 tp->nvram_lock_cnt++;
2351 }
2352 return 0;
2353}
2354
2355/* tp->lock is held. */
2356static void tg3_nvram_unlock(struct tg3 *tp)
2357{
2358 if (tp->tg3_flags & TG3_FLAG_NVRAM) {
2359 if (tp->nvram_lock_cnt > 0)
2360 tp->nvram_lock_cnt--;
2361 if (tp->nvram_lock_cnt == 0)
2362 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
2363 }
2364}
2365
2366/* tp->lock is held. */
2367static void tg3_enable_nvram_access(struct tg3 *tp)
2368{
2369 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Matt Carlsonf66a29b2009-11-13 13:03:36 +00002370 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002371 u32 nvaccess = tr32(NVRAM_ACCESS);
2372
2373 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
2374 }
2375}
2376
2377/* tp->lock is held. */
2378static void tg3_disable_nvram_access(struct tg3 *tp)
2379{
2380 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Matt Carlsonf66a29b2009-11-13 13:03:36 +00002381 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002382 u32 nvaccess = tr32(NVRAM_ACCESS);
2383
2384 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
2385 }
2386}
2387
2388static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
2389 u32 offset, u32 *val)
2390{
2391 u32 tmp;
2392 int i;
2393
2394 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
2395 return -EINVAL;
2396
2397 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
2398 EEPROM_ADDR_DEVID_MASK |
2399 EEPROM_ADDR_READ);
2400 tw32(GRC_EEPROM_ADDR,
2401 tmp |
2402 (0 << EEPROM_ADDR_DEVID_SHIFT) |
2403 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
2404 EEPROM_ADDR_ADDR_MASK) |
2405 EEPROM_ADDR_READ | EEPROM_ADDR_START);
2406
2407 for (i = 0; i < 1000; i++) {
2408 tmp = tr32(GRC_EEPROM_ADDR);
2409
2410 if (tmp & EEPROM_ADDR_COMPLETE)
2411 break;
2412 msleep(1);
2413 }
2414 if (!(tmp & EEPROM_ADDR_COMPLETE))
2415 return -EBUSY;
2416
Matt Carlson62cedd12009-04-20 14:52:29 -07002417 tmp = tr32(GRC_EEPROM_DATA);
2418
2419 /*
2420 * The data will always be opposite the native endian
2421 * format. Perform a blind byteswap to compensate.
2422 */
2423 *val = swab32(tmp);
2424
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002425 return 0;
2426}
2427
2428#define NVRAM_CMD_TIMEOUT 10000
2429
2430static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
2431{
2432 int i;
2433
2434 tw32(NVRAM_CMD, nvram_cmd);
2435 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
2436 udelay(10);
2437 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
2438 udelay(10);
2439 break;
2440 }
2441 }
2442
2443 if (i == NVRAM_CMD_TIMEOUT)
2444 return -EBUSY;
2445
2446 return 0;
2447}
2448
2449static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
2450{
2451 if ((tp->tg3_flags & TG3_FLAG_NVRAM) &&
2452 (tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) &&
2453 (tp->tg3_flags2 & TG3_FLG2_FLASH) &&
2454 !(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM_ADDR_TRANS) &&
2455 (tp->nvram_jedecnum == JEDEC_ATMEL))
2456
2457 addr = ((addr / tp->nvram_pagesize) <<
2458 ATMEL_AT45DB0X1B_PAGE_POS) +
2459 (addr % tp->nvram_pagesize);
2460
2461 return addr;
2462}
2463
2464static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
2465{
2466 if ((tp->tg3_flags & TG3_FLAG_NVRAM) &&
2467 (tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) &&
2468 (tp->tg3_flags2 & TG3_FLG2_FLASH) &&
2469 !(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM_ADDR_TRANS) &&
2470 (tp->nvram_jedecnum == JEDEC_ATMEL))
2471
2472 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
2473 tp->nvram_pagesize) +
2474 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
2475
2476 return addr;
2477}
2478
Matt Carlsone4f34112009-02-25 14:25:00 +00002479/* NOTE: Data read in from NVRAM is byteswapped according to
2480 * the byteswapping settings for all other register accesses.
2481 * tg3 devices are BE devices, so on a BE machine, the data
2482 * returned will be exactly as it is seen in NVRAM. On a LE
2483 * machine, the 32-bit value will be byteswapped.
2484 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002485static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
2486{
2487 int ret;
2488
2489 if (!(tp->tg3_flags & TG3_FLAG_NVRAM))
2490 return tg3_nvram_read_using_eeprom(tp, offset, val);
2491
2492 offset = tg3_nvram_phys_addr(tp, offset);
2493
2494 if (offset > NVRAM_ADDR_MSK)
2495 return -EINVAL;
2496
2497 ret = tg3_nvram_lock(tp);
2498 if (ret)
2499 return ret;
2500
2501 tg3_enable_nvram_access(tp);
2502
2503 tw32(NVRAM_ADDR, offset);
2504 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
2505 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
2506
2507 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00002508 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002509
2510 tg3_disable_nvram_access(tp);
2511
2512 tg3_nvram_unlock(tp);
2513
2514 return ret;
2515}
2516
Matt Carlsona9dc5292009-02-25 14:25:30 +00002517/* Ensures NVRAM data is in bytestream format. */
2518static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002519{
2520 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00002521 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002522 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00002523 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002524 return res;
2525}
2526
2527/* tp->lock is held. */
Matt Carlson3f007892008-11-03 16:51:36 -08002528static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
2529{
2530 u32 addr_high, addr_low;
2531 int i;
2532
2533 addr_high = ((tp->dev->dev_addr[0] << 8) |
2534 tp->dev->dev_addr[1]);
2535 addr_low = ((tp->dev->dev_addr[2] << 24) |
2536 (tp->dev->dev_addr[3] << 16) |
2537 (tp->dev->dev_addr[4] << 8) |
2538 (tp->dev->dev_addr[5] << 0));
2539 for (i = 0; i < 4; i++) {
2540 if (i == 1 && skip_mac_1)
2541 continue;
2542 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
2543 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
2544 }
2545
2546 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2547 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2548 for (i = 0; i < 12; i++) {
2549 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
2550 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
2551 }
2552 }
2553
2554 addr_high = (tp->dev->dev_addr[0] +
2555 tp->dev->dev_addr[1] +
2556 tp->dev->dev_addr[2] +
2557 tp->dev->dev_addr[3] +
2558 tp->dev->dev_addr[4] +
2559 tp->dev->dev_addr[5]) &
2560 TX_BACKOFF_SEED_MASK;
2561 tw32(MAC_TX_BACKOFF_SEED, addr_high);
2562}
2563
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002564static void tg3_enable_register_access(struct tg3 *tp)
2565{
2566 /*
2567 * Make sure register accesses (indirect or otherwise) will function
2568 * correctly.
2569 */
2570 pci_write_config_dword(tp->pdev,
2571 TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
2572}
2573
2574static int tg3_power_up(struct tg3 *tp)
2575{
2576 tg3_enable_register_access(tp);
2577
2578 pci_set_power_state(tp->pdev, PCI_D0);
2579
2580 /* Switch out of Vaux if it is a NIC */
2581 if (tp->tg3_flags2 & TG3_FLG2_IS_NIC)
2582 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl, 100);
2583
2584 return 0;
2585}
2586
2587static int tg3_power_down_prepare(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588{
2589 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002590 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002592 tg3_enable_register_access(tp);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002593
2594 /* Restore the CLKREQ setting. */
2595 if (tp->tg3_flags3 & TG3_FLG3_CLKREQ_BUG) {
2596 u16 lnkctl;
2597
2598 pci_read_config_word(tp->pdev,
2599 tp->pcie_cap + PCI_EXP_LNKCTL,
2600 &lnkctl);
2601 lnkctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
2602 pci_write_config_word(tp->pdev,
2603 tp->pcie_cap + PCI_EXP_LNKCTL,
2604 lnkctl);
2605 }
2606
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
2608 tw32(TG3PCI_MISC_HOST_CTRL,
2609 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
2610
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002611 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002612 (tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
2613
Matt Carlsondd477002008-05-25 23:45:58 -07002614 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08002615 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002616 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson80096062010-08-02 11:26:06 +00002617 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002618 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002619 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002620
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002621 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002622
Matt Carlson80096062010-08-02 11:26:06 +00002623 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002624
2625 tp->link_config.orig_speed = phydev->speed;
2626 tp->link_config.orig_duplex = phydev->duplex;
2627 tp->link_config.orig_autoneg = phydev->autoneg;
2628 tp->link_config.orig_advertising = phydev->advertising;
2629
2630 advertising = ADVERTISED_TP |
2631 ADVERTISED_Pause |
2632 ADVERTISED_Autoneg |
2633 ADVERTISED_10baseT_Half;
2634
2635 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002636 device_should_wake) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002637 if (tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB)
2638 advertising |=
2639 ADVERTISED_100baseT_Half |
2640 ADVERTISED_100baseT_Full |
2641 ADVERTISED_10baseT_Full;
2642 else
2643 advertising |= ADVERTISED_10baseT_Full;
2644 }
2645
2646 phydev->advertising = advertising;
2647
2648 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002649
2650 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00002651 if (phyid != PHY_ID_BCMAC131) {
2652 phyid &= PHY_BCM_OUI_MASK;
2653 if (phyid == PHY_BCM_OUI_1 ||
2654 phyid == PHY_BCM_OUI_2 ||
2655 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08002656 do_low_power = true;
2657 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002658 }
Matt Carlsondd477002008-05-25 23:45:58 -07002659 } else {
Matt Carlson20232762008-12-21 20:18:56 -08002660 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002661
Matt Carlson80096062010-08-02 11:26:06 +00002662 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
2663 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07002664 tp->link_config.orig_speed = tp->link_config.speed;
2665 tp->link_config.orig_duplex = tp->link_config.duplex;
2666 tp->link_config.orig_autoneg = tp->link_config.autoneg;
2667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002669 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Matt Carlsondd477002008-05-25 23:45:58 -07002670 tp->link_config.speed = SPEED_10;
2671 tp->link_config.duplex = DUPLEX_HALF;
2672 tp->link_config.autoneg = AUTONEG_ENABLE;
2673 tg3_setup_phy(tp, 0);
2674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 }
2676
Michael Chanb5d37722006-09-27 16:06:21 -07002677 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
2678 u32 val;
2679
2680 val = tr32(GRC_VCPU_EXT_CTRL);
2681 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
2682 } else if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08002683 int i;
2684 u32 val;
2685
2686 for (i = 0; i < 200; i++) {
2687 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
2688 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
2689 break;
2690 msleep(1);
2691 }
2692 }
Gary Zambranoa85feb82007-05-05 11:52:19 -07002693 if (tp->tg3_flags & TG3_FLAG_WOL_CAP)
2694 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
2695 WOL_DRV_STATE_SHUTDOWN |
2696 WOL_DRV_WOL |
2697 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08002698
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002699 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 u32 mac_mode;
2701
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002702 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08002703 if (do_low_power) {
Matt Carlsondd477002008-05-25 23:45:58 -07002704 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x5a);
2705 udelay(40);
2706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002708 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07002709 mac_mode = MAC_MODE_PORT_MODE_GMII;
2710 else
2711 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002713 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
2714 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2715 ASIC_REV_5700) {
2716 u32 speed = (tp->tg3_flags &
2717 TG3_FLAG_WOL_SPEED_100MB) ?
2718 SPEED_100 : SPEED_10;
2719 if (tg3_5700_link_polarity(tp, speed))
2720 mac_mode |= MAC_MODE_LINK_POLARITY;
2721 else
2722 mac_mode &= ~MAC_MODE_LINK_POLARITY;
2723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 } else {
2725 mac_mode = MAC_MODE_PORT_MODE_TBI;
2726 }
2727
John W. Linvillecbf46852005-04-21 17:01:29 -07002728 if (!(tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 tw32(MAC_LED_CTRL, tp->led_ctrl);
2730
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002731 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
2732 if (((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
2733 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) &&
2734 ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
2735 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)))
2736 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737
Matt Carlsond2394e6b2010-11-24 08:31:47 +00002738 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
2739 mac_mode |= MAC_MODE_APE_TX_EN |
2740 MAC_MODE_APE_RX_EN |
2741 MAC_MODE_TDE_ENABLE;
Matt Carlson3bda1252008-08-15 14:08:22 -07002742
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 tw32_f(MAC_MODE, mac_mode);
2744 udelay(100);
2745
2746 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
2747 udelay(10);
2748 }
2749
2750 if (!(tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB) &&
2751 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2752 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
2753 u32 base_val;
2754
2755 base_val = tp->pci_clock_ctrl;
2756 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
2757 CLOCK_CTRL_TXCLK_DISABLE);
2758
Michael Chanb401e9e2005-12-19 16:27:04 -08002759 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
2760 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Michael Chand7b0a852007-02-13 12:17:38 -08002761 } else if ((tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
Matt Carlson795d01c2007-10-07 23:28:17 -07002762 (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
Michael Chand7b0a852007-02-13 12:17:38 -08002763 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)) {
Michael Chan4cf78e42005-07-25 12:29:19 -07002764 /* do nothing */
Michael Chan85e94ce2005-04-21 17:05:28 -07002765 } else if (!((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF))) {
2767 u32 newbits1, newbits2;
2768
2769 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2770 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2771 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
2772 CLOCK_CTRL_TXCLK_DISABLE |
2773 CLOCK_CTRL_ALTCLK);
2774 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2775 } else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
2776 newbits1 = CLOCK_CTRL_625_CORE;
2777 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
2778 } else {
2779 newbits1 = CLOCK_CTRL_ALTCLK;
2780 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2781 }
2782
Michael Chanb401e9e2005-12-19 16:27:04 -08002783 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
2784 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785
Michael Chanb401e9e2005-12-19 16:27:04 -08002786 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
2787 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788
2789 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
2790 u32 newbits3;
2791
2792 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2793 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2794 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
2795 CLOCK_CTRL_TXCLK_DISABLE |
2796 CLOCK_CTRL_44MHZ_CORE);
2797 } else {
2798 newbits3 = CLOCK_CTRL_44MHZ_CORE;
2799 }
2800
Michael Chanb401e9e2005-12-19 16:27:04 -08002801 tw32_wait_f(TG3PCI_CLOCK_CTRL,
2802 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 }
2804 }
2805
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002806 if (!(device_should_wake) &&
Matt Carlson22435842008-11-21 17:21:13 -08002807 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08002808 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08002809
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 tg3_frob_aux_power(tp);
2811
2812 /* Workaround for unstable PLL clock */
2813 if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
2814 (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
2815 u32 val = tr32(0x7d00);
2816
2817 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
2818 tw32(0x7d00, val);
Michael Chan6921d202005-12-13 21:15:53 -08002819 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08002820 int err;
2821
2822 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08002824 if (!err)
2825 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08002826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 }
2828
Michael Chanbbadf502006-04-06 21:46:34 -07002829 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
2830
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 return 0;
2832}
2833
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002834static void tg3_power_down(struct tg3 *tp)
2835{
2836 tg3_power_down_prepare(tp);
2837
2838 pci_wake_from_d3(tp->pdev, tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
2839 pci_set_power_state(tp->pdev, PCI_D3hot);
2840}
2841
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
2843{
2844 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
2845 case MII_TG3_AUX_STAT_10HALF:
2846 *speed = SPEED_10;
2847 *duplex = DUPLEX_HALF;
2848 break;
2849
2850 case MII_TG3_AUX_STAT_10FULL:
2851 *speed = SPEED_10;
2852 *duplex = DUPLEX_FULL;
2853 break;
2854
2855 case MII_TG3_AUX_STAT_100HALF:
2856 *speed = SPEED_100;
2857 *duplex = DUPLEX_HALF;
2858 break;
2859
2860 case MII_TG3_AUX_STAT_100FULL:
2861 *speed = SPEED_100;
2862 *duplex = DUPLEX_FULL;
2863 break;
2864
2865 case MII_TG3_AUX_STAT_1000HALF:
2866 *speed = SPEED_1000;
2867 *duplex = DUPLEX_HALF;
2868 break;
2869
2870 case MII_TG3_AUX_STAT_1000FULL:
2871 *speed = SPEED_1000;
2872 *duplex = DUPLEX_FULL;
2873 break;
2874
2875 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002876 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07002877 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
2878 SPEED_10;
2879 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
2880 DUPLEX_HALF;
2881 break;
2882 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 *speed = SPEED_INVALID;
2884 *duplex = DUPLEX_INVALID;
2885 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002886 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887}
2888
2889static void tg3_phy_copper_begin(struct tg3 *tp)
2890{
2891 u32 new_adv;
2892 int i;
2893
Matt Carlson80096062010-08-02 11:26:06 +00002894 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 /* Entering low power mode. Disable gigabit and
2896 * 100baseT advertisements.
2897 */
2898 tg3_writephy(tp, MII_TG3_CTRL, 0);
2899
2900 new_adv = (ADVERTISE_10HALF | ADVERTISE_10FULL |
2901 ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
2902 if (tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB)
2903 new_adv |= (ADVERTISE_100HALF | ADVERTISE_100FULL);
2904
2905 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2906 } else if (tp->link_config.speed == SPEED_INVALID) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002907 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 tp->link_config.advertising &=
2909 ~(ADVERTISED_1000baseT_Half |
2910 ADVERTISED_1000baseT_Full);
2911
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002912 new_adv = ADVERTISE_CSMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 if (tp->link_config.advertising & ADVERTISED_10baseT_Half)
2914 new_adv |= ADVERTISE_10HALF;
2915 if (tp->link_config.advertising & ADVERTISED_10baseT_Full)
2916 new_adv |= ADVERTISE_10FULL;
2917 if (tp->link_config.advertising & ADVERTISED_100baseT_Half)
2918 new_adv |= ADVERTISE_100HALF;
2919 if (tp->link_config.advertising & ADVERTISED_100baseT_Full)
2920 new_adv |= ADVERTISE_100FULL;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002921
2922 new_adv |= tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2923
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2925
2926 if (tp->link_config.advertising &
2927 (ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full)) {
2928 new_adv = 0;
2929 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
2930 new_adv |= MII_TG3_CTRL_ADV_1000_HALF;
2931 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
2932 new_adv |= MII_TG3_CTRL_ADV_1000_FULL;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002933 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2935 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0))
2936 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2937 MII_TG3_CTRL_ENABLE_AS_MASTER);
2938 tg3_writephy(tp, MII_TG3_CTRL, new_adv);
2939 } else {
2940 tg3_writephy(tp, MII_TG3_CTRL, 0);
2941 }
2942 } else {
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002943 new_adv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2944 new_adv |= ADVERTISE_CSMA;
2945
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 /* Asking for a specific link mode. */
2947 if (tp->link_config.speed == SPEED_1000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2949
2950 if (tp->link_config.duplex == DUPLEX_FULL)
2951 new_adv = MII_TG3_CTRL_ADV_1000_FULL;
2952 else
2953 new_adv = MII_TG3_CTRL_ADV_1000_HALF;
2954 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2955 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
2956 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2957 MII_TG3_CTRL_ENABLE_AS_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 if (tp->link_config.speed == SPEED_100) {
2960 if (tp->link_config.duplex == DUPLEX_FULL)
2961 new_adv |= ADVERTISE_100FULL;
2962 else
2963 new_adv |= ADVERTISE_100HALF;
2964 } else {
2965 if (tp->link_config.duplex == DUPLEX_FULL)
2966 new_adv |= ADVERTISE_10FULL;
2967 else
2968 new_adv |= ADVERTISE_10HALF;
2969 }
2970 tg3_writephy(tp, MII_ADVERTISE, new_adv);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002971
2972 new_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002974
2975 tg3_writephy(tp, MII_TG3_CTRL, new_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 }
2977
Matt Carlson52b02d02010-10-14 10:37:41 +00002978 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
Matt Carlsona6b68da2010-12-06 08:28:52 +00002979 u32 val;
Matt Carlson52b02d02010-10-14 10:37:41 +00002980
2981 tw32(TG3_CPMU_EEE_MODE,
2982 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
2983
2984 /* Enable SM_DSP clock and tx 6dB coding. */
2985 val = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
2986 MII_TG3_AUXCTL_ACTL_SMDSP_ENA |
2987 MII_TG3_AUXCTL_ACTL_TX_6DB;
2988 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
2989
Matt Carlson21a00ab2011-01-25 15:58:55 +00002990 switch (GET_ASIC_REV(tp->pci_chip_rev_id)) {
2991 case ASIC_REV_5717:
2992 case ASIC_REV_57765:
2993 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
2994 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
2995 MII_TG3_DSP_CH34TP2_HIBW01);
2996 /* Fall through */
2997 case ASIC_REV_5719:
2998 val = MII_TG3_DSP_TAP26_ALNOKO |
2999 MII_TG3_DSP_TAP26_RMRXSTO |
3000 MII_TG3_DSP_TAP26_OPCSINPT;
3001 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
3002 }
Matt Carlson52b02d02010-10-14 10:37:41 +00003003
Matt Carlsona6b68da2010-12-06 08:28:52 +00003004 val = 0;
Matt Carlson52b02d02010-10-14 10:37:41 +00003005 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3006 /* Advertise 100-BaseTX EEE ability */
3007 if (tp->link_config.advertising &
Matt Carlson3110f5f52010-12-06 08:28:50 +00003008 ADVERTISED_100baseT_Full)
3009 val |= MDIO_AN_EEE_ADV_100TX;
Matt Carlson52b02d02010-10-14 10:37:41 +00003010 /* Advertise 1000-BaseT EEE ability */
3011 if (tp->link_config.advertising &
Matt Carlson3110f5f52010-12-06 08:28:50 +00003012 ADVERTISED_1000baseT_Full)
3013 val |= MDIO_AN_EEE_ADV_1000T;
Matt Carlson52b02d02010-10-14 10:37:41 +00003014 }
Matt Carlson3110f5f52010-12-06 08:28:50 +00003015 tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlson52b02d02010-10-14 10:37:41 +00003016
3017 /* Turn off SM_DSP clock. */
3018 val = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
3019 MII_TG3_AUXCTL_ACTL_TX_6DB;
3020 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
3021 }
3022
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 if (tp->link_config.autoneg == AUTONEG_DISABLE &&
3024 tp->link_config.speed != SPEED_INVALID) {
3025 u32 bmcr, orig_bmcr;
3026
3027 tp->link_config.active_speed = tp->link_config.speed;
3028 tp->link_config.active_duplex = tp->link_config.duplex;
3029
3030 bmcr = 0;
3031 switch (tp->link_config.speed) {
3032 default:
3033 case SPEED_10:
3034 break;
3035
3036 case SPEED_100:
3037 bmcr |= BMCR_SPEED100;
3038 break;
3039
3040 case SPEED_1000:
3041 bmcr |= TG3_BMCR_SPEED1000;
3042 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003043 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044
3045 if (tp->link_config.duplex == DUPLEX_FULL)
3046 bmcr |= BMCR_FULLDPLX;
3047
3048 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
3049 (bmcr != orig_bmcr)) {
3050 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
3051 for (i = 0; i < 1500; i++) {
3052 u32 tmp;
3053
3054 udelay(10);
3055 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
3056 tg3_readphy(tp, MII_BMSR, &tmp))
3057 continue;
3058 if (!(tmp & BMSR_LSTATUS)) {
3059 udelay(40);
3060 break;
3061 }
3062 }
3063 tg3_writephy(tp, MII_BMCR, bmcr);
3064 udelay(40);
3065 }
3066 } else {
3067 tg3_writephy(tp, MII_BMCR,
3068 BMCR_ANENABLE | BMCR_ANRESTART);
3069 }
3070}
3071
3072static int tg3_init_5401phy_dsp(struct tg3 *tp)
3073{
3074 int err;
3075
3076 /* Turn off tap power management. */
3077 /* Set Extended packet length bit */
3078 err = tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4c20);
3079
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00003080 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
3081 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
3082 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
3083 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
3084 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085
3086 udelay(40);
3087
3088 return err;
3089}
3090
Michael Chan3600d912006-12-07 00:21:48 -08003091static int tg3_copper_is_advertising_all(struct tg3 *tp, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092{
Michael Chan3600d912006-12-07 00:21:48 -08003093 u32 adv_reg, all_mask = 0;
3094
3095 if (mask & ADVERTISED_10baseT_Half)
3096 all_mask |= ADVERTISE_10HALF;
3097 if (mask & ADVERTISED_10baseT_Full)
3098 all_mask |= ADVERTISE_10FULL;
3099 if (mask & ADVERTISED_100baseT_Half)
3100 all_mask |= ADVERTISE_100HALF;
3101 if (mask & ADVERTISED_100baseT_Full)
3102 all_mask |= ADVERTISE_100FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103
3104 if (tg3_readphy(tp, MII_ADVERTISE, &adv_reg))
3105 return 0;
3106
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 if ((adv_reg & all_mask) != all_mask)
3108 return 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003109 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 u32 tg3_ctrl;
3111
Michael Chan3600d912006-12-07 00:21:48 -08003112 all_mask = 0;
3113 if (mask & ADVERTISED_1000baseT_Half)
3114 all_mask |= ADVERTISE_1000HALF;
3115 if (mask & ADVERTISED_1000baseT_Full)
3116 all_mask |= ADVERTISE_1000FULL;
3117
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 if (tg3_readphy(tp, MII_TG3_CTRL, &tg3_ctrl))
3119 return 0;
3120
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 if ((tg3_ctrl & all_mask) != all_mask)
3122 return 0;
3123 }
3124 return 1;
3125}
3126
Matt Carlsonef167e22007-12-20 20:10:01 -08003127static int tg3_adv_1000T_flowctrl_ok(struct tg3 *tp, u32 *lcladv, u32 *rmtadv)
3128{
3129 u32 curadv, reqadv;
3130
3131 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
3132 return 1;
3133
3134 curadv = *lcladv & (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
3135 reqadv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
3136
3137 if (tp->link_config.active_duplex == DUPLEX_FULL) {
3138 if (curadv != reqadv)
3139 return 0;
3140
3141 if (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG)
3142 tg3_readphy(tp, MII_LPA, rmtadv);
3143 } else {
3144 /* Reprogram the advertisement register, even if it
3145 * does not affect the current link. If the link
3146 * gets renegotiated in the future, we can save an
3147 * additional renegotiation cycle by advertising
3148 * it correctly in the first place.
3149 */
3150 if (curadv != reqadv) {
3151 *lcladv &= ~(ADVERTISE_PAUSE_CAP |
3152 ADVERTISE_PAUSE_ASYM);
3153 tg3_writephy(tp, MII_ADVERTISE, *lcladv | reqadv);
3154 }
3155 }
3156
3157 return 1;
3158}
3159
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
3161{
3162 int current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003163 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08003164 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 u16 current_speed;
3166 u8 current_duplex;
3167 int i, err;
3168
3169 tw32(MAC_EVENT, 0);
3170
3171 tw32_f(MAC_STATUS,
3172 (MAC_STATUS_SYNC_CHANGED |
3173 MAC_STATUS_CFG_CHANGED |
3174 MAC_STATUS_MI_COMPLETION |
3175 MAC_STATUS_LNKSTATE_CHANGED));
3176 udelay(40);
3177
Matt Carlson8ef21422008-05-02 16:47:53 -07003178 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
3179 tw32_f(MAC_MI_MODE,
3180 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
3181 udelay(80);
3182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183
3184 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x02);
3185
3186 /* Some third-party PHYs need to be reset on link going
3187 * down.
3188 */
3189 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3190 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
3191 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
3192 netif_carrier_ok(tp->dev)) {
3193 tg3_readphy(tp, MII_BMSR, &bmsr);
3194 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3195 !(bmsr & BMSR_LSTATUS))
3196 force_reset = 1;
3197 }
3198 if (force_reset)
3199 tg3_phy_reset(tp);
3200
Matt Carlson79eb6902010-02-17 15:17:03 +00003201 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 tg3_readphy(tp, MII_BMSR, &bmsr);
3203 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
3204 !(tp->tg3_flags & TG3_FLAG_INIT_COMPLETE))
3205 bmsr = 0;
3206
3207 if (!(bmsr & BMSR_LSTATUS)) {
3208 err = tg3_init_5401phy_dsp(tp);
3209 if (err)
3210 return err;
3211
3212 tg3_readphy(tp, MII_BMSR, &bmsr);
3213 for (i = 0; i < 1000; i++) {
3214 udelay(10);
3215 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3216 (bmsr & BMSR_LSTATUS)) {
3217 udelay(40);
3218 break;
3219 }
3220 }
3221
Matt Carlson79eb6902010-02-17 15:17:03 +00003222 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
3223 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 !(bmsr & BMSR_LSTATUS) &&
3225 tp->link_config.active_speed == SPEED_1000) {
3226 err = tg3_phy_reset(tp);
3227 if (!err)
3228 err = tg3_init_5401phy_dsp(tp);
3229 if (err)
3230 return err;
3231 }
3232 }
3233 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3234 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
3235 /* 5701 {A0,B0} CRC bug workaround */
3236 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00003237 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
3238 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
3239 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 }
3241
3242 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003243 tg3_readphy(tp, MII_TG3_ISTAT, &val);
3244 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003246 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003248 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 tg3_writephy(tp, MII_TG3_IMASK, ~0);
3250
3251 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3252 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3253 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
3254 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3255 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
3256 else
3257 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
3258 }
3259
3260 current_link_up = 0;
3261 current_speed = SPEED_INVALID;
3262 current_duplex = DUPLEX_INVALID;
3263
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003264 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4007);
3266 tg3_readphy(tp, MII_TG3_AUX_CTRL, &val);
3267 if (!(val & (1 << 10))) {
3268 val |= (1 << 10);
3269 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
3270 goto relink;
3271 }
3272 }
3273
3274 bmsr = 0;
3275 for (i = 0; i < 100; i++) {
3276 tg3_readphy(tp, MII_BMSR, &bmsr);
3277 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3278 (bmsr & BMSR_LSTATUS))
3279 break;
3280 udelay(40);
3281 }
3282
3283 if (bmsr & BMSR_LSTATUS) {
3284 u32 aux_stat, bmcr;
3285
3286 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
3287 for (i = 0; i < 2000; i++) {
3288 udelay(10);
3289 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
3290 aux_stat)
3291 break;
3292 }
3293
3294 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
3295 &current_speed,
3296 &current_duplex);
3297
3298 bmcr = 0;
3299 for (i = 0; i < 200; i++) {
3300 tg3_readphy(tp, MII_BMCR, &bmcr);
3301 if (tg3_readphy(tp, MII_BMCR, &bmcr))
3302 continue;
3303 if (bmcr && bmcr != 0x7fff)
3304 break;
3305 udelay(10);
3306 }
3307
Matt Carlsonef167e22007-12-20 20:10:01 -08003308 lcl_adv = 0;
3309 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310
Matt Carlsonef167e22007-12-20 20:10:01 -08003311 tp->link_config.active_speed = current_speed;
3312 tp->link_config.active_duplex = current_duplex;
3313
3314 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3315 if ((bmcr & BMCR_ANENABLE) &&
3316 tg3_copper_is_advertising_all(tp,
3317 tp->link_config.advertising)) {
3318 if (tg3_adv_1000T_flowctrl_ok(tp, &lcl_adv,
3319 &rmt_adv))
3320 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 }
3322 } else {
3323 if (!(bmcr & BMCR_ANENABLE) &&
3324 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08003325 tp->link_config.duplex == current_duplex &&
3326 tp->link_config.flowctrl ==
3327 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 }
3330 }
3331
Matt Carlsonef167e22007-12-20 20:10:01 -08003332 if (current_link_up == 1 &&
3333 tp->link_config.active_duplex == DUPLEX_FULL)
3334 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 }
3336
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337relink:
Matt Carlson80096062010-08-02 11:26:06 +00003338 if (current_link_up == 0 || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 tg3_phy_copper_begin(tp);
3340
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003341 tg3_readphy(tp, MII_BMSR, &bmsr);
3342 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3343 (bmsr & BMSR_LSTATUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 current_link_up = 1;
3345 }
3346
3347 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
3348 if (current_link_up == 1) {
3349 if (tp->link_config.active_speed == SPEED_100 ||
3350 tp->link_config.active_speed == SPEED_10)
3351 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3352 else
3353 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003354 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00003355 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3356 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
3358
3359 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
3360 if (tp->link_config.active_duplex == DUPLEX_HALF)
3361 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
3362
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003364 if (current_link_up == 1 &&
3365 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003367 else
3368 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 }
3370
3371 /* ??? Without this setting Netgear GA302T PHY does not
3372 * ??? send/receive packets...
3373 */
Matt Carlson79eb6902010-02-17 15:17:03 +00003374 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
3376 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
3377 tw32_f(MAC_MI_MODE, tp->mi_mode);
3378 udelay(80);
3379 }
3380
3381 tw32_f(MAC_MODE, tp->mac_mode);
3382 udelay(40);
3383
Matt Carlson52b02d02010-10-14 10:37:41 +00003384 tg3_phy_eee_adjust(tp, current_link_up);
3385
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 if (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) {
3387 /* Polled via timer. */
3388 tw32_f(MAC_EVENT, 0);
3389 } else {
3390 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
3391 }
3392 udelay(40);
3393
3394 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
3395 current_link_up == 1 &&
3396 tp->link_config.active_speed == SPEED_1000 &&
3397 ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) ||
3398 (tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED))) {
3399 udelay(120);
3400 tw32_f(MAC_STATUS,
3401 (MAC_STATUS_SYNC_CHANGED |
3402 MAC_STATUS_CFG_CHANGED));
3403 udelay(40);
3404 tg3_write_mem(tp,
3405 NIC_SRAM_FIRMWARE_MBOX,
3406 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
3407 }
3408
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003409 /* Prevent send BD corruption. */
3410 if (tp->tg3_flags3 & TG3_FLG3_CLKREQ_BUG) {
3411 u16 oldlnkctl, newlnkctl;
3412
3413 pci_read_config_word(tp->pdev,
3414 tp->pcie_cap + PCI_EXP_LNKCTL,
3415 &oldlnkctl);
3416 if (tp->link_config.active_speed == SPEED_100 ||
3417 tp->link_config.active_speed == SPEED_10)
3418 newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
3419 else
3420 newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
3421 if (newlnkctl != oldlnkctl)
3422 pci_write_config_word(tp->pdev,
3423 tp->pcie_cap + PCI_EXP_LNKCTL,
3424 newlnkctl);
3425 }
3426
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 if (current_link_up != netif_carrier_ok(tp->dev)) {
3428 if (current_link_up)
3429 netif_carrier_on(tp->dev);
3430 else
3431 netif_carrier_off(tp->dev);
3432 tg3_link_report(tp);
3433 }
3434
3435 return 0;
3436}
3437
3438struct tg3_fiber_aneginfo {
3439 int state;
3440#define ANEG_STATE_UNKNOWN 0
3441#define ANEG_STATE_AN_ENABLE 1
3442#define ANEG_STATE_RESTART_INIT 2
3443#define ANEG_STATE_RESTART 3
3444#define ANEG_STATE_DISABLE_LINK_OK 4
3445#define ANEG_STATE_ABILITY_DETECT_INIT 5
3446#define ANEG_STATE_ABILITY_DETECT 6
3447#define ANEG_STATE_ACK_DETECT_INIT 7
3448#define ANEG_STATE_ACK_DETECT 8
3449#define ANEG_STATE_COMPLETE_ACK_INIT 9
3450#define ANEG_STATE_COMPLETE_ACK 10
3451#define ANEG_STATE_IDLE_DETECT_INIT 11
3452#define ANEG_STATE_IDLE_DETECT 12
3453#define ANEG_STATE_LINK_OK 13
3454#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
3455#define ANEG_STATE_NEXT_PAGE_WAIT 15
3456
3457 u32 flags;
3458#define MR_AN_ENABLE 0x00000001
3459#define MR_RESTART_AN 0x00000002
3460#define MR_AN_COMPLETE 0x00000004
3461#define MR_PAGE_RX 0x00000008
3462#define MR_NP_LOADED 0x00000010
3463#define MR_TOGGLE_TX 0x00000020
3464#define MR_LP_ADV_FULL_DUPLEX 0x00000040
3465#define MR_LP_ADV_HALF_DUPLEX 0x00000080
3466#define MR_LP_ADV_SYM_PAUSE 0x00000100
3467#define MR_LP_ADV_ASYM_PAUSE 0x00000200
3468#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
3469#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
3470#define MR_LP_ADV_NEXT_PAGE 0x00001000
3471#define MR_TOGGLE_RX 0x00002000
3472#define MR_NP_RX 0x00004000
3473
3474#define MR_LINK_OK 0x80000000
3475
3476 unsigned long link_time, cur_time;
3477
3478 u32 ability_match_cfg;
3479 int ability_match_count;
3480
3481 char ability_match, idle_match, ack_match;
3482
3483 u32 txconfig, rxconfig;
3484#define ANEG_CFG_NP 0x00000080
3485#define ANEG_CFG_ACK 0x00000040
3486#define ANEG_CFG_RF2 0x00000020
3487#define ANEG_CFG_RF1 0x00000010
3488#define ANEG_CFG_PS2 0x00000001
3489#define ANEG_CFG_PS1 0x00008000
3490#define ANEG_CFG_HD 0x00004000
3491#define ANEG_CFG_FD 0x00002000
3492#define ANEG_CFG_INVAL 0x00001f06
3493
3494};
3495#define ANEG_OK 0
3496#define ANEG_DONE 1
3497#define ANEG_TIMER_ENAB 2
3498#define ANEG_FAILED -1
3499
3500#define ANEG_STATE_SETTLE_TIME 10000
3501
3502static int tg3_fiber_aneg_smachine(struct tg3 *tp,
3503 struct tg3_fiber_aneginfo *ap)
3504{
Matt Carlson5be73b42007-12-20 20:09:29 -08003505 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 unsigned long delta;
3507 u32 rx_cfg_reg;
3508 int ret;
3509
3510 if (ap->state == ANEG_STATE_UNKNOWN) {
3511 ap->rxconfig = 0;
3512 ap->link_time = 0;
3513 ap->cur_time = 0;
3514 ap->ability_match_cfg = 0;
3515 ap->ability_match_count = 0;
3516 ap->ability_match = 0;
3517 ap->idle_match = 0;
3518 ap->ack_match = 0;
3519 }
3520 ap->cur_time++;
3521
3522 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
3523 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
3524
3525 if (rx_cfg_reg != ap->ability_match_cfg) {
3526 ap->ability_match_cfg = rx_cfg_reg;
3527 ap->ability_match = 0;
3528 ap->ability_match_count = 0;
3529 } else {
3530 if (++ap->ability_match_count > 1) {
3531 ap->ability_match = 1;
3532 ap->ability_match_cfg = rx_cfg_reg;
3533 }
3534 }
3535 if (rx_cfg_reg & ANEG_CFG_ACK)
3536 ap->ack_match = 1;
3537 else
3538 ap->ack_match = 0;
3539
3540 ap->idle_match = 0;
3541 } else {
3542 ap->idle_match = 1;
3543 ap->ability_match_cfg = 0;
3544 ap->ability_match_count = 0;
3545 ap->ability_match = 0;
3546 ap->ack_match = 0;
3547
3548 rx_cfg_reg = 0;
3549 }
3550
3551 ap->rxconfig = rx_cfg_reg;
3552 ret = ANEG_OK;
3553
Matt Carlson33f401a2010-04-05 10:19:27 +00003554 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 case ANEG_STATE_UNKNOWN:
3556 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
3557 ap->state = ANEG_STATE_AN_ENABLE;
3558
3559 /* fallthru */
3560 case ANEG_STATE_AN_ENABLE:
3561 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
3562 if (ap->flags & MR_AN_ENABLE) {
3563 ap->link_time = 0;
3564 ap->cur_time = 0;
3565 ap->ability_match_cfg = 0;
3566 ap->ability_match_count = 0;
3567 ap->ability_match = 0;
3568 ap->idle_match = 0;
3569 ap->ack_match = 0;
3570
3571 ap->state = ANEG_STATE_RESTART_INIT;
3572 } else {
3573 ap->state = ANEG_STATE_DISABLE_LINK_OK;
3574 }
3575 break;
3576
3577 case ANEG_STATE_RESTART_INIT:
3578 ap->link_time = ap->cur_time;
3579 ap->flags &= ~(MR_NP_LOADED);
3580 ap->txconfig = 0;
3581 tw32(MAC_TX_AUTO_NEG, 0);
3582 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3583 tw32_f(MAC_MODE, tp->mac_mode);
3584 udelay(40);
3585
3586 ret = ANEG_TIMER_ENAB;
3587 ap->state = ANEG_STATE_RESTART;
3588
3589 /* fallthru */
3590 case ANEG_STATE_RESTART:
3591 delta = ap->cur_time - ap->link_time;
Matt Carlson859a5882010-04-05 10:19:28 +00003592 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a5882010-04-05 10:19:28 +00003594 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 break;
3597
3598 case ANEG_STATE_DISABLE_LINK_OK:
3599 ret = ANEG_DONE;
3600 break;
3601
3602 case ANEG_STATE_ABILITY_DETECT_INIT:
3603 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08003604 ap->txconfig = ANEG_CFG_FD;
3605 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3606 if (flowctrl & ADVERTISE_1000XPAUSE)
3607 ap->txconfig |= ANEG_CFG_PS1;
3608 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3609 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3611 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3612 tw32_f(MAC_MODE, tp->mac_mode);
3613 udelay(40);
3614
3615 ap->state = ANEG_STATE_ABILITY_DETECT;
3616 break;
3617
3618 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a5882010-04-05 10:19:28 +00003619 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 break;
3622
3623 case ANEG_STATE_ACK_DETECT_INIT:
3624 ap->txconfig |= ANEG_CFG_ACK;
3625 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3626 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3627 tw32_f(MAC_MODE, tp->mac_mode);
3628 udelay(40);
3629
3630 ap->state = ANEG_STATE_ACK_DETECT;
3631
3632 /* fallthru */
3633 case ANEG_STATE_ACK_DETECT:
3634 if (ap->ack_match != 0) {
3635 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
3636 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
3637 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
3638 } else {
3639 ap->state = ANEG_STATE_AN_ENABLE;
3640 }
3641 } else if (ap->ability_match != 0 &&
3642 ap->rxconfig == 0) {
3643 ap->state = ANEG_STATE_AN_ENABLE;
3644 }
3645 break;
3646
3647 case ANEG_STATE_COMPLETE_ACK_INIT:
3648 if (ap->rxconfig & ANEG_CFG_INVAL) {
3649 ret = ANEG_FAILED;
3650 break;
3651 }
3652 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
3653 MR_LP_ADV_HALF_DUPLEX |
3654 MR_LP_ADV_SYM_PAUSE |
3655 MR_LP_ADV_ASYM_PAUSE |
3656 MR_LP_ADV_REMOTE_FAULT1 |
3657 MR_LP_ADV_REMOTE_FAULT2 |
3658 MR_LP_ADV_NEXT_PAGE |
3659 MR_TOGGLE_RX |
3660 MR_NP_RX);
3661 if (ap->rxconfig & ANEG_CFG_FD)
3662 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
3663 if (ap->rxconfig & ANEG_CFG_HD)
3664 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
3665 if (ap->rxconfig & ANEG_CFG_PS1)
3666 ap->flags |= MR_LP_ADV_SYM_PAUSE;
3667 if (ap->rxconfig & ANEG_CFG_PS2)
3668 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
3669 if (ap->rxconfig & ANEG_CFG_RF1)
3670 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
3671 if (ap->rxconfig & ANEG_CFG_RF2)
3672 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
3673 if (ap->rxconfig & ANEG_CFG_NP)
3674 ap->flags |= MR_LP_ADV_NEXT_PAGE;
3675
3676 ap->link_time = ap->cur_time;
3677
3678 ap->flags ^= (MR_TOGGLE_TX);
3679 if (ap->rxconfig & 0x0008)
3680 ap->flags |= MR_TOGGLE_RX;
3681 if (ap->rxconfig & ANEG_CFG_NP)
3682 ap->flags |= MR_NP_RX;
3683 ap->flags |= MR_PAGE_RX;
3684
3685 ap->state = ANEG_STATE_COMPLETE_ACK;
3686 ret = ANEG_TIMER_ENAB;
3687 break;
3688
3689 case ANEG_STATE_COMPLETE_ACK:
3690 if (ap->ability_match != 0 &&
3691 ap->rxconfig == 0) {
3692 ap->state = ANEG_STATE_AN_ENABLE;
3693 break;
3694 }
3695 delta = ap->cur_time - ap->link_time;
3696 if (delta > ANEG_STATE_SETTLE_TIME) {
3697 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
3698 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3699 } else {
3700 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
3701 !(ap->flags & MR_NP_RX)) {
3702 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3703 } else {
3704 ret = ANEG_FAILED;
3705 }
3706 }
3707 }
3708 break;
3709
3710 case ANEG_STATE_IDLE_DETECT_INIT:
3711 ap->link_time = ap->cur_time;
3712 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3713 tw32_f(MAC_MODE, tp->mac_mode);
3714 udelay(40);
3715
3716 ap->state = ANEG_STATE_IDLE_DETECT;
3717 ret = ANEG_TIMER_ENAB;
3718 break;
3719
3720 case ANEG_STATE_IDLE_DETECT:
3721 if (ap->ability_match != 0 &&
3722 ap->rxconfig == 0) {
3723 ap->state = ANEG_STATE_AN_ENABLE;
3724 break;
3725 }
3726 delta = ap->cur_time - ap->link_time;
3727 if (delta > ANEG_STATE_SETTLE_TIME) {
3728 /* XXX another gem from the Broadcom driver :( */
3729 ap->state = ANEG_STATE_LINK_OK;
3730 }
3731 break;
3732
3733 case ANEG_STATE_LINK_OK:
3734 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
3735 ret = ANEG_DONE;
3736 break;
3737
3738 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
3739 /* ??? unimplemented */
3740 break;
3741
3742 case ANEG_STATE_NEXT_PAGE_WAIT:
3743 /* ??? unimplemented */
3744 break;
3745
3746 default:
3747 ret = ANEG_FAILED;
3748 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750
3751 return ret;
3752}
3753
Matt Carlson5be73b42007-12-20 20:09:29 -08003754static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755{
3756 int res = 0;
3757 struct tg3_fiber_aneginfo aninfo;
3758 int status = ANEG_FAILED;
3759 unsigned int tick;
3760 u32 tmp;
3761
3762 tw32_f(MAC_TX_AUTO_NEG, 0);
3763
3764 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
3765 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
3766 udelay(40);
3767
3768 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
3769 udelay(40);
3770
3771 memset(&aninfo, 0, sizeof(aninfo));
3772 aninfo.flags |= MR_AN_ENABLE;
3773 aninfo.state = ANEG_STATE_UNKNOWN;
3774 aninfo.cur_time = 0;
3775 tick = 0;
3776 while (++tick < 195000) {
3777 status = tg3_fiber_aneg_smachine(tp, &aninfo);
3778 if (status == ANEG_DONE || status == ANEG_FAILED)
3779 break;
3780
3781 udelay(1);
3782 }
3783
3784 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3785 tw32_f(MAC_MODE, tp->mac_mode);
3786 udelay(40);
3787
Matt Carlson5be73b42007-12-20 20:09:29 -08003788 *txflags = aninfo.txconfig;
3789 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790
3791 if (status == ANEG_DONE &&
3792 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
3793 MR_LP_ADV_FULL_DUPLEX)))
3794 res = 1;
3795
3796 return res;
3797}
3798
3799static void tg3_init_bcm8002(struct tg3 *tp)
3800{
3801 u32 mac_status = tr32(MAC_STATUS);
3802 int i;
3803
3804 /* Reset when initting first time or we have a link. */
3805 if ((tp->tg3_flags & TG3_FLAG_INIT_COMPLETE) &&
3806 !(mac_status & MAC_STATUS_PCS_SYNCED))
3807 return;
3808
3809 /* Set PLL lock range. */
3810 tg3_writephy(tp, 0x16, 0x8007);
3811
3812 /* SW reset */
3813 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
3814
3815 /* Wait for reset to complete. */
3816 /* XXX schedule_timeout() ... */
3817 for (i = 0; i < 500; i++)
3818 udelay(10);
3819
3820 /* Config mode; select PMA/Ch 1 regs. */
3821 tg3_writephy(tp, 0x10, 0x8411);
3822
3823 /* Enable auto-lock and comdet, select txclk for tx. */
3824 tg3_writephy(tp, 0x11, 0x0a10);
3825
3826 tg3_writephy(tp, 0x18, 0x00a0);
3827 tg3_writephy(tp, 0x16, 0x41ff);
3828
3829 /* Assert and deassert POR. */
3830 tg3_writephy(tp, 0x13, 0x0400);
3831 udelay(40);
3832 tg3_writephy(tp, 0x13, 0x0000);
3833
3834 tg3_writephy(tp, 0x11, 0x0a50);
3835 udelay(40);
3836 tg3_writephy(tp, 0x11, 0x0a10);
3837
3838 /* Wait for signal to stabilize */
3839 /* XXX schedule_timeout() ... */
3840 for (i = 0; i < 15000; i++)
3841 udelay(10);
3842
3843 /* Deselect the channel register so we can read the PHYID
3844 * later.
3845 */
3846 tg3_writephy(tp, 0x10, 0x8011);
3847}
3848
3849static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
3850{
Matt Carlson82cd3d12007-12-20 20:09:00 -08003851 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 u32 sg_dig_ctrl, sg_dig_status;
3853 u32 serdes_cfg, expected_sg_dig_ctrl;
3854 int workaround, port_a;
3855 int current_link_up;
3856
3857 serdes_cfg = 0;
3858 expected_sg_dig_ctrl = 0;
3859 workaround = 0;
3860 port_a = 1;
3861 current_link_up = 0;
3862
3863 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
3864 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
3865 workaround = 1;
3866 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
3867 port_a = 0;
3868
3869 /* preserve bits 0-11,13,14 for signal pre-emphasis */
3870 /* preserve bits 20-23 for voltage regulator */
3871 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
3872 }
3873
3874 sg_dig_ctrl = tr32(SG_DIG_CTRL);
3875
3876 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003877 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 if (workaround) {
3879 u32 val = serdes_cfg;
3880
3881 if (port_a)
3882 val |= 0xc010000;
3883 else
3884 val |= 0x4010000;
3885 tw32_f(MAC_SERDES_CFG, val);
3886 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003887
3888 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889 }
3890 if (mac_status & MAC_STATUS_PCS_SYNCED) {
3891 tg3_setup_flow_control(tp, 0, 0);
3892 current_link_up = 1;
3893 }
3894 goto out;
3895 }
3896
3897 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003898 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899
Matt Carlson82cd3d12007-12-20 20:09:00 -08003900 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3901 if (flowctrl & ADVERTISE_1000XPAUSE)
3902 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
3903 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3904 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905
3906 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003907 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07003908 tp->serdes_counter &&
3909 ((mac_status & (MAC_STATUS_PCS_SYNCED |
3910 MAC_STATUS_RCVD_CFG)) ==
3911 MAC_STATUS_PCS_SYNCED)) {
3912 tp->serdes_counter--;
3913 current_link_up = 1;
3914 goto out;
3915 }
3916restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 if (workaround)
3918 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003919 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920 udelay(5);
3921 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
3922
Michael Chan3d3ebe72006-09-27 15:59:15 -07003923 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003924 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
3926 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003927 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928 mac_status = tr32(MAC_STATUS);
3929
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003930 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08003932 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933
Matt Carlson82cd3d12007-12-20 20:09:00 -08003934 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
3935 local_adv |= ADVERTISE_1000XPAUSE;
3936 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
3937 local_adv |= ADVERTISE_1000XPSE_ASYM;
3938
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003939 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003940 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003941 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003942 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943
3944 tg3_setup_flow_control(tp, local_adv, remote_adv);
3945 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003946 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003947 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003948 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003949 if (tp->serdes_counter)
3950 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 else {
3952 if (workaround) {
3953 u32 val = serdes_cfg;
3954
3955 if (port_a)
3956 val |= 0xc010000;
3957 else
3958 val |= 0x4010000;
3959
3960 tw32_f(MAC_SERDES_CFG, val);
3961 }
3962
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003963 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 udelay(40);
3965
3966 /* Link parallel detection - link is up */
3967 /* only if we have PCS_SYNC and not */
3968 /* receiving config code words */
3969 mac_status = tr32(MAC_STATUS);
3970 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
3971 !(mac_status & MAC_STATUS_RCVD_CFG)) {
3972 tg3_setup_flow_control(tp, 0, 0);
3973 current_link_up = 1;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003974 tp->phy_flags |=
3975 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003976 tp->serdes_counter =
3977 SERDES_PARALLEL_DET_TIMEOUT;
3978 } else
3979 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 }
3981 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07003982 } else {
3983 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003984 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 }
3986
3987out:
3988 return current_link_up;
3989}
3990
3991static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
3992{
3993 int current_link_up = 0;
3994
Michael Chan5cf64b82007-05-05 12:11:21 -07003995 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997
3998 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08003999 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004001
Matt Carlson5be73b42007-12-20 20:09:29 -08004002 if (fiber_autoneg(tp, &txflags, &rxflags)) {
4003 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004
Matt Carlson5be73b42007-12-20 20:09:29 -08004005 if (txflags & ANEG_CFG_PS1)
4006 local_adv |= ADVERTISE_1000XPAUSE;
4007 if (txflags & ANEG_CFG_PS2)
4008 local_adv |= ADVERTISE_1000XPSE_ASYM;
4009
4010 if (rxflags & MR_LP_ADV_SYM_PAUSE)
4011 remote_adv |= LPA_1000XPAUSE;
4012 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
4013 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014
4015 tg3_setup_flow_control(tp, local_adv, remote_adv);
4016
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 current_link_up = 1;
4018 }
4019 for (i = 0; i < 30; i++) {
4020 udelay(20);
4021 tw32_f(MAC_STATUS,
4022 (MAC_STATUS_SYNC_CHANGED |
4023 MAC_STATUS_CFG_CHANGED));
4024 udelay(40);
4025 if ((tr32(MAC_STATUS) &
4026 (MAC_STATUS_SYNC_CHANGED |
4027 MAC_STATUS_CFG_CHANGED)) == 0)
4028 break;
4029 }
4030
4031 mac_status = tr32(MAC_STATUS);
4032 if (current_link_up == 0 &&
4033 (mac_status & MAC_STATUS_PCS_SYNCED) &&
4034 !(mac_status & MAC_STATUS_RCVD_CFG))
4035 current_link_up = 1;
4036 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08004037 tg3_setup_flow_control(tp, 0, 0);
4038
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 /* Forcing 1000FD link up. */
4040 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041
4042 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
4043 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004044
4045 tw32_f(MAC_MODE, tp->mac_mode);
4046 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 }
4048
4049out:
4050 return current_link_up;
4051}
4052
4053static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
4054{
4055 u32 orig_pause_cfg;
4056 u16 orig_active_speed;
4057 u8 orig_active_duplex;
4058 u32 mac_status;
4059 int current_link_up;
4060 int i;
4061
Matt Carlson8d018622007-12-20 20:05:44 -08004062 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 orig_active_speed = tp->link_config.active_speed;
4064 orig_active_duplex = tp->link_config.active_duplex;
4065
4066 if (!(tp->tg3_flags2 & TG3_FLG2_HW_AUTONEG) &&
4067 netif_carrier_ok(tp->dev) &&
4068 (tp->tg3_flags & TG3_FLAG_INIT_COMPLETE)) {
4069 mac_status = tr32(MAC_STATUS);
4070 mac_status &= (MAC_STATUS_PCS_SYNCED |
4071 MAC_STATUS_SIGNAL_DET |
4072 MAC_STATUS_CFG_CHANGED |
4073 MAC_STATUS_RCVD_CFG);
4074 if (mac_status == (MAC_STATUS_PCS_SYNCED |
4075 MAC_STATUS_SIGNAL_DET)) {
4076 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4077 MAC_STATUS_CFG_CHANGED));
4078 return 0;
4079 }
4080 }
4081
4082 tw32_f(MAC_TX_AUTO_NEG, 0);
4083
4084 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
4085 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
4086 tw32_f(MAC_MODE, tp->mac_mode);
4087 udelay(40);
4088
Matt Carlson79eb6902010-02-17 15:17:03 +00004089 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090 tg3_init_bcm8002(tp);
4091
4092 /* Enable link change event even when serdes polling. */
4093 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4094 udelay(40);
4095
4096 current_link_up = 0;
4097 mac_status = tr32(MAC_STATUS);
4098
4099 if (tp->tg3_flags2 & TG3_FLG2_HW_AUTONEG)
4100 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
4101 else
4102 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
4103
Matt Carlson898a56f2009-08-28 14:02:40 +00004104 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00004106 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107
4108 for (i = 0; i < 100; i++) {
4109 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4110 MAC_STATUS_CFG_CHANGED));
4111 udelay(5);
4112 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07004113 MAC_STATUS_CFG_CHANGED |
4114 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 break;
4116 }
4117
4118 mac_status = tr32(MAC_STATUS);
4119 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
4120 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004121 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
4122 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 tw32_f(MAC_MODE, (tp->mac_mode |
4124 MAC_MODE_SEND_CONFIGS));
4125 udelay(1);
4126 tw32_f(MAC_MODE, tp->mac_mode);
4127 }
4128 }
4129
4130 if (current_link_up == 1) {
4131 tp->link_config.active_speed = SPEED_1000;
4132 tp->link_config.active_duplex = DUPLEX_FULL;
4133 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4134 LED_CTRL_LNKLED_OVERRIDE |
4135 LED_CTRL_1000MBPS_ON));
4136 } else {
4137 tp->link_config.active_speed = SPEED_INVALID;
4138 tp->link_config.active_duplex = DUPLEX_INVALID;
4139 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4140 LED_CTRL_LNKLED_OVERRIDE |
4141 LED_CTRL_TRAFFIC_OVERRIDE));
4142 }
4143
4144 if (current_link_up != netif_carrier_ok(tp->dev)) {
4145 if (current_link_up)
4146 netif_carrier_on(tp->dev);
4147 else
4148 netif_carrier_off(tp->dev);
4149 tg3_link_report(tp);
4150 } else {
Matt Carlson8d018622007-12-20 20:05:44 -08004151 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 if (orig_pause_cfg != now_pause_cfg ||
4153 orig_active_speed != tp->link_config.active_speed ||
4154 orig_active_duplex != tp->link_config.active_duplex)
4155 tg3_link_report(tp);
4156 }
4157
4158 return 0;
4159}
4160
Michael Chan747e8f82005-07-25 12:33:22 -07004161static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
4162{
4163 int current_link_up, err = 0;
4164 u32 bmsr, bmcr;
4165 u16 current_speed;
4166 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08004167 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07004168
4169 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4170 tw32_f(MAC_MODE, tp->mac_mode);
4171 udelay(40);
4172
4173 tw32(MAC_EVENT, 0);
4174
4175 tw32_f(MAC_STATUS,
4176 (MAC_STATUS_SYNC_CHANGED |
4177 MAC_STATUS_CFG_CHANGED |
4178 MAC_STATUS_MI_COMPLETION |
4179 MAC_STATUS_LNKSTATE_CHANGED));
4180 udelay(40);
4181
4182 if (force_reset)
4183 tg3_phy_reset(tp);
4184
4185 current_link_up = 0;
4186 current_speed = SPEED_INVALID;
4187 current_duplex = DUPLEX_INVALID;
4188
4189 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4190 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004191 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
4192 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4193 bmsr |= BMSR_LSTATUS;
4194 else
4195 bmsr &= ~BMSR_LSTATUS;
4196 }
Michael Chan747e8f82005-07-25 12:33:22 -07004197
4198 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
4199
4200 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004201 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004202 /* do nothing, just check for link up at the end */
4203 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4204 u32 adv, new_adv;
4205
4206 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4207 new_adv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
4208 ADVERTISE_1000XPAUSE |
4209 ADVERTISE_1000XPSE_ASYM |
4210 ADVERTISE_SLCT);
4211
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004212 new_adv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Michael Chan747e8f82005-07-25 12:33:22 -07004213
4214 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
4215 new_adv |= ADVERTISE_1000XHALF;
4216 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
4217 new_adv |= ADVERTISE_1000XFULL;
4218
4219 if ((new_adv != adv) || !(bmcr & BMCR_ANENABLE)) {
4220 tg3_writephy(tp, MII_ADVERTISE, new_adv);
4221 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
4222 tg3_writephy(tp, MII_BMCR, bmcr);
4223
4224 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07004225 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004226 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004227
4228 return err;
4229 }
4230 } else {
4231 u32 new_bmcr;
4232
4233 bmcr &= ~BMCR_SPEED1000;
4234 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
4235
4236 if (tp->link_config.duplex == DUPLEX_FULL)
4237 new_bmcr |= BMCR_FULLDPLX;
4238
4239 if (new_bmcr != bmcr) {
4240 /* BMCR_SPEED1000 is a reserved bit that needs
4241 * to be set on write.
4242 */
4243 new_bmcr |= BMCR_SPEED1000;
4244
4245 /* Force a linkdown */
4246 if (netif_carrier_ok(tp->dev)) {
4247 u32 adv;
4248
4249 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4250 adv &= ~(ADVERTISE_1000XFULL |
4251 ADVERTISE_1000XHALF |
4252 ADVERTISE_SLCT);
4253 tg3_writephy(tp, MII_ADVERTISE, adv);
4254 tg3_writephy(tp, MII_BMCR, bmcr |
4255 BMCR_ANRESTART |
4256 BMCR_ANENABLE);
4257 udelay(10);
4258 netif_carrier_off(tp->dev);
4259 }
4260 tg3_writephy(tp, MII_BMCR, new_bmcr);
4261 bmcr = new_bmcr;
4262 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4263 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004264 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
4265 ASIC_REV_5714) {
4266 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4267 bmsr |= BMSR_LSTATUS;
4268 else
4269 bmsr &= ~BMSR_LSTATUS;
4270 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004271 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004272 }
4273 }
4274
4275 if (bmsr & BMSR_LSTATUS) {
4276 current_speed = SPEED_1000;
4277 current_link_up = 1;
4278 if (bmcr & BMCR_FULLDPLX)
4279 current_duplex = DUPLEX_FULL;
4280 else
4281 current_duplex = DUPLEX_HALF;
4282
Matt Carlsonef167e22007-12-20 20:10:01 -08004283 local_adv = 0;
4284 remote_adv = 0;
4285
Michael Chan747e8f82005-07-25 12:33:22 -07004286 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08004287 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07004288
4289 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
4290 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
4291 common = local_adv & remote_adv;
4292 if (common & (ADVERTISE_1000XHALF |
4293 ADVERTISE_1000XFULL)) {
4294 if (common & ADVERTISE_1000XFULL)
4295 current_duplex = DUPLEX_FULL;
4296 else
4297 current_duplex = DUPLEX_HALF;
Matt Carlson57d8b882010-06-05 17:24:35 +00004298 } else if (!(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
4299 /* Link is up via parallel detect */
Matt Carlson859a5882010-04-05 10:19:28 +00004300 } else {
Michael Chan747e8f82005-07-25 12:33:22 -07004301 current_link_up = 0;
Matt Carlson859a5882010-04-05 10:19:28 +00004302 }
Michael Chan747e8f82005-07-25 12:33:22 -07004303 }
4304 }
4305
Matt Carlsonef167e22007-12-20 20:10:01 -08004306 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
4307 tg3_setup_flow_control(tp, local_adv, remote_adv);
4308
Michael Chan747e8f82005-07-25 12:33:22 -07004309 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4310 if (tp->link_config.active_duplex == DUPLEX_HALF)
4311 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4312
4313 tw32_f(MAC_MODE, tp->mac_mode);
4314 udelay(40);
4315
4316 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4317
4318 tp->link_config.active_speed = current_speed;
4319 tp->link_config.active_duplex = current_duplex;
4320
4321 if (current_link_up != netif_carrier_ok(tp->dev)) {
4322 if (current_link_up)
4323 netif_carrier_on(tp->dev);
4324 else {
4325 netif_carrier_off(tp->dev);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004326 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004327 }
4328 tg3_link_report(tp);
4329 }
4330 return err;
4331}
4332
4333static void tg3_serdes_parallel_detect(struct tg3 *tp)
4334{
Michael Chan3d3ebe72006-09-27 15:59:15 -07004335 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07004336 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07004337 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07004338 return;
4339 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00004340
Michael Chan747e8f82005-07-25 12:33:22 -07004341 if (!netif_carrier_ok(tp->dev) &&
4342 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
4343 u32 bmcr;
4344
4345 tg3_readphy(tp, MII_BMCR, &bmcr);
4346 if (bmcr & BMCR_ANENABLE) {
4347 u32 phy1, phy2;
4348
4349 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004350 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
4351 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07004352
4353 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004354 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
4355 MII_TG3_DSP_EXP1_INT_STAT);
4356 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
4357 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07004358
4359 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
4360 /* We have signal detect and not receiving
4361 * config code words, link is up by parallel
4362 * detection.
4363 */
4364
4365 bmcr &= ~BMCR_ANENABLE;
4366 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
4367 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004368 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004369 }
4370 }
Matt Carlson859a5882010-04-05 10:19:28 +00004371 } else if (netif_carrier_ok(tp->dev) &&
4372 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004373 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004374 u32 phy2;
4375
4376 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004377 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
4378 MII_TG3_DSP_EXP1_INT_STAT);
4379 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07004380 if (phy2 & 0x20) {
4381 u32 bmcr;
4382
4383 /* Config code words received, turn on autoneg. */
4384 tg3_readphy(tp, MII_BMCR, &bmcr);
4385 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
4386
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004387 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004388
4389 }
4390 }
4391}
4392
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393static int tg3_setup_phy(struct tg3 *tp, int force_reset)
4394{
4395 int err;
4396
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004397 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004399 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07004400 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a5882010-04-05 10:19:28 +00004401 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403
Matt Carlsonbcb37f62008-11-03 16:52:09 -08004404 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08004405 u32 val, scale;
4406
4407 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
4408 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
4409 scale = 65;
4410 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
4411 scale = 6;
4412 else
4413 scale = 12;
4414
4415 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
4416 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
4417 tw32(GRC_MISC_CFG, val);
4418 }
4419
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 if (tp->link_config.active_speed == SPEED_1000 &&
4421 tp->link_config.active_duplex == DUPLEX_HALF)
4422 tw32(MAC_TX_LENGTHS,
4423 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4424 (6 << TX_LENGTHS_IPG_SHIFT) |
4425 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
4426 else
4427 tw32(MAC_TX_LENGTHS,
4428 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4429 (6 << TX_LENGTHS_IPG_SHIFT) |
4430 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
4431
4432 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
4433 if (netif_carrier_ok(tp->dev)) {
4434 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07004435 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 } else {
4437 tw32(HOSTCC_STAT_COAL_TICKS, 0);
4438 }
4439 }
4440
Matt Carlson8ed5d972007-05-07 00:25:49 -07004441 if (tp->tg3_flags & TG3_FLAG_ASPM_WORKAROUND) {
4442 u32 val = tr32(PCIE_PWR_MGMT_THRESH);
4443 if (!netif_carrier_ok(tp->dev))
4444 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
4445 tp->pwrmgmt_thresh;
4446 else
4447 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
4448 tw32(PCIE_PWR_MGMT_THRESH, val);
4449 }
4450
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 return err;
4452}
4453
Matt Carlson66cfd1b2010-09-30 10:34:30 +00004454static inline int tg3_irq_sync(struct tg3 *tp)
4455{
4456 return tp->irq_sync;
4457}
4458
Michael Chandf3e6542006-05-26 17:48:07 -07004459/* This is called whenever we suspect that the system chipset is re-
4460 * ordering the sequence of MMIO to the tx send mailbox. The symptom
4461 * is bogus tx completions. We try to recover by setting the
4462 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
4463 * in the workqueue.
4464 */
4465static void tg3_tx_recover(struct tg3 *tp)
4466{
4467 BUG_ON((tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER) ||
4468 tp->write32_tx_mbox == tg3_write_indirect_mbox);
4469
Matt Carlson5129c3a2010-04-05 10:19:23 +00004470 netdev_warn(tp->dev,
4471 "The system may be re-ordering memory-mapped I/O "
4472 "cycles to the network device, attempting to recover. "
4473 "Please report the problem to the driver maintainer "
4474 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07004475
4476 spin_lock(&tp->lock);
Michael Chandf3e6542006-05-26 17:48:07 -07004477 tp->tg3_flags |= TG3_FLAG_TX_RECOVERY_PENDING;
Michael Chandf3e6542006-05-26 17:48:07 -07004478 spin_unlock(&tp->lock);
4479}
4480
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004481static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07004482{
Matt Carlsonf65aac12010-08-02 11:26:03 +00004483 /* Tell compiler to fetch tx indices from memory. */
4484 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004485 return tnapi->tx_pending -
4486 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07004487}
4488
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489/* Tigon3 never reports partial packet sends. So we do not
4490 * need special logic to handle SKBs that have not had all
4491 * of their frags sent yet, like SunGEM does.
4492 */
Matt Carlson17375d22009-08-28 14:02:18 +00004493static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494{
Matt Carlson17375d22009-08-28 14:02:18 +00004495 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00004496 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004497 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004498 struct netdev_queue *txq;
4499 int index = tnapi - tp->napi;
4500
Matt Carlson19cfaec2009-12-03 08:36:20 +00004501 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004502 index--;
4503
4504 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505
4506 while (sw_idx != hw_idx) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00004507 struct ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07004509 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510
Michael Chandf3e6542006-05-26 17:48:07 -07004511 if (unlikely(skb == NULL)) {
4512 tg3_tx_recover(tp);
4513 return;
4514 }
4515
Alexander Duyckf4188d82009-12-02 16:48:38 +00004516 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004517 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00004518 skb_headlen(skb),
4519 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520
4521 ri->skb = NULL;
4522
4523 sw_idx = NEXT_TX(sw_idx);
4524
4525 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004526 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07004527 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
4528 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00004529
4530 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004531 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00004532 skb_shinfo(skb)->frags[i].size,
4533 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 sw_idx = NEXT_TX(sw_idx);
4535 }
4536
David S. Millerf47c11e2005-06-24 20:18:35 -07004537 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07004538
4539 if (unlikely(tx_bug)) {
4540 tg3_tx_recover(tp);
4541 return;
4542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543 }
4544
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004545 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546
Michael Chan1b2a7202006-08-07 21:46:02 -07004547 /* Need to make the tx_cons update visible to tg3_start_xmit()
4548 * before checking for netif_queue_stopped(). Without the
4549 * memory barrier, there is a small possibility that tg3_start_xmit()
4550 * will miss it and cause the queue to be stopped forever.
4551 */
4552 smp_mb();
4553
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004554 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004555 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004556 __netif_tx_lock(txq, smp_processor_id());
4557 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004558 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004559 netif_tx_wake_queue(txq);
4560 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07004561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562}
4563
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004564static void tg3_rx_skb_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
4565{
4566 if (!ri->skb)
4567 return;
4568
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004569 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004570 map_sz, PCI_DMA_FROMDEVICE);
4571 dev_kfree_skb_any(ri->skb);
4572 ri->skb = NULL;
4573}
4574
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575/* Returns size of skb allocated or < 0 on error.
4576 *
4577 * We only need to fill in the address because the other members
4578 * of the RX descriptor are invariant, see tg3_init_rings.
4579 *
4580 * Note the purposeful assymetry of cpu vs. chip accesses. For
4581 * posting buffers we only dirty the first cache line of the RX
4582 * descriptor (containing the address). Whereas for the RX status
4583 * buffers the cpu only reads the last cacheline of the RX descriptor
4584 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
4585 */
Matt Carlson86b21e52009-11-13 13:03:45 +00004586static int tg3_alloc_rx_skb(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Matt Carlsona3896162009-11-13 13:03:44 +00004587 u32 opaque_key, u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588{
4589 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00004590 struct ring_info *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591 struct sk_buff *skb;
4592 dma_addr_t mapping;
4593 int skb_size, dest_idx;
4594
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595 switch (opaque_key) {
4596 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00004597 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00004598 desc = &tpr->rx_std[dest_idx];
4599 map = &tpr->rx_std_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004600 skb_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601 break;
4602
4603 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00004604 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00004605 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00004606 map = &tpr->rx_jmb_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004607 skb_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 break;
4609
4610 default:
4611 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613
4614 /* Do not overwrite any of the map or rp information
4615 * until we are sure we can commit to a new buffer.
4616 *
4617 * Callers depend upon this behavior and assume that
4618 * we leave everything unchanged if we fail.
4619 */
Matt Carlson287be122009-08-28 13:58:46 +00004620 skb = netdev_alloc_skb(tp->dev, skb_size + tp->rx_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 if (skb == NULL)
4622 return -ENOMEM;
4623
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 skb_reserve(skb, tp->rx_offset);
4625
Matt Carlson287be122009-08-28 13:58:46 +00004626 mapping = pci_map_single(tp->pdev, skb->data, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627 PCI_DMA_FROMDEVICE);
Matt Carlsona21771d2009-11-02 14:25:31 +00004628 if (pci_dma_mapping_error(tp->pdev, mapping)) {
4629 dev_kfree_skb(skb);
4630 return -EIO;
4631 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632
4633 map->skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004634 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636 desc->addr_hi = ((u64)mapping >> 32);
4637 desc->addr_lo = ((u64)mapping & 0xffffffff);
4638
4639 return skb_size;
4640}
4641
4642/* We only need to move over in the address because the other
4643 * members of the RX descriptor are invariant. See notes above
4644 * tg3_alloc_rx_skb for full details.
4645 */
Matt Carlsona3896162009-11-13 13:03:44 +00004646static void tg3_recycle_rx(struct tg3_napi *tnapi,
4647 struct tg3_rx_prodring_set *dpr,
4648 u32 opaque_key, int src_idx,
4649 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650{
Matt Carlson17375d22009-08-28 14:02:18 +00004651 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
4653 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00004654 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00004655 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656
4657 switch (opaque_key) {
4658 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00004659 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00004660 dest_desc = &dpr->rx_std[dest_idx];
4661 dest_map = &dpr->rx_std_buffers[dest_idx];
4662 src_desc = &spr->rx_std[src_idx];
4663 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 break;
4665
4666 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00004667 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00004668 dest_desc = &dpr->rx_jmb[dest_idx].std;
4669 dest_map = &dpr->rx_jmb_buffers[dest_idx];
4670 src_desc = &spr->rx_jmb[src_idx].std;
4671 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672 break;
4673
4674 default:
4675 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677
4678 dest_map->skb = src_map->skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004679 dma_unmap_addr_set(dest_map, mapping,
4680 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 dest_desc->addr_hi = src_desc->addr_hi;
4682 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00004683
4684 /* Ensure that the update to the skb happens after the physical
4685 * addresses have been transferred to the new BD location.
4686 */
4687 smp_wmb();
4688
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 src_map->skb = NULL;
4690}
4691
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692/* The RX ring scheme is composed of multiple rings which post fresh
4693 * buffers to the chip, and one special ring the chip uses to report
4694 * status back to the host.
4695 *
4696 * The special ring reports the status of received packets to the
4697 * host. The chip does not write into the original descriptor the
4698 * RX buffer was obtained from. The chip simply takes the original
4699 * descriptor as provided by the host, updates the status and length
4700 * field, then writes this into the next status ring entry.
4701 *
4702 * Each ring the host uses to post buffers to the chip is described
4703 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
4704 * it is first placed into the on-chip ram. When the packet's length
4705 * is known, it walks down the TG3_BDINFO entries to select the ring.
4706 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
4707 * which is within the range of the new packet's length is chosen.
4708 *
4709 * The "separate ring for rx status" scheme may sound queer, but it makes
4710 * sense from a cache coherency perspective. If only the host writes
4711 * to the buffer post rings, and only the chip writes to the rx status
4712 * rings, then cache lines never move beyond shared-modified state.
4713 * If both the host and chip were to write into the same ring, cache line
4714 * eviction could occur since both entities want it in an exclusive state.
4715 */
Matt Carlson17375d22009-08-28 14:02:18 +00004716static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717{
Matt Carlson17375d22009-08-28 14:02:18 +00004718 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07004719 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00004720 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00004721 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07004722 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00004724 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004726 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727 /*
4728 * We need to order the read of hw_idx and the read of
4729 * the opaque cookie.
4730 */
4731 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732 work_mask = 0;
4733 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00004734 std_prod_idx = tpr->rx_std_prod_idx;
4735 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00004737 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00004738 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739 unsigned int len;
4740 struct sk_buff *skb;
4741 dma_addr_t dma_addr;
4742 u32 opaque_key, desc_idx, *post_ptr;
4743
4744 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
4745 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
4746 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00004747 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004748 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00004749 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00004750 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07004751 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00004753 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004754 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00004755 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00004756 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00004757 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759
4760 work_mask |= opaque_key;
4761
4762 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
4763 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
4764 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00004765 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 desc_idx, *post_ptr);
4767 drop_it_no_recycle:
4768 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00004769 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770 goto next_pkt;
4771 }
4772
Matt Carlsonad829262008-11-21 17:16:16 -08004773 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
4774 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775
Matt Carlsond2757fc2010-04-12 06:58:27 +00004776 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 int skb_size;
4778
Matt Carlson86b21e52009-11-13 13:03:45 +00004779 skb_size = tg3_alloc_rx_skb(tp, tpr, opaque_key,
Matt Carlsonafc081f2009-11-13 13:03:43 +00004780 *post_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 if (skb_size < 0)
4782 goto drop_it;
4783
Matt Carlson287be122009-08-28 13:58:46 +00004784 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785 PCI_DMA_FROMDEVICE);
4786
Matt Carlson61e800c2010-02-17 15:16:54 +00004787 /* Ensure that the update to the skb happens
4788 * after the usage of the old DMA mapping.
4789 */
4790 smp_wmb();
4791
4792 ri->skb = NULL;
4793
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 skb_put(skb, len);
4795 } else {
4796 struct sk_buff *copy_skb;
4797
Matt Carlsona3896162009-11-13 13:03:44 +00004798 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799 desc_idx, *post_ptr);
4800
Matt Carlsonbf933c82011-01-25 15:58:49 +00004801 copy_skb = netdev_alloc_skb(tp->dev, len +
Matt Carlson9dc7a112010-04-12 06:58:28 +00004802 TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803 if (copy_skb == NULL)
4804 goto drop_it_no_recycle;
4805
Matt Carlsonbf933c82011-01-25 15:58:49 +00004806 skb_reserve(copy_skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807 skb_put(copy_skb, len);
4808 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03004809 skb_copy_from_linear_data(skb, copy_skb->data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
4811
4812 /* We'll reuse the original ring buffer. */
4813 skb = copy_skb;
4814 }
4815
4816 if ((tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) &&
4817 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
4818 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
4819 >> RXD_TCPCSUM_SHIFT) == 0xffff))
4820 skb->ip_summed = CHECKSUM_UNNECESSARY;
4821 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07004822 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823
4824 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00004825
4826 if (len > (tp->dev->mtu + ETH_HLEN) &&
4827 skb->protocol != htons(ETH_P_8021Q)) {
4828 dev_kfree_skb(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00004829 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00004830 }
4831
Matt Carlson9dc7a112010-04-12 06:58:28 +00004832 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00004833 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
4834 __vlan_hwaccel_put_tag(skb,
4835 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00004836
Matt Carlsonbf933c82011-01-25 15:58:49 +00004837 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839 received++;
4840 budget--;
4841
4842next_pkt:
4843 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07004844
4845 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00004846 tpr->rx_std_prod_idx = std_prod_idx &
4847 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00004848 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
4849 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07004850 work_mask &= ~RXD_OPAQUE_RING_STD;
4851 rx_std_posted = 0;
4852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07004854 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00004855 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07004856
4857 /* Refresh hw_idx to see if there is new work */
4858 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004859 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07004860 rmb();
4861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862 }
4863
4864 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00004865 tnapi->rx_rcb_ptr = sw_idx;
4866 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867
4868 /* Refill RX ring(s). */
Matt Carlsone4af1af2010-02-12 14:47:05 +00004869 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004870 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00004871 tpr->rx_std_prod_idx = std_prod_idx &
4872 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004873 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
4874 tpr->rx_std_prod_idx);
4875 }
4876 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00004877 tpr->rx_jmb_prod_idx = jmb_prod_idx &
4878 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004879 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
4880 tpr->rx_jmb_prod_idx);
4881 }
4882 mmiowb();
4883 } else if (work_mask) {
4884 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
4885 * updated before the producer indices can be updated.
4886 */
4887 smp_wmb();
4888
Matt Carlson2c49a442010-09-30 10:34:35 +00004889 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
4890 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004891
Matt Carlsone4af1af2010-02-12 14:47:05 +00004892 if (tnapi != &tp->napi[1])
4893 napi_schedule(&tp->napi[1].napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895
4896 return received;
4897}
4898
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004899static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901 /* handle link change and other phy events */
4902 if (!(tp->tg3_flags &
4903 (TG3_FLAG_USE_LINKCHG_REG |
4904 TG3_FLAG_POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004905 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
4906
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 if (sblk->status & SD_STATUS_LINK_CHG) {
4908 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004909 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07004910 spin_lock(&tp->lock);
Matt Carlsondd477002008-05-25 23:45:58 -07004911 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
4912 tw32_f(MAC_STATUS,
4913 (MAC_STATUS_SYNC_CHANGED |
4914 MAC_STATUS_CFG_CHANGED |
4915 MAC_STATUS_MI_COMPLETION |
4916 MAC_STATUS_LNKSTATE_CHANGED));
4917 udelay(40);
4918 } else
4919 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07004920 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 }
4922 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004923}
4924
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004925static int tg3_rx_prodring_xfer(struct tg3 *tp,
4926 struct tg3_rx_prodring_set *dpr,
4927 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004928{
4929 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004930 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004931
4932 while (1) {
4933 src_prod_idx = spr->rx_std_prod_idx;
4934
4935 /* Make sure updates to the rx_std_buffers[] entries and the
4936 * standard producer index are seen in the correct order.
4937 */
4938 smp_rmb();
4939
4940 if (spr->rx_std_cons_idx == src_prod_idx)
4941 break;
4942
4943 if (spr->rx_std_cons_idx < src_prod_idx)
4944 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
4945 else
Matt Carlson2c49a442010-09-30 10:34:35 +00004946 cpycnt = tp->rx_std_ring_mask + 1 -
4947 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004948
Matt Carlson2c49a442010-09-30 10:34:35 +00004949 cpycnt = min(cpycnt,
4950 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004951
4952 si = spr->rx_std_cons_idx;
4953 di = dpr->rx_std_prod_idx;
4954
Matt Carlsone92967b2010-02-12 14:47:06 +00004955 for (i = di; i < di + cpycnt; i++) {
4956 if (dpr->rx_std_buffers[i].skb) {
4957 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004958 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00004959 break;
4960 }
4961 }
4962
4963 if (!cpycnt)
4964 break;
4965
4966 /* Ensure that updates to the rx_std_buffers ring and the
4967 * shadowed hardware producer ring from tg3_recycle_skb() are
4968 * ordered correctly WRT the skb check above.
4969 */
4970 smp_rmb();
4971
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004972 memcpy(&dpr->rx_std_buffers[di],
4973 &spr->rx_std_buffers[si],
4974 cpycnt * sizeof(struct ring_info));
4975
4976 for (i = 0; i < cpycnt; i++, di++, si++) {
4977 struct tg3_rx_buffer_desc *sbd, *dbd;
4978 sbd = &spr->rx_std[si];
4979 dbd = &dpr->rx_std[di];
4980 dbd->addr_hi = sbd->addr_hi;
4981 dbd->addr_lo = sbd->addr_lo;
4982 }
4983
Matt Carlson2c49a442010-09-30 10:34:35 +00004984 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
4985 tp->rx_std_ring_mask;
4986 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
4987 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004988 }
4989
4990 while (1) {
4991 src_prod_idx = spr->rx_jmb_prod_idx;
4992
4993 /* Make sure updates to the rx_jmb_buffers[] entries and
4994 * the jumbo producer index are seen in the correct order.
4995 */
4996 smp_rmb();
4997
4998 if (spr->rx_jmb_cons_idx == src_prod_idx)
4999 break;
5000
5001 if (spr->rx_jmb_cons_idx < src_prod_idx)
5002 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
5003 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005004 cpycnt = tp->rx_jmb_ring_mask + 1 -
5005 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005006
5007 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00005008 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005009
5010 si = spr->rx_jmb_cons_idx;
5011 di = dpr->rx_jmb_prod_idx;
5012
Matt Carlsone92967b2010-02-12 14:47:06 +00005013 for (i = di; i < di + cpycnt; i++) {
5014 if (dpr->rx_jmb_buffers[i].skb) {
5015 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005016 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005017 break;
5018 }
5019 }
5020
5021 if (!cpycnt)
5022 break;
5023
5024 /* Ensure that updates to the rx_jmb_buffers ring and the
5025 * shadowed hardware producer ring from tg3_recycle_skb() are
5026 * ordered correctly WRT the skb check above.
5027 */
5028 smp_rmb();
5029
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005030 memcpy(&dpr->rx_jmb_buffers[di],
5031 &spr->rx_jmb_buffers[si],
5032 cpycnt * sizeof(struct ring_info));
5033
5034 for (i = 0; i < cpycnt; i++, di++, si++) {
5035 struct tg3_rx_buffer_desc *sbd, *dbd;
5036 sbd = &spr->rx_jmb[si].std;
5037 dbd = &dpr->rx_jmb[di].std;
5038 dbd->addr_hi = sbd->addr_hi;
5039 dbd->addr_lo = sbd->addr_lo;
5040 }
5041
Matt Carlson2c49a442010-09-30 10:34:35 +00005042 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
5043 tp->rx_jmb_ring_mask;
5044 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
5045 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005046 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005047
5048 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005049}
5050
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005051static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
5052{
5053 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054
5055 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005056 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00005057 tg3_tx(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07005058 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
Michael Chan4fd7ab52007-10-12 01:39:50 -07005059 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060 }
5061
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062 /* run RX thread, within the bounds set by NAPI.
5063 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005064 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005066 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00005067 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005069 if ((tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005070 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005071 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00005072 u32 std_prod_idx = dpr->rx_std_prod_idx;
5073 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005074
Matt Carlsone4af1af2010-02-12 14:47:05 +00005075 for (i = 1; i < tp->irq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005076 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00005077 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005078
5079 wmb();
5080
Matt Carlsone4af1af2010-02-12 14:47:05 +00005081 if (std_prod_idx != dpr->rx_std_prod_idx)
5082 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5083 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005084
Matt Carlsone4af1af2010-02-12 14:47:05 +00005085 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
5086 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5087 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005088
5089 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005090
5091 if (err)
5092 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005093 }
5094
David S. Miller6f535762007-10-11 18:08:29 -07005095 return work_done;
5096}
David S. Millerf7383c22005-05-18 22:50:53 -07005097
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005098static int tg3_poll_msix(struct napi_struct *napi, int budget)
5099{
5100 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5101 struct tg3 *tp = tnapi->tp;
5102 int work_done = 0;
5103 struct tg3_hw_status *sblk = tnapi->hw_status;
5104
5105 while (1) {
5106 work_done = tg3_poll_work(tnapi, work_done, budget);
5107
5108 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
5109 goto tx_recovery;
5110
5111 if (unlikely(work_done >= budget))
5112 break;
5113
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005114 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005115 * to tell the hw how much work has been processed,
5116 * so we must read it before checking for more work.
5117 */
5118 tnapi->last_tag = sblk->status_tag;
5119 tnapi->last_irq_tag = tnapi->last_tag;
5120 rmb();
5121
5122 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00005123 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
5124 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005125 napi_complete(napi);
5126 /* Reenable interrupts. */
5127 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
5128 mmiowb();
5129 break;
5130 }
5131 }
5132
5133 return work_done;
5134
5135tx_recovery:
5136 /* work_done is guaranteed to be less than budget. */
5137 napi_complete(napi);
5138 schedule_work(&tp->reset_task);
5139 return work_done;
5140}
5141
David S. Miller6f535762007-10-11 18:08:29 -07005142static int tg3_poll(struct napi_struct *napi, int budget)
5143{
Matt Carlson8ef04422009-08-28 14:01:37 +00005144 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5145 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07005146 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00005147 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07005148
5149 while (1) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005150 tg3_poll_link(tp);
5151
Matt Carlson17375d22009-08-28 14:02:18 +00005152 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07005153
5154 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
5155 goto tx_recovery;
5156
5157 if (unlikely(work_done >= budget))
5158 break;
5159
Michael Chan4fd7ab52007-10-12 01:39:50 -07005160 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) {
Matt Carlson17375d22009-08-28 14:02:18 +00005161 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07005162 * to tell the hw how much work has been processed,
5163 * so we must read it before checking for more work.
5164 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005165 tnapi->last_tag = sblk->status_tag;
5166 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07005167 rmb();
5168 } else
5169 sblk->status &= ~SD_STATUS_UPDATED;
5170
Matt Carlson17375d22009-08-28 14:02:18 +00005171 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005172 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00005173 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07005174 break;
5175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176 }
5177
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005178 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07005179
5180tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07005181 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08005182 napi_complete(napi);
David S. Miller6f535762007-10-11 18:08:29 -07005183 schedule_work(&tp->reset_task);
Michael Chan4fd7ab52007-10-12 01:39:50 -07005184 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185}
5186
Matt Carlson66cfd1b2010-09-30 10:34:30 +00005187static void tg3_napi_disable(struct tg3 *tp)
5188{
5189 int i;
5190
5191 for (i = tp->irq_cnt - 1; i >= 0; i--)
5192 napi_disable(&tp->napi[i].napi);
5193}
5194
5195static void tg3_napi_enable(struct tg3 *tp)
5196{
5197 int i;
5198
5199 for (i = 0; i < tp->irq_cnt; i++)
5200 napi_enable(&tp->napi[i].napi);
5201}
5202
5203static void tg3_napi_init(struct tg3 *tp)
5204{
5205 int i;
5206
5207 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
5208 for (i = 1; i < tp->irq_cnt; i++)
5209 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
5210}
5211
5212static void tg3_napi_fini(struct tg3 *tp)
5213{
5214 int i;
5215
5216 for (i = 0; i < tp->irq_cnt; i++)
5217 netif_napi_del(&tp->napi[i].napi);
5218}
5219
5220static inline void tg3_netif_stop(struct tg3 *tp)
5221{
5222 tp->dev->trans_start = jiffies; /* prevent tx timeout */
5223 tg3_napi_disable(tp);
5224 netif_tx_disable(tp->dev);
5225}
5226
5227static inline void tg3_netif_start(struct tg3 *tp)
5228{
5229 /* NOTE: unconditional netif_tx_wake_all_queues is only
5230 * appropriate so long as all callers are assured to
5231 * have free tx slots (such as after tg3_init_hw)
5232 */
5233 netif_tx_wake_all_queues(tp->dev);
5234
5235 tg3_napi_enable(tp);
5236 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
5237 tg3_enable_ints(tp);
5238}
5239
David S. Millerf47c11e2005-06-24 20:18:35 -07005240static void tg3_irq_quiesce(struct tg3 *tp)
5241{
Matt Carlson4f125f42009-09-01 12:55:02 +00005242 int i;
5243
David S. Millerf47c11e2005-06-24 20:18:35 -07005244 BUG_ON(tp->irq_sync);
5245
5246 tp->irq_sync = 1;
5247 smp_mb();
5248
Matt Carlson4f125f42009-09-01 12:55:02 +00005249 for (i = 0; i < tp->irq_cnt; i++)
5250 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07005251}
5252
David S. Millerf47c11e2005-06-24 20:18:35 -07005253/* Fully shutdown all tg3 driver activity elsewhere in the system.
5254 * If irq_sync is non-zero, then the IRQ handler must be synchronized
5255 * with as well. Most of the time, this is not necessary except when
5256 * shutting down the device.
5257 */
5258static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
5259{
Michael Chan46966542007-07-11 19:47:19 -07005260 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07005261 if (irq_sync)
5262 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07005263}
5264
5265static inline void tg3_full_unlock(struct tg3 *tp)
5266{
David S. Millerf47c11e2005-06-24 20:18:35 -07005267 spin_unlock_bh(&tp->lock);
5268}
5269
Michael Chanfcfa0a32006-03-20 22:28:41 -08005270/* One-shot MSI handler - Chip automatically disables interrupt
5271 * after sending MSI so driver doesn't have to do it.
5272 */
David Howells7d12e782006-10-05 14:55:46 +01005273static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08005274{
Matt Carlson09943a12009-08-28 14:01:57 +00005275 struct tg3_napi *tnapi = dev_id;
5276 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08005277
Matt Carlson898a56f2009-08-28 14:02:40 +00005278 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005279 if (tnapi->rx_rcb)
5280 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005281
5282 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005283 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005284
5285 return IRQ_HANDLED;
5286}
5287
Michael Chan88b06bc2005-04-21 17:13:25 -07005288/* MSI ISR - No need to check for interrupt sharing and no need to
5289 * flush status block and interrupt mailbox. PCI ordering rules
5290 * guarantee that MSI will arrive after the status block.
5291 */
David Howells7d12e782006-10-05 14:55:46 +01005292static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc2005-04-21 17:13:25 -07005293{
Matt Carlson09943a12009-08-28 14:01:57 +00005294 struct tg3_napi *tnapi = dev_id;
5295 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc2005-04-21 17:13:25 -07005296
Matt Carlson898a56f2009-08-28 14:02:40 +00005297 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005298 if (tnapi->rx_rcb)
5299 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc2005-04-21 17:13:25 -07005300 /*
David S. Millerfac9b832005-05-18 22:46:34 -07005301 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc2005-04-21 17:13:25 -07005302 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07005303 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc2005-04-21 17:13:25 -07005304 * NIC to stop sending us irqs, engaging "in-intr-handler"
5305 * event coalescing.
5306 */
5307 tw32_mailbox(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07005308 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005309 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07005310
Michael Chan88b06bc2005-04-21 17:13:25 -07005311 return IRQ_RETVAL(1);
5312}
5313
David Howells7d12e782006-10-05 14:55:46 +01005314static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005315{
Matt Carlson09943a12009-08-28 14:01:57 +00005316 struct tg3_napi *tnapi = dev_id;
5317 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005318 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319 unsigned int handled = 1;
5320
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321 /* In INTx mode, it is possible for the interrupt to arrive at
5322 * the CPU before the status block posted prior to the interrupt.
5323 * Reading the PCI State register will confirm whether the
5324 * interrupt is ours and will flush the status block.
5325 */
Michael Chand18edcb2007-03-24 20:57:11 -07005326 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
5327 if ((tp->tg3_flags & TG3_FLAG_CHIP_RESETTING) ||
5328 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5329 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005330 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07005331 }
Michael Chand18edcb2007-03-24 20:57:11 -07005332 }
5333
5334 /*
5335 * Writing any value to intr-mbox-0 clears PCI INTA# and
5336 * chip-internal interrupt pending events.
5337 * Writing non-zero to intr-mbox-0 additional tells the
5338 * NIC to stop sending us irqs, engaging "in-intr-handler"
5339 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005340 *
5341 * Flush the mailbox to de-assert the IRQ immediately to prevent
5342 * spurious interrupts. The flush impacts performance but
5343 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005344 */
Michael Chanc04cb342007-05-07 00:26:15 -07005345 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07005346 if (tg3_irq_sync(tp))
5347 goto out;
5348 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00005349 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00005350 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00005351 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07005352 } else {
5353 /* No work, shared interrupt perhaps? re-enable
5354 * interrupts, and flush that PCI write
5355 */
5356 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
5357 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07005358 }
David S. Millerf47c11e2005-06-24 20:18:35 -07005359out:
David S. Millerfac9b832005-05-18 22:46:34 -07005360 return IRQ_RETVAL(handled);
5361}
5362
David Howells7d12e782006-10-05 14:55:46 +01005363static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07005364{
Matt Carlson09943a12009-08-28 14:01:57 +00005365 struct tg3_napi *tnapi = dev_id;
5366 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005367 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07005368 unsigned int handled = 1;
5369
David S. Millerfac9b832005-05-18 22:46:34 -07005370 /* In INTx mode, it is possible for the interrupt to arrive at
5371 * the CPU before the status block posted prior to the interrupt.
5372 * Reading the PCI State register will confirm whether the
5373 * interrupt is ours and will flush the status block.
5374 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005375 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Michael Chand18edcb2007-03-24 20:57:11 -07005376 if ((tp->tg3_flags & TG3_FLAG_CHIP_RESETTING) ||
5377 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5378 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005379 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380 }
Michael Chand18edcb2007-03-24 20:57:11 -07005381 }
5382
5383 /*
5384 * writing any value to intr-mbox-0 clears PCI INTA# and
5385 * chip-internal interrupt pending events.
5386 * writing non-zero to intr-mbox-0 additional tells the
5387 * NIC to stop sending us irqs, engaging "in-intr-handler"
5388 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005389 *
5390 * Flush the mailbox to de-assert the IRQ immediately to prevent
5391 * spurious interrupts. The flush impacts performance but
5392 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005393 */
Michael Chanc04cb342007-05-07 00:26:15 -07005394 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00005395
5396 /*
5397 * In a shared interrupt configuration, sometimes other devices'
5398 * interrupts will scream. We record the current status tag here
5399 * so that the above check can report that the screaming interrupts
5400 * are unhandled. Eventually they will be silenced.
5401 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005402 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00005403
Michael Chand18edcb2007-03-24 20:57:11 -07005404 if (tg3_irq_sync(tp))
5405 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00005406
Matt Carlson72334482009-08-28 14:03:01 +00005407 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00005408
Matt Carlson09943a12009-08-28 14:01:57 +00005409 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00005410
David S. Millerf47c11e2005-06-24 20:18:35 -07005411out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412 return IRQ_RETVAL(handled);
5413}
5414
Michael Chan79381092005-04-21 17:13:59 -07005415/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01005416static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07005417{
Matt Carlson09943a12009-08-28 14:01:57 +00005418 struct tg3_napi *tnapi = dev_id;
5419 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005420 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07005421
Michael Chanf9804dd2005-09-27 12:13:10 -07005422 if ((sblk->status & SD_STATUS_UPDATED) ||
5423 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07005424 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07005425 return IRQ_RETVAL(1);
5426 }
5427 return IRQ_RETVAL(0);
5428}
5429
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07005430static int tg3_init_hw(struct tg3 *, int);
Michael Chan944d9802005-05-29 14:57:48 -07005431static int tg3_halt(struct tg3 *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432
Michael Chanb9ec6c12006-07-25 16:37:27 -07005433/* Restart hardware after configuration changes, self-test, etc.
5434 * Invoked with tp->lock held.
5435 */
5436static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
Eric Dumazet78c61462008-04-24 23:33:06 -07005437 __releases(tp->lock)
5438 __acquires(tp->lock)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005439{
5440 int err;
5441
5442 err = tg3_init_hw(tp, reset_phy);
5443 if (err) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00005444 netdev_err(tp->dev,
5445 "Failed to re-initialize device, aborting\n");
Michael Chanb9ec6c12006-07-25 16:37:27 -07005446 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
5447 tg3_full_unlock(tp);
5448 del_timer_sync(&tp->timer);
5449 tp->irq_sync = 0;
Matt Carlsonfed97812009-09-01 13:10:19 +00005450 tg3_napi_enable(tp);
Michael Chanb9ec6c12006-07-25 16:37:27 -07005451 dev_close(tp->dev);
5452 tg3_full_lock(tp, 0);
5453 }
5454 return err;
5455}
5456
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457#ifdef CONFIG_NET_POLL_CONTROLLER
5458static void tg3_poll_controller(struct net_device *dev)
5459{
Matt Carlson4f125f42009-09-01 12:55:02 +00005460 int i;
Michael Chan88b06bc2005-04-21 17:13:25 -07005461 struct tg3 *tp = netdev_priv(dev);
5462
Matt Carlson4f125f42009-09-01 12:55:02 +00005463 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00005464 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465}
5466#endif
5467
David Howellsc4028952006-11-22 14:57:56 +00005468static void tg3_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469{
David Howellsc4028952006-11-22 14:57:56 +00005470 struct tg3 *tp = container_of(work, struct tg3, reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005471 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 unsigned int restart_timer;
5473
Michael Chan7faa0062006-02-02 17:29:28 -08005474 tg3_full_lock(tp, 0);
Michael Chan7faa0062006-02-02 17:29:28 -08005475
5476 if (!netif_running(tp->dev)) {
Michael Chan7faa0062006-02-02 17:29:28 -08005477 tg3_full_unlock(tp);
5478 return;
5479 }
5480
5481 tg3_full_unlock(tp);
5482
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005483 tg3_phy_stop(tp);
5484
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485 tg3_netif_stop(tp);
5486
David S. Millerf47c11e2005-06-24 20:18:35 -07005487 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488
5489 restart_timer = tp->tg3_flags2 & TG3_FLG2_RESTART_TIMER;
5490 tp->tg3_flags2 &= ~TG3_FLG2_RESTART_TIMER;
5491
Michael Chandf3e6542006-05-26 17:48:07 -07005492 if (tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING) {
5493 tp->write32_tx_mbox = tg3_write32_tx_mbox;
5494 tp->write32_rx_mbox = tg3_write_flush_reg32;
5495 tp->tg3_flags |= TG3_FLAG_MBOX_WRITE_REORDER;
5496 tp->tg3_flags &= ~TG3_FLAG_TX_RECOVERY_PENDING;
5497 }
5498
Michael Chan944d9802005-05-29 14:57:48 -07005499 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005500 err = tg3_init_hw(tp, 1);
5501 if (err)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005502 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503
5504 tg3_netif_start(tp);
5505
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506 if (restart_timer)
5507 mod_timer(&tp->timer, jiffies + 1);
Michael Chan7faa0062006-02-02 17:29:28 -08005508
Michael Chanb9ec6c12006-07-25 16:37:27 -07005509out:
Michael Chan7faa0062006-02-02 17:29:28 -08005510 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005511
5512 if (!err)
5513 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514}
5515
Michael Chanb0408752007-02-13 12:18:30 -08005516static void tg3_dump_short_state(struct tg3 *tp)
5517{
Joe Perches05dbe002010-02-17 19:44:19 +00005518 netdev_err(tp->dev, "DEBUG: MAC_TX_STATUS[%08x] MAC_RX_STATUS[%08x]\n",
5519 tr32(MAC_TX_STATUS), tr32(MAC_RX_STATUS));
5520 netdev_err(tp->dev, "DEBUG: RDMAC_STATUS[%08x] WDMAC_STATUS[%08x]\n",
5521 tr32(RDMAC_STATUS), tr32(WDMAC_STATUS));
Michael Chanb0408752007-02-13 12:18:30 -08005522}
5523
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524static void tg3_tx_timeout(struct net_device *dev)
5525{
5526 struct tg3 *tp = netdev_priv(dev);
5527
Michael Chanb0408752007-02-13 12:18:30 -08005528 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00005529 netdev_err(dev, "transmit timed out, resetting\n");
Michael Chanb0408752007-02-13 12:18:30 -08005530 tg3_dump_short_state(tp);
5531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532
5533 schedule_work(&tp->reset_task);
5534}
5535
Michael Chanc58ec932005-09-17 00:46:27 -07005536/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
5537static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
5538{
5539 u32 base = (u32) mapping & 0xffffffff;
5540
Eric Dumazet807540b2010-09-23 05:40:09 +00005541 return (base > 0xffffdcc0) && (base + len + 8 < base);
Michael Chanc58ec932005-09-17 00:46:27 -07005542}
5543
Michael Chan72f2afb2006-03-06 19:28:35 -08005544/* Test for DMA addresses > 40-bit */
5545static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
5546 int len)
5547{
5548#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Michael Chan6728a8e2006-03-27 23:16:49 -08005549 if (tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG)
Eric Dumazet807540b2010-09-23 05:40:09 +00005550 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08005551 return 0;
5552#else
5553 return 0;
5554#endif
5555}
5556
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005557static void tg3_set_txd(struct tg3_napi *, int, dma_addr_t, int, u32, u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558
Michael Chan72f2afb2006-03-06 19:28:35 -08005559/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00005560static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
5561 struct sk_buff *skb, u32 last_plus_one,
5562 u32 *start, u32 base_flags, u32 mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563{
Matt Carlson24f4efd2009-11-13 13:03:35 +00005564 struct tg3 *tp = tnapi->tp;
Matt Carlson41588ba2008-04-19 18:12:33 -07005565 struct sk_buff *new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07005566 dma_addr_t new_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567 u32 entry = *start;
Michael Chanc58ec932005-09-17 00:46:27 -07005568 int i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569
Matt Carlson41588ba2008-04-19 18:12:33 -07005570 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
5571 new_skb = skb_copy(skb, GFP_ATOMIC);
5572 else {
5573 int more_headroom = 4 - ((unsigned long)skb->data & 3);
5574
5575 new_skb = skb_copy_expand(skb,
5576 skb_headroom(skb) + more_headroom,
5577 skb_tailroom(skb), GFP_ATOMIC);
5578 }
5579
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07005581 ret = -1;
5582 } else {
5583 /* New SKB is guaranteed to be linear. */
5584 entry = *start;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005585 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
5586 PCI_DMA_TODEVICE);
5587 /* Make sure the mapping succeeded */
5588 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
5589 ret = -1;
5590 dev_kfree_skb(new_skb);
5591 new_skb = NULL;
David S. Miller90079ce2008-09-11 04:52:51 -07005592
Michael Chanc58ec932005-09-17 00:46:27 -07005593 /* Make sure new skb does not cross any 4G boundaries.
5594 * Drop the packet if it does.
5595 */
Alexander Duyckf4188d82009-12-02 16:48:38 +00005596 } else if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
5597 tg3_4g_overflow_test(new_addr, new_skb->len)) {
5598 pci_unmap_single(tp->pdev, new_addr, new_skb->len,
5599 PCI_DMA_TODEVICE);
Michael Chanc58ec932005-09-17 00:46:27 -07005600 ret = -1;
5601 dev_kfree_skb(new_skb);
5602 new_skb = NULL;
5603 } else {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005604 tg3_set_txd(tnapi, entry, new_addr, new_skb->len,
Michael Chanc58ec932005-09-17 00:46:27 -07005605 base_flags, 1 | (mss << 1));
5606 *start = NEXT_TX(entry);
5607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608 }
5609
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610 /* Now clean up the sw ring entries. */
5611 i = 0;
5612 while (entry != last_plus_one) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00005613 int len;
5614
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005615 if (i == 0)
Alexander Duyckf4188d82009-12-02 16:48:38 +00005616 len = skb_headlen(skb);
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005617 else
Alexander Duyckf4188d82009-12-02 16:48:38 +00005618 len = skb_shinfo(skb)->frags[i-1].size;
5619
5620 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005621 dma_unmap_addr(&tnapi->tx_buffers[entry],
Alexander Duyckf4188d82009-12-02 16:48:38 +00005622 mapping),
5623 len, PCI_DMA_TODEVICE);
5624 if (i == 0) {
5625 tnapi->tx_buffers[entry].skb = new_skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005626 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00005627 new_addr);
5628 } else {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005629 tnapi->tx_buffers[entry].skb = NULL;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005630 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631 entry = NEXT_TX(entry);
5632 i++;
5633 }
5634
5635 dev_kfree_skb(skb);
5636
Michael Chanc58ec932005-09-17 00:46:27 -07005637 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638}
5639
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005640static void tg3_set_txd(struct tg3_napi *tnapi, int entry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641 dma_addr_t mapping, int len, u32 flags,
5642 u32 mss_and_is_end)
5643{
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005644 struct tg3_tx_buffer_desc *txd = &tnapi->tx_ring[entry];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645 int is_end = (mss_and_is_end & 0x1);
5646 u32 mss = (mss_and_is_end >> 1);
5647 u32 vlan_tag = 0;
5648
5649 if (is_end)
5650 flags |= TXD_FLAG_END;
5651 if (flags & TXD_FLAG_VLAN) {
5652 vlan_tag = flags >> 16;
5653 flags &= 0xffff;
5654 }
5655 vlan_tag |= (mss << TXD_MSS_SHIFT);
5656
5657 txd->addr_hi = ((u64) mapping >> 32);
5658 txd->addr_lo = ((u64) mapping & 0xffffffff);
5659 txd->len_flags = (len << TXD_LEN_SHIFT) | flags;
5660 txd->vlan_tag = vlan_tag << TXD_VLAN_TAG_SHIFT;
5661}
5662
Michael Chan5a6f3072006-03-20 22:28:05 -08005663/* hard_start_xmit for devices that don't have any bugs and
Matt Carlsone849cdc2009-11-13 13:03:38 +00005664 * support TG3_FLG2_HW_TSO_2 and TG3_FLG2_HW_TSO_3 only.
Michael Chan5a6f3072006-03-20 22:28:05 -08005665 */
Stephen Hemminger613573252009-08-31 19:50:58 +00005666static netdev_tx_t tg3_start_xmit(struct sk_buff *skb,
5667 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668{
5669 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670 u32 len, entry, base_flags, mss;
David S. Miller90079ce2008-09-11 04:52:51 -07005671 dma_addr_t mapping;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005672 struct tg3_napi *tnapi;
5673 struct netdev_queue *txq;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005674 unsigned int i, last;
5675
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005676 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
5677 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Matt Carlson19cfaec2009-12-03 08:36:20 +00005678 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005679 tnapi++;
Michael Chan5a6f3072006-03-20 22:28:05 -08005680
Michael Chan00b70502006-06-17 21:58:45 -07005681 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005682 * and TX reclaim runs via tp->napi.poll inside of a software
Michael Chan5a6f3072006-03-20 22:28:05 -08005683 * interrupt. Furthermore, IRQ processing runs lockless so we have
5684 * no IRQ context deadlocks to worry about either. Rejoice!
5685 */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005686 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005687 if (!netif_tx_queue_stopped(txq)) {
5688 netif_tx_stop_queue(txq);
Michael Chan5a6f3072006-03-20 22:28:05 -08005689
5690 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00005691 netdev_err(dev,
5692 "BUG! Tx Ring full when queue awake!\n");
Michael Chan5a6f3072006-03-20 22:28:05 -08005693 }
Michael Chan5a6f3072006-03-20 22:28:05 -08005694 return NETDEV_TX_BUSY;
5695 }
5696
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005697 entry = tnapi->tx_prod;
Michael Chan5a6f3072006-03-20 22:28:05 -08005698 base_flags = 0;
Matt Carlsonbe98da62010-07-11 09:31:46 +00005699 mss = skb_shinfo(skb)->gso_size;
5700 if (mss) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005701 int tcp_opt_len, ip_tcp_len;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005702 u32 hdrlen;
Michael Chan5a6f3072006-03-20 22:28:05 -08005703
5704 if (skb_header_cloned(skb) &&
5705 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
5706 dev_kfree_skb(skb);
5707 goto out_unlock;
5708 }
5709
Matt Carlson02e96082010-09-15 08:59:59 +00005710 if (skb_is_gso_v6(skb)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005711 hdrlen = skb_headlen(skb) - ETH_HLEN;
Matt Carlson02e96082010-09-15 08:59:59 +00005712 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005713 struct iphdr *iph = ip_hdr(skb);
5714
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07005715 tcp_opt_len = tcp_optlen(skb);
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -03005716 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
Michael Chanb0026622006-07-03 19:42:14 -07005717
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005718 iph->check = 0;
5719 iph->tot_len = htons(mss + ip_tcp_len + tcp_opt_len);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005720 hdrlen = ip_tcp_len + tcp_opt_len;
Michael Chanb0026622006-07-03 19:42:14 -07005721 }
Michael Chan5a6f3072006-03-20 22:28:05 -08005722
Matt Carlsone849cdc2009-11-13 13:03:38 +00005723 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005724 mss |= (hdrlen & 0xc) << 12;
5725 if (hdrlen & 0x10)
5726 base_flags |= 0x00000010;
5727 base_flags |= (hdrlen & 0x3e0) << 5;
5728 } else
5729 mss |= hdrlen << 9;
5730
Michael Chan5a6f3072006-03-20 22:28:05 -08005731 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
5732 TXD_FLAG_CPU_POST_DMA);
5733
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005734 tcp_hdr(skb)->check = 0;
Michael Chan5a6f3072006-03-20 22:28:05 -08005735
Matt Carlson859a5882010-04-05 10:19:28 +00005736 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005737 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson859a5882010-04-05 10:19:28 +00005738 }
5739
Jesse Grosseab6d182010-10-20 13:56:03 +00005740 if (vlan_tx_tag_present(skb))
Michael Chan5a6f3072006-03-20 22:28:05 -08005741 base_flags |= (TXD_FLAG_VLAN |
5742 (vlan_tx_tag_get(skb) << 16));
Michael Chan5a6f3072006-03-20 22:28:05 -08005743
Alexander Duyckf4188d82009-12-02 16:48:38 +00005744 len = skb_headlen(skb);
5745
5746 /* Queue skb data, a.k.a. the main skb fragment. */
5747 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
5748 if (pci_dma_mapping_error(tp->pdev, mapping)) {
David S. Miller90079ce2008-09-11 04:52:51 -07005749 dev_kfree_skb(skb);
5750 goto out_unlock;
5751 }
5752
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005753 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005754 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005755
Matt Carlsonb703df62009-12-03 08:36:21 +00005756 if ((tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) &&
Matt Carlson8fc2f992010-12-06 08:28:49 +00005757 !mss && skb->len > VLAN_ETH_FRAME_LEN)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005758 base_flags |= TXD_FLAG_JMB_PKT;
5759
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005760 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
Michael Chan5a6f3072006-03-20 22:28:05 -08005761 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
5762
5763 entry = NEXT_TX(entry);
5764
5765 /* Now loop through additional data fragments, and queue them. */
5766 if (skb_shinfo(skb)->nr_frags > 0) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005767 last = skb_shinfo(skb)->nr_frags - 1;
5768 for (i = 0; i <= last; i++) {
5769 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5770
5771 len = frag->size;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005772 mapping = pci_map_page(tp->pdev,
5773 frag->page,
5774 frag->page_offset,
5775 len, PCI_DMA_TODEVICE);
5776 if (pci_dma_mapping_error(tp->pdev, mapping))
5777 goto dma_error;
5778
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005779 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005780 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00005781 mapping);
Michael Chan5a6f3072006-03-20 22:28:05 -08005782
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005783 tg3_set_txd(tnapi, entry, mapping, len,
Michael Chan5a6f3072006-03-20 22:28:05 -08005784 base_flags, (i == last) | (mss << 1));
5785
5786 entry = NEXT_TX(entry);
5787 }
5788 }
5789
5790 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005791 tw32_tx_mbox(tnapi->prodmbox, entry);
Michael Chan5a6f3072006-03-20 22:28:05 -08005792
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005793 tnapi->tx_prod = entry;
5794 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005795 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00005796
5797 /* netif_tx_stop_queue() must be done before checking
5798 * checking tx index in tg3_tx_avail() below, because in
5799 * tg3_tx(), we update tx index before checking for
5800 * netif_tx_queue_stopped().
5801 */
5802 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005803 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005804 netif_tx_wake_queue(txq);
Michael Chan5a6f3072006-03-20 22:28:05 -08005805 }
5806
5807out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00005808 mmiowb();
Michael Chan5a6f3072006-03-20 22:28:05 -08005809
5810 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005811
5812dma_error:
5813 last = i;
5814 entry = tnapi->tx_prod;
5815 tnapi->tx_buffers[entry].skb = NULL;
5816 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005817 dma_unmap_addr(&tnapi->tx_buffers[entry], mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005818 skb_headlen(skb),
5819 PCI_DMA_TODEVICE);
5820 for (i = 0; i <= last; i++) {
5821 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5822 entry = NEXT_TX(entry);
5823
5824 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005825 dma_unmap_addr(&tnapi->tx_buffers[entry],
Alexander Duyckf4188d82009-12-02 16:48:38 +00005826 mapping),
5827 frag->size, PCI_DMA_TODEVICE);
5828 }
5829
5830 dev_kfree_skb(skb);
5831 return NETDEV_TX_OK;
Michael Chan5a6f3072006-03-20 22:28:05 -08005832}
5833
Stephen Hemminger613573252009-08-31 19:50:58 +00005834static netdev_tx_t tg3_start_xmit_dma_bug(struct sk_buff *,
5835 struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07005836
5837/* Use GSO to workaround a rare TSO bug that may be triggered when the
5838 * TSO header is greater than 80 bytes.
5839 */
5840static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
5841{
5842 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005843 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07005844
5845 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005846 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07005847 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00005848
5849 /* netif_tx_stop_queue() must be done before checking
5850 * checking tx index in tg3_tx_avail() below, because in
5851 * tg3_tx(), we update tx index before checking for
5852 * netif_tx_queue_stopped().
5853 */
5854 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005855 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08005856 return NETDEV_TX_BUSY;
5857
5858 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07005859 }
5860
5861 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07005862 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07005863 goto tg3_tso_bug_end;
5864
5865 do {
5866 nskb = segs;
5867 segs = segs->next;
5868 nskb->next = NULL;
5869 tg3_start_xmit_dma_bug(nskb, tp->dev);
5870 } while (segs);
5871
5872tg3_tso_bug_end:
5873 dev_kfree_skb(skb);
5874
5875 return NETDEV_TX_OK;
5876}
Michael Chan52c0fd82006-06-29 20:15:54 -07005877
Michael Chan5a6f3072006-03-20 22:28:05 -08005878/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
5879 * support TG3_FLG2_HW_TSO_1 or firmware TSO only.
5880 */
Stephen Hemminger613573252009-08-31 19:50:58 +00005881static netdev_tx_t tg3_start_xmit_dma_bug(struct sk_buff *skb,
5882 struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08005883{
5884 struct tg3 *tp = netdev_priv(dev);
Michael Chan5a6f3072006-03-20 22:28:05 -08005885 u32 len, entry, base_flags, mss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005886 int would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07005887 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00005888 struct tg3_napi *tnapi;
5889 struct netdev_queue *txq;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005890 unsigned int i, last;
5891
Matt Carlson24f4efd2009-11-13 13:03:35 +00005892 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
5893 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Matt Carlson19cfaec2009-12-03 08:36:20 +00005894 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlson24f4efd2009-11-13 13:03:35 +00005895 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005896
Michael Chan00b70502006-06-17 21:58:45 -07005897 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005898 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07005899 * interrupt. Furthermore, IRQ processing runs lockless so we have
5900 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07005901 */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005902 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00005903 if (!netif_tx_queue_stopped(txq)) {
5904 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08005905
5906 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00005907 netdev_err(dev,
5908 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08005909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005910 return NETDEV_TX_BUSY;
5911 }
5912
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005913 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07005915 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005916 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00005917
Matt Carlsonbe98da62010-07-11 09:31:46 +00005918 mss = skb_shinfo(skb)->gso_size;
5919 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005920 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00005921 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922
5923 if (skb_header_cloned(skb) &&
5924 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
5925 dev_kfree_skb(skb);
5926 goto out_unlock;
5927 }
5928
Matt Carlson34195c32010-07-11 09:31:42 +00005929 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07005930 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005931
Matt Carlson02e96082010-09-15 08:59:59 +00005932 if (skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00005933 hdr_len = skb_headlen(skb) - ETH_HLEN;
5934 } else {
5935 u32 ip_tcp_len;
5936
5937 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
5938 hdr_len = ip_tcp_len + tcp_opt_len;
5939
5940 iph->check = 0;
5941 iph->tot_len = htons(mss + hdr_len);
5942 }
5943
Michael Chan52c0fd82006-06-29 20:15:54 -07005944 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Michael Chan7f62ad52007-02-20 23:25:40 -08005945 (tp->tg3_flags2 & TG3_FLG2_TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00005946 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07005947
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
5949 TXD_FLAG_CPU_POST_DMA);
5950
Linus Torvalds1da177e2005-04-16 15:20:36 -07005951 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005952 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005953 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005954 } else
5955 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
5956 iph->daddr, 0,
5957 IPPROTO_TCP,
5958 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005959
Matt Carlson615774f2009-11-13 13:03:39 +00005960 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) {
5961 mss |= (hdr_len & 0xc) << 12;
5962 if (hdr_len & 0x10)
5963 base_flags |= 0x00000010;
5964 base_flags |= (hdr_len & 0x3e0) << 5;
5965 } else if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_2)
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005966 mss |= hdr_len << 9;
5967 else if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_1) ||
5968 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005969 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005970 int tsflags;
5971
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005972 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005973 mss |= (tsflags << 11);
5974 }
5975 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005976 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977 int tsflags;
5978
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005979 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005980 base_flags |= tsflags << 12;
5981 }
5982 }
5983 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00005984
Jesse Grosseab6d182010-10-20 13:56:03 +00005985 if (vlan_tx_tag_present(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986 base_flags |= (TXD_FLAG_VLAN |
5987 (vlan_tx_tag_get(skb) << 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005988
Matt Carlsonb703df62009-12-03 08:36:21 +00005989 if ((tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) &&
Matt Carlson8fc2f992010-12-06 08:28:49 +00005990 !mss && skb->len > VLAN_ETH_FRAME_LEN)
Matt Carlson615774f2009-11-13 13:03:39 +00005991 base_flags |= TXD_FLAG_JMB_PKT;
5992
Alexander Duyckf4188d82009-12-02 16:48:38 +00005993 len = skb_headlen(skb);
5994
5995 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
5996 if (pci_dma_mapping_error(tp->pdev, mapping)) {
David S. Miller90079ce2008-09-11 04:52:51 -07005997 dev_kfree_skb(skb);
5998 goto out_unlock;
5999 }
6000
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006001 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006002 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006003
6004 would_hit_hwbug = 0;
6005
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006006 if ((tp->tg3_flags3 & TG3_FLG3_SHORT_DMA_BUG) && len <= 8)
6007 would_hit_hwbug = 1;
6008
Matt Carlson0e1406d2009-11-02 12:33:33 +00006009 if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
6010 tg3_4g_overflow_test(mapping, len))
Matt Carlson41588ba2008-04-19 18:12:33 -07006011 would_hit_hwbug = 1;
Matt Carlson0e1406d2009-11-02 12:33:33 +00006012
6013 if ((tp->tg3_flags3 & TG3_FLG3_40BIT_DMA_LIMIT_BUG) &&
6014 tg3_40bit_overflow_test(tp, mapping, len))
6015 would_hit_hwbug = 1;
6016
6017 if (tp->tg3_flags3 & TG3_FLG3_5701_DMA_BUG)
Michael Chanc58ec932005-09-17 00:46:27 -07006018 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006020 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006021 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
6022
6023 entry = NEXT_TX(entry);
6024
6025 /* Now loop through additional data fragments, and queue them. */
6026 if (skb_shinfo(skb)->nr_frags > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027 last = skb_shinfo(skb)->nr_frags - 1;
6028 for (i = 0; i <= last; i++) {
6029 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6030
6031 len = frag->size;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006032 mapping = pci_map_page(tp->pdev,
6033 frag->page,
6034 frag->page_offset,
6035 len, PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006037 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006038 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00006039 mapping);
6040 if (pci_dma_mapping_error(tp->pdev, mapping))
6041 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006042
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006043 if ((tp->tg3_flags3 & TG3_FLG3_SHORT_DMA_BUG) &&
6044 len <= 8)
6045 would_hit_hwbug = 1;
6046
Matt Carlson0e1406d2009-11-02 12:33:33 +00006047 if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
6048 tg3_4g_overflow_test(mapping, len))
Michael Chanc58ec932005-09-17 00:46:27 -07006049 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006050
Matt Carlson0e1406d2009-11-02 12:33:33 +00006051 if ((tp->tg3_flags3 & TG3_FLG3_40BIT_DMA_LIMIT_BUG) &&
6052 tg3_40bit_overflow_test(tp, mapping, len))
Michael Chan72f2afb2006-03-06 19:28:35 -08006053 would_hit_hwbug = 1;
6054
Linus Torvalds1da177e2005-04-16 15:20:36 -07006055 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006056 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006057 base_flags, (i == last)|(mss << 1));
6058 else
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006059 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006060 base_flags, (i == last));
6061
6062 entry = NEXT_TX(entry);
6063 }
6064 }
6065
6066 if (would_hit_hwbug) {
6067 u32 last_plus_one = entry;
6068 u32 start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069
Michael Chanc58ec932005-09-17 00:46:27 -07006070 start = entry - 1 - skb_shinfo(skb)->nr_frags;
6071 start &= (TG3_TX_RING_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072
6073 /* If the workaround fails due to memory/mapping
6074 * failure, silently drop this packet.
6075 */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006076 if (tigon3_dma_hwbug_workaround(tnapi, skb, last_plus_one,
Michael Chanc58ec932005-09-17 00:46:27 -07006077 &start, base_flags, mss))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006078 goto out_unlock;
6079
6080 entry = start;
6081 }
6082
6083 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006084 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006085
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006086 tnapi->tx_prod = entry;
6087 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006088 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006089
6090 /* netif_tx_stop_queue() must be done before checking
6091 * checking tx index in tg3_tx_avail() below, because in
6092 * tg3_tx(), we update tx index before checking for
6093 * netif_tx_queue_stopped().
6094 */
6095 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006096 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006097 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07006098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006099
6100out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00006101 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006102
6103 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006104
6105dma_error:
6106 last = i;
6107 entry = tnapi->tx_prod;
6108 tnapi->tx_buffers[entry].skb = NULL;
6109 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006110 dma_unmap_addr(&tnapi->tx_buffers[entry], mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006111 skb_headlen(skb),
6112 PCI_DMA_TODEVICE);
6113 for (i = 0; i <= last; i++) {
6114 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6115 entry = NEXT_TX(entry);
6116
6117 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006118 dma_unmap_addr(&tnapi->tx_buffers[entry],
Alexander Duyckf4188d82009-12-02 16:48:38 +00006119 mapping),
6120 frag->size, PCI_DMA_TODEVICE);
6121 }
6122
6123 dev_kfree_skb(skb);
6124 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006125}
6126
6127static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
6128 int new_mtu)
6129{
6130 dev->mtu = new_mtu;
6131
Michael Chanef7f5ec2005-07-25 12:32:25 -07006132 if (new_mtu > ETH_DATA_LEN) {
Michael Chana4e2b342005-10-26 15:46:52 -07006133 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
Michael Chanef7f5ec2005-07-25 12:32:25 -07006134 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
6135 ethtool_op_set_tso(dev, 0);
Matt Carlson859a5882010-04-05 10:19:28 +00006136 } else {
Michael Chanef7f5ec2005-07-25 12:32:25 -07006137 tp->tg3_flags |= TG3_FLAG_JUMBO_RING_ENABLE;
Matt Carlson859a5882010-04-05 10:19:28 +00006138 }
Michael Chanef7f5ec2005-07-25 12:32:25 -07006139 } else {
Michael Chana4e2b342005-10-26 15:46:52 -07006140 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
Michael Chanef7f5ec2005-07-25 12:32:25 -07006141 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
Michael Chan0f893dc2005-07-25 12:30:38 -07006142 tp->tg3_flags &= ~TG3_FLAG_JUMBO_RING_ENABLE;
Michael Chanef7f5ec2005-07-25 12:32:25 -07006143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006144}
6145
6146static int tg3_change_mtu(struct net_device *dev, int new_mtu)
6147{
6148 struct tg3 *tp = netdev_priv(dev);
Michael Chanb9ec6c12006-07-25 16:37:27 -07006149 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006150
6151 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
6152 return -EINVAL;
6153
6154 if (!netif_running(dev)) {
6155 /* We'll just catch it later when the
6156 * device is up'd.
6157 */
6158 tg3_set_mtu(dev, tp, new_mtu);
6159 return 0;
6160 }
6161
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006162 tg3_phy_stop(tp);
6163
Linus Torvalds1da177e2005-04-16 15:20:36 -07006164 tg3_netif_stop(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07006165
6166 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006167
Michael Chan944d9802005-05-29 14:57:48 -07006168 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006169
6170 tg3_set_mtu(dev, tp, new_mtu);
6171
Michael Chanb9ec6c12006-07-25 16:37:27 -07006172 err = tg3_restart_hw(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006173
Michael Chanb9ec6c12006-07-25 16:37:27 -07006174 if (!err)
6175 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006176
David S. Millerf47c11e2005-06-24 20:18:35 -07006177 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006178
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006179 if (!err)
6180 tg3_phy_start(tp);
6181
Michael Chanb9ec6c12006-07-25 16:37:27 -07006182 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006183}
6184
Matt Carlson21f581a2009-08-28 14:00:25 +00006185static void tg3_rx_prodring_free(struct tg3 *tp,
6186 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006187{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006188 int i;
6189
Matt Carlson8fea32b2010-09-15 08:59:58 +00006190 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006191 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00006192 i = (i + 1) & tp->rx_std_ring_mask)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006193 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6194 tp->rx_pkt_map_sz);
6195
6196 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
6197 for (i = tpr->rx_jmb_cons_idx;
6198 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00006199 i = (i + 1) & tp->rx_jmb_ring_mask) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006200 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6201 TG3_RX_JMB_MAP_SZ);
6202 }
6203 }
6204
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006205 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006207
Matt Carlson2c49a442010-09-30 10:34:35 +00006208 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006209 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6210 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211
Matt Carlson48035722010-10-14 10:37:43 +00006212 if ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) &&
6213 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006214 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006215 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6216 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217 }
6218}
6219
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006220/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006221 *
6222 * The chip has been shut down and the driver detached from
6223 * the networking, so no interrupts or new tx packets will
6224 * end up in the driver. tp->{tx,}lock are held and thus
6225 * we may not sleep.
6226 */
Matt Carlson21f581a2009-08-28 14:00:25 +00006227static int tg3_rx_prodring_alloc(struct tg3 *tp,
6228 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006229{
Matt Carlson287be122009-08-28 13:58:46 +00006230 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006231
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006232 tpr->rx_std_cons_idx = 0;
6233 tpr->rx_std_prod_idx = 0;
6234 tpr->rx_jmb_cons_idx = 0;
6235 tpr->rx_jmb_prod_idx = 0;
6236
Matt Carlson8fea32b2010-09-15 08:59:58 +00006237 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006238 memset(&tpr->rx_std_buffers[0], 0,
6239 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00006240 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006241 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00006242 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006243 goto done;
6244 }
6245
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00006247 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006248
Matt Carlson287be122009-08-28 13:58:46 +00006249 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Michael Chana4e2b342005-10-26 15:46:52 -07006250 if ((tp->tg3_flags2 & TG3_FLG2_5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00006251 tp->dev->mtu > ETH_DATA_LEN)
6252 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
6253 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07006254
Linus Torvalds1da177e2005-04-16 15:20:36 -07006255 /* Initialize invariants of the rings, we only set this
6256 * stuff once. This works because the card does not
6257 * write into the rx buffer posting rings.
6258 */
Matt Carlson2c49a442010-09-30 10:34:35 +00006259 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006260 struct tg3_rx_buffer_desc *rxd;
6261
Matt Carlson21f581a2009-08-28 14:00:25 +00006262 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00006263 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006264 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
6265 rxd->opaque = (RXD_OPAQUE_RING_STD |
6266 (i << RXD_OPAQUE_INDEX_SHIFT));
6267 }
6268
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006269 /* Now allocate fresh SKBs for each rx ring. */
6270 for (i = 0; i < tp->rx_pending; i++) {
Matt Carlson86b21e52009-11-13 13:03:45 +00006271 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_STD, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006272 netdev_warn(tp->dev,
6273 "Using a smaller RX standard ring. Only "
6274 "%d out of %d buffers were allocated "
6275 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006276 if (i == 0)
6277 goto initfail;
6278 tp->rx_pending = i;
6279 break;
6280 }
6281 }
6282
Matt Carlson48035722010-10-14 10:37:43 +00006283 if (!(tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) ||
6284 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006285 goto done;
6286
Matt Carlson2c49a442010-09-30 10:34:35 +00006287 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006288
Matt Carlson0d86df82010-02-17 15:17:00 +00006289 if (!(tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE))
6290 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006291
Matt Carlson2c49a442010-09-30 10:34:35 +00006292 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00006293 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006294
Matt Carlson0d86df82010-02-17 15:17:00 +00006295 rxd = &tpr->rx_jmb[i].std;
6296 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
6297 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
6298 RXD_FLAG_JUMBO;
6299 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
6300 (i << RXD_OPAQUE_INDEX_SHIFT));
6301 }
6302
6303 for (i = 0; i < tp->rx_jumbo_pending; i++) {
6304 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_JUMBO, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006305 netdev_warn(tp->dev,
6306 "Using a smaller RX jumbo ring. Only %d "
6307 "out of %d buffers were allocated "
6308 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00006309 if (i == 0)
6310 goto initfail;
6311 tp->rx_jumbo_pending = i;
6312 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006313 }
6314 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006315
6316done:
Michael Chan32d8c572006-07-25 16:38:29 -07006317 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006318
6319initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00006320 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006321 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006322}
6323
Matt Carlson21f581a2009-08-28 14:00:25 +00006324static void tg3_rx_prodring_fini(struct tg3 *tp,
6325 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006326{
Matt Carlson21f581a2009-08-28 14:00:25 +00006327 kfree(tpr->rx_std_buffers);
6328 tpr->rx_std_buffers = NULL;
6329 kfree(tpr->rx_jmb_buffers);
6330 tpr->rx_jmb_buffers = NULL;
6331 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006332 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
6333 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00006334 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006335 }
Matt Carlson21f581a2009-08-28 14:00:25 +00006336 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006337 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
6338 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00006339 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006340 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006341}
6342
Matt Carlson21f581a2009-08-28 14:00:25 +00006343static int tg3_rx_prodring_init(struct tg3 *tp,
6344 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006345{
Matt Carlson2c49a442010-09-30 10:34:35 +00006346 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
6347 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006348 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006349 return -ENOMEM;
6350
Matt Carlson4bae65c2010-11-24 08:31:52 +00006351 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
6352 TG3_RX_STD_RING_BYTES(tp),
6353 &tpr->rx_std_mapping,
6354 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006355 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006356 goto err_out;
6357
Matt Carlson48035722010-10-14 10:37:43 +00006358 if ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) &&
6359 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006360 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00006361 GFP_KERNEL);
6362 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006363 goto err_out;
6364
Matt Carlson4bae65c2010-11-24 08:31:52 +00006365 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
6366 TG3_RX_JMB_RING_BYTES(tp),
6367 &tpr->rx_jmb_mapping,
6368 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006369 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006370 goto err_out;
6371 }
6372
6373 return 0;
6374
6375err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00006376 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006377 return -ENOMEM;
6378}
6379
6380/* Free up pending packets in all rx/tx rings.
6381 *
6382 * The chip has been shut down and the driver detached from
6383 * the networking, so no interrupts or new tx packets will
6384 * end up in the driver. tp->{tx,}lock is not held and we are not
6385 * in an interrupt context and thus may sleep.
6386 */
6387static void tg3_free_rings(struct tg3 *tp)
6388{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006389 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006390
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006391 for (j = 0; j < tp->irq_cnt; j++) {
6392 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006393
Matt Carlson8fea32b2010-09-15 08:59:58 +00006394 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00006395
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006396 if (!tnapi->tx_buffers)
6397 continue;
6398
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006399 for (i = 0; i < TG3_TX_RING_SIZE; ) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00006400 struct ring_info *txp;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006401 struct sk_buff *skb;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006402 unsigned int k;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006403
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006404 txp = &tnapi->tx_buffers[i];
6405 skb = txp->skb;
6406
6407 if (skb == NULL) {
6408 i++;
6409 continue;
6410 }
6411
Alexander Duyckf4188d82009-12-02 16:48:38 +00006412 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006413 dma_unmap_addr(txp, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006414 skb_headlen(skb),
6415 PCI_DMA_TODEVICE);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006416 txp->skb = NULL;
6417
Alexander Duyckf4188d82009-12-02 16:48:38 +00006418 i++;
6419
6420 for (k = 0; k < skb_shinfo(skb)->nr_frags; k++) {
6421 txp = &tnapi->tx_buffers[i & (TG3_TX_RING_SIZE - 1)];
6422 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006423 dma_unmap_addr(txp, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006424 skb_shinfo(skb)->frags[k].size,
6425 PCI_DMA_TODEVICE);
6426 i++;
6427 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006428
6429 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006430 }
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006431 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006432}
6433
6434/* Initialize tx/rx rings for packet processing.
6435 *
6436 * The chip has been shut down and the driver detached from
6437 * the networking, so no interrupts or new tx packets will
6438 * end up in the driver. tp->{tx,}lock are held and thus
6439 * we may not sleep.
6440 */
6441static int tg3_init_rings(struct tg3 *tp)
6442{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006443 int i;
Matt Carlson72334482009-08-28 14:03:01 +00006444
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006445 /* Free up all the SKBs. */
6446 tg3_free_rings(tp);
6447
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006448 for (i = 0; i < tp->irq_cnt; i++) {
6449 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006450
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006451 tnapi->last_tag = 0;
6452 tnapi->last_irq_tag = 0;
6453 tnapi->hw_status->status = 0;
6454 tnapi->hw_status->status_tag = 0;
6455 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6456
6457 tnapi->tx_prod = 0;
6458 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006459 if (tnapi->tx_ring)
6460 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006461
6462 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006463 if (tnapi->rx_rcb)
6464 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006465
Matt Carlson8fea32b2010-09-15 08:59:58 +00006466 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00006467 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006468 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006469 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006470 }
Matt Carlson72334482009-08-28 14:03:01 +00006471
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006472 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006473}
6474
6475/*
6476 * Must not be invoked with interrupt sources disabled and
6477 * the hardware shutdown down.
6478 */
6479static void tg3_free_consistent(struct tg3 *tp)
6480{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006481 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006482
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006483 for (i = 0; i < tp->irq_cnt; i++) {
6484 struct tg3_napi *tnapi = &tp->napi[i];
6485
6486 if (tnapi->tx_ring) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006487 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006488 tnapi->tx_ring, tnapi->tx_desc_mapping);
6489 tnapi->tx_ring = NULL;
6490 }
6491
6492 kfree(tnapi->tx_buffers);
6493 tnapi->tx_buffers = NULL;
6494
6495 if (tnapi->rx_rcb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006496 dma_free_coherent(&tp->pdev->dev,
6497 TG3_RX_RCB_RING_BYTES(tp),
6498 tnapi->rx_rcb,
6499 tnapi->rx_rcb_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006500 tnapi->rx_rcb = NULL;
6501 }
6502
Matt Carlson8fea32b2010-09-15 08:59:58 +00006503 tg3_rx_prodring_fini(tp, &tnapi->prodring);
6504
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006505 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006506 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
6507 tnapi->hw_status,
6508 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006509 tnapi->hw_status = NULL;
6510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006511 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006512
Linus Torvalds1da177e2005-04-16 15:20:36 -07006513 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006514 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
6515 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006516 tp->hw_stats = NULL;
6517 }
6518}
6519
6520/*
6521 * Must not be invoked with interrupt sources disabled and
6522 * the hardware shutdown down. Can sleep.
6523 */
6524static int tg3_alloc_consistent(struct tg3 *tp)
6525{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006526 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006527
Matt Carlson4bae65c2010-11-24 08:31:52 +00006528 tp->hw_stats = dma_alloc_coherent(&tp->pdev->dev,
6529 sizeof(struct tg3_hw_stats),
6530 &tp->stats_mapping,
6531 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006532 if (!tp->hw_stats)
6533 goto err_out;
6534
Linus Torvalds1da177e2005-04-16 15:20:36 -07006535 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6536
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006537 for (i = 0; i < tp->irq_cnt; i++) {
6538 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006539 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006540
Matt Carlson4bae65c2010-11-24 08:31:52 +00006541 tnapi->hw_status = dma_alloc_coherent(&tp->pdev->dev,
6542 TG3_HW_STATUS_SIZE,
6543 &tnapi->status_mapping,
6544 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006545 if (!tnapi->hw_status)
6546 goto err_out;
6547
6548 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006549 sblk = tnapi->hw_status;
6550
Matt Carlson8fea32b2010-09-15 08:59:58 +00006551 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
6552 goto err_out;
6553
Matt Carlson19cfaec2009-12-03 08:36:20 +00006554 /* If multivector TSS is enabled, vector 0 does not handle
6555 * tx interrupts. Don't allocate any resources for it.
6556 */
6557 if ((!i && !(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)) ||
6558 (i && (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS))) {
6559 tnapi->tx_buffers = kzalloc(sizeof(struct ring_info) *
6560 TG3_TX_RING_SIZE,
6561 GFP_KERNEL);
6562 if (!tnapi->tx_buffers)
6563 goto err_out;
6564
Matt Carlson4bae65c2010-11-24 08:31:52 +00006565 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
6566 TG3_TX_RING_BYTES,
6567 &tnapi->tx_desc_mapping,
6568 GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00006569 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 Carlson4bae65c2010-11-24 08:31:52 +00006601 tnapi->rx_rcb = dma_alloc_coherent(&tp->pdev->dev,
6602 TG3_RX_RCB_RING_BYTES(tp),
6603 &tnapi->rx_rcb_mapping,
6604 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006605 if (!tnapi->rx_rcb)
6606 goto err_out;
6607
6608 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006609 }
6610
Linus Torvalds1da177e2005-04-16 15:20:36 -07006611 return 0;
6612
6613err_out:
6614 tg3_free_consistent(tp);
6615 return -ENOMEM;
6616}
6617
6618#define MAX_WAIT_CNT 1000
6619
6620/* To stop a block, clear the enable bit and poll till it
6621 * clears. tp->lock is held.
6622 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006623static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006624{
6625 unsigned int i;
6626 u32 val;
6627
6628 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
6629 switch (ofs) {
6630 case RCVLSC_MODE:
6631 case DMAC_MODE:
6632 case MBFREE_MODE:
6633 case BUFMGR_MODE:
6634 case MEMARB_MODE:
6635 /* We can't enable/disable these bits of the
6636 * 5705/5750, just say success.
6637 */
6638 return 0;
6639
6640 default:
6641 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006642 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006643 }
6644
6645 val = tr32(ofs);
6646 val &= ~enable_bit;
6647 tw32_f(ofs, val);
6648
6649 for (i = 0; i < MAX_WAIT_CNT; i++) {
6650 udelay(100);
6651 val = tr32(ofs);
6652 if ((val & enable_bit) == 0)
6653 break;
6654 }
6655
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006656 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00006657 dev_err(&tp->pdev->dev,
6658 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
6659 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006660 return -ENODEV;
6661 }
6662
6663 return 0;
6664}
6665
6666/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006667static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006668{
6669 int i, err;
6670
6671 tg3_disable_ints(tp);
6672
6673 tp->rx_mode &= ~RX_MODE_ENABLE;
6674 tw32_f(MAC_RX_MODE, tp->rx_mode);
6675 udelay(10);
6676
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006677 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
6678 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
6679 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
6680 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
6681 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
6682 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006683
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006684 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
6685 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
6686 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
6687 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
6688 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
6689 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
6690 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006691
6692 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
6693 tw32_f(MAC_MODE, tp->mac_mode);
6694 udelay(40);
6695
6696 tp->tx_mode &= ~TX_MODE_ENABLE;
6697 tw32_f(MAC_TX_MODE, tp->tx_mode);
6698
6699 for (i = 0; i < MAX_WAIT_CNT; i++) {
6700 udelay(100);
6701 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
6702 break;
6703 }
6704 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00006705 dev_err(&tp->pdev->dev,
6706 "%s timed out, TX_MODE_ENABLE will not clear "
6707 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07006708 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006709 }
6710
Michael Chane6de8ad2005-05-05 14:42:41 -07006711 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006712 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
6713 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006714
6715 tw32(FTQ_RESET, 0xffffffff);
6716 tw32(FTQ_RESET, 0x00000000);
6717
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006718 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
6719 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006720
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006721 for (i = 0; i < tp->irq_cnt; i++) {
6722 struct tg3_napi *tnapi = &tp->napi[i];
6723 if (tnapi->hw_status)
6724 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6725 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006726 if (tp->hw_stats)
6727 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6728
Linus Torvalds1da177e2005-04-16 15:20:36 -07006729 return err;
6730}
6731
Matt Carlson0d3031d2007-10-10 18:02:43 -07006732static void tg3_ape_send_event(struct tg3 *tp, u32 event)
6733{
6734 int i;
6735 u32 apedata;
6736
Matt Carlsondc6d0742010-09-15 08:59:55 +00006737 /* NCSI does not support APE events */
6738 if (tp->tg3_flags3 & TG3_FLG3_APE_HAS_NCSI)
6739 return;
6740
Matt Carlson0d3031d2007-10-10 18:02:43 -07006741 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
6742 if (apedata != APE_SEG_SIG_MAGIC)
6743 return;
6744
6745 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
Matt Carlson731fd792008-08-15 14:07:51 -07006746 if (!(apedata & APE_FW_STATUS_READY))
Matt Carlson0d3031d2007-10-10 18:02:43 -07006747 return;
6748
6749 /* Wait for up to 1 millisecond for APE to service previous event. */
6750 for (i = 0; i < 10; i++) {
6751 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
6752 return;
6753
6754 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
6755
6756 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6757 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
6758 event | APE_EVENT_STATUS_EVENT_PENDING);
6759
6760 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
6761
6762 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6763 break;
6764
6765 udelay(100);
6766 }
6767
6768 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6769 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
6770}
6771
6772static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
6773{
6774 u32 event;
6775 u32 apedata;
6776
6777 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
6778 return;
6779
6780 switch (kind) {
Matt Carlson33f401a2010-04-05 10:19:27 +00006781 case RESET_KIND_INIT:
6782 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
6783 APE_HOST_SEG_SIG_MAGIC);
6784 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
6785 APE_HOST_SEG_LEN_MAGIC);
6786 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
6787 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
6788 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
Matt Carlson6867c842010-07-11 09:31:44 +00006789 APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
Matt Carlson33f401a2010-04-05 10:19:27 +00006790 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
6791 APE_HOST_BEHAV_NO_PHYLOCK);
Matt Carlsondc6d0742010-09-15 08:59:55 +00006792 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
6793 TG3_APE_HOST_DRVR_STATE_START);
Matt Carlson0d3031d2007-10-10 18:02:43 -07006794
Matt Carlson33f401a2010-04-05 10:19:27 +00006795 event = APE_EVENT_STATUS_STATE_START;
6796 break;
6797 case RESET_KIND_SHUTDOWN:
6798 /* With the interface we are currently using,
6799 * APE does not track driver state. Wiping
6800 * out the HOST SEGMENT SIGNATURE forces
6801 * the APE to assume OS absent status.
6802 */
6803 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
Matt Carlsonb2aee152008-11-03 16:51:11 -08006804
Matt Carlsondc6d0742010-09-15 08:59:55 +00006805 if (device_may_wakeup(&tp->pdev->dev) &&
6806 (tp->tg3_flags & TG3_FLAG_WOL_ENABLE)) {
6807 tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
6808 TG3_APE_HOST_WOL_SPEED_AUTO);
6809 apedata = TG3_APE_HOST_DRVR_STATE_WOL;
6810 } else
6811 apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
6812
6813 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
6814
Matt Carlson33f401a2010-04-05 10:19:27 +00006815 event = APE_EVENT_STATUS_STATE_UNLOAD;
6816 break;
6817 case RESET_KIND_SUSPEND:
6818 event = APE_EVENT_STATUS_STATE_SUSPEND;
6819 break;
6820 default:
6821 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -07006822 }
6823
6824 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
6825
6826 tg3_ape_send_event(tp, event);
6827}
6828
Michael Chane6af3012005-04-21 17:12:05 -07006829/* tp->lock is held. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006830static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
6831{
David S. Millerf49639e2006-06-09 11:58:36 -07006832 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
6833 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006834
6835 if (tp->tg3_flags2 & TG3_FLG2_ASF_NEW_HANDSHAKE) {
6836 switch (kind) {
6837 case RESET_KIND_INIT:
6838 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6839 DRV_STATE_START);
6840 break;
6841
6842 case RESET_KIND_SHUTDOWN:
6843 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6844 DRV_STATE_UNLOAD);
6845 break;
6846
6847 case RESET_KIND_SUSPEND:
6848 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6849 DRV_STATE_SUSPEND);
6850 break;
6851
6852 default:
6853 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006855 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07006856
6857 if (kind == RESET_KIND_INIT ||
6858 kind == RESET_KIND_SUSPEND)
6859 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006860}
6861
6862/* tp->lock is held. */
6863static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
6864{
6865 if (tp->tg3_flags2 & TG3_FLG2_ASF_NEW_HANDSHAKE) {
6866 switch (kind) {
6867 case RESET_KIND_INIT:
6868 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6869 DRV_STATE_START_DONE);
6870 break;
6871
6872 case RESET_KIND_SHUTDOWN:
6873 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6874 DRV_STATE_UNLOAD_DONE);
6875 break;
6876
6877 default:
6878 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006880 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07006881
6882 if (kind == RESET_KIND_SHUTDOWN)
6883 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006884}
6885
6886/* tp->lock is held. */
6887static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
6888{
6889 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) {
6890 switch (kind) {
6891 case RESET_KIND_INIT:
6892 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6893 DRV_STATE_START);
6894 break;
6895
6896 case RESET_KIND_SHUTDOWN:
6897 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6898 DRV_STATE_UNLOAD);
6899 break;
6900
6901 case RESET_KIND_SUSPEND:
6902 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6903 DRV_STATE_SUSPEND);
6904 break;
6905
6906 default:
6907 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006909 }
6910}
6911
Michael Chan7a6f4362006-09-27 16:03:31 -07006912static int tg3_poll_fw(struct tg3 *tp)
6913{
6914 int i;
6915 u32 val;
6916
Michael Chanb5d37722006-09-27 16:06:21 -07006917 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Gary Zambrano0ccead12006-11-14 16:34:00 -08006918 /* Wait up to 20ms for init done. */
6919 for (i = 0; i < 200; i++) {
Michael Chanb5d37722006-09-27 16:06:21 -07006920 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
6921 return 0;
Gary Zambrano0ccead12006-11-14 16:34:00 -08006922 udelay(100);
Michael Chanb5d37722006-09-27 16:06:21 -07006923 }
6924 return -ENODEV;
6925 }
6926
Michael Chan7a6f4362006-09-27 16:03:31 -07006927 /* Wait for firmware initialization to complete. */
6928 for (i = 0; i < 100000; i++) {
6929 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
6930 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
6931 break;
6932 udelay(10);
6933 }
6934
6935 /* Chip might not be fitted with firmware. Some Sun onboard
6936 * parts are configured like that. So don't signal the timeout
6937 * of the above loop as an error, but do report the lack of
6938 * running firmware once.
6939 */
6940 if (i >= 100000 &&
6941 !(tp->tg3_flags2 & TG3_FLG2_NO_FWARE_REPORTED)) {
6942 tp->tg3_flags2 |= TG3_FLG2_NO_FWARE_REPORTED;
6943
Joe Perches05dbe002010-02-17 19:44:19 +00006944 netdev_info(tp->dev, "No firmware running\n");
Michael Chan7a6f4362006-09-27 16:03:31 -07006945 }
6946
Matt Carlson6b10c162010-02-12 14:47:08 +00006947 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
6948 /* The 57765 A0 needs a little more
6949 * time to do some important work.
6950 */
6951 mdelay(10);
6952 }
6953
Michael Chan7a6f4362006-09-27 16:03:31 -07006954 return 0;
6955}
6956
Michael Chanee6a99b2007-07-18 21:49:10 -07006957/* Save PCI command register before chip reset */
6958static void tg3_save_pci_state(struct tg3 *tp)
6959{
Matt Carlson8a6eac92007-10-21 16:17:55 -07006960 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07006961}
6962
6963/* Restore PCI state after chip reset */
6964static void tg3_restore_pci_state(struct tg3 *tp)
6965{
6966 u32 val;
6967
6968 /* Re-enable indirect register accesses. */
6969 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
6970 tp->misc_host_ctrl);
6971
6972 /* Set MAX PCI retry to zero. */
6973 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
6974 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
6975 (tp->tg3_flags & TG3_FLAG_PCIX_MODE))
6976 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07006977 /* Allow reads and writes to the APE register and memory space. */
6978 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
6979 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc2010-06-05 17:24:30 +00006980 PCISTATE_ALLOW_APE_SHMEM_WR |
6981 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07006982 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
6983
Matt Carlson8a6eac92007-10-21 16:17:55 -07006984 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07006985
Matt Carlsonfcb389d2008-11-03 16:55:44 -08006986 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785) {
6987 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)
Matt Carlsoncf790032010-11-24 08:31:48 +00006988 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlsonfcb389d2008-11-03 16:55:44 -08006989 else {
6990 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
6991 tp->pci_cacheline_sz);
6992 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
6993 tp->pci_lat_timer);
6994 }
Michael Chan114342f2007-10-15 02:12:26 -07006995 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08006996
Michael Chanee6a99b2007-07-18 21:49:10 -07006997 /* Make sure PCI-X relaxed ordering bit is clear. */
Matt Carlson52f44902008-11-21 17:17:04 -08006998 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
Matt Carlson9974a352007-10-07 23:27:28 -07006999 u16 pcix_cmd;
7000
7001 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7002 &pcix_cmd);
7003 pcix_cmd &= ~PCI_X_CMD_ERO;
7004 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7005 pcix_cmd);
7006 }
Michael Chanee6a99b2007-07-18 21:49:10 -07007007
7008 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007009
7010 /* Chip reset on 5780 will reset MSI enable bit,
7011 * so need to restore it.
7012 */
7013 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
7014 u16 ctrl;
7015
7016 pci_read_config_word(tp->pdev,
7017 tp->msi_cap + PCI_MSI_FLAGS,
7018 &ctrl);
7019 pci_write_config_word(tp->pdev,
7020 tp->msi_cap + PCI_MSI_FLAGS,
7021 ctrl | PCI_MSI_FLAGS_ENABLE);
7022 val = tr32(MSGINT_MODE);
7023 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
7024 }
7025 }
7026}
7027
Linus Torvalds1da177e2005-04-16 15:20:36 -07007028static void tg3_stop_fw(struct tg3 *);
7029
7030/* tp->lock is held. */
7031static int tg3_chip_reset(struct tg3 *tp)
7032{
7033 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07007034 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00007035 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007036
David S. Millerf49639e2006-06-09 11:58:36 -07007037 tg3_nvram_lock(tp);
7038
Matt Carlson77b483f2008-08-15 14:07:24 -07007039 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
7040
David S. Millerf49639e2006-06-09 11:58:36 -07007041 /* No matching tg3_nvram_unlock() after this because
7042 * chip reset below will undo the nvram lock.
7043 */
7044 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007045
Michael Chanee6a99b2007-07-18 21:49:10 -07007046 /* GRC_MISC_CFG core clock reset will clear the memory
7047 * enable bit in PCI register 4 and the MSI enable bit
7048 * on some chips, so we save relevant registers here.
7049 */
7050 tg3_save_pci_state(tp);
7051
Michael Chand9ab5ad2006-03-20 22:27:35 -08007052 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08007053 (tp->tg3_flags3 & TG3_FLG3_5755_PLUS))
Michael Chand9ab5ad2006-03-20 22:27:35 -08007054 tw32(GRC_FASTBOOT_PC, 0);
7055
Linus Torvalds1da177e2005-04-16 15:20:36 -07007056 /*
7057 * We must avoid the readl() that normally takes place.
7058 * It locks machines, causes machine checks, and other
7059 * fun things. So, temporarily disable the 5701
7060 * hardware workaround, while we do the reset.
7061 */
Michael Chan1ee582d2005-08-09 20:16:46 -07007062 write_op = tp->write32;
7063 if (write_op == tg3_write_flush_reg32)
7064 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007065
Michael Chand18edcb2007-03-24 20:57:11 -07007066 /* Prevent the irq handler from reading or writing PCI registers
7067 * during chip reset when the memory enable bit in the PCI command
7068 * register may be cleared. The chip does not generate interrupt
7069 * at this time, but the irq handler may still be called due to irq
7070 * sharing or irqpoll.
7071 */
7072 tp->tg3_flags |= TG3_FLAG_CHIP_RESETTING;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007073 for (i = 0; i < tp->irq_cnt; i++) {
7074 struct tg3_napi *tnapi = &tp->napi[i];
7075 if (tnapi->hw_status) {
7076 tnapi->hw_status->status = 0;
7077 tnapi->hw_status->status_tag = 0;
7078 }
7079 tnapi->last_tag = 0;
7080 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07007081 }
Michael Chand18edcb2007-03-24 20:57:11 -07007082 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00007083
7084 for (i = 0; i < tp->irq_cnt; i++)
7085 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07007086
Matt Carlson255ca312009-08-25 10:07:27 +00007087 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7088 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7089 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
7090 }
7091
Linus Torvalds1da177e2005-04-16 15:20:36 -07007092 /* do the reset */
7093 val = GRC_MISC_CFG_CORECLK_RESET;
7094
7095 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
Matt Carlson88075d92010-08-02 11:25:58 +00007096 /* Force PCIe 1.0a mode */
7097 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
7098 !(tp->tg3_flags3 & TG3_FLG3_5717_PLUS) &&
7099 tr32(TG3_PCIE_PHY_TSTCTL) ==
7100 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
7101 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
7102
Linus Torvalds1da177e2005-04-16 15:20:36 -07007103 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
7104 tw32(GRC_MISC_CFG, (1 << 29));
7105 val |= (1 << 29);
7106 }
7107 }
7108
Michael Chanb5d37722006-09-27 16:06:21 -07007109 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7110 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
7111 tw32(GRC_VCPU_EXT_CTRL,
7112 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
7113 }
7114
Matt Carlsonf37500d2010-08-02 11:25:59 +00007115 /* Manage gphy power for all CPMU absent PCIe devices. */
7116 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
7117 !(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007118 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00007119
Linus Torvalds1da177e2005-04-16 15:20:36 -07007120 tw32(GRC_MISC_CFG, val);
7121
Michael Chan1ee582d2005-08-09 20:16:46 -07007122 /* restore 5701 hardware bug workaround write method */
7123 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007124
7125 /* Unfortunately, we have to delay before the PCI read back.
7126 * Some 575X chips even will not respond to a PCI cfg access
7127 * when the reset command is given to the chip.
7128 *
7129 * How do these hardware designers expect things to work
7130 * properly if the PCI write is posted for a long period
7131 * of time? It is always necessary to have some method by
7132 * which a register read back can occur to push the write
7133 * out which does the reset.
7134 *
7135 * For most tg3 variants the trick below was working.
7136 * Ho hum...
7137 */
7138 udelay(120);
7139
7140 /* Flush PCI posted writes. The normal MMIO registers
7141 * are inaccessible at this time so this is the only
7142 * way to make this reliably (actually, this is no longer
7143 * the case, see above). I tried to use indirect
7144 * register read/write but this upset some 5701 variants.
7145 */
7146 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
7147
7148 udelay(120);
7149
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007150 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) && tp->pcie_cap) {
Matt Carlsone7126992009-08-25 10:08:16 +00007151 u16 val16;
7152
Linus Torvalds1da177e2005-04-16 15:20:36 -07007153 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
7154 int i;
7155 u32 cfg_val;
7156
7157 /* Wait for link training to complete. */
7158 for (i = 0; i < 5000; i++)
7159 udelay(100);
7160
7161 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
7162 pci_write_config_dword(tp->pdev, 0xc4,
7163 cfg_val | (1 << 15));
7164 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007165
Matt Carlsone7126992009-08-25 10:08:16 +00007166 /* Clear the "no snoop" and "relaxed ordering" bits. */
7167 pci_read_config_word(tp->pdev,
7168 tp->pcie_cap + PCI_EXP_DEVCTL,
7169 &val16);
7170 val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
7171 PCI_EXP_DEVCTL_NOSNOOP_EN);
7172 /*
7173 * Older PCIe devices only support the 128 byte
7174 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007175 */
Matt Carlson6de34cb2010-08-02 11:25:55 +00007176 if (!(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT))
Matt Carlsone7126992009-08-25 10:08:16 +00007177 val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007178 pci_write_config_word(tp->pdev,
7179 tp->pcie_cap + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007180 val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007181
Matt Carlsoncf790032010-11-24 08:31:48 +00007182 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007183
7184 /* Clear error status */
7185 pci_write_config_word(tp->pdev,
7186 tp->pcie_cap + PCI_EXP_DEVSTA,
7187 PCI_EXP_DEVSTA_CED |
7188 PCI_EXP_DEVSTA_NFED |
7189 PCI_EXP_DEVSTA_FED |
7190 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007191 }
7192
Michael Chanee6a99b2007-07-18 21:49:10 -07007193 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007194
Michael Chand18edcb2007-03-24 20:57:11 -07007195 tp->tg3_flags &= ~TG3_FLAG_CHIP_RESETTING;
7196
Michael Chanee6a99b2007-07-18 21:49:10 -07007197 val = 0;
7198 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
Michael Chan4cf78e42005-07-25 12:29:19 -07007199 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07007200 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007201
7202 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
7203 tg3_stop_fw(tp);
7204 tw32(0x5000, 0x400);
7205 }
7206
7207 tw32(GRC_MODE, tp->grc_mode);
7208
7209 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007210 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007211
7212 tw32(0xc4, val | (1 << 15));
7213 }
7214
7215 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
7216 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
7217 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
7218 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
7219 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
7220 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7221 }
7222
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007223 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
7224 tp->mac_mode = MAC_MODE_APE_TX_EN |
7225 MAC_MODE_APE_RX_EN |
7226 MAC_MODE_TDE_ENABLE;
7227
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007228 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007229 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
7230 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007231 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007232 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
7233 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007234 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007235 val = 0;
7236
7237 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007238 udelay(40);
7239
Matt Carlson77b483f2008-08-15 14:07:24 -07007240 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
7241
Michael Chan7a6f4362006-09-27 16:03:31 -07007242 err = tg3_poll_fw(tp);
7243 if (err)
7244 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007245
Matt Carlson0a9140c2009-08-28 12:27:50 +00007246 tg3_mdio_start(tp);
7247
Linus Torvalds1da177e2005-04-16 15:20:36 -07007248 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007249 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
7250 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonc885e822010-08-02 11:25:57 +00007251 !(tp->tg3_flags3 & TG3_FLG3_5717_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007252 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007253
7254 tw32(0x7c00, val | (1 << 25));
7255 }
7256
7257 /* Reprobe ASF enable state. */
7258 tp->tg3_flags &= ~TG3_FLAG_ENABLE_ASF;
7259 tp->tg3_flags2 &= ~TG3_FLG2_ASF_NEW_HANDSHAKE;
7260 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
7261 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
7262 u32 nic_cfg;
7263
7264 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
7265 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
7266 tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
Matt Carlson4ba526c2008-08-15 14:10:04 -07007267 tp->last_event_jiffies = jiffies;
John W. Linvillecbf46852005-04-21 17:01:29 -07007268 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007269 tp->tg3_flags2 |= TG3_FLG2_ASF_NEW_HANDSHAKE;
7270 }
7271 }
7272
7273 return 0;
7274}
7275
7276/* tp->lock is held. */
7277static void tg3_stop_fw(struct tg3 *tp)
7278{
Matt Carlson0d3031d2007-10-10 18:02:43 -07007279 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
7280 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07007281 /* Wait for RX cpu to ACK the previous event. */
7282 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007283
7284 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007285
7286 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007287
Matt Carlson7c5026a2008-05-02 16:49:29 -07007288 /* Wait for RX cpu to ACK this event. */
7289 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007290 }
7291}
7292
7293/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07007294static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007295{
7296 int err;
7297
7298 tg3_stop_fw(tp);
7299
Michael Chan944d9802005-05-29 14:57:48 -07007300 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007301
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007302 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007303 err = tg3_chip_reset(tp);
7304
Matt Carlsondaba2a62009-04-20 06:58:52 +00007305 __tg3_set_mac_addr(tp, 0);
7306
Michael Chan944d9802005-05-29 14:57:48 -07007307 tg3_write_sig_legacy(tp, kind);
7308 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007309
7310 if (err)
7311 return err;
7312
7313 return 0;
7314}
7315
Linus Torvalds1da177e2005-04-16 15:20:36 -07007316#define RX_CPU_SCRATCH_BASE 0x30000
7317#define RX_CPU_SCRATCH_SIZE 0x04000
7318#define TX_CPU_SCRATCH_BASE 0x34000
7319#define TX_CPU_SCRATCH_SIZE 0x04000
7320
7321/* tp->lock is held. */
7322static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
7323{
7324 int i;
7325
Eric Sesterhenn5d9428d2006-04-02 13:52:48 +02007326 BUG_ON(offset == TX_CPU_BASE &&
7327 (tp->tg3_flags2 & TG3_FLG2_5705_PLUS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007328
Michael Chanb5d37722006-09-27 16:06:21 -07007329 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7330 u32 val = tr32(GRC_VCPU_EXT_CTRL);
7331
7332 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
7333 return 0;
7334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007335 if (offset == RX_CPU_BASE) {
7336 for (i = 0; i < 10000; i++) {
7337 tw32(offset + CPU_STATE, 0xffffffff);
7338 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7339 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7340 break;
7341 }
7342
7343 tw32(offset + CPU_STATE, 0xffffffff);
7344 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
7345 udelay(10);
7346 } else {
7347 for (i = 0; i < 10000; i++) {
7348 tw32(offset + CPU_STATE, 0xffffffff);
7349 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7350 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7351 break;
7352 }
7353 }
7354
7355 if (i >= 10000) {
Joe Perches05dbe002010-02-17 19:44:19 +00007356 netdev_err(tp->dev, "%s timed out, %s CPU\n",
7357 __func__, offset == RX_CPU_BASE ? "RX" : "TX");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007358 return -ENODEV;
7359 }
Michael Chanec41c7d2006-01-17 02:40:55 -08007360
7361 /* Clear firmware's nvram arbitration. */
7362 if (tp->tg3_flags & TG3_FLAG_NVRAM)
7363 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007364 return 0;
7365}
7366
7367struct fw_info {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007368 unsigned int fw_base;
7369 unsigned int fw_len;
7370 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007371};
7372
7373/* tp->lock is held. */
7374static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base, u32 cpu_scratch_base,
7375 int cpu_scratch_size, struct fw_info *info)
7376{
Michael Chanec41c7d2006-01-17 02:40:55 -08007377 int err, lock_err, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007378 void (*write_op)(struct tg3 *, u32, u32);
7379
7380 if (cpu_base == TX_CPU_BASE &&
7381 (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007382 netdev_err(tp->dev,
7383 "%s: Trying to load TX cpu firmware which is 5705\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007384 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007385 return -EINVAL;
7386 }
7387
7388 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
7389 write_op = tg3_write_mem;
7390 else
7391 write_op = tg3_write_indirect_reg32;
7392
Michael Chan1b628152005-05-29 14:59:49 -07007393 /* It is possible that bootcode is still loading at this point.
7394 * Get the nvram lock first before halting the cpu.
7395 */
Michael Chanec41c7d2006-01-17 02:40:55 -08007396 lock_err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007397 err = tg3_halt_cpu(tp, cpu_base);
Michael Chanec41c7d2006-01-17 02:40:55 -08007398 if (!lock_err)
7399 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007400 if (err)
7401 goto out;
7402
7403 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
7404 write_op(tp, cpu_scratch_base + i, 0);
7405 tw32(cpu_base + CPU_STATE, 0xffffffff);
7406 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007407 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007408 write_op(tp, (cpu_scratch_base +
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007409 (info->fw_base & 0xffff) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07007410 (i * sizeof(u32))),
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007411 be32_to_cpu(info->fw_data[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007412
7413 err = 0;
7414
7415out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007416 return err;
7417}
7418
7419/* tp->lock is held. */
7420static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
7421{
7422 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007423 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007424 int err, i;
7425
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007426 fw_data = (void *)tp->fw->data;
7427
7428 /* Firmware blob starts with version numbers, followed by
7429 start address and length. We are setting complete length.
7430 length = end_address_of_bss - start_address_of_text.
7431 Remainder is the blob to be loaded contiguously
7432 from start address. */
7433
7434 info.fw_base = be32_to_cpu(fw_data[1]);
7435 info.fw_len = tp->fw->size - 12;
7436 info.fw_data = &fw_data[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007437
7438 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
7439 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
7440 &info);
7441 if (err)
7442 return err;
7443
7444 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
7445 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
7446 &info);
7447 if (err)
7448 return err;
7449
7450 /* Now startup only the RX cpu. */
7451 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007452 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007453
7454 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007455 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007456 break;
7457 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7458 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007459 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007460 udelay(1000);
7461 }
7462 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007463 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
7464 "should be %08x\n", __func__,
Joe Perches05dbe002010-02-17 19:44:19 +00007465 tr32(RX_CPU_BASE + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007466 return -ENODEV;
7467 }
7468 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7469 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
7470
7471 return 0;
7472}
7473
Linus Torvalds1da177e2005-04-16 15:20:36 -07007474/* 5705 needs a special version of the TSO firmware. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007475
7476/* tp->lock is held. */
7477static int tg3_load_tso_firmware(struct tg3 *tp)
7478{
7479 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007480 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007481 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
7482 int err, i;
7483
7484 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
7485 return 0;
7486
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007487 fw_data = (void *)tp->fw->data;
7488
7489 /* Firmware blob starts with version numbers, followed by
7490 start address and length. We are setting complete length.
7491 length = end_address_of_bss - start_address_of_text.
7492 Remainder is the blob to be loaded contiguously
7493 from start address. */
7494
7495 info.fw_base = be32_to_cpu(fw_data[1]);
7496 cpu_scratch_size = tp->fw_len;
7497 info.fw_len = tp->fw->size - 12;
7498 info.fw_data = &fw_data[3];
7499
Linus Torvalds1da177e2005-04-16 15:20:36 -07007500 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007501 cpu_base = RX_CPU_BASE;
7502 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007503 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007504 cpu_base = TX_CPU_BASE;
7505 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
7506 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
7507 }
7508
7509 err = tg3_load_firmware_cpu(tp, cpu_base,
7510 cpu_scratch_base, cpu_scratch_size,
7511 &info);
7512 if (err)
7513 return err;
7514
7515 /* Now startup the cpu. */
7516 tw32(cpu_base + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007517 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007518
7519 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007520 if (tr32(cpu_base + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007521 break;
7522 tw32(cpu_base + CPU_STATE, 0xffffffff);
7523 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007524 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007525 udelay(1000);
7526 }
7527 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007528 netdev_err(tp->dev,
7529 "%s fails to set CPU PC, is %08x should be %08x\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007530 __func__, tr32(cpu_base + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007531 return -ENODEV;
7532 }
7533 tw32(cpu_base + CPU_STATE, 0xffffffff);
7534 tw32_f(cpu_base + CPU_MODE, 0x00000000);
7535 return 0;
7536}
7537
Linus Torvalds1da177e2005-04-16 15:20:36 -07007538
Linus Torvalds1da177e2005-04-16 15:20:36 -07007539static int tg3_set_mac_addr(struct net_device *dev, void *p)
7540{
7541 struct tg3 *tp = netdev_priv(dev);
7542 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07007543 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007544
Michael Chanf9804dd2005-09-27 12:13:10 -07007545 if (!is_valid_ether_addr(addr->sa_data))
7546 return -EINVAL;
7547
Linus Torvalds1da177e2005-04-16 15:20:36 -07007548 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7549
Michael Chane75f7c92006-03-20 21:33:26 -08007550 if (!netif_running(dev))
7551 return 0;
7552
Michael Chan58712ef2006-04-29 18:58:01 -07007553 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) {
Michael Chan986e0ae2007-05-05 12:10:20 -07007554 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07007555
Michael Chan986e0ae2007-05-05 12:10:20 -07007556 addr0_high = tr32(MAC_ADDR_0_HIGH);
7557 addr0_low = tr32(MAC_ADDR_0_LOW);
7558 addr1_high = tr32(MAC_ADDR_1_HIGH);
7559 addr1_low = tr32(MAC_ADDR_1_LOW);
7560
7561 /* Skip MAC addr 1 if ASF is using it. */
7562 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
7563 !(addr1_high == 0 && addr1_low == 0))
7564 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07007565 }
Michael Chan986e0ae2007-05-05 12:10:20 -07007566 spin_lock_bh(&tp->lock);
7567 __tg3_set_mac_addr(tp, skip_mac_1);
7568 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007569
Michael Chanb9ec6c12006-07-25 16:37:27 -07007570 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007571}
7572
7573/* tp->lock is held. */
7574static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
7575 dma_addr_t mapping, u32 maxlen_flags,
7576 u32 nic_addr)
7577{
7578 tg3_write_mem(tp,
7579 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
7580 ((u64) mapping >> 32));
7581 tg3_write_mem(tp,
7582 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
7583 ((u64) mapping & 0xffffffff));
7584 tg3_write_mem(tp,
7585 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
7586 maxlen_flags);
7587
7588 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7589 tg3_write_mem(tp,
7590 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
7591 nic_addr);
7592}
7593
7594static void __tg3_set_rx_mode(struct net_device *);
Michael Chand244c892005-07-05 14:42:33 -07007595static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07007596{
Matt Carlsonb6080e12009-09-01 13:12:00 +00007597 int i;
7598
Matt Carlson19cfaec2009-12-03 08:36:20 +00007599 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007600 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
7601 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
7602 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007603 } else {
7604 tw32(HOSTCC_TXCOL_TICKS, 0);
7605 tw32(HOSTCC_TXMAX_FRAMES, 0);
7606 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007607 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007608
Matt Carlson20d73752010-07-11 09:31:41 +00007609 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00007610 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
7611 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
7612 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
7613 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007614 tw32(HOSTCC_RXCOL_TICKS, 0);
7615 tw32(HOSTCC_RXMAX_FRAMES, 0);
7616 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07007617 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007618
David S. Miller15f98502005-05-18 22:49:26 -07007619 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7620 u32 val = ec->stats_block_coalesce_usecs;
7621
Matt Carlsonb6080e12009-09-01 13:12:00 +00007622 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
7623 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
7624
David S. Miller15f98502005-05-18 22:49:26 -07007625 if (!netif_carrier_ok(tp->dev))
7626 val = 0;
7627
7628 tw32(HOSTCC_STAT_COAL_TICKS, val);
7629 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007630
7631 for (i = 0; i < tp->irq_cnt - 1; i++) {
7632 u32 reg;
7633
7634 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
7635 tw32(reg, ec->rx_coalesce_usecs);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007636 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
7637 tw32(reg, ec->rx_max_coalesced_frames);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007638 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
7639 tw32(reg, ec->rx_max_coalesced_frames_irq);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007640
7641 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS) {
7642 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
7643 tw32(reg, ec->tx_coalesce_usecs);
7644 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
7645 tw32(reg, ec->tx_max_coalesced_frames);
7646 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
7647 tw32(reg, ec->tx_max_coalesced_frames_irq);
7648 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007649 }
7650
7651 for (; i < tp->irq_max - 1; i++) {
7652 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007653 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007654 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007655
7656 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS) {
7657 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
7658 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
7659 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
7660 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007661 }
David S. Miller15f98502005-05-18 22:49:26 -07007662}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007663
7664/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00007665static void tg3_rings_reset(struct tg3 *tp)
7666{
7667 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007668 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007669 struct tg3_napi *tnapi = &tp->napi[0];
7670
7671 /* Disable all transmit rings but the first. */
7672 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7673 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlson3d377282010-10-14 10:37:39 +00007674 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
7675 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
7676 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
Matt Carlsonb703df62009-12-03 08:36:21 +00007677 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
7678 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007679 else
7680 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7681
7682 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7683 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
7684 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
7685 BDINFO_FLAGS_DISABLED);
7686
7687
7688 /* Disable all receive return rings but the first. */
Matt Carlsona50d0792010-06-05 17:24:37 +00007689 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
7690 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007691 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
7692 else if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00007693 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00007694 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
7695 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson2d31eca2009-09-01 12:53:31 +00007696 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
7697 else
7698 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7699
7700 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7701 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
7702 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
7703 BDINFO_FLAGS_DISABLED);
7704
7705 /* Disable interrupts */
7706 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
7707
7708 /* Zero mailbox registers. */
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007709 if (tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00007710 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007711 tp->napi[i].tx_prod = 0;
7712 tp->napi[i].tx_cons = 0;
Matt Carlsonc2353a32010-01-20 16:58:08 +00007713 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
7714 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007715 tw32_rx_mbox(tp->napi[i].consmbox, 0);
7716 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
7717 }
Matt Carlsonc2353a32010-01-20 16:58:08 +00007718 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS))
7719 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007720 } else {
7721 tp->napi[0].tx_prod = 0;
7722 tp->napi[0].tx_cons = 0;
7723 tw32_mailbox(tp->napi[0].prodmbox, 0);
7724 tw32_rx_mbox(tp->napi[0].consmbox, 0);
7725 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007726
7727 /* Make sure the NIC-based send BD rings are disabled. */
7728 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7729 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
7730 for (i = 0; i < 16; i++)
7731 tw32_tx_mbox(mbox + i * 8, 0);
7732 }
7733
7734 txrcb = NIC_SRAM_SEND_RCB;
7735 rxrcb = NIC_SRAM_RCV_RET_RCB;
7736
7737 /* Clear status block in ram. */
7738 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7739
7740 /* Set status block DMA address */
7741 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
7742 ((u64) tnapi->status_mapping >> 32));
7743 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
7744 ((u64) tnapi->status_mapping & 0xffffffff));
7745
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007746 if (tnapi->tx_ring) {
7747 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7748 (TG3_TX_RING_SIZE <<
7749 BDINFO_FLAGS_MAXLEN_SHIFT),
7750 NIC_SRAM_TX_BUFFER_DESC);
7751 txrcb += TG3_BDINFO_SIZE;
7752 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007753
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007754 if (tnapi->rx_rcb) {
7755 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00007756 (tp->rx_ret_ring_mask + 1) <<
7757 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007758 rxrcb += TG3_BDINFO_SIZE;
7759 }
7760
7761 stblk = HOSTCC_STATBLCK_RING1;
7762
7763 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
7764 u64 mapping = (u64)tnapi->status_mapping;
7765 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
7766 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
7767
7768 /* Clear status block in ram. */
7769 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7770
Matt Carlson19cfaec2009-12-03 08:36:20 +00007771 if (tnapi->tx_ring) {
7772 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7773 (TG3_TX_RING_SIZE <<
7774 BDINFO_FLAGS_MAXLEN_SHIFT),
7775 NIC_SRAM_TX_BUFFER_DESC);
7776 txrcb += TG3_BDINFO_SIZE;
7777 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007778
7779 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00007780 ((tp->rx_ret_ring_mask + 1) <<
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007781 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
7782
7783 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007784 rxrcb += TG3_BDINFO_SIZE;
7785 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007786}
7787
7788/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07007789static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007790{
7791 u32 val, rdmac_mode;
7792 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00007793 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007794
7795 tg3_disable_ints(tp);
7796
7797 tg3_stop_fw(tp);
7798
7799 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
7800
Matt Carlson859a5882010-04-05 10:19:28 +00007801 if (tp->tg3_flags & TG3_FLAG_INIT_COMPLETE)
Michael Chane6de8ad2005-05-05 14:42:41 -07007802 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007803
Matt Carlson699c0192010-12-06 08:28:51 +00007804 /* Enable MAC control of LPI */
7805 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
7806 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL,
7807 TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
7808 TG3_CPMU_EEE_LNKIDL_UART_IDL);
7809
7810 tw32_f(TG3_CPMU_EEE_CTRL,
7811 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
7812
Matt Carlsona386b902010-12-06 08:28:53 +00007813 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
7814 TG3_CPMU_EEEMD_LPI_IN_TX |
7815 TG3_CPMU_EEEMD_LPI_IN_RX |
7816 TG3_CPMU_EEEMD_EEE_ENABLE;
7817
7818 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
7819 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
7820
7821 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
7822 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
7823
7824 tw32_f(TG3_CPMU_EEE_MODE, val);
7825
7826 tw32_f(TG3_CPMU_EEE_DBTMR1,
7827 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
7828 TG3_CPMU_DBTMR1_LNKIDLE_2047US);
7829
7830 tw32_f(TG3_CPMU_EEE_DBTMR2,
Matt Carlsond7f2ab22011-01-25 15:58:56 +00007831 TG3_CPMU_DBTMR2_APE_TX_2047US |
Matt Carlsona386b902010-12-06 08:28:53 +00007832 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
Matt Carlson699c0192010-12-06 08:28:51 +00007833 }
7834
Matt Carlson603f1172010-02-12 14:47:10 +00007835 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08007836 tg3_phy_reset(tp);
7837
Linus Torvalds1da177e2005-04-16 15:20:36 -07007838 err = tg3_chip_reset(tp);
7839 if (err)
7840 return err;
7841
7842 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
7843
Matt Carlsonbcb37f62008-11-03 16:52:09 -08007844 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07007845 val = tr32(TG3_CPMU_CTRL);
7846 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
7847 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08007848
7849 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
7850 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
7851 val |= CPMU_LSPD_10MB_MACCLK_6_25;
7852 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
7853
7854 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
7855 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
7856 val |= CPMU_LNK_AWARE_MACCLK_6_25;
7857 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
7858
7859 val = tr32(TG3_CPMU_HST_ACC);
7860 val &= ~CPMU_HST_ACC_MACCLK_MASK;
7861 val |= CPMU_HST_ACC_MACCLK_6_25;
7862 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07007863 }
7864
Matt Carlson33466d92009-04-20 06:57:41 +00007865 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7866 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
7867 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
7868 PCIE_PWR_MGMT_L1_THRESH_4MS;
7869 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00007870
7871 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
7872 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
7873
7874 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00007875
Matt Carlsonf40386c2009-11-02 14:24:02 +00007876 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7877 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00007878 }
7879
Matt Carlson614b05902010-01-20 16:58:02 +00007880 if (tp->tg3_flags3 & TG3_FLG3_L1PLLPD_EN) {
7881 u32 grc_mode = tr32(GRC_MODE);
7882
7883 /* Access the lower 1K of PL PCIE block registers. */
7884 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
7885 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
7886
7887 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
7888 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
7889 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
7890
7891 tw32(GRC_MODE, grc_mode);
7892 }
7893
Matt Carlson5093eed2010-11-24 08:31:45 +00007894 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
7895 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
7896 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00007897
Matt Carlson5093eed2010-11-24 08:31:45 +00007898 /* Access the lower 1K of PL PCIE block registers. */
7899 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
7900 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00007901
Matt Carlson5093eed2010-11-24 08:31:45 +00007902 val = tr32(TG3_PCIE_TLDLPL_PORT +
7903 TG3_PCIE_PL_LO_PHYCTL5);
7904 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
7905 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00007906
Matt Carlson5093eed2010-11-24 08:31:45 +00007907 tw32(GRC_MODE, grc_mode);
7908 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00007909
7910 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
7911 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
7912 val |= CPMU_LSPD_10MB_MACCLK_6_25;
7913 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00007914 }
7915
Linus Torvalds1da177e2005-04-16 15:20:36 -07007916 /* This works around an issue with Athlon chipsets on
7917 * B3 tigon3 silicon. This bit has no effect on any
7918 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07007919 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007920 */
Matt Carlson795d01c2007-10-07 23:28:17 -07007921 if (!(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT)) {
7922 if (!(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
7923 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
7924 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007926
7927 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
7928 (tp->tg3_flags & TG3_FLAG_PCIX_MODE)) {
7929 val = tr32(TG3PCI_PCISTATE);
7930 val |= PCISTATE_RETRY_SAME_DMA;
7931 tw32(TG3PCI_PCISTATE, val);
7932 }
7933
Matt Carlson0d3031d2007-10-10 18:02:43 -07007934 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
7935 /* Allow reads and writes to the
7936 * APE register and memory space.
7937 */
7938 val = tr32(TG3PCI_PCISTATE);
7939 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc2010-06-05 17:24:30 +00007940 PCISTATE_ALLOW_APE_SHMEM_WR |
7941 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07007942 tw32(TG3PCI_PCISTATE, val);
7943 }
7944
Linus Torvalds1da177e2005-04-16 15:20:36 -07007945 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
7946 /* Enable some hw fixes. */
7947 val = tr32(TG3PCI_MSI_DATA);
7948 val |= (1 << 26) | (1 << 28) | (1 << 29);
7949 tw32(TG3PCI_MSI_DATA, val);
7950 }
7951
7952 /* Descriptor ring init may make accesses to the
7953 * NIC SRAM area to setup the TX descriptors, so we
7954 * can only do this after the hardware has been
7955 * successfully reset.
7956 */
Michael Chan32d8c572006-07-25 16:38:29 -07007957 err = tg3_init_rings(tp);
7958 if (err)
7959 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007960
Matt Carlsonc885e822010-08-02 11:25:57 +00007961 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00007962 val = tr32(TG3PCI_DMA_RW_CTRL) &
7963 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Matt Carlson1a319022010-04-12 06:58:25 +00007964 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
7965 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00007966 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
7967 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
7968 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07007969 /* This value is determined during the probe time DMA
7970 * engine test, tg3_test_dma.
7971 */
7972 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
7973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007974
7975 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
7976 GRC_MODE_4X_NIC_SEND_RINGS |
7977 GRC_MODE_NO_TX_PHDR_CSUM |
7978 GRC_MODE_NO_RX_PHDR_CSUM);
7979 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07007980
7981 /* Pseudo-header checksum is done by hardware logic and not
7982 * the offload processers, so make the chip do the pseudo-
7983 * header checksums on receive. For transmit it is more
7984 * convenient to do the pseudo-header checksum in software
7985 * as Linux does that on transmit for us in all cases.
7986 */
7987 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007988
7989 tw32(GRC_MODE,
7990 tp->grc_mode |
7991 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
7992
7993 /* Setup the timer prescalar register. Clock is always 66Mhz. */
7994 val = tr32(GRC_MISC_CFG);
7995 val &= ~0xff;
7996 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
7997 tw32(GRC_MISC_CFG, val);
7998
7999 /* Initialize MBUF/DESC pool. */
John W. Linvillecbf46852005-04-21 17:01:29 -07008000 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008001 /* Do nothing. */
8002 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
8003 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
8004 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
8005 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
8006 else
8007 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
8008 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
8009 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Matt Carlson859a5882010-04-05 10:19:28 +00008010 } else if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008011 int fw_len;
8012
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08008013 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008014 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
8015 tw32(BUFMGR_MB_POOL_ADDR,
8016 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
8017 tw32(BUFMGR_MB_POOL_SIZE,
8018 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
8019 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008020
Michael Chan0f893dc2005-07-25 12:30:38 -07008021 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008022 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8023 tp->bufmgr_config.mbuf_read_dma_low_water);
8024 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8025 tp->bufmgr_config.mbuf_mac_rx_low_water);
8026 tw32(BUFMGR_MB_HIGH_WATER,
8027 tp->bufmgr_config.mbuf_high_water);
8028 } else {
8029 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8030 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
8031 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8032 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
8033 tw32(BUFMGR_MB_HIGH_WATER,
8034 tp->bufmgr_config.mbuf_high_water_jumbo);
8035 }
8036 tw32(BUFMGR_DMA_LOW_WATER,
8037 tp->bufmgr_config.dma_low_water);
8038 tw32(BUFMGR_DMA_HIGH_WATER,
8039 tp->bufmgr_config.dma_high_water);
8040
Matt Carlsond309a462010-09-30 10:34:31 +00008041 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
8042 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
8043 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
8044 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008045 for (i = 0; i < 2000; i++) {
8046 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
8047 break;
8048 udelay(10);
8049 }
8050 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00008051 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008052 return -ENODEV;
8053 }
8054
8055 /* Setup replenish threshold. */
Michael Chanf92905d2006-06-29 20:14:29 -07008056 val = tp->rx_pending / 8;
8057 if (val == 0)
8058 val = 1;
8059 else if (val > tp->rx_std_max_post)
8060 val = tp->rx_std_max_post;
Michael Chanb5d37722006-09-27 16:06:21 -07008061 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
8062 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
8063 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
8064
8065 if (val > (TG3_RX_INTERNAL_RING_SZ_5906 / 2))
8066 val = TG3_RX_INTERNAL_RING_SZ_5906 / 2;
8067 }
Michael Chanf92905d2006-06-29 20:14:29 -07008068
8069 tw32(RCVBDI_STD_THRESH, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008070
8071 /* Initialize TG3_BDINFO's at:
8072 * RCVDBDI_STD_BD: standard eth size rx ring
8073 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
8074 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
8075 *
8076 * like so:
8077 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
8078 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
8079 * ring attribute flags
8080 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
8081 *
8082 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
8083 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
8084 *
8085 * The size of each ring is fixed in the firmware, but the location is
8086 * configurable.
8087 */
8088 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008089 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008090 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008091 ((u64) tpr->rx_std_mapping & 0xffffffff));
Matt Carlsona50d0792010-06-05 17:24:37 +00008092 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
8093 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5719)
Matt Carlson87668d32009-11-13 13:03:34 +00008094 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
8095 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008096
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008097 /* Disable the mini ring */
8098 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008099 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
8100 BDINFO_FLAGS_DISABLED);
8101
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008102 /* Program the jumbo buffer descriptor ring control
8103 * blocks on those devices that have them.
8104 */
Matt Carlsonbb18bb92011-03-09 16:58:19 +00008105 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlson4d163b72011-01-25 15:58:48 +00008106 ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) &&
8107 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008108 /* Setup replenish threshold. */
8109 tw32(RCVBDI_JUMBO_THRESH, tp->rx_jumbo_pending / 8);
8110
Michael Chan0f893dc2005-07-25 12:30:38 -07008111 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008112 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008113 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008114 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008115 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +00008116 val = TG3_RX_JMB_RING_SIZE(tp) <<
8117 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008118 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +00008119 val | BDINFO_FLAGS_USE_EXT_RECV);
Matt Carlsona50d0792010-06-05 17:24:37 +00008120 if (!(tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) ||
8121 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson87668d32009-11-13 13:03:34 +00008122 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
8123 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008124 } else {
8125 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
8126 BDINFO_FLAGS_DISABLED);
8127 }
8128
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008129 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
8130 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlsonde9f5232011-04-05 14:22:43 +00008131 val = TG3_RX_STD_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008132 else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008133 val = TG3_RX_STD_MAX_SIZE_5717;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008134 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
8135 val |= (TG3_RX_STD_DMA_SZ << 2);
8136 } else
Matt Carlson04380d42010-04-12 06:58:29 +00008137 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008138 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008139 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008140
8141 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008142
Matt Carlson411da642009-11-13 13:03:46 +00008143 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e62009-11-13 13:03:49 +00008144 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008145
Matt Carlson411da642009-11-13 13:03:46 +00008146 tpr->rx_jmb_prod_idx = (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) ?
Matt Carlson21f581a2009-08-28 14:00:25 +00008147 tp->rx_jumbo_pending : 0;
Matt Carlson66711e62009-11-13 13:03:49 +00008148 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008149
Matt Carlsonc885e822010-08-02 11:25:57 +00008150 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008151 tw32(STD_REPLENISH_LWM, 32);
8152 tw32(JMB_REPLENISH_LWM, 16);
8153 }
8154
Matt Carlson2d31eca2009-09-01 12:53:31 +00008155 tg3_rings_reset(tp);
8156
Linus Torvalds1da177e2005-04-16 15:20:36 -07008157 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07008158 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008159
8160 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00008161 tw32(MAC_RX_MTU_SIZE,
8162 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008163
8164 /* The slot time is changed by tg3_setup_phy if we
8165 * run at gigabit with half duplex.
8166 */
8167 tw32(MAC_TX_LENGTHS,
8168 (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
8169 (6 << TX_LENGTHS_IPG_SHIFT) |
8170 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
8171
8172 /* Receive rules. */
8173 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
8174 tw32(RCVLPC_CONFIG, 0x0181);
8175
8176 /* Calculate RDMAC_MODE setting early, we need it to determine
8177 * the RCVLPC_STATE_ENABLE mask.
8178 */
8179 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
8180 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
8181 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
8182 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
8183 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07008184
Matt Carlsondeabaac2010-11-24 08:31:50 +00008185 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +00008186 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
8187
Matt Carlson57e69832008-05-25 23:48:31 -07008188 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08008189 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8190 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07008191 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
8192 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
8193 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
8194
Matt Carlsonc5908932011-03-09 16:58:25 +00008195 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8196 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008197 if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07008198 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008199 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
8200 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
8201 !(tp->tg3_flags2 & TG3_FLG2_IS_5788)) {
8202 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8203 }
8204 }
8205
Michael Chan85e94ce2005-04-21 17:05:28 -07008206 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)
8207 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8208
Linus Torvalds1da177e2005-04-16 15:20:36 -07008209 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
Matt Carlson027455a2008-12-21 20:19:30 -08008210 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
8211
Matt Carlsone849cdc2009-11-13 13:03:38 +00008212 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
8213 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson027455a2008-12-21 20:19:30 -08008214 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
8215 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008216
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008217 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
8218 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
8219 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8220 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
8221 (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)) {
8222 val = tr32(TG3_RDMA_RSRVCTRL_REG);
Matt Carlsonb75cc0e2010-11-24 08:31:46 +00008223 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +00008224 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
8225 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
8226 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
8227 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
8228 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
8229 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +00008230 }
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008231 tw32(TG3_RDMA_RSRVCTRL_REG,
8232 val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
8233 }
8234
Matt Carlsond309a462010-09-30 10:34:31 +00008235 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) {
8236 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
8237 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val |
8238 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
8239 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
8240 }
8241
Linus Torvalds1da177e2005-04-16 15:20:36 -07008242 /* Receive/send statistics. */
Michael Chan16613942006-06-29 20:15:13 -07008243 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
8244 val = tr32(RCVLPC_STATS_ENABLE);
8245 val &= ~RCVLPC_STATSENAB_DACK_FIX;
8246 tw32(RCVLPC_STATS_ENABLE, val);
8247 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
8248 (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008249 val = tr32(RCVLPC_STATS_ENABLE);
8250 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
8251 tw32(RCVLPC_STATS_ENABLE, val);
8252 } else {
8253 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
8254 }
8255 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
8256 tw32(SNDDATAI_STATSENAB, 0xffffff);
8257 tw32(SNDDATAI_STATSCTRL,
8258 (SNDDATAI_SCTRL_ENABLE |
8259 SNDDATAI_SCTRL_FASTUPD));
8260
8261 /* Setup host coalescing engine. */
8262 tw32(HOSTCC_MODE, 0);
8263 for (i = 0; i < 2000; i++) {
8264 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
8265 break;
8266 udelay(10);
8267 }
8268
Michael Chand244c892005-07-05 14:42:33 -07008269 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008270
Linus Torvalds1da177e2005-04-16 15:20:36 -07008271 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
8272 /* Status/statistics block address. See tg3_timer,
8273 * the tg3_periodic_fetch_stats call there, and
8274 * tg3_get_stats to see how this works for 5705/5750 chips.
8275 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008276 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8277 ((u64) tp->stats_mapping >> 32));
8278 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8279 ((u64) tp->stats_mapping & 0xffffffff));
8280 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008281
Linus Torvalds1da177e2005-04-16 15:20:36 -07008282 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008283
8284 /* Clear statistics and status block memory areas */
8285 for (i = NIC_SRAM_STATS_BLK;
8286 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
8287 i += sizeof(u32)) {
8288 tg3_write_mem(tp, i, 0);
8289 udelay(40);
8290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008291 }
8292
8293 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
8294
8295 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
8296 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
8297 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
8298 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
8299
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008300 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
8301 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -07008302 /* reset to prevent losing 1st rx packet intermittently */
8303 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8304 udelay(10);
8305 }
8306
Matt Carlson3bda1252008-08-15 14:08:22 -07008307 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008308 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -07008309 else
8310 tp->mac_mode = 0;
8311 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Linus Torvalds1da177e2005-04-16 15:20:36 -07008312 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE | MAC_MODE_FHDE_ENABLE;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07008313 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008314 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07008315 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
8316 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008317 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
8318 udelay(40);
8319
Michael Chan314fba32005-04-21 17:07:04 -07008320 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Michael Chan9d26e212006-12-07 00:21:14 -08008321 * If TG3_FLG2_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07008322 * register to preserve the GPIO settings for LOMs. The GPIOs,
8323 * whether used as inputs or outputs, are set by boot code after
8324 * reset.
8325 */
Michael Chan9d26e212006-12-07 00:21:14 -08008326 if (!(tp->tg3_flags2 & TG3_FLG2_IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07008327 u32 gpio_mask;
8328
Michael Chan9d26e212006-12-07 00:21:14 -08008329 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
8330 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
8331 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07008332
8333 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
8334 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
8335 GRC_LCLCTRL_GPIO_OUTPUT3;
8336
Michael Chanaf36e6b2006-03-23 01:28:06 -08008337 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
8338 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
8339
Gary Zambranoaaf84462007-05-05 11:51:45 -07008340 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07008341 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
8342
8343 /* GPIO1 must be driven high for eeprom write protect */
Michael Chan9d26e212006-12-07 00:21:14 -08008344 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT)
8345 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
8346 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07008347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008348 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8349 udelay(100);
8350
Matt Carlson0583d522011-01-25 15:58:50 +00008351 if ((tp->tg3_flags2 & TG3_FLG2_USING_MSIX) &&
8352 tp->irq_cnt > 1) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008353 val = tr32(MSGINT_MODE);
8354 val |= MSGINT_MODE_MULTIVEC_EN | MSGINT_MODE_ENABLE;
8355 tw32(MSGINT_MODE, val);
8356 }
8357
Linus Torvalds1da177e2005-04-16 15:20:36 -07008358 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
8359 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
8360 udelay(40);
8361 }
8362
8363 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
8364 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
8365 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
8366 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
8367 WDMAC_MODE_LNGREAD_ENAB);
8368
Matt Carlsonc5908932011-03-09 16:58:25 +00008369 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8370 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Matt Carlson29ea0952009-08-25 10:07:54 +00008371 if ((tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008372 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
8373 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
8374 /* nothing */
8375 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Matt Carlsonc5908932011-03-09 16:58:25 +00008376 !(tp->tg3_flags2 & TG3_FLG2_IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008377 val |= WDMAC_MODE_RX_ACCEL;
8378 }
8379 }
8380
Michael Chand9ab5ad2006-03-20 22:27:35 -08008381 /* Enable host coalescing bug fix */
Matt Carlson321d32a2008-11-21 17:22:19 -08008382 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Matt Carlsonf51f3562008-05-25 23:45:08 -07008383 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad2006-03-20 22:27:35 -08008384
Matt Carlson788a0352009-11-02 14:26:03 +00008385 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
8386 val |= WDMAC_MODE_BURST_ALL_DATA;
8387
Linus Torvalds1da177e2005-04-16 15:20:36 -07008388 tw32_f(WDMAC_MODE, val);
8389 udelay(40);
8390
Matt Carlson9974a352007-10-07 23:27:28 -07008391 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
8392 u16 pcix_cmd;
8393
8394 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8395 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008396 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07008397 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
8398 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008399 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07008400 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
8401 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008402 }
Matt Carlson9974a352007-10-07 23:27:28 -07008403 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8404 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008405 }
8406
8407 tw32_f(RDMAC_MODE, rdmac_mode);
8408 udelay(40);
8409
8410 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
8411 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
8412 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07008413
8414 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
8415 tw32(SNDDATAC_MODE,
8416 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
8417 else
8418 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
8419
Linus Torvalds1da177e2005-04-16 15:20:36 -07008420 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
8421 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008422 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Matt Carlsonde9f5232011-04-05 14:22:43 +00008423 if (tp->tg3_flags3 & TG3_FLG3_LRG_PROD_RING_CAP)
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008424 val |= RCVDBDI_MODE_LRG_RING_SZ;
8425 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008426 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008427 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
8428 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008429 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Matt Carlson19cfaec2009-12-03 08:36:20 +00008430 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008431 val |= SNDBDI_MODE_MULTI_TXQ_EN;
8432 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008433 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
8434
8435 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
8436 err = tg3_load_5701_a0_firmware_fix(tp);
8437 if (err)
8438 return err;
8439 }
8440
Linus Torvalds1da177e2005-04-16 15:20:36 -07008441 if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) {
8442 err = tg3_load_tso_firmware(tp);
8443 if (err)
8444 return err;
8445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008446
8447 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonb1d05212010-06-05 17:24:31 +00008448 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
8449 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
8450 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008451 tw32_f(MAC_TX_MODE, tp->tx_mode);
8452 udelay(100);
8453
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008454 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS) {
8455 u32 reg = MAC_RSS_INDIR_TBL_0;
8456 u8 *ent = (u8 *)&val;
8457
8458 /* Setup the indirection table */
8459 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
8460 int idx = i % sizeof(val);
8461
Matt Carlson5efeeea2010-07-11 09:31:40 +00008462 ent[idx] = i % (tp->irq_cnt - 1);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008463 if (idx == sizeof(val) - 1) {
8464 tw32(reg, val);
8465 reg += 4;
8466 }
8467 }
8468
8469 /* Setup the "secret" hash key. */
8470 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
8471 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
8472 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
8473 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
8474 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
8475 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
8476 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
8477 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
8478 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
8479 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
8480 }
8481
Linus Torvalds1da177e2005-04-16 15:20:36 -07008482 tp->rx_mode = RX_MODE_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08008483 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Michael Chanaf36e6b2006-03-23 01:28:06 -08008484 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
8485
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008486 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)
8487 tp->rx_mode |= RX_MODE_RSS_ENABLE |
8488 RX_MODE_RSS_ITBL_HASH_BITS_7 |
8489 RX_MODE_RSS_IPV6_HASH_EN |
8490 RX_MODE_RSS_TCP_IPV6_HASH_EN |
8491 RX_MODE_RSS_IPV4_HASH_EN |
8492 RX_MODE_RSS_TCP_IPV4_HASH_EN;
8493
Linus Torvalds1da177e2005-04-16 15:20:36 -07008494 tw32_f(MAC_RX_MODE, tp->rx_mode);
8495 udelay(10);
8496
Linus Torvalds1da177e2005-04-16 15:20:36 -07008497 tw32(MAC_LED_CTRL, tp->led_ctrl);
8498
8499 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008500 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008501 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8502 udelay(10);
8503 }
8504 tw32_f(MAC_RX_MODE, tp->rx_mode);
8505 udelay(10);
8506
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008507 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008508 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008509 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008510 /* Set drive transmission level to 1.2V */
8511 /* only if the signal pre-emphasis bit is not set */
8512 val = tr32(MAC_SERDES_CFG);
8513 val &= 0xfffff000;
8514 val |= 0x880;
8515 tw32(MAC_SERDES_CFG, val);
8516 }
8517 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
8518 tw32(MAC_SERDES_CFG, 0x616000);
8519 }
8520
8521 /* Prevent chip from dropping frames when flow control
8522 * is enabled.
8523 */
Matt Carlson666bc832010-01-20 16:58:03 +00008524 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8525 val = 1;
8526 else
8527 val = 2;
8528 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008529
8530 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008531 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008532 /* Use hardware link auto-negotiation */
8533 tp->tg3_flags2 |= TG3_FLG2_HW_AUTONEG;
8534 }
8535
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008536 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Michael Chand4d2c552006-03-20 17:47:20 -08008537 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)) {
8538 u32 tmp;
8539
8540 tmp = tr32(SERDES_RX_CTRL);
8541 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
8542 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
8543 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
8544 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8545 }
8546
Matt Carlsondd477002008-05-25 23:45:58 -07008547 if (!(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)) {
Matt Carlson80096062010-08-02 11:26:06 +00008548 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
8549 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07008550 tp->link_config.speed = tp->link_config.orig_speed;
8551 tp->link_config.duplex = tp->link_config.orig_duplex;
8552 tp->link_config.autoneg = tp->link_config.orig_autoneg;
8553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008554
Matt Carlsondd477002008-05-25 23:45:58 -07008555 err = tg3_setup_phy(tp, 0);
8556 if (err)
8557 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008558
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008559 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
8560 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07008561 u32 tmp;
8562
8563 /* Clear CRC stats. */
8564 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
8565 tg3_writephy(tp, MII_TG3_TEST1,
8566 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00008567 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -07008568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008569 }
8570 }
8571
8572 __tg3_set_rx_mode(tp->dev);
8573
8574 /* Initialize receive rules. */
8575 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
8576 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
8577 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
8578 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
8579
Michael Chan4cf78e42005-07-25 12:29:19 -07008580 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Michael Chana4e2b342005-10-26 15:46:52 -07008581 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008582 limit = 8;
8583 else
8584 limit = 16;
8585 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF)
8586 limit -= 4;
8587 switch (limit) {
8588 case 16:
8589 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
8590 case 15:
8591 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
8592 case 14:
8593 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
8594 case 13:
8595 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
8596 case 12:
8597 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
8598 case 11:
8599 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
8600 case 10:
8601 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
8602 case 9:
8603 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
8604 case 8:
8605 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
8606 case 7:
8607 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
8608 case 6:
8609 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
8610 case 5:
8611 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
8612 case 4:
8613 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
8614 case 3:
8615 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
8616 case 2:
8617 case 1:
8618
8619 default:
8620 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07008621 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008622
Matt Carlson9ce768e2007-10-11 19:49:11 -07008623 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
8624 /* Write our heartbeat update interval to APE. */
8625 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
8626 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07008627
Linus Torvalds1da177e2005-04-16 15:20:36 -07008628 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
8629
Linus Torvalds1da177e2005-04-16 15:20:36 -07008630 return 0;
8631}
8632
8633/* Called at device open time to get the chip ready for
8634 * packet processing. Invoked with tp->lock held.
8635 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008636static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008637{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008638 tg3_switch_clocks(tp);
8639
8640 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
8641
Matt Carlson2f751b62008-08-04 23:17:34 -07008642 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008643}
8644
8645#define TG3_STAT_ADD32(PSTAT, REG) \
8646do { u32 __val = tr32(REG); \
8647 (PSTAT)->low += __val; \
8648 if ((PSTAT)->low < __val) \
8649 (PSTAT)->high += 1; \
8650} while (0)
8651
8652static void tg3_periodic_fetch_stats(struct tg3 *tp)
8653{
8654 struct tg3_hw_stats *sp = tp->hw_stats;
8655
8656 if (!netif_carrier_ok(tp->dev))
8657 return;
8658
8659 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
8660 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
8661 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
8662 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
8663 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
8664 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
8665 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
8666 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
8667 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
8668 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
8669 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
8670 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
8671 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
8672
8673 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
8674 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
8675 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
8676 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
8677 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
8678 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
8679 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
8680 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
8681 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
8682 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
8683 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
8684 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
8685 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
8686 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07008687
8688 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
8689 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
8690 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008691}
8692
8693static void tg3_timer(unsigned long __opaque)
8694{
8695 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008696
Michael Chanf475f162006-03-27 23:20:14 -08008697 if (tp->irq_sync)
8698 goto restart_timer;
8699
David S. Millerf47c11e2005-06-24 20:18:35 -07008700 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008701
David S. Millerfac9b832005-05-18 22:46:34 -07008702 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)) {
8703 /* All of this garbage is because when using non-tagged
8704 * IRQ status the mailbox/status_block protocol the chip
8705 * uses with the cpu is race prone.
8706 */
Matt Carlson898a56f2009-08-28 14:02:40 +00008707 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -07008708 tw32(GRC_LOCAL_CTRL,
8709 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
8710 } else {
8711 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00008712 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -07008713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008714
David S. Millerfac9b832005-05-18 22:46:34 -07008715 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
8716 tp->tg3_flags2 |= TG3_FLG2_RESTART_TIMER;
David S. Millerf47c11e2005-06-24 20:18:35 -07008717 spin_unlock(&tp->lock);
David S. Millerfac9b832005-05-18 22:46:34 -07008718 schedule_work(&tp->reset_task);
8719 return;
8720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008721 }
8722
Linus Torvalds1da177e2005-04-16 15:20:36 -07008723 /* This part only runs once per second. */
8724 if (!--tp->timer_counter) {
David S. Millerfac9b832005-05-18 22:46:34 -07008725 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
8726 tg3_periodic_fetch_stats(tp);
8727
Matt Carlson52b02d02010-10-14 10:37:41 +00008728 if (tp->setlpicnt && !--tp->setlpicnt) {
8729 u32 val = tr32(TG3_CPMU_EEE_MODE);
8730 tw32(TG3_CPMU_EEE_MODE,
8731 val | TG3_CPMU_EEEMD_LPI_ENABLE);
8732 }
8733
Linus Torvalds1da177e2005-04-16 15:20:36 -07008734 if (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) {
8735 u32 mac_stat;
8736 int phy_event;
8737
8738 mac_stat = tr32(MAC_STATUS);
8739
8740 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008741 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008742 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
8743 phy_event = 1;
8744 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
8745 phy_event = 1;
8746
8747 if (phy_event)
8748 tg3_setup_phy(tp, 0);
8749 } else if (tp->tg3_flags & TG3_FLAG_POLL_SERDES) {
8750 u32 mac_stat = tr32(MAC_STATUS);
8751 int need_setup = 0;
8752
8753 if (netif_carrier_ok(tp->dev) &&
8754 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
8755 need_setup = 1;
8756 }
Matt Carlsonbe98da62010-07-11 09:31:46 +00008757 if (!netif_carrier_ok(tp->dev) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008758 (mac_stat & (MAC_STATUS_PCS_SYNCED |
8759 MAC_STATUS_SIGNAL_DET))) {
8760 need_setup = 1;
8761 }
8762 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07008763 if (!tp->serdes_counter) {
8764 tw32_f(MAC_MODE,
8765 (tp->mac_mode &
8766 ~MAC_MODE_PORT_MODE_MASK));
8767 udelay(40);
8768 tw32_f(MAC_MODE, tp->mac_mode);
8769 udelay(40);
8770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008771 tg3_setup_phy(tp, 0);
8772 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008773 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Matt Carlson2138c002010-07-11 09:31:43 +00008774 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -07008775 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +00008776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008777
8778 tp->timer_counter = tp->timer_multiplier;
8779 }
8780
Michael Chan130b8e42006-09-27 16:00:40 -07008781 /* Heartbeat is only sent once every 2 seconds.
8782 *
8783 * The heartbeat is to tell the ASF firmware that the host
8784 * driver is still alive. In the event that the OS crashes,
8785 * ASF needs to reset the hardware to free up the FIFO space
8786 * that may be filled with rx packets destined for the host.
8787 * If the FIFO is full, ASF will no longer function properly.
8788 *
8789 * Unintended resets have been reported on real time kernels
8790 * where the timer doesn't run on time. Netpoll will also have
8791 * same problem.
8792 *
8793 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
8794 * to check the ring condition when the heartbeat is expiring
8795 * before doing the reset. This will prevent most unintended
8796 * resets.
8797 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008798 if (!--tp->asf_counter) {
Matt Carlsonbc7959b2008-08-15 14:08:55 -07008799 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
8800 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07008801 tg3_wait_for_event_ack(tp);
8802
Michael Chanbbadf502006-04-06 21:46:34 -07008803 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07008804 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07008805 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +00008806 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
8807 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -07008808
8809 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008810 }
8811 tp->asf_counter = tp->asf_multiplier;
8812 }
8813
David S. Millerf47c11e2005-06-24 20:18:35 -07008814 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008815
Michael Chanf475f162006-03-27 23:20:14 -08008816restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07008817 tp->timer.expires = jiffies + tp->timer_offset;
8818 add_timer(&tp->timer);
8819}
8820
Matt Carlson4f125f42009-09-01 12:55:02 +00008821static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -08008822{
David Howells7d12e782006-10-05 14:55:46 +01008823 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008824 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +00008825 char *name;
8826 struct tg3_napi *tnapi = &tp->napi[irq_num];
8827
8828 if (tp->irq_cnt == 1)
8829 name = tp->dev->name;
8830 else {
8831 name = &tnapi->irq_lbl[0];
8832 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
8833 name[IFNAMSIZ-1] = 0;
8834 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08008835
Matt Carlson679563f2009-09-01 12:55:46 +00008836 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI_OR_MSIX) {
Michael Chanfcfa0a32006-03-20 22:28:41 -08008837 fn = tg3_msi;
8838 if (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)
8839 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00008840 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008841 } else {
8842 fn = tg3_interrupt;
8843 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)
8844 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00008845 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008846 }
Matt Carlson4f125f42009-09-01 12:55:02 +00008847
8848 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08008849}
8850
Michael Chan79381092005-04-21 17:13:59 -07008851static int tg3_test_interrupt(struct tg3 *tp)
8852{
Matt Carlson09943a12009-08-28 14:01:57 +00008853 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -07008854 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -07008855 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008856 u32 val;
Michael Chan79381092005-04-21 17:13:59 -07008857
Michael Chand4bc3922005-05-29 14:59:20 -07008858 if (!netif_running(dev))
8859 return -ENODEV;
8860
Michael Chan79381092005-04-21 17:13:59 -07008861 tg3_disable_ints(tp);
8862
Matt Carlson4f125f42009-09-01 12:55:02 +00008863 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07008864
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008865 /*
8866 * Turn off MSI one shot mode. Otherwise this test has no
8867 * observable way to know whether the interrupt was delivered.
8868 */
Matt Carlsonc885e822010-08-02 11:25:57 +00008869 if ((tp->tg3_flags3 & TG3_FLG3_5717_PLUS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008870 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
8871 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
8872 tw32(MSGINT_MODE, val);
8873 }
8874
Matt Carlson4f125f42009-09-01 12:55:02 +00008875 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Matt Carlson09943a12009-08-28 14:01:57 +00008876 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07008877 if (err)
8878 return err;
8879
Matt Carlson898a56f2009-08-28 14:02:40 +00008880 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -07008881 tg3_enable_ints(tp);
8882
8883 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00008884 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -07008885
8886 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -07008887 u32 int_mbox, misc_host_ctrl;
8888
Matt Carlson898a56f2009-08-28 14:02:40 +00008889 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -07008890 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
8891
8892 if ((int_mbox != 0) ||
8893 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
8894 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -07008895 break;
Michael Chanb16250e2006-09-27 16:10:14 -07008896 }
8897
Michael Chan79381092005-04-21 17:13:59 -07008898 msleep(10);
8899 }
8900
8901 tg3_disable_ints(tp);
8902
Matt Carlson4f125f42009-09-01 12:55:02 +00008903 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008904
Matt Carlson4f125f42009-09-01 12:55:02 +00008905 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07008906
8907 if (err)
8908 return err;
8909
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008910 if (intr_ok) {
8911 /* Reenable MSI one shot mode. */
Matt Carlsonc885e822010-08-02 11:25:57 +00008912 if ((tp->tg3_flags3 & TG3_FLG3_5717_PLUS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008913 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
8914 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
8915 tw32(MSGINT_MODE, val);
8916 }
Michael Chan79381092005-04-21 17:13:59 -07008917 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008918 }
Michael Chan79381092005-04-21 17:13:59 -07008919
8920 return -EIO;
8921}
8922
8923/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
8924 * successfully restored
8925 */
8926static int tg3_test_msi(struct tg3 *tp)
8927{
Michael Chan79381092005-04-21 17:13:59 -07008928 int err;
8929 u16 pci_cmd;
8930
8931 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSI))
8932 return 0;
8933
8934 /* Turn off SERR reporting in case MSI terminates with Master
8935 * Abort.
8936 */
8937 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
8938 pci_write_config_word(tp->pdev, PCI_COMMAND,
8939 pci_cmd & ~PCI_COMMAND_SERR);
8940
8941 err = tg3_test_interrupt(tp);
8942
8943 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
8944
8945 if (!err)
8946 return 0;
8947
8948 /* other failures */
8949 if (err != -EIO)
8950 return err;
8951
8952 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +00008953 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
8954 "to INTx mode. Please report this failure to the PCI "
8955 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -07008956
Matt Carlson4f125f42009-09-01 12:55:02 +00008957 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +00008958
Michael Chan79381092005-04-21 17:13:59 -07008959 pci_disable_msi(tp->pdev);
8960
8961 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI;
Andre Detschdc8bf1b2010-04-26 07:27:07 +00008962 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -07008963
Matt Carlson4f125f42009-09-01 12:55:02 +00008964 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07008965 if (err)
8966 return err;
8967
8968 /* Need to reset the chip because the MSI cycle may have terminated
8969 * with Master Abort.
8970 */
David S. Millerf47c11e2005-06-24 20:18:35 -07008971 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07008972
Michael Chan944d9802005-05-29 14:57:48 -07008973 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008974 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07008975
David S. Millerf47c11e2005-06-24 20:18:35 -07008976 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07008977
8978 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +00008979 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -07008980
8981 return err;
8982}
8983
Matt Carlson9e9fd122009-01-19 16:57:45 -08008984static int tg3_request_firmware(struct tg3 *tp)
8985{
8986 const __be32 *fw_data;
8987
8988 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00008989 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
8990 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08008991 return -ENOENT;
8992 }
8993
8994 fw_data = (void *)tp->fw->data;
8995
8996 /* Firmware blob starts with version numbers, followed by
8997 * start address and _full_ length including BSS sections
8998 * (which must be longer than the actual data, of course
8999 */
9000
9001 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
9002 if (tp->fw_len < (tp->fw->size - 12)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009003 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
9004 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009005 release_firmware(tp->fw);
9006 tp->fw = NULL;
9007 return -EINVAL;
9008 }
9009
9010 /* We no longer need firmware; we have it. */
9011 tp->fw_needed = NULL;
9012 return 0;
9013}
9014
Matt Carlson679563f2009-09-01 12:55:46 +00009015static bool tg3_enable_msix(struct tg3 *tp)
9016{
9017 int i, rc, cpus = num_online_cpus();
9018 struct msix_entry msix_ent[tp->irq_max];
9019
9020 if (cpus == 1)
9021 /* Just fallback to the simpler MSI mode. */
9022 return false;
9023
9024 /*
9025 * We want as many rx rings enabled as there are cpus.
9026 * The first MSIX vector only deals with link interrupts, etc,
9027 * so we add one to the number of vectors we are requesting.
9028 */
9029 tp->irq_cnt = min_t(unsigned, cpus + 1, tp->irq_max);
9030
9031 for (i = 0; i < tp->irq_max; i++) {
9032 msix_ent[i].entry = i;
9033 msix_ent[i].vector = 0;
9034 }
9035
9036 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +00009037 if (rc < 0) {
9038 return false;
9039 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +00009040 if (pci_enable_msix(tp->pdev, msix_ent, rc))
9041 return false;
Joe Perches05dbe002010-02-17 19:44:19 +00009042 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
9043 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +00009044 tp->irq_cnt = rc;
9045 }
9046
9047 for (i = 0; i < tp->irq_max; i++)
9048 tp->napi[i].irq_vec = msix_ent[i].vector;
9049
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009050 netif_set_real_num_tx_queues(tp->dev, 1);
9051 rc = tp->irq_cnt > 1 ? tp->irq_cnt - 1 : 1;
9052 if (netif_set_real_num_rx_queues(tp->dev, rc)) {
9053 pci_disable_msix(tp->pdev);
9054 return false;
9055 }
Matt Carlsonb92b9042010-11-24 08:31:51 +00009056
9057 if (tp->irq_cnt > 1) {
Matt Carlson2430b032010-06-05 17:24:34 +00009058 tp->tg3_flags3 |= TG3_FLG3_ENABLE_RSS;
Matt Carlsonb92b9042010-11-24 08:31:51 +00009059 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) {
9060 tp->tg3_flags3 |= TG3_FLG3_ENABLE_TSS;
9061 netif_set_real_num_tx_queues(tp->dev, tp->irq_cnt - 1);
9062 }
9063 }
Matt Carlson2430b032010-06-05 17:24:34 +00009064
Matt Carlson679563f2009-09-01 12:55:46 +00009065 return true;
9066}
9067
Matt Carlson07b01732009-08-28 14:01:15 +00009068static void tg3_ints_init(struct tg3 *tp)
9069{
Matt Carlson679563f2009-09-01 12:55:46 +00009070 if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSI_OR_MSIX) &&
9071 !(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +00009072 /* All MSI supporting chips should support tagged
9073 * status. Assert that this is the case.
9074 */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009075 netdev_warn(tp->dev,
9076 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +00009077 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +00009078 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009079
Matt Carlson679563f2009-09-01 12:55:46 +00009080 if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX) && tg3_enable_msix(tp))
9081 tp->tg3_flags2 |= TG3_FLG2_USING_MSIX;
9082 else if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSI) &&
9083 pci_enable_msi(tp->pdev) == 0)
9084 tp->tg3_flags2 |= TG3_FLG2_USING_MSI;
9085
9086 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI_OR_MSIX) {
9087 u32 msi_mode = tr32(MSGINT_MODE);
Matt Carlson0583d522011-01-25 15:58:50 +00009088 if ((tp->tg3_flags2 & TG3_FLG2_USING_MSIX) &&
9089 tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009090 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson679563f2009-09-01 12:55:46 +00009091 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
9092 }
9093defcfg:
9094 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSIX)) {
9095 tp->irq_cnt = 1;
9096 tp->napi[0].irq_vec = tp->pdev->irq;
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009097 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -07009098 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +00009099 }
Matt Carlson07b01732009-08-28 14:01:15 +00009100}
9101
9102static void tg3_ints_fini(struct tg3 *tp)
9103{
Matt Carlson679563f2009-09-01 12:55:46 +00009104 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
9105 pci_disable_msix(tp->pdev);
9106 else if (tp->tg3_flags2 & TG3_FLG2_USING_MSI)
9107 pci_disable_msi(tp->pdev);
9108 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI_OR_MSIX;
Matt Carlson774ee752010-08-02 11:25:56 +00009109 tp->tg3_flags3 &= ~(TG3_FLG3_ENABLE_RSS | TG3_FLG3_ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +00009110}
9111
Linus Torvalds1da177e2005-04-16 15:20:36 -07009112static int tg3_open(struct net_device *dev)
9113{
9114 struct tg3 *tp = netdev_priv(dev);
Matt Carlson4f125f42009-09-01 12:55:02 +00009115 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009116
Matt Carlson9e9fd122009-01-19 16:57:45 -08009117 if (tp->fw_needed) {
9118 err = tg3_request_firmware(tp);
9119 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
9120 if (err)
9121 return err;
9122 } else if (err) {
Joe Perches05dbe002010-02-17 19:44:19 +00009123 netdev_warn(tp->dev, "TSO capability disabled\n");
Matt Carlson9e9fd122009-01-19 16:57:45 -08009124 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
9125 } else if (!(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009126 netdev_notice(tp->dev, "TSO capability restored\n");
Matt Carlson9e9fd122009-01-19 16:57:45 -08009127 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
9128 }
9129 }
9130
Michael Chanc49a1562006-12-17 17:07:29 -08009131 netif_carrier_off(tp->dev);
9132
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009133 err = tg3_power_up(tp);
Matt Carlson2f751b62008-08-04 23:17:34 -07009134 if (err)
Michael Chanbc1c7562006-03-20 17:48:03 -08009135 return err;
Matt Carlson2f751b62008-08-04 23:17:34 -07009136
9137 tg3_full_lock(tp, 0);
Michael Chanbc1c7562006-03-20 17:48:03 -08009138
Linus Torvalds1da177e2005-04-16 15:20:36 -07009139 tg3_disable_ints(tp);
9140 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
9141
David S. Millerf47c11e2005-06-24 20:18:35 -07009142 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009143
Matt Carlson679563f2009-09-01 12:55:46 +00009144 /*
9145 * Setup interrupts first so we know how
9146 * many NAPI resources to allocate
9147 */
9148 tg3_ints_init(tp);
9149
Linus Torvalds1da177e2005-04-16 15:20:36 -07009150 /* The placement of this call is tied
9151 * to the setup and use of Host TX descriptors.
9152 */
9153 err = tg3_alloc_consistent(tp);
9154 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009155 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009156
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009157 tg3_napi_init(tp);
9158
Matt Carlsonfed97812009-09-01 13:10:19 +00009159 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07009160
Matt Carlson4f125f42009-09-01 12:55:02 +00009161 for (i = 0; i < tp->irq_cnt; i++) {
9162 struct tg3_napi *tnapi = &tp->napi[i];
9163 err = tg3_request_irq(tp, i);
9164 if (err) {
9165 for (i--; i >= 0; i--)
9166 free_irq(tnapi->irq_vec, tnapi);
9167 break;
9168 }
9169 }
Matt Carlson07b01732009-08-28 14:01:15 +00009170
9171 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009172 goto err_out2;
Matt Carlson07b01732009-08-28 14:01:15 +00009173
David S. Millerf47c11e2005-06-24 20:18:35 -07009174 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009175
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009176 err = tg3_init_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009177 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -07009178 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009179 tg3_free_rings(tp);
9180 } else {
David S. Millerfac9b832005-05-18 22:46:34 -07009181 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)
9182 tp->timer_offset = HZ;
9183 else
9184 tp->timer_offset = HZ / 10;
9185
9186 BUG_ON(tp->timer_offset > HZ);
9187 tp->timer_counter = tp->timer_multiplier =
9188 (HZ / tp->timer_offset);
9189 tp->asf_counter = tp->asf_multiplier =
Michael Chan28fbef72005-10-26 15:48:35 -07009190 ((HZ / tp->timer_offset) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009191
9192 init_timer(&tp->timer);
9193 tp->timer.expires = jiffies + tp->timer_offset;
9194 tp->timer.data = (unsigned long) tp;
9195 tp->timer.function = tg3_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009196 }
9197
David S. Millerf47c11e2005-06-24 20:18:35 -07009198 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009199
Matt Carlson07b01732009-08-28 14:01:15 +00009200 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009201 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009202
Michael Chan79381092005-04-21 17:13:59 -07009203 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
9204 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -07009205
Michael Chan79381092005-04-21 17:13:59 -07009206 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -07009207 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -07009208 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -07009209 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07009210 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009211
Matt Carlson679563f2009-09-01 12:55:46 +00009212 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -07009213 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009214
Matt Carlsonc885e822010-08-02 11:25:57 +00009215 if (!(tp->tg3_flags3 & TG3_FLG3_5717_PLUS) &&
9216 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009217 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009218
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009219 tw32(PCIE_TRANSACTION_CFG,
9220 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009221 }
Michael Chan79381092005-04-21 17:13:59 -07009222 }
9223
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009224 tg3_phy_start(tp);
9225
David S. Millerf47c11e2005-06-24 20:18:35 -07009226 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009227
Michael Chan79381092005-04-21 17:13:59 -07009228 add_timer(&tp->timer);
9229 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009230 tg3_enable_ints(tp);
9231
David S. Millerf47c11e2005-06-24 20:18:35 -07009232 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009233
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009234 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009235
9236 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +00009237
Matt Carlson679563f2009-09-01 12:55:46 +00009238err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +00009239 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9240 struct tg3_napi *tnapi = &tp->napi[i];
9241 free_irq(tnapi->irq_vec, tnapi);
9242 }
Matt Carlson07b01732009-08-28 14:01:15 +00009243
Matt Carlson679563f2009-09-01 12:55:46 +00009244err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +00009245 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009246 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00009247 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +00009248
9249err_out1:
9250 tg3_ints_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00009251 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009252}
9253
Eric Dumazet511d2222010-07-07 20:44:24 +00009254static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *,
9255 struct rtnl_link_stats64 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009256static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *);
9257
9258static int tg3_close(struct net_device *dev)
9259{
Matt Carlson4f125f42009-09-01 12:55:02 +00009260 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009261 struct tg3 *tp = netdev_priv(dev);
9262
Matt Carlsonfed97812009-09-01 13:10:19 +00009263 tg3_napi_disable(tp);
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07009264 cancel_work_sync(&tp->reset_task);
Michael Chan7faa0062006-02-02 17:29:28 -08009265
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009266 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009267
9268 del_timer_sync(&tp->timer);
9269
Matt Carlson24bb4fb2009-10-05 17:55:29 +00009270 tg3_phy_stop(tp);
9271
David S. Millerf47c11e2005-06-24 20:18:35 -07009272 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009273
9274 tg3_disable_ints(tp);
9275
Michael Chan944d9802005-05-29 14:57:48 -07009276 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009277 tg3_free_rings(tp);
Michael Chan5cf64b82007-05-05 12:11:21 -07009278 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009279
David S. Millerf47c11e2005-06-24 20:18:35 -07009280 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009281
Matt Carlson4f125f42009-09-01 12:55:02 +00009282 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9283 struct tg3_napi *tnapi = &tp->napi[i];
9284 free_irq(tnapi->irq_vec, tnapi);
9285 }
Matt Carlson07b01732009-08-28 14:01:15 +00009286
9287 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009288
Eric Dumazet511d2222010-07-07 20:44:24 +00009289 tg3_get_stats64(tp->dev, &tp->net_stats_prev);
9290
Linus Torvalds1da177e2005-04-16 15:20:36 -07009291 memcpy(&tp->estats_prev, tg3_get_estats(tp),
9292 sizeof(tp->estats_prev));
9293
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009294 tg3_napi_fini(tp);
9295
Linus Torvalds1da177e2005-04-16 15:20:36 -07009296 tg3_free_consistent(tp);
9297
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009298 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -08009299
9300 netif_carrier_off(tp->dev);
9301
Linus Torvalds1da177e2005-04-16 15:20:36 -07009302 return 0;
9303}
9304
Eric Dumazet511d2222010-07-07 20:44:24 +00009305static inline u64 get_stat64(tg3_stat64_t *val)
Stefan Buehler816f8b82008-08-15 14:10:54 -07009306{
9307 return ((u64)val->high << 32) | ((u64)val->low);
9308}
9309
Eric Dumazet511d2222010-07-07 20:44:24 +00009310static u64 calc_crc_errors(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009311{
9312 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9313
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009314 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009315 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
9316 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009317 u32 val;
9318
David S. Millerf47c11e2005-06-24 20:18:35 -07009319 spin_lock_bh(&tp->lock);
Michael Chan569a5df2007-02-13 12:18:15 -08009320 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
9321 tg3_writephy(tp, MII_TG3_TEST1,
9322 val | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009323 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009324 } else
9325 val = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07009326 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009327
9328 tp->phy_crc_errors += val;
9329
9330 return tp->phy_crc_errors;
9331 }
9332
9333 return get_stat64(&hw_stats->rx_fcs_errors);
9334}
9335
9336#define ESTAT_ADD(member) \
9337 estats->member = old_estats->member + \
Eric Dumazet511d2222010-07-07 20:44:24 +00009338 get_stat64(&hw_stats->member)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009339
9340static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *tp)
9341{
9342 struct tg3_ethtool_stats *estats = &tp->estats;
9343 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
9344 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9345
9346 if (!hw_stats)
9347 return old_estats;
9348
9349 ESTAT_ADD(rx_octets);
9350 ESTAT_ADD(rx_fragments);
9351 ESTAT_ADD(rx_ucast_packets);
9352 ESTAT_ADD(rx_mcast_packets);
9353 ESTAT_ADD(rx_bcast_packets);
9354 ESTAT_ADD(rx_fcs_errors);
9355 ESTAT_ADD(rx_align_errors);
9356 ESTAT_ADD(rx_xon_pause_rcvd);
9357 ESTAT_ADD(rx_xoff_pause_rcvd);
9358 ESTAT_ADD(rx_mac_ctrl_rcvd);
9359 ESTAT_ADD(rx_xoff_entered);
9360 ESTAT_ADD(rx_frame_too_long_errors);
9361 ESTAT_ADD(rx_jabbers);
9362 ESTAT_ADD(rx_undersize_packets);
9363 ESTAT_ADD(rx_in_length_errors);
9364 ESTAT_ADD(rx_out_length_errors);
9365 ESTAT_ADD(rx_64_or_less_octet_packets);
9366 ESTAT_ADD(rx_65_to_127_octet_packets);
9367 ESTAT_ADD(rx_128_to_255_octet_packets);
9368 ESTAT_ADD(rx_256_to_511_octet_packets);
9369 ESTAT_ADD(rx_512_to_1023_octet_packets);
9370 ESTAT_ADD(rx_1024_to_1522_octet_packets);
9371 ESTAT_ADD(rx_1523_to_2047_octet_packets);
9372 ESTAT_ADD(rx_2048_to_4095_octet_packets);
9373 ESTAT_ADD(rx_4096_to_8191_octet_packets);
9374 ESTAT_ADD(rx_8192_to_9022_octet_packets);
9375
9376 ESTAT_ADD(tx_octets);
9377 ESTAT_ADD(tx_collisions);
9378 ESTAT_ADD(tx_xon_sent);
9379 ESTAT_ADD(tx_xoff_sent);
9380 ESTAT_ADD(tx_flow_control);
9381 ESTAT_ADD(tx_mac_errors);
9382 ESTAT_ADD(tx_single_collisions);
9383 ESTAT_ADD(tx_mult_collisions);
9384 ESTAT_ADD(tx_deferred);
9385 ESTAT_ADD(tx_excessive_collisions);
9386 ESTAT_ADD(tx_late_collisions);
9387 ESTAT_ADD(tx_collide_2times);
9388 ESTAT_ADD(tx_collide_3times);
9389 ESTAT_ADD(tx_collide_4times);
9390 ESTAT_ADD(tx_collide_5times);
9391 ESTAT_ADD(tx_collide_6times);
9392 ESTAT_ADD(tx_collide_7times);
9393 ESTAT_ADD(tx_collide_8times);
9394 ESTAT_ADD(tx_collide_9times);
9395 ESTAT_ADD(tx_collide_10times);
9396 ESTAT_ADD(tx_collide_11times);
9397 ESTAT_ADD(tx_collide_12times);
9398 ESTAT_ADD(tx_collide_13times);
9399 ESTAT_ADD(tx_collide_14times);
9400 ESTAT_ADD(tx_collide_15times);
9401 ESTAT_ADD(tx_ucast_packets);
9402 ESTAT_ADD(tx_mcast_packets);
9403 ESTAT_ADD(tx_bcast_packets);
9404 ESTAT_ADD(tx_carrier_sense_errors);
9405 ESTAT_ADD(tx_discards);
9406 ESTAT_ADD(tx_errors);
9407
9408 ESTAT_ADD(dma_writeq_full);
9409 ESTAT_ADD(dma_write_prioq_full);
9410 ESTAT_ADD(rxbds_empty);
9411 ESTAT_ADD(rx_discards);
9412 ESTAT_ADD(rx_errors);
9413 ESTAT_ADD(rx_threshold_hit);
9414
9415 ESTAT_ADD(dma_readq_full);
9416 ESTAT_ADD(dma_read_prioq_full);
9417 ESTAT_ADD(tx_comp_queue_full);
9418
9419 ESTAT_ADD(ring_set_send_prod_index);
9420 ESTAT_ADD(ring_status_update);
9421 ESTAT_ADD(nic_irqs);
9422 ESTAT_ADD(nic_avoided_irqs);
9423 ESTAT_ADD(nic_tx_threshold_hit);
9424
9425 return estats;
9426}
9427
Eric Dumazet511d2222010-07-07 20:44:24 +00009428static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
9429 struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009430{
9431 struct tg3 *tp = netdev_priv(dev);
Eric Dumazet511d2222010-07-07 20:44:24 +00009432 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009433 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9434
9435 if (!hw_stats)
9436 return old_stats;
9437
9438 stats->rx_packets = old_stats->rx_packets +
9439 get_stat64(&hw_stats->rx_ucast_packets) +
9440 get_stat64(&hw_stats->rx_mcast_packets) +
9441 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009442
Linus Torvalds1da177e2005-04-16 15:20:36 -07009443 stats->tx_packets = old_stats->tx_packets +
9444 get_stat64(&hw_stats->tx_ucast_packets) +
9445 get_stat64(&hw_stats->tx_mcast_packets) +
9446 get_stat64(&hw_stats->tx_bcast_packets);
9447
9448 stats->rx_bytes = old_stats->rx_bytes +
9449 get_stat64(&hw_stats->rx_octets);
9450 stats->tx_bytes = old_stats->tx_bytes +
9451 get_stat64(&hw_stats->tx_octets);
9452
9453 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -07009454 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009455 stats->tx_errors = old_stats->tx_errors +
9456 get_stat64(&hw_stats->tx_errors) +
9457 get_stat64(&hw_stats->tx_mac_errors) +
9458 get_stat64(&hw_stats->tx_carrier_sense_errors) +
9459 get_stat64(&hw_stats->tx_discards);
9460
9461 stats->multicast = old_stats->multicast +
9462 get_stat64(&hw_stats->rx_mcast_packets);
9463 stats->collisions = old_stats->collisions +
9464 get_stat64(&hw_stats->tx_collisions);
9465
9466 stats->rx_length_errors = old_stats->rx_length_errors +
9467 get_stat64(&hw_stats->rx_frame_too_long_errors) +
9468 get_stat64(&hw_stats->rx_undersize_packets);
9469
9470 stats->rx_over_errors = old_stats->rx_over_errors +
9471 get_stat64(&hw_stats->rxbds_empty);
9472 stats->rx_frame_errors = old_stats->rx_frame_errors +
9473 get_stat64(&hw_stats->rx_align_errors);
9474 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
9475 get_stat64(&hw_stats->tx_discards);
9476 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
9477 get_stat64(&hw_stats->tx_carrier_sense_errors);
9478
9479 stats->rx_crc_errors = old_stats->rx_crc_errors +
9480 calc_crc_errors(tp);
9481
John W. Linville4f63b872005-09-12 14:43:18 -07009482 stats->rx_missed_errors = old_stats->rx_missed_errors +
9483 get_stat64(&hw_stats->rx_discards);
9484
Eric Dumazetb0057c52010-10-10 19:55:52 +00009485 stats->rx_dropped = tp->rx_dropped;
9486
Linus Torvalds1da177e2005-04-16 15:20:36 -07009487 return stats;
9488}
9489
9490static inline u32 calc_crc(unsigned char *buf, int len)
9491{
9492 u32 reg;
9493 u32 tmp;
9494 int j, k;
9495
9496 reg = 0xffffffff;
9497
9498 for (j = 0; j < len; j++) {
9499 reg ^= buf[j];
9500
9501 for (k = 0; k < 8; k++) {
9502 tmp = reg & 0x01;
9503
9504 reg >>= 1;
9505
Matt Carlson859a5882010-04-05 10:19:28 +00009506 if (tmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009507 reg ^= 0xedb88320;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009508 }
9509 }
9510
9511 return ~reg;
9512}
9513
9514static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
9515{
9516 /* accept or reject all multicast frames */
9517 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
9518 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
9519 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
9520 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
9521}
9522
9523static void __tg3_set_rx_mode(struct net_device *dev)
9524{
9525 struct tg3 *tp = netdev_priv(dev);
9526 u32 rx_mode;
9527
9528 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
9529 RX_MODE_KEEP_VLAN_TAG);
9530
Matt Carlsonbf933c82011-01-25 15:58:49 +00009531#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009532 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
9533 * flag clear.
9534 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009535 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
9536 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9537#endif
9538
9539 if (dev->flags & IFF_PROMISC) {
9540 /* Promiscuous mode. */
9541 rx_mode |= RX_MODE_PROMISC;
9542 } else if (dev->flags & IFF_ALLMULTI) {
9543 /* Accept all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +00009544 tg3_set_multi(tp, 1);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00009545 } else if (netdev_mc_empty(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009546 /* Reject all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +00009547 tg3_set_multi(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009548 } else {
9549 /* Accept one or more multicast(s). */
Jiri Pirko22bedad2010-04-01 21:22:57 +00009550 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009551 u32 mc_filter[4] = { 0, };
9552 u32 regidx;
9553 u32 bit;
9554 u32 crc;
9555
Jiri Pirko22bedad2010-04-01 21:22:57 +00009556 netdev_for_each_mc_addr(ha, dev) {
9557 crc = calc_crc(ha->addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009558 bit = ~crc & 0x7f;
9559 regidx = (bit & 0x60) >> 5;
9560 bit &= 0x1f;
9561 mc_filter[regidx] |= (1 << bit);
9562 }
9563
9564 tw32(MAC_HASH_REG_0, mc_filter[0]);
9565 tw32(MAC_HASH_REG_1, mc_filter[1]);
9566 tw32(MAC_HASH_REG_2, mc_filter[2]);
9567 tw32(MAC_HASH_REG_3, mc_filter[3]);
9568 }
9569
9570 if (rx_mode != tp->rx_mode) {
9571 tp->rx_mode = rx_mode;
9572 tw32_f(MAC_RX_MODE, rx_mode);
9573 udelay(10);
9574 }
9575}
9576
9577static void tg3_set_rx_mode(struct net_device *dev)
9578{
9579 struct tg3 *tp = netdev_priv(dev);
9580
Michael Chane75f7c92006-03-20 21:33:26 -08009581 if (!netif_running(dev))
9582 return;
9583
David S. Millerf47c11e2005-06-24 20:18:35 -07009584 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009585 __tg3_set_rx_mode(dev);
David S. Millerf47c11e2005-06-24 20:18:35 -07009586 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009587}
9588
9589#define TG3_REGDUMP_LEN (32 * 1024)
9590
9591static int tg3_get_regs_len(struct net_device *dev)
9592{
9593 return TG3_REGDUMP_LEN;
9594}
9595
9596static void tg3_get_regs(struct net_device *dev,
9597 struct ethtool_regs *regs, void *_p)
9598{
9599 u32 *p = _p;
9600 struct tg3 *tp = netdev_priv(dev);
9601 u8 *orig_p = _p;
9602 int i;
9603
9604 regs->version = 0;
9605
9606 memset(p, 0, TG3_REGDUMP_LEN);
9607
Matt Carlson80096062010-08-02 11:26:06 +00009608 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -08009609 return;
9610
David S. Millerf47c11e2005-06-24 20:18:35 -07009611 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009612
9613#define __GET_REG32(reg) (*(p)++ = tr32(reg))
Matt Carlsonbe98da62010-07-11 09:31:46 +00009614#define GET_REG32_LOOP(base, len) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07009615do { p = (u32 *)(orig_p + (base)); \
9616 for (i = 0; i < len; i += 4) \
9617 __GET_REG32((base) + i); \
9618} while (0)
9619#define GET_REG32_1(reg) \
9620do { p = (u32 *)(orig_p + (reg)); \
9621 __GET_REG32((reg)); \
9622} while (0)
9623
9624 GET_REG32_LOOP(TG3PCI_VENDOR, 0xb0);
9625 GET_REG32_LOOP(MAILBOX_INTERRUPT_0, 0x200);
9626 GET_REG32_LOOP(MAC_MODE, 0x4f0);
9627 GET_REG32_LOOP(SNDDATAI_MODE, 0xe0);
9628 GET_REG32_1(SNDDATAC_MODE);
9629 GET_REG32_LOOP(SNDBDS_MODE, 0x80);
9630 GET_REG32_LOOP(SNDBDI_MODE, 0x48);
9631 GET_REG32_1(SNDBDC_MODE);
9632 GET_REG32_LOOP(RCVLPC_MODE, 0x20);
9633 GET_REG32_LOOP(RCVLPC_SELLST_BASE, 0x15c);
9634 GET_REG32_LOOP(RCVDBDI_MODE, 0x0c);
9635 GET_REG32_LOOP(RCVDBDI_JUMBO_BD, 0x3c);
9636 GET_REG32_LOOP(RCVDBDI_BD_PROD_IDX_0, 0x44);
9637 GET_REG32_1(RCVDCC_MODE);
9638 GET_REG32_LOOP(RCVBDI_MODE, 0x20);
9639 GET_REG32_LOOP(RCVCC_MODE, 0x14);
9640 GET_REG32_LOOP(RCVLSC_MODE, 0x08);
9641 GET_REG32_1(MBFREE_MODE);
9642 GET_REG32_LOOP(HOSTCC_MODE, 0x100);
9643 GET_REG32_LOOP(MEMARB_MODE, 0x10);
9644 GET_REG32_LOOP(BUFMGR_MODE, 0x58);
9645 GET_REG32_LOOP(RDMAC_MODE, 0x08);
9646 GET_REG32_LOOP(WDMAC_MODE, 0x08);
Chris Elmquist091465d2005-12-20 13:25:19 -08009647 GET_REG32_1(RX_CPU_MODE);
9648 GET_REG32_1(RX_CPU_STATE);
9649 GET_REG32_1(RX_CPU_PGMCTR);
9650 GET_REG32_1(RX_CPU_HWBKPT);
9651 GET_REG32_1(TX_CPU_MODE);
9652 GET_REG32_1(TX_CPU_STATE);
9653 GET_REG32_1(TX_CPU_PGMCTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009654 GET_REG32_LOOP(GRCMBOX_INTERRUPT_0, 0x110);
9655 GET_REG32_LOOP(FTQ_RESET, 0x120);
9656 GET_REG32_LOOP(MSGINT_MODE, 0x0c);
9657 GET_REG32_1(DMAC_MODE);
9658 GET_REG32_LOOP(GRC_MODE, 0x4c);
9659 if (tp->tg3_flags & TG3_FLAG_NVRAM)
9660 GET_REG32_LOOP(NVRAM_CMD, 0x24);
9661
9662#undef __GET_REG32
9663#undef GET_REG32_LOOP
9664#undef GET_REG32_1
9665
David S. Millerf47c11e2005-06-24 20:18:35 -07009666 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009667}
9668
9669static int tg3_get_eeprom_len(struct net_device *dev)
9670{
9671 struct tg3 *tp = netdev_priv(dev);
9672
9673 return tp->nvram_size;
9674}
9675
Linus Torvalds1da177e2005-04-16 15:20:36 -07009676static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9677{
9678 struct tg3 *tp = netdev_priv(dev);
9679 int ret;
9680 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -08009681 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009682 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009683
Matt Carlsondf259d82009-04-20 06:57:14 +00009684 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM)
9685 return -EINVAL;
9686
Matt Carlson80096062010-08-02 11:26:06 +00009687 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -08009688 return -EAGAIN;
9689
Linus Torvalds1da177e2005-04-16 15:20:36 -07009690 offset = eeprom->offset;
9691 len = eeprom->len;
9692 eeprom->len = 0;
9693
9694 eeprom->magic = TG3_EEPROM_MAGIC;
9695
9696 if (offset & 3) {
9697 /* adjustments to start on required 4 byte boundary */
9698 b_offset = offset & 3;
9699 b_count = 4 - b_offset;
9700 if (b_count > len) {
9701 /* i.e. offset=1 len=2 */
9702 b_count = len;
9703 }
Matt Carlsona9dc5292009-02-25 14:25:30 +00009704 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009705 if (ret)
9706 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +00009707 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009708 len -= b_count;
9709 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00009710 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009711 }
9712
9713 /* read bytes upto the last 4 byte boundary */
9714 pd = &data[eeprom->len];
9715 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +00009716 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009717 if (ret) {
9718 eeprom->len += i;
9719 return ret;
9720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009721 memcpy(pd + i, &val, 4);
9722 }
9723 eeprom->len += i;
9724
9725 if (len & 3) {
9726 /* read last bytes not ending on 4 byte boundary */
9727 pd = &data[eeprom->len];
9728 b_count = len & 3;
9729 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009730 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009731 if (ret)
9732 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009733 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009734 eeprom->len += b_count;
9735 }
9736 return 0;
9737}
9738
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009739static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009740
9741static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9742{
9743 struct tg3 *tp = netdev_priv(dev);
9744 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009745 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009746 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009747 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009748
Matt Carlson80096062010-08-02 11:26:06 +00009749 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -08009750 return -EAGAIN;
9751
Matt Carlsondf259d82009-04-20 06:57:14 +00009752 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
9753 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009754 return -EINVAL;
9755
9756 offset = eeprom->offset;
9757 len = eeprom->len;
9758
9759 if ((b_offset = (offset & 3))) {
9760 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +00009761 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009762 if (ret)
9763 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009764 len += b_offset;
9765 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -07009766 if (len < 4)
9767 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009768 }
9769
9770 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -07009771 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009772 /* adjustments to end on required 4 byte boundary */
9773 odd_len = 1;
9774 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009775 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009776 if (ret)
9777 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009778 }
9779
9780 buf = data;
9781 if (b_offset || odd_len) {
9782 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009783 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009784 return -ENOMEM;
9785 if (b_offset)
9786 memcpy(buf, &start, 4);
9787 if (odd_len)
9788 memcpy(buf+len-4, &end, 4);
9789 memcpy(buf + b_offset, data, eeprom->len);
9790 }
9791
9792 ret = tg3_nvram_write_block(tp, offset, len, buf);
9793
9794 if (buf != data)
9795 kfree(buf);
9796
9797 return ret;
9798}
9799
9800static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9801{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009802 struct tg3 *tp = netdev_priv(dev);
9803
9804 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009805 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009806 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009807 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009808 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
9809 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009810 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009811
Linus Torvalds1da177e2005-04-16 15:20:36 -07009812 cmd->supported = (SUPPORTED_Autoneg);
9813
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009814 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009815 cmd->supported |= (SUPPORTED_1000baseT_Half |
9816 SUPPORTED_1000baseT_Full);
9817
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009818 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009819 cmd->supported |= (SUPPORTED_100baseT_Half |
9820 SUPPORTED_100baseT_Full |
9821 SUPPORTED_10baseT_Half |
9822 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -08009823 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -07009824 cmd->port = PORT_TP;
9825 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009826 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -07009827 cmd->port = PORT_FIBRE;
9828 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009829
Linus Torvalds1da177e2005-04-16 15:20:36 -07009830 cmd->advertising = tp->link_config.advertising;
9831 if (netif_running(dev)) {
9832 cmd->speed = tp->link_config.active_speed;
9833 cmd->duplex = tp->link_config.active_duplex;
Matt Carlson64c22182010-10-14 10:37:44 +00009834 } else {
9835 cmd->speed = SPEED_INVALID;
9836 cmd->duplex = DUPLEX_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009837 }
Matt Carlson882e9792009-09-01 13:21:36 +00009838 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +00009839 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009840 cmd->autoneg = tp->link_config.autoneg;
9841 cmd->maxtxpkt = 0;
9842 cmd->maxrxpkt = 0;
9843 return 0;
9844}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009845
Linus Torvalds1da177e2005-04-16 15:20:36 -07009846static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9847{
9848 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009849
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009850 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009851 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009852 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009853 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009854 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
9855 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009856 }
9857
Matt Carlson7e5856b2009-02-25 14:23:01 +00009858 if (cmd->autoneg != AUTONEG_ENABLE &&
9859 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -07009860 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +00009861
9862 if (cmd->autoneg == AUTONEG_DISABLE &&
9863 cmd->duplex != DUPLEX_FULL &&
9864 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -07009865 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009866
Matt Carlson7e5856b2009-02-25 14:23:01 +00009867 if (cmd->autoneg == AUTONEG_ENABLE) {
9868 u32 mask = ADVERTISED_Autoneg |
9869 ADVERTISED_Pause |
9870 ADVERTISED_Asym_Pause;
9871
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009872 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +00009873 mask |= ADVERTISED_1000baseT_Half |
9874 ADVERTISED_1000baseT_Full;
9875
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009876 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +00009877 mask |= ADVERTISED_100baseT_Half |
9878 ADVERTISED_100baseT_Full |
9879 ADVERTISED_10baseT_Half |
9880 ADVERTISED_10baseT_Full |
9881 ADVERTISED_TP;
9882 else
9883 mask |= ADVERTISED_FIBRE;
9884
9885 if (cmd->advertising & ~mask)
9886 return -EINVAL;
9887
9888 mask &= (ADVERTISED_1000baseT_Half |
9889 ADVERTISED_1000baseT_Full |
9890 ADVERTISED_100baseT_Half |
9891 ADVERTISED_100baseT_Full |
9892 ADVERTISED_10baseT_Half |
9893 ADVERTISED_10baseT_Full);
9894
9895 cmd->advertising &= mask;
9896 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009897 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
Matt Carlson7e5856b2009-02-25 14:23:01 +00009898 if (cmd->speed != SPEED_1000)
9899 return -EINVAL;
9900
9901 if (cmd->duplex != DUPLEX_FULL)
9902 return -EINVAL;
9903 } else {
9904 if (cmd->speed != SPEED_100 &&
9905 cmd->speed != SPEED_10)
9906 return -EINVAL;
9907 }
9908 }
9909
David S. Millerf47c11e2005-06-24 20:18:35 -07009910 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009911
9912 tp->link_config.autoneg = cmd->autoneg;
9913 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -07009914 tp->link_config.advertising = (cmd->advertising |
9915 ADVERTISED_Autoneg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009916 tp->link_config.speed = SPEED_INVALID;
9917 tp->link_config.duplex = DUPLEX_INVALID;
9918 } else {
9919 tp->link_config.advertising = 0;
9920 tp->link_config.speed = cmd->speed;
9921 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009922 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009923
Michael Chan24fcad62006-12-17 17:06:46 -08009924 tp->link_config.orig_speed = tp->link_config.speed;
9925 tp->link_config.orig_duplex = tp->link_config.duplex;
9926 tp->link_config.orig_autoneg = tp->link_config.autoneg;
9927
Linus Torvalds1da177e2005-04-16 15:20:36 -07009928 if (netif_running(dev))
9929 tg3_setup_phy(tp, 1);
9930
David S. Millerf47c11e2005-06-24 20:18:35 -07009931 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009932
Linus Torvalds1da177e2005-04-16 15:20:36 -07009933 return 0;
9934}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009935
Linus Torvalds1da177e2005-04-16 15:20:36 -07009936static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
9937{
9938 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009939
Linus Torvalds1da177e2005-04-16 15:20:36 -07009940 strcpy(info->driver, DRV_MODULE_NAME);
9941 strcpy(info->version, DRV_MODULE_VERSION);
Michael Chanc4e65752006-03-20 22:29:32 -08009942 strcpy(info->fw_version, tp->fw_ver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009943 strcpy(info->bus_info, pci_name(tp->pdev));
9944}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009945
Linus Torvalds1da177e2005-04-16 15:20:36 -07009946static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
9947{
9948 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009949
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009950 if ((tp->tg3_flags & TG3_FLAG_WOL_CAP) &&
9951 device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -07009952 wol->supported = WAKE_MAGIC;
9953 else
9954 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009955 wol->wolopts = 0;
Matt Carlson05ac4cb2008-11-03 16:53:46 -08009956 if ((tp->tg3_flags & TG3_FLAG_WOL_ENABLE) &&
9957 device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009958 wol->wolopts = WAKE_MAGIC;
9959 memset(&wol->sopass, 0, sizeof(wol->sopass));
9960}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009961
Linus Torvalds1da177e2005-04-16 15:20:36 -07009962static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
9963{
9964 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009965 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009966
Linus Torvalds1da177e2005-04-16 15:20:36 -07009967 if (wol->wolopts & ~WAKE_MAGIC)
9968 return -EINVAL;
9969 if ((wol->wolopts & WAKE_MAGIC) &&
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009970 !((tp->tg3_flags & TG3_FLAG_WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009971 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009972
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +00009973 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
9974
David S. Millerf47c11e2005-06-24 20:18:35 -07009975 spin_lock_bh(&tp->lock);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +00009976 if (device_may_wakeup(dp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009977 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +00009978 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07009979 tp->tg3_flags &= ~TG3_FLAG_WOL_ENABLE;
David S. Millerf47c11e2005-06-24 20:18:35 -07009980 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009981
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +00009982
Linus Torvalds1da177e2005-04-16 15:20:36 -07009983 return 0;
9984}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009985
Linus Torvalds1da177e2005-04-16 15:20:36 -07009986static u32 tg3_get_msglevel(struct net_device *dev)
9987{
9988 struct tg3 *tp = netdev_priv(dev);
9989 return tp->msg_enable;
9990}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009991
Linus Torvalds1da177e2005-04-16 15:20:36 -07009992static void tg3_set_msglevel(struct net_device *dev, u32 value)
9993{
9994 struct tg3 *tp = netdev_priv(dev);
9995 tp->msg_enable = value;
9996}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009997
Linus Torvalds1da177e2005-04-16 15:20:36 -07009998static int tg3_set_tso(struct net_device *dev, u32 value)
9999{
10000 struct tg3 *tp = netdev_priv(dev);
10001
10002 if (!(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
10003 if (value)
10004 return -EINVAL;
10005 return 0;
10006 }
Matt Carlson027455a2008-12-21 20:19:30 -080010007 if ((dev->features & NETIF_F_IPV6_CSUM) &&
Matt Carlsone849cdc2009-11-13 13:03:38 +000010008 ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_2) ||
10009 (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3))) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070010010 if (value) {
Michael Chanb0026622006-07-03 19:42:14 -070010011 dev->features |= NETIF_F_TSO6;
Matt Carlsone849cdc2009-11-13 13:03:38 +000010012 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
10013 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070010014 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
10015 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080010016 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000010017 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson9936bcf2007-10-10 18:03:07 -070010018 dev->features |= NETIF_F_TSO_ECN;
10019 } else
10020 dev->features &= ~(NETIF_F_TSO6 | NETIF_F_TSO_ECN);
Michael Chanb0026622006-07-03 19:42:14 -070010021 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010022 return ethtool_op_set_tso(dev, value);
10023}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010024
Linus Torvalds1da177e2005-04-16 15:20:36 -070010025static int tg3_nway_reset(struct net_device *dev)
10026{
10027 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010028 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010029
Linus Torvalds1da177e2005-04-16 15:20:36 -070010030 if (!netif_running(dev))
10031 return -EAGAIN;
10032
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010033 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070010034 return -EINVAL;
10035
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010036 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010037 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010038 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010039 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010040 } else {
10041 u32 bmcr;
10042
10043 spin_lock_bh(&tp->lock);
10044 r = -EINVAL;
10045 tg3_readphy(tp, MII_BMCR, &bmcr);
10046 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
10047 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010048 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010049 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
10050 BMCR_ANENABLE);
10051 r = 0;
10052 }
10053 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010054 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010055
Linus Torvalds1da177e2005-04-16 15:20:36 -070010056 return r;
10057}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010058
Linus Torvalds1da177e2005-04-16 15:20:36 -070010059static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10060{
10061 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010062
Matt Carlson2c49a442010-09-30 10:34:35 +000010063 ering->rx_max_pending = tp->rx_std_ring_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010064 ering->rx_mini_max_pending = 0;
Michael Chan4f81c322006-03-20 21:33:42 -080010065 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE)
Matt Carlson2c49a442010-09-30 10:34:35 +000010066 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080010067 else
10068 ering->rx_jumbo_max_pending = 0;
10069
10070 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010071
10072 ering->rx_pending = tp->rx_pending;
10073 ering->rx_mini_pending = 0;
Michael Chan4f81c322006-03-20 21:33:42 -080010074 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE)
10075 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
10076 else
10077 ering->rx_jumbo_pending = 0;
10078
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010079 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010080}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010081
Linus Torvalds1da177e2005-04-16 15:20:36 -070010082static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10083{
10084 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000010085 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010086
Matt Carlson2c49a442010-09-30 10:34:35 +000010087 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
10088 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070010089 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
10090 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Michael Chan7f62ad52007-02-20 23:25:40 -080010091 ((tp->tg3_flags2 & TG3_FLG2_TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070010092 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010093 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010094
Michael Chanbbe832c2005-06-24 20:20:04 -070010095 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010096 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010097 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010098 irq_sync = 1;
10099 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010100
Michael Chanbbe832c2005-06-24 20:20:04 -070010101 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010102
Linus Torvalds1da177e2005-04-16 15:20:36 -070010103 tp->rx_pending = ering->rx_pending;
10104
10105 if ((tp->tg3_flags2 & TG3_FLG2_MAX_RXPEND_64) &&
10106 tp->rx_pending > 63)
10107 tp->rx_pending = 63;
10108 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000010109
Matt Carlson6fd45cb2010-09-15 08:59:57 +000010110 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000010111 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010112
10113 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070010114 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -070010115 err = tg3_restart_hw(tp, 1);
10116 if (!err)
10117 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010118 }
10119
David S. Millerf47c11e2005-06-24 20:18:35 -070010120 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010121
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010122 if (irq_sync && !err)
10123 tg3_phy_start(tp);
10124
Michael Chanb9ec6c12006-07-25 16:37:27 -070010125 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010126}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010127
Linus Torvalds1da177e2005-04-16 15:20:36 -070010128static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10129{
10130 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010131
Linus Torvalds1da177e2005-04-16 15:20:36 -070010132 epause->autoneg = (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG) != 0;
Matt Carlson8d018622007-12-20 20:05:44 -080010133
Steve Glendinninge18ce342008-12-16 02:00:00 -080010134 if (tp->link_config.active_flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080010135 epause->rx_pause = 1;
10136 else
10137 epause->rx_pause = 0;
10138
Steve Glendinninge18ce342008-12-16 02:00:00 -080010139 if (tp->link_config.active_flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080010140 epause->tx_pause = 1;
10141 else
10142 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010143}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010144
Linus Torvalds1da177e2005-04-16 15:20:36 -070010145static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10146{
10147 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010148 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010149
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010150 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson27121682010-02-17 15:16:57 +000010151 u32 newadv;
10152 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010153
Matt Carlson27121682010-02-17 15:16:57 +000010154 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010155
Matt Carlson27121682010-02-17 15:16:57 +000010156 if (!(phydev->supported & SUPPORTED_Pause) ||
10157 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000010158 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000010159 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010160
Matt Carlson27121682010-02-17 15:16:57 +000010161 tp->link_config.flowctrl = 0;
10162 if (epause->rx_pause) {
10163 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010164
Matt Carlson27121682010-02-17 15:16:57 +000010165 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080010166 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000010167 newadv = ADVERTISED_Pause;
10168 } else
10169 newadv = ADVERTISED_Pause |
10170 ADVERTISED_Asym_Pause;
10171 } else if (epause->tx_pause) {
10172 tp->link_config.flowctrl |= FLOW_CTRL_TX;
10173 newadv = ADVERTISED_Asym_Pause;
10174 } else
10175 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010176
Matt Carlson27121682010-02-17 15:16:57 +000010177 if (epause->autoneg)
10178 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
10179 else
10180 tp->tg3_flags &= ~TG3_FLAG_PAUSE_AUTONEG;
10181
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010182 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000010183 u32 oldadv = phydev->advertising &
10184 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
10185 if (oldadv != newadv) {
10186 phydev->advertising &=
10187 ~(ADVERTISED_Pause |
10188 ADVERTISED_Asym_Pause);
10189 phydev->advertising |= newadv;
10190 if (phydev->autoneg) {
10191 /*
10192 * Always renegotiate the link to
10193 * inform our link partner of our
10194 * flow control settings, even if the
10195 * flow control is forced. Let
10196 * tg3_adjust_link() do the final
10197 * flow control setup.
10198 */
10199 return phy_start_aneg(phydev);
10200 }
10201 }
10202
10203 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010204 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000010205 } else {
10206 tp->link_config.orig_advertising &=
10207 ~(ADVERTISED_Pause |
10208 ADVERTISED_Asym_Pause);
10209 tp->link_config.orig_advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010210 }
10211 } else {
10212 int irq_sync = 0;
10213
10214 if (netif_running(dev)) {
10215 tg3_netif_stop(tp);
10216 irq_sync = 1;
10217 }
10218
10219 tg3_full_lock(tp, irq_sync);
10220
10221 if (epause->autoneg)
10222 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
10223 else
10224 tp->tg3_flags &= ~TG3_FLAG_PAUSE_AUTONEG;
10225 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010226 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010227 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010228 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010229 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010230 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010231 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010232 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010233
10234 if (netif_running(dev)) {
10235 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10236 err = tg3_restart_hw(tp, 1);
10237 if (!err)
10238 tg3_netif_start(tp);
10239 }
10240
10241 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010243
Michael Chanb9ec6c12006-07-25 16:37:27 -070010244 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010245}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010246
Linus Torvalds1da177e2005-04-16 15:20:36 -070010247static u32 tg3_get_rx_csum(struct net_device *dev)
10248{
10249 struct tg3 *tp = netdev_priv(dev);
10250 return (tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) != 0;
10251}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010252
Linus Torvalds1da177e2005-04-16 15:20:36 -070010253static int tg3_set_rx_csum(struct net_device *dev, u32 data)
10254{
10255 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010256
Linus Torvalds1da177e2005-04-16 15:20:36 -070010257 if (tp->tg3_flags & TG3_FLAG_BROKEN_CHECKSUMS) {
10258 if (data != 0)
10259 return -EINVAL;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010260 return 0;
10261 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010262
David S. Millerf47c11e2005-06-24 20:18:35 -070010263 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010264 if (data)
10265 tp->tg3_flags |= TG3_FLAG_RX_CHECKSUMS;
10266 else
10267 tp->tg3_flags &= ~TG3_FLAG_RX_CHECKSUMS;
David S. Millerf47c11e2005-06-24 20:18:35 -070010268 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010269
Linus Torvalds1da177e2005-04-16 15:20:36 -070010270 return 0;
10271}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010272
Linus Torvalds1da177e2005-04-16 15:20:36 -070010273static int tg3_set_tx_csum(struct net_device *dev, u32 data)
10274{
10275 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010276
Linus Torvalds1da177e2005-04-16 15:20:36 -070010277 if (tp->tg3_flags & TG3_FLAG_BROKEN_CHECKSUMS) {
10278 if (data != 0)
10279 return -EINVAL;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010280 return 0;
10281 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010282
Matt Carlson321d32a2008-11-21 17:22:19 -080010283 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Michael Chan6460d942007-07-14 19:07:52 -070010284 ethtool_op_set_tx_ipv6_csum(dev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010285 else
Michael Chan9c27dbd2006-03-20 22:28:27 -080010286 ethtool_op_set_tx_csum(dev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010287
10288 return 0;
10289}
10290
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010291static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010292{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010293 switch (sset) {
10294 case ETH_SS_TEST:
10295 return TG3_NUM_TEST;
10296 case ETH_SS_STATS:
10297 return TG3_NUM_STATS;
10298 default:
10299 return -EOPNOTSUPP;
10300 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070010301}
10302
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010303static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010304{
10305 switch (stringset) {
10306 case ETH_SS_STATS:
10307 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
10308 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070010309 case ETH_SS_TEST:
10310 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
10311 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010312 default:
10313 WARN_ON(1); /* we need a WARN() */
10314 break;
10315 }
10316}
10317
Michael Chan4009a932005-09-05 17:52:54 -070010318static int tg3_phys_id(struct net_device *dev, u32 data)
10319{
10320 struct tg3 *tp = netdev_priv(dev);
10321 int i;
10322
10323 if (!netif_running(tp->dev))
10324 return -EAGAIN;
10325
10326 if (data == 0)
Stephen Hemminger759afc32008-02-23 19:51:59 -080010327 data = UINT_MAX / 2;
Michael Chan4009a932005-09-05 17:52:54 -070010328
10329 for (i = 0; i < (data * 2); i++) {
10330 if ((i % 2) == 0)
10331 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10332 LED_CTRL_1000MBPS_ON |
10333 LED_CTRL_100MBPS_ON |
10334 LED_CTRL_10MBPS_ON |
10335 LED_CTRL_TRAFFIC_OVERRIDE |
10336 LED_CTRL_TRAFFIC_BLINK |
10337 LED_CTRL_TRAFFIC_LED);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010338
Michael Chan4009a932005-09-05 17:52:54 -070010339 else
10340 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10341 LED_CTRL_TRAFFIC_OVERRIDE);
10342
10343 if (msleep_interruptible(500))
10344 break;
10345 }
10346 tw32(MAC_LED_CTRL, tp->led_ctrl);
10347 return 0;
10348}
10349
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010350static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010351 struct ethtool_stats *estats, u64 *tmp_stats)
10352{
10353 struct tg3 *tp = netdev_priv(dev);
10354 memcpy(tmp_stats, tg3_get_estats(tp), sizeof(tp->estats));
10355}
10356
Michael Chan566f86a2005-05-29 14:56:58 -070010357#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080010358#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
10359#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
10360#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Michael Chanb16250e2006-09-27 16:10:14 -070010361#define NVRAM_SELFBOOT_HW_SIZE 0x20
10362#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070010363
10364static int tg3_test_nvram(struct tg3 *tp)
10365{
Al Virob9fc7dc2007-12-17 22:59:57 -080010366 u32 csum, magic;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010367 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010368 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070010369
Matt Carlsondf259d82009-04-20 06:57:14 +000010370 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM)
10371 return 0;
10372
Matt Carlsone4f34112009-02-25 14:25:00 +000010373 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080010374 return -EIO;
10375
Michael Chan1b277772006-03-20 22:27:48 -080010376 if (magic == TG3_EEPROM_MAGIC)
10377 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070010378 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080010379 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
10380 TG3_EEPROM_SB_FORMAT_1) {
10381 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
10382 case TG3_EEPROM_SB_REVISION_0:
10383 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
10384 break;
10385 case TG3_EEPROM_SB_REVISION_2:
10386 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
10387 break;
10388 case TG3_EEPROM_SB_REVISION_3:
10389 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
10390 break;
10391 default:
10392 return 0;
10393 }
10394 } else
Michael Chan1b277772006-03-20 22:27:48 -080010395 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070010396 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
10397 size = NVRAM_SELFBOOT_HW_SIZE;
10398 else
Michael Chan1b277772006-03-20 22:27:48 -080010399 return -EIO;
10400
10401 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070010402 if (buf == NULL)
10403 return -ENOMEM;
10404
Michael Chan1b277772006-03-20 22:27:48 -080010405 err = -EIO;
10406 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010407 err = tg3_nvram_read_be32(tp, i, &buf[j]);
10408 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070010409 break;
Michael Chan566f86a2005-05-29 14:56:58 -070010410 }
Michael Chan1b277772006-03-20 22:27:48 -080010411 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070010412 goto out;
10413
Michael Chan1b277772006-03-20 22:27:48 -080010414 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010415 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080010416 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010417 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080010418 u8 *buf8 = (u8 *) buf, csum8 = 0;
10419
Al Virob9fc7dc2007-12-17 22:59:57 -080010420 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080010421 TG3_EEPROM_SB_REVISION_2) {
10422 /* For rev 2, the csum doesn't include the MBA. */
10423 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
10424 csum8 += buf8[i];
10425 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
10426 csum8 += buf8[i];
10427 } else {
10428 for (i = 0; i < size; i++)
10429 csum8 += buf8[i];
10430 }
Michael Chan1b277772006-03-20 22:27:48 -080010431
Adrian Bunkad96b482006-04-05 22:21:04 -070010432 if (csum8 == 0) {
10433 err = 0;
10434 goto out;
10435 }
10436
10437 err = -EIO;
10438 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080010439 }
Michael Chan566f86a2005-05-29 14:56:58 -070010440
Al Virob9fc7dc2007-12-17 22:59:57 -080010441 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010442 TG3_EEPROM_MAGIC_HW) {
10443 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000010444 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070010445 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070010446
10447 /* Separate the parity bits and the data bytes. */
10448 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
10449 if ((i == 0) || (i == 8)) {
10450 int l;
10451 u8 msk;
10452
10453 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
10454 parity[k++] = buf8[i] & msk;
10455 i++;
Matt Carlson859a5882010-04-05 10:19:28 +000010456 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070010457 int l;
10458 u8 msk;
10459
10460 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
10461 parity[k++] = buf8[i] & msk;
10462 i++;
10463
10464 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
10465 parity[k++] = buf8[i] & msk;
10466 i++;
10467 }
10468 data[j++] = buf8[i];
10469 }
10470
10471 err = -EIO;
10472 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
10473 u8 hw8 = hweight8(data[i]);
10474
10475 if ((hw8 & 0x1) && parity[i])
10476 goto out;
10477 else if (!(hw8 & 0x1) && !parity[i])
10478 goto out;
10479 }
10480 err = 0;
10481 goto out;
10482 }
10483
Matt Carlson01c3a392011-03-09 16:58:20 +000010484 err = -EIO;
10485
Michael Chan566f86a2005-05-29 14:56:58 -070010486 /* Bootstrap checksum at offset 0x10 */
10487 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000010488 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070010489 goto out;
10490
10491 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
10492 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000010493 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000010494 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070010495
Matt Carlsond4894f32011-03-09 16:58:21 +000010496 for (i = 0; i < TG3_NVM_VPD_LEN; i += 4) {
10497 /* The data is in little-endian format in NVRAM.
10498 * Use the big-endian read routines to preserve
10499 * the byte order as it exists in NVRAM.
10500 */
10501 if (tg3_nvram_read_be32(tp, TG3_NVM_VPD_OFF + i, &buf[i/4]))
10502 goto out;
10503 }
10504
10505 i = pci_vpd_find_tag((u8 *)buf, 0, TG3_NVM_VPD_LEN,
10506 PCI_VPD_LRDT_RO_DATA);
10507 if (i > 0) {
10508 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
10509 if (j < 0)
10510 goto out;
10511
10512 if (i + PCI_VPD_LRDT_TAG_SIZE + j > TG3_NVM_VPD_LEN)
10513 goto out;
10514
10515 i += PCI_VPD_LRDT_TAG_SIZE;
10516 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
10517 PCI_VPD_RO_KEYWORD_CHKSUM);
10518 if (j > 0) {
10519 u8 csum8 = 0;
10520
10521 j += PCI_VPD_INFO_FLD_HDR_SIZE;
10522
10523 for (i = 0; i <= j; i++)
10524 csum8 += ((u8 *)buf)[i];
10525
10526 if (csum8)
10527 goto out;
10528 }
10529 }
10530
Michael Chan566f86a2005-05-29 14:56:58 -070010531 err = 0;
10532
10533out:
10534 kfree(buf);
10535 return err;
10536}
10537
Michael Chanca430072005-05-29 14:57:23 -070010538#define TG3_SERDES_TIMEOUT_SEC 2
10539#define TG3_COPPER_TIMEOUT_SEC 6
10540
10541static int tg3_test_link(struct tg3 *tp)
10542{
10543 int i, max;
10544
10545 if (!netif_running(tp->dev))
10546 return -ENODEV;
10547
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010548 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070010549 max = TG3_SERDES_TIMEOUT_SEC;
10550 else
10551 max = TG3_COPPER_TIMEOUT_SEC;
10552
10553 for (i = 0; i < max; i++) {
10554 if (netif_carrier_ok(tp->dev))
10555 return 0;
10556
10557 if (msleep_interruptible(1000))
10558 break;
10559 }
10560
10561 return -EIO;
10562}
10563
Michael Chana71116d2005-05-29 14:58:11 -070010564/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080010565static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070010566{
Michael Chanb16250e2006-09-27 16:10:14 -070010567 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070010568 u32 offset, read_mask, write_mask, val, save_val, read_val;
10569 static struct {
10570 u16 offset;
10571 u16 flags;
10572#define TG3_FL_5705 0x1
10573#define TG3_FL_NOT_5705 0x2
10574#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070010575#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070010576 u32 read_mask;
10577 u32 write_mask;
10578 } reg_tbl[] = {
10579 /* MAC Control Registers */
10580 { MAC_MODE, TG3_FL_NOT_5705,
10581 0x00000000, 0x00ef6f8c },
10582 { MAC_MODE, TG3_FL_5705,
10583 0x00000000, 0x01ef6b8c },
10584 { MAC_STATUS, TG3_FL_NOT_5705,
10585 0x03800107, 0x00000000 },
10586 { MAC_STATUS, TG3_FL_5705,
10587 0x03800100, 0x00000000 },
10588 { MAC_ADDR_0_HIGH, 0x0000,
10589 0x00000000, 0x0000ffff },
10590 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010591 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070010592 { MAC_RX_MTU_SIZE, 0x0000,
10593 0x00000000, 0x0000ffff },
10594 { MAC_TX_MODE, 0x0000,
10595 0x00000000, 0x00000070 },
10596 { MAC_TX_LENGTHS, 0x0000,
10597 0x00000000, 0x00003fff },
10598 { MAC_RX_MODE, TG3_FL_NOT_5705,
10599 0x00000000, 0x000007fc },
10600 { MAC_RX_MODE, TG3_FL_5705,
10601 0x00000000, 0x000007dc },
10602 { MAC_HASH_REG_0, 0x0000,
10603 0x00000000, 0xffffffff },
10604 { MAC_HASH_REG_1, 0x0000,
10605 0x00000000, 0xffffffff },
10606 { MAC_HASH_REG_2, 0x0000,
10607 0x00000000, 0xffffffff },
10608 { MAC_HASH_REG_3, 0x0000,
10609 0x00000000, 0xffffffff },
10610
10611 /* Receive Data and Receive BD Initiator Control Registers. */
10612 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
10613 0x00000000, 0xffffffff },
10614 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
10615 0x00000000, 0xffffffff },
10616 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
10617 0x00000000, 0x00000003 },
10618 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
10619 0x00000000, 0xffffffff },
10620 { RCVDBDI_STD_BD+0, 0x0000,
10621 0x00000000, 0xffffffff },
10622 { RCVDBDI_STD_BD+4, 0x0000,
10623 0x00000000, 0xffffffff },
10624 { RCVDBDI_STD_BD+8, 0x0000,
10625 0x00000000, 0xffff0002 },
10626 { RCVDBDI_STD_BD+0xc, 0x0000,
10627 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010628
Michael Chana71116d2005-05-29 14:58:11 -070010629 /* Receive BD Initiator Control Registers. */
10630 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
10631 0x00000000, 0xffffffff },
10632 { RCVBDI_STD_THRESH, TG3_FL_5705,
10633 0x00000000, 0x000003ff },
10634 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
10635 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010636
Michael Chana71116d2005-05-29 14:58:11 -070010637 /* Host Coalescing Control Registers. */
10638 { HOSTCC_MODE, TG3_FL_NOT_5705,
10639 0x00000000, 0x00000004 },
10640 { HOSTCC_MODE, TG3_FL_5705,
10641 0x00000000, 0x000000f6 },
10642 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
10643 0x00000000, 0xffffffff },
10644 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
10645 0x00000000, 0x000003ff },
10646 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
10647 0x00000000, 0xffffffff },
10648 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
10649 0x00000000, 0x000003ff },
10650 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
10651 0x00000000, 0xffffffff },
10652 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10653 0x00000000, 0x000000ff },
10654 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
10655 0x00000000, 0xffffffff },
10656 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10657 0x00000000, 0x000000ff },
10658 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
10659 0x00000000, 0xffffffff },
10660 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
10661 0x00000000, 0xffffffff },
10662 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10663 0x00000000, 0xffffffff },
10664 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10665 0x00000000, 0x000000ff },
10666 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10667 0x00000000, 0xffffffff },
10668 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10669 0x00000000, 0x000000ff },
10670 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
10671 0x00000000, 0xffffffff },
10672 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
10673 0x00000000, 0xffffffff },
10674 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
10675 0x00000000, 0xffffffff },
10676 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
10677 0x00000000, 0xffffffff },
10678 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
10679 0x00000000, 0xffffffff },
10680 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
10681 0xffffffff, 0x00000000 },
10682 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
10683 0xffffffff, 0x00000000 },
10684
10685 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070010686 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010687 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070010688 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010689 0x00000000, 0x007fffff },
10690 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
10691 0x00000000, 0x0000003f },
10692 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
10693 0x00000000, 0x000001ff },
10694 { BUFMGR_MB_HIGH_WATER, 0x0000,
10695 0x00000000, 0x000001ff },
10696 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
10697 0xffffffff, 0x00000000 },
10698 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
10699 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010700
Michael Chana71116d2005-05-29 14:58:11 -070010701 /* Mailbox Registers */
10702 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
10703 0x00000000, 0x000001ff },
10704 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
10705 0x00000000, 0x000001ff },
10706 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
10707 0x00000000, 0x000007ff },
10708 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
10709 0x00000000, 0x000001ff },
10710
10711 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
10712 };
10713
Michael Chanb16250e2006-09-27 16:10:14 -070010714 is_5705 = is_5750 = 0;
10715 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
Michael Chana71116d2005-05-29 14:58:11 -070010716 is_5705 = 1;
Michael Chanb16250e2006-09-27 16:10:14 -070010717 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
10718 is_5750 = 1;
10719 }
Michael Chana71116d2005-05-29 14:58:11 -070010720
10721 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
10722 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
10723 continue;
10724
10725 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
10726 continue;
10727
10728 if ((tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
10729 (reg_tbl[i].flags & TG3_FL_NOT_5788))
10730 continue;
10731
Michael Chanb16250e2006-09-27 16:10:14 -070010732 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
10733 continue;
10734
Michael Chana71116d2005-05-29 14:58:11 -070010735 offset = (u32) reg_tbl[i].offset;
10736 read_mask = reg_tbl[i].read_mask;
10737 write_mask = reg_tbl[i].write_mask;
10738
10739 /* Save the original register content */
10740 save_val = tr32(offset);
10741
10742 /* Determine the read-only value. */
10743 read_val = save_val & read_mask;
10744
10745 /* Write zero to the register, then make sure the read-only bits
10746 * are not changed and the read/write bits are all zeros.
10747 */
10748 tw32(offset, 0);
10749
10750 val = tr32(offset);
10751
10752 /* Test the read-only and read/write bits. */
10753 if (((val & read_mask) != read_val) || (val & write_mask))
10754 goto out;
10755
10756 /* Write ones to all the bits defined by RdMask and WrMask, then
10757 * make sure the read-only bits are not changed and the
10758 * read/write bits are all ones.
10759 */
10760 tw32(offset, read_mask | write_mask);
10761
10762 val = tr32(offset);
10763
10764 /* Test the read-only bits. */
10765 if ((val & read_mask) != read_val)
10766 goto out;
10767
10768 /* Test the read/write bits. */
10769 if ((val & write_mask) != write_mask)
10770 goto out;
10771
10772 tw32(offset, save_val);
10773 }
10774
10775 return 0;
10776
10777out:
Michael Chan9f88f292006-12-07 00:22:54 -080010778 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000010779 netdev_err(tp->dev,
10780 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070010781 tw32(offset, save_val);
10782 return -EIO;
10783}
10784
Michael Chan7942e1d2005-05-29 14:58:36 -070010785static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
10786{
Arjan van de Venf71e1302006-03-03 21:33:57 -050010787 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070010788 int i;
10789 u32 j;
10790
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020010791 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070010792 for (j = 0; j < len; j += 4) {
10793 u32 val;
10794
10795 tg3_write_mem(tp, offset + j, test_pattern[i]);
10796 tg3_read_mem(tp, offset + j, &val);
10797 if (val != test_pattern[i])
10798 return -EIO;
10799 }
10800 }
10801 return 0;
10802}
10803
10804static int tg3_test_memory(struct tg3 *tp)
10805{
10806 static struct mem_entry {
10807 u32 offset;
10808 u32 len;
10809 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080010810 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070010811 { 0x00002000, 0x1c000},
10812 { 0xffffffff, 0x00000}
10813 }, mem_tbl_5705[] = {
10814 { 0x00000100, 0x0000c},
10815 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070010816 { 0x00004000, 0x00800},
10817 { 0x00006000, 0x01000},
10818 { 0x00008000, 0x02000},
10819 { 0x00010000, 0x0e000},
10820 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080010821 }, mem_tbl_5755[] = {
10822 { 0x00000200, 0x00008},
10823 { 0x00004000, 0x00800},
10824 { 0x00006000, 0x00800},
10825 { 0x00008000, 0x02000},
10826 { 0x00010000, 0x0c000},
10827 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070010828 }, mem_tbl_5906[] = {
10829 { 0x00000200, 0x00008},
10830 { 0x00004000, 0x00400},
10831 { 0x00006000, 0x00400},
10832 { 0x00008000, 0x01000},
10833 { 0x00010000, 0x01000},
10834 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000010835 }, mem_tbl_5717[] = {
10836 { 0x00000200, 0x00008},
10837 { 0x00010000, 0x0a000},
10838 { 0x00020000, 0x13c00},
10839 { 0xffffffff, 0x00000}
10840 }, mem_tbl_57765[] = {
10841 { 0x00000200, 0x00008},
10842 { 0x00004000, 0x00800},
10843 { 0x00006000, 0x09800},
10844 { 0x00010000, 0x0a000},
10845 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070010846 };
10847 struct mem_entry *mem_tbl;
10848 int err = 0;
10849 int i;
10850
Matt Carlsona50d0792010-06-05 17:24:37 +000010851 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
10852 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlson8b5a6c42010-01-20 16:58:06 +000010853 mem_tbl = mem_tbl_5717;
10854 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
10855 mem_tbl = mem_tbl_57765;
10856 else if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Matt Carlson321d32a2008-11-21 17:22:19 -080010857 mem_tbl = mem_tbl_5755;
10858 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
10859 mem_tbl = mem_tbl_5906;
10860 else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
10861 mem_tbl = mem_tbl_5705;
10862 else
Michael Chan7942e1d2005-05-29 14:58:36 -070010863 mem_tbl = mem_tbl_570x;
10864
10865 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000010866 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
10867 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070010868 break;
10869 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010870
Michael Chan7942e1d2005-05-29 14:58:36 -070010871 return err;
10872}
10873
Michael Chan9f40dea2005-09-05 17:53:06 -070010874#define TG3_MAC_LOOPBACK 0
10875#define TG3_PHY_LOOPBACK 1
10876
10877static int tg3_run_loopback(struct tg3 *tp, int loopback_mode)
Michael Chanc76949a2005-05-29 14:58:59 -070010878{
Michael Chan9f40dea2005-09-05 17:53:06 -070010879 u32 mac_mode, rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010880 u32 desc_idx, coal_now;
Michael Chanc76949a2005-05-29 14:58:59 -070010881 struct sk_buff *skb, *rx_skb;
10882 u8 *tx_data;
10883 dma_addr_t map;
10884 int num_pkts, tx_len, rx_len, i, err;
10885 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000010886 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000010887 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070010888
Matt Carlsonc8873402010-02-12 14:47:11 +000010889 tnapi = &tp->napi[0];
10890 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000010891 if (tp->irq_cnt > 1) {
Matt Carlson1da85aa2010-09-30 10:34:34 +000010892 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)
10893 rnapi = &tp->napi[1];
Matt Carlsonc8873402010-02-12 14:47:11 +000010894 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
10895 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000010896 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010897 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000010898
Michael Chan9f40dea2005-09-05 17:53:06 -070010899 if (loopback_mode == TG3_MAC_LOOPBACK) {
Michael Chanc94e3942005-09-27 12:12:42 -070010900 /* HW errata - mac loopback fails in some cases on 5780.
10901 * Normal traffic and PHY loopback are not affected by
Matt Carlsonaba49f22011-01-25 15:58:53 +000010902 * errata. Also, the MAC loopback test is deprecated for
10903 * all newer ASIC revisions.
Michael Chanc94e3942005-09-27 12:12:42 -070010904 */
Matt Carlsonaba49f22011-01-25 15:58:53 +000010905 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
10906 (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT))
Michael Chanc94e3942005-09-27 12:12:42 -070010907 return 0;
10908
Matt Carlson49692ca2011-01-25 15:58:52 +000010909 mac_mode = tp->mac_mode &
10910 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
10911 mac_mode |= MAC_MODE_PORT_INT_LPBACK;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010912 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
10913 mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010914 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
Michael Chan3f7045c2006-09-27 16:02:29 -070010915 mac_mode |= MAC_MODE_PORT_MODE_MII;
10916 else
10917 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chan9f40dea2005-09-05 17:53:06 -070010918 tw32(MAC_MODE, mac_mode);
10919 } else if (loopback_mode == TG3_PHY_LOOPBACK) {
Michael Chan3f7045c2006-09-27 16:02:29 -070010920 u32 val;
10921
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010922 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +000010923 tg3_phy_fet_toggle_apd(tp, false);
Michael Chan5d64ad32006-12-07 00:19:40 -080010924 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED100;
10925 } else
10926 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED1000;
Michael Chan3f7045c2006-09-27 16:02:29 -070010927
Matt Carlson9ef8ca92007-07-11 19:48:29 -070010928 tg3_phy_toggle_automdix(tp, 0);
10929
Michael Chan3f7045c2006-09-27 16:02:29 -070010930 tg3_writephy(tp, MII_BMCR, val);
Michael Chanc94e3942005-09-27 12:12:42 -070010931 udelay(40);
Michael Chan5d64ad32006-12-07 00:19:40 -080010932
Matt Carlson49692ca2011-01-25 15:58:52 +000010933 mac_mode = tp->mac_mode &
10934 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010935 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson1061b7c2010-02-12 14:47:12 +000010936 tg3_writephy(tp, MII_TG3_FET_PTEST,
10937 MII_TG3_FET_PTEST_FRC_TX_LINK |
10938 MII_TG3_FET_PTEST_FRC_TX_LOCK);
10939 /* The write needs to be flushed for the AC131 */
10940 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
10941 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
Michael Chan5d64ad32006-12-07 00:19:40 -080010942 mac_mode |= MAC_MODE_PORT_MODE_MII;
10943 } else
10944 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chanb16250e2006-09-27 16:10:14 -070010945
Michael Chanc94e3942005-09-27 12:12:42 -070010946 /* reset to prevent losing 1st rx packet intermittently */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010947 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Michael Chanc94e3942005-09-27 12:12:42 -070010948 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
10949 udelay(10);
10950 tw32_f(MAC_RX_MODE, tp->rx_mode);
10951 }
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010952 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlson79eb6902010-02-17 15:17:03 +000010953 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
10954 if (masked_phy_id == TG3_PHY_ID_BCM5401)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010955 mac_mode &= ~MAC_MODE_LINK_POLARITY;
Matt Carlson79eb6902010-02-17 15:17:03 +000010956 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010957 mac_mode |= MAC_MODE_LINK_POLARITY;
Michael Chanff18ff02006-03-27 23:17:27 -080010958 tg3_writephy(tp, MII_TG3_EXT_CTRL,
10959 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
10960 }
Michael Chan9f40dea2005-09-05 17:53:06 -070010961 tw32(MAC_MODE, mac_mode);
Matt Carlson49692ca2011-01-25 15:58:52 +000010962
10963 /* Wait for link */
10964 for (i = 0; i < 100; i++) {
10965 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
10966 break;
10967 mdelay(1);
10968 }
Matt Carlson859a5882010-04-05 10:19:28 +000010969 } else {
Michael Chan9f40dea2005-09-05 17:53:06 -070010970 return -EINVAL;
Matt Carlson859a5882010-04-05 10:19:28 +000010971 }
Michael Chanc76949a2005-05-29 14:58:59 -070010972
10973 err = -EIO;
10974
Michael Chanc76949a2005-05-29 14:58:59 -070010975 tx_len = 1514;
David S. Millera20e9c62006-07-31 22:38:16 -070010976 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070010977 if (!skb)
10978 return -ENOMEM;
10979
Michael Chanc76949a2005-05-29 14:58:59 -070010980 tx_data = skb_put(skb, tx_len);
10981 memcpy(tx_data, tp->dev->dev_addr, 6);
10982 memset(tx_data + 6, 0x0, 8);
10983
10984 tw32(MAC_RX_MTU_SIZE, tx_len + 4);
10985
10986 for (i = 14; i < tx_len; i++)
10987 tx_data[i] = (u8) (i & 0xff);
10988
Alexander Duyckf4188d82009-12-02 16:48:38 +000010989 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
10990 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000010991 dev_kfree_skb(skb);
10992 return -EIO;
10993 }
Michael Chanc76949a2005-05-29 14:58:59 -070010994
10995 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010996 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070010997
10998 udelay(10);
10999
Matt Carlson898a56f2009-08-28 14:02:40 +000011000 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070011001
Michael Chanc76949a2005-05-29 14:58:59 -070011002 num_pkts = 0;
11003
Alexander Duyckf4188d82009-12-02 16:48:38 +000011004 tg3_set_txd(tnapi, tnapi->tx_prod, map, tx_len, 0, 1);
Michael Chanc76949a2005-05-29 14:58:59 -070011005
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011006 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070011007 num_pkts++;
11008
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011009 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
11010 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070011011
11012 udelay(10);
11013
Matt Carlson303fc922009-11-02 14:27:34 +000011014 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
11015 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070011016 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011017 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011018
11019 udelay(10);
11020
Matt Carlson898a56f2009-08-28 14:02:40 +000011021 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
11022 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011023 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070011024 (rx_idx == (rx_start_idx + num_pkts)))
11025 break;
11026 }
11027
Alexander Duyckf4188d82009-12-02 16:48:38 +000011028 pci_unmap_single(tp->pdev, map, tx_len, PCI_DMA_TODEVICE);
Michael Chanc76949a2005-05-29 14:58:59 -070011029 dev_kfree_skb(skb);
11030
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011031 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070011032 goto out;
11033
11034 if (rx_idx != rx_start_idx + num_pkts)
11035 goto out;
11036
Matt Carlson72334482009-08-28 14:03:01 +000011037 desc = &rnapi->rx_rcb[rx_start_idx];
Michael Chanc76949a2005-05-29 14:58:59 -070011038 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
11039 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
11040 if (opaque_key != RXD_OPAQUE_RING_STD)
11041 goto out;
11042
11043 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
11044 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
11045 goto out;
11046
11047 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) - 4;
11048 if (rx_len != tx_len)
11049 goto out;
11050
Matt Carlson21f581a2009-08-28 14:00:25 +000011051 rx_skb = tpr->rx_std_buffers[desc_idx].skb;
Michael Chanc76949a2005-05-29 14:58:59 -070011052
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +000011053 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx], mapping);
Michael Chanc76949a2005-05-29 14:58:59 -070011054 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len, PCI_DMA_FROMDEVICE);
11055
11056 for (i = 14; i < tx_len; i++) {
11057 if (*(rx_skb->data + i) != (u8) (i & 0xff))
11058 goto out;
11059 }
11060 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011061
Michael Chanc76949a2005-05-29 14:58:59 -070011062 /* tg3_free_rings will unmap and free the rx_skb */
11063out:
11064 return err;
11065}
11066
Michael Chan9f40dea2005-09-05 17:53:06 -070011067#define TG3_MAC_LOOPBACK_FAILED 1
11068#define TG3_PHY_LOOPBACK_FAILED 2
11069#define TG3_LOOPBACK_FAILED (TG3_MAC_LOOPBACK_FAILED | \
11070 TG3_PHY_LOOPBACK_FAILED)
11071
11072static int tg3_test_loopback(struct tg3 *tp)
11073{
11074 int err = 0;
Matt Carlsonab789042011-01-25 15:58:54 +000011075 u32 eee_cap, cpmuctrl = 0;
Michael Chan9f40dea2005-09-05 17:53:06 -070011076
11077 if (!netif_running(tp->dev))
11078 return TG3_LOOPBACK_FAILED;
11079
Matt Carlsonab789042011-01-25 15:58:54 +000011080 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
11081 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
11082
Michael Chanb9ec6c12006-07-25 16:37:27 -070011083 err = tg3_reset_hw(tp, 1);
Matt Carlsonab789042011-01-25 15:58:54 +000011084 if (err) {
11085 err = TG3_LOOPBACK_FAILED;
11086 goto done;
11087 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011088
Matt Carlson6833c042008-11-21 17:18:59 -080011089 /* Turn off gphy autopowerdown. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011090 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -080011091 tg3_phy_toggle_apd(tp, false);
11092
Matt Carlson321d32a2008-11-21 17:22:19 -080011093 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070011094 int i;
11095 u32 status;
11096
11097 tw32(TG3_CPMU_MUTEX_REQ, CPMU_MUTEX_REQ_DRIVER);
11098
11099 /* Wait for up to 40 microseconds to acquire lock. */
11100 for (i = 0; i < 4; i++) {
11101 status = tr32(TG3_CPMU_MUTEX_GNT);
11102 if (status == CPMU_MUTEX_GNT_DRIVER)
11103 break;
11104 udelay(10);
11105 }
11106
Matt Carlsonab789042011-01-25 15:58:54 +000011107 if (status != CPMU_MUTEX_GNT_DRIVER) {
11108 err = TG3_LOOPBACK_FAILED;
11109 goto done;
11110 }
Matt Carlson9936bcf2007-10-10 18:03:07 -070011111
Matt Carlsonb2a5c192008-04-03 21:44:44 -070011112 /* Turn off link-based power management. */
Matt Carlsone8750932007-11-12 21:11:51 -080011113 cpmuctrl = tr32(TG3_CPMU_CTRL);
Matt Carlson109115e2008-05-02 16:48:59 -070011114 tw32(TG3_CPMU_CTRL,
11115 cpmuctrl & ~(CPMU_CTRL_LINK_SPEED_MODE |
11116 CPMU_CTRL_LINK_AWARE_MODE));
Matt Carlson9936bcf2007-10-10 18:03:07 -070011117 }
11118
Michael Chan9f40dea2005-09-05 17:53:06 -070011119 if (tg3_run_loopback(tp, TG3_MAC_LOOPBACK))
11120 err |= TG3_MAC_LOOPBACK_FAILED;
Matt Carlson9936bcf2007-10-10 18:03:07 -070011121
Matt Carlson321d32a2008-11-21 17:22:19 -080011122 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070011123 tw32(TG3_CPMU_CTRL, cpmuctrl);
11124
11125 /* Release the mutex */
11126 tw32(TG3_CPMU_MUTEX_GNT, CPMU_MUTEX_GNT_DRIVER);
11127 }
11128
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011129 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Matt Carlsondd477002008-05-25 23:45:58 -070011130 !(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)) {
Michael Chan9f40dea2005-09-05 17:53:06 -070011131 if (tg3_run_loopback(tp, TG3_PHY_LOOPBACK))
11132 err |= TG3_PHY_LOOPBACK_FAILED;
11133 }
11134
Matt Carlson6833c042008-11-21 17:18:59 -080011135 /* Re-enable gphy autopowerdown. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011136 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -080011137 tg3_phy_toggle_apd(tp, true);
11138
Matt Carlsonab789042011-01-25 15:58:54 +000011139done:
11140 tp->phy_flags |= eee_cap;
11141
Michael Chan9f40dea2005-09-05 17:53:06 -070011142 return err;
11143}
11144
Michael Chan4cafd3f2005-05-29 14:56:34 -070011145static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
11146 u64 *data)
11147{
Michael Chan566f86a2005-05-29 14:56:58 -070011148 struct tg3 *tp = netdev_priv(dev);
11149
Matt Carlson80096062010-08-02 11:26:06 +000011150 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000011151 tg3_power_up(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080011152
Michael Chan566f86a2005-05-29 14:56:58 -070011153 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
11154
11155 if (tg3_test_nvram(tp) != 0) {
11156 etest->flags |= ETH_TEST_FL_FAILED;
11157 data[0] = 1;
11158 }
Michael Chanca430072005-05-29 14:57:23 -070011159 if (tg3_test_link(tp) != 0) {
11160 etest->flags |= ETH_TEST_FL_FAILED;
11161 data[1] = 1;
11162 }
Michael Chana71116d2005-05-29 14:58:11 -070011163 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011164 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070011165
Michael Chanbbe832c2005-06-24 20:20:04 -070011166 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011167 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070011168 tg3_netif_stop(tp);
11169 irq_sync = 1;
11170 }
11171
11172 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070011173
11174 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080011175 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011176 tg3_halt_cpu(tp, RX_CPU_BASE);
11177 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
11178 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080011179 if (!err)
11180 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011181
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011182 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad2006-03-20 22:27:35 -080011183 tg3_phy_reset(tp);
11184
Michael Chana71116d2005-05-29 14:58:11 -070011185 if (tg3_test_registers(tp) != 0) {
11186 etest->flags |= ETH_TEST_FL_FAILED;
11187 data[2] = 1;
11188 }
Michael Chan7942e1d2005-05-29 14:58:36 -070011189 if (tg3_test_memory(tp) != 0) {
11190 etest->flags |= ETH_TEST_FL_FAILED;
11191 data[3] = 1;
11192 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011193 if ((data[4] = tg3_test_loopback(tp)) != 0)
Michael Chanc76949a2005-05-29 14:58:59 -070011194 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070011195
David S. Millerf47c11e2005-06-24 20:18:35 -070011196 tg3_full_unlock(tp);
11197
Michael Chand4bc3922005-05-29 14:59:20 -070011198 if (tg3_test_interrupt(tp) != 0) {
11199 etest->flags |= ETH_TEST_FL_FAILED;
11200 data[5] = 1;
11201 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011202
11203 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070011204
Michael Chana71116d2005-05-29 14:58:11 -070011205 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
11206 if (netif_running(dev)) {
11207 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011208 err2 = tg3_restart_hw(tp, 1);
11209 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070011210 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011211 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011212
11213 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011214
11215 if (irq_sync && !err2)
11216 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011217 }
Matt Carlson80096062010-08-02 11:26:06 +000011218 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000011219 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080011220
Michael Chan4cafd3f2005-05-29 14:56:34 -070011221}
11222
Linus Torvalds1da177e2005-04-16 15:20:36 -070011223static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
11224{
11225 struct mii_ioctl_data *data = if_mii(ifr);
11226 struct tg3 *tp = netdev_priv(dev);
11227 int err;
11228
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011229 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011230 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011231 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011232 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011233 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000011234 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011235 }
11236
Matt Carlson33f401a2010-04-05 10:19:27 +000011237 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011238 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000011239 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011240
11241 /* fallthru */
11242 case SIOCGMIIREG: {
11243 u32 mii_regval;
11244
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011245 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011246 break; /* We have no PHY */
11247
Matt Carlsonf746a312011-01-25 15:58:51 +000011248 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) ||
11249 ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
11250 !netif_running(dev)))
Michael Chanbc1c7562006-03-20 17:48:03 -080011251 return -EAGAIN;
11252
David S. Millerf47c11e2005-06-24 20:18:35 -070011253 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011254 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070011255 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011256
11257 data->val_out = mii_regval;
11258
11259 return err;
11260 }
11261
11262 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011263 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011264 break; /* We have no PHY */
11265
Matt Carlsonf746a312011-01-25 15:58:51 +000011266 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) ||
11267 ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
11268 !netif_running(dev)))
Michael Chanbc1c7562006-03-20 17:48:03 -080011269 return -EAGAIN;
11270
David S. Millerf47c11e2005-06-24 20:18:35 -070011271 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011272 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070011273 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011274
11275 return err;
11276
11277 default:
11278 /* do nothing */
11279 break;
11280 }
11281 return -EOPNOTSUPP;
11282}
11283
David S. Miller15f98502005-05-18 22:49:26 -070011284static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11285{
11286 struct tg3 *tp = netdev_priv(dev);
11287
11288 memcpy(ec, &tp->coal, sizeof(*ec));
11289 return 0;
11290}
11291
Michael Chand244c892005-07-05 14:42:33 -070011292static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11293{
11294 struct tg3 *tp = netdev_priv(dev);
11295 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
11296 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
11297
11298 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
11299 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
11300 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
11301 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
11302 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
11303 }
11304
11305 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
11306 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
11307 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
11308 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
11309 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
11310 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
11311 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
11312 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
11313 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
11314 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
11315 return -EINVAL;
11316
11317 /* No rx interrupts will be generated if both are zero */
11318 if ((ec->rx_coalesce_usecs == 0) &&
11319 (ec->rx_max_coalesced_frames == 0))
11320 return -EINVAL;
11321
11322 /* No tx interrupts will be generated if both are zero */
11323 if ((ec->tx_coalesce_usecs == 0) &&
11324 (ec->tx_max_coalesced_frames == 0))
11325 return -EINVAL;
11326
11327 /* Only copy relevant parameters, ignore all others. */
11328 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
11329 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
11330 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
11331 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
11332 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
11333 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
11334 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
11335 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
11336 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
11337
11338 if (netif_running(dev)) {
11339 tg3_full_lock(tp, 0);
11340 __tg3_set_coalesce(tp, &tp->coal);
11341 tg3_full_unlock(tp);
11342 }
11343 return 0;
11344}
11345
Jeff Garzik7282d492006-09-13 14:30:00 -040011346static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011347 .get_settings = tg3_get_settings,
11348 .set_settings = tg3_set_settings,
11349 .get_drvinfo = tg3_get_drvinfo,
11350 .get_regs_len = tg3_get_regs_len,
11351 .get_regs = tg3_get_regs,
11352 .get_wol = tg3_get_wol,
11353 .set_wol = tg3_set_wol,
11354 .get_msglevel = tg3_get_msglevel,
11355 .set_msglevel = tg3_set_msglevel,
11356 .nway_reset = tg3_nway_reset,
11357 .get_link = ethtool_op_get_link,
11358 .get_eeprom_len = tg3_get_eeprom_len,
11359 .get_eeprom = tg3_get_eeprom,
11360 .set_eeprom = tg3_set_eeprom,
11361 .get_ringparam = tg3_get_ringparam,
11362 .set_ringparam = tg3_set_ringparam,
11363 .get_pauseparam = tg3_get_pauseparam,
11364 .set_pauseparam = tg3_set_pauseparam,
11365 .get_rx_csum = tg3_get_rx_csum,
11366 .set_rx_csum = tg3_set_rx_csum,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011367 .set_tx_csum = tg3_set_tx_csum,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011368 .set_sg = ethtool_op_set_sg,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011369 .set_tso = tg3_set_tso,
Michael Chan4cafd3f2005-05-29 14:56:34 -070011370 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011371 .get_strings = tg3_get_strings,
Michael Chan4009a932005-09-05 17:52:54 -070011372 .phys_id = tg3_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011373 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070011374 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070011375 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070011376 .get_sset_count = tg3_get_sset_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011377};
11378
11379static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
11380{
Michael Chan1b277772006-03-20 22:27:48 -080011381 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011382
11383 tp->nvram_size = EEPROM_CHIP_SIZE;
11384
Matt Carlsone4f34112009-02-25 14:25:00 +000011385 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011386 return;
11387
Michael Chanb16250e2006-09-27 16:10:14 -070011388 if ((magic != TG3_EEPROM_MAGIC) &&
11389 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
11390 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011391 return;
11392
11393 /*
11394 * Size the chip by reading offsets at increasing powers of two.
11395 * When we encounter our validation signature, we know the addressing
11396 * has wrapped around, and thus have our chip size.
11397 */
Michael Chan1b277772006-03-20 22:27:48 -080011398 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011399
11400 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000011401 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011402 return;
11403
Michael Chan18201802006-03-20 22:29:15 -080011404 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011405 break;
11406
11407 cursize <<= 1;
11408 }
11409
11410 tp->nvram_size = cursize;
11411}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011412
Linus Torvalds1da177e2005-04-16 15:20:36 -070011413static void __devinit tg3_get_nvram_size(struct tg3 *tp)
11414{
11415 u32 val;
11416
Matt Carlsondf259d82009-04-20 06:57:14 +000011417 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
11418 tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080011419 return;
11420
11421 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080011422 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080011423 tg3_get_eeprom_size(tp);
11424 return;
11425 }
11426
Matt Carlson6d348f22009-02-25 14:25:52 +000011427 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011428 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000011429 /* This is confusing. We want to operate on the
11430 * 16-bit value at offset 0xf2. The tg3_nvram_read()
11431 * call will read from NVRAM and byteswap the data
11432 * according to the byteswapping settings for all
11433 * other register accesses. This ensures the data we
11434 * want will always reside in the lower 16-bits.
11435 * However, the data in NVRAM is in LE format, which
11436 * means the data from the NVRAM read will always be
11437 * opposite the endianness of the CPU. The 16-bit
11438 * byteswap then brings the data to CPU endianness.
11439 */
11440 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011441 return;
11442 }
11443 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070011444 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011445}
11446
11447static void __devinit tg3_get_nvram_info(struct tg3 *tp)
11448{
11449 u32 nvcfg1;
11450
11451 nvcfg1 = tr32(NVRAM_CFG1);
11452 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
11453 tp->tg3_flags2 |= TG3_FLG2_FLASH;
Matt Carlson8590a602009-08-28 12:29:16 +000011454 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011455 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11456 tw32(NVRAM_CFG1, nvcfg1);
11457 }
11458
Michael Chan4c987482005-09-05 17:52:38 -070011459 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750) ||
Michael Chana4e2b342005-10-26 15:46:52 -070011460 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011461 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011462 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
11463 tp->nvram_jedecnum = JEDEC_ATMEL;
11464 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
11465 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11466 break;
11467 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
11468 tp->nvram_jedecnum = JEDEC_ATMEL;
11469 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
11470 break;
11471 case FLASH_VENDOR_ATMEL_EEPROM:
11472 tp->nvram_jedecnum = JEDEC_ATMEL;
11473 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11474 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11475 break;
11476 case FLASH_VENDOR_ST:
11477 tp->nvram_jedecnum = JEDEC_ST;
11478 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
11479 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11480 break;
11481 case FLASH_VENDOR_SAIFUN:
11482 tp->nvram_jedecnum = JEDEC_SAIFUN;
11483 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
11484 break;
11485 case FLASH_VENDOR_SST_SMALL:
11486 case FLASH_VENDOR_SST_LARGE:
11487 tp->nvram_jedecnum = JEDEC_SST;
11488 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
11489 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011490 }
Matt Carlson8590a602009-08-28 12:29:16 +000011491 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011492 tp->nvram_jedecnum = JEDEC_ATMEL;
11493 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
11494 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11495 }
11496}
11497
Matt Carlsona1b950d2009-09-01 13:20:17 +000011498static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
11499{
11500 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
11501 case FLASH_5752PAGE_SIZE_256:
11502 tp->nvram_pagesize = 256;
11503 break;
11504 case FLASH_5752PAGE_SIZE_512:
11505 tp->nvram_pagesize = 512;
11506 break;
11507 case FLASH_5752PAGE_SIZE_1K:
11508 tp->nvram_pagesize = 1024;
11509 break;
11510 case FLASH_5752PAGE_SIZE_2K:
11511 tp->nvram_pagesize = 2048;
11512 break;
11513 case FLASH_5752PAGE_SIZE_4K:
11514 tp->nvram_pagesize = 4096;
11515 break;
11516 case FLASH_5752PAGE_SIZE_264:
11517 tp->nvram_pagesize = 264;
11518 break;
11519 case FLASH_5752PAGE_SIZE_528:
11520 tp->nvram_pagesize = 528;
11521 break;
11522 }
11523}
11524
Michael Chan361b4ac2005-04-21 17:11:21 -070011525static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
11526{
11527 u32 nvcfg1;
11528
11529 nvcfg1 = tr32(NVRAM_CFG1);
11530
Michael Chane6af3012005-04-21 17:12:05 -070011531 /* NVRAM protection for TPM */
11532 if (nvcfg1 & (1 << 27))
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011533 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Michael Chane6af3012005-04-21 17:12:05 -070011534
Michael Chan361b4ac2005-04-21 17:11:21 -070011535 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011536 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
11537 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
11538 tp->nvram_jedecnum = JEDEC_ATMEL;
11539 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11540 break;
11541 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11542 tp->nvram_jedecnum = JEDEC_ATMEL;
11543 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11544 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11545 break;
11546 case FLASH_5752VENDOR_ST_M45PE10:
11547 case FLASH_5752VENDOR_ST_M45PE20:
11548 case FLASH_5752VENDOR_ST_M45PE40:
11549 tp->nvram_jedecnum = JEDEC_ST;
11550 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11551 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11552 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070011553 }
11554
11555 if (tp->tg3_flags2 & TG3_FLG2_FLASH) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000011556 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000011557 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070011558 /* For eeprom, set pagesize to maximum eeprom size */
11559 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11560
11561 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11562 tw32(NVRAM_CFG1, nvcfg1);
11563 }
11564}
11565
Michael Chand3c7b882006-03-23 01:28:25 -080011566static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
11567{
Matt Carlson989a9d22007-05-05 11:51:05 -070011568 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080011569
11570 nvcfg1 = tr32(NVRAM_CFG1);
11571
11572 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070011573 if (nvcfg1 & (1 << 27)) {
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011574 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Matt Carlson989a9d22007-05-05 11:51:05 -070011575 protect = 1;
11576 }
Michael Chand3c7b882006-03-23 01:28:25 -080011577
Matt Carlson989a9d22007-05-05 11:51:05 -070011578 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11579 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011580 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11581 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11582 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11583 case FLASH_5755VENDOR_ATMEL_FLASH_5:
11584 tp->nvram_jedecnum = JEDEC_ATMEL;
11585 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11586 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11587 tp->nvram_pagesize = 264;
11588 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
11589 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
11590 tp->nvram_size = (protect ? 0x3e200 :
11591 TG3_NVRAM_SIZE_512KB);
11592 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
11593 tp->nvram_size = (protect ? 0x1f200 :
11594 TG3_NVRAM_SIZE_256KB);
11595 else
11596 tp->nvram_size = (protect ? 0x1f200 :
11597 TG3_NVRAM_SIZE_128KB);
11598 break;
11599 case FLASH_5752VENDOR_ST_M45PE10:
11600 case FLASH_5752VENDOR_ST_M45PE20:
11601 case FLASH_5752VENDOR_ST_M45PE40:
11602 tp->nvram_jedecnum = JEDEC_ST;
11603 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11604 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11605 tp->nvram_pagesize = 256;
11606 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
11607 tp->nvram_size = (protect ?
11608 TG3_NVRAM_SIZE_64KB :
11609 TG3_NVRAM_SIZE_128KB);
11610 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
11611 tp->nvram_size = (protect ?
11612 TG3_NVRAM_SIZE_64KB :
11613 TG3_NVRAM_SIZE_256KB);
11614 else
11615 tp->nvram_size = (protect ?
11616 TG3_NVRAM_SIZE_128KB :
11617 TG3_NVRAM_SIZE_512KB);
11618 break;
Michael Chand3c7b882006-03-23 01:28:25 -080011619 }
11620}
11621
Michael Chan1b277772006-03-20 22:27:48 -080011622static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
11623{
11624 u32 nvcfg1;
11625
11626 nvcfg1 = tr32(NVRAM_CFG1);
11627
11628 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011629 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
11630 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11631 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
11632 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11633 tp->nvram_jedecnum = JEDEC_ATMEL;
11634 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11635 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080011636
Matt Carlson8590a602009-08-28 12:29:16 +000011637 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11638 tw32(NVRAM_CFG1, nvcfg1);
11639 break;
11640 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11641 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11642 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11643 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11644 tp->nvram_jedecnum = JEDEC_ATMEL;
11645 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11646 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11647 tp->nvram_pagesize = 264;
11648 break;
11649 case FLASH_5752VENDOR_ST_M45PE10:
11650 case FLASH_5752VENDOR_ST_M45PE20:
11651 case FLASH_5752VENDOR_ST_M45PE40:
11652 tp->nvram_jedecnum = JEDEC_ST;
11653 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11654 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11655 tp->nvram_pagesize = 256;
11656 break;
Michael Chan1b277772006-03-20 22:27:48 -080011657 }
11658}
11659
Matt Carlson6b91fa02007-10-10 18:01:09 -070011660static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
11661{
11662 u32 nvcfg1, protect = 0;
11663
11664 nvcfg1 = tr32(NVRAM_CFG1);
11665
11666 /* NVRAM protection for TPM */
11667 if (nvcfg1 & (1 << 27)) {
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011668 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011669 protect = 1;
11670 }
11671
11672 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11673 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011674 case FLASH_5761VENDOR_ATMEL_ADB021D:
11675 case FLASH_5761VENDOR_ATMEL_ADB041D:
11676 case FLASH_5761VENDOR_ATMEL_ADB081D:
11677 case FLASH_5761VENDOR_ATMEL_ADB161D:
11678 case FLASH_5761VENDOR_ATMEL_MDB021D:
11679 case FLASH_5761VENDOR_ATMEL_MDB041D:
11680 case FLASH_5761VENDOR_ATMEL_MDB081D:
11681 case FLASH_5761VENDOR_ATMEL_MDB161D:
11682 tp->nvram_jedecnum = JEDEC_ATMEL;
11683 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11684 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11685 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
11686 tp->nvram_pagesize = 256;
11687 break;
11688 case FLASH_5761VENDOR_ST_A_M45PE20:
11689 case FLASH_5761VENDOR_ST_A_M45PE40:
11690 case FLASH_5761VENDOR_ST_A_M45PE80:
11691 case FLASH_5761VENDOR_ST_A_M45PE16:
11692 case FLASH_5761VENDOR_ST_M_M45PE20:
11693 case FLASH_5761VENDOR_ST_M_M45PE40:
11694 case FLASH_5761VENDOR_ST_M_M45PE80:
11695 case FLASH_5761VENDOR_ST_M_M45PE16:
11696 tp->nvram_jedecnum = JEDEC_ST;
11697 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11698 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11699 tp->nvram_pagesize = 256;
11700 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011701 }
11702
11703 if (protect) {
11704 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
11705 } else {
11706 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011707 case FLASH_5761VENDOR_ATMEL_ADB161D:
11708 case FLASH_5761VENDOR_ATMEL_MDB161D:
11709 case FLASH_5761VENDOR_ST_A_M45PE16:
11710 case FLASH_5761VENDOR_ST_M_M45PE16:
11711 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
11712 break;
11713 case FLASH_5761VENDOR_ATMEL_ADB081D:
11714 case FLASH_5761VENDOR_ATMEL_MDB081D:
11715 case FLASH_5761VENDOR_ST_A_M45PE80:
11716 case FLASH_5761VENDOR_ST_M_M45PE80:
11717 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
11718 break;
11719 case FLASH_5761VENDOR_ATMEL_ADB041D:
11720 case FLASH_5761VENDOR_ATMEL_MDB041D:
11721 case FLASH_5761VENDOR_ST_A_M45PE40:
11722 case FLASH_5761VENDOR_ST_M_M45PE40:
11723 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11724 break;
11725 case FLASH_5761VENDOR_ATMEL_ADB021D:
11726 case FLASH_5761VENDOR_ATMEL_MDB021D:
11727 case FLASH_5761VENDOR_ST_A_M45PE20:
11728 case FLASH_5761VENDOR_ST_M_M45PE20:
11729 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11730 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011731 }
11732 }
11733}
11734
Michael Chanb5d37722006-09-27 16:06:21 -070011735static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
11736{
11737 tp->nvram_jedecnum = JEDEC_ATMEL;
11738 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11739 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11740}
11741
Matt Carlson321d32a2008-11-21 17:22:19 -080011742static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
11743{
11744 u32 nvcfg1;
11745
11746 nvcfg1 = tr32(NVRAM_CFG1);
11747
11748 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11749 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11750 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11751 tp->nvram_jedecnum = JEDEC_ATMEL;
11752 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11753 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11754
11755 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11756 tw32(NVRAM_CFG1, nvcfg1);
11757 return;
11758 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11759 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11760 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11761 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11762 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11763 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11764 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
11765 tp->nvram_jedecnum = JEDEC_ATMEL;
11766 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11767 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11768
11769 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11770 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11771 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11772 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11773 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11774 break;
11775 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11776 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11777 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11778 break;
11779 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11780 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
11781 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11782 break;
11783 }
11784 break;
11785 case FLASH_5752VENDOR_ST_M45PE10:
11786 case FLASH_5752VENDOR_ST_M45PE20:
11787 case FLASH_5752VENDOR_ST_M45PE40:
11788 tp->nvram_jedecnum = JEDEC_ST;
11789 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11790 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11791
11792 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11793 case FLASH_5752VENDOR_ST_M45PE10:
11794 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11795 break;
11796 case FLASH_5752VENDOR_ST_M45PE20:
11797 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11798 break;
11799 case FLASH_5752VENDOR_ST_M45PE40:
11800 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11801 break;
11802 }
11803 break;
11804 default:
Matt Carlsondf259d82009-04-20 06:57:14 +000011805 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
Matt Carlson321d32a2008-11-21 17:22:19 -080011806 return;
11807 }
11808
Matt Carlsona1b950d2009-09-01 13:20:17 +000011809 tg3_nvram_get_pagesize(tp, nvcfg1);
11810 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Matt Carlson321d32a2008-11-21 17:22:19 -080011811 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011812}
11813
11814
11815static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
11816{
11817 u32 nvcfg1;
11818
11819 nvcfg1 = tr32(NVRAM_CFG1);
11820
11821 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11822 case FLASH_5717VENDOR_ATMEL_EEPROM:
11823 case FLASH_5717VENDOR_MICRO_EEPROM:
11824 tp->nvram_jedecnum = JEDEC_ATMEL;
11825 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11826 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11827
11828 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11829 tw32(NVRAM_CFG1, nvcfg1);
11830 return;
11831 case FLASH_5717VENDOR_ATMEL_MDB011D:
11832 case FLASH_5717VENDOR_ATMEL_ADB011B:
11833 case FLASH_5717VENDOR_ATMEL_ADB011D:
11834 case FLASH_5717VENDOR_ATMEL_MDB021D:
11835 case FLASH_5717VENDOR_ATMEL_ADB021B:
11836 case FLASH_5717VENDOR_ATMEL_ADB021D:
11837 case FLASH_5717VENDOR_ATMEL_45USPT:
11838 tp->nvram_jedecnum = JEDEC_ATMEL;
11839 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11840 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11841
11842 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11843 case FLASH_5717VENDOR_ATMEL_MDB021D:
11844 case FLASH_5717VENDOR_ATMEL_ADB021B:
11845 case FLASH_5717VENDOR_ATMEL_ADB021D:
11846 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11847 break;
11848 default:
11849 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11850 break;
11851 }
Matt Carlson321d32a2008-11-21 17:22:19 -080011852 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011853 case FLASH_5717VENDOR_ST_M_M25PE10:
11854 case FLASH_5717VENDOR_ST_A_M25PE10:
11855 case FLASH_5717VENDOR_ST_M_M45PE10:
11856 case FLASH_5717VENDOR_ST_A_M45PE10:
11857 case FLASH_5717VENDOR_ST_M_M25PE20:
11858 case FLASH_5717VENDOR_ST_A_M25PE20:
11859 case FLASH_5717VENDOR_ST_M_M45PE20:
11860 case FLASH_5717VENDOR_ST_A_M45PE20:
11861 case FLASH_5717VENDOR_ST_25USPT:
11862 case FLASH_5717VENDOR_ST_45USPT:
11863 tp->nvram_jedecnum = JEDEC_ST;
11864 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11865 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11866
11867 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11868 case FLASH_5717VENDOR_ST_M_M25PE20:
11869 case FLASH_5717VENDOR_ST_A_M25PE20:
11870 case FLASH_5717VENDOR_ST_M_M45PE20:
11871 case FLASH_5717VENDOR_ST_A_M45PE20:
11872 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11873 break;
11874 default:
11875 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11876 break;
11877 }
Matt Carlson321d32a2008-11-21 17:22:19 -080011878 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011879 default:
11880 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
11881 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080011882 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000011883
11884 tg3_nvram_get_pagesize(tp, nvcfg1);
11885 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
11886 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
Matt Carlson321d32a2008-11-21 17:22:19 -080011887}
11888
Linus Torvalds1da177e2005-04-16 15:20:36 -070011889/* Chips other than 5700/5701 use the NVRAM for fetching info. */
11890static void __devinit tg3_nvram_init(struct tg3 *tp)
11891{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011892 tw32_f(GRC_EEPROM_ADDR,
11893 (EEPROM_ADDR_FSM_RESET |
11894 (EEPROM_DEFAULT_CLOCK_PERIOD <<
11895 EEPROM_ADDR_CLKPERD_SHIFT)));
11896
Michael Chan9d57f012006-12-07 00:23:25 -080011897 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011898
11899 /* Enable seeprom accesses. */
11900 tw32_f(GRC_LOCAL_CTRL,
11901 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
11902 udelay(100);
11903
11904 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
11905 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
11906 tp->tg3_flags |= TG3_FLAG_NVRAM;
11907
Michael Chanec41c7d2006-01-17 02:40:55 -080011908 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000011909 netdev_warn(tp->dev,
11910 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000011911 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080011912 return;
11913 }
Michael Chane6af3012005-04-21 17:12:05 -070011914 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011915
Matt Carlson989a9d22007-05-05 11:51:05 -070011916 tp->nvram_size = 0;
11917
Michael Chan361b4ac2005-04-21 17:11:21 -070011918 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
11919 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080011920 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
11921 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070011922 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070011923 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
11924 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080011925 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070011926 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
11927 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070011928 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
11929 tg3_get_5906_nvram_info(tp);
Matt Carlsonb703df62009-12-03 08:36:21 +000011930 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
11931 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson321d32a2008-11-21 17:22:19 -080011932 tg3_get_57780_nvram_info(tp);
Matt Carlsona50d0792010-06-05 17:24:37 +000011933 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
11934 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000011935 tg3_get_5717_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070011936 else
11937 tg3_get_nvram_info(tp);
11938
Matt Carlson989a9d22007-05-05 11:51:05 -070011939 if (tp->nvram_size == 0)
11940 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011941
Michael Chane6af3012005-04-21 17:12:05 -070011942 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080011943 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011944
11945 } else {
11946 tp->tg3_flags &= ~(TG3_FLAG_NVRAM | TG3_FLAG_NVRAM_BUFFERED);
11947
11948 tg3_get_eeprom_size(tp);
11949 }
11950}
11951
Linus Torvalds1da177e2005-04-16 15:20:36 -070011952static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
11953 u32 offset, u32 len, u8 *buf)
11954{
11955 int i, j, rc = 0;
11956 u32 val;
11957
11958 for (i = 0; i < len; i += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080011959 u32 addr;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011960 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011961
11962 addr = offset + i;
11963
11964 memcpy(&data, buf + i, 4);
11965
Matt Carlson62cedd12009-04-20 14:52:29 -070011966 /*
11967 * The SEEPROM interface expects the data to always be opposite
11968 * the native endian format. We accomplish this by reversing
11969 * all the operations that would have been performed on the
11970 * data from a call to tg3_nvram_read_be32().
11971 */
11972 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011973
11974 val = tr32(GRC_EEPROM_ADDR);
11975 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
11976
11977 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
11978 EEPROM_ADDR_READ);
11979 tw32(GRC_EEPROM_ADDR, val |
11980 (0 << EEPROM_ADDR_DEVID_SHIFT) |
11981 (addr & EEPROM_ADDR_ADDR_MASK) |
11982 EEPROM_ADDR_START |
11983 EEPROM_ADDR_WRITE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011984
Michael Chan9d57f012006-12-07 00:23:25 -080011985 for (j = 0; j < 1000; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011986 val = tr32(GRC_EEPROM_ADDR);
11987
11988 if (val & EEPROM_ADDR_COMPLETE)
11989 break;
Michael Chan9d57f012006-12-07 00:23:25 -080011990 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011991 }
11992 if (!(val & EEPROM_ADDR_COMPLETE)) {
11993 rc = -EBUSY;
11994 break;
11995 }
11996 }
11997
11998 return rc;
11999}
12000
12001/* offset and length are dword aligned */
12002static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
12003 u8 *buf)
12004{
12005 int ret = 0;
12006 u32 pagesize = tp->nvram_pagesize;
12007 u32 pagemask = pagesize - 1;
12008 u32 nvram_cmd;
12009 u8 *tmp;
12010
12011 tmp = kmalloc(pagesize, GFP_KERNEL);
12012 if (tmp == NULL)
12013 return -ENOMEM;
12014
12015 while (len) {
12016 int j;
Michael Chane6af3012005-04-21 17:12:05 -070012017 u32 phy_addr, page_off, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012018
12019 phy_addr = offset & ~pagemask;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012020
Linus Torvalds1da177e2005-04-16 15:20:36 -070012021 for (j = 0; j < pagesize; j += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012022 ret = tg3_nvram_read_be32(tp, phy_addr + j,
12023 (__be32 *) (tmp + j));
12024 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012025 break;
12026 }
12027 if (ret)
12028 break;
12029
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012030 page_off = offset & pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012031 size = pagesize;
12032 if (len < size)
12033 size = len;
12034
12035 len -= size;
12036
12037 memcpy(tmp + page_off, buf, size);
12038
12039 offset = offset + (pagesize - page_off);
12040
Michael Chane6af3012005-04-21 17:12:05 -070012041 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012042
12043 /*
12044 * Before we can erase the flash page, we need
12045 * to issue a special "write enable" command.
12046 */
12047 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12048
12049 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12050 break;
12051
12052 /* Erase the target page */
12053 tw32(NVRAM_ADDR, phy_addr);
12054
12055 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
12056 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
12057
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012058 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012059 break;
12060
12061 /* Issue another write enable to start the write. */
12062 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12063
12064 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12065 break;
12066
12067 for (j = 0; j < pagesize; j += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012068 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012069
Al Virob9fc7dc2007-12-17 22:59:57 -080012070 data = *((__be32 *) (tmp + j));
Matt Carlsona9dc5292009-02-25 14:25:30 +000012071
Al Virob9fc7dc2007-12-17 22:59:57 -080012072 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012073
12074 tw32(NVRAM_ADDR, phy_addr + j);
12075
12076 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
12077 NVRAM_CMD_WR;
12078
12079 if (j == 0)
12080 nvram_cmd |= NVRAM_CMD_FIRST;
12081 else if (j == (pagesize - 4))
12082 nvram_cmd |= NVRAM_CMD_LAST;
12083
12084 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12085 break;
12086 }
12087 if (ret)
12088 break;
12089 }
12090
12091 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12092 tg3_nvram_exec_cmd(tp, nvram_cmd);
12093
12094 kfree(tmp);
12095
12096 return ret;
12097}
12098
12099/* offset and length are dword aligned */
12100static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
12101 u8 *buf)
12102{
12103 int i, ret = 0;
12104
12105 for (i = 0; i < len; i += 4, offset += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012106 u32 page_off, phy_addr, nvram_cmd;
12107 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012108
12109 memcpy(&data, buf + i, 4);
Al Virob9fc7dc2007-12-17 22:59:57 -080012110 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012111
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012112 page_off = offset % tp->nvram_pagesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012113
Michael Chan18201802006-03-20 22:29:15 -080012114 phy_addr = tg3_nvram_phys_addr(tp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012115
12116 tw32(NVRAM_ADDR, phy_addr);
12117
12118 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
12119
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012120 if (page_off == 0 || i == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012121 nvram_cmd |= NVRAM_CMD_FIRST;
Michael Chanf6d9a252006-04-29 19:00:24 -070012122 if (page_off == (tp->nvram_pagesize - 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012123 nvram_cmd |= NVRAM_CMD_LAST;
12124
12125 if (i == (len - 4))
12126 nvram_cmd |= NVRAM_CMD_LAST;
12127
Matt Carlson321d32a2008-11-21 17:22:19 -080012128 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
12129 !(tp->tg3_flags3 & TG3_FLG3_5755_PLUS) &&
Michael Chan4c987482005-09-05 17:52:38 -070012130 (tp->nvram_jedecnum == JEDEC_ST) &&
12131 (nvram_cmd & NVRAM_CMD_FIRST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012132
12133 if ((ret = tg3_nvram_exec_cmd(tp,
12134 NVRAM_CMD_WREN | NVRAM_CMD_GO |
12135 NVRAM_CMD_DONE)))
12136
12137 break;
12138 }
12139 if (!(tp->tg3_flags2 & TG3_FLG2_FLASH)) {
12140 /* We always do complete word writes to eeprom. */
12141 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
12142 }
12143
12144 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12145 break;
12146 }
12147 return ret;
12148}
12149
12150/* offset and length are dword aligned */
12151static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
12152{
12153 int ret;
12154
Linus Torvalds1da177e2005-04-16 15:20:36 -070012155 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT) {
Michael Chan314fba32005-04-21 17:07:04 -070012156 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
12157 ~GRC_LCLCTRL_GPIO_OUTPUT1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012158 udelay(40);
12159 }
12160
12161 if (!(tp->tg3_flags & TG3_FLAG_NVRAM)) {
12162 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
Matt Carlson859a5882010-04-05 10:19:28 +000012163 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012164 u32 grc_mode;
12165
Michael Chanec41c7d2006-01-17 02:40:55 -080012166 ret = tg3_nvram_lock(tp);
12167 if (ret)
12168 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012169
Michael Chane6af3012005-04-21 17:12:05 -070012170 tg3_enable_nvram_access(tp);
12171 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Matt Carlsonf66a29b2009-11-13 13:03:36 +000012172 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012173 tw32(NVRAM_WRITE1, 0x406);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012174
12175 grc_mode = tr32(GRC_MODE);
12176 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
12177
12178 if ((tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) ||
12179 !(tp->tg3_flags2 & TG3_FLG2_FLASH)) {
12180
12181 ret = tg3_nvram_write_block_buffered(tp, offset, len,
12182 buf);
Matt Carlson859a5882010-04-05 10:19:28 +000012183 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012184 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
12185 buf);
12186 }
12187
12188 grc_mode = tr32(GRC_MODE);
12189 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
12190
Michael Chane6af3012005-04-21 17:12:05 -070012191 tg3_disable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012192 tg3_nvram_unlock(tp);
12193 }
12194
12195 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT) {
Michael Chan314fba32005-04-21 17:07:04 -070012196 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012197 udelay(40);
12198 }
12199
12200 return ret;
12201}
12202
12203struct subsys_tbl_ent {
12204 u16 subsys_vendor, subsys_devid;
12205 u32 phy_id;
12206};
12207
Matt Carlson24daf2b2010-02-17 15:17:02 +000012208static struct subsys_tbl_ent subsys_id_to_phy_id[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012209 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012210 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012211 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012212 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012213 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012214 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012215 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012216 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12217 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
12218 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012219 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012220 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012221 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012222 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12223 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
12224 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012225 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012226 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012227 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012228 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012229 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012230 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012231 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012232
12233 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012234 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012235 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012236 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012237 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012238 { TG3PCI_SUBVENDOR_ID_3COM,
12239 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
12240 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012241 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012242 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012243 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012244
12245 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012246 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012247 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012248 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012249 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012250 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012251 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012252 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012253 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012254
12255 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012256 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012257 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012258 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012259 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012260 { TG3PCI_SUBVENDOR_ID_COMPAQ,
12261 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
12262 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012263 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012264 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012265 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012266
12267 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012268 { TG3PCI_SUBVENDOR_ID_IBM,
12269 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012270};
12271
Matt Carlson24daf2b2010-02-17 15:17:02 +000012272static struct subsys_tbl_ent * __devinit tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012273{
12274 int i;
12275
12276 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
12277 if ((subsys_id_to_phy_id[i].subsys_vendor ==
12278 tp->pdev->subsystem_vendor) &&
12279 (subsys_id_to_phy_id[i].subsys_devid ==
12280 tp->pdev->subsystem_device))
12281 return &subsys_id_to_phy_id[i];
12282 }
12283 return NULL;
12284}
12285
Michael Chan7d0c41e2005-04-21 17:06:20 -070012286static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012287{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012288 u32 val;
Michael Chancaf636c72006-03-22 01:05:31 -080012289 u16 pmcsr;
12290
12291 /* On some early chips the SRAM cannot be accessed in D3hot state,
12292 * so need make sure we're in D0.
12293 */
12294 pci_read_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, &pmcsr);
12295 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
12296 pci_write_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, pmcsr);
12297 msleep(1);
Michael Chan7d0c41e2005-04-21 17:06:20 -070012298
12299 /* Make sure register accesses (indirect or otherwise)
12300 * will function correctly.
12301 */
12302 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
12303 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012304
David S. Millerf49639e2006-06-09 11:58:36 -070012305 /* The memory arbiter has to be enabled in order for SRAM accesses
12306 * to succeed. Normally on powerup the tg3 chip firmware will make
12307 * sure it is enabled, but other entities such as system netboot
12308 * code might disable it.
12309 */
12310 val = tr32(MEMARB_MODE);
12311 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
12312
Matt Carlson79eb6902010-02-17 15:17:03 +000012313 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012314 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12315
Gary Zambranoa85feb82007-05-05 11:52:19 -070012316 /* Assume an onboard device and WOL capable by default. */
12317 tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT | TG3_FLAG_WOL_CAP;
David S. Miller72b845e2006-03-14 14:11:48 -080012318
Michael Chanb5d37722006-09-27 16:06:21 -070012319 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080012320 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Michael Chanb5d37722006-09-27 16:06:21 -070012321 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080012322 tp->tg3_flags2 |= TG3_FLG2_IS_NIC;
12323 }
Matt Carlson0527ba32007-10-10 18:03:30 -070012324 val = tr32(VCPU_CFGSHDW);
12325 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Matt Carlson8ed5d972007-05-07 00:25:49 -070012326 tp->tg3_flags |= TG3_FLAG_ASPM_WORKAROUND;
Matt Carlson0527ba32007-10-10 18:03:30 -070012327 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Matt Carlson20232762008-12-21 20:18:56 -080012328 (val & VCPU_CFGSHDW_WOL_MAGPKT))
Matt Carlson0527ba32007-10-10 18:03:30 -070012329 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012330 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070012331 }
12332
Linus Torvalds1da177e2005-04-16 15:20:36 -070012333 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
12334 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
12335 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070012336 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012337 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012338
12339 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
12340 tp->nic_sram_data_cfg = nic_cfg;
12341
12342 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
12343 ver >>= NIC_SRAM_DATA_VER_SHIFT;
12344 if ((GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700) &&
12345 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) &&
12346 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703) &&
12347 (ver > 0) && (ver < 0x100))
12348 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
12349
Matt Carlsona9daf362008-05-25 23:49:44 -070012350 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
12351 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
12352
Linus Torvalds1da177e2005-04-16 15:20:36 -070012353 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
12354 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
12355 eeprom_phy_serdes = 1;
12356
12357 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
12358 if (nic_phy_id != 0) {
12359 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
12360 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
12361
12362 eeprom_phy_id = (id1 >> 16) << 10;
12363 eeprom_phy_id |= (id2 & 0xfc00) << 16;
12364 eeprom_phy_id |= (id2 & 0x03ff) << 0;
12365 } else
12366 eeprom_phy_id = 0;
12367
Michael Chan7d0c41e2005-04-21 17:06:20 -070012368 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070012369 if (eeprom_phy_serdes) {
Matt Carlsona50d0792010-06-05 17:24:37 +000012370 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012371 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000012372 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012373 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070012374 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070012375
John W. Linvillecbf46852005-04-21 17:01:29 -070012376 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012377 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
12378 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070012379 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070012380 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
12381
12382 switch (led_cfg) {
12383 default:
12384 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
12385 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12386 break;
12387
12388 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
12389 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12390 break;
12391
12392 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
12393 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070012394
12395 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
12396 * read on some older 5700/5701 bootcode.
12397 */
12398 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
12399 ASIC_REV_5700 ||
12400 GET_ASIC_REV(tp->pci_chip_rev_id) ==
12401 ASIC_REV_5701)
12402 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12403
Linus Torvalds1da177e2005-04-16 15:20:36 -070012404 break;
12405
12406 case SHASTA_EXT_LED_SHARED:
12407 tp->led_ctrl = LED_CTRL_MODE_SHARED;
12408 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
12409 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
12410 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12411 LED_CTRL_MODE_PHY_2);
12412 break;
12413
12414 case SHASTA_EXT_LED_MAC:
12415 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
12416 break;
12417
12418 case SHASTA_EXT_LED_COMBO:
12419 tp->led_ctrl = LED_CTRL_MODE_COMBO;
12420 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
12421 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12422 LED_CTRL_MODE_PHY_2);
12423 break;
12424
Stephen Hemminger855e1112008-04-16 16:37:28 -070012425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012426
12427 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
12428 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
12429 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
12430 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12431
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012432 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
12433 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080012434
Michael Chan9d26e212006-12-07 00:21:14 -080012435 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012436 tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080012437 if ((tp->pdev->subsystem_vendor ==
12438 PCI_VENDOR_ID_ARIMA) &&
12439 (tp->pdev->subsystem_device == 0x205a ||
12440 tp->pdev->subsystem_device == 0x2063))
12441 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
12442 } else {
David S. Millerf49639e2006-06-09 11:58:36 -070012443 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080012444 tp->tg3_flags2 |= TG3_FLG2_IS_NIC;
12445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012446
12447 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
12448 tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
John W. Linvillecbf46852005-04-21 17:01:29 -070012449 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012450 tp->tg3_flags2 |= TG3_FLG2_ASF_NEW_HANDSHAKE;
12451 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012452
12453 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
12454 (tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
Matt Carlson0d3031d2007-10-10 18:02:43 -070012455 tp->tg3_flags3 |= TG3_FLG3_ENABLE_APE;
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012456
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012457 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070012458 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
12459 tp->tg3_flags &= ~TG3_FLAG_WOL_CAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012460
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070012461 if ((tp->tg3_flags & TG3_FLAG_WOL_CAP) &&
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012462 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE))
Matt Carlson0527ba32007-10-10 18:03:30 -070012463 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
12464
Linus Torvalds1da177e2005-04-16 15:20:36 -070012465 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012466 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012467
12468 /* serdes signal pre-emphasis in register 0x590 set by */
12469 /* bootcode if bit 18 is set */
12470 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012471 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070012472
Matt Carlson2e1e3292010-11-24 08:31:53 +000012473 if (((tp->tg3_flags3 & TG3_FLG3_5717_PLUS) ||
12474 ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
12475 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX))) &&
Matt Carlson6833c042008-11-21 17:18:59 -080012476 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012477 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080012478
Matt Carlson8c69b1e2010-08-02 11:26:00 +000012479 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
12480 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
12481 !(tp->tg3_flags3 & TG3_FLG3_5717_PLUS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070012482 u32 cfg3;
12483
12484 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
12485 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
12486 tp->tg3_flags |= TG3_FLAG_ASPM_WORKAROUND;
12487 }
Matt Carlsona9daf362008-05-25 23:49:44 -070012488
Matt Carlson14417062010-02-17 15:16:59 +000012489 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
12490 tp->tg3_flags3 |= TG3_FLG3_RGMII_INBAND_DISABLE;
Matt Carlsona9daf362008-05-25 23:49:44 -070012491 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
12492 tp->tg3_flags3 |= TG3_FLG3_RGMII_EXT_IBND_RX_EN;
12493 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
12494 tp->tg3_flags3 |= TG3_FLG3_RGMII_EXT_IBND_TX_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012495 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012496done:
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000012497 if (tp->tg3_flags & TG3_FLAG_WOL_CAP)
12498 device_set_wakeup_enable(&tp->pdev->dev,
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012499 tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000012500 else
12501 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070012502}
12503
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012504static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
12505{
12506 int i;
12507 u32 val;
12508
12509 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
12510 tw32(OTP_CTRL, cmd);
12511
12512 /* Wait for up to 1 ms for command to execute. */
12513 for (i = 0; i < 100; i++) {
12514 val = tr32(OTP_STATUS);
12515 if (val & OTP_STATUS_CMD_DONE)
12516 break;
12517 udelay(10);
12518 }
12519
12520 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
12521}
12522
12523/* Read the gphy configuration from the OTP region of the chip. The gphy
12524 * configuration is a 32-bit value that straddles the alignment boundary.
12525 * We do two 32-bit reads and then shift and merge the results.
12526 */
12527static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
12528{
12529 u32 bhalf_otp, thalf_otp;
12530
12531 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
12532
12533 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
12534 return 0;
12535
12536 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
12537
12538 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12539 return 0;
12540
12541 thalf_otp = tr32(OTP_READ_DATA);
12542
12543 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
12544
12545 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12546 return 0;
12547
12548 bhalf_otp = tr32(OTP_READ_DATA);
12549
12550 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
12551}
12552
Matt Carlsone256f8a2011-03-09 16:58:24 +000012553static void __devinit tg3_phy_init_link_config(struct tg3 *tp)
12554{
12555 u32 adv = ADVERTISED_Autoneg |
12556 ADVERTISED_Pause;
12557
12558 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
12559 adv |= ADVERTISED_1000baseT_Half |
12560 ADVERTISED_1000baseT_Full;
12561
12562 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
12563 adv |= ADVERTISED_100baseT_Half |
12564 ADVERTISED_100baseT_Full |
12565 ADVERTISED_10baseT_Half |
12566 ADVERTISED_10baseT_Full |
12567 ADVERTISED_TP;
12568 else
12569 adv |= ADVERTISED_FIBRE;
12570
12571 tp->link_config.advertising = adv;
12572 tp->link_config.speed = SPEED_INVALID;
12573 tp->link_config.duplex = DUPLEX_INVALID;
12574 tp->link_config.autoneg = AUTONEG_ENABLE;
12575 tp->link_config.active_speed = SPEED_INVALID;
12576 tp->link_config.active_duplex = DUPLEX_INVALID;
12577 tp->link_config.orig_speed = SPEED_INVALID;
12578 tp->link_config.orig_duplex = DUPLEX_INVALID;
12579 tp->link_config.orig_autoneg = AUTONEG_INVALID;
12580}
12581
Michael Chan7d0c41e2005-04-21 17:06:20 -070012582static int __devinit tg3_phy_probe(struct tg3 *tp)
12583{
12584 u32 hw_phy_id_1, hw_phy_id_2;
12585 u32 hw_phy_id, hw_phy_id_masked;
12586 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012587
Matt Carlsone256f8a2011-03-09 16:58:24 +000012588 /* flow control autonegotiation is default behavior */
12589 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
12590 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
12591
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012592 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)
12593 return tg3_phy_init(tp);
12594
Linus Torvalds1da177e2005-04-16 15:20:36 -070012595 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010012596 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012597 */
12598 err = 0;
Matt Carlson0d3031d2007-10-10 18:02:43 -070012599 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
12600 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000012601 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012602 } else {
12603 /* Now read the physical PHY_ID from the chip and verify
12604 * that it is sane. If it doesn't look good, we fall back
12605 * to either the hard-coded table based PHY_ID and failing
12606 * that the value found in the eeprom area.
12607 */
12608 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
12609 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
12610
12611 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
12612 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
12613 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
12614
Matt Carlson79eb6902010-02-17 15:17:03 +000012615 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012616 }
12617
Matt Carlson79eb6902010-02-17 15:17:03 +000012618 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012619 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000012620 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012621 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070012622 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012623 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012624 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000012625 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070012626 /* Do nothing, phy ID already set up in
12627 * tg3_get_eeprom_hw_cfg().
12628 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070012629 } else {
12630 struct subsys_tbl_ent *p;
12631
12632 /* No eeprom signature? Try the hardcoded
12633 * subsys device table.
12634 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012635 p = tg3_lookup_by_subsys(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012636 if (!p)
12637 return -ENODEV;
12638
12639 tp->phy_id = p->phy_id;
12640 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000012641 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012642 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012643 }
12644 }
12645
Matt Carlsona6b68da2010-12-06 08:28:52 +000012646 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
12647 ((tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 &&
12648 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0) ||
12649 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12650 tp->pci_chip_rev_id != CHIPREV_ID_57765_A0)))
Matt Carlson52b02d02010-10-14 10:37:41 +000012651 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
12652
Matt Carlsone256f8a2011-03-09 16:58:24 +000012653 tg3_phy_init_link_config(tp);
12654
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012655 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Matt Carlson0d3031d2007-10-10 18:02:43 -070012656 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070012657 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chan3600d912006-12-07 00:21:48 -080012658 u32 bmsr, adv_reg, tg3_ctrl, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012659
12660 tg3_readphy(tp, MII_BMSR, &bmsr);
12661 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
12662 (bmsr & BMSR_LSTATUS))
12663 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012664
Linus Torvalds1da177e2005-04-16 15:20:36 -070012665 err = tg3_phy_reset(tp);
12666 if (err)
12667 return err;
12668
12669 adv_reg = (ADVERTISE_10HALF | ADVERTISE_10FULL |
12670 ADVERTISE_100HALF | ADVERTISE_100FULL |
12671 ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
12672 tg3_ctrl = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012673 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012674 tg3_ctrl = (MII_TG3_CTRL_ADV_1000_HALF |
12675 MII_TG3_CTRL_ADV_1000_FULL);
12676 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
12677 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
12678 tg3_ctrl |= (MII_TG3_CTRL_AS_MASTER |
12679 MII_TG3_CTRL_ENABLE_AS_MASTER);
12680 }
12681
Michael Chan3600d912006-12-07 00:21:48 -080012682 mask = (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
12683 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
12684 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full);
12685 if (!tg3_copper_is_advertising_all(tp, mask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012686 tg3_writephy(tp, MII_ADVERTISE, adv_reg);
12687
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012688 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012689 tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
12690
12691 tg3_writephy(tp, MII_BMCR,
12692 BMCR_ANENABLE | BMCR_ANRESTART);
12693 }
12694 tg3_phy_set_wirespeed(tp);
12695
12696 tg3_writephy(tp, MII_ADVERTISE, adv_reg);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012697 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012698 tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
12699 }
12700
12701skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000012702 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012703 err = tg3_init_5401phy_dsp(tp);
12704 if (err)
12705 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012706
Linus Torvalds1da177e2005-04-16 15:20:36 -070012707 err = tg3_init_5401phy_dsp(tp);
12708 }
12709
Linus Torvalds1da177e2005-04-16 15:20:36 -070012710 return err;
12711}
12712
Matt Carlson184b8902010-04-05 10:19:25 +000012713static void __devinit tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012714{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000012715 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000012716 unsigned int block_end, rosize, len;
Matt Carlson184b8902010-04-05 10:19:25 +000012717 int j, i = 0;
Michael Chan1b277772006-03-20 22:27:48 -080012718 u32 magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012719
Matt Carlsondf259d82009-04-20 06:57:14 +000012720 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
12721 tg3_nvram_read(tp, 0x0, &magic))
Matt Carlsona4a8bb12010-09-15 09:00:00 +000012722 goto out_no_vpd;
12723
12724 vpd_data = kmalloc(TG3_NVM_VPD_LEN, GFP_KERNEL);
12725 if (!vpd_data)
12726 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012727
Michael Chan18201802006-03-20 22:29:15 -080012728 if (magic == TG3_EEPROM_MAGIC) {
Matt Carlson141518c2009-12-03 08:36:22 +000012729 for (i = 0; i < TG3_NVM_VPD_LEN; i += 4) {
Michael Chan1b277772006-03-20 22:27:48 -080012730 u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012731
Matt Carlson6d348f22009-02-25 14:25:52 +000012732 /* The data is in little-endian format in NVRAM.
12733 * Use the big-endian read routines to preserve
12734 * the byte order as it exists in NVRAM.
12735 */
Matt Carlson141518c2009-12-03 08:36:22 +000012736 if (tg3_nvram_read_be32(tp, TG3_NVM_VPD_OFF + i, &tmp))
Michael Chan1b277772006-03-20 22:27:48 -080012737 goto out_not_found;
12738
Matt Carlson6d348f22009-02-25 14:25:52 +000012739 memcpy(&vpd_data[i], &tmp, sizeof(tmp));
Michael Chan1b277772006-03-20 22:27:48 -080012740 }
12741 } else {
Matt Carlson94c982b2009-12-03 08:36:23 +000012742 ssize_t cnt;
Matt Carlson4181b2c2010-02-26 14:04:45 +000012743 unsigned int pos = 0;
Michael Chan1b277772006-03-20 22:27:48 -080012744
Matt Carlson94c982b2009-12-03 08:36:23 +000012745 for (; pos < TG3_NVM_VPD_LEN && i < 3; i++, pos += cnt) {
12746 cnt = pci_read_vpd(tp->pdev, pos,
12747 TG3_NVM_VPD_LEN - pos,
12748 &vpd_data[pos]);
David Sterba824f5f32010-12-29 03:40:31 +000012749 if (cnt == -ETIMEDOUT || cnt == -EINTR)
Matt Carlson94c982b2009-12-03 08:36:23 +000012750 cnt = 0;
12751 else if (cnt < 0)
David S. Millerf49639e2006-06-09 11:58:36 -070012752 goto out_not_found;
Michael Chan1b277772006-03-20 22:27:48 -080012753 }
Matt Carlson94c982b2009-12-03 08:36:23 +000012754 if (pos != TG3_NVM_VPD_LEN)
12755 goto out_not_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012756 }
12757
Matt Carlson4181b2c2010-02-26 14:04:45 +000012758 i = pci_vpd_find_tag(vpd_data, 0, TG3_NVM_VPD_LEN,
12759 PCI_VPD_LRDT_RO_DATA);
12760 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012761 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000012762
12763 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
12764 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
12765 i += PCI_VPD_LRDT_TAG_SIZE;
12766
12767 if (block_end > TG3_NVM_VPD_LEN)
12768 goto out_not_found;
12769
Matt Carlson184b8902010-04-05 10:19:25 +000012770 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
12771 PCI_VPD_RO_KEYWORD_MFR_ID);
12772 if (j > 0) {
12773 len = pci_vpd_info_field_size(&vpd_data[j]);
12774
12775 j += PCI_VPD_INFO_FLD_HDR_SIZE;
12776 if (j + len > block_end || len != 4 ||
12777 memcmp(&vpd_data[j], "1028", 4))
12778 goto partno;
12779
12780 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
12781 PCI_VPD_RO_KEYWORD_VENDOR0);
12782 if (j < 0)
12783 goto partno;
12784
12785 len = pci_vpd_info_field_size(&vpd_data[j]);
12786
12787 j += PCI_VPD_INFO_FLD_HDR_SIZE;
12788 if (j + len > block_end)
12789 goto partno;
12790
12791 memcpy(tp->fw_ver, &vpd_data[j], len);
12792 strncat(tp->fw_ver, " bc ", TG3_NVM_VPD_LEN - len - 1);
12793 }
12794
12795partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000012796 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
12797 PCI_VPD_RO_KEYWORD_PARTNO);
12798 if (i < 0)
12799 goto out_not_found;
12800
12801 len = pci_vpd_info_field_size(&vpd_data[i]);
12802
12803 i += PCI_VPD_INFO_FLD_HDR_SIZE;
12804 if (len > TG3_BPN_SIZE ||
12805 (len + i) > TG3_NVM_VPD_LEN)
12806 goto out_not_found;
12807
12808 memcpy(tp->board_part_number, &vpd_data[i], len);
12809
Linus Torvalds1da177e2005-04-16 15:20:36 -070012810out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000012811 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000012812 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000012813 return;
12814
12815out_no_vpd:
Matt Carlson37a949c2010-09-30 10:34:33 +000012816 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
12817 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717)
12818 strcpy(tp->board_part_number, "BCM5717");
12819 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
12820 strcpy(tp->board_part_number, "BCM5718");
12821 else
12822 goto nomatch;
12823 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
12824 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
12825 strcpy(tp->board_part_number, "BCM57780");
12826 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
12827 strcpy(tp->board_part_number, "BCM57760");
12828 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
12829 strcpy(tp->board_part_number, "BCM57790");
12830 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
12831 strcpy(tp->board_part_number, "BCM57788");
12832 else
12833 goto nomatch;
12834 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
12835 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
12836 strcpy(tp->board_part_number, "BCM57761");
12837 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
12838 strcpy(tp->board_part_number, "BCM57765");
12839 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
12840 strcpy(tp->board_part_number, "BCM57781");
12841 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
12842 strcpy(tp->board_part_number, "BCM57785");
12843 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
12844 strcpy(tp->board_part_number, "BCM57791");
12845 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
12846 strcpy(tp->board_part_number, "BCM57795");
12847 else
12848 goto nomatch;
12849 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070012850 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000012851 } else {
12852nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070012853 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000012854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012855}
12856
Matt Carlson9c8a6202007-10-21 16:16:08 -070012857static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
12858{
12859 u32 val;
12860
Matt Carlsone4f34112009-02-25 14:25:00 +000012861 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070012862 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000012863 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070012864 val != 0)
12865 return 0;
12866
12867 return 1;
12868}
12869
Matt Carlsonacd9c112009-02-25 14:26:33 +000012870static void __devinit tg3_read_bc_ver(struct tg3 *tp)
12871{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012872 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000012873 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012874 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000012875
12876 if (tg3_nvram_read(tp, 0xc, &offset) ||
12877 tg3_nvram_read(tp, 0x4, &start))
12878 return;
12879
12880 offset = tg3_nvram_logical_addr(tp, offset);
12881
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012882 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000012883 return;
12884
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012885 if ((val & 0xfc000000) == 0x0c000000) {
12886 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000012887 return;
12888
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012889 if (val == 0)
12890 newver = true;
12891 }
12892
Matt Carlson75f99362010-04-05 10:19:24 +000012893 dst_off = strlen(tp->fw_ver);
12894
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012895 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000012896 if (TG3_VER_SIZE - dst_off < 16 ||
12897 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012898 return;
12899
12900 offset = offset + ver_offset - start;
12901 for (i = 0; i < 16; i += 4) {
12902 __be32 v;
12903 if (tg3_nvram_read_be32(tp, offset + i, &v))
12904 return;
12905
Matt Carlson75f99362010-04-05 10:19:24 +000012906 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012907 }
12908 } else {
12909 u32 major, minor;
12910
12911 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
12912 return;
12913
12914 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
12915 TG3_NVM_BCVER_MAJSFT;
12916 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000012917 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
12918 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000012919 }
12920}
12921
Matt Carlsona6f6cb12009-02-25 14:27:43 +000012922static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
12923{
12924 u32 val, major, minor;
12925
12926 /* Use native endian representation */
12927 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
12928 return;
12929
12930 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
12931 TG3_NVM_HWSB_CFG1_MAJSFT;
12932 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
12933 TG3_NVM_HWSB_CFG1_MINSFT;
12934
12935 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
12936}
12937
Matt Carlsondfe00d72008-11-21 17:19:41 -080012938static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
12939{
12940 u32 offset, major, minor, build;
12941
Matt Carlson75f99362010-04-05 10:19:24 +000012942 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080012943
12944 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
12945 return;
12946
12947 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
12948 case TG3_EEPROM_SB_REVISION_0:
12949 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
12950 break;
12951 case TG3_EEPROM_SB_REVISION_2:
12952 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
12953 break;
12954 case TG3_EEPROM_SB_REVISION_3:
12955 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
12956 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000012957 case TG3_EEPROM_SB_REVISION_4:
12958 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
12959 break;
12960 case TG3_EEPROM_SB_REVISION_5:
12961 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
12962 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000012963 case TG3_EEPROM_SB_REVISION_6:
12964 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
12965 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080012966 default:
12967 return;
12968 }
12969
Matt Carlsone4f34112009-02-25 14:25:00 +000012970 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080012971 return;
12972
12973 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
12974 TG3_EEPROM_SB_EDH_BLD_SHFT;
12975 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
12976 TG3_EEPROM_SB_EDH_MAJ_SHFT;
12977 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
12978
12979 if (minor > 99 || build > 26)
12980 return;
12981
Matt Carlson75f99362010-04-05 10:19:24 +000012982 offset = strlen(tp->fw_ver);
12983 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
12984 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080012985
12986 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000012987 offset = strlen(tp->fw_ver);
12988 if (offset < TG3_VER_SIZE - 1)
12989 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080012990 }
12991}
12992
Matt Carlsonacd9c112009-02-25 14:26:33 +000012993static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080012994{
12995 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000012996 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070012997
12998 for (offset = TG3_NVM_DIR_START;
12999 offset < TG3_NVM_DIR_END;
13000 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000013001 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013002 return;
13003
13004 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
13005 break;
13006 }
13007
13008 if (offset == TG3_NVM_DIR_END)
13009 return;
13010
13011 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
13012 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000013013 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013014 return;
13015
Matt Carlsone4f34112009-02-25 14:25:00 +000013016 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013017 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013018 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013019 return;
13020
13021 offset += val - start;
13022
Matt Carlsonacd9c112009-02-25 14:26:33 +000013023 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013024
Matt Carlsonacd9c112009-02-25 14:26:33 +000013025 tp->fw_ver[vlen++] = ',';
13026 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070013027
13028 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000013029 __be32 v;
13030 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013031 return;
13032
Al Virob9fc7dc2007-12-17 22:59:57 -080013033 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013034
Matt Carlsonacd9c112009-02-25 14:26:33 +000013035 if (vlen > TG3_VER_SIZE - sizeof(v)) {
13036 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013037 break;
13038 }
13039
Matt Carlsonacd9c112009-02-25 14:26:33 +000013040 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
13041 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013042 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000013043}
13044
Matt Carlson7fd76442009-02-25 14:27:20 +000013045static void __devinit tg3_read_dash_ver(struct tg3 *tp)
13046{
13047 int vlen;
13048 u32 apedata;
Matt Carlsonecc79642010-08-02 11:26:01 +000013049 char *fwtype;
Matt Carlson7fd76442009-02-25 14:27:20 +000013050
13051 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) ||
13052 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
13053 return;
13054
13055 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
13056 if (apedata != APE_SEG_SIG_MAGIC)
13057 return;
13058
13059 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
13060 if (!(apedata & APE_FW_STATUS_READY))
13061 return;
13062
13063 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
13064
Matt Carlsondc6d0742010-09-15 08:59:55 +000013065 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI) {
13066 tp->tg3_flags3 |= TG3_FLG3_APE_HAS_NCSI;
Matt Carlsonecc79642010-08-02 11:26:01 +000013067 fwtype = "NCSI";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013068 } else {
Matt Carlsonecc79642010-08-02 11:26:01 +000013069 fwtype = "DASH";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013070 }
Matt Carlsonecc79642010-08-02 11:26:01 +000013071
Matt Carlson7fd76442009-02-25 14:27:20 +000013072 vlen = strlen(tp->fw_ver);
13073
Matt Carlsonecc79642010-08-02 11:26:01 +000013074 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
13075 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000013076 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
13077 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
13078 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
13079 (apedata & APE_FW_VERSION_BLDMSK));
13080}
13081
Matt Carlsonacd9c112009-02-25 14:26:33 +000013082static void __devinit tg3_read_fw_ver(struct tg3 *tp)
13083{
13084 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000013085 bool vpd_vers = false;
13086
13087 if (tp->fw_ver[0] != 0)
13088 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013089
Matt Carlsondf259d82009-04-20 06:57:14 +000013090 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) {
Matt Carlson75f99362010-04-05 10:19:24 +000013091 strcat(tp->fw_ver, "sb");
Matt Carlsondf259d82009-04-20 06:57:14 +000013092 return;
13093 }
13094
Matt Carlsonacd9c112009-02-25 14:26:33 +000013095 if (tg3_nvram_read(tp, 0, &val))
13096 return;
13097
13098 if (val == TG3_EEPROM_MAGIC)
13099 tg3_read_bc_ver(tp);
13100 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
13101 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013102 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
13103 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013104 else
13105 return;
13106
13107 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
Matt Carlson75f99362010-04-05 10:19:24 +000013108 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) || vpd_vers)
13109 goto done;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013110
13111 tg3_read_mgmtfw_ver(tp);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013112
Matt Carlson75f99362010-04-05 10:19:24 +000013113done:
Matt Carlson9c8a6202007-10-21 16:16:08 -070013114 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080013115}
13116
Michael Chan7544b092007-05-05 13:08:32 -070013117static struct pci_dev * __devinit tg3_find_peer(struct tg3 *);
13118
Javier Martinez Canillas6303e6e2011-03-26 16:42:33 +000013119static inline void vlan_features_add(struct net_device *dev, unsigned long flags)
Eric Dumazet7fe876a2010-07-08 06:14:55 +000013120{
Eric Dumazet7fe876a2010-07-08 06:14:55 +000013121 dev->vlan_features |= flags;
Eric Dumazet7fe876a2010-07-08 06:14:55 +000013122}
13123
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013124static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
13125{
Matt Carlsonde9f5232011-04-05 14:22:43 +000013126 if (tp->tg3_flags3 & TG3_FLG3_LRG_PROD_RING_CAP)
13127 return TG3_RX_RET_MAX_SIZE_5717;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013128 else if ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) &&
13129 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013130 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013131 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000013132 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013133}
13134
Matt Carlson41434702011-03-09 16:58:22 +000013135static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080013136 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
13137 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
13138 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
13139 { },
13140};
13141
Linus Torvalds1da177e2005-04-16 15:20:36 -070013142static int __devinit tg3_get_invariants(struct tg3 *tp)
13143{
Linus Torvalds1da177e2005-04-16 15:20:36 -070013144 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013145 u32 pci_state_reg, grc_misc_cfg;
13146 u32 val;
13147 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013148 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013149
Linus Torvalds1da177e2005-04-16 15:20:36 -070013150 /* Force memory write invalidate off. If we leave it on,
13151 * then on 5700_BX chips we have to enable a workaround.
13152 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
13153 * to match the cacheline size. The Broadcom driver have this
13154 * workaround but turns MWI off all the times so never uses
13155 * it. This seems to suggest that the workaround is insufficient.
13156 */
13157 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13158 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
13159 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13160
13161 /* It is absolutely critical that TG3PCI_MISC_HOST_CTRL
13162 * has the register indirect write enable bit set before
13163 * we try to access any of the MMIO registers. It is also
13164 * critical that the PCI-X hw workaround situation is decided
13165 * before that as well.
13166 */
13167 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13168 &misc_ctrl_reg);
13169
13170 tp->pci_chip_rev_id = (misc_ctrl_reg >>
13171 MISC_HOST_CTRL_CHIPREV_SHIFT);
Matt Carlson795d01c2007-10-07 23:28:17 -070013172 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
13173 u32 prod_id_asic_rev;
13174
Matt Carlson5001e2f2009-11-13 13:03:51 +000013175 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
13176 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
Matt Carlsona50d0792010-06-05 17:24:37 +000013177 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013178 pci_read_config_dword(tp->pdev,
13179 TG3PCI_GEN2_PRODID_ASICREV,
13180 &prod_id_asic_rev);
Matt Carlsonb703df62009-12-03 08:36:21 +000013181 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
13182 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
13183 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
13184 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
13185 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
13186 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13187 pci_read_config_dword(tp->pdev,
13188 TG3PCI_GEN15_PRODID_ASICREV,
13189 &prod_id_asic_rev);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013190 else
13191 pci_read_config_dword(tp->pdev, TG3PCI_PRODID_ASICREV,
13192 &prod_id_asic_rev);
13193
Matt Carlson321d32a2008-11-21 17:22:19 -080013194 tp->pci_chip_rev_id = prod_id_asic_rev;
Matt Carlson795d01c2007-10-07 23:28:17 -070013195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013196
Michael Chanff645be2005-04-21 17:09:53 -070013197 /* Wrong chip ID in 5752 A0. This code can be removed later
13198 * as A0 is not in production.
13199 */
13200 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
13201 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
13202
Michael Chan68929142005-08-09 20:17:14 -070013203 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
13204 * we need to disable memory and use config. cycles
13205 * only to access all registers. The 5702/03 chips
13206 * can mistakenly decode the special cycles from the
13207 * ICH chipsets as memory write cycles, causing corruption
13208 * of register and memory space. Only certain ICH bridges
13209 * will drive special cycles with non-zero data during the
13210 * address phase which can fall within the 5703's address
13211 * range. This is not an ICH bug as the PCI spec allows
13212 * non-zero address during special cycles. However, only
13213 * these ICH bridges are known to drive non-zero addresses
13214 * during special cycles.
13215 *
13216 * Since special cycles do not cross PCI bridges, we only
13217 * enable this workaround if the 5703 is on the secondary
13218 * bus of these ICH bridges.
13219 */
13220 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
13221 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
13222 static struct tg3_dev_id {
13223 u32 vendor;
13224 u32 device;
13225 u32 rev;
13226 } ich_chipsets[] = {
13227 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
13228 PCI_ANY_ID },
13229 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
13230 PCI_ANY_ID },
13231 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
13232 0xa },
13233 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
13234 PCI_ANY_ID },
13235 { },
13236 };
13237 struct tg3_dev_id *pci_id = &ich_chipsets[0];
13238 struct pci_dev *bridge = NULL;
13239
13240 while (pci_id->vendor != 0) {
13241 bridge = pci_get_device(pci_id->vendor, pci_id->device,
13242 bridge);
13243 if (!bridge) {
13244 pci_id++;
13245 continue;
13246 }
13247 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070013248 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070013249 continue;
13250 }
13251 if (bridge->subordinate &&
13252 (bridge->subordinate->number ==
13253 tp->pdev->bus->number)) {
13254
13255 tp->tg3_flags2 |= TG3_FLG2_ICH_WORKAROUND;
13256 pci_dev_put(bridge);
13257 break;
13258 }
13259 }
13260 }
13261
Matt Carlson41588ba2008-04-19 18:12:33 -070013262 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
13263 static struct tg3_dev_id {
13264 u32 vendor;
13265 u32 device;
13266 } bridge_chipsets[] = {
13267 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
13268 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
13269 { },
13270 };
13271 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
13272 struct pci_dev *bridge = NULL;
13273
13274 while (pci_id->vendor != 0) {
13275 bridge = pci_get_device(pci_id->vendor,
13276 pci_id->device,
13277 bridge);
13278 if (!bridge) {
13279 pci_id++;
13280 continue;
13281 }
13282 if (bridge->subordinate &&
13283 (bridge->subordinate->number <=
13284 tp->pdev->bus->number) &&
13285 (bridge->subordinate->subordinate >=
13286 tp->pdev->bus->number)) {
13287 tp->tg3_flags3 |= TG3_FLG3_5701_DMA_BUG;
13288 pci_dev_put(bridge);
13289 break;
13290 }
13291 }
13292 }
13293
Michael Chan4a29cc22006-03-19 13:21:12 -080013294 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
13295 * DMA addresses > 40-bit. This bridge may have other additional
13296 * 57xx devices behind it in some 4-port NIC designs for example.
13297 * Any tg3 device found behind the bridge will also need the 40-bit
13298 * DMA workaround.
13299 */
Michael Chana4e2b342005-10-26 15:46:52 -070013300 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
13301 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
13302 tp->tg3_flags2 |= TG3_FLG2_5780_CLASS;
Michael Chan4a29cc22006-03-19 13:21:12 -080013303 tp->tg3_flags |= TG3_FLAG_40BIT_DMA_BUG;
Michael Chan4cf78e42005-07-25 12:29:19 -070013304 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a5882010-04-05 10:19:28 +000013305 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080013306 struct pci_dev *bridge = NULL;
13307
13308 do {
13309 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
13310 PCI_DEVICE_ID_SERVERWORKS_EPB,
13311 bridge);
13312 if (bridge && bridge->subordinate &&
13313 (bridge->subordinate->number <=
13314 tp->pdev->bus->number) &&
13315 (bridge->subordinate->subordinate >=
13316 tp->pdev->bus->number)) {
13317 tp->tg3_flags |= TG3_FLAG_40BIT_DMA_BUG;
13318 pci_dev_put(bridge);
13319 break;
13320 }
13321 } while (bridge);
13322 }
Michael Chan4cf78e42005-07-25 12:29:19 -070013323
Linus Torvalds1da177e2005-04-16 15:20:36 -070013324 /* Initialize misc host control in PCI block. */
13325 tp->misc_host_ctrl |= (misc_ctrl_reg &
13326 MISC_HOST_CTRL_CHIPREV);
13327 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13328 tp->misc_host_ctrl);
13329
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013330 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
13331 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
13332 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Michael Chan7544b092007-05-05 13:08:32 -070013333 tp->pdev_peer = tg3_find_peer(tp);
13334
Matt Carlsonc885e822010-08-02 11:25:57 +000013335 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13336 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13337 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
13338 tp->tg3_flags3 |= TG3_FLG3_5717_PLUS;
13339
Matt Carlson321d32a2008-11-21 17:22:19 -080013340 /* Intentionally exclude ASIC_REV_5906 */
13341 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Michael Chand9ab5ad2006-03-20 22:27:35 -080013342 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013343 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013344 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013345 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013346 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlsonc885e822010-08-02 11:25:57 +000013347 (tp->tg3_flags3 & TG3_FLG3_5717_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080013348 tp->tg3_flags3 |= TG3_FLG3_5755_PLUS;
13349
13350 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13351 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Michael Chanb5d37722006-09-27 16:06:21 -070013352 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013353 (tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
Michael Chana4e2b342005-10-26 15:46:52 -070013354 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
John W. Linville6708e5c2005-04-21 17:00:52 -070013355 tp->tg3_flags2 |= TG3_FLG2_5750_PLUS;
13356
John W. Linville1b440c562005-04-21 17:03:18 -070013357 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) ||
13358 (tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
13359 tp->tg3_flags2 |= TG3_FLG2_5705_PLUS;
13360
Matt Carlson027455a2008-12-21 20:19:30 -080013361 /* 5700 B0 chips do not support checksumming correctly due
13362 * to hardware bugs.
13363 */
13364 if (tp->pci_chip_rev_id == CHIPREV_ID_5700_B0)
13365 tp->tg3_flags |= TG3_FLAG_BROKEN_CHECKSUMS;
13366 else {
Eric Dumazet7fe876a2010-07-08 06:14:55 +000013367 unsigned long features = NETIF_F_IP_CSUM | NETIF_F_SG | NETIF_F_GRO;
13368
Matt Carlson027455a2008-12-21 20:19:30 -080013369 tp->tg3_flags |= TG3_FLAG_RX_CHECKSUMS;
Matt Carlson027455a2008-12-21 20:19:30 -080013370 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Eric Dumazet7fe876a2010-07-08 06:14:55 +000013371 features |= NETIF_F_IPV6_CSUM;
13372 tp->dev->features |= features;
13373 vlan_features_add(tp->dev, features);
Matt Carlson027455a2008-12-21 20:19:30 -080013374 }
13375
Matt Carlson507399f2009-11-13 13:03:37 +000013376 /* Determine TSO capabilities */
Matt Carlson2866d952011-02-10 20:06:46 -080013377 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlson4d163b72011-01-25 15:58:48 +000013378 ; /* Do nothing. HW bug. */
13379 else if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)
Matt Carlsone849cdc2009-11-13 13:03:38 +000013380 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_3;
13381 else if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
13382 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlson507399f2009-11-13 13:03:37 +000013383 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_2;
13384 else if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
13385 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_1 | TG3_FLG2_TSO_BUG;
13386 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
13387 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
13388 tp->tg3_flags2 &= ~TG3_FLG2_TSO_BUG;
13389 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13390 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13391 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
13392 tp->tg3_flags2 |= TG3_FLG2_TSO_BUG;
13393 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
13394 tp->fw_needed = FIRMWARE_TG3TSO5;
13395 else
13396 tp->fw_needed = FIRMWARE_TG3TSO;
13397 }
13398
13399 tp->irq_max = 1;
13400
Michael Chan5a6f3072006-03-20 22:28:05 -080013401 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
Michael Chan7544b092007-05-05 13:08:32 -070013402 tp->tg3_flags |= TG3_FLAG_SUPPORT_MSI;
13403 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
13404 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
13405 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
13406 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
13407 tp->pdev_peer == tp->pdev))
13408 tp->tg3_flags &= ~TG3_FLAG_SUPPORT_MSI;
13409
Matt Carlson321d32a2008-11-21 17:22:19 -080013410 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070013411 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanfcfa0a32006-03-20 22:28:41 -080013412 tp->tg3_flags2 |= TG3_FLG2_1SHOT_MSI;
Michael Chan52c0fd82006-06-29 20:15:54 -070013413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013414
Matt Carlsonc885e822010-08-02 11:25:57 +000013415 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
Matt Carlson507399f2009-11-13 13:03:37 +000013416 tp->tg3_flags |= TG3_FLAG_SUPPORT_MSIX;
13417 tp->irq_max = TG3_IRQ_MAX_VECS;
13418 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013419 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000013420
Matt Carlson615774f2009-11-13 13:03:39 +000013421 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsona50d0792010-06-05 17:24:37 +000013422 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlson615774f2009-11-13 13:03:39 +000013423 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
13424 tp->tg3_flags3 |= TG3_FLG3_SHORT_DMA_BUG;
13425 else if (!(tp->tg3_flags3 & TG3_FLG3_5755_PLUS)) {
13426 tp->tg3_flags3 |= TG3_FLG3_4G_DMA_BNDRY_BUG;
13427 tp->tg3_flags3 |= TG3_FLG3_40BIT_DMA_LIMIT_BUG;
Matt Carlson0e1406d2009-11-02 12:33:33 +000013428 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013429
Matt Carlsonde9f5232011-04-05 14:22:43 +000013430 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13431 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
13432 tp->tg3_flags3 |= TG3_FLG3_LRG_PROD_RING_CAP;
13433
Matt Carlson4d163b72011-01-25 15:58:48 +000013434 if ((tp->tg3_flags3 & TG3_FLG3_5717_PLUS) &&
Matt Carlson2866d952011-02-10 20:06:46 -080013435 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5719)
Matt Carlsonb703df62009-12-03 08:36:21 +000013436 tp->tg3_flags3 |= TG3_FLG3_USE_JUMBO_BDFLAG;
13437
Matt Carlsonf51f3562008-05-25 23:45:08 -070013438 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
Matt Carlsonc6cdf432010-04-05 10:19:26 +000013439 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
13440 (tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG))
Matt Carlson8f666b02009-08-28 13:58:24 +000013441 tp->tg3_flags |= TG3_FLAG_JUMBO_CAPABLE;
Michael Chan0f893dc2005-07-25 12:30:38 -070013442
Matt Carlson52f44902008-11-21 17:17:04 -080013443 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
13444 &pci_state_reg);
13445
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013446 tp->pcie_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_EXP);
13447 if (tp->pcie_cap != 0) {
13448 u16 lnkctl;
13449
Linus Torvalds1da177e2005-04-16 15:20:36 -070013450 tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS;
Matt Carlson5f5c51e2007-11-12 21:19:37 -080013451
Matt Carlsoncf790032010-11-24 08:31:48 +000013452 tp->pcie_readrq = 4096;
Matt Carlsonb4495ed2011-01-25 15:58:47 +000013453 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
13454 tp->pcie_readrq = 2048;
Matt Carlsoncf790032010-11-24 08:31:48 +000013455
13456 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080013457
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013458 pci_read_config_word(tp->pdev,
13459 tp->pcie_cap + PCI_EXP_LNKCTL,
13460 &lnkctl);
13461 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
13462 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanc7835a72006-11-15 21:14:42 -080013463 tp->tg3_flags2 &= ~TG3_FLG2_HW_TSO_2;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013464 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013465 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000013466 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
13467 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013468 tp->tg3_flags3 |= TG3_FLG3_CLKREQ_BUG;
Matt Carlson614b05902010-01-20 16:58:02 +000013469 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
13470 tp->tg3_flags3 |= TG3_FLG3_L1PLLPD_EN;
Michael Chanc7835a72006-11-15 21:14:42 -080013471 }
Matt Carlson52f44902008-11-21 17:17:04 -080013472 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Matt Carlsonfcb389d2008-11-03 16:55:44 -080013473 tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS;
Matt Carlson52f44902008-11-21 17:17:04 -080013474 } else if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
13475 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
13476 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
13477 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000013478 dev_err(&tp->pdev->dev,
13479 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080013480 return -EIO;
13481 }
13482
13483 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
13484 tp->tg3_flags |= TG3_FLAG_PCIX_MODE;
13485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013486
Michael Chan399de502005-10-03 14:02:39 -070013487 /* If we have an AMD 762 or VIA K8T800 chipset, write
13488 * reordering to the mailbox registers done by the host
13489 * controller can cause major troubles. We read back from
13490 * every mailbox register write to force the writes to be
13491 * posted to the chip in order.
13492 */
Matt Carlson41434702011-03-09 16:58:22 +000013493 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Michael Chan399de502005-10-03 14:02:39 -070013494 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
13495 tp->tg3_flags |= TG3_FLAG_MBOX_WRITE_REORDER;
13496
Matt Carlson69fc4052008-12-21 20:19:57 -080013497 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
13498 &tp->pci_cacheline_sz);
13499 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13500 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013501 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
13502 tp->pci_lat_timer < 64) {
13503 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080013504 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13505 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013506 }
13507
Matt Carlson52f44902008-11-21 17:17:04 -080013508 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
13509 /* 5700 BX chips need to have their TX producer index
13510 * mailboxes written twice to workaround a bug.
13511 */
13512 tp->tg3_flags |= TG3_FLAG_TXD_MBOX_HWBUG;
Matt Carlson9974a352007-10-07 23:27:28 -070013513
Matt Carlson52f44902008-11-21 17:17:04 -080013514 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013515 *
13516 * The workaround is to use indirect register accesses
13517 * for all chip writes not to mailbox registers.
13518 */
Matt Carlson52f44902008-11-21 17:17:04 -080013519 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013520 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013521
13522 tp->tg3_flags |= TG3_FLAG_PCIX_TARGET_HWBUG;
13523
13524 /* The chip can have it's power management PCI config
13525 * space registers clobbered due to this bug.
13526 * So explicitly force the chip into D0 here.
13527 */
Matt Carlson9974a352007-10-07 23:27:28 -070013528 pci_read_config_dword(tp->pdev,
13529 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013530 &pm_reg);
13531 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
13532 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070013533 pci_write_config_dword(tp->pdev,
13534 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013535 pm_reg);
13536
13537 /* Also, force SERR#/PERR# in PCI command. */
13538 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13539 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
13540 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13541 }
13542 }
13543
Linus Torvalds1da177e2005-04-16 15:20:36 -070013544 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
13545 tp->tg3_flags |= TG3_FLAG_PCI_HIGH_SPEED;
13546 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
13547 tp->tg3_flags |= TG3_FLAG_PCI_32BIT;
13548
13549 /* Chip-specific fixup from Broadcom driver */
13550 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
13551 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
13552 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
13553 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
13554 }
13555
Michael Chan1ee582d2005-08-09 20:16:46 -070013556 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070013557 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013558 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070013559 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070013560 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013561 tp->write32_tx_mbox = tg3_write32;
13562 tp->write32_rx_mbox = tg3_write32;
13563
13564 /* Various workaround register access methods */
13565 if (tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG)
13566 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013567 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
13568 ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
13569 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
13570 /*
13571 * Back to back register writes can cause problems on these
13572 * chips, the workaround is to read back all reg writes
13573 * except those to mailbox regs.
13574 *
13575 * See tg3_write_indirect_reg32().
13576 */
Michael Chan1ee582d2005-08-09 20:16:46 -070013577 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013578 }
13579
Michael Chan1ee582d2005-08-09 20:16:46 -070013580 if ((tp->tg3_flags & TG3_FLAG_TXD_MBOX_HWBUG) ||
13581 (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)) {
13582 tp->write32_tx_mbox = tg3_write32_tx_mbox;
13583 if (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)
13584 tp->write32_rx_mbox = tg3_write_flush_reg32;
13585 }
Michael Chan20094932005-08-09 20:16:32 -070013586
Michael Chan68929142005-08-09 20:17:14 -070013587 if (tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND) {
13588 tp->read32 = tg3_read_indirect_reg32;
13589 tp->write32 = tg3_write_indirect_reg32;
13590 tp->read32_mbox = tg3_read_indirect_mbox;
13591 tp->write32_mbox = tg3_write_indirect_mbox;
13592 tp->write32_tx_mbox = tg3_write_indirect_mbox;
13593 tp->write32_rx_mbox = tg3_write_indirect_mbox;
13594
13595 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070013596 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070013597
13598 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13599 pci_cmd &= ~PCI_COMMAND_MEMORY;
13600 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13601 }
Michael Chanb5d37722006-09-27 16:06:21 -070013602 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
13603 tp->read32_mbox = tg3_read32_mbox_5906;
13604 tp->write32_mbox = tg3_write32_mbox_5906;
13605 tp->write32_tx_mbox = tg3_write32_mbox_5906;
13606 tp->write32_rx_mbox = tg3_write32_mbox_5906;
13607 }
Michael Chan68929142005-08-09 20:17:14 -070013608
Michael Chanbbadf502006-04-06 21:46:34 -070013609 if (tp->write32 == tg3_write_indirect_reg32 ||
13610 ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) &&
13611 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070013612 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Michael Chanbbadf502006-04-06 21:46:34 -070013613 tp->tg3_flags |= TG3_FLAG_SRAM_USE_CONFIG;
13614
Michael Chan7d0c41e2005-04-21 17:06:20 -070013615 /* Get eeprom hw config before calling tg3_set_power_state().
Michael Chan9d26e212006-12-07 00:21:14 -080013616 * In particular, the TG3_FLG2_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070013617 * determined before calling tg3_set_power_state() so that
13618 * we know whether or not to switch out of Vaux power.
13619 * When the flag is set, it means that GPIO1 is used for eeprom
13620 * write protect and also implies that it is a LOM where GPIOs
13621 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013622 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070013623 tg3_get_eeprom_hw_cfg(tp);
13624
Matt Carlson0d3031d2007-10-10 18:02:43 -070013625 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
13626 /* Allow reads and writes to the
13627 * APE register and memory space.
13628 */
13629 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc2010-06-05 17:24:30 +000013630 PCISTATE_ALLOW_APE_SHMEM_WR |
13631 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070013632 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
13633 pci_state_reg);
13634 }
13635
Matt Carlson9936bcf2007-10-10 18:03:07 -070013636 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013637 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013638 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013639 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlsonc885e822010-08-02 11:25:57 +000013640 (tp->tg3_flags3 & TG3_FLG3_5717_PLUS))
Matt Carlsond30cdd22007-10-07 23:28:35 -070013641 tp->tg3_flags |= TG3_FLAG_CPMU_PRESENT;
13642
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000013643 /* Set up tp->grc_local_ctrl before calling tg_power_up().
Michael Chan314fba32005-04-21 17:07:04 -070013644 * GPIO1 driven high will bring 5700's external PHY out of reset.
13645 * It is also used as eeprom write protect on LOMs.
13646 */
13647 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
13648 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
13649 (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT))
13650 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
13651 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070013652 /* Unused GPIO3 must be driven as output on 5752 because there
13653 * are no pull-up resistors on unused GPIO pins.
13654 */
13655 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
13656 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070013657
Matt Carlson321d32a2008-11-21 17:22:19 -080013658 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsoncb4ed1f2010-01-20 16:58:09 +000013659 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
13660 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Michael Chanaf36e6b2006-03-23 01:28:06 -080013661 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
13662
Matt Carlson8d519ab2009-04-20 06:58:01 +000013663 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
13664 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070013665 /* Turn off the debug UART. */
13666 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
13667 if (tp->tg3_flags2 & TG3_FLG2_IS_NIC)
13668 /* Keep VMain power. */
13669 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
13670 GRC_LCLCTRL_GPIO_OUTPUT0;
13671 }
13672
Linus Torvalds1da177e2005-04-16 15:20:36 -070013673 /* Force the chip into D0. */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000013674 err = tg3_power_up(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013675 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000013676 dev_err(&tp->pdev->dev, "Transition to D0 failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070013677 return err;
13678 }
13679
Linus Torvalds1da177e2005-04-16 15:20:36 -070013680 /* Derive initial jumbo mode from MTU assigned in
13681 * ether_setup() via the alloc_etherdev() call
13682 */
Michael Chan0f893dc2005-07-25 12:30:38 -070013683 if (tp->dev->mtu > ETH_DATA_LEN &&
Michael Chana4e2b342005-10-26 15:46:52 -070013684 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Michael Chan0f893dc2005-07-25 12:30:38 -070013685 tp->tg3_flags |= TG3_FLAG_JUMBO_RING_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013686
13687 /* Determine WakeOnLan speed to use. */
13688 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13689 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
13690 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
13691 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
13692 tp->tg3_flags &= ~(TG3_FLAG_WOL_SPEED_100MB);
13693 } else {
13694 tp->tg3_flags |= TG3_FLAG_WOL_SPEED_100MB;
13695 }
13696
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013697 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013698 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013699
Linus Torvalds1da177e2005-04-16 15:20:36 -070013700 /* A few boards don't want Ethernet@WireSpeed phy feature */
13701 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
13702 ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
13703 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070013704 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013705 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
13706 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
13707 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013708
13709 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
13710 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013711 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013712 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013713 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013714
Matt Carlson321d32a2008-11-21 17:22:19 -080013715 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013716 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080013717 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013718 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
Matt Carlsonc885e822010-08-02 11:25:57 +000013719 !(tp->tg3_flags3 & TG3_FLG3_5717_PLUS)) {
Michael Chanc424cb22006-04-29 18:56:34 -070013720 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013721 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013722 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
13723 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080013724 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
13725 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013726 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080013727 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013728 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080013729 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013730 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070013731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013732
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013733 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
13734 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
13735 tp->phy_otp = tg3_read_otp_phycfg(tp);
13736 if (tp->phy_otp == 0)
13737 tp->phy_otp = TG3_OTP_DEFAULT;
13738 }
13739
Matt Carlsonf51f3562008-05-25 23:45:08 -070013740 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT)
Matt Carlson8ef21422008-05-02 16:47:53 -070013741 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
13742 else
13743 tp->mi_mode = MAC_MI_MODE_BASE;
13744
Linus Torvalds1da177e2005-04-16 15:20:36 -070013745 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013746 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
13747 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
13748 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
13749
Matt Carlson321d32a2008-11-21 17:22:19 -080013750 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
13751 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson57e69832008-05-25 23:48:31 -070013752 tp->tg3_flags3 |= TG3_FLG3_USE_PHYLIB;
13753
Matt Carlson158d7ab2008-05-29 01:37:54 -070013754 err = tg3_mdio_init(tp);
13755 if (err)
13756 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013757
13758 /* Initialize data/descriptor byte/word swapping. */
13759 val = tr32(GRC_MODE);
13760 val &= GRC_MODE_HOST_STACKUP;
13761 tw32(GRC_MODE, val | tp->grc_mode);
13762
13763 tg3_switch_clocks(tp);
13764
13765 /* Clear this out for sanity. */
13766 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
13767
13768 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
13769 &pci_state_reg);
13770 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
13771 (tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG) == 0) {
13772 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
13773
13774 if (chiprevid == CHIPREV_ID_5701_A0 ||
13775 chiprevid == CHIPREV_ID_5701_B0 ||
13776 chiprevid == CHIPREV_ID_5701_B2 ||
13777 chiprevid == CHIPREV_ID_5701_B5) {
13778 void __iomem *sram_base;
13779
13780 /* Write some dummy words into the SRAM status block
13781 * area, see if it reads back correctly. If the return
13782 * value is bad, force enable the PCIX workaround.
13783 */
13784 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
13785
13786 writel(0x00000000, sram_base);
13787 writel(0x00000000, sram_base + 4);
13788 writel(0xffffffff, sram_base + 4);
13789 if (readl(sram_base) != 0x00000000)
13790 tp->tg3_flags |= TG3_FLAG_PCIX_TARGET_HWBUG;
13791 }
13792 }
13793
13794 udelay(50);
13795 tg3_nvram_init(tp);
13796
13797 grc_misc_cfg = tr32(GRC_MISC_CFG);
13798 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
13799
Linus Torvalds1da177e2005-04-16 15:20:36 -070013800 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
13801 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
13802 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
13803 tp->tg3_flags2 |= TG3_FLG2_IS_5788;
13804
David S. Millerfac9b832005-05-18 22:46:34 -070013805 if (!(tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
13806 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700))
13807 tp->tg3_flags |= TG3_FLAG_TAGGED_STATUS;
13808 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) {
13809 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
13810 HOSTCC_MODE_CLRTICK_TXBD);
13811
13812 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
13813 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13814 tp->misc_host_ctrl);
13815 }
13816
Matt Carlson3bda1252008-08-15 14:08:22 -070013817 /* Preserve the APE MAC_MODE bits */
13818 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
Matt Carlsond2394e6b2010-11-24 08:31:47 +000013819 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070013820 else
13821 tp->mac_mode = TG3_DEF_MAC_MODE;
13822
Linus Torvalds1da177e2005-04-16 15:20:36 -070013823 /* these are limited to 10/100 only */
13824 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
13825 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
13826 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
13827 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
13828 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
13829 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
13830 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
13831 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
13832 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
Michael Chan676917d2006-12-07 00:20:22 -080013833 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
13834 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013835 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
Matt Carlsond1101142010-02-17 15:16:55 +000013836 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
13837 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013838 (tp->phy_flags & TG3_PHYFLG_IS_FET))
13839 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013840
13841 err = tg3_phy_probe(tp);
13842 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000013843 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013844 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013845 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013846 }
13847
Matt Carlson184b8902010-04-05 10:19:25 +000013848 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080013849 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013850
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013851 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
13852 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013853 } else {
13854 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013855 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013856 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013857 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013858 }
13859
13860 /* 5700 {AX,BX} chips have a broken status block link
13861 * change bit implementation, so we must use the
13862 * status register in those cases.
13863 */
13864 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
13865 tp->tg3_flags |= TG3_FLAG_USE_LINKCHG_REG;
13866 else
13867 tp->tg3_flags &= ~TG3_FLAG_USE_LINKCHG_REG;
13868
13869 /* The led_ctrl is set during tg3_phy_probe, here we might
13870 * have to force the link status polling mechanism based
13871 * upon subsystem IDs.
13872 */
13873 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070013874 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013875 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
13876 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
13877 tp->tg3_flags |= TG3_FLAG_USE_LINKCHG_REG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013878 }
13879
13880 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013881 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013882 tp->tg3_flags |= TG3_FLAG_POLL_SERDES;
13883 else
13884 tp->tg3_flags &= ~TG3_FLAG_POLL_SERDES;
13885
Matt Carlsonbf933c82011-01-25 15:58:49 +000013886 tp->rx_offset = NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000013887 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013888 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsond2757fc2010-04-12 06:58:27 +000013889 (tp->tg3_flags & TG3_FLAG_PCIX_MODE) != 0) {
Matt Carlsonbf933c82011-01-25 15:58:49 +000013890 tp->rx_offset = 0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000013891#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000013892 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000013893#endif
13894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013895
Matt Carlson2c49a442010-09-30 10:34:35 +000013896 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
13897 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013898 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
13899
Matt Carlson2c49a442010-09-30 10:34:35 +000013900 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070013901
13902 /* Increment the rx prod index on the rx std ring by at most
13903 * 8 for these chips to workaround hw errata.
13904 */
13905 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13906 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
13907 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
13908 tp->rx_std_max_post = 8;
13909
Matt Carlson8ed5d972007-05-07 00:25:49 -070013910 if (tp->tg3_flags & TG3_FLAG_ASPM_WORKAROUND)
13911 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
13912 PCIE_PWR_MGMT_L1_THRESH_MSK;
13913
Linus Torvalds1da177e2005-04-16 15:20:36 -070013914 return err;
13915}
13916
David S. Miller49b6e95f2007-03-29 01:38:42 -070013917#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070013918static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
13919{
13920 struct net_device *dev = tp->dev;
13921 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070013922 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070013923 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070013924 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013925
David S. Miller49b6e95f2007-03-29 01:38:42 -070013926 addr = of_get_property(dp, "local-mac-address", &len);
13927 if (addr && len == 6) {
13928 memcpy(dev->dev_addr, addr, 6);
13929 memcpy(dev->perm_addr, dev->dev_addr, 6);
13930 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013931 }
13932 return -ENODEV;
13933}
13934
13935static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
13936{
13937 struct net_device *dev = tp->dev;
13938
13939 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070013940 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013941 return 0;
13942}
13943#endif
13944
13945static int __devinit tg3_get_device_address(struct tg3 *tp)
13946{
13947 struct net_device *dev = tp->dev;
13948 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080013949 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013950
David S. Miller49b6e95f2007-03-29 01:38:42 -070013951#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070013952 if (!tg3_get_macaddr_sparc(tp))
13953 return 0;
13954#endif
13955
13956 mac_offset = 0x7c;
David S. Millerf49639e2006-06-09 11:58:36 -070013957 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) ||
Michael Chana4e2b342005-10-26 15:46:52 -070013958 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013959 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
13960 mac_offset = 0xcc;
13961 if (tg3_nvram_lock(tp))
13962 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
13963 else
13964 tg3_nvram_unlock(tp);
Matt Carlsona50d0792010-06-05 17:24:37 +000013965 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13966 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) {
13967 if (PCI_FUNC(tp->pdev->devfn) & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000013968 mac_offset = 0xcc;
Matt Carlsona50d0792010-06-05 17:24:37 +000013969 if (PCI_FUNC(tp->pdev->devfn) > 1)
13970 mac_offset += 0x18c;
Matt Carlsona1b950d2009-09-01 13:20:17 +000013971 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070013972 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013973
13974 /* First try to get it from MAC address mailbox. */
13975 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
13976 if ((hi >> 16) == 0x484b) {
13977 dev->dev_addr[0] = (hi >> 8) & 0xff;
13978 dev->dev_addr[1] = (hi >> 0) & 0xff;
13979
13980 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
13981 dev->dev_addr[2] = (lo >> 24) & 0xff;
13982 dev->dev_addr[3] = (lo >> 16) & 0xff;
13983 dev->dev_addr[4] = (lo >> 8) & 0xff;
13984 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013985
Michael Chan008652b2006-03-27 23:14:53 -080013986 /* Some old bootcode may report a 0 MAC address in SRAM */
13987 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
13988 }
13989 if (!addr_ok) {
13990 /* Next, try NVRAM. */
Matt Carlsondf259d82009-04-20 06:57:14 +000013991 if (!(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) &&
13992 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000013993 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070013994 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
13995 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080013996 }
13997 /* Finally just fetch it out of the MAC control regs. */
13998 else {
13999 hi = tr32(MAC_ADDR_0_HIGH);
14000 lo = tr32(MAC_ADDR_0_LOW);
14001
14002 dev->dev_addr[5] = lo & 0xff;
14003 dev->dev_addr[4] = (lo >> 8) & 0xff;
14004 dev->dev_addr[3] = (lo >> 16) & 0xff;
14005 dev->dev_addr[2] = (lo >> 24) & 0xff;
14006 dev->dev_addr[1] = hi & 0xff;
14007 dev->dev_addr[0] = (hi >> 8) & 0xff;
14008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014009 }
14010
14011 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070014012#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014013 if (!tg3_get_default_macaddr_sparc(tp))
14014 return 0;
14015#endif
14016 return -EINVAL;
14017 }
John W. Linville2ff43692005-09-12 14:44:20 -070014018 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014019 return 0;
14020}
14021
David S. Miller59e6b432005-05-18 22:50:10 -070014022#define BOUNDARY_SINGLE_CACHELINE 1
14023#define BOUNDARY_MULTI_CACHELINE 2
14024
14025static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
14026{
14027 int cacheline_size;
14028 u8 byte;
14029 int goal;
14030
14031 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
14032 if (byte == 0)
14033 cacheline_size = 1024;
14034 else
14035 cacheline_size = (int) byte * 4;
14036
14037 /* On 5703 and later chips, the boundary bits have no
14038 * effect.
14039 */
14040 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14041 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
14042 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
14043 goto out;
14044
14045#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
14046 goal = BOUNDARY_MULTI_CACHELINE;
14047#else
14048#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
14049 goal = BOUNDARY_SINGLE_CACHELINE;
14050#else
14051 goal = 0;
14052#endif
14053#endif
14054
Matt Carlsonc885e822010-08-02 11:25:57 +000014055 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014056 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
14057 goto out;
14058 }
14059
David S. Miller59e6b432005-05-18 22:50:10 -070014060 if (!goal)
14061 goto out;
14062
14063 /* PCI controllers on most RISC systems tend to disconnect
14064 * when a device tries to burst across a cache-line boundary.
14065 * Therefore, letting tg3 do so just wastes PCI bandwidth.
14066 *
14067 * Unfortunately, for PCI-E there are only limited
14068 * write-side controls for this, and thus for reads
14069 * we will still get the disconnects. We'll also waste
14070 * these PCI cycles for both read and write for chips
14071 * other than 5700 and 5701 which do not implement the
14072 * boundary bits.
14073 */
14074 if ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) &&
14075 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)) {
14076 switch (cacheline_size) {
14077 case 16:
14078 case 32:
14079 case 64:
14080 case 128:
14081 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14082 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
14083 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
14084 } else {
14085 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14086 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14087 }
14088 break;
14089
14090 case 256:
14091 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
14092 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
14093 break;
14094
14095 default:
14096 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14097 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14098 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014099 }
David S. Miller59e6b432005-05-18 22:50:10 -070014100 } else if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
14101 switch (cacheline_size) {
14102 case 16:
14103 case 32:
14104 case 64:
14105 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14106 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14107 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
14108 break;
14109 }
14110 /* fallthrough */
14111 case 128:
14112 default:
14113 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14114 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
14115 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014116 }
David S. Miller59e6b432005-05-18 22:50:10 -070014117 } else {
14118 switch (cacheline_size) {
14119 case 16:
14120 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14121 val |= (DMA_RWCTRL_READ_BNDRY_16 |
14122 DMA_RWCTRL_WRITE_BNDRY_16);
14123 break;
14124 }
14125 /* fallthrough */
14126 case 32:
14127 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14128 val |= (DMA_RWCTRL_READ_BNDRY_32 |
14129 DMA_RWCTRL_WRITE_BNDRY_32);
14130 break;
14131 }
14132 /* fallthrough */
14133 case 64:
14134 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14135 val |= (DMA_RWCTRL_READ_BNDRY_64 |
14136 DMA_RWCTRL_WRITE_BNDRY_64);
14137 break;
14138 }
14139 /* fallthrough */
14140 case 128:
14141 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14142 val |= (DMA_RWCTRL_READ_BNDRY_128 |
14143 DMA_RWCTRL_WRITE_BNDRY_128);
14144 break;
14145 }
14146 /* fallthrough */
14147 case 256:
14148 val |= (DMA_RWCTRL_READ_BNDRY_256 |
14149 DMA_RWCTRL_WRITE_BNDRY_256);
14150 break;
14151 case 512:
14152 val |= (DMA_RWCTRL_READ_BNDRY_512 |
14153 DMA_RWCTRL_WRITE_BNDRY_512);
14154 break;
14155 case 1024:
14156 default:
14157 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
14158 DMA_RWCTRL_WRITE_BNDRY_1024);
14159 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014160 }
David S. Miller59e6b432005-05-18 22:50:10 -070014161 }
14162
14163out:
14164 return val;
14165}
14166
Linus Torvalds1da177e2005-04-16 15:20:36 -070014167static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
14168{
14169 struct tg3_internal_buffer_desc test_desc;
14170 u32 sram_dma_descs;
14171 int i, ret;
14172
14173 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
14174
14175 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
14176 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
14177 tw32(RDMAC_STATUS, 0);
14178 tw32(WDMAC_STATUS, 0);
14179
14180 tw32(BUFMGR_MODE, 0);
14181 tw32(FTQ_RESET, 0);
14182
14183 test_desc.addr_hi = ((u64) buf_dma) >> 32;
14184 test_desc.addr_lo = buf_dma & 0xffffffff;
14185 test_desc.nic_mbuf = 0x00002100;
14186 test_desc.len = size;
14187
14188 /*
14189 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
14190 * the *second* time the tg3 driver was getting loaded after an
14191 * initial scan.
14192 *
14193 * Broadcom tells me:
14194 * ...the DMA engine is connected to the GRC block and a DMA
14195 * reset may affect the GRC block in some unpredictable way...
14196 * The behavior of resets to individual blocks has not been tested.
14197 *
14198 * Broadcom noted the GRC reset will also reset all sub-components.
14199 */
14200 if (to_device) {
14201 test_desc.cqid_sqid = (13 << 8) | 2;
14202
14203 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
14204 udelay(40);
14205 } else {
14206 test_desc.cqid_sqid = (16 << 8) | 7;
14207
14208 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
14209 udelay(40);
14210 }
14211 test_desc.flags = 0x00000005;
14212
14213 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
14214 u32 val;
14215
14216 val = *(((u32 *)&test_desc) + i);
14217 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
14218 sram_dma_descs + (i * sizeof(u32)));
14219 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
14220 }
14221 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
14222
Matt Carlson859a5882010-04-05 10:19:28 +000014223 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014224 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a5882010-04-05 10:19:28 +000014225 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070014226 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014227
14228 ret = -ENODEV;
14229 for (i = 0; i < 40; i++) {
14230 u32 val;
14231
14232 if (to_device)
14233 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
14234 else
14235 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
14236 if ((val & 0xffff) == sram_dma_descs) {
14237 ret = 0;
14238 break;
14239 }
14240
14241 udelay(100);
14242 }
14243
14244 return ret;
14245}
14246
David S. Millerded73402005-05-23 13:59:47 -070014247#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070014248
Matt Carlson41434702011-03-09 16:58:22 +000014249static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080014250 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
14251 { },
14252};
14253
Linus Torvalds1da177e2005-04-16 15:20:36 -070014254static int __devinit tg3_test_dma(struct tg3 *tp)
14255{
14256 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070014257 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014258 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014259
Matt Carlson4bae65c2010-11-24 08:31:52 +000014260 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
14261 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014262 if (!buf) {
14263 ret = -ENOMEM;
14264 goto out_nofree;
14265 }
14266
14267 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
14268 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
14269
David S. Miller59e6b432005-05-18 22:50:10 -070014270 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014271
Matt Carlsonc885e822010-08-02 11:25:57 +000014272 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014273 goto out;
14274
Linus Torvalds1da177e2005-04-16 15:20:36 -070014275 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
14276 /* DMA read watermark not used on PCIE */
14277 tp->dma_rwctrl |= 0x00180000;
14278 } else if (!(tp->tg3_flags & TG3_FLAG_PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070014279 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
14280 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014281 tp->dma_rwctrl |= 0x003f0000;
14282 else
14283 tp->dma_rwctrl |= 0x003f000f;
14284 } else {
14285 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14286 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
14287 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080014288 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014289
Michael Chan4a29cc22006-03-19 13:21:12 -080014290 /* If the 5704 is behind the EPB bridge, we can
14291 * do the less restrictive ONE_DMA workaround for
14292 * better performance.
14293 */
14294 if ((tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG) &&
14295 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14296 tp->dma_rwctrl |= 0x8000;
14297 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014298 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
14299
Michael Chan49afdeb2007-02-13 12:17:03 -080014300 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
14301 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070014302 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080014303 tp->dma_rwctrl |=
14304 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
14305 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
14306 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070014307 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
14308 /* 5780 always in PCIX mode */
14309 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070014310 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
14311 /* 5714 always in PCIX mode */
14312 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014313 } else {
14314 tp->dma_rwctrl |= 0x001b000f;
14315 }
14316 }
14317
14318 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14319 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14320 tp->dma_rwctrl &= 0xfffffff0;
14321
14322 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14323 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
14324 /* Remove this if it causes problems for some boards. */
14325 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
14326
14327 /* On 5700/5701 chips, we need to set this bit.
14328 * Otherwise the chip will issue cacheline transactions
14329 * to streamable DMA memory with not all the byte
14330 * enables turned on. This is an error on several
14331 * RISC PCI controllers, in particular sparc64.
14332 *
14333 * On 5703/5704 chips, this bit has been reassigned
14334 * a different meaning. In particular, it is used
14335 * on those chips to enable a PCI-X workaround.
14336 */
14337 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
14338 }
14339
14340 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14341
14342#if 0
14343 /* Unneeded, already done by tg3_get_invariants. */
14344 tg3_switch_clocks(tp);
14345#endif
14346
Linus Torvalds1da177e2005-04-16 15:20:36 -070014347 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14348 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
14349 goto out;
14350
David S. Miller59e6b432005-05-18 22:50:10 -070014351 /* It is best to perform DMA test with maximum write burst size
14352 * to expose the 5700/5701 write DMA bug.
14353 */
14354 saved_dma_rwctrl = tp->dma_rwctrl;
14355 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14356 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14357
Linus Torvalds1da177e2005-04-16 15:20:36 -070014358 while (1) {
14359 u32 *p = buf, i;
14360
14361 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
14362 p[i] = i;
14363
14364 /* Send the buffer to the chip. */
14365 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
14366 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000014367 dev_err(&tp->pdev->dev,
14368 "%s: Buffer write failed. err = %d\n",
14369 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014370 break;
14371 }
14372
14373#if 0
14374 /* validate data reached card RAM correctly. */
14375 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14376 u32 val;
14377 tg3_read_mem(tp, 0x2100 + (i*4), &val);
14378 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000014379 dev_err(&tp->pdev->dev,
14380 "%s: Buffer corrupted on device! "
14381 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014382 /* ret = -ENODEV here? */
14383 }
14384 p[i] = 0;
14385 }
14386#endif
14387 /* Now read it back. */
14388 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
14389 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000014390 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
14391 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014392 break;
14393 }
14394
14395 /* Verify it. */
14396 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14397 if (p[i] == i)
14398 continue;
14399
David S. Miller59e6b432005-05-18 22:50:10 -070014400 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14401 DMA_RWCTRL_WRITE_BNDRY_16) {
14402 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014403 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
14404 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14405 break;
14406 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000014407 dev_err(&tp->pdev->dev,
14408 "%s: Buffer corrupted on read back! "
14409 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014410 ret = -ENODEV;
14411 goto out;
14412 }
14413 }
14414
14415 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
14416 /* Success. */
14417 ret = 0;
14418 break;
14419 }
14420 }
David S. Miller59e6b432005-05-18 22:50:10 -070014421 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14422 DMA_RWCTRL_WRITE_BNDRY_16) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014423
David S. Miller59e6b432005-05-18 22:50:10 -070014424 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070014425 * now look for chipsets that are known to expose the
14426 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070014427 */
Matt Carlson41434702011-03-09 16:58:22 +000014428 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014429 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14430 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a5882010-04-05 10:19:28 +000014431 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014432 /* Safe to use the calculated DMA boundary. */
14433 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a5882010-04-05 10:19:28 +000014434 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070014435
David S. Miller59e6b432005-05-18 22:50:10 -070014436 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014438
14439out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000014440 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014441out_nofree:
14442 return ret;
14443}
14444
Linus Torvalds1da177e2005-04-16 15:20:36 -070014445static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
14446{
Matt Carlsonc885e822010-08-02 11:25:57 +000014447 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
Matt Carlson666bc832010-01-20 16:58:03 +000014448 tp->bufmgr_config.mbuf_read_dma_low_water =
14449 DEFAULT_MB_RDMA_LOW_WATER_5705;
14450 tp->bufmgr_config.mbuf_mac_rx_low_water =
14451 DEFAULT_MB_MACRX_LOW_WATER_57765;
14452 tp->bufmgr_config.mbuf_high_water =
14453 DEFAULT_MB_HIGH_WATER_57765;
14454
14455 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14456 DEFAULT_MB_RDMA_LOW_WATER_5705;
14457 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14458 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
14459 tp->bufmgr_config.mbuf_high_water_jumbo =
14460 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
14461 } else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
Michael Chanfdfec172005-07-25 12:31:48 -070014462 tp->bufmgr_config.mbuf_read_dma_low_water =
14463 DEFAULT_MB_RDMA_LOW_WATER_5705;
14464 tp->bufmgr_config.mbuf_mac_rx_low_water =
14465 DEFAULT_MB_MACRX_LOW_WATER_5705;
14466 tp->bufmgr_config.mbuf_high_water =
14467 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070014468 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
14469 tp->bufmgr_config.mbuf_mac_rx_low_water =
14470 DEFAULT_MB_MACRX_LOW_WATER_5906;
14471 tp->bufmgr_config.mbuf_high_water =
14472 DEFAULT_MB_HIGH_WATER_5906;
14473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014474
Michael Chanfdfec172005-07-25 12:31:48 -070014475 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14476 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
14477 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14478 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
14479 tp->bufmgr_config.mbuf_high_water_jumbo =
14480 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
14481 } else {
14482 tp->bufmgr_config.mbuf_read_dma_low_water =
14483 DEFAULT_MB_RDMA_LOW_WATER;
14484 tp->bufmgr_config.mbuf_mac_rx_low_water =
14485 DEFAULT_MB_MACRX_LOW_WATER;
14486 tp->bufmgr_config.mbuf_high_water =
14487 DEFAULT_MB_HIGH_WATER;
14488
14489 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14490 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
14491 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14492 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
14493 tp->bufmgr_config.mbuf_high_water_jumbo =
14494 DEFAULT_MB_HIGH_WATER_JUMBO;
14495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014496
14497 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
14498 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
14499}
14500
14501static char * __devinit tg3_phy_string(struct tg3 *tp)
14502{
Matt Carlson79eb6902010-02-17 15:17:03 +000014503 switch (tp->phy_id & TG3_PHY_ID_MASK) {
14504 case TG3_PHY_ID_BCM5400: return "5400";
14505 case TG3_PHY_ID_BCM5401: return "5401";
14506 case TG3_PHY_ID_BCM5411: return "5411";
14507 case TG3_PHY_ID_BCM5701: return "5701";
14508 case TG3_PHY_ID_BCM5703: return "5703";
14509 case TG3_PHY_ID_BCM5704: return "5704";
14510 case TG3_PHY_ID_BCM5705: return "5705";
14511 case TG3_PHY_ID_BCM5750: return "5750";
14512 case TG3_PHY_ID_BCM5752: return "5752";
14513 case TG3_PHY_ID_BCM5714: return "5714";
14514 case TG3_PHY_ID_BCM5780: return "5780";
14515 case TG3_PHY_ID_BCM5755: return "5755";
14516 case TG3_PHY_ID_BCM5787: return "5787";
14517 case TG3_PHY_ID_BCM5784: return "5784";
14518 case TG3_PHY_ID_BCM5756: return "5722/5756";
14519 case TG3_PHY_ID_BCM5906: return "5906";
14520 case TG3_PHY_ID_BCM5761: return "5761";
14521 case TG3_PHY_ID_BCM5718C: return "5718C";
14522 case TG3_PHY_ID_BCM5718S: return "5718S";
14523 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000014524 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson79eb6902010-02-17 15:17:03 +000014525 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070014526 case 0: return "serdes";
14527 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070014528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014529}
14530
Michael Chanf9804dd2005-09-27 12:13:10 -070014531static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
14532{
14533 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
14534 strcpy(str, "PCI Express");
14535 return str;
14536 } else if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
14537 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
14538
14539 strcpy(str, "PCIX:");
14540
14541 if ((clock_ctrl == 7) ||
14542 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
14543 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
14544 strcat(str, "133MHz");
14545 else if (clock_ctrl == 0)
14546 strcat(str, "33MHz");
14547 else if (clock_ctrl == 2)
14548 strcat(str, "50MHz");
14549 else if (clock_ctrl == 4)
14550 strcat(str, "66MHz");
14551 else if (clock_ctrl == 6)
14552 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070014553 } else {
14554 strcpy(str, "PCI:");
14555 if (tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED)
14556 strcat(str, "66MHz");
14557 else
14558 strcat(str, "33MHz");
14559 }
14560 if (tp->tg3_flags & TG3_FLAG_PCI_32BIT)
14561 strcat(str, ":32-bit");
14562 else
14563 strcat(str, ":64-bit");
14564 return str;
14565}
14566
Michael Chan8c2dc7e2005-12-19 16:26:02 -080014567static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014568{
14569 struct pci_dev *peer;
14570 unsigned int func, devnr = tp->pdev->devfn & ~7;
14571
14572 for (func = 0; func < 8; func++) {
14573 peer = pci_get_slot(tp->pdev->bus, devnr | func);
14574 if (peer && peer != tp->pdev)
14575 break;
14576 pci_dev_put(peer);
14577 }
Michael Chan16fe9d72005-12-13 21:09:54 -080014578 /* 5704 can be configured in single-port mode, set peer to
14579 * tp->pdev in that case.
14580 */
14581 if (!peer) {
14582 peer = tp->pdev;
14583 return peer;
14584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014585
14586 /*
14587 * We don't need to keep the refcount elevated; there's no way
14588 * to remove one half of this device without removing the other
14589 */
14590 pci_dev_put(peer);
14591
14592 return peer;
14593}
14594
David S. Miller15f98502005-05-18 22:49:26 -070014595static void __devinit tg3_init_coal(struct tg3 *tp)
14596{
14597 struct ethtool_coalesce *ec = &tp->coal;
14598
14599 memset(ec, 0, sizeof(*ec));
14600 ec->cmd = ETHTOOL_GCOALESCE;
14601 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
14602 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
14603 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
14604 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
14605 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
14606 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
14607 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
14608 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
14609 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
14610
14611 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
14612 HOSTCC_MODE_CLRTICK_TXBD)) {
14613 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
14614 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
14615 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
14616 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
14617 }
Michael Chand244c892005-07-05 14:42:33 -070014618
14619 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
14620 ec->rx_coalesce_usecs_irq = 0;
14621 ec->tx_coalesce_usecs_irq = 0;
14622 ec->stats_block_coalesce_usecs = 0;
14623 }
David S. Miller15f98502005-05-18 22:49:26 -070014624}
14625
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080014626static const struct net_device_ops tg3_netdev_ops = {
14627 .ndo_open = tg3_open,
14628 .ndo_stop = tg3_close,
Stephen Hemminger00829822008-11-20 20:14:53 -080014629 .ndo_start_xmit = tg3_start_xmit,
Eric Dumazet511d2222010-07-07 20:44:24 +000014630 .ndo_get_stats64 = tg3_get_stats64,
Stephen Hemminger00829822008-11-20 20:14:53 -080014631 .ndo_validate_addr = eth_validate_addr,
14632 .ndo_set_multicast_list = tg3_set_rx_mode,
14633 .ndo_set_mac_address = tg3_set_mac_addr,
14634 .ndo_do_ioctl = tg3_ioctl,
14635 .ndo_tx_timeout = tg3_tx_timeout,
14636 .ndo_change_mtu = tg3_change_mtu,
Stephen Hemminger00829822008-11-20 20:14:53 -080014637#ifdef CONFIG_NET_POLL_CONTROLLER
14638 .ndo_poll_controller = tg3_poll_controller,
14639#endif
14640};
14641
14642static const struct net_device_ops tg3_netdev_ops_dma_bug = {
14643 .ndo_open = tg3_open,
14644 .ndo_stop = tg3_close,
14645 .ndo_start_xmit = tg3_start_xmit_dma_bug,
Eric Dumazet511d2222010-07-07 20:44:24 +000014646 .ndo_get_stats64 = tg3_get_stats64,
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080014647 .ndo_validate_addr = eth_validate_addr,
14648 .ndo_set_multicast_list = tg3_set_rx_mode,
14649 .ndo_set_mac_address = tg3_set_mac_addr,
14650 .ndo_do_ioctl = tg3_ioctl,
14651 .ndo_tx_timeout = tg3_tx_timeout,
14652 .ndo_change_mtu = tg3_change_mtu,
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080014653#ifdef CONFIG_NET_POLL_CONTROLLER
14654 .ndo_poll_controller = tg3_poll_controller,
14655#endif
14656};
14657
Linus Torvalds1da177e2005-04-16 15:20:36 -070014658static int __devinit tg3_init_one(struct pci_dev *pdev,
14659 const struct pci_device_id *ent)
14660{
Linus Torvalds1da177e2005-04-16 15:20:36 -070014661 struct net_device *dev;
14662 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000014663 int i, err, pm_cap;
14664 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070014665 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080014666 u64 dma_mask, persist_dma_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014667
Joe Perches05dbe002010-02-17 19:44:19 +000014668 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014669
14670 err = pci_enable_device(pdev);
14671 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014672 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014673 return err;
14674 }
14675
Linus Torvalds1da177e2005-04-16 15:20:36 -070014676 err = pci_request_regions(pdev, DRV_MODULE_NAME);
14677 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014678 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014679 goto err_out_disable_pdev;
14680 }
14681
14682 pci_set_master(pdev);
14683
14684 /* Find power-management capability. */
14685 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
14686 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000014687 dev_err(&pdev->dev,
14688 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014689 err = -EIO;
14690 goto err_out_free_res;
14691 }
14692
Matt Carlsonfe5f5782009-09-01 13:09:39 +000014693 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014694 if (!dev) {
Matt Carlson2445e462010-04-05 10:19:21 +000014695 dev_err(&pdev->dev, "Etherdev alloc failed, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014696 err = -ENOMEM;
14697 goto err_out_free_res;
14698 }
14699
Linus Torvalds1da177e2005-04-16 15:20:36 -070014700 SET_NETDEV_DEV(dev, &pdev->dev);
14701
Linus Torvalds1da177e2005-04-16 15:20:36 -070014702 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014703
14704 tp = netdev_priv(dev);
14705 tp->pdev = pdev;
14706 tp->dev = dev;
14707 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014708 tp->rx_mode = TG3_DEF_RX_MODE;
14709 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070014710
Linus Torvalds1da177e2005-04-16 15:20:36 -070014711 if (tg3_debug > 0)
14712 tp->msg_enable = tg3_debug;
14713 else
14714 tp->msg_enable = TG3_DEF_MSG_ENABLE;
14715
14716 /* The word/byte swap controls here control register access byte
14717 * swapping. DMA data byte swapping is controlled in the GRC_MODE
14718 * setting below.
14719 */
14720 tp->misc_host_ctrl =
14721 MISC_HOST_CTRL_MASK_PCI_INT |
14722 MISC_HOST_CTRL_WORD_SWAP |
14723 MISC_HOST_CTRL_INDIR_ACCESS |
14724 MISC_HOST_CTRL_PCISTATE_RW;
14725
14726 /* The NONFRM (non-frame) byte/word swap controls take effect
14727 * on descriptor entries, anything which isn't packet data.
14728 *
14729 * The StrongARM chips on the board (one for tx, one for rx)
14730 * are running in big-endian mode.
14731 */
14732 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
14733 GRC_MODE_WSWAP_NONFRM_DATA);
14734#ifdef __BIG_ENDIAN
14735 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
14736#endif
14737 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014738 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000014739 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014740
Matt Carlsond5fe4882008-11-21 17:20:32 -080014741 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010014742 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014743 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014744 err = -ENOMEM;
14745 goto err_out_free_dev;
14746 }
14747
Linus Torvalds1da177e2005-04-16 15:20:36 -070014748 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
14749 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014750
Linus Torvalds1da177e2005-04-16 15:20:36 -070014751 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014752 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014753 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014754
14755 err = tg3_get_invariants(tp);
14756 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014757 dev_err(&pdev->dev,
14758 "Problem fetching invariants of chip, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014759 goto err_out_iounmap;
14760 }
14761
Matt Carlson615774f2009-11-13 13:03:39 +000014762 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) &&
Matt Carlson2e9f7a72010-09-15 08:59:56 +000014763 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
Matt Carlsona50d0792010-06-05 17:24:37 +000014764 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5719)
Stephen Hemminger00829822008-11-20 20:14:53 -080014765 dev->netdev_ops = &tg3_netdev_ops;
14766 else
14767 dev->netdev_ops = &tg3_netdev_ops_dma_bug;
14768
14769
Michael Chan4a29cc22006-03-19 13:21:12 -080014770 /* The EPB bridge inside 5714, 5715, and 5780 and any
14771 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080014772 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
14773 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
14774 * do DMA address check in tg3_start_xmit().
14775 */
Michael Chan4a29cc22006-03-19 13:21:12 -080014776 if (tp->tg3_flags2 & TG3_FLG2_IS_5788)
Yang Hongyang284901a2009-04-06 19:01:15 -070014777 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Michael Chan4a29cc22006-03-19 13:21:12 -080014778 else if (tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070014779 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080014780#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070014781 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080014782#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080014783 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070014784 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080014785
14786 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070014787 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080014788 err = pci_set_dma_mask(pdev, dma_mask);
14789 if (!err) {
14790 dev->features |= NETIF_F_HIGHDMA;
14791 err = pci_set_consistent_dma_mask(pdev,
14792 persist_dma_mask);
14793 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014794 dev_err(&pdev->dev, "Unable to obtain 64 bit "
14795 "DMA for consistent allocations\n");
Michael Chan72f2afb2006-03-06 19:28:35 -080014796 goto err_out_iounmap;
14797 }
14798 }
14799 }
Yang Hongyang284901a2009-04-06 19:01:15 -070014800 if (err || dma_mask == DMA_BIT_MASK(32)) {
14801 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080014802 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014803 dev_err(&pdev->dev,
14804 "No usable DMA configuration, aborting\n");
Michael Chan72f2afb2006-03-06 19:28:35 -080014805 goto err_out_iounmap;
14806 }
14807 }
14808
Michael Chanfdfec172005-07-25 12:31:48 -070014809 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014810
Matt Carlson507399f2009-11-13 13:03:37 +000014811 /* Selectively allow TSO based on operating conditions */
14812 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO) ||
14813 (tp->fw_needed && !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)))
14814 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
14815 else {
14816 tp->tg3_flags2 &= ~(TG3_FLG2_TSO_CAPABLE | TG3_FLG2_TSO_BUG);
14817 tp->fw_needed = NULL;
14818 }
14819
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014820 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
Matt Carlson9e9fd122009-01-19 16:57:45 -080014821 tp->fw_needed = FIRMWARE_TG3;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014822
Michael Chan4e3a7aa2006-03-20 17:47:44 -080014823 /* TSO is on by default on chips that support hardware TSO.
14824 * Firmware TSO on older chips gives lower performance, so it
14825 * is off by default, but can be enabled using ethtool.
14826 */
Matt Carlsone849cdc2009-11-13 13:03:38 +000014827 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO) &&
Eric Dumazet7fe876a2010-07-08 06:14:55 +000014828 (dev->features & NETIF_F_IP_CSUM)) {
Matt Carlsone849cdc2009-11-13 13:03:38 +000014829 dev->features |= NETIF_F_TSO;
Eric Dumazet7fe876a2010-07-08 06:14:55 +000014830 vlan_features_add(dev, NETIF_F_TSO);
14831 }
Matt Carlsone849cdc2009-11-13 13:03:38 +000014832 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_2) ||
14833 (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3)) {
Eric Dumazet7fe876a2010-07-08 06:14:55 +000014834 if (dev->features & NETIF_F_IPV6_CSUM) {
Michael Chanb0026622006-07-03 19:42:14 -070014835 dev->features |= NETIF_F_TSO6;
Eric Dumazet7fe876a2010-07-08 06:14:55 +000014836 vlan_features_add(dev, NETIF_F_TSO6);
14837 }
Matt Carlsone849cdc2009-11-13 13:03:38 +000014838 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
14839 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070014840 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
14841 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014842 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Eric Dumazet7fe876a2010-07-08 06:14:55 +000014843 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070014844 dev->features |= NETIF_F_TSO_ECN;
Eric Dumazet7fe876a2010-07-08 06:14:55 +000014845 vlan_features_add(dev, NETIF_F_TSO_ECN);
14846 }
Michael Chanb0026622006-07-03 19:42:14 -070014847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014848
Linus Torvalds1da177e2005-04-16 15:20:36 -070014849 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
14850 !(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) &&
14851 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
14852 tp->tg3_flags2 |= TG3_FLG2_MAX_RXPEND_64;
14853 tp->rx_pending = 63;
14854 }
14855
Linus Torvalds1da177e2005-04-16 15:20:36 -070014856 err = tg3_get_device_address(tp);
14857 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014858 dev_err(&pdev->dev,
14859 "Could not obtain valid ethernet address, aborting\n");
Matt Carlson026a6c22009-12-03 08:36:24 +000014860 goto err_out_iounmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014861 }
14862
Matt Carlson0d3031d2007-10-10 18:02:43 -070014863 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
Matt Carlson63532392008-11-03 16:49:57 -080014864 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
Al Viro79ea13c2008-01-24 02:06:46 -080014865 if (!tp->aperegs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014866 dev_err(&pdev->dev,
14867 "Cannot map APE registers, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070014868 err = -ENOMEM;
Matt Carlson026a6c22009-12-03 08:36:24 +000014869 goto err_out_iounmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070014870 }
14871
14872 tg3_ape_lock_init(tp);
Matt Carlson7fd76442009-02-25 14:27:20 +000014873
14874 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF)
14875 tg3_read_dash_ver(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070014876 }
14877
Matt Carlsonc88864d2007-11-12 21:07:01 -080014878 /*
14879 * Reset chip in case UNDI or EFI driver did not shutdown
14880 * DMA self test will enable WDMAC and we'll see (spurious)
14881 * pending DMA on the PCI bus at that point.
14882 */
14883 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
14884 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
14885 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
14886 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
14887 }
14888
14889 err = tg3_test_dma(tp);
14890 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014891 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080014892 goto err_out_apeunmap;
14893 }
14894
Matt Carlson78f90dc2009-11-13 13:03:42 +000014895 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
14896 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
14897 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000014898 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000014899 struct tg3_napi *tnapi = &tp->napi[i];
14900
14901 tnapi->tp = tp;
14902 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
14903
14904 tnapi->int_mbox = intmbx;
14905 if (i < 4)
14906 intmbx += 0x8;
14907 else
14908 intmbx += 0x4;
14909
14910 tnapi->consmbox = rcvmbx;
14911 tnapi->prodmbox = sndmbx;
14912
Matt Carlson66cfd1b2010-09-30 10:34:30 +000014913 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000014914 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000014915 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000014916 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000014917
14918 if (!(tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX))
14919 break;
14920
14921 /*
14922 * If we support MSIX, we'll be using RSS. If we're using
14923 * RSS, the first vector only handles link interrupts and the
14924 * remaining vectors handle rx and tx interrupts. Reuse the
14925 * mailbox values for the next iteration. The values we setup
14926 * above are still useful for the single vectored mode.
14927 */
14928 if (!i)
14929 continue;
14930
14931 rcvmbx += 0x8;
14932
14933 if (sndmbx & 0x4)
14934 sndmbx -= 0x4;
14935 else
14936 sndmbx += 0xc;
14937 }
14938
Matt Carlsonc88864d2007-11-12 21:07:01 -080014939 tg3_init_coal(tp);
14940
Michael Chanc49a1562006-12-17 17:07:29 -080014941 pci_set_drvdata(pdev, dev);
14942
Linus Torvalds1da177e2005-04-16 15:20:36 -070014943 err = register_netdev(dev);
14944 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014945 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070014946 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014947 }
14948
Joe Perches05dbe002010-02-17 19:44:19 +000014949 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
14950 tp->board_part_number,
14951 tp->pci_chip_rev_id,
14952 tg3_bus_string(tp, str),
14953 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014954
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014955 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000014956 struct phy_device *phydev;
14957 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000014958 netdev_info(dev,
14959 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000014960 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014961 } else {
14962 char *ethtype;
14963
14964 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
14965 ethtype = "10/100Base-TX";
14966 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
14967 ethtype = "1000Base-SX";
14968 else
14969 ethtype = "10/100/1000Base-T";
14970
Matt Carlson5129c3a2010-04-05 10:19:23 +000014971 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014972 "(WireSpeed[%d])\n", tg3_phy_string(tp), ethtype,
14973 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0);
14974 }
Matt Carlsondf59c942008-11-03 16:52:56 -080014975
Joe Perches05dbe002010-02-17 19:44:19 +000014976 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
14977 (tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) != 0,
14978 (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014979 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches05dbe002010-02-17 19:44:19 +000014980 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0,
14981 (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) != 0);
14982 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
14983 tp->dma_rwctrl,
14984 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
14985 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014986
14987 return 0;
14988
Matt Carlson0d3031d2007-10-10 18:02:43 -070014989err_out_apeunmap:
14990 if (tp->aperegs) {
14991 iounmap(tp->aperegs);
14992 tp->aperegs = NULL;
14993 }
14994
Linus Torvalds1da177e2005-04-16 15:20:36 -070014995err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070014996 if (tp->regs) {
14997 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070014998 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070014999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015000
15001err_out_free_dev:
15002 free_netdev(dev);
15003
15004err_out_free_res:
15005 pci_release_regions(pdev);
15006
15007err_out_disable_pdev:
15008 pci_disable_device(pdev);
15009 pci_set_drvdata(pdev, NULL);
15010 return err;
15011}
15012
15013static void __devexit tg3_remove_one(struct pci_dev *pdev)
15014{
15015 struct net_device *dev = pci_get_drvdata(pdev);
15016
15017 if (dev) {
15018 struct tg3 *tp = netdev_priv(dev);
15019
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080015020 if (tp->fw)
15021 release_firmware(tp->fw);
15022
Tejun Heo23f333a2010-12-12 16:45:14 +010015023 cancel_work_sync(&tp->reset_task);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015024
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015025 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
15026 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015027 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015028 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070015029
Linus Torvalds1da177e2005-04-16 15:20:36 -070015030 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015031 if (tp->aperegs) {
15032 iounmap(tp->aperegs);
15033 tp->aperegs = NULL;
15034 }
Michael Chan68929142005-08-09 20:17:14 -070015035 if (tp->regs) {
15036 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015037 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015038 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015039 free_netdev(dev);
15040 pci_release_regions(pdev);
15041 pci_disable_device(pdev);
15042 pci_set_drvdata(pdev, NULL);
15043 }
15044}
15045
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015046#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015047static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015048{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015049 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015050 struct net_device *dev = pci_get_drvdata(pdev);
15051 struct tg3 *tp = netdev_priv(dev);
15052 int err;
15053
15054 if (!netif_running(dev))
15055 return 0;
15056
Tejun Heo23f333a2010-12-12 16:45:14 +010015057 flush_work_sync(&tp->reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015058 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015059 tg3_netif_stop(tp);
15060
15061 del_timer_sync(&tp->timer);
15062
David S. Millerf47c11e2005-06-24 20:18:35 -070015063 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015064 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070015065 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015066
15067 netif_device_detach(dev);
15068
David S. Millerf47c11e2005-06-24 20:18:35 -070015069 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070015070 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan6a9eba12005-12-13 21:08:58 -080015071 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
David S. Millerf47c11e2005-06-24 20:18:35 -070015072 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015073
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015074 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015075 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015076 int err2;
15077
David S. Millerf47c11e2005-06-24 20:18:35 -070015078 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015079
Michael Chan6a9eba12005-12-13 21:08:58 -080015080 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015081 err2 = tg3_restart_hw(tp, 1);
15082 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070015083 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015084
15085 tp->timer.expires = jiffies + tp->timer_offset;
15086 add_timer(&tp->timer);
15087
15088 netif_device_attach(dev);
15089 tg3_netif_start(tp);
15090
Michael Chanb9ec6c12006-07-25 16:37:27 -070015091out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015092 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015093
15094 if (!err2)
15095 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015096 }
15097
15098 return err;
15099}
15100
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015101static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015102{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015103 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015104 struct net_device *dev = pci_get_drvdata(pdev);
15105 struct tg3 *tp = netdev_priv(dev);
15106 int err;
15107
15108 if (!netif_running(dev))
15109 return 0;
15110
Linus Torvalds1da177e2005-04-16 15:20:36 -070015111 netif_device_attach(dev);
15112
David S. Millerf47c11e2005-06-24 20:18:35 -070015113 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015114
Michael Chan6a9eba12005-12-13 21:08:58 -080015115 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Michael Chanb9ec6c12006-07-25 16:37:27 -070015116 err = tg3_restart_hw(tp, 1);
15117 if (err)
15118 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015119
15120 tp->timer.expires = jiffies + tp->timer_offset;
15121 add_timer(&tp->timer);
15122
Linus Torvalds1da177e2005-04-16 15:20:36 -070015123 tg3_netif_start(tp);
15124
Michael Chanb9ec6c12006-07-25 16:37:27 -070015125out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015126 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015127
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015128 if (!err)
15129 tg3_phy_start(tp);
15130
Michael Chanb9ec6c12006-07-25 16:37:27 -070015131 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015132}
15133
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015134static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015135#define TG3_PM_OPS (&tg3_pm_ops)
15136
15137#else
15138
15139#define TG3_PM_OPS NULL
15140
15141#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015142
Linus Torvalds1da177e2005-04-16 15:20:36 -070015143static struct pci_driver tg3_driver = {
15144 .name = DRV_MODULE_NAME,
15145 .id_table = tg3_pci_tbl,
15146 .probe = tg3_init_one,
15147 .remove = __devexit_p(tg3_remove_one),
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015148 .driver.pm = TG3_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070015149};
15150
15151static int __init tg3_init(void)
15152{
Jeff Garzik29917622006-08-19 17:48:59 -040015153 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015154}
15155
15156static void __exit tg3_cleanup(void)
15157{
15158 pci_unregister_driver(&tg3_driver);
15159}
15160
15161module_init(tg3_init);
15162module_exit(tg3_cleanup);